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);
295 /* If we have a DEFINE style macro, we may have parameters in braces,
296 * otherwise we may have parameters without braces.
298 if (Style == MAC_STYLE_CLASSIC) {
301 if (Tok == TOK_LPAREN) {
309 /* Parse the parameter list */
312 while (Tok == TOK_IDENT) {
314 /* Create a struct holding the identifier */
315 IdDesc* I = NewIdDesc (SVal);
317 /* Insert the struct into the list, checking for duplicate idents */
318 if (M->ParamCount == 0) {
321 IdDesc* List = M->Params;
323 if (strcmp (List->Id, SVal) == 0) {
324 Error (ERR_SYM_ALREADY_DEFINED, SVal);
326 if (List->Next == 0) {
339 /* Maybe there are more params... */
340 if (Tok == TOK_COMMA) {
348 /* For class macros, we expect a separator token, for define style macros,
349 * we expect the closing paren.
351 if (Style == MAC_STYLE_CLASSIC) {
353 } else if (HaveParams) {
357 /* Preparse the macro body. We will read the tokens until we reach end of
358 * file, or a .endmacro (or end of line for DEFINE style macros) and store
359 * them into an token list internal to the macro. For classic macros, there
360 * the .LOCAL command is detected and removed at this time.
364 /* Check for end of macro */
365 if (Style == MAC_STYLE_CLASSIC) {
366 /* In classic macros, only .endmacro is allowed */
367 if (Tok == TOK_ENDMACRO) {
371 /* May not have end of file in a macro definition */
372 if (Tok == TOK_EOF) {
373 Error (ERR_ENDMACRO_EXPECTED);
377 /* Accept a newline or end of file for new style macros */
378 if (Tok == TOK_SEP || Tok == TOK_EOF) {
383 /* Check for a .LOCAL declaration */
384 if (Tok == TOK_LOCAL && Style == MAC_STYLE_CLASSIC) {
390 /* Skip .local or comma */
393 /* Need an identifer */
394 if (Tok != TOK_IDENT) {
395 Error (ERR_IDENT_EXPECTED);
400 /* Put the identifier into the locals list and skip it */
401 I = NewIdDesc (SVal);
407 /* Check for end of list */
408 if (Tok != TOK_COMMA) {
414 /* We need end of line after the locals */
419 /* Create a token node for the current token */
422 /* If the token is an ident, check if it is a local parameter */
423 if (Tok == TOK_IDENT) {
425 IdDesc* I = M->Params;
427 if (strcmp (I->Id, SVal) == 0) {
428 /* Local param name, replace it */
429 T->Tok = TOK_MACPARAM;
438 /* Insert the new token in the list */
439 if (M->TokCount == 0) {
441 M->TokRoot = M->TokLast = T;
443 /* We have already tokens */
444 M->TokLast->Next = T;
449 /* Read the next token */
453 /* Skip the .endmacro for a classic macro */
454 if (Style == MAC_STYLE_CLASSIC) {
461 static int MacExpand (void* Data)
462 /* If we're currently expanding a macro, set the the scanner token and
463 * attribute to the next value and return true. If we are not expanding
464 * a macro, return false.
467 /* Cast the Data pointer to the actual data structure */
468 MacExp* Mac = (MacExp*) Data;
470 /* Check if we should abort this macro */
476 /* Abort any open .IF statements in this macro expansion */
477 CleanupIfStack (Mac->IfSP);
479 /* Terminate macro expansion */
483 /* We're expanding a macro. Check if we are expanding one of the
488 /* Ok, use token from parameter list */
489 TokSet (Mac->ParamExp);
491 /* Set pointer to next token */
492 Mac->ParamExp = Mac->ParamExp->Next;
499 /* We're not expanding macro parameters. Check if we have tokens left from
504 /* Use next macro token */
507 /* Set pointer to next token */
508 Mac->Exp = Mac->Exp->Next;
510 /* Is it a request for actual parameter count? */
511 if (Tok == TOK_PARAMCOUNT) {
513 IVal = Mac->ParamCount;
517 /* Is it the name of a macro parameter? */
518 if (Tok == TOK_MACPARAM) {
520 /* Start to expand the parameter token list */
521 Mac->ParamExp = Mac->Params [IVal];
523 /* Recursive call to expand the parameter */
524 return MacExpand (Mac);
527 /* If it's an identifier, it may in fact be a local symbol */
528 if (Tok == TOK_IDENT && Mac->M->LocalCount) {
529 /* Search for the local symbol in the list */
531 IdDesc* I = Mac->M->Locals;
533 if (strcmp (SVal, I->Id) == 0) {
534 /* This is in fact a local symbol, change the name */
535 sprintf (SVal, "___%04X__", Mac->LocalStart + Index);
547 /* The token was successfully set */
552 /* No more macro tokens. Do we have a final token? */
555 /* Set the final token and remove it */
557 FreeTokNode (Mac->Final);
560 /* The token was successfully set */
566 /* End of macro expansion */
569 /* Pop the input function */
572 /* No token available */
578 static void StartExpClassic (Macro* M)
579 /* Start expanding the classic macro M */
583 /* Skip the macro name */
586 /* Create a structure holding expansion data */
589 /* Read the actual parameters */
590 while (Tok != TOK_SEP && Tok != TOK_EOF) {
594 /* Check for maximum parameter count */
595 if (E->ParamCount >= M->ParamCount) {
596 Error (ERR_TOO_MANY_PARAMS);
601 /* Read tokens for one parameter, accept empty params */
603 while (Tok != TOK_COMMA && Tok != TOK_SEP) {
607 /* Check for end of file */
608 if (Tok == TOK_EOF) {
613 /* Get the next token in a node */
616 /* Insert it into the list */
618 E->Params [E->ParamCount] = T;
628 /* One parameter more */
631 /* Check for a comma */
632 if (Tok == TOK_COMMA) {
639 /* Insert a new token input function */
640 PushInput (MacExpand, E, ".MACRO");
645 static void StartExpDefine (Macro* M)
646 /* Start expanding a DEFINE style macro */
648 /* Create a structure holding expansion data */
649 MacExp* E = NewMacExp (M);
651 /* A define style macro must be called with as many actual parameters
652 * as there are formal ones. Get the parameter count.
654 unsigned Count = M->ParamCount;
656 /* Skip the current token */
659 /* Read the actual parameters */
664 /* Check if there is really a parameter */
665 if (Tok == TOK_SEP || Tok == TOK_EOF || Tok == TOK_COMMA) {
666 Error (ERR_MACRO_PARAM_EXPECTED);
671 /* Read tokens for one parameter */
677 /* Get the next token in a node */
680 /* Insert it into the list */
682 E->Params [E->ParamCount] = T;
691 } while (Tok != TOK_COMMA && Tok != TOK_SEP && Tok != TOK_EOF);
693 /* One parameter more */
696 /* Check for a comma */
698 if (Tok == TOK_COMMA) {
701 Error (ERR_COMMA_EXPECTED);
706 /* Macro expansion will overwrite the current token. This is a problem
707 * for define style macros since these are called from the scanner level.
708 * To avoid it, remember the current token and re-insert it if macro
711 E->Final = NewTokNode ();
713 /* Insert a new token input function */
714 PushInput (MacExpand, E, ".DEFINE");
719 void MacExpandStart (void)
720 /* Start expanding the macro in SVal */
722 /* Search for the macro */
723 Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
726 /* Call the apropriate subroutine */
728 case MAC_STYLE_CLASSIC: StartExpClassic (M); break;
729 case MAC_STYLE_DEFINE: StartExpDefine (M); break;
730 default: Internal ("Invalid macro style: %d", M->Style);
737 /* Abort the current macro expansion */
739 /* Must have an expansion */
740 CHECK (MacExpansions > 0);
742 /* Set a flag so macro expansion will terminate on the next call */
748 int IsMacro (const char* Name)
749 /* Return true if the given name is the name of a macro */
751 return MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE) != 0;
756 int IsDefine (const char* Name)
757 /* Return true if the given name is the name of a define style macro */
759 Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
760 return (M != 0 && M->Style == MAC_STYLE_DEFINE);
765 int InMacExpansion (void)
766 /* Return true if we're currently expanding a macro */
768 return (MacExpansions > 0);