NASM 0.98p3.4
[nasm/avx512.git] / preproc.c
blobe16432a56e5e24d49caeb324f57b4a760e8247dc
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 #define br0 '{'
12 #define br1 "{"
13 #define br2 '}'
14 #define br3 "}"
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <limits.h>
23 #include "nasm.h"
24 #include "nasmlib.h"
26 typedef struct SMacro SMacro;
27 typedef struct MMacro MMacro;
28 typedef struct Context Context;
29 typedef struct Token Token;
30 typedef struct Line Line;
31 typedef struct Include Include;
32 typedef struct Cond Cond;
33 typedef struct IncPath IncPath;
36 * Store the definition of a single-line macro.
38 struct SMacro {
39 SMacro *next;
40 char *name;
41 int casesense;
42 int nparam;
43 int in_progress;
44 Token *expansion;
48 * Store the definition of a multi-line macro. This is also used to
49 * store the interiors of `%rep...%endrep' blocks, which are
50 * effectively self-re-invoking multi-line macros which simply
51 * don't have a name or bother to appear in the hash tables. %rep
52 * blocks are signified by having a NULL `name' field.
54 * In a MMacro describing a `%rep' block, the `in_progress' field
55 * isn't merely boolean, but gives the number of repeats left to
56 * run.
58 * The `next' field is used for storing MMacros in hash tables; the
59 * `next_active' field is for stacking them on istk entries.
61 * When a MMacro is being expanded, `params', `iline', `nparam',
62 * `paramlen', `rotate' and `unique' are local to the invocation.
64 struct MMacro {
65 MMacro *next;
66 char *name;
67 int casesense;
68 int nparam_min, nparam_max;
69 int plus; /* is the last parameter greedy? */
70 int nolist; /* is this macro listing-inhibited? */
71 int in_progress;
72 Token *dlist; /* All defaults as one list */
73 Token **defaults; /* Parameter default pointers */
74 int ndefs; /* number of default parameters */
75 Line *expansion;
77 MMacro *next_active;
78 MMacro *rep_nest; /* used for nesting %rep */
79 Token **params; /* actual parameters */
80 Token *iline; /* invocation line */
81 int nparam, rotate, *paramlen;
82 unsigned long unique;
86 * The context stack is composed of a linked list of these.
88 struct Context {
89 Context *next;
90 SMacro *localmac;
91 char *name;
92 unsigned long number;
96 * This is the internal form which we break input lines up into.
97 * Typically stored in linked lists.
99 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
100 * necessarily used as-is, but is intended to denote the number of
101 * the substituted parameter. So in the definition
103 * %define a(x,y) ( (x) & ~(y) )
105 * the token representing `x' will have its type changed to
106 * TOK_SMAC_PARAM, but the one representing `y' will be
107 * TOK_SMAC_PARAM+1.
109 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
110 * which doesn't need quotes around it. Used in the pre-include
111 * mechanism as an alternative to trying to find a sensible type of
112 * quote to use on the filename we were passed.
114 struct Token {
115 Token *next;
116 char *text;
117 SMacro *mac; /* associated macro for TOK_SMAC_END */
118 int type;
120 enum {
121 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
122 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
123 TOK_INTERNAL_STRING
127 * Multi-line macro definitions are stored as a linked list of
128 * these, which is essentially a container to allow several linked
129 * lists of Tokens.
131 * Note that in this module, linked lists are treated as stacks
132 * wherever possible. For this reason, Lines are _pushed_ on to the
133 * `expansion' field in MMacro structures, so that the linked list,
134 * if walked, would give the macro lines in reverse order; this
135 * means that we can walk the list when expanding a macro, and thus
136 * push the lines on to the `expansion' field in _istk_ in reverse
137 * order (so that when popped back off they are in the right
138 * order). It may seem cockeyed, and it relies on my design having
139 * an even number of steps in, but it works...
141 * Some of these structures, rather than being actual lines, are
142 * markers delimiting the end of the expansion of a given macro.
143 * This is for use in the cycle-tracking and %rep-handling code.
144 * Such structures have `finishes' non-NULL, and `first' NULL. All
145 * others have `finishes' NULL, but `first' may still be NULL if
146 * the line is blank.
148 struct Line {
149 Line *next;
150 MMacro *finishes;
151 Token *first;
155 * To handle an arbitrary level of file inclusion, we maintain a
156 * stack (ie linked list) of these things.
158 struct Include {
159 Include *next;
160 FILE *fp;
161 Cond *conds;
162 Line *expansion;
163 char *fname;
164 int lineno, lineinc;
165 MMacro *mstk; /* stack of active macros/reps */
169 * Include search path. This is simply a list of strings which get
170 * prepended, in turn, to the name of an include file, in an
171 * attempt to find the file if it's not in the current directory.
173 struct IncPath {
174 IncPath *next;
175 char *path;
179 * Conditional assembly: we maintain a separate stack of these for
180 * each level of file inclusion. (The only reason we keep the
181 * stacks separate is to ensure that a stray `%endif' in a file
182 * included from within the true branch of a `%if' won't terminate
183 * it and cause confusion: instead, rightly, it'll cause an error.)
185 struct Cond {
186 Cond *next;
187 int state;
189 enum {
191 * These states are for use just after %if or %elif: IF_TRUE
192 * means the condition has evaluated to truth so we are
193 * currently emitting, whereas IF_FALSE means we are not
194 * currently emitting but will start doing so if a %else comes
195 * up. In these states, all directives are admissible: %elif,
196 * %else and %endif. (And of course %if.)
198 COND_IF_TRUE, COND_IF_FALSE,
200 * These states come up after a %else: ELSE_TRUE means we're
201 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
202 * any %elif or %else will cause an error.
204 COND_ELSE_TRUE, COND_ELSE_FALSE,
206 * This state means that we're not emitting now, and also that
207 * nothing until %endif will be emitted at all. It's for use in
208 * two circumstances: (i) when we've had our moment of emission
209 * and have now started seeing %elifs, and (ii) when the
210 * condition construct in question is contained within a
211 * non-emitting branch of a larger condition construct.
213 COND_NEVER
215 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
218 * Condition codes. Note that we use c_ prefix not C_ because C_ is
219 * used in nasm.h for the "real" condition codes. At _this_ level,
220 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
221 * ones, so we need a different enum...
223 static char *conditions[] = {
224 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
225 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
226 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
228 enum {
229 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
230 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
231 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
233 static int inverse_ccs[] = {
234 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
235 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,
236 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
240 * Directive names.
242 static char *directives[] = {
243 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
244 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
245 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
246 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
247 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
248 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
249 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
250 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include", "%line",
251 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate"
253 enum {
254 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
255 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFNCTX, PP_ELIFNDEF,
256 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNNUM, PP_ELIFNSTR,
257 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
258 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
259 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFNCTX,
260 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNNUM,
261 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE, PP_LINE,
262 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE
266 static Context *cstk;
267 static Include *istk;
268 static IncPath *ipath = NULL;
270 static efunc error;
271 static evalfunc evaluate;
273 static int pass;
275 static unsigned long unique; /* unique identifier numbers */
277 static Line *predef = NULL;
279 static ListGen *list;
282 * The number of hash values we use for the macro lookup tables.
283 * FIXME: We should *really* be able to configure this at run time,
284 * or even have the hash table automatically expanding when necessary.
286 #define NHASH 31
289 * The current set of multi-line macros we have defined.
291 static MMacro *mmacros[NHASH];
294 * The current set of single-line macros we have defined.
296 static SMacro *smacros[NHASH];
299 * The multi-line macro we are currently defining, or the %rep
300 * block we are currently reading, if any.
302 static MMacro *defining;
305 * The number of macro parameters to allocate space for at a time.
307 #define PARAM_DELTA 16
310 * The standard macro set: defined as `static char *stdmac[]'. Also
311 * gives our position in the macro set, when we're processing it.
313 #include "macros.c"
314 static char **stdmacpos;
317 * The extra standard macros that come from the object format, if
318 * any.
320 static char **extrastdmac = NULL;
321 int any_extrastdmac;
324 * Forward declarations.
326 static Token *expand_smacro (Token *tline);
327 static void make_tok_num(Token *tok, long val);
330 * Macros for safe checking of token pointers, avoid *(NULL)
332 #define tok_type_(x,t) ((x) && (x)->type == (t))
333 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
334 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
335 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
338 * The pre-preprocessing stage... This function translates line
339 * number indications as they emerge from GNU cpp (`# lineno "file"
340 * flags') into NASM preprocessor line number indications (`%line
341 * lineno file').
343 static char *prepreproc(char *line)
345 int lineno, fnlen;
346 char *fname, *oldline;
348 if (line[0] == '#' && line[1] == ' ') {
349 oldline = line;
350 fname = oldline+2;
351 lineno = atoi(fname);
352 fname += strspn(fname, "0123456789 ");
353 if (*fname == '"')
354 fname++;
355 fnlen = strcspn(fname, "\"");
356 line = nasm_malloc(20+fnlen);
357 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
358 nasm_free (oldline);
360 return line;
364 * The hash function for macro lookups. Note that due to some
365 * macros having case-insensitive names, the hash function must be
366 * invariant under case changes. We implement this by applying a
367 * perfectly normal hash function to the uppercase of the string.
369 static int hash(char *s)
371 unsigned int h = 0;
372 int i = 0;
374 * Powers of three, mod 31.
376 static const int multipliers[] = {
377 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
378 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
382 while (*s) {
383 h += multipliers[i] * (unsigned char) (toupper(*s));
384 s++;
385 if (++i >= sizeof(multipliers)/sizeof(*multipliers))
386 i = 0;
388 h %= NHASH;
389 return h;
393 * Free a linked list of tokens.
395 static void free_tlist (Token *list)
397 Token *t;
398 while (list) {
399 t = list;
400 list = list->next;
401 nasm_free (t->text);
402 nasm_free (t);
407 * Free a linked list of lines.
409 static void free_llist (Line *list)
411 Line *l;
412 while (list) {
413 l = list;
414 list = list->next;
415 free_tlist (l->first);
416 nasm_free (l);
421 * Free an MMacro
423 static void free_mmacro (MMacro *m)
425 nasm_free (m->name);
426 free_tlist (m->dlist);
427 nasm_free (m->defaults);
428 free_llist (m->expansion);
429 nasm_free (m);
433 * Pop the context stack.
435 static void ctx_pop (void)
437 Context *c = cstk;
438 SMacro *smac, *s;
440 cstk = cstk->next;
441 smac = c->localmac;
442 while (smac) {
443 s = smac;
444 smac = smac->next;
445 nasm_free (s->name);
446 free_tlist (s->expansion);
447 nasm_free (s);
449 nasm_free (c->name);
450 nasm_free (c);
453 #define BUF_DELTA 512
455 * Read a line from the top file in istk, handling multiple CR/LFs
456 * at the end of the line read, and handling spurious ^Zs. Will
457 * return lines from the standard macro set if this has not already
458 * been done.
460 static char *read_line (void)
462 char *buffer, *p, *q;
463 int bufsize;
465 if (stdmacpos) {
466 if (*stdmacpos) {
467 char *ret = nasm_strdup(*stdmacpos++);
468 if (!*stdmacpos && any_extrastdmac)
470 stdmacpos = extrastdmac;
471 any_extrastdmac = FALSE;
472 return ret;
475 * Nasty hack: here we push the contents of `predef' on
476 * to the top-level expansion stack, since this is the
477 * most convenient way to implement the pre-include and
478 * pre-define features.
480 if (!*stdmacpos)
482 Line *pd, *l;
483 Token *head, **tail, *t, *tt;
485 for (pd = predef; pd; pd = pd->next) {
486 head = NULL;
487 tail = &head;
488 for (t = pd->first; t; t = t->next) {
489 tt = *tail = nasm_malloc(sizeof(Token));
490 tt->next = NULL;
491 tail = &tt->next;
492 tt->type = t->type;
493 tt->text = nasm_strdup(t->text);
494 tt->mac = t->mac; /* always NULL here, in fact */
496 l = nasm_malloc(sizeof(Line));
497 l->next = istk->expansion;
498 l->first = head;
499 l->finishes = FALSE;
500 istk->expansion = l;
503 return ret;
505 else {
506 stdmacpos = NULL;
510 bufsize = BUF_DELTA;
511 buffer = nasm_malloc(BUF_DELTA);
512 p = buffer;
513 while (1) {
514 q = fgets(p, bufsize-(p-buffer), istk->fp);
515 if (!q)
516 break;
517 p += strlen(p);
518 if (p > buffer && p[-1] == '\n') {
519 break;
521 if (p-buffer > bufsize-10) {
522 long offset = p-buffer;
523 bufsize += BUF_DELTA;
524 buffer = nasm_realloc(buffer, bufsize);
525 p = buffer+offset; /* prevent stale-pointer problems */
529 if (!q && p == buffer) {
530 nasm_free (buffer);
531 return NULL;
534 src_set_linnum(src_get_linnum() + istk->lineinc);
537 * Play safe: remove CRs as well as LFs, if any of either are
538 * present at the end of the line.
540 while (--p >= buffer && (*p == '\n' || *p == '\r'))
541 *p = '\0';
544 * Handle spurious ^Z, which may be inserted into source files
545 * by some file transfer utilities.
547 buffer[strcspn(buffer, "\032")] = '\0';
549 list->line (LIST_READ, buffer);
551 return buffer;
555 * Tokenise a line of text. This is a very simple process since we
556 * don't need to parse the value out of e.g. numeric tokens: we
557 * simply split one string into many.
559 static Token *tokenise (char *line)
561 char *p = line;
562 int type;
563 Token *list = NULL;
564 Token *t, **tail = &list;
566 while (*line) {
567 p = line;
568 if (*p == '%' && ( isdigit(p[1]) ||
569 ((p[1] == '-' || p[1] == '+') && isdigit(p[2]))))
571 p++;
572 do {
573 p++;
574 } while (isdigit(*p));
575 type = TOK_PREPROC_ID;
577 else if (*p == '%' && p[1] == '{') {
578 p += 2;
579 while (*p && *p != '}') {
580 p[-1] = *p;
581 p++;
583 p[-1] = '\0';
584 if (*p) p++;
585 type = TOK_PREPROC_ID;
587 else if (*p == '%' && (isidchar(p[1]) ||
588 ((p[1] == '!' || p[1] == '%' || p[1] == '$') &&
589 isidchar(p[2]))))
591 p++;
592 do {
593 p++;
594 } while (isidchar(*p));
595 type = TOK_PREPROC_ID;
597 else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
598 type = TOK_ID;
599 p++;
600 while (*p && isidchar(*p))
601 p++;
603 else if (*p == '\'' || *p == '"') {
605 * A string token.
607 char c = *p;
608 p++;
609 type = TOK_STRING;
610 while (*p && *p != c)
611 p++;
612 if (*p) p++;
614 else if (isnumstart(*p)) {
616 * A number token.
618 type = TOK_NUMBER;
619 p++;
620 while (*p && isnumchar(*p))
621 p++;
623 else if (isspace(*p)) {
624 type = TOK_WHITESPACE;
625 p++;
626 while (*p && isspace(*p))
627 p++;
629 * Whitespace just before end-of-line is discarded by
630 * pretending it's a comment; whitespace just before a
631 * comment gets lumped into the comment.
633 if (!*p || *p == ';') {
634 type = TOK_COMMENT;
635 while (*p) p++;
638 else if (*p == ';') {
639 type = TOK_COMMENT;
640 while (*p) p++;
642 else {
644 * Anything else is an operator of some kind. We check
645 * for all the double-character operators (>>, <<, //,
646 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
647 * else is a single-character operator.
649 type = TOK_OTHER;
650 if ((p[0] == '>' && p[1] == '>') ||
651 (p[0] == '<' && p[1] == '<') ||
652 (p[0] == '/' && p[1] == '/') ||
653 (p[0] == '%' && p[1] == '%') ||
654 (p[0] == '<' && p[1] == '=') ||
655 (p[0] == '>' && p[1] == '=') ||
656 (p[0] == '=' && p[1] == '=') ||
657 (p[0] == '!' && p[1] == '=') ||
658 (p[0] == '<' && p[1] == '>') ||
659 (p[0] == '&' && p[1] == '&') ||
660 (p[0] == '|' && p[1] == '|') ||
661 (p[0] == '^' && p[1] == '^'))
663 p++;
665 p++;
667 if (type != TOK_COMMENT) {
668 *tail = t = nasm_malloc (sizeof(Token));
669 tail = &t->next;
670 t->next = NULL;
671 t->type = type;
672 t->text = nasm_malloc(1+p-line);
673 strncpy(t->text, line, p-line);
674 t->text[p-line] = '\0';
676 line = p;
679 return list;
683 * Convert a line of tokens back into text.
685 char *detoken (Token *tlist)
687 Token *t;
688 int len;
689 char *line, *p;
691 len = 0;
692 for (t = tlist; t; t = t->next) {
693 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
694 char *p = getenv(t->text+2);
695 nasm_free (t->text);
696 if (p)
697 t->text = nasm_strdup(p);
698 else
699 t->text = NULL;
701 if (t->text)
702 len += strlen(t->text);
704 p = line = nasm_malloc(len+1);
705 for (t = tlist; t; t = t->next) {
706 if (t->text) {
707 strcpy (p, t->text);
708 p += strlen(p);
711 *p = '\0';
712 return line;
716 * A scanner, suitable for use by the expression evaluator, which
717 * operates on a line of Tokens. Expects a pointer to a pointer to
718 * the first token in the line to be passed in as its private_data
719 * field.
721 static int ppscan(void *private_data, struct tokenval *tokval)
723 Token **tlineptr = private_data;
724 Token *tline;
726 do {
727 tline = *tlineptr;
728 *tlineptr = tline ? tline->next : NULL;
729 } while (tline && (tline->type == TOK_WHITESPACE ||
730 tline->type == TOK_COMMENT));
732 if (!tline)
733 return tokval->t_type = TOKEN_EOS;
735 if (tline->text[0] == '$' && !tline->text[1])
736 return tokval->t_type = TOKEN_HERE;
737 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[1])
738 return tokval->t_type = TOKEN_BASE;
740 if (tline->type == TOK_ID) {
741 tokval->t_charptr = tline->text;
742 if (tline->text[0] == '$') {
743 tokval->t_charptr++;
744 return tokval->t_type = TOKEN_ID;
748 * This is the only special case we actually need to worry
749 * about in this restricted context.
751 if (!nasm_stricmp(tline->text, "seg"))
752 return tokval->t_type = TOKEN_SEG;
754 return tokval->t_type = TOKEN_ID;
757 if (tline->type == TOK_NUMBER) {
758 int rn_error;
760 tokval->t_integer = readnum(tline->text, &rn_error);
761 if (rn_error)
762 return tokval->t_type = TOKEN_ERRNUM;
763 tokval->t_charptr = NULL;
764 return tokval->t_type = TOKEN_NUM;
767 if (tline->type == TOK_STRING) {
768 int rn_warn;
769 char q, *r;
770 int l;
772 r = tline->text;
773 q = *r++;
774 l = strlen(r);
776 if (l == 0 || r[l-1] != q)
777 return tokval->t_type = TOKEN_ERRNUM;
778 tokval->t_integer = readstrnum(r, l-1, &rn_warn);
779 if (rn_warn)
780 error(ERR_WARNING|ERR_PASS1,
781 "character constant too long");
782 tokval->t_charptr = NULL;
783 return tokval->t_type = TOKEN_NUM;
786 if (tline->type == TOK_OTHER) {
787 if (!strcmp(tline->text, "<<")) return tokval->t_type = TOKEN_SHL;
788 if (!strcmp(tline->text, ">>")) return tokval->t_type = TOKEN_SHR;
789 if (!strcmp(tline->text, "//")) return tokval->t_type = TOKEN_SDIV;
790 if (!strcmp(tline->text, "%%")) return tokval->t_type = TOKEN_SMOD;
791 if (!strcmp(tline->text, "==")) return tokval->t_type = TOKEN_EQ;
792 if (!strcmp(tline->text, "<>")) return tokval->t_type = TOKEN_NE;
793 if (!strcmp(tline->text, "!=")) return tokval->t_type = TOKEN_NE;
794 if (!strcmp(tline->text, "<=")) return tokval->t_type = TOKEN_LE;
795 if (!strcmp(tline->text, ">=")) return tokval->t_type = TOKEN_GE;
796 if (!strcmp(tline->text, "&&")) return tokval->t_type = TOKEN_DBL_AND;
797 if (!strcmp(tline->text, "^^")) return tokval->t_type = TOKEN_DBL_XOR;
798 if (!strcmp(tline->text, "||")) return tokval->t_type = TOKEN_DBL_OR;
802 * We have no other options: just return the first character of
803 * the token text.
805 return tokval->t_type = tline->text[0];
809 * Return the Context structure associated with a %$ token. Return
810 * NULL, having _already_ reported an error condition, if the
811 * context stack isn't deep enough for the supplied number of $
812 * signs.
814 static Context *get_ctx (char *name)
816 Context *ctx;
817 int i;
819 if (!cstk) {
820 error (ERR_NONFATAL, "`%s': context stack is empty", name);
821 return NULL;
824 i = 1;
825 ctx = cstk;
826 while (name[i+1] == '$') {
827 i++;
828 ctx = ctx->next;
829 if (!ctx) {
830 error (ERR_NONFATAL, "`%s': context stack is only"
831 " %d level%s deep", name, i-1, (i==2 ? "" : "s"));
832 return NULL;
835 return ctx;
839 * Compare a string to the name of an existing macro; this is a
840 * simple wrapper which calls either strcmp or nasm_stricmp
841 * depending on the value of the `casesense' parameter.
843 static int mstrcmp(char *p, char *q, int casesense)
845 return casesense ? strcmp(p,q) : nasm_stricmp(p,q);
849 * Open an include file. This routine must always return a valid
850 * file pointer if it returns - it's responsible for throwing an
851 * ERR_FATAL and bombing out completely if not. It should also try
852 * the include path one by one until it finds the file or reaches
853 * the end of the path.
855 static FILE *inc_fopen(char *file)
857 FILE *fp;
858 char *prefix = "", *combine;
859 IncPath *ip = ipath;
861 while (1) {
862 combine = nasm_strcat(prefix,file);
863 fp = fopen(combine, "r");
864 nasm_free (combine);
865 if (fp)
866 return fp;
867 if (!ip)
868 break;
869 prefix = ip->path;
870 ip = ip->next;
873 error (ERR_FATAL,
874 "unable to open include file `%s'", file);
875 return NULL; /* never reached - placate compilers */
879 * Determine if we should warn on defining a single-line macro of
880 * name `name', with `nparam' parameters. If nparam is 0, will
881 * return TRUE if _any_ single-line macro of that name is defined.
882 * Otherwise, will return TRUE if a single-line macro with either
883 * `nparam' or no parameters is defined.
885 * If a macro with precisely the right number of parameters is
886 * defined, the address of the definition structure will be
887 * returned in `defn'; otherwise NULL will be returned. If `defn'
888 * is NULL, no action will be taken regarding its contents, and no
889 * error will occur.
891 * Note that this is also called with nparam zero to resolve
892 * `ifdef'.
894 static int smacro_defined (char *name, int nparam, SMacro **defn, int nocase)
896 SMacro *m;
897 Context *ctx;
898 char *p;
900 if (name[0] == '%' && name[1] == '$') {
901 ctx = get_ctx (name);
902 if (!ctx)
903 return FALSE; /* got to return _something_ */
904 m = ctx->localmac;
905 p = name+1;
906 p += strspn(p, "$");
907 } else {
908 m = smacros[hash(name)];
909 p = name;
912 while (m) {
913 if (!mstrcmp(m->name, p, m->casesense & nocase) &&
914 (nparam == 0 || m->nparam == 0 || nparam == m->nparam)) {
915 if (defn) {
916 if (nparam == m->nparam)
917 *defn = m;
918 else
919 *defn = NULL;
921 return TRUE;
923 m = m->next;
925 return FALSE;
929 * Count and mark off the parameters in a multi-line macro call.
930 * This is called both from within the multi-line macro expansion
931 * code, and also to mark off the default parameters when provided
932 * in a %macro definition line.
934 static void count_mmac_params (Token *t, int *nparam, Token ***params)
936 int paramsize, brace;
938 *nparam = paramsize = 0;
939 *params = NULL;
940 while (t) {
941 if (*nparam >= paramsize) {
942 paramsize += PARAM_DELTA;
943 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
945 skip_white_(t);
946 brace = FALSE;
947 if (tok_is_(t, "{"))
948 brace = TRUE;
949 (*params)[(*nparam)++] = t;
950 while (tok_isnt_(t, brace ? "}" : ","))
951 t = t->next;
952 if (t) { /* got a comma/brace */
953 t = t->next;
954 if (brace) {
956 * Now we've found the closing brace, look further
957 * for the comma.
959 skip_white_(t);
960 if (tok_isnt_(t, ",")) {
961 error (ERR_NONFATAL,
962 "braces do not enclose all of macro parameter");
963 while (tok_isnt_(t, ","))
964 t = t->next;
966 if (t)
967 t = t->next; /* eat the comma */
974 * Determine whether one of the various `if' conditions is true or
975 * not.
977 * We must free the tline we get passed.
979 static int if_condition (Token *tline, int i)
981 int j, casesense;
982 Token * t, * tt, ** tptr, * origline;
983 struct tokenval tokval;
984 expr * evalresult;
986 origline = tline;
988 switch (i) {
989 case PP_IFCTX: case PP_ELIFCTX:
990 case PP_IFNCTX: case PP_ELIFNCTX:
991 j = FALSE; /* have we matched yet? */
992 if (!cstk)
993 error(ERR_FATAL,
994 "`%s': context stack is empty", directives[i]);
995 else while (tline) {
996 skip_white_(tline);
997 if (!tline || tline->type != TOK_ID) {
998 error(ERR_NONFATAL,
999 "`%s' expects context identifiers", directives[i]);
1000 free_tlist (origline);
1001 return -1;
1003 if (!nasm_stricmp(tline->text, cstk->name))
1004 j = TRUE;
1005 tline = tline->next;
1007 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1008 j = !j;
1009 free_tlist (origline);
1010 return j;
1012 case PP_IFDEF: case PP_ELIFDEF:
1013 case PP_IFNDEF: case PP_ELIFNDEF:
1014 j = FALSE; /* have we matched yet? */
1015 while (tline) {
1016 skip_white_(tline);
1017 if (!tline || (tline->type != TOK_ID &&
1018 (tline->type != TOK_PREPROC_ID ||
1019 tline->text[1] != '$'))) {
1020 error(ERR_NONFATAL,
1021 "`%%if%sdef' expects macro identifiers",
1022 (i==PP_ELIFNDEF ? "n" : ""));
1023 free_tlist (origline);
1024 return -1;
1026 if (smacro_defined(tline->text, 0, NULL, 1))
1027 j = TRUE;
1028 tline = tline->next;
1030 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1031 j = !j;
1032 free_tlist (origline);
1033 return j;
1035 case PP_IFIDN: case PP_ELIFIDN: case PP_IFNIDN: case PP_ELIFNIDN:
1036 case PP_IFIDNI: case PP_ELIFIDNI: case PP_IFNIDNI: case PP_ELIFNIDNI:
1037 tline = expand_smacro(tline);
1038 t = tt = tline;
1039 while (tok_isnt_(tt, ","))
1040 tt = tt->next;
1041 if (!tt) {
1042 error(ERR_NONFATAL, "`%s' expects two comma-separated arguments");
1043 free_tlist (tline);
1044 return -1;
1046 tt = tt->next;
1047 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1048 i == PP_IFNIDN || i == PP_ELIFNIDN);
1049 j = TRUE; /* assume equality unless proved not */
1050 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1051 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1052 error(ERR_NONFATAL, "`%s': more than one comma on line",
1053 directives[i]);
1054 free_tlist (tline);
1055 return -1;
1057 if (t->type == TOK_WHITESPACE) {
1058 t = t->next;
1059 continue;
1060 } else if (tt->type == TOK_WHITESPACE) {
1061 tt = tt->next;
1062 continue;
1063 } else if (tt->type != t->type ||
1064 (casesense ? strcmp(tt->text, t->text) :
1065 nasm_stricmp(tt->text, t->text))) {
1066 j = FALSE; /* found mismatching tokens */
1067 break;
1068 } else {
1069 t = t->next;
1070 tt = tt->next;
1071 continue;
1074 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1075 j = FALSE; /* trailing gunk on one end or other */
1076 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1077 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1078 j = !j;
1079 free_tlist (tline);
1080 return j;
1082 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1083 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1084 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1085 tline = expand_smacro(tline);
1086 t = tline;
1087 while (tok_type_(t, TOK_WHITESPACE))
1088 t = t->next;
1089 j = FALSE; /* placate optimiser */
1090 if (t) switch (i) {
1091 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1092 j = (t->type == TOK_ID);
1093 break;
1094 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1095 j = (t->type == TOK_NUMBER);
1096 break;
1097 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1098 j = (t->type == TOK_STRING);
1099 break;
1101 if (i == PP_IFNID || i == PP_ELIFNID ||
1102 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1103 i == PP_IFNSTR || i == PP_ELIFNSTR)
1104 j = !j;
1105 free_tlist (tline);
1106 return j;
1108 case PP_IF: case PP_ELIF:
1109 t = tline = expand_smacro(tline);
1110 tptr = &t;
1111 tokval.t_type = TOKEN_INVALID;
1112 evalresult = evaluate (ppscan, tptr, &tokval,
1113 NULL, pass | 0x10, error, NULL);
1114 free_tlist (tline);
1115 if (!evalresult)
1116 return -1;
1117 if (tokval.t_type)
1118 error(ERR_WARNING,
1119 "trailing garbage after expression ignored");
1120 if (!is_simple(evalresult)) {
1121 error(ERR_NONFATAL,
1122 "non-constant value given to `%s'", directives[i]);
1123 return -1;
1125 return reloc_value(evalresult) != 0;
1127 default:
1128 error(ERR_FATAL,
1129 "preprocessor directive `%s' not yet implemented",
1130 directives[i]);
1131 free_tlist (origline);
1132 return -1; /* yeah, right */
1137 * Find out if a line contains a preprocessor directive, and deal
1138 * with it if so.
1140 * If a directive _is_ found, we are expected to free_tlist() the
1141 * line.
1143 * Return values go like this:
1145 * bit 0 is set if a directive was found (so the line gets freed)
1147 static int do_directive (Token *tline)
1149 int i, j, k, m, nparam, nolist;
1150 char *p, *mname;
1151 Include *inc;
1152 Context *ctx;
1153 Cond *cond;
1154 SMacro *smac, **smhead;
1155 MMacro *mmac;
1156 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1157 Line *l;
1158 struct tokenval tokval;
1159 expr *evalresult;
1160 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1162 origline = tline;
1164 skip_white_(tline);
1165 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1166 (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!'))
1167 return 0;
1169 i = -1;
1170 j = sizeof(directives)/sizeof(*directives);
1171 while (j-i > 1) {
1172 k = (j+i) / 2;
1173 m = nasm_stricmp(tline->text, directives[k]);
1174 if (m == 0) {
1175 i = k;
1176 j = -2;
1177 break;
1178 } else if (m < 0) {
1179 j = k;
1180 } else
1181 i = k;
1185 * If we're in a non-emitting branch of a condition construct,
1186 * or walking to the end of an already terminated %rep block,
1187 * we should ignore all directives except for condition
1188 * directives.
1190 if (((istk->conds && !emitting(istk->conds->state)) ||
1191 (istk->mstk && !istk->mstk->in_progress)) &&
1192 i != PP_IF && i != PP_ELIF &&
1193 i != PP_IFCTX && i != PP_ELIFCTX &&
1194 i != PP_IFDEF && i != PP_ELIFDEF &&
1195 i != PP_IFID && i != PP_ELIFID &&
1196 i != PP_IFIDN && i != PP_ELIFIDN &&
1197 i != PP_IFIDNI && i != PP_ELIFIDNI &&
1198 i != PP_IFNCTX && i != PP_ELIFNCTX &&
1199 i != PP_IFNDEF && i != PP_ELIFNDEF &&
1200 i != PP_IFNID && i != PP_ELIFNID &&
1201 i != PP_IFNIDN && i != PP_ELIFNIDN &&
1202 i != PP_IFNIDNI && i != PP_ELIFNIDNI &&
1203 i != PP_IFNNUM && i != PP_ELIFNNUM &&
1204 i != PP_IFNSTR && i != PP_ELIFNSTR &&
1205 i != PP_IFNUM && i != PP_ELIFNUM &&
1206 i != PP_IFSTR && i != PP_ELIFSTR &&
1207 i != PP_ELSE && i != PP_ENDIF)
1209 return 0;
1213 * If we're defining a macro or reading a %rep block, we should
1214 * ignore all directives except for %macro/%imacro (which
1215 * generate an error), %endm/%endmacro, and (only if we're in a
1216 * %rep block) %endrep. If we're in a %rep block, another %rep
1217 * causes an error, so should be let through.
1219 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1220 i != PP_ENDMACRO && i != PP_ENDM &&
1221 (defining->name || (i != PP_ENDREP && i != PP_REP)))
1223 return 0;
1226 if (j != -2) {
1227 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1228 tline->text);
1229 return 0; /* didn't get it */
1232 switch (i) {
1234 case PP_CLEAR:
1235 if (tline->next)
1236 error(ERR_WARNING,
1237 "trailing garbage after `%%clear' ignored");
1238 for (j=0; j<NHASH; j++) {
1239 while (mmacros[j]) {
1240 MMacro *m = mmacros[j];
1241 mmacros[j] = m->next;
1242 free_mmacro(m);
1244 while (smacros[j]) {
1245 SMacro *s = smacros[j];
1246 smacros[j] = smacros[j]->next;
1247 nasm_free (s->name);
1248 free_tlist (s->expansion);
1249 nasm_free (s);
1252 free_tlist (origline);
1253 return 3;
1255 case PP_INCLUDE:
1256 tline = tline->next;
1257 skip_white_(tline);
1258 if (!tline || (tline->type != TOK_STRING &&
1259 tline->type != TOK_INTERNAL_STRING))
1261 error(ERR_NONFATAL, "`%%include' expects a file name");
1262 free_tlist (origline);
1263 return 3; /* but we did _something_ */
1265 if (tline->next)
1266 error(ERR_WARNING,
1267 "trailing garbage after `%%include' ignored");
1268 if (tline->type != TOK_INTERNAL_STRING) {
1269 p = tline->text+1; /* point past the quote to the name */
1270 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
1271 } else
1272 p = tline->text; /* internal_string is easier */
1273 inc = nasm_malloc(sizeof(Include));
1274 inc->next = istk;
1275 inc->conds = NULL;
1276 inc->fp = inc_fopen(p);
1277 inc->fname = src_set_fname(nasm_strdup(p));
1278 inc->lineno = src_set_linnum(0);
1279 inc->lineinc = 1;
1280 inc->expansion = NULL;
1281 inc->mstk = NULL;
1282 istk = inc;
1283 list->uplevel (LIST_INCLUDE);
1284 free_tlist (origline);
1285 return 5;
1287 case PP_PUSH:
1288 tline = tline->next;
1289 skip_white_(tline);
1290 if (!tok_type_(tline, TOK_ID)) {
1291 error(ERR_NONFATAL,
1292 "`%%push' expects a context identifier");
1293 free_tlist (origline);
1294 return 3; /* but we did _something_ */
1296 if (tline->next)
1297 error(ERR_WARNING,
1298 "trailing garbage after `%%push' ignored");
1299 ctx = nasm_malloc(sizeof(Context));
1300 ctx->next = cstk;
1301 ctx->localmac = NULL;
1302 ctx->name = nasm_strdup(tline->text);
1303 ctx->number = unique++;
1304 cstk = ctx;
1305 free_tlist (origline);
1306 break;
1308 case PP_REPL:
1309 tline = tline->next;
1310 skip_white_(tline);
1311 if (!tok_type_(tline, TOK_ID)) {
1312 error(ERR_NONFATAL,
1313 "`%%repl' 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 `%%repl' ignored");
1320 if (!cstk)
1321 error(ERR_NONFATAL,
1322 "`%%repl': context stack is empty");
1323 else {
1324 nasm_free (cstk->name);
1325 cstk->name = nasm_strdup(tline->text);
1327 free_tlist (origline);
1328 break;
1330 case PP_POP:
1331 if (tline->next)
1332 error(ERR_WARNING,
1333 "trailing garbage after `%%pop' ignored");
1334 if (!cstk)
1335 error(ERR_NONFATAL,
1336 "`%%pop': context stack is already empty");
1337 else
1338 ctx_pop();
1339 free_tlist (origline);
1340 break;
1342 case PP_ERROR:
1343 tline->next = expand_smacro (tline->next);
1344 tline = tline->next;
1345 skip_white_(tline);
1346 if (tok_type_(tline, TOK_STRING)) {
1347 p = tline->text+1; /* point past the quote to the name */
1348 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
1349 error(ERR_NONFATAL, "user error: %s", p);
1350 } else {
1351 p = detoken(tline);
1352 error(ERR_WARNING, "user error: %s", p);
1353 nasm_free(p);
1355 free_tlist (origline);
1356 break;
1358 case PP_IF:
1359 case PP_IFCTX:
1360 case PP_IFDEF:
1361 case PP_IFID:
1362 case PP_IFIDN:
1363 case PP_IFIDNI:
1364 case PP_IFNCTX:
1365 case PP_IFNDEF:
1366 case PP_IFNID:
1367 case PP_IFNIDN:
1368 case PP_IFNIDNI:
1369 case PP_IFNNUM:
1370 case PP_IFNSTR:
1371 case PP_IFNUM:
1372 case PP_IFSTR:
1373 if (istk->conds && !emitting(istk->conds->state))
1374 j = COND_NEVER;
1375 else {
1376 j = if_condition(tline->next, i);
1377 tline->next = NULL; /* it got freed */
1378 free_tlist (origline);
1379 if (j < 0)
1381 * Bogus expression in %if, but we should pretend
1382 * it was OK anyway, so that we don't get an error
1383 * cascade on the subsequent %else / %endif.
1385 j = COND_NEVER;
1386 else
1387 j = j ? COND_IF_TRUE : COND_IF_FALSE;
1389 cond = nasm_malloc(sizeof(Cond));
1390 cond->next = istk->conds;
1391 cond->state = j;
1392 istk->conds = cond;
1393 return (j == COND_IF_TRUE ? 3 : 1);
1395 case PP_ELIF:
1396 case PP_ELIFCTX:
1397 case PP_ELIFDEF:
1398 case PP_ELIFID:
1399 case PP_ELIFIDN:
1400 case PP_ELIFIDNI:
1401 case PP_ELIFNCTX:
1402 case PP_ELIFNDEF:
1403 case PP_ELIFNID:
1404 case PP_ELIFNIDN:
1405 case PP_ELIFNIDNI:
1406 case PP_ELIFNNUM:
1407 case PP_ELIFNSTR:
1408 case PP_ELIFNUM:
1409 case PP_ELIFSTR:
1410 if (!istk->conds)
1411 error(ERR_FATAL, "`%s': no matching `%%if'",
1412 directives[i]);
1413 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1414 istk->conds->state = COND_NEVER;
1415 else {
1416 j = if_condition(tline->next, i);
1417 tline->next = NULL; /* it got freed */
1418 free_tlist (origline);
1419 if (j < 0)
1421 * The expression was bogus, but let's make
1422 * %endif not complain about missing %if
1424 j = COND_NEVER;
1425 else
1426 istk->conds->state = j ? COND_IF_TRUE : COND_IF_FALSE;
1428 return (istk->conds->state == COND_IF_TRUE ? 5 : 1);
1430 case PP_ELSE:
1431 if (tline->next)
1432 error(ERR_WARNING,
1433 "trailing garbage after `%%else' ignored");
1434 if (!istk->conds)
1435 error(ERR_FATAL,
1436 "`%%else': no matching `%%if'");
1437 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1438 istk->conds->state = COND_ELSE_FALSE;
1439 else
1440 istk->conds->state = COND_ELSE_TRUE;
1441 free_tlist (origline);
1442 return 5;
1444 case PP_ENDIF:
1445 if (tline->next)
1446 error(ERR_WARNING,
1447 "trailing garbage after `%%endif' ignored");
1448 if (!istk->conds)
1449 error(ERR_FATAL,
1450 "`%%endif': no matching `%%if'");
1451 cond = istk->conds;
1452 istk->conds = cond->next;
1453 nasm_free (cond);
1454 free_tlist (origline);
1455 return 5;
1457 case PP_MACRO:
1458 case PP_IMACRO:
1459 if (defining)
1460 error (ERR_FATAL,
1461 "`%%%smacro': already defining a macro",
1462 (i == PP_IMACRO ? "i" : ""));
1463 tline = tline->next;
1464 skip_white_(tline);
1465 if (!tok_type_(tline, TOK_ID)) {
1466 error (ERR_NONFATAL,
1467 "`%%%smacro' expects a macro name",
1468 (i == PP_IMACRO ? "i" : ""));
1469 return 3;
1471 defining = nasm_malloc(sizeof(MMacro));
1472 defining->name = nasm_strdup(tline->text);
1473 defining->casesense = (i == PP_MACRO);
1474 defining->plus = FALSE;
1475 defining->nolist = FALSE;
1476 defining->in_progress = FALSE;
1477 defining->rep_nest = NULL;
1478 tline = tline->next;
1479 skip_white_(tline);
1480 if (!tok_type_(tline, TOK_NUMBER)) {
1481 error (ERR_NONFATAL,
1482 "`%%%smacro' expects a parameter count",
1483 (i == PP_IMACRO ? "i" : ""));
1484 defining->nparam_min = defining->nparam_max = 0;
1485 } else {
1486 defining->nparam_min = defining->nparam_max =
1487 readnum(tline->text, &j);
1488 if (j)
1489 error (ERR_NONFATAL,
1490 "unable to parse parameter count `%s'", tline->text);
1492 if (tline && tok_is_(tline->next, "-")) {
1493 tline = tline->next->next;
1494 if (tok_is_(tline, "*"))
1495 defining->nparam_max = INT_MAX;
1496 else if (!tok_type_(tline, TOK_NUMBER))
1497 error (ERR_NONFATAL,
1498 "`%%%smacro' expects a parameter count after `-'",
1499 (i == PP_IMACRO ? "i" : ""));
1500 else {
1501 defining->nparam_max = readnum(tline->text, &j);
1502 if (j)
1503 error (ERR_NONFATAL,
1504 "unable to parse parameter count `%s'",
1505 tline->text);
1506 if (defining->nparam_min > defining->nparam_max)
1507 error (ERR_NONFATAL,
1508 "minimum parameter count exceeds maximum");
1511 if (tline && tok_is_(tline->next, "+")) {
1512 tline = tline->next;
1513 defining->plus = TRUE;
1515 if (tline && tok_type_(tline->next, TOK_ID) &&
1516 !nasm_stricmp(tline->next->text, ".nolist"))
1518 tline = tline->next;
1519 defining->nolist = TRUE;
1521 mmac = mmacros[hash(defining->name)];
1522 while (mmac) {
1523 if (!strcmp(mmac->name, defining->name) &&
1524 (mmac->nparam_min<=defining->nparam_max || defining->plus) &&
1525 (defining->nparam_min<=mmac->nparam_max || mmac->plus))
1527 error (ERR_WARNING,
1528 "redefining multi-line macro `%s'", defining->name);
1529 break;
1531 mmac = mmac->next;
1534 * Handle default parameters.
1536 if (tline && tline->next) {
1537 defining->dlist = tline->next;
1538 tline->next = NULL;
1539 count_mmac_params (defining->dlist, &defining->ndefs,
1540 &defining->defaults);
1541 } else {
1542 defining->dlist = NULL;
1543 defining->defaults = NULL;
1545 defining->expansion = NULL;
1546 free_tlist (origline);
1547 return 1;
1549 case PP_ENDM:
1550 case PP_ENDMACRO:
1551 if (!defining) {
1552 error (ERR_NONFATAL, "`%s': not defining a macro",
1553 tline->text);
1554 return 3;
1556 k = hash(defining->name);
1557 defining->next = mmacros[k];
1558 mmacros[k] = defining;
1559 defining = NULL;
1560 free_tlist (origline);
1561 return 5;
1563 case PP_ROTATE:
1564 if (tline->next && tline->next->type == TOK_WHITESPACE)
1565 tline = tline->next;
1566 t = expand_smacro(tline->next);
1567 tline->next = NULL;
1568 free_tlist (origline);
1569 tline = t;
1570 tptr = &t;
1571 tokval.t_type = TOKEN_INVALID;
1572 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1573 free_tlist (tline);
1574 if (!evalresult)
1575 return 3;
1576 if (tokval.t_type)
1577 error(ERR_WARNING,
1578 "trailing garbage after expression ignored");
1579 if (!is_simple(evalresult)) {
1580 error(ERR_NONFATAL,
1581 "non-constant value given to `%%rotate'");
1582 return 3;
1584 mmac = istk->mstk;
1585 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
1586 mmac = mmac->next_active;
1587 if (!mmac)
1588 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
1589 mmac->rotate = mmac->rotate + reloc_value(evalresult);
1590 if (mmac->rotate < 0)
1591 mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam;
1592 mmac->rotate %= mmac->nparam;
1593 return 1;
1595 case PP_REP:
1596 nolist = FALSE;
1597 tline = tline->next;
1598 if (tline->next && tline->next->type == TOK_WHITESPACE)
1599 tline = tline->next;
1600 if (tline->next && tline->next->type == TOK_ID &&
1601 !nasm_stricmp(tline->next->text, ".nolist")) {
1602 tline = tline->next;
1603 nolist = TRUE;
1605 t = expand_smacro(tline->next);
1606 tline->next = NULL;
1607 free_tlist (origline);
1608 tline = t;
1609 tptr = &t;
1610 tokval.t_type = TOKEN_INVALID;
1611 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1612 free_tlist (tline);
1613 if (!evalresult)
1614 return 3;
1615 if (tokval.t_type)
1616 error(ERR_WARNING,
1617 "trailing garbage after expression ignored");
1618 if (!is_simple(evalresult)) {
1619 error(ERR_NONFATAL,
1620 "non-constant value given to `%%rep'");
1621 return 3;
1623 tmp_defining = defining;
1624 defining = nasm_malloc(sizeof(MMacro));
1625 defining->name = NULL; /* flags this macro as a %rep block */
1626 defining->casesense = 0;
1627 defining->plus = FALSE;
1628 defining->nolist = nolist;
1629 defining->in_progress = reloc_value(evalresult) + 1;
1630 defining->nparam_min = defining->nparam_max = 0;
1631 defining->defaults = NULL;
1632 defining->dlist = NULL;
1633 defining->expansion = NULL;
1634 defining->next_active = istk->mstk;
1635 defining->rep_nest = tmp_defining;
1636 return 1;
1638 case PP_ENDREP:
1639 if (!defining || defining->name) {
1640 error (ERR_NONFATAL,
1641 "`%%endrep': no matching `%%rep'");
1642 return 3;
1646 * Now we have a "macro" defined - although it has no name
1647 * and we won't be entering it in the hash tables - we must
1648 * push a macro-end marker for it on to istk->expansion.
1649 * After that, it will take care of propagating itself (a
1650 * macro-end marker line for a macro which is really a %rep
1651 * block will cause the macro to be re-expanded, complete
1652 * with another macro-end marker to ensure the process
1653 * continues) until the whole expansion is forcibly removed
1654 * from istk->expansion by a %exitrep.
1656 l = nasm_malloc(sizeof(Line));
1657 l->next = istk->expansion;
1658 l->finishes = defining;
1659 l->first = NULL;
1660 istk->expansion = l;
1662 istk->mstk = defining;
1664 list->uplevel (defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
1665 tmp_defining = defining;
1666 defining = defining->rep_nest;
1667 free_tlist (origline);
1668 return 1;
1670 case PP_EXITREP:
1672 * We must search along istk->expansion until we hit a
1673 * macro-end marker for a macro with no name. Then we set
1674 * its `in_progress' flag to 0.
1676 for (l = istk->expansion; l; l = l->next)
1677 if (l->finishes && !l->finishes->name)
1678 break;
1680 if (l)
1681 l->finishes->in_progress = 0;
1682 else
1683 error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
1684 free_tlist (origline);
1685 return 1;
1687 case PP_DEFINE:
1688 case PP_IDEFINE:
1689 tline = tline->next;
1690 skip_white_(tline);
1691 if (!tline || (tline->type != TOK_ID &&
1692 (tline->type != TOK_PREPROC_ID ||
1693 tline->text[1] != '$'))) {
1694 error (ERR_NONFATAL,
1695 "`%%%sdefine' expects a macro identifier",
1696 (i == PP_IDEFINE ? "i" : ""));
1697 free_tlist (origline);
1698 return 3;
1700 mname = tline->text;
1701 if (tline->type == TOK_ID) {
1702 p = tline->text;
1703 smhead = &smacros[hash(mname)];
1704 } else {
1705 ctx = get_ctx (tline->text);
1706 if (ctx == NULL)
1707 return 3;
1708 else {
1709 p = tline->text+1;
1710 p += strspn(p, "$");
1711 smhead = &ctx->localmac;
1714 last = tline;
1715 param_start = tline = tline->next;
1716 nparam = 0;
1717 if (tok_is_(tline, "(")) {
1719 * This macro has parameters.
1722 tline = tline->next;
1723 while (1) {
1724 skip_white_(tline);
1725 if (!tline) {
1726 error (ERR_NONFATAL,
1727 "parameter identifier expected");
1728 free_tlist (origline);
1729 return 3;
1731 if (tline->type != TOK_ID) {
1732 error (ERR_NONFATAL,
1733 "`%s': parameter identifier expected",
1734 tline->text);
1735 free_tlist (origline);
1736 return 3;
1738 tline->type = TOK_SMAC_PARAM + nparam++;
1739 tline = tline->next;
1740 skip_white_(tline);
1741 if (tok_is_(tline, ",")) {
1742 tline = tline->next;
1743 continue;
1745 if (!tok_is_(tline, ")")) {
1746 error (ERR_NONFATAL,
1747 "`)' expected to terminate macro template");
1748 free_tlist (origline);
1749 return 3;
1751 break;
1753 last = tline;
1754 tline = tline->next;
1756 if (tok_type_(tline, TOK_WHITESPACE))
1757 last = tline, tline = tline->next;
1758 macro_start = NULL;
1759 last->next = NULL;
1760 t = tline;
1761 while (t) {
1762 if (t->type == TOK_ID) {
1763 for (tt = param_start; tt; tt = tt->next)
1764 if (tt->type >= TOK_SMAC_PARAM &&
1765 !strcmp(tt->text, t->text))
1766 t->type = tt->type;
1768 tt = t->next;
1769 t->next = macro_start;
1770 macro_start = t;
1771 t = tt;
1774 * Good. We now have a macro name, a parameter count, and a
1775 * token list (in reverse order) for an expansion. We ought
1776 * to be OK just to create an SMacro, store it, and let
1777 * free_tlist have the rest of the line (which we have
1778 * carefully re-terminated after chopping off the expansion
1779 * from the end).
1781 if (smacro_defined (mname, nparam, &smac, i==PP_DEFINE)) {
1782 if (!smac) {
1783 error (ERR_WARNING,
1784 "single-line macro `%s' defined both with and"
1785 " without parameters", mname);
1786 free_tlist (origline);
1787 free_tlist (macro_start);
1788 return 3;
1789 } else {
1791 * We're redefining, so we have to take over an
1792 * existing SMacro structure. This means freeing
1793 * what was already in it.
1795 nasm_free (smac->name);
1796 free_tlist (smac->expansion);
1798 } else {
1799 smac = nasm_malloc(sizeof(SMacro));
1800 smac->next = *smhead;
1801 *smhead = smac;
1803 smac->name = nasm_strdup(p);
1804 smac->casesense = (i == PP_DEFINE);
1805 smac->nparam = nparam;
1806 smac->expansion = macro_start;
1807 smac->in_progress = FALSE;
1808 free_tlist (origline);
1809 return 3;
1811 case PP_ASSIGN:
1812 case PP_IASSIGN:
1813 tline = tline->next;
1814 skip_white_(tline);
1815 if (!tline || (tline->type != TOK_ID &&
1816 (tline->type != TOK_PREPROC_ID ||
1817 tline->text[1] != '$'))) {
1818 error (ERR_NONFATAL,
1819 "`%%%sassign' expects a macro identifier",
1820 (i == PP_IASSIGN ? "i" : ""));
1821 free_tlist (origline);
1822 return 3;
1824 mname = tline->text;
1825 if (tline->type == TOK_ID) {
1826 p = tline->text;
1827 smhead = &smacros[hash(mname)];
1828 } else {
1829 ctx = get_ctx (tline->text);
1830 if (ctx == NULL) {
1831 free_tlist (origline);
1832 return 3;
1833 } else {
1834 p = tline->text+1;
1835 p += strspn(p, "$");
1836 smhead = &ctx->localmac;
1839 last = tline;
1840 tline = tline->next;
1841 last->next = NULL;
1843 tline = expand_smacro (tline);
1844 t = tline;
1845 tptr = &t;
1846 tokval.t_type = TOKEN_INVALID;
1847 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1848 free_tlist (tline);
1849 if (!evalresult) {
1850 free_tlist (origline);
1851 return 3;
1854 if (tokval.t_type)
1855 error(ERR_WARNING,
1856 "trailing garbage after expression ignored");
1858 if (!is_simple(evalresult)) {
1859 error(ERR_NONFATAL,
1860 "non-constant value given to `%%%sassign'",
1861 (i == PP_IASSIGN ? "i" : ""));
1862 free_tlist (origline);
1863 return 3;
1866 macro_start = nasm_malloc(sizeof(*macro_start));
1867 macro_start->next = NULL;
1868 make_tok_num(macro_start, reloc_value(evalresult));
1869 macro_start->mac = NULL;
1872 * We now have a macro name, an implicit parameter count of
1873 * zero, and a numeric token to use as an expansion. Create
1874 * and store an SMacro.
1876 if (smacro_defined (mname, 0, &smac, i==PP_ASSIGN)) {
1877 if (!smac)
1878 error (ERR_WARNING,
1879 "single-line macro `%s' defined both with and"
1880 " without parameters", mname);
1881 else {
1883 * We're redefining, so we have to take over an
1884 * existing SMacro structure. This means freeing
1885 * what was already in it.
1887 nasm_free (smac->name);
1888 free_tlist (smac->expansion);
1891 else {
1892 smac = nasm_malloc(sizeof(SMacro));
1893 smac->next = *smhead;
1894 *smhead = smac;
1896 smac->name = nasm_strdup(p);
1897 smac->casesense = (i == PP_ASSIGN);
1898 smac->nparam = 0;
1899 smac->expansion = macro_start;
1900 smac->in_progress = FALSE;
1901 free_tlist (origline);
1902 return 3;
1904 case PP_LINE:
1906 * Syntax is `%line nnn[+mmm] [filename]'
1908 tline = tline->next;
1909 skip_white_(tline);
1910 if (!tok_type_(tline, TOK_NUMBER)) {
1911 error (ERR_NONFATAL, "`%%line' expects line number");
1912 free_tlist (origline);
1913 return 3;
1915 k = readnum(tline->text, &j);
1916 m = 1;
1917 tline = tline->next;
1918 if (tok_is_(tline, "+")) {
1919 tline = tline->next;
1920 if (!tok_type_(tline, TOK_NUMBER)) {
1921 error (ERR_NONFATAL,
1922 "`%%line' expects line increment");
1923 free_tlist (origline);
1924 return 3;
1926 m = readnum(tline->text, &j);
1927 tline = tline->next;
1929 skip_white_(tline);
1930 src_set_linnum(k);
1931 istk->lineinc = m;
1932 if (tline) {
1933 nasm_free ( src_set_fname ( detoken(tline) ) );
1935 free_tlist (origline);
1936 return 5;
1938 default:
1939 error(ERR_FATAL,
1940 "preprocessor directive `%s' not yet implemented",
1941 directives[i]);
1942 break;
1944 return 3;
1948 * Ensure that a macro parameter contains a condition code and
1949 * nothing else. Return the condition code index if so, or -1
1950 * otherwise.
1952 static int find_cc (Token *t)
1954 Token *tt;
1955 int i, j, k, m;
1957 skip_white_(t);
1958 if (t->type != TOK_ID)
1959 return -1;
1960 tt = t->next;
1961 skip_white_(tt);
1962 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
1963 return -1;
1965 i = -1;
1966 j = sizeof(conditions)/sizeof(*conditions);
1967 while (j-i > 1) {
1968 k = (j+i) / 2;
1969 m = nasm_stricmp(t->text, conditions[k]);
1970 if (m == 0) {
1971 i = k;
1972 j = -2;
1973 break;
1974 } else if (m < 0) {
1975 j = k;
1976 } else
1977 i = k;
1979 if (j != -2)
1980 return -1;
1981 return i;
1985 * Expand MMacro-local things: parameter references (%0, %n, %+n,
1986 * %-n) and MMacro-local identifiers (%%foo).
1988 static Token *expand_mmac_params (Token *tline)
1990 Token *t, *tt, *ttt, **tail, *thead;
1992 tail = &thead;
1993 thead = NULL;
1995 while (tline) {
1996 if (tline->type == TOK_PREPROC_ID &&
1997 (tline->text[1] == '+' || tline->text[1] == '-' ||
1998 tline->text[1] == '%' ||
1999 (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2000 char *text = NULL;
2001 int type = 0, cc; /* type = 0 to placate optimisers */
2002 char tmpbuf[30];
2003 int n, i;
2004 MMacro *mac;
2006 t = tline;
2007 tline = tline->next;
2009 mac = istk->mstk;
2010 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2011 mac = mac->next_active;
2012 if (!mac)
2013 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2014 else switch (t->text[1]) {
2016 * We have to make a substitution of one of the
2017 * forms %1, %-1, %+1, %%foo, %0.
2019 case '0':
2020 type = TOK_NUMBER;
2021 sprintf(tmpbuf, "%d", mac->nparam);
2022 text = nasm_strdup(tmpbuf);
2023 break;
2024 case '%':
2025 type = TOK_ID;
2026 sprintf(tmpbuf, "..@%lu.", mac->unique);
2027 text = nasm_strcat(tmpbuf, t->text+2);
2028 break;
2029 case '-':
2030 n = atoi(t->text+2)-1;
2031 if (n >= mac->nparam)
2032 tt = NULL;
2033 else {
2034 if (mac->nparam > 1)
2035 n = (n + mac->rotate) % mac->nparam;
2036 tt = mac->params[n];
2038 cc = find_cc (tt);
2039 if (cc == -1) {
2040 error (ERR_NONFATAL,
2041 "macro parameter %d is not a condition code",
2042 n+1);
2043 text = NULL;
2044 } else {
2045 type = TOK_ID;
2046 if (inverse_ccs[cc] == -1) {
2047 error (ERR_NONFATAL,
2048 "condition code `%s' is not invertible",
2049 conditions[cc]);
2050 text = NULL;
2051 } else
2052 text = nasm_strdup(conditions[inverse_ccs[cc]]);
2054 break;
2055 case '+':
2056 n = atoi(t->text+2)-1;
2057 if (n >= mac->nparam)
2058 tt = NULL;
2059 else {
2060 if (mac->nparam > 1)
2061 n = (n + mac->rotate) % mac->nparam;
2062 tt = mac->params[n];
2064 cc = find_cc (tt);
2065 if (cc == -1) {
2066 error (ERR_NONFATAL,
2067 "macro parameter %d is not a condition code",
2068 n+1);
2069 text = NULL;
2070 } else {
2071 type = TOK_ID;
2072 text = nasm_strdup(conditions[cc]);
2074 break;
2075 default:
2076 n = atoi(t->text+1)-1;
2077 if (n >= mac->nparam)
2078 tt = NULL;
2079 else {
2080 if (mac->nparam > 1)
2081 n = (n + mac->rotate) % mac->nparam;
2082 tt = mac->params[n];
2084 if (tt) {
2085 for (i=0; i<mac->paramlen[n]; i++) {
2086 ttt = *tail = nasm_malloc(sizeof(Token));
2087 tail = &ttt->next;
2088 ttt->type = tt->type;
2089 ttt->text = nasm_strdup(tt->text);
2090 ttt->mac = NULL;
2091 tt = tt->next;
2094 text = NULL; /* we've done it here */
2095 break;
2097 nasm_free (t->text);
2098 if (!text) {
2099 nasm_free (t);
2100 } else {
2101 *tail = t;
2102 tail = &t->next;
2103 t->type = type;
2104 t->text = text;
2105 t->mac = NULL;
2107 continue;
2108 } else {
2109 t = *tail = tline;
2110 tline = tline->next;
2111 t->mac = NULL;
2112 tail = &t->next;
2115 *tail = NULL;
2116 t = thead;
2117 for (; t && (tt=t->next)!=NULL ; t = t->next)
2118 switch (t->type) {
2119 case TOK_WHITESPACE:
2120 if (tt->type == TOK_WHITESPACE) {
2121 t->next = tt->next;
2122 nasm_free(tt->text);
2123 nasm_free(tt);
2125 break;
2126 case TOK_ID:
2127 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2128 char *tmp = nasm_strcat(t->text, tt->text);
2129 nasm_free(t->text);
2130 t->text = tmp;
2131 t->next = tt->next;
2132 nasm_free(tt->text);
2133 nasm_free(tt);
2135 break;
2136 case TOK_NUMBER:
2137 if (tt->type == TOK_NUMBER) {
2138 char *tmp = nasm_strcat(t->text, tt->text);
2139 nasm_free(t->text);
2140 t->text = tmp;
2141 t->next = tt->next;
2142 nasm_free(tt->text);
2143 nasm_free(tt);
2145 break;
2148 return thead;
2152 * Expand all single-line macro calls made in the given line.
2153 * Return the expanded version of the line. The original is deemed
2154 * to be destroyed in the process. (In reality we'll just move
2155 * Tokens from input to output a lot of the time, rather than
2156 * actually bothering to destroy and replicate.)
2158 static Token *expand_smacro (Token *tline)
2160 Token *t, *tt, *mstart, **tail, *thead;
2161 SMacro *head = NULL, *m;
2162 Token **params;
2163 int *paramsize;
2164 int nparam, sparam, brackets;
2165 char *p;
2167 tail = &thead;
2168 thead = NULL;
2170 while (tline) { /* main token loop */
2171 p = NULL;
2172 if (tline->type == TOK_ID) {
2173 head = smacros[hash(tline->text)];
2174 p = tline->text;
2175 } else if (tline->type == TOK_PREPROC_ID && tline->text[1] == '$') {
2176 Context *ctx = get_ctx (tline->text);
2177 if (ctx) {
2178 head = ctx->localmac;
2179 p = tline->text+2;
2180 p += strspn(p, "$");
2183 if (p) {
2185 * We've hit an identifier. As in is_mmacro below, we first
2186 * check whether the identifier is a single-line macro at
2187 * all, then think about checking for parameters if
2188 * necessary.
2190 for (m = head; m; m = m->next)
2191 if (!mstrcmp(m->name, p, m->casesense))
2192 break;
2193 if (m) {
2194 mstart = tline;
2195 params = NULL;
2196 paramsize = NULL;
2197 if (m->nparam == 0) {
2199 * Simple case: the macro is parameterless. Discard the
2200 * one token that the macro call took, and push the
2201 * expansion back on the to-do stack.
2203 if (!m->expansion)
2205 if (!strcmp("__FILE__", m->name)) {
2206 long num=0;
2207 src_get(&num, &(tline->text));
2208 nasm_quote(&(tline->text));
2209 tline->type = TOK_STRING;
2210 continue;
2212 if (!strcmp("__LINE__", m->name)) {
2213 nasm_free(tline->text);
2214 make_tok_num(tline, src_get_linnum());
2215 continue;
2217 t = tline;
2218 tline = tline->next;
2219 nasm_free (t->text);
2220 nasm_free (t);
2221 continue;
2224 else {
2226 * Complicated case: at least one macro with this name
2227 * exists and takes parameters. We must find the
2228 * parameters in the call, count them, find the SMacro
2229 * that corresponds to that form of the macro call, and
2230 * substitute for the parameters when we expand. What a
2231 * pain.
2233 tline = tline->next;
2234 skip_white_(tline);
2235 if (!tok_is_(tline, "(")) {
2237 * This macro wasn't called with parameters: ignore
2238 * the call. (Behaviour borrowed from gnu cpp.)
2240 tline = mstart;
2241 m = NULL;
2243 else {
2244 int paren = 0;
2245 int white = 0;
2246 brackets = 0;
2247 nparam = 0;
2248 tline = tline->next;
2249 sparam = PARAM_DELTA;
2250 params = nasm_malloc (sparam*sizeof(Token *));
2251 params[0] = tline;
2252 paramsize = nasm_malloc (sparam*sizeof(int));
2253 paramsize[0] = 0;
2254 for (;;tline = tline->next) { /* parameter loop */
2255 if (!tline) {
2256 error(ERR_NONFATAL,
2257 "macro call expects terminating `)'");
2258 break;
2260 if (tline->type == TOK_WHITESPACE && brackets<=0) {
2261 if (paramsize[nparam])
2262 white++;
2263 else
2264 params[nparam] = tline->next;
2265 continue; /* parameter loop */
2267 if (tline->type == TOK_OTHER && tline->text[1]==0) {
2268 char ch = tline->text[0];
2269 if (ch == ',' && !paren && brackets<=0) {
2270 if (++nparam >= sparam) {
2271 sparam += PARAM_DELTA;
2272 params = nasm_realloc (params,
2273 sparam*sizeof(Token *));
2274 paramsize = nasm_realloc (paramsize,
2275 sparam*sizeof(int));
2277 params[nparam] = tline->next;
2278 paramsize[nparam] = 0;
2279 white = 0;
2280 continue; /* parameter loop */
2282 if (ch == br0 &&
2283 (brackets>0 || (brackets==0 &&
2284 !paramsize[nparam])))
2286 if (!(brackets++))
2288 params[nparam] = tline->next;
2289 continue; /* parameter loop */
2292 if (ch == br2 && brackets>0)
2293 if (--brackets == 0) {
2294 brackets = -1;
2295 continue; /* parameter loop */
2297 if (ch == '(' && !brackets)
2298 paren++;
2299 if (ch == ')' && brackets<=0)
2300 if (--paren < 0)
2301 break;
2303 if (brackets<0) {
2304 brackets = 0;
2305 error (ERR_NONFATAL, "braces do not "
2306 "enclose all of macro parameter");
2308 paramsize[nparam] += white+1;
2309 white = 0;
2310 } /* parameter loop */
2311 nparam++;
2312 while (m && (m->nparam != nparam ||
2313 mstrcmp(m->name, p, m->casesense)))
2314 m = m->next;
2315 if (!m)
2316 error (ERR_WARNING|ERR_WARN_MNP,
2317 "macro `%s' exists, "
2318 "but not taking %d parameters",
2319 mstart->text, nparam);
2322 if (m && m->in_progress)
2323 m = NULL;
2324 if (!m) /* in progess or didn't find '(' or wrong nparam */
2327 * Design question: should we handle !tline, which
2328 * indicates missing ')' here, or expand those
2329 * macros anyway, which requires the (t) test a few
2330 * lines down?
2332 nasm_free (params);
2333 nasm_free (paramsize);
2334 tline = mstart;
2336 else {
2338 * Expand the macro: we are placed on the last token of the
2339 * call, so that we can easily split the call from the
2340 * following tokens. We also start by pushing an SMAC_END
2341 * token for the cycle removal.
2343 t = tline;
2344 if (t) {
2345 tline = t->next;
2346 t->next = NULL;
2348 tt = nasm_malloc(sizeof(Token));
2349 tt->type = TOK_SMAC_END;
2350 tt->text = NULL;
2351 tt->mac = m;
2352 m->in_progress = TRUE;
2353 tt->next = tline;
2354 tline = tt;
2355 for (t = m->expansion; t; t = t->next) {
2356 if (t->type >= TOK_SMAC_PARAM) {
2357 Token *pcopy = tline, **ptail = &pcopy;
2358 Token *ttt, *pt;
2359 int i;
2361 ttt = params[t->type - TOK_SMAC_PARAM];
2362 for (i=paramsize[t->type-TOK_SMAC_PARAM]; --i>=0;) {
2363 pt = *ptail = nasm_malloc(sizeof(Token));
2364 pt->next = tline;
2365 ptail = &pt->next;
2366 pt->text = nasm_strdup(ttt->text);
2367 pt->type = ttt->type;
2368 pt->mac = NULL;
2369 ttt = ttt->next;
2371 tline = pcopy;
2372 } else {
2373 tt = nasm_malloc(sizeof(Token));
2374 tt->type = t->type;
2375 tt->text = nasm_strdup(t->text);
2376 tt->mac = NULL;
2377 tt->next = tline;
2378 tline = tt;
2383 * Having done that, get rid of the macro call, and clean
2384 * up the parameters.
2386 nasm_free (params);
2387 nasm_free (paramsize);
2388 free_tlist (mstart);
2389 continue; /* main token loop */
2394 if (tline->type == TOK_SMAC_END) {
2395 tline->mac->in_progress = FALSE;
2396 t = tline;
2397 tline = tline->next;
2398 nasm_free (t);
2399 } else {
2400 t = *tail = tline;
2401 tline = tline->next;
2402 t->mac = NULL;
2403 t->next = NULL;
2404 tail = &t->next;
2405 if (t->type == TOK_PREPROC_ID && t->text[1] == '$') {
2406 Context *c = get_ctx (t->text);
2407 char *p, *q, buffer[40];
2409 t->type = TOK_ID;
2410 if (c) {
2411 q = t->text+1;
2412 q += strspn(q, "$");
2413 sprintf(buffer, "..@%lu.", c->number);
2414 p = nasm_strcat (buffer,q);
2415 nasm_free (t->text);
2416 t->text = p;
2422 return thead;
2426 * Determine whether the given line constitutes a multi-line macro
2427 * call, and return the MMacro structure called if so. Doesn't have
2428 * to check for an initial label - that's taken care of in
2429 * expand_mmacro - but must check numbers of parameters. Guaranteed
2430 * to be called with tline->type == TOK_ID, so the putative macro
2431 * name is easy to find.
2433 static MMacro *is_mmacro (Token *tline, Token ***params_array)
2435 MMacro *head, *m;
2436 Token **params;
2437 int nparam;
2439 head = mmacros[hash(tline->text)];
2442 * Efficiency: first we see if any macro exists with the given
2443 * name. If not, we can return NULL immediately. _Then_ we
2444 * count the parameters, and then we look further along the
2445 * list if necessary to find the proper MMacro.
2447 for (m = head; m; m = m->next)
2448 if (!mstrcmp(m->name, tline->text, m->casesense))
2449 break;
2450 if (!m)
2451 return NULL;
2454 * OK, we have a potential macro. Count and demarcate the
2455 * parameters.
2457 count_mmac_params (tline->next, &nparam, &params);
2460 * So we know how many parameters we've got. Find the MMacro
2461 * structure that handles this number.
2463 while (m) {
2464 if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) {
2466 * This one is right. Just check if cycle removal
2467 * prohibits us using it before we actually celebrate...
2469 if (m->in_progress) {
2470 #if 0
2471 error (ERR_NONFATAL,
2472 "self-reference in multi-line macro `%s'",
2473 m->name);
2474 #endif
2475 nasm_free (params);
2476 return NULL;
2479 * It's right, and we can use it. Add its default
2480 * parameters to the end of our list if necessary.
2482 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
2483 params = nasm_realloc (params, ((m->nparam_min+m->ndefs+1) *
2484 sizeof(*params)));
2485 while (nparam < m->nparam_min + m->ndefs) {
2486 params[nparam] = m->defaults[nparam - m->nparam_min];
2487 nparam++;
2491 * If we've gone over the maximum parameter count (and
2492 * we're in Plus mode), ignore parameters beyond
2493 * nparam_max.
2495 if (m->plus && nparam > m->nparam_max)
2496 nparam = m->nparam_max;
2498 * Then terminate the parameter list, and leave.
2500 if (!params) { /* need this special case */
2501 params = nasm_malloc(sizeof(*params));
2502 nparam = 0;
2504 params[nparam] = NULL;
2505 *params_array = params;
2506 return m;
2509 * This one wasn't right: look for the next one with the
2510 * same name.
2512 for (m = m->next; m; m = m->next)
2513 if (!mstrcmp(m->name, tline->text, m->casesense))
2514 break;
2518 * After all that, we didn't find one with the right number of
2519 * parameters. Issue a warning, and fail to expand the macro.
2521 error (ERR_WARNING|ERR_WARN_MNP,
2522 "macro `%s' exists, but not taking %d parameters",
2523 tline->text, nparam);
2524 nasm_free (params);
2525 return NULL;
2529 * Expand the multi-line macro call made by the given line, if
2530 * there is one to be expanded. If there is, push the expansion on
2531 * istk->expansion and return 1. Otherwise return 0.
2533 static int expand_mmacro (Token *tline)
2535 Token *startline = tline;
2536 Token *label = NULL;
2537 int dont_prepend = 0;
2538 Token **params, *t, *tt;
2539 MMacro *m;
2540 Line *l, *ll;
2541 int i, nparam, *paramlen;
2543 t = tline;
2544 skip_white_(t);
2545 if (!tok_type_(t, TOK_ID))
2546 return 0;
2547 m = is_mmacro (t, &params);
2548 if (!m) {
2549 Token *last;
2551 * We have an id which isn't a macro call. We'll assume
2552 * it might be a label; we'll also check to see if a
2553 * colon follows it. Then, if there's another id after
2554 * that lot, we'll check it again for macro-hood.
2556 label = last = t;
2557 t = t->next;
2558 if (tok_type_(t, TOK_WHITESPACE))
2559 last = t, t = t->next;
2560 if (tok_is_(t, ":")) {
2561 dont_prepend = 1;
2562 last = t, t = t->next;
2563 if (tok_type_(t, TOK_WHITESPACE))
2564 last = t, t = t->next;
2566 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
2567 return 0;
2568 last->next = NULL;
2569 tline = t;
2573 * Fix up the parameters: this involves stripping leading and
2574 * trailing whitespace, then stripping braces if they are
2575 * present.
2577 for (nparam = 0; params[nparam]; nparam++)
2579 paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL;
2581 for (i = 0; params[i]; i++) {
2582 int brace = FALSE;
2583 int comma = (!m->plus || i < nparam-1);
2585 t = params[i];
2586 skip_white_(t);
2587 if (tok_is_(t, "{"))
2588 t = t->next, brace = TRUE, comma = FALSE;
2589 params[i] = t;
2590 paramlen[i] = 0;
2591 while (t) {
2592 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
2593 break; /* ... because we have hit a comma */
2594 if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
2595 break; /* ... or a space then a comma */
2596 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
2597 break; /* ... or a brace */
2598 t = t->next;
2599 paramlen[i]++;
2604 * OK, we have a MMacro structure together with a set of
2605 * parameters. We must now go through the expansion and push
2606 * copies of each Line on to istk->expansion. Substitution of
2607 * parameter tokens and macro-local tokens doesn't get done
2608 * until the single-line macro substitution process; this is
2609 * because delaying them allows us to change the semantics
2610 * later through %rotate.
2612 * First, push an end marker on to istk->expansion, mark this
2613 * macro as in progress, and set up its invocation-specific
2614 * variables.
2616 ll = nasm_malloc(sizeof(Line));
2617 ll->next = istk->expansion;
2618 ll->finishes = m;
2619 ll->first = NULL;
2620 istk->expansion = ll;
2622 m->in_progress = TRUE;
2623 m->params = params;
2624 m->iline = tline;
2625 m->nparam = nparam;
2626 m->rotate = 0;
2627 m->paramlen = paramlen;
2628 m->unique = unique++;
2630 m->next_active = istk->mstk;
2631 istk->mstk = m;
2633 for (l = m->expansion; l; l = l->next) {
2634 Token **tail;
2636 ll = nasm_malloc(sizeof(Line));
2637 ll->finishes = NULL;
2638 ll->next = istk->expansion;
2639 istk->expansion = ll;
2640 tail = &ll->first;
2642 for (t = l->first; t; t = t->next) {
2643 Token *x = t;
2644 if (t->type == TOK_PREPROC_ID &&
2645 t->text[1]=='0' && t->text[2]=='0')
2647 dont_prepend = -1;
2648 x = label;
2649 if (!x)
2650 continue;
2652 tt = *tail = nasm_malloc(sizeof(Token));
2653 tail = &tt->next;
2654 tt->type = x->type;
2655 tt->text = nasm_strdup(x->text);
2656 tt->mac = NULL;
2658 *tail = NULL;
2662 * If we had a label, push it on as the first line of
2663 * the macro expansion.
2665 if (label)
2666 if (dont_prepend<0)
2667 free_tlist(startline);
2668 else {
2669 ll = nasm_malloc(sizeof(Line));
2670 ll->finishes = NULL;
2671 ll->next = istk->expansion;
2672 istk->expansion = ll;
2673 ll->first = startline;
2674 if (!dont_prepend) {
2675 while (label->next)
2676 label = label->next;
2677 label->next = tt = nasm_malloc(sizeof(Token));
2678 tt->next = NULL;
2679 tt->mac = NULL;
2680 tt->type = TOK_OTHER;
2681 tt->text = nasm_strdup(":");
2685 list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2687 return 1;
2690 static void pp_reset (char *file, int apass, efunc errfunc, evalfunc eval,
2691 ListGen *listgen)
2693 int h;
2695 error = errfunc;
2696 cstk = NULL;
2697 istk = nasm_malloc(sizeof(Include));
2698 istk->next = NULL;
2699 istk->conds = NULL;
2700 istk->expansion = NULL;
2701 istk->mstk = NULL;
2702 istk->fp = fopen(file, "r");
2703 istk->fname = NULL;
2704 src_set_fname(nasm_strdup(file));
2705 src_set_linnum(0);
2706 istk->lineinc = 1;
2707 if (!istk->fp)
2708 error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file);
2709 defining = NULL;
2710 for (h=0; h<NHASH; h++) {
2711 mmacros[h] = NULL;
2712 smacros[h] = NULL;
2714 unique = 0;
2715 stdmacpos = stdmac;
2716 any_extrastdmac = (extrastdmac != NULL);
2717 list = listgen;
2718 evaluate = eval;
2719 pass = apass;
2722 static char *pp_getline (void)
2724 char *line;
2725 Token *tline;
2726 int ret;
2728 while (1) {
2730 * Fetch a tokenised line, either from the macro-expansion
2731 * buffer or from the input file.
2733 tline = NULL;
2734 while (istk->expansion && istk->expansion->finishes) {
2735 Line *l = istk->expansion;
2736 if (!l->finishes->name && l->finishes->in_progress > 1) {
2737 Line *ll;
2740 * This is a macro-end marker for a macro with no
2741 * name, which means it's not really a macro at all
2742 * but a %rep block, and the `in_progress' field is
2743 * more than 1, meaning that we still need to
2744 * repeat. (1 means the natural last repetition; 0
2745 * means termination by %exitrep.) We have
2746 * therefore expanded up to the %endrep, and must
2747 * push the whole block on to the expansion buffer
2748 * again. We don't bother to remove the macro-end
2749 * marker: we'd only have to generate another one
2750 * if we did.
2752 l->finishes->in_progress--;
2753 for (l = l->finishes->expansion; l; l = l->next) {
2754 Token *t, *tt, **tail;
2756 ll = nasm_malloc(sizeof(Line));
2757 ll->next = istk->expansion;
2758 ll->finishes = NULL;
2759 ll->first = NULL;
2760 tail = &ll->first;
2762 for (t = l->first; t; t = t->next) {
2763 if (t->text) {
2764 tt = *tail = nasm_malloc(sizeof(Token));
2765 tt->next = NULL;
2766 tail = &tt->next;
2767 tt->type = t->type;
2768 tt->text = nasm_strdup(t->text);
2769 tt->mac = NULL;
2773 istk->expansion = ll;
2775 } else {
2777 * Check whether a `%rep' was started and not ended
2778 * within this macro expansion. This can happen and
2779 * should be detected. It's a fatal error because
2780 * I'm too confused to work out how to recover
2781 * sensibly from it.
2783 if (defining) {
2784 if (defining->name)
2785 error (ERR_PANIC,
2786 "defining with name in expansion");
2787 else if (istk->mstk->name)
2788 error (ERR_FATAL, "`%%rep' without `%%endrep' within"
2789 " expansion of macro `%s'", istk->mstk->name);
2793 * FIXME: investigate the relationship at this point between
2794 * istk->mstk and l->finishes
2797 MMacro *m = istk->mstk;
2798 istk->mstk = m->next_active;
2799 if (m->name) {
2801 * This was a real macro call, not a %rep, and
2802 * therefore the parameter information needs to
2803 * be freed.
2805 nasm_free(m->params);
2806 free_tlist(m->iline);
2807 nasm_free(m->paramlen);
2808 l->finishes->in_progress = FALSE;
2810 else
2811 free_mmacro(m);
2813 istk->expansion = l->next;
2814 nasm_free (l);
2815 list->downlevel (LIST_MACRO);
2818 while (1) { /* until we get a line we can use */
2820 if (istk->expansion) { /* from a macro expansion */
2821 char *p;
2822 Line *l = istk->expansion;
2823 tline = l->first;
2824 istk->expansion = l->next;
2825 nasm_free (l);
2826 p = detoken(tline);
2827 list->line (LIST_MACRO, p);
2828 nasm_free(p);
2829 break;
2831 line = read_line();
2832 if (line) { /* from the current input file */
2833 line = prepreproc(line);
2834 tline = tokenise(line);
2835 nasm_free (line);
2836 break;
2839 * The current file has ended; work down the istk
2842 Include *i = istk;
2843 fclose(i->fp);
2844 if (i->conds)
2845 error(ERR_FATAL, "expected `%%endif' before end of file");
2846 istk = i->next;
2847 list->downlevel (LIST_INCLUDE);
2848 src_set_linnum(i->lineno);
2849 nasm_free ( src_set_fname(i->fname) );
2850 nasm_free (i);
2851 if (!istk)
2852 return NULL;
2857 * We must expand MMacro parameters and MMacro-local labels
2858 * _before_ we plunge into directive processing, to cope
2859 * with things like `%define something %1' such as STRUC
2860 * uses. Unless we're _defining_ a MMacro, in which case
2861 * those tokens should be left alone to go into the
2862 * definition; and unless we're in a non-emitting
2863 * condition, in which case we don't want to meddle with
2864 * anything.
2866 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
2867 tline = expand_mmac_params(tline);
2870 * Check the line to see if it's a preprocessor directive.
2872 ret = do_directive(tline);
2873 if (ret & 1) {
2874 continue;
2875 } else if (defining) {
2877 * We're defining a multi-line macro. We emit nothing
2878 * at all, and just
2879 * shove the tokenised line on to the macro definition.
2881 Line *l = nasm_malloc(sizeof(Line));
2882 l->next = defining->expansion;
2883 l->first = tline;
2884 l->finishes = FALSE;
2885 defining->expansion = l;
2886 continue;
2887 } else if (istk->conds && !emitting(istk->conds->state)) {
2889 * We're in a non-emitting branch of a condition block.
2890 * Emit nothing at all, not even a blank line: when we
2891 * emerge from the condition we'll give a line-number
2892 * directive so we keep our place correctly.
2894 free_tlist(tline);
2895 continue;
2896 } else if (istk->mstk && !istk->mstk->in_progress) {
2898 * We're in a %rep block which has been terminated, so
2899 * we're walking through to the %endrep without
2900 * emitting anything. Emit nothing at all, not even a
2901 * blank line: when we emerge from the %rep block we'll
2902 * give a line-number directive so we keep our place
2903 * correctly.
2905 free_tlist(tline);
2906 continue;
2907 } else {
2908 tline = expand_smacro(tline);
2909 ret = expand_mmacro(tline);
2910 if (!ret) {
2912 * De-tokenise the line again, and emit it.
2914 line = detoken(tline);
2915 free_tlist (tline);
2916 break;
2917 } else {
2918 continue; /* expand_mmacro calls free_tlist */
2923 return line;
2926 static void pp_cleanup (void)
2928 int h;
2930 if (defining) {
2931 error (ERR_NONFATAL, "end of file while still defining macro `%s'",
2932 defining->name);
2933 free_mmacro (defining);
2935 while (cstk)
2936 ctx_pop();
2937 for (h=0; h<NHASH; h++) {
2938 while (mmacros[h]) {
2939 MMacro *m = mmacros[h];
2940 mmacros[h] = mmacros[h]->next;
2941 free_mmacro(m);
2943 while (smacros[h]) {
2944 SMacro *s = smacros[h];
2945 smacros[h] = smacros[h]->next;
2946 nasm_free (s->name);
2947 free_tlist (s->expansion);
2948 nasm_free (s);
2951 while (istk) {
2952 Include *i = istk;
2953 istk = istk->next;
2954 fclose(i->fp);
2955 nasm_free (i->fname);
2956 nasm_free (i);
2958 while (cstk)
2959 ctx_pop();
2962 void pp_include_path (char *path)
2964 IncPath *i;
2966 i = nasm_malloc(sizeof(IncPath));
2967 i->path = nasm_strdup(path);
2968 i->next = ipath;
2970 ipath = i;
2973 void pp_pre_include (char *fname)
2975 Token *inc, *space, *name;
2976 Line *l;
2978 inc = nasm_malloc(sizeof(Token));
2979 inc->next = space = nasm_malloc(sizeof(Token));
2980 space->next = name = nasm_malloc(sizeof(Token));
2981 name->next = NULL;
2983 inc->type = TOK_PREPROC_ID;
2984 inc->text = nasm_strdup("%include");
2985 space->type = TOK_WHITESPACE;
2986 space->text = nasm_strdup(" ");
2987 name->type = TOK_INTERNAL_STRING;
2988 name->text = nasm_strdup(fname);
2990 inc->mac = space->mac = name->mac = NULL;
2992 l = nasm_malloc(sizeof(Line));
2993 l->next = predef;
2994 l->first = inc;
2995 l->finishes = FALSE;
2996 predef = l;
2999 void pp_pre_define (char *definition)
3001 Token *def, *space;
3002 Line *l;
3003 char *equals;
3005 equals = strchr(definition, '=');
3007 def = nasm_malloc(sizeof(Token));
3008 def->next = space = nasm_malloc(sizeof(Token));
3009 if (equals)
3010 *equals = ' ';
3011 space->next = tokenise(definition);
3012 if (equals)
3013 *equals = '=';
3015 def->type = TOK_PREPROC_ID;
3016 def->text = nasm_strdup("%define");
3017 space->type = TOK_WHITESPACE;
3018 space->text = nasm_strdup(" ");
3020 def->mac = space->mac = NULL;
3022 l = nasm_malloc(sizeof(Line));
3023 l->next = predef;
3024 l->first = def;
3025 l->finishes = FALSE;
3026 predef = l;
3029 void pp_extra_stdmac (char **macros)
3031 extrastdmac = macros;
3034 static void make_tok_num(Token *tok, long val)
3036 char numbuf[20];
3037 sprintf(numbuf, "%ld", val);
3038 tok->text = nasm_strdup(numbuf);
3039 tok->type = TOK_NUMBER;
3042 Preproc nasmpp = {
3043 pp_reset,
3044 pp_getline,
3045 pp_cleanup