diff --git a/src/ca65/global.c b/src/ca65/global.c index 050d19e09..e8599a675 100644 --- a/src/ca65/global.c +++ b/src/ca65/global.c @@ -69,6 +69,7 @@ unsigned char RelaxChecks = 0; /* Relax a few assembler checks */ unsigned char StringEscapes = 0; /* Allow C-style escapes in strings */ unsigned char LongJsrJmpRts = 0; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */ unsigned char WarningsAsErrors = 0; /* Error if any warnings */ +unsigned char ExpandMacros = 0; /* Expand macros in listing */ /* Emulation features */ unsigned char DollarIsPC = 0; /* Allow the $ symbol as current PC */ diff --git a/src/ca65/global.h b/src/ca65/global.h index b3de99df5..5ec4a8d82 100644 --- a/src/ca65/global.h +++ b/src/ca65/global.h @@ -71,6 +71,7 @@ extern unsigned char RelaxChecks; /* Relax a few assembler checks */ extern unsigned char StringEscapes; /* Allow C-style escapes in strings */ extern unsigned char LongJsrJmpRts; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */ extern unsigned char WarningsAsErrors; /* Error if any warnings */ +extern unsigned char ExpandMacros; /* Expand macros in listing */ /* Emulation features */ extern unsigned char DollarIsPC; /* Allow the $ symbol as current PC */ diff --git a/src/ca65/istack.c b/src/ca65/istack.c index 7a95e7e8c..b9c45ac55 100644 --- a/src/ca65/istack.c +++ b/src/ca65/istack.c @@ -93,6 +93,7 @@ void PushInput (int (*Func) (void*), void* Data, const char* Desc) /* Push it */ E->Next = IStack; + ICount++; IStack = E; } @@ -111,7 +112,7 @@ void PopInput (void) /* Pop it */ IStack = IStack->Next; - + ICount--; /* And delete it */ xfree (E); } @@ -156,3 +157,8 @@ void CheckInputStack (void) Error ("Open %s", IStack->Desc); } } + +unsigned GetStackDepth (void) +{ + return ICount; +} diff --git a/src/ca65/istack.h b/src/ca65/istack.h index aa37bab14..90ba513b0 100644 --- a/src/ca65/istack.h +++ b/src/ca65/istack.h @@ -63,7 +63,7 @@ void CheckInputStack (void); ** stuff on the input stack. */ - +unsigned GetStackDepth (void); /* End of istack.h */ diff --git a/src/ca65/macro.c b/src/ca65/macro.c index 904c80756..bd0dd9030 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -53,6 +53,7 @@ #include "pseudo.h" #include "toklist.h" #include "macro.h" +#include "listing.h" @@ -74,7 +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); /*****************************************************************************/ /* Data */ @@ -689,6 +691,8 @@ ExpandParam: Mac->ParamLI = 0; } + /* boolean to indicate that we need to start a new macro expansion line*/ + static int new_expand_line = 1; /* We're not expanding macro parameters. Check if we have tokens left from ** the macro itself. @@ -697,7 +701,16 @@ ExpandParam: /* Use next macro token */ TokSet (Mac->Exp); - + if (ExpandMacros) { + if (new_expand_line) { + StrBuf mac_line = MakeLineFromTokens (Mac->Exp); + NewListingLine (&mac_line, 0, 0); + new_expand_line = 0; + } + if (CurTok.Tok == TOK_SEP) { + new_expand_line = 1; + } + } /* Create new line info for this token */ if (Mac->LI) { EndLine (Mac->LI); @@ -1065,3 +1078,127 @@ void EnableDefineStyleMacros (void) PRECONDITION (DisableDefines > 0); --DisableDefines; } + +static StrBuf MakeLineFromTokens (TokNode* first) +{ + /* This code reconstitutes a Macro line from the 'compiled' tokens*/ + unsigned I; + /* string to be returned */ + StrBuf S = STATIC_STRBUF_INITIALIZER; + + + /* prepend depth indicator */ + for (I = 0; I < GetStackDepth (); I++) { + SB_AppendStr (&S, ">"); + } + SB_AppendStr (&S, " "); + + TokNode* tn = first; + while (tn) { + /* per token string */ + StrBuf T = STATIC_STRBUF_INITIALIZER; + + Token* token = &tn->T; + tn = tn->Next; + char* token_string; + /* leading white space?*/ + if (token->WS) SB_AppendChar (&T, ' '); + /* is it a string of some sort?*/ + unsigned len = SB_GetLen (&token->SVal); + if (len > 0) { + token_string = xmalloc (len + 1); + memcpy (token_string, SB_GetBuf (&token->SVal), len); + token_string[len] = 0; + SB_AppendStr (&T, token_string); + xfree (token_string); + } else if (token->Tok == TOK_INTCON) { + char ival[11]; // max size a long can be + snprintf (ival, 11, "%d", token->IVal); + 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; + } + } + return S; +} + +static char* GetTokenString (Token* T) +{ + switch (T->Tok) { + + case TOK_ASSIGN: return ":="; /* := */ + case TOK_ULABEL: return ":++"; /* :++ or :-- */ + + case TOK_EQ:return "="; /* = */ + case TOK_NE: return "<>"; /* <> */ + case TOK_LT: return "<"; /* < */ + case TOK_GT:return ">"; /* > */ + case TOK_LE: return "<="; /* <= */ + case TOK_GE:return ">="; /* >= */ + + //TOK_BOOLAND, /* .and */ + //TOK_BOOLOR, /* .or */ + ///TOK_BOOLXOR, /* .xor */ + //TOK_BOOLNOT, /* .not */ + + case TOK_PLUS: return "+"; /* + */ + case TOK_MINUS:return "-"; /* - */ + case TOK_MUL: return "*"; /* * */ + case TOK_DIV: return "/"; /* / */ + case TOK_MOD:return "!"; /* ! */ + case TOK_OR: return "|"; /* | */ + case TOK_XOR: return "^"; /* ^ */ + case TOK_AND:return "&"; /* & */ + case TOK_SHL: return "<<"; /* << */ + case TOK_SHR: return ">>"; /* >> */ + case TOK_NOT:return "~"; /* ~ */ + + case TOK_PC: return "$"; /* $ if enabled */ + case TOK_NAMESPACE:return "::"; /* :: */ + case TOK_DOT:return "."; /* . */ + case TOK_COMMA:return ","; /* , */ + case TOK_HASH: return "#"; /* # */ + case TOK_COLON:return ":"; /* : */ + case TOK_LPAREN:return "("; /* ( */ + case TOK_RPAREN:return ")"; /* ) */ + case TOK_LBRACK:return "["; /* [ */ + case TOK_RBRACK:return "]"; /* ] */ + case TOK_LCURLY:return "{"; /* { */ + case TOK_RCURLY:return "}"; /* } */ + case TOK_AT:return "@"; /* @ - in Sweet16 mode */ + + case TOK_OVERRIDE_ZP:return "z:"; /* z: */ + case TOK_OVERRIDE_ABS:return "a:"; /* a: */ + case TOK_OVERRIDE_FAR:return "f:"; /* f: */ + default: return NULL; + } +} +StrBuf xMakeLineFromTokens (TokNode* first) +{ + StrBuf S = STATIC_STRBUF_INITIALIZER; + StrBuf T = STATIC_STRBUF_INITIALIZER; + SB_AppendStr (&S, ">> "); + TokNode* tn = first; + while (tn) { + Token* t = &tn->T; + tn = tn->Next; + char str[100]; + int len = SB_GetLen (&t->SVal); + if (len > 0) { + memcpy (str, SB_GetBuf (&t->SVal), len); + str[len] = 0; + } else str[0] = 0; + SB_Printf (&T, "%s ", str); + SB_Append (&S, &T); + if (t->Tok == TOK_SEP) { + + return S; + } + } + return S; +} diff --git a/src/ca65/main.c b/src/ca65/main.c index 3ec6c84ee..cc5f99786 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -107,6 +107,7 @@ static void Usage (void) " -s\t\t\t\tEnable smart mode\n" " -t sys\t\t\tSet the target system\n" " -v\t\t\t\tIncrease verbosity\n" + " -x\t\t\t\tExpand macros\n" "\n" "Long options:\n" " --auto-import\t\t\tMark unresolved symbols as import\n" @@ -129,7 +130,8 @@ static void Usage (void) " --smart\t\t\tEnable smart mode\n" " --target sys\t\t\tSet the target system\n" " --verbose\t\t\tIncrease verbosity\n" - " --version\t\t\tPrint the assembler version\n", + " --version\t\t\tPrint the assembler version\n" + " --expand_macros\t\tExpand macros in the listing\n", ProgName); } @@ -670,7 +672,12 @@ static void OptWarningsAsErrors (const char* Opt attribute ((unused)), WarningsAsErrors = 1; } - +static void OptExpandMacros (const char* Opt attribute ((unused)), + const char* Arg attribute ((unused))) + /* Exapnd macros in listing */ +{ + ExpandMacros = 1; +} static void DoPCAssign (void) /* Start absolute code */ @@ -695,9 +702,9 @@ static void OneLine (void) int Instr = -1; /* Initialize the new listing line if we are actually reading from file - ** and not from internally pushed input. + ** and not from internally pushed input, unless expanding macros */ - if (!HavePushedInput ()) { + if (!HavePushedInput () || ExpandMacros) { InitListingLine (); } @@ -962,6 +969,7 @@ int main (int argc, char* argv []) { "--verbose", 0, OptVerbose }, { "--version", 0, OptVersion }, { "--warnings-as-errors", 0, OptWarningsAsErrors }, + { "--expand_macros", 0, OptExpandMacros }, }; /* Name of the global name space */ @@ -1069,6 +1077,10 @@ int main (int argc, char* argv []) case 'W': WarnLevel = atoi (GetArg (&I, 2)); break; + case 'x': + ExpandMacros = 1; + break; + default: UnknownOption (Arg); diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 185100025..b7de4f9b5 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -378,6 +378,16 @@ static void IFMarkStart (CharSource* S) static void IFNextChar (CharSource* S) /* Read the next character from the input file */ { + /* if expanding macros the next input line gets pushed too early + ** to the output. So defer the push. + ** Its harmless to do it regardless of expansion or not + */ + static int pending_line = 0; + if (pending_line) { + NewListingLine (&S->V.File.Line, S->V.File.Pos.Name, FCount); + pending_line = 0; + }; + /* Check for end of line, read the next line if needed */ while (SB_GetIndex (&S->V.File.Line) >= SB_GetLen (&S->V.File.Line)) { @@ -443,7 +453,8 @@ static void IFNextChar (CharSource* S) S->V.File.Pos.Line++; /* Remember the new line for the listing */ - NewListingLine (&S->V.File.Line, S->V.File.Pos.Name, FCount); + + pending_line = 1; }