NASM 0.98p3.5
[nasm.git] / preproc.c
blob44609075dcfd0d2b677fe84eb7cfafda9cf4bf57
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
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenised lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
21 * }
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
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stddef.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <limits.h>
43 #include "nasm.h"
44 #include "nasmlib.h"
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.
58 struct SMacro {
59 SMacro *next;
60 char *name;
61 int casesense;
62 int nparam;
63 int in_progress;
64 Token *expansion;
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
76 * run.
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.
84 struct MMacro {
85 MMacro *next;
86 char *name;
87 int casesense;
88 int nparam_min, nparam_max;
89 int plus; /* is the last parameter greedy? */
90 int nolist; /* is this macro listing-inhibited? */
91 int in_progress;
92 Token *dlist; /* All defaults as one list */
93 Token **defaults; /* Parameter default pointers */
94 int ndefs; /* number of default parameters */
95 Line *expansion;
97 MMacro *next_active;
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.
108 struct Context {
109 Context *next;
110 SMacro *localmac;
111 char *name;
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
127 * TOK_SMAC_PARAM+1.
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.
134 struct Token {
135 Token *next;
136 char *text;
137 SMacro *mac; /* associated macro for TOK_SMAC_END */
138 int type;
140 enum {
141 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
142 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
143 TOK_INTERNAL_STRING
147 * Multi-line macro definitions are stored as a linked list of
148 * these, which is essentially a container to allow several linked
149 * lists of Tokens.
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
166 * the line is blank.
168 struct Line {
169 Line *next;
170 MMacro *finishes;
171 Token *first;
175 * To handle an arbitrary level of file inclusion, we maintain a
176 * stack (ie linked list) of these things.
178 struct Include {
179 Include *next;
180 FILE *fp;
181 Cond *conds;
182 Line *expansion;
183 char *fname;
184 int lineno, lineinc;
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.
193 struct IncPath {
194 IncPath *next;
195 char *path;
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.)
205 struct Cond {
206 Cond *next;
207 int state;
209 enum {
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.
233 COND_NEVER
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"
248 enum {
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
260 * Directive names.
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"
273 enum {
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;
290 static efunc error;
291 static evalfunc evaluate;
293 static int pass;
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.
306 #define NHASH 31
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.
333 #include "macros.c"
334 static char **stdmacpos;
337 * The extra standard macros that come from the object format, if
338 * any.
340 static char **extrastdmac = NULL;
341 int any_extrastdmac;
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
362 * lineno file').
364 static char *prepreproc(char *line)
366 int lineno, fnlen;
367 char *fname, *oldline;
369 if (line[0] == '#' && line[1] == ' ') {
370 oldline = line;
371 fname = oldline+2;
372 lineno = atoi(fname);
373 fname += strspn(fname, "0123456789 ");
374 if (*fname == '"')
375 fname++;
376 fnlen = strcspn(fname, "\"");
377 line = nasm_malloc(20+fnlen);
378 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
379 nasm_free (oldline);
381 return line;
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)
392 unsigned int h = 0;
393 int i = 0;
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
403 while (*s) {
404 h += multipliers[i] * (unsigned char) (toupper(*s));
405 s++;
406 if (++i >= sizeof(multipliers)/sizeof(*multipliers))
407 i = 0;
409 h %= NHASH;
410 return h;
414 * Free a linked list of tokens.
416 static void free_tlist (Token *list)
418 Token *t;
419 while (list) {
420 t = list;
421 list = list->next;
422 nasm_free (t->text);
423 nasm_free (t);
428 * Free a linked list of lines.
430 static void free_llist (Line *list)
432 Line *l;
433 while (list) {
434 l = list;
435 list = list->next;
436 free_tlist (l->first);
437 nasm_free (l);
442 * Free an MMacro
444 static void free_mmacro (MMacro *m)
446 nasm_free (m->name);
447 free_tlist (m->dlist);
448 nasm_free (m->defaults);
449 free_llist (m->expansion);
450 nasm_free (m);
454 * Pop the context stack.
456 static void ctx_pop (void)
458 Context *c = cstk;
459 SMacro *smac, *s;
461 cstk = cstk->next;
462 smac = c->localmac;
463 while (smac) {
464 s = smac;
465 smac = smac->next;
466 nasm_free (s->name);
467 free_tlist (s->expansion);
468 nasm_free (s);
470 nasm_free (c->name);
471 nasm_free (c);
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
479 * been done.
481 static char *read_line (void)
483 char *buffer, *p, *q;
484 int bufsize;
486 if (stdmacpos) {
487 if (*stdmacpos) {
488 char *ret = nasm_strdup(*stdmacpos++);
489 if (!*stdmacpos && any_extrastdmac)
491 stdmacpos = extrastdmac;
492 any_extrastdmac = FALSE;
493 return ret;
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.
501 if (!*stdmacpos)
503 Line *pd, *l;
504 Token *head, **tail, *t, *tt;
506 for (pd = predef; pd; pd = pd->next) {
507 head = NULL;
508 tail = &head;
509 for (t = pd->first; t; t = t->next) {
510 tt = *tail = nasm_malloc(sizeof(Token));
511 tt->next = NULL;
512 tail = &tt->next;
513 tt->type = t->type;
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;
519 l->first = head;
520 l->finishes = FALSE;
521 istk->expansion = l;
524 return ret;
526 else {
527 stdmacpos = NULL;
531 bufsize = BUF_DELTA;
532 buffer = nasm_malloc(BUF_DELTA);
533 p = buffer;
534 while (1) {
535 q = fgets(p, bufsize-(p-buffer), istk->fp);
536 if (!q)
537 break;
538 p += strlen(p);
539 if (p > buffer && p[-1] == '\n') {
540 break;
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) {
551 nasm_free (buffer);
552 return NULL;
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'))
562 *p = '\0';
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);
572 return 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)
582 char *p = line;
583 int type;
584 Token *list = NULL;
585 Token *t, **tail = &list;
587 while (*line) {
588 p = line;
589 if (*p == '%' && ( isdigit(p[1]) ||
590 ((p[1] == '-' || p[1] == '+') && isdigit(p[2]))))
592 p++;
593 do {
594 p++;
595 } while (isdigit(*p));
596 type = TOK_PREPROC_ID;
598 else if (*p == '%' && p[1] == '{') {
599 p += 2;
600 while (*p && *p != '}') {
601 p[-1] = *p;
602 p++;
604 p[-1] = '\0';
605 if (*p) p++;
606 type = TOK_PREPROC_ID;
608 else if (*p == '%' && (isidchar(p[1]) ||
609 ((p[1] == '!' || p[1] == '%' || p[1] == '$') &&
610 isidchar(p[2]))))
612 p++;
613 do {
614 p++;
615 } while (isidchar(*p));
616 type = TOK_PREPROC_ID;
618 else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
619 type = TOK_ID;
620 p++;
621 while (*p && isidchar(*p))
622 p++;
624 else if (*p == '\'' || *p == '"') {
626 * A string token.
628 char c = *p;
629 p++;
630 type = TOK_STRING;
631 while (*p && *p != c)
632 p++;
633 if (*p) p++;
635 else if (isnumstart(*p)) {
637 * A number token.
639 type = TOK_NUMBER;
640 p++;
641 while (*p && isnumchar(*p))
642 p++;
644 else if (isspace(*p)) {
645 type = TOK_WHITESPACE;
646 p++;
647 while (*p && isspace(*p))
648 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 == ';') {
655 type = TOK_COMMENT;
656 while (*p) p++;
659 else if (*p == ';') {
660 type = TOK_COMMENT;
661 while (*p) p++;
663 else {
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.
670 type = TOK_OTHER;
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] == '^'))
684 p++;
686 p++;
688 if (type != TOK_COMMENT) {
689 *tail = t = nasm_malloc (sizeof(Token));
690 tail = &t->next;
691 t->next = NULL;
692 t->type = type;
693 t->text = nasm_malloc(1+p-line);
694 strncpy(t->text, line, p-line);
695 t->text[p-line] = '\0';
697 line = p;
700 return list;
704 * Convert a line of tokens back into text.
706 char *detoken (Token *tlist)
708 Token *t;
709 int len;
710 char *line, *p;
712 len = 0;
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);
716 nasm_free (t->text);
717 if (p)
718 t->text = nasm_strdup(p);
719 else
720 t->text = NULL;
722 if (t->text)
723 len += strlen(t->text);
725 p = line = nasm_malloc(len+1);
726 for (t = tlist; t; t = t->next) {
727 if (t->text) {
728 strcpy (p, t->text);
729 p += strlen(p);
732 *p = '\0';
733 return line;
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
740 * field.
742 static int ppscan(void *private_data, struct tokenval *tokval)
744 Token **tlineptr = private_data;
745 Token *tline;
747 do {
748 tline = *tlineptr;
749 *tlineptr = tline ? tline->next : NULL;
750 } while (tline && (tline->type == TOK_WHITESPACE ||
751 tline->type == TOK_COMMENT));
753 if (!tline)
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] == '$') {
764 tokval->t_charptr++;
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) {
779 int rn_error;
781 tokval->t_integer = readnum(tline->text, &rn_error);
782 if (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) {
789 int rn_warn;
790 char q, *r;
791 int l;
793 r = tline->text;
794 q = *r++;
795 l = strlen(r);
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);
800 if (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
824 * the token text.
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 $
833 * signs.
835 static Context *get_ctx (char *name)
837 Context *ctx;
838 int i;
840 if (!cstk) {
841 error (ERR_NONFATAL, "`%s': context stack is empty", name);
842 return NULL;
845 i = 1;
846 ctx = cstk;
847 while (name[i+1] == '$') {
848 i++;
849 ctx = ctx->next;
850 if (!ctx) {
851 error (ERR_NONFATAL, "`%s': context stack is only"
852 " %d level%s deep", name, i-1, (i==2 ? "" : "s"));
853 return NULL;
856 return ctx;
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)
878 FILE *fp;
879 char *prefix = "", *combine;
880 IncPath *ip = ipath;
882 while (1) {
883 combine = nasm_strcat(prefix,file);
884 fp = fopen(combine, "r");
885 nasm_free (combine);
886 if (fp)
887 return fp;
888 if (!ip)
889 break;
890 prefix = ip->path;
891 ip = ip->next;
894 error (ERR_FATAL,
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
910 * error will occur.
912 * Note that this is also called with nparam zero to resolve
913 * `ifdef'.
915 static int smacro_defined (char *name, int nparam, SMacro **defn, int nocase)
917 SMacro *m;
918 Context *ctx;
919 char *p;
921 if (name[0] == '%' && name[1] == '$') {
922 ctx = get_ctx (name);
923 if (!ctx)
924 return FALSE; /* got to return _something_ */
925 m = ctx->localmac;
926 p = name+1;
927 p += strspn(p, "$");
928 } else {
929 m = smacros[hash(name)];
930 p = name;
933 while (m) {
934 if (!mstrcmp(m->name, p, m->casesense & nocase) &&
935 (nparam == 0 || m->nparam == 0 || nparam == m->nparam)) {
936 if (defn) {
937 if (nparam == m->nparam)
938 *defn = m;
939 else
940 *defn = NULL;
942 return TRUE;
944 m = m->next;
946 return FALSE;
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;
960 *params = NULL;
961 while (t) {
962 if (*nparam >= paramsize) {
963 paramsize += PARAM_DELTA;
964 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
966 skip_white_(t);
967 brace = FALSE;
968 if (tok_is_(t, "{"))
969 brace = TRUE;
970 (*params)[(*nparam)++] = t;
971 while (tok_isnt_(t, brace ? "}" : ","))
972 t = t->next;
973 if (t) { /* got a comma/brace */
974 t = t->next;
975 if (brace) {
977 * Now we've found the closing brace, look further
978 * for the comma.
980 skip_white_(t);
981 if (tok_isnt_(t, ",")) {
982 error (ERR_NONFATAL,
983 "braces do not enclose all of macro parameter");
984 while (tok_isnt_(t, ","))
985 t = t->next;
987 if (t)
988 t = t->next; /* eat the comma */
995 * Determine whether one of the various `if' conditions is true or
996 * not.
998 * We must free the tline we get passed.
1000 static int if_condition (Token *tline, int i)
1002 int j, casesense;
1003 Token * t, * tt, ** tptr, * origline;
1004 struct tokenval tokval;
1005 expr * evalresult;
1007 origline = tline;
1009 switch (i) {
1010 case PP_IFCTX: case PP_ELIFCTX:
1011 case PP_IFNCTX: case PP_ELIFNCTX:
1012 j = FALSE; /* have we matched yet? */
1013 if (!cstk)
1014 error(ERR_FATAL,
1015 "`%s': context stack is empty", directives[i]);
1016 else while (tline) {
1017 skip_white_(tline);
1018 if (!tline || tline->type != TOK_ID) {
1019 error(ERR_NONFATAL,
1020 "`%s' expects context identifiers", directives[i]);
1021 free_tlist (origline);
1022 return -1;
1024 if (!nasm_stricmp(tline->text, cstk->name))
1025 j = TRUE;
1026 tline = tline->next;
1028 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1029 j = !j;
1030 free_tlist (origline);
1031 return j;
1033 case PP_IFDEF: case PP_ELIFDEF:
1034 case PP_IFNDEF: case PP_ELIFNDEF:
1035 j = FALSE; /* have we matched yet? */
1036 while (tline) {
1037 skip_white_(tline);
1038 if (!tline || (tline->type != TOK_ID &&
1039 (tline->type != TOK_PREPROC_ID ||
1040 tline->text[1] != '$'))) {
1041 error(ERR_NONFATAL,
1042 "`%%if%sdef' expects macro identifiers",
1043 (i==PP_ELIFNDEF ? "n" : ""));
1044 free_tlist (origline);
1045 return -1;
1047 if (smacro_defined(tline->text, 0, NULL, 1))
1048 j = TRUE;
1049 tline = tline->next;
1051 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1052 j = !j;
1053 free_tlist (origline);
1054 return j;
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);
1059 t = tt = tline;
1060 while (tok_isnt_(tt, ","))
1061 tt = tt->next;
1062 if (!tt) {
1063 error(ERR_NONFATAL, "`%s' expects two comma-separated arguments");
1064 free_tlist (tline);
1065 return -1;
1067 tt = tt->next;
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",
1074 directives[i]);
1075 free_tlist (tline);
1076 return -1;
1078 if (t->type == TOK_WHITESPACE) {
1079 t = t->next;
1080 continue;
1081 } else if (tt->type == TOK_WHITESPACE) {
1082 tt = tt->next;
1083 continue;
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 */
1088 break;
1089 } else {
1090 t = t->next;
1091 tt = tt->next;
1092 continue;
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)
1099 j = !j;
1100 free_tlist (tline);
1101 return j;
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);
1107 t = tline;
1108 while (tok_type_(t, TOK_WHITESPACE))
1109 t = t->next;
1110 j = FALSE; /* placate optimiser */
1111 if (t) switch (i) {
1112 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1113 j = (t->type == TOK_ID);
1114 break;
1115 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1116 j = (t->type == TOK_NUMBER);
1117 break;
1118 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1119 j = (t->type == TOK_STRING);
1120 break;
1122 if (i == PP_IFNID || i == PP_ELIFNID ||
1123 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1124 i == PP_IFNSTR || i == PP_ELIFNSTR)
1125 j = !j;
1126 free_tlist (tline);
1127 return j;
1129 case PP_IF: case PP_ELIF:
1130 t = tline = expand_smacro(tline);
1131 tptr = &t;
1132 tokval.t_type = TOKEN_INVALID;
1133 evalresult = evaluate (ppscan, tptr, &tokval,
1134 NULL, pass | 0x10, error, NULL);
1135 free_tlist (tline);
1136 if (!evalresult)
1137 return -1;
1138 if (tokval.t_type)
1139 error(ERR_WARNING,
1140 "trailing garbage after expression ignored");
1141 if (!is_simple(evalresult)) {
1142 error(ERR_NONFATAL,
1143 "non-constant value given to `%s'", directives[i]);
1144 return -1;
1146 return reloc_value(evalresult) != 0;
1148 default:
1149 error(ERR_FATAL,
1150 "preprocessor directive `%s' not yet implemented",
1151 directives[i]);
1152 free_tlist (origline);
1153 return -1; /* yeah, right */
1158 * Find out if a line contains a preprocessor directive, and deal
1159 * with it if so.
1161 * If a directive _is_ found, we are expected to free_tlist() the
1162 * line.
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;
1171 char *p, *mname;
1172 Include *inc;
1173 Context *ctx;
1174 Cond *cond;
1175 SMacro *smac, **smhead;
1176 MMacro *mmac;
1177 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1178 Line *l;
1179 struct tokenval tokval;
1180 expr *evalresult;
1181 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1183 origline = tline;
1185 skip_white_(tline);
1186 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1187 (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!'))
1188 return 0;
1190 i = -1;
1191 j = sizeof(directives)/sizeof(*directives);
1192 while (j-i > 1) {
1193 k = (j+i) / 2;
1194 m = nasm_stricmp(tline->text, directives[k]);
1195 if (m == 0) {
1196 i = k;
1197 j = -2;
1198 break;
1199 } else if (m < 0) {
1200 j = k;
1201 } else
1202 i = 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
1209 * directives.
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)
1230 return 0;
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)))
1244 return 0;
1247 if (j != -2) {
1248 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1249 tline->text);
1250 return 0; /* didn't get it */
1253 switch (i) {
1255 case PP_CLEAR:
1256 if (tline->next)
1257 error(ERR_WARNING,
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;
1263 free_mmacro(m);
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);
1270 nasm_free (s);
1273 free_tlist (origline);
1274 return 3;
1276 case PP_INCLUDE:
1277 tline = tline->next;
1278 skip_white_(tline);
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_ */
1286 if (tline->next)
1287 error(ERR_WARNING,
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 */
1292 } else
1293 p = tline->text; /* internal_string is easier */
1294 inc = nasm_malloc(sizeof(Include));
1295 inc->next = istk;
1296 inc->conds = NULL;
1297 inc->fp = inc_fopen(p);
1298 inc->fname = src_set_fname(nasm_strdup(p));
1299 inc->lineno = src_set_linnum(0);
1300 inc->lineinc = 1;
1301 inc->expansion = NULL;
1302 inc->mstk = NULL;
1303 istk = inc;
1304 list->uplevel (LIST_INCLUDE);
1305 free_tlist (origline);
1306 return 5;
1308 case PP_PUSH:
1309 tline = tline->next;
1310 skip_white_(tline);
1311 if (!tok_type_(tline, TOK_ID)) {
1312 error(ERR_NONFATAL,
1313 "`%%push' expects a context identifier");
1314 free_tlist (origline);
1315 return 3; /* but we did _something_ */
1317 if (tline->next)
1318 error(ERR_WARNING,
1319 "trailing garbage after `%%push' ignored");
1320 ctx = nasm_malloc(sizeof(Context));
1321 ctx->next = cstk;
1322 ctx->localmac = NULL;
1323 ctx->name = nasm_strdup(tline->text);
1324 ctx->number = unique++;
1325 cstk = ctx;
1326 free_tlist (origline);
1327 break;
1329 case PP_REPL:
1330 tline = tline->next;
1331 skip_white_(tline);
1332 if (!tok_type_(tline, TOK_ID)) {
1333 error(ERR_NONFATAL,
1334 "`%%repl' expects a context identifier");
1335 free_tlist (origline);
1336 return 3; /* but we did _something_ */
1338 if (tline->next)
1339 error(ERR_WARNING,
1340 "trailing garbage after `%%repl' ignored");
1341 if (!cstk)
1342 error(ERR_NONFATAL,
1343 "`%%repl': context stack is empty");
1344 else {
1345 nasm_free (cstk->name);
1346 cstk->name = nasm_strdup(tline->text);
1348 free_tlist (origline);
1349 break;
1351 case PP_POP:
1352 if (tline->next)
1353 error(ERR_WARNING,
1354 "trailing garbage after `%%pop' ignored");
1355 if (!cstk)
1356 error(ERR_NONFATAL,
1357 "`%%pop': context stack is already empty");
1358 else
1359 ctx_pop();
1360 free_tlist (origline);
1361 break;
1363 case PP_ERROR:
1364 tline->next = expand_smacro (tline->next);
1365 tline = tline->next;
1366 skip_white_(tline);
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);
1371 } else {
1372 p = detoken(tline);
1373 error(ERR_WARNING, "user error: %s", p);
1374 nasm_free(p);
1376 free_tlist (origline);
1377 break;
1379 case PP_IF:
1380 case PP_IFCTX:
1381 case PP_IFDEF:
1382 case PP_IFID:
1383 case PP_IFIDN:
1384 case PP_IFIDNI:
1385 case PP_IFNCTX:
1386 case PP_IFNDEF:
1387 case PP_IFNID:
1388 case PP_IFNIDN:
1389 case PP_IFNIDNI:
1390 case PP_IFNNUM:
1391 case PP_IFNSTR:
1392 case PP_IFNUM:
1393 case PP_IFSTR:
1394 if (istk->conds && !emitting(istk->conds->state))
1395 j = COND_NEVER;
1396 else {
1397 j = if_condition(tline->next, i);
1398 tline->next = NULL; /* it got freed */
1399 free_tlist (origline);
1400 if (j < 0)
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.
1406 j = COND_NEVER;
1407 else
1408 j = j ? COND_IF_TRUE : COND_IF_FALSE;
1410 cond = nasm_malloc(sizeof(Cond));
1411 cond->next = istk->conds;
1412 cond->state = j;
1413 istk->conds = cond;
1414 return (j == COND_IF_TRUE ? 3 : 1);
1416 case PP_ELIF:
1417 case PP_ELIFCTX:
1418 case PP_ELIFDEF:
1419 case PP_ELIFID:
1420 case PP_ELIFIDN:
1421 case PP_ELIFIDNI:
1422 case PP_ELIFNCTX:
1423 case PP_ELIFNDEF:
1424 case PP_ELIFNID:
1425 case PP_ELIFNIDN:
1426 case PP_ELIFNIDNI:
1427 case PP_ELIFNNUM:
1428 case PP_ELIFNSTR:
1429 case PP_ELIFNUM:
1430 case PP_ELIFSTR:
1431 if (!istk->conds)
1432 error(ERR_FATAL, "`%s': no matching `%%if'",
1433 directives[i]);
1434 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1435 istk->conds->state = COND_NEVER;
1436 else {
1437 j = if_condition(expand_mmac_params(tline->next), i);
1438 tline->next = NULL; /* it got freed */
1439 free_tlist (origline);
1440 if (j < 0)
1442 * The expression was bogus, but let's make
1443 * %endif not complain about missing %if
1445 j = COND_NEVER;
1446 else
1447 istk->conds->state = j ? COND_IF_TRUE : COND_IF_FALSE;
1449 return (istk->conds->state == COND_IF_TRUE ? 5 : 1);
1451 case PP_ELSE:
1452 if (tline->next)
1453 error(ERR_WARNING,
1454 "trailing garbage after `%%else' ignored");
1455 if (!istk->conds)
1456 error(ERR_FATAL,
1457 "`%%else': no matching `%%if'");
1458 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1459 istk->conds->state = COND_ELSE_FALSE;
1460 else
1461 istk->conds->state = COND_ELSE_TRUE;
1462 free_tlist (origline);
1463 return 5;
1465 case PP_ENDIF:
1466 if (tline->next)
1467 error(ERR_WARNING,
1468 "trailing garbage after `%%endif' ignored");
1469 if (!istk->conds)
1470 error(ERR_FATAL,
1471 "`%%endif': no matching `%%if'");
1472 cond = istk->conds;
1473 istk->conds = cond->next;
1474 nasm_free (cond);
1475 free_tlist (origline);
1476 return 5;
1478 case PP_MACRO:
1479 case PP_IMACRO:
1480 if (defining)
1481 error (ERR_FATAL,
1482 "`%%%smacro': already defining a macro",
1483 (i == PP_IMACRO ? "i" : ""));
1484 tline = tline->next;
1485 skip_white_(tline);
1486 if (!tok_type_(tline, TOK_ID)) {
1487 error (ERR_NONFATAL,
1488 "`%%%smacro' expects a macro name",
1489 (i == PP_IMACRO ? "i" : ""));
1490 return 3;
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;
1500 skip_white_(tline);
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;
1506 } else {
1507 defining->nparam_min = defining->nparam_max =
1508 readnum(tline->text, &j);
1509 if (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" : ""));
1521 else {
1522 defining->nparam_max = readnum(tline->text, &j);
1523 if (j)
1524 error (ERR_NONFATAL,
1525 "unable to parse parameter count `%s'",
1526 tline->text);
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)];
1543 while (mmac) {
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))
1548 error (ERR_WARNING,
1549 "redefining multi-line macro `%s'", defining->name);
1550 break;
1552 mmac = mmac->next;
1555 * Handle default parameters.
1557 if (tline && tline->next) {
1558 defining->dlist = tline->next;
1559 tline->next = NULL;
1560 count_mmac_params (defining->dlist, &defining->ndefs,
1561 &defining->defaults);
1562 } else {
1563 defining->dlist = NULL;
1564 defining->defaults = NULL;
1566 defining->expansion = NULL;
1567 free_tlist (origline);
1568 return 1;
1570 case PP_ENDM:
1571 case PP_ENDMACRO:
1572 if (!defining) {
1573 error (ERR_NONFATAL, "`%s': not defining a macro",
1574 tline->text);
1575 return 3;
1577 k = hash(defining->name);
1578 defining->next = mmacros[k];
1579 mmacros[k] = defining;
1580 defining = NULL;
1581 free_tlist (origline);
1582 return 5;
1584 case PP_ROTATE:
1585 if (tline->next && tline->next->type == TOK_WHITESPACE)
1586 tline = tline->next;
1587 t = expand_smacro(tline->next);
1588 tline->next = NULL;
1589 free_tlist (origline);
1590 tline = t;
1591 tptr = &t;
1592 tokval.t_type = TOKEN_INVALID;
1593 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1594 free_tlist (tline);
1595 if (!evalresult)
1596 return 3;
1597 if (tokval.t_type)
1598 error(ERR_WARNING,
1599 "trailing garbage after expression ignored");
1600 if (!is_simple(evalresult)) {
1601 error(ERR_NONFATAL,
1602 "non-constant value given to `%%rotate'");
1603 return 3;
1605 mmac = istk->mstk;
1606 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
1607 mmac = mmac->next_active;
1608 if (!mmac)
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;
1614 return 1;
1616 case PP_REP:
1617 nolist = FALSE;
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;
1624 nolist = TRUE;
1626 t = expand_smacro(tline->next);
1627 tline->next = NULL;
1628 free_tlist (origline);
1629 tline = t;
1630 tptr = &t;
1631 tokval.t_type = TOKEN_INVALID;
1632 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1633 free_tlist (tline);
1634 if (!evalresult)
1635 return 3;
1636 if (tokval.t_type)
1637 error(ERR_WARNING,
1638 "trailing garbage after expression ignored");
1639 if (!is_simple(evalresult)) {
1640 error(ERR_NONFATAL,
1641 "non-constant value given to `%%rep'");
1642 return 3;
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;
1657 return 1;
1659 case PP_ENDREP:
1660 if (!defining || defining->name) {
1661 error (ERR_NONFATAL,
1662 "`%%endrep': no matching `%%rep'");
1663 return 3;
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;
1680 l->first = NULL;
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);
1689 return 1;
1691 case PP_EXITREP:
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)
1699 break;
1701 if (l)
1702 l->finishes->in_progress = 0;
1703 else
1704 error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
1705 free_tlist (origline);
1706 return 1;
1708 case PP_DEFINE:
1709 case PP_IDEFINE:
1710 tline = tline->next;
1711 skip_white_(tline);
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);
1719 return 3;
1721 mname = tline->text;
1722 if (tline->type == TOK_ID) {
1723 p = tline->text;
1724 smhead = &smacros[hash(mname)];
1725 } else {
1726 ctx = get_ctx (tline->text);
1727 if (ctx == NULL)
1728 return 3;
1729 else {
1730 p = tline->text+1;
1731 p += strspn(p, "$");
1732 smhead = &ctx->localmac;
1735 last = tline;
1736 param_start = tline = tline->next;
1737 nparam = 0;
1738 if (tok_is_(tline, "(")) {
1740 * This macro has parameters.
1743 tline = tline->next;
1744 while (1) {
1745 skip_white_(tline);
1746 if (!tline) {
1747 error (ERR_NONFATAL,
1748 "parameter identifier expected");
1749 free_tlist (origline);
1750 return 3;
1752 if (tline->type != TOK_ID) {
1753 error (ERR_NONFATAL,
1754 "`%s': parameter identifier expected",
1755 tline->text);
1756 free_tlist (origline);
1757 return 3;
1759 tline->type = TOK_SMAC_PARAM + nparam++;
1760 tline = tline->next;
1761 skip_white_(tline);
1762 if (tok_is_(tline, ",")) {
1763 tline = tline->next;
1764 continue;
1766 if (!tok_is_(tline, ")")) {
1767 error (ERR_NONFATAL,
1768 "`)' expected to terminate macro template");
1769 free_tlist (origline);
1770 return 3;
1772 break;
1774 last = tline;
1775 tline = tline->next;
1777 if (tok_type_(tline, TOK_WHITESPACE))
1778 last = tline, tline = tline->next;
1779 macro_start = NULL;
1780 last->next = NULL;
1781 t = tline;
1782 while (t) {
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))
1787 t->type = tt->type;
1789 tt = t->next;
1790 t->next = macro_start;
1791 macro_start = t;
1792 t = tt;
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
1800 * from the end).
1802 if (smacro_defined (mname, nparam, &smac, i==PP_DEFINE)) {
1803 if (!smac) {
1804 error (ERR_WARNING,
1805 "single-line macro `%s' defined both with and"
1806 " without parameters", mname);
1807 free_tlist (origline);
1808 free_tlist (macro_start);
1809 return 3;
1810 } else {
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);
1819 } else {
1820 smac = nasm_malloc(sizeof(SMacro));
1821 smac->next = *smhead;
1822 *smhead = smac;
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);
1830 return 3;
1832 case PP_ASSIGN:
1833 case PP_IASSIGN:
1834 tline = tline->next;
1835 skip_white_(tline);
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);
1843 return 3;
1845 mname = tline->text;
1846 if (tline->type == TOK_ID) {
1847 p = tline->text;
1848 smhead = &smacros[hash(mname)];
1849 } else {
1850 ctx = get_ctx (tline->text);
1851 if (ctx == NULL) {
1852 free_tlist (origline);
1853 return 3;
1854 } else {
1855 p = tline->text+1;
1856 p += strspn(p, "$");
1857 smhead = &ctx->localmac;
1860 last = tline;
1861 tline = tline->next;
1862 last->next = NULL;
1864 tline = expand_smacro (tline);
1865 t = tline;
1866 tptr = &t;
1867 tokval.t_type = TOKEN_INVALID;
1868 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1869 free_tlist (tline);
1870 if (!evalresult) {
1871 free_tlist (origline);
1872 return 3;
1875 if (tokval.t_type)
1876 error(ERR_WARNING,
1877 "trailing garbage after expression ignored");
1879 if (!is_simple(evalresult)) {
1880 error(ERR_NONFATAL,
1881 "non-constant value given to `%%%sassign'",
1882 (i == PP_IASSIGN ? "i" : ""));
1883 free_tlist (origline);
1884 return 3;
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)) {
1898 if (!smac)
1899 error (ERR_WARNING,
1900 "single-line macro `%s' defined both with and"
1901 " without parameters", mname);
1902 else {
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);
1912 else {
1913 smac = nasm_malloc(sizeof(SMacro));
1914 smac->next = *smhead;
1915 *smhead = smac;
1917 smac->name = nasm_strdup(p);
1918 smac->casesense = (i == PP_ASSIGN);
1919 smac->nparam = 0;
1920 smac->expansion = macro_start;
1921 smac->in_progress = FALSE;
1922 free_tlist (origline);
1923 return 3;
1925 case PP_LINE:
1927 * Syntax is `%line nnn[+mmm] [filename]'
1929 tline = tline->next;
1930 skip_white_(tline);
1931 if (!tok_type_(tline, TOK_NUMBER)) {
1932 error (ERR_NONFATAL, "`%%line' expects line number");
1933 free_tlist (origline);
1934 return 3;
1936 k = readnum(tline->text, &j);
1937 m = 1;
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);
1945 return 3;
1947 m = readnum(tline->text, &j);
1948 tline = tline->next;
1950 skip_white_(tline);
1951 src_set_linnum(k);
1952 istk->lineinc = m;
1953 if (tline) {
1954 nasm_free ( src_set_fname ( detoken(tline) ) );
1956 free_tlist (origline);
1957 return 5;
1959 default:
1960 error(ERR_FATAL,
1961 "preprocessor directive `%s' not yet implemented",
1962 directives[i]);
1963 break;
1965 return 3;
1969 * Ensure that a macro parameter contains a condition code and
1970 * nothing else. Return the condition code index if so, or -1
1971 * otherwise.
1973 static int find_cc (Token *t)
1975 Token *tt;
1976 int i, j, k, m;
1978 skip_white_(t);
1979 if (t->type != TOK_ID)
1980 return -1;
1981 tt = t->next;
1982 skip_white_(tt);
1983 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
1984 return -1;
1986 i = -1;
1987 j = sizeof(conditions)/sizeof(*conditions);
1988 while (j-i > 1) {
1989 k = (j+i) / 2;
1990 m = nasm_stricmp(t->text, conditions[k]);
1991 if (m == 0) {
1992 i = k;
1993 j = -2;
1994 break;
1995 } else if (m < 0) {
1996 j = k;
1997 } else
1998 i = k;
2000 if (j != -2)
2001 return -1;
2002 return i;
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;
2013 tail = &thead;
2014 thead = NULL;
2016 while (tline) {
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'))) {
2021 char *text = NULL;
2022 int type = 0, cc; /* type = 0 to placate optimisers */
2023 char tmpbuf[30];
2024 int n, i;
2025 MMacro *mac;
2027 t = tline;
2028 tline = tline->next;
2030 mac = istk->mstk;
2031 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2032 mac = mac->next_active;
2033 if (!mac)
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.
2040 case '0':
2041 type = TOK_NUMBER;
2042 sprintf(tmpbuf, "%d", mac->nparam);
2043 text = nasm_strdup(tmpbuf);
2044 break;
2045 case '%':
2046 type = TOK_ID;
2047 sprintf(tmpbuf, "..@%lu.", mac->unique);
2048 text = nasm_strcat(tmpbuf, t->text+2);
2049 break;
2050 case '-':
2051 n = atoi(t->text+2)-1;
2052 if (n >= mac->nparam)
2053 tt = NULL;
2054 else {
2055 if (mac->nparam > 1)
2056 n = (n + mac->rotate) % mac->nparam;
2057 tt = mac->params[n];
2059 cc = find_cc (tt);
2060 if (cc == -1) {
2061 error (ERR_NONFATAL,
2062 "macro parameter %d is not a condition code",
2063 n+1);
2064 text = NULL;
2065 } else {
2066 type = TOK_ID;
2067 if (inverse_ccs[cc] == -1) {
2068 error (ERR_NONFATAL,
2069 "condition code `%s' is not invertible",
2070 conditions[cc]);
2071 text = NULL;
2072 } else
2073 text = nasm_strdup(conditions[inverse_ccs[cc]]);
2075 break;
2076 case '+':
2077 n = atoi(t->text+2)-1;
2078 if (n >= mac->nparam)
2079 tt = NULL;
2080 else {
2081 if (mac->nparam > 1)
2082 n = (n + mac->rotate) % mac->nparam;
2083 tt = mac->params[n];
2085 cc = find_cc (tt);
2086 if (cc == -1) {
2087 error (ERR_NONFATAL,
2088 "macro parameter %d is not a condition code",
2089 n+1);
2090 text = NULL;
2091 } else {
2092 type = TOK_ID;
2093 text = nasm_strdup(conditions[cc]);
2095 break;
2096 default:
2097 n = atoi(t->text+1)-1;
2098 if (n >= mac->nparam)
2099 tt = NULL;
2100 else {
2101 if (mac->nparam > 1)
2102 n = (n + mac->rotate) % mac->nparam;
2103 tt = mac->params[n];
2105 if (tt) {
2106 for (i=0; i<mac->paramlen[n]; i++) {
2107 ttt = *tail = nasm_malloc(sizeof(Token));
2108 tail = &ttt->next;
2109 ttt->type = tt->type;
2110 ttt->text = nasm_strdup(tt->text);
2111 ttt->mac = NULL;
2112 tt = tt->next;
2115 text = NULL; /* we've done it here */
2116 break;
2118 nasm_free (t->text);
2119 if (!text) {
2120 nasm_free (t);
2121 } else {
2122 *tail = t;
2123 tail = &t->next;
2124 t->type = type;
2125 t->text = text;
2126 t->mac = NULL;
2128 continue;
2129 } else {
2130 t = *tail = tline;
2131 tline = tline->next;
2132 t->mac = NULL;
2133 tail = &t->next;
2136 *tail = NULL;
2137 t = thead;
2138 for (; t && (tt=t->next)!=NULL ; t = t->next)
2139 switch (t->type) {
2140 case TOK_WHITESPACE:
2141 if (tt->type == TOK_WHITESPACE) {
2142 t->next = tt->next;
2143 nasm_free(tt->text);
2144 nasm_free(tt);
2146 break;
2147 case TOK_ID:
2148 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2149 char *tmp = nasm_strcat(t->text, tt->text);
2150 nasm_free(t->text);
2151 t->text = tmp;
2152 t->next = tt->next;
2153 nasm_free(tt->text);
2154 nasm_free(tt);
2156 break;
2157 case TOK_NUMBER:
2158 if (tt->type == TOK_NUMBER) {
2159 char *tmp = nasm_strcat(t->text, tt->text);
2160 nasm_free(t->text);
2161 t->text = tmp;
2162 t->next = tt->next;
2163 nasm_free(tt->text);
2164 nasm_free(tt);
2166 break;
2169 return thead;
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;
2183 Token **params;
2184 int *paramsize;
2185 int nparam, sparam, brackets;
2186 char *p;
2188 tail = &thead;
2189 thead = NULL;
2191 while (tline) { /* main token loop */
2192 p = NULL;
2193 if (tline->type == TOK_ID) {
2194 head = smacros[hash(tline->text)];
2195 p = tline->text;
2196 } else if (tline->type == TOK_PREPROC_ID && tline->text[1] == '$') {
2197 Context *ctx = get_ctx (tline->text);
2198 if (ctx) {
2199 head = ctx->localmac;
2200 p = tline->text+2;
2201 p += strspn(p, "$");
2204 if (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
2209 * necessary.
2211 for (m = head; m; m = m->next)
2212 if (!mstrcmp(m->name, p, m->casesense))
2213 break;
2214 if (m) {
2215 mstart = tline;
2216 params = NULL;
2217 paramsize = NULL;
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.
2224 if (!m->expansion)
2226 if (!strcmp("__FILE__", m->name)) {
2227 long num=0;
2228 src_get(&num, &(tline->text));
2229 nasm_quote(&(tline->text));
2230 tline->type = TOK_STRING;
2231 continue;
2233 if (!strcmp("__LINE__", m->name)) {
2234 nasm_free(tline->text);
2235 make_tok_num(tline, src_get_linnum());
2236 continue;
2238 t = tline;
2239 tline = tline->next;
2240 nasm_free (t->text);
2241 nasm_free (t);
2242 continue;
2245 else {
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
2252 * pain.
2254 tline = tline->next;
2255 skip_white_(tline);
2256 if (!tok_is_(tline, "(")) {
2258 * This macro wasn't called with parameters: ignore
2259 * the call. (Behaviour borrowed from gnu cpp.)
2261 tline = mstart;
2262 m = NULL;
2264 else {
2265 int paren = 0;
2266 int white = 0;
2267 brackets = 0;
2268 nparam = 0;
2269 tline = tline->next;
2270 sparam = PARAM_DELTA;
2271 params = nasm_malloc (sparam*sizeof(Token *));
2272 params[0] = tline;
2273 paramsize = nasm_malloc (sparam*sizeof(int));
2274 paramsize[0] = 0;
2275 for (;;tline = tline->next) { /* parameter loop */
2276 if (!tline) {
2277 error(ERR_NONFATAL,
2278 "macro call expects terminating `)'");
2279 break;
2281 if (tline->type == TOK_WHITESPACE && brackets<=0) {
2282 if (paramsize[nparam])
2283 white++;
2284 else
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;
2300 white = 0;
2301 continue; /* parameter loop */
2303 if (ch == '{' &&
2304 (brackets>0 || (brackets==0 &&
2305 !paramsize[nparam])))
2307 if (!(brackets++))
2309 params[nparam] = tline->next;
2310 continue; /* parameter loop */
2313 if (ch == '}' && brackets>0)
2314 if (--brackets == 0) {
2315 brackets = -1;
2316 continue; /* parameter loop */
2318 if (ch == '(' && !brackets)
2319 paren++;
2320 if (ch == ')' && brackets<=0)
2321 if (--paren < 0)
2322 break;
2324 if (brackets<0) {
2325 brackets = 0;
2326 error (ERR_NONFATAL, "braces do not "
2327 "enclose all of macro parameter");
2329 paramsize[nparam] += white+1;
2330 white = 0;
2331 } /* parameter loop */
2332 nparam++;
2333 while (m && (m->nparam != nparam ||
2334 mstrcmp(m->name, p, m->casesense)))
2335 m = m->next;
2336 if (!m)
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)
2344 m = NULL;
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
2351 * lines down?
2353 nasm_free (params);
2354 nasm_free (paramsize);
2355 tline = mstart;
2357 else {
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.
2364 t = tline;
2365 if (t) {
2366 tline = t->next;
2367 t->next = NULL;
2369 tt = nasm_malloc(sizeof(Token));
2370 tt->type = TOK_SMAC_END;
2371 tt->text = NULL;
2372 tt->mac = m;
2373 m->in_progress = TRUE;
2374 tt->next = tline;
2375 tline = tt;
2376 for (t = m->expansion; t; t = t->next) {
2377 if (t->type >= TOK_SMAC_PARAM) {
2378 Token *pcopy = tline, **ptail = &pcopy;
2379 Token *ttt, *pt;
2380 int i;
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));
2385 pt->next = tline;
2386 ptail = &pt->next;
2387 pt->text = nasm_strdup(ttt->text);
2388 pt->type = ttt->type;
2389 pt->mac = NULL;
2390 ttt = ttt->next;
2392 tline = pcopy;
2393 } else {
2394 tt = nasm_malloc(sizeof(Token));
2395 tt->type = t->type;
2396 tt->text = nasm_strdup(t->text);
2397 tt->mac = NULL;
2398 tt->next = tline;
2399 tline = tt;
2404 * Having done that, get rid of the macro call, and clean
2405 * up the parameters.
2407 nasm_free (params);
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;
2417 t = tline;
2418 tline = tline->next;
2419 nasm_free (t);
2420 } else {
2421 t = *tail = tline;
2422 tline = tline->next;
2423 t->mac = NULL;
2424 t->next = NULL;
2425 tail = &t->next;
2426 if (t->type == TOK_PREPROC_ID && t->text[1] == '$') {
2427 Context *c = get_ctx (t->text);
2428 char *p, *q, buffer[40];
2430 t->type = TOK_ID;
2431 if (c) {
2432 q = t->text+1;
2433 q += strspn(q, "$");
2434 sprintf(buffer, "..@%lu.", c->number);
2435 p = nasm_strcat (buffer,q);
2436 nasm_free (t->text);
2437 t->text = p;
2443 return thead;
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)
2456 MMacro *head, *m;
2457 Token **params;
2458 int nparam;
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))
2470 break;
2471 if (!m)
2472 return NULL;
2475 * OK, we have a potential macro. Count and demarcate the
2476 * parameters.
2478 count_mmac_params (tline->next, &nparam, &params);
2481 * So we know how many parameters we've got. Find the MMacro
2482 * structure that handles this number.
2484 while (m) {
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) {
2491 #if 0
2492 error (ERR_NONFATAL,
2493 "self-reference in multi-line macro `%s'",
2494 m->name);
2495 #endif
2496 nasm_free (params);
2497 return NULL;
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) *
2505 sizeof(*params)));
2506 while (nparam < m->nparam_min + m->ndefs) {
2507 params[nparam] = m->defaults[nparam - m->nparam_min];
2508 nparam++;
2512 * If we've gone over the maximum parameter count (and
2513 * we're in Plus mode), ignore parameters beyond
2514 * nparam_max.
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));
2523 nparam = 0;
2525 params[nparam] = NULL;
2526 *params_array = params;
2527 return m;
2530 * This one wasn't right: look for the next one with the
2531 * same name.
2533 for (m = m->next; m; m = m->next)
2534 if (!mstrcmp(m->name, tline->text, m->casesense))
2535 break;
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);
2545 nasm_free (params);
2546 return NULL;
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;
2560 MMacro *m;
2561 Line *l, *ll;
2562 int i, nparam, *paramlen;
2564 t = tline;
2565 skip_white_(t);
2566 if (!tok_type_(t, TOK_ID))
2567 return 0;
2568 m = is_mmacro (t, &params);
2569 if (!m) {
2570 Token *last;
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.
2577 label = last = t;
2578 t = t->next;
2579 if (tok_type_(t, TOK_WHITESPACE))
2580 last = t, t = t->next;
2581 if (tok_is_(t, ":")) {
2582 dont_prepend = 1;
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, &params)) == NULL)
2588 return 0;
2589 last->next = NULL;
2590 tline = t;
2594 * Fix up the parameters: this involves stripping leading and
2595 * trailing whitespace, then stripping braces if they are
2596 * present.
2598 for (nparam = 0; params[nparam]; nparam++)
2600 paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL;
2602 for (i = 0; params[i]; i++) {
2603 int brace = FALSE;
2604 int comma = (!m->plus || i < nparam-1);
2606 t = params[i];
2607 skip_white_(t);
2608 if (tok_is_(t, "{"))
2609 t = t->next, brace = TRUE, comma = FALSE;
2610 params[i] = t;
2611 paramlen[i] = 0;
2612 while (t) {
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 */
2619 t = t->next;
2620 paramlen[i]++;
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
2635 * variables.
2637 ll = nasm_malloc(sizeof(Line));
2638 ll->next = istk->expansion;
2639 ll->finishes = m;
2640 ll->first = NULL;
2641 istk->expansion = ll;
2643 m->in_progress = TRUE;
2644 m->params = params;
2645 m->iline = tline;
2646 m->nparam = nparam;
2647 m->rotate = 0;
2648 m->paramlen = paramlen;
2649 m->unique = unique++;
2651 m->next_active = istk->mstk;
2652 istk->mstk = m;
2654 for (l = m->expansion; l; l = l->next) {
2655 Token **tail;
2657 ll = nasm_malloc(sizeof(Line));
2658 ll->finishes = NULL;
2659 ll->next = istk->expansion;
2660 istk->expansion = ll;
2661 tail = &ll->first;
2663 for (t = l->first; t; t = t->next) {
2664 Token *x = t;
2665 if (t->type == TOK_PREPROC_ID &&
2666 t->text[1]=='0' && t->text[2]=='0')
2668 dont_prepend = -1;
2669 x = label;
2670 if (!x)
2671 continue;
2673 tt = *tail = nasm_malloc(sizeof(Token));
2674 tail = &tt->next;
2675 tt->type = x->type;
2676 tt->text = nasm_strdup(x->text);
2677 tt->mac = NULL;
2679 *tail = NULL;
2683 * If we had a label, push it on as the first line of
2684 * the macro expansion.
2686 if (label)
2687 if (dont_prepend<0)
2688 free_tlist(startline);
2689 else {
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) {
2696 while (label->next)
2697 label = label->next;
2698 label->next = tt = nasm_malloc(sizeof(Token));
2699 tt->next = NULL;
2700 tt->mac = NULL;
2701 tt->type = TOK_OTHER;
2702 tt->text = nasm_strdup(":");
2706 list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2708 return 1;
2711 static void pp_reset (char *file, int apass, efunc errfunc, evalfunc eval,
2712 ListGen *listgen)
2714 int h;
2716 error = errfunc;
2717 cstk = NULL;
2718 istk = nasm_malloc(sizeof(Include));
2719 istk->next = NULL;
2720 istk->conds = NULL;
2721 istk->expansion = NULL;
2722 istk->mstk = NULL;
2723 istk->fp = fopen(file, "r");
2724 istk->fname = NULL;
2725 src_set_fname(nasm_strdup(file));
2726 src_set_linnum(0);
2727 istk->lineinc = 1;
2728 if (!istk->fp)
2729 error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file);
2730 defining = NULL;
2731 for (h=0; h<NHASH; h++) {
2732 mmacros[h] = NULL;
2733 smacros[h] = NULL;
2735 unique = 0;
2736 stdmacpos = stdmac;
2737 any_extrastdmac = (extrastdmac != NULL);
2738 list = listgen;
2739 evaluate = eval;
2740 pass = apass;
2743 static char *pp_getline (void)
2745 char *line;
2746 Token *tline;
2747 int ret;
2749 while (1) {
2751 * Fetch a tokenised line, either from the macro-expansion
2752 * buffer or from the input file.
2754 tline = NULL;
2755 while (istk->expansion && istk->expansion->finishes) {
2756 Line *l = istk->expansion;
2757 if (!l->finishes->name && l->finishes->in_progress > 1) {
2758 Line *ll;
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
2771 * if we did.
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;
2780 ll->first = NULL;
2781 tail = &ll->first;
2783 for (t = l->first; t; t = t->next) {
2784 if (t->text) {
2785 tt = *tail = nasm_malloc(sizeof(Token));
2786 tt->next = NULL;
2787 tail = &tt->next;
2788 tt->type = t->type;
2789 tt->text = nasm_strdup(t->text);
2790 tt->mac = NULL;
2794 istk->expansion = ll;
2796 } else {
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
2802 * sensibly from it.
2804 if (defining) {
2805 if (defining->name)
2806 error (ERR_PANIC,
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;
2820 if (m->name) {
2822 * This was a real macro call, not a %rep, and
2823 * therefore the parameter information needs to
2824 * be freed.
2826 nasm_free(m->params);
2827 free_tlist(m->iline);
2828 nasm_free(m->paramlen);
2829 l->finishes->in_progress = FALSE;
2831 else
2832 free_mmacro(m);
2834 istk->expansion = l->next;
2835 nasm_free (l);
2836 list->downlevel (LIST_MACRO);
2839 while (1) { /* until we get a line we can use */
2841 if (istk->expansion) { /* from a macro expansion */
2842 char *p;
2843 Line *l = istk->expansion;
2844 tline = l->first;
2845 istk->expansion = l->next;
2846 nasm_free (l);
2847 p = detoken(tline);
2848 list->line (LIST_MACRO, p);
2849 nasm_free(p);
2850 break;
2852 line = read_line();
2853 if (line) { /* from the current input file */
2854 line = prepreproc(line);
2855 tline = tokenise(line);
2856 nasm_free (line);
2857 break;
2860 * The current file has ended; work down the istk
2863 Include *i = istk;
2864 fclose(i->fp);
2865 if (i->conds)
2866 error(ERR_FATAL, "expected `%%endif' before end of file");
2867 istk = i->next;
2868 list->downlevel (LIST_INCLUDE);
2869 src_set_linnum(i->lineno);
2870 nasm_free ( src_set_fname(i->fname) );
2871 nasm_free (i);
2872 if (!istk)
2873 return NULL;
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
2885 * anything.
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);
2894 if (ret & 1) {
2895 continue;
2896 } else if (defining) {
2898 * We're defining a multi-line macro. We emit nothing
2899 * at all, and just
2900 * shove the tokenised line on to the macro definition.
2902 Line *l = nasm_malloc(sizeof(Line));
2903 l->next = defining->expansion;
2904 l->first = tline;
2905 l->finishes = FALSE;
2906 defining->expansion = l;
2907 continue;
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.
2915 free_tlist(tline);
2916 continue;
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
2924 * correctly.
2926 free_tlist(tline);
2927 continue;
2928 } else {
2929 tline = expand_smacro(tline);
2930 ret = expand_mmacro(tline);
2931 if (!ret) {
2933 * De-tokenise the line again, and emit it.
2935 line = detoken(tline);
2936 free_tlist (tline);
2937 break;
2938 } else {
2939 continue; /* expand_mmacro calls free_tlist */
2944 return line;
2947 static void pp_cleanup (void)
2949 int h;
2951 if (defining) {
2952 error (ERR_NONFATAL, "end of file while still defining macro `%s'",
2953 defining->name);
2954 free_mmacro (defining);
2956 while (cstk)
2957 ctx_pop();
2958 for (h=0; h<NHASH; h++) {
2959 while (mmacros[h]) {
2960 MMacro *m = mmacros[h];
2961 mmacros[h] = mmacros[h]->next;
2962 free_mmacro(m);
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);
2969 nasm_free (s);
2972 while (istk) {
2973 Include *i = istk;
2974 istk = istk->next;
2975 fclose(i->fp);
2976 nasm_free (i->fname);
2977 nasm_free (i);
2979 while (cstk)
2980 ctx_pop();
2983 void pp_include_path (char *path)
2985 IncPath *i;
2987 i = nasm_malloc(sizeof(IncPath));
2988 i->path = nasm_strdup(path);
2989 i->next = ipath;
2990 ipath = i;
2993 void pp_pre_include (char *fname)
2995 Token *inc, *space, *name;
2996 Line *l;
2998 inc = nasm_malloc(sizeof(Token));
2999 inc->next = space = nasm_malloc(sizeof(Token));
3000 space->next = name = nasm_malloc(sizeof(Token));
3001 name->next = NULL;
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));
3013 l->next = predef;
3014 l->first = inc;
3015 l->finishes = FALSE;
3016 predef = l;
3019 void pp_pre_define (char *definition)
3021 Token *def, *space;
3022 Line *l;
3023 char *equals;
3025 equals = strchr(definition, '=');
3027 def = nasm_malloc(sizeof(Token));
3028 def->next = space = nasm_malloc(sizeof(Token));
3029 if (equals)
3030 *equals = ' ';
3031 space->next = tokenise(definition);
3032 if (equals)
3033 *equals = '=';
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));
3043 l->next = predef;
3044 l->first = def;
3045 l->finishes = FALSE;
3046 predef = l;
3049 void pp_extra_stdmac (char **macros)
3051 extrastdmac = macros;
3054 static void make_tok_num(Token *tok, long val)
3056 char numbuf[20];
3057 sprintf(numbuf, "%ld", val);
3058 tok->text = nasm_strdup(numbuf);
3059 tok->type = TOK_NUMBER;
3062 Preproc nasmpp = {
3063 pp_reset,
3064 pp_getline,
3065 pp_cleanup