initial branch of %pragma support
[nasm.git] / preproc.c
blob96b1f25029d37beae9927b2594964f09384b0bac
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
48 * }
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
63 #include "compiler.h"
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <string.h>
70 #include <ctype.h>
71 #include <limits.h>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
84 typedef struct SMacro SMacro;
85 typedef struct ExpDef ExpDef;
86 typedef struct ExpInv ExpInv;
87 typedef struct Context Context;
88 typedef struct Token Token;
89 typedef struct Blocks Blocks;
90 typedef struct Line Line;
91 typedef struct Include Include;
92 typedef struct Cond Cond;
93 typedef struct IncPath IncPath;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
107 struct SMacro {
108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
117 * The context stack is composed of a linked list of these.
119 struct Context {
120 Context *next;
121 char *name;
122 struct hash_table localmac;
123 uint32_t number;
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
134 * %define a(x,y) ( (x) & ~(y) )
136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
145 enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
157 #define PP_CONCAT_MASK(x) (1 << (x))
159 struct tokseq_match {
160 int mask_head;
161 int mask_tail;
164 struct Token {
165 Token *next;
166 char *text;
167 union {
168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
171 enum pp_token_type type;
175 * Expansion definitions are stored as a linked list of
176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
184 struct Line {
185 Line *next;
186 Token *first;
190 * Expansion Types
192 enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
194 EXP_MMACRO, EXP_REP,
195 EXP_IF, EXP_WHILE,
196 EXP_COMMENT, EXP_FINAL,
197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
210 struct ExpDef {
211 ExpDef *prev; /* previous definition */
212 ExpDef *next; /* next in hash table */
213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
223 int prepend; /* label prepend state */
224 Line *label;
225 Line *line;
226 Line *last;
227 int linecount; /* number of lines within expansion */
229 int64_t def_depth; /* current number of definition pairs deep */
230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
238 * Store the invocation of an expansion.
240 * The `prev' field is for the `istk->expansion` linked-list.
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
245 struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
252 Line *current; /* pointer to current line in invocation */
254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
259 uint64_t unique;
260 bool emitting;
261 int lineno; /* current line number in expansion */
262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
270 struct Include {
271 Include *next;
272 FILE *fp;
273 Cond *conds;
274 ExpInv *expansion;
275 char *fname;
276 int lineno, lineinc;
277 int mmac_depth;
281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
285 struct IncPath {
286 IncPath *next;
287 char *path;
291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
297 enum {
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
306 COND_IF_TRUE, COND_IF_FALSE,
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
322 COND_DONE, COND_NEVER
324 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
327 * These defines are used as the possible return values for do_directive
329 #define NO_DIRECTIVE_FOUND 0
330 #define DIRECTIVE_FOUND 1
333 * This define sets the upper limit for smacro and expansions
335 #define DEADMAN_LIMIT (1 << 20)
337 /* max reps */
338 #define REP_LIMIT ((INT64_C(1) << 62))
341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
346 static const char * const conditions[] = {
347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
351 enum pp_conds {
352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
357 static const enum pp_conds inverse_ccs[] = {
358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 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,
360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
363 /* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
369 enum {
370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
374 static const char * const tasm_directives[] = {
375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
379 static int StackSize = 4;
380 static char *StackPointer = "ebp";
381 static int ArgOffset = 8;
382 static int LocalOffset = 0;
384 static Context *cstk;
385 static Include *istk;
386 static IncPath *ipath = NULL;
388 static int pass; /* HACK: pass 0 = generate dependencies only */
389 static StrList **dephead, **deptail; /* Dependency list */
391 static uint64_t unique; /* unique identifier numbers */
393 static Line *predef = NULL;
394 static bool do_predef;
396 static ListGen *list;
399 * The current set of expansion definitions we have defined.
401 static struct hash_table expdefs;
404 * The current set of single-line macros we have defined.
406 static struct hash_table smacros;
409 * Linked List of all active expansion definitions
411 struct ExpDef *expansions = NULL;
414 * The expansion we are currently defining
416 static ExpDef *defining = NULL;
418 static uint64_t nested_mac_count;
419 static uint64_t nested_rep_count;
422 * Linked-list of lines to preprocess, prior to cleanup
424 static Line *finals = NULL;
425 static bool in_final = false;
428 * The number of macro parameters to allocate space for at a time.
430 #define PARAM_DELTA 16
433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
436 static macros_t *stdmacpos;
439 * The extra standard macros that come from the object format, if
440 * any.
442 static macros_t *extrastdmac = NULL;
443 static bool any_extrastdmac;
446 * Tokens are allocated in blocks to improve speed
448 #define TOKEN_BLOCKSIZE 4096
449 static Token *freeTokens = NULL;
450 struct Blocks {
451 Blocks *next;
452 void *chunk;
455 static Blocks blocks = { NULL, NULL };
458 * Forward declarations.
460 static Token *expand_mmac_params(Token * tline);
461 static Token *expand_smacro(Token * tline);
462 static Token *expand_id(Token * tline);
463 static Context *get_ctx(const char *name, const char **namep,
464 bool all_contexts);
465 static void make_tok_num(Token * tok, int64_t val);
466 static void error(int severity, const char *fmt, ...);
467 static void error_precond(int severity, const char *fmt, ...);
468 static void *new_Block(size_t size);
469 static void delete_Blocks(void);
470 static Token *new_Token(Token * next, enum pp_token_type type,
471 const char *text, int txtlen);
472 static Token *copy_Token(Token * tline);
473 static Token *delete_Token(Token * t);
474 static Line *new_Line(void);
475 static ExpDef *new_ExpDef(int exp_type);
476 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
479 * Macros for safe checking of token pointers, avoid *(NULL)
481 #define tok_type_(x,t) ((x) && (x)->type == (t))
482 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
486 #ifdef NASM_TRACE
488 #define dump_token(t) raw_dump_token(t, __FILE__, __LINE__, __func__);
489 static void raw_dump_token(Token *token, const char *file, int line, const char *func)
491 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
492 if (token) {
493 Token *t;
494 list_for_each(t, token) {
495 if (t->text)
496 printf("'%s' ", t->text);
498 printf("\n");
502 #endif
505 * nasm_unquote with error if the string contains NUL characters.
506 * If the string contains NUL characters, issue an error and return
507 * the C len, i.e. truncate at the NUL.
509 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
511 size_t len = nasm_unquote(qstr, NULL);
512 size_t clen = strlen(qstr);
514 if (len != clen)
515 error(ERR_NONFATAL, "NUL character in `%s' directive",
516 pp_directives[directive]);
518 return clen;
522 * In-place reverse a list of tokens.
524 static Token *reverse_tokens(Token *t)
526 Token *prev = NULL;
527 Token *next;
529 while (t) {
530 next = t->next;
531 t->next = prev;
532 prev = t;
533 t = next;
536 return prev;
540 * Handle TASM specific directives, which do not contain a % in
541 * front of them. We do it here because I could not find any other
542 * place to do it for the moment, and it is a hack (ideally it would
543 * be nice to be able to use the NASM pre-processor to do it).
545 static char *check_tasm_directive(char *line)
547 int32_t i, j, k, m, len;
548 char *p, *q, *oldline, oldchar;
550 p = nasm_skip_spaces(line);
552 /* Binary search for the directive name */
553 i = -1;
554 j = ARRAY_SIZE(tasm_directives);
555 q = nasm_skip_word(p);
556 len = q - p;
557 if (len) {
558 oldchar = p[len];
559 p[len] = 0;
560 while (j - i > 1) {
561 k = (j + i) / 2;
562 m = nasm_stricmp(p, tasm_directives[k]);
563 if (m == 0) {
564 /* We have found a directive, so jam a % in front of it
565 * so that NASM will then recognise it as one if it's own.
567 p[len] = oldchar;
568 len = strlen(p);
569 oldline = line;
570 line = nasm_malloc(len + 2);
571 line[0] = '%';
572 if (k == TM_IFDIFI) {
574 * NASM does not recognise IFDIFI, so we convert
575 * it to %if 0. This is not used in NASM
576 * compatible code, but does need to parse for the
577 * TASM macro package.
579 strcpy(line + 1, "if 0");
580 } else {
581 memcpy(line + 1, p, len + 1);
583 nasm_free(oldline);
584 return line;
585 } else if (m < 0) {
586 j = k;
587 } else
588 i = k;
590 p[len] = oldchar;
592 return line;
596 * The pre-preprocessing stage... This function translates line
597 * number indications as they emerge from GNU cpp (`# lineno "file"
598 * flags') into NASM preprocessor line number indications (`%line
599 * lineno file').
601 static char *prepreproc(char *line)
603 int lineno, fnlen;
604 char *fname, *oldline;
606 if (line[0] == '#' && line[1] == ' ') {
607 oldline = line;
608 fname = oldline + 2;
609 lineno = atoi(fname);
610 fname += strspn(fname, "0123456789 ");
611 if (*fname == '"')
612 fname++;
613 fnlen = strcspn(fname, "\"");
614 line = nasm_malloc(20 + fnlen);
615 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
616 nasm_free(oldline);
618 if (tasm_compatible_mode)
619 return check_tasm_directive(line);
620 return line;
624 * Free a linked list of tokens.
626 static void free_tlist(Token * list)
628 while (list)
629 list = delete_Token(list);
633 * Free a linked list of lines.
635 static void free_llist(Line * list)
637 Line *l, *tmp;
638 list_for_each_safe(l, tmp, list) {
639 free_tlist(l->first);
640 nasm_free(l);
645 * Free an ExpDef
647 static void free_expdef(ExpDef * ed)
649 nasm_free(ed->name);
650 free_tlist(ed->dlist);
651 nasm_free(ed->defaults);
652 free_llist(ed->line);
653 nasm_free(ed);
657 * Free an ExpInv
659 static void free_expinv(ExpInv * ei)
661 if (ei->name != NULL)
662 nasm_free(ei->name);
663 if (ei->label_text != NULL)
664 nasm_free(ei->label_text);
665 nasm_free(ei);
669 * Free all currently defined macros, and free the hash tables
671 static void free_smacro_table(struct hash_table *smt)
673 SMacro *s, *tmp;
674 const char *key;
675 struct hash_tbl_node *it = NULL;
677 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
678 nasm_free((void *)key);
679 list_for_each_safe(s, tmp, s) {
680 nasm_free(s->name);
681 free_tlist(s->expansion);
682 nasm_free(s);
685 hash_free(smt);
688 static void free_expdef_table(struct hash_table *edt)
690 ExpDef *ed, *tmp;
691 const char *key;
692 struct hash_tbl_node *it = NULL;
694 it = NULL;
695 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
696 nasm_free((void *)key);
697 list_for_each_safe(ed ,tmp, ed)
698 free_expdef(ed);
700 hash_free(edt);
703 static void free_macros(void)
705 free_smacro_table(&smacros);
706 free_expdef_table(&expdefs);
710 * Initialize the hash tables
712 static void init_macros(void)
714 hash_init(&smacros, HASH_LARGE);
715 hash_init(&expdefs, HASH_LARGE);
719 * Pop the context stack.
721 static void ctx_pop(void)
723 Context *c = cstk;
725 cstk = cstk->next;
726 free_smacro_table(&c->localmac);
727 nasm_free(c->name);
728 nasm_free(c);
732 * Search for a key in the hash index; adding it if necessary
733 * (in which case we initialize the data pointer to NULL.)
735 static void **
736 hash_findi_add(struct hash_table *hash, const char *str)
738 struct hash_insert hi;
739 void **r;
740 char *strx;
742 r = hash_findi(hash, str, &hi);
743 if (r)
744 return r;
746 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
747 return hash_add(&hi, strx, NULL);
751 * Like hash_findi, but returns the data element rather than a pointer
752 * to it. Used only when not adding a new element, hence no third
753 * argument.
755 static void *
756 hash_findix(struct hash_table *hash, const char *str)
758 void **p;
760 p = hash_findi(hash, str, NULL);
761 return p ? *p : NULL;
765 * read line from standard macros set,
766 * if there no more left -- return NULL
768 static char *line_from_stdmac(void)
770 unsigned char c;
771 const unsigned char *p = stdmacpos;
772 char *line, *q;
773 size_t len = 0;
775 if (!stdmacpos)
776 return NULL;
778 while ((c = *p++)) {
779 if (c >= 0x80)
780 len += pp_directives_len[c - 0x80] + 1;
781 else
782 len++;
785 line = nasm_malloc(len + 1);
786 q = line;
787 while ((c = *stdmacpos++)) {
788 if (c >= 0x80) {
789 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
790 q += pp_directives_len[c - 0x80];
791 *q++ = ' ';
792 } else {
793 *q++ = c;
796 stdmacpos = p;
797 *q = '\0';
799 if (!*stdmacpos) {
800 /* This was the last of the standard macro chain... */
801 stdmacpos = NULL;
802 if (any_extrastdmac) {
803 stdmacpos = extrastdmac;
804 any_extrastdmac = false;
805 } else if (do_predef) {
806 ExpInv *ei;
807 Line *pd, *l;
808 Token *head, **tail, *t;
811 * Nasty hack: here we push the contents of
812 * `predef' on to the top-level expansion stack,
813 * since this is the most convenient way to
814 * implement the pre-include and pre-define
815 * features.
817 list_for_each(pd, predef) {
818 head = NULL;
819 tail = &head;
820 list_for_each(t, pd->first) {
821 *tail = new_Token(NULL, t->type, t->text, 0);
822 tail = &(*tail)->next;
825 l = new_Line();
826 l->first = head;
827 ei = new_ExpInv(EXP_PREDEF, NULL);
828 ei->current = l;
829 ei->emitting = true;
830 ei->prev = istk->expansion;
831 istk->expansion = ei;
833 do_predef = false;
837 return line;
840 #define BUF_DELTA 512
842 * Read a line from the top file in istk, handling multiple CR/LFs
843 * at the end of the line read, and handling spurious ^Zs. Will
844 * return lines from the standard macro set if this has not already
845 * been done.
847 static char *read_line(void)
849 char *buffer, *p, *q;
850 int bufsize, continued_count;
853 * standart macros set (predefined) goes first
855 p = line_from_stdmac();
856 if (p)
857 return p;
860 * regular read from a file
862 bufsize = BUF_DELTA;
863 buffer = nasm_malloc(BUF_DELTA);
864 p = buffer;
865 continued_count = 0;
866 while (1) {
867 q = fgets(p, bufsize - (p - buffer), istk->fp);
868 if (!q)
869 break;
870 p += strlen(p);
871 if (p > buffer && p[-1] == '\n') {
873 * Convert backslash-CRLF line continuation sequences into
874 * nothing at all (for DOS and Windows)
876 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
877 p -= 3;
878 *p = 0;
879 continued_count++;
882 * Also convert backslash-LF line continuation sequences into
883 * nothing at all (for Unix)
885 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
886 p -= 2;
887 *p = 0;
888 continued_count++;
889 } else {
890 break;
893 if (p - buffer > bufsize - 10) {
894 int32_t offset = p - buffer;
895 bufsize += BUF_DELTA;
896 buffer = nasm_realloc(buffer, bufsize);
897 p = buffer + offset; /* prevent stale-pointer problems */
901 if (!q && p == buffer) {
902 nasm_free(buffer);
903 return NULL;
906 src_set_linnum(src_get_linnum() + istk->lineinc +
907 (continued_count * istk->lineinc));
910 * Play safe: remove CRs as well as LFs, if any of either are
911 * present at the end of the line.
913 while (--p >= buffer && (*p == '\n' || *p == '\r'))
914 *p = '\0';
917 * Handle spurious ^Z, which may be inserted into source files
918 * by some file transfer utilities.
920 buffer[strcspn(buffer, "\032")] = '\0';
922 list->line(LIST_READ, buffer);
924 return buffer;
928 * Tokenize a line of text. This is a very simple process since we
929 * don't need to parse the value out of e.g. numeric tokens: we
930 * simply split one string into many.
932 static Token *tokenize(char *line)
934 char c, *p = line;
935 enum pp_token_type type;
936 Token *list = NULL;
937 Token *t, **tail = &list;
938 bool verbose = true;
940 if ((defining != NULL) && (defining->ignoring == true)) {
941 verbose = false;
944 while (*line) {
945 p = line;
946 if (*p == '%') {
947 p++;
948 if (*p == '+' && !nasm_isdigit(p[1])) {
949 p++;
950 type = TOK_PASTE;
951 } else if (nasm_isdigit(*p) ||
952 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
953 do {
954 p++;
956 while (nasm_isdigit(*p));
957 type = TOK_PREPROC_ID;
958 } else if (*p == '{') {
959 p++;
960 while (*p && *p != '}') {
961 p[-1] = *p;
962 p++;
964 p[-1] = '\0';
965 if (*p)
966 p++;
967 type = TOK_PREPROC_ID;
968 } else if (*p == '[') {
969 int lvl = 1;
970 line += 2; /* Skip the leading %[ */
971 p++;
972 while (lvl && (c = *p++)) {
973 switch (c) {
974 case ']':
975 lvl--;
976 break;
977 case '%':
978 if (*p == '[')
979 lvl++;
980 break;
981 case '\'':
982 case '\"':
983 case '`':
984 p = nasm_skip_string(p - 1) + 1;
985 break;
986 default:
987 break;
990 p--;
991 if (*p)
992 *p++ = '\0';
993 if (lvl && verbose)
994 error(ERR_NONFATAL, "unterminated %[ construct");
995 type = TOK_INDIRECT;
996 } else if (*p == '?') {
997 type = TOK_PREPROC_Q; /* %? */
998 p++;
999 if (*p == '?') {
1000 type = TOK_PREPROC_QQ; /* %?? */
1001 p++;
1003 } else if (*p == '!') {
1004 type = TOK_PREPROC_ID;
1005 p++;
1006 if (isidchar(*p)) {
1007 do {
1008 p++;
1009 } while (isidchar(*p));
1010 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1011 p = nasm_skip_string(p);
1012 if (*p)
1013 p++;
1014 else if(verbose)
1015 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1016 } else {
1017 /* %! without string or identifier */
1018 type = TOK_OTHER; /* Legacy behavior... */
1020 } else if (isidchar(*p) ||
1021 ((*p == '!' || *p == '%' || *p == '$') &&
1022 isidchar(p[1]))) {
1023 do {
1024 p++;
1026 while (isidchar(*p));
1027 type = TOK_PREPROC_ID;
1028 } else {
1029 type = TOK_OTHER;
1030 if (*p == '%')
1031 p++;
1033 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1034 type = TOK_ID;
1035 p++;
1036 while (*p && isidchar(*p))
1037 p++;
1038 } else if (*p == '\'' || *p == '"' || *p == '`') {
1040 * A string token.
1042 type = TOK_STRING;
1043 p = nasm_skip_string(p);
1045 if (*p) {
1046 p++;
1047 } else if(verbose) {
1048 error(ERR_WARNING|ERR_PASS1, "unterminated string");
1049 /* Handling unterminated strings by UNV */
1050 /* type = -1; */
1052 } else if (p[0] == '$' && p[1] == '$') {
1053 type = TOK_OTHER; /* TOKEN_BASE */
1054 p += 2;
1055 } else if (isnumstart(*p)) {
1056 bool is_hex = false;
1057 bool is_float = false;
1058 bool has_e = false;
1059 char c, *r;
1062 * A numeric token.
1065 if (*p == '$') {
1066 p++;
1067 is_hex = true;
1070 for (;;) {
1071 c = *p++;
1073 if (!is_hex && (c == 'e' || c == 'E')) {
1074 has_e = true;
1075 if (*p == '+' || *p == '-') {
1077 * e can only be followed by +/- if it is either a
1078 * prefixed hex number or a floating-point number
1080 p++;
1081 is_float = true;
1083 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1084 is_hex = true;
1085 } else if (c == 'P' || c == 'p') {
1086 is_float = true;
1087 if (*p == '+' || *p == '-')
1088 p++;
1089 } else if (isnumchar(c) || c == '_')
1090 ; /* just advance */
1091 else if (c == '.') {
1093 * we need to deal with consequences of the legacy
1094 * parser, like "1.nolist" being two tokens
1095 * (TOK_NUMBER, TOK_ID) here; at least give it
1096 * a shot for now. In the future, we probably need
1097 * a flex-based scanner with proper pattern matching
1098 * to do it as well as it can be done. Nothing in
1099 * the world is going to help the person who wants
1100 * 0x123.p16 interpreted as two tokens, though.
1102 r = p;
1103 while (*r == '_')
1104 r++;
1106 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1107 (!is_hex && (*r == 'e' || *r == 'E')) ||
1108 (*r == 'p' || *r == 'P')) {
1109 p = r;
1110 is_float = true;
1111 } else
1112 break; /* Terminate the token */
1113 } else
1114 break;
1116 p--; /* Point to first character beyond number */
1118 if (p == line+1 && *line == '$') {
1119 type = TOK_OTHER; /* TOKEN_HERE */
1120 } else {
1121 if (has_e && !is_hex) {
1122 /* 1e13 is floating-point, but 1e13h is not */
1123 is_float = true;
1126 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1128 } else if (nasm_isspace(*p)) {
1129 type = TOK_WHITESPACE;
1130 p = nasm_skip_spaces(p);
1132 * Whitespace just before end-of-line is discarded by
1133 * pretending it's a comment; whitespace just before a
1134 * comment gets lumped into the comment.
1136 if (!*p || *p == ';') {
1137 type = TOK_COMMENT;
1138 while (*p)
1139 p++;
1141 } else if (*p == ';') {
1142 type = TOK_COMMENT;
1143 while (*p)
1144 p++;
1145 } else {
1147 * Anything else is an operator of some kind. We check
1148 * for all the double-character operators (>>, <<, //,
1149 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1150 * else is a single-character operator.
1152 type = TOK_OTHER;
1153 if ((p[0] == '>' && p[1] == '>') ||
1154 (p[0] == '<' && p[1] == '<') ||
1155 (p[0] == '/' && p[1] == '/') ||
1156 (p[0] == '<' && p[1] == '=') ||
1157 (p[0] == '>' && p[1] == '=') ||
1158 (p[0] == '=' && p[1] == '=') ||
1159 (p[0] == '!' && p[1] == '=') ||
1160 (p[0] == '<' && p[1] == '>') ||
1161 (p[0] == '&' && p[1] == '&') ||
1162 (p[0] == '|' && p[1] == '|') ||
1163 (p[0] == '^' && p[1] == '^')) {
1164 p++;
1166 p++;
1169 /* Handling unterminated string by UNV */
1170 /*if (type == -1)
1172 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1173 t->text[p-line] = *line;
1174 tail = &t->next;
1176 else */
1177 if (type != TOK_COMMENT) {
1178 *tail = t = new_Token(NULL, type, line, p - line);
1179 tail = &t->next;
1181 line = p;
1183 return list;
1187 * this function allocates a new managed block of memory and
1188 * returns a pointer to the block. The managed blocks are
1189 * deleted only all at once by the delete_Blocks function.
1191 static void *new_Block(size_t size)
1193 Blocks *b = &blocks;
1195 /* first, get to the end of the linked list */
1196 while (b->next)
1197 b = b->next;
1199 /* now allocate the requested chunk */
1200 b->chunk = nasm_malloc(size);
1202 /* now allocate a new block for the next request */
1203 b->next = nasm_zalloc(sizeof(Blocks));
1205 return b->chunk;
1209 * this function deletes all managed blocks of memory
1211 static void delete_Blocks(void)
1213 Blocks *a, *b = &blocks;
1216 * keep in mind that the first block, pointed to by blocks
1217 * is a static and not dynamically allocated, so we don't
1218 * free it.
1220 while (b) {
1221 if (b->chunk)
1222 nasm_free(b->chunk);
1223 a = b;
1224 b = b->next;
1225 if (a != &blocks)
1226 nasm_free(a);
1231 * this function creates a new Token and passes a pointer to it
1232 * back to the caller. It sets the type and text elements, and
1233 * also the a.mac and next elements to NULL.
1235 static Token *new_Token(Token * next, enum pp_token_type type,
1236 const char *text, int txtlen)
1238 Token *t;
1239 int i;
1241 if (!freeTokens) {
1242 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1243 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1244 freeTokens[i].next = &freeTokens[i + 1];
1245 freeTokens[i].next = NULL;
1247 t = freeTokens;
1248 freeTokens = t->next;
1249 t->next = next;
1250 t->a.mac = NULL;
1251 t->type = type;
1252 if (type == TOK_WHITESPACE || !text) {
1253 t->text = NULL;
1254 } else {
1255 if (txtlen == 0)
1256 txtlen = strlen(text);
1257 t->text = nasm_malloc(txtlen+1);
1258 memcpy(t->text, text, txtlen);
1259 t->text[txtlen] = '\0';
1261 return t;
1264 static Token *copy_Token(Token * tline)
1266 Token *t, *tt, *first = NULL, *prev = NULL;
1267 int i;
1268 for (tt = tline; tt != NULL; tt = tt->next) {
1269 if (!freeTokens) {
1270 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1271 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1272 freeTokens[i].next = &freeTokens[i + 1];
1273 freeTokens[i].next = NULL;
1275 t = freeTokens;
1276 freeTokens = t->next;
1277 t->next = NULL;
1278 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1279 t->a.mac = tt->a.mac;
1280 t->a.len = tt->a.len;
1281 t->type = tt->type;
1282 if (prev != NULL) {
1283 prev->next = t;
1284 } else {
1285 first = t;
1287 prev = t;
1289 return first;
1292 static Token *delete_Token(Token * t)
1294 Token *next = t->next;
1295 nasm_free(t->text);
1296 t->next = freeTokens;
1297 freeTokens = t;
1298 return next;
1302 * Convert a line of tokens back into text.
1303 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1304 * will be transformed into ..@ctxnum.xxx
1306 static char *detoken(Token * tlist, bool expand_locals)
1308 Token *t;
1309 char *line, *p;
1310 const char *q;
1311 int len = 0;
1313 list_for_each(t, tlist) {
1314 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1315 char *v;
1316 char *q = t->text;
1318 v = t->text + 2;
1319 if (*v == '\'' || *v == '\"' || *v == '`') {
1320 size_t len = nasm_unquote(v, NULL);
1321 size_t clen = strlen(v);
1323 if (len != clen) {
1324 error(ERR_NONFATAL | ERR_PASS1,
1325 "NUL character in %! string");
1326 v = NULL;
1330 if (v) {
1331 char *p = getenv(v);
1332 if (!p) {
1333 error(ERR_NONFATAL | ERR_PASS1,
1334 "nonexistent environment variable `%s'", v);
1335 p = "";
1337 t->text = nasm_strdup(p);
1339 nasm_free(q);
1342 /* Expand local macros here and not during preprocessing */
1343 if (expand_locals &&
1344 t->type == TOK_PREPROC_ID && t->text &&
1345 t->text[0] == '%' && t->text[1] == '$') {
1346 const char *q;
1347 char *p;
1348 Context *ctx = get_ctx(t->text, &q, false);
1349 if (ctx) {
1350 char buffer[40];
1351 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1352 p = nasm_strcat(buffer, q);
1353 nasm_free(t->text);
1354 t->text = p;
1358 /* Expand %? and %?? directives */
1359 if ((istk->expansion != NULL) &&
1360 ((t->type == TOK_PREPROC_Q) ||
1361 (t->type == TOK_PREPROC_QQ))) {
1362 ExpInv *ei;
1363 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1364 if (ei->type == EXP_MMACRO) {
1365 nasm_free(t->text);
1366 if (t->type == TOK_PREPROC_Q) {
1367 t->text = nasm_strdup(ei->name);
1368 } else {
1369 t->text = nasm_strdup(ei->def->name);
1371 break;
1376 if (t->type == TOK_WHITESPACE)
1377 len++;
1378 else if (t->text)
1379 len += strlen(t->text);
1382 p = line = nasm_malloc(len + 1);
1384 list_for_each(t, tlist) {
1385 if (t->type == TOK_WHITESPACE) {
1386 *p++ = ' ';
1387 } else if (t->text) {
1388 q = t->text;
1389 while (*q)
1390 *p++ = *q++;
1393 *p = '\0';
1395 return line;
1399 * Initialize a new Line
1401 static inline Line *new_Line(void)
1403 return (Line *)nasm_zalloc(sizeof(Line));
1408 * Initialize a new Expansion Definition
1410 static ExpDef *new_ExpDef(int exp_type)
1412 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1413 ed->type = exp_type;
1414 ed->casesense = true;
1415 ed->state = COND_NEVER;
1417 return ed;
1422 * Initialize a new Expansion Instance
1424 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1426 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1427 ei->type = exp_type;
1428 ei->def = ed;
1429 ei->unique = ++unique;
1431 if ((istk->mmac_depth < 1) &&
1432 (istk->expansion == NULL) &&
1433 (ed != NULL) &&
1434 (ed->type != EXP_MMACRO) &&
1435 (ed->type != EXP_REP) &&
1436 (ed->type != EXP_WHILE)) {
1437 ei->linnum = src_get_linnum();
1438 src_set_linnum(ei->linnum - ed->linecount - 1);
1439 } else {
1440 ei->linnum = -1;
1442 if ((istk->expansion == NULL) ||
1443 (ei->type == EXP_MMACRO)) {
1444 ei->relno = 0;
1445 } else {
1446 ei->relno = istk->expansion->lineno;
1447 if (ed != NULL) {
1448 ei->relno -= (ed->linecount + 1);
1451 return ei;
1455 * A scanner, suitable for use by the expression evaluator, which
1456 * operates on a line of Tokens. Expects a pointer to a pointer to
1457 * the first token in the line to be passed in as its private_data
1458 * field.
1460 * FIX: This really needs to be unified with stdscan.
1462 static int ppscan(void *private_data, struct tokenval *tokval)
1464 Token **tlineptr = private_data;
1465 Token *tline;
1466 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1468 do {
1469 tline = *tlineptr;
1470 *tlineptr = tline ? tline->next : NULL;
1471 } while (tline && (tline->type == TOK_WHITESPACE ||
1472 tline->type == TOK_COMMENT));
1474 if (!tline)
1475 return tokval->t_type = TOKEN_EOS;
1477 tokval->t_charptr = tline->text;
1479 if (tline->text[0] == '$' && !tline->text[1])
1480 return tokval->t_type = TOKEN_HERE;
1481 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1482 return tokval->t_type = TOKEN_BASE;
1484 if (tline->type == TOK_ID) {
1485 p = tokval->t_charptr = tline->text;
1486 if (p[0] == '$') {
1487 tokval->t_charptr++;
1488 return tokval->t_type = TOKEN_ID;
1491 for (r = p, s = ourcopy; *r; r++) {
1492 if (r >= p+MAX_KEYWORD)
1493 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1494 *s++ = nasm_tolower(*r);
1496 *s = '\0';
1497 /* right, so we have an identifier sitting in temp storage. now,
1498 * is it actually a register or instruction name, or what? */
1499 return nasm_token_hash(ourcopy, tokval);
1502 if (tline->type == TOK_NUMBER) {
1503 bool rn_error;
1504 tokval->t_integer = readnum(tline->text, &rn_error);
1505 tokval->t_charptr = tline->text;
1506 if (rn_error)
1507 return tokval->t_type = TOKEN_ERRNUM;
1508 else
1509 return tokval->t_type = TOKEN_NUM;
1512 if (tline->type == TOK_FLOAT) {
1513 return tokval->t_type = TOKEN_FLOAT;
1516 if (tline->type == TOK_STRING) {
1517 char bq, *ep;
1519 bq = tline->text[0];
1520 tokval->t_charptr = tline->text;
1521 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1523 if (ep[0] != bq || ep[1] != '\0')
1524 return tokval->t_type = TOKEN_ERRSTR;
1525 else
1526 return tokval->t_type = TOKEN_STR;
1529 if (tline->type == TOK_OTHER) {
1530 if (!strcmp(tline->text, "<<"))
1531 return tokval->t_type = TOKEN_SHL;
1532 if (!strcmp(tline->text, ">>"))
1533 return tokval->t_type = TOKEN_SHR;
1534 if (!strcmp(tline->text, "//"))
1535 return tokval->t_type = TOKEN_SDIV;
1536 if (!strcmp(tline->text, "%%"))
1537 return tokval->t_type = TOKEN_SMOD;
1538 if (!strcmp(tline->text, "=="))
1539 return tokval->t_type = TOKEN_EQ;
1540 if (!strcmp(tline->text, "<>"))
1541 return tokval->t_type = TOKEN_NE;
1542 if (!strcmp(tline->text, "!="))
1543 return tokval->t_type = TOKEN_NE;
1544 if (!strcmp(tline->text, "<="))
1545 return tokval->t_type = TOKEN_LE;
1546 if (!strcmp(tline->text, ">="))
1547 return tokval->t_type = TOKEN_GE;
1548 if (!strcmp(tline->text, "&&"))
1549 return tokval->t_type = TOKEN_DBL_AND;
1550 if (!strcmp(tline->text, "^^"))
1551 return tokval->t_type = TOKEN_DBL_XOR;
1552 if (!strcmp(tline->text, "||"))
1553 return tokval->t_type = TOKEN_DBL_OR;
1557 * We have no other options: just return the first character of
1558 * the token text.
1560 return tokval->t_type = tline->text[0];
1564 * Compare a string to the name of an existing macro; this is a
1565 * simple wrapper which calls either strcmp or nasm_stricmp
1566 * depending on the value of the `casesense' parameter.
1568 static int mstrcmp(const char *p, const char *q, bool casesense)
1570 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1574 * Compare a string to the name of an existing macro; this is a
1575 * simple wrapper which calls either strcmp or nasm_stricmp
1576 * depending on the value of the `casesense' parameter.
1578 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1580 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1584 * Return the Context structure associated with a %$ token. Return
1585 * NULL, having _already_ reported an error condition, if the
1586 * context stack isn't deep enough for the supplied number of $
1587 * signs.
1588 * If all_contexts == true, contexts that enclose current are
1589 * also scanned for such smacro, until it is found; if not -
1590 * only the context that directly results from the number of $'s
1591 * in variable's name.
1593 * If "namep" is non-NULL, set it to the pointer to the macro name
1594 * tail, i.e. the part beyond %$...
1596 static Context *get_ctx(const char *name, const char **namep,
1597 bool all_contexts)
1599 Context *ctx;
1600 SMacro *m;
1601 int i;
1603 if (namep)
1604 *namep = name;
1606 if (!name || name[0] != '%' || name[1] != '$')
1607 return NULL;
1609 if (!cstk) {
1610 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1611 return NULL;
1614 name += 2;
1615 ctx = cstk;
1616 i = 0;
1617 while (ctx && *name == '$') {
1618 name++;
1619 i++;
1620 ctx = ctx->next;
1622 if (!ctx) {
1623 error(ERR_NONFATAL, "`%s': context stack is only"
1624 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1625 return NULL;
1628 if (namep)
1629 *namep = name;
1631 if (!all_contexts)
1632 return ctx;
1634 do {
1635 /* Search for this smacro in found context */
1636 m = hash_findix(&ctx->localmac, name);
1637 while (m) {
1638 if (!mstrcmp(m->name, name, m->casesense))
1639 return ctx;
1640 m = m->next;
1642 ctx = ctx->next;
1644 while (ctx);
1645 return NULL;
1649 * Check to see if a file is already in a string list
1651 static bool in_list(const StrList *list, const char *str)
1653 while (list) {
1654 if (!strcmp(list->str, str))
1655 return true;
1656 list = list->next;
1658 return false;
1662 * Open an include file. This routine must always return a valid
1663 * file pointer if it returns - it's responsible for throwing an
1664 * ERR_FATAL and bombing out completely if not. It should also try
1665 * the include path one by one until it finds the file or reaches
1666 * the end of the path.
1668 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1669 bool missing_ok)
1671 FILE *fp;
1672 char *prefix = "";
1673 IncPath *ip = ipath;
1674 int len = strlen(file);
1675 size_t prefix_len = 0;
1676 StrList *sl;
1678 while (1) {
1679 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1680 sl->next = NULL;
1681 memcpy(sl->str, prefix, prefix_len);
1682 memcpy(sl->str+prefix_len, file, len+1);
1683 fp = fopen(sl->str, "r");
1684 if (fp && dhead && !in_list(*dhead, sl->str)) {
1685 **dtail = sl;
1686 *dtail = &sl->next;
1687 } else {
1688 nasm_free(sl);
1690 if (fp)
1691 return fp;
1692 if (!ip) {
1693 if (!missing_ok)
1694 break;
1695 prefix = NULL;
1696 } else {
1697 prefix = ip->path;
1698 ip = ip->next;
1700 if (prefix) {
1701 prefix_len = strlen(prefix);
1702 } else {
1703 /* -MG given and file not found */
1704 if (dhead && !in_list(*dhead, file)) {
1705 sl = nasm_malloc(len+1+sizeof sl->next);
1706 sl->next = NULL;
1707 strcpy(sl->str, file);
1708 **dtail = sl;
1709 *dtail = &sl->next;
1711 return NULL;
1715 error(ERR_FATAL, "unable to open include file `%s'", file);
1716 return NULL;
1720 * Determine if we should warn on defining a single-line macro of
1721 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1722 * return true if _any_ single-line macro of that name is defined.
1723 * Otherwise, will return true if a single-line macro with either
1724 * `nparam' or no parameters is defined.
1726 * If a macro with precisely the right number of parameters is
1727 * defined, or nparam is -1, the address of the definition structure
1728 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1729 * is NULL, no action will be taken regarding its contents, and no
1730 * error will occur.
1732 * Note that this is also called with nparam zero to resolve
1733 * `ifdef'.
1735 * If you already know which context macro belongs to, you can pass
1736 * the context pointer as first parameter; if you won't but name begins
1737 * with %$ the context will be automatically computed. If all_contexts
1738 * is true, macro will be searched in outer contexts as well.
1740 static bool
1741 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1742 bool nocase)
1744 struct hash_table *smtbl;
1745 SMacro *m;
1747 if (ctx) {
1748 smtbl = &ctx->localmac;
1749 } else if (name[0] == '%' && name[1] == '$') {
1750 if (cstk)
1751 ctx = get_ctx(name, &name, false);
1752 if (!ctx)
1753 return false; /* got to return _something_ */
1754 smtbl = &ctx->localmac;
1755 } else {
1756 smtbl = &smacros;
1758 m = (SMacro *) hash_findix(smtbl, name);
1760 while (m) {
1761 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1762 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1763 if (defn) {
1764 if (nparam == (int) m->nparam || nparam == -1)
1765 *defn = m;
1766 else
1767 *defn = NULL;
1769 return true;
1771 m = m->next;
1774 return false;
1778 * Count and mark off the parameters in a multi-line macro call.
1779 * This is called both from within the multi-line macro expansion
1780 * code, and also to mark off the default parameters when provided
1781 * in a %macro definition line.
1783 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1785 int paramsize, brace;
1787 *nparam = paramsize = 0;
1788 *params = NULL;
1789 while (t) {
1790 /* +1: we need space for the final NULL */
1791 if (*nparam+1 >= paramsize) {
1792 paramsize += PARAM_DELTA;
1793 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1795 skip_white_(t);
1796 brace = false;
1797 if (tok_is_(t, "{"))
1798 brace = true;
1799 (*params)[(*nparam)++] = t;
1800 while (tok_isnt_(t, brace ? "}" : ","))
1801 t = t->next;
1802 if (t) { /* got a comma/brace */
1803 t = t->next;
1804 if (brace) {
1806 * Now we've found the closing brace, look further
1807 * for the comma.
1809 skip_white_(t);
1810 if (tok_isnt_(t, ",")) {
1811 error(ERR_NONFATAL,
1812 "braces do not enclose all of macro parameter");
1813 while (tok_isnt_(t, ","))
1814 t = t->next;
1816 if (t)
1817 t = t->next; /* eat the comma */
1824 * Determine whether one of the various `if' conditions is true or
1825 * not.
1827 * We must free the tline we get passed.
1829 static bool if_condition(Token * tline, enum preproc_token ct)
1831 enum pp_conditional i = PP_COND(ct);
1832 bool j;
1833 Token *t, *tt, **tptr, *origline;
1834 struct tokenval tokval;
1835 expr *evalresult;
1836 enum pp_token_type needtype;
1837 char *p;
1839 origline = tline;
1841 switch (i) {
1842 case PPC_IFCTX:
1843 j = false; /* have we matched yet? */
1844 while (true) {
1845 skip_white_(tline);
1846 if (!tline)
1847 break;
1848 if (tline->type != TOK_ID) {
1849 error(ERR_NONFATAL,
1850 "`%s' expects context identifiers", pp_directives[ct]);
1851 free_tlist(origline);
1852 return -1;
1854 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1855 j = true;
1856 tline = tline->next;
1858 break;
1860 case PPC_IFDEF:
1861 j = false; /* have we matched yet? */
1862 while (tline) {
1863 skip_white_(tline);
1864 if (!tline || (tline->type != TOK_ID &&
1865 (tline->type != TOK_PREPROC_ID ||
1866 tline->text[1] != '$'))) {
1867 error(ERR_NONFATAL,
1868 "`%s' expects macro identifiers", pp_directives[ct]);
1869 goto fail;
1871 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1872 j = true;
1873 tline = tline->next;
1875 break;
1877 case PPC_IFENV:
1878 tline = expand_smacro(tline);
1879 j = false; /* have we matched yet? */
1880 while (tline) {
1881 skip_white_(tline);
1882 if (!tline || (tline->type != TOK_ID &&
1883 tline->type != TOK_STRING &&
1884 (tline->type != TOK_PREPROC_ID ||
1885 tline->text[1] != '!'))) {
1886 error(ERR_NONFATAL,
1887 "`%s' expects environment variable names",
1888 pp_directives[ct]);
1889 goto fail;
1891 p = tline->text;
1892 if (tline->type == TOK_PREPROC_ID)
1893 p += 2; /* Skip leading %! */
1894 if (*p == '\'' || *p == '\"' || *p == '`')
1895 nasm_unquote_cstr(p, ct);
1896 if (getenv(p))
1897 j = true;
1898 tline = tline->next;
1900 break;
1902 case PPC_IFIDN:
1903 case PPC_IFIDNI:
1904 tline = expand_smacro(tline);
1905 t = tt = tline;
1906 while (tok_isnt_(tt, ","))
1907 tt = tt->next;
1908 if (!tt) {
1909 error(ERR_NONFATAL,
1910 "`%s' expects two comma-separated arguments",
1911 pp_directives[ct]);
1912 goto fail;
1914 tt = tt->next;
1915 j = true; /* assume equality unless proved not */
1916 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1917 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1918 error(ERR_NONFATAL, "`%s': more than one comma on line",
1919 pp_directives[ct]);
1920 goto fail;
1922 if (t->type == TOK_WHITESPACE) {
1923 t = t->next;
1924 continue;
1926 if (tt->type == TOK_WHITESPACE) {
1927 tt = tt->next;
1928 continue;
1930 if (tt->type != t->type) {
1931 j = false; /* found mismatching tokens */
1932 break;
1934 /* When comparing strings, need to unquote them first */
1935 if (t->type == TOK_STRING) {
1936 size_t l1 = nasm_unquote(t->text, NULL);
1937 size_t l2 = nasm_unquote(tt->text, NULL);
1939 if (l1 != l2) {
1940 j = false;
1941 break;
1943 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1944 j = false;
1945 break;
1947 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1948 j = false; /* found mismatching tokens */
1949 break;
1952 t = t->next;
1953 tt = tt->next;
1955 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1956 j = false; /* trailing gunk on one end or other */
1957 break;
1959 case PPC_IFMACRO:
1961 bool found = false;
1962 ExpDef searching, *ed;
1964 skip_white_(tline);
1965 tline = expand_id(tline);
1966 if (!tok_type_(tline, TOK_ID)) {
1967 error(ERR_NONFATAL,
1968 "`%s' expects a macro name", pp_directives[ct]);
1969 goto fail;
1971 memset(&searching, 0, sizeof(searching));
1972 searching.name = nasm_strdup(tline->text);
1973 searching.casesense = true;
1974 searching.nparam_max = INT_MAX;
1975 tline = expand_smacro(tline->next);
1976 skip_white_(tline);
1977 if (!tline) {
1978 } else if (!tok_type_(tline, TOK_NUMBER)) {
1979 error(ERR_NONFATAL,
1980 "`%s' expects a parameter count or nothing",
1981 pp_directives[ct]);
1982 } else {
1983 searching.nparam_min = searching.nparam_max =
1984 readnum(tline->text, &j);
1985 if (j)
1986 error(ERR_NONFATAL,
1987 "unable to parse parameter count `%s'",
1988 tline->text);
1990 if (tline && tok_is_(tline->next, "-")) {
1991 tline = tline->next->next;
1992 if (tok_is_(tline, "*"))
1993 searching.nparam_max = INT_MAX;
1994 else if (!tok_type_(tline, TOK_NUMBER))
1995 error(ERR_NONFATAL,
1996 "`%s' expects a parameter count after `-'",
1997 pp_directives[ct]);
1998 else {
1999 searching.nparam_max = readnum(tline->text, &j);
2000 if (j)
2001 error(ERR_NONFATAL,
2002 "unable to parse parameter count `%s'",
2003 tline->text);
2004 if (searching.nparam_min > searching.nparam_max)
2005 error(ERR_NONFATAL,
2006 "minimum parameter count exceeds maximum");
2009 if (tline && tok_is_(tline->next, "+")) {
2010 tline = tline->next;
2011 searching.plus = true;
2013 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2014 while (ed != NULL) {
2015 if (!strcmp(ed->name, searching.name) &&
2016 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2017 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
2018 found = true;
2019 break;
2021 ed = ed->next;
2023 if (tline && tline->next)
2024 error(ERR_WARNING|ERR_PASS1,
2025 "trailing garbage after %%ifmacro ignored");
2026 nasm_free(searching.name);
2027 j = found;
2028 break;
2031 case PPC_IFID:
2032 needtype = TOK_ID;
2033 goto iftype;
2034 case PPC_IFNUM:
2035 needtype = TOK_NUMBER;
2036 goto iftype;
2037 case PPC_IFSTR:
2038 needtype = TOK_STRING;
2039 goto iftype;
2041 iftype:
2042 t = tline = expand_smacro(tline);
2044 while (tok_type_(t, TOK_WHITESPACE) ||
2045 (needtype == TOK_NUMBER &&
2046 tok_type_(t, TOK_OTHER) &&
2047 (t->text[0] == '-' || t->text[0] == '+') &&
2048 !t->text[1]))
2049 t = t->next;
2051 j = tok_type_(t, needtype);
2052 break;
2054 case PPC_IFTOKEN:
2055 t = tline = expand_smacro(tline);
2056 while (tok_type_(t, TOK_WHITESPACE))
2057 t = t->next;
2059 j = false;
2060 if (t) {
2061 t = t->next; /* Skip the actual token */
2062 while (tok_type_(t, TOK_WHITESPACE))
2063 t = t->next;
2064 j = !t; /* Should be nothing left */
2066 break;
2068 case PPC_IFEMPTY:
2069 t = tline = expand_smacro(tline);
2070 while (tok_type_(t, TOK_WHITESPACE))
2071 t = t->next;
2073 j = !t; /* Should be empty */
2074 break;
2076 case PPC_IF:
2077 t = tline = expand_smacro(tline);
2078 tptr = &t;
2079 tokval.t_type = TOKEN_INVALID;
2080 evalresult = evaluate(ppscan, tptr, &tokval,
2081 NULL, pass | CRITICAL, error, NULL);
2082 if (!evalresult)
2083 return -1;
2084 if (tokval.t_type)
2085 error(ERR_WARNING|ERR_PASS1,
2086 "trailing garbage after expression ignored");
2087 if (!is_simple(evalresult)) {
2088 error(ERR_NONFATAL,
2089 "non-constant value given to `%s'", pp_directives[ct]);
2090 goto fail;
2092 j = reloc_value(evalresult) != 0;
2093 break;
2095 default:
2096 error(ERR_FATAL,
2097 "preprocessor directive `%s' not yet implemented",
2098 pp_directives[ct]);
2099 goto fail;
2102 free_tlist(origline);
2103 return j ^ PP_NEGATIVE(ct);
2105 fail:
2106 free_tlist(origline);
2107 return -1;
2111 * Common code for defining an smacro
2113 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2114 int nparam, Token *expansion)
2116 SMacro *smac, **smhead;
2117 struct hash_table *smtbl;
2119 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2120 if (!smac) {
2121 error(ERR_WARNING|ERR_PASS1,
2122 "single-line macro `%s' defined both with and"
2123 " without parameters", mname);
2125 * Some instances of the old code considered this a failure,
2126 * some others didn't. What is the right thing to do here?
2128 free_tlist(expansion);
2129 return false; /* Failure */
2130 } else {
2132 * We're redefining, so we have to take over an
2133 * existing SMacro structure. This means freeing
2134 * what was already in it.
2136 nasm_free(smac->name);
2137 free_tlist(smac->expansion);
2139 } else {
2140 smtbl = ctx ? &ctx->localmac : &smacros;
2141 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2142 smac = nasm_zalloc(sizeof(SMacro));
2143 smac->next = *smhead;
2144 *smhead = smac;
2146 smac->name = nasm_strdup(mname);
2147 smac->casesense = casesense;
2148 smac->nparam = nparam;
2149 smac->expansion = expansion;
2150 smac->in_progress = false;
2151 return true; /* Success */
2155 * Undefine an smacro
2157 static void undef_smacro(Context *ctx, const char *mname)
2159 SMacro **smhead, *s, **sp;
2160 struct hash_table *smtbl;
2162 smtbl = ctx ? &ctx->localmac : &smacros;
2163 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2165 if (smhead) {
2167 * We now have a macro name... go hunt for it.
2169 sp = smhead;
2170 while ((s = *sp) != NULL) {
2171 if (!mstrcmp(s->name, mname, s->casesense)) {
2172 *sp = s->next;
2173 nasm_free(s->name);
2174 free_tlist(s->expansion);
2175 nasm_free(s);
2176 } else {
2177 sp = &s->next;
2184 * Parse a mmacro specification.
2186 static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
2188 bool err;
2190 tline = tline->next;
2191 skip_white_(tline);
2192 tline = expand_id(tline);
2193 if (!tok_type_(tline, TOK_ID)) {
2194 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2195 return false;
2198 def->name = nasm_strdup(tline->text);
2199 def->plus = false;
2200 def->nolist = false;
2201 // def->in_progress = 0;
2202 // def->rep_nest = NULL;
2203 def->nparam_min = 0;
2204 def->nparam_max = 0;
2206 tline = expand_smacro(tline->next);
2207 skip_white_(tline);
2208 if (!tok_type_(tline, TOK_NUMBER)) {
2209 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2210 } else {
2211 def->nparam_min = def->nparam_max =
2212 readnum(tline->text, &err);
2213 if (err)
2214 error(ERR_NONFATAL,
2215 "unable to parse parameter count `%s'", tline->text);
2217 if (tline && tok_is_(tline->next, "-")) {
2218 tline = tline->next->next;
2219 if (tok_is_(tline, "*")) {
2220 def->nparam_max = INT_MAX;
2221 } else if (!tok_type_(tline, TOK_NUMBER)) {
2222 error(ERR_NONFATAL,
2223 "`%s' expects a parameter count after `-'", directive);
2224 } else {
2225 def->nparam_max = readnum(tline->text, &err);
2226 if (err) {
2227 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2228 tline->text);
2230 if (def->nparam_min > def->nparam_max) {
2231 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2235 if (tline && tok_is_(tline->next, "+")) {
2236 tline = tline->next;
2237 def->plus = true;
2239 if (tline && tok_type_(tline->next, TOK_ID) &&
2240 !nasm_stricmp(tline->next->text, ".nolist")) {
2241 tline = tline->next;
2242 def->nolist = true;
2246 * Handle default parameters.
2248 if (tline && tline->next) {
2249 def->dlist = tline->next;
2250 tline->next = NULL;
2251 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2252 } else {
2253 def->dlist = NULL;
2254 def->defaults = NULL;
2256 def->line = NULL;
2258 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2259 !def->plus)
2260 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2261 "too many default macro parameters");
2263 return true;
2268 * Decode a size directive
2270 static int parse_size(const char *str) {
2271 static const char *size_names[] =
2272 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2273 static const int sizes[] =
2274 { 0, 1, 4, 16, 8, 10, 2, 32 };
2276 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2280 * find and process pragma directive in passed line
2281 * Find out if a line contains a pragma directive, and deal
2282 * with it if so.
2284 * @param tline a pointer to the current tokeninzed line linked list
2285 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2288 static int do_pragma(Token * tline)
2290 enum pragma_token i;
2292 i = pr_token_hash(tline->text);
2294 switch (i) {
2295 case PR_INVALID:
2296 error(ERR_NONFATAL, "unknown pragma directive `%s'",
2297 tline->text);
2298 return NO_DIRECTIVE_FOUND; /* didn't get it */
2300 default:
2301 error(ERR_FATAL,
2302 "pragma directive `%s' not yet implemented",
2303 pr_directives[i]);
2304 return DIRECTIVE_FOUND;
2310 * find and process preprocessor directive in passed line
2311 * Find out if a line contains a preprocessor directive, and deal
2312 * with it if so.
2314 * If a directive _is_ found, it is the responsibility of this routine
2315 * (and not the caller) to free_tlist() the line.
2317 * @param tline a pointer to the current tokeninzed line linked list
2318 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2321 static int do_directive(Token * tline)
2323 enum preproc_token i;
2324 int j;
2325 bool err;
2326 int nparam;
2327 bool nolist;
2328 bool casesense;
2329 int k, m;
2330 int offset;
2331 char *p, *pp;
2332 const char *mname;
2333 Include *inc;
2334 Context *ctx;
2335 Line *l;
2336 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2337 struct tokenval tokval;
2338 expr *evalresult;
2339 ExpDef *ed, *eed, **edhead;
2340 ExpInv *ei, *eei;
2341 int64_t count;
2342 size_t len;
2343 int severity;
2345 origline = tline;
2347 skip_white_(tline);
2348 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2349 (tline->text[1] == '%' || tline->text[1] == '$'
2350 || tline->text[1] == '!'))
2351 return NO_DIRECTIVE_FOUND;
2353 i = pp_token_hash(tline->text);
2355 switch (i) {
2356 case PP_INVALID:
2357 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2358 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2359 tline->text);
2360 return NO_DIRECTIVE_FOUND; /* didn't get it */
2362 case PP_STACKSIZE:
2363 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2364 /* Directive to tell NASM what the default stack size is. The
2365 * default is for a 16-bit stack, and this can be overriden with
2366 * %stacksize large.
2368 tline = tline->next;
2369 if (tline && tline->type == TOK_WHITESPACE)
2370 tline = tline->next;
2371 if (!tline || tline->type != TOK_ID) {
2372 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2373 free_tlist(origline);
2374 return DIRECTIVE_FOUND;
2376 if (nasm_stricmp(tline->text, "flat") == 0) {
2377 /* All subsequent ARG directives are for a 32-bit stack */
2378 StackSize = 4;
2379 StackPointer = "ebp";
2380 ArgOffset = 8;
2381 LocalOffset = 0;
2382 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2383 /* All subsequent ARG directives are for a 64-bit stack */
2384 StackSize = 8;
2385 StackPointer = "rbp";
2386 ArgOffset = 16;
2387 LocalOffset = 0;
2388 } else if (nasm_stricmp(tline->text, "large") == 0) {
2389 /* All subsequent ARG directives are for a 16-bit stack,
2390 * far function call.
2392 StackSize = 2;
2393 StackPointer = "bp";
2394 ArgOffset = 4;
2395 LocalOffset = 0;
2396 } else if (nasm_stricmp(tline->text, "small") == 0) {
2397 /* All subsequent ARG directives are for a 16-bit stack,
2398 * far function call. We don't support near functions.
2400 StackSize = 2;
2401 StackPointer = "bp";
2402 ArgOffset = 6;
2403 LocalOffset = 0;
2404 } else {
2405 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2406 free_tlist(origline);
2407 return DIRECTIVE_FOUND;
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2412 case PP_ARG:
2413 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2414 /* TASM like ARG directive to define arguments to functions, in
2415 * the following form:
2417 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2419 offset = ArgOffset;
2420 do {
2421 char *arg, directive[256];
2422 int size = StackSize;
2424 /* Find the argument name */
2425 tline = tline->next;
2426 if (tline && tline->type == TOK_WHITESPACE)
2427 tline = tline->next;
2428 if (!tline || tline->type != TOK_ID) {
2429 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2430 free_tlist(origline);
2431 return DIRECTIVE_FOUND;
2433 arg = tline->text;
2435 /* Find the argument size type */
2436 tline = tline->next;
2437 if (!tline || tline->type != TOK_OTHER
2438 || tline->text[0] != ':') {
2439 error(ERR_NONFATAL,
2440 "Syntax error processing `%%arg' directive");
2441 free_tlist(origline);
2442 return DIRECTIVE_FOUND;
2444 tline = tline->next;
2445 if (!tline || tline->type != TOK_ID) {
2446 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2451 /* Allow macro expansion of type parameter */
2452 tt = tokenize(tline->text);
2453 tt = expand_smacro(tt);
2454 size = parse_size(tt->text);
2455 if (!size) {
2456 error(ERR_NONFATAL,
2457 "Invalid size type for `%%arg' missing directive");
2458 free_tlist(tt);
2459 free_tlist(origline);
2460 return DIRECTIVE_FOUND;
2462 free_tlist(tt);
2464 /* Round up to even stack slots */
2465 size = ALIGN(size, StackSize);
2467 /* Now define the macro for the argument */
2468 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2469 arg, StackPointer, offset);
2470 do_directive(tokenize(directive));
2471 offset += size;
2473 /* Move to the next argument in the list */
2474 tline = tline->next;
2475 if (tline && tline->type == TOK_WHITESPACE)
2476 tline = tline->next;
2477 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2478 ArgOffset = offset;
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2482 case PP_LOCAL:
2483 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2484 /* TASM like LOCAL directive to define local variables for a
2485 * function, in the following form:
2487 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2489 * The '= LocalSize' at the end is ignored by NASM, but is
2490 * required by TASM to define the local parameter size (and used
2491 * by the TASM macro package).
2493 offset = LocalOffset;
2494 do {
2495 char *local, directive[256];
2496 int size = StackSize;
2498 /* Find the argument name */
2499 tline = tline->next;
2500 if (tline && tline->type == TOK_WHITESPACE)
2501 tline = tline->next;
2502 if (!tline || tline->type != TOK_ID) {
2503 error(ERR_NONFATAL,
2504 "`%%local' missing argument parameter");
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2508 local = tline->text;
2510 /* Find the argument size type */
2511 tline = tline->next;
2512 if (!tline || tline->type != TOK_OTHER
2513 || tline->text[0] != ':') {
2514 error(ERR_NONFATAL,
2515 "Syntax error processing `%%local' directive");
2516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
2519 tline = tline->next;
2520 if (!tline || tline->type != TOK_ID) {
2521 error(ERR_NONFATAL,
2522 "`%%local' missing size type parameter");
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2527 /* Allow macro expansion of type parameter */
2528 tt = tokenize(tline->text);
2529 tt = expand_smacro(tt);
2530 size = parse_size(tt->text);
2531 if (!size) {
2532 error(ERR_NONFATAL,
2533 "Invalid size type for `%%local' missing directive");
2534 free_tlist(tt);
2535 free_tlist(origline);
2536 return DIRECTIVE_FOUND;
2538 free_tlist(tt);
2540 /* Round up to even stack slots */
2541 size = ALIGN(size, StackSize);
2543 offset += size; /* Negative offset, increment before */
2545 /* Now define the macro for the argument */
2546 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2547 local, StackPointer, offset);
2548 do_directive(tokenize(directive));
2550 /* Now define the assign to setup the enter_c macro correctly */
2551 snprintf(directive, sizeof(directive),
2552 "%%assign %%$localsize %%$localsize+%d", size);
2553 do_directive(tokenize(directive));
2555 /* Move to the next argument in the list */
2556 tline = tline->next;
2557 if (tline && tline->type == TOK_WHITESPACE)
2558 tline = tline->next;
2559 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2560 LocalOffset = offset;
2561 free_tlist(origline);
2562 return DIRECTIVE_FOUND;
2564 case PP_CLEAR:
2565 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2566 if (tline->next)
2567 error(ERR_WARNING|ERR_PASS1,
2568 "trailing garbage after `%%clear' ignored");
2569 free_macros();
2570 init_macros();
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2574 case PP_DEPEND:
2575 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2576 t = tline->next = expand_smacro(tline->next);
2577 skip_white_(t);
2578 if (!t || (t->type != TOK_STRING &&
2579 t->type != TOK_INTERNAL_STRING)) {
2580 error(ERR_NONFATAL, "`%%depend' expects a file name");
2581 free_tlist(origline);
2582 return DIRECTIVE_FOUND; /* but we did _something_ */
2584 if (t->next)
2585 error(ERR_WARNING|ERR_PASS1,
2586 "trailing garbage after `%%depend' ignored");
2587 p = t->text;
2588 if (t->type != TOK_INTERNAL_STRING)
2589 nasm_unquote_cstr(p, i);
2590 if (dephead && !in_list(*dephead, p)) {
2591 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2592 sl->next = NULL;
2593 strcpy(sl->str, p);
2594 *deptail = sl;
2595 deptail = &sl->next;
2597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
2600 case PP_INCLUDE:
2601 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2602 t = tline->next = expand_smacro(tline->next);
2603 skip_white_(t);
2605 if (!t || (t->type != TOK_STRING &&
2606 t->type != TOK_INTERNAL_STRING)) {
2607 error(ERR_NONFATAL, "`%%include' expects a file name");
2608 free_tlist(origline);
2609 return DIRECTIVE_FOUND; /* but we did _something_ */
2611 if (t->next)
2612 error(ERR_WARNING|ERR_PASS1,
2613 "trailing garbage after `%%include' ignored");
2614 p = t->text;
2615 if (t->type != TOK_INTERNAL_STRING)
2616 nasm_unquote_cstr(p, i);
2617 inc = nasm_zalloc(sizeof(Include));
2618 inc->next = istk;
2619 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2620 if (!inc->fp) {
2621 /* -MG given but file not found */
2622 nasm_free(inc);
2623 } else {
2624 inc->fname = src_set_fname(nasm_strdup(p));
2625 inc->lineno = src_set_linnum(0);
2626 inc->lineinc = 1;
2627 inc->expansion = NULL;
2628 istk = inc;
2629 list->uplevel(LIST_INCLUDE);
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
2634 case PP_USE:
2635 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2637 static macros_t *use_pkg;
2638 const char *pkg_macro = NULL;
2640 tline = tline->next;
2641 skip_white_(tline);
2642 tline = expand_id(tline);
2644 if (!tline || (tline->type != TOK_STRING &&
2645 tline->type != TOK_INTERNAL_STRING &&
2646 tline->type != TOK_ID)) {
2647 error(ERR_NONFATAL, "`%%use' expects a package name");
2648 free_tlist(origline);
2649 return DIRECTIVE_FOUND; /* but we did _something_ */
2651 if (tline->next)
2652 error(ERR_WARNING|ERR_PASS1,
2653 "trailing garbage after `%%use' ignored");
2654 if (tline->type == TOK_STRING)
2655 nasm_unquote_cstr(tline->text, i);
2656 use_pkg = nasm_stdmac_find_package(tline->text);
2657 if (!use_pkg)
2658 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2659 else
2660 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2661 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2662 /* Not already included, go ahead and include it */
2663 stdmacpos = use_pkg;
2665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
2668 case PP_PUSH:
2669 case PP_REPL:
2670 case PP_POP:
2671 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2672 tline = tline->next;
2673 skip_white_(tline);
2674 tline = expand_id(tline);
2675 if (tline) {
2676 if (!tok_type_(tline, TOK_ID)) {
2677 error(ERR_NONFATAL, "`%s' expects a context identifier",
2678 pp_directives[i]);
2679 free_tlist(origline);
2680 return DIRECTIVE_FOUND; /* but we did _something_ */
2682 if (tline->next)
2683 error(ERR_WARNING|ERR_PASS1,
2684 "trailing garbage after `%s' ignored",
2685 pp_directives[i]);
2686 p = nasm_strdup(tline->text);
2687 } else {
2688 p = NULL; /* Anonymous */
2691 if (i == PP_PUSH) {
2692 ctx = nasm_zalloc(sizeof(Context));
2693 ctx->next = cstk;
2694 hash_init(&ctx->localmac, HASH_SMALL);
2695 ctx->name = p;
2696 ctx->number = unique++;
2697 cstk = ctx;
2698 } else {
2699 /* %pop or %repl */
2700 if (!cstk) {
2701 error(ERR_NONFATAL, "`%s': context stack is empty",
2702 pp_directives[i]);
2703 } else if (i == PP_POP) {
2704 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2705 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2706 "expected %s",
2707 cstk->name ? cstk->name : "anonymous", p);
2708 else
2709 ctx_pop();
2710 } else {
2711 /* i == PP_REPL */
2712 nasm_free(cstk->name);
2713 cstk->name = p;
2714 p = NULL;
2716 nasm_free(p);
2718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
2720 case PP_FATAL:
2721 severity = ERR_FATAL;
2722 goto issue_error;
2723 case PP_ERROR:
2724 severity = ERR_NONFATAL;
2725 goto issue_error;
2726 case PP_WARNING:
2727 severity = ERR_WARNING|ERR_WARN_USER;
2728 goto issue_error;
2730 issue_error:
2731 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2733 /* Only error out if this is the final pass */
2734 if (pass != 2 && i != PP_FATAL)
2735 return DIRECTIVE_FOUND;
2737 tline->next = expand_smacro(tline->next);
2738 tline = tline->next;
2739 skip_white_(tline);
2740 t = tline ? tline->next : NULL;
2741 skip_white_(t);
2742 if (tok_type_(tline, TOK_STRING) && !t) {
2743 /* The line contains only a quoted string */
2744 p = tline->text;
2745 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2746 error(severity, "%s", p);
2747 } else {
2748 /* Not a quoted string, or more than a quoted string */
2749 p = detoken(tline, false);
2750 error(severity, "%s", p);
2751 nasm_free(p);
2753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
2757 CASE_PP_IF:
2758 if (defining != NULL) {
2759 if (defining->type == EXP_IF) {
2760 defining->def_depth ++;
2762 return NO_DIRECTIVE_FOUND;
2764 if ((istk->expansion != NULL) &&
2765 (istk->expansion->emitting == false)) {
2766 j = COND_NEVER;
2767 } else {
2768 j = if_condition(tline->next, i);
2769 tline->next = NULL; /* it got freed */
2770 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2772 ed = new_ExpDef(EXP_IF);
2773 ed->state = j;
2774 ed->nolist = NULL;
2775 ed->def_depth = 0;
2776 ed->cur_depth = 0;
2777 ed->max_depth = 0;
2778 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2779 ed->prev = defining;
2780 defining = ed;
2781 free_tlist(origline);
2782 return DIRECTIVE_FOUND;
2784 CASE_PP_ELIF:
2785 if (defining != NULL) {
2786 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2787 return NO_DIRECTIVE_FOUND;
2790 if ((defining == NULL) || (defining->type != EXP_IF)) {
2791 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2793 switch (defining->state) {
2794 case COND_IF_TRUE:
2795 defining->state = COND_DONE;
2796 defining->ignoring = true;
2797 break;
2799 case COND_DONE:
2800 case COND_NEVER:
2801 defining->ignoring = true;
2802 break;
2804 case COND_ELSE_TRUE:
2805 case COND_ELSE_FALSE:
2806 error_precond(ERR_WARNING|ERR_PASS1,
2807 "`%%elif' after `%%else' ignored");
2808 defining->state = COND_NEVER;
2809 defining->ignoring = true;
2810 break;
2812 case COND_IF_FALSE:
2814 * IMPORTANT: In the case of %if, we will already have
2815 * called expand_mmac_params(); however, if we're
2816 * processing an %elif we must have been in a
2817 * non-emitting mode, which would have inhibited
2818 * the normal invocation of expand_mmac_params().
2819 * Therefore, we have to do it explicitly here.
2821 j = if_condition(expand_mmac_params(tline->next), i);
2822 tline->next = NULL; /* it got freed */
2823 defining->state =
2824 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2825 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
2826 break;
2828 free_tlist(origline);
2829 return DIRECTIVE_FOUND;
2831 case PP_ELSE:
2832 if (defining != NULL) {
2833 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2834 return NO_DIRECTIVE_FOUND;
2837 if (tline->next)
2838 error_precond(ERR_WARNING|ERR_PASS1,
2839 "trailing garbage after `%%else' ignored");
2840 if ((defining == NULL) || (defining->type != EXP_IF)) {
2841 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2843 switch (defining->state) {
2844 case COND_IF_TRUE:
2845 case COND_DONE:
2846 defining->state = COND_ELSE_FALSE;
2847 defining->ignoring = true;
2848 break;
2850 case COND_NEVER:
2851 defining->ignoring = true;
2852 break;
2854 case COND_IF_FALSE:
2855 defining->state = COND_ELSE_TRUE;
2856 defining->ignoring = false;
2857 break;
2859 case COND_ELSE_TRUE:
2860 case COND_ELSE_FALSE:
2861 error_precond(ERR_WARNING|ERR_PASS1,
2862 "`%%else' after `%%else' ignored.");
2863 defining->state = COND_NEVER;
2864 defining->ignoring = true;
2865 break;
2867 free_tlist(origline);
2868 return DIRECTIVE_FOUND;
2870 case PP_ENDIF:
2871 if (defining != NULL) {
2872 if (defining->type == EXP_IF) {
2873 if (defining->def_depth > 0) {
2874 defining->def_depth --;
2875 return NO_DIRECTIVE_FOUND;
2877 } else {
2878 return NO_DIRECTIVE_FOUND;
2881 if (tline->next)
2882 error_precond(ERR_WARNING|ERR_PASS1,
2883 "trailing garbage after `%%endif' ignored");
2884 if ((defining == NULL) || (defining->type != EXP_IF)) {
2885 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2886 return DIRECTIVE_FOUND;
2888 ed = defining;
2889 defining = ed->prev;
2890 ed->prev = expansions;
2891 expansions = ed;
2892 ei = new_ExpInv(EXP_IF, ed);
2893 ei->current = ed->line;
2894 ei->emitting = true;
2895 ei->prev = istk->expansion;
2896 istk->expansion = ei;
2897 free_tlist(origline);
2898 return DIRECTIVE_FOUND;
2900 case PP_RMACRO:
2901 case PP_IRMACRO:
2902 case PP_MACRO:
2903 case PP_IMACRO:
2904 if (defining != NULL) {
2905 if (defining->type == EXP_MMACRO) {
2906 defining->def_depth ++;
2908 return NO_DIRECTIVE_FOUND;
2910 ed = new_ExpDef(EXP_MMACRO);
2911 ed->max_depth =
2912 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2913 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2914 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2915 nasm_free(ed);
2916 ed = NULL;
2917 return DIRECTIVE_FOUND;
2919 ed->def_depth = 0;
2920 ed->cur_depth = 0;
2921 ed->max_depth = (ed->max_depth + 1);
2922 ed->ignoring = false;
2923 ed->prev = defining;
2924 defining = ed;
2926 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2927 while (eed) {
2928 if (!strcmp(eed->name, ed->name) &&
2929 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2930 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
2931 error(ERR_WARNING|ERR_PASS1,
2932 "redefining multi-line macro `%s'", ed->name);
2933 return DIRECTIVE_FOUND;
2935 eed = eed->next;
2937 free_tlist(origline);
2938 return DIRECTIVE_FOUND;
2940 case PP_ENDM:
2941 case PP_ENDMACRO:
2942 if (defining != NULL) {
2943 if (defining->type == EXP_MMACRO) {
2944 if (defining->def_depth > 0) {
2945 defining->def_depth --;
2946 return NO_DIRECTIVE_FOUND;
2948 } else {
2949 return NO_DIRECTIVE_FOUND;
2952 if (!(defining) || (defining->type != EXP_MMACRO)) {
2953 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2954 return DIRECTIVE_FOUND;
2956 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2957 defining->next = *edhead;
2958 *edhead = defining;
2959 ed = defining;
2960 defining = ed->prev;
2961 ed->prev = expansions;
2962 expansions = ed;
2963 ed = NULL;
2964 free_tlist(origline);
2965 return DIRECTIVE_FOUND;
2967 case PP_EXITMACRO:
2968 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2970 * We must search along istk->expansion until we hit a
2971 * macro invocation. Then we disable the emitting state(s)
2972 * between exitmacro and endmacro.
2974 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2975 if(ei->type == EXP_MMACRO) {
2976 break;
2980 if (ei != NULL) {
2982 * Set all invocations leading back to the macro
2983 * invocation to a non-emitting state.
2985 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2986 eei->emitting = false;
2988 eei->emitting = false;
2989 } else {
2990 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2992 free_tlist(origline);
2993 return DIRECTIVE_FOUND;
2995 case PP_UNMACRO:
2996 case PP_UNIMACRO:
2997 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2999 ExpDef **ed_p;
3000 ExpDef spec;
3002 spec.casesense = (i == PP_UNMACRO);
3003 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3004 return DIRECTIVE_FOUND;
3006 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3007 while (ed_p && *ed_p) {
3008 ed = *ed_p;
3009 if (ed->casesense == spec.casesense &&
3010 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3011 ed->nparam_min == spec.nparam_min &&
3012 ed->nparam_max == spec.nparam_max &&
3013 ed->plus == spec.plus) {
3014 if (ed->cur_depth > 0) {
3015 error(ERR_NONFATAL, "`%s' ignored on active macro",
3016 pp_directives[i]);
3017 break;
3018 } else {
3019 *ed_p = ed->next;
3020 free_expdef(ed);
3022 } else {
3023 ed_p = &ed->next;
3026 free_tlist(origline);
3027 free_tlist(spec.dlist);
3028 return DIRECTIVE_FOUND;
3031 case PP_ROTATE:
3032 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3033 if (tline->next && tline->next->type == TOK_WHITESPACE)
3034 tline = tline->next;
3035 if (!tline->next) {
3036 free_tlist(origline);
3037 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3038 return DIRECTIVE_FOUND;
3040 t = expand_smacro(tline->next);
3041 tline->next = NULL;
3042 free_tlist(origline);
3043 tline = t;
3044 tptr = &t;
3045 tokval.t_type = TOKEN_INVALID;
3046 evalresult =
3047 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3048 free_tlist(tline);
3049 if (!evalresult)
3050 return DIRECTIVE_FOUND;
3051 if (tokval.t_type)
3052 error(ERR_WARNING|ERR_PASS1,
3053 "trailing garbage after expression ignored");
3054 if (!is_simple(evalresult)) {
3055 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3056 return DIRECTIVE_FOUND;
3058 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3059 if (ei->type == EXP_MMACRO) {
3060 break;
3063 if (ei == NULL) {
3064 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3065 } else if (ei->nparam == 0) {
3066 error(ERR_NONFATAL,
3067 "`%%rotate' invoked within macro without parameters");
3068 } else {
3069 int rotate = ei->rotate + reloc_value(evalresult);
3071 rotate %= (int)ei->nparam;
3072 if (rotate < 0)
3073 rotate += ei->nparam;
3074 ei->rotate = rotate;
3076 return DIRECTIVE_FOUND;
3078 case PP_REP:
3079 if (defining != NULL) {
3080 if (defining->type == EXP_REP) {
3081 defining->def_depth ++;
3083 return NO_DIRECTIVE_FOUND;
3085 nolist = false;
3086 do {
3087 tline = tline->next;
3088 } while (tok_type_(tline, TOK_WHITESPACE));
3090 if (tok_type_(tline, TOK_ID) &&
3091 nasm_stricmp(tline->text, ".nolist") == 0) {
3092 nolist = true;
3093 do {
3094 tline = tline->next;
3095 } while (tok_type_(tline, TOK_WHITESPACE));
3098 if (tline) {
3099 t = expand_smacro(tline);
3100 tptr = &t;
3101 tokval.t_type = TOKEN_INVALID;
3102 evalresult =
3103 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3104 if (!evalresult) {
3105 free_tlist(origline);
3106 return DIRECTIVE_FOUND;
3108 if (tokval.t_type)
3109 error(ERR_WARNING|ERR_PASS1,
3110 "trailing garbage after expression ignored");
3111 if (!is_simple(evalresult)) {
3112 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3113 return DIRECTIVE_FOUND;
3115 count = reloc_value(evalresult);
3116 if (count >= REP_LIMIT) {
3117 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
3118 count = 0;
3119 } else
3120 count++;
3121 } else {
3122 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
3123 count = 0;
3125 free_tlist(origline);
3126 ed = new_ExpDef(EXP_REP);
3127 ed->nolist = nolist;
3128 ed->def_depth = 0;
3129 ed->cur_depth = 1;
3130 ed->max_depth = (count - 1);
3131 ed->ignoring = false;
3132 ed->prev = defining;
3133 defining = ed;
3134 return DIRECTIVE_FOUND;
3136 case PP_ENDREP:
3137 if (defining != NULL) {
3138 if (defining->type == EXP_REP) {
3139 if (defining->def_depth > 0) {
3140 defining->def_depth --;
3141 return NO_DIRECTIVE_FOUND;
3143 } else {
3144 return NO_DIRECTIVE_FOUND;
3147 if ((defining == NULL) || (defining->type != EXP_REP)) {
3148 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3149 return DIRECTIVE_FOUND;
3153 * Now we have a "macro" defined - although it has no name
3154 * and we won't be entering it in the hash tables - we must
3155 * push a macro-end marker for it on to istk->expansion.
3156 * After that, it will take care of propagating itself (a
3157 * macro-end marker line for a macro which is really a %rep
3158 * block will cause the macro to be re-expanded, complete
3159 * with another macro-end marker to ensure the process
3160 * continues) until the whole expansion is forcibly removed
3161 * from istk->expansion by a %exitrep.
3163 ed = defining;
3164 defining = ed->prev;
3165 ed->prev = expansions;
3166 expansions = ed;
3167 ei = new_ExpInv(EXP_REP, ed);
3168 ei->current = ed->line;
3169 ei->emitting = ((ed->max_depth > 0) ? true : false);
3170 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3171 ei->prev = istk->expansion;
3172 istk->expansion = ei;
3173 free_tlist(origline);
3174 return DIRECTIVE_FOUND;
3176 case PP_EXITREP:
3177 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3179 * We must search along istk->expansion until we hit a
3180 * rep invocation. Then we disable the emitting state(s)
3181 * between exitrep and endrep.
3183 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3184 if (ei->type == EXP_REP) {
3185 break;
3189 if (ei != NULL) {
3191 * Set all invocations leading back to the rep
3192 * invocation to a non-emitting state.
3194 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3195 eei->emitting = false;
3197 eei->emitting = false;
3198 eei->current = NULL;
3199 eei->def->cur_depth = eei->def->max_depth;
3200 } else {
3201 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
3206 case PP_XDEFINE:
3207 case PP_IXDEFINE:
3208 case PP_DEFINE:
3209 case PP_IDEFINE:
3210 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3211 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3213 tline = tline->next;
3214 skip_white_(tline);
3215 tline = expand_id(tline);
3216 if (!tline || (tline->type != TOK_ID &&
3217 (tline->type != TOK_PREPROC_ID ||
3218 tline->text[1] != '$'))) {
3219 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3220 pp_directives[i]);
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3225 ctx = get_ctx(tline->text, &mname, false);
3226 last = tline;
3227 param_start = tline = tline->next;
3228 nparam = 0;
3230 /* Expand the macro definition now for %xdefine and %ixdefine */
3231 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3232 tline = expand_smacro(tline);
3234 if (tok_is_(tline, "(")) {
3236 * This macro has parameters.
3239 tline = tline->next;
3240 while (1) {
3241 skip_white_(tline);
3242 if (!tline) {
3243 error(ERR_NONFATAL, "parameter identifier expected");
3244 free_tlist(origline);
3245 return DIRECTIVE_FOUND;
3247 if (tline->type != TOK_ID) {
3248 error(ERR_NONFATAL,
3249 "`%s': parameter identifier expected",
3250 tline->text);
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3254 tline->type = TOK_SMAC_PARAM + nparam++;
3255 tline = tline->next;
3256 skip_white_(tline);
3257 if (tok_is_(tline, ",")) {
3258 tline = tline->next;
3259 } else {
3260 if (!tok_is_(tline, ")")) {
3261 error(ERR_NONFATAL,
3262 "`)' expected to terminate macro template");
3263 free_tlist(origline);
3264 return DIRECTIVE_FOUND;
3266 break;
3269 last = tline;
3270 tline = tline->next;
3272 if (tok_type_(tline, TOK_WHITESPACE))
3273 last = tline, tline = tline->next;
3274 macro_start = NULL;
3275 last->next = NULL;
3276 t = tline;
3277 while (t) {
3278 if (t->type == TOK_ID) {
3279 list_for_each(tt, param_start)
3280 if (tt->type >= TOK_SMAC_PARAM &&
3281 !strcmp(tt->text, t->text))
3282 t->type = tt->type;
3284 tt = t->next;
3285 t->next = macro_start;
3286 macro_start = t;
3287 t = tt;
3290 * Good. We now have a macro name, a parameter count, and a
3291 * token list (in reverse order) for an expansion. We ought
3292 * to be OK just to create an SMacro, store it, and let
3293 * free_tlist have the rest of the line (which we have
3294 * carefully re-terminated after chopping off the expansion
3295 * from the end).
3297 define_smacro(ctx, mname, casesense, nparam, macro_start);
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3301 case PP_UNDEF:
3302 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3303 tline = tline->next;
3304 skip_white_(tline);
3305 tline = expand_id(tline);
3306 if (!tline || (tline->type != TOK_ID &&
3307 (tline->type != TOK_PREPROC_ID ||
3308 tline->text[1] != '$'))) {
3309 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3313 if (tline->next) {
3314 error(ERR_WARNING|ERR_PASS1,
3315 "trailing garbage after macro name ignored");
3318 /* Find the context that symbol belongs to */
3319 ctx = get_ctx(tline->text, &mname, false);
3320 undef_smacro(ctx, mname);
3321 free_tlist(origline);
3322 return DIRECTIVE_FOUND;
3324 case PP_DEFSTR:
3325 case PP_IDEFSTR:
3326 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3327 casesense = (i == PP_DEFSTR);
3329 tline = tline->next;
3330 skip_white_(tline);
3331 tline = expand_id(tline);
3332 if (!tline || (tline->type != TOK_ID &&
3333 (tline->type != TOK_PREPROC_ID ||
3334 tline->text[1] != '$'))) {
3335 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3336 pp_directives[i]);
3337 free_tlist(origline);
3338 return DIRECTIVE_FOUND;
3341 ctx = get_ctx(tline->text, &mname, false);
3342 last = tline;
3343 tline = expand_smacro(tline->next);
3344 last->next = NULL;
3346 while (tok_type_(tline, TOK_WHITESPACE))
3347 tline = delete_Token(tline);
3349 p = detoken(tline, false);
3350 macro_start = nasm_zalloc(sizeof(*macro_start));
3351 macro_start->text = nasm_quote(p, strlen(p));
3352 macro_start->type = TOK_STRING;
3353 nasm_free(p);
3356 * We now have a macro name, an implicit parameter count of
3357 * zero, and a string token to use as an expansion. Create
3358 * and store an SMacro.
3360 define_smacro(ctx, mname, casesense, 0, macro_start);
3361 free_tlist(origline);
3362 return DIRECTIVE_FOUND;
3364 case PP_DEFTOK:
3365 case PP_IDEFTOK:
3366 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3367 casesense = (i == PP_DEFTOK);
3369 tline = tline->next;
3370 skip_white_(tline);
3371 tline = expand_id(tline);
3372 if (!tline || (tline->type != TOK_ID &&
3373 (tline->type != TOK_PREPROC_ID ||
3374 tline->text[1] != '$'))) {
3375 error(ERR_NONFATAL,
3376 "`%s' expects a macro identifier as first parameter",
3377 pp_directives[i]);
3378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
3381 ctx = get_ctx(tline->text, &mname, false);
3382 last = tline;
3383 tline = expand_smacro(tline->next);
3384 last->next = NULL;
3386 t = tline;
3387 while (tok_type_(t, TOK_WHITESPACE))
3388 t = t->next;
3389 /* t should now point to the string */
3390 if (!tok_type_(t, TOK_STRING)) {
3391 error(ERR_NONFATAL,
3392 "`%s` requires string as second parameter",
3393 pp_directives[i]);
3394 free_tlist(tline);
3395 free_tlist(origline);
3396 return DIRECTIVE_FOUND;
3400 * Convert the string to a token stream. Note that smacros
3401 * are stored with the token stream reversed, so we have to
3402 * reverse the output of tokenize().
3404 nasm_unquote_cstr(t->text, i);
3405 macro_start = reverse_tokens(tokenize(t->text));
3408 * We now have a macro name, an implicit parameter count of
3409 * zero, and a numeric token to use as an expansion. Create
3410 * and store an SMacro.
3412 define_smacro(ctx, mname, casesense, 0, macro_start);
3413 free_tlist(tline);
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3417 case PP_PATHSEARCH:
3418 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3420 FILE *fp;
3421 StrList *xsl = NULL;
3422 StrList **xst = &xsl;
3424 casesense = true;
3426 tline = tline->next;
3427 skip_white_(tline);
3428 tline = expand_id(tline);
3429 if (!tline || (tline->type != TOK_ID &&
3430 (tline->type != TOK_PREPROC_ID ||
3431 tline->text[1] != '$'))) {
3432 error(ERR_NONFATAL,
3433 "`%%pathsearch' expects a macro identifier as first parameter");
3434 free_tlist(origline);
3435 return DIRECTIVE_FOUND;
3437 ctx = get_ctx(tline->text, &mname, false);
3438 last = tline;
3439 tline = expand_smacro(tline->next);
3440 last->next = NULL;
3442 t = tline;
3443 while (tok_type_(t, TOK_WHITESPACE))
3444 t = t->next;
3446 if (!t || (t->type != TOK_STRING &&
3447 t->type != TOK_INTERNAL_STRING)) {
3448 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3449 free_tlist(tline);
3450 free_tlist(origline);
3451 return DIRECTIVE_FOUND; /* but we did _something_ */
3453 if (t->next)
3454 error(ERR_WARNING|ERR_PASS1,
3455 "trailing garbage after `%%pathsearch' ignored");
3456 p = t->text;
3457 if (t->type != TOK_INTERNAL_STRING)
3458 nasm_unquote(p, NULL);
3460 fp = inc_fopen(p, &xsl, &xst, true);
3461 if (fp) {
3462 p = xsl->str;
3463 fclose(fp); /* Don't actually care about the file */
3465 macro_start = nasm_zalloc(sizeof(*macro_start));
3466 macro_start->text = nasm_quote(p, strlen(p));
3467 macro_start->type = TOK_STRING;
3468 if (xsl)
3469 nasm_free(xsl);
3472 * We now have a macro name, an implicit parameter count of
3473 * zero, and a string token to use as an expansion. Create
3474 * and store an SMacro.
3476 define_smacro(ctx, mname, casesense, 0, macro_start);
3477 free_tlist(tline);
3478 free_tlist(origline);
3479 return DIRECTIVE_FOUND;
3482 case PP_STRLEN:
3483 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3484 casesense = true;
3486 tline = tline->next;
3487 skip_white_(tline);
3488 tline = expand_id(tline);
3489 if (!tline || (tline->type != TOK_ID &&
3490 (tline->type != TOK_PREPROC_ID ||
3491 tline->text[1] != '$'))) {
3492 error(ERR_NONFATAL,
3493 "`%%strlen' expects a macro identifier as first parameter");
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3497 ctx = get_ctx(tline->text, &mname, false);
3498 last = tline;
3499 tline = expand_smacro(tline->next);
3500 last->next = NULL;
3502 t = tline;
3503 while (tok_type_(t, TOK_WHITESPACE))
3504 t = t->next;
3505 /* t should now point to the string */
3506 if (!tok_type_(t, TOK_STRING)) {
3507 error(ERR_NONFATAL,
3508 "`%%strlen` requires string as second parameter");
3509 free_tlist(tline);
3510 free_tlist(origline);
3511 return DIRECTIVE_FOUND;
3514 macro_start = nasm_zalloc(sizeof(*macro_start));
3515 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3518 * We now have a macro name, an implicit parameter count of
3519 * zero, and a numeric token to use as an expansion. Create
3520 * and store an SMacro.
3522 define_smacro(ctx, mname, casesense, 0, macro_start);
3523 free_tlist(tline);
3524 free_tlist(origline);
3525 return DIRECTIVE_FOUND;
3527 case PP_STRCAT:
3528 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3529 casesense = true;
3531 tline = tline->next;
3532 skip_white_(tline);
3533 tline = expand_id(tline);
3534 if (!tline || (tline->type != TOK_ID &&
3535 (tline->type != TOK_PREPROC_ID ||
3536 tline->text[1] != '$'))) {
3537 error(ERR_NONFATAL,
3538 "`%%strcat' expects a macro identifier as first parameter");
3539 free_tlist(origline);
3540 return DIRECTIVE_FOUND;
3542 ctx = get_ctx(tline->text, &mname, false);
3543 last = tline;
3544 tline = expand_smacro(tline->next);
3545 last->next = NULL;
3547 len = 0;
3548 list_for_each(t, tline) {
3549 switch (t->type) {
3550 case TOK_WHITESPACE:
3551 break;
3552 case TOK_STRING:
3553 len += t->a.len = nasm_unquote(t->text, NULL);
3554 break;
3555 case TOK_OTHER:
3556 if (!strcmp(t->text, ",")) /* permit comma separators */
3557 break;
3558 /* else fall through */
3559 default:
3560 error(ERR_NONFATAL,
3561 "non-string passed to `%%strcat' (%d)", t->type);
3562 free_tlist(tline);
3563 free_tlist(origline);
3564 return DIRECTIVE_FOUND;
3568 p = pp = nasm_malloc(len);
3569 list_for_each(t, tline) {
3570 if (t->type == TOK_STRING) {
3571 memcpy(p, t->text, t->a.len);
3572 p += t->a.len;
3577 * We now have a macro name, an implicit parameter count of
3578 * zero, and a numeric token to use as an expansion. Create
3579 * and store an SMacro.
3581 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3582 macro_start->text = nasm_quote(pp, len);
3583 nasm_free(pp);
3584 define_smacro(ctx, mname, casesense, 0, macro_start);
3585 free_tlist(tline);
3586 free_tlist(origline);
3587 return DIRECTIVE_FOUND;
3589 case PP_SUBSTR:
3590 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3592 int64_t start, count;
3593 size_t len;
3595 casesense = true;
3597 tline = tline->next;
3598 skip_white_(tline);
3599 tline = expand_id(tline);
3600 if (!tline || (tline->type != TOK_ID &&
3601 (tline->type != TOK_PREPROC_ID ||
3602 tline->text[1] != '$'))) {
3603 error(ERR_NONFATAL,
3604 "`%%substr' expects a macro identifier as first parameter");
3605 free_tlist(origline);
3606 return DIRECTIVE_FOUND;
3608 ctx = get_ctx(tline->text, &mname, false);
3609 last = tline;
3610 tline = expand_smacro(tline->next);
3611 last->next = NULL;
3613 if (tline) /* skip expanded id */
3614 t = tline->next;
3615 while (tok_type_(t, TOK_WHITESPACE))
3616 t = t->next;
3618 /* t should now point to the string */
3619 if (!tok_type_(t, TOK_STRING)) {
3620 error(ERR_NONFATAL,
3621 "`%%substr` requires string as second parameter");
3622 free_tlist(tline);
3623 free_tlist(origline);
3624 return DIRECTIVE_FOUND;
3627 tt = t->next;
3628 tptr = &tt;
3629 tokval.t_type = TOKEN_INVALID;
3630 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3631 pass, error, NULL);
3632 if (!evalresult) {
3633 free_tlist(tline);
3634 free_tlist(origline);
3635 return DIRECTIVE_FOUND;
3636 } else if (!is_simple(evalresult)) {
3637 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3638 free_tlist(tline);
3639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3642 start = evalresult->value - 1;
3644 while (tok_type_(tt, TOK_WHITESPACE))
3645 tt = tt->next;
3646 if (!tt) {
3647 count = 1; /* Backwards compatibility: one character */
3648 } else {
3649 tokval.t_type = TOKEN_INVALID;
3650 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3651 pass, error, NULL);
3652 if (!evalresult) {
3653 free_tlist(tline);
3654 free_tlist(origline);
3655 return DIRECTIVE_FOUND;
3656 } else if (!is_simple(evalresult)) {
3657 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3662 count = evalresult->value;
3665 len = nasm_unquote(t->text, NULL);
3666 /* make start and count being in range */
3667 if (start < 0)
3668 start = 0;
3669 if (count < 0)
3670 count = len + count + 1 - start;
3671 if (start + count > (int64_t)len)
3672 count = len - start;
3673 if (!len || count < 0 || start >=(int64_t)len)
3674 start = -1, count = 0; /* empty string */
3676 macro_start = nasm_zalloc(sizeof(*macro_start));
3677 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3678 macro_start->type = TOK_STRING;
3681 * We now have a macro name, an implicit parameter count of
3682 * zero, and a numeric token to use as an expansion. Create
3683 * and store an SMacro.
3685 define_smacro(ctx, mname, casesense, 0, macro_start);
3686 free_tlist(tline);
3687 free_tlist(origline);
3688 return DIRECTIVE_FOUND;
3691 case PP_ASSIGN:
3692 case PP_IASSIGN:
3693 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3694 casesense = (i == PP_ASSIGN);
3696 tline = tline->next;
3697 skip_white_(tline);
3698 tline = expand_id(tline);
3699 if (!tline || (tline->type != TOK_ID &&
3700 (tline->type != TOK_PREPROC_ID ||
3701 tline->text[1] != '$'))) {
3702 error(ERR_NONFATAL,
3703 "`%%%sassign' expects a macro identifier",
3704 (i == PP_IASSIGN ? "i" : ""));
3705 free_tlist(origline);
3706 return DIRECTIVE_FOUND;
3708 ctx = get_ctx(tline->text, &mname, false);
3709 last = tline;
3710 tline = expand_smacro(tline->next);
3711 last->next = NULL;
3713 t = tline;
3714 tptr = &t;
3715 tokval.t_type = TOKEN_INVALID;
3716 evalresult =
3717 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3718 free_tlist(tline);
3719 if (!evalresult) {
3720 free_tlist(origline);
3721 return DIRECTIVE_FOUND;
3724 if (tokval.t_type)
3725 error(ERR_WARNING|ERR_PASS1,
3726 "trailing garbage after expression ignored");
3728 if (!is_simple(evalresult)) {
3729 error(ERR_NONFATAL,
3730 "non-constant value given to `%%%sassign'",
3731 (i == PP_IASSIGN ? "i" : ""));
3732 free_tlist(origline);
3733 return DIRECTIVE_FOUND;
3736 macro_start = nasm_zalloc(sizeof(*macro_start));
3737 make_tok_num(macro_start, reloc_value(evalresult));
3740 * We now have a macro name, an implicit parameter count of
3741 * zero, and a numeric token to use as an expansion. Create
3742 * and store an SMacro.
3744 define_smacro(ctx, mname, casesense, 0, macro_start);
3745 free_tlist(origline);
3746 return DIRECTIVE_FOUND;
3748 case PP_LINE:
3749 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3751 * Syntax is `%line nnn[+mmm] [filename]'
3753 tline = tline->next;
3754 skip_white_(tline);
3755 if (!tok_type_(tline, TOK_NUMBER)) {
3756 error(ERR_NONFATAL, "`%%line' expects line number");
3757 free_tlist(origline);
3758 return DIRECTIVE_FOUND;
3760 k = readnum(tline->text, &err);
3761 m = 1;
3762 tline = tline->next;
3763 if (tok_is_(tline, "+")) {
3764 tline = tline->next;
3765 if (!tok_type_(tline, TOK_NUMBER)) {
3766 error(ERR_NONFATAL, "`%%line' expects line increment");
3767 free_tlist(origline);
3768 return DIRECTIVE_FOUND;
3770 m = readnum(tline->text, &err);
3771 tline = tline->next;
3773 skip_white_(tline);
3774 src_set_linnum(k);
3775 istk->lineinc = m;
3776 if (tline) {
3777 nasm_free(src_set_fname(detoken(tline, false)));
3779 free_tlist(origline);
3780 return DIRECTIVE_FOUND;
3782 case PP_WHILE:
3783 if (defining != NULL) {
3784 if (defining->type == EXP_WHILE) {
3785 defining->def_depth ++;
3787 return NO_DIRECTIVE_FOUND;
3789 l = NULL;
3790 if ((istk->expansion != NULL) &&
3791 (istk->expansion->emitting == false)) {
3792 j = COND_NEVER;
3793 } else {
3794 l = new_Line();
3795 l->first = copy_Token(tline->next);
3796 j = if_condition(tline->next, i);
3797 tline->next = NULL; /* it got freed */
3798 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3800 ed = new_ExpDef(EXP_WHILE);
3801 ed->state = j;
3802 ed->cur_depth = 1;
3803 ed->max_depth = DEADMAN_LIMIT;
3804 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3805 if (ed->ignoring == false) {
3806 ed->line = l;
3807 ed->last = l;
3808 } else if (l != NULL) {
3809 delete_Token(l->first);
3810 nasm_free(l);
3811 l = NULL;
3813 ed->prev = defining;
3814 defining = ed;
3815 free_tlist(origline);
3816 return DIRECTIVE_FOUND;
3818 case PP_ENDWHILE:
3819 if (defining != NULL) {
3820 if (defining->type == EXP_WHILE) {
3821 if (defining->def_depth > 0) {
3822 defining->def_depth --;
3823 return NO_DIRECTIVE_FOUND;
3825 } else {
3826 return NO_DIRECTIVE_FOUND;
3829 if (tline->next != NULL) {
3830 error_precond(ERR_WARNING|ERR_PASS1,
3831 "trailing garbage after `%%endwhile' ignored");
3833 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3834 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3835 return DIRECTIVE_FOUND;
3837 ed = defining;
3838 defining = ed->prev;
3839 if (ed->ignoring == false) {
3840 ed->prev = expansions;
3841 expansions = ed;
3842 ei = new_ExpInv(EXP_WHILE, ed);
3843 ei->current = ed->line->next;
3844 ei->emitting = true;
3845 ei->prev = istk->expansion;
3846 istk->expansion = ei;
3847 } else {
3848 nasm_free(ed);
3850 free_tlist(origline);
3851 return DIRECTIVE_FOUND;
3853 case PP_EXITWHILE:
3854 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3856 * We must search along istk->expansion until we hit a
3857 * while invocation. Then we disable the emitting state(s)
3858 * between exitwhile and endwhile.
3860 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3861 if (ei->type == EXP_WHILE) {
3862 break;
3866 if (ei != NULL) {
3868 * Set all invocations leading back to the while
3869 * invocation to a non-emitting state.
3871 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3872 eei->emitting = false;
3874 eei->emitting = false;
3875 eei->current = NULL;
3876 eei->def->cur_depth = eei->def->max_depth;
3877 } else {
3878 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3880 free_tlist(origline);
3881 return DIRECTIVE_FOUND;
3883 case PP_COMMENT:
3884 if (defining != NULL) {
3885 if (defining->type == EXP_COMMENT) {
3886 defining->def_depth ++;
3888 return NO_DIRECTIVE_FOUND;
3890 ed = new_ExpDef(EXP_COMMENT);
3891 ed->ignoring = true;
3892 ed->prev = defining;
3893 defining = ed;
3894 free_tlist(origline);
3895 return DIRECTIVE_FOUND;
3897 case PP_ENDCOMMENT:
3898 if (defining != NULL) {
3899 if (defining->type == EXP_COMMENT) {
3900 if (defining->def_depth > 0) {
3901 defining->def_depth --;
3902 return NO_DIRECTIVE_FOUND;
3904 } else {
3905 return NO_DIRECTIVE_FOUND;
3908 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3909 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3910 return DIRECTIVE_FOUND;
3912 ed = defining;
3913 defining = ed->prev;
3914 nasm_free(ed);
3915 free_tlist(origline);
3916 return DIRECTIVE_FOUND;
3918 case PP_FINAL:
3919 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3920 if (in_final != false) {
3921 error(ERR_FATAL, "`%%final' cannot be used recursively");
3923 tline = tline->next;
3924 skip_white_(tline);
3925 if (tline == NULL) {
3926 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3927 } else {
3928 l = new_Line();
3929 l->first = copy_Token(tline);
3930 l->next = finals;
3931 finals = l;
3933 free_tlist(origline);
3934 return DIRECTIVE_FOUND;
3936 case PP_PRAGMA:
3937 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3938 tline = tline->next;
3939 skip_white_(tline);
3940 if (tline == NULL) {
3941 error(ERR_NONFATAL, "`%%pragma' expects at least one parameter");
3942 } else {
3943 do_pragma(tline);
3945 free_tlist(origline);
3946 return DIRECTIVE_FOUND;
3948 default:
3949 error(ERR_FATAL,
3950 "preprocessor directive `%s' not yet implemented",
3951 pp_directives[i]);
3952 return DIRECTIVE_FOUND;
3957 * Ensure that a macro parameter contains a condition code and
3958 * nothing else. Return the condition code index if so, or -1
3959 * otherwise.
3961 static int find_cc(Token * t)
3963 Token *tt;
3964 int i, j, k, m;
3966 if (!t)
3967 return -1; /* Probably a %+ without a space */
3969 skip_white_(t);
3970 if (t->type != TOK_ID)
3971 return -1;
3972 tt = t->next;
3973 skip_white_(tt);
3974 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3975 return -1;
3977 i = -1;
3978 j = ARRAY_SIZE(conditions);
3979 while (j - i > 1) {
3980 k = (j + i) / 2;
3981 m = nasm_stricmp(t->text, conditions[k]);
3982 if (m == 0) {
3983 i = k;
3984 j = -2;
3985 break;
3986 } else if (m < 0) {
3987 j = k;
3988 } else
3989 i = k;
3991 if (j != -2)
3992 return -1;
3993 return i;
3996 static bool paste_tokens(Token **head, const struct tokseq_match *m,
3997 int mnum, bool handle_paste_tokens)
3999 Token **tail, *t, *tt;
4000 Token **paste_head;
4001 bool did_paste = false;
4002 char *tmp;
4003 int i;
4005 /* Now handle token pasting... */
4006 paste_head = NULL;
4007 tail = head;
4008 while ((t = *tail) && (tt = t->next)) {
4009 switch (t->type) {
4010 case TOK_WHITESPACE:
4011 if (tt->type == TOK_WHITESPACE) {
4012 /* Zap adjacent whitespace tokens */
4013 t->next = delete_Token(tt);
4014 } else {
4015 /* Do not advance paste_head here */
4016 tail = &t->next;
4018 break;
4019 case TOK_PASTE: /* %+ */
4020 if (handle_paste_tokens) {
4021 /* Zap %+ and whitespace tokens to the right */
4022 while (t && (t->type == TOK_WHITESPACE ||
4023 t->type == TOK_PASTE))
4024 t = *tail = delete_Token(t);
4025 if (!paste_head || !t)
4026 break; /* Nothing to paste with */
4027 tail = paste_head;
4028 t = *tail;
4029 tt = t->next;
4030 while (tok_type_(tt, TOK_WHITESPACE))
4031 tt = t->next = delete_Token(tt);
4032 if (tt) {
4033 tmp = nasm_strcat(t->text, tt->text);
4034 delete_Token(t);
4035 tt = delete_Token(tt);
4036 t = *tail = tokenize(tmp);
4037 nasm_free(tmp);
4038 while (t->next) {
4039 tail = &t->next;
4040 t = t->next;
4042 t->next = tt; /* Attach the remaining token chain */
4043 did_paste = true;
4045 paste_head = tail;
4046 tail = &t->next;
4047 break;
4049 /* else fall through */
4050 default:
4052 * Concatenation of tokens might look nontrivial
4053 * but in real it's pretty simple -- the caller
4054 * prepares the masks of token types to be concatenated
4055 * and we simply find matched sequences and slip
4056 * them together
4058 for (i = 0; i < mnum; i++) {
4059 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4060 size_t len = 0;
4061 char *tmp, *p;
4063 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
4064 len += strlen(tt->text);
4065 tt = tt->next;
4069 * Now tt points to the first token after
4070 * the potential paste area...
4072 if (tt != t->next) {
4073 /* We have at least two tokens... */
4074 len += strlen(t->text);
4075 p = tmp = nasm_malloc(len+1);
4076 while (t != tt) {
4077 strcpy(p, t->text);
4078 p = strchr(p, '\0');
4079 t = delete_Token(t);
4081 t = *tail = tokenize(tmp);
4082 nasm_free(tmp);
4083 while (t->next) {
4084 tail = &t->next;
4085 t = t->next;
4087 t->next = tt; /* Attach the remaining token chain */
4088 did_paste = true;
4090 paste_head = tail;
4091 tail = &t->next;
4092 break;
4095 if (i >= mnum) { /* no match */
4096 tail = &t->next;
4097 if (!tok_type_(t->next, TOK_WHITESPACE))
4098 paste_head = tail;
4100 break;
4103 return did_paste;
4107 * expands to a list of tokens from %{x:y}
4109 static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
4111 Token *t = tline, **tt, *tm, *head;
4112 char *pos;
4113 int fst, lst, j, i;
4115 pos = strchr(tline->text, ':');
4116 nasm_assert(pos);
4118 lst = atoi(pos + 1);
4119 fst = atoi(tline->text + 1);
4122 * only macros params are accounted so
4123 * if someone passes %0 -- we reject such
4124 * value(s)
4126 if (lst == 0 || fst == 0)
4127 goto err;
4129 /* the values should be sane */
4130 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4131 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
4132 goto err;
4134 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4135 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
4137 /* counted from zero */
4138 fst--, lst--;
4141 * it will be at least one token
4143 tm = ei->params[(fst + ei->rotate) % ei->nparam];
4144 t = new_Token(NULL, tm->type, tm->text, 0);
4145 head = t, tt = &t->next;
4146 if (fst < lst) {
4147 for (i = fst + 1; i <= lst; i++) {
4148 t = new_Token(NULL, TOK_OTHER, ",", 0);
4149 *tt = t, tt = &t->next;
4150 j = (i + ei->rotate) % ei->nparam;
4151 tm = ei->params[j];
4152 t = new_Token(NULL, tm->type, tm->text, 0);
4153 *tt = t, tt = &t->next;
4155 } else {
4156 for (i = fst - 1; i >= lst; i--) {
4157 t = new_Token(NULL, TOK_OTHER, ",", 0);
4158 *tt = t, tt = &t->next;
4159 j = (i + ei->rotate) % ei->nparam;
4160 tm = ei->params[j];
4161 t = new_Token(NULL, tm->type, tm->text, 0);
4162 *tt = t, tt = &t->next;
4166 *last = tt;
4167 return head;
4169 err:
4170 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4171 &tline->text[1]);
4172 return tline;
4176 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4177 * %-n) and MMacro-local identifiers (%%foo) as well as
4178 * macro indirection (%[...]) and range (%{..:..}).
4180 static Token *expand_mmac_params(Token * tline)
4182 Token *t, *tt, **tail, *thead;
4183 bool changed = false;
4184 char *pos;
4186 tail = &thead;
4187 thead = NULL;
4189 while (tline) {
4190 if (tline->type == TOK_PREPROC_ID &&
4191 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4192 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4193 tline->text[1] == '%')) {
4194 char *text = NULL;
4195 int type = 0, cc; /* type = 0 to placate optimisers */
4196 char tmpbuf[30];
4197 unsigned int n;
4198 int i;
4199 ExpInv *ei;
4201 t = tline;
4202 tline = tline->next;
4204 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4205 if (ei->type == EXP_MMACRO) {
4206 break;
4209 if (ei == NULL) {
4210 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
4211 } else {
4212 pos = strchr(t->text, ':');
4213 if (!pos) {
4214 switch (t->text[1]) {
4216 * We have to make a substitution of one of the
4217 * forms %1, %-1, %+1, %%foo, %0.
4219 case '0':
4220 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4221 type = TOK_ID;
4222 text = nasm_strdup(ei->label_text);
4223 } else {
4224 type = TOK_NUMBER;
4225 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4226 text = nasm_strdup(tmpbuf);
4228 break;
4229 case '%':
4230 type = TOK_ID;
4231 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
4232 ei->unique);
4233 text = nasm_strcat(tmpbuf, t->text + 2);
4234 break;
4235 case '-':
4236 n = atoi(t->text + 2) - 1;
4237 if (n >= ei->nparam)
4238 tt = NULL;
4239 else {
4240 if (ei->nparam > 1)
4241 n = (n + ei->rotate) % ei->nparam;
4242 tt = ei->params[n];
4244 cc = find_cc(tt);
4245 if (cc == -1) {
4246 error(ERR_NONFATAL,
4247 "macro parameter %d is not a condition code",
4248 n + 1);
4249 text = NULL;
4250 } else {
4251 type = TOK_ID;
4252 if (inverse_ccs[cc] == -1) {
4253 error(ERR_NONFATAL,
4254 "condition code `%s' is not invertible",
4255 conditions[cc]);
4256 text = NULL;
4257 } else
4258 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4260 break;
4261 case '+':
4262 n = atoi(t->text + 2) - 1;
4263 if (n >= ei->nparam)
4264 tt = NULL;
4265 else {
4266 if (ei->nparam > 1)
4267 n = (n + ei->rotate) % ei->nparam;
4268 tt = ei->params[n];
4270 cc = find_cc(tt);
4271 if (cc == -1) {
4272 error(ERR_NONFATAL,
4273 "macro parameter %d is not a condition code",
4274 n + 1);
4275 text = NULL;
4276 } else {
4277 type = TOK_ID;
4278 text = nasm_strdup(conditions[cc]);
4280 break;
4281 default:
4282 n = atoi(t->text + 1) - 1;
4283 if (n >= ei->nparam)
4284 tt = NULL;
4285 else {
4286 if (ei->nparam > 1)
4287 n = (n + ei->rotate) % ei->nparam;
4288 tt = ei->params[n];
4290 if (tt) {
4291 for (i = 0; i < ei->paramlen[n]; i++) {
4292 *tail = new_Token(NULL, tt->type, tt->text, 0);
4293 tail = &(*tail)->next;
4294 tt = tt->next;
4297 text = NULL; /* we've done it here */
4298 break;
4300 } else {
4302 * seems we have a parameters range here
4304 Token *head, **last;
4305 head = expand_mmac_params_range(ei, t, &last);
4306 if (head != t) {
4307 *tail = head;
4308 *last = tline;
4309 tline = head;
4310 text = NULL;
4314 if (!text) {
4315 delete_Token(t);
4316 } else {
4317 *tail = t;
4318 tail = &t->next;
4319 t->type = type;
4320 nasm_free(t->text);
4321 t->text = text;
4322 t->a.mac = NULL;
4324 changed = true;
4325 continue;
4326 } else if (tline->type == TOK_INDIRECT) {
4327 t = tline;
4328 tline = tline->next;
4329 tt = tokenize(t->text);
4330 tt = expand_mmac_params(tt);
4331 tt = expand_smacro(tt);
4332 *tail = tt;
4333 while (tt) {
4334 tt->a.mac = NULL; /* Necessary? */
4335 tail = &tt->next;
4336 tt = tt->next;
4338 delete_Token(t);
4339 changed = true;
4340 } else {
4341 t = *tail = tline;
4342 tline = tline->next;
4343 t->a.mac = NULL;
4344 tail = &t->next;
4347 *tail = NULL;
4349 if (changed) {
4350 const struct tokseq_match t[] = {
4352 PP_CONCAT_MASK(TOK_ID) |
4353 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4354 PP_CONCAT_MASK(TOK_ID) |
4355 PP_CONCAT_MASK(TOK_NUMBER) |
4356 PP_CONCAT_MASK(TOK_FLOAT) |
4357 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4360 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4361 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4364 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4367 return thead;
4371 * Expand all single-line macro calls made in the given line.
4372 * Return the expanded version of the line. The original is deemed
4373 * to be destroyed in the process. (In reality we'll just move
4374 * Tokens from input to output a lot of the time, rather than
4375 * actually bothering to destroy and replicate.)
4378 static Token *expand_smacro(Token * tline)
4380 Token *t, *tt, *mstart, **tail, *thead;
4381 SMacro *head = NULL, *m;
4382 Token **params;
4383 int *paramsize;
4384 unsigned int nparam, sparam;
4385 int brackets;
4386 Token *org_tline = tline;
4387 Context *ctx;
4388 const char *mname;
4389 int deadman = DEADMAN_LIMIT;
4390 bool expanded;
4393 * Trick: we should avoid changing the start token pointer since it can
4394 * be contained in "next" field of other token. Because of this
4395 * we allocate a copy of first token and work with it; at the end of
4396 * routine we copy it back
4398 if (org_tline) {
4399 tline = new_Token(org_tline->next, org_tline->type,
4400 org_tline->text, 0);
4401 tline->a.mac = org_tline->a.mac;
4402 nasm_free(org_tline->text);
4403 org_tline->text = NULL;
4406 expanded = true; /* Always expand %+ at least once */
4408 again:
4409 thead = NULL;
4410 tail = &thead;
4412 while (tline) { /* main token loop */
4413 if (!--deadman) {
4414 error(ERR_NONFATAL, "interminable macro recursion");
4415 goto err;
4418 if ((mname = tline->text)) {
4419 /* if this token is a local macro, look in local context */
4420 if (tline->type == TOK_ID) {
4421 head = (SMacro *)hash_findix(&smacros, mname);
4422 } else if (tline->type == TOK_PREPROC_ID) {
4423 ctx = get_ctx(mname, &mname, false);
4424 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4425 } else
4426 head = NULL;
4429 * We've hit an identifier. As in is_mmacro below, we first
4430 * check whether the identifier is a single-line macro at
4431 * all, then think about checking for parameters if
4432 * necessary.
4434 list_for_each(m, head)
4435 if (!mstrcmp(m->name, mname, m->casesense))
4436 break;
4437 if (m) {
4438 mstart = tline;
4439 params = NULL;
4440 paramsize = NULL;
4441 if (m->nparam == 0) {
4443 * Simple case: the macro is parameterless. Discard the
4444 * one token that the macro call took, and push the
4445 * expansion back on the to-do stack.
4447 if (!m->expansion) {
4448 if (!strcmp("__FILE__", m->name)) {
4449 int32_t num = 0;
4450 char *file = NULL;
4451 src_get(&num, &file);
4452 tline->text = nasm_quote(file, strlen(file));
4453 tline->type = TOK_STRING;
4454 nasm_free(file);
4455 continue;
4457 if (!strcmp("__LINE__", m->name)) {
4458 nasm_free(tline->text);
4459 make_tok_num(tline, src_get_linnum());
4460 continue;
4462 if (!strcmp("__BITS__", m->name)) {
4463 nasm_free(tline->text);
4464 make_tok_num(tline, globalbits);
4465 continue;
4467 tline = delete_Token(tline);
4468 continue;
4470 } else {
4472 * Complicated case: at least one macro with this name
4473 * exists and takes parameters. We must find the
4474 * parameters in the call, count them, find the SMacro
4475 * that corresponds to that form of the macro call, and
4476 * substitute for the parameters when we expand. What a
4477 * pain.
4479 /*tline = tline->next;
4480 skip_white_(tline); */
4481 do {
4482 t = tline->next;
4483 while (tok_type_(t, TOK_SMAC_END)) {
4484 t->a.mac->in_progress = false;
4485 t->text = NULL;
4486 t = tline->next = delete_Token(t);
4488 tline = t;
4489 } while (tok_type_(tline, TOK_WHITESPACE));
4490 if (!tok_is_(tline, "(")) {
4492 * This macro wasn't called with parameters: ignore
4493 * the call. (Behaviour borrowed from gnu cpp.)
4495 tline = mstart;
4496 m = NULL;
4497 } else {
4498 int paren = 0;
4499 int white = 0;
4500 brackets = 0;
4501 nparam = 0;
4502 sparam = PARAM_DELTA;
4503 params = nasm_malloc(sparam * sizeof(Token *));
4504 params[0] = tline->next;
4505 paramsize = nasm_malloc(sparam * sizeof(int));
4506 paramsize[0] = 0;
4507 while (true) { /* parameter loop */
4509 * For some unusual expansions
4510 * which concatenates function call
4512 t = tline->next;
4513 while (tok_type_(t, TOK_SMAC_END)) {
4514 t->a.mac->in_progress = false;
4515 t->text = NULL;
4516 t = tline->next = delete_Token(t);
4518 tline = t;
4520 if (!tline) {
4521 error(ERR_NONFATAL,
4522 "macro call expects terminating `)'");
4523 break;
4525 if (tline->type == TOK_WHITESPACE
4526 && brackets <= 0) {
4527 if (paramsize[nparam])
4528 white++;
4529 else
4530 params[nparam] = tline->next;
4531 continue; /* parameter loop */
4533 if (tline->type == TOK_OTHER
4534 && tline->text[1] == 0) {
4535 char ch = tline->text[0];
4536 if (ch == ',' && !paren && brackets <= 0) {
4537 if (++nparam >= sparam) {
4538 sparam += PARAM_DELTA;
4539 params = nasm_realloc(params,
4540 sparam * sizeof(Token *));
4541 paramsize = nasm_realloc(paramsize,
4542 sparam * sizeof(int));
4544 params[nparam] = tline->next;
4545 paramsize[nparam] = 0;
4546 white = 0;
4547 continue; /* parameter loop */
4549 if (ch == '{' &&
4550 (brackets > 0 || (brackets == 0 &&
4551 !paramsize[nparam])))
4553 if (!(brackets++)) {
4554 params[nparam] = tline->next;
4555 continue; /* parameter loop */
4558 if (ch == '}' && brackets > 0)
4559 if (--brackets == 0) {
4560 brackets = -1;
4561 continue; /* parameter loop */
4563 if (ch == '(' && !brackets)
4564 paren++;
4565 if (ch == ')' && brackets <= 0)
4566 if (--paren < 0)
4567 break;
4569 if (brackets < 0) {
4570 brackets = 0;
4571 error(ERR_NONFATAL, "braces do not "
4572 "enclose all of macro parameter");
4574 paramsize[nparam] += white + 1;
4575 white = 0;
4576 } /* parameter loop */
4577 nparam++;
4578 while (m && (m->nparam != nparam ||
4579 mstrcmp(m->name, mname,
4580 m->casesense)))
4581 m = m->next;
4582 if (!m)
4583 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4584 "macro `%s' exists, "
4585 "but not taking %d parameters",
4586 mstart->text, nparam);
4589 if (m && m->in_progress)
4590 m = NULL;
4591 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4593 * Design question: should we handle !tline, which
4594 * indicates missing ')' here, or expand those
4595 * macros anyway, which requires the (t) test a few
4596 * lines down?
4598 nasm_free(params);
4599 nasm_free(paramsize);
4600 tline = mstart;
4601 } else {
4603 * Expand the macro: we are placed on the last token of the
4604 * call, so that we can easily split the call from the
4605 * following tokens. We also start by pushing an SMAC_END
4606 * token for the cycle removal.
4608 t = tline;
4609 if (t) {
4610 tline = t->next;
4611 t->next = NULL;
4613 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4614 tt->a.mac = m;
4615 m->in_progress = true;
4616 tline = tt;
4617 list_for_each(t, m->expansion) {
4618 if (t->type >= TOK_SMAC_PARAM) {
4619 Token *pcopy = tline, **ptail = &pcopy;
4620 Token *ttt, *pt;
4621 int i;
4623 ttt = params[t->type - TOK_SMAC_PARAM];
4624 i = paramsize[t->type - TOK_SMAC_PARAM];
4625 while (--i >= 0) {
4626 pt = *ptail = new_Token(tline, ttt->type,
4627 ttt->text, 0);
4628 ptail = &pt->next;
4629 ttt = ttt->next;
4631 tline = pcopy;
4632 } else if (t->type == TOK_PREPROC_Q) {
4633 tt = new_Token(tline, TOK_ID, mname, 0);
4634 tline = tt;
4635 } else if (t->type == TOK_PREPROC_QQ) {
4636 tt = new_Token(tline, TOK_ID, m->name, 0);
4637 tline = tt;
4638 } else {
4639 tt = new_Token(tline, t->type, t->text, 0);
4640 tline = tt;
4645 * Having done that, get rid of the macro call, and clean
4646 * up the parameters.
4648 nasm_free(params);
4649 nasm_free(paramsize);
4650 free_tlist(mstart);
4651 expanded = true;
4652 continue; /* main token loop */
4657 if (tline->type == TOK_SMAC_END) {
4658 tline->a.mac->in_progress = false;
4659 tline = delete_Token(tline);
4660 } else {
4661 t = *tail = tline;
4662 tline = tline->next;
4663 t->a.mac = NULL;
4664 t->next = NULL;
4665 tail = &t->next;
4670 * Now scan the entire line and look for successive TOK_IDs that resulted
4671 * after expansion (they can't be produced by tokenize()). The successive
4672 * TOK_IDs should be concatenated.
4673 * Also we look for %+ tokens and concatenate the tokens before and after
4674 * them (without white spaces in between).
4676 if (expanded) {
4677 const struct tokseq_match t[] = {
4679 PP_CONCAT_MASK(TOK_ID) |
4680 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4681 PP_CONCAT_MASK(TOK_ID) |
4682 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4683 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4686 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4688 * If we concatenated something, *and* we had previously expanded
4689 * an actual macro, scan the lines again for macros...
4691 tline = thead;
4692 expanded = false;
4693 goto again;
4697 err:
4698 if (org_tline) {
4699 if (thead) {
4700 *org_tline = *thead;
4701 /* since we just gave text to org_line, don't free it */
4702 thead->text = NULL;
4703 delete_Token(thead);
4704 } else {
4705 /* the expression expanded to empty line;
4706 we can't return NULL for some reasons
4707 we just set the line to a single WHITESPACE token. */
4708 memset(org_tline, 0, sizeof(*org_tline));
4709 org_tline->text = NULL;
4710 org_tline->type = TOK_WHITESPACE;
4712 thead = org_tline;
4715 return thead;
4719 * Similar to expand_smacro but used exclusively with macro identifiers
4720 * right before they are fetched in. The reason is that there can be
4721 * identifiers consisting of several subparts. We consider that if there
4722 * are more than one element forming the name, user wants a expansion,
4723 * otherwise it will be left as-is. Example:
4725 * %define %$abc cde
4727 * the identifier %$abc will be left as-is so that the handler for %define
4728 * will suck it and define the corresponding value. Other case:
4730 * %define _%$abc cde
4732 * In this case user wants name to be expanded *before* %define starts
4733 * working, so we'll expand %$abc into something (if it has a value;
4734 * otherwise it will be left as-is) then concatenate all successive
4735 * PP_IDs into one.
4737 static Token *expand_id(Token * tline)
4739 Token *cur, *oldnext = NULL;
4741 if (!tline || !tline->next)
4742 return tline;
4744 cur = tline;
4745 while (cur->next &&
4746 (cur->next->type == TOK_ID ||
4747 cur->next->type == TOK_PREPROC_ID
4748 || cur->next->type == TOK_NUMBER))
4749 cur = cur->next;
4751 /* If identifier consists of just one token, don't expand */
4752 if (cur == tline)
4753 return tline;
4755 if (cur) {
4756 oldnext = cur->next; /* Detach the tail past identifier */
4757 cur->next = NULL; /* so that expand_smacro stops here */
4760 tline = expand_smacro(tline);
4762 if (cur) {
4763 /* expand_smacro possibly changhed tline; re-scan for EOL */
4764 cur = tline;
4765 while (cur && cur->next)
4766 cur = cur->next;
4767 if (cur)
4768 cur->next = oldnext;
4771 return tline;
4775 * Determine whether the given line constitutes a multi-line macro
4776 * call, and return the ExpDef structure called if so. Doesn't have
4777 * to check for an initial label - that's taken care of in
4778 * expand_mmacro - but must check numbers of parameters. Guaranteed
4779 * to be called with tline->type == TOK_ID, so the putative macro
4780 * name is easy to find.
4782 static ExpDef *is_mmacro(Token * tline, Token *** params_array)
4784 ExpDef *head, *ed;
4785 Token **params;
4786 int nparam;
4788 head = (ExpDef *) hash_findix(&expdefs, tline->text);
4791 * Efficiency: first we see if any macro exists with the given
4792 * name. If not, we can return NULL immediately. _Then_ we
4793 * count the parameters, and then we look further along the
4794 * list if necessary to find the proper ExpDef.
4796 list_for_each(ed, head)
4797 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4798 break;
4799 if (!ed)
4800 return NULL;
4803 * OK, we have a potential macro. Count and demarcate the
4804 * parameters.
4806 count_mmac_params(tline->next, &nparam, &params);
4809 * So we know how many parameters we've got. Find the ExpDef
4810 * structure that handles this number.
4812 while (ed) {
4813 if (ed->nparam_min <= nparam
4814 && (ed->plus || nparam <= ed->nparam_max)) {
4816 * It's right, and we can use it. Add its default
4817 * parameters to the end of our list if necessary.
4819 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
4820 params =
4821 nasm_realloc(params,
4822 ((ed->nparam_min + ed->ndefs +
4823 1) * sizeof(*params)));
4824 while (nparam < ed->nparam_min + ed->ndefs) {
4825 params[nparam] = ed->defaults[nparam - ed->nparam_min];
4826 nparam++;
4830 * If we've gone over the maximum parameter count (and
4831 * we're in Plus mode), ignore parameters beyond
4832 * nparam_max.
4834 if (ed->plus && nparam > ed->nparam_max)
4835 nparam = ed->nparam_max;
4837 * Then terminate the parameter list, and leave.
4839 if (!params) { /* need this special case */
4840 params = nasm_malloc(sizeof(*params));
4841 nparam = 0;
4843 params[nparam] = NULL;
4844 *params_array = params;
4845 return ed;
4848 * This one wasn't right: look for the next one with the
4849 * same name.
4851 list_for_each(ed, ed->next)
4852 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4853 break;
4857 * After all that, we didn't find one with the right number of
4858 * parameters. Issue a warning, and fail to expand the macro.
4860 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4861 "macro `%s' exists, but not taking %d parameters",
4862 tline->text, nparam);
4863 nasm_free(params);
4864 return NULL;
4868 * Expand the multi-line macro call made by the given line, if
4869 * there is one to be expanded. If there is, push the expansion on
4870 * istk->expansion and return true. Otherwise return false.
4872 static bool expand_mmacro(Token * tline)
4874 Token *label = NULL;
4875 int dont_prepend = 0;
4876 Token **params, *t, *mtok;
4877 Line *l = NULL;
4878 ExpDef *ed;
4879 ExpInv *ei;
4880 int i, nparam, *paramlen;
4881 const char *mname;
4883 t = tline;
4884 skip_white_(t);
4885 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4886 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4887 return false;
4888 mtok = t;
4889 ed = is_mmacro(t, &params);
4890 if (ed != NULL) {
4891 mname = t->text;
4892 } else {
4893 Token *last;
4895 * We have an id which isn't a macro call. We'll assume
4896 * it might be a label; we'll also check to see if a
4897 * colon follows it. Then, if there's another id after
4898 * that lot, we'll check it again for macro-hood.
4900 label = last = t;
4901 t = t->next;
4902 if (tok_type_(t, TOK_WHITESPACE))
4903 last = t, t = t->next;
4904 if (tok_is_(t, ":")) {
4905 dont_prepend = 1;
4906 last = t, t = t->next;
4907 if (tok_type_(t, TOK_WHITESPACE))
4908 last = t, t = t->next;
4910 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4911 return false;
4912 last->next = NULL;
4913 mname = t->text;
4914 tline = t;
4918 * Fix up the parameters: this involves stripping leading and
4919 * trailing whitespace, then stripping braces if they are
4920 * present.
4922 for (nparam = 0; params[nparam]; nparam++) ;
4923 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4925 for (i = 0; params[i]; i++) {
4926 int brace = false;
4927 int comma = (!ed->plus || i < nparam - 1);
4929 t = params[i];
4930 skip_white_(t);
4931 if (tok_is_(t, "{"))
4932 t = t->next, brace = true, comma = false;
4933 params[i] = t;
4934 paramlen[i] = 0;
4935 while (t) {
4936 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4937 break; /* ... because we have hit a comma */
4938 if (comma && t->type == TOK_WHITESPACE
4939 && tok_is_(t->next, ","))
4940 break; /* ... or a space then a comma */
4941 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4942 break; /* ... or a brace */
4943 t = t->next;
4944 paramlen[i]++;
4948 if (ed->cur_depth >= ed->max_depth) {
4949 if (ed->max_depth > 1) {
4950 error(ERR_WARNING,
4951 "reached maximum macro recursion depth of %i for %s",
4952 ed->max_depth,ed->name);
4954 return false;
4955 } else {
4956 ed->cur_depth ++;
4960 * OK, we have found a ExpDef structure representing a
4961 * previously defined mmacro. Create an expansion invocation
4962 * and point it back to the expansion definition. Substitution of
4963 * parameter tokens and macro-local tokens doesn't get done
4964 * until the single-line macro substitution process; this is
4965 * because delaying them allows us to change the semantics
4966 * later through %rotate.
4968 ei = new_ExpInv(EXP_MMACRO, ed);
4969 ei->name = nasm_strdup(mname);
4970 //ei->label = label;
4971 //ei->label_text = detoken(label, false);
4972 ei->current = ed->line;
4973 ei->emitting = true;
4974 //ei->iline = tline;
4975 ei->params = params;
4976 ei->nparam = nparam;
4977 ei->rotate = 0;
4978 ei->paramlen = paramlen;
4979 ei->lineno = 0;
4981 ei->prev = istk->expansion;
4982 istk->expansion = ei;
4985 * Special case: detect %00 on first invocation; if found,
4986 * avoid emitting any labels that precede the mmacro call.
4987 * ed->prepend is set to -1 when %00 is detected, else 1.
4989 if (ed->prepend == 0) {
4990 for (l = ed->line; l != NULL; l = l->next) {
4991 for (t = l->first; t != NULL; t = t->next) {
4992 if ((t->type == TOK_PREPROC_ID) &&
4993 (strlen(t->text) == 3) &&
4994 (t->text[1] == '0') && (t->text[2] == '0')) {
4995 dont_prepend = -1;
4996 break;
4999 if (dont_prepend < 0) {
5000 break;
5003 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5007 * If we had a label, push it on as the first line of
5008 * the macro expansion.
5010 if (label != NULL) {
5011 if (ed->prepend < 0) {
5012 ei->label_text = detoken(label, false);
5013 } else {
5014 if (dont_prepend == 0) {
5015 t = label;
5016 while (t->next != NULL) {
5017 t = t->next;
5019 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5021 l = new_Line();
5022 l->first = copy_Token(label);
5023 l->next = ei->current;
5024 ei->current = l;
5028 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
5030 istk->mmac_depth++;
5031 return true;
5034 /* The function that actually does the error reporting */
5035 static void verror(int severity, const char *fmt, va_list arg)
5037 char buff[1024];
5039 vsnprintf(buff, sizeof(buff), fmt, arg);
5041 if (istk && istk->mmac_depth > 0) {
5042 ExpInv *ei = istk->expansion;
5043 int lineno = ei->lineno;
5044 while (ei) {
5045 if (ei->type == EXP_MMACRO)
5046 break;
5047 lineno += ei->relno;
5048 ei = ei->prev;
5050 nasm_error(severity, "(%s:%d) %s", ei->def->name,
5051 lineno, buff);
5052 } else
5053 nasm_error(severity, "%s", buff);
5057 * Since preprocessor always operate only on the line that didn't
5058 * arrived yet, we should always use ERR_OFFBY1.
5060 static void error(int severity, const char *fmt, ...)
5062 va_list arg;
5063 va_start(arg, fmt);
5064 verror(severity, fmt, arg);
5065 va_end(arg);
5069 * Because %else etc are evaluated in the state context
5070 * of the previous branch, errors might get lost with error():
5071 * %if 0 ... %else trailing garbage ... %endif
5072 * So %else etc should report errors with this function.
5074 static void error_precond(int severity, const char *fmt, ...)
5076 va_list arg;
5078 /* Only ignore the error if it's really in a dead branch */
5079 if ((istk != NULL) &&
5080 (istk->expansion != NULL) &&
5081 (istk->expansion->type == EXP_IF) &&
5082 (istk->expansion->def->state == COND_NEVER))
5083 return;
5085 va_start(arg, fmt);
5086 verror(severity, fmt, arg);
5087 va_end(arg);
5090 static void
5091 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
5093 Token *t;
5095 cstk = NULL;
5096 istk = nasm_zalloc(sizeof(Include));
5097 istk->fp = fopen(file, "r");
5098 src_set_fname(nasm_strdup(file));
5099 src_set_linnum(0);
5100 istk->lineinc = 1;
5101 if (!istk->fp)
5102 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
5103 file);
5104 defining = NULL;
5105 finals = NULL;
5106 in_final = false;
5107 nested_mac_count = 0;
5108 nested_rep_count = 0;
5109 init_macros();
5110 unique = 0;
5111 if (tasm_compatible_mode) {
5112 stdmacpos = nasm_stdmac;
5113 } else {
5114 stdmacpos = nasm_stdmac_after_tasm;
5116 any_extrastdmac = extrastdmac && *extrastdmac;
5117 do_predef = true;
5118 list = listgen;
5121 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5122 * The caller, however, will also pass in 3 for preprocess-only so
5123 * we can set __PASS__ accordingly.
5125 pass = apass > 2 ? 2 : apass;
5127 dephead = deptail = deplist;
5128 if (deplist) {
5129 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5130 sl->next = NULL;
5131 strcpy(sl->str, file);
5132 *deptail = sl;
5133 deptail = &sl->next;
5137 * Define the __PASS__ macro. This is defined here unlike
5138 * all the other builtins, because it is special -- it varies between
5139 * passes.
5141 t = nasm_zalloc(sizeof(*t));
5142 make_tok_num(t, apass);
5143 define_smacro(NULL, "__PASS__", true, 0, t);
5146 static char *pp_getline(void)
5148 char *line;
5149 Token *tline;
5150 ExpDef *ed;
5151 ExpInv *ei;
5152 Line *l;
5153 int j;
5155 while (1) {
5157 * Fetch a tokenized line, either from the expansion
5158 * buffer or from the input file.
5160 tline = NULL;
5162 while (1) { /* until we get a line we can use */
5164 * Fetch a tokenized line from the expansion buffer
5166 if (istk->expansion != NULL) {
5167 ei = istk->expansion;
5168 if (ei->current != NULL) {
5169 if (ei->emitting == false) {
5170 ei->current = NULL;
5171 continue;
5173 l = ei->current;
5174 ei->current = l->next;
5175 ei->lineno++;
5176 tline = copy_Token(l->first);
5177 if (((ei->type == EXP_REP) ||
5178 (ei->type == EXP_MMACRO) ||
5179 (ei->type == EXP_WHILE))
5180 && (ei->def->nolist == false)) {
5181 char *p = detoken(tline, false);
5182 list->line(LIST_MACRO, p);
5183 nasm_free(p);
5185 if (ei->linnum > -1) {
5186 src_set_linnum(src_get_linnum() + 1);
5188 break;
5189 } else if ((ei->type == EXP_REP) &&
5190 (ei->def->cur_depth < ei->def->max_depth)) {
5191 ei->def->cur_depth ++;
5192 ei->current = ei->def->line;
5193 ei->lineno = 0;
5194 continue;
5195 } else if ((ei->type == EXP_WHILE) &&
5196 (ei->def->cur_depth < ei->def->max_depth)) {
5197 ei->current = ei->def->line;
5198 ei->lineno = 0;
5199 tline = copy_Token(ei->current->first);
5200 j = if_condition(tline, PP_WHILE);
5201 tline = NULL;
5202 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5203 if (j == COND_IF_TRUE) {
5204 ei->current = ei->current->next;
5205 ei->def->cur_depth ++;
5206 } else {
5207 ei->emitting = false;
5208 ei->current = NULL;
5209 ei->def->cur_depth = ei->def->max_depth;
5211 continue;
5212 } else {
5213 istk->expansion = ei->prev;
5214 ed = ei->def;
5215 if (ed != NULL) {
5216 if ((ei->emitting == true) &&
5217 (ed->max_depth == DEADMAN_LIMIT) &&
5218 (ed->cur_depth == DEADMAN_LIMIT)
5220 error(ERR_FATAL, "runaway expansion detected, aborting");
5222 if (ed->cur_depth > 0) {
5223 ed->cur_depth --;
5224 } else if (ed->type != EXP_MMACRO) {
5225 expansions = ed->prev;
5226 free_expdef(ed);
5228 if ((ei->type == EXP_REP) ||
5229 (ei->type == EXP_MMACRO) ||
5230 (ei->type == EXP_WHILE)) {
5231 list->downlevel(LIST_MACRO);
5232 if (ei->type == EXP_MMACRO) {
5233 istk->mmac_depth--;
5237 if (ei->linnum > -1) {
5238 src_set_linnum(ei->linnum);
5240 free_expinv(ei);
5241 continue;
5246 * Read in line from input and tokenize
5248 line = read_line();
5249 if (line) { /* from the current input file */
5250 line = prepreproc(line);
5251 tline = tokenize(line);
5252 nasm_free(line);
5253 break;
5257 * The current file has ended; work down the istk
5260 Include *i = istk;
5261 fclose(i->fp);
5262 if (i->expansion != NULL) {
5263 error(ERR_FATAL,
5264 "end of file while still in an expansion");
5266 /* only set line and file name if there's a next node */
5267 if (i->next) {
5268 src_set_linnum(i->lineno);
5269 nasm_free(src_set_fname(nasm_strdup(i->fname)));
5271 if ((i->next == NULL) && (finals != NULL)) {
5272 in_final = true;
5273 ei = new_ExpInv(EXP_FINAL, NULL);
5274 ei->emitting = true;
5275 ei->current = finals;
5276 istk->expansion = ei;
5277 finals = NULL;
5278 continue;
5280 istk = i->next;
5281 list->downlevel(LIST_INCLUDE);
5282 nasm_free(i);
5283 if (istk == NULL) {
5284 if (finals != NULL) {
5285 in_final = true;
5286 } else {
5287 return NULL;
5290 continue;
5294 if (defining == NULL) {
5295 tline = expand_mmac_params(tline);
5299 * Check the line to see if it's a preprocessor directive.
5301 if (do_directive(tline) == DIRECTIVE_FOUND) {
5302 continue;
5303 } else if (defining != NULL) {
5305 * We're defining an expansion. We emit nothing at all,
5306 * and just shove the tokenized line on to the definition.
5308 if (defining->ignoring == false) {
5309 Line *l = new_Line();
5310 l->first = tline;
5311 if (defining->line == NULL) {
5312 defining->line = l;
5313 defining->last = l;
5314 } else {
5315 defining->last->next = l;
5316 defining->last = l;
5318 } else {
5319 free_tlist(tline);
5321 defining->linecount++;
5322 continue;
5323 } else if ((istk->expansion != NULL) &&
5324 (istk->expansion->emitting != true)) {
5326 * We're in a non-emitting branch of an expansion.
5327 * Emit nothing at all, not even a blank line: when we
5328 * emerge from the expansion we'll give a line-number
5329 * directive so we keep our place correctly.
5331 free_tlist(tline);
5332 continue;
5333 } else {
5334 tline = expand_smacro(tline);
5335 if (expand_mmacro(tline) != true) {
5337 * De-tokenize the line again, and emit it.
5339 line = detoken(tline, true);
5340 free_tlist(tline);
5341 break;
5342 } else {
5343 continue;
5347 return line;
5350 static void pp_cleanup(int pass)
5352 if (defining != NULL) {
5353 error(ERR_NONFATAL, "end of file while still defining an expansion");
5354 while (defining != NULL) {
5355 ExpDef *ed = defining;
5356 defining = ed->prev;
5357 free_expdef(ed);
5359 defining = NULL;
5361 while (cstk != NULL)
5362 ctx_pop();
5363 free_macros();
5364 while (istk != NULL) {
5365 Include *i = istk;
5366 istk = istk->next;
5367 fclose(i->fp);
5368 nasm_free(i->fname);
5369 while (i->expansion != NULL) {
5370 ExpInv *ei = i->expansion;
5371 i->expansion = ei->prev;
5372 free_expinv(ei);
5374 nasm_free(i);
5376 while (cstk)
5377 ctx_pop();
5378 nasm_free(src_set_fname(NULL));
5379 if (pass == 0) {
5380 IncPath *i;
5381 free_llist(predef);
5382 delete_Blocks();
5383 while ((i = ipath)) {
5384 ipath = i->next;
5385 if (i->path)
5386 nasm_free(i->path);
5387 nasm_free(i);
5392 void pp_include_path(char *path)
5394 IncPath *i = nasm_zalloc(sizeof(IncPath));
5396 if (path)
5397 i->path = nasm_strdup(path);
5399 if (ipath) {
5400 IncPath *j = ipath;
5401 while (j->next)
5402 j = j->next;
5403 j->next = i;
5404 } else {
5405 ipath = i;
5409 void pp_pre_include(char *fname)
5411 Token *inc, *space, *name;
5412 Line *l;
5414 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5415 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5416 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5418 l = new_Line();
5419 l->next = predef;
5420 l->first = inc;
5421 predef = l;
5424 void pp_pre_define(char *definition)
5426 Token *def, *space;
5427 Line *l;
5428 char *equals;
5430 equals = strchr(definition, '=');
5431 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5432 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5433 if (equals)
5434 *equals = ' ';
5435 space->next = tokenize(definition);
5436 if (equals)
5437 *equals = '=';
5439 l = new_Line();
5440 l->next = predef;
5441 l->first = def;
5442 predef = l;
5445 void pp_pre_undefine(char *definition)
5447 Token *def, *space;
5448 Line *l;
5450 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5451 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5452 space->next = tokenize(definition);
5454 l = new_Line();
5455 l->next = predef;
5456 l->first = def;
5457 predef = l;
5461 * This function is used to assist with "runtime" preprocessor
5462 * directives, e.g. pp_runtime("%define __BITS__ 64");
5464 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5465 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5468 void pp_runtime(char *definition)
5470 Token *def;
5472 def = tokenize(definition);
5473 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5474 free_tlist(def);
5478 void pp_extra_stdmac(macros_t *macros)
5480 extrastdmac = macros;
5483 static void make_tok_num(Token * tok, int64_t val)
5485 char numbuf[20];
5486 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5487 tok->text = nasm_strdup(numbuf);
5488 tok->type = TOK_NUMBER;
5491 Preproc nasmpp = {
5492 pp_reset,
5493 pp_getline,
5494 pp_cleanup