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"
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
286 static Context
*cstk
;
287 static Include
*istk
;
288 static IncPath
*ipath
= NULL
;
291 static evalfunc evaluate
;
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
>= sizeof(multipliers
)/sizeof(*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
;
883 combine
= nasm_strcat(prefix
,file
);
884 fp
= fopen(combine
, "r");
895 "unable to open include file `%s'", file
);
896 return NULL
; /* never reached - placate compilers */
900 * Determine if we should warn on defining a single-line macro of
901 * name `name', with `nparam' parameters. If nparam is 0, will
902 * return TRUE if _any_ single-line macro of that name is defined.
903 * Otherwise, will return TRUE if a single-line macro with either
904 * `nparam' or no parameters is defined.
906 * If a macro with precisely the right number of parameters is
907 * defined, the address of the definition structure will be
908 * returned in `defn'; otherwise NULL will be returned. If `defn'
909 * is NULL, no action will be taken regarding its contents, and no
912 * Note that this is also called with nparam zero to resolve
915 static int smacro_defined (char *name
, int nparam
, SMacro
**defn
, int nocase
)
921 if (name
[0] == '%' && name
[1] == '$') {
922 ctx
= get_ctx (name
);
924 return FALSE
; /* got to return _something_ */
929 m
= smacros
[hash(name
)];
934 if (!mstrcmp(m
->name
, p
, m
->casesense
& nocase
) &&
935 (nparam
== 0 || m
->nparam
== 0 || nparam
== m
->nparam
)) {
937 if (nparam
== m
->nparam
)
950 * Count and mark off the parameters in a multi-line macro call.
951 * This is called both from within the multi-line macro expansion
952 * code, and also to mark off the default parameters when provided
953 * in a %macro definition line.
955 static void count_mmac_params (Token
*t
, int *nparam
, Token
***params
)
957 int paramsize
, brace
;
959 *nparam
= paramsize
= 0;
962 if (*nparam
>= paramsize
) {
963 paramsize
+= PARAM_DELTA
;
964 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
970 (*params
)[(*nparam
)++] = t
;
971 while (tok_isnt_(t
, brace
? "}" : ","))
973 if (t
) { /* got a comma/brace */
977 * Now we've found the closing brace, look further
981 if (tok_isnt_(t
, ",")) {
983 "braces do not enclose all of macro parameter");
984 while (tok_isnt_(t
, ","))
988 t
= t
->next
; /* eat the comma */
995 * Determine whether one of the various `if' conditions is true or
998 * We must free the tline we get passed.
1000 static int if_condition (Token
*tline
, int i
)
1003 Token
* t
, * tt
, ** tptr
, * origline
;
1004 struct tokenval tokval
;
1010 case PP_IFCTX
: case PP_ELIFCTX
:
1011 case PP_IFNCTX
: case PP_ELIFNCTX
:
1012 j
= FALSE
; /* have we matched yet? */
1015 "`%s': context stack is empty", directives
[i
]);
1016 else while (tline
) {
1018 if (!tline
|| tline
->type
!= TOK_ID
) {
1020 "`%s' expects context identifiers", directives
[i
]);
1021 free_tlist (origline
);
1024 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1026 tline
= tline
->next
;
1028 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1030 free_tlist (origline
);
1033 case PP_IFDEF
: case PP_ELIFDEF
:
1034 case PP_IFNDEF
: case PP_ELIFNDEF
:
1035 j
= FALSE
; /* have we matched yet? */
1038 if (!tline
|| (tline
->type
!= TOK_ID
&&
1039 (tline
->type
!= TOK_PREPROC_ID
||
1040 tline
->text
[1] != '$'))) {
1042 "`%%if%sdef' expects macro identifiers",
1043 (i
==PP_ELIFNDEF
? "n" : ""));
1044 free_tlist (origline
);
1047 if (smacro_defined(tline
->text
, 0, NULL
, 1))
1049 tline
= tline
->next
;
1051 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1053 free_tlist (origline
);
1056 case PP_IFIDN
: case PP_ELIFIDN
: case PP_IFNIDN
: case PP_ELIFNIDN
:
1057 case PP_IFIDNI
: case PP_ELIFIDNI
: case PP_IFNIDNI
: case PP_ELIFNIDNI
:
1058 tline
= expand_smacro(tline
);
1060 while (tok_isnt_(tt
, ","))
1063 error(ERR_NONFATAL
, "`%s' expects two comma-separated arguments");
1068 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1069 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1070 j
= TRUE
; /* assume equality unless proved not */
1071 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1072 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1073 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1078 if (t
->type
== TOK_WHITESPACE
) {
1081 } else if (tt
->type
== TOK_WHITESPACE
) {
1084 } else if (tt
->type
!= t
->type
||
1085 (casesense
? strcmp(tt
->text
, t
->text
) :
1086 nasm_stricmp(tt
->text
, t
->text
))) {
1087 j
= FALSE
; /* found mismatching tokens */
1095 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1096 j
= FALSE
; /* trailing gunk on one end or other */
1097 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1098 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1103 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1104 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1105 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1106 tline
= expand_smacro(tline
);
1108 while (tok_type_(t
, TOK_WHITESPACE
))
1110 j
= FALSE
; /* placate optimiser */
1112 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1113 j
= (t
->type
== TOK_ID
);
1115 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1116 j
= (t
->type
== TOK_NUMBER
);
1118 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1119 j
= (t
->type
== TOK_STRING
);
1122 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1123 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1124 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1129 case PP_IF
: case PP_ELIF
:
1130 t
= tline
= expand_smacro(tline
);
1132 tokval
.t_type
= TOKEN_INVALID
;
1133 evalresult
= evaluate (ppscan
, tptr
, &tokval
,
1134 NULL
, pass
| 0x10, error
, NULL
);
1140 "trailing garbage after expression ignored");
1141 if (!is_simple(evalresult
)) {
1143 "non-constant value given to `%s'", directives
[i
]);
1146 return reloc_value(evalresult
) != 0;
1150 "preprocessor directive `%s' not yet implemented",
1152 free_tlist (origline
);
1153 return -1; /* yeah, right */
1158 * Find out if a line contains a preprocessor directive, and deal
1161 * If a directive _is_ found, we are expected to free_tlist() the
1164 * Return values go like this:
1166 * bit 0 is set if a directive was found (so the line gets freed)
1168 static int do_directive (Token
*tline
)
1170 int i
, j
, k
, m
, nparam
, nolist
;
1175 SMacro
*smac
, **smhead
;
1177 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1179 struct tokenval tokval
;
1181 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1186 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1187 (tline
->text
[1]=='%' || tline
->text
[1]=='$' || tline
->text
[1]=='!'))
1191 j
= sizeof(directives
)/sizeof(*directives
);
1194 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1206 * If we're in a non-emitting branch of a condition construct,
1207 * or walking to the end of an already terminated %rep block,
1208 * we should ignore all directives except for condition
1211 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1212 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1213 i
!= PP_IF
&& i
!= PP_ELIF
&&
1214 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1215 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1216 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1217 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1218 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1219 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1220 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1221 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1222 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1223 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1224 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1225 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1226 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1227 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&&
1228 i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1234 * If we're defining a macro or reading a %rep block, we should
1235 * ignore all directives except for %macro/%imacro (which
1236 * generate an error), %endm/%endmacro, and (only if we're in a
1237 * %rep block) %endrep. If we're in a %rep block, another %rep
1238 * causes an error, so should be let through.
1240 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1241 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1242 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1248 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1250 return 0; /* didn't get it */
1258 "trailing garbage after `%%clear' ignored");
1259 for (j
=0; j
<NHASH
; j
++) {
1260 while (mmacros
[j
]) {
1261 MMacro
*m
= mmacros
[j
];
1262 mmacros
[j
] = m
->next
;
1265 while (smacros
[j
]) {
1266 SMacro
*s
= smacros
[j
];
1267 smacros
[j
] = smacros
[j
]->next
;
1268 nasm_free (s
->name
);
1269 free_tlist (s
->expansion
);
1273 free_tlist (origline
);
1277 tline
= tline
->next
;
1279 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1280 tline
->type
!= TOK_INTERNAL_STRING
))
1282 error(ERR_NONFATAL
, "`%%include' expects a file name");
1283 free_tlist (origline
);
1284 return 3; /* but we did _something_ */
1288 "trailing garbage after `%%include' ignored");
1289 if (tline
->type
!= TOK_INTERNAL_STRING
) {
1290 p
= tline
->text
+1; /* point past the quote to the name */
1291 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1293 p
= tline
->text
; /* internal_string is easier */
1294 inc
= nasm_malloc(sizeof(Include
));
1297 inc
->fp
= inc_fopen(p
);
1298 inc
->fname
= src_set_fname(nasm_strdup(p
));
1299 inc
->lineno
= src_set_linnum(0);
1301 inc
->expansion
= NULL
;
1304 list
->uplevel (LIST_INCLUDE
);
1305 free_tlist (origline
);
1309 tline
= tline
->next
;
1311 if (!tok_type_(tline
, TOK_ID
)) {
1313 "`%%push' expects a context identifier");
1314 free_tlist (origline
);
1315 return 3; /* but we did _something_ */
1319 "trailing garbage after `%%push' ignored");
1320 ctx
= nasm_malloc(sizeof(Context
));
1322 ctx
->localmac
= NULL
;
1323 ctx
->name
= nasm_strdup(tline
->text
);
1324 ctx
->number
= unique
++;
1326 free_tlist (origline
);
1330 tline
= tline
->next
;
1332 if (!tok_type_(tline
, TOK_ID
)) {
1334 "`%%repl' expects a context identifier");
1335 free_tlist (origline
);
1336 return 3; /* but we did _something_ */
1340 "trailing garbage after `%%repl' ignored");
1343 "`%%repl': context stack is empty");
1345 nasm_free (cstk
->name
);
1346 cstk
->name
= nasm_strdup(tline
->text
);
1348 free_tlist (origline
);
1354 "trailing garbage after `%%pop' ignored");
1357 "`%%pop': context stack is already empty");
1360 free_tlist (origline
);
1364 tline
->next
= expand_smacro (tline
->next
);
1365 tline
= tline
->next
;
1367 if (tok_type_(tline
, TOK_STRING
)) {
1368 p
= tline
->text
+1; /* point past the quote to the name */
1369 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1370 error(ERR_NONFATAL
, "user error: %s", p
);
1373 error(ERR_WARNING
, "user error: %s", p
);
1376 free_tlist (origline
);
1394 if (istk
->conds
&& !emitting(istk
->conds
->state
))
1397 j
= if_condition(tline
->next
, i
);
1398 tline
->next
= NULL
; /* it got freed */
1399 free_tlist (origline
);
1402 * Bogus expression in %if, but we should pretend
1403 * it was OK anyway, so that we don't get an error
1404 * cascade on the subsequent %else / %endif.
1408 j
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1410 cond
= nasm_malloc(sizeof(Cond
));
1411 cond
->next
= istk
->conds
;
1414 return (j
== COND_IF_TRUE
? 3 : 1);
1432 error(ERR_FATAL
, "`%s': no matching `%%if'",
1434 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1435 istk
->conds
->state
= COND_NEVER
;
1437 j
= if_condition(expand_mmac_params(tline
->next
), i
);
1438 tline
->next
= NULL
; /* it got freed */
1439 free_tlist (origline
);
1442 * The expression was bogus, but let's make
1443 * %endif not complain about missing %if
1447 istk
->conds
->state
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1449 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
1454 "trailing garbage after `%%else' ignored");
1457 "`%%else': no matching `%%if'");
1458 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1459 istk
->conds
->state
= COND_ELSE_FALSE
;
1461 istk
->conds
->state
= COND_ELSE_TRUE
;
1462 free_tlist (origline
);
1468 "trailing garbage after `%%endif' ignored");
1471 "`%%endif': no matching `%%if'");
1473 istk
->conds
= cond
->next
;
1475 free_tlist (origline
);
1482 "`%%%smacro': already defining a macro",
1483 (i
== PP_IMACRO
? "i" : ""));
1484 tline
= tline
->next
;
1486 if (!tok_type_(tline
, TOK_ID
)) {
1487 error (ERR_NONFATAL
,
1488 "`%%%smacro' expects a macro name",
1489 (i
== PP_IMACRO
? "i" : ""));
1492 defining
= nasm_malloc(sizeof(MMacro
));
1493 defining
->name
= nasm_strdup(tline
->text
);
1494 defining
->casesense
= (i
== PP_MACRO
);
1495 defining
->plus
= FALSE
;
1496 defining
->nolist
= FALSE
;
1497 defining
->in_progress
= FALSE
;
1498 defining
->rep_nest
= NULL
;
1499 tline
= tline
->next
;
1501 if (!tok_type_(tline
, TOK_NUMBER
)) {
1502 error (ERR_NONFATAL
,
1503 "`%%%smacro' expects a parameter count",
1504 (i
== PP_IMACRO
? "i" : ""));
1505 defining
->nparam_min
= defining
->nparam_max
= 0;
1507 defining
->nparam_min
= defining
->nparam_max
=
1508 readnum(tline
->text
, &j
);
1510 error (ERR_NONFATAL
,
1511 "unable to parse parameter count `%s'", tline
->text
);
1513 if (tline
&& tok_is_(tline
->next
, "-")) {
1514 tline
= tline
->next
->next
;
1515 if (tok_is_(tline
, "*"))
1516 defining
->nparam_max
= INT_MAX
;
1517 else if (!tok_type_(tline
, TOK_NUMBER
))
1518 error (ERR_NONFATAL
,
1519 "`%%%smacro' expects a parameter count after `-'",
1520 (i
== PP_IMACRO
? "i" : ""));
1522 defining
->nparam_max
= readnum(tline
->text
, &j
);
1524 error (ERR_NONFATAL
,
1525 "unable to parse parameter count `%s'",
1527 if (defining
->nparam_min
> defining
->nparam_max
)
1528 error (ERR_NONFATAL
,
1529 "minimum parameter count exceeds maximum");
1532 if (tline
&& tok_is_(tline
->next
, "+")) {
1533 tline
= tline
->next
;
1534 defining
->plus
= TRUE
;
1536 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1537 !nasm_stricmp(tline
->next
->text
, ".nolist"))
1539 tline
= tline
->next
;
1540 defining
->nolist
= TRUE
;
1542 mmac
= mmacros
[hash(defining
->name
)];
1544 if (!strcmp(mmac
->name
, defining
->name
) &&
1545 (mmac
->nparam_min
<=defining
->nparam_max
|| defining
->plus
) &&
1546 (defining
->nparam_min
<=mmac
->nparam_max
|| mmac
->plus
))
1549 "redefining multi-line macro `%s'", defining
->name
);
1555 * Handle default parameters.
1557 if (tline
&& tline
->next
) {
1558 defining
->dlist
= tline
->next
;
1560 count_mmac_params (defining
->dlist
, &defining
->ndefs
,
1561 &defining
->defaults
);
1563 defining
->dlist
= NULL
;
1564 defining
->defaults
= NULL
;
1566 defining
->expansion
= NULL
;
1567 free_tlist (origline
);
1573 error (ERR_NONFATAL
, "`%s': not defining a macro",
1577 k
= hash(defining
->name
);
1578 defining
->next
= mmacros
[k
];
1579 mmacros
[k
] = defining
;
1581 free_tlist (origline
);
1585 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1586 tline
= tline
->next
;
1587 t
= expand_smacro(tline
->next
);
1589 free_tlist (origline
);
1592 tokval
.t_type
= TOKEN_INVALID
;
1593 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1599 "trailing garbage after expression ignored");
1600 if (!is_simple(evalresult
)) {
1602 "non-constant value given to `%%rotate'");
1606 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
1607 mmac
= mmac
->next_active
;
1609 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
1610 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
1611 if (mmac
->rotate
< 0)
1612 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
1613 mmac
->rotate
%= mmac
->nparam
;
1618 tline
= tline
->next
;
1619 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1620 tline
= tline
->next
;
1621 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
1622 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1623 tline
= tline
->next
;
1626 t
= expand_smacro(tline
->next
);
1628 free_tlist (origline
);
1631 tokval
.t_type
= TOKEN_INVALID
;
1632 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1638 "trailing garbage after expression ignored");
1639 if (!is_simple(evalresult
)) {
1641 "non-constant value given to `%%rep'");
1644 tmp_defining
= defining
;
1645 defining
= nasm_malloc(sizeof(MMacro
));
1646 defining
->name
= NULL
; /* flags this macro as a %rep block */
1647 defining
->casesense
= 0;
1648 defining
->plus
= FALSE
;
1649 defining
->nolist
= nolist
;
1650 defining
->in_progress
= reloc_value(evalresult
) + 1;
1651 defining
->nparam_min
= defining
->nparam_max
= 0;
1652 defining
->defaults
= NULL
;
1653 defining
->dlist
= NULL
;
1654 defining
->expansion
= NULL
;
1655 defining
->next_active
= istk
->mstk
;
1656 defining
->rep_nest
= tmp_defining
;
1660 if (!defining
|| defining
->name
) {
1661 error (ERR_NONFATAL
,
1662 "`%%endrep': no matching `%%rep'");
1667 * Now we have a "macro" defined - although it has no name
1668 * and we won't be entering it in the hash tables - we must
1669 * push a macro-end marker for it on to istk->expansion.
1670 * After that, it will take care of propagating itself (a
1671 * macro-end marker line for a macro which is really a %rep
1672 * block will cause the macro to be re-expanded, complete
1673 * with another macro-end marker to ensure the process
1674 * continues) until the whole expansion is forcibly removed
1675 * from istk->expansion by a %exitrep.
1677 l
= nasm_malloc(sizeof(Line
));
1678 l
->next
= istk
->expansion
;
1679 l
->finishes
= defining
;
1681 istk
->expansion
= l
;
1683 istk
->mstk
= defining
;
1685 list
->uplevel (defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
1686 tmp_defining
= defining
;
1687 defining
= defining
->rep_nest
;
1688 free_tlist (origline
);
1693 * We must search along istk->expansion until we hit a
1694 * macro-end marker for a macro with no name. Then we set
1695 * its `in_progress' flag to 0.
1697 for (l
= istk
->expansion
; l
; l
= l
->next
)
1698 if (l
->finishes
&& !l
->finishes
->name
)
1702 l
->finishes
->in_progress
= 0;
1704 error (ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
1705 free_tlist (origline
);
1710 tline
= tline
->next
;
1712 if (!tline
|| (tline
->type
!= TOK_ID
&&
1713 (tline
->type
!= TOK_PREPROC_ID
||
1714 tline
->text
[1] != '$'))) {
1715 error (ERR_NONFATAL
,
1716 "`%%%sdefine' expects a macro identifier",
1717 (i
== PP_IDEFINE
? "i" : ""));
1718 free_tlist (origline
);
1721 mname
= tline
->text
;
1722 if (tline
->type
== TOK_ID
) {
1724 smhead
= &smacros
[hash(mname
)];
1726 ctx
= get_ctx (tline
->text
);
1731 p
+= strspn(p
, "$");
1732 smhead
= &ctx
->localmac
;
1736 param_start
= tline
= tline
->next
;
1738 if (tok_is_(tline
, "(")) {
1740 * This macro has parameters.
1743 tline
= tline
->next
;
1747 error (ERR_NONFATAL
,
1748 "parameter identifier expected");
1749 free_tlist (origline
);
1752 if (tline
->type
!= TOK_ID
) {
1753 error (ERR_NONFATAL
,
1754 "`%s': parameter identifier expected",
1756 free_tlist (origline
);
1759 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
1760 tline
= tline
->next
;
1762 if (tok_is_(tline
, ",")) {
1763 tline
= tline
->next
;
1766 if (!tok_is_(tline
, ")")) {
1767 error (ERR_NONFATAL
,
1768 "`)' expected to terminate macro template");
1769 free_tlist (origline
);
1775 tline
= tline
->next
;
1777 if (tok_type_(tline
, TOK_WHITESPACE
))
1778 last
= tline
, tline
= tline
->next
;
1783 if (t
->type
== TOK_ID
) {
1784 for (tt
= param_start
; tt
; tt
= tt
->next
)
1785 if (tt
->type
>= TOK_SMAC_PARAM
&&
1786 !strcmp(tt
->text
, t
->text
))
1790 t
->next
= macro_start
;
1795 * Good. We now have a macro name, a parameter count, and a
1796 * token list (in reverse order) for an expansion. We ought
1797 * to be OK just to create an SMacro, store it, and let
1798 * free_tlist have the rest of the line (which we have
1799 * carefully re-terminated after chopping off the expansion
1802 if (smacro_defined (mname
, nparam
, &smac
, i
==PP_DEFINE
)) {
1805 "single-line macro `%s' defined both with and"
1806 " without parameters", mname
);
1807 free_tlist (origline
);
1808 free_tlist (macro_start
);
1812 * We're redefining, so we have to take over an
1813 * existing SMacro structure. This means freeing
1814 * what was already in it.
1816 nasm_free (smac
->name
);
1817 free_tlist (smac
->expansion
);
1820 smac
= nasm_malloc(sizeof(SMacro
));
1821 smac
->next
= *smhead
;
1824 smac
->name
= nasm_strdup(p
);
1825 smac
->casesense
= (i
== PP_DEFINE
);
1826 smac
->nparam
= nparam
;
1827 smac
->expansion
= macro_start
;
1828 smac
->in_progress
= FALSE
;
1829 free_tlist (origline
);
1834 tline
= tline
->next
;
1836 if (!tline
|| (tline
->type
!= TOK_ID
&&
1837 (tline
->type
!= TOK_PREPROC_ID
||
1838 tline
->text
[1] != '$'))) {
1839 error (ERR_NONFATAL
,
1840 "`%%%sassign' expects a macro identifier",
1841 (i
== PP_IASSIGN
? "i" : ""));
1842 free_tlist (origline
);
1845 mname
= tline
->text
;
1846 if (tline
->type
== TOK_ID
) {
1848 smhead
= &smacros
[hash(mname
)];
1850 ctx
= get_ctx (tline
->text
);
1852 free_tlist (origline
);
1856 p
+= strspn(p
, "$");
1857 smhead
= &ctx
->localmac
;
1861 tline
= tline
->next
;
1864 tline
= expand_smacro (tline
);
1867 tokval
.t_type
= TOKEN_INVALID
;
1868 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1871 free_tlist (origline
);
1877 "trailing garbage after expression ignored");
1879 if (!is_simple(evalresult
)) {
1881 "non-constant value given to `%%%sassign'",
1882 (i
== PP_IASSIGN
? "i" : ""));
1883 free_tlist (origline
);
1887 macro_start
= nasm_malloc(sizeof(*macro_start
));
1888 macro_start
->next
= NULL
;
1889 make_tok_num(macro_start
, reloc_value(evalresult
));
1890 macro_start
->mac
= NULL
;
1893 * We now have a macro name, an implicit parameter count of
1894 * zero, and a numeric token to use as an expansion. Create
1895 * and store an SMacro.
1897 if (smacro_defined (mname
, 0, &smac
, i
==PP_ASSIGN
)) {
1900 "single-line macro `%s' defined both with and"
1901 " without parameters", mname
);
1904 * We're redefining, so we have to take over an
1905 * existing SMacro structure. This means freeing
1906 * what was already in it.
1908 nasm_free (smac
->name
);
1909 free_tlist (smac
->expansion
);
1913 smac
= nasm_malloc(sizeof(SMacro
));
1914 smac
->next
= *smhead
;
1917 smac
->name
= nasm_strdup(p
);
1918 smac
->casesense
= (i
== PP_ASSIGN
);
1920 smac
->expansion
= macro_start
;
1921 smac
->in_progress
= FALSE
;
1922 free_tlist (origline
);
1927 * Syntax is `%line nnn[+mmm] [filename]'
1929 tline
= tline
->next
;
1931 if (!tok_type_(tline
, TOK_NUMBER
)) {
1932 error (ERR_NONFATAL
, "`%%line' expects line number");
1933 free_tlist (origline
);
1936 k
= readnum(tline
->text
, &j
);
1938 tline
= tline
->next
;
1939 if (tok_is_(tline
, "+")) {
1940 tline
= tline
->next
;
1941 if (!tok_type_(tline
, TOK_NUMBER
)) {
1942 error (ERR_NONFATAL
,
1943 "`%%line' expects line increment");
1944 free_tlist (origline
);
1947 m
= readnum(tline
->text
, &j
);
1948 tline
= tline
->next
;
1954 nasm_free ( src_set_fname ( detoken(tline
) ) );
1956 free_tlist (origline
);
1961 "preprocessor directive `%s' not yet implemented",
1969 * Ensure that a macro parameter contains a condition code and
1970 * nothing else. Return the condition code index if so, or -1
1973 static int find_cc (Token
*t
)
1979 if (t
->type
!= TOK_ID
)
1983 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
1987 j
= sizeof(conditions
)/sizeof(*conditions
);
1990 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2006 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2007 * %-n) and MMacro-local identifiers (%%foo).
2009 static Token
*expand_mmac_params (Token
*tline
)
2011 Token
*t
, *tt
, *ttt
, **tail
, *thead
;
2017 if (tline
->type
== TOK_PREPROC_ID
&&
2018 (tline
->text
[1] == '+' || tline
->text
[1] == '-' ||
2019 tline
->text
[1] == '%' ||
2020 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2022 int type
= 0, cc
; /* type = 0 to placate optimisers */
2028 tline
= tline
->next
;
2031 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2032 mac
= mac
->next_active
;
2034 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2035 else switch (t
->text
[1]) {
2037 * We have to make a substitution of one of the
2038 * forms %1, %-1, %+1, %%foo, %0.
2042 sprintf(tmpbuf
, "%d", mac
->nparam
);
2043 text
= nasm_strdup(tmpbuf
);
2047 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
2048 text
= nasm_strcat(tmpbuf
, t
->text
+2);
2051 n
= atoi(t
->text
+2)-1;
2052 if (n
>= mac
->nparam
)
2055 if (mac
->nparam
> 1)
2056 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2057 tt
= mac
->params
[n
];
2061 error (ERR_NONFATAL
,
2062 "macro parameter %d is not a condition code",
2067 if (inverse_ccs
[cc
] == -1) {
2068 error (ERR_NONFATAL
,
2069 "condition code `%s' is not invertible",
2073 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2077 n
= atoi(t
->text
+2)-1;
2078 if (n
>= mac
->nparam
)
2081 if (mac
->nparam
> 1)
2082 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2083 tt
= mac
->params
[n
];
2087 error (ERR_NONFATAL
,
2088 "macro parameter %d is not a condition code",
2093 text
= nasm_strdup(conditions
[cc
]);
2097 n
= atoi(t
->text
+1)-1;
2098 if (n
>= mac
->nparam
)
2101 if (mac
->nparam
> 1)
2102 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2103 tt
= mac
->params
[n
];
2106 for (i
=0; i
<mac
->paramlen
[n
]; i
++) {
2107 ttt
= *tail
= nasm_malloc(sizeof(Token
));
2109 ttt
->type
= tt
->type
;
2110 ttt
->text
= nasm_strdup(tt
->text
);
2115 text
= NULL
; /* we've done it here */
2118 nasm_free (t
->text
);
2131 tline
= tline
->next
;
2138 for (; t
&& (tt
=t
->next
)!=NULL
; t
= t
->next
)
2140 case TOK_WHITESPACE
:
2141 if (tt
->type
== TOK_WHITESPACE
) {
2143 nasm_free(tt
->text
);
2148 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2149 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2153 nasm_free(tt
->text
);
2158 if (tt
->type
== TOK_NUMBER
) {
2159 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2163 nasm_free(tt
->text
);
2173 * Expand all single-line macro calls made in the given line.
2174 * Return the expanded version of the line. The original is deemed
2175 * to be destroyed in the process. (In reality we'll just move
2176 * Tokens from input to output a lot of the time, rather than
2177 * actually bothering to destroy and replicate.)
2179 static Token
*expand_smacro (Token
*tline
)
2181 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2182 SMacro
*head
= NULL
, *m
;
2185 int nparam
, sparam
, brackets
;
2191 while (tline
) { /* main token loop */
2193 if (tline
->type
== TOK_ID
) {
2194 head
= smacros
[hash(tline
->text
)];
2196 } else if (tline
->type
== TOK_PREPROC_ID
&& tline
->text
[1] == '$') {
2197 Context
*ctx
= get_ctx (tline
->text
);
2199 head
= ctx
->localmac
;
2201 p
+= strspn(p
, "$");
2206 * We've hit an identifier. As in is_mmacro below, we first
2207 * check whether the identifier is a single-line macro at
2208 * all, then think about checking for parameters if
2211 for (m
= head
; m
; m
= m
->next
)
2212 if (!mstrcmp(m
->name
, p
, m
->casesense
))
2218 if (m
->nparam
== 0) {
2220 * Simple case: the macro is parameterless. Discard the
2221 * one token that the macro call took, and push the
2222 * expansion back on the to-do stack.
2226 if (!strcmp("__FILE__", m
->name
)) {
2228 src_get(&num
, &(tline
->text
));
2229 nasm_quote(&(tline
->text
));
2230 tline
->type
= TOK_STRING
;
2233 if (!strcmp("__LINE__", m
->name
)) {
2234 nasm_free(tline
->text
);
2235 make_tok_num(tline
, src_get_linnum());
2239 tline
= tline
->next
;
2240 nasm_free (t
->text
);
2247 * Complicated case: at least one macro with this name
2248 * exists and takes parameters. We must find the
2249 * parameters in the call, count them, find the SMacro
2250 * that corresponds to that form of the macro call, and
2251 * substitute for the parameters when we expand. What a
2254 tline
= tline
->next
;
2256 if (!tok_is_(tline
, "(")) {
2258 * This macro wasn't called with parameters: ignore
2259 * the call. (Behaviour borrowed from gnu cpp.)
2269 tline
= tline
->next
;
2270 sparam
= PARAM_DELTA
;
2271 params
= nasm_malloc (sparam
*sizeof(Token
*));
2273 paramsize
= nasm_malloc (sparam
*sizeof(int));
2275 for (;;tline
= tline
->next
) { /* parameter loop */
2278 "macro call expects terminating `)'");
2281 if (tline
->type
== TOK_WHITESPACE
&& brackets
<=0) {
2282 if (paramsize
[nparam
])
2285 params
[nparam
] = tline
->next
;
2286 continue; /* parameter loop */
2288 if (tline
->type
== TOK_OTHER
&& tline
->text
[1]==0) {
2289 char ch
= tline
->text
[0];
2290 if (ch
== ',' && !paren
&& brackets
<=0) {
2291 if (++nparam
>= sparam
) {
2292 sparam
+= PARAM_DELTA
;
2293 params
= nasm_realloc (params
,
2294 sparam
*sizeof(Token
*));
2295 paramsize
= nasm_realloc (paramsize
,
2296 sparam
*sizeof(int));
2298 params
[nparam
] = tline
->next
;
2299 paramsize
[nparam
] = 0;
2301 continue; /* parameter loop */
2304 (brackets
>0 || (brackets
==0 &&
2305 !paramsize
[nparam
])))
2309 params
[nparam
] = tline
->next
;
2310 continue; /* parameter loop */
2313 if (ch
== '}' && brackets
>0)
2314 if (--brackets
== 0) {
2316 continue; /* parameter loop */
2318 if (ch
== '(' && !brackets
)
2320 if (ch
== ')' && brackets
<=0)
2326 error (ERR_NONFATAL
, "braces do not "
2327 "enclose all of macro parameter");
2329 paramsize
[nparam
] += white
+1;
2331 } /* parameter loop */
2333 while (m
&& (m
->nparam
!= nparam
||
2334 mstrcmp(m
->name
, p
, m
->casesense
)))
2337 error (ERR_WARNING
|ERR_WARN_MNP
,
2338 "macro `%s' exists, "
2339 "but not taking %d parameters",
2340 mstart
->text
, nparam
);
2343 if (m
&& m
->in_progress
)
2345 if (!m
) /* in progess or didn't find '(' or wrong nparam */
2348 * Design question: should we handle !tline, which
2349 * indicates missing ')' here, or expand those
2350 * macros anyway, which requires the (t) test a few
2354 nasm_free (paramsize
);
2359 * Expand the macro: we are placed on the last token of the
2360 * call, so that we can easily split the call from the
2361 * following tokens. We also start by pushing an SMAC_END
2362 * token for the cycle removal.
2369 tt
= nasm_malloc(sizeof(Token
));
2370 tt
->type
= TOK_SMAC_END
;
2373 m
->in_progress
= TRUE
;
2376 for (t
= m
->expansion
; t
; t
= t
->next
) {
2377 if (t
->type
>= TOK_SMAC_PARAM
) {
2378 Token
*pcopy
= tline
, **ptail
= &pcopy
;
2382 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
2383 for (i
=paramsize
[t
->type
-TOK_SMAC_PARAM
]; --i
>=0;) {
2384 pt
= *ptail
= nasm_malloc(sizeof(Token
));
2387 pt
->text
= nasm_strdup(ttt
->text
);
2388 pt
->type
= ttt
->type
;
2394 tt
= nasm_malloc(sizeof(Token
));
2396 tt
->text
= nasm_strdup(t
->text
);
2404 * Having done that, get rid of the macro call, and clean
2405 * up the parameters.
2408 nasm_free (paramsize
);
2409 free_tlist (mstart
);
2410 continue; /* main token loop */
2415 if (tline
->type
== TOK_SMAC_END
) {
2416 tline
->mac
->in_progress
= FALSE
;
2418 tline
= tline
->next
;
2422 tline
= tline
->next
;
2426 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '$') {
2427 Context
*c
= get_ctx (t
->text
);
2428 char *p
, *q
, buffer
[40];
2433 q
+= strspn(q
, "$");
2434 sprintf(buffer
, "..@%lu.", c
->number
);
2435 p
= nasm_strcat (buffer
,q
);
2436 nasm_free (t
->text
);
2447 * Determine whether the given line constitutes a multi-line macro
2448 * call, and return the MMacro structure called if so. Doesn't have
2449 * to check for an initial label - that's taken care of in
2450 * expand_mmacro - but must check numbers of parameters. Guaranteed
2451 * to be called with tline->type == TOK_ID, so the putative macro
2452 * name is easy to find.
2454 static MMacro
*is_mmacro (Token
*tline
, Token
***params_array
)
2460 head
= mmacros
[hash(tline
->text
)];
2463 * Efficiency: first we see if any macro exists with the given
2464 * name. If not, we can return NULL immediately. _Then_ we
2465 * count the parameters, and then we look further along the
2466 * list if necessary to find the proper MMacro.
2468 for (m
= head
; m
; m
= m
->next
)
2469 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2475 * OK, we have a potential macro. Count and demarcate the
2478 count_mmac_params (tline
->next
, &nparam
, ¶ms
);
2481 * So we know how many parameters we've got. Find the MMacro
2482 * structure that handles this number.
2485 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
)) {
2487 * This one is right. Just check if cycle removal
2488 * prohibits us using it before we actually celebrate...
2490 if (m
->in_progress
) {
2492 error (ERR_NONFATAL
,
2493 "self-reference in multi-line macro `%s'",
2500 * It's right, and we can use it. Add its default
2501 * parameters to the end of our list if necessary.
2503 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
2504 params
= nasm_realloc (params
, ((m
->nparam_min
+m
->ndefs
+1) *
2506 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
2507 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
2512 * If we've gone over the maximum parameter count (and
2513 * we're in Plus mode), ignore parameters beyond
2516 if (m
->plus
&& nparam
> m
->nparam_max
)
2517 nparam
= m
->nparam_max
;
2519 * Then terminate the parameter list, and leave.
2521 if (!params
) { /* need this special case */
2522 params
= nasm_malloc(sizeof(*params
));
2525 params
[nparam
] = NULL
;
2526 *params_array
= params
;
2530 * This one wasn't right: look for the next one with the
2533 for (m
= m
->next
; m
; m
= m
->next
)
2534 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2539 * After all that, we didn't find one with the right number of
2540 * parameters. Issue a warning, and fail to expand the macro.
2542 error (ERR_WARNING
|ERR_WARN_MNP
,
2543 "macro `%s' exists, but not taking %d parameters",
2544 tline
->text
, nparam
);
2550 * Expand the multi-line macro call made by the given line, if
2551 * there is one to be expanded. If there is, push the expansion on
2552 * istk->expansion and return 1. Otherwise return 0.
2554 static int expand_mmacro (Token
*tline
)
2556 Token
*startline
= tline
;
2557 Token
*label
= NULL
;
2558 int dont_prepend
= 0;
2559 Token
**params
, *t
, *tt
;
2562 int i
, nparam
, *paramlen
;
2566 if (!tok_type_(t
, TOK_ID
))
2568 m
= is_mmacro (t
, ¶ms
);
2572 * We have an id which isn't a macro call. We'll assume
2573 * it might be a label; we'll also check to see if a
2574 * colon follows it. Then, if there's another id after
2575 * that lot, we'll check it again for macro-hood.
2579 if (tok_type_(t
, TOK_WHITESPACE
))
2580 last
= t
, t
= t
->next
;
2581 if (tok_is_(t
, ":")) {
2583 last
= t
, t
= t
->next
;
2584 if (tok_type_(t
, TOK_WHITESPACE
))
2585 last
= t
, t
= t
->next
;
2587 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
2594 * Fix up the parameters: this involves stripping leading and
2595 * trailing whitespace, then stripping braces if they are
2598 for (nparam
= 0; params
[nparam
]; nparam
++)
2600 paramlen
= nparam
? nasm_malloc(nparam
*sizeof(*paramlen
)) : NULL
;
2602 for (i
= 0; params
[i
]; i
++) {
2604 int comma
= (!m
->plus
|| i
< nparam
-1);
2608 if (tok_is_(t
, "{"))
2609 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
2613 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
2614 break; /* ... because we have hit a comma */
2615 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
2616 break; /* ... or a space then a comma */
2617 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
2618 break; /* ... or a brace */
2625 * OK, we have a MMacro structure together with a set of
2626 * parameters. We must now go through the expansion and push
2627 * copies of each Line on to istk->expansion. Substitution of
2628 * parameter tokens and macro-local tokens doesn't get done
2629 * until the single-line macro substitution process; this is
2630 * because delaying them allows us to change the semantics
2631 * later through %rotate.
2633 * First, push an end marker on to istk->expansion, mark this
2634 * macro as in progress, and set up its invocation-specific
2637 ll
= nasm_malloc(sizeof(Line
));
2638 ll
->next
= istk
->expansion
;
2641 istk
->expansion
= ll
;
2643 m
->in_progress
= TRUE
;
2648 m
->paramlen
= paramlen
;
2649 m
->unique
= unique
++;
2651 m
->next_active
= istk
->mstk
;
2654 for (l
= m
->expansion
; l
; l
= l
->next
) {
2657 ll
= nasm_malloc(sizeof(Line
));
2658 ll
->finishes
= NULL
;
2659 ll
->next
= istk
->expansion
;
2660 istk
->expansion
= ll
;
2663 for (t
= l
->first
; t
; t
= t
->next
) {
2665 if (t
->type
== TOK_PREPROC_ID
&&
2666 t
->text
[1]=='0' && t
->text
[2]=='0')
2673 tt
= *tail
= nasm_malloc(sizeof(Token
));
2676 tt
->text
= nasm_strdup(x
->text
);
2683 * If we had a label, push it on as the first line of
2684 * the macro expansion.
2688 free_tlist(startline
);
2690 ll
= nasm_malloc(sizeof(Line
));
2691 ll
->finishes
= NULL
;
2692 ll
->next
= istk
->expansion
;
2693 istk
->expansion
= ll
;
2694 ll
->first
= startline
;
2695 if (!dont_prepend
) {
2697 label
= label
->next
;
2698 label
->next
= tt
= nasm_malloc(sizeof(Token
));
2701 tt
->type
= TOK_OTHER
;
2702 tt
->text
= nasm_strdup(":");
2706 list
->uplevel (m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2711 static void pp_reset (char *file
, int apass
, efunc errfunc
, evalfunc eval
,
2718 istk
= nasm_malloc(sizeof(Include
));
2721 istk
->expansion
= NULL
;
2723 istk
->fp
= fopen(file
, "r");
2725 src_set_fname(nasm_strdup(file
));
2729 error (ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'", file
);
2731 for (h
=0; h
<NHASH
; h
++) {
2737 any_extrastdmac
= (extrastdmac
!= NULL
);
2743 static char *pp_getline (void)
2751 * Fetch a tokenised line, either from the macro-expansion
2752 * buffer or from the input file.
2755 while (istk
->expansion
&& istk
->expansion
->finishes
) {
2756 Line
*l
= istk
->expansion
;
2757 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
2761 * This is a macro-end marker for a macro with no
2762 * name, which means it's not really a macro at all
2763 * but a %rep block, and the `in_progress' field is
2764 * more than 1, meaning that we still need to
2765 * repeat. (1 means the natural last repetition; 0
2766 * means termination by %exitrep.) We have
2767 * therefore expanded up to the %endrep, and must
2768 * push the whole block on to the expansion buffer
2769 * again. We don't bother to remove the macro-end
2770 * marker: we'd only have to generate another one
2773 l
->finishes
->in_progress
--;
2774 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
2775 Token
*t
, *tt
, **tail
;
2777 ll
= nasm_malloc(sizeof(Line
));
2778 ll
->next
= istk
->expansion
;
2779 ll
->finishes
= NULL
;
2783 for (t
= l
->first
; t
; t
= t
->next
) {
2785 tt
= *tail
= nasm_malloc(sizeof(Token
));
2789 tt
->text
= nasm_strdup(t
->text
);
2794 istk
->expansion
= ll
;
2798 * Check whether a `%rep' was started and not ended
2799 * within this macro expansion. This can happen and
2800 * should be detected. It's a fatal error because
2801 * I'm too confused to work out how to recover
2807 "defining with name in expansion");
2808 else if (istk
->mstk
->name
)
2809 error (ERR_FATAL
, "`%%rep' without `%%endrep' within"
2810 " expansion of macro `%s'", istk
->mstk
->name
);
2814 * FIXME: investigate the relationship at this point between
2815 * istk->mstk and l->finishes
2818 MMacro
*m
= istk
->mstk
;
2819 istk
->mstk
= m
->next_active
;
2822 * This was a real macro call, not a %rep, and
2823 * therefore the parameter information needs to
2826 nasm_free(m
->params
);
2827 free_tlist(m
->iline
);
2828 nasm_free(m
->paramlen
);
2829 l
->finishes
->in_progress
= FALSE
;
2834 istk
->expansion
= l
->next
;
2836 list
->downlevel (LIST_MACRO
);
2839 while (1) { /* until we get a line we can use */
2841 if (istk
->expansion
) { /* from a macro expansion */
2843 Line
*l
= istk
->expansion
;
2845 istk
->expansion
= l
->next
;
2848 list
->line (LIST_MACRO
, p
);
2853 if (line
) { /* from the current input file */
2854 line
= prepreproc(line
);
2855 tline
= tokenise(line
);
2860 * The current file has ended; work down the istk
2866 error(ERR_FATAL
, "expected `%%endif' before end of file");
2868 list
->downlevel (LIST_INCLUDE
);
2869 src_set_linnum(i
->lineno
);
2870 nasm_free ( src_set_fname(i
->fname
) );
2878 * We must expand MMacro parameters and MMacro-local labels
2879 * _before_ we plunge into directive processing, to cope
2880 * with things like `%define something %1' such as STRUC
2881 * uses. Unless we're _defining_ a MMacro, in which case
2882 * those tokens should be left alone to go into the
2883 * definition; and unless we're in a non-emitting
2884 * condition, in which case we don't want to meddle with
2887 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
2888 tline
= expand_mmac_params(tline
);
2891 * Check the line to see if it's a preprocessor directive.
2893 ret
= do_directive(tline
);
2896 } else if (defining
) {
2898 * We're defining a multi-line macro. We emit nothing
2900 * shove the tokenised line on to the macro definition.
2902 Line
*l
= nasm_malloc(sizeof(Line
));
2903 l
->next
= defining
->expansion
;
2905 l
->finishes
= FALSE
;
2906 defining
->expansion
= l
;
2908 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
2910 * We're in a non-emitting branch of a condition block.
2911 * Emit nothing at all, not even a blank line: when we
2912 * emerge from the condition we'll give a line-number
2913 * directive so we keep our place correctly.
2917 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
2919 * We're in a %rep block which has been terminated, so
2920 * we're walking through to the %endrep without
2921 * emitting anything. Emit nothing at all, not even a
2922 * blank line: when we emerge from the %rep block we'll
2923 * give a line-number directive so we keep our place
2929 tline
= expand_smacro(tline
);
2930 ret
= expand_mmacro(tline
);
2933 * De-tokenise the line again, and emit it.
2935 line
= detoken(tline
);
2939 continue; /* expand_mmacro calls free_tlist */
2947 static void pp_cleanup (void)
2952 error (ERR_NONFATAL
, "end of file while still defining macro `%s'",
2954 free_mmacro (defining
);
2958 for (h
=0; h
<NHASH
; h
++) {
2959 while (mmacros
[h
]) {
2960 MMacro
*m
= mmacros
[h
];
2961 mmacros
[h
] = mmacros
[h
]->next
;
2964 while (smacros
[h
]) {
2965 SMacro
*s
= smacros
[h
];
2966 smacros
[h
] = smacros
[h
]->next
;
2967 nasm_free (s
->name
);
2968 free_tlist (s
->expansion
);
2976 nasm_free (i
->fname
);
2983 void pp_include_path (char *path
)
2987 i
= nasm_malloc(sizeof(IncPath
));
2988 i
->path
= nasm_strdup(path
);
2993 void pp_pre_include (char *fname
)
2995 Token
*inc
, *space
, *name
;
2998 inc
= nasm_malloc(sizeof(Token
));
2999 inc
->next
= space
= nasm_malloc(sizeof(Token
));
3000 space
->next
= name
= nasm_malloc(sizeof(Token
));
3003 inc
->type
= TOK_PREPROC_ID
;
3004 inc
->text
= nasm_strdup("%include");
3005 space
->type
= TOK_WHITESPACE
;
3006 space
->text
= nasm_strdup(" ");
3007 name
->type
= TOK_INTERNAL_STRING
;
3008 name
->text
= nasm_strdup(fname
);
3010 inc
->mac
= space
->mac
= name
->mac
= NULL
;
3012 l
= nasm_malloc(sizeof(Line
));
3015 l
->finishes
= FALSE
;
3019 void pp_pre_define (char *definition
)
3025 equals
= strchr(definition
, '=');
3027 def
= nasm_malloc(sizeof(Token
));
3028 def
->next
= space
= nasm_malloc(sizeof(Token
));
3031 space
->next
= tokenise(definition
);
3035 def
->type
= TOK_PREPROC_ID
;
3036 def
->text
= nasm_strdup("%define");
3037 space
->type
= TOK_WHITESPACE
;
3038 space
->text
= nasm_strdup(" ");
3040 def
->mac
= space
->mac
= NULL
;
3042 l
= nasm_malloc(sizeof(Line
));
3045 l
->finishes
= FALSE
;
3049 void pp_extra_stdmac (char **macros
)
3051 extrastdmac
= macros
;
3054 static void make_tok_num(Token
*tok
, long val
)
3057 sprintf(numbuf
, "%ld", val
);
3058 tok
->text
= nasm_strdup(numbuf
);
3059 tok
->type
= TOK_NUMBER
;