1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenised lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
46 typedef struct SMacro SMacro
;
47 typedef struct MMacro MMacro
;
48 typedef struct Context Context
;
49 typedef struct Token Token
;
50 typedef struct Line Line
;
51 typedef struct Include Include
;
52 typedef struct Cond Cond
;
53 typedef struct IncPath IncPath
;
56 * Store the definition of a single-line macro.
68 * Store the definition of a multi-line macro. This is also used to
69 * store the interiors of `%rep...%endrep' blocks, which are
70 * effectively self-re-invoking multi-line macros which simply
71 * don't have a name or bother to appear in the hash tables. %rep
72 * blocks are signified by having a NULL `name' field.
74 * In a MMacro describing a `%rep' block, the `in_progress' field
75 * isn't merely boolean, but gives the number of repeats left to
78 * The `next' field is used for storing MMacros in hash tables; the
79 * `next_active' field is for stacking them on istk entries.
81 * When a MMacro is being expanded, `params', `iline', `nparam',
82 * `paramlen', `rotate' and `unique' are local to the invocation.
88 int nparam_min
, nparam_max
;
89 int plus
; /* is the last parameter greedy? */
90 int nolist
; /* is this macro listing-inhibited? */
92 Token
*dlist
; /* All defaults as one list */
93 Token
**defaults
; /* Parameter default pointers */
94 int ndefs
; /* number of default parameters */
98 MMacro
*rep_nest
; /* used for nesting %rep */
99 Token
**params
; /* actual parameters */
100 Token
*iline
; /* invocation line */
101 int nparam
, rotate
, *paramlen
;
102 unsigned long unique
;
106 * The context stack is composed of a linked list of these.
112 unsigned long number
;
116 * This is the internal form which we break input lines up into.
117 * Typically stored in linked lists.
119 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
120 * necessarily used as-is, but is intended to denote the number of
121 * the substituted parameter. So in the definition
123 * %define a(x,y) ( (x) & ~(y) )
125 * the token representing `x' will have its type changed to
126 * TOK_SMAC_PARAM, but the one representing `y' will be
129 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
130 * which doesn't need quotes around it. Used in the pre-include
131 * mechanism as an alternative to trying to find a sensible type of
132 * quote to use on the filename we were passed.
137 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
141 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
142 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
147 * Multi-line macro definitions are stored as a linked list of
148 * these, which is essentially a container to allow several linked
151 * Note that in this module, linked lists are treated as stacks
152 * wherever possible. For this reason, Lines are _pushed_ on to the
153 * `expansion' field in MMacro structures, so that the linked list,
154 * if walked, would give the macro lines in reverse order; this
155 * means that we can walk the list when expanding a macro, and thus
156 * push the lines on to the `expansion' field in _istk_ in reverse
157 * order (so that when popped back off they are in the right
158 * order). It may seem cockeyed, and it relies on my design having
159 * an even number of steps in, but it works...
161 * Some of these structures, rather than being actual lines, are
162 * markers delimiting the end of the expansion of a given macro.
163 * This is for use in the cycle-tracking and %rep-handling code.
164 * Such structures have `finishes' non-NULL, and `first' NULL. All
165 * others have `finishes' NULL, but `first' may still be NULL if
175 * To handle an arbitrary level of file inclusion, we maintain a
176 * stack (ie linked list) of these things.
185 MMacro
*mstk
; /* stack of active macros/reps */
189 * Include search path. This is simply a list of strings which get
190 * prepended, in turn, to the name of an include file, in an
191 * attempt to find the file if it's not in the current directory.
199 * Conditional assembly: we maintain a separate stack of these for
200 * each level of file inclusion. (The only reason we keep the
201 * stacks separate is to ensure that a stray `%endif' in a file
202 * included from within the true branch of a `%if' won't terminate
203 * it and cause confusion: instead, rightly, it'll cause an error.)
211 * These states are for use just after %if or %elif: IF_TRUE
212 * means the condition has evaluated to truth so we are
213 * currently emitting, whereas IF_FALSE means we are not
214 * currently emitting but will start doing so if a %else comes
215 * up. In these states, all directives are admissible: %elif,
216 * %else and %endif. (And of course %if.)
218 COND_IF_TRUE
, COND_IF_FALSE
,
220 * These states come up after a %else: ELSE_TRUE means we're
221 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
222 * any %elif or %else will cause an error.
224 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
226 * This state means that we're not emitting now, and also that
227 * nothing until %endif will be emitted at all. It's for use in
228 * two circumstances: (i) when we've had our moment of emission
229 * and have now started seeing %elifs, and (ii) when the
230 * condition construct in question is contained within a
231 * non-emitting branch of a larger condition construct.
235 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
238 * Condition codes. Note that we use c_ prefix not C_ because C_ is
239 * used in nasm.h for the "real" condition codes. At _this_ level,
240 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
241 * ones, so we need a different enum...
243 static char *conditions
[] = {
244 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
245 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
246 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
249 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
250 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
251 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
253 static int inverse_ccs
[] = {
254 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
255 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_E
, c_G
, c_GE
, c_L
, c_LE
, c_O
, c_P
, c_S
,
256 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
262 static char *directives
[] = {
263 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
264 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
265 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
266 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
267 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
268 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
269 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
270 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include", "%line",
271 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate", "%undef"
274 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
275 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFNCTX
, PP_ELIFNDEF
,
276 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNNUM
, PP_ELIFNSTR
,
277 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
278 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
279 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFNCTX
,
280 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNNUM
,
281 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
, PP_LINE
,
282 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
, PP_UNDEF
286 static Context
*cstk
;
287 static Include
*istk
;
288 static IncPath
*ipath
= NULL
;
291 static evalfunc evaluate
;
293 static int pass
; /* HACK: pass 0 = generate dependencies only */
295 static unsigned long unique
; /* unique identifier numbers */
297 static Line
*predef
= NULL
;
299 static ListGen
*list
;
302 * The number of hash values we use for the macro lookup tables.
303 * FIXME: We should *really* be able to configure this at run time,
304 * or even have the hash table automatically expanding when necessary.
309 * The current set of multi-line macros we have defined.
311 static MMacro
*mmacros
[NHASH
];
314 * The current set of single-line macros we have defined.
316 static SMacro
*smacros
[NHASH
];
319 * The multi-line macro we are currently defining, or the %rep
320 * block we are currently reading, if any.
322 static MMacro
*defining
;
325 * The number of macro parameters to allocate space for at a time.
327 #define PARAM_DELTA 16
330 * The standard macro set: defined as `static char *stdmac[]'. Also
331 * gives our position in the macro set, when we're processing it.
334 static char **stdmacpos
;
337 * The extra standard macros that come from the object format, if
340 static char **extrastdmac
= NULL
;
344 * Forward declarations.
346 static Token
*expand_mmac_params (Token
*tline
);
347 static Token
*expand_smacro (Token
*tline
);
348 static void make_tok_num(Token
*tok
, long val
);
351 * Macros for safe checking of token pointers, avoid *(NULL)
353 #define tok_type_(x,t) ((x) && (x)->type == (t))
354 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
355 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
356 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
359 * The pre-preprocessing stage... This function translates line
360 * number indications as they emerge from GNU cpp (`# lineno "file"
361 * flags') into NASM preprocessor line number indications (`%line
364 static char *prepreproc(char *line
)
367 char *fname
, *oldline
;
369 if (line
[0] == '#' && line
[1] == ' ') {
372 lineno
= atoi(fname
);
373 fname
+= strspn(fname
, "0123456789 ");
376 fnlen
= strcspn(fname
, "\"");
377 line
= nasm_malloc(20+fnlen
);
378 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
385 * The hash function for macro lookups. Note that due to some
386 * macros having case-insensitive names, the hash function must be
387 * invariant under case changes. We implement this by applying a
388 * perfectly normal hash function to the uppercase of the string.
390 static int hash(char *s
)
395 * Powers of three, mod 31.
397 static const int multipliers
[] = {
398 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
399 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
404 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
406 if (++i
>= elements(multipliers
))
414 * Free a linked list of tokens.
416 static void free_tlist (Token
*list
)
428 * Free a linked list of lines.
430 static void free_llist (Line
*list
)
436 free_tlist (l
->first
);
444 static void free_mmacro (MMacro
*m
)
447 free_tlist (m
->dlist
);
448 nasm_free (m
->defaults
);
449 free_llist (m
->expansion
);
454 * Pop the context stack.
456 static void ctx_pop (void)
467 free_tlist (s
->expansion
);
474 #define BUF_DELTA 512
476 * Read a line from the top file in istk, handling multiple CR/LFs
477 * at the end of the line read, and handling spurious ^Zs. Will
478 * return lines from the standard macro set if this has not already
481 static char *read_line (void)
483 char *buffer
, *p
, *q
;
488 char *ret
= nasm_strdup(*stdmacpos
++);
489 if (!*stdmacpos
&& any_extrastdmac
)
491 stdmacpos
= extrastdmac
;
492 any_extrastdmac
= FALSE
;
496 * Nasty hack: here we push the contents of `predef' on
497 * to the top-level expansion stack, since this is the
498 * most convenient way to implement the pre-include and
499 * pre-define features.
504 Token
*head
, **tail
, *t
, *tt
;
506 for (pd
= predef
; pd
; pd
= pd
->next
) {
509 for (t
= pd
->first
; t
; t
= t
->next
) {
510 tt
= *tail
= nasm_malloc(sizeof(Token
));
514 tt
->text
= nasm_strdup(t
->text
);
515 tt
->mac
= t
->mac
; /* always NULL here, in fact */
517 l
= nasm_malloc(sizeof(Line
));
518 l
->next
= istk
->expansion
;
532 buffer
= nasm_malloc(BUF_DELTA
);
535 q
= fgets(p
, bufsize
-(p
-buffer
), istk
->fp
);
539 if (p
> buffer
&& p
[-1] == '\n') {
542 if (p
-buffer
> bufsize
-10) {
543 long offset
= p
-buffer
;
544 bufsize
+= BUF_DELTA
;
545 buffer
= nasm_realloc(buffer
, bufsize
);
546 p
= buffer
+offset
; /* prevent stale-pointer problems */
550 if (!q
&& p
== buffer
) {
555 src_set_linnum(src_get_linnum() + istk
->lineinc
);
558 * Play safe: remove CRs as well as LFs, if any of either are
559 * present at the end of the line.
561 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
565 * Handle spurious ^Z, which may be inserted into source files
566 * by some file transfer utilities.
568 buffer
[strcspn(buffer
, "\032")] = '\0';
570 list
->line (LIST_READ
, buffer
);
576 * Tokenise a line of text. This is a very simple process since we
577 * don't need to parse the value out of e.g. numeric tokens: we
578 * simply split one string into many.
580 static Token
*tokenise (char *line
)
585 Token
*t
, **tail
= &list
;
589 if (*p
== '%' && ( isdigit(p
[1]) ||
590 ((p
[1] == '-' || p
[1] == '+') && isdigit(p
[2]))))
595 } while (isdigit(*p
));
596 type
= TOK_PREPROC_ID
;
598 else if (*p
== '%' && p
[1] == '{') {
600 while (*p
&& *p
!= '}') {
606 type
= TOK_PREPROC_ID
;
608 else if (*p
== '%' && (isidchar(p
[1]) ||
609 ((p
[1] == '!' || p
[1] == '%' || p
[1] == '$') &&
615 } while (isidchar(*p
));
616 type
= TOK_PREPROC_ID
;
618 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
621 while (*p
&& isidchar(*p
))
624 else if (*p
== '\'' || *p
== '"') {
631 while (*p
&& *p
!= c
)
635 else if (isnumstart(*p
)) {
641 while (*p
&& isnumchar(*p
))
644 else if (isspace(*p
)) {
645 type
= TOK_WHITESPACE
;
647 while (*p
&& isspace(*p
))
650 * Whitespace just before end-of-line is discarded by
651 * pretending it's a comment; whitespace just before a
652 * comment gets lumped into the comment.
654 if (!*p
|| *p
== ';') {
659 else if (*p
== ';') {
665 * Anything else is an operator of some kind. We check
666 * for all the double-character operators (>>, <<, //,
667 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
668 * else is a single-character operator.
671 if ((p
[0] == '>' && p
[1] == '>') ||
672 (p
[0] == '<' && p
[1] == '<') ||
673 (p
[0] == '/' && p
[1] == '/') ||
674 (p
[0] == '%' && p
[1] == '%') ||
675 (p
[0] == '<' && p
[1] == '=') ||
676 (p
[0] == '>' && p
[1] == '=') ||
677 (p
[0] == '=' && p
[1] == '=') ||
678 (p
[0] == '!' && p
[1] == '=') ||
679 (p
[0] == '<' && p
[1] == '>') ||
680 (p
[0] == '&' && p
[1] == '&') ||
681 (p
[0] == '|' && p
[1] == '|') ||
682 (p
[0] == '^' && p
[1] == '^'))
688 if (type
!= TOK_COMMENT
) {
689 *tail
= t
= nasm_malloc (sizeof(Token
));
693 t
->text
= nasm_malloc(1+p
-line
);
694 strncpy(t
->text
, line
, p
-line
);
695 t
->text
[p
-line
] = '\0';
704 * Convert a line of tokens back into text.
706 char *detoken (Token
*tlist
)
713 for (t
= tlist
; t
; t
= t
->next
) {
714 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
715 char *p
= getenv(t
->text
+2);
718 t
->text
= nasm_strdup(p
);
723 len
+= strlen(t
->text
);
725 p
= line
= nasm_malloc(len
+1);
726 for (t
= tlist
; t
; t
= t
->next
) {
737 * A scanner, suitable for use by the expression evaluator, which
738 * operates on a line of Tokens. Expects a pointer to a pointer to
739 * the first token in the line to be passed in as its private_data
742 static int ppscan(void *private_data
, struct tokenval
*tokval
)
744 Token
**tlineptr
= private_data
;
749 *tlineptr
= tline
? tline
->next
: NULL
;
750 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
751 tline
->type
== TOK_COMMENT
));
754 return tokval
->t_type
= TOKEN_EOS
;
756 if (tline
->text
[0] == '$' && !tline
->text
[1])
757 return tokval
->t_type
= TOKEN_HERE
;
758 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[1])
759 return tokval
->t_type
= TOKEN_BASE
;
761 if (tline
->type
== TOK_ID
) {
762 tokval
->t_charptr
= tline
->text
;
763 if (tline
->text
[0] == '$') {
765 return tokval
->t_type
= TOKEN_ID
;
769 * This is the only special case we actually need to worry
770 * about in this restricted context.
772 if (!nasm_stricmp(tline
->text
, "seg"))
773 return tokval
->t_type
= TOKEN_SEG
;
775 return tokval
->t_type
= TOKEN_ID
;
778 if (tline
->type
== TOK_NUMBER
) {
781 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
783 return tokval
->t_type
= TOKEN_ERRNUM
;
784 tokval
->t_charptr
= NULL
;
785 return tokval
->t_type
= TOKEN_NUM
;
788 if (tline
->type
== TOK_STRING
) {
797 if (l
== 0 || r
[l
-1] != q
)
798 return tokval
->t_type
= TOKEN_ERRNUM
;
799 tokval
->t_integer
= readstrnum(r
, l
-1, &rn_warn
);
801 error(ERR_WARNING
|ERR_PASS1
,
802 "character constant too long");
803 tokval
->t_charptr
= NULL
;
804 return tokval
->t_type
= TOKEN_NUM
;
807 if (tline
->type
== TOK_OTHER
) {
808 if (!strcmp(tline
->text
, "<<")) return tokval
->t_type
= TOKEN_SHL
;
809 if (!strcmp(tline
->text
, ">>")) return tokval
->t_type
= TOKEN_SHR
;
810 if (!strcmp(tline
->text
, "//")) return tokval
->t_type
= TOKEN_SDIV
;
811 if (!strcmp(tline
->text
, "%%")) return tokval
->t_type
= TOKEN_SMOD
;
812 if (!strcmp(tline
->text
, "==")) return tokval
->t_type
= TOKEN_EQ
;
813 if (!strcmp(tline
->text
, "<>")) return tokval
->t_type
= TOKEN_NE
;
814 if (!strcmp(tline
->text
, "!=")) return tokval
->t_type
= TOKEN_NE
;
815 if (!strcmp(tline
->text
, "<=")) return tokval
->t_type
= TOKEN_LE
;
816 if (!strcmp(tline
->text
, ">=")) return tokval
->t_type
= TOKEN_GE
;
817 if (!strcmp(tline
->text
, "&&")) return tokval
->t_type
= TOKEN_DBL_AND
;
818 if (!strcmp(tline
->text
, "^^")) return tokval
->t_type
= TOKEN_DBL_XOR
;
819 if (!strcmp(tline
->text
, "||")) return tokval
->t_type
= TOKEN_DBL_OR
;
823 * We have no other options: just return the first character of
826 return tokval
->t_type
= tline
->text
[0];
830 * Return the Context structure associated with a %$ token. Return
831 * NULL, having _already_ reported an error condition, if the
832 * context stack isn't deep enough for the supplied number of $
835 static Context
*get_ctx (char *name
)
841 error (ERR_NONFATAL
, "`%s': context stack is empty", name
);
847 while (name
[i
+1] == '$') {
851 error (ERR_NONFATAL
, "`%s': context stack is only"
852 " %d level%s deep", name
, i
-1, (i
==2 ? "" : "s"));
860 * Compare a string to the name of an existing macro; this is a
861 * simple wrapper which calls either strcmp or nasm_stricmp
862 * depending on the value of the `casesense' parameter.
864 static int mstrcmp(char *p
, char *q
, int casesense
)
866 return casesense
? strcmp(p
,q
) : nasm_stricmp(p
,q
);
870 * Open an include file. This routine must always return a valid
871 * file pointer if it returns - it's responsible for throwing an
872 * ERR_FATAL and bombing out completely if not. It should also try
873 * the include path one by one until it finds the file or reaches
874 * the end of the path.
876 static FILE *inc_fopen(char *file
)
879 char *prefix
= "", *combine
;
881 static int namelen
= 0;
884 combine
= nasm_strcat(prefix
,file
);
885 fp
= fopen(combine
, "r");
888 namelen
+= strlen(combine
) + 1;
894 printf(" %s", combine
);
906 "unable to open include file `%s'", file
);
907 return NULL
; /* never reached - placate compilers */
911 * Determine if we should warn on defining a single-line macro of
912 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
913 * return TRUE if _any_ single-line macro of that name is defined.
914 * Otherwise, will return TRUE if a single-line macro with either
915 * `nparam' or no parameters is defined.
917 * If a macro with precisely the right number of parameters is
918 * defined, or nparam is -1, the address of the definition structure
919 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
920 * is NULL, no action will be taken regarding its contents, and no
923 * Note that this is also called with nparam zero to resolve
926 static int smacro_defined (char *name
, int nparam
, SMacro
**defn
, int nocase
)
932 if (name
[0] == '%' && name
[1] == '$') {
933 ctx
= get_ctx (name
);
935 return FALSE
; /* got to return _something_ */
940 m
= smacros
[hash(name
)];
945 if (!mstrcmp(m
->name
, p
, m
->casesense
& nocase
) &&
946 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
)) {
948 if (nparam
== m
->nparam
|| nparam
== -1)
961 * Count and mark off the parameters in a multi-line macro call.
962 * This is called both from within the multi-line macro expansion
963 * code, and also to mark off the default parameters when provided
964 * in a %macro definition line.
966 static void count_mmac_params (Token
*t
, int *nparam
, Token
***params
)
968 int paramsize
, brace
;
970 *nparam
= paramsize
= 0;
973 if (*nparam
>= paramsize
) {
974 paramsize
+= PARAM_DELTA
;
975 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
981 (*params
)[(*nparam
)++] = t
;
982 while (tok_isnt_(t
, brace
? "}" : ","))
984 if (t
) { /* got a comma/brace */
988 * Now we've found the closing brace, look further
992 if (tok_isnt_(t
, ",")) {
994 "braces do not enclose all of macro parameter");
995 while (tok_isnt_(t
, ","))
999 t
= t
->next
; /* eat the comma */
1006 * Determine whether one of the various `if' conditions is true or
1009 * We must free the tline we get passed.
1011 static int if_condition (Token
*tline
, int i
)
1014 Token
* t
, * tt
, ** tptr
, * origline
;
1015 struct tokenval tokval
;
1021 case PP_IFCTX
: case PP_ELIFCTX
:
1022 case PP_IFNCTX
: case PP_ELIFNCTX
:
1023 j
= FALSE
; /* have we matched yet? */
1026 "`%s': context stack is empty", directives
[i
]);
1027 else while (tline
) {
1029 if (!tline
|| tline
->type
!= TOK_ID
) {
1031 "`%s' expects context identifiers", directives
[i
]);
1032 free_tlist (origline
);
1035 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1037 tline
= tline
->next
;
1039 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1041 free_tlist (origline
);
1044 case PP_IFDEF
: case PP_ELIFDEF
:
1045 case PP_IFNDEF
: case PP_ELIFNDEF
:
1046 j
= FALSE
; /* have we matched yet? */
1049 if (!tline
|| (tline
->type
!= TOK_ID
&&
1050 (tline
->type
!= TOK_PREPROC_ID
||
1051 tline
->text
[1] != '$'))) {
1053 "`%%if%sdef' expects macro identifiers",
1054 (i
==PP_ELIFNDEF
? "n" : ""));
1055 free_tlist (origline
);
1058 if (smacro_defined(tline
->text
, 0, NULL
, 1))
1060 tline
= tline
->next
;
1062 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1064 free_tlist (origline
);
1067 case PP_IFIDN
: case PP_ELIFIDN
: case PP_IFNIDN
: case PP_ELIFNIDN
:
1068 case PP_IFIDNI
: case PP_ELIFIDNI
: case PP_IFNIDNI
: case PP_ELIFNIDNI
:
1069 tline
= expand_smacro(tline
);
1071 while (tok_isnt_(tt
, ","))
1074 error(ERR_NONFATAL
, "`%s' expects two comma-separated arguments",
1080 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1081 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1082 j
= TRUE
; /* assume equality unless proved not */
1083 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1084 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1085 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1090 if (t
->type
== TOK_WHITESPACE
) {
1093 } else if (tt
->type
== TOK_WHITESPACE
) {
1096 } else if (tt
->type
!= t
->type
||
1097 (casesense
? strcmp(tt
->text
, t
->text
) :
1098 nasm_stricmp(tt
->text
, t
->text
))) {
1099 j
= FALSE
; /* found mismatching tokens */
1107 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1108 j
= FALSE
; /* trailing gunk on one end or other */
1109 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1110 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1115 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1116 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1117 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1118 tline
= expand_smacro(tline
);
1120 while (tok_type_(t
, TOK_WHITESPACE
))
1122 j
= FALSE
; /* placate optimiser */
1124 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1125 j
= (t
->type
== TOK_ID
);
1127 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1128 j
= (t
->type
== TOK_NUMBER
);
1130 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1131 j
= (t
->type
== TOK_STRING
);
1134 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1135 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1136 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1141 case PP_IF
: case PP_ELIF
:
1142 t
= tline
= expand_smacro(tline
);
1144 tokval
.t_type
= TOKEN_INVALID
;
1145 evalresult
= evaluate (ppscan
, tptr
, &tokval
,
1146 NULL
, pass
| 0x10, error
, NULL
);
1152 "trailing garbage after expression ignored");
1153 if (!is_simple(evalresult
)) {
1155 "non-constant value given to `%s'", directives
[i
]);
1158 return reloc_value(evalresult
) != 0;
1162 "preprocessor directive `%s' not yet implemented",
1164 free_tlist (origline
);
1165 return -1; /* yeah, right */
1170 * Find out if a line contains a preprocessor directive, and deal
1173 * If a directive _is_ found, we are expected to free_tlist() the
1176 * Return values go like this:
1178 * bit 0 is set if a directive was found (so the line gets freed)
1180 static int do_directive (Token
*tline
)
1182 int i
, j
, k
, m
, nparam
, nolist
;
1187 SMacro
*smac
, **smhead
;
1189 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1191 struct tokenval tokval
;
1193 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1198 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1199 (tline
->text
[1]=='%' || tline
->text
[1]=='$' || tline
->text
[1]=='!'))
1203 j
= sizeof(directives
)/sizeof(*directives
);
1206 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1218 * If we're in a non-emitting branch of a condition construct,
1219 * or walking to the end of an already terminated %rep block,
1220 * we should ignore all directives except for condition
1223 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1224 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1225 i
!= PP_IF
&& i
!= PP_ELIF
&&
1226 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1227 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1228 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1229 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1230 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1231 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1232 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1233 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1234 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1235 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1236 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1237 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1238 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1239 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&&
1240 i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1246 * If we're defining a macro or reading a %rep block, we should
1247 * ignore all directives except for %macro/%imacro (which
1248 * generate an error), %endm/%endmacro, and (only if we're in a
1249 * %rep block) %endrep. If we're in a %rep block, another %rep
1250 * causes an error, so should be let through.
1252 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1253 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1254 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1260 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1262 return 0; /* didn't get it */
1270 "trailing garbage after `%%clear' ignored");
1271 for (j
=0; j
<NHASH
; j
++) {
1272 while (mmacros
[j
]) {
1273 MMacro
*m
= mmacros
[j
];
1274 mmacros
[j
] = m
->next
;
1277 while (smacros
[j
]) {
1278 SMacro
*s
= smacros
[j
];
1279 smacros
[j
] = smacros
[j
]->next
;
1280 nasm_free (s
->name
);
1281 free_tlist (s
->expansion
);
1285 free_tlist (origline
);
1289 tline
= tline
->next
;
1291 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1292 tline
->type
!= TOK_INTERNAL_STRING
))
1294 error(ERR_NONFATAL
, "`%%include' expects a file name");
1295 free_tlist (origline
);
1296 return 3; /* but we did _something_ */
1300 "trailing garbage after `%%include' ignored");
1301 if (tline
->type
!= TOK_INTERNAL_STRING
) {
1302 p
= tline
->text
+1; /* point past the quote to the name */
1303 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1305 p
= tline
->text
; /* internal_string is easier */
1306 inc
= nasm_malloc(sizeof(Include
));
1309 inc
->fp
= inc_fopen(p
);
1310 inc
->fname
= src_set_fname(nasm_strdup(p
));
1311 inc
->lineno
= src_set_linnum(0);
1313 inc
->expansion
= NULL
;
1316 list
->uplevel (LIST_INCLUDE
);
1317 free_tlist (origline
);
1321 tline
= tline
->next
;
1323 if (!tok_type_(tline
, TOK_ID
)) {
1325 "`%%push' expects a context identifier");
1326 free_tlist (origline
);
1327 return 3; /* but we did _something_ */
1331 "trailing garbage after `%%push' ignored");
1332 ctx
= nasm_malloc(sizeof(Context
));
1334 ctx
->localmac
= NULL
;
1335 ctx
->name
= nasm_strdup(tline
->text
);
1336 ctx
->number
= unique
++;
1338 free_tlist (origline
);
1342 tline
= tline
->next
;
1344 if (!tok_type_(tline
, TOK_ID
)) {
1346 "`%%repl' expects a context identifier");
1347 free_tlist (origline
);
1348 return 3; /* but we did _something_ */
1352 "trailing garbage after `%%repl' ignored");
1355 "`%%repl': context stack is empty");
1357 nasm_free (cstk
->name
);
1358 cstk
->name
= nasm_strdup(tline
->text
);
1360 free_tlist (origline
);
1366 "trailing garbage after `%%pop' ignored");
1369 "`%%pop': context stack is already empty");
1372 free_tlist (origline
);
1376 tline
->next
= expand_smacro (tline
->next
);
1377 tline
= tline
->next
;
1379 if (tok_type_(tline
, TOK_STRING
)) {
1380 p
= tline
->text
+1; /* point past the quote to the name */
1381 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1382 error(ERR_NONFATAL
, "user error: %s", p
);
1385 error(ERR_WARNING
, "user error: %s", p
);
1388 free_tlist (origline
);
1406 if (istk
->conds
&& !emitting(istk
->conds
->state
))
1409 j
= if_condition(tline
->next
, i
);
1410 tline
->next
= NULL
; /* it got freed */
1411 free_tlist (origline
);
1414 * Bogus expression in %if, but we should pretend
1415 * it was OK anyway, so that we don't get an error
1416 * cascade on the subsequent %else / %endif.
1420 j
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1422 cond
= nasm_malloc(sizeof(Cond
));
1423 cond
->next
= istk
->conds
;
1426 return (j
== COND_IF_TRUE
? 3 : 1);
1444 error(ERR_FATAL
, "`%s': no matching `%%if'",
1446 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1447 istk
->conds
->state
= COND_NEVER
;
1449 j
= if_condition(expand_mmac_params(tline
->next
), i
);
1450 tline
->next
= NULL
; /* it got freed */
1451 free_tlist (origline
);
1454 * The expression was bogus, but let's make
1455 * %endif not complain about missing %if
1459 istk
->conds
->state
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1461 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
1466 "trailing garbage after `%%else' ignored");
1469 "`%%else': no matching `%%if'");
1470 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1471 istk
->conds
->state
= COND_ELSE_FALSE
;
1473 istk
->conds
->state
= COND_ELSE_TRUE
;
1474 free_tlist (origline
);
1480 "trailing garbage after `%%endif' ignored");
1483 "`%%endif': no matching `%%if'");
1485 istk
->conds
= cond
->next
;
1487 free_tlist (origline
);
1494 "`%%%smacro': already defining a macro",
1495 (i
== PP_IMACRO
? "i" : ""));
1496 tline
= tline
->next
;
1498 if (!tok_type_(tline
, TOK_ID
)) {
1499 error (ERR_NONFATAL
,
1500 "`%%%smacro' expects a macro name",
1501 (i
== PP_IMACRO
? "i" : ""));
1504 defining
= nasm_malloc(sizeof(MMacro
));
1505 defining
->name
= nasm_strdup(tline
->text
);
1506 defining
->casesense
= (i
== PP_MACRO
);
1507 defining
->plus
= FALSE
;
1508 defining
->nolist
= FALSE
;
1509 defining
->in_progress
= FALSE
;
1510 defining
->rep_nest
= NULL
;
1511 tline
= tline
->next
;
1513 if (!tok_type_(tline
, TOK_NUMBER
)) {
1514 error (ERR_NONFATAL
,
1515 "`%%%smacro' expects a parameter count",
1516 (i
== PP_IMACRO
? "i" : ""));
1517 defining
->nparam_min
= defining
->nparam_max
= 0;
1519 defining
->nparam_min
= defining
->nparam_max
=
1520 readnum(tline
->text
, &j
);
1522 error (ERR_NONFATAL
,
1523 "unable to parse parameter count `%s'", tline
->text
);
1525 if (tline
&& tok_is_(tline
->next
, "-")) {
1526 tline
= tline
->next
->next
;
1527 if (tok_is_(tline
, "*"))
1528 defining
->nparam_max
= INT_MAX
;
1529 else if (!tok_type_(tline
, TOK_NUMBER
))
1530 error (ERR_NONFATAL
,
1531 "`%%%smacro' expects a parameter count after `-'",
1532 (i
== PP_IMACRO
? "i" : ""));
1534 defining
->nparam_max
= readnum(tline
->text
, &j
);
1536 error (ERR_NONFATAL
,
1537 "unable to parse parameter count `%s'",
1539 if (defining
->nparam_min
> defining
->nparam_max
)
1540 error (ERR_NONFATAL
,
1541 "minimum parameter count exceeds maximum");
1544 if (tline
&& tok_is_(tline
->next
, "+")) {
1545 tline
= tline
->next
;
1546 defining
->plus
= TRUE
;
1548 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1549 !nasm_stricmp(tline
->next
->text
, ".nolist"))
1551 tline
= tline
->next
;
1552 defining
->nolist
= TRUE
;
1554 mmac
= mmacros
[hash(defining
->name
)];
1556 if (!strcmp(mmac
->name
, defining
->name
) &&
1557 (mmac
->nparam_min
<=defining
->nparam_max
|| defining
->plus
) &&
1558 (defining
->nparam_min
<=mmac
->nparam_max
|| mmac
->plus
))
1561 "redefining multi-line macro `%s'", defining
->name
);
1567 * Handle default parameters.
1569 if (tline
&& tline
->next
) {
1570 defining
->dlist
= tline
->next
;
1572 count_mmac_params (defining
->dlist
, &defining
->ndefs
,
1573 &defining
->defaults
);
1575 defining
->dlist
= NULL
;
1576 defining
->defaults
= NULL
;
1578 defining
->expansion
= NULL
;
1579 free_tlist (origline
);
1585 error (ERR_NONFATAL
, "`%s': not defining a macro",
1589 k
= hash(defining
->name
);
1590 defining
->next
= mmacros
[k
];
1591 mmacros
[k
] = defining
;
1593 free_tlist (origline
);
1597 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1598 tline
= tline
->next
;
1599 t
= expand_smacro(tline
->next
);
1601 free_tlist (origline
);
1604 tokval
.t_type
= TOKEN_INVALID
;
1605 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1611 "trailing garbage after expression ignored");
1612 if (!is_simple(evalresult
)) {
1614 "non-constant value given to `%%rotate'");
1618 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
1619 mmac
= mmac
->next_active
;
1621 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
1622 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
1623 if (mmac
->rotate
< 0)
1624 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
1625 mmac
->rotate
%= mmac
->nparam
;
1630 tline
= tline
->next
;
1631 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1632 tline
= tline
->next
;
1633 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
1634 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1635 tline
= tline
->next
;
1638 t
= expand_smacro(tline
->next
);
1640 free_tlist (origline
);
1643 tokval
.t_type
= TOKEN_INVALID
;
1644 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1650 "trailing garbage after expression ignored");
1651 if (!is_simple(evalresult
)) {
1653 "non-constant value given to `%%rep'");
1656 tmp_defining
= defining
;
1657 defining
= nasm_malloc(sizeof(MMacro
));
1658 defining
->name
= NULL
; /* flags this macro as a %rep block */
1659 defining
->casesense
= 0;
1660 defining
->plus
= FALSE
;
1661 defining
->nolist
= nolist
;
1662 defining
->in_progress
= reloc_value(evalresult
) + 1;
1663 defining
->nparam_min
= defining
->nparam_max
= 0;
1664 defining
->defaults
= NULL
;
1665 defining
->dlist
= NULL
;
1666 defining
->expansion
= NULL
;
1667 defining
->next_active
= istk
->mstk
;
1668 defining
->rep_nest
= tmp_defining
;
1672 if (!defining
|| defining
->name
) {
1673 error (ERR_NONFATAL
,
1674 "`%%endrep': no matching `%%rep'");
1679 * Now we have a "macro" defined - although it has no name
1680 * and we won't be entering it in the hash tables - we must
1681 * push a macro-end marker for it on to istk->expansion.
1682 * After that, it will take care of propagating itself (a
1683 * macro-end marker line for a macro which is really a %rep
1684 * block will cause the macro to be re-expanded, complete
1685 * with another macro-end marker to ensure the process
1686 * continues) until the whole expansion is forcibly removed
1687 * from istk->expansion by a %exitrep.
1689 l
= nasm_malloc(sizeof(Line
));
1690 l
->next
= istk
->expansion
;
1691 l
->finishes
= defining
;
1693 istk
->expansion
= l
;
1695 istk
->mstk
= defining
;
1697 list
->uplevel (defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
1698 tmp_defining
= defining
;
1699 defining
= defining
->rep_nest
;
1700 free_tlist (origline
);
1705 * We must search along istk->expansion until we hit a
1706 * macro-end marker for a macro with no name. Then we set
1707 * its `in_progress' flag to 0.
1709 for (l
= istk
->expansion
; l
; l
= l
->next
)
1710 if (l
->finishes
&& !l
->finishes
->name
)
1714 l
->finishes
->in_progress
= 0;
1716 error (ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
1717 free_tlist (origline
);
1722 tline
= tline
->next
;
1724 if (!tline
|| (tline
->type
!= TOK_ID
&&
1725 (tline
->type
!= TOK_PREPROC_ID
||
1726 tline
->text
[1] != '$'))) {
1727 error (ERR_NONFATAL
,
1728 "`%%%sdefine' expects a macro identifier",
1729 (i
== PP_IDEFINE
? "i" : ""));
1730 free_tlist (origline
);
1733 mname
= tline
->text
;
1734 if (tline
->type
== TOK_ID
) {
1736 smhead
= &smacros
[hash(mname
)];
1738 ctx
= get_ctx (tline
->text
);
1743 p
+= strspn(p
, "$");
1744 smhead
= &ctx
->localmac
;
1748 param_start
= tline
= tline
->next
;
1750 if (tok_is_(tline
, "(")) {
1752 * This macro has parameters.
1755 tline
= tline
->next
;
1759 error (ERR_NONFATAL
,
1760 "parameter identifier expected");
1761 free_tlist (origline
);
1764 if (tline
->type
!= TOK_ID
) {
1765 error (ERR_NONFATAL
,
1766 "`%s': parameter identifier expected",
1768 free_tlist (origline
);
1771 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
1772 tline
= tline
->next
;
1774 if (tok_is_(tline
, ",")) {
1775 tline
= tline
->next
;
1778 if (!tok_is_(tline
, ")")) {
1779 error (ERR_NONFATAL
,
1780 "`)' expected to terminate macro template");
1781 free_tlist (origline
);
1787 tline
= tline
->next
;
1789 if (tok_type_(tline
, TOK_WHITESPACE
))
1790 last
= tline
, tline
= tline
->next
;
1795 if (t
->type
== TOK_ID
) {
1796 for (tt
= param_start
; tt
; tt
= tt
->next
)
1797 if (tt
->type
>= TOK_SMAC_PARAM
&&
1798 !strcmp(tt
->text
, t
->text
))
1802 t
->next
= macro_start
;
1807 * Good. We now have a macro name, a parameter count, and a
1808 * token list (in reverse order) for an expansion. We ought
1809 * to be OK just to create an SMacro, store it, and let
1810 * free_tlist have the rest of the line (which we have
1811 * carefully re-terminated after chopping off the expansion
1814 if (smacro_defined (mname
, nparam
, &smac
, i
==PP_DEFINE
)) {
1817 "single-line macro `%s' defined both with and"
1818 " without parameters", mname
);
1819 free_tlist (origline
);
1820 free_tlist (macro_start
);
1824 * We're redefining, so we have to take over an
1825 * existing SMacro structure. This means freeing
1826 * what was already in it.
1828 nasm_free (smac
->name
);
1829 free_tlist (smac
->expansion
);
1832 smac
= nasm_malloc(sizeof(SMacro
));
1833 smac
->next
= *smhead
;
1836 smac
->name
= nasm_strdup(p
);
1837 smac
->casesense
= (i
== PP_DEFINE
);
1838 smac
->nparam
= nparam
;
1839 smac
->expansion
= macro_start
;
1840 smac
->in_progress
= FALSE
;
1841 free_tlist (origline
);
1845 tline
= tline
->next
;
1847 if (!tline
|| (tline
->type
!= TOK_ID
&&
1848 (tline
->type
!= TOK_PREPROC_ID
||
1849 tline
->text
[1] != '$'))) {
1850 error (ERR_NONFATAL
,
1851 "`%%undef' expects a macro identifier");
1852 free_tlist (origline
);
1855 mname
= tline
->text
;
1856 if (tline
->type
== TOK_ID
) {
1858 smhead
= &smacros
[hash(mname
)];
1860 ctx
= get_ctx (tline
->text
);
1862 free_tlist (origline
);
1866 p
+= strspn(p
, "$");
1867 smhead
= &ctx
->localmac
;
1871 tline
= tline
->next
;
1876 "trailing garbage after macro name ignored");
1879 * We now have a macro name... go hunt for it.
1881 while (smacro_defined (mname
, -1, &smac
, 1)) {
1882 /* Defined, so we need to find its predecessor and nuke it */
1884 for ( s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
1887 nasm_free(smac
->name
);
1888 free_tlist(smac
->expansion
);
1896 tline
= tline
->next
;
1898 if (!tline
|| (tline
->type
!= TOK_ID
&&
1899 (tline
->type
!= TOK_PREPROC_ID
||
1900 tline
->text
[1] != '$'))) {
1901 error (ERR_NONFATAL
,
1902 "`%%%sassign' expects a macro identifier",
1903 (i
== PP_IASSIGN
? "i" : ""));
1904 free_tlist (origline
);
1907 mname
= tline
->text
;
1908 if (tline
->type
== TOK_ID
) {
1910 smhead
= &smacros
[hash(mname
)];
1912 ctx
= get_ctx (tline
->text
);
1914 free_tlist (origline
);
1918 p
+= strspn(p
, "$");
1919 smhead
= &ctx
->localmac
;
1923 tline
= tline
->next
;
1926 tline
= expand_smacro (tline
);
1929 tokval
.t_type
= TOKEN_INVALID
;
1930 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1933 free_tlist (origline
);
1939 "trailing garbage after expression ignored");
1941 if (!is_simple(evalresult
)) {
1943 "non-constant value given to `%%%sassign'",
1944 (i
== PP_IASSIGN
? "i" : ""));
1945 free_tlist (origline
);
1949 macro_start
= nasm_malloc(sizeof(*macro_start
));
1950 macro_start
->next
= NULL
;
1951 make_tok_num(macro_start
, reloc_value(evalresult
));
1952 macro_start
->mac
= NULL
;
1955 * We now have a macro name, an implicit parameter count of
1956 * zero, and a numeric token to use as an expansion. Create
1957 * and store an SMacro.
1959 if (smacro_defined (mname
, 0, &smac
, i
==PP_ASSIGN
)) {
1962 "single-line macro `%s' defined both with and"
1963 " without parameters", mname
);
1966 * We're redefining, so we have to take over an
1967 * existing SMacro structure. This means freeing
1968 * what was already in it.
1970 nasm_free (smac
->name
);
1971 free_tlist (smac
->expansion
);
1975 smac
= nasm_malloc(sizeof(SMacro
));
1976 smac
->next
= *smhead
;
1979 smac
->name
= nasm_strdup(p
);
1980 smac
->casesense
= (i
== PP_ASSIGN
);
1982 smac
->expansion
= macro_start
;
1983 smac
->in_progress
= FALSE
;
1984 free_tlist (origline
);
1989 * Syntax is `%line nnn[+mmm] [filename]'
1991 tline
= tline
->next
;
1993 if (!tok_type_(tline
, TOK_NUMBER
)) {
1994 error (ERR_NONFATAL
, "`%%line' expects line number");
1995 free_tlist (origline
);
1998 k
= readnum(tline
->text
, &j
);
2000 tline
= tline
->next
;
2001 if (tok_is_(tline
, "+")) {
2002 tline
= tline
->next
;
2003 if (!tok_type_(tline
, TOK_NUMBER
)) {
2004 error (ERR_NONFATAL
,
2005 "`%%line' expects line increment");
2006 free_tlist (origline
);
2009 m
= readnum(tline
->text
, &j
);
2010 tline
= tline
->next
;
2016 nasm_free ( src_set_fname ( detoken(tline
) ) );
2018 free_tlist (origline
);
2023 "preprocessor directive `%s' not yet implemented",
2031 * Ensure that a macro parameter contains a condition code and
2032 * nothing else. Return the condition code index if so, or -1
2035 static int find_cc (Token
*t
)
2041 if (t
->type
!= TOK_ID
)
2045 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2049 j
= sizeof(conditions
)/sizeof(*conditions
);
2052 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2068 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2069 * %-n) and MMacro-local identifiers (%%foo).
2071 static Token
*expand_mmac_params (Token
*tline
)
2073 Token
*t
, *tt
, *ttt
, **tail
, *thead
;
2079 if (tline
->type
== TOK_PREPROC_ID
&&
2080 (tline
->text
[1] == '+' || tline
->text
[1] == '-' ||
2081 tline
->text
[1] == '%' ||
2082 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2084 int type
= 0, cc
; /* type = 0 to placate optimisers */
2090 tline
= tline
->next
;
2093 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2094 mac
= mac
->next_active
;
2096 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2097 else switch (t
->text
[1]) {
2099 * We have to make a substitution of one of the
2100 * forms %1, %-1, %+1, %%foo, %0.
2104 sprintf(tmpbuf
, "%d", mac
->nparam
);
2105 text
= nasm_strdup(tmpbuf
);
2109 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
2110 text
= nasm_strcat(tmpbuf
, t
->text
+2);
2113 n
= atoi(t
->text
+2)-1;
2114 if (n
>= mac
->nparam
)
2117 if (mac
->nparam
> 1)
2118 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2119 tt
= mac
->params
[n
];
2123 error (ERR_NONFATAL
,
2124 "macro parameter %d is not a condition code",
2129 if (inverse_ccs
[cc
] == -1) {
2130 error (ERR_NONFATAL
,
2131 "condition code `%s' is not invertible",
2135 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2139 n
= atoi(t
->text
+2)-1;
2140 if (n
>= mac
->nparam
)
2143 if (mac
->nparam
> 1)
2144 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2145 tt
= mac
->params
[n
];
2149 error (ERR_NONFATAL
,
2150 "macro parameter %d is not a condition code",
2155 text
= nasm_strdup(conditions
[cc
]);
2159 n
= atoi(t
->text
+1)-1;
2160 if (n
>= mac
->nparam
)
2163 if (mac
->nparam
> 1)
2164 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2165 tt
= mac
->params
[n
];
2168 for (i
=0; i
<mac
->paramlen
[n
]; i
++) {
2169 ttt
= *tail
= nasm_malloc(sizeof(Token
));
2171 ttt
->type
= tt
->type
;
2172 ttt
->text
= nasm_strdup(tt
->text
);
2177 text
= NULL
; /* we've done it here */
2180 nasm_free (t
->text
);
2193 tline
= tline
->next
;
2200 for (; t
&& (tt
=t
->next
)!=NULL
; t
= t
->next
)
2202 case TOK_WHITESPACE
:
2203 if (tt
->type
== TOK_WHITESPACE
) {
2205 nasm_free(tt
->text
);
2210 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2211 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2215 nasm_free(tt
->text
);
2220 if (tt
->type
== TOK_NUMBER
) {
2221 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2225 nasm_free(tt
->text
);
2235 * Expand all single-line macro calls made in the given line.
2236 * Return the expanded version of the line. The original is deemed
2237 * to be destroyed in the process. (In reality we'll just move
2238 * Tokens from input to output a lot of the time, rather than
2239 * actually bothering to destroy and replicate.)
2241 static Token
*expand_smacro (Token
*tline
)
2243 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2244 SMacro
*head
= NULL
, *m
;
2247 int nparam
, sparam
, brackets
;
2253 while (tline
) { /* main token loop */
2255 if (tline
->type
== TOK_ID
) {
2256 head
= smacros
[hash(tline
->text
)];
2258 } else if (tline
->type
== TOK_PREPROC_ID
&& tline
->text
[1] == '$') {
2259 Context
*ctx
= get_ctx (tline
->text
);
2261 head
= ctx
->localmac
;
2263 p
+= strspn(p
, "$");
2268 * We've hit an identifier. As in is_mmacro below, we first
2269 * check whether the identifier is a single-line macro at
2270 * all, then think about checking for parameters if
2273 for (m
= head
; m
; m
= m
->next
)
2274 if (!mstrcmp(m
->name
, p
, m
->casesense
))
2280 if (m
->nparam
== 0) {
2282 * Simple case: the macro is parameterless. Discard the
2283 * one token that the macro call took, and push the
2284 * expansion back on the to-do stack.
2288 if (!strcmp("__FILE__", m
->name
)) {
2290 src_get(&num
, &(tline
->text
));
2291 nasm_quote(&(tline
->text
));
2292 tline
->type
= TOK_STRING
;
2295 if (!strcmp("__LINE__", m
->name
)) {
2296 nasm_free(tline
->text
);
2297 make_tok_num(tline
, src_get_linnum());
2301 tline
= tline
->next
;
2302 nasm_free (t
->text
);
2309 * Complicated case: at least one macro with this name
2310 * exists and takes parameters. We must find the
2311 * parameters in the call, count them, find the SMacro
2312 * that corresponds to that form of the macro call, and
2313 * substitute for the parameters when we expand. What a
2316 tline
= tline
->next
;
2318 if (!tok_is_(tline
, "(")) {
2320 * This macro wasn't called with parameters: ignore
2321 * the call. (Behaviour borrowed from gnu cpp.)
2331 tline
= tline
->next
;
2332 sparam
= PARAM_DELTA
;
2333 params
= nasm_malloc (sparam
*sizeof(Token
*));
2335 paramsize
= nasm_malloc (sparam
*sizeof(int));
2337 for (;;tline
= tline
->next
) { /* parameter loop */
2340 "macro call expects terminating `)'");
2343 if (tline
->type
== TOK_WHITESPACE
&& brackets
<=0) {
2344 if (paramsize
[nparam
])
2347 params
[nparam
] = tline
->next
;
2348 continue; /* parameter loop */
2350 if (tline
->type
== TOK_OTHER
&& tline
->text
[1]==0) {
2351 char ch
= tline
->text
[0];
2352 if (ch
== ',' && !paren
&& brackets
<=0) {
2353 if (++nparam
>= sparam
) {
2354 sparam
+= PARAM_DELTA
;
2355 params
= nasm_realloc (params
,
2356 sparam
*sizeof(Token
*));
2357 paramsize
= nasm_realloc (paramsize
,
2358 sparam
*sizeof(int));
2360 params
[nparam
] = tline
->next
;
2361 paramsize
[nparam
] = 0;
2363 continue; /* parameter loop */
2366 (brackets
>0 || (brackets
==0 &&
2367 !paramsize
[nparam
])))
2371 params
[nparam
] = tline
->next
;
2372 continue; /* parameter loop */
2375 if (ch
== '}' && brackets
>0)
2376 if (--brackets
== 0) {
2378 continue; /* parameter loop */
2380 if (ch
== '(' && !brackets
)
2382 if (ch
== ')' && brackets
<=0)
2388 error (ERR_NONFATAL
, "braces do not "
2389 "enclose all of macro parameter");
2391 paramsize
[nparam
] += white
+1;
2393 } /* parameter loop */
2395 while (m
&& (m
->nparam
!= nparam
||
2396 mstrcmp(m
->name
, p
, m
->casesense
)))
2399 error (ERR_WARNING
|ERR_WARN_MNP
,
2400 "macro `%s' exists, "
2401 "but not taking %d parameters",
2402 mstart
->text
, nparam
);
2405 if (m
&& m
->in_progress
)
2407 if (!m
) /* in progess or didn't find '(' or wrong nparam */
2410 * Design question: should we handle !tline, which
2411 * indicates missing ')' here, or expand those
2412 * macros anyway, which requires the (t) test a few
2416 nasm_free (paramsize
);
2421 * Expand the macro: we are placed on the last token of the
2422 * call, so that we can easily split the call from the
2423 * following tokens. We also start by pushing an SMAC_END
2424 * token for the cycle removal.
2431 tt
= nasm_malloc(sizeof(Token
));
2432 tt
->type
= TOK_SMAC_END
;
2435 m
->in_progress
= TRUE
;
2438 for (t
= m
->expansion
; t
; t
= t
->next
) {
2439 if (t
->type
>= TOK_SMAC_PARAM
) {
2440 Token
*pcopy
= tline
, **ptail
= &pcopy
;
2444 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
2445 for (i
=paramsize
[t
->type
-TOK_SMAC_PARAM
]; --i
>=0;) {
2446 pt
= *ptail
= nasm_malloc(sizeof(Token
));
2449 pt
->text
= nasm_strdup(ttt
->text
);
2450 pt
->type
= ttt
->type
;
2456 tt
= nasm_malloc(sizeof(Token
));
2458 tt
->text
= nasm_strdup(t
->text
);
2466 * Having done that, get rid of the macro call, and clean
2467 * up the parameters.
2470 nasm_free (paramsize
);
2471 free_tlist (mstart
);
2472 continue; /* main token loop */
2477 if (tline
->type
== TOK_SMAC_END
) {
2478 tline
->mac
->in_progress
= FALSE
;
2480 tline
= tline
->next
;
2484 tline
= tline
->next
;
2488 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '$') {
2489 Context
*c
= get_ctx (t
->text
);
2490 char *p
, *q
, buffer
[40];
2495 q
+= strspn(q
, "$");
2496 sprintf(buffer
, "..@%lu.", c
->number
);
2497 p
= nasm_strcat (buffer
,q
);
2498 nasm_free (t
->text
);
2509 * Determine whether the given line constitutes a multi-line macro
2510 * call, and return the MMacro structure called if so. Doesn't have
2511 * to check for an initial label - that's taken care of in
2512 * expand_mmacro - but must check numbers of parameters. Guaranteed
2513 * to be called with tline->type == TOK_ID, so the putative macro
2514 * name is easy to find.
2516 static MMacro
*is_mmacro (Token
*tline
, Token
***params_array
)
2522 head
= mmacros
[hash(tline
->text
)];
2525 * Efficiency: first we see if any macro exists with the given
2526 * name. If not, we can return NULL immediately. _Then_ we
2527 * count the parameters, and then we look further along the
2528 * list if necessary to find the proper MMacro.
2530 for (m
= head
; m
; m
= m
->next
)
2531 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2537 * OK, we have a potential macro. Count and demarcate the
2540 count_mmac_params (tline
->next
, &nparam
, ¶ms
);
2543 * So we know how many parameters we've got. Find the MMacro
2544 * structure that handles this number.
2547 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
)) {
2549 * This one is right. Just check if cycle removal
2550 * prohibits us using it before we actually celebrate...
2552 if (m
->in_progress
) {
2554 error (ERR_NONFATAL
,
2555 "self-reference in multi-line macro `%s'",
2562 * It's right, and we can use it. Add its default
2563 * parameters to the end of our list if necessary.
2565 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
2566 params
= nasm_realloc (params
, ((m
->nparam_min
+m
->ndefs
+1) *
2568 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
2569 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
2574 * If we've gone over the maximum parameter count (and
2575 * we're in Plus mode), ignore parameters beyond
2578 if (m
->plus
&& nparam
> m
->nparam_max
)
2579 nparam
= m
->nparam_max
;
2581 * Then terminate the parameter list, and leave.
2583 if (!params
) { /* need this special case */
2584 params
= nasm_malloc(sizeof(*params
));
2587 params
[nparam
] = NULL
;
2588 *params_array
= params
;
2592 * This one wasn't right: look for the next one with the
2595 for (m
= m
->next
; m
; m
= m
->next
)
2596 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2601 * After all that, we didn't find one with the right number of
2602 * parameters. Issue a warning, and fail to expand the macro.
2604 error (ERR_WARNING
|ERR_WARN_MNP
,
2605 "macro `%s' exists, but not taking %d parameters",
2606 tline
->text
, nparam
);
2612 * Expand the multi-line macro call made by the given line, if
2613 * there is one to be expanded. If there is, push the expansion on
2614 * istk->expansion and return 1. Otherwise return 0.
2616 static int expand_mmacro (Token
*tline
)
2618 Token
*startline
= tline
;
2619 Token
*label
= NULL
;
2620 int dont_prepend
= 0;
2621 Token
**params
, *t
, *tt
;
2624 int i
, nparam
, *paramlen
;
2628 if (!tok_type_(t
, TOK_ID
))
2630 m
= is_mmacro (t
, ¶ms
);
2634 * We have an id which isn't a macro call. We'll assume
2635 * it might be a label; we'll also check to see if a
2636 * colon follows it. Then, if there's another id after
2637 * that lot, we'll check it again for macro-hood.
2641 if (tok_type_(t
, TOK_WHITESPACE
))
2642 last
= t
, t
= t
->next
;
2643 if (tok_is_(t
, ":")) {
2645 last
= t
, t
= t
->next
;
2646 if (tok_type_(t
, TOK_WHITESPACE
))
2647 last
= t
, t
= t
->next
;
2649 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
2656 * Fix up the parameters: this involves stripping leading and
2657 * trailing whitespace, then stripping braces if they are
2660 for (nparam
= 0; params
[nparam
]; nparam
++)
2662 paramlen
= nparam
? nasm_malloc(nparam
*sizeof(*paramlen
)) : NULL
;
2664 for (i
= 0; params
[i
]; i
++) {
2666 int comma
= (!m
->plus
|| i
< nparam
-1);
2670 if (tok_is_(t
, "{"))
2671 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
2675 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
2676 break; /* ... because we have hit a comma */
2677 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
2678 break; /* ... or a space then a comma */
2679 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
2680 break; /* ... or a brace */
2687 * OK, we have a MMacro structure together with a set of
2688 * parameters. We must now go through the expansion and push
2689 * copies of each Line on to istk->expansion. Substitution of
2690 * parameter tokens and macro-local tokens doesn't get done
2691 * until the single-line macro substitution process; this is
2692 * because delaying them allows us to change the semantics
2693 * later through %rotate.
2695 * First, push an end marker on to istk->expansion, mark this
2696 * macro as in progress, and set up its invocation-specific
2699 ll
= nasm_malloc(sizeof(Line
));
2700 ll
->next
= istk
->expansion
;
2703 istk
->expansion
= ll
;
2705 m
->in_progress
= TRUE
;
2710 m
->paramlen
= paramlen
;
2711 m
->unique
= unique
++;
2713 m
->next_active
= istk
->mstk
;
2716 for (l
= m
->expansion
; l
; l
= l
->next
) {
2719 ll
= nasm_malloc(sizeof(Line
));
2720 ll
->finishes
= NULL
;
2721 ll
->next
= istk
->expansion
;
2722 istk
->expansion
= ll
;
2725 for (t
= l
->first
; t
; t
= t
->next
) {
2727 if (t
->type
== TOK_PREPROC_ID
&&
2728 t
->text
[1]=='0' && t
->text
[2]=='0')
2735 tt
= *tail
= nasm_malloc(sizeof(Token
));
2738 tt
->text
= nasm_strdup(x
->text
);
2745 * If we had a label, push it on as the first line of
2746 * the macro expansion.
2750 free_tlist(startline
);
2752 ll
= nasm_malloc(sizeof(Line
));
2753 ll
->finishes
= NULL
;
2754 ll
->next
= istk
->expansion
;
2755 istk
->expansion
= ll
;
2756 ll
->first
= startline
;
2757 if (!dont_prepend
) {
2759 label
= label
->next
;
2760 label
->next
= tt
= nasm_malloc(sizeof(Token
));
2763 tt
->type
= TOK_OTHER
;
2764 tt
->text
= nasm_strdup(":");
2768 list
->uplevel (m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2773 static void pp_reset (char *file
, int apass
, efunc errfunc
, evalfunc eval
,
2780 istk
= nasm_malloc(sizeof(Include
));
2783 istk
->expansion
= NULL
;
2785 istk
->fp
= fopen(file
, "r");
2787 src_set_fname(nasm_strdup(file
));
2791 error (ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'", file
);
2793 for (h
=0; h
<NHASH
; h
++) {
2799 any_extrastdmac
= (extrastdmac
!= NULL
);
2805 static char *pp_getline (void)
2813 * Fetch a tokenised line, either from the macro-expansion
2814 * buffer or from the input file.
2817 while (istk
->expansion
&& istk
->expansion
->finishes
) {
2818 Line
*l
= istk
->expansion
;
2819 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
2823 * This is a macro-end marker for a macro with no
2824 * name, which means it's not really a macro at all
2825 * but a %rep block, and the `in_progress' field is
2826 * more than 1, meaning that we still need to
2827 * repeat. (1 means the natural last repetition; 0
2828 * means termination by %exitrep.) We have
2829 * therefore expanded up to the %endrep, and must
2830 * push the whole block on to the expansion buffer
2831 * again. We don't bother to remove the macro-end
2832 * marker: we'd only have to generate another one
2835 l
->finishes
->in_progress
--;
2836 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
2837 Token
*t
, *tt
, **tail
;
2839 ll
= nasm_malloc(sizeof(Line
));
2840 ll
->next
= istk
->expansion
;
2841 ll
->finishes
= NULL
;
2845 for (t
= l
->first
; t
; t
= t
->next
) {
2847 tt
= *tail
= nasm_malloc(sizeof(Token
));
2851 tt
->text
= nasm_strdup(t
->text
);
2856 istk
->expansion
= ll
;
2860 * Check whether a `%rep' was started and not ended
2861 * within this macro expansion. This can happen and
2862 * should be detected. It's a fatal error because
2863 * I'm too confused to work out how to recover
2869 "defining with name in expansion");
2870 else if (istk
->mstk
->name
)
2871 error (ERR_FATAL
, "`%%rep' without `%%endrep' within"
2872 " expansion of macro `%s'", istk
->mstk
->name
);
2876 * FIXME: investigate the relationship at this point between
2877 * istk->mstk and l->finishes
2880 MMacro
*m
= istk
->mstk
;
2881 istk
->mstk
= m
->next_active
;
2884 * This was a real macro call, not a %rep, and
2885 * therefore the parameter information needs to
2888 nasm_free(m
->params
);
2889 free_tlist(m
->iline
);
2890 nasm_free(m
->paramlen
);
2891 l
->finishes
->in_progress
= FALSE
;
2896 istk
->expansion
= l
->next
;
2898 list
->downlevel (LIST_MACRO
);
2901 while (1) { /* until we get a line we can use */
2903 if (istk
->expansion
) { /* from a macro expansion */
2905 Line
*l
= istk
->expansion
;
2907 istk
->expansion
= l
->next
;
2910 list
->line (LIST_MACRO
, p
);
2915 if (line
) { /* from the current input file */
2916 line
= prepreproc(line
);
2917 tline
= tokenise(line
);
2922 * The current file has ended; work down the istk
2928 error(ERR_FATAL
, "expected `%%endif' before end of file");
2930 list
->downlevel (LIST_INCLUDE
);
2931 src_set_linnum(i
->lineno
);
2932 nasm_free ( src_set_fname(i
->fname
) );
2940 * We must expand MMacro parameters and MMacro-local labels
2941 * _before_ we plunge into directive processing, to cope
2942 * with things like `%define something %1' such as STRUC
2943 * uses. Unless we're _defining_ a MMacro, in which case
2944 * those tokens should be left alone to go into the
2945 * definition; and unless we're in a non-emitting
2946 * condition, in which case we don't want to meddle with
2949 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
2950 tline
= expand_mmac_params(tline
);
2953 * Check the line to see if it's a preprocessor directive.
2955 ret
= do_directive(tline
);
2958 } else if (defining
) {
2960 * We're defining a multi-line macro. We emit nothing
2962 * shove the tokenised line on to the macro definition.
2964 Line
*l
= nasm_malloc(sizeof(Line
));
2965 l
->next
= defining
->expansion
;
2967 l
->finishes
= FALSE
;
2968 defining
->expansion
= l
;
2970 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
2972 * We're in a non-emitting branch of a condition block.
2973 * Emit nothing at all, not even a blank line: when we
2974 * emerge from the condition we'll give a line-number
2975 * directive so we keep our place correctly.
2979 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
2981 * We're in a %rep block which has been terminated, so
2982 * we're walking through to the %endrep without
2983 * emitting anything. Emit nothing at all, not even a
2984 * blank line: when we emerge from the %rep block we'll
2985 * give a line-number directive so we keep our place
2991 tline
= expand_smacro(tline
);
2992 ret
= expand_mmacro(tline
);
2995 * De-tokenise the line again, and emit it.
2997 line
= detoken(tline
);
3001 continue; /* expand_mmacro calls free_tlist */
3009 static void pp_cleanup (void)
3014 error (ERR_NONFATAL
, "end of file while still defining macro `%s'",
3016 free_mmacro (defining
);
3020 for (h
=0; h
<NHASH
; h
++) {
3021 while (mmacros
[h
]) {
3022 MMacro
*m
= mmacros
[h
];
3023 mmacros
[h
] = mmacros
[h
]->next
;
3026 while (smacros
[h
]) {
3027 SMacro
*s
= smacros
[h
];
3028 smacros
[h
] = smacros
[h
]->next
;
3029 nasm_free (s
->name
);
3030 free_tlist (s
->expansion
);
3038 nasm_free (i
->fname
);
3045 void pp_include_path (char *path
)
3049 i
= nasm_malloc(sizeof(IncPath
));
3050 i
->path
= nasm_strdup(path
);
3055 void pp_pre_include (char *fname
)
3057 Token
*inc
, *space
, *name
;
3060 inc
= nasm_malloc(sizeof(Token
));
3061 inc
->next
= space
= nasm_malloc(sizeof(Token
));
3062 space
->next
= name
= nasm_malloc(sizeof(Token
));
3065 inc
->type
= TOK_PREPROC_ID
;
3066 inc
->text
= nasm_strdup("%include");
3067 space
->type
= TOK_WHITESPACE
;
3068 space
->text
= nasm_strdup(" ");
3069 name
->type
= TOK_INTERNAL_STRING
;
3070 name
->text
= nasm_strdup(fname
);
3072 inc
->mac
= space
->mac
= name
->mac
= NULL
;
3074 l
= nasm_malloc(sizeof(Line
));
3077 l
->finishes
= FALSE
;
3081 void pp_pre_define (char *definition
)
3087 equals
= strchr(definition
, '=');
3089 def
= nasm_malloc(sizeof(Token
));
3090 def
->next
= space
= nasm_malloc(sizeof(Token
));
3093 space
->next
= tokenise(definition
);
3097 def
->type
= TOK_PREPROC_ID
;
3098 def
->text
= nasm_strdup("%define");
3099 space
->type
= TOK_WHITESPACE
;
3100 space
->text
= nasm_strdup(" ");
3102 def
->mac
= space
->mac
= NULL
;
3104 l
= nasm_malloc(sizeof(Line
));
3107 l
->finishes
= FALSE
;
3111 void pp_pre_undefine (char *definition
)
3116 def
= nasm_malloc(sizeof(Token
));
3117 def
->next
= space
= nasm_malloc(sizeof(Token
));
3118 space
->next
= tokenise(definition
);
3120 def
->type
= TOK_PREPROC_ID
;
3121 def
->text
= nasm_strdup("%undef");
3122 space
->type
= TOK_WHITESPACE
;
3123 space
->text
= nasm_strdup(" ");
3125 def
->mac
= space
->mac
= NULL
;
3127 l
= nasm_malloc(sizeof(Line
));
3130 l
->finishes
= FALSE
;
3134 void pp_extra_stdmac (char **macros
)
3136 extrastdmac
= macros
;
3139 static void make_tok_num(Token
*tok
, long val
)
3142 sprintf(numbuf
, "%ld", val
);
3143 tok
->text
= nasm_strdup(numbuf
);
3144 tok
->type
= TOK_NUMBER
;