From 241afdc738bcd6e0d8ee66d34cce5d8d6fdbece7 Mon Sep 17 00:00:00 2001 From: uz Date: Fri, 8 Jul 2011 09:22:26 +0000 Subject: [PATCH] Don't search twice for a macro. git-svn-id: svn://svn.cc65.org/cc65/trunk@5075 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/macro.c | 29 ++++++++++++++++------------- src/ca65/macro.h | 20 ++++++++++++++------ src/ca65/main.c | 18 +++++++++--------- src/ca65/scanner.c | 14 ++++++++------ 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/ca65/macro.c b/src/ca65/macro.c index 522db7ac2..204ac2b95 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -95,7 +95,6 @@ struct IdDesc { /* Struct that describes a macro definition */ -typedef struct Macro Macro; struct Macro { HashNode Node; /* Hash list node */ Macro* List; /* List of all macros */ @@ -641,7 +640,7 @@ static int MacExpand (void* Data) /* Check if we should abort this macro */ if (DoMacAbort) { - /* Reset the flag */ + /* Reset the flag */ DoMacAbort = 0; /* Abort any open .IF statements in this macro expansion */ @@ -936,14 +935,13 @@ static void StartExpDefine (MacExp* E) -void MacExpandStart (void) -/* Start expanding the macro in SVal */ +void MacExpandStart (Macro* M) +/* Start expanding a macro */ { MacExp* E; - /* Search for the macro */ - Macro* M = HT_FindEntry (&MacroTab, &CurTok.SVal); - CHECK (M != 0 && (M->Style != MAC_STYLE_DEFINE || DisableDefines == 0)); + /* Check the argument */ + PRECONDITION (M && (M->Style != MAC_STYLE_DEFINE || DisableDefines == 0)); /* We cannot expand an incomplete macro */ if (M->Incomplete) { @@ -984,16 +982,21 @@ void MacAbort (void) -int IsMacro (const StrBuf* Name) -/* Return true if the given name is the name of a macro */ +Macro* FindMacro (const StrBuf* Name) +/* Try to find the macro with the given name and return it. If no macro with + * this name was found, return NULL. + */ { - return (HT_Find (&MacroTab, Name) != 0); + Macro* M = HT_FindEntry (&MacroTab, Name); + return (M != 0 && M->Style == MAC_STYLE_CLASSIC)? M : 0; } -int IsDefine (const StrBuf* Name) -/* Return true if the given name is the name of a define style macro */ +Macro* FindDefine (const StrBuf* Name) +/* Try to find the define style macro with the given name and return it. If no + * such macro was found, return NULL. + */ { Macro* M; @@ -1004,7 +1007,7 @@ int IsDefine (const StrBuf* Name) /* Check if we have such a macro */ M = HT_FindEntry (&MacroTab, Name); - return (M != 0 && M->Style == MAC_STYLE_DEFINE); + return (M != 0 && M->Style == MAC_STYLE_DEFINE)? M : 0; } diff --git a/src/ca65/macro.h b/src/ca65/macro.h index 61ec56761..c2c6b5c8d 100644 --- a/src/ca65/macro.h +++ b/src/ca65/macro.h @@ -58,6 +58,10 @@ struct StrBuf; #define MAC_STYLE_CLASSIC 0 #define MAC_STYLE_DEFINE 1 +/* Macro as an opaque data type */ +struct Macro; +typedef struct Macro Macro; + /*****************************************************************************/ @@ -74,17 +78,21 @@ void MacUndef (const StrBuf* Name, unsigned char Style); * treated as if the macro didn't exist. */ -void MacExpandStart (void); -/* Start expanding the macro in SVal */ +void MacExpandStart (Macro* M); +/* Start expanding a macro */ void MacAbort (void); /* Abort the current macro expansion */ -int IsMacro (const StrBuf* Name); -/* Return true if the given name is the name of a macro */ +Macro* FindMacro (const StrBuf* Name); +/* Try to find the macro with the given name and return it. If no macro with + * this name was found, return NULL. + */ -int IsDefine (const StrBuf* Name); -/* Return true if the given name is the name of a define style macro */ +Macro* FindDefine (const StrBuf* Name); +/* Try to find the define style macro with the given name and return it. If no + * such macro was found, return NULL. + */ int InMacExpansion (void); /* Return true if we're currently expanding a macro */ diff --git a/src/ca65/main.c b/src/ca65/main.c index 800c8397b..c09beed12 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -596,7 +596,7 @@ static void OneLine (void) Segment* Seg = 0; unsigned long PC = 0; SymEntry* Sym = 0; - int Macro = 0; + Macro* Mac = 0; int Instr = -1; /* Initialize the new listing line if we are actually reading from file @@ -606,8 +606,8 @@ static void OneLine (void) InitListingLine (); } + /* Single colon means unnamed label */ if (CurTok.Tok == TOK_COLON) { - /* An unnamed label */ ULabDef (); NextTok (); } @@ -620,11 +620,11 @@ static void OneLine (void) /* Macros and symbols cannot use instruction names */ Instr = FindInstruction (&CurTok.SVal); if (Instr < 0) { - Macro = IsMacro (&CurTok.SVal); + Mac = FindMacro (&CurTok.SVal); } } else { /* Macros and symbols may use the names of instructions */ - Macro = IsMacro (&CurTok.SVal); + Mac = FindMacro (&CurTok.SVal); } } @@ -634,7 +634,7 @@ static void OneLine (void) */ if (CurTok.Tok == TOK_LOCAL_IDENT || CurTok.Tok == TOK_NAMESPACE || - (CurTok.Tok == TOK_IDENT && Instr < 0 && !Macro)) { + (CurTok.Tok == TOK_IDENT && Instr < 0 && Mac == 0)) { /* Did we have whitespace before the ident? */ int HadWS = CurTok.WS; @@ -715,11 +715,11 @@ static void OneLine (void) /* Macros and symbols cannot use instruction names */ Instr = FindInstruction (&CurTok.SVal); if (Instr < 0) { - Macro = IsMacro (&CurTok.SVal); + Mac = FindMacro (&CurTok.SVal); } } else { /* Macros and symbols may use the names of instructions */ - Macro = IsMacro (&CurTok.SVal); + Mac = FindMacro (&CurTok.SVal); } } } @@ -729,9 +729,9 @@ static void OneLine (void) if (CurTok.Tok >= TOK_FIRSTPSEUDO && CurTok.Tok <= TOK_LASTPSEUDO) { /* A control command */ HandlePseudo (); - } else if (Macro) { + } else if (Mac != 0) { /* A macro expansion */ - MacExpandStart (); + MacExpandStart (Mac); } else if (Instr >= 0 || (UbiquitousIdents && ((Instr = FindInstruction (&CurTok.SVal)) >= 0))) { /* A mnemonic - assemble one instruction */ diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 40b99041a..c921562ee 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -818,6 +818,8 @@ static int Sweet16Reg (const StrBuf* Id) void NextRawTok (void) /* Read the next raw token from the input stream */ { + Macro* M; + /* If we've a forced end of assembly, don't read further */ if (ForcedEnd) { CurTok.Tok = TOK_EOF; @@ -827,9 +829,9 @@ void NextRawTok (void) Restart: /* Check if we have tokens from another input source */ if (InputFromStack ()) { - if (CurTok.Tok == TOK_IDENT && IsDefine (&CurTok.SVal)) { + if (CurTok.Tok == TOK_IDENT && (M = FindDefine (&CurTok.SVal)) != 0) { /* This is a define style macro - expand it */ - MacExpandStart (); + MacExpandStart (M); goto Restart; } return; @@ -1001,9 +1003,9 @@ Again: /* An identifier with a dot. Check if it's a define style * macro. */ - if (IsDefine (&CurTok.SVal)) { + if ((M = FindDefine (&CurTok.SVal)) != 0) { /* This is a define style macro - expand it */ - MacExpandStart (); + MacExpandStart (M); goto Restart; } @@ -1108,9 +1110,9 @@ Again: } /* Check for define style macro */ - if (IsDefine (&CurTok.SVal)) { + if ((M = FindDefine (&CurTok.SVal)) != 0) { /* Macro - expand it */ - MacExpandStart (); + MacExpandStart (M); goto Restart; } else { /* An identifier */ -- 2.39.5