BR3041451: Implement upper bound for %rep counter
[nasm.git] / preproc.c
blob64c481ad74a29689890ef08ee888c48443effcfb
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
48 * }
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
63 #include "compiler.h"
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <string.h>
70 #include <ctype.h>
71 #include <limits.h>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
84 typedef struct SMacro SMacro;
85 typedef struct MMacro MMacro;
86 typedef struct MMacroInvocation MMacroInvocation;
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 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
133 struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
160 /* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
163 struct MMacroInvocation {
164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
170 uint64_t condcnt;
175 * The context stack is composed of a linked list of these.
177 struct Context {
178 Context *next;
179 char *name;
180 struct hash_table localmac;
181 uint32_t number;
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
192 * %define a(x,y) ( (x) & ~(y) )
194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
203 enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
215 struct Token {
216 Token *next;
217 char *text;
218 union {
219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
222 enum pp_token_type type;
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
247 struct Line {
248 Line *next;
249 MMacro *finishes;
250 Token *first;
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
257 struct Include {
258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
262 char *fname;
263 int lineno, lineinc;
264 MMacro *mstk; /* stack of active macros/reps */
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
272 struct IncPath {
273 IncPath *next;
274 char *path;
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
284 struct Cond {
285 Cond *next;
286 int state;
288 enum {
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
297 COND_IF_TRUE, COND_IF_FALSE,
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
313 COND_DONE, COND_NEVER
315 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318 * These defines are used as the possible return values for do_directive
320 #define NO_DIRECTIVE_FOUND 0
321 #define DIRECTIVE_FOUND 1
324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
327 #define DEADMAN_LIMIT (1 << 20)
329 /* max reps */
330 #define REP_LIMIT ((INT64_C(1) << 62))
333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
338 static const char * const conditions[] = {
339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
343 enum pp_conds {
344 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
345 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
346 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
347 c_none = -1
349 static const enum pp_conds inverse_ccs[] = {
350 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
351 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,
352 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
356 * Directive names.
358 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
359 static int is_condition(enum preproc_token arg)
361 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
364 /* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
370 enum {
371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375 static const char * const tasm_directives[] = {
376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
380 static int StackSize = 4;
381 static char *StackPointer = "ebp";
382 static int ArgOffset = 8;
383 static int LocalOffset = 0;
385 static Context *cstk;
386 static Include *istk;
387 static IncPath *ipath = NULL;
389 static int pass; /* HACK: pass 0 = generate dependencies only */
390 static StrList **dephead, **deptail; /* Dependency list */
392 static uint64_t unique; /* unique identifier numbers */
394 static Line *predef = NULL;
395 static bool do_predef;
397 static ListGen *list;
400 * The current set of multi-line macros we have defined.
402 static struct hash_table mmacros;
405 * The current set of single-line macros we have defined.
407 static struct hash_table smacros;
410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
413 static MMacro *defining;
415 static uint64_t nested_mac_count;
416 static uint64_t nested_rep_count;
419 * The number of macro parameters to allocate space for at a time.
421 #define PARAM_DELTA 16
424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
427 static macros_t *stdmacpos;
430 * The extra standard macros that come from the object format, if
431 * any.
433 static macros_t *extrastdmac = NULL;
434 static bool any_extrastdmac;
437 * Tokens are allocated in blocks to improve speed
439 #define TOKEN_BLOCKSIZE 4096
440 static Token *freeTokens = NULL;
441 struct Blocks {
442 Blocks *next;
443 void *chunk;
446 static Blocks blocks = { NULL, NULL };
449 * Forward declarations.
451 static Token *expand_mmac_params(Token * tline);
452 static Token *expand_smacro(Token * tline);
453 static Token *expand_id(Token * tline);
454 static Context *get_ctx(const char *name, const char **namep,
455 bool all_contexts);
456 static void make_tok_num(Token * tok, int64_t val);
457 static void error(int severity, const char *fmt, ...);
458 static void error_precond(int severity, const char *fmt, ...);
459 static void *new_Block(size_t size);
460 static void delete_Blocks(void);
461 static Token *new_Token(Token * next, enum pp_token_type type,
462 const char *text, int txtlen);
463 static Token *delete_Token(Token * t);
466 * Macros for safe checking of token pointers, avoid *(NULL)
468 #define tok_type_(x,t) ((x) && (x)->type == (t))
469 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
478 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
480 size_t len = nasm_unquote(qstr, NULL);
481 size_t clen = strlen(qstr);
483 if (len != clen)
484 error(ERR_NONFATAL, "NUL character in `%s' directive",
485 pp_directives[directive]);
487 return clen;
491 * Handle TASM specific directives, which do not contain a % in
492 * front of them. We do it here because I could not find any other
493 * place to do it for the moment, and it is a hack (ideally it would
494 * be nice to be able to use the NASM pre-processor to do it).
496 static char *check_tasm_directive(char *line)
498 int32_t i, j, k, m, len;
499 char *p, *q, *oldline, oldchar;
501 p = nasm_skip_spaces(line);
503 /* Binary search for the directive name */
504 i = -1;
505 j = ARRAY_SIZE(tasm_directives);
506 q = nasm_skip_word(p);
507 len = q - p;
508 if (len) {
509 oldchar = p[len];
510 p[len] = 0;
511 while (j - i > 1) {
512 k = (j + i) / 2;
513 m = nasm_stricmp(p, tasm_directives[k]);
514 if (m == 0) {
515 /* We have found a directive, so jam a % in front of it
516 * so that NASM will then recognise it as one if it's own.
518 p[len] = oldchar;
519 len = strlen(p);
520 oldline = line;
521 line = nasm_malloc(len + 2);
522 line[0] = '%';
523 if (k == TM_IFDIFI) {
525 * NASM does not recognise IFDIFI, so we convert
526 * it to %if 0. This is not used in NASM
527 * compatible code, but does need to parse for the
528 * TASM macro package.
530 strcpy(line + 1, "if 0");
531 } else {
532 memcpy(line + 1, p, len + 1);
534 nasm_free(oldline);
535 return line;
536 } else if (m < 0) {
537 j = k;
538 } else
539 i = k;
541 p[len] = oldchar;
543 return line;
547 * The pre-preprocessing stage... This function translates line
548 * number indications as they emerge from GNU cpp (`# lineno "file"
549 * flags') into NASM preprocessor line number indications (`%line
550 * lineno file').
552 static char *prepreproc(char *line)
554 int lineno, fnlen;
555 char *fname, *oldline;
557 if (line[0] == '#' && line[1] == ' ') {
558 oldline = line;
559 fname = oldline + 2;
560 lineno = atoi(fname);
561 fname += strspn(fname, "0123456789 ");
562 if (*fname == '"')
563 fname++;
564 fnlen = strcspn(fname, "\"");
565 line = nasm_malloc(20 + fnlen);
566 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
567 nasm_free(oldline);
569 if (tasm_compatible_mode)
570 return check_tasm_directive(line);
571 return line;
575 * Free a linked list of tokens.
577 static void free_tlist(Token * list)
579 while (list)
580 list = delete_Token(list);
584 * Free a linked list of lines.
586 static void free_llist(Line * list)
588 Line *l, *tmp;
589 list_for_each_safe(l, tmp, list) {
590 free_tlist(l->first);
591 nasm_free(l);
596 * Free an MMacro
598 static void free_mmacro(MMacro * m)
600 nasm_free(m->name);
601 free_tlist(m->dlist);
602 nasm_free(m->defaults);
603 free_llist(m->expansion);
604 nasm_free(m);
608 * Free all currently defined macros, and free the hash tables
610 static void free_smacro_table(struct hash_table *smt)
612 SMacro *s, *tmp;
613 const char *key;
614 struct hash_tbl_node *it = NULL;
616 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
617 nasm_free((void *)key);
618 list_for_each_safe(s, tmp, s) {
619 nasm_free(s->name);
620 free_tlist(s->expansion);
621 nasm_free(s);
624 hash_free(smt);
627 static void free_mmacro_table(struct hash_table *mmt)
629 MMacro *m, *tmp;
630 const char *key;
631 struct hash_tbl_node *it = NULL;
633 it = NULL;
634 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
635 nasm_free((void *)key);
636 list_for_each_safe(m ,tmp, m)
637 free_mmacro(m);
639 hash_free(mmt);
642 static void free_macros(void)
644 free_smacro_table(&smacros);
645 free_mmacro_table(&mmacros);
649 * Initialize the hash tables
651 static void init_macros(void)
653 hash_init(&smacros, HASH_LARGE);
654 hash_init(&mmacros, HASH_LARGE);
658 * Pop the context stack.
660 static void ctx_pop(void)
662 Context *c = cstk;
664 cstk = cstk->next;
665 free_smacro_table(&c->localmac);
666 nasm_free(c->name);
667 nasm_free(c);
671 * Search for a key in the hash index; adding it if necessary
672 * (in which case we initialize the data pointer to NULL.)
674 static void **
675 hash_findi_add(struct hash_table *hash, const char *str)
677 struct hash_insert hi;
678 void **r;
679 char *strx;
681 r = hash_findi(hash, str, &hi);
682 if (r)
683 return r;
685 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
686 return hash_add(&hi, strx, NULL);
690 * Like hash_findi, but returns the data element rather than a pointer
691 * to it. Used only when not adding a new element, hence no third
692 * argument.
694 static void *
695 hash_findix(struct hash_table *hash, const char *str)
697 void **p;
699 p = hash_findi(hash, str, NULL);
700 return p ? *p : NULL;
704 * read line from standart macros set,
705 * if there no more left -- return NULL
707 static char *line_from_stdmac(void)
709 unsigned char c;
710 const unsigned char *p = stdmacpos;
711 char *line, *q;
712 size_t len = 0;
714 if (!stdmacpos)
715 return NULL;
717 while ((c = *p++)) {
718 if (c >= 0x80)
719 len += pp_directives_len[c - 0x80] + 1;
720 else
721 len++;
724 line = nasm_malloc(len + 1);
725 q = line;
726 while ((c = *stdmacpos++)) {
727 if (c >= 0x80) {
728 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
729 q += pp_directives_len[c - 0x80];
730 *q++ = ' ';
731 } else {
732 *q++ = c;
735 stdmacpos = p;
736 *q = '\0';
738 if (!*stdmacpos) {
739 /* This was the last of the standard macro chain... */
740 stdmacpos = NULL;
741 if (any_extrastdmac) {
742 stdmacpos = extrastdmac;
743 any_extrastdmac = false;
744 } else if (do_predef) {
745 Line *pd, *l;
746 Token *head, **tail, *t;
749 * Nasty hack: here we push the contents of
750 * `predef' on to the top-level expansion stack,
751 * since this is the most convenient way to
752 * implement the pre-include and pre-define
753 * features.
755 list_for_each(pd, predef) {
756 head = NULL;
757 tail = &head;
758 list_for_each(t, pd->first) {
759 *tail = new_Token(NULL, t->type, t->text, 0);
760 tail = &(*tail)->next;
763 l = nasm_malloc(sizeof(Line));
764 l->next = istk->expansion;
765 l->first = head;
766 l->finishes = NULL;
768 istk->expansion = l;
770 do_predef = false;
774 return line;
777 #define BUF_DELTA 512
779 * Read a line from the top file in istk, handling multiple CR/LFs
780 * at the end of the line read, and handling spurious ^Zs. Will
781 * return lines from the standard macro set if this has not already
782 * been done.
784 static char *read_line(void)
786 char *buffer, *p, *q;
787 int bufsize, continued_count;
790 * standart macros set (predefined) goes first
792 p = line_from_stdmac();
793 if (p)
794 return p;
797 * regular read from a file
799 bufsize = BUF_DELTA;
800 buffer = nasm_malloc(BUF_DELTA);
801 p = buffer;
802 continued_count = 0;
803 while (1) {
804 q = fgets(p, bufsize - (p - buffer), istk->fp);
805 if (!q)
806 break;
807 p += strlen(p);
808 if (p > buffer && p[-1] == '\n') {
810 * Convert backslash-CRLF line continuation sequences into
811 * nothing at all (for DOS and Windows)
813 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
814 p -= 3;
815 *p = 0;
816 continued_count++;
819 * Also convert backslash-LF line continuation sequences into
820 * nothing at all (for Unix)
822 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
823 p -= 2;
824 *p = 0;
825 continued_count++;
826 } else {
827 break;
830 if (p - buffer > bufsize - 10) {
831 int32_t offset = p - buffer;
832 bufsize += BUF_DELTA;
833 buffer = nasm_realloc(buffer, bufsize);
834 p = buffer + offset; /* prevent stale-pointer problems */
838 if (!q && p == buffer) {
839 nasm_free(buffer);
840 return NULL;
843 src_set_linnum(src_get_linnum() + istk->lineinc +
844 (continued_count * istk->lineinc));
847 * Play safe: remove CRs as well as LFs, if any of either are
848 * present at the end of the line.
850 while (--p >= buffer && (*p == '\n' || *p == '\r'))
851 *p = '\0';
854 * Handle spurious ^Z, which may be inserted into source files
855 * by some file transfer utilities.
857 buffer[strcspn(buffer, "\032")] = '\0';
859 list->line(LIST_READ, buffer);
861 return buffer;
865 * Tokenize a line of text. This is a very simple process since we
866 * don't need to parse the value out of e.g. numeric tokens: we
867 * simply split one string into many.
869 static Token *tokenize(char *line)
871 char c, *p = line;
872 enum pp_token_type type;
873 Token *list = NULL;
874 Token *t, **tail = &list;
876 while (*line) {
877 p = line;
878 if (*p == '%') {
879 p++;
880 if (*p == '+' && !nasm_isdigit(p[1])) {
881 p++;
882 type = TOK_PASTE;
883 } else if (nasm_isdigit(*p) ||
884 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
885 do {
886 p++;
888 while (nasm_isdigit(*p));
889 type = TOK_PREPROC_ID;
890 } else if (*p == '{') {
891 p++;
892 while (*p && *p != '}') {
893 p[-1] = *p;
894 p++;
896 p[-1] = '\0';
897 if (*p)
898 p++;
899 type = TOK_PREPROC_ID;
900 } else if (*p == '[') {
901 int lvl = 1;
902 line += 2; /* Skip the leading %[ */
903 p++;
904 while (lvl && (c = *p++)) {
905 switch (c) {
906 case ']':
907 lvl--;
908 break;
909 case '%':
910 if (*p == '[')
911 lvl++;
912 break;
913 case '\'':
914 case '\"':
915 case '`':
916 p = nasm_skip_string(p - 1) + 1;
917 break;
918 default:
919 break;
922 p--;
923 if (*p)
924 *p++ = '\0';
925 if (lvl)
926 error(ERR_NONFATAL, "unterminated %[ construct");
927 type = TOK_INDIRECT;
928 } else if (*p == '?') {
929 type = TOK_PREPROC_Q; /* %? */
930 p++;
931 if (*p == '?') {
932 type = TOK_PREPROC_QQ; /* %?? */
933 p++;
935 } else if (*p == '!') {
936 type = TOK_PREPROC_ID;
937 p++;
938 if (isidchar(*p)) {
939 do {
940 p++;
942 while (isidchar(*p));
943 } else if (*p == '\'' || *p == '\"' || *p == '`') {
944 p = nasm_skip_string(p);
945 if (*p)
946 p++;
947 else
948 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
949 } else {
950 /* %! without string or identifier */
951 type = TOK_OTHER; /* Legacy behavior... */
953 } else if (isidchar(*p) ||
954 ((*p == '!' || *p == '%' || *p == '$') &&
955 isidchar(p[1]))) {
956 do {
957 p++;
959 while (isidchar(*p));
960 type = TOK_PREPROC_ID;
961 } else {
962 type = TOK_OTHER;
963 if (*p == '%')
964 p++;
966 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
967 type = TOK_ID;
968 p++;
969 while (*p && isidchar(*p))
970 p++;
971 } else if (*p == '\'' || *p == '"' || *p == '`') {
973 * A string token.
975 type = TOK_STRING;
976 p = nasm_skip_string(p);
978 if (*p) {
979 p++;
980 } else {
981 error(ERR_WARNING|ERR_PASS1, "unterminated string");
982 /* Handling unterminated strings by UNV */
983 /* type = -1; */
985 } else if (p[0] == '$' && p[1] == '$') {
986 type = TOK_OTHER; /* TOKEN_BASE */
987 p += 2;
988 } else if (isnumstart(*p)) {
989 bool is_hex = false;
990 bool is_float = false;
991 bool has_e = false;
992 char c, *r;
995 * A numeric token.
998 if (*p == '$') {
999 p++;
1000 is_hex = true;
1003 for (;;) {
1004 c = *p++;
1006 if (!is_hex && (c == 'e' || c == 'E')) {
1007 has_e = true;
1008 if (*p == '+' || *p == '-') {
1010 * e can only be followed by +/- if it is either a
1011 * prefixed hex number or a floating-point number
1013 p++;
1014 is_float = true;
1016 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1017 is_hex = true;
1018 } else if (c == 'P' || c == 'p') {
1019 is_float = true;
1020 if (*p == '+' || *p == '-')
1021 p++;
1022 } else if (isnumchar(c) || c == '_')
1023 ; /* just advance */
1024 else if (c == '.') {
1026 * we need to deal with consequences of the legacy
1027 * parser, like "1.nolist" being two tokens
1028 * (TOK_NUMBER, TOK_ID) here; at least give it
1029 * a shot for now. In the future, we probably need
1030 * a flex-based scanner with proper pattern matching
1031 * to do it as well as it can be done. Nothing in
1032 * the world is going to help the person who wants
1033 * 0x123.p16 interpreted as two tokens, though.
1035 r = p;
1036 while (*r == '_')
1037 r++;
1039 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1040 (!is_hex && (*r == 'e' || *r == 'E')) ||
1041 (*r == 'p' || *r == 'P')) {
1042 p = r;
1043 is_float = true;
1044 } else
1045 break; /* Terminate the token */
1046 } else
1047 break;
1049 p--; /* Point to first character beyond number */
1051 if (p == line+1 && *line == '$') {
1052 type = TOK_OTHER; /* TOKEN_HERE */
1053 } else {
1054 if (has_e && !is_hex) {
1055 /* 1e13 is floating-point, but 1e13h is not */
1056 is_float = true;
1059 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1061 } else if (nasm_isspace(*p)) {
1062 type = TOK_WHITESPACE;
1063 p = nasm_skip_spaces(p);
1065 * Whitespace just before end-of-line is discarded by
1066 * pretending it's a comment; whitespace just before a
1067 * comment gets lumped into the comment.
1069 if (!*p || *p == ';') {
1070 type = TOK_COMMENT;
1071 while (*p)
1072 p++;
1074 } else if (*p == ';') {
1075 type = TOK_COMMENT;
1076 while (*p)
1077 p++;
1078 } else {
1080 * Anything else is an operator of some kind. We check
1081 * for all the double-character operators (>>, <<, //,
1082 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1083 * else is a single-character operator.
1085 type = TOK_OTHER;
1086 if ((p[0] == '>' && p[1] == '>') ||
1087 (p[0] == '<' && p[1] == '<') ||
1088 (p[0] == '/' && p[1] == '/') ||
1089 (p[0] == '<' && p[1] == '=') ||
1090 (p[0] == '>' && p[1] == '=') ||
1091 (p[0] == '=' && p[1] == '=') ||
1092 (p[0] == '!' && p[1] == '=') ||
1093 (p[0] == '<' && p[1] == '>') ||
1094 (p[0] == '&' && p[1] == '&') ||
1095 (p[0] == '|' && p[1] == '|') ||
1096 (p[0] == '^' && p[1] == '^')) {
1097 p++;
1099 p++;
1102 /* Handling unterminated string by UNV */
1103 /*if (type == -1)
1105 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1106 t->text[p-line] = *line;
1107 tail = &t->next;
1109 else */
1110 if (type != TOK_COMMENT) {
1111 *tail = t = new_Token(NULL, type, line, p - line);
1112 tail = &t->next;
1114 line = p;
1116 return list;
1120 * this function allocates a new managed block of memory and
1121 * returns a pointer to the block. The managed blocks are
1122 * deleted only all at once by the delete_Blocks function.
1124 static void *new_Block(size_t size)
1126 Blocks *b = &blocks;
1128 /* first, get to the end of the linked list */
1129 while (b->next)
1130 b = b->next;
1131 /* now allocate the requested chunk */
1132 b->chunk = nasm_malloc(size);
1134 /* now allocate a new block for the next request */
1135 b->next = nasm_malloc(sizeof(Blocks));
1136 /* and initialize the contents of the new block */
1137 b->next->next = NULL;
1138 b->next->chunk = NULL;
1139 return b->chunk;
1143 * this function deletes all managed blocks of memory
1145 static void delete_Blocks(void)
1147 Blocks *a, *b = &blocks;
1150 * keep in mind that the first block, pointed to by blocks
1151 * is a static and not dynamically allocated, so we don't
1152 * free it.
1154 while (b) {
1155 if (b->chunk)
1156 nasm_free(b->chunk);
1157 a = b;
1158 b = b->next;
1159 if (a != &blocks)
1160 nasm_free(a);
1165 * this function creates a new Token and passes a pointer to it
1166 * back to the caller. It sets the type and text elements, and
1167 * also the a.mac and next elements to NULL.
1169 static Token *new_Token(Token * next, enum pp_token_type type,
1170 const char *text, int txtlen)
1172 Token *t;
1173 int i;
1175 if (!freeTokens) {
1176 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1177 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1178 freeTokens[i].next = &freeTokens[i + 1];
1179 freeTokens[i].next = NULL;
1181 t = freeTokens;
1182 freeTokens = t->next;
1183 t->next = next;
1184 t->a.mac = NULL;
1185 t->type = type;
1186 if (type == TOK_WHITESPACE || !text) {
1187 t->text = NULL;
1188 } else {
1189 if (txtlen == 0)
1190 txtlen = strlen(text);
1191 t->text = nasm_malloc(txtlen+1);
1192 memcpy(t->text, text, txtlen);
1193 t->text[txtlen] = '\0';
1195 return t;
1198 static Token *delete_Token(Token * t)
1200 Token *next = t->next;
1201 nasm_free(t->text);
1202 t->next = freeTokens;
1203 freeTokens = t;
1204 return next;
1208 * Convert a line of tokens back into text.
1209 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1210 * will be transformed into ..@ctxnum.xxx
1212 static char *detoken(Token * tlist, bool expand_locals)
1214 Token *t;
1215 char *line, *p;
1216 const char *q;
1217 int len = 0;
1219 list_for_each(t, tlist) {
1220 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1221 char *v;
1222 char *q = t->text;
1224 v = t->text + 2;
1225 if (*v == '\'' || *v == '\"' || *v == '`') {
1226 size_t len = nasm_unquote(v, NULL);
1227 size_t clen = strlen(v);
1229 if (len != clen) {
1230 error(ERR_NONFATAL | ERR_PASS1,
1231 "NUL character in %! string");
1232 v = NULL;
1236 if (v) {
1237 char *p = getenv(v);
1238 if (!p) {
1239 error(ERR_NONFATAL | ERR_PASS1,
1240 "nonexistent environment variable `%s'", v);
1241 p = "";
1243 t->text = nasm_strdup(p);
1245 nasm_free(q);
1248 /* Expand local macros here and not during preprocessing */
1249 if (expand_locals &&
1250 t->type == TOK_PREPROC_ID && t->text &&
1251 t->text[0] == '%' && t->text[1] == '$') {
1252 const char *q;
1253 char *p;
1254 Context *ctx = get_ctx(t->text, &q, false);
1255 if (ctx) {
1256 char buffer[40];
1257 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1258 p = nasm_strcat(buffer, q);
1259 nasm_free(t->text);
1260 t->text = p;
1263 if (t->type == TOK_WHITESPACE)
1264 len++;
1265 else if (t->text)
1266 len += strlen(t->text);
1269 p = line = nasm_malloc(len + 1);
1271 list_for_each(t, tlist) {
1272 if (t->type == TOK_WHITESPACE) {
1273 *p++ = ' ';
1274 } else if (t->text) {
1275 q = t->text;
1276 while (*q)
1277 *p++ = *q++;
1280 *p = '\0';
1282 return line;
1286 * A scanner, suitable for use by the expression evaluator, which
1287 * operates on a line of Tokens. Expects a pointer to a pointer to
1288 * the first token in the line to be passed in as its private_data
1289 * field.
1291 * FIX: This really needs to be unified with stdscan.
1293 static int ppscan(void *private_data, struct tokenval *tokval)
1295 Token **tlineptr = private_data;
1296 Token *tline;
1297 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1299 do {
1300 tline = *tlineptr;
1301 *tlineptr = tline ? tline->next : NULL;
1302 } while (tline && (tline->type == TOK_WHITESPACE ||
1303 tline->type == TOK_COMMENT));
1305 if (!tline)
1306 return tokval->t_type = TOKEN_EOS;
1308 tokval->t_charptr = tline->text;
1310 if (tline->text[0] == '$' && !tline->text[1])
1311 return tokval->t_type = TOKEN_HERE;
1312 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1313 return tokval->t_type = TOKEN_BASE;
1315 if (tline->type == TOK_ID) {
1316 p = tokval->t_charptr = tline->text;
1317 if (p[0] == '$') {
1318 tokval->t_charptr++;
1319 return tokval->t_type = TOKEN_ID;
1322 for (r = p, s = ourcopy; *r; r++) {
1323 if (r >= p+MAX_KEYWORD)
1324 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1325 *s++ = nasm_tolower(*r);
1327 *s = '\0';
1328 /* right, so we have an identifier sitting in temp storage. now,
1329 * is it actually a register or instruction name, or what? */
1330 return nasm_token_hash(ourcopy, tokval);
1333 if (tline->type == TOK_NUMBER) {
1334 bool rn_error;
1335 tokval->t_integer = readnum(tline->text, &rn_error);
1336 tokval->t_charptr = tline->text;
1337 if (rn_error)
1338 return tokval->t_type = TOKEN_ERRNUM;
1339 else
1340 return tokval->t_type = TOKEN_NUM;
1343 if (tline->type == TOK_FLOAT) {
1344 return tokval->t_type = TOKEN_FLOAT;
1347 if (tline->type == TOK_STRING) {
1348 char bq, *ep;
1350 bq = tline->text[0];
1351 tokval->t_charptr = tline->text;
1352 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1354 if (ep[0] != bq || ep[1] != '\0')
1355 return tokval->t_type = TOKEN_ERRSTR;
1356 else
1357 return tokval->t_type = TOKEN_STR;
1360 if (tline->type == TOK_OTHER) {
1361 if (!strcmp(tline->text, "<<"))
1362 return tokval->t_type = TOKEN_SHL;
1363 if (!strcmp(tline->text, ">>"))
1364 return tokval->t_type = TOKEN_SHR;
1365 if (!strcmp(tline->text, "//"))
1366 return tokval->t_type = TOKEN_SDIV;
1367 if (!strcmp(tline->text, "%%"))
1368 return tokval->t_type = TOKEN_SMOD;
1369 if (!strcmp(tline->text, "=="))
1370 return tokval->t_type = TOKEN_EQ;
1371 if (!strcmp(tline->text, "<>"))
1372 return tokval->t_type = TOKEN_NE;
1373 if (!strcmp(tline->text, "!="))
1374 return tokval->t_type = TOKEN_NE;
1375 if (!strcmp(tline->text, "<="))
1376 return tokval->t_type = TOKEN_LE;
1377 if (!strcmp(tline->text, ">="))
1378 return tokval->t_type = TOKEN_GE;
1379 if (!strcmp(tline->text, "&&"))
1380 return tokval->t_type = TOKEN_DBL_AND;
1381 if (!strcmp(tline->text, "^^"))
1382 return tokval->t_type = TOKEN_DBL_XOR;
1383 if (!strcmp(tline->text, "||"))
1384 return tokval->t_type = TOKEN_DBL_OR;
1388 * We have no other options: just return the first character of
1389 * the token text.
1391 return tokval->t_type = tline->text[0];
1395 * Compare a string to the name of an existing macro; this is a
1396 * simple wrapper which calls either strcmp or nasm_stricmp
1397 * depending on the value of the `casesense' parameter.
1399 static int mstrcmp(const char *p, const char *q, bool casesense)
1401 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1405 * Compare a string to the name of an existing macro; this is a
1406 * simple wrapper which calls either strcmp or nasm_stricmp
1407 * depending on the value of the `casesense' parameter.
1409 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1411 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1415 * Return the Context structure associated with a %$ token. Return
1416 * NULL, having _already_ reported an error condition, if the
1417 * context stack isn't deep enough for the supplied number of $
1418 * signs.
1419 * If all_contexts == true, contexts that enclose current are
1420 * also scanned for such smacro, until it is found; if not -
1421 * only the context that directly results from the number of $'s
1422 * in variable's name.
1424 * If "namep" is non-NULL, set it to the pointer to the macro name
1425 * tail, i.e. the part beyond %$...
1427 static Context *get_ctx(const char *name, const char **namep,
1428 bool all_contexts)
1430 Context *ctx;
1431 SMacro *m;
1432 int i;
1434 if (namep)
1435 *namep = name;
1437 if (!name || name[0] != '%' || name[1] != '$')
1438 return NULL;
1440 if (!cstk) {
1441 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1442 return NULL;
1445 name += 2;
1446 ctx = cstk;
1447 i = 0;
1448 while (ctx && *name == '$') {
1449 name++;
1450 i++;
1451 ctx = ctx->next;
1453 if (!ctx) {
1454 error(ERR_NONFATAL, "`%s': context stack is only"
1455 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1456 return NULL;
1459 if (namep)
1460 *namep = name;
1462 if (!all_contexts) {
1463 return ctx;
1464 } else {
1465 error(ERR_WARNING, "context-local label expansion"
1466 " to outer contexts will be deprecated"
1467 " starting in NASM 2.10, please update your"
1468 " code accordingly");
1471 do {
1472 /* Search for this smacro in found context */
1473 m = hash_findix(&ctx->localmac, name);
1474 while (m) {
1475 if (!mstrcmp(m->name, name, m->casesense))
1476 return ctx;
1477 m = m->next;
1479 ctx = ctx->next;
1481 while (ctx);
1482 return NULL;
1486 * Check to see if a file is already in a string list
1488 static bool in_list(const StrList *list, const char *str)
1490 while (list) {
1491 if (!strcmp(list->str, str))
1492 return true;
1493 list = list->next;
1495 return false;
1499 * Open an include file. This routine must always return a valid
1500 * file pointer if it returns - it's responsible for throwing an
1501 * ERR_FATAL and bombing out completely if not. It should also try
1502 * the include path one by one until it finds the file or reaches
1503 * the end of the path.
1505 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1506 bool missing_ok)
1508 FILE *fp;
1509 char *prefix = "";
1510 IncPath *ip = ipath;
1511 int len = strlen(file);
1512 size_t prefix_len = 0;
1513 StrList *sl;
1515 while (1) {
1516 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1517 memcpy(sl->str, prefix, prefix_len);
1518 memcpy(sl->str+prefix_len, file, len+1);
1519 fp = fopen(sl->str, "r");
1520 if (fp && dhead && !in_list(*dhead, sl->str)) {
1521 sl->next = NULL;
1522 **dtail = sl;
1523 *dtail = &sl->next;
1524 } else {
1525 nasm_free(sl);
1527 if (fp)
1528 return fp;
1529 if (!ip) {
1530 if (!missing_ok)
1531 break;
1532 prefix = NULL;
1533 } else {
1534 prefix = ip->path;
1535 ip = ip->next;
1537 if (prefix) {
1538 prefix_len = strlen(prefix);
1539 } else {
1540 /* -MG given and file not found */
1541 if (dhead && !in_list(*dhead, file)) {
1542 sl = nasm_malloc(len+1+sizeof sl->next);
1543 sl->next = NULL;
1544 strcpy(sl->str, file);
1545 **dtail = sl;
1546 *dtail = &sl->next;
1548 return NULL;
1552 error(ERR_FATAL, "unable to open include file `%s'", file);
1553 return NULL;
1557 * Determine if we should warn on defining a single-line macro of
1558 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1559 * return true if _any_ single-line macro of that name is defined.
1560 * Otherwise, will return true if a single-line macro with either
1561 * `nparam' or no parameters is defined.
1563 * If a macro with precisely the right number of parameters is
1564 * defined, or nparam is -1, the address of the definition structure
1565 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1566 * is NULL, no action will be taken regarding its contents, and no
1567 * error will occur.
1569 * Note that this is also called with nparam zero to resolve
1570 * `ifdef'.
1572 * If you already know which context macro belongs to, you can pass
1573 * the context pointer as first parameter; if you won't but name begins
1574 * with %$ the context will be automatically computed. If all_contexts
1575 * is true, macro will be searched in outer contexts as well.
1577 static bool
1578 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1579 bool nocase)
1581 struct hash_table *smtbl;
1582 SMacro *m;
1584 if (ctx) {
1585 smtbl = &ctx->localmac;
1586 } else if (name[0] == '%' && name[1] == '$') {
1587 if (cstk)
1588 ctx = get_ctx(name, &name, false);
1589 if (!ctx)
1590 return false; /* got to return _something_ */
1591 smtbl = &ctx->localmac;
1592 } else {
1593 smtbl = &smacros;
1595 m = (SMacro *) hash_findix(smtbl, name);
1597 while (m) {
1598 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1599 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1600 if (defn) {
1601 if (nparam == (int) m->nparam || nparam == -1)
1602 *defn = m;
1603 else
1604 *defn = NULL;
1606 return true;
1608 m = m->next;
1611 return false;
1615 * Count and mark off the parameters in a multi-line macro call.
1616 * This is called both from within the multi-line macro expansion
1617 * code, and also to mark off the default parameters when provided
1618 * in a %macro definition line.
1620 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1622 int paramsize, brace;
1624 *nparam = paramsize = 0;
1625 *params = NULL;
1626 while (t) {
1627 /* +1: we need space for the final NULL */
1628 if (*nparam+1 >= paramsize) {
1629 paramsize += PARAM_DELTA;
1630 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1632 skip_white_(t);
1633 brace = false;
1634 if (tok_is_(t, "{"))
1635 brace = true;
1636 (*params)[(*nparam)++] = t;
1637 while (tok_isnt_(t, brace ? "}" : ","))
1638 t = t->next;
1639 if (t) { /* got a comma/brace */
1640 t = t->next;
1641 if (brace) {
1643 * Now we've found the closing brace, look further
1644 * for the comma.
1646 skip_white_(t);
1647 if (tok_isnt_(t, ",")) {
1648 error(ERR_NONFATAL,
1649 "braces do not enclose all of macro parameter");
1650 while (tok_isnt_(t, ","))
1651 t = t->next;
1653 if (t)
1654 t = t->next; /* eat the comma */
1661 * Determine whether one of the various `if' conditions is true or
1662 * not.
1664 * We must free the tline we get passed.
1666 static bool if_condition(Token * tline, enum preproc_token ct)
1668 enum pp_conditional i = PP_COND(ct);
1669 bool j;
1670 Token *t, *tt, **tptr, *origline;
1671 struct tokenval tokval;
1672 expr *evalresult;
1673 enum pp_token_type needtype;
1674 char *p;
1676 origline = tline;
1678 switch (i) {
1679 case PPC_IFCTX:
1680 j = false; /* have we matched yet? */
1681 while (true) {
1682 skip_white_(tline);
1683 if (!tline)
1684 break;
1685 if (tline->type != TOK_ID) {
1686 error(ERR_NONFATAL,
1687 "`%s' expects context identifiers", pp_directives[ct]);
1688 free_tlist(origline);
1689 return -1;
1691 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1692 j = true;
1693 tline = tline->next;
1695 break;
1697 case PPC_IFDEF:
1698 j = false; /* have we matched yet? */
1699 while (tline) {
1700 skip_white_(tline);
1701 if (!tline || (tline->type != TOK_ID &&
1702 (tline->type != TOK_PREPROC_ID ||
1703 tline->text[1] != '$'))) {
1704 error(ERR_NONFATAL,
1705 "`%s' expects macro identifiers", pp_directives[ct]);
1706 goto fail;
1708 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1709 j = true;
1710 tline = tline->next;
1712 break;
1714 case PPC_IFENV:
1715 tline = expand_smacro(tline);
1716 j = false; /* have we matched yet? */
1717 while (tline) {
1718 skip_white_(tline);
1719 if (!tline || (tline->type != TOK_ID &&
1720 tline->type != TOK_STRING &&
1721 (tline->type != TOK_PREPROC_ID ||
1722 tline->text[1] != '!'))) {
1723 error(ERR_NONFATAL,
1724 "`%s' expects environment variable names",
1725 pp_directives[ct]);
1726 goto fail;
1728 p = tline->text;
1729 if (tline->type == TOK_PREPROC_ID)
1730 p += 2; /* Skip leading %! */
1731 if (*p == '\'' || *p == '\"' || *p == '`')
1732 nasm_unquote_cstr(p, ct);
1733 if (getenv(p))
1734 j = true;
1735 tline = tline->next;
1737 break;
1739 case PPC_IFIDN:
1740 case PPC_IFIDNI:
1741 tline = expand_smacro(tline);
1742 t = tt = tline;
1743 while (tok_isnt_(tt, ","))
1744 tt = tt->next;
1745 if (!tt) {
1746 error(ERR_NONFATAL,
1747 "`%s' expects two comma-separated arguments",
1748 pp_directives[ct]);
1749 goto fail;
1751 tt = tt->next;
1752 j = true; /* assume equality unless proved not */
1753 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1754 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1755 error(ERR_NONFATAL, "`%s': more than one comma on line",
1756 pp_directives[ct]);
1757 goto fail;
1759 if (t->type == TOK_WHITESPACE) {
1760 t = t->next;
1761 continue;
1763 if (tt->type == TOK_WHITESPACE) {
1764 tt = tt->next;
1765 continue;
1767 if (tt->type != t->type) {
1768 j = false; /* found mismatching tokens */
1769 break;
1771 /* When comparing strings, need to unquote them first */
1772 if (t->type == TOK_STRING) {
1773 size_t l1 = nasm_unquote(t->text, NULL);
1774 size_t l2 = nasm_unquote(tt->text, NULL);
1776 if (l1 != l2) {
1777 j = false;
1778 break;
1780 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1781 j = false;
1782 break;
1784 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1785 j = false; /* found mismatching tokens */
1786 break;
1789 t = t->next;
1790 tt = tt->next;
1792 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1793 j = false; /* trailing gunk on one end or other */
1794 break;
1796 case PPC_IFMACRO:
1798 bool found = false;
1799 MMacro searching, *mmac;
1801 skip_white_(tline);
1802 tline = expand_id(tline);
1803 if (!tok_type_(tline, TOK_ID)) {
1804 error(ERR_NONFATAL,
1805 "`%s' expects a macro name", pp_directives[ct]);
1806 goto fail;
1808 searching.name = nasm_strdup(tline->text);
1809 searching.casesense = true;
1810 searching.plus = false;
1811 searching.nolist = false;
1812 searching.in_progress = 0;
1813 searching.max_depth = 0;
1814 searching.rep_nest = NULL;
1815 searching.nparam_min = 0;
1816 searching.nparam_max = INT_MAX;
1817 tline = expand_smacro(tline->next);
1818 skip_white_(tline);
1819 if (!tline) {
1820 } else if (!tok_type_(tline, TOK_NUMBER)) {
1821 error(ERR_NONFATAL,
1822 "`%s' expects a parameter count or nothing",
1823 pp_directives[ct]);
1824 } else {
1825 searching.nparam_min = searching.nparam_max =
1826 readnum(tline->text, &j);
1827 if (j)
1828 error(ERR_NONFATAL,
1829 "unable to parse parameter count `%s'",
1830 tline->text);
1832 if (tline && tok_is_(tline->next, "-")) {
1833 tline = tline->next->next;
1834 if (tok_is_(tline, "*"))
1835 searching.nparam_max = INT_MAX;
1836 else if (!tok_type_(tline, TOK_NUMBER))
1837 error(ERR_NONFATAL,
1838 "`%s' expects a parameter count after `-'",
1839 pp_directives[ct]);
1840 else {
1841 searching.nparam_max = readnum(tline->text, &j);
1842 if (j)
1843 error(ERR_NONFATAL,
1844 "unable to parse parameter count `%s'",
1845 tline->text);
1846 if (searching.nparam_min > searching.nparam_max)
1847 error(ERR_NONFATAL,
1848 "minimum parameter count exceeds maximum");
1851 if (tline && tok_is_(tline->next, "+")) {
1852 tline = tline->next;
1853 searching.plus = true;
1855 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1856 while (mmac) {
1857 if (!strcmp(mmac->name, searching.name) &&
1858 (mmac->nparam_min <= searching.nparam_max
1859 || searching.plus)
1860 && (searching.nparam_min <= mmac->nparam_max
1861 || mmac->plus)) {
1862 found = true;
1863 break;
1865 mmac = mmac->next;
1867 if (tline && tline->next)
1868 error(ERR_WARNING|ERR_PASS1,
1869 "trailing garbage after %%ifmacro ignored");
1870 nasm_free(searching.name);
1871 j = found;
1872 break;
1875 case PPC_IFID:
1876 needtype = TOK_ID;
1877 goto iftype;
1878 case PPC_IFNUM:
1879 needtype = TOK_NUMBER;
1880 goto iftype;
1881 case PPC_IFSTR:
1882 needtype = TOK_STRING;
1883 goto iftype;
1885 iftype:
1886 t = tline = expand_smacro(tline);
1888 while (tok_type_(t, TOK_WHITESPACE) ||
1889 (needtype == TOK_NUMBER &&
1890 tok_type_(t, TOK_OTHER) &&
1891 (t->text[0] == '-' || t->text[0] == '+') &&
1892 !t->text[1]))
1893 t = t->next;
1895 j = tok_type_(t, needtype);
1896 break;
1898 case PPC_IFTOKEN:
1899 t = tline = expand_smacro(tline);
1900 while (tok_type_(t, TOK_WHITESPACE))
1901 t = t->next;
1903 j = false;
1904 if (t) {
1905 t = t->next; /* Skip the actual token */
1906 while (tok_type_(t, TOK_WHITESPACE))
1907 t = t->next;
1908 j = !t; /* Should be nothing left */
1910 break;
1912 case PPC_IFEMPTY:
1913 t = tline = expand_smacro(tline);
1914 while (tok_type_(t, TOK_WHITESPACE))
1915 t = t->next;
1917 j = !t; /* Should be empty */
1918 break;
1920 case PPC_IF:
1921 t = tline = expand_smacro(tline);
1922 tptr = &t;
1923 tokval.t_type = TOKEN_INVALID;
1924 evalresult = evaluate(ppscan, tptr, &tokval,
1925 NULL, pass | CRITICAL, error, NULL);
1926 if (!evalresult)
1927 return -1;
1928 if (tokval.t_type)
1929 error(ERR_WARNING|ERR_PASS1,
1930 "trailing garbage after expression ignored");
1931 if (!is_simple(evalresult)) {
1932 error(ERR_NONFATAL,
1933 "non-constant value given to `%s'", pp_directives[ct]);
1934 goto fail;
1936 j = reloc_value(evalresult) != 0;
1937 break;
1939 default:
1940 error(ERR_FATAL,
1941 "preprocessor directive `%s' not yet implemented",
1942 pp_directives[ct]);
1943 goto fail;
1946 free_tlist(origline);
1947 return j ^ PP_NEGATIVE(ct);
1949 fail:
1950 free_tlist(origline);
1951 return -1;
1955 * Common code for defining an smacro
1957 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1958 int nparam, Token *expansion)
1960 SMacro *smac, **smhead;
1961 struct hash_table *smtbl;
1963 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1964 if (!smac) {
1965 error(ERR_WARNING|ERR_PASS1,
1966 "single-line macro `%s' defined both with and"
1967 " without parameters", mname);
1969 * Some instances of the old code considered this a failure,
1970 * some others didn't. What is the right thing to do here?
1972 free_tlist(expansion);
1973 return false; /* Failure */
1974 } else {
1976 * We're redefining, so we have to take over an
1977 * existing SMacro structure. This means freeing
1978 * what was already in it.
1980 nasm_free(smac->name);
1981 free_tlist(smac->expansion);
1983 } else {
1984 smtbl = ctx ? &ctx->localmac : &smacros;
1985 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1986 smac = nasm_malloc(sizeof(SMacro));
1987 smac->next = *smhead;
1988 *smhead = smac;
1990 smac->name = nasm_strdup(mname);
1991 smac->casesense = casesense;
1992 smac->nparam = nparam;
1993 smac->expansion = expansion;
1994 smac->in_progress = false;
1995 return true; /* Success */
1999 * Undefine an smacro
2001 static void undef_smacro(Context *ctx, const char *mname)
2003 SMacro **smhead, *s, **sp;
2004 struct hash_table *smtbl;
2006 smtbl = ctx ? &ctx->localmac : &smacros;
2007 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2009 if (smhead) {
2011 * We now have a macro name... go hunt for it.
2013 sp = smhead;
2014 while ((s = *sp) != NULL) {
2015 if (!mstrcmp(s->name, mname, s->casesense)) {
2016 *sp = s->next;
2017 nasm_free(s->name);
2018 free_tlist(s->expansion);
2019 nasm_free(s);
2020 } else {
2021 sp = &s->next;
2028 * Parse a mmacro specification.
2030 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2032 bool err;
2034 tline = tline->next;
2035 skip_white_(tline);
2036 tline = expand_id(tline);
2037 if (!tok_type_(tline, TOK_ID)) {
2038 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2039 return false;
2042 def->prev = NULL;
2043 def->name = nasm_strdup(tline->text);
2044 def->plus = false;
2045 def->nolist = false;
2046 def->in_progress = 0;
2047 def->rep_nest = NULL;
2048 def->nparam_min = 0;
2049 def->nparam_max = 0;
2051 tline = expand_smacro(tline->next);
2052 skip_white_(tline);
2053 if (!tok_type_(tline, TOK_NUMBER)) {
2054 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2055 } else {
2056 def->nparam_min = def->nparam_max =
2057 readnum(tline->text, &err);
2058 if (err)
2059 error(ERR_NONFATAL,
2060 "unable to parse parameter count `%s'", tline->text);
2062 if (tline && tok_is_(tline->next, "-")) {
2063 tline = tline->next->next;
2064 if (tok_is_(tline, "*")) {
2065 def->nparam_max = INT_MAX;
2066 } else if (!tok_type_(tline, TOK_NUMBER)) {
2067 error(ERR_NONFATAL,
2068 "`%s' expects a parameter count after `-'", directive);
2069 } else {
2070 def->nparam_max = readnum(tline->text, &err);
2071 if (err) {
2072 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2073 tline->text);
2075 if (def->nparam_min > def->nparam_max) {
2076 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2080 if (tline && tok_is_(tline->next, "+")) {
2081 tline = tline->next;
2082 def->plus = true;
2084 if (tline && tok_type_(tline->next, TOK_ID) &&
2085 !nasm_stricmp(tline->next->text, ".nolist")) {
2086 tline = tline->next;
2087 def->nolist = true;
2091 * Handle default parameters.
2093 if (tline && tline->next) {
2094 def->dlist = tline->next;
2095 tline->next = NULL;
2096 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2097 } else {
2098 def->dlist = NULL;
2099 def->defaults = NULL;
2101 def->expansion = NULL;
2103 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2104 !def->plus)
2105 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2106 "too many default macro parameters");
2108 return true;
2113 * Decode a size directive
2115 static int parse_size(const char *str) {
2116 static const char *size_names[] =
2117 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2118 static const int sizes[] =
2119 { 0, 1, 4, 16, 8, 10, 2, 32 };
2121 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2125 * find and process preprocessor directive in passed line
2126 * Find out if a line contains a preprocessor directive, and deal
2127 * with it if so.
2129 * If a directive _is_ found, it is the responsibility of this routine
2130 * (and not the caller) to free_tlist() the line.
2132 * @param tline a pointer to the current tokeninzed line linked list
2133 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2136 static int do_directive(Token * tline)
2138 enum preproc_token i;
2139 int j;
2140 bool err;
2141 int nparam;
2142 bool nolist;
2143 bool casesense;
2144 int k, m;
2145 int offset;
2146 char *p, *pp;
2147 const char *mname;
2148 Include *inc;
2149 Context *ctx;
2150 Cond *cond;
2151 MMacro *mmac, **mmhead;
2152 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2153 Line *l;
2154 struct tokenval tokval;
2155 expr *evalresult;
2156 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2157 int64_t count;
2158 size_t len;
2159 int severity;
2161 origline = tline;
2163 skip_white_(tline);
2164 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2165 (tline->text[1] == '%' || tline->text[1] == '$'
2166 || tline->text[1] == '!'))
2167 return NO_DIRECTIVE_FOUND;
2169 i = pp_token_hash(tline->text);
2172 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2173 * since they are known to be buggy at moment, we need to fix them
2174 * in future release (2.09-2.10)
2176 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2177 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2178 tline->text);
2179 return NO_DIRECTIVE_FOUND;
2183 * If we're in a non-emitting branch of a condition construct,
2184 * or walking to the end of an already terminated %rep block,
2185 * we should ignore all directives except for condition
2186 * directives.
2188 if (((istk->conds && !emitting(istk->conds->state)) ||
2189 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2190 return NO_DIRECTIVE_FOUND;
2194 * If we're defining a macro or reading a %rep block, we should
2195 * ignore all directives except for %macro/%imacro (which nest),
2196 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2197 * If we're in a %rep block, another %rep nests, so should be let through.
2199 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2200 i != PP_RMACRO && i != PP_IRMACRO &&
2201 i != PP_ENDMACRO && i != PP_ENDM &&
2202 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2203 return NO_DIRECTIVE_FOUND;
2206 if (defining) {
2207 if (i == PP_MACRO || i == PP_IMACRO ||
2208 i == PP_RMACRO || i == PP_IRMACRO) {
2209 nested_mac_count++;
2210 return NO_DIRECTIVE_FOUND;
2211 } else if (nested_mac_count > 0) {
2212 if (i == PP_ENDMACRO) {
2213 nested_mac_count--;
2214 return NO_DIRECTIVE_FOUND;
2217 if (!defining->name) {
2218 if (i == PP_REP) {
2219 nested_rep_count++;
2220 return NO_DIRECTIVE_FOUND;
2221 } else if (nested_rep_count > 0) {
2222 if (i == PP_ENDREP) {
2223 nested_rep_count--;
2224 return NO_DIRECTIVE_FOUND;
2230 switch (i) {
2231 case PP_INVALID:
2232 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2233 tline->text);
2234 return NO_DIRECTIVE_FOUND; /* didn't get it */
2236 case PP_STACKSIZE:
2237 /* Directive to tell NASM what the default stack size is. The
2238 * default is for a 16-bit stack, and this can be overriden with
2239 * %stacksize large.
2241 tline = tline->next;
2242 if (tline && tline->type == TOK_WHITESPACE)
2243 tline = tline->next;
2244 if (!tline || tline->type != TOK_ID) {
2245 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2246 free_tlist(origline);
2247 return DIRECTIVE_FOUND;
2249 if (nasm_stricmp(tline->text, "flat") == 0) {
2250 /* All subsequent ARG directives are for a 32-bit stack */
2251 StackSize = 4;
2252 StackPointer = "ebp";
2253 ArgOffset = 8;
2254 LocalOffset = 0;
2255 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2256 /* All subsequent ARG directives are for a 64-bit stack */
2257 StackSize = 8;
2258 StackPointer = "rbp";
2259 ArgOffset = 16;
2260 LocalOffset = 0;
2261 } else if (nasm_stricmp(tline->text, "large") == 0) {
2262 /* All subsequent ARG directives are for a 16-bit stack,
2263 * far function call.
2265 StackSize = 2;
2266 StackPointer = "bp";
2267 ArgOffset = 4;
2268 LocalOffset = 0;
2269 } else if (nasm_stricmp(tline->text, "small") == 0) {
2270 /* All subsequent ARG directives are for a 16-bit stack,
2271 * far function call. We don't support near functions.
2273 StackSize = 2;
2274 StackPointer = "bp";
2275 ArgOffset = 6;
2276 LocalOffset = 0;
2277 } else {
2278 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND;
2282 free_tlist(origline);
2283 return DIRECTIVE_FOUND;
2285 case PP_ARG:
2286 /* TASM like ARG directive to define arguments to functions, in
2287 * the following form:
2289 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2291 offset = ArgOffset;
2292 do {
2293 char *arg, directive[256];
2294 int size = StackSize;
2296 /* Find the argument name */
2297 tline = tline->next;
2298 if (tline && tline->type == TOK_WHITESPACE)
2299 tline = tline->next;
2300 if (!tline || tline->type != TOK_ID) {
2301 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
2305 arg = tline->text;
2307 /* Find the argument size type */
2308 tline = tline->next;
2309 if (!tline || tline->type != TOK_OTHER
2310 || tline->text[0] != ':') {
2311 error(ERR_NONFATAL,
2312 "Syntax error processing `%%arg' directive");
2313 free_tlist(origline);
2314 return DIRECTIVE_FOUND;
2316 tline = tline->next;
2317 if (!tline || tline->type != TOK_ID) {
2318 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2319 free_tlist(origline);
2320 return DIRECTIVE_FOUND;
2323 /* Allow macro expansion of type parameter */
2324 tt = tokenize(tline->text);
2325 tt = expand_smacro(tt);
2326 size = parse_size(tt->text);
2327 if (!size) {
2328 error(ERR_NONFATAL,
2329 "Invalid size type for `%%arg' missing directive");
2330 free_tlist(tt);
2331 free_tlist(origline);
2332 return DIRECTIVE_FOUND;
2334 free_tlist(tt);
2336 /* Round up to even stack slots */
2337 size = ALIGN(size, StackSize);
2339 /* Now define the macro for the argument */
2340 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2341 arg, StackPointer, offset);
2342 do_directive(tokenize(directive));
2343 offset += size;
2345 /* Move to the next argument in the list */
2346 tline = tline->next;
2347 if (tline && tline->type == TOK_WHITESPACE)
2348 tline = tline->next;
2349 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2350 ArgOffset = offset;
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2354 case PP_LOCAL:
2355 /* TASM like LOCAL directive to define local variables for a
2356 * function, in the following form:
2358 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2360 * The '= LocalSize' at the end is ignored by NASM, but is
2361 * required by TASM to define the local parameter size (and used
2362 * by the TASM macro package).
2364 offset = LocalOffset;
2365 do {
2366 char *local, directive[256];
2367 int size = StackSize;
2369 /* Find the argument name */
2370 tline = tline->next;
2371 if (tline && tline->type == TOK_WHITESPACE)
2372 tline = tline->next;
2373 if (!tline || tline->type != TOK_ID) {
2374 error(ERR_NONFATAL,
2375 "`%%local' missing argument parameter");
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2379 local = tline->text;
2381 /* Find the argument size type */
2382 tline = tline->next;
2383 if (!tline || tline->type != TOK_OTHER
2384 || tline->text[0] != ':') {
2385 error(ERR_NONFATAL,
2386 "Syntax error processing `%%local' directive");
2387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
2390 tline = tline->next;
2391 if (!tline || tline->type != TOK_ID) {
2392 error(ERR_NONFATAL,
2393 "`%%local' missing size type parameter");
2394 free_tlist(origline);
2395 return DIRECTIVE_FOUND;
2398 /* Allow macro expansion of type parameter */
2399 tt = tokenize(tline->text);
2400 tt = expand_smacro(tt);
2401 size = parse_size(tt->text);
2402 if (!size) {
2403 error(ERR_NONFATAL,
2404 "Invalid size type for `%%local' missing directive");
2405 free_tlist(tt);
2406 free_tlist(origline);
2407 return DIRECTIVE_FOUND;
2409 free_tlist(tt);
2411 /* Round up to even stack slots */
2412 size = ALIGN(size, StackSize);
2414 offset += size; /* Negative offset, increment before */
2416 /* Now define the macro for the argument */
2417 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2418 local, StackPointer, offset);
2419 do_directive(tokenize(directive));
2421 /* Now define the assign to setup the enter_c macro correctly */
2422 snprintf(directive, sizeof(directive),
2423 "%%assign %%$localsize %%$localsize+%d", size);
2424 do_directive(tokenize(directive));
2426 /* Move to the next argument in the list */
2427 tline = tline->next;
2428 if (tline && tline->type == TOK_WHITESPACE)
2429 tline = tline->next;
2430 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2431 LocalOffset = offset;
2432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
2435 case PP_CLEAR:
2436 if (tline->next)
2437 error(ERR_WARNING|ERR_PASS1,
2438 "trailing garbage after `%%clear' ignored");
2439 free_macros();
2440 init_macros();
2441 free_tlist(origline);
2442 return DIRECTIVE_FOUND;
2444 case PP_DEPEND:
2445 t = tline->next = expand_smacro(tline->next);
2446 skip_white_(t);
2447 if (!t || (t->type != TOK_STRING &&
2448 t->type != TOK_INTERNAL_STRING)) {
2449 error(ERR_NONFATAL, "`%%depend' expects a file name");
2450 free_tlist(origline);
2451 return DIRECTIVE_FOUND; /* but we did _something_ */
2453 if (t->next)
2454 error(ERR_WARNING|ERR_PASS1,
2455 "trailing garbage after `%%depend' ignored");
2456 p = t->text;
2457 if (t->type != TOK_INTERNAL_STRING)
2458 nasm_unquote_cstr(p, i);
2459 if (dephead && !in_list(*dephead, p)) {
2460 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2461 sl->next = NULL;
2462 strcpy(sl->str, p);
2463 *deptail = sl;
2464 deptail = &sl->next;
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2469 case PP_INCLUDE:
2470 t = tline->next = expand_smacro(tline->next);
2471 skip_white_(t);
2473 if (!t || (t->type != TOK_STRING &&
2474 t->type != TOK_INTERNAL_STRING)) {
2475 error(ERR_NONFATAL, "`%%include' expects a file name");
2476 free_tlist(origline);
2477 return DIRECTIVE_FOUND; /* but we did _something_ */
2479 if (t->next)
2480 error(ERR_WARNING|ERR_PASS1,
2481 "trailing garbage after `%%include' ignored");
2482 p = t->text;
2483 if (t->type != TOK_INTERNAL_STRING)
2484 nasm_unquote_cstr(p, i);
2485 inc = nasm_malloc(sizeof(Include));
2486 inc->next = istk;
2487 inc->conds = NULL;
2488 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2489 if (!inc->fp) {
2490 /* -MG given but file not found */
2491 nasm_free(inc);
2492 } else {
2493 inc->fname = src_set_fname(nasm_strdup(p));
2494 inc->lineno = src_set_linnum(0);
2495 inc->lineinc = 1;
2496 inc->expansion = NULL;
2497 inc->mstk = NULL;
2498 istk = inc;
2499 list->uplevel(LIST_INCLUDE);
2501 free_tlist(origline);
2502 return DIRECTIVE_FOUND;
2504 case PP_USE:
2506 static macros_t *use_pkg;
2507 const char *pkg_macro = NULL;
2509 tline = tline->next;
2510 skip_white_(tline);
2511 tline = expand_id(tline);
2513 if (!tline || (tline->type != TOK_STRING &&
2514 tline->type != TOK_INTERNAL_STRING &&
2515 tline->type != TOK_ID)) {
2516 error(ERR_NONFATAL, "`%%use' expects a package name");
2517 free_tlist(origline);
2518 return DIRECTIVE_FOUND; /* but we did _something_ */
2520 if (tline->next)
2521 error(ERR_WARNING|ERR_PASS1,
2522 "trailing garbage after `%%use' ignored");
2523 if (tline->type == TOK_STRING)
2524 nasm_unquote_cstr(tline->text, i);
2525 use_pkg = nasm_stdmac_find_package(tline->text);
2526 if (!use_pkg)
2527 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2528 else
2529 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2530 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2531 /* Not already included, go ahead and include it */
2532 stdmacpos = use_pkg;
2534 free_tlist(origline);
2535 return DIRECTIVE_FOUND;
2537 case PP_PUSH:
2538 case PP_REPL:
2539 case PP_POP:
2540 tline = tline->next;
2541 skip_white_(tline);
2542 tline = expand_id(tline);
2543 if (tline) {
2544 if (!tok_type_(tline, TOK_ID)) {
2545 error(ERR_NONFATAL, "`%s' expects a context identifier",
2546 pp_directives[i]);
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND; /* but we did _something_ */
2550 if (tline->next)
2551 error(ERR_WARNING|ERR_PASS1,
2552 "trailing garbage after `%s' ignored",
2553 pp_directives[i]);
2554 p = nasm_strdup(tline->text);
2555 } else {
2556 p = NULL; /* Anonymous */
2559 if (i == PP_PUSH) {
2560 ctx = nasm_malloc(sizeof(Context));
2561 ctx->next = cstk;
2562 hash_init(&ctx->localmac, HASH_SMALL);
2563 ctx->name = p;
2564 ctx->number = unique++;
2565 cstk = ctx;
2566 } else {
2567 /* %pop or %repl */
2568 if (!cstk) {
2569 error(ERR_NONFATAL, "`%s': context stack is empty",
2570 pp_directives[i]);
2571 } else if (i == PP_POP) {
2572 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2573 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2574 "expected %s",
2575 cstk->name ? cstk->name : "anonymous", p);
2576 else
2577 ctx_pop();
2578 } else {
2579 /* i == PP_REPL */
2580 nasm_free(cstk->name);
2581 cstk->name = p;
2582 p = NULL;
2584 nasm_free(p);
2586 free_tlist(origline);
2587 return DIRECTIVE_FOUND;
2588 case PP_FATAL:
2589 severity = ERR_FATAL;
2590 goto issue_error;
2591 case PP_ERROR:
2592 severity = ERR_NONFATAL;
2593 goto issue_error;
2594 case PP_WARNING:
2595 severity = ERR_WARNING|ERR_WARN_USER;
2596 goto issue_error;
2598 issue_error:
2600 /* Only error out if this is the final pass */
2601 if (pass != 2 && i != PP_FATAL)
2602 return DIRECTIVE_FOUND;
2604 tline->next = expand_smacro(tline->next);
2605 tline = tline->next;
2606 skip_white_(tline);
2607 t = tline ? tline->next : NULL;
2608 skip_white_(t);
2609 if (tok_type_(tline, TOK_STRING) && !t) {
2610 /* The line contains only a quoted string */
2611 p = tline->text;
2612 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2613 error(severity, "%s", p);
2614 } else {
2615 /* Not a quoted string, or more than a quoted string */
2616 p = detoken(tline, false);
2617 error(severity, "%s", p);
2618 nasm_free(p);
2620 free_tlist(origline);
2621 return DIRECTIVE_FOUND;
2624 CASE_PP_IF:
2625 if (istk->conds && !emitting(istk->conds->state))
2626 j = COND_NEVER;
2627 else {
2628 j = if_condition(tline->next, i);
2629 tline->next = NULL; /* it got freed */
2630 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2632 cond = nasm_malloc(sizeof(Cond));
2633 cond->next = istk->conds;
2634 cond->state = j;
2635 istk->conds = cond;
2636 if(istk->mstk)
2637 istk->mstk->condcnt ++;
2638 free_tlist(origline);
2639 return DIRECTIVE_FOUND;
2641 CASE_PP_ELIF:
2642 if (!istk->conds)
2643 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2644 switch(istk->conds->state) {
2645 case COND_IF_TRUE:
2646 istk->conds->state = COND_DONE;
2647 break;
2649 case COND_DONE:
2650 case COND_NEVER:
2651 break;
2653 case COND_ELSE_TRUE:
2654 case COND_ELSE_FALSE:
2655 error_precond(ERR_WARNING|ERR_PASS1,
2656 "`%%elif' after `%%else' ignored");
2657 istk->conds->state = COND_NEVER;
2658 break;
2660 case COND_IF_FALSE:
2662 * IMPORTANT: In the case of %if, we will already have
2663 * called expand_mmac_params(); however, if we're
2664 * processing an %elif we must have been in a
2665 * non-emitting mode, which would have inhibited
2666 * the normal invocation of expand_mmac_params().
2667 * Therefore, we have to do it explicitly here.
2669 j = if_condition(expand_mmac_params(tline->next), i);
2670 tline->next = NULL; /* it got freed */
2671 istk->conds->state =
2672 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2673 break;
2675 free_tlist(origline);
2676 return DIRECTIVE_FOUND;
2678 case PP_ELSE:
2679 if (tline->next)
2680 error_precond(ERR_WARNING|ERR_PASS1,
2681 "trailing garbage after `%%else' ignored");
2682 if (!istk->conds)
2683 error(ERR_FATAL, "`%%else': no matching `%%if'");
2684 switch(istk->conds->state) {
2685 case COND_IF_TRUE:
2686 case COND_DONE:
2687 istk->conds->state = COND_ELSE_FALSE;
2688 break;
2690 case COND_NEVER:
2691 break;
2693 case COND_IF_FALSE:
2694 istk->conds->state = COND_ELSE_TRUE;
2695 break;
2697 case COND_ELSE_TRUE:
2698 case COND_ELSE_FALSE:
2699 error_precond(ERR_WARNING|ERR_PASS1,
2700 "`%%else' after `%%else' ignored.");
2701 istk->conds->state = COND_NEVER;
2702 break;
2704 free_tlist(origline);
2705 return DIRECTIVE_FOUND;
2707 case PP_ENDIF:
2708 if (tline->next)
2709 error_precond(ERR_WARNING|ERR_PASS1,
2710 "trailing garbage after `%%endif' ignored");
2711 if (!istk->conds)
2712 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2713 cond = istk->conds;
2714 istk->conds = cond->next;
2715 nasm_free(cond);
2716 if(istk->mstk)
2717 istk->mstk->condcnt --;
2718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
2721 case PP_RMACRO:
2722 case PP_IRMACRO:
2723 case PP_MACRO:
2724 case PP_IMACRO:
2725 if (defining) {
2726 error(ERR_FATAL, "`%s': already defining a macro",
2727 pp_directives[i]);
2728 return DIRECTIVE_FOUND;
2730 defining = nasm_malloc(sizeof(MMacro));
2731 defining->max_depth =
2732 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2733 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2734 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2735 nasm_free(defining);
2736 defining = NULL;
2737 return DIRECTIVE_FOUND;
2740 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2741 while (mmac) {
2742 if (!strcmp(mmac->name, defining->name) &&
2743 (mmac->nparam_min <= defining->nparam_max
2744 || defining->plus)
2745 && (defining->nparam_min <= mmac->nparam_max
2746 || mmac->plus)) {
2747 error(ERR_WARNING|ERR_PASS1,
2748 "redefining multi-line macro `%s'", defining->name);
2749 return DIRECTIVE_FOUND;
2751 mmac = mmac->next;
2753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
2756 case PP_ENDM:
2757 case PP_ENDMACRO:
2758 if (! (defining && defining->name)) {
2759 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2760 return DIRECTIVE_FOUND;
2762 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2763 defining->next = *mmhead;
2764 *mmhead = defining;
2765 defining = NULL;
2766 free_tlist(origline);
2767 return DIRECTIVE_FOUND;
2769 case PP_EXITMACRO:
2771 * We must search along istk->expansion until we hit a
2772 * macro-end marker for a macro with a name. Then we
2773 * bypass all lines between exitmacro and endmacro.
2775 list_for_each(l, istk->expansion)
2776 if (l->finishes && l->finishes->name)
2777 break;
2779 if (l) {
2781 * Remove all conditional entries relative to this
2782 * macro invocation. (safe to do in this context)
2784 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2785 cond = istk->conds;
2786 istk->conds = cond->next;
2787 nasm_free(cond);
2789 istk->expansion = l;
2790 } else {
2791 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
2796 case PP_UNMACRO:
2797 case PP_UNIMACRO:
2799 MMacro **mmac_p;
2800 MMacro spec;
2802 spec.casesense = (i == PP_UNMACRO);
2803 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2804 return DIRECTIVE_FOUND;
2806 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2807 while (mmac_p && *mmac_p) {
2808 mmac = *mmac_p;
2809 if (mmac->casesense == spec.casesense &&
2810 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2811 mmac->nparam_min == spec.nparam_min &&
2812 mmac->nparam_max == spec.nparam_max &&
2813 mmac->plus == spec.plus) {
2814 *mmac_p = mmac->next;
2815 free_mmacro(mmac);
2816 } else {
2817 mmac_p = &mmac->next;
2820 free_tlist(origline);
2821 free_tlist(spec.dlist);
2822 return DIRECTIVE_FOUND;
2825 case PP_ROTATE:
2826 if (tline->next && tline->next->type == TOK_WHITESPACE)
2827 tline = tline->next;
2828 if (!tline->next) {
2829 free_tlist(origline);
2830 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2831 return DIRECTIVE_FOUND;
2833 t = expand_smacro(tline->next);
2834 tline->next = NULL;
2835 free_tlist(origline);
2836 tline = t;
2837 tptr = &t;
2838 tokval.t_type = TOKEN_INVALID;
2839 evalresult =
2840 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2841 free_tlist(tline);
2842 if (!evalresult)
2843 return DIRECTIVE_FOUND;
2844 if (tokval.t_type)
2845 error(ERR_WARNING|ERR_PASS1,
2846 "trailing garbage after expression ignored");
2847 if (!is_simple(evalresult)) {
2848 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2849 return DIRECTIVE_FOUND;
2851 mmac = istk->mstk;
2852 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2853 mmac = mmac->next_active;
2854 if (!mmac) {
2855 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2856 } else if (mmac->nparam == 0) {
2857 error(ERR_NONFATAL,
2858 "`%%rotate' invoked within macro without parameters");
2859 } else {
2860 int rotate = mmac->rotate + reloc_value(evalresult);
2862 rotate %= (int)mmac->nparam;
2863 if (rotate < 0)
2864 rotate += mmac->nparam;
2866 mmac->rotate = rotate;
2868 return DIRECTIVE_FOUND;
2870 case PP_REP:
2871 nolist = false;
2872 do {
2873 tline = tline->next;
2874 } while (tok_type_(tline, TOK_WHITESPACE));
2876 if (tok_type_(tline, TOK_ID) &&
2877 nasm_stricmp(tline->text, ".nolist") == 0) {
2878 nolist = true;
2879 do {
2880 tline = tline->next;
2881 } while (tok_type_(tline, TOK_WHITESPACE));
2884 if (tline) {
2885 t = expand_smacro(tline);
2886 tptr = &t;
2887 tokval.t_type = TOKEN_INVALID;
2888 evalresult =
2889 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2890 if (!evalresult) {
2891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
2894 if (tokval.t_type)
2895 error(ERR_WARNING|ERR_PASS1,
2896 "trailing garbage after expression ignored");
2897 if (!is_simple(evalresult)) {
2898 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2899 return DIRECTIVE_FOUND;
2901 count = reloc_value(evalresult);
2902 if (count >= REP_LIMIT) {
2903 error(ERR_NONFATAL, "`%%rep' evalue exceeds limit");
2904 count = 0;
2905 } else
2906 count++;
2907 } else {
2908 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2909 count = 0;
2911 free_tlist(origline);
2913 tmp_defining = defining;
2914 defining = nasm_malloc(sizeof(MMacro));
2915 defining->prev = NULL;
2916 defining->name = NULL; /* flags this macro as a %rep block */
2917 defining->casesense = false;
2918 defining->plus = false;
2919 defining->nolist = nolist;
2920 defining->in_progress = count;
2921 defining->max_depth = 0;
2922 defining->nparam_min = defining->nparam_max = 0;
2923 defining->defaults = NULL;
2924 defining->dlist = NULL;
2925 defining->expansion = NULL;
2926 defining->next_active = istk->mstk;
2927 defining->rep_nest = tmp_defining;
2928 return DIRECTIVE_FOUND;
2930 case PP_ENDREP:
2931 if (!defining || defining->name) {
2932 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2933 return DIRECTIVE_FOUND;
2937 * Now we have a "macro" defined - although it has no name
2938 * and we won't be entering it in the hash tables - we must
2939 * push a macro-end marker for it on to istk->expansion.
2940 * After that, it will take care of propagating itself (a
2941 * macro-end marker line for a macro which is really a %rep
2942 * block will cause the macro to be re-expanded, complete
2943 * with another macro-end marker to ensure the process
2944 * continues) until the whole expansion is forcibly removed
2945 * from istk->expansion by a %exitrep.
2947 l = nasm_malloc(sizeof(Line));
2948 l->next = istk->expansion;
2949 l->finishes = defining;
2950 l->first = NULL;
2951 istk->expansion = l;
2953 istk->mstk = defining;
2955 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2956 tmp_defining = defining;
2957 defining = defining->rep_nest;
2958 free_tlist(origline);
2959 return DIRECTIVE_FOUND;
2961 case PP_EXITREP:
2963 * We must search along istk->expansion until we hit a
2964 * macro-end marker for a macro with no name. Then we set
2965 * its `in_progress' flag to 0.
2967 list_for_each(l, istk->expansion)
2968 if (l->finishes && !l->finishes->name)
2969 break;
2971 if (l)
2972 l->finishes->in_progress = 1;
2973 else
2974 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2975 free_tlist(origline);
2976 return DIRECTIVE_FOUND;
2978 case PP_XDEFINE:
2979 case PP_IXDEFINE:
2980 case PP_DEFINE:
2981 case PP_IDEFINE:
2982 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2984 tline = tline->next;
2985 skip_white_(tline);
2986 tline = expand_id(tline);
2987 if (!tline || (tline->type != TOK_ID &&
2988 (tline->type != TOK_PREPROC_ID ||
2989 tline->text[1] != '$'))) {
2990 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2991 pp_directives[i]);
2992 free_tlist(origline);
2993 return DIRECTIVE_FOUND;
2996 ctx = get_ctx(tline->text, &mname, false);
2997 last = tline;
2998 param_start = tline = tline->next;
2999 nparam = 0;
3001 /* Expand the macro definition now for %xdefine and %ixdefine */
3002 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3003 tline = expand_smacro(tline);
3005 if (tok_is_(tline, "(")) {
3007 * This macro has parameters.
3010 tline = tline->next;
3011 while (1) {
3012 skip_white_(tline);
3013 if (!tline) {
3014 error(ERR_NONFATAL, "parameter identifier expected");
3015 free_tlist(origline);
3016 return DIRECTIVE_FOUND;
3018 if (tline->type != TOK_ID) {
3019 error(ERR_NONFATAL,
3020 "`%s': parameter identifier expected",
3021 tline->text);
3022 free_tlist(origline);
3023 return DIRECTIVE_FOUND;
3025 tline->type = TOK_SMAC_PARAM + nparam++;
3026 tline = tline->next;
3027 skip_white_(tline);
3028 if (tok_is_(tline, ",")) {
3029 tline = tline->next;
3030 } else {
3031 if (!tok_is_(tline, ")")) {
3032 error(ERR_NONFATAL,
3033 "`)' expected to terminate macro template");
3034 free_tlist(origline);
3035 return DIRECTIVE_FOUND;
3037 break;
3040 last = tline;
3041 tline = tline->next;
3043 if (tok_type_(tline, TOK_WHITESPACE))
3044 last = tline, tline = tline->next;
3045 macro_start = NULL;
3046 last->next = NULL;
3047 t = tline;
3048 while (t) {
3049 if (t->type == TOK_ID) {
3050 list_for_each(tt, param_start)
3051 if (tt->type >= TOK_SMAC_PARAM &&
3052 !strcmp(tt->text, t->text))
3053 t->type = tt->type;
3055 tt = t->next;
3056 t->next = macro_start;
3057 macro_start = t;
3058 t = tt;
3061 * Good. We now have a macro name, a parameter count, and a
3062 * token list (in reverse order) for an expansion. We ought
3063 * to be OK just to create an SMacro, store it, and let
3064 * free_tlist have the rest of the line (which we have
3065 * carefully re-terminated after chopping off the expansion
3066 * from the end).
3068 define_smacro(ctx, mname, casesense, nparam, macro_start);
3069 free_tlist(origline);
3070 return DIRECTIVE_FOUND;
3072 case PP_UNDEF:
3073 tline = tline->next;
3074 skip_white_(tline);
3075 tline = expand_id(tline);
3076 if (!tline || (tline->type != TOK_ID &&
3077 (tline->type != TOK_PREPROC_ID ||
3078 tline->text[1] != '$'))) {
3079 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3080 free_tlist(origline);
3081 return DIRECTIVE_FOUND;
3083 if (tline->next) {
3084 error(ERR_WARNING|ERR_PASS1,
3085 "trailing garbage after macro name ignored");
3088 /* Find the context that symbol belongs to */
3089 ctx = get_ctx(tline->text, &mname, false);
3090 undef_smacro(ctx, mname);
3091 free_tlist(origline);
3092 return DIRECTIVE_FOUND;
3094 case PP_DEFSTR:
3095 case PP_IDEFSTR:
3096 casesense = (i == PP_DEFSTR);
3098 tline = tline->next;
3099 skip_white_(tline);
3100 tline = expand_id(tline);
3101 if (!tline || (tline->type != TOK_ID &&
3102 (tline->type != TOK_PREPROC_ID ||
3103 tline->text[1] != '$'))) {
3104 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3105 pp_directives[i]);
3106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
3110 ctx = get_ctx(tline->text, &mname, false);
3111 last = tline;
3112 tline = expand_smacro(tline->next);
3113 last->next = NULL;
3115 while (tok_type_(tline, TOK_WHITESPACE))
3116 tline = delete_Token(tline);
3118 p = detoken(tline, false);
3119 macro_start = nasm_malloc(sizeof(*macro_start));
3120 macro_start->next = NULL;
3121 macro_start->text = nasm_quote(p, strlen(p));
3122 macro_start->type = TOK_STRING;
3123 macro_start->a.mac = NULL;
3124 nasm_free(p);
3127 * We now have a macro name, an implicit parameter count of
3128 * zero, and a string token to use as an expansion. Create
3129 * and store an SMacro.
3131 define_smacro(ctx, mname, casesense, 0, macro_start);
3132 free_tlist(origline);
3133 return DIRECTIVE_FOUND;
3135 case PP_DEFTOK:
3136 case PP_IDEFTOK:
3137 casesense = (i == PP_DEFTOK);
3139 tline = tline->next;
3140 skip_white_(tline);
3141 tline = expand_id(tline);
3142 if (!tline || (tline->type != TOK_ID &&
3143 (tline->type != TOK_PREPROC_ID ||
3144 tline->text[1] != '$'))) {
3145 error(ERR_NONFATAL,
3146 "`%s' expects a macro identifier as first parameter",
3147 pp_directives[i]);
3148 free_tlist(origline);
3149 return DIRECTIVE_FOUND;
3151 ctx = get_ctx(tline->text, &mname, false);
3152 last = tline;
3153 tline = expand_smacro(tline->next);
3154 last->next = NULL;
3156 t = tline;
3157 while (tok_type_(t, TOK_WHITESPACE))
3158 t = t->next;
3159 /* t should now point to the string */
3160 if (t->type != TOK_STRING) {
3161 error(ERR_NONFATAL,
3162 "`%s` requires string as second parameter",
3163 pp_directives[i]);
3164 free_tlist(tline);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3169 nasm_unquote_cstr(t->text, i);
3170 macro_start = tokenize(t->text);
3173 * We now have a macro name, an implicit parameter count of
3174 * zero, and a numeric token to use as an expansion. Create
3175 * and store an SMacro.
3177 define_smacro(ctx, mname, casesense, 0, macro_start);
3178 free_tlist(tline);
3179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
3182 case PP_PATHSEARCH:
3184 FILE *fp;
3185 StrList *xsl = NULL;
3186 StrList **xst = &xsl;
3188 casesense = true;
3190 tline = tline->next;
3191 skip_white_(tline);
3192 tline = expand_id(tline);
3193 if (!tline || (tline->type != TOK_ID &&
3194 (tline->type != TOK_PREPROC_ID ||
3195 tline->text[1] != '$'))) {
3196 error(ERR_NONFATAL,
3197 "`%%pathsearch' expects a macro identifier as first parameter");
3198 free_tlist(origline);
3199 return DIRECTIVE_FOUND;
3201 ctx = get_ctx(tline->text, &mname, false);
3202 last = tline;
3203 tline = expand_smacro(tline->next);
3204 last->next = NULL;
3206 t = tline;
3207 while (tok_type_(t, TOK_WHITESPACE))
3208 t = t->next;
3210 if (!t || (t->type != TOK_STRING &&
3211 t->type != TOK_INTERNAL_STRING)) {
3212 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3213 free_tlist(tline);
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND; /* but we did _something_ */
3217 if (t->next)
3218 error(ERR_WARNING|ERR_PASS1,
3219 "trailing garbage after `%%pathsearch' ignored");
3220 p = t->text;
3221 if (t->type != TOK_INTERNAL_STRING)
3222 nasm_unquote(p, NULL);
3224 fp = inc_fopen(p, &xsl, &xst, true);
3225 if (fp) {
3226 p = xsl->str;
3227 fclose(fp); /* Don't actually care about the file */
3229 macro_start = nasm_malloc(sizeof(*macro_start));
3230 macro_start->next = NULL;
3231 macro_start->text = nasm_quote(p, strlen(p));
3232 macro_start->type = TOK_STRING;
3233 macro_start->a.mac = NULL;
3234 if (xsl)
3235 nasm_free(xsl);
3238 * We now have a macro name, an implicit parameter count of
3239 * zero, and a string token to use as an expansion. Create
3240 * and store an SMacro.
3242 define_smacro(ctx, mname, casesense, 0, macro_start);
3243 free_tlist(tline);
3244 free_tlist(origline);
3245 return DIRECTIVE_FOUND;
3248 case PP_STRLEN:
3249 casesense = true;
3251 tline = tline->next;
3252 skip_white_(tline);
3253 tline = expand_id(tline);
3254 if (!tline || (tline->type != TOK_ID &&
3255 (tline->type != TOK_PREPROC_ID ||
3256 tline->text[1] != '$'))) {
3257 error(ERR_NONFATAL,
3258 "`%%strlen' expects a macro identifier as first parameter");
3259 free_tlist(origline);
3260 return DIRECTIVE_FOUND;
3262 ctx = get_ctx(tline->text, &mname, false);
3263 last = tline;
3264 tline = expand_smacro(tline->next);
3265 last->next = NULL;
3267 t = tline;
3268 while (tok_type_(t, TOK_WHITESPACE))
3269 t = t->next;
3270 /* t should now point to the string */
3271 if (!tok_type_(t, TOK_STRING)) {
3272 error(ERR_NONFATAL,
3273 "`%%strlen` requires string as second parameter");
3274 free_tlist(tline);
3275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3279 macro_start = nasm_malloc(sizeof(*macro_start));
3280 macro_start->next = NULL;
3281 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3282 macro_start->a.mac = NULL;
3285 * We now have a macro name, an implicit parameter count of
3286 * zero, and a numeric token to use as an expansion. Create
3287 * and store an SMacro.
3289 define_smacro(ctx, mname, casesense, 0, macro_start);
3290 free_tlist(tline);
3291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3294 case PP_STRCAT:
3295 casesense = true;
3297 tline = tline->next;
3298 skip_white_(tline);
3299 tline = expand_id(tline);
3300 if (!tline || (tline->type != TOK_ID &&
3301 (tline->type != TOK_PREPROC_ID ||
3302 tline->text[1] != '$'))) {
3303 error(ERR_NONFATAL,
3304 "`%%strcat' expects a macro identifier as first parameter");
3305 free_tlist(origline);
3306 return DIRECTIVE_FOUND;
3308 ctx = get_ctx(tline->text, &mname, false);
3309 last = tline;
3310 tline = expand_smacro(tline->next);
3311 last->next = NULL;
3313 len = 0;
3314 list_for_each(t, tline) {
3315 switch (t->type) {
3316 case TOK_WHITESPACE:
3317 break;
3318 case TOK_STRING:
3319 len += t->a.len = nasm_unquote(t->text, NULL);
3320 break;
3321 case TOK_OTHER:
3322 if (!strcmp(t->text, ",")) /* permit comma separators */
3323 break;
3324 /* else fall through */
3325 default:
3326 error(ERR_NONFATAL,
3327 "non-string passed to `%%strcat' (%d)", t->type);
3328 free_tlist(tline);
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3334 p = pp = nasm_malloc(len);
3335 list_for_each(t, tline) {
3336 if (t->type == TOK_STRING) {
3337 memcpy(p, t->text, t->a.len);
3338 p += t->a.len;
3343 * We now have a macro name, an implicit parameter count of
3344 * zero, and a numeric token to use as an expansion. Create
3345 * and store an SMacro.
3347 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3348 macro_start->text = nasm_quote(pp, len);
3349 nasm_free(pp);
3350 define_smacro(ctx, mname, casesense, 0, macro_start);
3351 free_tlist(tline);
3352 free_tlist(origline);
3353 return DIRECTIVE_FOUND;
3355 case PP_SUBSTR:
3357 int64_t a1, a2;
3358 size_t len;
3360 casesense = true;
3362 tline = tline->next;
3363 skip_white_(tline);
3364 tline = expand_id(tline);
3365 if (!tline || (tline->type != TOK_ID &&
3366 (tline->type != TOK_PREPROC_ID ||
3367 tline->text[1] != '$'))) {
3368 error(ERR_NONFATAL,
3369 "`%%substr' expects a macro identifier as first parameter");
3370 free_tlist(origline);
3371 return DIRECTIVE_FOUND;
3373 ctx = get_ctx(tline->text, &mname, false);
3374 last = tline;
3375 tline = expand_smacro(tline->next);
3376 last->next = NULL;
3378 t = tline->next;
3379 while (tok_type_(t, TOK_WHITESPACE))
3380 t = t->next;
3382 /* t should now point to the string */
3383 if (t->type != TOK_STRING) {
3384 error(ERR_NONFATAL,
3385 "`%%substr` requires string as second parameter");
3386 free_tlist(tline);
3387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
3391 tt = t->next;
3392 tptr = &tt;
3393 tokval.t_type = TOKEN_INVALID;
3394 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3395 pass, error, NULL);
3396 if (!evalresult) {
3397 free_tlist(tline);
3398 free_tlist(origline);
3399 return DIRECTIVE_FOUND;
3400 } else if (!is_simple(evalresult)) {
3401 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3402 free_tlist(tline);
3403 free_tlist(origline);
3404 return DIRECTIVE_FOUND;
3406 a1 = evalresult->value-1;
3408 while (tok_type_(tt, TOK_WHITESPACE))
3409 tt = tt->next;
3410 if (!tt) {
3411 a2 = 1; /* Backwards compatibility: one character */
3412 } else {
3413 tokval.t_type = TOKEN_INVALID;
3414 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3415 pass, error, NULL);
3416 if (!evalresult) {
3417 free_tlist(tline);
3418 free_tlist(origline);
3419 return DIRECTIVE_FOUND;
3420 } else if (!is_simple(evalresult)) {
3421 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3422 free_tlist(tline);
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3426 a2 = evalresult->value;
3429 len = nasm_unquote(t->text, NULL);
3430 if (a2 < 0)
3431 a2 = a2+1+len-a1;
3432 if (a1+a2 > (int64_t)len)
3433 a2 = len-a1;
3435 macro_start = nasm_malloc(sizeof(*macro_start));
3436 macro_start->next = NULL;
3437 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3438 macro_start->type = TOK_STRING;
3439 macro_start->a.mac = NULL;
3442 * We now have a macro name, an implicit parameter count of
3443 * zero, and a numeric token to use as an expansion. Create
3444 * and store an SMacro.
3446 define_smacro(ctx, mname, casesense, 0, macro_start);
3447 free_tlist(tline);
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3452 case PP_ASSIGN:
3453 case PP_IASSIGN:
3454 casesense = (i == PP_ASSIGN);
3456 tline = tline->next;
3457 skip_white_(tline);
3458 tline = expand_id(tline);
3459 if (!tline || (tline->type != TOK_ID &&
3460 (tline->type != TOK_PREPROC_ID ||
3461 tline->text[1] != '$'))) {
3462 error(ERR_NONFATAL,
3463 "`%%%sassign' expects a macro identifier",
3464 (i == PP_IASSIGN ? "i" : ""));
3465 free_tlist(origline);
3466 return DIRECTIVE_FOUND;
3468 ctx = get_ctx(tline->text, &mname, false);
3469 last = tline;
3470 tline = expand_smacro(tline->next);
3471 last->next = NULL;
3473 t = tline;
3474 tptr = &t;
3475 tokval.t_type = TOKEN_INVALID;
3476 evalresult =
3477 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3478 free_tlist(tline);
3479 if (!evalresult) {
3480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
3484 if (tokval.t_type)
3485 error(ERR_WARNING|ERR_PASS1,
3486 "trailing garbage after expression ignored");
3488 if (!is_simple(evalresult)) {
3489 error(ERR_NONFATAL,
3490 "non-constant value given to `%%%sassign'",
3491 (i == PP_IASSIGN ? "i" : ""));
3492 free_tlist(origline);
3493 return DIRECTIVE_FOUND;
3496 macro_start = nasm_malloc(sizeof(*macro_start));
3497 macro_start->next = NULL;
3498 make_tok_num(macro_start, reloc_value(evalresult));
3499 macro_start->a.mac = NULL;
3502 * We now have a macro name, an implicit parameter count of
3503 * zero, and a numeric token to use as an expansion. Create
3504 * and store an SMacro.
3506 define_smacro(ctx, mname, casesense, 0, macro_start);
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3510 case PP_LINE:
3512 * Syntax is `%line nnn[+mmm] [filename]'
3514 tline = tline->next;
3515 skip_white_(tline);
3516 if (!tok_type_(tline, TOK_NUMBER)) {
3517 error(ERR_NONFATAL, "`%%line' expects line number");
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
3521 k = readnum(tline->text, &err);
3522 m = 1;
3523 tline = tline->next;
3524 if (tok_is_(tline, "+")) {
3525 tline = tline->next;
3526 if (!tok_type_(tline, TOK_NUMBER)) {
3527 error(ERR_NONFATAL, "`%%line' expects line increment");
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3531 m = readnum(tline->text, &err);
3532 tline = tline->next;
3534 skip_white_(tline);
3535 src_set_linnum(k);
3536 istk->lineinc = m;
3537 if (tline) {
3538 nasm_free(src_set_fname(detoken(tline, false)));
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
3543 default:
3544 error(ERR_FATAL,
3545 "preprocessor directive `%s' not yet implemented",
3546 pp_directives[i]);
3547 return DIRECTIVE_FOUND;
3552 * Ensure that a macro parameter contains a condition code and
3553 * nothing else. Return the condition code index if so, or -1
3554 * otherwise.
3556 static int find_cc(Token * t)
3558 Token *tt;
3559 int i, j, k, m;
3561 if (!t)
3562 return -1; /* Probably a %+ without a space */
3564 skip_white_(t);
3565 if (t->type != TOK_ID)
3566 return -1;
3567 tt = t->next;
3568 skip_white_(tt);
3569 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3570 return -1;
3572 i = -1;
3573 j = ARRAY_SIZE(conditions);
3574 while (j - i > 1) {
3575 k = (j + i) / 2;
3576 m = nasm_stricmp(t->text, conditions[k]);
3577 if (m == 0) {
3578 i = k;
3579 j = -2;
3580 break;
3581 } else if (m < 0) {
3582 j = k;
3583 } else
3584 i = k;
3586 if (j != -2)
3587 return -1;
3588 return i;
3591 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3593 Token **tail, *t, *tt;
3594 Token **paste_head;
3595 bool did_paste = false;
3596 char *tmp;
3598 /* Now handle token pasting... */
3599 paste_head = NULL;
3600 tail = head;
3601 while ((t = *tail) && (tt = t->next)) {
3602 switch (t->type) {
3603 case TOK_WHITESPACE:
3604 if (tt->type == TOK_WHITESPACE) {
3605 /* Zap adjacent whitespace tokens */
3606 t->next = delete_Token(tt);
3607 } else {
3608 /* Do not advance paste_head here */
3609 tail = &t->next;
3611 break;
3612 case TOK_ID:
3613 case TOK_NUMBER:
3614 case TOK_FLOAT:
3616 size_t len = 0;
3617 char *tmp, *p;
3619 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3620 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3621 tt->type == TOK_OTHER)) {
3622 len += strlen(tt->text);
3623 tt = tt->next;
3627 * Now tt points to the first token after
3628 * the potential paste area...
3630 if (tt != t->next) {
3631 /* We have at least two tokens... */
3632 len += strlen(t->text);
3633 p = tmp = nasm_malloc(len+1);
3635 while (t != tt) {
3636 strcpy(p, t->text);
3637 p = strchr(p, '\0');
3638 t = delete_Token(t);
3641 t = *tail = tokenize(tmp);
3642 nasm_free(tmp);
3644 while (t->next) {
3645 tail = &t->next;
3646 t = t->next;
3648 t->next = tt; /* Attach the remaining token chain */
3650 did_paste = true;
3652 paste_head = tail;
3653 tail = &t->next;
3654 break;
3656 case TOK_PASTE: /* %+ */
3657 if (handle_paste_tokens) {
3658 /* Zap %+ and whitespace tokens to the right */
3659 while (t && (t->type == TOK_WHITESPACE ||
3660 t->type == TOK_PASTE))
3661 t = *tail = delete_Token(t);
3662 if (!paste_head || !t)
3663 break; /* Nothing to paste with */
3664 tail = paste_head;
3665 t = *tail;
3666 tt = t->next;
3667 while (tok_type_(tt, TOK_WHITESPACE))
3668 tt = t->next = delete_Token(tt);
3670 if (tt) {
3671 tmp = nasm_strcat(t->text, tt->text);
3672 delete_Token(t);
3673 tt = delete_Token(tt);
3674 t = *tail = tokenize(tmp);
3675 nasm_free(tmp);
3676 while (t->next) {
3677 tail = &t->next;
3678 t = t->next;
3680 t->next = tt; /* Attach the remaining token chain */
3681 did_paste = true;
3683 paste_head = tail;
3684 tail = &t->next;
3685 break;
3687 /* else fall through */
3688 default:
3689 tail = &t->next;
3690 if (!tok_type_(t->next, TOK_WHITESPACE))
3691 paste_head = tail;
3692 break;
3695 return did_paste;
3699 * expands to a list of tokens from %{x:y}
3701 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3703 Token *t = tline, **tt, *tm, *head;
3704 char *pos;
3705 int fst, lst, j, i;
3707 pos = strchr(tline->text, ':');
3708 nasm_assert(pos);
3710 lst = atoi(pos + 1);
3711 fst = atoi(tline->text + 1);
3714 * only macros params are accounted so
3715 * if someone passes %0 -- we reject such
3716 * value(s)
3718 if (lst == 0 || fst == 0)
3719 goto err;
3721 /* the values should be sane */
3722 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3723 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3724 goto err;
3726 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3727 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3729 /* counted from zero */
3730 fst--, lst--;
3733 * it will be at least one token
3735 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3736 t = new_Token(NULL, tm->type, tm->text, 0);
3737 head = t, tt = &t->next;
3738 if (fst < lst) {
3739 for (i = fst + 1; i <= lst; i++) {
3740 t = new_Token(NULL, TOK_OTHER, ",", 0);
3741 *tt = t, tt = &t->next;
3742 j = (i + mac->rotate) % mac->nparam;
3743 tm = mac->params[j];
3744 t = new_Token(NULL, tm->type, tm->text, 0);
3745 *tt = t, tt = &t->next;
3747 } else {
3748 for (i = fst - 1; i >= lst; i--) {
3749 t = new_Token(NULL, TOK_OTHER, ",", 0);
3750 *tt = t, tt = &t->next;
3751 j = (i + mac->rotate) % mac->nparam;
3752 tm = mac->params[j];
3753 t = new_Token(NULL, tm->type, tm->text, 0);
3754 *tt = t, tt = &t->next;
3758 *last = tt;
3759 return head;
3761 err:
3762 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3763 &tline->text[1]);
3764 return tline;
3768 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3769 * %-n) and MMacro-local identifiers (%%foo) as well as
3770 * macro indirection (%[...]) and range (%{..:..}).
3772 static Token *expand_mmac_params(Token * tline)
3774 Token *t, *tt, **tail, *thead;
3775 bool changed = false;
3776 char *pos;
3778 tail = &thead;
3779 thead = NULL;
3781 while (tline) {
3782 if (tline->type == TOK_PREPROC_ID &&
3783 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3784 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3785 tline->text[1] == '%')) {
3786 char *text = NULL;
3787 int type = 0, cc; /* type = 0 to placate optimisers */
3788 char tmpbuf[30];
3789 unsigned int n;
3790 int i;
3791 MMacro *mac;
3793 t = tline;
3794 tline = tline->next;
3796 mac = istk->mstk;
3797 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3798 mac = mac->next_active;
3799 if (!mac) {
3800 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3801 } else {
3802 pos = strchr(t->text, ':');
3803 if (!pos) {
3804 switch (t->text[1]) {
3806 * We have to make a substitution of one of the
3807 * forms %1, %-1, %+1, %%foo, %0.
3809 case '0':
3810 type = TOK_NUMBER;
3811 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3812 text = nasm_strdup(tmpbuf);
3813 break;
3814 case '%':
3815 type = TOK_ID;
3816 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3817 mac->unique);
3818 text = nasm_strcat(tmpbuf, t->text + 2);
3819 break;
3820 case '-':
3821 n = atoi(t->text + 2) - 1;
3822 if (n >= mac->nparam)
3823 tt = NULL;
3824 else {
3825 if (mac->nparam > 1)
3826 n = (n + mac->rotate) % mac->nparam;
3827 tt = mac->params[n];
3829 cc = find_cc(tt);
3830 if (cc == -1) {
3831 error(ERR_NONFATAL,
3832 "macro parameter %d is not a condition code",
3833 n + 1);
3834 text = NULL;
3835 } else {
3836 type = TOK_ID;
3837 if (inverse_ccs[cc] == -1) {
3838 error(ERR_NONFATAL,
3839 "condition code `%s' is not invertible",
3840 conditions[cc]);
3841 text = NULL;
3842 } else
3843 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3845 break;
3846 case '+':
3847 n = atoi(t->text + 2) - 1;
3848 if (n >= mac->nparam)
3849 tt = NULL;
3850 else {
3851 if (mac->nparam > 1)
3852 n = (n + mac->rotate) % mac->nparam;
3853 tt = mac->params[n];
3855 cc = find_cc(tt);
3856 if (cc == -1) {
3857 error(ERR_NONFATAL,
3858 "macro parameter %d is not a condition code",
3859 n + 1);
3860 text = NULL;
3861 } else {
3862 type = TOK_ID;
3863 text = nasm_strdup(conditions[cc]);
3865 break;
3866 default:
3867 n = atoi(t->text + 1) - 1;
3868 if (n >= mac->nparam)
3869 tt = NULL;
3870 else {
3871 if (mac->nparam > 1)
3872 n = (n + mac->rotate) % mac->nparam;
3873 tt = mac->params[n];
3875 if (tt) {
3876 for (i = 0; i < mac->paramlen[n]; i++) {
3877 *tail = new_Token(NULL, tt->type, tt->text, 0);
3878 tail = &(*tail)->next;
3879 tt = tt->next;
3882 text = NULL; /* we've done it here */
3883 break;
3885 } else {
3887 * seems we have a parameters range here
3889 Token *head, **last;
3890 head = expand_mmac_params_range(mac, t, &last);
3891 if (head != t) {
3892 *tail = head;
3893 *last = tline;
3894 tline = head;
3895 text = NULL;
3899 if (!text) {
3900 delete_Token(t);
3901 } else {
3902 *tail = t;
3903 tail = &t->next;
3904 t->type = type;
3905 nasm_free(t->text);
3906 t->text = text;
3907 t->a.mac = NULL;
3909 changed = true;
3910 continue;
3911 } else if (tline->type == TOK_INDIRECT) {
3912 t = tline;
3913 tline = tline->next;
3914 tt = tokenize(t->text);
3915 tt = expand_mmac_params(tt);
3916 tt = expand_smacro(tt);
3917 *tail = tt;
3918 while (tt) {
3919 tt->a.mac = NULL; /* Necessary? */
3920 tail = &tt->next;
3921 tt = tt->next;
3923 delete_Token(t);
3924 changed = true;
3925 } else {
3926 t = *tail = tline;
3927 tline = tline->next;
3928 t->a.mac = NULL;
3929 tail = &t->next;
3932 *tail = NULL;
3934 if (changed)
3935 paste_tokens(&thead, false);
3937 return thead;
3941 * Expand all single-line macro calls made in the given line.
3942 * Return the expanded version of the line. The original is deemed
3943 * to be destroyed in the process. (In reality we'll just move
3944 * Tokens from input to output a lot of the time, rather than
3945 * actually bothering to destroy and replicate.)
3948 static Token *expand_smacro(Token * tline)
3950 Token *t, *tt, *mstart, **tail, *thead;
3951 SMacro *head = NULL, *m;
3952 Token **params;
3953 int *paramsize;
3954 unsigned int nparam, sparam;
3955 int brackets;
3956 Token *org_tline = tline;
3957 Context *ctx;
3958 const char *mname;
3959 int deadman = DEADMAN_LIMIT;
3960 bool expanded;
3963 * Trick: we should avoid changing the start token pointer since it can
3964 * be contained in "next" field of other token. Because of this
3965 * we allocate a copy of first token and work with it; at the end of
3966 * routine we copy it back
3968 if (org_tline) {
3969 tline = new_Token(org_tline->next, org_tline->type,
3970 org_tline->text, 0);
3971 tline->a.mac = org_tline->a.mac;
3972 nasm_free(org_tline->text);
3973 org_tline->text = NULL;
3976 expanded = true; /* Always expand %+ at least once */
3978 again:
3979 thead = NULL;
3980 tail = &thead;
3982 while (tline) { /* main token loop */
3983 if (!--deadman) {
3984 error(ERR_NONFATAL, "interminable macro recursion");
3985 goto err;
3988 if ((mname = tline->text)) {
3989 /* if this token is a local macro, look in local context */
3990 if (tline->type == TOK_ID) {
3991 head = (SMacro *)hash_findix(&smacros, mname);
3992 } else if (tline->type == TOK_PREPROC_ID) {
3993 ctx = get_ctx(mname, &mname, true);
3994 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3995 } else
3996 head = NULL;
3999 * We've hit an identifier. As in is_mmacro below, we first
4000 * check whether the identifier is a single-line macro at
4001 * all, then think about checking for parameters if
4002 * necessary.
4004 list_for_each(m, head)
4005 if (!mstrcmp(m->name, mname, m->casesense))
4006 break;
4007 if (m) {
4008 mstart = tline;
4009 params = NULL;
4010 paramsize = NULL;
4011 if (m->nparam == 0) {
4013 * Simple case: the macro is parameterless. Discard the
4014 * one token that the macro call took, and push the
4015 * expansion back on the to-do stack.
4017 if (!m->expansion) {
4018 if (!strcmp("__FILE__", m->name)) {
4019 int32_t num = 0;
4020 char *file = NULL;
4021 src_get(&num, &file);
4022 tline->text = nasm_quote(file, strlen(file));
4023 tline->type = TOK_STRING;
4024 nasm_free(file);
4025 continue;
4027 if (!strcmp("__LINE__", m->name)) {
4028 nasm_free(tline->text);
4029 make_tok_num(tline, src_get_linnum());
4030 continue;
4032 if (!strcmp("__BITS__", m->name)) {
4033 nasm_free(tline->text);
4034 make_tok_num(tline, globalbits);
4035 continue;
4037 tline = delete_Token(tline);
4038 continue;
4040 } else {
4042 * Complicated case: at least one macro with this name
4043 * exists and takes parameters. We must find the
4044 * parameters in the call, count them, find the SMacro
4045 * that corresponds to that form of the macro call, and
4046 * substitute for the parameters when we expand. What a
4047 * pain.
4049 /*tline = tline->next;
4050 skip_white_(tline); */
4051 do {
4052 t = tline->next;
4053 while (tok_type_(t, TOK_SMAC_END)) {
4054 t->a.mac->in_progress = false;
4055 t->text = NULL;
4056 t = tline->next = delete_Token(t);
4058 tline = t;
4059 } while (tok_type_(tline, TOK_WHITESPACE));
4060 if (!tok_is_(tline, "(")) {
4062 * This macro wasn't called with parameters: ignore
4063 * the call. (Behaviour borrowed from gnu cpp.)
4065 tline = mstart;
4066 m = NULL;
4067 } else {
4068 int paren = 0;
4069 int white = 0;
4070 brackets = 0;
4071 nparam = 0;
4072 sparam = PARAM_DELTA;
4073 params = nasm_malloc(sparam * sizeof(Token *));
4074 params[0] = tline->next;
4075 paramsize = nasm_malloc(sparam * sizeof(int));
4076 paramsize[0] = 0;
4077 while (true) { /* parameter loop */
4079 * For some unusual expansions
4080 * which concatenates function call
4082 t = tline->next;
4083 while (tok_type_(t, TOK_SMAC_END)) {
4084 t->a.mac->in_progress = false;
4085 t->text = NULL;
4086 t = tline->next = delete_Token(t);
4088 tline = t;
4090 if (!tline) {
4091 error(ERR_NONFATAL,
4092 "macro call expects terminating `)'");
4093 break;
4095 if (tline->type == TOK_WHITESPACE
4096 && brackets <= 0) {
4097 if (paramsize[nparam])
4098 white++;
4099 else
4100 params[nparam] = tline->next;
4101 continue; /* parameter loop */
4103 if (tline->type == TOK_OTHER
4104 && tline->text[1] == 0) {
4105 char ch = tline->text[0];
4106 if (ch == ',' && !paren && brackets <= 0) {
4107 if (++nparam >= sparam) {
4108 sparam += PARAM_DELTA;
4109 params = nasm_realloc(params,
4110 sparam * sizeof(Token *));
4111 paramsize = nasm_realloc(paramsize,
4112 sparam * sizeof(int));
4114 params[nparam] = tline->next;
4115 paramsize[nparam] = 0;
4116 white = 0;
4117 continue; /* parameter loop */
4119 if (ch == '{' &&
4120 (brackets > 0 || (brackets == 0 &&
4121 !paramsize[nparam])))
4123 if (!(brackets++)) {
4124 params[nparam] = tline->next;
4125 continue; /* parameter loop */
4128 if (ch == '}' && brackets > 0)
4129 if (--brackets == 0) {
4130 brackets = -1;
4131 continue; /* parameter loop */
4133 if (ch == '(' && !brackets)
4134 paren++;
4135 if (ch == ')' && brackets <= 0)
4136 if (--paren < 0)
4137 break;
4139 if (brackets < 0) {
4140 brackets = 0;
4141 error(ERR_NONFATAL, "braces do not "
4142 "enclose all of macro parameter");
4144 paramsize[nparam] += white + 1;
4145 white = 0;
4146 } /* parameter loop */
4147 nparam++;
4148 while (m && (m->nparam != nparam ||
4149 mstrcmp(m->name, mname,
4150 m->casesense)))
4151 m = m->next;
4152 if (!m)
4153 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4154 "macro `%s' exists, "
4155 "but not taking %d parameters",
4156 mstart->text, nparam);
4159 if (m && m->in_progress)
4160 m = NULL;
4161 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4163 * Design question: should we handle !tline, which
4164 * indicates missing ')' here, or expand those
4165 * macros anyway, which requires the (t) test a few
4166 * lines down?
4168 nasm_free(params);
4169 nasm_free(paramsize);
4170 tline = mstart;
4171 } else {
4173 * Expand the macro: we are placed on the last token of the
4174 * call, so that we can easily split the call from the
4175 * following tokens. We also start by pushing an SMAC_END
4176 * token for the cycle removal.
4178 t = tline;
4179 if (t) {
4180 tline = t->next;
4181 t->next = NULL;
4183 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4184 tt->a.mac = m;
4185 m->in_progress = true;
4186 tline = tt;
4187 list_for_each(t, m->expansion) {
4188 if (t->type >= TOK_SMAC_PARAM) {
4189 Token *pcopy = tline, **ptail = &pcopy;
4190 Token *ttt, *pt;
4191 int i;
4193 ttt = params[t->type - TOK_SMAC_PARAM];
4194 i = paramsize[t->type - TOK_SMAC_PARAM];
4195 while (--i >= 0) {
4196 pt = *ptail = new_Token(tline, ttt->type,
4197 ttt->text, 0);
4198 ptail = &pt->next;
4199 ttt = ttt->next;
4201 tline = pcopy;
4202 } else if (t->type == TOK_PREPROC_Q) {
4203 tt = new_Token(tline, TOK_ID, mname, 0);
4204 tline = tt;
4205 } else if (t->type == TOK_PREPROC_QQ) {
4206 tt = new_Token(tline, TOK_ID, m->name, 0);
4207 tline = tt;
4208 } else {
4209 tt = new_Token(tline, t->type, t->text, 0);
4210 tline = tt;
4215 * Having done that, get rid of the macro call, and clean
4216 * up the parameters.
4218 nasm_free(params);
4219 nasm_free(paramsize);
4220 free_tlist(mstart);
4221 expanded = true;
4222 continue; /* main token loop */
4227 if (tline->type == TOK_SMAC_END) {
4228 tline->a.mac->in_progress = false;
4229 tline = delete_Token(tline);
4230 } else {
4231 t = *tail = tline;
4232 tline = tline->next;
4233 t->a.mac = NULL;
4234 t->next = NULL;
4235 tail = &t->next;
4240 * Now scan the entire line and look for successive TOK_IDs that resulted
4241 * after expansion (they can't be produced by tokenize()). The successive
4242 * TOK_IDs should be concatenated.
4243 * Also we look for %+ tokens and concatenate the tokens before and after
4244 * them (without white spaces in between).
4246 if (expanded && paste_tokens(&thead, true)) {
4248 * If we concatenated something, *and* we had previously expanded
4249 * an actual macro, scan the lines again for macros...
4251 tline = thead;
4252 expanded = false;
4253 goto again;
4256 err:
4257 if (org_tline) {
4258 if (thead) {
4259 *org_tline = *thead;
4260 /* since we just gave text to org_line, don't free it */
4261 thead->text = NULL;
4262 delete_Token(thead);
4263 } else {
4264 /* the expression expanded to empty line;
4265 we can't return NULL for some reasons
4266 we just set the line to a single WHITESPACE token. */
4267 memset(org_tline, 0, sizeof(*org_tline));
4268 org_tline->text = NULL;
4269 org_tline->type = TOK_WHITESPACE;
4271 thead = org_tline;
4274 return thead;
4278 * Similar to expand_smacro but used exclusively with macro identifiers
4279 * right before they are fetched in. The reason is that there can be
4280 * identifiers consisting of several subparts. We consider that if there
4281 * are more than one element forming the name, user wants a expansion,
4282 * otherwise it will be left as-is. Example:
4284 * %define %$abc cde
4286 * the identifier %$abc will be left as-is so that the handler for %define
4287 * will suck it and define the corresponding value. Other case:
4289 * %define _%$abc cde
4291 * In this case user wants name to be expanded *before* %define starts
4292 * working, so we'll expand %$abc into something (if it has a value;
4293 * otherwise it will be left as-is) then concatenate all successive
4294 * PP_IDs into one.
4296 static Token *expand_id(Token * tline)
4298 Token *cur, *oldnext = NULL;
4300 if (!tline || !tline->next)
4301 return tline;
4303 cur = tline;
4304 while (cur->next &&
4305 (cur->next->type == TOK_ID ||
4306 cur->next->type == TOK_PREPROC_ID
4307 || cur->next->type == TOK_NUMBER))
4308 cur = cur->next;
4310 /* If identifier consists of just one token, don't expand */
4311 if (cur == tline)
4312 return tline;
4314 if (cur) {
4315 oldnext = cur->next; /* Detach the tail past identifier */
4316 cur->next = NULL; /* so that expand_smacro stops here */
4319 tline = expand_smacro(tline);
4321 if (cur) {
4322 /* expand_smacro possibly changhed tline; re-scan for EOL */
4323 cur = tline;
4324 while (cur && cur->next)
4325 cur = cur->next;
4326 if (cur)
4327 cur->next = oldnext;
4330 return tline;
4334 * Determine whether the given line constitutes a multi-line macro
4335 * call, and return the MMacro structure called if so. Doesn't have
4336 * to check for an initial label - that's taken care of in
4337 * expand_mmacro - but must check numbers of parameters. Guaranteed
4338 * to be called with tline->type == TOK_ID, so the putative macro
4339 * name is easy to find.
4341 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4343 MMacro *head, *m;
4344 Token **params;
4345 int nparam;
4347 head = (MMacro *) hash_findix(&mmacros, tline->text);
4350 * Efficiency: first we see if any macro exists with the given
4351 * name. If not, we can return NULL immediately. _Then_ we
4352 * count the parameters, and then we look further along the
4353 * list if necessary to find the proper MMacro.
4355 list_for_each(m, head)
4356 if (!mstrcmp(m->name, tline->text, m->casesense))
4357 break;
4358 if (!m)
4359 return NULL;
4362 * OK, we have a potential macro. Count and demarcate the
4363 * parameters.
4365 count_mmac_params(tline->next, &nparam, &params);
4368 * So we know how many parameters we've got. Find the MMacro
4369 * structure that handles this number.
4371 while (m) {
4372 if (m->nparam_min <= nparam
4373 && (m->plus || nparam <= m->nparam_max)) {
4375 * This one is right. Just check if cycle removal
4376 * prohibits us using it before we actually celebrate...
4378 if (m->in_progress > m->max_depth) {
4379 if (m->max_depth > 0) {
4380 error(ERR_WARNING,
4381 "reached maximum recursion depth of %i",
4382 m->max_depth);
4384 nasm_free(params);
4385 return NULL;
4388 * It's right, and we can use it. Add its default
4389 * parameters to the end of our list if necessary.
4391 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4392 params =
4393 nasm_realloc(params,
4394 ((m->nparam_min + m->ndefs +
4395 1) * sizeof(*params)));
4396 while (nparam < m->nparam_min + m->ndefs) {
4397 params[nparam] = m->defaults[nparam - m->nparam_min];
4398 nparam++;
4402 * If we've gone over the maximum parameter count (and
4403 * we're in Plus mode), ignore parameters beyond
4404 * nparam_max.
4406 if (m->plus && nparam > m->nparam_max)
4407 nparam = m->nparam_max;
4409 * Then terminate the parameter list, and leave.
4411 if (!params) { /* need this special case */
4412 params = nasm_malloc(sizeof(*params));
4413 nparam = 0;
4415 params[nparam] = NULL;
4416 *params_array = params;
4417 return m;
4420 * This one wasn't right: look for the next one with the
4421 * same name.
4423 list_for_each(m, m->next)
4424 if (!mstrcmp(m->name, tline->text, m->casesense))
4425 break;
4429 * After all that, we didn't find one with the right number of
4430 * parameters. Issue a warning, and fail to expand the macro.
4432 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4433 "macro `%s' exists, but not taking %d parameters",
4434 tline->text, nparam);
4435 nasm_free(params);
4436 return NULL;
4441 * Save MMacro invocation specific fields in
4442 * preparation for a recursive macro expansion
4444 static void push_mmacro(MMacro *m)
4446 MMacroInvocation *i;
4448 i = nasm_malloc(sizeof(MMacroInvocation));
4449 i->prev = m->prev;
4450 i->params = m->params;
4451 i->iline = m->iline;
4452 i->nparam = m->nparam;
4453 i->rotate = m->rotate;
4454 i->paramlen = m->paramlen;
4455 i->unique = m->unique;
4456 i->condcnt = m->condcnt;
4457 m->prev = i;
4462 * Restore MMacro invocation specific fields that were
4463 * saved during a previous recursive macro expansion
4465 static void pop_mmacro(MMacro *m)
4467 MMacroInvocation *i;
4469 if (m->prev) {
4470 i = m->prev;
4471 m->prev = i->prev;
4472 m->params = i->params;
4473 m->iline = i->iline;
4474 m->nparam = i->nparam;
4475 m->rotate = i->rotate;
4476 m->paramlen = i->paramlen;
4477 m->unique = i->unique;
4478 m->condcnt = i->condcnt;
4479 nasm_free(i);
4485 * Expand the multi-line macro call made by the given line, if
4486 * there is one to be expanded. If there is, push the expansion on
4487 * istk->expansion and return 1. Otherwise return 0.
4489 static int expand_mmacro(Token * tline)
4491 Token *startline = tline;
4492 Token *label = NULL;
4493 int dont_prepend = 0;
4494 Token **params, *t, *mtok, *tt;
4495 MMacro *m;
4496 Line *l, *ll;
4497 int i, nparam, *paramlen;
4498 const char *mname;
4500 t = tline;
4501 skip_white_(t);
4502 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4503 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4504 return 0;
4505 mtok = t;
4506 m = is_mmacro(t, &params);
4507 if (m) {
4508 mname = t->text;
4509 } else {
4510 Token *last;
4512 * We have an id which isn't a macro call. We'll assume
4513 * it might be a label; we'll also check to see if a
4514 * colon follows it. Then, if there's another id after
4515 * that lot, we'll check it again for macro-hood.
4517 label = last = t;
4518 t = t->next;
4519 if (tok_type_(t, TOK_WHITESPACE))
4520 last = t, t = t->next;
4521 if (tok_is_(t, ":")) {
4522 dont_prepend = 1;
4523 last = t, t = t->next;
4524 if (tok_type_(t, TOK_WHITESPACE))
4525 last = t, t = t->next;
4527 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4528 return 0;
4529 last->next = NULL;
4530 mname = t->text;
4531 tline = t;
4535 * Fix up the parameters: this involves stripping leading and
4536 * trailing whitespace, then stripping braces if they are
4537 * present.
4539 for (nparam = 0; params[nparam]; nparam++) ;
4540 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4542 for (i = 0; params[i]; i++) {
4543 int brace = false;
4544 int comma = (!m->plus || i < nparam - 1);
4546 t = params[i];
4547 skip_white_(t);
4548 if (tok_is_(t, "{"))
4549 t = t->next, brace = true, comma = false;
4550 params[i] = t;
4551 paramlen[i] = 0;
4552 while (t) {
4553 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4554 break; /* ... because we have hit a comma */
4555 if (comma && t->type == TOK_WHITESPACE
4556 && tok_is_(t->next, ","))
4557 break; /* ... or a space then a comma */
4558 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4559 break; /* ... or a brace */
4560 t = t->next;
4561 paramlen[i]++;
4566 * OK, we have a MMacro structure together with a set of
4567 * parameters. We must now go through the expansion and push
4568 * copies of each Line on to istk->expansion. Substitution of
4569 * parameter tokens and macro-local tokens doesn't get done
4570 * until the single-line macro substitution process; this is
4571 * because delaying them allows us to change the semantics
4572 * later through %rotate.
4574 * First, push an end marker on to istk->expansion, mark this
4575 * macro as in progress, and set up its invocation-specific
4576 * variables.
4578 ll = nasm_malloc(sizeof(Line));
4579 ll->next = istk->expansion;
4580 ll->finishes = m;
4581 ll->first = NULL;
4582 istk->expansion = ll;
4585 * Save the previous MMacro expansion in the case of
4586 * macro recursion
4588 if (m->max_depth && m->in_progress)
4589 push_mmacro(m);
4591 m->in_progress ++;
4592 m->params = params;
4593 m->iline = tline;
4594 m->nparam = nparam;
4595 m->rotate = 0;
4596 m->paramlen = paramlen;
4597 m->unique = unique++;
4598 m->lineno = 0;
4599 m->condcnt = 0;
4601 m->next_active = istk->mstk;
4602 istk->mstk = m;
4604 list_for_each(l, m->expansion) {
4605 Token **tail;
4607 ll = nasm_malloc(sizeof(Line));
4608 ll->finishes = NULL;
4609 ll->next = istk->expansion;
4610 istk->expansion = ll;
4611 tail = &ll->first;
4613 list_for_each(t, l->first) {
4614 Token *x = t;
4615 switch (t->type) {
4616 case TOK_PREPROC_Q:
4617 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4618 break;
4619 case TOK_PREPROC_QQ:
4620 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4621 break;
4622 case TOK_PREPROC_ID:
4623 if (t->text[1] == '0' && t->text[2] == '0') {
4624 dont_prepend = -1;
4625 x = label;
4626 if (!x)
4627 continue;
4629 /* fall through */
4630 default:
4631 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4632 break;
4634 tail = &tt->next;
4636 *tail = NULL;
4640 * If we had a label, push it on as the first line of
4641 * the macro expansion.
4643 if (label) {
4644 if (dont_prepend < 0)
4645 free_tlist(startline);
4646 else {
4647 ll = nasm_malloc(sizeof(Line));
4648 ll->finishes = NULL;
4649 ll->next = istk->expansion;
4650 istk->expansion = ll;
4651 ll->first = startline;
4652 if (!dont_prepend) {
4653 while (label->next)
4654 label = label->next;
4655 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4660 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4662 return 1;
4665 /* The function that actually does the error reporting */
4666 static void verror(int severity, const char *fmt, va_list arg)
4668 char buff[1024];
4670 vsnprintf(buff, sizeof(buff), fmt, arg);
4672 if (istk && istk->mstk && istk->mstk->name)
4673 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4674 istk->mstk->lineno, buff);
4675 else
4676 nasm_error(severity, "%s", buff);
4680 * Since preprocessor always operate only on the line that didn't
4681 * arrived yet, we should always use ERR_OFFBY1.
4683 static void error(int severity, const char *fmt, ...)
4685 va_list arg;
4687 /* If we're in a dead branch of IF or something like it, ignore the error */
4688 if (istk && istk->conds && !emitting(istk->conds->state))
4689 return;
4691 va_start(arg, fmt);
4692 verror(severity, fmt, arg);
4693 va_end(arg);
4697 * Because %else etc are evaluated in the state context
4698 * of the previous branch, errors might get lost with error():
4699 * %if 0 ... %else trailing garbage ... %endif
4700 * So %else etc should report errors with this function.
4702 static void error_precond(int severity, const char *fmt, ...)
4704 va_list arg;
4706 /* Only ignore the error if it's really in a dead branch */
4707 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4708 return;
4710 va_start(arg, fmt);
4711 verror(severity, fmt, arg);
4712 va_end(arg);
4715 static void
4716 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4718 Token *t;
4720 cstk = NULL;
4721 istk = nasm_malloc(sizeof(Include));
4722 istk->next = NULL;
4723 istk->conds = NULL;
4724 istk->expansion = NULL;
4725 istk->mstk = NULL;
4726 istk->fp = fopen(file, "r");
4727 istk->fname = NULL;
4728 src_set_fname(nasm_strdup(file));
4729 src_set_linnum(0);
4730 istk->lineinc = 1;
4731 if (!istk->fp)
4732 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4733 file);
4734 defining = NULL;
4735 nested_mac_count = 0;
4736 nested_rep_count = 0;
4737 init_macros();
4738 unique = 0;
4739 if (tasm_compatible_mode) {
4740 stdmacpos = nasm_stdmac;
4741 } else {
4742 stdmacpos = nasm_stdmac_after_tasm;
4744 any_extrastdmac = extrastdmac && *extrastdmac;
4745 do_predef = true;
4746 list = listgen;
4749 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4750 * The caller, however, will also pass in 3 for preprocess-only so
4751 * we can set __PASS__ accordingly.
4753 pass = apass > 2 ? 2 : apass;
4755 dephead = deptail = deplist;
4756 if (deplist) {
4757 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4758 sl->next = NULL;
4759 strcpy(sl->str, file);
4760 *deptail = sl;
4761 deptail = &sl->next;
4765 * Define the __PASS__ macro. This is defined here unlike
4766 * all the other builtins, because it is special -- it varies between
4767 * passes.
4769 t = nasm_malloc(sizeof(*t));
4770 t->next = NULL;
4771 make_tok_num(t, apass);
4772 t->a.mac = NULL;
4773 define_smacro(NULL, "__PASS__", true, 0, t);
4776 static char *pp_getline(void)
4778 char *line;
4779 Token *tline;
4781 while (1) {
4783 * Fetch a tokenized line, either from the macro-expansion
4784 * buffer or from the input file.
4786 tline = NULL;
4787 while (istk->expansion && istk->expansion->finishes) {
4788 Line *l = istk->expansion;
4789 if (!l->finishes->name && l->finishes->in_progress > 1) {
4790 Line *ll;
4793 * This is a macro-end marker for a macro with no
4794 * name, which means it's not really a macro at all
4795 * but a %rep block, and the `in_progress' field is
4796 * more than 1, meaning that we still need to
4797 * repeat. (1 means the natural last repetition; 0
4798 * means termination by %exitrep.) We have
4799 * therefore expanded up to the %endrep, and must
4800 * push the whole block on to the expansion buffer
4801 * again. We don't bother to remove the macro-end
4802 * marker: we'd only have to generate another one
4803 * if we did.
4805 l->finishes->in_progress--;
4806 list_for_each(l, l->finishes->expansion) {
4807 Token *t, *tt, **tail;
4809 ll = nasm_malloc(sizeof(Line));
4810 ll->next = istk->expansion;
4811 ll->finishes = NULL;
4812 ll->first = NULL;
4813 tail = &ll->first;
4815 list_for_each(t, l->first) {
4816 if (t->text || t->type == TOK_WHITESPACE) {
4817 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4818 tail = &tt->next;
4822 istk->expansion = ll;
4824 } else {
4826 * Check whether a `%rep' was started and not ended
4827 * within this macro expansion. This can happen and
4828 * should be detected. It's a fatal error because
4829 * I'm too confused to work out how to recover
4830 * sensibly from it.
4832 if (defining) {
4833 if (defining->name)
4834 error(ERR_PANIC,
4835 "defining with name in expansion");
4836 else if (istk->mstk->name)
4837 error(ERR_FATAL,
4838 "`%%rep' without `%%endrep' within"
4839 " expansion of macro `%s'",
4840 istk->mstk->name);
4844 * FIXME: investigate the relationship at this point between
4845 * istk->mstk and l->finishes
4848 MMacro *m = istk->mstk;
4849 istk->mstk = m->next_active;
4850 if (m->name) {
4852 * This was a real macro call, not a %rep, and
4853 * therefore the parameter information needs to
4854 * be freed.
4856 if (m->prev) {
4857 pop_mmacro(m);
4858 l->finishes->in_progress --;
4859 } else {
4860 nasm_free(m->params);
4861 free_tlist(m->iline);
4862 nasm_free(m->paramlen);
4863 l->finishes->in_progress = 0;
4865 } else
4866 free_mmacro(m);
4868 istk->expansion = l->next;
4869 nasm_free(l);
4870 list->downlevel(LIST_MACRO);
4873 while (1) { /* until we get a line we can use */
4875 if (istk->expansion) { /* from a macro expansion */
4876 char *p;
4877 Line *l = istk->expansion;
4878 if (istk->mstk)
4879 istk->mstk->lineno++;
4880 tline = l->first;
4881 istk->expansion = l->next;
4882 nasm_free(l);
4883 p = detoken(tline, false);
4884 list->line(LIST_MACRO, p);
4885 nasm_free(p);
4886 break;
4888 line = read_line();
4889 if (line) { /* from the current input file */
4890 line = prepreproc(line);
4891 tline = tokenize(line);
4892 nasm_free(line);
4893 break;
4896 * The current file has ended; work down the istk
4899 Include *i = istk;
4900 fclose(i->fp);
4901 if (i->conds)
4902 error(ERR_FATAL,
4903 "expected `%%endif' before end of file");
4904 /* only set line and file name if there's a next node */
4905 if (i->next) {
4906 src_set_linnum(i->lineno);
4907 nasm_free(src_set_fname(i->fname));
4909 istk = i->next;
4910 list->downlevel(LIST_INCLUDE);
4911 nasm_free(i);
4912 if (!istk)
4913 return NULL;
4914 if (istk->expansion && istk->expansion->finishes)
4915 break;
4920 * We must expand MMacro parameters and MMacro-local labels
4921 * _before_ we plunge into directive processing, to cope
4922 * with things like `%define something %1' such as STRUC
4923 * uses. Unless we're _defining_ a MMacro, in which case
4924 * those tokens should be left alone to go into the
4925 * definition; and unless we're in a non-emitting
4926 * condition, in which case we don't want to meddle with
4927 * anything.
4929 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4930 && !(istk->mstk && !istk->mstk->in_progress)) {
4931 tline = expand_mmac_params(tline);
4935 * Check the line to see if it's a preprocessor directive.
4937 if (do_directive(tline) == DIRECTIVE_FOUND) {
4938 continue;
4939 } else if (defining) {
4941 * We're defining a multi-line macro. We emit nothing
4942 * at all, and just
4943 * shove the tokenized line on to the macro definition.
4945 Line *l = nasm_malloc(sizeof(Line));
4946 l->next = defining->expansion;
4947 l->first = tline;
4948 l->finishes = NULL;
4949 defining->expansion = l;
4950 continue;
4951 } else if (istk->conds && !emitting(istk->conds->state)) {
4953 * We're in a non-emitting branch of a condition block.
4954 * Emit nothing at all, not even a blank line: when we
4955 * emerge from the condition we'll give a line-number
4956 * directive so we keep our place correctly.
4958 free_tlist(tline);
4959 continue;
4960 } else if (istk->mstk && !istk->mstk->in_progress) {
4962 * We're in a %rep block which has been terminated, so
4963 * we're walking through to the %endrep without
4964 * emitting anything. Emit nothing at all, not even a
4965 * blank line: when we emerge from the %rep block we'll
4966 * give a line-number directive so we keep our place
4967 * correctly.
4969 free_tlist(tline);
4970 continue;
4971 } else {
4972 tline = expand_smacro(tline);
4973 if (!expand_mmacro(tline)) {
4975 * De-tokenize the line again, and emit it.
4977 line = detoken(tline, true);
4978 free_tlist(tline);
4979 break;
4980 } else {
4981 continue; /* expand_mmacro calls free_tlist */
4986 return line;
4989 static void pp_cleanup(int pass)
4991 if (defining) {
4992 if (defining->name) {
4993 error(ERR_NONFATAL,
4994 "end of file while still defining macro `%s'",
4995 defining->name);
4996 } else {
4997 error(ERR_NONFATAL, "end of file while still in %%rep");
5000 free_mmacro(defining);
5001 defining = NULL;
5003 while (cstk)
5004 ctx_pop();
5005 free_macros();
5006 while (istk) {
5007 Include *i = istk;
5008 istk = istk->next;
5009 fclose(i->fp);
5010 nasm_free(i->fname);
5011 nasm_free(i);
5013 while (cstk)
5014 ctx_pop();
5015 nasm_free(src_set_fname(NULL));
5016 if (pass == 0) {
5017 IncPath *i;
5018 free_llist(predef);
5019 delete_Blocks();
5020 while ((i = ipath)) {
5021 ipath = i->next;
5022 if (i->path)
5023 nasm_free(i->path);
5024 nasm_free(i);
5029 void pp_include_path(char *path)
5031 IncPath *i;
5033 i = nasm_malloc(sizeof(IncPath));
5034 i->path = path ? nasm_strdup(path) : NULL;
5035 i->next = NULL;
5037 if (ipath) {
5038 IncPath *j = ipath;
5039 while (j->next)
5040 j = j->next;
5041 j->next = i;
5042 } else {
5043 ipath = i;
5047 void pp_pre_include(char *fname)
5049 Token *inc, *space, *name;
5050 Line *l;
5052 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5053 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5054 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5056 l = nasm_malloc(sizeof(Line));
5057 l->next = predef;
5058 l->first = inc;
5059 l->finishes = NULL;
5060 predef = l;
5063 void pp_pre_define(char *definition)
5065 Token *def, *space;
5066 Line *l;
5067 char *equals;
5069 equals = strchr(definition, '=');
5070 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5071 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5072 if (equals)
5073 *equals = ' ';
5074 space->next = tokenize(definition);
5075 if (equals)
5076 *equals = '=';
5078 l = nasm_malloc(sizeof(Line));
5079 l->next = predef;
5080 l->first = def;
5081 l->finishes = NULL;
5082 predef = l;
5085 void pp_pre_undefine(char *definition)
5087 Token *def, *space;
5088 Line *l;
5090 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5091 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5092 space->next = tokenize(definition);
5094 l = nasm_malloc(sizeof(Line));
5095 l->next = predef;
5096 l->first = def;
5097 l->finishes = NULL;
5098 predef = l;
5102 * Added by Keith Kanios:
5104 * This function is used to assist with "runtime" preprocessor
5105 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5107 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5108 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5111 void pp_runtime(char *definition)
5113 Token *def;
5115 def = tokenize(definition);
5116 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5117 free_tlist(def);
5121 void pp_extra_stdmac(macros_t *macros)
5123 extrastdmac = macros;
5126 static void make_tok_num(Token * tok, int64_t val)
5128 char numbuf[20];
5129 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5130 tok->text = nasm_strdup(numbuf);
5131 tok->type = TOK_NUMBER;
5134 Preproc nasmpp = {
5135 pp_reset,
5136 pp_getline,
5137 pp_cleanup