Formatting: kill off "stealth whitespace"
[platform/upstream/nasm.git] / preproc.c
1 /* preproc.c   macro preprocessor for the Netwide Assembler
2  *
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.
7  *
8  * initial version 18/iii/97 by Simon Tatham
9  */
10
11 /* Typical flow of text through preproc
12  *
13  * pp_getline gets tokenized lines, either
14  *
15  *   from a macro expansion
16  *
17  * or
18  *   {
19  *   read_line  gets raw text from stdmacpos, or predef, or current input file
20  *   tokenize   converts to tokens
21  *   }
22  *
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
26  *
27  * do_directive checks for directives
28  *
29  * expand_smacro is used to expand single line macros
30  *
31  * expand_mmacro is used to expand multi-line macros
32  *
33  * detoken is used to convert the line back to text
34  */
35
36 #include "compiler.h"
37
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
46
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51
52 typedef struct SMacro SMacro;
53 typedef struct MMacro MMacro;
54 typedef struct Context Context;
55 typedef struct Token Token;
56 typedef struct Blocks Blocks;
57 typedef struct Line Line;
58 typedef struct Include Include;
59 typedef struct Cond Cond;
60 typedef struct IncPath IncPath;
61
62 /*
63  * Note on the storage of both SMacro and MMacros: the hash table
64  * indexes them case-insensitively, and we then have to go through a
65  * linked list of potential case aliases (and, for MMacros, parameter
66  * ranges); this is to preserve the matching semantics of the earlier
67  * code.  If the number of case aliases for a specific macro is a
68  * performance issue, you may want to reconsider your coding style.
69  */
70
71 /*
72  * Store the definition of a single-line macro.
73  */
74 struct SMacro {
75     SMacro *next;
76     char *name;
77     bool casesense;
78     bool in_progress;
79     unsigned int nparam;
80     Token *expansion;
81 };
82
83 /*
84  * Store the definition of a multi-line macro. This is also used to
85  * store the interiors of `%rep...%endrep' blocks, which are
86  * effectively self-re-invoking multi-line macros which simply
87  * don't have a name or bother to appear in the hash tables. %rep
88  * blocks are signified by having a NULL `name' field.
89  *
90  * In a MMacro describing a `%rep' block, the `in_progress' field
91  * isn't merely boolean, but gives the number of repeats left to
92  * run.
93  *
94  * The `next' field is used for storing MMacros in hash tables; the
95  * `next_active' field is for stacking them on istk entries.
96  *
97  * When a MMacro is being expanded, `params', `iline', `nparam',
98  * `paramlen', `rotate' and `unique' are local to the invocation.
99  */
100 struct MMacro {
101     MMacro *next;
102     char *name;
103     int nparam_min, nparam_max;
104     bool casesense;
105     bool plus;                   /* is the last parameter greedy? */
106     bool nolist;                 /* is this macro listing-inhibited? */
107     int64_t in_progress;
108     Token *dlist;               /* All defaults as one list */
109     Token **defaults;           /* Parameter default pointers */
110     int ndefs;                  /* number of default parameters */
111     Line *expansion;
112
113     MMacro *next_active;
114     MMacro *rep_nest;           /* used for nesting %rep */
115     Token **params;             /* actual parameters */
116     Token *iline;               /* invocation line */
117     unsigned int nparam, rotate;
118     int *paramlen;
119     uint64_t unique;
120     int lineno;                 /* Current line number on expansion */
121 };
122
123 /*
124  * The context stack is composed of a linked list of these.
125  */
126 struct Context {
127     Context *next;
128     SMacro *localmac;
129     char *name;
130     uint32_t number;
131 };
132
133 /*
134  * This is the internal form which we break input lines up into.
135  * Typically stored in linked lists.
136  *
137  * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
138  * necessarily used as-is, but is intended to denote the number of
139  * the substituted parameter. So in the definition
140  *
141  *     %define a(x,y) ( (x) & ~(y) )
142  *
143  * the token representing `x' will have its type changed to
144  * TOK_SMAC_PARAM, but the one representing `y' will be
145  * TOK_SMAC_PARAM+1.
146  *
147  * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
148  * which doesn't need quotes around it. Used in the pre-include
149  * mechanism as an alternative to trying to find a sensible type of
150  * quote to use on the filename we were passed.
151  */
152 enum pp_token_type {
153     TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
154     TOK_PREPROC_ID, TOK_STRING,
155     TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
156     TOK_INTERNAL_STRING
157 };
158
159 struct Token {
160     Token *next;
161     char *text;
162     SMacro *mac;                /* associated macro for TOK_SMAC_END */
163     enum pp_token_type type;
164 };
165
166 /*
167  * Multi-line macro definitions are stored as a linked list of
168  * these, which is essentially a container to allow several linked
169  * lists of Tokens.
170  *
171  * Note that in this module, linked lists are treated as stacks
172  * wherever possible. For this reason, Lines are _pushed_ on to the
173  * `expansion' field in MMacro structures, so that the linked list,
174  * if walked, would give the macro lines in reverse order; this
175  * means that we can walk the list when expanding a macro, and thus
176  * push the lines on to the `expansion' field in _istk_ in reverse
177  * order (so that when popped back off they are in the right
178  * order). It may seem cockeyed, and it relies on my design having
179  * an even number of steps in, but it works...
180  *
181  * Some of these structures, rather than being actual lines, are
182  * markers delimiting the end of the expansion of a given macro.
183  * This is for use in the cycle-tracking and %rep-handling code.
184  * Such structures have `finishes' non-NULL, and `first' NULL. All
185  * others have `finishes' NULL, but `first' may still be NULL if
186  * the line is blank.
187  */
188 struct Line {
189     Line *next;
190     MMacro *finishes;
191     Token *first;
192 };
193
194 /*
195  * To handle an arbitrary level of file inclusion, we maintain a
196  * stack (ie linked list) of these things.
197  */
198 struct Include {
199     Include *next;
200     FILE *fp;
201     Cond *conds;
202     Line *expansion;
203     char *fname;
204     int lineno, lineinc;
205     MMacro *mstk;               /* stack of active macros/reps */
206 };
207
208 /*
209  * Include search path. This is simply a list of strings which get
210  * prepended, in turn, to the name of an include file, in an
211  * attempt to find the file if it's not in the current directory.
212  */
213 struct IncPath {
214     IncPath *next;
215     char *path;
216 };
217
218 /*
219  * Conditional assembly: we maintain a separate stack of these for
220  * each level of file inclusion. (The only reason we keep the
221  * stacks separate is to ensure that a stray `%endif' in a file
222  * included from within the true branch of a `%if' won't terminate
223  * it and cause confusion: instead, rightly, it'll cause an error.)
224  */
225 struct Cond {
226     Cond *next;
227     int state;
228 };
229 enum {
230     /*
231      * These states are for use just after %if or %elif: IF_TRUE
232      * means the condition has evaluated to truth so we are
233      * currently emitting, whereas IF_FALSE means we are not
234      * currently emitting but will start doing so if a %else comes
235      * up. In these states, all directives are admissible: %elif,
236      * %else and %endif. (And of course %if.)
237      */
238     COND_IF_TRUE, COND_IF_FALSE,
239     /*
240      * These states come up after a %else: ELSE_TRUE means we're
241      * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
242      * any %elif or %else will cause an error.
243      */
244     COND_ELSE_TRUE, COND_ELSE_FALSE,
245     /*
246      * This state means that we're not emitting now, and also that
247      * nothing until %endif will be emitted at all. It's for use in
248      * two circumstances: (i) when we've had our moment of emission
249      * and have now started seeing %elifs, and (ii) when the
250      * condition construct in question is contained within a
251      * non-emitting branch of a larger condition construct.
252      */
253     COND_NEVER
254 };
255 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
256
257 /*
258  * These defines are used as the possible return values for do_directive
259  */
260 #define NO_DIRECTIVE_FOUND  0
261 #define DIRECTIVE_FOUND     1
262
263 /*
264  * Condition codes. Note that we use c_ prefix not C_ because C_ is
265  * used in nasm.h for the "real" condition codes. At _this_ level,
266  * we treat CXZ and ECXZ as condition codes, albeit non-invertible
267  * ones, so we need a different enum...
268  */
269 static const char * const conditions[] = {
270     "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
271     "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
272     "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
273 };
274 enum pp_conds {
275     c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
276     c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
277     c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
278     c_none = -1
279 };
280 static const enum pp_conds inverse_ccs[] = {
281     c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
282     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,
283     c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
284 };
285
286 /*
287  * Directive names.
288  */
289 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
290 static int is_condition(enum preproc_token arg)
291 {
292     return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
293 }
294
295 /* For TASM compatibility we need to be able to recognise TASM compatible
296  * conditional compilation directives. Using the NASM pre-processor does
297  * not work, so we look for them specifically from the following list and
298  * then jam in the equivalent NASM directive into the input stream.
299  */
300
301 #ifndef MAX
302 #       define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
303 #endif
304
305 enum {
306     TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
307     TM_IFNDEF, TM_INCLUDE, TM_LOCAL
308 };
309
310 static const char * const tasm_directives[] = {
311     "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
312     "ifndef", "include", "local"
313 };
314
315 static int StackSize = 4;
316 static char *StackPointer = "ebp";
317 static int ArgOffset = 8;
318 static int LocalOffset = 4;
319
320 static Context *cstk;
321 static Include *istk;
322 static IncPath *ipath = NULL;
323
324 static efunc _error;            /* Pointer to client-provided error reporting function */
325 static evalfunc evaluate;
326
327 static int pass;                /* HACK: pass 0 = generate dependencies only */
328
329 static uint64_t unique;    /* unique identifier numbers */
330
331 static Line *predef = NULL;
332
333 static ListGen *list;
334
335 /*
336  * The current set of multi-line macros we have defined.
337  */
338 static struct hash_table *mmacros;
339
340 /*
341  * The current set of single-line macros we have defined.
342  */
343 static struct hash_table *smacros;
344
345 /*
346  * The multi-line macro we are currently defining, or the %rep
347  * block we are currently reading, if any.
348  */
349 static MMacro *defining;
350
351 /*
352  * The number of macro parameters to allocate space for at a time.
353  */
354 #define PARAM_DELTA 16
355
356 /*
357  * The standard macro set: defined as `static char *stdmac[]'. Also
358  * gives our position in the macro set, when we're processing it.
359  */
360 #include "macros.c"
361 static const char **stdmacpos;
362
363 /*
364  * The extra standard macros that come from the object format, if
365  * any.
366  */
367 static const char **extrastdmac = NULL;
368 bool any_extrastdmac;
369
370 /*
371  * Tokens are allocated in blocks to improve speed
372  */
373 #define TOKEN_BLOCKSIZE 4096
374 static Token *freeTokens = NULL;
375 struct Blocks {
376     Blocks *next;
377     void *chunk;
378 };
379
380 static Blocks blocks = { NULL, NULL };
381
382 /*
383  * Forward declarations.
384  */
385 static Token *expand_mmac_params(Token * tline);
386 static Token *expand_smacro(Token * tline);
387 static Token *expand_id(Token * tline);
388 static Context *get_ctx(char *name, bool all_contexts);
389 static void make_tok_num(Token * tok, int64_t val);
390 static void error(int severity, const char *fmt, ...);
391 static void *new_Block(size_t size);
392 static void delete_Blocks(void);
393 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
394 static Token *delete_Token(Token * t);
395
396 /*
397  * Macros for safe checking of token pointers, avoid *(NULL)
398  */
399 #define tok_type_(x,t) ((x) && (x)->type == (t))
400 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
403
404 /* Handle TASM specific directives, which do not contain a % in
405  * front of them. We do it here because I could not find any other
406  * place to do it for the moment, and it is a hack (ideally it would
407  * be nice to be able to use the NASM pre-processor to do it).
408  */
409 static char *check_tasm_directive(char *line)
410 {
411     int32_t i, j, k, m, len;
412     char *p = line, *oldline, oldchar;
413
414     /* Skip whitespace */
415     while (isspace(*p) && *p != 0)
416         p++;
417
418     /* Binary search for the directive name */
419     i = -1;
420     j = elements(tasm_directives);
421     len = 0;
422     while (!isspace(p[len]) && p[len] != 0)
423         len++;
424     if (len) {
425         oldchar = p[len];
426         p[len] = 0;
427         while (j - i > 1) {
428             k = (j + i) / 2;
429             m = nasm_stricmp(p, tasm_directives[k]);
430             if (m == 0) {
431                 /* We have found a directive, so jam a % in front of it
432                  * so that NASM will then recognise it as one if it's own.
433                  */
434                 p[len] = oldchar;
435                 len = strlen(p);
436                 oldline = line;
437                 line = nasm_malloc(len + 2);
438                 line[0] = '%';
439                 if (k == TM_IFDIFI) {
440                     /* NASM does not recognise IFDIFI, so we convert it to
441                      * %ifdef BOGUS. This is not used in NASM comaptible
442                      * code, but does need to parse for the TASM macro
443                      * package.
444                      */
445                     strcpy(line + 1, "ifdef BOGUS");
446                 } else {
447                     memcpy(line + 1, p, len + 1);
448                 }
449                 nasm_free(oldline);
450                 return line;
451             } else if (m < 0) {
452                 j = k;
453             } else
454                 i = k;
455         }
456         p[len] = oldchar;
457     }
458     return line;
459 }
460
461 /*
462  * The pre-preprocessing stage... This function translates line
463  * number indications as they emerge from GNU cpp (`# lineno "file"
464  * flags') into NASM preprocessor line number indications (`%line
465  * lineno file').
466  */
467 static char *prepreproc(char *line)
468 {
469     int lineno, fnlen;
470     char *fname, *oldline;
471
472     if (line[0] == '#' && line[1] == ' ') {
473         oldline = line;
474         fname = oldline + 2;
475         lineno = atoi(fname);
476         fname += strspn(fname, "0123456789 ");
477         if (*fname == '"')
478             fname++;
479         fnlen = strcspn(fname, "\"");
480         line = nasm_malloc(20 + fnlen);
481         snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
482         nasm_free(oldline);
483     }
484     if (tasm_compatible_mode)
485         return check_tasm_directive(line);
486     return line;
487 }
488
489 /*
490  * Free a linked list of tokens.
491  */
492 static void free_tlist(Token * list)
493 {
494     while (list) {
495         list = delete_Token(list);
496     }
497 }
498
499 /*
500  * Free a linked list of lines.
501  */
502 static void free_llist(Line * list)
503 {
504     Line *l;
505     while (list) {
506         l = list;
507         list = list->next;
508         free_tlist(l->first);
509         nasm_free(l);
510     }
511 }
512
513 /*
514  * Free an MMacro
515  */
516 static void free_mmacro(MMacro * m)
517 {
518     nasm_free(m->name);
519     free_tlist(m->dlist);
520     nasm_free(m->defaults);
521     free_llist(m->expansion);
522     nasm_free(m);
523 }
524
525 /*
526  * Free all currently defined macros, and free the hash tables
527  */
528 static void free_macros(void)
529 {
530     struct hash_tbl_node *it;
531     const char *key;
532     SMacro *s;
533     MMacro *m;
534
535     it = NULL;
536     while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
537         nasm_free((void *)key);
538         while (s) {
539             SMacro *ns = s->next;
540             nasm_free(s->name);
541             free_tlist(s->expansion);
542             nasm_free(s);
543             s = ns;
544         }
545     }
546     hash_free(smacros);
547
548     it = NULL;
549     while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
550         nasm_free((void *)key);
551         while (m) {
552             MMacro *nm = m->next;
553             free_mmacro(m);
554             m = nm;
555         }
556     }
557     hash_free(mmacros);
558 }
559
560 /*
561  * Initialize the hash tables
562  */
563 static void init_macros(void)
564 {
565     smacros = hash_init();
566     mmacros = hash_init();
567 }
568
569 /*
570  * Pop the context stack.
571  */
572 static void ctx_pop(void)
573 {
574     Context *c = cstk;
575     SMacro *smac, *s;
576
577     cstk = cstk->next;
578     smac = c->localmac;
579     while (smac) {
580         s = smac;
581         smac = smac->next;
582         nasm_free(s->name);
583         free_tlist(s->expansion);
584         nasm_free(s);
585     }
586     nasm_free(c->name);
587     nasm_free(c);
588 }
589
590 #define BUF_DELTA 512
591 /*
592  * Read a line from the top file in istk, handling multiple CR/LFs
593  * at the end of the line read, and handling spurious ^Zs. Will
594  * return lines from the standard macro set if this has not already
595  * been done.
596  */
597 static char *read_line(void)
598 {
599     char *buffer, *p, *q;
600     int bufsize, continued_count;
601
602     if (stdmacpos) {
603         if (*stdmacpos) {
604             char *ret = nasm_strdup(*stdmacpos++);
605             if (!*stdmacpos && any_extrastdmac) {
606                 stdmacpos = extrastdmac;
607                 any_extrastdmac = false;
608                 return ret;
609             }
610             /*
611              * Nasty hack: here we push the contents of `predef' on
612              * to the top-level expansion stack, since this is the
613              * most convenient way to implement the pre-include and
614              * pre-define features.
615              */
616             if (!*stdmacpos) {
617                 Line *pd, *l;
618                 Token *head, **tail, *t;
619
620                 for (pd = predef; pd; pd = pd->next) {
621                     head = NULL;
622                     tail = &head;
623                     for (t = pd->first; t; t = t->next) {
624                         *tail = new_Token(NULL, t->type, t->text, 0);
625                         tail = &(*tail)->next;
626                     }
627                     l = nasm_malloc(sizeof(Line));
628                     l->next = istk->expansion;
629                     l->first = head;
630                     l->finishes = false;
631                     istk->expansion = l;
632                 }
633             }
634             return ret;
635         } else {
636             stdmacpos = NULL;
637         }
638     }
639
640     bufsize = BUF_DELTA;
641     buffer = nasm_malloc(BUF_DELTA);
642     p = buffer;
643     continued_count = 0;
644     while (1) {
645         q = fgets(p, bufsize - (p - buffer), istk->fp);
646         if (!q)
647             break;
648         p += strlen(p);
649         if (p > buffer && p[-1] == '\n') {
650             /* Convert backslash-CRLF line continuation sequences into
651                nothing at all (for DOS and Windows) */
652             if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
653                 p -= 3;
654                 *p = 0;
655                 continued_count++;
656             }
657             /* Also convert backslash-LF line continuation sequences into
658                nothing at all (for Unix) */
659             else if (((p - 1) > buffer) && (p[-2] == '\\')) {
660                 p -= 2;
661                 *p = 0;
662                 continued_count++;
663             } else {
664                 break;
665             }
666         }
667         if (p - buffer > bufsize - 10) {
668             int32_t offset = p - buffer;
669             bufsize += BUF_DELTA;
670             buffer = nasm_realloc(buffer, bufsize);
671             p = buffer + offset;        /* prevent stale-pointer problems */
672         }
673     }
674
675     if (!q && p == buffer) {
676         nasm_free(buffer);
677         return NULL;
678     }
679
680     src_set_linnum(src_get_linnum() + istk->lineinc +
681                    (continued_count * istk->lineinc));
682
683     /*
684      * Play safe: remove CRs as well as LFs, if any of either are
685      * present at the end of the line.
686      */
687     while (--p >= buffer && (*p == '\n' || *p == '\r'))
688         *p = '\0';
689
690     /*
691      * Handle spurious ^Z, which may be inserted into source files
692      * by some file transfer utilities.
693      */
694     buffer[strcspn(buffer, "\032")] = '\0';
695
696     list->line(LIST_READ, buffer);
697
698     return buffer;
699 }
700
701 /*
702  * Tokenize a line of text. This is a very simple process since we
703  * don't need to parse the value out of e.g. numeric tokens: we
704  * simply split one string into many.
705  */
706 static Token *tokenize(char *line)
707 {
708     char *p = line;
709     enum pp_token_type type;
710     Token *list = NULL;
711     Token *t, **tail = &list;
712
713     while (*line) {
714         p = line;
715         if (*p == '%') {
716             p++;
717             if (isdigit(*p) ||
718                 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
719                 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
720                 do {
721                     p++;
722                 }
723                 while (isdigit(*p));
724                 type = TOK_PREPROC_ID;
725             } else if (*p == '{') {
726                 p++;
727                 while (*p && *p != '}') {
728                     p[-1] = *p;
729                     p++;
730                 }
731                 p[-1] = '\0';
732                 if (*p)
733                     p++;
734                 type = TOK_PREPROC_ID;
735             } else if (isidchar(*p) ||
736                        ((*p == '!' || *p == '%' || *p == '$') &&
737                         isidchar(p[1]))) {
738                 do {
739                     p++;
740                 }
741                 while (isidchar(*p));
742                 type = TOK_PREPROC_ID;
743             } else {
744                 type = TOK_OTHER;
745                 if (*p == '%')
746                     p++;
747             }
748         } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
749             type = TOK_ID;
750             p++;
751             while (*p && isidchar(*p))
752                 p++;
753         } else if (*p == '\'' || *p == '"') {
754             /*
755              * A string token.
756              */
757             char c = *p;
758             p++;
759             type = TOK_STRING;
760             while (*p && *p != c)
761                 p++;
762
763             if (*p) {
764                 p++;
765             } else {
766                 error(ERR_WARNING, "unterminated string");
767                 /* Handling unterminated strings by UNV */
768                 /* type = -1; */
769             }
770         } else if (isnumstart(*p)) {
771             /*
772              * A number token.
773              */
774             type = TOK_NUMBER;
775             p++;
776             while (*p && isnumchar(*p))
777                 p++;
778         } else if (isspace(*p)) {
779             type = TOK_WHITESPACE;
780             p++;
781             while (*p && isspace(*p))
782                 p++;
783             /*
784              * Whitespace just before end-of-line is discarded by
785              * pretending it's a comment; whitespace just before a
786              * comment gets lumped into the comment.
787              */
788             if (!*p || *p == ';') {
789                 type = TOK_COMMENT;
790                 while (*p)
791                     p++;
792             }
793         } else if (*p == ';') {
794             type = TOK_COMMENT;
795             while (*p)
796                 p++;
797         } else {
798             /*
799              * Anything else is an operator of some kind. We check
800              * for all the double-character operators (>>, <<, //,
801              * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
802              * else is a single-character operator.
803              */
804             type = TOK_OTHER;
805             if ((p[0] == '>' && p[1] == '>') ||
806                 (p[0] == '<' && p[1] == '<') ||
807                 (p[0] == '/' && p[1] == '/') ||
808                 (p[0] == '<' && p[1] == '=') ||
809                 (p[0] == '>' && p[1] == '=') ||
810                 (p[0] == '=' && p[1] == '=') ||
811                 (p[0] == '!' && p[1] == '=') ||
812                 (p[0] == '<' && p[1] == '>') ||
813                 (p[0] == '&' && p[1] == '&') ||
814                 (p[0] == '|' && p[1] == '|') ||
815                 (p[0] == '^' && p[1] == '^')) {
816                 p++;
817             }
818             p++;
819         }
820
821         /* Handling unterminated string by UNV */
822         /*if (type == -1)
823            {
824            *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
825            t->text[p-line] = *line;
826            tail = &t->next;
827            }
828            else */
829         if (type != TOK_COMMENT) {
830             *tail = t = new_Token(NULL, type, line, p - line);
831             tail = &t->next;
832         }
833         line = p;
834     }
835     return list;
836 }
837
838 /*
839  * this function allocates a new managed block of memory and
840  * returns a pointer to the block.  The managed blocks are
841  * deleted only all at once by the delete_Blocks function.
842  */
843 static void *new_Block(size_t size)
844 {
845     Blocks *b = &blocks;
846
847     /* first, get to the end of the linked list */
848     while (b->next)
849         b = b->next;
850     /* now allocate the requested chunk */
851     b->chunk = nasm_malloc(size);
852
853     /* now allocate a new block for the next request */
854     b->next = nasm_malloc(sizeof(Blocks));
855     /* and initialize the contents of the new block */
856     b->next->next = NULL;
857     b->next->chunk = NULL;
858     return b->chunk;
859 }
860
861 /*
862  * this function deletes all managed blocks of memory
863  */
864 static void delete_Blocks(void)
865 {
866     Blocks *a, *b = &blocks;
867
868     /*
869      * keep in mind that the first block, pointed to by blocks
870      * is a static and not dynamically allocated, so we don't
871      * free it.
872      */
873     while (b) {
874         if (b->chunk)
875             nasm_free(b->chunk);
876         a = b;
877         b = b->next;
878         if (a != &blocks)
879             nasm_free(a);
880     }
881 }
882
883 /*
884  *  this function creates a new Token and passes a pointer to it
885  *  back to the caller.  It sets the type and text elements, and
886  *  also the mac and next elements to NULL.
887  */
888 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
889 {
890     Token *t;
891     int i;
892
893     if (freeTokens == NULL) {
894         freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
895         for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
896             freeTokens[i].next = &freeTokens[i + 1];
897         freeTokens[i].next = NULL;
898     }
899     t = freeTokens;
900     freeTokens = t->next;
901     t->next = next;
902     t->mac = NULL;
903     t->type = type;
904     if (type == TOK_WHITESPACE || text == NULL) {
905         t->text = NULL;
906     } else {
907         if (txtlen == 0)
908             txtlen = strlen(text);
909         t->text = nasm_malloc(1 + txtlen);
910         strncpy(t->text, text, txtlen);
911         t->text[txtlen] = '\0';
912     }
913     return t;
914 }
915
916 static Token *delete_Token(Token * t)
917 {
918     Token *next = t->next;
919     nasm_free(t->text);
920     t->next = freeTokens;
921     freeTokens = t;
922     return next;
923 }
924
925 /*
926  * Convert a line of tokens back into text.
927  * If expand_locals is not zero, identifiers of the form "%$*xxx"
928  * will be transformed into ..@ctxnum.xxx
929  */
930 static char *detoken(Token * tlist, int expand_locals)
931 {
932     Token *t;
933     int len;
934     char *line, *p;
935
936     len = 0;
937     for (t = tlist; t; t = t->next) {
938         if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
939             char *p = getenv(t->text + 2);
940             nasm_free(t->text);
941             if (p)
942                 t->text = nasm_strdup(p);
943             else
944                 t->text = NULL;
945         }
946         /* Expand local macros here and not during preprocessing */
947         if (expand_locals &&
948             t->type == TOK_PREPROC_ID && t->text &&
949             t->text[0] == '%' && t->text[1] == '$') {
950             Context *ctx = get_ctx(t->text, false);
951             if (ctx) {
952                 char buffer[40];
953                 char *p, *q = t->text + 2;
954
955                 q += strspn(q, "$");
956                 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
957                 p = nasm_strcat(buffer, q);
958                 nasm_free(t->text);
959                 t->text = p;
960             }
961         }
962         if (t->type == TOK_WHITESPACE) {
963             len++;
964         } else if (t->text) {
965             len += strlen(t->text);
966         }
967     }
968     p = line = nasm_malloc(len + 1);
969     for (t = tlist; t; t = t->next) {
970         if (t->type == TOK_WHITESPACE) {
971             *p = ' ';
972             p++;
973             *p = '\0';
974         } else if (t->text) {
975             strcpy(p, t->text);
976             p += strlen(p);
977         }
978     }
979     *p = '\0';
980     return line;
981 }
982
983 /*
984  * A scanner, suitable for use by the expression evaluator, which
985  * operates on a line of Tokens. Expects a pointer to a pointer to
986  * the first token in the line to be passed in as its private_data
987  * field.
988  */
989 static int ppscan(void *private_data, struct tokenval *tokval)
990 {
991     Token **tlineptr = private_data;
992     Token *tline;
993
994     do {
995         tline = *tlineptr;
996         *tlineptr = tline ? tline->next : NULL;
997     }
998     while (tline && (tline->type == TOK_WHITESPACE ||
999                      tline->type == TOK_COMMENT));
1000
1001     if (!tline)
1002         return tokval->t_type = TOKEN_EOS;
1003
1004     if (tline->text[0] == '$' && !tline->text[1])
1005         return tokval->t_type = TOKEN_HERE;
1006     if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1007         return tokval->t_type = TOKEN_BASE;
1008
1009     if (tline->type == TOK_ID) {
1010         tokval->t_charptr = tline->text;
1011         if (tline->text[0] == '$') {
1012             tokval->t_charptr++;
1013             return tokval->t_type = TOKEN_ID;
1014         }
1015
1016         /*
1017          * This is the only special case we actually need to worry
1018          * about in this restricted context.
1019          */
1020         if (!nasm_stricmp(tline->text, "seg"))
1021             return tokval->t_type = TOKEN_SEG;
1022
1023         return tokval->t_type = TOKEN_ID;
1024     }
1025
1026     if (tline->type == TOK_NUMBER) {
1027         bool rn_error;
1028
1029         tokval->t_integer = readnum(tline->text, &rn_error);
1030         if (rn_error)
1031             return tokval->t_type = TOKEN_ERRNUM;
1032         tokval->t_charptr = NULL;
1033         return tokval->t_type = TOKEN_NUM;
1034     }
1035
1036     if (tline->type == TOK_STRING) {
1037         bool rn_warn;
1038         char q, *r;
1039         int l;
1040
1041         r = tline->text;
1042         q = *r++;
1043         l = strlen(r);
1044
1045         if (l == 0 || r[l - 1] != q)
1046             return tokval->t_type = TOKEN_ERRNUM;
1047         tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1048         if (rn_warn)
1049             error(ERR_WARNING | ERR_PASS1, "character constant too long");
1050         tokval->t_charptr = NULL;
1051         return tokval->t_type = TOKEN_NUM;
1052     }
1053
1054     if (tline->type == TOK_OTHER) {
1055         if (!strcmp(tline->text, "<<"))
1056             return tokval->t_type = TOKEN_SHL;
1057         if (!strcmp(tline->text, ">>"))
1058             return tokval->t_type = TOKEN_SHR;
1059         if (!strcmp(tline->text, "//"))
1060             return tokval->t_type = TOKEN_SDIV;
1061         if (!strcmp(tline->text, "%%"))
1062             return tokval->t_type = TOKEN_SMOD;
1063         if (!strcmp(tline->text, "=="))
1064             return tokval->t_type = TOKEN_EQ;
1065         if (!strcmp(tline->text, "<>"))
1066             return tokval->t_type = TOKEN_NE;
1067         if (!strcmp(tline->text, "!="))
1068             return tokval->t_type = TOKEN_NE;
1069         if (!strcmp(tline->text, "<="))
1070             return tokval->t_type = TOKEN_LE;
1071         if (!strcmp(tline->text, ">="))
1072             return tokval->t_type = TOKEN_GE;
1073         if (!strcmp(tline->text, "&&"))
1074             return tokval->t_type = TOKEN_DBL_AND;
1075         if (!strcmp(tline->text, "^^"))
1076             return tokval->t_type = TOKEN_DBL_XOR;
1077         if (!strcmp(tline->text, "||"))
1078             return tokval->t_type = TOKEN_DBL_OR;
1079     }
1080
1081     /*
1082      * We have no other options: just return the first character of
1083      * the token text.
1084      */
1085     return tokval->t_type = tline->text[0];
1086 }
1087
1088 /*
1089  * Compare a string to the name of an existing macro; this is a
1090  * simple wrapper which calls either strcmp or nasm_stricmp
1091  * depending on the value of the `casesense' parameter.
1092  */
1093 static int mstrcmp(const char *p, const char *q, bool casesense)
1094 {
1095     return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1096 }
1097
1098 /*
1099  * Return the Context structure associated with a %$ token. Return
1100  * NULL, having _already_ reported an error condition, if the
1101  * context stack isn't deep enough for the supplied number of $
1102  * signs.
1103  * If all_contexts == true, contexts that enclose current are
1104  * also scanned for such smacro, until it is found; if not -
1105  * only the context that directly results from the number of $'s
1106  * in variable's name.
1107  */
1108 static Context *get_ctx(char *name, bool all_contexts)
1109 {
1110     Context *ctx;
1111     SMacro *m;
1112     int i;
1113
1114     if (!name || name[0] != '%' || name[1] != '$')
1115         return NULL;
1116
1117     if (!cstk) {
1118         error(ERR_NONFATAL, "`%s': context stack is empty", name);
1119         return NULL;
1120     }
1121
1122     for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1123         ctx = ctx->next;
1124 /*        i--;  Lino - 02/25/02 */
1125     }
1126     if (!ctx) {
1127         error(ERR_NONFATAL, "`%s': context stack is only"
1128               " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1129         return NULL;
1130     }
1131     if (!all_contexts)
1132         return ctx;
1133
1134     do {
1135         /* Search for this smacro in found context */
1136         m = ctx->localmac;
1137         while (m) {
1138             if (!mstrcmp(m->name, name, m->casesense))
1139                 return ctx;
1140             m = m->next;
1141         }
1142         ctx = ctx->next;
1143     }
1144     while (ctx);
1145     return NULL;
1146 }
1147
1148 /*
1149  * Open an include file. This routine must always return a valid
1150  * file pointer if it returns - it's responsible for throwing an
1151  * ERR_FATAL and bombing out completely if not. It should also try
1152  * the include path one by one until it finds the file or reaches
1153  * the end of the path.
1154  */
1155 static FILE *inc_fopen(char *file)
1156 {
1157     FILE *fp;
1158     char *prefix = "", *combine;
1159     IncPath *ip = ipath;
1160     static int namelen = 0;
1161     int len = strlen(file);
1162
1163     while (1) {
1164         combine = nasm_malloc(strlen(prefix) + len + 1);
1165         strcpy(combine, prefix);
1166         strcat(combine, file);
1167         fp = fopen(combine, "r");
1168         if (pass == 0 && fp) {
1169             namelen += strlen(combine) + 1;
1170             if (namelen > 62) {
1171                 printf(" \\\n  ");
1172                 namelen = 2;
1173             }
1174             printf(" %s", combine);
1175         }
1176         nasm_free(combine);
1177         if (fp)
1178             return fp;
1179         if (!ip)
1180             break;
1181         prefix = ip->path;
1182         ip = ip->next;
1183
1184         if (!prefix) {
1185                 /* -MG given and file not found */
1186                 if (pass == 0) {
1187                         namelen += strlen(file) + 1;
1188                         if (namelen > 62) {
1189                                 printf(" \\\n  ");
1190                                 namelen = 2;
1191                         }
1192                         printf(" %s", file);
1193                 }
1194             return NULL;
1195         }
1196     }
1197
1198     error(ERR_FATAL, "unable to open include file `%s'", file);
1199     return NULL;                /* never reached - placate compilers */
1200 }
1201
1202 /*
1203  * Search for a key in the hash index; adding it if necessary
1204  * (in which case we initialize the data pointer to NULL.)
1205  */
1206 static void **
1207 hash_findi_add(struct hash_table *hash, const char *str)
1208 {
1209     struct hash_insert hi;
1210     void **r;
1211     char *strx;
1212
1213     r = hash_findi(hash, str, &hi);
1214     if (r)
1215         return r;
1216
1217     strx = nasm_strdup(str);    /* Use a more efficient allocator here? */
1218     return hash_add(&hi, strx, NULL);
1219 }
1220
1221 /*
1222  * Like hash_findi, but returns the data element rather than a pointer
1223  * to it.  Used only when not adding a new element, hence no third
1224  * argument.
1225  */
1226 static void *
1227 hash_findix(struct hash_table *hash, const char *str)
1228 {
1229     void **p;
1230
1231     p = hash_findi(hash, str, NULL);
1232     return p ? *p : NULL;
1233 }
1234
1235 /*
1236  * Determine if we should warn on defining a single-line macro of
1237  * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1238  * return true if _any_ single-line macro of that name is defined.
1239  * Otherwise, will return true if a single-line macro with either
1240  * `nparam' or no parameters is defined.
1241  *
1242  * If a macro with precisely the right number of parameters is
1243  * defined, or nparam is -1, the address of the definition structure
1244  * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1245  * is NULL, no action will be taken regarding its contents, and no
1246  * error will occur.
1247  *
1248  * Note that this is also called with nparam zero to resolve
1249  * `ifdef'.
1250  *
1251  * If you already know which context macro belongs to, you can pass
1252  * the context pointer as first parameter; if you won't but name begins
1253  * with %$ the context will be automatically computed. If all_contexts
1254  * is true, macro will be searched in outer contexts as well.
1255  */
1256 static bool
1257 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1258                bool nocase)
1259 {
1260     SMacro *m;
1261
1262     if (ctx) {
1263         m = ctx->localmac;
1264     } else if (name[0] == '%' && name[1] == '$') {
1265         if (cstk)
1266             ctx = get_ctx(name, false);
1267         if (!ctx)
1268             return false;       /* got to return _something_ */
1269         m = ctx->localmac;
1270     } else {
1271         m = (SMacro *) hash_findix(smacros, name);
1272     }
1273
1274     while (m) {
1275         if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1276             (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1277             if (defn) {
1278                 if (nparam == (int) m->nparam || nparam == -1)
1279                     *defn = m;
1280                 else
1281                     *defn = NULL;
1282             }
1283             return true;
1284         }
1285         m = m->next;
1286     }
1287
1288     return false;
1289 }
1290
1291 /*
1292  * Count and mark off the parameters in a multi-line macro call.
1293  * This is called both from within the multi-line macro expansion
1294  * code, and also to mark off the default parameters when provided
1295  * in a %macro definition line.
1296  */
1297 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1298 {
1299     int paramsize, brace;
1300
1301     *nparam = paramsize = 0;
1302     *params = NULL;
1303     while (t) {
1304         if (*nparam >= paramsize) {
1305             paramsize += PARAM_DELTA;
1306             *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1307         }
1308         skip_white_(t);
1309         brace = false;
1310         if (tok_is_(t, "{"))
1311             brace = true;
1312         (*params)[(*nparam)++] = t;
1313         while (tok_isnt_(t, brace ? "}" : ","))
1314             t = t->next;
1315         if (t) {                /* got a comma/brace */
1316             t = t->next;
1317             if (brace) {
1318                 /*
1319                  * Now we've found the closing brace, look further
1320                  * for the comma.
1321                  */
1322                 skip_white_(t);
1323                 if (tok_isnt_(t, ",")) {
1324                     error(ERR_NONFATAL,
1325                           "braces do not enclose all of macro parameter");
1326                     while (tok_isnt_(t, ","))
1327                         t = t->next;
1328                 }
1329                 if (t)
1330                     t = t->next;        /* eat the comma */
1331             }
1332         }
1333     }
1334 }
1335
1336 /*
1337  * Determine whether one of the various `if' conditions is true or
1338  * not.
1339  *
1340  * We must free the tline we get passed.
1341  */
1342 static bool if_condition(Token * tline, enum preproc_token ct)
1343 {
1344     enum pp_conditional i = PP_COND(ct);
1345     bool j;
1346     Token *t, *tt, **tptr, *origline;
1347     struct tokenval tokval;
1348     expr *evalresult;
1349     enum pp_token_type needtype;
1350
1351     origline = tline;
1352
1353     switch (i) {
1354     case PPC_IFCTX:
1355         j = false;              /* have we matched yet? */
1356         while (cstk && tline) {
1357             skip_white_(tline);
1358             if (!tline || tline->type != TOK_ID) {
1359                 error(ERR_NONFATAL,
1360                       "`%s' expects context identifiers", pp_directives[ct]);
1361                 free_tlist(origline);
1362                 return -1;
1363             }
1364             if (!nasm_stricmp(tline->text, cstk->name))
1365                 j = true;
1366             tline = tline->next;
1367         }
1368         break;
1369
1370     case PPC_IFDEF:
1371         j = false;              /* have we matched yet? */
1372         while (tline) {
1373             skip_white_(tline);
1374             if (!tline || (tline->type != TOK_ID &&
1375                            (tline->type != TOK_PREPROC_ID ||
1376                             tline->text[1] != '$'))) {
1377                 error(ERR_NONFATAL,
1378                       "`%s' expects macro identifiers", pp_directives[ct]);
1379                 goto fail;
1380             }
1381             if (smacro_defined(NULL, tline->text, 0, NULL, true))
1382                 j = true;
1383             tline = tline->next;
1384         }
1385         break;
1386
1387     case PPC_IFIDN:
1388     case PPC_IFIDNI:
1389         tline = expand_smacro(tline);
1390         t = tt = tline;
1391         while (tok_isnt_(tt, ","))
1392             tt = tt->next;
1393         if (!tt) {
1394             error(ERR_NONFATAL,
1395                   "`%s' expects two comma-separated arguments",
1396                   pp_directives[ct]);
1397             goto fail;
1398         }
1399         tt = tt->next;
1400         j = true;               /* assume equality unless proved not */
1401         while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1402             if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1403                 error(ERR_NONFATAL, "`%s': more than one comma on line",
1404                       pp_directives[ct]);
1405                 goto fail;
1406             }
1407             if (t->type == TOK_WHITESPACE) {
1408                 t = t->next;
1409                 continue;
1410             }
1411             if (tt->type == TOK_WHITESPACE) {
1412                 tt = tt->next;
1413                 continue;
1414             }
1415             if (tt->type != t->type) {
1416                 j = false;      /* found mismatching tokens */
1417                 break;
1418             }
1419             /* Unify surrounding quotes for strings */
1420             if (t->type == TOK_STRING) {
1421                 tt->text[0] = t->text[0];
1422                 tt->text[strlen(tt->text) - 1] = t->text[0];
1423             }
1424             if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1425                 j = false;      /* found mismatching tokens */
1426                 break;
1427             }
1428
1429             t = t->next;
1430             tt = tt->next;
1431         }
1432         if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1433             j = false;          /* trailing gunk on one end or other */
1434         break;
1435
1436     case PPC_IFMACRO:
1437         {
1438             bool found = false;
1439             MMacro searching, *mmac;
1440
1441             tline = tline->next;
1442             skip_white_(tline);
1443             tline = expand_id(tline);
1444             if (!tok_type_(tline, TOK_ID)) {
1445                 error(ERR_NONFATAL,
1446                       "`%s' expects a macro name", pp_directives[ct]);
1447                 goto fail;
1448             }
1449             searching.name = nasm_strdup(tline->text);
1450             searching.casesense = true;
1451             searching.plus = false;
1452             searching.nolist = false;
1453             searching.in_progress = 0;
1454             searching.rep_nest = NULL;
1455             searching.nparam_min = 0;
1456             searching.nparam_max = INT_MAX;
1457             tline = expand_smacro(tline->next);
1458             skip_white_(tline);
1459             if (!tline) {
1460             } else if (!tok_type_(tline, TOK_NUMBER)) {
1461                 error(ERR_NONFATAL,
1462                       "`%s' expects a parameter count or nothing",
1463                       pp_directives[ct]);
1464             } else {
1465                 searching.nparam_min = searching.nparam_max =
1466                     readnum(tline->text, &j);
1467                 if (j)
1468                     error(ERR_NONFATAL,
1469                           "unable to parse parameter count `%s'",
1470                           tline->text);
1471             }
1472             if (tline && tok_is_(tline->next, "-")) {
1473                 tline = tline->next->next;
1474                 if (tok_is_(tline, "*"))
1475                     searching.nparam_max = INT_MAX;
1476                 else if (!tok_type_(tline, TOK_NUMBER))
1477                     error(ERR_NONFATAL,
1478                           "`%s' expects a parameter count after `-'",
1479                           pp_directives[ct]);
1480                 else {
1481                     searching.nparam_max = readnum(tline->text, &j);
1482                     if (j)
1483                         error(ERR_NONFATAL,
1484                               "unable to parse parameter count `%s'",
1485                               tline->text);
1486                     if (searching.nparam_min > searching.nparam_max)
1487                         error(ERR_NONFATAL,
1488                               "minimum parameter count exceeds maximum");
1489                 }
1490             }
1491             if (tline && tok_is_(tline->next, "+")) {
1492                 tline = tline->next;
1493                 searching.plus = true;
1494             }
1495             mmac = (MMacro *) hash_findix(mmacros, searching.name);
1496             while (mmac) {
1497                 if (!strcmp(mmac->name, searching.name) &&
1498                     (mmac->nparam_min <= searching.nparam_max
1499                      || searching.plus)
1500                     && (searching.nparam_min <= mmac->nparam_max
1501                         || mmac->plus)) {
1502                     found = true;
1503                     break;
1504                 }
1505                 mmac = mmac->next;
1506             }
1507             nasm_free(searching.name);
1508             j = found;
1509             break;
1510         }
1511
1512     case PPC_IFID:
1513         needtype = TOK_ID;
1514         goto iftype;
1515     case PPC_IFNUM:
1516         needtype = TOK_NUMBER;
1517         goto iftype;
1518     case PPC_IFSTR:
1519         needtype = TOK_STRING;
1520         goto iftype;
1521
1522     iftype:
1523         tline = expand_smacro(tline);
1524         t = tline;
1525         while (tok_type_(t, TOK_WHITESPACE))
1526             t = t->next;
1527         j = false;              /* placate optimiser */
1528         if (t)
1529             j = t->type == needtype;
1530         break;
1531
1532     case PPC_IF:
1533         t = tline = expand_smacro(tline);
1534         tptr = &t;
1535         tokval.t_type = TOKEN_INVALID;
1536         evalresult = evaluate(ppscan, tptr, &tokval,
1537                               NULL, pass | CRITICAL, error, NULL);
1538         if (!evalresult)
1539             return -1;
1540         if (tokval.t_type)
1541             error(ERR_WARNING,
1542                   "trailing garbage after expression ignored");
1543         if (!is_simple(evalresult)) {
1544             error(ERR_NONFATAL,
1545                   "non-constant value given to `%s'", pp_directives[ct]);
1546             goto fail;
1547         }
1548         j = reloc_value(evalresult) != 0;
1549         return j;
1550
1551     default:
1552         error(ERR_FATAL,
1553               "preprocessor directive `%s' not yet implemented",
1554               pp_directives[ct]);
1555         goto fail;
1556     }
1557
1558     free_tlist(origline);
1559     return j ^ PP_NEGATIVE(ct);
1560
1561 fail:
1562     free_tlist(origline);
1563     return -1;
1564 }
1565
1566 /*
1567  * Expand macros in a string. Used in %error and %include directives.
1568  * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1569  * The returned variable should ALWAYS be freed after usage.
1570  */
1571 void expand_macros_in_string(char **p)
1572 {
1573     Token *line = tokenize(*p);
1574     line = expand_smacro(line);
1575     *p = detoken(line, false);
1576 }
1577
1578 /*
1579  * Common code for defining an smacro
1580  */
1581 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1582                           int nparam, Token *expansion)
1583 {
1584     SMacro *smac, **smhead;
1585
1586     if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1587         if (!smac) {
1588             error(ERR_WARNING,
1589                   "single-line macro `%s' defined both with and"
1590                   " without parameters", mname);
1591
1592             /* Some instances of the old code considered this a failure,
1593                some others didn't.  What is the right thing to do here? */
1594             free_tlist(expansion);
1595             return false;       /* Failure */
1596         } else {
1597             /*
1598              * We're redefining, so we have to take over an
1599              * existing SMacro structure. This means freeing
1600              * what was already in it.
1601              */
1602             nasm_free(smac->name);
1603             free_tlist(smac->expansion);
1604         }
1605     } else {
1606         if (!ctx)
1607             smhead = (SMacro **) hash_findi_add(smacros, mname);
1608         else
1609             smhead = &ctx->localmac;
1610
1611         smac = nasm_malloc(sizeof(SMacro));
1612         smac->next = *smhead;
1613         *smhead = smac;
1614     }
1615     smac->name = nasm_strdup(mname);
1616     smac->casesense = casesense;
1617     smac->nparam = nparam;
1618     smac->expansion = expansion;
1619     smac->in_progress = false;
1620     return true;                /* Success */
1621 }
1622
1623 /*
1624  * Undefine an smacro
1625  */
1626 static void undef_smacro(Context *ctx, const char *mname)
1627 {
1628     SMacro **smhead, *s, **sp;
1629
1630     if (!ctx)
1631         smhead = (SMacro **) hash_findi(smacros, mname, NULL);
1632     else
1633         smhead = &ctx->localmac;
1634
1635     if (smhead) {
1636         /*
1637          * We now have a macro name... go hunt for it.
1638          */
1639         sp = smhead;
1640         while ((s = *sp) != NULL) {
1641             if (!mstrcmp(s->name, mname, s->casesense)) {
1642                 *sp = s->next;
1643                 nasm_free(s->name);
1644                 free_tlist(s->expansion);
1645                 nasm_free(s);
1646             } else {
1647                 sp = &s->next;
1648             }
1649         }
1650     }
1651 }
1652
1653 /**
1654  * find and process preprocessor directive in passed line
1655  * Find out if a line contains a preprocessor directive, and deal
1656  * with it if so.
1657  *
1658  * If a directive _is_ found, it is the responsibility of this routine
1659  * (and not the caller) to free_tlist() the line.
1660  *
1661  * @param tline a pointer to the current tokeninzed line linked list
1662  * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1663  *
1664  */
1665 static int do_directive(Token * tline)
1666 {
1667     enum preproc_token i;
1668     int j;
1669     bool err;
1670     int nparam;
1671     bool nolist;
1672     bool casesense;
1673     int k, m;
1674     int offset;
1675     char *p, *mname;
1676     Include *inc;
1677     Context *ctx;
1678     Cond *cond;
1679     MMacro *mmac, **mmhead;
1680     Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1681     Line *l;
1682     struct tokenval tokval;
1683     expr *evalresult;
1684     MMacro *tmp_defining;       /* Used when manipulating rep_nest */
1685     int64_t count;
1686
1687     origline = tline;
1688
1689     skip_white_(tline);
1690     if (!tok_type_(tline, TOK_PREPROC_ID) ||
1691         (tline->text[1] == '%' || tline->text[1] == '$'
1692          || tline->text[1] == '!'))
1693         return NO_DIRECTIVE_FOUND;
1694
1695     i = pp_token_hash(tline->text);
1696
1697     /*
1698      * If we're in a non-emitting branch of a condition construct,
1699      * or walking to the end of an already terminated %rep block,
1700      * we should ignore all directives except for condition
1701      * directives.
1702      */
1703     if (((istk->conds && !emitting(istk->conds->state)) ||
1704          (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1705         return NO_DIRECTIVE_FOUND;
1706     }
1707
1708     /*
1709      * If we're defining a macro or reading a %rep block, we should
1710      * ignore all directives except for %macro/%imacro (which
1711      * generate an error), %endm/%endmacro, and (only if we're in a
1712      * %rep block) %endrep. If we're in a %rep block, another %rep
1713      * causes an error, so should be let through.
1714      */
1715     if (defining && i != PP_MACRO && i != PP_IMACRO &&
1716         i != PP_ENDMACRO && i != PP_ENDM &&
1717         (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1718         return NO_DIRECTIVE_FOUND;
1719     }
1720
1721     switch (i) {
1722     case PP_INVALID:
1723         error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1724               tline->text);
1725         return NO_DIRECTIVE_FOUND;      /* didn't get it */
1726
1727     case PP_STACKSIZE:
1728         /* Directive to tell NASM what the default stack size is. The
1729          * default is for a 16-bit stack, and this can be overriden with
1730          * %stacksize large.
1731          * the following form:
1732          *
1733          *      ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1734          */
1735         tline = tline->next;
1736         if (tline && tline->type == TOK_WHITESPACE)
1737             tline = tline->next;
1738         if (!tline || tline->type != TOK_ID) {
1739             error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1740             free_tlist(origline);
1741             return DIRECTIVE_FOUND;
1742         }
1743         if (nasm_stricmp(tline->text, "flat") == 0) {
1744             /* All subsequent ARG directives are for a 32-bit stack */
1745             StackSize = 4;
1746             StackPointer = "ebp";
1747             ArgOffset = 8;
1748             LocalOffset = 4;
1749         } else if (nasm_stricmp(tline->text, "large") == 0) {
1750             /* All subsequent ARG directives are for a 16-bit stack,
1751              * far function call.
1752              */
1753             StackSize = 2;
1754             StackPointer = "bp";
1755             ArgOffset = 4;
1756             LocalOffset = 2;
1757         } else if (nasm_stricmp(tline->text, "small") == 0) {
1758             /* All subsequent ARG directives are for a 16-bit stack,
1759              * far function call. We don't support near functions.
1760              */
1761             StackSize = 2;
1762             StackPointer = "bp";
1763             ArgOffset = 6;
1764             LocalOffset = 2;
1765         } else {
1766             error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1767             free_tlist(origline);
1768             return DIRECTIVE_FOUND;
1769         }
1770         free_tlist(origline);
1771         return DIRECTIVE_FOUND;
1772
1773     case PP_ARG:
1774         /* TASM like ARG directive to define arguments to functions, in
1775          * the following form:
1776          *
1777          *      ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1778          */
1779         offset = ArgOffset;
1780         do {
1781             char *arg, directive[256];
1782             int size = StackSize;
1783
1784             /* Find the argument name */
1785             tline = tline->next;
1786             if (tline && tline->type == TOK_WHITESPACE)
1787                 tline = tline->next;
1788             if (!tline || tline->type != TOK_ID) {
1789                 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1790                 free_tlist(origline);
1791                 return DIRECTIVE_FOUND;
1792             }
1793             arg = tline->text;
1794
1795             /* Find the argument size type */
1796             tline = tline->next;
1797             if (!tline || tline->type != TOK_OTHER
1798                 || tline->text[0] != ':') {
1799                 error(ERR_NONFATAL,
1800                       "Syntax error processing `%%arg' directive");
1801                 free_tlist(origline);
1802                 return DIRECTIVE_FOUND;
1803             }
1804             tline = tline->next;
1805             if (!tline || tline->type != TOK_ID) {
1806                 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1807                 free_tlist(origline);
1808                 return DIRECTIVE_FOUND;
1809             }
1810
1811             /* Allow macro expansion of type parameter */
1812             tt = tokenize(tline->text);
1813             tt = expand_smacro(tt);
1814             if (nasm_stricmp(tt->text, "byte") == 0) {
1815                 size = MAX(StackSize, 1);
1816             } else if (nasm_stricmp(tt->text, "word") == 0) {
1817                 size = MAX(StackSize, 2);
1818             } else if (nasm_stricmp(tt->text, "dword") == 0) {
1819                 size = MAX(StackSize, 4);
1820             } else if (nasm_stricmp(tt->text, "qword") == 0) {
1821                 size = MAX(StackSize, 8);
1822             } else if (nasm_stricmp(tt->text, "tword") == 0) {
1823                 size = MAX(StackSize, 10);
1824             } else {
1825                 error(ERR_NONFATAL,
1826                       "Invalid size type for `%%arg' missing directive");
1827                 free_tlist(tt);
1828                 free_tlist(origline);
1829                 return DIRECTIVE_FOUND;
1830             }
1831             free_tlist(tt);
1832
1833             /* Now define the macro for the argument */
1834             snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1835                      arg, StackPointer, offset);
1836             do_directive(tokenize(directive));
1837             offset += size;
1838
1839             /* Move to the next argument in the list */
1840             tline = tline->next;
1841             if (tline && tline->type == TOK_WHITESPACE)
1842                 tline = tline->next;
1843         }
1844         while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1845         free_tlist(origline);
1846         return DIRECTIVE_FOUND;
1847
1848     case PP_LOCAL:
1849         /* TASM like LOCAL directive to define local variables for a
1850          * function, in the following form:
1851          *
1852          *      LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1853          *
1854          * The '= LocalSize' at the end is ignored by NASM, but is
1855          * required by TASM to define the local parameter size (and used
1856          * by the TASM macro package).
1857          */
1858         offset = LocalOffset;
1859         do {
1860             char *local, directive[256];
1861             int size = StackSize;
1862
1863             /* Find the argument name */
1864             tline = tline->next;
1865             if (tline && tline->type == TOK_WHITESPACE)
1866                 tline = tline->next;
1867             if (!tline || tline->type != TOK_ID) {
1868                 error(ERR_NONFATAL,
1869                       "`%%local' missing argument parameter");
1870                 free_tlist(origline);
1871                 return DIRECTIVE_FOUND;
1872             }
1873             local = tline->text;
1874
1875             /* Find the argument size type */
1876             tline = tline->next;
1877             if (!tline || tline->type != TOK_OTHER
1878                 || tline->text[0] != ':') {
1879                 error(ERR_NONFATAL,
1880                       "Syntax error processing `%%local' directive");
1881                 free_tlist(origline);
1882                 return DIRECTIVE_FOUND;
1883             }
1884             tline = tline->next;
1885             if (!tline || tline->type != TOK_ID) {
1886                 error(ERR_NONFATAL,
1887                       "`%%local' missing size type parameter");
1888                 free_tlist(origline);
1889                 return DIRECTIVE_FOUND;
1890             }
1891
1892             /* Allow macro expansion of type parameter */
1893             tt = tokenize(tline->text);
1894             tt = expand_smacro(tt);
1895             if (nasm_stricmp(tt->text, "byte") == 0) {
1896                 size = MAX(StackSize, 1);
1897             } else if (nasm_stricmp(tt->text, "word") == 0) {
1898                 size = MAX(StackSize, 2);
1899             } else if (nasm_stricmp(tt->text, "dword") == 0) {
1900                 size = MAX(StackSize, 4);
1901             } else if (nasm_stricmp(tt->text, "qword") == 0) {
1902                 size = MAX(StackSize, 8);
1903             } else if (nasm_stricmp(tt->text, "tword") == 0) {
1904                 size = MAX(StackSize, 10);
1905             } else {
1906                 error(ERR_NONFATAL,
1907                       "Invalid size type for `%%local' missing directive");
1908                 free_tlist(tt);
1909                 free_tlist(origline);
1910                 return DIRECTIVE_FOUND;
1911             }
1912             free_tlist(tt);
1913
1914             /* Now define the macro for the argument */
1915             snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1916                      local, StackPointer, offset);
1917             do_directive(tokenize(directive));
1918             offset += size;
1919
1920             /* Now define the assign to setup the enter_c macro correctly */
1921             snprintf(directive, sizeof(directive),
1922                      "%%assign %%$localsize %%$localsize+%d", size);
1923             do_directive(tokenize(directive));
1924
1925             /* Move to the next argument in the list */
1926             tline = tline->next;
1927             if (tline && tline->type == TOK_WHITESPACE)
1928                 tline = tline->next;
1929         }
1930         while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1931         free_tlist(origline);
1932         return DIRECTIVE_FOUND;
1933
1934     case PP_CLEAR:
1935         if (tline->next)
1936             error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1937         free_macros();
1938         init_macros();
1939         free_tlist(origline);
1940         return DIRECTIVE_FOUND;
1941
1942     case PP_INCLUDE:
1943         tline = tline->next;
1944         skip_white_(tline);
1945         if (!tline || (tline->type != TOK_STRING &&
1946                        tline->type != TOK_INTERNAL_STRING)) {
1947             error(ERR_NONFATAL, "`%%include' expects a file name");
1948             free_tlist(origline);
1949             return DIRECTIVE_FOUND;     /* but we did _something_ */
1950         }
1951         if (tline->next)
1952             error(ERR_WARNING,
1953                   "trailing garbage after `%%include' ignored");
1954         if (tline->type != TOK_INTERNAL_STRING) {
1955             p = tline->text + 1;        /* point past the quote to the name */
1956             p[strlen(p) - 1] = '\0';    /* remove the trailing quote */
1957         } else
1958             p = tline->text;    /* internal_string is easier */
1959         expand_macros_in_string(&p);
1960         inc = nasm_malloc(sizeof(Include));
1961         inc->next = istk;
1962         inc->conds = NULL;
1963         inc->fp = inc_fopen(p);
1964         if (!inc->fp && pass == 0) {
1965             /* -MG given but file not found */
1966             nasm_free(inc);
1967         } else {
1968             inc->fname = src_set_fname(p);
1969             inc->lineno = src_set_linnum(0);
1970             inc->lineinc = 1;
1971             inc->expansion = NULL;
1972             inc->mstk = NULL;
1973             istk = inc;
1974             list->uplevel(LIST_INCLUDE);
1975         }
1976         free_tlist(origline);
1977         return DIRECTIVE_FOUND;
1978
1979     case PP_PUSH:
1980         tline = tline->next;
1981         skip_white_(tline);
1982         tline = expand_id(tline);
1983         if (!tok_type_(tline, TOK_ID)) {
1984             error(ERR_NONFATAL, "`%%push' expects a context identifier");
1985             free_tlist(origline);
1986             return DIRECTIVE_FOUND;     /* but we did _something_ */
1987         }
1988         if (tline->next)
1989             error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1990         ctx = nasm_malloc(sizeof(Context));
1991         ctx->next = cstk;
1992         ctx->localmac = NULL;
1993         ctx->name = nasm_strdup(tline->text);
1994         ctx->number = unique++;
1995         cstk = ctx;
1996         free_tlist(origline);
1997         break;
1998
1999     case PP_REPL:
2000         tline = tline->next;
2001         skip_white_(tline);
2002         tline = expand_id(tline);
2003         if (!tok_type_(tline, TOK_ID)) {
2004             error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2005             free_tlist(origline);
2006             return DIRECTIVE_FOUND;     /* but we did _something_ */
2007         }
2008         if (tline->next)
2009             error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2010         if (!cstk)
2011             error(ERR_NONFATAL, "`%%repl': context stack is empty");
2012         else {
2013             nasm_free(cstk->name);
2014             cstk->name = nasm_strdup(tline->text);
2015         }
2016         free_tlist(origline);
2017         break;
2018
2019     case PP_POP:
2020         if (tline->next)
2021             error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2022         if (!cstk)
2023             error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2024         else
2025             ctx_pop();
2026         free_tlist(origline);
2027         break;
2028
2029     case PP_ERROR:
2030         tline->next = expand_smacro(tline->next);
2031         tline = tline->next;
2032         skip_white_(tline);
2033         if (tok_type_(tline, TOK_STRING)) {
2034             p = tline->text + 1;        /* point past the quote to the name */
2035             p[strlen(p) - 1] = '\0';    /* remove the trailing quote */
2036             expand_macros_in_string(&p);
2037             error(ERR_NONFATAL, "%s", p);
2038             nasm_free(p);
2039         } else {
2040             p = detoken(tline, false);
2041             error(ERR_WARNING, "%s", p);
2042             nasm_free(p);
2043         }
2044         free_tlist(origline);
2045         break;
2046
2047     CASE_PP_IF:
2048         if (istk->conds && !emitting(istk->conds->state))
2049             j = COND_NEVER;
2050         else {
2051             j = if_condition(tline->next, i);
2052             tline->next = NULL; /* it got freed */
2053             free_tlist(origline);
2054             j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2055         }
2056         cond = nasm_malloc(sizeof(Cond));
2057         cond->next = istk->conds;
2058         cond->state = j;
2059         istk->conds = cond;
2060         return DIRECTIVE_FOUND;
2061
2062     CASE_PP_ELIF:
2063         if (!istk->conds)
2064             error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2065         if (emitting(istk->conds->state)
2066             || istk->conds->state == COND_NEVER)
2067             istk->conds->state = COND_NEVER;
2068         else {
2069             /*
2070              * IMPORTANT: In the case of %if, we will already have
2071              * called expand_mmac_params(); however, if we're
2072              * processing an %elif we must have been in a
2073              * non-emitting mode, which would have inhibited
2074              * the normal invocation of expand_mmac_params().  Therefore,
2075              * we have to do it explicitly here.
2076              */
2077             j = if_condition(expand_mmac_params(tline->next), i);
2078             tline->next = NULL; /* it got freed */
2079             free_tlist(origline);
2080             istk->conds->state =
2081                 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2082         }
2083         return DIRECTIVE_FOUND;
2084
2085     case PP_ELSE:
2086         if (tline->next)
2087             error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2088         if (!istk->conds)
2089             error(ERR_FATAL, "`%%else': no matching `%%if'");
2090         if (emitting(istk->conds->state)
2091             || istk->conds->state == COND_NEVER)
2092             istk->conds->state = COND_ELSE_FALSE;
2093         else
2094             istk->conds->state = COND_ELSE_TRUE;
2095         free_tlist(origline);
2096         return DIRECTIVE_FOUND;
2097
2098     case PP_ENDIF:
2099         if (tline->next)
2100             error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2101         if (!istk->conds)
2102             error(ERR_FATAL, "`%%endif': no matching `%%if'");
2103         cond = istk->conds;
2104         istk->conds = cond->next;
2105         nasm_free(cond);
2106         free_tlist(origline);
2107         return DIRECTIVE_FOUND;
2108
2109     case PP_MACRO:
2110     case PP_IMACRO:
2111         if (defining)
2112             error(ERR_FATAL,
2113                   "`%%%smacro': already defining a macro",
2114                   (i == PP_IMACRO ? "i" : ""));
2115         tline = tline->next;
2116         skip_white_(tline);
2117         tline = expand_id(tline);
2118         if (!tok_type_(tline, TOK_ID)) {
2119             error(ERR_NONFATAL,
2120                   "`%%%smacro' expects a macro name",
2121                   (i == PP_IMACRO ? "i" : ""));
2122             return DIRECTIVE_FOUND;
2123         }
2124         defining = nasm_malloc(sizeof(MMacro));
2125         defining->name = nasm_strdup(tline->text);
2126         defining->casesense = (i == PP_MACRO);
2127         defining->plus = false;
2128         defining->nolist = false;
2129         defining->in_progress = 0;
2130         defining->rep_nest = NULL;
2131         tline = expand_smacro(tline->next);
2132         skip_white_(tline);
2133         if (!tok_type_(tline, TOK_NUMBER)) {
2134             error(ERR_NONFATAL,
2135                   "`%%%smacro' expects a parameter count",
2136                   (i == PP_IMACRO ? "i" : ""));
2137             defining->nparam_min = defining->nparam_max = 0;
2138         } else {
2139             defining->nparam_min = defining->nparam_max =
2140                 readnum(tline->text, &err);
2141             if (err)
2142                 error(ERR_NONFATAL,
2143                       "unable to parse parameter count `%s'", tline->text);
2144         }
2145         if (tline && tok_is_(tline->next, "-")) {
2146             tline = tline->next->next;
2147             if (tok_is_(tline, "*"))
2148                 defining->nparam_max = INT_MAX;
2149             else if (!tok_type_(tline, TOK_NUMBER))
2150                 error(ERR_NONFATAL,
2151                       "`%%%smacro' expects a parameter count after `-'",
2152                       (i == PP_IMACRO ? "i" : ""));
2153             else {
2154                 defining->nparam_max = readnum(tline->text, &err);
2155                 if (err)
2156                     error(ERR_NONFATAL,
2157                           "unable to parse parameter count `%s'",
2158                           tline->text);
2159                 if (defining->nparam_min > defining->nparam_max)
2160                     error(ERR_NONFATAL,
2161                           "minimum parameter count exceeds maximum");
2162             }
2163         }
2164         if (tline && tok_is_(tline->next, "+")) {
2165             tline = tline->next;
2166             defining->plus = true;
2167         }
2168         if (tline && tok_type_(tline->next, TOK_ID) &&
2169             !nasm_stricmp(tline->next->text, ".nolist")) {
2170             tline = tline->next;
2171             defining->nolist = true;
2172         }
2173         mmac = (MMacro *) hash_findix(mmacros, defining->name);
2174         while (mmac) {
2175             if (!strcmp(mmac->name, defining->name) &&
2176                 (mmac->nparam_min <= defining->nparam_max
2177                  || defining->plus)
2178                 && (defining->nparam_min <= mmac->nparam_max
2179                     || mmac->plus)) {
2180                 error(ERR_WARNING,
2181                       "redefining multi-line macro `%s'", defining->name);
2182                 break;
2183             }
2184             mmac = mmac->next;
2185         }
2186         /*
2187          * Handle default parameters.
2188          */
2189         if (tline && tline->next) {
2190             defining->dlist = tline->next;
2191             tline->next = NULL;
2192             count_mmac_params(defining->dlist, &defining->ndefs,
2193                               &defining->defaults);
2194         } else {
2195             defining->dlist = NULL;
2196             defining->defaults = NULL;
2197         }
2198         defining->expansion = NULL;
2199         free_tlist(origline);
2200         return DIRECTIVE_FOUND;
2201
2202     case PP_ENDM:
2203     case PP_ENDMACRO:
2204         if (!defining) {
2205             error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2206             return DIRECTIVE_FOUND;
2207         }
2208         mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2209         defining->next = *mmhead;
2210         *mmhead = defining;
2211         defining = NULL;
2212         free_tlist(origline);
2213         return DIRECTIVE_FOUND;
2214
2215     case PP_ROTATE:
2216         if (tline->next && tline->next->type == TOK_WHITESPACE)
2217             tline = tline->next;
2218         if (tline->next == NULL) {
2219             free_tlist(origline);
2220             error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2221             return DIRECTIVE_FOUND;
2222         }
2223         t = expand_smacro(tline->next);
2224         tline->next = NULL;
2225         free_tlist(origline);
2226         tline = t;
2227         tptr = &t;
2228         tokval.t_type = TOKEN_INVALID;
2229         evalresult =
2230             evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2231         free_tlist(tline);
2232         if (!evalresult)
2233             return DIRECTIVE_FOUND;
2234         if (tokval.t_type)
2235             error(ERR_WARNING,
2236                   "trailing garbage after expression ignored");
2237         if (!is_simple(evalresult)) {
2238             error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2239             return DIRECTIVE_FOUND;
2240         }
2241         mmac = istk->mstk;
2242         while (mmac && !mmac->name)     /* avoid mistaking %reps for macros */
2243             mmac = mmac->next_active;
2244         if (!mmac) {
2245             error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2246         } else if (mmac->nparam == 0) {
2247             error(ERR_NONFATAL,
2248                   "`%%rotate' invoked within macro without parameters");
2249         } else {
2250             int rotate = mmac->rotate + reloc_value(evalresult);
2251
2252             rotate %= (int)mmac->nparam;
2253             if (rotate < 0)
2254                 rotate += mmac->nparam;
2255
2256             mmac->rotate = rotate;
2257         }
2258         return DIRECTIVE_FOUND;
2259
2260     case PP_REP:
2261         nolist = false;
2262         do {
2263             tline = tline->next;
2264         } while (tok_type_(tline, TOK_WHITESPACE));
2265
2266         if (tok_type_(tline, TOK_ID) &&
2267             nasm_stricmp(tline->text, ".nolist") == 0) {
2268             nolist = true;
2269             do {
2270                 tline = tline->next;
2271             } while (tok_type_(tline, TOK_WHITESPACE));
2272         }
2273
2274         if (tline) {
2275             t = expand_smacro(tline);
2276             tptr = &t;
2277             tokval.t_type = TOKEN_INVALID;
2278             evalresult =
2279                 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2280             if (!evalresult) {
2281                 free_tlist(origline);
2282                 return DIRECTIVE_FOUND;
2283             }
2284             if (tokval.t_type)
2285                 error(ERR_WARNING,
2286                       "trailing garbage after expression ignored");
2287             if (!is_simple(evalresult)) {
2288                 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2289                 return DIRECTIVE_FOUND;
2290             }
2291             count = reloc_value(evalresult) + 1;
2292         } else {
2293             error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2294             count = 0;
2295         }
2296         free_tlist(origline);
2297
2298         tmp_defining = defining;
2299         defining = nasm_malloc(sizeof(MMacro));
2300         defining->name = NULL;  /* flags this macro as a %rep block */
2301         defining->casesense = false;
2302         defining->plus = false;
2303         defining->nolist = nolist;
2304         defining->in_progress = count;
2305         defining->nparam_min = defining->nparam_max = 0;
2306         defining->defaults = NULL;
2307         defining->dlist = NULL;
2308         defining->expansion = NULL;
2309         defining->next_active = istk->mstk;
2310         defining->rep_nest = tmp_defining;
2311         return DIRECTIVE_FOUND;
2312
2313     case PP_ENDREP:
2314         if (!defining || defining->name) {
2315             error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2316             return DIRECTIVE_FOUND;
2317         }
2318
2319         /*
2320          * Now we have a "macro" defined - although it has no name
2321          * and we won't be entering it in the hash tables - we must
2322          * push a macro-end marker for it on to istk->expansion.
2323          * After that, it will take care of propagating itself (a
2324          * macro-end marker line for a macro which is really a %rep
2325          * block will cause the macro to be re-expanded, complete
2326          * with another macro-end marker to ensure the process
2327          * continues) until the whole expansion is forcibly removed
2328          * from istk->expansion by a %exitrep.
2329          */
2330         l = nasm_malloc(sizeof(Line));
2331         l->next = istk->expansion;
2332         l->finishes = defining;
2333         l->first = NULL;
2334         istk->expansion = l;
2335
2336         istk->mstk = defining;
2337
2338         list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2339         tmp_defining = defining;
2340         defining = defining->rep_nest;
2341         free_tlist(origline);
2342         return DIRECTIVE_FOUND;
2343
2344     case PP_EXITREP:
2345         /*
2346          * We must search along istk->expansion until we hit a
2347          * macro-end marker for a macro with no name. Then we set
2348          * its `in_progress' flag to 0.
2349          */
2350         for (l = istk->expansion; l; l = l->next)
2351             if (l->finishes && !l->finishes->name)
2352                 break;
2353
2354         if (l)
2355             l->finishes->in_progress = 0;
2356         else
2357             error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2358         free_tlist(origline);
2359         return DIRECTIVE_FOUND;
2360
2361     case PP_XDEFINE:
2362     case PP_IXDEFINE:
2363     case PP_DEFINE:
2364     case PP_IDEFINE:
2365         casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2366
2367         tline = tline->next;
2368         skip_white_(tline);
2369         tline = expand_id(tline);
2370         if (!tline || (tline->type != TOK_ID &&
2371                        (tline->type != TOK_PREPROC_ID ||
2372                         tline->text[1] != '$'))) {
2373             error(ERR_NONFATAL, "`%s' expects a macro identifier",
2374                   pp_directives[i]);
2375             free_tlist(origline);
2376             return DIRECTIVE_FOUND;
2377         }
2378
2379         ctx = get_ctx(tline->text, false);
2380
2381         mname = tline->text;
2382         last = tline;
2383         param_start = tline = tline->next;
2384         nparam = 0;
2385
2386         /* Expand the macro definition now for %xdefine and %ixdefine */
2387         if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2388             tline = expand_smacro(tline);
2389
2390         if (tok_is_(tline, "(")) {
2391             /*
2392              * This macro has parameters.
2393              */
2394
2395             tline = tline->next;
2396             while (1) {
2397                 skip_white_(tline);
2398                 if (!tline) {
2399                     error(ERR_NONFATAL, "parameter identifier expected");
2400                     free_tlist(origline);
2401                     return DIRECTIVE_FOUND;
2402                 }
2403                 if (tline->type != TOK_ID) {
2404                     error(ERR_NONFATAL,
2405                           "`%s': parameter identifier expected",
2406                           tline->text);
2407                     free_tlist(origline);
2408                     return DIRECTIVE_FOUND;
2409                 }
2410                 tline->type = TOK_SMAC_PARAM + nparam++;
2411                 tline = tline->next;
2412                 skip_white_(tline);
2413                 if (tok_is_(tline, ",")) {
2414                     tline = tline->next;
2415                     continue;
2416                 }
2417                 if (!tok_is_(tline, ")")) {
2418                     error(ERR_NONFATAL,
2419                           "`)' expected to terminate macro template");
2420                     free_tlist(origline);
2421                     return DIRECTIVE_FOUND;
2422                 }
2423                 break;
2424             }
2425             last = tline;
2426             tline = tline->next;
2427         }
2428         if (tok_type_(tline, TOK_WHITESPACE))
2429             last = tline, tline = tline->next;
2430         macro_start = NULL;
2431         last->next = NULL;
2432         t = tline;
2433         while (t) {
2434             if (t->type == TOK_ID) {
2435                 for (tt = param_start; tt; tt = tt->next)
2436                     if (tt->type >= TOK_SMAC_PARAM &&
2437                         !strcmp(tt->text, t->text))
2438                         t->type = tt->type;
2439             }
2440             tt = t->next;
2441             t->next = macro_start;
2442             macro_start = t;
2443             t = tt;
2444         }
2445         /*
2446          * Good. We now have a macro name, a parameter count, and a
2447          * token list (in reverse order) for an expansion. We ought
2448          * to be OK just to create an SMacro, store it, and let
2449          * free_tlist have the rest of the line (which we have
2450          * carefully re-terminated after chopping off the expansion
2451          * from the end).
2452          */
2453         define_smacro(ctx, mname, casesense, nparam, macro_start);
2454         free_tlist(origline);
2455         return DIRECTIVE_FOUND;
2456
2457     case PP_UNDEF:
2458         tline = tline->next;
2459         skip_white_(tline);
2460         tline = expand_id(tline);
2461         if (!tline || (tline->type != TOK_ID &&
2462                        (tline->type != TOK_PREPROC_ID ||
2463                         tline->text[1] != '$'))) {
2464             error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2465             free_tlist(origline);
2466             return DIRECTIVE_FOUND;
2467         }
2468         if (tline->next) {
2469             error(ERR_WARNING,
2470                   "trailing garbage after macro name ignored");
2471         }
2472
2473         /* Find the context that symbol belongs to */
2474         ctx = get_ctx(tline->text, false);
2475         undef_smacro(ctx, tline->text);
2476         free_tlist(origline);
2477         return DIRECTIVE_FOUND;
2478
2479     case PP_STRLEN:
2480         casesense = true;
2481
2482         tline = tline->next;
2483         skip_white_(tline);
2484         tline = expand_id(tline);
2485         if (!tline || (tline->type != TOK_ID &&
2486                        (tline->type != TOK_PREPROC_ID ||
2487                         tline->text[1] != '$'))) {
2488             error(ERR_NONFATAL,
2489                   "`%%strlen' expects a macro identifier as first parameter");
2490             free_tlist(origline);
2491             return DIRECTIVE_FOUND;
2492         }
2493         ctx = get_ctx(tline->text, false);
2494
2495         mname = tline->text;
2496         last = tline;
2497         tline = expand_smacro(tline->next);
2498         last->next = NULL;
2499
2500         t = tline;
2501         while (tok_type_(t, TOK_WHITESPACE))
2502             t = t->next;
2503         /* t should now point to the string */
2504         if (t->type != TOK_STRING) {
2505             error(ERR_NONFATAL,
2506                   "`%%strlen` requires string as second parameter");
2507             free_tlist(tline);
2508             free_tlist(origline);
2509             return DIRECTIVE_FOUND;
2510         }
2511
2512         macro_start = nasm_malloc(sizeof(*macro_start));
2513         macro_start->next = NULL;
2514         make_tok_num(macro_start, strlen(t->text) - 2);
2515         macro_start->mac = NULL;
2516
2517         /*
2518          * We now have a macro name, an implicit parameter count of
2519          * zero, and a numeric token to use as an expansion. Create
2520          * and store an SMacro.
2521          */
2522         define_smacro(ctx, mname, casesense, 0, macro_start);
2523         free_tlist(tline);
2524         free_tlist(origline);
2525         return DIRECTIVE_FOUND;
2526
2527     case PP_SUBSTR:
2528         casesense = true;
2529
2530         tline = tline->next;
2531         skip_white_(tline);
2532         tline = expand_id(tline);
2533         if (!tline || (tline->type != TOK_ID &&
2534                        (tline->type != TOK_PREPROC_ID ||
2535                         tline->text[1] != '$'))) {
2536             error(ERR_NONFATAL,
2537                   "`%%substr' expects a macro identifier as first parameter");
2538             free_tlist(origline);
2539             return DIRECTIVE_FOUND;
2540         }
2541         ctx = get_ctx(tline->text, false);
2542
2543         mname = tline->text;
2544         last = tline;
2545         tline = expand_smacro(tline->next);
2546         last->next = NULL;
2547
2548         t = tline->next;
2549         while (tok_type_(t, TOK_WHITESPACE))
2550             t = t->next;
2551
2552         /* t should now point to the string */
2553         if (t->type != TOK_STRING) {
2554             error(ERR_NONFATAL,
2555                   "`%%substr` requires string as second parameter");
2556             free_tlist(tline);
2557             free_tlist(origline);
2558             return DIRECTIVE_FOUND;
2559         }
2560
2561         tt = t->next;
2562         tptr = &tt;
2563         tokval.t_type = TOKEN_INVALID;
2564         evalresult =
2565             evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2566         if (!evalresult) {
2567             free_tlist(tline);
2568             free_tlist(origline);
2569             return DIRECTIVE_FOUND;
2570         }
2571         if (!is_simple(evalresult)) {
2572             error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2573             free_tlist(tline);
2574             free_tlist(origline);
2575             return DIRECTIVE_FOUND;
2576         }
2577
2578         macro_start = nasm_malloc(sizeof(*macro_start));
2579         macro_start->next = NULL;
2580         macro_start->text = nasm_strdup("'''");
2581         if (evalresult->value > 0
2582             && evalresult->value < (int) strlen(t->text) - 1) {
2583             macro_start->text[1] = t->text[evalresult->value];
2584         } else {
2585             macro_start->text[2] = '\0';
2586         }
2587         macro_start->type = TOK_STRING;
2588         macro_start->mac = NULL;
2589
2590         /*
2591          * We now have a macro name, an implicit parameter count of
2592          * zero, and a numeric token to use as an expansion. Create
2593          * and store an SMacro.
2594          */
2595         define_smacro(ctx, mname, casesense, 0, macro_start);
2596         free_tlist(tline);
2597         free_tlist(origline);
2598         return DIRECTIVE_FOUND;
2599
2600     case PP_ASSIGN:
2601     case PP_IASSIGN:
2602         casesense = (i == PP_ASSIGN);
2603
2604         tline = tline->next;
2605         skip_white_(tline);
2606         tline = expand_id(tline);
2607         if (!tline || (tline->type != TOK_ID &&
2608                        (tline->type != TOK_PREPROC_ID ||
2609                         tline->text[1] != '$'))) {
2610             error(ERR_NONFATAL,
2611                   "`%%%sassign' expects a macro identifier",
2612                   (i == PP_IASSIGN ? "i" : ""));
2613             free_tlist(origline);
2614             return DIRECTIVE_FOUND;
2615         }
2616         ctx = get_ctx(tline->text, false);
2617
2618         mname = tline->text;
2619         last = tline;
2620         tline = expand_smacro(tline->next);
2621         last->next = NULL;
2622
2623         t = tline;
2624         tptr = &t;
2625         tokval.t_type = TOKEN_INVALID;
2626         evalresult =
2627             evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2628         free_tlist(tline);
2629         if (!evalresult) {
2630             free_tlist(origline);
2631             return DIRECTIVE_FOUND;
2632         }
2633
2634         if (tokval.t_type)
2635             error(ERR_WARNING,
2636                   "trailing garbage after expression ignored");
2637
2638         if (!is_simple(evalresult)) {
2639             error(ERR_NONFATAL,
2640                   "non-constant value given to `%%%sassign'",
2641                   (i == PP_IASSIGN ? "i" : ""));
2642             free_tlist(origline);
2643             return DIRECTIVE_FOUND;
2644         }
2645
2646         macro_start = nasm_malloc(sizeof(*macro_start));
2647         macro_start->next = NULL;
2648         make_tok_num(macro_start, reloc_value(evalresult));
2649         macro_start->mac = NULL;
2650
2651         /*
2652          * We now have a macro name, an implicit parameter count of
2653          * zero, and a numeric token to use as an expansion. Create
2654          * and store an SMacro.
2655          */
2656         define_smacro(ctx, mname, casesense, 0, macro_start);
2657         free_tlist(origline);
2658         return DIRECTIVE_FOUND;
2659
2660     case PP_LINE:
2661         /*
2662          * Syntax is `%line nnn[+mmm] [filename]'
2663          */
2664         tline = tline->next;
2665         skip_white_(tline);
2666         if (!tok_type_(tline, TOK_NUMBER)) {
2667             error(ERR_NONFATAL, "`%%line' expects line number");
2668             free_tlist(origline);
2669             return DIRECTIVE_FOUND;
2670         }
2671         k = readnum(tline->text, &err);
2672         m = 1;
2673         tline = tline->next;
2674         if (tok_is_(tline, "+")) {
2675             tline = tline->next;
2676             if (!tok_type_(tline, TOK_NUMBER)) {
2677                 error(ERR_NONFATAL, "`%%line' expects line increment");
2678                 free_tlist(origline);
2679                 return DIRECTIVE_FOUND;
2680             }
2681             m = readnum(tline->text, &err);
2682             tline = tline->next;
2683         }
2684         skip_white_(tline);
2685         src_set_linnum(k);
2686         istk->lineinc = m;
2687         if (tline) {
2688             nasm_free(src_set_fname(detoken(tline, false)));
2689         }
2690         free_tlist(origline);
2691         return DIRECTIVE_FOUND;
2692
2693     default:
2694         error(ERR_FATAL,
2695               "preprocessor directive `%s' not yet implemented",
2696               pp_directives[i]);
2697         break;
2698     }
2699     return DIRECTIVE_FOUND;
2700 }
2701
2702 /*
2703  * Ensure that a macro parameter contains a condition code and
2704  * nothing else. Return the condition code index if so, or -1
2705  * otherwise.
2706  */
2707 static int find_cc(Token * t)
2708 {
2709     Token *tt;
2710     int i, j, k, m;
2711
2712     if (!t)
2713             return -1;          /* Probably a %+ without a space */
2714
2715     skip_white_(t);
2716     if (t->type != TOK_ID)
2717         return -1;
2718     tt = t->next;
2719     skip_white_(tt);
2720     if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2721         return -1;
2722
2723     i = -1;
2724     j = elements(conditions);
2725     while (j - i > 1) {
2726         k = (j + i) / 2;
2727         m = nasm_stricmp(t->text, conditions[k]);
2728         if (m == 0) {
2729             i = k;
2730             j = -2;
2731             break;
2732         } else if (m < 0) {
2733             j = k;
2734         } else
2735             i = k;
2736     }
2737     if (j != -2)
2738         return -1;
2739     return i;
2740 }
2741
2742 /*
2743  * Expand MMacro-local things: parameter references (%0, %n, %+n,
2744  * %-n) and MMacro-local identifiers (%%foo).
2745  */
2746 static Token *expand_mmac_params(Token * tline)
2747 {
2748     Token *t, *tt, **tail, *thead;
2749
2750     tail = &thead;
2751     thead = NULL;
2752
2753     while (tline) {
2754         if (tline->type == TOK_PREPROC_ID &&
2755             (((tline->text[1] == '+' || tline->text[1] == '-')
2756               && tline->text[2]) || tline->text[1] == '%'
2757              || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2758             char *text = NULL;
2759             int type = 0, cc;   /* type = 0 to placate optimisers */
2760             char tmpbuf[30];
2761             unsigned int n;
2762             int i;
2763             MMacro *mac;
2764
2765             t = tline;
2766             tline = tline->next;
2767
2768             mac = istk->mstk;
2769             while (mac && !mac->name)   /* avoid mistaking %reps for macros */
2770                 mac = mac->next_active;
2771             if (!mac)
2772                 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2773             else
2774                 switch (t->text[1]) {
2775                     /*
2776                      * We have to make a substitution of one of the
2777                      * forms %1, %-1, %+1, %%foo, %0.
2778                      */
2779                 case '0':
2780                     type = TOK_NUMBER;
2781                     snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2782                     text = nasm_strdup(tmpbuf);
2783                     break;
2784                 case '%':
2785                     type = TOK_ID;
2786                     snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2787                              mac->unique);
2788                     text = nasm_strcat(tmpbuf, t->text + 2);
2789                     break;
2790                 case '-':
2791                     n = atoi(t->text + 2) - 1;
2792                     if (n >= mac->nparam)
2793                         tt = NULL;
2794                     else {
2795                         if (mac->nparam > 1)
2796                             n = (n + mac->rotate) % mac->nparam;
2797                         tt = mac->params[n];
2798                     }
2799                     cc = find_cc(tt);
2800                     if (cc == -1) {
2801                         error(ERR_NONFATAL,
2802                               "macro parameter %d is not a condition code",
2803                               n + 1);
2804                         text = NULL;
2805                     } else {
2806                         type = TOK_ID;
2807                         if (inverse_ccs[cc] == -1) {
2808                             error(ERR_NONFATAL,
2809                                   "condition code `%s' is not invertible",
2810                                   conditions[cc]);
2811                             text = NULL;
2812                         } else
2813                             text =
2814                                 nasm_strdup(conditions[inverse_ccs[cc]]);
2815                     }
2816                     break;
2817                 case '+':
2818                     n = atoi(t->text + 2) - 1;
2819                     if (n >= mac->nparam)
2820                         tt = NULL;
2821                     else {
2822                         if (mac->nparam > 1)
2823                             n = (n + mac->rotate) % mac->nparam;
2824                         tt = mac->params[n];
2825                     }
2826                     cc = find_cc(tt);
2827                     if (cc == -1) {
2828                         error(ERR_NONFATAL,
2829                               "macro parameter %d is not a condition code",
2830                               n + 1);
2831                         text = NULL;
2832                     } else {
2833                         type = TOK_ID;
2834                         text = nasm_strdup(conditions[cc]);
2835                     }
2836                     break;
2837                 default:
2838                     n = atoi(t->text + 1) - 1;
2839                     if (n >= mac->nparam)
2840                         tt = NULL;
2841                     else {
2842                         if (mac->nparam > 1)
2843                             n = (n + mac->rotate) % mac->nparam;
2844                         tt = mac->params[n];
2845                     }
2846                     if (tt) {
2847                         for (i = 0; i < mac->paramlen[n]; i++) {
2848                             *tail = new_Token(NULL, tt->type, tt->text, 0);
2849                             tail = &(*tail)->next;
2850                             tt = tt->next;
2851                         }
2852                     }
2853                     text = NULL;        /* we've done it here */
2854                     break;
2855                 }
2856             if (!text) {
2857                 delete_Token(t);
2858             } else {
2859                 *tail = t;
2860                 tail = &t->next;
2861                 t->type = type;
2862                 nasm_free(t->text);
2863                 t->text = text;
2864                 t->mac = NULL;
2865             }
2866             continue;
2867         } else {
2868             t = *tail = tline;
2869             tline = tline->next;
2870             t->mac = NULL;
2871             tail = &t->next;
2872         }
2873     }
2874     *tail = NULL;
2875     t = thead;
2876     for (; t && (tt = t->next) != NULL; t = t->next)
2877         switch (t->type) {
2878         case TOK_WHITESPACE:
2879             if (tt->type == TOK_WHITESPACE) {
2880                 t->next = delete_Token(tt);
2881             }
2882             break;
2883         case TOK_ID:
2884             if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2885                 char *tmp = nasm_strcat(t->text, tt->text);
2886                 nasm_free(t->text);
2887                 t->text = tmp;
2888                 t->next = delete_Token(tt);
2889             }
2890             break;
2891         case TOK_NUMBER:
2892             if (tt->type == TOK_NUMBER) {
2893                 char *tmp = nasm_strcat(t->text, tt->text);
2894                 nasm_free(t->text);
2895                 t->text = tmp;
2896                 t->next = delete_Token(tt);
2897             }
2898             break;
2899         default:
2900             break;
2901         }
2902
2903     return thead;
2904 }
2905
2906 /*
2907  * Expand all single-line macro calls made in the given line.
2908  * Return the expanded version of the line. The original is deemed
2909  * to be destroyed in the process. (In reality we'll just move
2910  * Tokens from input to output a lot of the time, rather than
2911  * actually bothering to destroy and replicate.)
2912  */
2913 static Token *expand_smacro(Token * tline)
2914 {
2915     Token *t, *tt, *mstart, **tail, *thead;
2916     SMacro *head = NULL, *m;
2917     Token **params;
2918     int *paramsize;
2919     unsigned int nparam, sparam;
2920     int brackets, rescan;
2921     Token *org_tline = tline;
2922     Context *ctx;
2923     char *mname;
2924
2925     /*
2926      * Trick: we should avoid changing the start token pointer since it can
2927      * be contained in "next" field of other token. Because of this
2928      * we allocate a copy of first token and work with it; at the end of
2929      * routine we copy it back
2930      */
2931     if (org_tline) {
2932         tline =
2933             new_Token(org_tline->next, org_tline->type, org_tline->text,
2934                       0);
2935         tline->mac = org_tline->mac;
2936         nasm_free(org_tline->text);
2937         org_tline->text = NULL;
2938     }
2939
2940   again:
2941     tail = &thead;
2942     thead = NULL;
2943
2944     while (tline) {             /* main token loop */
2945         if ((mname = tline->text)) {
2946             /* if this token is a local macro, look in local context */
2947             if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2948                 ctx = get_ctx(mname, true);
2949             else
2950                 ctx = NULL;
2951             if (!ctx) {
2952                 head = (SMacro *) hash_findix(smacros, mname);
2953             } else {
2954                 head = ctx->localmac;
2955             }
2956             /*
2957              * We've hit an identifier. As in is_mmacro below, we first
2958              * check whether the identifier is a single-line macro at
2959              * all, then think about checking for parameters if
2960              * necessary.
2961              */
2962             for (m = head; m; m = m->next)
2963                 if (!mstrcmp(m->name, mname, m->casesense))
2964                     break;
2965             if (m) {
2966                 mstart = tline;
2967                 params = NULL;
2968                 paramsize = NULL;
2969                 if (m->nparam == 0) {
2970                     /*
2971                      * Simple case: the macro is parameterless. Discard the
2972                      * one token that the macro call took, and push the
2973                      * expansion back on the to-do stack.
2974                      */
2975                     if (!m->expansion) {
2976                         if (!strcmp("__FILE__", m->name)) {
2977                             int32_t num = 0;
2978                             src_get(&num, &(tline->text));
2979                             nasm_quote(&(tline->text));
2980                             tline->type = TOK_STRING;
2981                             continue;
2982                         }
2983                         if (!strcmp("__LINE__", m->name)) {
2984                             nasm_free(tline->text);
2985                             make_tok_num(tline, src_get_linnum());
2986                             continue;
2987                         }
2988                         if (!strcmp("__BITS__", m->name)) {
2989                             nasm_free(tline->text);
2990                             make_tok_num(tline, globalbits);
2991                             continue;
2992                         }
2993                         tline = delete_Token(tline);
2994                         continue;
2995                     }
2996                 } else {
2997                     /*
2998                      * Complicated case: at least one macro with this name
2999                      * exists and takes parameters. We must find the
3000                      * parameters in the call, count them, find the SMacro
3001                      * that corresponds to that form of the macro call, and
3002                      * substitute for the parameters when we expand. What a
3003                      * pain.
3004                      */
3005                     /*tline = tline->next;
3006                        skip_white_(tline); */
3007                     do {
3008                         t = tline->next;
3009                         while (tok_type_(t, TOK_SMAC_END)) {
3010                             t->mac->in_progress = false;
3011                             t->text = NULL;
3012                             t = tline->next = delete_Token(t);
3013                         }
3014                         tline = t;
3015                     } while (tok_type_(tline, TOK_WHITESPACE));
3016                     if (!tok_is_(tline, "(")) {
3017                         /*
3018                          * This macro wasn't called with parameters: ignore
3019                          * the call. (Behaviour borrowed from gnu cpp.)
3020                          */
3021                         tline = mstart;
3022                         m = NULL;
3023                     } else {
3024                         int paren = 0;
3025                         int white = 0;
3026                         brackets = 0;
3027                         nparam = 0;
3028                         sparam = PARAM_DELTA;
3029                         params = nasm_malloc(sparam * sizeof(Token *));
3030                         params[0] = tline->next;
3031                         paramsize = nasm_malloc(sparam * sizeof(int));
3032                         paramsize[0] = 0;
3033                         while (true) {  /* parameter loop */
3034                             /*
3035                              * For some unusual expansions
3036                              * which concatenates function call
3037                              */
3038                             t = tline->next;
3039                             while (tok_type_(t, TOK_SMAC_END)) {
3040                                 t->mac->in_progress = false;
3041                                 t->text = NULL;
3042                                 t = tline->next = delete_Token(t);
3043                             }
3044                             tline = t;
3045
3046                             if (!tline) {
3047                                 error(ERR_NONFATAL,
3048                                       "macro call expects terminating `)'");
3049                                 break;
3050                             }
3051                             if (tline->type == TOK_WHITESPACE
3052                                 && brackets <= 0) {
3053                                 if (paramsize[nparam])
3054                                     white++;
3055                                 else
3056                                     params[nparam] = tline->next;
3057                                 continue;       /* parameter loop */
3058                             }
3059                             if (tline->type == TOK_OTHER
3060                                 && tline->text[1] == 0) {
3061                                 char ch = tline->text[0];
3062                                 if (ch == ',' && !paren && brackets <= 0) {
3063                                     if (++nparam >= sparam) {
3064                                         sparam += PARAM_DELTA;
3065                                         params = nasm_realloc(params,
3066                                                               sparam *
3067                                                               sizeof(Token
3068                                                                      *));
3069                                         paramsize =
3070                                             nasm_realloc(paramsize,
3071                                                          sparam *
3072                                                          sizeof(int));
3073                                     }
3074                                     params[nparam] = tline->next;
3075                                     paramsize[nparam] = 0;
3076                                     white = 0;
3077                                     continue;   /* parameter loop */
3078                                 }
3079                                 if (ch == '{' &&
3080                                     (brackets > 0 || (brackets == 0 &&
3081                                                       !paramsize[nparam])))
3082                                 {
3083                                     if (!(brackets++)) {
3084                                         params[nparam] = tline->next;
3085                                         continue;       /* parameter loop */
3086                                     }
3087                                 }
3088                                 if (ch == '}' && brackets > 0)
3089                                     if (--brackets == 0) {
3090                                         brackets = -1;
3091                                         continue;       /* parameter loop */
3092                                     }
3093                                 if (ch == '(' && !brackets)
3094                                     paren++;
3095                                 if (ch == ')' && brackets <= 0)
3096                                     if (--paren < 0)
3097                                         break;
3098                             }
3099                             if (brackets < 0) {
3100                                 brackets = 0;
3101                                 error(ERR_NONFATAL, "braces do not "
3102                                       "enclose all of macro parameter");
3103                             }
3104                             paramsize[nparam] += white + 1;
3105                             white = 0;
3106                         }       /* parameter loop */
3107                         nparam++;
3108                         while (m && (m->nparam != nparam ||
3109                                      mstrcmp(m->name, mname,
3110                                              m->casesense)))
3111                             m = m->next;
3112                         if (!m)
3113                             error(ERR_WARNING | ERR_WARN_MNP,
3114                                   "macro `%s' exists, "
3115                                   "but not taking %d parameters",
3116                                   mstart->text, nparam);
3117                     }
3118                 }
3119                 if (m && m->in_progress)
3120                     m = NULL;
3121                 if (!m) {       /* in progess or didn't find '(' or wrong nparam */
3122                     /*
3123                      * Design question: should we handle !tline, which
3124                      * indicates missing ')' here, or expand those
3125                      * macros anyway, which requires the (t) test a few
3126                      * lines down?
3127                      */
3128                     nasm_free(params);
3129                     nasm_free(paramsize);
3130                     tline = mstart;
3131                 } else {
3132                     /*
3133                      * Expand the macro: we are placed on the last token of the
3134                      * call, so that we can easily split the call from the
3135                      * following tokens. We also start by pushing an SMAC_END
3136                      * token for the cycle removal.
3137                      */
3138                     t = tline;
3139                     if (t) {
3140                         tline = t->next;
3141                         t->next = NULL;
3142                     }
3143                     tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3144                     tt->mac = m;
3145                     m->in_progress = true;
3146                     tline = tt;
3147                     for (t = m->expansion; t; t = t->next) {
3148                         if (t->type >= TOK_SMAC_PARAM) {
3149                             Token *pcopy = tline, **ptail = &pcopy;
3150                             Token *ttt, *pt;
3151                             int i;
3152
3153                             ttt = params[t->type - TOK_SMAC_PARAM];
3154                             for (i = paramsize[t->type - TOK_SMAC_PARAM];
3155                                  --i >= 0;) {
3156                                 pt = *ptail =
3157                                     new_Token(tline, ttt->type, ttt->text,
3158                                               0);
3159                                 ptail = &pt->next;
3160                                 ttt = ttt->next;
3161                             }
3162                             tline = pcopy;
3163                         } else {
3164                             tt = new_Token(tline, t->type, t->text, 0);
3165                             tline = tt;
3166                         }
3167                     }
3168
3169                     /*
3170                      * Having done that, get rid of the macro call, and clean
3171                      * up the parameters.
3172                      */
3173                     nasm_free(params);
3174                     nasm_free(paramsize);
3175                     free_tlist(mstart);
3176                     continue;   /* main token loop */
3177                 }
3178             }
3179         }
3180
3181         if (tline->type == TOK_SMAC_END) {
3182             tline->mac->in_progress = false;
3183             tline = delete_Token(tline);
3184         } else {
3185             t = *tail = tline;
3186             tline = tline->next;
3187             t->mac = NULL;
3188             t->next = NULL;
3189             tail = &t->next;
3190         }
3191     }
3192
3193     /*
3194      * Now scan the entire line and look for successive TOK_IDs that resulted
3195      * after expansion (they can't be produced by tokenize()). The successive
3196      * TOK_IDs should be concatenated.
3197      * Also we look for %+ tokens and concatenate the tokens before and after
3198      * them (without white spaces in between).
3199      */
3200     t = thead;
3201     rescan = 0;
3202     while (t) {
3203         while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3204             t = t->next;
3205         if (!t || !t->next)
3206             break;
3207         if (t->next->type == TOK_ID ||
3208             t->next->type == TOK_PREPROC_ID ||
3209             t->next->type == TOK_NUMBER) {
3210             char *p = nasm_strcat(t->text, t->next->text);
3211             nasm_free(t->text);
3212             t->next = delete_Token(t->next);
3213             t->text = p;
3214             rescan = 1;
3215         } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3216                    t->next->next->type == TOK_PREPROC_ID &&
3217                    strcmp(t->next->next->text, "%+") == 0) {
3218             /* free the next whitespace, the %+ token and next whitespace */
3219             int i;
3220             for (i = 1; i <= 3; i++) {
3221                 if (!t->next
3222                     || (i != 2 && t->next->type != TOK_WHITESPACE))
3223                     break;
3224                 t->next = delete_Token(t->next);
3225             }                   /* endfor */
3226         } else
3227             t = t->next;
3228     }
3229     /* If we concatenaded something, re-scan the line for macros */
3230     if (rescan) {
3231         tline = thead;
3232         goto again;
3233     }
3234
3235     if (org_tline) {
3236         if (thead) {
3237             *org_tline = *thead;
3238             /* since we just gave text to org_line, don't free it */
3239             thead->text = NULL;
3240             delete_Token(thead);
3241         } else {
3242             /* the expression expanded to empty line;
3243                we can't return NULL for some reasons
3244                we just set the line to a single WHITESPACE token. */
3245             memset(org_tline, 0, sizeof(*org_tline));
3246             org_tline->text = NULL;
3247             org_tline->type = TOK_WHITESPACE;
3248         }
3249         thead = org_tline;
3250     }
3251
3252     return thead;
3253 }
3254
3255 /*
3256  * Similar to expand_smacro but used exclusively with macro identifiers
3257  * right before they are fetched in. The reason is that there can be
3258  * identifiers consisting of several subparts. We consider that if there
3259  * are more than one element forming the name, user wants a expansion,
3260  * otherwise it will be left as-is. Example:
3261  *
3262  *      %define %$abc cde
3263  *
3264  * the identifier %$abc will be left as-is so that the handler for %define
3265  * will suck it and define the corresponding value. Other case:
3266  *
3267  *      %define _%$abc cde
3268  *
3269  * In this case user wants name to be expanded *before* %define starts
3270  * working, so we'll expand %$abc into something (if it has a value;
3271  * otherwise it will be left as-is) then concatenate all successive
3272  * PP_IDs into one.
3273  */
3274 static Token *expand_id(Token * tline)
3275 {
3276     Token *cur, *oldnext = NULL;
3277
3278     if (!tline || !tline->next)
3279         return tline;
3280
3281     cur = tline;
3282     while (cur->next &&
3283            (cur->next->type == TOK_ID ||
3284             cur->next->type == TOK_PREPROC_ID
3285             || cur->next->type == TOK_NUMBER))
3286         cur = cur->next;
3287
3288     /* If identifier consists of just one token, don't expand */
3289     if (cur == tline)
3290         return tline;
3291
3292     if (cur) {
3293         oldnext = cur->next;    /* Detach the tail past identifier */
3294         cur->next = NULL;       /* so that expand_smacro stops here */
3295     }
3296
3297     tline = expand_smacro(tline);
3298
3299     if (cur) {
3300         /* expand_smacro possibly changhed tline; re-scan for EOL */
3301         cur = tline;
3302         while (cur && cur->next)
3303             cur = cur->next;
3304         if (cur)
3305             cur->next = oldnext;
3306     }
3307
3308     return tline;
3309 }
3310
3311 /*
3312  * Determine whether the given line constitutes a multi-line macro
3313  * call, and return the MMacro structure called if so. Doesn't have
3314  * to check for an initial label - that's taken care of in
3315  * expand_mmacro - but must check numbers of parameters. Guaranteed
3316  * to be called with tline->type == TOK_ID, so the putative macro
3317  * name is easy to find.
3318  */
3319 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3320 {
3321     MMacro *head, *m;
3322     Token **params;
3323     int nparam;
3324
3325     head = (MMacro *) hash_findix(mmacros, tline->text);
3326
3327     /*
3328      * Efficiency: first we see if any macro exists with the given
3329      * name. If not, we can return NULL immediately. _Then_ we
3330      * count the parameters, and then we look further along the
3331      * list if necessary to find the proper MMacro.
3332      */
3333     for (m = head; m; m = m->next)
3334         if (!mstrcmp(m->name, tline->text, m->casesense))
3335             break;
3336     if (!m)
3337         return NULL;
3338
3339     /*
3340      * OK, we have a potential macro. Count and demarcate the
3341      * parameters.
3342      */
3343     count_mmac_params(tline->next, &nparam, &params);
3344
3345     /*
3346      * So we know how many parameters we've got. Find the MMacro
3347      * structure that handles this number.
3348      */
3349     while (m) {
3350         if (m->nparam_min <= nparam
3351             && (m->plus || nparam <= m->nparam_max)) {
3352             /*
3353              * This one is right. Just check if cycle removal
3354              * prohibits us using it before we actually celebrate...
3355              */
3356             if (m->in_progress) {
3357 #if 0
3358                 error(ERR_NONFATAL,
3359                       "self-reference in multi-line macro `%s'", m->name);
3360 #endif
3361                 nasm_free(params);
3362                 return NULL;
3363             }
3364             /*
3365              * It's right, and we can use it. Add its default
3366              * parameters to the end of our list if necessary.
3367              */
3368             if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3369                 params =
3370                     nasm_realloc(params,
3371                                  ((m->nparam_min + m->ndefs +
3372                                    1) * sizeof(*params)));
3373                 while (nparam < m->nparam_min + m->ndefs) {
3374                     params[nparam] = m->defaults[nparam - m->nparam_min];
3375                     nparam++;
3376                 }
3377             }
3378             /*
3379              * If we've gone over the maximum parameter count (and
3380              * we're in Plus mode), ignore parameters beyond
3381              * nparam_max.
3382              */
3383             if (m->plus && nparam > m->nparam_max)
3384                 nparam = m->nparam_max;
3385             /*
3386              * Then terminate the parameter list, and leave.
3387              */
3388             if (!params) {      /* need this special case */
3389                 params = nasm_malloc(sizeof(*params));
3390                 nparam = 0;
3391             }
3392             params[nparam] = NULL;
3393             *params_array = params;
3394             return m;
3395         }
3396         /*
3397          * This one wasn't right: look for the next one with the
3398          * same name.
3399          */
3400         for (m = m->next; m; m = m->next)
3401             if (!mstrcmp(m->name, tline->text, m->casesense))
3402                 break;
3403     }
3404
3405     /*
3406      * After all that, we didn't find one with the right number of
3407      * parameters. Issue a warning, and fail to expand the macro.
3408      */
3409     error(ERR_WARNING | ERR_WARN_MNP,
3410           "macro `%s' exists, but not taking %d parameters",
3411           tline->text, nparam);
3412     nasm_free(params);
3413     return NULL;
3414 }
3415
3416 /*
3417  * Expand the multi-line macro call made by the given line, if
3418  * there is one to be expanded. If there is, push the expansion on
3419  * istk->expansion and return 1. Otherwise return 0.
3420  */
3421 static int expand_mmacro(Token * tline)
3422 {
3423     Token *startline = tline;
3424     Token *label = NULL;
3425     int dont_prepend = 0;
3426     Token **params, *t, *tt;
3427     MMacro *m;
3428     Line *l, *ll;
3429     int i, nparam, *paramlen;
3430
3431     t = tline;
3432     skip_white_(t);
3433 /*    if (!tok_type_(t, TOK_ID))  Lino 02/25/02 */
3434     if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3435         return 0;
3436     m = is_mmacro(t, &params);
3437     if (!m) {
3438         Token *last;
3439         /*
3440          * We have an id which isn't a macro call. We'll assume
3441          * it might be a label; we'll also check to see if a
3442          * colon follows it. Then, if there's another id after
3443          * that lot, we'll check it again for macro-hood.
3444          */
3445         label = last = t;
3446         t = t->next;
3447         if (tok_type_(t, TOK_WHITESPACE))
3448             last = t, t = t->next;
3449         if (tok_is_(t, ":")) {
3450             dont_prepend = 1;
3451             last = t, t = t->next;
3452             if (tok_type_(t, TOK_WHITESPACE))
3453                 last = t, t = t->next;
3454         }
3455         if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3456             return 0;
3457         last->next = NULL;
3458         tline = t;
3459     }
3460
3461     /*
3462      * Fix up the parameters: this involves stripping leading and
3463      * trailing whitespace, then stripping braces if they are
3464      * present.
3465      */
3466     for (nparam = 0; params[nparam]; nparam++) ;
3467     paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3468
3469     for (i = 0; params[i]; i++) {
3470         int brace = false;
3471         int comma = (!m->plus || i < nparam - 1);
3472
3473         t = params[i];
3474         skip_white_(t);
3475         if (tok_is_(t, "{"))
3476             t = t->next, brace = true, comma = false;
3477         params[i] = t;
3478         paramlen[i] = 0;
3479         while (t) {
3480             if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3481                 break;          /* ... because we have hit a comma */
3482             if (comma && t->type == TOK_WHITESPACE
3483                 && tok_is_(t->next, ","))
3484                 break;          /* ... or a space then a comma */
3485             if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3486                 break;          /* ... or a brace */
3487             t = t->next;
3488             paramlen[i]++;
3489         }
3490     }
3491
3492     /*
3493      * OK, we have a MMacro structure together with a set of
3494      * parameters. We must now go through the expansion and push
3495      * copies of each Line on to istk->expansion. Substitution of
3496      * parameter tokens and macro-local tokens doesn't get done
3497      * until the single-line macro substitution process; this is
3498      * because delaying them allows us to change the semantics
3499      * later through %rotate.
3500      *
3501      * First, push an end marker on to istk->expansion, mark this
3502      * macro as in progress, and set up its invocation-specific
3503      * variables.
3504      */
3505     ll = nasm_malloc(sizeof(Line));
3506     ll->next = istk->expansion;
3507     ll->finishes = m;
3508     ll->first = NULL;
3509     istk->expansion = ll;
3510
3511     m->in_progress = true;
3512     m->params = params;
3513     m->iline = tline;
3514     m->nparam = nparam;
3515     m->rotate = 0;
3516     m->paramlen = paramlen;
3517     m->unique = unique++;
3518     m->lineno = 0;
3519
3520     m->next_active = istk->mstk;
3521     istk->mstk = m;
3522
3523     for (l = m->expansion; l; l = l->next) {
3524         Token **tail;
3525
3526         ll = nasm_malloc(sizeof(Line));
3527         ll->finishes = NULL;
3528         ll->next = istk->expansion;
3529         istk->expansion = ll;
3530         tail = &ll->first;
3531
3532         for (t = l->first; t; t = t->next) {
3533             Token *x = t;
3534             if (t->type == TOK_PREPROC_ID &&
3535                 t->text[1] == '0' && t->text[2] == '0') {
3536                 dont_prepend = -1;
3537                 x = label;
3538                 if (!x)
3539                     continue;
3540             }
3541             tt = *tail = new_Token(NULL, x->type, x->text, 0);
3542             tail = &tt->next;
3543         }
3544         *tail = NULL;
3545     }
3546
3547     /*
3548      * If we had a label, push it on as the first line of
3549      * the macro expansion.
3550      */
3551     if (label) {
3552         if (dont_prepend < 0)
3553             free_tlist(startline);
3554         else {
3555             ll = nasm_malloc(sizeof(Line));
3556             ll->finishes = NULL;
3557             ll->next = istk->expansion;
3558             istk->expansion = ll;
3559             ll->first = startline;
3560             if (!dont_prepend) {
3561                 while (label->next)
3562                     label = label->next;
3563                 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3564             }
3565         }
3566     }
3567
3568     list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3569
3570     return 1;
3571 }
3572
3573 /*
3574  * Since preprocessor always operate only on the line that didn't
3575  * arrived yet, we should always use ERR_OFFBY1. Also since user
3576  * won't want to see same error twice (preprocessing is done once
3577  * per pass) we will want to show errors only during pass one.
3578  */
3579 static void error(int severity, const char *fmt, ...)
3580 {
3581     va_list arg;
3582     char buff[1024];
3583
3584     /* If we're in a dead branch of IF or something like it, ignore the error */
3585     if (istk && istk->conds && !emitting(istk->conds->state))
3586         return;
3587
3588     va_start(arg, fmt);
3589     vsnprintf(buff, sizeof(buff), fmt, arg);
3590     va_end(arg);
3591
3592     if (istk && istk->mstk && istk->mstk->name)
3593         _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3594                istk->mstk->lineno, buff);
3595     else
3596         _error(severity | ERR_PASS1, "%s", buff);
3597 }
3598
3599 static void
3600 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3601          ListGen * listgen)
3602 {
3603     _error = errfunc;
3604     cstk = NULL;
3605     istk = nasm_malloc(sizeof(Include));
3606     istk->next = NULL;
3607     istk->conds = NULL;
3608     istk->expansion = NULL;
3609     istk->mstk = NULL;
3610     istk->fp = fopen(file, "r");
3611     istk->fname = NULL;
3612     src_set_fname(nasm_strdup(file));
3613     src_set_linnum(0);
3614     istk->lineinc = 1;
3615     if (!istk->fp)
3616         error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3617               file);
3618     defining = NULL;
3619     init_macros();
3620     unique = 0;
3621     if (tasm_compatible_mode) {
3622         stdmacpos = stdmac;
3623     } else {
3624         stdmacpos = &stdmac[TASM_MACRO_COUNT];
3625     }
3626     any_extrastdmac = (extrastdmac != NULL);
3627     list = listgen;
3628     evaluate = eval;
3629     pass = apass;
3630 }
3631
3632 static char *pp_getline(void)
3633 {
3634     char *line;
3635     Token *tline;
3636
3637     while (1) {
3638         /*
3639          * Fetch a tokenized line, either from the macro-expansion
3640          * buffer or from the input file.
3641          */
3642         tline = NULL;
3643         while (istk->expansion && istk->expansion->finishes) {
3644             Line *l = istk->expansion;
3645             if (!l->finishes->name && l->finishes->in_progress > 1) {
3646                 Line *ll;
3647
3648                 /*
3649                  * This is a macro-end marker for a macro with no
3650                  * name, which means it's not really a macro at all
3651                  * but a %rep block, and the `in_progress' field is
3652                  * more than 1, meaning that we still need to
3653                  * repeat. (1 means the natural last repetition; 0
3654                  * means termination by %exitrep.) We have
3655                  * therefore expanded up to the %endrep, and must
3656                  * push the whole block on to the expansion buffer
3657                  * again. We don't bother to remove the macro-end
3658                  * marker: we'd only have to generate another one
3659                  * if we did.
3660                  */
3661                 l->finishes->in_progress--;
3662                 for (l = l->finishes->expansion; l; l = l->next) {
3663                     Token *t, *tt, **tail;
3664
3665                     ll = nasm_malloc(sizeof(Line));
3666                     ll->next = istk->expansion;
3667                     ll->finishes = NULL;
3668                     ll->first = NULL;
3669                     tail = &ll->first;
3670
3671                     for (t = l->first; t; t = t->next) {
3672                         if (t->text || t->type == TOK_WHITESPACE) {
3673                             tt = *tail =
3674                                 new_Token(NULL, t->type, t->text, 0);
3675                             tail = &tt->next;
3676                         }
3677                     }
3678
3679                     istk->expansion = ll;
3680                 }
3681             } else {
3682                 /*
3683                  * Check whether a `%rep' was started and not ended
3684                  * within this macro expansion. This can happen and
3685                  * should be detected. It's a fatal error because
3686                  * I'm too confused to work out how to recover
3687                  * sensibly from it.
3688                  */
3689                 if (defining) {
3690                     if (defining->name)
3691                         error(ERR_PANIC,
3692                               "defining with name in expansion");
3693                     else if (istk->mstk->name)
3694                         error(ERR_FATAL,
3695                               "`%%rep' without `%%endrep' within"
3696                               " expansion of macro `%s'",
3697                               istk->mstk->name);
3698                 }
3699
3700                 /*
3701                  * FIXME:  investigate the relationship at this point between
3702                  * istk->mstk and l->finishes
3703                  */
3704                 {
3705                     MMacro *m = istk->mstk;
3706                     istk->mstk = m->next_active;
3707                     if (m->name) {
3708                         /*
3709                          * This was a real macro call, not a %rep, and
3710                          * therefore the parameter information needs to
3711                          * be freed.
3712                          */
3713                         nasm_free(m->params);
3714                         free_tlist(m->iline);
3715                         nasm_free(m->paramlen);
3716                         l->finishes->in_progress = false;
3717                     } else
3718                         free_mmacro(m);
3719                 }
3720                 istk->expansion = l->next;
3721                 nasm_free(l);
3722                 list->downlevel(LIST_MACRO);
3723             }
3724         }
3725         while (1) {             /* until we get a line we can use */
3726
3727             if (istk->expansion) {      /* from a macro expansion */
3728                 char *p;
3729                 Line *l = istk->expansion;
3730                 if (istk->mstk)
3731                     istk->mstk->lineno++;
3732                 tline = l->first;
3733                 istk->expansion = l->next;
3734                 nasm_free(l);
3735                 p = detoken(tline, false);
3736                 list->line(LIST_MACRO, p);
3737                 nasm_free(p);
3738                 break;
3739             }
3740             line = read_line();
3741             if (line) {         /* from the current input file */
3742                 line = prepreproc(line);
3743                 tline = tokenize(line);
3744                 nasm_free(line);
3745                 break;
3746             }
3747             /*
3748              * The current file has ended; work down the istk
3749              */
3750             {
3751                 Include *i = istk;
3752                 fclose(i->fp);
3753                 if (i->conds)
3754                     error(ERR_FATAL,
3755                           "expected `%%endif' before end of file");
3756                 /* only set line and file name if there's a next node */
3757                 if (i->next) {
3758                     src_set_linnum(i->lineno);
3759                     nasm_free(src_set_fname(i->fname));
3760                 }
3761                 istk = i->next;
3762                 list->downlevel(LIST_INCLUDE);
3763                 nasm_free(i);
3764                 if (!istk)
3765                     return NULL;
3766             }
3767         }
3768
3769         /*
3770          * We must expand MMacro parameters and MMacro-local labels
3771          * _before_ we plunge into directive processing, to cope
3772          * with things like `%define something %1' such as STRUC
3773          * uses. Unless we're _defining_ a MMacro, in which case
3774          * those tokens should be left alone to go into the
3775          * definition; and unless we're in a non-emitting
3776          * condition, in which case we don't want to meddle with
3777          * anything.
3778          */
3779         if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3780             tline = expand_mmac_params(tline);
3781
3782         /*
3783          * Check the line to see if it's a preprocessor directive.
3784          */
3785         if (do_directive(tline) == DIRECTIVE_FOUND) {
3786             continue;
3787         } else if (defining) {
3788             /*
3789              * We're defining a multi-line macro. We emit nothing
3790              * at all, and just
3791              * shove the tokenized line on to the macro definition.
3792              */
3793             Line *l = nasm_malloc(sizeof(Line));
3794             l->next = defining->expansion;
3795             l->first = tline;
3796             l->finishes = false;
3797             defining->expansion = l;
3798             continue;
3799         } else if (istk->conds && !emitting(istk->conds->state)) {
3800             /*
3801              * We're in a non-emitting branch of a condition block.
3802              * Emit nothing at all, not even a blank line: when we
3803              * emerge from the condition we'll give a line-number
3804              * directive so we keep our place correctly.
3805              */
3806             free_tlist(tline);
3807             continue;
3808         } else if (istk->mstk && !istk->mstk->in_progress) {
3809             /*
3810              * We're in a %rep block which has been terminated, so
3811              * we're walking through to the %endrep without
3812              * emitting anything. Emit nothing at all, not even a
3813              * blank line: when we emerge from the %rep block we'll
3814              * give a line-number directive so we keep our place
3815              * correctly.
3816              */
3817             free_tlist(tline);
3818             continue;
3819         } else {
3820             tline = expand_smacro(tline);
3821             if (!expand_mmacro(tline)) {
3822                 /*
3823                  * De-tokenize the line again, and emit it.
3824                  */
3825                 line = detoken(tline, true);
3826                 free_tlist(tline);
3827                 break;
3828             } else {
3829                 continue;       /* expand_mmacro calls free_tlist */
3830             }
3831         }
3832     }
3833
3834     return line;
3835 }
3836
3837 static void pp_cleanup(int pass)
3838 {
3839     if (defining) {
3840         error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3841               defining->name);
3842         free_mmacro(defining);
3843     }
3844     while (cstk)
3845         ctx_pop();
3846     free_macros();
3847     while (istk) {
3848         Include *i = istk;
3849         istk = istk->next;
3850         fclose(i->fp);
3851         nasm_free(i->fname);
3852         nasm_free(i);
3853     }
3854     while (cstk)
3855         ctx_pop();
3856     if (pass == 0) {
3857         free_llist(predef);
3858         delete_Blocks();
3859     }
3860 }
3861
3862 void pp_include_path(char *path)
3863 {
3864     IncPath *i;
3865
3866     i = nasm_malloc(sizeof(IncPath));
3867     i->path = path ? nasm_strdup(path) : NULL;
3868     i->next = NULL;
3869
3870     if (ipath != NULL) {
3871         IncPath *j = ipath;
3872         while (j->next != NULL)
3873             j = j->next;
3874         j->next = i;
3875     } else {
3876         ipath = i;
3877     }
3878 }
3879
3880 /*
3881  * added by alexfru:
3882  *
3883  * This function is used to "export" the include paths, e.g.
3884  * the paths specified in the '-I' command switch.
3885  * The need for such exporting is due to the 'incbin' directive,
3886  * which includes raw binary files (unlike '%include', which
3887  * includes text source files). It would be real nice to be
3888  * able to specify paths to search for incbin'ned files also.
3889  * So, this is a simple workaround.
3890  *
3891  * The function use is simple:
3892  *
3893  * The 1st call (with NULL argument) returns a pointer to the 1st path
3894  * (char** type) or NULL if none include paths available.
3895  *
3896  * All subsequent calls take as argument the value returned by this
3897  * function last. The return value is either the next path
3898  * (char** type) or NULL if the end of the paths list is reached.
3899  *
3900  * It is maybe not the best way to do things, but I didn't want
3901  * to export too much, just one or two functions and no types or
3902  * variables exported.
3903  *
3904  * Can't say I like the current situation with e.g. this path list either,
3905  * it seems to be never deallocated after creation...
3906  */
3907 char **pp_get_include_path_ptr(char **pPrevPath)
3908 {
3909 /*   This macro returns offset of a member of a structure */
3910 #define GetMemberOffset(StructType,MemberName)\
3911   ((size_t)&((StructType*)0)->MemberName)
3912     IncPath *i;
3913
3914     if (pPrevPath == NULL) {
3915         if (ipath != NULL)
3916             return &ipath->path;
3917         else
3918             return NULL;
3919     }
3920     i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
3921     i = i->next;
3922     if (i != NULL)
3923         return &i->path;
3924     else
3925         return NULL;
3926 #undef GetMemberOffset
3927 }
3928
3929 void pp_pre_include(char *fname)
3930 {
3931     Token *inc, *space, *name;
3932     Line *l;
3933
3934     name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3935     space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3936     inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
3937
3938     l = nasm_malloc(sizeof(Line));
3939     l->next = predef;
3940     l->first = inc;
3941     l->finishes = false;
3942     predef = l;
3943 }
3944
3945 void pp_pre_define(char *definition)
3946 {
3947     Token *def, *space;
3948     Line *l;
3949     char *equals;
3950
3951     equals = strchr(definition, '=');
3952     space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3953     def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
3954     if (equals)
3955         *equals = ' ';
3956     space->next = tokenize(definition);
3957     if (equals)
3958         *equals = '=';
3959
3960     l = nasm_malloc(sizeof(Line));
3961     l->next = predef;
3962     l->first = def;
3963     l->finishes = false;
3964     predef = l;
3965 }
3966
3967 void pp_pre_undefine(char *definition)
3968 {
3969     Token *def, *space;
3970     Line *l;
3971
3972     space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3973     def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
3974     space->next = tokenize(definition);
3975
3976     l = nasm_malloc(sizeof(Line));
3977     l->next = predef;
3978     l->first = def;
3979     l->finishes = false;
3980     predef = l;
3981 }
3982
3983 /*
3984  * Added by Keith Kanios:
3985  *
3986  * This function is used to assist with "runtime" preprocessor
3987  * directives. (e.g. pp_runtime("%define __BITS__ 64");)
3988  *
3989  * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
3990  * PASS A VALID STRING TO THIS FUNCTION!!!!!
3991  */
3992
3993 void pp_runtime(char *definition)
3994 {
3995     Token *def;
3996
3997     def = tokenize(definition);
3998     if(do_directive(def) == NO_DIRECTIVE_FOUND)
3999         free_tlist(def);
4000
4001 }
4002
4003 void pp_extra_stdmac(const char **macros)
4004 {
4005     extrastdmac = macros;
4006 }
4007
4008 static void make_tok_num(Token * tok, int64_t val)
4009 {
4010     char numbuf[20];
4011     snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4012     tok->text = nasm_strdup(numbuf);
4013     tok->type = TOK_NUMBER;
4014 }
4015
4016 Preproc nasmpp = {
4017     pp_reset,
4018     pp_getline,
4019     pp_cleanup
4020 };