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