1 /*****************************************************************************/
5 /* Macros for the ca65 macroassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
39 #include "../common/hashstr.h"
40 #include "../common/xmalloc.h"
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Struct that describes an identifer (macro param, local list) */
59 typedef struct IdDesc_ IdDesc;
61 IdDesc* Next; /* Linked list */
62 char Id [1]; /* Identifier, dynamically allocated */
67 /* Struct that describes a macro definition */
68 typedef struct Macro_ Macro;
70 Macro* Next; /* Next macro with same hash */
71 Macro* List; /* List of all macros */
72 unsigned LocalCount; /* Count of local symbols */
73 IdDesc* Locals; /* List of local symbols */
74 unsigned ParamCount; /* Parameter count of macro */
75 IdDesc* Params; /* Identifiers of macro parameters */
76 unsigned TokCount; /* Number of tokens for this macro */
77 TokNode* TokRoot; /* Root of token list */
78 TokNode* TokLast; /* Pointer to last token in list */
79 unsigned char Style; /* Macro style */
80 char Name [1]; /* Macro name, dynamically allocated */
83 /* Macro hash table */
84 #define HASHTAB_SIZE 117
85 static Macro* MacroTab [HASHTAB_SIZE];
87 /* Global macro data */
88 static Macro* MacroRoot = 0; /* List of all macros */
90 /* Structs that holds data for a macro expansion */
91 typedef struct MacExp_ MacExp;
93 MacExp* Next; /* Pointer to next expansion */
94 Macro* M; /* Which macro do we expand? */
95 unsigned IfSP; /* .IF stack pointer at start of expansion */
96 TokNode* Exp; /* Pointer to current token */
97 TokNode* Final; /* Pointer to final token */
98 unsigned LocalStart; /* Start of counter for local symbol names */
99 unsigned ParamCount; /* Number of actual parameters */
100 TokNode** Params; /* List of actual parameters */
101 TokNode* ParamExp; /* Node for expanding parameters */
104 /* Number of active macro expansions */
105 static unsigned MacExpansions = 0;
107 /* Flag if a macro expansion should get aborted */
108 static int DoMacAbort = 0;
110 /* Counter to create local names for symbols */
111 static unsigned LocalName = 0;
115 /*****************************************************************************/
117 /*****************************************************************************/
121 static IdDesc* NewIdDesc (const char* Id)
122 /* Create a new IdDesc, initialize and return it */
124 /* Allocate memory */
125 unsigned Len = strlen (Id);
126 IdDesc* I = xmalloc (sizeof (IdDesc) + Len);
128 /* Initialize the struct */
130 memcpy (I->Id, Id, Len);
133 /* Return the new struct */
139 static Macro* NewMacro (const char* Name, unsigned HashVal, unsigned char Style)
140 /* Generate a new macro entry, initialize and return it */
142 /* Allocate memory */
143 unsigned Len = strlen (Name);
144 Macro* M = xmalloc (sizeof (Macro) + Len);
146 /* Initialize the macro struct */
154 memcpy (M->Name, Name, Len);
155 M->Name [Len] = '\0';
157 /* Insert the macro into the global macro list */
161 /* Insert the macro into the hash table */
162 M->Next = MacroTab [HashVal];
163 MacroTab [HashVal] = M;
165 /* Return the new macro struct */
171 static MacExp* NewMacExp (Macro* M)
172 /* Create a new expansion structure for the given macro */
176 /* Allocate memory */
177 MacExp* E = xmalloc (sizeof (MacExp));
179 /* Initialize the data */
181 E->IfSP = GetIfStack ();
184 E->LocalStart = LocalName;
185 LocalName += M->LocalCount;
187 E->Params = xmalloc (M->ParamCount * sizeof (TokNode*));
189 for (I = 0; I < M->ParamCount; ++I) {
193 /* One macro expansion more */
196 /* Return the new macro expansion */
202 static void FreeMacExp (MacExp* E)
203 /* Remove and free the current macro expansion */
207 /* One macro expansion less */
210 /* Free the parameter list */
211 for (I = 0; I < E->ParamCount; ++I) {
212 xfree (E->Params [I]);
216 /* Free the final token if we have one */
218 FreeTokNode (E->Final);
221 /* Free the structure itself */
227 static void MacSkipDef (unsigned Style)
228 /* Skip a macro definition */
230 if (Style == MAC_STYLE_CLASSIC) {
231 /* Skip tokens until we reach the final .endmacro */
232 while (Tok != TOK_ENDMACRO && Tok != TOK_EOF) {
235 if (Tok != TOK_EOF) {
238 Error (ERR_ENDMACRO_EXPECTED);
241 /* Skip until end of line */
248 static Macro* MacFind (const char* Name, unsigned HashVal)
249 /* Search for a macro in the hash table */
251 /* Search for the identifier */
252 Macro* M = MacroTab [HashVal];
254 if (strcmp (Name, M->Name) == 0) {
264 void MacDef (unsigned Style)
265 /* Parse a macro definition */
272 /* We expect a macro name here */
273 if (Tok != TOK_IDENT) {
274 Error (ERR_IDENT_EXPECTED);
279 /* Generate the hash value */
280 HashVal = HashStr (SVal) % HASHTAB_SIZE;
282 /* Did we already define that macro? */
283 if (MacFind (SVal, HashVal) != 0) {
284 /* Macro is already defined */
285 Error (ERR_SYM_ALREADY_DEFINED, SVal);
286 /* Skip tokens until we reach the final .endmacro */
291 /* Define the macro */
292 M = NewMacro (SVal, HashVal, Style);
294 /* Switch to raw token mode and skip the macro name */
295 EnterRawTokenMode ();
298 /* If we have a DEFINE style macro, we may have parameters in braces,
299 * otherwise we may have parameters without braces.
301 if (Style == MAC_STYLE_CLASSIC) {
304 if (Tok == TOK_LPAREN) {
312 /* Parse the parameter list */
315 while (Tok == TOK_IDENT) {
317 /* Create a struct holding the identifier */
318 IdDesc* I = NewIdDesc (SVal);
320 /* Insert the struct into the list, checking for duplicate idents */
321 if (M->ParamCount == 0) {
324 IdDesc* List = M->Params;
326 if (strcmp (List->Id, SVal) == 0) {
327 Error (ERR_SYM_ALREADY_DEFINED, SVal);
329 if (List->Next == 0) {
342 /* Maybe there are more params... */
343 if (Tok == TOK_COMMA) {
351 /* For class macros, we expect a separator token, for define style macros,
352 * we expect the closing paren.
354 if (Style == MAC_STYLE_CLASSIC) {
356 } else if (HaveParams) {
360 /* Preparse the macro body. We will read the tokens until we reach end of
361 * file, or a .endmacro (or end of line for DEFINE style macros) and store
362 * them into an token list internal to the macro. For classic macros, there
363 * the .LOCAL command is detected and removed at this time.
367 /* Check for end of macro */
368 if (Style == MAC_STYLE_CLASSIC) {
369 /* In classic macros, only .endmacro is allowed */
370 if (Tok == TOK_ENDMACRO) {
374 /* May not have end of file in a macro definition */
375 if (Tok == TOK_EOF) {
376 Error (ERR_ENDMACRO_EXPECTED);
380 /* Accept a newline or end of file for new style macros */
381 if (Tok == TOK_SEP || Tok == TOK_EOF) {
386 /* Check for a .LOCAL declaration */
387 if (Tok == TOK_LOCAL && Style == MAC_STYLE_CLASSIC) {
393 /* Skip .local or comma */
396 /* Need an identifer */
397 if (Tok != TOK_IDENT) {
398 Error (ERR_IDENT_EXPECTED);
403 /* Put the identifier into the locals list and skip it */
404 I = NewIdDesc (SVal);
410 /* Check for end of list */
411 if (Tok != TOK_COMMA) {
417 /* We need end of line after the locals */
422 /* Create a token node for the current token */
425 /* If the token is an ident, check if it is a local parameter */
426 if (Tok == TOK_IDENT) {
428 IdDesc* I = M->Params;
430 if (strcmp (I->Id, SVal) == 0) {
431 /* Local param name, replace it */
432 T->Tok = TOK_MACPARAM;
441 /* Insert the new token in the list */
442 if (M->TokCount == 0) {
444 M->TokRoot = M->TokLast = T;
446 /* We have already tokens */
447 M->TokLast->Next = T;
452 /* Read the next token */
456 /* Skip the .endmacro for a classic macro */
457 if (Style == MAC_STYLE_CLASSIC) {
462 /* Switch out of raw token mode */
463 LeaveRawTokenMode ();
468 static int MacExpand (void* Data)
469 /* If we're currently expanding a macro, set the the scanner token and
470 * attribute to the next value and return true. If we are not expanding
471 * a macro, return false.
474 /* Cast the Data pointer to the actual data structure */
475 MacExp* Mac = (MacExp*) Data;
477 /* Check if we should abort this macro */
483 /* Abort any open .IF statements in this macro expansion */
484 CleanupIfStack (Mac->IfSP);
486 /* Terminate macro expansion */
490 /* We're expanding a macro. Check if we are expanding one of the
495 /* Ok, use token from parameter list */
496 TokSet (Mac->ParamExp);
498 /* Set pointer to next token */
499 Mac->ParamExp = Mac->ParamExp->Next;
506 /* We're not expanding macro parameters. Check if we have tokens left from
511 /* Use next macro token */
514 /* Set pointer to next token */
515 Mac->Exp = Mac->Exp->Next;
517 /* Is it a request for actual parameter count? */
518 if (Tok == TOK_PARAMCOUNT) {
520 IVal = Mac->ParamCount;
524 /* Is it the name of a macro parameter? */
525 if (Tok == TOK_MACPARAM) {
527 /* Start to expand the parameter token list */
528 Mac->ParamExp = Mac->Params [IVal];
530 /* Recursive call to expand the parameter */
531 return MacExpand (Mac);
534 /* If it's an identifier, it may in fact be a local symbol */
535 if (Tok == TOK_IDENT && Mac->M->LocalCount) {
536 /* Search for the local symbol in the list */
538 IdDesc* I = Mac->M->Locals;
540 if (strcmp (SVal, I->Id) == 0) {
541 /* This is in fact a local symbol, change the name */
542 sprintf (SVal, "___%04X__", Mac->LocalStart + Index);
554 /* The token was successfully set */
559 /* No more macro tokens. Do we have a final token? */
562 /* Set the final token and remove it */
564 FreeTokNode (Mac->Final);
567 /* The token was successfully set */
573 /* End of macro expansion */
576 /* Pop the input function */
579 /* No token available */
585 static void StartExpClassic (Macro* M)
586 /* Start expanding the classic macro M */
590 /* Skip the macro name */
593 /* Create a structure holding expansion data */
596 /* Read the actual parameters */
597 while (Tok != TOK_SEP && Tok != TOK_EOF) {
601 /* Check for maximum parameter count */
602 if (E->ParamCount >= M->ParamCount) {
603 Error (ERR_TOO_MANY_PARAMS);
608 /* Read tokens for one parameter, accept empty params */
610 while (Tok != TOK_COMMA && Tok != TOK_SEP) {
614 /* Check for end of file */
615 if (Tok == TOK_EOF) {
620 /* Get the next token in a node */
623 /* Insert it into the list */
625 E->Params [E->ParamCount] = T;
635 /* One parameter more */
638 /* Check for a comma */
639 if (Tok == TOK_COMMA) {
646 /* Insert a new token input function */
647 PushInput (MacExpand, E, ".MACRO");
652 static void StartExpDefine (Macro* M)
653 /* Start expanding a DEFINE style macro */
655 /* Create a structure holding expansion data */
656 MacExp* E = NewMacExp (M);
658 /* A define style macro must be called with as many actual parameters
659 * as there are formal ones. Get the parameter count.
661 unsigned Count = M->ParamCount;
663 /* Skip the current token */
666 /* Read the actual parameters */
671 /* Check if there is really a parameter */
672 if (Tok == TOK_SEP || Tok == TOK_EOF || Tok == TOK_COMMA) {
673 Error (ERR_MACRO_PARAM_EXPECTED);
678 /* Read tokens for one parameter */
684 /* Get the next token in a node */
687 /* Insert it into the list */
689 E->Params [E->ParamCount] = T;
698 } while (Tok != TOK_COMMA && Tok != TOK_SEP && Tok != TOK_EOF);
700 /* One parameter more */
703 /* Check for a comma */
705 if (Tok == TOK_COMMA) {
708 Error (ERR_COMMA_EXPECTED);
713 /* Macro expansion will overwrite the current token. This is a problem
714 * for define style macros since these are called from the scanner level.
715 * To avoid it, remember the current token and re-insert it if macro
718 E->Final = NewTokNode ();
720 /* Insert a new token input function */
721 PushInput (MacExpand, E, ".DEFINE");
726 void MacExpandStart (void)
727 /* Start expanding the macro in SVal */
729 /* Search for the macro */
730 Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
733 /* Call the apropriate subroutine */
735 case MAC_STYLE_CLASSIC: StartExpClassic (M); break;
736 case MAC_STYLE_DEFINE: StartExpDefine (M); break;
737 default: Internal ("Invalid macro style: %d", M->Style);
744 /* Abort the current macro expansion */
746 /* Must have an expansion */
747 CHECK (MacExpansions > 0);
749 /* Set a flag so macro expansion will terminate on the next call */
755 int IsMacro (const char* Name)
756 /* Return true if the given name is the name of a macro */
758 return MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE) != 0;
763 int IsDefine (const char* Name)
764 /* Return true if the given name is the name of a define style macro */
766 Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
767 return (M != 0 && M->Style == MAC_STYLE_DEFINE);
772 int InMacExpansion (void)
773 /* Return true if we're currently expanding a macro */
775 return (MacExpansions > 0);