A few more AVX2 spec instructions
[nasm-cyr.git] / preproc.c
blob3353b287d09b5b69fde13113da7b40b8b2ba974a
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2011 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 static void make_tok_num(Token * tok, int64_t val);
465 static void error(int severity, const char *fmt, ...);
466 static void error_precond(int severity, const char *fmt, ...);
467 static void *new_Block(size_t size);
468 static void delete_Blocks(void);
469 static Token *new_Token(Token * next, enum pp_token_type type,
470 const char *text, int txtlen);
471 static Token *copy_Token(Token * tline);
472 static Token *delete_Token(Token * t);
473 static Line *new_Line(void);
474 static ExpDef *new_ExpDef(int exp_type);
475 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
478 * Macros for safe checking of token pointers, avoid *(NULL)
480 #define tok_type_(x,t) ((x) && (x)->type == (t))
481 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
486 * A few helpers for single macros
489 /* We might be not smacro parameter at all */
490 static bool is_smacro_param(Token *t)
492 return t->type >= TOK_SMAC_PARAM;
495 /* smacro parameters are counted in a special way */
496 static int smacro_get_param_idx(Token *t)
498 return t->type - TOK_SMAC_PARAM;
501 /* encode smacro parameter index */
502 static int smacro_set_param_idx(Token *t, unsigned int index)
504 return t->type = TOK_SMAC_PARAM + index;
507 #ifdef NASM_TRACE
509 #define stringify(x) #x
511 #define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
512 #define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
513 #define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
515 /* FIXME: we really need some compound type here instead of inplace code */
516 static const char *nasm_get_tok_type_str(enum pp_token_type type)
518 #define SWITCH_TOK_NAME(type) \
519 case (type): \
520 return stringify(type)
522 switch (type) {
523 SWITCH_TOK_NAME(TOK_NONE);
524 SWITCH_TOK_NAME(TOK_WHITESPACE);
525 SWITCH_TOK_NAME(TOK_COMMENT);
526 SWITCH_TOK_NAME(TOK_ID);
527 SWITCH_TOK_NAME(TOK_PREPROC_ID);
528 SWITCH_TOK_NAME(TOK_STRING);
529 SWITCH_TOK_NAME(TOK_NUMBER);
530 SWITCH_TOK_NAME(TOK_FLOAT);
531 SWITCH_TOK_NAME(TOK_SMAC_END);
532 SWITCH_TOK_NAME(TOK_OTHER);
533 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
534 SWITCH_TOK_NAME(TOK_PREPROC_Q);
535 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
536 SWITCH_TOK_NAME(TOK_PASTE);
537 SWITCH_TOK_NAME(TOK_INDIRECT);
538 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
539 SWITCH_TOK_NAME(TOK_MAX);
542 return NULL;
545 static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
547 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
548 if (token) {
549 Token *t;
550 list_for_each(t, token) {
551 if (t->text)
552 printf("'%s'(%s) ", t->text,
553 nasm_get_tok_type_str(t->type));
555 printf("\n\n");
559 static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
561 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
562 if (token) {
563 Token *t;
564 list_for_each(t, token)
565 printf("%s", t->text ? t->text : " ");
566 printf("\n\n");
570 #else
571 #define nasm_trace(msg, ...)
572 #define nasm_dump_token(t)
573 #define nasm_dump_stream(t)
574 #endif
577 * nasm_unquote with error if the string contains NUL characters.
578 * If the string contains NUL characters, issue an error and return
579 * the C len, i.e. truncate at the NUL.
581 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
583 size_t len = nasm_unquote(qstr, NULL);
584 size_t clen = strlen(qstr);
586 if (len != clen)
587 error(ERR_NONFATAL, "NUL character in `%s' directive",
588 pp_directives[directive]);
590 return clen;
594 * In-place reverse a list of tokens.
596 static Token *reverse_tokens(Token *t)
598 Token *prev, *next;
600 list_reverse(t, prev, next);
602 return t;
606 * Handle TASM specific directives, which do not contain a % in
607 * front of them. We do it here because I could not find any other
608 * place to do it for the moment, and it is a hack (ideally it would
609 * be nice to be able to use the NASM pre-processor to do it).
611 static char *check_tasm_directive(char *line)
613 int32_t i, j, k, m, len;
614 char *p, *q, *oldline, oldchar;
616 p = nasm_skip_spaces(line);
618 /* Binary search for the directive name */
619 i = -1;
620 j = ARRAY_SIZE(tasm_directives);
621 q = nasm_skip_word(p);
622 len = q - p;
623 if (len) {
624 oldchar = p[len];
625 p[len] = 0;
626 while (j - i > 1) {
627 k = (j + i) / 2;
628 m = nasm_stricmp(p, tasm_directives[k]);
629 if (m == 0) {
630 /* We have found a directive, so jam a % in front of it
631 * so that NASM will then recognise it as one if it's own.
633 p[len] = oldchar;
634 len = strlen(p);
635 oldline = line;
636 line = nasm_malloc(len + 2);
637 line[0] = '%';
638 if (k == TM_IFDIFI) {
640 * NASM does not recognise IFDIFI, so we convert
641 * it to %if 0. This is not used in NASM
642 * compatible code, but does need to parse for the
643 * TASM macro package.
645 strcpy(line + 1, "if 0");
646 } else {
647 memcpy(line + 1, p, len + 1);
649 nasm_free(oldline);
650 return line;
651 } else if (m < 0) {
652 j = k;
653 } else
654 i = k;
656 p[len] = oldchar;
658 return line;
662 * The pre-preprocessing stage... This function translates line
663 * number indications as they emerge from GNU cpp (`# lineno "file"
664 * flags') into NASM preprocessor line number indications (`%line
665 * lineno file').
667 static char *prepreproc(char *line)
669 int lineno, fnlen;
670 char *fname, *oldline;
672 if (line[0] == '#' && line[1] == ' ') {
673 oldline = line;
674 fname = oldline + 2;
675 lineno = atoi(fname);
676 fname += strspn(fname, "0123456789 ");
677 if (*fname == '"')
678 fname++;
679 fnlen = strcspn(fname, "\"");
680 line = nasm_malloc(20 + fnlen);
681 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
682 nasm_free(oldline);
684 if (tasm_compatible_mode)
685 return check_tasm_directive(line);
686 return line;
690 * Free a linked list of tokens.
692 static void free_tlist(Token * list)
694 while (list)
695 list = delete_Token(list);
699 * Free a linked list of lines.
701 static void free_llist(Line * list)
703 Line *l, *tmp;
704 list_for_each_safe(l, tmp, list) {
705 free_tlist(l->first);
706 nasm_free(l);
711 * Free an ExpDef
713 static void free_expdef(ExpDef * ed)
715 nasm_free(ed->name);
716 free_tlist(ed->dlist);
717 nasm_free(ed->defaults);
718 free_llist(ed->line);
719 nasm_free(ed);
723 * Free an ExpInv
725 static void free_expinv(ExpInv * ei)
727 nasm_free(ei->name);
728 nasm_free(ei->label_text);
729 nasm_free(ei);
733 * Free all currently defined macros, and free the hash tables
735 static void free_smacro_table(struct hash_table *smt)
737 SMacro *s, *tmp;
738 const char *key;
739 struct hash_tbl_node *it = NULL;
741 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
742 nasm_free((void *)key);
743 list_for_each_safe(s, tmp, s) {
744 nasm_free(s->name);
745 free_tlist(s->expansion);
746 nasm_free(s);
749 hash_free(smt);
752 static void free_expdef_table(struct hash_table *edt)
754 ExpDef *ed, *tmp;
755 const char *key;
756 struct hash_tbl_node *it = NULL;
758 it = NULL;
759 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
760 nasm_free((void *)key);
761 list_for_each_safe(ed ,tmp, ed)
762 free_expdef(ed);
764 hash_free(edt);
767 static void free_macros(void)
769 free_smacro_table(&smacros);
770 free_expdef_table(&expdefs);
774 * Initialize the hash tables
776 static void init_macros(void)
778 hash_init(&smacros, HASH_LARGE);
779 hash_init(&expdefs, HASH_LARGE);
783 * Pop the context stack.
785 static void ctx_pop(void)
787 Context *c = cstk;
789 cstk = cstk->next;
790 free_smacro_table(&c->localmac);
791 nasm_free(c->name);
792 nasm_free(c);
796 * Search for a key in the hash index; adding it if necessary
797 * (in which case we initialize the data pointer to NULL.)
799 static void **
800 hash_findi_add(struct hash_table *hash, const char *str)
802 struct hash_insert hi;
803 void **r;
804 char *strx;
806 r = hash_findi(hash, str, &hi);
807 if (r)
808 return r;
810 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
811 return hash_add(&hi, strx, NULL);
815 * Like hash_findi, but returns the data element rather than a pointer
816 * to it. Used only when not adding a new element, hence no third
817 * argument.
819 static void *
820 hash_findix(struct hash_table *hash, const char *str)
822 void **p;
824 p = hash_findi(hash, str, NULL);
825 return p ? *p : NULL;
829 * read line from standard macros set,
830 * if there no more left -- return NULL
832 static char *line_from_stdmac(void)
834 unsigned char c;
835 const unsigned char *p = stdmacpos;
836 char *line, *q;
837 size_t len = 0;
839 if (!stdmacpos)
840 return NULL;
842 while ((c = *p++)) {
843 if (c >= 0x80)
844 len += pp_directives_len[c - 0x80] + 1;
845 else
846 len++;
849 line = nasm_malloc(len + 1);
850 q = line;
851 while ((c = *stdmacpos++)) {
852 if (c >= 0x80) {
853 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
854 q += pp_directives_len[c - 0x80];
855 *q++ = ' ';
856 } else {
857 *q++ = c;
860 stdmacpos = p;
861 *q = '\0';
863 if (!*stdmacpos) {
864 /* This was the last of the standard macro chain... */
865 stdmacpos = NULL;
866 if (any_extrastdmac) {
867 stdmacpos = extrastdmac;
868 any_extrastdmac = false;
869 } else if (do_predef) {
870 ExpInv *ei;
871 Line *pd, *l;
872 Token *head, **tail, *t;
875 * Nasty hack: here we push the contents of
876 * `predef' on to the top-level expansion stack,
877 * since this is the most convenient way to
878 * implement the pre-include and pre-define
879 * features.
881 list_for_each(pd, predef) {
882 head = NULL;
883 tail = &head;
884 list_for_each(t, pd->first) {
885 *tail = new_Token(NULL, t->type, t->text, 0);
886 tail = &(*tail)->next;
889 l = new_Line();
890 l->first = head;
891 ei = new_ExpInv(EXP_PREDEF, NULL);
892 ei->current = l;
893 ei->emitting = true;
894 ei->prev = istk->expansion;
895 istk->expansion = ei;
897 do_predef = false;
901 return line;
904 #define BUF_DELTA 512
906 * Read a line from the top file in istk, handling multiple CR/LFs
907 * at the end of the line read, and handling spurious ^Zs. Will
908 * return lines from the standard macro set if this has not already
909 * been done.
911 static char *read_line(void)
913 char *buffer, *p, *q;
914 int bufsize, continued_count;
917 * standart macros set (predefined) goes first
919 p = line_from_stdmac();
920 if (p)
921 return p;
924 * regular read from a file
926 bufsize = BUF_DELTA;
927 buffer = nasm_malloc(BUF_DELTA);
928 p = buffer;
929 continued_count = 0;
930 while (1) {
931 q = fgets(p, bufsize - (p - buffer), istk->fp);
932 if (!q)
933 break;
934 p += strlen(p);
935 if (p > buffer && p[-1] == '\n') {
937 * Convert backslash-CRLF line continuation sequences into
938 * nothing at all (for DOS and Windows)
940 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
941 p -= 3;
942 *p = 0;
943 continued_count++;
946 * Also convert backslash-LF line continuation sequences into
947 * nothing at all (for Unix)
949 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
950 p -= 2;
951 *p = 0;
952 continued_count++;
953 } else {
954 break;
957 if (p - buffer > bufsize - 10) {
958 int32_t offset = p - buffer;
959 bufsize += BUF_DELTA;
960 buffer = nasm_realloc(buffer, bufsize);
961 p = buffer + offset; /* prevent stale-pointer problems */
965 if (!q && p == buffer) {
966 nasm_free(buffer);
967 return NULL;
970 src_set_linnum(src_get_linnum() + istk->lineinc +
971 (continued_count * istk->lineinc));
974 * Play safe: remove CRs as well as LFs, if any of either are
975 * present at the end of the line.
977 while (--p >= buffer && (*p == '\n' || *p == '\r'))
978 *p = '\0';
981 * Handle spurious ^Z, which may be inserted into source files
982 * by some file transfer utilities.
984 buffer[strcspn(buffer, "\032")] = '\0';
986 list->line(LIST_READ, buffer);
988 return buffer;
992 * Tokenize a line of text. This is a very simple process since we
993 * don't need to parse the value out of e.g. numeric tokens: we
994 * simply split one string into many.
996 static Token *tokenize(char *line)
998 char c, *p = line;
999 enum pp_token_type type;
1000 Token *list = NULL;
1001 Token *t, **tail = &list;
1002 bool verbose = true;
1004 nasm_trace("Tokenize for '%s'", line);
1006 if ((defining != NULL) && (defining->ignoring == true)) {
1007 verbose = false;
1010 while (*line) {
1011 p = line;
1012 if (*p == '%') {
1013 p++;
1014 if (*p == '+' && !nasm_isdigit(p[1])) {
1015 p++;
1016 type = TOK_PASTE;
1017 } else if (nasm_isdigit(*p) ||
1018 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
1019 do {
1020 p++;
1022 while (nasm_isdigit(*p));
1023 type = TOK_PREPROC_ID;
1024 } else if (*p == '{') {
1025 p++;
1026 while (*p && *p != '}') {
1027 p[-1] = *p;
1028 p++;
1030 p[-1] = '\0';
1031 if (*p)
1032 p++;
1033 type = TOK_PREPROC_ID;
1034 } else if (*p == '[') {
1035 int lvl = 1;
1036 line += 2; /* Skip the leading %[ */
1037 p++;
1038 while (lvl && (c = *p++)) {
1039 switch (c) {
1040 case ']':
1041 lvl--;
1042 break;
1043 case '%':
1044 if (*p == '[')
1045 lvl++;
1046 break;
1047 case '\'':
1048 case '\"':
1049 case '`':
1050 p = nasm_skip_string(p - 1) + 1;
1051 break;
1052 default:
1053 break;
1056 p--;
1057 if (*p)
1058 *p++ = '\0';
1059 if (lvl && verbose)
1060 error(ERR_NONFATAL, "unterminated %[ construct");
1061 type = TOK_INDIRECT;
1062 } else if (*p == '?') {
1063 type = TOK_PREPROC_Q; /* %? */
1064 p++;
1065 if (*p == '?') {
1066 type = TOK_PREPROC_QQ; /* %?? */
1067 p++;
1069 } else if (*p == '!') {
1070 type = TOK_PREPROC_ID;
1071 p++;
1072 if (isidchar(*p)) {
1073 do {
1074 p++;
1075 } while (isidchar(*p));
1076 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1077 p = nasm_skip_string(p);
1078 if (*p)
1079 p++;
1080 else if(verbose)
1081 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1082 } else {
1083 /* %! without string or identifier */
1084 type = TOK_OTHER; /* Legacy behavior... */
1086 } else if (isidchar(*p) ||
1087 ((*p == '!' || *p == '%' || *p == '$') &&
1088 isidchar(p[1]))) {
1089 do {
1090 p++;
1092 while (isidchar(*p));
1093 type = TOK_PREPROC_ID;
1094 } else {
1095 type = TOK_OTHER;
1096 if (*p == '%')
1097 p++;
1099 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1100 type = TOK_ID;
1101 p++;
1102 while (*p && isidchar(*p))
1103 p++;
1104 } else if (*p == '\'' || *p == '"' || *p == '`') {
1106 * A string token.
1108 type = TOK_STRING;
1109 p = nasm_skip_string(p);
1111 if (*p) {
1112 p++;
1113 } else if(verbose) {
1114 error(ERR_WARNING|ERR_PASS1, "unterminated string");
1115 /* Handling unterminated strings by UNV */
1116 /* type = -1; */
1118 } else if (p[0] == '$' && p[1] == '$') {
1119 type = TOK_OTHER; /* TOKEN_BASE */
1120 p += 2;
1121 } else if (isnumstart(*p)) {
1122 bool is_hex = false;
1123 bool is_float = false;
1124 bool has_e = false;
1125 char c, *r;
1128 * A numeric token.
1131 if (*p == '$') {
1132 p++;
1133 is_hex = true;
1136 for (;;) {
1137 c = *p++;
1139 if (!is_hex && (c == 'e' || c == 'E')) {
1140 has_e = true;
1141 if (*p == '+' || *p == '-') {
1143 * e can only be followed by +/- if it is either a
1144 * prefixed hex number or a floating-point number
1146 p++;
1147 is_float = true;
1149 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1150 is_hex = true;
1151 } else if (c == 'P' || c == 'p') {
1152 is_float = true;
1153 if (*p == '+' || *p == '-')
1154 p++;
1155 } else if (isnumchar(c) || c == '_')
1156 ; /* just advance */
1157 else if (c == '.') {
1159 * we need to deal with consequences of the legacy
1160 * parser, like "1.nolist" being two tokens
1161 * (TOK_NUMBER, TOK_ID) here; at least give it
1162 * a shot for now. In the future, we probably need
1163 * a flex-based scanner with proper pattern matching
1164 * to do it as well as it can be done. Nothing in
1165 * the world is going to help the person who wants
1166 * 0x123.p16 interpreted as two tokens, though.
1168 r = p;
1169 while (*r == '_')
1170 r++;
1172 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1173 (!is_hex && (*r == 'e' || *r == 'E')) ||
1174 (*r == 'p' || *r == 'P')) {
1175 p = r;
1176 is_float = true;
1177 } else
1178 break; /* Terminate the token */
1179 } else
1180 break;
1182 p--; /* Point to first character beyond number */
1184 if (p == line+1 && *line == '$') {
1185 type = TOK_OTHER; /* TOKEN_HERE */
1186 } else {
1187 if (has_e && !is_hex) {
1188 /* 1e13 is floating-point, but 1e13h is not */
1189 is_float = true;
1192 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1194 } else if (nasm_isspace(*p)) {
1195 type = TOK_WHITESPACE;
1196 p = nasm_skip_spaces(p);
1198 * Whitespace just before end-of-line is discarded by
1199 * pretending it's a comment; whitespace just before a
1200 * comment gets lumped into the comment.
1202 if (!*p || *p == ';') {
1203 type = TOK_COMMENT;
1204 while (*p)
1205 p++;
1207 } else if (*p == ';') {
1208 type = TOK_COMMENT;
1209 while (*p)
1210 p++;
1211 } else {
1213 * Anything else is an operator of some kind. We check
1214 * for all the double-character operators (>>, <<, //,
1215 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1216 * else is a single-character operator.
1218 type = TOK_OTHER;
1219 if ((p[0] == '>' && p[1] == '>') ||
1220 (p[0] == '<' && p[1] == '<') ||
1221 (p[0] == '/' && p[1] == '/') ||
1222 (p[0] == '<' && p[1] == '=') ||
1223 (p[0] == '>' && p[1] == '=') ||
1224 (p[0] == '=' && p[1] == '=') ||
1225 (p[0] == '!' && p[1] == '=') ||
1226 (p[0] == '<' && p[1] == '>') ||
1227 (p[0] == '&' && p[1] == '&') ||
1228 (p[0] == '|' && p[1] == '|') ||
1229 (p[0] == '^' && p[1] == '^')) {
1230 p++;
1232 p++;
1235 /* Handling unterminated string by UNV */
1236 /*if (type == -1)
1238 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1239 t->text[p-line] = *line;
1240 tail = &t->next;
1242 else */
1243 if (type != TOK_COMMENT) {
1244 *tail = t = new_Token(NULL, type, line, p - line);
1245 tail = &t->next;
1247 line = p;
1250 nasm_dump_token(list);
1252 return list;
1256 * this function allocates a new managed block of memory and
1257 * returns a pointer to the block. The managed blocks are
1258 * deleted only all at once by the delete_Blocks function.
1260 static void *new_Block(size_t size)
1262 Blocks *b = &blocks;
1264 /* first, get to the end of the linked list */
1265 while (b->next)
1266 b = b->next;
1268 /* now allocate the requested chunk */
1269 b->chunk = nasm_malloc(size);
1271 /* now allocate a new block for the next request */
1272 b->next = nasm_zalloc(sizeof(Blocks));
1274 return b->chunk;
1278 * this function deletes all managed blocks of memory
1280 static void delete_Blocks(void)
1282 Blocks *a, *b = &blocks;
1285 * keep in mind that the first block, pointed to by blocks
1286 * is a static and not dynamically allocated, so we don't
1287 * free it.
1289 while (b) {
1290 nasm_free(b->chunk);
1291 a = b;
1292 b = b->next;
1293 if (a != &blocks)
1294 nasm_free(a);
1299 * this function creates a new Token and passes a pointer to it
1300 * back to the caller. It sets the type and text elements, and
1301 * also the a.mac and next elements to NULL.
1303 static Token *new_Token(Token * next, enum pp_token_type type,
1304 const char *text, int txtlen)
1306 Token *t;
1307 int i;
1309 if (!freeTokens) {
1310 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1311 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1312 freeTokens[i].next = &freeTokens[i + 1];
1313 freeTokens[i].next = NULL;
1315 t = freeTokens;
1316 freeTokens = t->next;
1317 t->next = next;
1318 t->a.mac = NULL;
1319 t->type = type;
1320 if (type == TOK_WHITESPACE || !text) {
1321 t->text = NULL;
1322 } else {
1323 if (txtlen == 0)
1324 txtlen = strlen(text);
1325 t->text = nasm_malloc(txtlen+1);
1326 memcpy(t->text, text, txtlen);
1327 t->text[txtlen] = '\0';
1329 return t;
1332 static Token *copy_Token(Token * tline)
1334 Token *t, *tt, *first = NULL, *prev = NULL;
1335 int i;
1336 for (tt = tline; tt != NULL; tt = tt->next) {
1337 if (!freeTokens) {
1338 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1339 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1340 freeTokens[i].next = &freeTokens[i + 1];
1341 freeTokens[i].next = NULL;
1343 t = freeTokens;
1344 freeTokens = t->next;
1345 t->next = NULL;
1346 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1347 t->a.mac = tt->a.mac;
1348 t->a.len = tt->a.len;
1349 t->type = tt->type;
1350 if (prev != NULL) {
1351 prev->next = t;
1352 } else {
1353 first = t;
1355 prev = t;
1357 return first;
1360 static Token *delete_Token(Token * t)
1362 Token *next = t->next;
1363 nasm_free(t->text);
1364 t->next = freeTokens;
1365 freeTokens = t;
1366 return next;
1370 * Convert a line of tokens back into text.
1371 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1372 * will be transformed into ..@ctxnum.xxx
1374 static char *detoken(Token * tlist, bool expand_locals)
1376 Token *t;
1377 char *line, *p;
1378 const char *q;
1379 int len = 0;
1381 list_for_each(t, tlist) {
1382 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1383 char *v;
1384 char *q = t->text;
1386 v = t->text + 2;
1387 if (*v == '\'' || *v == '\"' || *v == '`') {
1388 size_t len = nasm_unquote(v, NULL);
1389 size_t clen = strlen(v);
1391 if (len != clen) {
1392 error(ERR_NONFATAL | ERR_PASS1,
1393 "NUL character in %! string");
1394 v = NULL;
1398 if (v) {
1399 char *p = getenv(v);
1400 if (!p) {
1401 error(ERR_NONFATAL | ERR_PASS1,
1402 "nonexistent environment variable `%s'", v);
1403 p = "";
1405 t->text = nasm_strdup(p);
1407 nasm_free(q);
1410 /* Expand local macros here and not during preprocessing */
1411 if (expand_locals &&
1412 t->type == TOK_PREPROC_ID && t->text &&
1413 t->text[0] == '%' && t->text[1] == '$') {
1414 const char *q;
1415 char *p;
1416 Context *ctx = get_ctx(t->text, &q);
1417 if (ctx) {
1418 char buffer[40];
1419 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1420 p = nasm_strcat(buffer, q);
1421 nasm_free(t->text);
1422 t->text = p;
1426 /* Expand %? and %?? directives */
1427 if ((istk->expansion != NULL) &&
1428 ((t->type == TOK_PREPROC_Q) ||
1429 (t->type == TOK_PREPROC_QQ))) {
1430 ExpInv *ei;
1431 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1432 if (ei->type == EXP_MMACRO) {
1433 nasm_free(t->text);
1434 if (t->type == TOK_PREPROC_Q) {
1435 t->text = nasm_strdup(ei->name);
1436 } else {
1437 t->text = nasm_strdup(ei->def->name);
1439 break;
1444 if (t->type == TOK_WHITESPACE)
1445 len++;
1446 else if (t->text)
1447 len += strlen(t->text);
1450 p = line = nasm_malloc(len + 1);
1452 list_for_each(t, tlist) {
1453 if (t->type == TOK_WHITESPACE) {
1454 *p++ = ' ';
1455 } else if (t->text) {
1456 q = t->text;
1457 while (*q)
1458 *p++ = *q++;
1461 *p = '\0';
1463 return line;
1467 * Initialize a new Line
1469 static inline Line *new_Line(void)
1471 return (Line *)nasm_zalloc(sizeof(Line));
1476 * Initialize a new Expansion Definition
1478 static ExpDef *new_ExpDef(int exp_type)
1480 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1481 ed->type = exp_type;
1482 ed->casesense = true;
1483 ed->state = COND_NEVER;
1485 return ed;
1490 * Initialize a new Expansion Instance
1492 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1494 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1495 ei->type = exp_type;
1496 ei->def = ed;
1497 ei->unique = ++unique;
1499 if ((istk->mmac_depth < 1) &&
1500 (istk->expansion == NULL) &&
1501 (ed != NULL) &&
1502 (ed->type != EXP_MMACRO) &&
1503 (ed->type != EXP_REP) &&
1504 (ed->type != EXP_WHILE)) {
1505 ei->linnum = src_get_linnum();
1506 src_set_linnum(ei->linnum - ed->linecount - 1);
1507 } else {
1508 ei->linnum = -1;
1510 if ((istk->expansion == NULL) ||
1511 (ei->type == EXP_MMACRO)) {
1512 ei->relno = 0;
1513 } else {
1514 ei->relno = istk->expansion->lineno;
1515 if (ed != NULL) {
1516 ei->relno -= (ed->linecount + 1);
1519 return ei;
1523 * A scanner, suitable for use by the expression evaluator, which
1524 * operates on a line of Tokens. Expects a pointer to a pointer to
1525 * the first token in the line to be passed in as its private_data
1526 * field.
1528 * FIX: This really needs to be unified with stdscan.
1530 static int ppscan(void *private_data, struct tokenval *tokval)
1532 Token **tlineptr = private_data;
1533 Token *tline;
1534 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1536 do {
1537 tline = *tlineptr;
1538 *tlineptr = tline ? tline->next : NULL;
1539 } while (tline && (tline->type == TOK_WHITESPACE ||
1540 tline->type == TOK_COMMENT));
1542 if (!tline)
1543 return tokval->t_type = TOKEN_EOS;
1545 tokval->t_charptr = tline->text;
1547 if (tline->text[0] == '$' && !tline->text[1])
1548 return tokval->t_type = TOKEN_HERE;
1549 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1550 return tokval->t_type = TOKEN_BASE;
1552 if (tline->type == TOK_ID) {
1553 p = tokval->t_charptr = tline->text;
1554 if (p[0] == '$') {
1555 tokval->t_charptr++;
1556 return tokval->t_type = TOKEN_ID;
1559 for (r = p, s = ourcopy; *r; r++) {
1560 if (r >= p+MAX_KEYWORD)
1561 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1562 *s++ = nasm_tolower(*r);
1564 *s = '\0';
1565 /* right, so we have an identifier sitting in temp storage. now,
1566 * is it actually a register or instruction name, or what? */
1567 return nasm_token_hash(ourcopy, tokval);
1570 if (tline->type == TOK_NUMBER) {
1571 bool rn_error;
1572 tokval->t_integer = readnum(tline->text, &rn_error);
1573 tokval->t_charptr = tline->text;
1574 if (rn_error)
1575 return tokval->t_type = TOKEN_ERRNUM;
1576 else
1577 return tokval->t_type = TOKEN_NUM;
1580 if (tline->type == TOK_FLOAT) {
1581 return tokval->t_type = TOKEN_FLOAT;
1584 if (tline->type == TOK_STRING) {
1585 char bq, *ep;
1587 bq = tline->text[0];
1588 tokval->t_charptr = tline->text;
1589 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1591 if (ep[0] != bq || ep[1] != '\0')
1592 return tokval->t_type = TOKEN_ERRSTR;
1593 else
1594 return tokval->t_type = TOKEN_STR;
1597 if (tline->type == TOK_OTHER) {
1598 if (!strcmp(tline->text, "<<"))
1599 return tokval->t_type = TOKEN_SHL;
1600 if (!strcmp(tline->text, ">>"))
1601 return tokval->t_type = TOKEN_SHR;
1602 if (!strcmp(tline->text, "//"))
1603 return tokval->t_type = TOKEN_SDIV;
1604 if (!strcmp(tline->text, "%%"))
1605 return tokval->t_type = TOKEN_SMOD;
1606 if (!strcmp(tline->text, "=="))
1607 return tokval->t_type = TOKEN_EQ;
1608 if (!strcmp(tline->text, "<>"))
1609 return tokval->t_type = TOKEN_NE;
1610 if (!strcmp(tline->text, "!="))
1611 return tokval->t_type = TOKEN_NE;
1612 if (!strcmp(tline->text, "<="))
1613 return tokval->t_type = TOKEN_LE;
1614 if (!strcmp(tline->text, ">="))
1615 return tokval->t_type = TOKEN_GE;
1616 if (!strcmp(tline->text, "&&"))
1617 return tokval->t_type = TOKEN_DBL_AND;
1618 if (!strcmp(tline->text, "^^"))
1619 return tokval->t_type = TOKEN_DBL_XOR;
1620 if (!strcmp(tline->text, "||"))
1621 return tokval->t_type = TOKEN_DBL_OR;
1625 * We have no other options: just return the first character of
1626 * the token text.
1628 return tokval->t_type = tline->text[0];
1632 * Compare a string to the name of an existing macro; this is a
1633 * simple wrapper which calls either strcmp or nasm_stricmp
1634 * depending on the value of the `casesense' parameter.
1636 static int mstrcmp(const char *p, const char *q, bool casesense)
1638 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1642 * Compare a string to the name of an existing macro; this is a
1643 * simple wrapper which calls either strcmp or nasm_stricmp
1644 * depending on the value of the `casesense' parameter.
1646 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1648 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1652 * Return the Context structure associated with a %$ token. Return
1653 * NULL, having _already_ reported an error condition, if the
1654 * context stack isn't deep enough for the supplied number of $
1655 * signs.
1657 * If "namep" is non-NULL, set it to the pointer to the macro name
1658 * tail, i.e. the part beyond %$...
1660 static Context *get_ctx(const char *name, const char **namep)
1662 Context *ctx;
1663 int i;
1665 if (namep)
1666 *namep = name;
1668 if (!name || name[0] != '%' || name[1] != '$')
1669 return NULL;
1671 if (!cstk) {
1672 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1673 return NULL;
1676 name += 2;
1677 ctx = cstk;
1678 i = 0;
1679 while (ctx && *name == '$') {
1680 name++;
1681 i++;
1682 ctx = ctx->next;
1685 if (!ctx) {
1686 error(ERR_NONFATAL, "`%s': context stack is only"
1687 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1688 return NULL;
1691 if (namep)
1692 *namep = name;
1694 return ctx;
1698 * Check to see if a file is already in a string list
1700 static bool in_list(const StrList *list, const char *str)
1702 while (list) {
1703 if (!strcmp(list->str, str))
1704 return true;
1705 list = list->next;
1707 return false;
1711 * Open an include file. This routine must always return a valid
1712 * file pointer if it returns - it's responsible for throwing an
1713 * ERR_FATAL and bombing out completely if not. It should also try
1714 * the include path one by one until it finds the file or reaches
1715 * the end of the path.
1717 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1718 bool missing_ok)
1720 FILE *fp;
1721 char *prefix = "";
1722 IncPath *ip = ipath;
1723 int len = strlen(file);
1724 size_t prefix_len = 0;
1725 StrList *sl;
1727 while (1) {
1728 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1729 sl->next = NULL;
1730 memcpy(sl->str, prefix, prefix_len);
1731 memcpy(sl->str+prefix_len, file, len+1);
1732 fp = fopen(sl->str, "r");
1733 if (fp && dhead && !in_list(*dhead, sl->str)) {
1734 **dtail = sl;
1735 *dtail = &sl->next;
1736 } else {
1737 nasm_free(sl);
1739 if (fp)
1740 return fp;
1741 if (!ip) {
1742 if (!missing_ok)
1743 break;
1744 prefix = NULL;
1745 } else {
1746 prefix = ip->path;
1747 ip = ip->next;
1749 if (prefix) {
1750 prefix_len = strlen(prefix);
1751 } else {
1752 /* -MG given and file not found */
1753 if (dhead && !in_list(*dhead, file)) {
1754 sl = nasm_malloc(len+1+sizeof sl->next);
1755 sl->next = NULL;
1756 strcpy(sl->str, file);
1757 **dtail = sl;
1758 *dtail = &sl->next;
1760 return NULL;
1764 error(ERR_FATAL, "unable to open include file `%s'", file);
1765 return NULL;
1769 * Determine if we should warn on defining a single-line macro of
1770 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1771 * return true if _any_ single-line macro of that name is defined.
1772 * Otherwise, will return true if a single-line macro with either
1773 * `nparam' or no parameters is defined.
1775 * If a macro with precisely the right number of parameters is
1776 * defined, or nparam is -1, the address of the definition structure
1777 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1778 * is NULL, no action will be taken regarding its contents, and no
1779 * error will occur.
1781 * Note that this is also called with nparam zero to resolve
1782 * `ifdef'.
1784 * If you already know which context macro belongs to, you can pass
1785 * the context pointer as first parameter; if you won't but name begins
1786 * with %$ the context will be automatically computed. If all_contexts
1787 * is true, macro will be searched in outer contexts as well.
1789 static bool
1790 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1791 bool nocase)
1793 struct hash_table *smtbl;
1794 SMacro *m;
1796 if (ctx) {
1797 smtbl = &ctx->localmac;
1798 } else if (name[0] == '%' && name[1] == '$') {
1799 if (cstk)
1800 ctx = get_ctx(name, &name);
1801 if (!ctx)
1802 return false; /* got to return _something_ */
1803 smtbl = &ctx->localmac;
1804 } else {
1805 smtbl = &smacros;
1807 m = (SMacro *) hash_findix(smtbl, name);
1809 while (m) {
1810 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1811 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1812 if (defn) {
1813 if (nparam == (int) m->nparam || nparam == -1)
1814 *defn = m;
1815 else
1816 *defn = NULL;
1818 return true;
1820 m = m->next;
1823 return false;
1827 * Count and mark off the parameters in a multi-line macro call.
1828 * This is called both from within the multi-line macro expansion
1829 * code, and also to mark off the default parameters when provided
1830 * in a %macro definition line.
1832 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1834 int paramsize, brace;
1836 *nparam = paramsize = 0;
1837 *params = NULL;
1838 while (t) {
1839 /* +1: we need space for the final NULL */
1840 if (*nparam+1 >= paramsize) {
1841 paramsize += PARAM_DELTA;
1842 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1844 skip_white_(t);
1845 brace = false;
1846 if (tok_is_(t, "{"))
1847 brace = true;
1848 (*params)[(*nparam)++] = t;
1849 while (tok_isnt_(t, brace ? "}" : ","))
1850 t = t->next;
1851 if (t) { /* got a comma/brace */
1852 t = t->next;
1853 if (brace) {
1855 * Now we've found the closing brace, look further
1856 * for the comma.
1858 skip_white_(t);
1859 if (tok_isnt_(t, ",")) {
1860 error(ERR_NONFATAL,
1861 "braces do not enclose all of macro parameter");
1862 while (tok_isnt_(t, ","))
1863 t = t->next;
1865 if (t)
1866 t = t->next; /* eat the comma */
1873 * Determine whether one of the various `if' conditions is true or
1874 * not.
1876 * We must free the tline we get passed.
1878 static bool if_condition(Token * tline, enum preproc_token ct)
1880 enum pp_conditional i = PP_COND(ct);
1881 bool j;
1882 Token *t, *tt, **tptr, *origline;
1883 struct tokenval tokval;
1884 expr *evalresult;
1885 enum pp_token_type needtype;
1886 char *p;
1888 origline = tline;
1890 switch (i) {
1891 case PPC_IFCTX:
1892 j = false; /* have we matched yet? */
1893 while (true) {
1894 skip_white_(tline);
1895 if (!tline)
1896 break;
1897 if (tline->type != TOK_ID) {
1898 error(ERR_NONFATAL,
1899 "`%s' expects context identifiers", pp_directives[ct]);
1900 free_tlist(origline);
1901 return -1;
1903 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1904 j = true;
1905 tline = tline->next;
1907 break;
1909 case PPC_IFDEF:
1910 j = false; /* have we matched yet? */
1911 while (tline) {
1912 skip_white_(tline);
1913 if (!tline || (tline->type != TOK_ID &&
1914 (tline->type != TOK_PREPROC_ID ||
1915 tline->text[1] != '$'))) {
1916 error(ERR_NONFATAL,
1917 "`%s' expects macro identifiers", pp_directives[ct]);
1918 goto fail;
1920 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1921 j = true;
1922 tline = tline->next;
1924 break;
1926 case PPC_IFENV:
1927 tline = expand_smacro(tline);
1928 j = false; /* have we matched yet? */
1929 while (tline) {
1930 skip_white_(tline);
1931 if (!tline || (tline->type != TOK_ID &&
1932 tline->type != TOK_STRING &&
1933 (tline->type != TOK_PREPROC_ID ||
1934 tline->text[1] != '!'))) {
1935 error(ERR_NONFATAL,
1936 "`%s' expects environment variable names",
1937 pp_directives[ct]);
1938 goto fail;
1940 p = tline->text;
1941 if (tline->type == TOK_PREPROC_ID)
1942 p += 2; /* Skip leading %! */
1943 if (*p == '\'' || *p == '\"' || *p == '`')
1944 nasm_unquote_cstr(p, ct);
1945 if (getenv(p))
1946 j = true;
1947 tline = tline->next;
1949 break;
1951 case PPC_IFIDN:
1952 case PPC_IFIDNI:
1953 tline = expand_smacro(tline);
1954 t = tt = tline;
1955 while (tok_isnt_(tt, ","))
1956 tt = tt->next;
1957 if (!tt) {
1958 error(ERR_NONFATAL,
1959 "`%s' expects two comma-separated arguments",
1960 pp_directives[ct]);
1961 goto fail;
1963 tt = tt->next;
1964 j = true; /* assume equality unless proved not */
1965 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1966 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1967 error(ERR_NONFATAL, "`%s': more than one comma on line",
1968 pp_directives[ct]);
1969 goto fail;
1971 if (t->type == TOK_WHITESPACE) {
1972 t = t->next;
1973 continue;
1975 if (tt->type == TOK_WHITESPACE) {
1976 tt = tt->next;
1977 continue;
1979 if (tt->type != t->type) {
1980 j = false; /* found mismatching tokens */
1981 break;
1983 /* When comparing strings, need to unquote them first */
1984 if (t->type == TOK_STRING) {
1985 size_t l1 = nasm_unquote(t->text, NULL);
1986 size_t l2 = nasm_unquote(tt->text, NULL);
1988 if (l1 != l2) {
1989 j = false;
1990 break;
1992 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1993 j = false;
1994 break;
1996 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1997 j = false; /* found mismatching tokens */
1998 break;
2001 t = t->next;
2002 tt = tt->next;
2004 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
2005 j = false; /* trailing gunk on one end or other */
2006 break;
2008 case PPC_IFMACRO:
2010 bool found = false;
2011 ExpDef searching, *ed;
2013 skip_white_(tline);
2014 tline = expand_id(tline);
2015 if (!tok_type_(tline, TOK_ID)) {
2016 error(ERR_NONFATAL,
2017 "`%s' expects a macro name", pp_directives[ct]);
2018 goto fail;
2020 memset(&searching, 0, sizeof(searching));
2021 searching.name = nasm_strdup(tline->text);
2022 searching.casesense = true;
2023 searching.nparam_max = INT_MAX;
2024 tline = expand_smacro(tline->next);
2025 skip_white_(tline);
2026 if (!tline) {
2027 } else if (!tok_type_(tline, TOK_NUMBER)) {
2028 error(ERR_NONFATAL,
2029 "`%s' expects a parameter count or nothing",
2030 pp_directives[ct]);
2031 } else {
2032 searching.nparam_min = searching.nparam_max =
2033 readnum(tline->text, &j);
2034 if (j)
2035 error(ERR_NONFATAL,
2036 "unable to parse parameter count `%s'",
2037 tline->text);
2039 if (tline && tok_is_(tline->next, "-")) {
2040 tline = tline->next->next;
2041 if (tok_is_(tline, "*"))
2042 searching.nparam_max = INT_MAX;
2043 else if (!tok_type_(tline, TOK_NUMBER))
2044 error(ERR_NONFATAL,
2045 "`%s' expects a parameter count after `-'",
2046 pp_directives[ct]);
2047 else {
2048 searching.nparam_max = readnum(tline->text, &j);
2049 if (j)
2050 error(ERR_NONFATAL,
2051 "unable to parse parameter count `%s'",
2052 tline->text);
2053 if (searching.nparam_min > searching.nparam_max)
2054 error(ERR_NONFATAL,
2055 "minimum parameter count exceeds maximum");
2058 if (tline && tok_is_(tline->next, "+")) {
2059 tline = tline->next;
2060 searching.plus = true;
2062 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2063 while (ed != NULL) {
2064 if (!strcmp(ed->name, searching.name) &&
2065 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2066 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
2067 found = true;
2068 break;
2070 ed = ed->next;
2072 if (tline && tline->next)
2073 error(ERR_WARNING|ERR_PASS1,
2074 "trailing garbage after %%ifmacro ignored");
2075 nasm_free(searching.name);
2076 j = found;
2077 break;
2080 case PPC_IFID:
2081 needtype = TOK_ID;
2082 goto iftype;
2083 case PPC_IFNUM:
2084 needtype = TOK_NUMBER;
2085 goto iftype;
2086 case PPC_IFSTR:
2087 needtype = TOK_STRING;
2088 goto iftype;
2090 iftype:
2091 t = tline = expand_smacro(tline);
2093 while (tok_type_(t, TOK_WHITESPACE) ||
2094 (needtype == TOK_NUMBER &&
2095 tok_type_(t, TOK_OTHER) &&
2096 (t->text[0] == '-' || t->text[0] == '+') &&
2097 !t->text[1]))
2098 t = t->next;
2100 j = tok_type_(t, needtype);
2101 break;
2103 case PPC_IFTOKEN:
2104 t = tline = expand_smacro(tline);
2105 while (tok_type_(t, TOK_WHITESPACE))
2106 t = t->next;
2108 j = false;
2109 if (t) {
2110 t = t->next; /* Skip the actual token */
2111 while (tok_type_(t, TOK_WHITESPACE))
2112 t = t->next;
2113 j = !t; /* Should be nothing left */
2115 break;
2117 case PPC_IFEMPTY:
2118 t = tline = expand_smacro(tline);
2119 while (tok_type_(t, TOK_WHITESPACE))
2120 t = t->next;
2122 j = !t; /* Should be empty */
2123 break;
2125 case PPC_IF:
2126 t = tline = expand_smacro(tline);
2127 tptr = &t;
2128 tokval.t_type = TOKEN_INVALID;
2129 evalresult = evaluate(ppscan, tptr, &tokval,
2130 NULL, pass | CRITICAL, error, NULL);
2131 if (!evalresult)
2132 return -1;
2133 if (tokval.t_type)
2134 error(ERR_WARNING|ERR_PASS1,
2135 "trailing garbage after expression ignored");
2136 if (!is_simple(evalresult)) {
2137 error(ERR_NONFATAL,
2138 "non-constant value given to `%s'", pp_directives[ct]);
2139 goto fail;
2141 j = reloc_value(evalresult) != 0;
2142 break;
2144 default:
2145 error(ERR_FATAL,
2146 "preprocessor directive `%s' not yet implemented",
2147 pp_directives[ct]);
2148 goto fail;
2151 free_tlist(origline);
2152 return j ^ PP_NEGATIVE(ct);
2154 fail:
2155 free_tlist(origline);
2156 return -1;
2160 * Common code for defining an smacro
2162 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2163 int nparam, Token *expansion)
2165 SMacro *smac, **smhead;
2166 struct hash_table *smtbl;
2168 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2169 if (!smac) {
2170 error(ERR_WARNING|ERR_PASS1,
2171 "single-line macro `%s' defined both with and"
2172 " without parameters", mname);
2174 * Some instances of the old code considered this a failure,
2175 * some others didn't. What is the right thing to do here?
2177 free_tlist(expansion);
2178 return false; /* Failure */
2179 } else {
2181 * We're redefining, so we have to take over an
2182 * existing SMacro structure. This means freeing
2183 * what was already in it.
2185 nasm_free(smac->name);
2186 free_tlist(smac->expansion);
2188 } else {
2189 smtbl = ctx ? &ctx->localmac : &smacros;
2190 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2191 smac = nasm_zalloc(sizeof(SMacro));
2192 smac->next = *smhead;
2193 *smhead = smac;
2195 smac->name = nasm_strdup(mname);
2196 smac->casesense = casesense;
2197 smac->nparam = nparam;
2198 smac->expansion = expansion;
2199 smac->in_progress = false;
2200 return true; /* Success */
2204 * Undefine an smacro
2206 static void undef_smacro(Context *ctx, const char *mname)
2208 SMacro **smhead, *s, **sp;
2209 struct hash_table *smtbl;
2211 smtbl = ctx ? &ctx->localmac : &smacros;
2212 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2214 if (smhead) {
2216 * We now have a macro name... go hunt for it.
2218 sp = smhead;
2219 while ((s = *sp) != NULL) {
2220 if (!mstrcmp(s->name, mname, s->casesense)) {
2221 *sp = s->next;
2222 nasm_free(s->name);
2223 free_tlist(s->expansion);
2224 nasm_free(s);
2225 } else {
2226 sp = &s->next;
2233 * Parse a mmacro specification.
2235 static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
2237 bool err;
2239 tline = tline->next;
2240 skip_white_(tline);
2241 tline = expand_id(tline);
2242 if (!tok_type_(tline, TOK_ID)) {
2243 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2244 return false;
2247 def->name = nasm_strdup(tline->text);
2248 def->plus = false;
2249 def->nolist = false;
2250 // def->in_progress = 0;
2251 // def->rep_nest = NULL;
2252 def->nparam_min = 0;
2253 def->nparam_max = 0;
2255 tline = expand_smacro(tline->next);
2256 skip_white_(tline);
2257 if (!tok_type_(tline, TOK_NUMBER)) {
2258 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2259 } else {
2260 def->nparam_min = def->nparam_max =
2261 readnum(tline->text, &err);
2262 if (err)
2263 error(ERR_NONFATAL,
2264 "unable to parse parameter count `%s'", tline->text);
2266 if (tline && tok_is_(tline->next, "-")) {
2267 tline = tline->next->next;
2268 if (tok_is_(tline, "*")) {
2269 def->nparam_max = INT_MAX;
2270 } else if (!tok_type_(tline, TOK_NUMBER)) {
2271 error(ERR_NONFATAL,
2272 "`%s' expects a parameter count after `-'", directive);
2273 } else {
2274 def->nparam_max = readnum(tline->text, &err);
2275 if (err) {
2276 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2277 tline->text);
2279 if (def->nparam_min > def->nparam_max) {
2280 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2284 if (tline && tok_is_(tline->next, "+")) {
2285 tline = tline->next;
2286 def->plus = true;
2288 if (tline && tok_type_(tline->next, TOK_ID) &&
2289 !nasm_stricmp(tline->next->text, ".nolist")) {
2290 tline = tline->next;
2291 def->nolist = true;
2295 * Handle default parameters.
2297 if (tline && tline->next) {
2298 def->dlist = tline->next;
2299 tline->next = NULL;
2300 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2301 } else {
2302 def->dlist = NULL;
2303 def->defaults = NULL;
2305 def->line = NULL;
2307 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2308 !def->plus)
2309 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2310 "too many default macro parameters");
2312 return true;
2317 * Decode a size directive
2319 static int parse_size(const char *str) {
2320 static const char *size_names[] =
2321 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2322 static const int sizes[] =
2323 { 0, 1, 4, 16, 8, 10, 2, 32 };
2325 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2329 * find and process preprocessor directive in passed line
2330 * Find out if a line contains a preprocessor directive, and deal
2331 * with it if so.
2333 * If a directive _is_ found, it is the responsibility of this routine
2334 * (and not the caller) to free_tlist() the line.
2336 * @param tline a pointer to the current tokeninzed line linked list
2337 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2340 static int do_directive(Token * tline)
2342 enum preproc_token i;
2343 int j;
2344 bool err;
2345 int nparam;
2346 bool nolist;
2347 bool casesense;
2348 int k, m;
2349 int offset;
2350 char *p, *pp;
2351 const char *mname;
2352 Include *inc;
2353 Context *ctx;
2354 Line *l;
2355 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2356 struct tokenval tokval;
2357 expr *evalresult;
2358 ExpDef *ed, *eed, **edhead;
2359 ExpInv *ei, *eei;
2360 int64_t count;
2361 size_t len;
2362 int severity;
2364 origline = tline;
2366 skip_white_(tline);
2367 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2368 (tline->text[1] == '%' || tline->text[1] == '$'
2369 || tline->text[1] == '!'))
2370 return NO_DIRECTIVE_FOUND;
2372 i = pp_token_hash(tline->text);
2374 switch (i) {
2375 case PP_INVALID:
2376 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2377 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2378 tline->text);
2379 return NO_DIRECTIVE_FOUND; /* didn't get it */
2381 case PP_STACKSIZE:
2382 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2383 /* Directive to tell NASM what the default stack size is. The
2384 * default is for a 16-bit stack, and this can be overriden with
2385 * %stacksize large.
2387 tline = tline->next;
2388 if (tline && tline->type == TOK_WHITESPACE)
2389 tline = tline->next;
2390 if (!tline || tline->type != TOK_ID) {
2391 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
2395 if (nasm_stricmp(tline->text, "flat") == 0) {
2396 /* All subsequent ARG directives are for a 32-bit stack */
2397 StackSize = 4;
2398 StackPointer = "ebp";
2399 ArgOffset = 8;
2400 LocalOffset = 0;
2401 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2402 /* All subsequent ARG directives are for a 64-bit stack */
2403 StackSize = 8;
2404 StackPointer = "rbp";
2405 ArgOffset = 16;
2406 LocalOffset = 0;
2407 } else if (nasm_stricmp(tline->text, "large") == 0) {
2408 /* All subsequent ARG directives are for a 16-bit stack,
2409 * far function call.
2411 StackSize = 2;
2412 StackPointer = "bp";
2413 ArgOffset = 4;
2414 LocalOffset = 0;
2415 } else if (nasm_stricmp(tline->text, "small") == 0) {
2416 /* All subsequent ARG directives are for a 16-bit stack,
2417 * far function call. We don't support near functions.
2419 StackSize = 2;
2420 StackPointer = "bp";
2421 ArgOffset = 6;
2422 LocalOffset = 0;
2423 } else {
2424 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2431 case PP_ARG:
2432 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2433 /* TASM like ARG directive to define arguments to functions, in
2434 * the following form:
2436 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2438 offset = ArgOffset;
2439 do {
2440 char *arg, directive[256];
2441 int size = StackSize;
2443 /* Find the argument name */
2444 tline = tline->next;
2445 if (tline && tline->type == TOK_WHITESPACE)
2446 tline = tline->next;
2447 if (!tline || tline->type != TOK_ID) {
2448 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2452 arg = tline->text;
2454 /* Find the argument size type */
2455 tline = tline->next;
2456 if (!tline || tline->type != TOK_OTHER
2457 || tline->text[0] != ':') {
2458 error(ERR_NONFATAL,
2459 "Syntax error processing `%%arg' directive");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2463 tline = tline->next;
2464 if (!tline || tline->type != TOK_ID) {
2465 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2470 /* Allow macro expansion of type parameter */
2471 tt = tokenize(tline->text);
2472 tt = expand_smacro(tt);
2473 size = parse_size(tt->text);
2474 if (!size) {
2475 error(ERR_NONFATAL,
2476 "Invalid size type for `%%arg' missing directive");
2477 free_tlist(tt);
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2481 free_tlist(tt);
2483 /* Round up to even stack slots */
2484 size = ALIGN(size, StackSize);
2486 /* Now define the macro for the argument */
2487 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2488 arg, StackPointer, offset);
2489 do_directive(tokenize(directive));
2490 offset += size;
2492 /* Move to the next argument in the list */
2493 tline = tline->next;
2494 if (tline && tline->type == TOK_WHITESPACE)
2495 tline = tline->next;
2496 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2497 ArgOffset = offset;
2498 free_tlist(origline);
2499 return DIRECTIVE_FOUND;
2501 case PP_LOCAL:
2502 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2503 /* TASM like LOCAL directive to define local variables for a
2504 * function, in the following form:
2506 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2508 * The '= LocalSize' at the end is ignored by NASM, but is
2509 * required by TASM to define the local parameter size (and used
2510 * by the TASM macro package).
2512 offset = LocalOffset;
2513 do {
2514 char *local, directive[256];
2515 int size = StackSize;
2517 /* Find the argument name */
2518 tline = tline->next;
2519 if (tline && tline->type == TOK_WHITESPACE)
2520 tline = tline->next;
2521 if (!tline || tline->type != TOK_ID) {
2522 error(ERR_NONFATAL,
2523 "`%%local' missing argument parameter");
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
2527 local = tline->text;
2529 /* Find the argument size type */
2530 tline = tline->next;
2531 if (!tline || tline->type != TOK_OTHER
2532 || tline->text[0] != ':') {
2533 error(ERR_NONFATAL,
2534 "Syntax error processing `%%local' directive");
2535 free_tlist(origline);
2536 return DIRECTIVE_FOUND;
2538 tline = tline->next;
2539 if (!tline || tline->type != TOK_ID) {
2540 error(ERR_NONFATAL,
2541 "`%%local' missing size type parameter");
2542 free_tlist(origline);
2543 return DIRECTIVE_FOUND;
2546 /* Allow macro expansion of type parameter */
2547 tt = tokenize(tline->text);
2548 tt = expand_smacro(tt);
2549 size = parse_size(tt->text);
2550 if (!size) {
2551 error(ERR_NONFATAL,
2552 "Invalid size type for `%%local' missing directive");
2553 free_tlist(tt);
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2557 free_tlist(tt);
2559 /* Round up to even stack slots */
2560 size = ALIGN(size, StackSize);
2562 offset += size; /* Negative offset, increment before */
2564 /* Now define the macro for the argument */
2565 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2566 local, StackPointer, offset);
2567 do_directive(tokenize(directive));
2569 /* Now define the assign to setup the enter_c macro correctly */
2570 snprintf(directive, sizeof(directive),
2571 "%%assign %%$localsize %%$localsize+%d", size);
2572 do_directive(tokenize(directive));
2574 /* Move to the next argument in the list */
2575 tline = tline->next;
2576 if (tline && tline->type == TOK_WHITESPACE)
2577 tline = tline->next;
2578 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2579 LocalOffset = offset;
2580 free_tlist(origline);
2581 return DIRECTIVE_FOUND;
2583 case PP_CLEAR:
2584 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2585 if (tline->next)
2586 error(ERR_WARNING|ERR_PASS1,
2587 "trailing garbage after `%%clear' ignored");
2588 free_macros();
2589 init_macros();
2590 free_tlist(origline);
2591 return DIRECTIVE_FOUND;
2593 case PP_DEPEND:
2594 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2595 t = tline->next = expand_smacro(tline->next);
2596 skip_white_(t);
2597 if (!t || (t->type != TOK_STRING &&
2598 t->type != TOK_INTERNAL_STRING)) {
2599 error(ERR_NONFATAL, "`%%depend' expects a file name");
2600 free_tlist(origline);
2601 return DIRECTIVE_FOUND; /* but we did _something_ */
2603 if (t->next)
2604 error(ERR_WARNING|ERR_PASS1,
2605 "trailing garbage after `%%depend' ignored");
2606 p = t->text;
2607 if (t->type != TOK_INTERNAL_STRING)
2608 nasm_unquote_cstr(p, i);
2609 if (dephead && !in_list(*dephead, p)) {
2610 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2611 sl->next = NULL;
2612 strcpy(sl->str, p);
2613 *deptail = sl;
2614 deptail = &sl->next;
2616 free_tlist(origline);
2617 return DIRECTIVE_FOUND;
2619 case PP_INCLUDE:
2620 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2621 t = tline->next = expand_smacro(tline->next);
2622 skip_white_(t);
2624 if (!t || (t->type != TOK_STRING &&
2625 t->type != TOK_INTERNAL_STRING)) {
2626 error(ERR_NONFATAL, "`%%include' expects a file name");
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND; /* but we did _something_ */
2630 if (t->next)
2631 error(ERR_WARNING|ERR_PASS1,
2632 "trailing garbage after `%%include' ignored");
2633 p = t->text;
2634 if (t->type != TOK_INTERNAL_STRING)
2635 nasm_unquote_cstr(p, i);
2636 inc = nasm_zalloc(sizeof(Include));
2637 inc->next = istk;
2638 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2639 if (!inc->fp) {
2640 /* -MG given but file not found */
2641 nasm_free(inc);
2642 } else {
2643 inc->fname = src_set_fname(nasm_strdup(p));
2644 inc->lineno = src_set_linnum(0);
2645 inc->lineinc = 1;
2646 inc->expansion = NULL;
2647 istk = inc;
2648 list->uplevel(LIST_INCLUDE);
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2653 case PP_USE:
2654 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2656 static macros_t *use_pkg;
2657 const char *pkg_macro = NULL;
2659 tline = tline->next;
2660 skip_white_(tline);
2661 tline = expand_id(tline);
2663 if (!tline || (tline->type != TOK_STRING &&
2664 tline->type != TOK_INTERNAL_STRING &&
2665 tline->type != TOK_ID)) {
2666 error(ERR_NONFATAL, "`%%use' expects a package name");
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND; /* but we did _something_ */
2670 if (tline->next)
2671 error(ERR_WARNING|ERR_PASS1,
2672 "trailing garbage after `%%use' ignored");
2673 if (tline->type == TOK_STRING)
2674 nasm_unquote_cstr(tline->text, i);
2675 use_pkg = nasm_stdmac_find_package(tline->text);
2676 if (!use_pkg)
2677 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2678 else
2679 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2680 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2681 /* Not already included, go ahead and include it */
2682 stdmacpos = use_pkg;
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND;
2687 case PP_PUSH:
2688 case PP_REPL:
2689 case PP_POP:
2690 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2691 tline = tline->next;
2692 skip_white_(tline);
2693 tline = expand_id(tline);
2694 if (tline) {
2695 if (!tok_type_(tline, TOK_ID)) {
2696 error(ERR_NONFATAL, "`%s' expects a context identifier",
2697 pp_directives[i]);
2698 free_tlist(origline);
2699 return DIRECTIVE_FOUND; /* but we did _something_ */
2701 if (tline->next)
2702 error(ERR_WARNING|ERR_PASS1,
2703 "trailing garbage after `%s' ignored",
2704 pp_directives[i]);
2705 p = nasm_strdup(tline->text);
2706 } else {
2707 p = NULL; /* Anonymous */
2710 if (i == PP_PUSH) {
2711 ctx = nasm_zalloc(sizeof(Context));
2712 ctx->next = cstk;
2713 hash_init(&ctx->localmac, HASH_SMALL);
2714 ctx->name = p;
2715 ctx->number = unique++;
2716 cstk = ctx;
2717 } else {
2718 /* %pop or %repl */
2719 if (!cstk) {
2720 error(ERR_NONFATAL, "`%s': context stack is empty",
2721 pp_directives[i]);
2722 } else if (i == PP_POP) {
2723 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2724 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2725 "expected %s",
2726 cstk->name ? cstk->name : "anonymous", p);
2727 else
2728 ctx_pop();
2729 } else {
2730 /* i == PP_REPL */
2731 nasm_free(cstk->name);
2732 cstk->name = p;
2733 p = NULL;
2735 nasm_free(p);
2737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
2739 case PP_FATAL:
2740 severity = ERR_FATAL;
2741 goto issue_error;
2742 case PP_ERROR:
2743 severity = ERR_NONFATAL;
2744 goto issue_error;
2745 case PP_WARNING:
2746 severity = ERR_WARNING|ERR_WARN_USER;
2747 goto issue_error;
2749 issue_error:
2750 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2752 /* Only error out if this is the final pass */
2753 if (pass != 2 && i != PP_FATAL)
2754 return DIRECTIVE_FOUND;
2756 tline->next = expand_smacro(tline->next);
2757 tline = tline->next;
2758 skip_white_(tline);
2759 t = tline ? tline->next : NULL;
2760 skip_white_(t);
2761 if (tok_type_(tline, TOK_STRING) && !t) {
2762 /* The line contains only a quoted string */
2763 p = tline->text;
2764 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2765 error(severity, "%s", p);
2766 } else {
2767 /* Not a quoted string, or more than a quoted string */
2768 p = detoken(tline, false);
2769 error(severity, "%s", p);
2770 nasm_free(p);
2772 free_tlist(origline);
2773 return DIRECTIVE_FOUND;
2776 CASE_PP_IF:
2777 if (defining != NULL) {
2778 if (defining->type == EXP_IF) {
2779 defining->def_depth ++;
2781 return NO_DIRECTIVE_FOUND;
2783 if ((istk->expansion != NULL) &&
2784 (istk->expansion->emitting == false)) {
2785 j = COND_NEVER;
2786 } else {
2787 j = if_condition(tline->next, i);
2788 tline->next = NULL; /* it got freed */
2789 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2791 ed = new_ExpDef(EXP_IF);
2792 ed->state = j;
2793 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2794 ed->prev = defining;
2795 defining = ed;
2796 free_tlist(origline);
2797 return DIRECTIVE_FOUND;
2799 CASE_PP_ELIF:
2800 if (defining != NULL) {
2801 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2802 return NO_DIRECTIVE_FOUND;
2805 if ((defining == NULL) || (defining->type != EXP_IF)) {
2806 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2808 switch (defining->state) {
2809 case COND_IF_TRUE:
2810 defining->state = COND_DONE;
2811 defining->ignoring = true;
2812 break;
2814 case COND_DONE:
2815 case COND_NEVER:
2816 defining->ignoring = true;
2817 break;
2819 case COND_ELSE_TRUE:
2820 case COND_ELSE_FALSE:
2821 error_precond(ERR_WARNING|ERR_PASS1,
2822 "`%%elif' after `%%else' ignored");
2823 defining->state = COND_NEVER;
2824 defining->ignoring = true;
2825 break;
2827 case COND_IF_FALSE:
2829 * IMPORTANT: In the case of %if, we will already have
2830 * called expand_mmac_params(); however, if we're
2831 * processing an %elif we must have been in a
2832 * non-emitting mode, which would have inhibited
2833 * the normal invocation of expand_mmac_params().
2834 * Therefore, we have to do it explicitly here.
2836 j = if_condition(expand_mmac_params(tline->next), i);
2837 tline->next = NULL; /* it got freed */
2838 defining->state =
2839 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2840 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
2841 break;
2843 free_tlist(origline);
2844 return DIRECTIVE_FOUND;
2846 case PP_ELSE:
2847 if (defining != NULL) {
2848 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2849 return NO_DIRECTIVE_FOUND;
2852 if (tline->next)
2853 error_precond(ERR_WARNING|ERR_PASS1,
2854 "trailing garbage after `%%else' ignored");
2855 if ((defining == NULL) || (defining->type != EXP_IF)) {
2856 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2858 switch (defining->state) {
2859 case COND_IF_TRUE:
2860 case COND_DONE:
2861 defining->state = COND_ELSE_FALSE;
2862 defining->ignoring = true;
2863 break;
2865 case COND_NEVER:
2866 defining->ignoring = true;
2867 break;
2869 case COND_IF_FALSE:
2870 defining->state = COND_ELSE_TRUE;
2871 defining->ignoring = false;
2872 break;
2874 case COND_ELSE_TRUE:
2875 case COND_ELSE_FALSE:
2876 error_precond(ERR_WARNING|ERR_PASS1,
2877 "`%%else' after `%%else' ignored.");
2878 defining->state = COND_NEVER;
2879 defining->ignoring = true;
2880 break;
2882 free_tlist(origline);
2883 return DIRECTIVE_FOUND;
2885 case PP_ENDIF:
2886 if (defining != NULL) {
2887 if (defining->type == EXP_IF) {
2888 if (defining->def_depth > 0) {
2889 defining->def_depth --;
2890 return NO_DIRECTIVE_FOUND;
2892 } else {
2893 return NO_DIRECTIVE_FOUND;
2896 if (tline->next)
2897 error_precond(ERR_WARNING|ERR_PASS1,
2898 "trailing garbage after `%%endif' ignored");
2899 if ((defining == NULL) || (defining->type != EXP_IF)) {
2900 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2901 return DIRECTIVE_FOUND;
2903 ed = defining;
2904 defining = ed->prev;
2905 ed->prev = expansions;
2906 expansions = ed;
2907 ei = new_ExpInv(EXP_IF, ed);
2908 ei->current = ed->line;
2909 ei->emitting = true;
2910 ei->prev = istk->expansion;
2911 istk->expansion = ei;
2912 free_tlist(origline);
2913 return DIRECTIVE_FOUND;
2915 case PP_RMACRO:
2916 case PP_IRMACRO:
2917 case PP_MACRO:
2918 case PP_IMACRO:
2919 if (defining != NULL) {
2920 if (defining->type == EXP_MMACRO) {
2921 defining->def_depth ++;
2923 return NO_DIRECTIVE_FOUND;
2925 ed = new_ExpDef(EXP_MMACRO);
2926 ed->max_depth =
2927 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2928 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2929 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2930 nasm_free(ed);
2931 ed = NULL;
2932 return DIRECTIVE_FOUND;
2934 ed->def_depth = 0;
2935 ed->cur_depth = 0;
2936 ed->max_depth = (ed->max_depth + 1);
2937 ed->ignoring = false;
2938 ed->prev = defining;
2939 defining = ed;
2941 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2942 while (eed) {
2943 if (!strcmp(eed->name, ed->name) &&
2944 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2945 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
2946 error(ERR_WARNING|ERR_PASS1,
2947 "redefining multi-line macro `%s'", ed->name);
2948 return DIRECTIVE_FOUND;
2950 eed = eed->next;
2952 free_tlist(origline);
2953 return DIRECTIVE_FOUND;
2955 case PP_ENDM:
2956 case PP_ENDMACRO:
2957 if (defining != NULL) {
2958 if (defining->type == EXP_MMACRO) {
2959 if (defining->def_depth > 0) {
2960 defining->def_depth --;
2961 return NO_DIRECTIVE_FOUND;
2963 } else {
2964 return NO_DIRECTIVE_FOUND;
2967 if (!(defining) || (defining->type != EXP_MMACRO)) {
2968 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2969 return DIRECTIVE_FOUND;
2971 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2972 defining->next = *edhead;
2973 *edhead = defining;
2974 ed = defining;
2975 defining = ed->prev;
2976 ed->prev = expansions;
2977 expansions = ed;
2978 ed = NULL;
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2982 case PP_EXITMACRO:
2983 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2985 * We must search along istk->expansion until we hit a
2986 * macro invocation. Then we disable the emitting state(s)
2987 * between exitmacro and endmacro.
2989 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2990 if(ei->type == EXP_MMACRO) {
2991 break;
2995 if (ei != NULL) {
2997 * Set all invocations leading back to the macro
2998 * invocation to a non-emitting state.
3000 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3001 eei->emitting = false;
3003 eei->emitting = false;
3004 } else {
3005 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
3010 case PP_UNMACRO:
3011 case PP_UNIMACRO:
3012 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3014 ExpDef **ed_p;
3015 ExpDef spec;
3017 spec.casesense = (i == PP_UNMACRO);
3018 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3019 return DIRECTIVE_FOUND;
3021 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3022 while (ed_p && *ed_p) {
3023 ed = *ed_p;
3024 if (ed->casesense == spec.casesense &&
3025 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3026 ed->nparam_min == spec.nparam_min &&
3027 ed->nparam_max == spec.nparam_max &&
3028 ed->plus == spec.plus) {
3029 if (ed->cur_depth > 0) {
3030 error(ERR_NONFATAL, "`%s' ignored on active macro",
3031 pp_directives[i]);
3032 break;
3033 } else {
3034 *ed_p = ed->next;
3035 free_expdef(ed);
3037 } else {
3038 ed_p = &ed->next;
3041 free_tlist(origline);
3042 free_tlist(spec.dlist);
3043 return DIRECTIVE_FOUND;
3046 case PP_ROTATE:
3047 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3048 if (tline->next && tline->next->type == TOK_WHITESPACE)
3049 tline = tline->next;
3050 if (!tline->next) {
3051 free_tlist(origline);
3052 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3053 return DIRECTIVE_FOUND;
3055 t = expand_smacro(tline->next);
3056 tline->next = NULL;
3057 free_tlist(origline);
3058 tline = t;
3059 tptr = &t;
3060 tokval.t_type = TOKEN_INVALID;
3061 evalresult =
3062 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3063 free_tlist(tline);
3064 if (!evalresult)
3065 return DIRECTIVE_FOUND;
3066 if (tokval.t_type)
3067 error(ERR_WARNING|ERR_PASS1,
3068 "trailing garbage after expression ignored");
3069 if (!is_simple(evalresult)) {
3070 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3071 return DIRECTIVE_FOUND;
3073 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3074 if (ei->type == EXP_MMACRO) {
3075 break;
3078 if (ei == NULL) {
3079 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3080 } else if (ei->nparam == 0) {
3081 error(ERR_NONFATAL,
3082 "`%%rotate' invoked within macro without parameters");
3083 } else {
3084 int rotate = ei->rotate + reloc_value(evalresult);
3086 rotate %= (int)ei->nparam;
3087 if (rotate < 0)
3088 rotate += ei->nparam;
3089 ei->rotate = rotate;
3091 return DIRECTIVE_FOUND;
3093 case PP_REP:
3094 if (defining != NULL) {
3095 if (defining->type == EXP_REP) {
3096 defining->def_depth ++;
3098 return NO_DIRECTIVE_FOUND;
3100 nolist = false;
3101 do {
3102 tline = tline->next;
3103 } while (tok_type_(tline, TOK_WHITESPACE));
3105 if (tok_type_(tline, TOK_ID) &&
3106 nasm_stricmp(tline->text, ".nolist") == 0) {
3107 nolist = true;
3108 do {
3109 tline = tline->next;
3110 } while (tok_type_(tline, TOK_WHITESPACE));
3113 if (tline) {
3114 t = expand_smacro(tline);
3115 tptr = &t;
3116 tokval.t_type = TOKEN_INVALID;
3117 evalresult =
3118 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3119 if (!evalresult) {
3120 free_tlist(origline);
3121 return DIRECTIVE_FOUND;
3123 if (tokval.t_type)
3124 error(ERR_WARNING|ERR_PASS1,
3125 "trailing garbage after expression ignored");
3126 if (!is_simple(evalresult)) {
3127 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3128 return DIRECTIVE_FOUND;
3130 count = reloc_value(evalresult);
3131 if (count >= REP_LIMIT) {
3132 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
3133 count = 0;
3134 } else
3135 count++;
3136 } else {
3137 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
3138 count = 0;
3140 free_tlist(origline);
3141 ed = new_ExpDef(EXP_REP);
3142 ed->nolist = nolist;
3143 ed->def_depth = 0;
3144 ed->cur_depth = 1;
3145 ed->max_depth = (count - 1);
3146 ed->ignoring = false;
3147 ed->prev = defining;
3148 defining = ed;
3149 return DIRECTIVE_FOUND;
3151 case PP_ENDREP:
3152 if (defining != NULL) {
3153 if (defining->type == EXP_REP) {
3154 if (defining->def_depth > 0) {
3155 defining->def_depth --;
3156 return NO_DIRECTIVE_FOUND;
3158 } else {
3159 return NO_DIRECTIVE_FOUND;
3162 if ((defining == NULL) || (defining->type != EXP_REP)) {
3163 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3164 return DIRECTIVE_FOUND;
3168 * Now we have a "macro" defined - although it has no name
3169 * and we won't be entering it in the hash tables - we must
3170 * push a macro-end marker for it on to istk->expansion.
3171 * After that, it will take care of propagating itself (a
3172 * macro-end marker line for a macro which is really a %rep
3173 * block will cause the macro to be re-expanded, complete
3174 * with another macro-end marker to ensure the process
3175 * continues) until the whole expansion is forcibly removed
3176 * from istk->expansion by a %exitrep.
3178 ed = defining;
3179 defining = ed->prev;
3180 ed->prev = expansions;
3181 expansions = ed;
3182 ei = new_ExpInv(EXP_REP, ed);
3183 ei->current = ed->line;
3184 ei->emitting = ((ed->max_depth > 0) ? true : false);
3185 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3186 ei->prev = istk->expansion;
3187 istk->expansion = ei;
3188 free_tlist(origline);
3189 return DIRECTIVE_FOUND;
3191 case PP_EXITREP:
3192 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3194 * We must search along istk->expansion until we hit a
3195 * rep invocation. Then we disable the emitting state(s)
3196 * between exitrep and endrep.
3198 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3199 if (ei->type == EXP_REP) {
3200 break;
3204 if (ei != NULL) {
3206 * Set all invocations leading back to the rep
3207 * invocation to a non-emitting state.
3209 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3210 eei->emitting = false;
3212 eei->emitting = false;
3213 eei->current = NULL;
3214 eei->def->cur_depth = eei->def->max_depth;
3215 } else {
3216 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3221 case PP_XDEFINE:
3222 case PP_IXDEFINE:
3223 case PP_DEFINE:
3224 case PP_IDEFINE:
3225 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3226 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3228 tline = tline->next;
3229 skip_white_(tline);
3230 tline = expand_id(tline);
3231 if (!tline || (tline->type != TOK_ID &&
3232 (tline->type != TOK_PREPROC_ID ||
3233 tline->text[1] != '$'))) {
3234 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3235 pp_directives[i]);
3236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
3240 ctx = get_ctx(tline->text, &mname);
3241 last = tline;
3242 param_start = tline = tline->next;
3243 nparam = 0;
3245 /* Expand the macro definition now for %xdefine and %ixdefine */
3246 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3247 tline = expand_smacro(tline);
3249 if (tok_is_(tline, "(")) {
3251 * This macro has parameters.
3254 tline = tline->next;
3255 while (1) {
3256 skip_white_(tline);
3257 if (!tline) {
3258 error(ERR_NONFATAL, "parameter identifier expected");
3259 free_tlist(origline);
3260 return DIRECTIVE_FOUND;
3262 if (tline->type != TOK_ID) {
3263 error(ERR_NONFATAL,
3264 "`%s': parameter identifier expected",
3265 tline->text);
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3270 smacro_set_param_idx(tline, nparam);
3271 nparam++;
3273 tline = tline->next;
3274 skip_white_(tline);
3275 if (tok_is_(tline, ",")) {
3276 tline = tline->next;
3277 } else {
3278 if (!tok_is_(tline, ")")) {
3279 error(ERR_NONFATAL,
3280 "`)' expected to terminate macro template");
3281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
3284 break;
3287 last = tline;
3288 tline = tline->next;
3290 if (tok_type_(tline, TOK_WHITESPACE))
3291 last = tline, tline = tline->next;
3292 macro_start = NULL;
3293 last->next = NULL;
3294 t = tline;
3295 while (t) {
3296 if (t->type == TOK_ID) {
3297 list_for_each(tt, param_start)
3298 if (is_smacro_param(tt) &&
3299 !strcmp(tt->text, t->text))
3300 t->type = tt->type;
3302 tt = t->next;
3303 t->next = macro_start;
3304 macro_start = t;
3305 t = tt;
3308 * Good. We now have a macro name, a parameter count, and a
3309 * token list (in reverse order) for an expansion. We ought
3310 * to be OK just to create an SMacro, store it, and let
3311 * free_tlist have the rest of the line (which we have
3312 * carefully re-terminated after chopping off the expansion
3313 * from the end).
3315 define_smacro(ctx, mname, casesense, nparam, macro_start);
3316 free_tlist(origline);
3317 return DIRECTIVE_FOUND;
3319 case PP_UNDEF:
3320 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3321 tline = tline->next;
3322 skip_white_(tline);
3323 tline = expand_id(tline);
3324 if (!tline || (tline->type != TOK_ID &&
3325 (tline->type != TOK_PREPROC_ID ||
3326 tline->text[1] != '$'))) {
3327 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3331 if (tline->next) {
3332 error(ERR_WARNING|ERR_PASS1,
3333 "trailing garbage after macro name ignored");
3336 /* Find the context that symbol belongs to */
3337 ctx = get_ctx(tline->text, &mname);
3338 undef_smacro(ctx, mname);
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
3342 case PP_DEFSTR:
3343 case PP_IDEFSTR:
3344 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3345 casesense = (i == PP_DEFSTR);
3347 tline = tline->next;
3348 skip_white_(tline);
3349 tline = expand_id(tline);
3350 if (!tline || (tline->type != TOK_ID &&
3351 (tline->type != TOK_PREPROC_ID ||
3352 tline->text[1] != '$'))) {
3353 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3354 pp_directives[i]);
3355 free_tlist(origline);
3356 return DIRECTIVE_FOUND;
3359 ctx = get_ctx(tline->text, &mname);
3360 last = tline;
3361 tline = expand_smacro(tline->next);
3362 last->next = NULL;
3364 while (tok_type_(tline, TOK_WHITESPACE))
3365 tline = delete_Token(tline);
3367 p = detoken(tline, false);
3368 macro_start = nasm_zalloc(sizeof(*macro_start));
3369 macro_start->text = nasm_quote(p, strlen(p));
3370 macro_start->type = TOK_STRING;
3371 nasm_free(p);
3374 * We now have a macro name, an implicit parameter count of
3375 * zero, and a string token to use as an expansion. Create
3376 * and store an SMacro.
3378 define_smacro(ctx, mname, casesense, 0, macro_start);
3379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
3382 case PP_DEFTOK:
3383 case PP_IDEFTOK:
3384 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3385 casesense = (i == PP_DEFTOK);
3387 tline = tline->next;
3388 skip_white_(tline);
3389 tline = expand_id(tline);
3390 if (!tline || (tline->type != TOK_ID &&
3391 (tline->type != TOK_PREPROC_ID ||
3392 tline->text[1] != '$'))) {
3393 error(ERR_NONFATAL,
3394 "`%s' expects a macro identifier as first parameter",
3395 pp_directives[i]);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3399 ctx = get_ctx(tline->text, &mname);
3400 last = tline;
3401 tline = expand_smacro(tline->next);
3402 last->next = NULL;
3404 t = tline;
3405 while (tok_type_(t, TOK_WHITESPACE))
3406 t = t->next;
3407 /* t should now point to the string */
3408 if (!tok_type_(t, TOK_STRING)) {
3409 error(ERR_NONFATAL,
3410 "`%s` requires string as second parameter",
3411 pp_directives[i]);
3412 free_tlist(tline);
3413 free_tlist(origline);
3414 return DIRECTIVE_FOUND;
3418 * Convert the string to a token stream. Note that smacros
3419 * are stored with the token stream reversed, so we have to
3420 * reverse the output of tokenize().
3422 nasm_unquote_cstr(t->text, i);
3423 macro_start = reverse_tokens(tokenize(t->text));
3426 * We now have a macro name, an implicit parameter count of
3427 * zero, and a numeric token to use as an expansion. Create
3428 * and store an SMacro.
3430 define_smacro(ctx, mname, casesense, 0, macro_start);
3431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3435 case PP_PATHSEARCH:
3436 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3438 FILE *fp;
3439 StrList *xsl = NULL;
3440 StrList **xst = &xsl;
3442 casesense = true;
3444 tline = tline->next;
3445 skip_white_(tline);
3446 tline = expand_id(tline);
3447 if (!tline || (tline->type != TOK_ID &&
3448 (tline->type != TOK_PREPROC_ID ||
3449 tline->text[1] != '$'))) {
3450 error(ERR_NONFATAL,
3451 "`%%pathsearch' expects a macro identifier as first parameter");
3452 free_tlist(origline);
3453 return DIRECTIVE_FOUND;
3455 ctx = get_ctx(tline->text, &mname);
3456 last = tline;
3457 tline = expand_smacro(tline->next);
3458 last->next = NULL;
3460 t = tline;
3461 while (tok_type_(t, TOK_WHITESPACE))
3462 t = t->next;
3464 if (!t || (t->type != TOK_STRING &&
3465 t->type != TOK_INTERNAL_STRING)) {
3466 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND; /* but we did _something_ */
3471 if (t->next)
3472 error(ERR_WARNING|ERR_PASS1,
3473 "trailing garbage after `%%pathsearch' ignored");
3474 p = t->text;
3475 if (t->type != TOK_INTERNAL_STRING)
3476 nasm_unquote(p, NULL);
3478 fp = inc_fopen(p, &xsl, &xst, true);
3479 if (fp) {
3480 p = xsl->str;
3481 fclose(fp); /* Don't actually care about the file */
3483 macro_start = nasm_zalloc(sizeof(*macro_start));
3484 macro_start->text = nasm_quote(p, strlen(p));
3485 macro_start->type = TOK_STRING;
3486 nasm_free(xsl);
3489 * We now have a macro name, an implicit parameter count of
3490 * zero, and a string token to use as an expansion. Create
3491 * and store an SMacro.
3493 define_smacro(ctx, mname, casesense, 0, macro_start);
3494 free_tlist(tline);
3495 free_tlist(origline);
3496 return DIRECTIVE_FOUND;
3499 case PP_STRLEN:
3500 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3501 casesense = true;
3503 tline = tline->next;
3504 skip_white_(tline);
3505 tline = expand_id(tline);
3506 if (!tline || (tline->type != TOK_ID &&
3507 (tline->type != TOK_PREPROC_ID ||
3508 tline->text[1] != '$'))) {
3509 error(ERR_NONFATAL,
3510 "`%%strlen' expects a macro identifier as first parameter");
3511 free_tlist(origline);
3512 return DIRECTIVE_FOUND;
3514 ctx = get_ctx(tline->text, &mname);
3515 last = tline;
3516 tline = expand_smacro(tline->next);
3517 last->next = NULL;
3519 t = tline;
3520 while (tok_type_(t, TOK_WHITESPACE))
3521 t = t->next;
3522 /* t should now point to the string */
3523 if (!tok_type_(t, TOK_STRING)) {
3524 error(ERR_NONFATAL,
3525 "`%%strlen` requires string as second parameter");
3526 free_tlist(tline);
3527 free_tlist(origline);
3528 return DIRECTIVE_FOUND;
3531 macro_start = nasm_zalloc(sizeof(*macro_start));
3532 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3535 * We now have a macro name, an implicit parameter count of
3536 * zero, and a numeric token to use as an expansion. Create
3537 * and store an SMacro.
3539 define_smacro(ctx, mname, casesense, 0, macro_start);
3540 free_tlist(tline);
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3544 case PP_STRCAT:
3545 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3546 casesense = true;
3548 tline = tline->next;
3549 skip_white_(tline);
3550 tline = expand_id(tline);
3551 if (!tline || (tline->type != TOK_ID &&
3552 (tline->type != TOK_PREPROC_ID ||
3553 tline->text[1] != '$'))) {
3554 error(ERR_NONFATAL,
3555 "`%%strcat' expects a macro identifier as first parameter");
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3559 ctx = get_ctx(tline->text, &mname);
3560 last = tline;
3561 tline = expand_smacro(tline->next);
3562 last->next = NULL;
3564 len = 0;
3565 list_for_each(t, tline) {
3566 switch (t->type) {
3567 case TOK_WHITESPACE:
3568 break;
3569 case TOK_STRING:
3570 len += t->a.len = nasm_unquote(t->text, NULL);
3571 break;
3572 case TOK_OTHER:
3573 if (!strcmp(t->text, ",")) /* permit comma separators */
3574 break;
3575 /* else fall through */
3576 default:
3577 error(ERR_NONFATAL,
3578 "non-string passed to `%%strcat' (%d)", t->type);
3579 free_tlist(tline);
3580 free_tlist(origline);
3581 return DIRECTIVE_FOUND;
3585 p = pp = nasm_malloc(len);
3586 list_for_each(t, tline) {
3587 if (t->type == TOK_STRING) {
3588 memcpy(p, t->text, t->a.len);
3589 p += t->a.len;
3594 * We now have a macro name, an implicit parameter count of
3595 * zero, and a numeric token to use as an expansion. Create
3596 * and store an SMacro.
3598 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3599 macro_start->text = nasm_quote(pp, len);
3600 nasm_free(pp);
3601 define_smacro(ctx, mname, casesense, 0, macro_start);
3602 free_tlist(tline);
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
3606 case PP_SUBSTR:
3607 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3609 int64_t start, count;
3610 size_t len;
3612 casesense = true;
3614 tline = tline->next;
3615 skip_white_(tline);
3616 tline = expand_id(tline);
3617 if (!tline || (tline->type != TOK_ID &&
3618 (tline->type != TOK_PREPROC_ID ||
3619 tline->text[1] != '$'))) {
3620 error(ERR_NONFATAL,
3621 "`%%substr' expects a macro identifier as first parameter");
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3625 ctx = get_ctx(tline->text, &mname);
3626 last = tline;
3627 tline = expand_smacro(tline->next);
3628 last->next = NULL;
3630 if (tline) /* skip expanded id */
3631 t = tline->next;
3632 while (tok_type_(t, TOK_WHITESPACE))
3633 t = t->next;
3635 /* t should now point to the string */
3636 if (!tok_type_(t, TOK_STRING)) {
3637 error(ERR_NONFATAL,
3638 "`%%substr` requires string as second parameter");
3639 free_tlist(tline);
3640 free_tlist(origline);
3641 return DIRECTIVE_FOUND;
3644 tt = t->next;
3645 tptr = &tt;
3646 tokval.t_type = TOKEN_INVALID;
3647 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3648 pass, error, NULL);
3649 if (!evalresult) {
3650 free_tlist(tline);
3651 free_tlist(origline);
3652 return DIRECTIVE_FOUND;
3653 } else if (!is_simple(evalresult)) {
3654 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3655 free_tlist(tline);
3656 free_tlist(origline);
3657 return DIRECTIVE_FOUND;
3659 start = evalresult->value - 1;
3661 while (tok_type_(tt, TOK_WHITESPACE))
3662 tt = tt->next;
3663 if (!tt) {
3664 count = 1; /* Backwards compatibility: one character */
3665 } else {
3666 tokval.t_type = TOKEN_INVALID;
3667 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3668 pass, error, NULL);
3669 if (!evalresult) {
3670 free_tlist(tline);
3671 free_tlist(origline);
3672 return DIRECTIVE_FOUND;
3673 } else if (!is_simple(evalresult)) {
3674 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3675 free_tlist(tline);
3676 free_tlist(origline);
3677 return DIRECTIVE_FOUND;
3679 count = evalresult->value;
3682 len = nasm_unquote(t->text, NULL);
3683 /* make start and count being in range */
3684 if (start < 0)
3685 start = 0;
3686 if (count < 0)
3687 count = len + count + 1 - start;
3688 if (start + count > (int64_t)len)
3689 count = len - start;
3690 if (!len || count < 0 || start >=(int64_t)len)
3691 start = -1, count = 0; /* empty string */
3693 macro_start = nasm_zalloc(sizeof(*macro_start));
3694 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3695 macro_start->type = TOK_STRING;
3698 * We now have a macro name, an implicit parameter count of
3699 * zero, and a numeric token to use as an expansion. Create
3700 * and store an SMacro.
3702 define_smacro(ctx, mname, casesense, 0, macro_start);
3703 free_tlist(tline);
3704 free_tlist(origline);
3705 return DIRECTIVE_FOUND;
3708 case PP_ASSIGN:
3709 case PP_IASSIGN:
3710 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3711 casesense = (i == PP_ASSIGN);
3713 tline = tline->next;
3714 skip_white_(tline);
3715 tline = expand_id(tline);
3716 if (!tline || (tline->type != TOK_ID &&
3717 (tline->type != TOK_PREPROC_ID ||
3718 tline->text[1] != '$'))) {
3719 error(ERR_NONFATAL,
3720 "`%%%sassign' expects a macro identifier",
3721 (i == PP_IASSIGN ? "i" : ""));
3722 free_tlist(origline);
3723 return DIRECTIVE_FOUND;
3725 ctx = get_ctx(tline->text, &mname);
3726 last = tline;
3727 tline = expand_smacro(tline->next);
3728 last->next = NULL;
3730 t = tline;
3731 tptr = &t;
3732 tokval.t_type = TOKEN_INVALID;
3733 evalresult =
3734 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3735 free_tlist(tline);
3736 if (!evalresult) {
3737 free_tlist(origline);
3738 return DIRECTIVE_FOUND;
3741 if (tokval.t_type)
3742 error(ERR_WARNING|ERR_PASS1,
3743 "trailing garbage after expression ignored");
3745 if (!is_simple(evalresult)) {
3746 error(ERR_NONFATAL,
3747 "non-constant value given to `%%%sassign'",
3748 (i == PP_IASSIGN ? "i" : ""));
3749 free_tlist(origline);
3750 return DIRECTIVE_FOUND;
3753 macro_start = nasm_zalloc(sizeof(*macro_start));
3754 make_tok_num(macro_start, reloc_value(evalresult));
3757 * We now have a macro name, an implicit parameter count of
3758 * zero, and a numeric token to use as an expansion. Create
3759 * and store an SMacro.
3761 define_smacro(ctx, mname, casesense, 0, macro_start);
3762 free_tlist(origline);
3763 return DIRECTIVE_FOUND;
3765 case PP_LINE:
3766 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3768 * Syntax is `%line nnn[+mmm] [filename]'
3770 tline = tline->next;
3771 skip_white_(tline);
3772 if (!tok_type_(tline, TOK_NUMBER)) {
3773 error(ERR_NONFATAL, "`%%line' expects line number");
3774 free_tlist(origline);
3775 return DIRECTIVE_FOUND;
3777 k = readnum(tline->text, &err);
3778 m = 1;
3779 tline = tline->next;
3780 if (tok_is_(tline, "+")) {
3781 tline = tline->next;
3782 if (!tok_type_(tline, TOK_NUMBER)) {
3783 error(ERR_NONFATAL, "`%%line' expects line increment");
3784 free_tlist(origline);
3785 return DIRECTIVE_FOUND;
3787 m = readnum(tline->text, &err);
3788 tline = tline->next;
3790 skip_white_(tline);
3791 src_set_linnum(k);
3792 istk->lineinc = m;
3793 if (tline) {
3794 nasm_free(src_set_fname(detoken(tline, false)));
3796 free_tlist(origline);
3797 return DIRECTIVE_FOUND;
3799 case PP_WHILE:
3800 if (defining != NULL) {
3801 if (defining->type == EXP_WHILE) {
3802 defining->def_depth ++;
3804 return NO_DIRECTIVE_FOUND;
3806 l = NULL;
3807 if ((istk->expansion != NULL) &&
3808 (istk->expansion->emitting == false)) {
3809 j = COND_NEVER;
3810 } else {
3811 l = new_Line();
3812 l->first = copy_Token(tline->next);
3813 j = if_condition(tline->next, i);
3814 tline->next = NULL; /* it got freed */
3815 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3817 ed = new_ExpDef(EXP_WHILE);
3818 ed->state = j;
3819 ed->cur_depth = 1;
3820 ed->max_depth = DEADMAN_LIMIT;
3821 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3822 if (ed->ignoring == false) {
3823 ed->line = l;
3824 ed->last = l;
3825 } else if (l != NULL) {
3826 delete_Token(l->first);
3827 nasm_free(l);
3828 l = NULL;
3830 ed->prev = defining;
3831 defining = ed;
3832 free_tlist(origline);
3833 return DIRECTIVE_FOUND;
3835 case PP_ENDWHILE:
3836 if (defining != NULL) {
3837 if (defining->type == EXP_WHILE) {
3838 if (defining->def_depth > 0) {
3839 defining->def_depth --;
3840 return NO_DIRECTIVE_FOUND;
3842 } else {
3843 return NO_DIRECTIVE_FOUND;
3846 if (tline->next != NULL) {
3847 error_precond(ERR_WARNING|ERR_PASS1,
3848 "trailing garbage after `%%endwhile' ignored");
3850 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3851 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3852 return DIRECTIVE_FOUND;
3854 ed = defining;
3855 defining = ed->prev;
3856 if (ed->ignoring == false) {
3857 ed->prev = expansions;
3858 expansions = ed;
3859 ei = new_ExpInv(EXP_WHILE, ed);
3860 ei->current = ed->line->next;
3861 ei->emitting = true;
3862 ei->prev = istk->expansion;
3863 istk->expansion = ei;
3864 } else {
3865 nasm_free(ed);
3867 free_tlist(origline);
3868 return DIRECTIVE_FOUND;
3870 case PP_EXITWHILE:
3871 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3873 * We must search along istk->expansion until we hit a
3874 * while invocation. Then we disable the emitting state(s)
3875 * between exitwhile and endwhile.
3877 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3878 if (ei->type == EXP_WHILE) {
3879 break;
3883 if (ei != NULL) {
3885 * Set all invocations leading back to the while
3886 * invocation to a non-emitting state.
3888 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3889 eei->emitting = false;
3891 eei->emitting = false;
3892 eei->current = NULL;
3893 eei->def->cur_depth = eei->def->max_depth;
3894 } else {
3895 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3897 free_tlist(origline);
3898 return DIRECTIVE_FOUND;
3900 case PP_COMMENT:
3901 if (defining != NULL) {
3902 if (defining->type == EXP_COMMENT) {
3903 defining->def_depth ++;
3905 return NO_DIRECTIVE_FOUND;
3907 ed = new_ExpDef(EXP_COMMENT);
3908 ed->ignoring = true;
3909 ed->prev = defining;
3910 defining = ed;
3911 free_tlist(origline);
3912 return DIRECTIVE_FOUND;
3914 case PP_ENDCOMMENT:
3915 if (defining != NULL) {
3916 if (defining->type == EXP_COMMENT) {
3917 if (defining->def_depth > 0) {
3918 defining->def_depth --;
3919 return NO_DIRECTIVE_FOUND;
3921 } else {
3922 return NO_DIRECTIVE_FOUND;
3925 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3926 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3927 return DIRECTIVE_FOUND;
3929 ed = defining;
3930 defining = ed->prev;
3931 nasm_free(ed);
3932 free_tlist(origline);
3933 return DIRECTIVE_FOUND;
3935 case PP_FINAL:
3936 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3937 if (in_final != false) {
3938 error(ERR_FATAL, "`%%final' cannot be used recursively");
3940 tline = tline->next;
3941 skip_white_(tline);
3942 if (tline == NULL) {
3943 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3944 } else {
3945 l = new_Line();
3946 l->first = copy_Token(tline);
3947 l->next = finals;
3948 finals = l;
3950 free_tlist(origline);
3951 return DIRECTIVE_FOUND;
3953 default:
3954 error(ERR_FATAL,
3955 "preprocessor directive `%s' not yet implemented",
3956 pp_directives[i]);
3957 return DIRECTIVE_FOUND;
3962 * Ensure that a macro parameter contains a condition code and
3963 * nothing else. Return the condition code index if so, or -1
3964 * otherwise.
3966 static int find_cc(Token * t)
3968 Token *tt;
3969 int i, j, k, m;
3971 if (!t)
3972 return -1; /* Probably a %+ without a space */
3974 skip_white_(t);
3975 if (t->type != TOK_ID)
3976 return -1;
3977 tt = t->next;
3978 skip_white_(tt);
3979 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3980 return -1;
3982 i = -1;
3983 j = ARRAY_SIZE(conditions);
3984 while (j - i > 1) {
3985 k = (j + i) / 2;
3986 m = nasm_stricmp(t->text, conditions[k]);
3987 if (m == 0) {
3988 i = k;
3989 j = -2;
3990 break;
3991 } else if (m < 0) {
3992 j = k;
3993 } else
3994 i = k;
3996 if (j != -2)
3997 return -1;
3998 return i;
4001 static bool paste_tokens(Token **head, const struct tokseq_match *m,
4002 int mnum, bool handle_paste_tokens)
4004 Token **tail, *t, *tt;
4005 Token **paste_head;
4006 bool did_paste = false;
4007 char *tmp;
4008 int i;
4010 /* Now handle token pasting... */
4011 paste_head = NULL;
4012 tail = head;
4013 while ((t = *tail) && (tt = t->next)) {
4014 switch (t->type) {
4015 case TOK_WHITESPACE:
4016 if (tt->type == TOK_WHITESPACE) {
4017 /* Zap adjacent whitespace tokens */
4018 t->next = delete_Token(tt);
4019 } else {
4020 /* Do not advance paste_head here */
4021 tail = &t->next;
4023 break;
4024 case TOK_PASTE: /* %+ */
4025 if (handle_paste_tokens) {
4026 /* Zap %+ and whitespace tokens to the right */
4027 while (t && (t->type == TOK_WHITESPACE ||
4028 t->type == TOK_PASTE))
4029 t = *tail = delete_Token(t);
4030 if (!paste_head || !t)
4031 break; /* Nothing to paste with */
4032 tail = paste_head;
4033 t = *tail;
4034 tt = t->next;
4035 while (tok_type_(tt, TOK_WHITESPACE))
4036 tt = t->next = delete_Token(tt);
4037 if (tt) {
4038 tmp = nasm_strcat(t->text, tt->text);
4039 delete_Token(t);
4040 tt = delete_Token(tt);
4041 t = *tail = tokenize(tmp);
4042 nasm_free(tmp);
4043 while (t->next) {
4044 tail = &t->next;
4045 t = t->next;
4047 t->next = tt; /* Attach the remaining token chain */
4048 did_paste = true;
4050 paste_head = tail;
4051 tail = &t->next;
4052 break;
4054 /* else fall through */
4055 default:
4057 * Concatenation of tokens might look nontrivial
4058 * but in real it's pretty simple -- the caller
4059 * prepares the masks of token types to be concatenated
4060 * and we simply find matched sequences and slip
4061 * them together
4063 for (i = 0; i < mnum; i++) {
4064 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4065 size_t len = 0;
4066 char *tmp, *p;
4068 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
4069 len += strlen(tt->text);
4070 tt = tt->next;
4073 nasm_dump_token(tt);
4076 * Now tt points to the first token after
4077 * the potential paste area...
4079 if (tt != t->next) {
4080 /* We have at least two tokens... */
4081 len += strlen(t->text);
4082 p = tmp = nasm_malloc(len+1);
4083 while (t != tt) {
4084 strcpy(p, t->text);
4085 p = strchr(p, '\0');
4086 t = delete_Token(t);
4088 t = *tail = tokenize(tmp);
4089 nasm_free(tmp);
4090 while (t->next) {
4091 tail = &t->next;
4092 t = t->next;
4094 t->next = tt; /* Attach the remaining token chain */
4095 did_paste = true;
4097 paste_head = tail;
4098 tail = &t->next;
4099 break;
4102 if (i >= mnum) { /* no match */
4103 tail = &t->next;
4104 if (!tok_type_(t->next, TOK_WHITESPACE))
4105 paste_head = tail;
4107 break;
4110 return did_paste;
4114 * expands to a list of tokens from %{x:y}
4116 static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
4118 Token *t = tline, **tt, *tm, *head;
4119 char *pos;
4120 int fst, lst, j, i;
4122 pos = strchr(tline->text, ':');
4123 nasm_assert(pos);
4125 lst = atoi(pos + 1);
4126 fst = atoi(tline->text + 1);
4129 * only macros params are accounted so
4130 * if someone passes %0 -- we reject such
4131 * value(s)
4133 if (lst == 0 || fst == 0)
4134 goto err;
4136 /* the values should be sane */
4137 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4138 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
4139 goto err;
4141 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4142 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
4144 /* counted from zero */
4145 fst--, lst--;
4148 * it will be at least one token
4150 tm = ei->params[(fst + ei->rotate) % ei->nparam];
4151 t = new_Token(NULL, tm->type, tm->text, 0);
4152 head = t, tt = &t->next;
4153 if (fst < lst) {
4154 for (i = fst + 1; i <= lst; i++) {
4155 t = new_Token(NULL, TOK_OTHER, ",", 0);
4156 *tt = t, tt = &t->next;
4157 j = (i + ei->rotate) % ei->nparam;
4158 tm = ei->params[j];
4159 t = new_Token(NULL, tm->type, tm->text, 0);
4160 *tt = t, tt = &t->next;
4162 } else {
4163 for (i = fst - 1; i >= lst; i--) {
4164 t = new_Token(NULL, TOK_OTHER, ",", 0);
4165 *tt = t, tt = &t->next;
4166 j = (i + ei->rotate) % ei->nparam;
4167 tm = ei->params[j];
4168 t = new_Token(NULL, tm->type, tm->text, 0);
4169 *tt = t, tt = &t->next;
4173 *last = tt;
4174 return head;
4176 err:
4177 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4178 &tline->text[1]);
4179 return tline;
4183 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4184 * %-n) and MMacro-local identifiers (%%foo) as well as
4185 * macro indirection (%[...]) and range (%{..:..}).
4187 static Token *expand_mmac_params(Token * tline)
4189 Token *t, *tt, **tail, *thead;
4190 bool changed = false;
4191 char *pos;
4193 tail = &thead;
4194 thead = NULL;
4196 nasm_dump_stream(tline);
4198 while (tline) {
4199 if (tline->type == TOK_PREPROC_ID &&
4200 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4201 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4202 tline->text[1] == '%')) {
4203 char *text = NULL;
4204 int type = 0, cc; /* type = 0 to placate optimisers */
4205 char tmpbuf[30];
4206 unsigned int n;
4207 int i;
4208 ExpInv *ei;
4210 t = tline;
4211 tline = tline->next;
4213 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4214 if (ei->type == EXP_MMACRO) {
4215 break;
4218 if (ei == NULL) {
4219 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
4220 } else {
4221 pos = strchr(t->text, ':');
4222 if (!pos) {
4223 switch (t->text[1]) {
4225 * We have to make a substitution of one of the
4226 * forms %1, %-1, %+1, %%foo, %0.
4228 case '0':
4229 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4230 type = TOK_ID;
4231 text = nasm_strdup(ei->label_text);
4232 } else {
4233 type = TOK_NUMBER;
4234 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4235 text = nasm_strdup(tmpbuf);
4237 break;
4238 case '%':
4239 type = TOK_ID;
4240 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
4241 ei->unique);
4242 text = nasm_strcat(tmpbuf, t->text + 2);
4243 break;
4244 case '-':
4245 n = atoi(t->text + 2) - 1;
4246 if (n >= ei->nparam)
4247 tt = NULL;
4248 else {
4249 if (ei->nparam > 1)
4250 n = (n + ei->rotate) % ei->nparam;
4251 tt = ei->params[n];
4253 cc = find_cc(tt);
4254 if (cc == -1) {
4255 error(ERR_NONFATAL,
4256 "macro parameter %d is not a condition code",
4257 n + 1);
4258 text = NULL;
4259 } else {
4260 type = TOK_ID;
4261 if (inverse_ccs[cc] == -1) {
4262 error(ERR_NONFATAL,
4263 "condition code `%s' is not invertible",
4264 conditions[cc]);
4265 text = NULL;
4266 } else
4267 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4269 break;
4270 case '+':
4271 n = atoi(t->text + 2) - 1;
4272 if (n >= ei->nparam)
4273 tt = NULL;
4274 else {
4275 if (ei->nparam > 1)
4276 n = (n + ei->rotate) % ei->nparam;
4277 tt = ei->params[n];
4279 cc = find_cc(tt);
4280 if (cc == -1) {
4281 error(ERR_NONFATAL,
4282 "macro parameter %d is not a condition code",
4283 n + 1);
4284 text = NULL;
4285 } else {
4286 type = TOK_ID;
4287 text = nasm_strdup(conditions[cc]);
4289 break;
4290 default:
4291 n = atoi(t->text + 1) - 1;
4292 if (n >= ei->nparam)
4293 tt = NULL;
4294 else {
4295 if (ei->nparam > 1)
4296 n = (n + ei->rotate) % ei->nparam;
4297 tt = ei->params[n];
4299 if (tt) {
4300 for (i = 0; i < ei->paramlen[n]; i++) {
4301 *tail = new_Token(NULL, tt->type, tt->text, 0);
4302 tail = &(*tail)->next;
4303 tt = tt->next;
4306 text = NULL; /* we've done it here */
4307 break;
4309 } else {
4311 * seems we have a parameters range here
4313 Token *head, **last;
4314 head = expand_mmac_params_range(ei, t, &last);
4315 if (head != t) {
4316 *tail = head;
4317 *last = tline;
4318 tline = head;
4319 text = NULL;
4323 if (!text) {
4324 delete_Token(t);
4325 } else {
4326 *tail = t;
4327 tail = &t->next;
4328 t->type = type;
4329 nasm_free(t->text);
4330 t->text = text;
4331 t->a.mac = NULL;
4333 changed = true;
4334 continue;
4335 } else if (tline->type == TOK_INDIRECT) {
4336 t = tline;
4337 tline = tline->next;
4338 tt = tokenize(t->text);
4339 tt = expand_mmac_params(tt);
4340 tt = expand_smacro(tt);
4341 *tail = tt;
4342 while (tt) {
4343 tt->a.mac = NULL; /* Necessary? */
4344 tail = &tt->next;
4345 tt = tt->next;
4347 delete_Token(t);
4348 changed = true;
4349 } else {
4350 t = *tail = tline;
4351 tline = tline->next;
4352 t->a.mac = NULL;
4353 tail = &t->next;
4356 *tail = NULL;
4358 if (changed) {
4359 const struct tokseq_match t[] = {
4361 PP_CONCAT_MASK(TOK_ID) |
4362 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4363 PP_CONCAT_MASK(TOK_ID) |
4364 PP_CONCAT_MASK(TOK_NUMBER) |
4365 PP_CONCAT_MASK(TOK_FLOAT) |
4366 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4369 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4370 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4373 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4376 nasm_dump_token(thead);
4378 return thead;
4382 * Expand all single-line macro calls made in the given line.
4383 * Return the expanded version of the line. The original is deemed
4384 * to be destroyed in the process. (In reality we'll just move
4385 * Tokens from input to output a lot of the time, rather than
4386 * actually bothering to destroy and replicate.)
4389 static Token *expand_smacro(Token * tline)
4391 Token *t, *tt, *mstart, **tail, *thead;
4392 SMacro *head = NULL, *m;
4393 Token **params;
4394 int *paramsize;
4395 unsigned int nparam, sparam;
4396 int brackets;
4397 Token *org_tline = tline;
4398 Context *ctx;
4399 const char *mname;
4400 int deadman = DEADMAN_LIMIT;
4401 bool expanded;
4404 * Trick: we should avoid changing the start token pointer since it can
4405 * be contained in "next" field of other token. Because of this
4406 * we allocate a copy of first token and work with it; at the end of
4407 * routine we copy it back
4409 if (org_tline) {
4410 tline = new_Token(org_tline->next, org_tline->type,
4411 org_tline->text, 0);
4412 tline->a.mac = org_tline->a.mac;
4413 nasm_free(org_tline->text);
4414 org_tline->text = NULL;
4417 expanded = true; /* Always expand %+ at least once */
4419 again:
4420 thead = NULL;
4421 tail = &thead;
4423 while (tline) { /* main token loop */
4424 if (!--deadman) {
4425 error(ERR_NONFATAL, "interminable macro recursion");
4426 goto err;
4429 if ((mname = tline->text)) {
4430 /* if this token is a local macro, look in local context */
4431 if (tline->type == TOK_ID) {
4432 head = (SMacro *)hash_findix(&smacros, mname);
4433 } else if (tline->type == TOK_PREPROC_ID) {
4434 ctx = get_ctx(mname, &mname);
4435 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4436 } else
4437 head = NULL;
4440 * We've hit an identifier. As in is_mmacro below, we first
4441 * check whether the identifier is a single-line macro at
4442 * all, then think about checking for parameters if
4443 * necessary.
4445 list_for_each(m, head)
4446 if (!mstrcmp(m->name, mname, m->casesense))
4447 break;
4448 if (m) {
4449 mstart = tline;
4450 params = NULL;
4451 paramsize = NULL;
4452 if (m->nparam == 0) {
4454 * Simple case: the macro is parameterless. Discard the
4455 * one token that the macro call took, and push the
4456 * expansion back on the to-do stack.
4458 if (!m->expansion) {
4459 if (!strcmp("__FILE__", m->name)) {
4460 int32_t num = 0;
4461 char *file = NULL;
4462 src_get(&num, &file);
4463 tline->text = nasm_quote(file, strlen(file));
4464 tline->type = TOK_STRING;
4465 nasm_free(file);
4466 continue;
4468 if (!strcmp("__LINE__", m->name)) {
4469 nasm_free(tline->text);
4470 make_tok_num(tline, src_get_linnum());
4471 continue;
4473 if (!strcmp("__BITS__", m->name)) {
4474 nasm_free(tline->text);
4475 make_tok_num(tline, globalbits);
4476 continue;
4478 tline = delete_Token(tline);
4479 continue;
4481 } else {
4483 * Complicated case: at least one macro with this name
4484 * exists and takes parameters. We must find the
4485 * parameters in the call, count them, find the SMacro
4486 * that corresponds to that form of the macro call, and
4487 * substitute for the parameters when we expand. What a
4488 * pain.
4490 /*tline = tline->next;
4491 skip_white_(tline); */
4492 do {
4493 t = tline->next;
4494 while (tok_type_(t, TOK_SMAC_END)) {
4495 t->a.mac->in_progress = false;
4496 t->text = NULL;
4497 t = tline->next = delete_Token(t);
4499 tline = t;
4500 } while (tok_type_(tline, TOK_WHITESPACE));
4501 if (!tok_is_(tline, "(")) {
4503 * This macro wasn't called with parameters: ignore
4504 * the call. (Behaviour borrowed from gnu cpp.)
4506 tline = mstart;
4507 m = NULL;
4508 } else {
4509 int paren = 0;
4510 int white = 0;
4511 brackets = 0;
4512 nparam = 0;
4513 sparam = PARAM_DELTA;
4514 params = nasm_malloc(sparam * sizeof(Token *));
4515 params[0] = tline->next;
4516 paramsize = nasm_malloc(sparam * sizeof(int));
4517 paramsize[0] = 0;
4518 while (true) { /* parameter loop */
4520 * For some unusual expansions
4521 * which concatenates function call
4523 t = tline->next;
4524 while (tok_type_(t, TOK_SMAC_END)) {
4525 t->a.mac->in_progress = false;
4526 t->text = NULL;
4527 t = tline->next = delete_Token(t);
4529 tline = t;
4531 if (!tline) {
4532 error(ERR_NONFATAL,
4533 "macro call expects terminating `)'");
4534 break;
4536 if (tline->type == TOK_WHITESPACE
4537 && brackets <= 0) {
4538 if (paramsize[nparam])
4539 white++;
4540 else
4541 params[nparam] = tline->next;
4542 continue; /* parameter loop */
4544 if (tline->type == TOK_OTHER
4545 && tline->text[1] == 0) {
4546 char ch = tline->text[0];
4547 if (ch == ',' && !paren && brackets <= 0) {
4548 if (++nparam >= sparam) {
4549 sparam += PARAM_DELTA;
4550 params = nasm_realloc(params,
4551 sparam * sizeof(Token *));
4552 paramsize = nasm_realloc(paramsize,
4553 sparam * sizeof(int));
4555 params[nparam] = tline->next;
4556 paramsize[nparam] = 0;
4557 white = 0;
4558 continue; /* parameter loop */
4560 if (ch == '{' &&
4561 (brackets > 0 || (brackets == 0 &&
4562 !paramsize[nparam])))
4564 if (!(brackets++)) {
4565 params[nparam] = tline->next;
4566 continue; /* parameter loop */
4569 if (ch == '}' && brackets > 0)
4570 if (--brackets == 0) {
4571 brackets = -1;
4572 continue; /* parameter loop */
4574 if (ch == '(' && !brackets)
4575 paren++;
4576 if (ch == ')' && brackets <= 0)
4577 if (--paren < 0)
4578 break;
4580 if (brackets < 0) {
4581 brackets = 0;
4582 error(ERR_NONFATAL, "braces do not "
4583 "enclose all of macro parameter");
4585 paramsize[nparam] += white + 1;
4586 white = 0;
4587 } /* parameter loop */
4588 nparam++;
4589 while (m && (m->nparam != nparam ||
4590 mstrcmp(m->name, mname,
4591 m->casesense)))
4592 m = m->next;
4593 if (!m)
4594 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4595 "macro `%s' exists, "
4596 "but not taking %d parameters",
4597 mstart->text, nparam);
4600 if (m && m->in_progress)
4601 m = NULL;
4602 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4604 * Design question: should we handle !tline, which
4605 * indicates missing ')' here, or expand those
4606 * macros anyway, which requires the (t) test a few
4607 * lines down?
4609 nasm_free(params);
4610 nasm_free(paramsize);
4611 tline = mstart;
4612 } else {
4614 * Expand the macro: we are placed on the last token of the
4615 * call, so that we can easily split the call from the
4616 * following tokens. We also start by pushing an SMAC_END
4617 * token for the cycle removal.
4619 t = tline;
4620 if (t) {
4621 tline = t->next;
4622 t->next = NULL;
4624 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4625 tt->a.mac = m;
4626 m->in_progress = true;
4627 tline = tt;
4628 list_for_each(t, m->expansion) {
4629 if (is_smacro_param(t)) {
4630 Token *pcopy = tline, **ptail = &pcopy;
4631 Token *ttt;
4632 int i, idx;
4634 idx = smacro_get_param_idx(t);
4635 ttt = params[idx];
4638 * We need smacro paramters appended.
4640 for (i = paramsize[idx]; i > 0; i--) {
4641 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4642 ptail = &(*ptail)->next;
4643 ttt = ttt->next;
4646 tline = pcopy;
4647 } else if (t->type == TOK_PREPROC_Q) {
4648 tt = new_Token(tline, TOK_ID, mname, 0);
4649 tline = tt;
4650 } else if (t->type == TOK_PREPROC_QQ) {
4651 tt = new_Token(tline, TOK_ID, m->name, 0);
4652 tline = tt;
4653 } else {
4654 tt = new_Token(tline, t->type, t->text, 0);
4655 tline = tt;
4660 * Having done that, get rid of the macro call, and clean
4661 * up the parameters.
4663 nasm_free(params);
4664 nasm_free(paramsize);
4665 free_tlist(mstart);
4666 expanded = true;
4667 continue; /* main token loop */
4672 if (tline->type == TOK_SMAC_END) {
4673 tline->a.mac->in_progress = false;
4674 tline = delete_Token(tline);
4675 } else {
4676 t = *tail = tline;
4677 tline = tline->next;
4678 t->a.mac = NULL;
4679 t->next = NULL;
4680 tail = &t->next;
4685 * Now scan the entire line and look for successive TOK_IDs that resulted
4686 * after expansion (they can't be produced by tokenize()). The successive
4687 * TOK_IDs should be concatenated.
4688 * Also we look for %+ tokens and concatenate the tokens before and after
4689 * them (without white spaces in between).
4691 if (expanded) {
4692 const struct tokseq_match t[] = {
4694 PP_CONCAT_MASK(TOK_ID) |
4695 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4696 PP_CONCAT_MASK(TOK_ID) |
4697 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4698 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4701 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4703 * If we concatenated something, *and* we had previously expanded
4704 * an actual macro, scan the lines again for macros...
4706 tline = thead;
4707 expanded = false;
4708 goto again;
4712 err:
4713 if (org_tline) {
4714 if (thead) {
4715 *org_tline = *thead;
4716 /* since we just gave text to org_line, don't free it */
4717 thead->text = NULL;
4718 delete_Token(thead);
4719 } else {
4720 /* the expression expanded to empty line;
4721 we can't return NULL for some reasons
4722 we just set the line to a single WHITESPACE token. */
4723 memset(org_tline, 0, sizeof(*org_tline));
4724 org_tline->text = NULL;
4725 org_tline->type = TOK_WHITESPACE;
4727 thead = org_tline;
4730 return thead;
4734 * Similar to expand_smacro but used exclusively with macro identifiers
4735 * right before they are fetched in. The reason is that there can be
4736 * identifiers consisting of several subparts. We consider that if there
4737 * are more than one element forming the name, user wants a expansion,
4738 * otherwise it will be left as-is. Example:
4740 * %define %$abc cde
4742 * the identifier %$abc will be left as-is so that the handler for %define
4743 * will suck it and define the corresponding value. Other case:
4745 * %define _%$abc cde
4747 * In this case user wants name to be expanded *before* %define starts
4748 * working, so we'll expand %$abc into something (if it has a value;
4749 * otherwise it will be left as-is) then concatenate all successive
4750 * PP_IDs into one.
4752 static Token *expand_id(Token * tline)
4754 Token *cur, *oldnext = NULL;
4756 if (!tline || !tline->next)
4757 return tline;
4759 cur = tline;
4760 while (cur->next &&
4761 (cur->next->type == TOK_ID ||
4762 cur->next->type == TOK_PREPROC_ID ||
4763 cur->next->type == TOK_NUMBER))
4764 cur = cur->next;
4766 /* If identifier consists of just one token, don't expand */
4767 if (cur == tline)
4768 return tline;
4770 if (cur) {
4771 oldnext = cur->next; /* Detach the tail past identifier */
4772 cur->next = NULL; /* so that expand_smacro stops here */
4775 tline = expand_smacro(tline);
4777 if (cur) {
4778 /* expand_smacro possibly changhed tline; re-scan for EOL */
4779 cur = tline;
4780 while (cur && cur->next)
4781 cur = cur->next;
4782 if (cur)
4783 cur->next = oldnext;
4786 return tline;
4790 * Determine whether the given line constitutes a multi-line macro
4791 * call, and return the ExpDef structure called if so. Doesn't have
4792 * to check for an initial label - that's taken care of in
4793 * expand_mmacro - but must check numbers of parameters. Guaranteed
4794 * to be called with tline->type == TOK_ID, so the putative macro
4795 * name is easy to find.
4797 static ExpDef *is_mmacro(Token * tline, Token *** params_array)
4799 ExpDef *head, *ed;
4800 Token **params;
4801 int nparam;
4803 head = (ExpDef *) hash_findix(&expdefs, tline->text);
4806 * Efficiency: first we see if any macro exists with the given
4807 * name. If not, we can return NULL immediately. _Then_ we
4808 * count the parameters, and then we look further along the
4809 * list if necessary to find the proper ExpDef.
4811 list_for_each(ed, head)
4812 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4813 break;
4814 if (!ed)
4815 return NULL;
4818 * OK, we have a potential macro. Count and demarcate the
4819 * parameters.
4821 count_mmac_params(tline->next, &nparam, &params);
4824 * So we know how many parameters we've got. Find the ExpDef
4825 * structure that handles this number.
4827 while (ed) {
4828 if (ed->nparam_min <= nparam
4829 && (ed->plus || nparam <= ed->nparam_max)) {
4831 * It's right, and we can use it. Add its default
4832 * parameters to the end of our list if necessary.
4834 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
4835 params =
4836 nasm_realloc(params,
4837 ((ed->nparam_min + ed->ndefs +
4838 1) * sizeof(*params)));
4839 while (nparam < ed->nparam_min + ed->ndefs) {
4840 params[nparam] = ed->defaults[nparam - ed->nparam_min];
4841 nparam++;
4845 * If we've gone over the maximum parameter count (and
4846 * we're in Plus mode), ignore parameters beyond
4847 * nparam_max.
4849 if (ed->plus && nparam > ed->nparam_max)
4850 nparam = ed->nparam_max;
4852 * Then terminate the parameter list, and leave.
4854 if (!params) { /* need this special case */
4855 params = nasm_malloc(sizeof(*params));
4856 nparam = 0;
4858 params[nparam] = NULL;
4859 *params_array = params;
4860 return ed;
4863 * This one wasn't right: look for the next one with the
4864 * same name.
4866 list_for_each(ed, ed->next)
4867 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4868 break;
4872 * After all that, we didn't find one with the right number of
4873 * parameters. Issue a warning, and fail to expand the macro.
4875 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4876 "macro `%s' exists, but not taking %d parameters",
4877 tline->text, nparam);
4878 nasm_free(params);
4879 return NULL;
4883 * Expand the multi-line macro call made by the given line, if
4884 * there is one to be expanded. If there is, push the expansion on
4885 * istk->expansion and return true. Otherwise return false.
4887 static bool expand_mmacro(Token * tline)
4889 Token *label = NULL;
4890 int dont_prepend = 0;
4891 Token **params, *t;
4892 Line *l = NULL;
4893 ExpDef *ed;
4894 ExpInv *ei;
4895 int i, nparam, *paramlen;
4896 const char *mname;
4898 t = tline;
4899 skip_white_(t);
4900 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4901 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4902 return false;
4903 ed = is_mmacro(t, &params);
4904 if (ed != NULL) {
4905 mname = t->text;
4906 } else {
4907 Token *last;
4909 * We have an id which isn't a macro call. We'll assume
4910 * it might be a label; we'll also check to see if a
4911 * colon follows it. Then, if there's another id after
4912 * that lot, we'll check it again for macro-hood.
4914 label = last = t;
4915 t = t->next;
4916 if (tok_type_(t, TOK_WHITESPACE))
4917 last = t, t = t->next;
4918 if (tok_is_(t, ":")) {
4919 dont_prepend = 1;
4920 last = t, t = t->next;
4921 if (tok_type_(t, TOK_WHITESPACE))
4922 last = t, t = t->next;
4924 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4925 return false;
4926 last->next = NULL;
4927 mname = t->text;
4928 tline = t;
4932 * Fix up the parameters: this involves stripping leading and
4933 * trailing whitespace, then stripping braces if they are
4934 * present.
4936 for (nparam = 0; params[nparam]; nparam++) ;
4937 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4939 for (i = 0; params[i]; i++) {
4940 int brace = false;
4941 int comma = (!ed->plus || i < nparam - 1);
4943 t = params[i];
4944 skip_white_(t);
4945 if (tok_is_(t, "{"))
4946 t = t->next, brace = true, comma = false;
4947 params[i] = t;
4948 paramlen[i] = 0;
4949 while (t) {
4950 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4951 break; /* ... because we have hit a comma */
4952 if (comma && t->type == TOK_WHITESPACE
4953 && tok_is_(t->next, ","))
4954 break; /* ... or a space then a comma */
4955 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4956 break; /* ... or a brace */
4957 t = t->next;
4958 paramlen[i]++;
4962 if (ed->cur_depth >= ed->max_depth) {
4963 if (ed->max_depth > 1) {
4964 error(ERR_WARNING,
4965 "reached maximum macro recursion depth of %i for %s",
4966 ed->max_depth,ed->name);
4968 return false;
4969 } else {
4970 ed->cur_depth ++;
4974 * OK, we have found a ExpDef structure representing a
4975 * previously defined mmacro. Create an expansion invocation
4976 * and point it back to the expansion definition. Substitution of
4977 * parameter tokens and macro-local tokens doesn't get done
4978 * until the single-line macro substitution process; this is
4979 * because delaying them allows us to change the semantics
4980 * later through %rotate.
4982 ei = new_ExpInv(EXP_MMACRO, ed);
4983 ei->name = nasm_strdup(mname);
4984 //ei->label = label;
4985 //ei->label_text = detoken(label, false);
4986 ei->current = ed->line;
4987 ei->emitting = true;
4988 //ei->iline = tline;
4989 ei->params = params;
4990 ei->nparam = nparam;
4991 ei->rotate = 0;
4992 ei->paramlen = paramlen;
4993 ei->lineno = 0;
4995 ei->prev = istk->expansion;
4996 istk->expansion = ei;
4999 * Special case: detect %00 on first invocation; if found,
5000 * avoid emitting any labels that precede the mmacro call.
5001 * ed->prepend is set to -1 when %00 is detected, else 1.
5003 if (ed->prepend == 0) {
5004 for (l = ed->line; l != NULL; l = l->next) {
5005 for (t = l->first; t != NULL; t = t->next) {
5006 if ((t->type == TOK_PREPROC_ID) &&
5007 (strlen(t->text) == 3) &&
5008 (t->text[1] == '0') && (t->text[2] == '0')) {
5009 dont_prepend = -1;
5010 break;
5013 if (dont_prepend < 0) {
5014 break;
5017 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5021 * If we had a label, push it on as the first line of
5022 * the macro expansion.
5024 if (label != NULL) {
5025 if (ed->prepend < 0) {
5026 ei->label_text = detoken(label, false);
5027 } else {
5028 if (dont_prepend == 0) {
5029 t = label;
5030 while (t->next != NULL) {
5031 t = t->next;
5033 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5035 l = new_Line();
5036 l->first = copy_Token(label);
5037 l->next = ei->current;
5038 ei->current = l;
5042 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
5044 istk->mmac_depth++;
5045 return true;
5048 /* The function that actually does the error reporting */
5049 static void verror(int severity, const char *fmt, va_list arg)
5051 char buff[1024];
5053 vsnprintf(buff, sizeof(buff), fmt, arg);
5055 if (istk && istk->mmac_depth > 0) {
5056 ExpInv *ei = istk->expansion;
5057 int lineno = ei->lineno;
5058 while (ei) {
5059 if (ei->type == EXP_MMACRO)
5060 break;
5061 lineno += ei->relno;
5062 ei = ei->prev;
5064 nasm_error(severity, "(%s:%d) %s", ei->def->name,
5065 lineno, buff);
5066 } else
5067 nasm_error(severity, "%s", buff);
5071 * Since preprocessor always operate only on the line that didn't
5072 * arrived yet, we should always use ERR_OFFBY1.
5074 static void error(int severity, const char *fmt, ...)
5076 va_list arg;
5077 va_start(arg, fmt);
5078 verror(severity, fmt, arg);
5079 va_end(arg);
5083 * Because %else etc are evaluated in the state context
5084 * of the previous branch, errors might get lost with error():
5085 * %if 0 ... %else trailing garbage ... %endif
5086 * So %else etc should report errors with this function.
5088 static void error_precond(int severity, const char *fmt, ...)
5090 va_list arg;
5092 /* Only ignore the error if it's really in a dead branch */
5093 if ((istk != NULL) &&
5094 (istk->expansion != NULL) &&
5095 (istk->expansion->type == EXP_IF) &&
5096 (istk->expansion->def->state == COND_NEVER))
5097 return;
5099 va_start(arg, fmt);
5100 verror(severity, fmt, arg);
5101 va_end(arg);
5104 static void
5105 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
5107 Token *t;
5109 cstk = NULL;
5110 istk = nasm_zalloc(sizeof(Include));
5111 istk->fp = fopen(file, "r");
5112 src_set_fname(nasm_strdup(file));
5113 src_set_linnum(0);
5114 istk->lineinc = 1;
5115 if (!istk->fp)
5116 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
5117 file);
5118 defining = NULL;
5119 finals = NULL;
5120 in_final = false;
5121 nested_mac_count = 0;
5122 nested_rep_count = 0;
5123 init_macros();
5124 unique = 0;
5125 if (tasm_compatible_mode) {
5126 stdmacpos = nasm_stdmac;
5127 } else {
5128 stdmacpos = nasm_stdmac_after_tasm;
5130 any_extrastdmac = extrastdmac && *extrastdmac;
5131 do_predef = true;
5132 list = listgen;
5135 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5136 * The caller, however, will also pass in 3 for preprocess-only so
5137 * we can set __PASS__ accordingly.
5139 pass = apass > 2 ? 2 : apass;
5141 dephead = deptail = deplist;
5142 if (deplist) {
5143 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5144 sl->next = NULL;
5145 strcpy(sl->str, file);
5146 *deptail = sl;
5147 deptail = &sl->next;
5151 * Define the __PASS__ macro. This is defined here unlike
5152 * all the other builtins, because it is special -- it varies between
5153 * passes.
5155 t = nasm_zalloc(sizeof(*t));
5156 make_tok_num(t, apass);
5157 define_smacro(NULL, "__PASS__", true, 0, t);
5160 static char *pp_getline(void)
5162 char *line;
5163 Token *tline;
5164 ExpDef *ed;
5165 ExpInv *ei;
5166 Line *l;
5167 int j;
5169 while (1) {
5171 * Fetch a tokenized line, either from the expansion
5172 * buffer or from the input file.
5174 tline = NULL;
5176 while (1) { /* until we get a line we can use */
5178 * Fetch a tokenized line from the expansion buffer
5180 if (istk->expansion != NULL) {
5181 ei = istk->expansion;
5182 if (ei->current != NULL) {
5183 if (ei->emitting == false) {
5184 ei->current = NULL;
5185 continue;
5187 l = ei->current;
5188 ei->current = l->next;
5189 ei->lineno++;
5190 tline = copy_Token(l->first);
5191 if (((ei->type == EXP_REP) ||
5192 (ei->type == EXP_MMACRO) ||
5193 (ei->type == EXP_WHILE))
5194 && (ei->def->nolist == false)) {
5195 char *p = detoken(tline, false);
5196 list->line(LIST_MACRO, p);
5197 nasm_free(p);
5199 if (ei->linnum > -1) {
5200 src_set_linnum(src_get_linnum() + 1);
5202 break;
5203 } else if ((ei->type == EXP_REP) &&
5204 (ei->def->cur_depth < ei->def->max_depth)) {
5205 ei->def->cur_depth ++;
5206 ei->current = ei->def->line;
5207 ei->lineno = 0;
5208 continue;
5209 } else if ((ei->type == EXP_WHILE) &&
5210 (ei->def->cur_depth < ei->def->max_depth)) {
5211 ei->current = ei->def->line;
5212 ei->lineno = 0;
5213 tline = copy_Token(ei->current->first);
5214 j = if_condition(tline, PP_WHILE);
5215 tline = NULL;
5216 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5217 if (j == COND_IF_TRUE) {
5218 ei->current = ei->current->next;
5219 ei->def->cur_depth ++;
5220 } else {
5221 ei->emitting = false;
5222 ei->current = NULL;
5223 ei->def->cur_depth = ei->def->max_depth;
5225 continue;
5226 } else {
5227 istk->expansion = ei->prev;
5228 ed = ei->def;
5229 if (ed != NULL) {
5230 if ((ei->emitting == true) &&
5231 (ed->max_depth == DEADMAN_LIMIT) &&
5232 (ed->cur_depth == DEADMAN_LIMIT)
5234 error(ERR_FATAL, "runaway expansion detected, aborting");
5236 if (ed->cur_depth > 0) {
5237 ed->cur_depth --;
5238 } else if (ed->type != EXP_MMACRO) {
5239 expansions = ed->prev;
5240 free_expdef(ed);
5242 if ((ei->type == EXP_REP) ||
5243 (ei->type == EXP_MMACRO) ||
5244 (ei->type == EXP_WHILE)) {
5245 list->downlevel(LIST_MACRO);
5246 if (ei->type == EXP_MMACRO) {
5247 istk->mmac_depth--;
5251 if (ei->linnum > -1) {
5252 src_set_linnum(ei->linnum);
5254 free_expinv(ei);
5255 continue;
5260 * Read in line from input and tokenize
5262 line = read_line();
5263 if (line) { /* from the current input file */
5264 line = prepreproc(line);
5265 tline = tokenize(line);
5266 nasm_free(line);
5267 break;
5271 * The current file has ended; work down the istk
5274 Include *i = istk;
5275 fclose(i->fp);
5276 if (i->expansion != NULL) {
5277 error(ERR_FATAL,
5278 "end of file while still in an expansion");
5280 /* only set line and file name if there's a next node */
5281 if (i->next) {
5282 src_set_linnum(i->lineno);
5283 nasm_free(src_set_fname(nasm_strdup(i->fname)));
5285 if ((i->next == NULL) && (finals != NULL)) {
5286 in_final = true;
5287 ei = new_ExpInv(EXP_FINAL, NULL);
5288 ei->emitting = true;
5289 ei->current = finals;
5290 istk->expansion = ei;
5291 finals = NULL;
5292 continue;
5294 istk = i->next;
5295 list->downlevel(LIST_INCLUDE);
5296 nasm_free(i);
5297 if (istk == NULL) {
5298 if (finals != NULL) {
5299 in_final = true;
5300 } else {
5301 return NULL;
5304 continue;
5308 if (defining == NULL) {
5309 tline = expand_mmac_params(tline);
5313 * Check the line to see if it's a preprocessor directive.
5315 if (do_directive(tline) == DIRECTIVE_FOUND) {
5316 continue;
5317 } else if (defining != NULL) {
5319 * We're defining an expansion. We emit nothing at all,
5320 * and just shove the tokenized line on to the definition.
5322 if (defining->ignoring == false) {
5323 Line *l = new_Line();
5324 l->first = tline;
5325 if (defining->line == NULL) {
5326 defining->line = l;
5327 defining->last = l;
5328 } else {
5329 defining->last->next = l;
5330 defining->last = l;
5332 } else {
5333 free_tlist(tline);
5335 defining->linecount++;
5336 continue;
5337 } else if ((istk->expansion != NULL) &&
5338 (istk->expansion->emitting != true)) {
5340 * We're in a non-emitting branch of an expansion.
5341 * Emit nothing at all, not even a blank line: when we
5342 * emerge from the expansion we'll give a line-number
5343 * directive so we keep our place correctly.
5345 free_tlist(tline);
5346 continue;
5347 } else {
5348 tline = expand_smacro(tline);
5349 if (expand_mmacro(tline) != true) {
5351 * De-tokenize the line again, and emit it.
5353 line = detoken(tline, true);
5354 free_tlist(tline);
5355 break;
5356 } else {
5357 continue;
5361 return line;
5364 static void pp_cleanup(int pass)
5366 if (defining != NULL) {
5367 error(ERR_NONFATAL, "end of file while still defining an expansion");
5368 while (defining != NULL) {
5369 ExpDef *ed = defining;
5370 defining = ed->prev;
5371 free_expdef(ed);
5373 defining = NULL;
5375 while (cstk != NULL)
5376 ctx_pop();
5377 free_macros();
5378 while (istk != NULL) {
5379 Include *i = istk;
5380 istk = istk->next;
5381 fclose(i->fp);
5382 nasm_free(i->fname);
5383 while (i->expansion != NULL) {
5384 ExpInv *ei = i->expansion;
5385 i->expansion = ei->prev;
5386 free_expinv(ei);
5388 nasm_free(i);
5390 while (cstk)
5391 ctx_pop();
5392 nasm_free(src_set_fname(NULL));
5393 if (pass == 0) {
5394 IncPath *i;
5395 free_llist(predef);
5396 delete_Blocks();
5397 while ((i = ipath)) {
5398 ipath = i->next;
5399 nasm_free(i->path);
5400 nasm_free(i);
5405 void pp_include_path(char *path)
5407 IncPath *i = nasm_zalloc(sizeof(IncPath));
5409 if (path)
5410 i->path = nasm_strdup(path);
5412 if (ipath) {
5413 IncPath *j = ipath;
5414 while (j->next)
5415 j = j->next;
5416 j->next = i;
5417 } else {
5418 ipath = i;
5422 void pp_pre_include(char *fname)
5424 Token *inc, *space, *name;
5425 Line *l;
5427 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5428 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5429 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5431 l = new_Line();
5432 l->next = predef;
5433 l->first = inc;
5434 predef = l;
5437 void pp_pre_define(char *definition)
5439 Token *def, *space;
5440 Line *l;
5441 char *equals;
5443 equals = strchr(definition, '=');
5444 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5445 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5446 if (equals)
5447 *equals = ' ';
5448 space->next = tokenize(definition);
5449 if (equals)
5450 *equals = '=';
5452 l = new_Line();
5453 l->next = predef;
5454 l->first = def;
5455 predef = l;
5458 void pp_pre_undefine(char *definition)
5460 Token *def, *space;
5461 Line *l;
5463 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5464 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5465 space->next = tokenize(definition);
5467 l = new_Line();
5468 l->next = predef;
5469 l->first = def;
5470 predef = l;
5474 * This function is used to assist with "runtime" preprocessor
5475 * directives, e.g. pp_runtime("%define __BITS__ 64");
5477 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5478 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5481 void pp_runtime(char *definition)
5483 Token *def;
5485 def = tokenize(definition);
5486 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5487 free_tlist(def);
5491 void pp_extra_stdmac(macros_t *macros)
5493 extrastdmac = macros;
5496 static void make_tok_num(Token * tok, int64_t val)
5498 char numbuf[20];
5499 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5500 tok->text = nasm_strdup(numbuf);
5501 tok->type = TOK_NUMBER;
5504 struct preproc_ops nasmpp = {
5505 pp_reset,
5506 pp_getline,
5507 pp_cleanup