1 /*****************************************************************************/
5 /* Macros for the ca65 macroassembler */
9 /* (C) 1998-2003 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 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Struct that describes an identifer (macro param, local list) */
63 typedef struct IdDesc_ IdDesc;
65 IdDesc* Next; /* Linked list */
66 char Id [1]; /* Identifier, dynamically allocated */
71 /* Struct that describes a macro definition */
72 typedef struct Macro_ Macro;
74 Macro* Next; /* Next macro with same hash */
75 Macro* List; /* List of all macros */
76 unsigned LocalCount; /* Count of local symbols */
77 IdDesc* Locals; /* List of local symbols */
78 unsigned ParamCount; /* Parameter count of macro */
79 IdDesc* Params; /* Identifiers of macro parameters */
80 unsigned TokCount; /* Number of tokens for this macro */
81 TokNode* TokRoot; /* Root of token list */
82 TokNode* TokLast; /* Pointer to last token in list */
83 unsigned char Style; /* Macro style */
84 char Name [1]; /* Macro name, dynamically allocated */
87 /* Macro hash table */
88 #define HASHTAB_SIZE 117
89 static Macro* MacroTab [HASHTAB_SIZE];
91 /* Global macro data */
92 static Macro* MacroRoot = 0; /* List of all macros */
94 /* Structs that holds data for a macro expansion */
95 typedef struct MacExp_ MacExp;
97 MacExp* Next; /* Pointer to next expansion */
98 Macro* M; /* Which macro do we expand? */
99 unsigned IfSP; /* .IF stack pointer at start of expansion */
100 TokNode* Exp; /* Pointer to current token */
101 TokNode* Final; /* Pointer to final token */
102 unsigned LocalStart; /* Start of counter for local symbol names */
103 unsigned ParamCount; /* Number of actual parameters */
104 TokNode** Params; /* List of actual parameters */
105 TokNode* ParamExp; /* Node for expanding parameters */
108 /* Number of active macro expansions */
109 static unsigned MacExpansions = 0;
111 /* Flag if a macro expansion should get aborted */
112 static int DoMacAbort = 0;
114 /* Counter to create local names for symbols */
115 static unsigned LocalName = 0;
119 /*****************************************************************************/
121 /*****************************************************************************/
125 static IdDesc* NewIdDesc (const char* Id)
126 /* Create a new IdDesc, initialize and return it */
128 /* Allocate memory */
129 unsigned Len = strlen (Id);
130 IdDesc* I = xmalloc (sizeof (IdDesc) + Len);
132 /* Initialize the struct */
134 memcpy (I->Id, Id, Len);
137 /* Return the new struct */
143 static Macro* NewMacro (const char* Name, unsigned HashVal, unsigned char Style)
144 /* Generate a new macro entry, initialize and return it */
146 /* Allocate memory */
147 unsigned Len = strlen (Name);
148 Macro* M = xmalloc (sizeof (Macro) + Len);
150 /* Initialize the macro struct */
158 memcpy (M->Name, Name, Len);
159 M->Name [Len] = '\0';
161 /* Insert the macro into the global macro list */
165 /* Insert the macro into the hash table */
166 M->Next = MacroTab [HashVal];
167 MacroTab [HashVal] = M;
169 /* Return the new macro struct */
175 static MacExp* NewMacExp (Macro* M)
176 /* Create a new expansion structure for the given macro */
180 /* Allocate memory */
181 MacExp* E = xmalloc (sizeof (MacExp));
183 /* Initialize the data */
185 E->IfSP = GetIfStack ();
188 E->LocalStart = LocalName;
189 LocalName += M->LocalCount;
191 E->Params = xmalloc (M->ParamCount * sizeof (TokNode*));
193 for (I = 0; I < M->ParamCount; ++I) {
197 /* One macro expansion more */
200 /* Return the new macro expansion */
206 static void FreeMacExp (MacExp* E)
207 /* Remove and free the current macro expansion */
211 /* One macro expansion less */
214 /* Free the parameter list */
215 for (I = 0; I < E->ParamCount; ++I) {
216 xfree (E->Params [I]);
220 /* Free the final token if we have one */
222 FreeTokNode (E->Final);
225 /* Free the structure itself */
231 static void MacSkipDef (unsigned Style)
232 /* Skip a macro definition */
234 if (Style == MAC_STYLE_CLASSIC) {
235 /* Skip tokens until we reach the final .endmacro */
236 while (Tok != TOK_ENDMACRO && Tok != TOK_EOF) {
239 if (Tok != TOK_EOF) {
242 Error (ERR_ENDMACRO_EXPECTED);
245 /* Skip until end of line */
252 static Macro* MacFind (const char* Name, unsigned HashVal)
253 /* Search for a macro in the hash table */
255 /* Search for the identifier */
256 Macro* M = MacroTab [HashVal];
258 if (strcmp (Name, M->Name) == 0) {
268 void MacDef (unsigned Style)
269 /* Parse a macro definition */
276 /* We expect a macro name here */
277 if (Tok != TOK_IDENT) {
278 Error (ERR_IDENT_EXPECTED);
283 /* Generate the hash value */
284 HashVal = HashStr (SVal) % HASHTAB_SIZE;
286 /* Did we already define that macro? */
287 if (MacFind (SVal, HashVal) != 0) {
288 /* Macro is already defined */
289 Error (ERR_SYM_ALREADY_DEFINED, SVal);
290 /* Skip tokens until we reach the final .endmacro */
295 /* Define the macro */
296 M = NewMacro (SVal, HashVal, Style);
298 /* Switch to raw token mode and skip the macro name */
299 EnterRawTokenMode ();
302 /* If we have a DEFINE style macro, we may have parameters in braces,
303 * otherwise we may have parameters without braces.
305 if (Style == MAC_STYLE_CLASSIC) {
308 if (Tok == TOK_LPAREN) {
316 /* Parse the parameter list */
319 while (Tok == TOK_IDENT) {
321 /* Create a struct holding the identifier */
322 IdDesc* I = NewIdDesc (SVal);
324 /* Insert the struct into the list, checking for duplicate idents */
325 if (M->ParamCount == 0) {
328 IdDesc* List = M->Params;
330 if (strcmp (List->Id, SVal) == 0) {
331 Error (ERR_SYM_ALREADY_DEFINED, SVal);
333 if (List->Next == 0) {
346 /* Maybe there are more params... */
347 if (Tok == TOK_COMMA) {
355 /* For class macros, we expect a separator token, for define style macros,
356 * we expect the closing paren.
358 if (Style == MAC_STYLE_CLASSIC) {
360 } else if (HaveParams) {
364 /* Preparse the macro body. We will read the tokens until we reach end of
365 * file, or a .endmacro (or end of line for DEFINE style macros) and store
366 * them into an token list internal to the macro. For classic macros, there
367 * the .LOCAL command is detected and removed at this time.
371 /* Check for end of macro */
372 if (Style == MAC_STYLE_CLASSIC) {
373 /* In classic macros, only .endmacro is allowed */
374 if (Tok == TOK_ENDMACRO) {
378 /* May not have end of file in a macro definition */
379 if (Tok == TOK_EOF) {
380 Error (ERR_ENDMACRO_EXPECTED);
384 /* Accept a newline or end of file for new style macros */
385 if (TokIsSep (Tok)) {
390 /* Check for a .LOCAL declaration */
391 if (Tok == TOK_LOCAL && Style == MAC_STYLE_CLASSIC) {
397 /* Skip .local or comma */
400 /* Need an identifer */
401 if (Tok != TOK_IDENT) {
402 Error (ERR_IDENT_EXPECTED);
407 /* Put the identifier into the locals list and skip it */
408 I = NewIdDesc (SVal);
414 /* Check for end of list */
415 if (Tok != TOK_COMMA) {
421 /* We need end of line after the locals */
426 /* Create a token node for the current token */
429 /* If the token is an ident, check if it is a local parameter */
430 if (Tok == TOK_IDENT) {
432 IdDesc* I = M->Params;
434 if (strcmp (I->Id, SVal) == 0) {
435 /* Local param name, replace it */
436 T->Tok = TOK_MACPARAM;
445 /* Insert the new token in the list */
446 if (M->TokCount == 0) {
448 M->TokRoot = M->TokLast = T;
450 /* We have already tokens */
451 M->TokLast->Next = T;
456 /* Read the next token */
460 /* Skip the .endmacro for a classic macro */
461 if (Style == MAC_STYLE_CLASSIC) {
466 /* Switch out of raw token mode */
467 LeaveRawTokenMode ();
472 static int MacExpand (void* Data)
473 /* If we're currently expanding a macro, set the the scanner token and
474 * attribute to the next value and return true. If we are not expanding
475 * a macro, return false.
478 /* Cast the Data pointer to the actual data structure */
479 MacExp* Mac = (MacExp*) Data;
481 /* Check if we should abort this macro */
487 /* Abort any open .IF statements in this macro expansion */
488 CleanupIfStack (Mac->IfSP);
490 /* Terminate macro expansion */
494 /* We're expanding a macro. Check if we are expanding one of the
499 /* Ok, use token from parameter list */
500 TokSet (Mac->ParamExp);
502 /* Set pointer to next token */
503 Mac->ParamExp = Mac->ParamExp->Next;
510 /* We're not expanding macro parameters. Check if we have tokens left from
515 /* Use next macro token */
518 /* Set pointer to next token */
519 Mac->Exp = Mac->Exp->Next;
521 /* Is it a request for actual parameter count? */
522 if (Tok == TOK_PARAMCOUNT) {
524 IVal = Mac->ParamCount;
528 /* Is it the name of a macro parameter? */
529 if (Tok == TOK_MACPARAM) {
531 /* Start to expand the parameter token list */
532 Mac->ParamExp = Mac->Params [IVal];
534 /* Recursive call to expand the parameter */
535 return MacExpand (Mac);
538 /* If it's an identifier, it may in fact be a local symbol */
539 if (Tok == TOK_IDENT && Mac->M->LocalCount) {
540 /* Search for the local symbol in the list */
542 IdDesc* I = Mac->M->Locals;
544 if (strcmp (SVal, I->Id) == 0) {
545 /* This is in fact a local symbol, change the name. Be sure
546 * to generate a local label name if the original name was
547 * a local label, and also generate a name that cannot be
548 * generated by a user.
550 unsigned PrefixLen = (I->Id[0] == LocalStart);
551 sprintf (SVal, "%.*sLOCAL-MACRO-SYMBOL-%04X", PrefixLen,
552 I->Id, Mac->LocalStart + Index);
564 /* The token was successfully set */
569 /* No more macro tokens. Do we have a final token? */
572 /* Set the final token and remove it */
574 FreeTokNode (Mac->Final);
577 /* The token was successfully set */
583 /* End of macro expansion */
586 /* Pop the input function */
589 /* No token available */
595 static void StartExpClassic (Macro* M)
596 /* Start expanding the classic macro M */
600 /* Skip the macro name */
603 /* Create a structure holding expansion data */
606 /* Read the actual parameters */
607 while (!TokIsSep (Tok)) {
611 /* Check for maximum parameter count */
612 if (E->ParamCount >= M->ParamCount) {
613 Error (ERR_TOO_MANY_PARAMS);
618 /* Read tokens for one parameter, accept empty params */
620 while (Tok != TOK_COMMA && Tok != TOK_SEP) {
624 /* Check for end of file */
625 if (Tok == TOK_EOF) {
626 Error (ERR_UNEXPECTED_EOF);
630 /* Get the next token in a node */
633 /* Insert it into the list */
635 E->Params [E->ParamCount] = T;
645 /* One parameter more */
648 /* Check for a comma */
649 if (Tok == TOK_COMMA) {
656 /* Insert a new token input function */
657 PushInput (MacExpand, E, ".MACRO");
662 static void StartExpDefine (Macro* M)
663 /* Start expanding a DEFINE style macro */
665 /* Create a structure holding expansion data */
666 MacExp* E = NewMacExp (M);
668 /* A define style macro must be called with as many actual parameters
669 * as there are formal ones. Get the parameter count.
671 unsigned Count = M->ParamCount;
673 /* Skip the current token */
676 /* Read the actual parameters */
681 /* Check if there is really a parameter */
682 if (TokIsSep (Tok) || Tok == TOK_COMMA) {
683 Error (ERR_MACRO_PARAM_EXPECTED);
688 /* Read tokens for one parameter */
694 /* Get the next token in a node */
697 /* Insert it into the list */
699 E->Params [E->ParamCount] = T;
708 } while (Tok != TOK_COMMA && !TokIsSep (Tok));
710 /* One parameter more */
713 /* Check for a comma */
715 if (Tok == TOK_COMMA) {
718 Error (ERR_COMMA_EXPECTED);
723 /* Macro expansion will overwrite the current token. This is a problem
724 * for define style macros since these are called from the scanner level.
725 * To avoid it, remember the current token and re-insert it if macro
728 E->Final = NewTokNode ();
730 /* Insert a new token input function */
731 PushInput (MacExpand, E, ".DEFINE");
736 void MacExpandStart (void)
737 /* Start expanding the macro in SVal */
739 /* Search for the macro */
740 Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
743 /* Call the apropriate subroutine */
745 case MAC_STYLE_CLASSIC: StartExpClassic (M); break;
746 case MAC_STYLE_DEFINE: StartExpDefine (M); break;
747 default: Internal ("Invalid macro style: %d", M->Style);
754 /* Abort the current macro expansion */
756 /* Must have an expansion */
757 CHECK (MacExpansions > 0);
759 /* Set a flag so macro expansion will terminate on the next call */
765 int IsMacro (const char* Name)
766 /* Return true if the given name is the name of a macro */
768 return MacFind (Name, HashStr (Name) % HASHTAB_SIZE) != 0;
773 int IsDefine (const char* Name)
774 /* Return true if the given name is the name of a define style macro */
776 Macro* M = MacFind (Name, HashStr (Name) % HASHTAB_SIZE);
777 return (M != 0 && M->Style == MAC_STYLE_DEFINE);
782 int InMacExpansion (void)
783 /* Return true if we're currently expanding a macro */
785 return (MacExpansions > 0);