From 617eb0e065de12aff25411042ce1ae27ee10289c Mon Sep 17 00:00:00 2001 From: paul moore Date: Sun, 3 Dec 2023 11:59:05 -0800 Subject: [PATCH] Added repeat support. Added short vs long expansion --- src/ca65/macro.c | 27 ++++++++++++++++++++++----- src/ca65/macro.h | 4 ++-- src/ca65/main.c | 11 ++++++++--- src/ca65/toklist.c | 24 +++++++++++++++++++++++- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/ca65/macro.c b/src/ca65/macro.c index f8c9b81e6..9d8499388 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -75,8 +75,8 @@ static int HT_Compare (const void* Key1, const void* Key2); ** than zero if Key1 is greater then Key2. */ -static StrBuf MakeLineFromTokens (TokNode* first); static char* GetTokenString (Token* T); +/* decompile a token back to a string */ /*****************************************************************************/ /* Data */ @@ -135,6 +135,7 @@ struct MacExp { TokNode* ParamExp; /* Node for expanding parameters */ LineInfo* LI; /* Line info for the expansion */ LineInfo* ParamLI; /* Line info for parameter expansion */ + unsigned ExpandStart; /* First pass through expansion ?*/ }; /* Maximum number of nested macro expansions */ @@ -314,6 +315,7 @@ static MacExp* NewMacExp (Macro* M) E->ParamExp = 0; E->LI = 0; E->ParamLI = 0; + E->ExpandStart = 1; /* set up detection of first call */ /* Mark the macro as expanding */ ++M->Expansions; @@ -703,9 +705,24 @@ ExpandParam: TokSet (Mac->Exp); if (ExpandMacros) { if (new_expand_line) { - StrBuf mac_line = MakeLineFromTokens (Mac->Exp); + /* Suppress unneeded lines if short expansion + ** the ExpandStart is used to ensure that + ** the invokation line itself isnt suppress + ** This is becuase we are always working a line behind + ** Lines we want to keep are upvoted so that this downvote + ** will not suppress them + */ + if (LineLast->FragList == 0 && ExpandMacros == 1 && !Mac->ExpandStart) { + LineCur->Output--; + } + Mac->ExpandStart = 0; + StrBuf mac_line = MakeLineFromTokens (Mac->Exp); NewListingLine (&mac_line, 0, 0); InitListingLine (); + if (CurTok.Tok == TOK_SEGMENT) { + /* upvote the lines to keep*/ + LineCur->Output = 2; + } SB_Done (&mac_line); new_expand_line = 0; } @@ -809,6 +826,7 @@ MacEnd: PopInput (); /* No token available */ + new_expand_line = 1; return 0; } @@ -1080,8 +1098,7 @@ void EnableDefineStyleMacros (void) PRECONDITION (DisableDefines > 0); --DisableDefines; } - -static StrBuf MakeLineFromTokens (TokNode* first) +StrBuf MakeLineFromTokens (TokNode* first) { /* This code reconstitutes a Macro line from the 'compiled' tokens*/ unsigned I; @@ -1115,7 +1132,7 @@ static StrBuf MakeLineFromTokens (TokNode* first) SB_AppendStr (&T, ival); } else if ((token_string = GetTokenString (token)) != NULL) { SB_AppendStr (&T, token_string); - } + } SB_Append (&S, &T); if (token->Tok == TOK_SEP) { return S; diff --git a/src/ca65/macro.h b/src/ca65/macro.h index 7f4335706..a1ed455f8 100644 --- a/src/ca65/macro.h +++ b/src/ca65/macro.h @@ -36,7 +36,7 @@ #ifndef MACRO_H #define MACRO_H - +#include "toklist.h" /*****************************************************************************/ /* Forwards */ @@ -105,7 +105,7 @@ void EnableDefineStyleMacros (void); ** DisableDefineStyleMacros. */ - +StrBuf MakeLineFromTokens (TokNode* first); /* End of macro.h */ diff --git a/src/ca65/main.c b/src/ca65/main.c index bbb82736f..a86110269 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -674,9 +674,13 @@ static void OptWarningsAsErrors (const char* Opt attribute ((unused)), static void OptExpandMacros (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) - /* Exapnd macros in listing */ + /* Expand macros in listing + ** one -m means short listing + ** two means full listing + */ { - ExpandMacros = 1; + + ExpandMacros++; } static void DoPCAssign (void) @@ -895,6 +899,7 @@ static void Assemble (void) while (CurTok.Tok != TOK_EOF) { OneLine (); } + } @@ -1080,7 +1085,7 @@ int main (int argc, char* argv []) WarnLevel = atoi (GetArg (&I, 2)); break; case 'x': - ExpandMacros = 1; + ExpandMacros++; break; diff --git a/src/ca65/toklist.c b/src/ca65/toklist.c index 16efd24df..7fd8cfcd3 100644 --- a/src/ca65/toklist.c +++ b/src/ca65/toklist.c @@ -46,7 +46,9 @@ #include "nexttok.h" #include "scanner.h" #include "toklist.h" - +#include "macro.h" +#include "listing.h" +#include "global.h" /*****************************************************************************/ @@ -248,6 +250,26 @@ static int ReplayTokList (void* List) } L->LI = StartLine (&CurTok.Pos, LI_TYPE_ASM, PushCounter); + /* see description in macro.c */ + static int new_expand_line = 1; + if (ExpandMacros) { + if (new_expand_line) { + if (LineLast->FragList == 0 && ExpandMacros==1) { + LineCur->Output--; + } + StrBuf mac_line = MakeLineFromTokens (L->Last); + if (L->Last->T.Tok == TOK_SEGMENT) { + LineCur->Output = 2; + } + NewListingLine (&mac_line, 0, 0); + InitListingLine (); + SB_Done (&mac_line); + new_expand_line = 0; + } + if (L->Last->T.Tok == TOK_SEP) { + new_expand_line = 1; + } + } /* If a check function is defined, call it, so it may look at the token ** just set and changed it as apropriate. */