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