NASM 2.10rc1
[nasm.git] / preproc.c
blobeb7902a8a8850befde2340125c210920c2f00a33
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 #define PP_CONCAT_MASK(x) (1 << (x))
217 struct Token {
218 Token *next;
219 char *text;
220 union {
221 SMacro *mac; /* associated macro for TOK_SMAC_END */
222 size_t len; /* scratch length field */
223 } a; /* Auxiliary data */
224 enum pp_token_type type;
228 * Multi-line macro definitions are stored as a linked list of
229 * these, which is essentially a container to allow several linked
230 * lists of Tokens.
232 * Note that in this module, linked lists are treated as stacks
233 * wherever possible. For this reason, Lines are _pushed_ on to the
234 * `expansion' field in MMacro structures, so that the linked list,
235 * if walked, would give the macro lines in reverse order; this
236 * means that we can walk the list when expanding a macro, and thus
237 * push the lines on to the `expansion' field in _istk_ in reverse
238 * order (so that when popped back off they are in the right
239 * order). It may seem cockeyed, and it relies on my design having
240 * an even number of steps in, but it works...
242 * Some of these structures, rather than being actual lines, are
243 * markers delimiting the end of the expansion of a given macro.
244 * This is for use in the cycle-tracking and %rep-handling code.
245 * Such structures have `finishes' non-NULL, and `first' NULL. All
246 * others have `finishes' NULL, but `first' may still be NULL if
247 * the line is blank.
249 struct Line {
250 Line *next;
251 MMacro *finishes;
252 Token *first;
256 * To handle an arbitrary level of file inclusion, we maintain a
257 * stack (ie linked list) of these things.
259 struct Include {
260 Include *next;
261 FILE *fp;
262 Cond *conds;
263 Line *expansion;
264 char *fname;
265 int lineno, lineinc;
266 MMacro *mstk; /* stack of active macros/reps */
270 * Include search path. This is simply a list of strings which get
271 * prepended, in turn, to the name of an include file, in an
272 * attempt to find the file if it's not in the current directory.
274 struct IncPath {
275 IncPath *next;
276 char *path;
280 * Conditional assembly: we maintain a separate stack of these for
281 * each level of file inclusion. (The only reason we keep the
282 * stacks separate is to ensure that a stray `%endif' in a file
283 * included from within the true branch of a `%if' won't terminate
284 * it and cause confusion: instead, rightly, it'll cause an error.)
286 struct Cond {
287 Cond *next;
288 int state;
290 enum {
292 * These states are for use just after %if or %elif: IF_TRUE
293 * means the condition has evaluated to truth so we are
294 * currently emitting, whereas IF_FALSE means we are not
295 * currently emitting but will start doing so if a %else comes
296 * up. In these states, all directives are admissible: %elif,
297 * %else and %endif. (And of course %if.)
299 COND_IF_TRUE, COND_IF_FALSE,
301 * These states come up after a %else: ELSE_TRUE means we're
302 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
303 * any %elif or %else will cause an error.
305 COND_ELSE_TRUE, COND_ELSE_FALSE,
307 * These states mean that we're not emitting now, and also that
308 * nothing until %endif will be emitted at all. COND_DONE is
309 * used when we've had our moment of emission
310 * and have now started seeing %elifs. COND_NEVER is used when
311 * the condition construct in question is contained within a
312 * non-emitting branch of a larger condition construct,
313 * or if there is an error.
315 COND_DONE, COND_NEVER
317 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
320 * These defines are used as the possible return values for do_directive
322 #define NO_DIRECTIVE_FOUND 0
323 #define DIRECTIVE_FOUND 1
326 * This define sets the upper limit for smacro and recursive mmacro
327 * expansions
329 #define DEADMAN_LIMIT (1 << 20)
331 /* max reps */
332 #define REP_LIMIT ((INT64_C(1) << 62))
335 * Condition codes. Note that we use c_ prefix not C_ because C_ is
336 * used in nasm.h for the "real" condition codes. At _this_ level,
337 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
338 * ones, so we need a different enum...
340 static const char * const conditions[] = {
341 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
342 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
343 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
345 enum pp_conds {
346 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
347 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
348 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
349 c_none = -1
351 static const enum pp_conds inverse_ccs[] = {
352 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
353 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,
354 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
358 * Directive names.
360 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
361 static int is_condition(enum preproc_token arg)
363 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
366 /* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
372 enum {
373 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
374 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
377 static const char * const tasm_directives[] = {
378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
382 static int StackSize = 4;
383 static char *StackPointer = "ebp";
384 static int ArgOffset = 8;
385 static int LocalOffset = 0;
387 static Context *cstk;
388 static Include *istk;
389 static IncPath *ipath = NULL;
391 static int pass; /* HACK: pass 0 = generate dependencies only */
392 static StrList **dephead, **deptail; /* Dependency list */
394 static uint64_t unique; /* unique identifier numbers */
396 static Line *predef = NULL;
397 static bool do_predef;
399 static ListGen *list;
402 * The current set of multi-line macros we have defined.
404 static struct hash_table mmacros;
407 * The current set of single-line macros we have defined.
409 static struct hash_table smacros;
412 * The multi-line macro we are currently defining, or the %rep
413 * block we are currently reading, if any.
415 static MMacro *defining;
417 static uint64_t nested_mac_count;
418 static uint64_t nested_rep_count;
421 * The number of macro parameters to allocate space for at a time.
423 #define PARAM_DELTA 16
426 * The standard macro set: defined in macros.c in the array nasm_stdmac.
427 * This gives our position in the macro set, when we're processing it.
429 static macros_t *stdmacpos;
432 * The extra standard macros that come from the object format, if
433 * any.
435 static macros_t *extrastdmac = NULL;
436 static bool any_extrastdmac;
439 * Tokens are allocated in blocks to improve speed
441 #define TOKEN_BLOCKSIZE 4096
442 static Token *freeTokens = NULL;
443 struct Blocks {
444 Blocks *next;
445 void *chunk;
448 static Blocks blocks = { NULL, NULL };
451 * Forward declarations.
453 static Token *expand_mmac_params(Token * tline);
454 static Token *expand_smacro(Token * tline);
455 static Token *expand_id(Token * tline);
456 static Context *get_ctx(const char *name, const char **namep,
457 bool all_contexts);
458 static void make_tok_num(Token * tok, int64_t val);
459 static void error(int severity, const char *fmt, ...);
460 static void error_precond(int severity, const char *fmt, ...);
461 static void *new_Block(size_t size);
462 static void delete_Blocks(void);
463 static Token *new_Token(Token * next, enum pp_token_type type,
464 const char *text, int txtlen);
465 static Token *delete_Token(Token * t);
468 * Macros for safe checking of token pointers, avoid *(NULL)
470 #define tok_type_(x,t) ((x) && (x)->type == (t))
471 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
472 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
473 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
476 * nasm_unquote with error if the string contains NUL characters.
477 * If the string contains NUL characters, issue an error and return
478 * the C len, i.e. truncate at the NUL.
480 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
482 size_t len = nasm_unquote(qstr, NULL);
483 size_t clen = strlen(qstr);
485 if (len != clen)
486 error(ERR_NONFATAL, "NUL character in `%s' directive",
487 pp_directives[directive]);
489 return clen;
493 * In-place reverse a list of tokens.
495 static Token *reverse_tokens(Token *t)
497 Token *prev = NULL;
498 Token *next;
500 while (t) {
501 next = t->next;
502 t->next = prev;
503 prev = t;
504 t = next;
507 return prev;
511 * Handle TASM specific directives, which do not contain a % in
512 * front of them. We do it here because I could not find any other
513 * place to do it for the moment, and it is a hack (ideally it would
514 * be nice to be able to use the NASM pre-processor to do it).
516 static char *check_tasm_directive(char *line)
518 int32_t i, j, k, m, len;
519 char *p, *q, *oldline, oldchar;
521 p = nasm_skip_spaces(line);
523 /* Binary search for the directive name */
524 i = -1;
525 j = ARRAY_SIZE(tasm_directives);
526 q = nasm_skip_word(p);
527 len = q - p;
528 if (len) {
529 oldchar = p[len];
530 p[len] = 0;
531 while (j - i > 1) {
532 k = (j + i) / 2;
533 m = nasm_stricmp(p, tasm_directives[k]);
534 if (m == 0) {
535 /* We have found a directive, so jam a % in front of it
536 * so that NASM will then recognise it as one if it's own.
538 p[len] = oldchar;
539 len = strlen(p);
540 oldline = line;
541 line = nasm_malloc(len + 2);
542 line[0] = '%';
543 if (k == TM_IFDIFI) {
545 * NASM does not recognise IFDIFI, so we convert
546 * it to %if 0. This is not used in NASM
547 * compatible code, but does need to parse for the
548 * TASM macro package.
550 strcpy(line + 1, "if 0");
551 } else {
552 memcpy(line + 1, p, len + 1);
554 nasm_free(oldline);
555 return line;
556 } else if (m < 0) {
557 j = k;
558 } else
559 i = k;
561 p[len] = oldchar;
563 return line;
567 * The pre-preprocessing stage... This function translates line
568 * number indications as they emerge from GNU cpp (`# lineno "file"
569 * flags') into NASM preprocessor line number indications (`%line
570 * lineno file').
572 static char *prepreproc(char *line)
574 int lineno, fnlen;
575 char *fname, *oldline;
577 if (line[0] == '#' && line[1] == ' ') {
578 oldline = line;
579 fname = oldline + 2;
580 lineno = atoi(fname);
581 fname += strspn(fname, "0123456789 ");
582 if (*fname == '"')
583 fname++;
584 fnlen = strcspn(fname, "\"");
585 line = nasm_malloc(20 + fnlen);
586 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
587 nasm_free(oldline);
589 if (tasm_compatible_mode)
590 return check_tasm_directive(line);
591 return line;
595 * Free a linked list of tokens.
597 static void free_tlist(Token * list)
599 while (list)
600 list = delete_Token(list);
604 * Free a linked list of lines.
606 static void free_llist(Line * list)
608 Line *l, *tmp;
609 list_for_each_safe(l, tmp, list) {
610 free_tlist(l->first);
611 nasm_free(l);
616 * Free an MMacro
618 static void free_mmacro(MMacro * m)
620 nasm_free(m->name);
621 free_tlist(m->dlist);
622 nasm_free(m->defaults);
623 free_llist(m->expansion);
624 nasm_free(m);
628 * Free all currently defined macros, and free the hash tables
630 static void free_smacro_table(struct hash_table *smt)
632 SMacro *s, *tmp;
633 const char *key;
634 struct hash_tbl_node *it = NULL;
636 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
637 nasm_free((void *)key);
638 list_for_each_safe(s, tmp, s) {
639 nasm_free(s->name);
640 free_tlist(s->expansion);
641 nasm_free(s);
644 hash_free(smt);
647 static void free_mmacro_table(struct hash_table *mmt)
649 MMacro *m, *tmp;
650 const char *key;
651 struct hash_tbl_node *it = NULL;
653 it = NULL;
654 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
655 nasm_free((void *)key);
656 list_for_each_safe(m ,tmp, m)
657 free_mmacro(m);
659 hash_free(mmt);
662 static void free_macros(void)
664 free_smacro_table(&smacros);
665 free_mmacro_table(&mmacros);
669 * Initialize the hash tables
671 static void init_macros(void)
673 hash_init(&smacros, HASH_LARGE);
674 hash_init(&mmacros, HASH_LARGE);
678 * Pop the context stack.
680 static void ctx_pop(void)
682 Context *c = cstk;
684 cstk = cstk->next;
685 free_smacro_table(&c->localmac);
686 nasm_free(c->name);
687 nasm_free(c);
691 * Search for a key in the hash index; adding it if necessary
692 * (in which case we initialize the data pointer to NULL.)
694 static void **
695 hash_findi_add(struct hash_table *hash, const char *str)
697 struct hash_insert hi;
698 void **r;
699 char *strx;
701 r = hash_findi(hash, str, &hi);
702 if (r)
703 return r;
705 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
706 return hash_add(&hi, strx, NULL);
710 * Like hash_findi, but returns the data element rather than a pointer
711 * to it. Used only when not adding a new element, hence no third
712 * argument.
714 static void *
715 hash_findix(struct hash_table *hash, const char *str)
717 void **p;
719 p = hash_findi(hash, str, NULL);
720 return p ? *p : NULL;
724 * read line from standart macros set,
725 * if there no more left -- return NULL
727 static char *line_from_stdmac(void)
729 unsigned char c;
730 const unsigned char *p = stdmacpos;
731 char *line, *q;
732 size_t len = 0;
734 if (!stdmacpos)
735 return NULL;
737 while ((c = *p++)) {
738 if (c >= 0x80)
739 len += pp_directives_len[c - 0x80] + 1;
740 else
741 len++;
744 line = nasm_malloc(len + 1);
745 q = line;
746 while ((c = *stdmacpos++)) {
747 if (c >= 0x80) {
748 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
749 q += pp_directives_len[c - 0x80];
750 *q++ = ' ';
751 } else {
752 *q++ = c;
755 stdmacpos = p;
756 *q = '\0';
758 if (!*stdmacpos) {
759 /* This was the last of the standard macro chain... */
760 stdmacpos = NULL;
761 if (any_extrastdmac) {
762 stdmacpos = extrastdmac;
763 any_extrastdmac = false;
764 } else if (do_predef) {
765 Line *pd, *l;
766 Token *head, **tail, *t;
769 * Nasty hack: here we push the contents of
770 * `predef' on to the top-level expansion stack,
771 * since this is the most convenient way to
772 * implement the pre-include and pre-define
773 * features.
775 list_for_each(pd, predef) {
776 head = NULL;
777 tail = &head;
778 list_for_each(t, pd->first) {
779 *tail = new_Token(NULL, t->type, t->text, 0);
780 tail = &(*tail)->next;
783 l = nasm_malloc(sizeof(Line));
784 l->next = istk->expansion;
785 l->first = head;
786 l->finishes = NULL;
788 istk->expansion = l;
790 do_predef = false;
794 return line;
797 #define BUF_DELTA 512
799 * Read a line from the top file in istk, handling multiple CR/LFs
800 * at the end of the line read, and handling spurious ^Zs. Will
801 * return lines from the standard macro set if this has not already
802 * been done.
804 static char *read_line(void)
806 char *buffer, *p, *q;
807 int bufsize, continued_count;
810 * standart macros set (predefined) goes first
812 p = line_from_stdmac();
813 if (p)
814 return p;
817 * regular read from a file
819 bufsize = BUF_DELTA;
820 buffer = nasm_malloc(BUF_DELTA);
821 p = buffer;
822 continued_count = 0;
823 while (1) {
824 q = fgets(p, bufsize - (p - buffer), istk->fp);
825 if (!q)
826 break;
827 p += strlen(p);
828 if (p > buffer && p[-1] == '\n') {
830 * Convert backslash-CRLF line continuation sequences into
831 * nothing at all (for DOS and Windows)
833 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
834 p -= 3;
835 *p = 0;
836 continued_count++;
839 * Also convert backslash-LF line continuation sequences into
840 * nothing at all (for Unix)
842 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
843 p -= 2;
844 *p = 0;
845 continued_count++;
846 } else {
847 break;
850 if (p - buffer > bufsize - 10) {
851 int32_t offset = p - buffer;
852 bufsize += BUF_DELTA;
853 buffer = nasm_realloc(buffer, bufsize);
854 p = buffer + offset; /* prevent stale-pointer problems */
858 if (!q && p == buffer) {
859 nasm_free(buffer);
860 return NULL;
863 src_set_linnum(src_get_linnum() + istk->lineinc +
864 (continued_count * istk->lineinc));
867 * Play safe: remove CRs as well as LFs, if any of either are
868 * present at the end of the line.
870 while (--p >= buffer && (*p == '\n' || *p == '\r'))
871 *p = '\0';
874 * Handle spurious ^Z, which may be inserted into source files
875 * by some file transfer utilities.
877 buffer[strcspn(buffer, "\032")] = '\0';
879 list->line(LIST_READ, buffer);
881 return buffer;
885 * Tokenize a line of text. This is a very simple process since we
886 * don't need to parse the value out of e.g. numeric tokens: we
887 * simply split one string into many.
889 static Token *tokenize(char *line)
891 char c, *p = line;
892 enum pp_token_type type;
893 Token *list = NULL;
894 Token *t, **tail = &list;
896 while (*line) {
897 p = line;
898 if (*p == '%') {
899 p++;
900 if (*p == '+' && !nasm_isdigit(p[1])) {
901 p++;
902 type = TOK_PASTE;
903 } else if (nasm_isdigit(*p) ||
904 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
905 do {
906 p++;
908 while (nasm_isdigit(*p));
909 type = TOK_PREPROC_ID;
910 } else if (*p == '{') {
911 p++;
912 while (*p && *p != '}') {
913 p[-1] = *p;
914 p++;
916 p[-1] = '\0';
917 if (*p)
918 p++;
919 type = TOK_PREPROC_ID;
920 } else if (*p == '[') {
921 int lvl = 1;
922 line += 2; /* Skip the leading %[ */
923 p++;
924 while (lvl && (c = *p++)) {
925 switch (c) {
926 case ']':
927 lvl--;
928 break;
929 case '%':
930 if (*p == '[')
931 lvl++;
932 break;
933 case '\'':
934 case '\"':
935 case '`':
936 p = nasm_skip_string(p - 1) + 1;
937 break;
938 default:
939 break;
942 p--;
943 if (*p)
944 *p++ = '\0';
945 if (lvl)
946 error(ERR_NONFATAL, "unterminated %[ construct");
947 type = TOK_INDIRECT;
948 } else if (*p == '?') {
949 type = TOK_PREPROC_Q; /* %? */
950 p++;
951 if (*p == '?') {
952 type = TOK_PREPROC_QQ; /* %?? */
953 p++;
955 } else if (*p == '!') {
956 type = TOK_PREPROC_ID;
957 p++;
958 if (isidchar(*p)) {
959 do {
960 p++;
961 } while (isidchar(*p));
962 } else if (*p == '\'' || *p == '\"' || *p == '`') {
963 p = nasm_skip_string(p);
964 if (*p)
965 p++;
966 else
967 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
968 } else {
969 /* %! without string or identifier */
970 type = TOK_OTHER; /* Legacy behavior... */
972 } else if (isidchar(*p) ||
973 ((*p == '!' || *p == '%' || *p == '$') &&
974 isidchar(p[1]))) {
975 do {
976 p++;
978 while (isidchar(*p));
979 type = TOK_PREPROC_ID;
980 } else {
981 type = TOK_OTHER;
982 if (*p == '%')
983 p++;
985 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
986 type = TOK_ID;
987 p++;
988 while (*p && isidchar(*p))
989 p++;
990 } else if (*p == '\'' || *p == '"' || *p == '`') {
992 * A string token.
994 type = TOK_STRING;
995 p = nasm_skip_string(p);
997 if (*p) {
998 p++;
999 } else {
1000 error(ERR_WARNING|ERR_PASS1, "unterminated string");
1001 /* Handling unterminated strings by UNV */
1002 /* type = -1; */
1004 } else if (p[0] == '$' && p[1] == '$') {
1005 type = TOK_OTHER; /* TOKEN_BASE */
1006 p += 2;
1007 } else if (isnumstart(*p)) {
1008 bool is_hex = false;
1009 bool is_float = false;
1010 bool has_e = false;
1011 char c, *r;
1014 * A numeric token.
1017 if (*p == '$') {
1018 p++;
1019 is_hex = true;
1022 for (;;) {
1023 c = *p++;
1025 if (!is_hex && (c == 'e' || c == 'E')) {
1026 has_e = true;
1027 if (*p == '+' || *p == '-') {
1029 * e can only be followed by +/- if it is either a
1030 * prefixed hex number or a floating-point number
1032 p++;
1033 is_float = true;
1035 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1036 is_hex = true;
1037 } else if (c == 'P' || c == 'p') {
1038 is_float = true;
1039 if (*p == '+' || *p == '-')
1040 p++;
1041 } else if (isnumchar(c) || c == '_')
1042 ; /* just advance */
1043 else if (c == '.') {
1045 * we need to deal with consequences of the legacy
1046 * parser, like "1.nolist" being two tokens
1047 * (TOK_NUMBER, TOK_ID) here; at least give it
1048 * a shot for now. In the future, we probably need
1049 * a flex-based scanner with proper pattern matching
1050 * to do it as well as it can be done. Nothing in
1051 * the world is going to help the person who wants
1052 * 0x123.p16 interpreted as two tokens, though.
1054 r = p;
1055 while (*r == '_')
1056 r++;
1058 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1059 (!is_hex && (*r == 'e' || *r == 'E')) ||
1060 (*r == 'p' || *r == 'P')) {
1061 p = r;
1062 is_float = true;
1063 } else
1064 break; /* Terminate the token */
1065 } else
1066 break;
1068 p--; /* Point to first character beyond number */
1070 if (p == line+1 && *line == '$') {
1071 type = TOK_OTHER; /* TOKEN_HERE */
1072 } else {
1073 if (has_e && !is_hex) {
1074 /* 1e13 is floating-point, but 1e13h is not */
1075 is_float = true;
1078 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1080 } else if (nasm_isspace(*p)) {
1081 type = TOK_WHITESPACE;
1082 p = nasm_skip_spaces(p);
1084 * Whitespace just before end-of-line is discarded by
1085 * pretending it's a comment; whitespace just before a
1086 * comment gets lumped into the comment.
1088 if (!*p || *p == ';') {
1089 type = TOK_COMMENT;
1090 while (*p)
1091 p++;
1093 } else if (*p == ';') {
1094 type = TOK_COMMENT;
1095 while (*p)
1096 p++;
1097 } else {
1099 * Anything else is an operator of some kind. We check
1100 * for all the double-character operators (>>, <<, //,
1101 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1102 * else is a single-character operator.
1104 type = TOK_OTHER;
1105 if ((p[0] == '>' && p[1] == '>') ||
1106 (p[0] == '<' && p[1] == '<') ||
1107 (p[0] == '/' && p[1] == '/') ||
1108 (p[0] == '<' && p[1] == '=') ||
1109 (p[0] == '>' && p[1] == '=') ||
1110 (p[0] == '=' && p[1] == '=') ||
1111 (p[0] == '!' && p[1] == '=') ||
1112 (p[0] == '<' && p[1] == '>') ||
1113 (p[0] == '&' && p[1] == '&') ||
1114 (p[0] == '|' && p[1] == '|') ||
1115 (p[0] == '^' && p[1] == '^')) {
1116 p++;
1118 p++;
1121 /* Handling unterminated string by UNV */
1122 /*if (type == -1)
1124 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1125 t->text[p-line] = *line;
1126 tail = &t->next;
1128 else */
1129 if (type != TOK_COMMENT) {
1130 *tail = t = new_Token(NULL, type, line, p - line);
1131 tail = &t->next;
1133 line = p;
1135 return list;
1139 * this function allocates a new managed block of memory and
1140 * returns a pointer to the block. The managed blocks are
1141 * deleted only all at once by the delete_Blocks function.
1143 static void *new_Block(size_t size)
1145 Blocks *b = &blocks;
1147 /* first, get to the end of the linked list */
1148 while (b->next)
1149 b = b->next;
1150 /* now allocate the requested chunk */
1151 b->chunk = nasm_malloc(size);
1153 /* now allocate a new block for the next request */
1154 b->next = nasm_malloc(sizeof(Blocks));
1155 /* and initialize the contents of the new block */
1156 b->next->next = NULL;
1157 b->next->chunk = NULL;
1158 return b->chunk;
1162 * this function deletes all managed blocks of memory
1164 static void delete_Blocks(void)
1166 Blocks *a, *b = &blocks;
1169 * keep in mind that the first block, pointed to by blocks
1170 * is a static and not dynamically allocated, so we don't
1171 * free it.
1173 while (b) {
1174 if (b->chunk)
1175 nasm_free(b->chunk);
1176 a = b;
1177 b = b->next;
1178 if (a != &blocks)
1179 nasm_free(a);
1184 * this function creates a new Token and passes a pointer to it
1185 * back to the caller. It sets the type and text elements, and
1186 * also the a.mac and next elements to NULL.
1188 static Token *new_Token(Token * next, enum pp_token_type type,
1189 const char *text, int txtlen)
1191 Token *t;
1192 int i;
1194 if (!freeTokens) {
1195 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1196 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1197 freeTokens[i].next = &freeTokens[i + 1];
1198 freeTokens[i].next = NULL;
1200 t = freeTokens;
1201 freeTokens = t->next;
1202 t->next = next;
1203 t->a.mac = NULL;
1204 t->type = type;
1205 if (type == TOK_WHITESPACE || !text) {
1206 t->text = NULL;
1207 } else {
1208 if (txtlen == 0)
1209 txtlen = strlen(text);
1210 t->text = nasm_malloc(txtlen+1);
1211 memcpy(t->text, text, txtlen);
1212 t->text[txtlen] = '\0';
1214 return t;
1217 static Token *delete_Token(Token * t)
1219 Token *next = t->next;
1220 nasm_free(t->text);
1221 t->next = freeTokens;
1222 freeTokens = t;
1223 return next;
1227 * Convert a line of tokens back into text.
1228 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1229 * will be transformed into ..@ctxnum.xxx
1231 static char *detoken(Token * tlist, bool expand_locals)
1233 Token *t;
1234 char *line, *p;
1235 const char *q;
1236 int len = 0;
1238 list_for_each(t, tlist) {
1239 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1240 char *v;
1241 char *q = t->text;
1243 v = t->text + 2;
1244 if (*v == '\'' || *v == '\"' || *v == '`') {
1245 size_t len = nasm_unquote(v, NULL);
1246 size_t clen = strlen(v);
1248 if (len != clen) {
1249 error(ERR_NONFATAL | ERR_PASS1,
1250 "NUL character in %! string");
1251 v = NULL;
1255 if (v) {
1256 char *p = getenv(v);
1257 if (!p) {
1258 error(ERR_NONFATAL | ERR_PASS1,
1259 "nonexistent environment variable `%s'", v);
1260 p = "";
1262 t->text = nasm_strdup(p);
1264 nasm_free(q);
1267 /* Expand local macros here and not during preprocessing */
1268 if (expand_locals &&
1269 t->type == TOK_PREPROC_ID && t->text &&
1270 t->text[0] == '%' && t->text[1] == '$') {
1271 const char *q;
1272 char *p;
1273 Context *ctx = get_ctx(t->text, &q, false);
1274 if (ctx) {
1275 char buffer[40];
1276 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1277 p = nasm_strcat(buffer, q);
1278 nasm_free(t->text);
1279 t->text = p;
1282 if (t->type == TOK_WHITESPACE)
1283 len++;
1284 else if (t->text)
1285 len += strlen(t->text);
1288 p = line = nasm_malloc(len + 1);
1290 list_for_each(t, tlist) {
1291 if (t->type == TOK_WHITESPACE) {
1292 *p++ = ' ';
1293 } else if (t->text) {
1294 q = t->text;
1295 while (*q)
1296 *p++ = *q++;
1299 *p = '\0';
1301 return line;
1305 * A scanner, suitable for use by the expression evaluator, which
1306 * operates on a line of Tokens. Expects a pointer to a pointer to
1307 * the first token in the line to be passed in as its private_data
1308 * field.
1310 * FIX: This really needs to be unified with stdscan.
1312 static int ppscan(void *private_data, struct tokenval *tokval)
1314 Token **tlineptr = private_data;
1315 Token *tline;
1316 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1318 do {
1319 tline = *tlineptr;
1320 *tlineptr = tline ? tline->next : NULL;
1321 } while (tline && (tline->type == TOK_WHITESPACE ||
1322 tline->type == TOK_COMMENT));
1324 if (!tline)
1325 return tokval->t_type = TOKEN_EOS;
1327 tokval->t_charptr = tline->text;
1329 if (tline->text[0] == '$' && !tline->text[1])
1330 return tokval->t_type = TOKEN_HERE;
1331 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1332 return tokval->t_type = TOKEN_BASE;
1334 if (tline->type == TOK_ID) {
1335 p = tokval->t_charptr = tline->text;
1336 if (p[0] == '$') {
1337 tokval->t_charptr++;
1338 return tokval->t_type = TOKEN_ID;
1341 for (r = p, s = ourcopy; *r; r++) {
1342 if (r >= p+MAX_KEYWORD)
1343 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1344 *s++ = nasm_tolower(*r);
1346 *s = '\0';
1347 /* right, so we have an identifier sitting in temp storage. now,
1348 * is it actually a register or instruction name, or what? */
1349 return nasm_token_hash(ourcopy, tokval);
1352 if (tline->type == TOK_NUMBER) {
1353 bool rn_error;
1354 tokval->t_integer = readnum(tline->text, &rn_error);
1355 tokval->t_charptr = tline->text;
1356 if (rn_error)
1357 return tokval->t_type = TOKEN_ERRNUM;
1358 else
1359 return tokval->t_type = TOKEN_NUM;
1362 if (tline->type == TOK_FLOAT) {
1363 return tokval->t_type = TOKEN_FLOAT;
1366 if (tline->type == TOK_STRING) {
1367 char bq, *ep;
1369 bq = tline->text[0];
1370 tokval->t_charptr = tline->text;
1371 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1373 if (ep[0] != bq || ep[1] != '\0')
1374 return tokval->t_type = TOKEN_ERRSTR;
1375 else
1376 return tokval->t_type = TOKEN_STR;
1379 if (tline->type == TOK_OTHER) {
1380 if (!strcmp(tline->text, "<<"))
1381 return tokval->t_type = TOKEN_SHL;
1382 if (!strcmp(tline->text, ">>"))
1383 return tokval->t_type = TOKEN_SHR;
1384 if (!strcmp(tline->text, "//"))
1385 return tokval->t_type = TOKEN_SDIV;
1386 if (!strcmp(tline->text, "%%"))
1387 return tokval->t_type = TOKEN_SMOD;
1388 if (!strcmp(tline->text, "=="))
1389 return tokval->t_type = TOKEN_EQ;
1390 if (!strcmp(tline->text, "<>"))
1391 return tokval->t_type = TOKEN_NE;
1392 if (!strcmp(tline->text, "!="))
1393 return tokval->t_type = TOKEN_NE;
1394 if (!strcmp(tline->text, "<="))
1395 return tokval->t_type = TOKEN_LE;
1396 if (!strcmp(tline->text, ">="))
1397 return tokval->t_type = TOKEN_GE;
1398 if (!strcmp(tline->text, "&&"))
1399 return tokval->t_type = TOKEN_DBL_AND;
1400 if (!strcmp(tline->text, "^^"))
1401 return tokval->t_type = TOKEN_DBL_XOR;
1402 if (!strcmp(tline->text, "||"))
1403 return tokval->t_type = TOKEN_DBL_OR;
1407 * We have no other options: just return the first character of
1408 * the token text.
1410 return tokval->t_type = tline->text[0];
1414 * Compare a string to the name of an existing macro; this is a
1415 * simple wrapper which calls either strcmp or nasm_stricmp
1416 * depending on the value of the `casesense' parameter.
1418 static int mstrcmp(const char *p, const char *q, bool casesense)
1420 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1428 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1430 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1434 * Return the Context structure associated with a %$ token. Return
1435 * NULL, having _already_ reported an error condition, if the
1436 * context stack isn't deep enough for the supplied number of $
1437 * signs.
1438 * If all_contexts == true, contexts that enclose current are
1439 * also scanned for such smacro, until it is found; if not -
1440 * only the context that directly results from the number of $'s
1441 * in variable's name.
1443 * If "namep" is non-NULL, set it to the pointer to the macro name
1444 * tail, i.e. the part beyond %$...
1446 static Context *get_ctx(const char *name, const char **namep,
1447 bool all_contexts)
1449 Context *ctx;
1450 SMacro *m;
1451 int i;
1453 if (namep)
1454 *namep = name;
1456 if (!name || name[0] != '%' || name[1] != '$')
1457 return NULL;
1459 if (!cstk) {
1460 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1461 return NULL;
1464 name += 2;
1465 ctx = cstk;
1466 i = 0;
1467 while (ctx && *name == '$') {
1468 name++;
1469 i++;
1470 ctx = ctx->next;
1472 if (!ctx) {
1473 error(ERR_NONFATAL, "`%s': context stack is only"
1474 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1475 return NULL;
1478 if (namep)
1479 *namep = name;
1481 if (!all_contexts)
1482 return ctx;
1485 * NOTE: In 2.10 we will not need lookup in extarnal
1486 * contexts, so this is a gentle way to inform users
1487 * about their source code need to be updated
1490 /* first round -- check the current context */
1491 m = hash_findix(&ctx->localmac, name);
1492 while (m) {
1493 if (!mstrcmp(m->name, name, m->casesense))
1494 return ctx;
1495 m = m->next;
1498 /* second round - external contexts */
1499 while ((ctx = ctx->next)) {
1500 /* Search for this smacro in found context */
1501 m = hash_findix(&ctx->localmac, name);
1502 while (m) {
1503 if (!mstrcmp(m->name, name, m->casesense)) {
1504 /* NOTE: deprecated as of 2.10 */
1505 static int once = 0;
1506 if (!once) {
1507 error(ERR_WARNING, "context-local macro expansion"
1508 " fall-through (automatic searching of outer"
1509 " contexts) will be deprecated starting in"
1510 " NASM 2.10, please see the NASM Manual for"
1511 " more information");
1512 once = 1;
1514 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
1515 return ctx;
1517 m = m->next;
1521 return NULL;
1525 * Check to see if a file is already in a string list
1527 static bool in_list(const StrList *list, const char *str)
1529 while (list) {
1530 if (!strcmp(list->str, str))
1531 return true;
1532 list = list->next;
1534 return false;
1538 * Open an include file. This routine must always return a valid
1539 * file pointer if it returns - it's responsible for throwing an
1540 * ERR_FATAL and bombing out completely if not. It should also try
1541 * the include path one by one until it finds the file or reaches
1542 * the end of the path.
1544 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1545 bool missing_ok)
1547 FILE *fp;
1548 char *prefix = "";
1549 IncPath *ip = ipath;
1550 int len = strlen(file);
1551 size_t prefix_len = 0;
1552 StrList *sl;
1554 while (1) {
1555 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1556 memcpy(sl->str, prefix, prefix_len);
1557 memcpy(sl->str+prefix_len, file, len+1);
1558 fp = fopen(sl->str, "r");
1559 if (fp && dhead && !in_list(*dhead, sl->str)) {
1560 sl->next = NULL;
1561 **dtail = sl;
1562 *dtail = &sl->next;
1563 } else {
1564 nasm_free(sl);
1566 if (fp)
1567 return fp;
1568 if (!ip) {
1569 if (!missing_ok)
1570 break;
1571 prefix = NULL;
1572 } else {
1573 prefix = ip->path;
1574 ip = ip->next;
1576 if (prefix) {
1577 prefix_len = strlen(prefix);
1578 } else {
1579 /* -MG given and file not found */
1580 if (dhead && !in_list(*dhead, file)) {
1581 sl = nasm_malloc(len+1+sizeof sl->next);
1582 sl->next = NULL;
1583 strcpy(sl->str, file);
1584 **dtail = sl;
1585 *dtail = &sl->next;
1587 return NULL;
1591 error(ERR_FATAL, "unable to open include file `%s'", file);
1592 return NULL;
1596 * Determine if we should warn on defining a single-line macro of
1597 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1598 * return true if _any_ single-line macro of that name is defined.
1599 * Otherwise, will return true if a single-line macro with either
1600 * `nparam' or no parameters is defined.
1602 * If a macro with precisely the right number of parameters is
1603 * defined, or nparam is -1, the address of the definition structure
1604 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1605 * is NULL, no action will be taken regarding its contents, and no
1606 * error will occur.
1608 * Note that this is also called with nparam zero to resolve
1609 * `ifdef'.
1611 * If you already know which context macro belongs to, you can pass
1612 * the context pointer as first parameter; if you won't but name begins
1613 * with %$ the context will be automatically computed. If all_contexts
1614 * is true, macro will be searched in outer contexts as well.
1616 static bool
1617 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1618 bool nocase)
1620 struct hash_table *smtbl;
1621 SMacro *m;
1623 if (ctx) {
1624 smtbl = &ctx->localmac;
1625 } else if (name[0] == '%' && name[1] == '$') {
1626 if (cstk)
1627 ctx = get_ctx(name, &name, false);
1628 if (!ctx)
1629 return false; /* got to return _something_ */
1630 smtbl = &ctx->localmac;
1631 } else {
1632 smtbl = &smacros;
1634 m = (SMacro *) hash_findix(smtbl, name);
1636 while (m) {
1637 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1638 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1639 if (defn) {
1640 if (nparam == (int) m->nparam || nparam == -1)
1641 *defn = m;
1642 else
1643 *defn = NULL;
1645 return true;
1647 m = m->next;
1650 return false;
1654 * Count and mark off the parameters in a multi-line macro call.
1655 * This is called both from within the multi-line macro expansion
1656 * code, and also to mark off the default parameters when provided
1657 * in a %macro definition line.
1659 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1661 int paramsize, brace;
1663 *nparam = paramsize = 0;
1664 *params = NULL;
1665 while (t) {
1666 /* +1: we need space for the final NULL */
1667 if (*nparam+1 >= paramsize) {
1668 paramsize += PARAM_DELTA;
1669 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1671 skip_white_(t);
1672 brace = false;
1673 if (tok_is_(t, "{"))
1674 brace = true;
1675 (*params)[(*nparam)++] = t;
1676 while (tok_isnt_(t, brace ? "}" : ","))
1677 t = t->next;
1678 if (t) { /* got a comma/brace */
1679 t = t->next;
1680 if (brace) {
1682 * Now we've found the closing brace, look further
1683 * for the comma.
1685 skip_white_(t);
1686 if (tok_isnt_(t, ",")) {
1687 error(ERR_NONFATAL,
1688 "braces do not enclose all of macro parameter");
1689 while (tok_isnt_(t, ","))
1690 t = t->next;
1692 if (t)
1693 t = t->next; /* eat the comma */
1700 * Determine whether one of the various `if' conditions is true or
1701 * not.
1703 * We must free the tline we get passed.
1705 static bool if_condition(Token * tline, enum preproc_token ct)
1707 enum pp_conditional i = PP_COND(ct);
1708 bool j;
1709 Token *t, *tt, **tptr, *origline;
1710 struct tokenval tokval;
1711 expr *evalresult;
1712 enum pp_token_type needtype;
1713 char *p;
1715 origline = tline;
1717 switch (i) {
1718 case PPC_IFCTX:
1719 j = false; /* have we matched yet? */
1720 while (true) {
1721 skip_white_(tline);
1722 if (!tline)
1723 break;
1724 if (tline->type != TOK_ID) {
1725 error(ERR_NONFATAL,
1726 "`%s' expects context identifiers", pp_directives[ct]);
1727 free_tlist(origline);
1728 return -1;
1730 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1731 j = true;
1732 tline = tline->next;
1734 break;
1736 case PPC_IFDEF:
1737 j = false; /* have we matched yet? */
1738 while (tline) {
1739 skip_white_(tline);
1740 if (!tline || (tline->type != TOK_ID &&
1741 (tline->type != TOK_PREPROC_ID ||
1742 tline->text[1] != '$'))) {
1743 error(ERR_NONFATAL,
1744 "`%s' expects macro identifiers", pp_directives[ct]);
1745 goto fail;
1747 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1748 j = true;
1749 tline = tline->next;
1751 break;
1753 case PPC_IFENV:
1754 tline = expand_smacro(tline);
1755 j = false; /* have we matched yet? */
1756 while (tline) {
1757 skip_white_(tline);
1758 if (!tline || (tline->type != TOK_ID &&
1759 tline->type != TOK_STRING &&
1760 (tline->type != TOK_PREPROC_ID ||
1761 tline->text[1] != '!'))) {
1762 error(ERR_NONFATAL,
1763 "`%s' expects environment variable names",
1764 pp_directives[ct]);
1765 goto fail;
1767 p = tline->text;
1768 if (tline->type == TOK_PREPROC_ID)
1769 p += 2; /* Skip leading %! */
1770 if (*p == '\'' || *p == '\"' || *p == '`')
1771 nasm_unquote_cstr(p, ct);
1772 if (getenv(p))
1773 j = true;
1774 tline = tline->next;
1776 break;
1778 case PPC_IFIDN:
1779 case PPC_IFIDNI:
1780 tline = expand_smacro(tline);
1781 t = tt = tline;
1782 while (tok_isnt_(tt, ","))
1783 tt = tt->next;
1784 if (!tt) {
1785 error(ERR_NONFATAL,
1786 "`%s' expects two comma-separated arguments",
1787 pp_directives[ct]);
1788 goto fail;
1790 tt = tt->next;
1791 j = true; /* assume equality unless proved not */
1792 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1793 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1794 error(ERR_NONFATAL, "`%s': more than one comma on line",
1795 pp_directives[ct]);
1796 goto fail;
1798 if (t->type == TOK_WHITESPACE) {
1799 t = t->next;
1800 continue;
1802 if (tt->type == TOK_WHITESPACE) {
1803 tt = tt->next;
1804 continue;
1806 if (tt->type != t->type) {
1807 j = false; /* found mismatching tokens */
1808 break;
1810 /* When comparing strings, need to unquote them first */
1811 if (t->type == TOK_STRING) {
1812 size_t l1 = nasm_unquote(t->text, NULL);
1813 size_t l2 = nasm_unquote(tt->text, NULL);
1815 if (l1 != l2) {
1816 j = false;
1817 break;
1819 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1820 j = false;
1821 break;
1823 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1824 j = false; /* found mismatching tokens */
1825 break;
1828 t = t->next;
1829 tt = tt->next;
1831 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1832 j = false; /* trailing gunk on one end or other */
1833 break;
1835 case PPC_IFMACRO:
1837 bool found = false;
1838 MMacro searching, *mmac;
1840 skip_white_(tline);
1841 tline = expand_id(tline);
1842 if (!tok_type_(tline, TOK_ID)) {
1843 error(ERR_NONFATAL,
1844 "`%s' expects a macro name", pp_directives[ct]);
1845 goto fail;
1847 searching.name = nasm_strdup(tline->text);
1848 searching.casesense = true;
1849 searching.plus = false;
1850 searching.nolist = false;
1851 searching.in_progress = 0;
1852 searching.max_depth = 0;
1853 searching.rep_nest = NULL;
1854 searching.nparam_min = 0;
1855 searching.nparam_max = INT_MAX;
1856 tline = expand_smacro(tline->next);
1857 skip_white_(tline);
1858 if (!tline) {
1859 } else if (!tok_type_(tline, TOK_NUMBER)) {
1860 error(ERR_NONFATAL,
1861 "`%s' expects a parameter count or nothing",
1862 pp_directives[ct]);
1863 } else {
1864 searching.nparam_min = searching.nparam_max =
1865 readnum(tline->text, &j);
1866 if (j)
1867 error(ERR_NONFATAL,
1868 "unable to parse parameter count `%s'",
1869 tline->text);
1871 if (tline && tok_is_(tline->next, "-")) {
1872 tline = tline->next->next;
1873 if (tok_is_(tline, "*"))
1874 searching.nparam_max = INT_MAX;
1875 else if (!tok_type_(tline, TOK_NUMBER))
1876 error(ERR_NONFATAL,
1877 "`%s' expects a parameter count after `-'",
1878 pp_directives[ct]);
1879 else {
1880 searching.nparam_max = readnum(tline->text, &j);
1881 if (j)
1882 error(ERR_NONFATAL,
1883 "unable to parse parameter count `%s'",
1884 tline->text);
1885 if (searching.nparam_min > searching.nparam_max)
1886 error(ERR_NONFATAL,
1887 "minimum parameter count exceeds maximum");
1890 if (tline && tok_is_(tline->next, "+")) {
1891 tline = tline->next;
1892 searching.plus = true;
1894 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1895 while (mmac) {
1896 if (!strcmp(mmac->name, searching.name) &&
1897 (mmac->nparam_min <= searching.nparam_max
1898 || searching.plus)
1899 && (searching.nparam_min <= mmac->nparam_max
1900 || mmac->plus)) {
1901 found = true;
1902 break;
1904 mmac = mmac->next;
1906 if (tline && tline->next)
1907 error(ERR_WARNING|ERR_PASS1,
1908 "trailing garbage after %%ifmacro ignored");
1909 nasm_free(searching.name);
1910 j = found;
1911 break;
1914 case PPC_IFID:
1915 needtype = TOK_ID;
1916 goto iftype;
1917 case PPC_IFNUM:
1918 needtype = TOK_NUMBER;
1919 goto iftype;
1920 case PPC_IFSTR:
1921 needtype = TOK_STRING;
1922 goto iftype;
1924 iftype:
1925 t = tline = expand_smacro(tline);
1927 while (tok_type_(t, TOK_WHITESPACE) ||
1928 (needtype == TOK_NUMBER &&
1929 tok_type_(t, TOK_OTHER) &&
1930 (t->text[0] == '-' || t->text[0] == '+') &&
1931 !t->text[1]))
1932 t = t->next;
1934 j = tok_type_(t, needtype);
1935 break;
1937 case PPC_IFTOKEN:
1938 t = tline = expand_smacro(tline);
1939 while (tok_type_(t, TOK_WHITESPACE))
1940 t = t->next;
1942 j = false;
1943 if (t) {
1944 t = t->next; /* Skip the actual token */
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
1947 j = !t; /* Should be nothing left */
1949 break;
1951 case PPC_IFEMPTY:
1952 t = tline = expand_smacro(tline);
1953 while (tok_type_(t, TOK_WHITESPACE))
1954 t = t->next;
1956 j = !t; /* Should be empty */
1957 break;
1959 case PPC_IF:
1960 t = tline = expand_smacro(tline);
1961 tptr = &t;
1962 tokval.t_type = TOKEN_INVALID;
1963 evalresult = evaluate(ppscan, tptr, &tokval,
1964 NULL, pass | CRITICAL, error, NULL);
1965 if (!evalresult)
1966 return -1;
1967 if (tokval.t_type)
1968 error(ERR_WARNING|ERR_PASS1,
1969 "trailing garbage after expression ignored");
1970 if (!is_simple(evalresult)) {
1971 error(ERR_NONFATAL,
1972 "non-constant value given to `%s'", pp_directives[ct]);
1973 goto fail;
1975 j = reloc_value(evalresult) != 0;
1976 break;
1978 default:
1979 error(ERR_FATAL,
1980 "preprocessor directive `%s' not yet implemented",
1981 pp_directives[ct]);
1982 goto fail;
1985 free_tlist(origline);
1986 return j ^ PP_NEGATIVE(ct);
1988 fail:
1989 free_tlist(origline);
1990 return -1;
1994 * Common code for defining an smacro
1996 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1997 int nparam, Token *expansion)
1999 SMacro *smac, **smhead;
2000 struct hash_table *smtbl;
2002 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2003 if (!smac) {
2004 error(ERR_WARNING|ERR_PASS1,
2005 "single-line macro `%s' defined both with and"
2006 " without parameters", mname);
2008 * Some instances of the old code considered this a failure,
2009 * some others didn't. What is the right thing to do here?
2011 free_tlist(expansion);
2012 return false; /* Failure */
2013 } else {
2015 * We're redefining, so we have to take over an
2016 * existing SMacro structure. This means freeing
2017 * what was already in it.
2019 nasm_free(smac->name);
2020 free_tlist(smac->expansion);
2022 } else {
2023 smtbl = ctx ? &ctx->localmac : &smacros;
2024 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2025 smac = nasm_malloc(sizeof(SMacro));
2026 smac->next = *smhead;
2027 *smhead = smac;
2029 smac->name = nasm_strdup(mname);
2030 smac->casesense = casesense;
2031 smac->nparam = nparam;
2032 smac->expansion = expansion;
2033 smac->in_progress = false;
2034 return true; /* Success */
2038 * Undefine an smacro
2040 static void undef_smacro(Context *ctx, const char *mname)
2042 SMacro **smhead, *s, **sp;
2043 struct hash_table *smtbl;
2045 smtbl = ctx ? &ctx->localmac : &smacros;
2046 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2048 if (smhead) {
2050 * We now have a macro name... go hunt for it.
2052 sp = smhead;
2053 while ((s = *sp) != NULL) {
2054 if (!mstrcmp(s->name, mname, s->casesense)) {
2055 *sp = s->next;
2056 nasm_free(s->name);
2057 free_tlist(s->expansion);
2058 nasm_free(s);
2059 } else {
2060 sp = &s->next;
2067 * Parse a mmacro specification.
2069 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2071 bool err;
2073 tline = tline->next;
2074 skip_white_(tline);
2075 tline = expand_id(tline);
2076 if (!tok_type_(tline, TOK_ID)) {
2077 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2078 return false;
2081 def->prev = NULL;
2082 def->name = nasm_strdup(tline->text);
2083 def->plus = false;
2084 def->nolist = false;
2085 def->in_progress = 0;
2086 def->rep_nest = NULL;
2087 def->nparam_min = 0;
2088 def->nparam_max = 0;
2090 tline = expand_smacro(tline->next);
2091 skip_white_(tline);
2092 if (!tok_type_(tline, TOK_NUMBER)) {
2093 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2094 } else {
2095 def->nparam_min = def->nparam_max =
2096 readnum(tline->text, &err);
2097 if (err)
2098 error(ERR_NONFATAL,
2099 "unable to parse parameter count `%s'", tline->text);
2101 if (tline && tok_is_(tline->next, "-")) {
2102 tline = tline->next->next;
2103 if (tok_is_(tline, "*")) {
2104 def->nparam_max = INT_MAX;
2105 } else if (!tok_type_(tline, TOK_NUMBER)) {
2106 error(ERR_NONFATAL,
2107 "`%s' expects a parameter count after `-'", directive);
2108 } else {
2109 def->nparam_max = readnum(tline->text, &err);
2110 if (err) {
2111 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2112 tline->text);
2114 if (def->nparam_min > def->nparam_max) {
2115 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2119 if (tline && tok_is_(tline->next, "+")) {
2120 tline = tline->next;
2121 def->plus = true;
2123 if (tline && tok_type_(tline->next, TOK_ID) &&
2124 !nasm_stricmp(tline->next->text, ".nolist")) {
2125 tline = tline->next;
2126 def->nolist = true;
2130 * Handle default parameters.
2132 if (tline && tline->next) {
2133 def->dlist = tline->next;
2134 tline->next = NULL;
2135 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2136 } else {
2137 def->dlist = NULL;
2138 def->defaults = NULL;
2140 def->expansion = NULL;
2142 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2143 !def->plus)
2144 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2145 "too many default macro parameters");
2147 return true;
2152 * Decode a size directive
2154 static int parse_size(const char *str) {
2155 static const char *size_names[] =
2156 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2157 static const int sizes[] =
2158 { 0, 1, 4, 16, 8, 10, 2, 32 };
2160 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2164 * find and process preprocessor directive in passed line
2165 * Find out if a line contains a preprocessor directive, and deal
2166 * with it if so.
2168 * If a directive _is_ found, it is the responsibility of this routine
2169 * (and not the caller) to free_tlist() the line.
2171 * @param tline a pointer to the current tokeninzed line linked list
2172 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2175 static int do_directive(Token * tline)
2177 enum preproc_token i;
2178 int j;
2179 bool err;
2180 int nparam;
2181 bool nolist;
2182 bool casesense;
2183 int k, m;
2184 int offset;
2185 char *p, *pp;
2186 const char *mname;
2187 Include *inc;
2188 Context *ctx;
2189 Cond *cond;
2190 MMacro *mmac, **mmhead;
2191 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2192 Line *l;
2193 struct tokenval tokval;
2194 expr *evalresult;
2195 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2196 int64_t count;
2197 size_t len;
2198 int severity;
2200 origline = tline;
2202 skip_white_(tline);
2203 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2204 (tline->text[1] == '%' || tline->text[1] == '$'
2205 || tline->text[1] == '!'))
2206 return NO_DIRECTIVE_FOUND;
2208 i = pp_token_hash(tline->text);
2211 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2212 * since they are known to be buggy at moment, we need to fix them
2213 * in future release (2.09-2.10)
2215 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2216 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2217 tline->text);
2218 return NO_DIRECTIVE_FOUND;
2222 * If we're in a non-emitting branch of a condition construct,
2223 * or walking to the end of an already terminated %rep block,
2224 * we should ignore all directives except for condition
2225 * directives.
2227 if (((istk->conds && !emitting(istk->conds->state)) ||
2228 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2229 return NO_DIRECTIVE_FOUND;
2233 * If we're defining a macro or reading a %rep block, we should
2234 * ignore all directives except for %macro/%imacro (which nest),
2235 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2236 * If we're in a %rep block, another %rep nests, so should be let through.
2238 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2239 i != PP_RMACRO && i != PP_IRMACRO &&
2240 i != PP_ENDMACRO && i != PP_ENDM &&
2241 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2242 return NO_DIRECTIVE_FOUND;
2245 if (defining) {
2246 if (i == PP_MACRO || i == PP_IMACRO ||
2247 i == PP_RMACRO || i == PP_IRMACRO) {
2248 nested_mac_count++;
2249 return NO_DIRECTIVE_FOUND;
2250 } else if (nested_mac_count > 0) {
2251 if (i == PP_ENDMACRO) {
2252 nested_mac_count--;
2253 return NO_DIRECTIVE_FOUND;
2256 if (!defining->name) {
2257 if (i == PP_REP) {
2258 nested_rep_count++;
2259 return NO_DIRECTIVE_FOUND;
2260 } else if (nested_rep_count > 0) {
2261 if (i == PP_ENDREP) {
2262 nested_rep_count--;
2263 return NO_DIRECTIVE_FOUND;
2269 switch (i) {
2270 case PP_INVALID:
2271 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2272 tline->text);
2273 return NO_DIRECTIVE_FOUND; /* didn't get it */
2275 case PP_STACKSIZE:
2276 /* Directive to tell NASM what the default stack size is. The
2277 * default is for a 16-bit stack, and this can be overriden with
2278 * %stacksize large.
2280 tline = tline->next;
2281 if (tline && tline->type == TOK_WHITESPACE)
2282 tline = tline->next;
2283 if (!tline || tline->type != TOK_ID) {
2284 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2285 free_tlist(origline);
2286 return DIRECTIVE_FOUND;
2288 if (nasm_stricmp(tline->text, "flat") == 0) {
2289 /* All subsequent ARG directives are for a 32-bit stack */
2290 StackSize = 4;
2291 StackPointer = "ebp";
2292 ArgOffset = 8;
2293 LocalOffset = 0;
2294 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2295 /* All subsequent ARG directives are for a 64-bit stack */
2296 StackSize = 8;
2297 StackPointer = "rbp";
2298 ArgOffset = 16;
2299 LocalOffset = 0;
2300 } else if (nasm_stricmp(tline->text, "large") == 0) {
2301 /* All subsequent ARG directives are for a 16-bit stack,
2302 * far function call.
2304 StackSize = 2;
2305 StackPointer = "bp";
2306 ArgOffset = 4;
2307 LocalOffset = 0;
2308 } else if (nasm_stricmp(tline->text, "small") == 0) {
2309 /* All subsequent ARG directives are for a 16-bit stack,
2310 * far function call. We don't support near functions.
2312 StackSize = 2;
2313 StackPointer = "bp";
2314 ArgOffset = 6;
2315 LocalOffset = 0;
2316 } else {
2317 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2318 free_tlist(origline);
2319 return DIRECTIVE_FOUND;
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2324 case PP_ARG:
2325 /* TASM like ARG directive to define arguments to functions, in
2326 * the following form:
2328 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2330 offset = ArgOffset;
2331 do {
2332 char *arg, directive[256];
2333 int size = StackSize;
2335 /* Find the argument name */
2336 tline = tline->next;
2337 if (tline && tline->type == TOK_WHITESPACE)
2338 tline = tline->next;
2339 if (!tline || tline->type != TOK_ID) {
2340 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2341 free_tlist(origline);
2342 return DIRECTIVE_FOUND;
2344 arg = tline->text;
2346 /* Find the argument size type */
2347 tline = tline->next;
2348 if (!tline || tline->type != TOK_OTHER
2349 || tline->text[0] != ':') {
2350 error(ERR_NONFATAL,
2351 "Syntax error processing `%%arg' directive");
2352 free_tlist(origline);
2353 return DIRECTIVE_FOUND;
2355 tline = tline->next;
2356 if (!tline || tline->type != TOK_ID) {
2357 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2362 /* Allow macro expansion of type parameter */
2363 tt = tokenize(tline->text);
2364 tt = expand_smacro(tt);
2365 size = parse_size(tt->text);
2366 if (!size) {
2367 error(ERR_NONFATAL,
2368 "Invalid size type for `%%arg' missing directive");
2369 free_tlist(tt);
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2373 free_tlist(tt);
2375 /* Round up to even stack slots */
2376 size = ALIGN(size, StackSize);
2378 /* Now define the macro for the argument */
2379 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2380 arg, StackPointer, offset);
2381 do_directive(tokenize(directive));
2382 offset += size;
2384 /* Move to the next argument in the list */
2385 tline = tline->next;
2386 if (tline && tline->type == TOK_WHITESPACE)
2387 tline = tline->next;
2388 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2389 ArgOffset = offset;
2390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2393 case PP_LOCAL:
2394 /* TASM like LOCAL directive to define local variables for a
2395 * function, in the following form:
2397 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2399 * The '= LocalSize' at the end is ignored by NASM, but is
2400 * required by TASM to define the local parameter size (and used
2401 * by the TASM macro package).
2403 offset = LocalOffset;
2404 do {
2405 char *local, directive[256];
2406 int size = StackSize;
2408 /* Find the argument name */
2409 tline = tline->next;
2410 if (tline && tline->type == TOK_WHITESPACE)
2411 tline = tline->next;
2412 if (!tline || tline->type != TOK_ID) {
2413 error(ERR_NONFATAL,
2414 "`%%local' missing argument parameter");
2415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2418 local = tline->text;
2420 /* Find the argument size type */
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_OTHER
2423 || tline->text[0] != ':') {
2424 error(ERR_NONFATAL,
2425 "Syntax error processing `%%local' directive");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2429 tline = tline->next;
2430 if (!tline || tline->type != TOK_ID) {
2431 error(ERR_NONFATAL,
2432 "`%%local' missing size type parameter");
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
2437 /* Allow macro expansion of type parameter */
2438 tt = tokenize(tline->text);
2439 tt = expand_smacro(tt);
2440 size = parse_size(tt->text);
2441 if (!size) {
2442 error(ERR_NONFATAL,
2443 "Invalid size type for `%%local' missing directive");
2444 free_tlist(tt);
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2448 free_tlist(tt);
2450 /* Round up to even stack slots */
2451 size = ALIGN(size, StackSize);
2453 offset += size; /* Negative offset, increment before */
2455 /* Now define the macro for the argument */
2456 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2457 local, StackPointer, offset);
2458 do_directive(tokenize(directive));
2460 /* Now define the assign to setup the enter_c macro correctly */
2461 snprintf(directive, sizeof(directive),
2462 "%%assign %%$localsize %%$localsize+%d", size);
2463 do_directive(tokenize(directive));
2465 /* Move to the next argument in the list */
2466 tline = tline->next;
2467 if (tline && tline->type == TOK_WHITESPACE)
2468 tline = tline->next;
2469 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2470 LocalOffset = offset;
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2474 case PP_CLEAR:
2475 if (tline->next)
2476 error(ERR_WARNING|ERR_PASS1,
2477 "trailing garbage after `%%clear' ignored");
2478 free_macros();
2479 init_macros();
2480 free_tlist(origline);
2481 return DIRECTIVE_FOUND;
2483 case PP_DEPEND:
2484 t = tline->next = expand_smacro(tline->next);
2485 skip_white_(t);
2486 if (!t || (t->type != TOK_STRING &&
2487 t->type != TOK_INTERNAL_STRING)) {
2488 error(ERR_NONFATAL, "`%%depend' expects a file name");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND; /* but we did _something_ */
2492 if (t->next)
2493 error(ERR_WARNING|ERR_PASS1,
2494 "trailing garbage after `%%depend' ignored");
2495 p = t->text;
2496 if (t->type != TOK_INTERNAL_STRING)
2497 nasm_unquote_cstr(p, i);
2498 if (dephead && !in_list(*dephead, p)) {
2499 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2500 sl->next = NULL;
2501 strcpy(sl->str, p);
2502 *deptail = sl;
2503 deptail = &sl->next;
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2508 case PP_INCLUDE:
2509 t = tline->next = expand_smacro(tline->next);
2510 skip_white_(t);
2512 if (!t || (t->type != TOK_STRING &&
2513 t->type != TOK_INTERNAL_STRING)) {
2514 error(ERR_NONFATAL, "`%%include' expects a file name");
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND; /* but we did _something_ */
2518 if (t->next)
2519 error(ERR_WARNING|ERR_PASS1,
2520 "trailing garbage after `%%include' ignored");
2521 p = t->text;
2522 if (t->type != TOK_INTERNAL_STRING)
2523 nasm_unquote_cstr(p, i);
2524 inc = nasm_malloc(sizeof(Include));
2525 inc->next = istk;
2526 inc->conds = NULL;
2527 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2528 if (!inc->fp) {
2529 /* -MG given but file not found */
2530 nasm_free(inc);
2531 } else {
2532 inc->fname = src_set_fname(nasm_strdup(p));
2533 inc->lineno = src_set_linnum(0);
2534 inc->lineinc = 1;
2535 inc->expansion = NULL;
2536 inc->mstk = NULL;
2537 istk = inc;
2538 list->uplevel(LIST_INCLUDE);
2540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
2543 case PP_USE:
2545 static macros_t *use_pkg;
2546 const char *pkg_macro = NULL;
2548 tline = tline->next;
2549 skip_white_(tline);
2550 tline = expand_id(tline);
2552 if (!tline || (tline->type != TOK_STRING &&
2553 tline->type != TOK_INTERNAL_STRING &&
2554 tline->type != TOK_ID)) {
2555 error(ERR_NONFATAL, "`%%use' expects a package name");
2556 free_tlist(origline);
2557 return DIRECTIVE_FOUND; /* but we did _something_ */
2559 if (tline->next)
2560 error(ERR_WARNING|ERR_PASS1,
2561 "trailing garbage after `%%use' ignored");
2562 if (tline->type == TOK_STRING)
2563 nasm_unquote_cstr(tline->text, i);
2564 use_pkg = nasm_stdmac_find_package(tline->text);
2565 if (!use_pkg)
2566 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2567 else
2568 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2569 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2570 /* Not already included, go ahead and include it */
2571 stdmacpos = use_pkg;
2573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
2576 case PP_PUSH:
2577 case PP_REPL:
2578 case PP_POP:
2579 tline = tline->next;
2580 skip_white_(tline);
2581 tline = expand_id(tline);
2582 if (tline) {
2583 if (!tok_type_(tline, TOK_ID)) {
2584 error(ERR_NONFATAL, "`%s' expects a context identifier",
2585 pp_directives[i]);
2586 free_tlist(origline);
2587 return DIRECTIVE_FOUND; /* but we did _something_ */
2589 if (tline->next)
2590 error(ERR_WARNING|ERR_PASS1,
2591 "trailing garbage after `%s' ignored",
2592 pp_directives[i]);
2593 p = nasm_strdup(tline->text);
2594 } else {
2595 p = NULL; /* Anonymous */
2598 if (i == PP_PUSH) {
2599 ctx = nasm_malloc(sizeof(Context));
2600 ctx->next = cstk;
2601 hash_init(&ctx->localmac, HASH_SMALL);
2602 ctx->name = p;
2603 ctx->number = unique++;
2604 cstk = ctx;
2605 } else {
2606 /* %pop or %repl */
2607 if (!cstk) {
2608 error(ERR_NONFATAL, "`%s': context stack is empty",
2609 pp_directives[i]);
2610 } else if (i == PP_POP) {
2611 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2612 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2613 "expected %s",
2614 cstk->name ? cstk->name : "anonymous", p);
2615 else
2616 ctx_pop();
2617 } else {
2618 /* i == PP_REPL */
2619 nasm_free(cstk->name);
2620 cstk->name = p;
2621 p = NULL;
2623 nasm_free(p);
2625 free_tlist(origline);
2626 return DIRECTIVE_FOUND;
2627 case PP_FATAL:
2628 severity = ERR_FATAL;
2629 goto issue_error;
2630 case PP_ERROR:
2631 severity = ERR_NONFATAL;
2632 goto issue_error;
2633 case PP_WARNING:
2634 severity = ERR_WARNING|ERR_WARN_USER;
2635 goto issue_error;
2637 issue_error:
2639 /* Only error out if this is the final pass */
2640 if (pass != 2 && i != PP_FATAL)
2641 return DIRECTIVE_FOUND;
2643 tline->next = expand_smacro(tline->next);
2644 tline = tline->next;
2645 skip_white_(tline);
2646 t = tline ? tline->next : NULL;
2647 skip_white_(t);
2648 if (tok_type_(tline, TOK_STRING) && !t) {
2649 /* The line contains only a quoted string */
2650 p = tline->text;
2651 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2652 error(severity, "%s", p);
2653 } else {
2654 /* Not a quoted string, or more than a quoted string */
2655 p = detoken(tline, false);
2656 error(severity, "%s", p);
2657 nasm_free(p);
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2663 CASE_PP_IF:
2664 if (istk->conds && !emitting(istk->conds->state))
2665 j = COND_NEVER;
2666 else {
2667 j = if_condition(tline->next, i);
2668 tline->next = NULL; /* it got freed */
2669 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2671 cond = nasm_malloc(sizeof(Cond));
2672 cond->next = istk->conds;
2673 cond->state = j;
2674 istk->conds = cond;
2675 if(istk->mstk)
2676 istk->mstk->condcnt ++;
2677 free_tlist(origline);
2678 return DIRECTIVE_FOUND;
2680 CASE_PP_ELIF:
2681 if (!istk->conds)
2682 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2683 switch(istk->conds->state) {
2684 case COND_IF_TRUE:
2685 istk->conds->state = COND_DONE;
2686 break;
2688 case COND_DONE:
2689 case COND_NEVER:
2690 break;
2692 case COND_ELSE_TRUE:
2693 case COND_ELSE_FALSE:
2694 error_precond(ERR_WARNING|ERR_PASS1,
2695 "`%%elif' after `%%else' ignored");
2696 istk->conds->state = COND_NEVER;
2697 break;
2699 case COND_IF_FALSE:
2701 * IMPORTANT: In the case of %if, we will already have
2702 * called expand_mmac_params(); however, if we're
2703 * processing an %elif we must have been in a
2704 * non-emitting mode, which would have inhibited
2705 * the normal invocation of expand_mmac_params().
2706 * Therefore, we have to do it explicitly here.
2708 j = if_condition(expand_mmac_params(tline->next), i);
2709 tline->next = NULL; /* it got freed */
2710 istk->conds->state =
2711 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2712 break;
2714 free_tlist(origline);
2715 return DIRECTIVE_FOUND;
2717 case PP_ELSE:
2718 if (tline->next)
2719 error_precond(ERR_WARNING|ERR_PASS1,
2720 "trailing garbage after `%%else' ignored");
2721 if (!istk->conds)
2722 error(ERR_FATAL, "`%%else': no matching `%%if'");
2723 switch(istk->conds->state) {
2724 case COND_IF_TRUE:
2725 case COND_DONE:
2726 istk->conds->state = COND_ELSE_FALSE;
2727 break;
2729 case COND_NEVER:
2730 break;
2732 case COND_IF_FALSE:
2733 istk->conds->state = COND_ELSE_TRUE;
2734 break;
2736 case COND_ELSE_TRUE:
2737 case COND_ELSE_FALSE:
2738 error_precond(ERR_WARNING|ERR_PASS1,
2739 "`%%else' after `%%else' ignored.");
2740 istk->conds->state = COND_NEVER;
2741 break;
2743 free_tlist(origline);
2744 return DIRECTIVE_FOUND;
2746 case PP_ENDIF:
2747 if (tline->next)
2748 error_precond(ERR_WARNING|ERR_PASS1,
2749 "trailing garbage after `%%endif' ignored");
2750 if (!istk->conds)
2751 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2752 cond = istk->conds;
2753 istk->conds = cond->next;
2754 nasm_free(cond);
2755 if(istk->mstk)
2756 istk->mstk->condcnt --;
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
2760 case PP_RMACRO:
2761 case PP_IRMACRO:
2762 case PP_MACRO:
2763 case PP_IMACRO:
2764 if (defining) {
2765 error(ERR_FATAL, "`%s': already defining a macro",
2766 pp_directives[i]);
2767 return DIRECTIVE_FOUND;
2769 defining = nasm_malloc(sizeof(MMacro));
2770 defining->max_depth =
2771 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2772 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2773 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2774 nasm_free(defining);
2775 defining = NULL;
2776 return DIRECTIVE_FOUND;
2779 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2780 while (mmac) {
2781 if (!strcmp(mmac->name, defining->name) &&
2782 (mmac->nparam_min <= defining->nparam_max
2783 || defining->plus)
2784 && (defining->nparam_min <= mmac->nparam_max
2785 || mmac->plus)) {
2786 error(ERR_WARNING|ERR_PASS1,
2787 "redefining multi-line macro `%s'", defining->name);
2788 return DIRECTIVE_FOUND;
2790 mmac = mmac->next;
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
2795 case PP_ENDM:
2796 case PP_ENDMACRO:
2797 if (! (defining && defining->name)) {
2798 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2799 return DIRECTIVE_FOUND;
2801 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2802 defining->next = *mmhead;
2803 *mmhead = defining;
2804 defining = NULL;
2805 free_tlist(origline);
2806 return DIRECTIVE_FOUND;
2808 case PP_EXITMACRO:
2810 * We must search along istk->expansion until we hit a
2811 * macro-end marker for a macro with a name. Then we
2812 * bypass all lines between exitmacro and endmacro.
2814 list_for_each(l, istk->expansion)
2815 if (l->finishes && l->finishes->name)
2816 break;
2818 if (l) {
2820 * Remove all conditional entries relative to this
2821 * macro invocation. (safe to do in this context)
2823 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2824 cond = istk->conds;
2825 istk->conds = cond->next;
2826 nasm_free(cond);
2828 istk->expansion = l;
2829 } else {
2830 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2832 free_tlist(origline);
2833 return DIRECTIVE_FOUND;
2835 case PP_UNMACRO:
2836 case PP_UNIMACRO:
2838 MMacro **mmac_p;
2839 MMacro spec;
2841 spec.casesense = (i == PP_UNMACRO);
2842 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2843 return DIRECTIVE_FOUND;
2845 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2846 while (mmac_p && *mmac_p) {
2847 mmac = *mmac_p;
2848 if (mmac->casesense == spec.casesense &&
2849 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2850 mmac->nparam_min == spec.nparam_min &&
2851 mmac->nparam_max == spec.nparam_max &&
2852 mmac->plus == spec.plus) {
2853 *mmac_p = mmac->next;
2854 free_mmacro(mmac);
2855 } else {
2856 mmac_p = &mmac->next;
2859 free_tlist(origline);
2860 free_tlist(spec.dlist);
2861 return DIRECTIVE_FOUND;
2864 case PP_ROTATE:
2865 if (tline->next && tline->next->type == TOK_WHITESPACE)
2866 tline = tline->next;
2867 if (!tline->next) {
2868 free_tlist(origline);
2869 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2870 return DIRECTIVE_FOUND;
2872 t = expand_smacro(tline->next);
2873 tline->next = NULL;
2874 free_tlist(origline);
2875 tline = t;
2876 tptr = &t;
2877 tokval.t_type = TOKEN_INVALID;
2878 evalresult =
2879 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2880 free_tlist(tline);
2881 if (!evalresult)
2882 return DIRECTIVE_FOUND;
2883 if (tokval.t_type)
2884 error(ERR_WARNING|ERR_PASS1,
2885 "trailing garbage after expression ignored");
2886 if (!is_simple(evalresult)) {
2887 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2888 return DIRECTIVE_FOUND;
2890 mmac = istk->mstk;
2891 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2892 mmac = mmac->next_active;
2893 if (!mmac) {
2894 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2895 } else if (mmac->nparam == 0) {
2896 error(ERR_NONFATAL,
2897 "`%%rotate' invoked within macro without parameters");
2898 } else {
2899 int rotate = mmac->rotate + reloc_value(evalresult);
2901 rotate %= (int)mmac->nparam;
2902 if (rotate < 0)
2903 rotate += mmac->nparam;
2905 mmac->rotate = rotate;
2907 return DIRECTIVE_FOUND;
2909 case PP_REP:
2910 nolist = false;
2911 do {
2912 tline = tline->next;
2913 } while (tok_type_(tline, TOK_WHITESPACE));
2915 if (tok_type_(tline, TOK_ID) &&
2916 nasm_stricmp(tline->text, ".nolist") == 0) {
2917 nolist = true;
2918 do {
2919 tline = tline->next;
2920 } while (tok_type_(tline, TOK_WHITESPACE));
2923 if (tline) {
2924 t = expand_smacro(tline);
2925 tptr = &t;
2926 tokval.t_type = TOKEN_INVALID;
2927 evalresult =
2928 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2929 if (!evalresult) {
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
2933 if (tokval.t_type)
2934 error(ERR_WARNING|ERR_PASS1,
2935 "trailing garbage after expression ignored");
2936 if (!is_simple(evalresult)) {
2937 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2938 return DIRECTIVE_FOUND;
2940 count = reloc_value(evalresult);
2941 if (count >= REP_LIMIT) {
2942 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2943 count = 0;
2944 } else
2945 count++;
2946 } else {
2947 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2948 count = 0;
2950 free_tlist(origline);
2952 tmp_defining = defining;
2953 defining = nasm_malloc(sizeof(MMacro));
2954 defining->prev = NULL;
2955 defining->name = NULL; /* flags this macro as a %rep block */
2956 defining->casesense = false;
2957 defining->plus = false;
2958 defining->nolist = nolist;
2959 defining->in_progress = count;
2960 defining->max_depth = 0;
2961 defining->nparam_min = defining->nparam_max = 0;
2962 defining->defaults = NULL;
2963 defining->dlist = NULL;
2964 defining->expansion = NULL;
2965 defining->next_active = istk->mstk;
2966 defining->rep_nest = tmp_defining;
2967 return DIRECTIVE_FOUND;
2969 case PP_ENDREP:
2970 if (!defining || defining->name) {
2971 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2972 return DIRECTIVE_FOUND;
2976 * Now we have a "macro" defined - although it has no name
2977 * and we won't be entering it in the hash tables - we must
2978 * push a macro-end marker for it on to istk->expansion.
2979 * After that, it will take care of propagating itself (a
2980 * macro-end marker line for a macro which is really a %rep
2981 * block will cause the macro to be re-expanded, complete
2982 * with another macro-end marker to ensure the process
2983 * continues) until the whole expansion is forcibly removed
2984 * from istk->expansion by a %exitrep.
2986 l = nasm_malloc(sizeof(Line));
2987 l->next = istk->expansion;
2988 l->finishes = defining;
2989 l->first = NULL;
2990 istk->expansion = l;
2992 istk->mstk = defining;
2994 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2995 tmp_defining = defining;
2996 defining = defining->rep_nest;
2997 free_tlist(origline);
2998 return DIRECTIVE_FOUND;
3000 case PP_EXITREP:
3002 * We must search along istk->expansion until we hit a
3003 * macro-end marker for a macro with no name. Then we set
3004 * its `in_progress' flag to 0.
3006 list_for_each(l, istk->expansion)
3007 if (l->finishes && !l->finishes->name)
3008 break;
3010 if (l)
3011 l->finishes->in_progress = 1;
3012 else
3013 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
3017 case PP_XDEFINE:
3018 case PP_IXDEFINE:
3019 case PP_DEFINE:
3020 case PP_IDEFINE:
3021 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3023 tline = tline->next;
3024 skip_white_(tline);
3025 tline = expand_id(tline);
3026 if (!tline || (tline->type != TOK_ID &&
3027 (tline->type != TOK_PREPROC_ID ||
3028 tline->text[1] != '$'))) {
3029 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3030 pp_directives[i]);
3031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
3035 ctx = get_ctx(tline->text, &mname, false);
3036 last = tline;
3037 param_start = tline = tline->next;
3038 nparam = 0;
3040 /* Expand the macro definition now for %xdefine and %ixdefine */
3041 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3042 tline = expand_smacro(tline);
3044 if (tok_is_(tline, "(")) {
3046 * This macro has parameters.
3049 tline = tline->next;
3050 while (1) {
3051 skip_white_(tline);
3052 if (!tline) {
3053 error(ERR_NONFATAL, "parameter identifier expected");
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3057 if (tline->type != TOK_ID) {
3058 error(ERR_NONFATAL,
3059 "`%s': parameter identifier expected",
3060 tline->text);
3061 free_tlist(origline);
3062 return DIRECTIVE_FOUND;
3064 tline->type = TOK_SMAC_PARAM + nparam++;
3065 tline = tline->next;
3066 skip_white_(tline);
3067 if (tok_is_(tline, ",")) {
3068 tline = tline->next;
3069 } else {
3070 if (!tok_is_(tline, ")")) {
3071 error(ERR_NONFATAL,
3072 "`)' expected to terminate macro template");
3073 free_tlist(origline);
3074 return DIRECTIVE_FOUND;
3076 break;
3079 last = tline;
3080 tline = tline->next;
3082 if (tok_type_(tline, TOK_WHITESPACE))
3083 last = tline, tline = tline->next;
3084 macro_start = NULL;
3085 last->next = NULL;
3086 t = tline;
3087 while (t) {
3088 if (t->type == TOK_ID) {
3089 list_for_each(tt, param_start)
3090 if (tt->type >= TOK_SMAC_PARAM &&
3091 !strcmp(tt->text, t->text))
3092 t->type = tt->type;
3094 tt = t->next;
3095 t->next = macro_start;
3096 macro_start = t;
3097 t = tt;
3100 * Good. We now have a macro name, a parameter count, and a
3101 * token list (in reverse order) for an expansion. We ought
3102 * to be OK just to create an SMacro, store it, and let
3103 * free_tlist have the rest of the line (which we have
3104 * carefully re-terminated after chopping off the expansion
3105 * from the end).
3107 define_smacro(ctx, mname, casesense, nparam, macro_start);
3108 free_tlist(origline);
3109 return DIRECTIVE_FOUND;
3111 case PP_UNDEF:
3112 tline = tline->next;
3113 skip_white_(tline);
3114 tline = expand_id(tline);
3115 if (!tline || (tline->type != TOK_ID &&
3116 (tline->type != TOK_PREPROC_ID ||
3117 tline->text[1] != '$'))) {
3118 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3119 free_tlist(origline);
3120 return DIRECTIVE_FOUND;
3122 if (tline->next) {
3123 error(ERR_WARNING|ERR_PASS1,
3124 "trailing garbage after macro name ignored");
3127 /* Find the context that symbol belongs to */
3128 ctx = get_ctx(tline->text, &mname, false);
3129 undef_smacro(ctx, mname);
3130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
3133 case PP_DEFSTR:
3134 case PP_IDEFSTR:
3135 casesense = (i == PP_DEFSTR);
3137 tline = tline->next;
3138 skip_white_(tline);
3139 tline = expand_id(tline);
3140 if (!tline || (tline->type != TOK_ID &&
3141 (tline->type != TOK_PREPROC_ID ||
3142 tline->text[1] != '$'))) {
3143 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3144 pp_directives[i]);
3145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
3149 ctx = get_ctx(tline->text, &mname, false);
3150 last = tline;
3151 tline = expand_smacro(tline->next);
3152 last->next = NULL;
3154 while (tok_type_(tline, TOK_WHITESPACE))
3155 tline = delete_Token(tline);
3157 p = detoken(tline, false);
3158 macro_start = nasm_malloc(sizeof(*macro_start));
3159 macro_start->next = NULL;
3160 macro_start->text = nasm_quote(p, strlen(p));
3161 macro_start->type = TOK_STRING;
3162 macro_start->a.mac = NULL;
3163 nasm_free(p);
3166 * We now have a macro name, an implicit parameter count of
3167 * zero, and a string token to use as an expansion. Create
3168 * and store an SMacro.
3170 define_smacro(ctx, mname, casesense, 0, macro_start);
3171 free_tlist(origline);
3172 return DIRECTIVE_FOUND;
3174 case PP_DEFTOK:
3175 case PP_IDEFTOK:
3176 casesense = (i == PP_DEFTOK);
3178 tline = tline->next;
3179 skip_white_(tline);
3180 tline = expand_id(tline);
3181 if (!tline || (tline->type != TOK_ID &&
3182 (tline->type != TOK_PREPROC_ID ||
3183 tline->text[1] != '$'))) {
3184 error(ERR_NONFATAL,
3185 "`%s' expects a macro identifier as first parameter",
3186 pp_directives[i]);
3187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
3190 ctx = get_ctx(tline->text, &mname, false);
3191 last = tline;
3192 tline = expand_smacro(tline->next);
3193 last->next = NULL;
3195 t = tline;
3196 while (tok_type_(t, TOK_WHITESPACE))
3197 t = t->next;
3198 /* t should now point to the string */
3199 if (!tok_type_(t, TOK_STRING)) {
3200 error(ERR_NONFATAL,
3201 "`%s` requires string as second parameter",
3202 pp_directives[i]);
3203 free_tlist(tline);
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3209 * Convert the string to a token stream. Note that smacros
3210 * are stored with the token stream reversed, so we have to
3211 * reverse the output of tokenize().
3213 nasm_unquote_cstr(t->text, i);
3214 macro_start = reverse_tokens(tokenize(t->text));
3217 * We now have a macro name, an implicit parameter count of
3218 * zero, and a numeric token to use as an expansion. Create
3219 * and store an SMacro.
3221 define_smacro(ctx, mname, casesense, 0, macro_start);
3222 free_tlist(tline);
3223 free_tlist(origline);
3224 return DIRECTIVE_FOUND;
3226 case PP_PATHSEARCH:
3228 FILE *fp;
3229 StrList *xsl = NULL;
3230 StrList **xst = &xsl;
3232 casesense = true;
3234 tline = tline->next;
3235 skip_white_(tline);
3236 tline = expand_id(tline);
3237 if (!tline || (tline->type != TOK_ID &&
3238 (tline->type != TOK_PREPROC_ID ||
3239 tline->text[1] != '$'))) {
3240 error(ERR_NONFATAL,
3241 "`%%pathsearch' expects a macro identifier as first parameter");
3242 free_tlist(origline);
3243 return DIRECTIVE_FOUND;
3245 ctx = get_ctx(tline->text, &mname, false);
3246 last = tline;
3247 tline = expand_smacro(tline->next);
3248 last->next = NULL;
3250 t = tline;
3251 while (tok_type_(t, TOK_WHITESPACE))
3252 t = t->next;
3254 if (!t || (t->type != TOK_STRING &&
3255 t->type != TOK_INTERNAL_STRING)) {
3256 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3257 free_tlist(tline);
3258 free_tlist(origline);
3259 return DIRECTIVE_FOUND; /* but we did _something_ */
3261 if (t->next)
3262 error(ERR_WARNING|ERR_PASS1,
3263 "trailing garbage after `%%pathsearch' ignored");
3264 p = t->text;
3265 if (t->type != TOK_INTERNAL_STRING)
3266 nasm_unquote(p, NULL);
3268 fp = inc_fopen(p, &xsl, &xst, true);
3269 if (fp) {
3270 p = xsl->str;
3271 fclose(fp); /* Don't actually care about the file */
3273 macro_start = nasm_malloc(sizeof(*macro_start));
3274 macro_start->next = NULL;
3275 macro_start->text = nasm_quote(p, strlen(p));
3276 macro_start->type = TOK_STRING;
3277 macro_start->a.mac = NULL;
3278 if (xsl)
3279 nasm_free(xsl);
3282 * We now have a macro name, an implicit parameter count of
3283 * zero, and a string token to use as an expansion. Create
3284 * and store an SMacro.
3286 define_smacro(ctx, mname, casesense, 0, macro_start);
3287 free_tlist(tline);
3288 free_tlist(origline);
3289 return DIRECTIVE_FOUND;
3292 case PP_STRLEN:
3293 casesense = true;
3295 tline = tline->next;
3296 skip_white_(tline);
3297 tline = expand_id(tline);
3298 if (!tline || (tline->type != TOK_ID &&
3299 (tline->type != TOK_PREPROC_ID ||
3300 tline->text[1] != '$'))) {
3301 error(ERR_NONFATAL,
3302 "`%%strlen' expects a macro identifier as first parameter");
3303 free_tlist(origline);
3304 return DIRECTIVE_FOUND;
3306 ctx = get_ctx(tline->text, &mname, false);
3307 last = tline;
3308 tline = expand_smacro(tline->next);
3309 last->next = NULL;
3311 t = tline;
3312 while (tok_type_(t, TOK_WHITESPACE))
3313 t = t->next;
3314 /* t should now point to the string */
3315 if (!tok_type_(t, TOK_STRING)) {
3316 error(ERR_NONFATAL,
3317 "`%%strlen` requires string as second parameter");
3318 free_tlist(tline);
3319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
3323 macro_start = nasm_malloc(sizeof(*macro_start));
3324 macro_start->next = NULL;
3325 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3326 macro_start->a.mac = NULL;
3329 * We now have a macro name, an implicit parameter count of
3330 * zero, and a numeric token to use as an expansion. Create
3331 * and store an SMacro.
3333 define_smacro(ctx, mname, casesense, 0, macro_start);
3334 free_tlist(tline);
3335 free_tlist(origline);
3336 return DIRECTIVE_FOUND;
3338 case PP_STRCAT:
3339 casesense = true;
3341 tline = tline->next;
3342 skip_white_(tline);
3343 tline = expand_id(tline);
3344 if (!tline || (tline->type != TOK_ID &&
3345 (tline->type != TOK_PREPROC_ID ||
3346 tline->text[1] != '$'))) {
3347 error(ERR_NONFATAL,
3348 "`%%strcat' expects a macro identifier as first parameter");
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
3352 ctx = get_ctx(tline->text, &mname, false);
3353 last = tline;
3354 tline = expand_smacro(tline->next);
3355 last->next = NULL;
3357 len = 0;
3358 list_for_each(t, tline) {
3359 switch (t->type) {
3360 case TOK_WHITESPACE:
3361 break;
3362 case TOK_STRING:
3363 len += t->a.len = nasm_unquote(t->text, NULL);
3364 break;
3365 case TOK_OTHER:
3366 if (!strcmp(t->text, ",")) /* permit comma separators */
3367 break;
3368 /* else fall through */
3369 default:
3370 error(ERR_NONFATAL,
3371 "non-string passed to `%%strcat' (%d)", t->type);
3372 free_tlist(tline);
3373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3378 p = pp = nasm_malloc(len);
3379 list_for_each(t, tline) {
3380 if (t->type == TOK_STRING) {
3381 memcpy(p, t->text, t->a.len);
3382 p += t->a.len;
3387 * We now have a macro name, an implicit parameter count of
3388 * zero, and a numeric token to use as an expansion. Create
3389 * and store an SMacro.
3391 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3392 macro_start->text = nasm_quote(pp, len);
3393 nasm_free(pp);
3394 define_smacro(ctx, mname, casesense, 0, macro_start);
3395 free_tlist(tline);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3399 case PP_SUBSTR:
3401 int64_t start, count;
3402 size_t len;
3404 casesense = true;
3406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
3412 error(ERR_NONFATAL,
3413 "`%%substr' expects a macro identifier as first parameter");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3417 ctx = get_ctx(tline->text, &mname, false);
3418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
3422 if (tline) /* skip expanded id */
3423 t = tline->next;
3424 while (tok_type_(t, TOK_WHITESPACE))
3425 t = t->next;
3427 /* t should now point to the string */
3428 if (!tok_type_(t, TOK_STRING)) {
3429 error(ERR_NONFATAL,
3430 "`%%substr` requires string as second parameter");
3431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3436 tt = t->next;
3437 tptr = &tt;
3438 tokval.t_type = TOKEN_INVALID;
3439 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3440 pass, error, NULL);
3441 if (!evalresult) {
3442 free_tlist(tline);
3443 free_tlist(origline);
3444 return DIRECTIVE_FOUND;
3445 } else if (!is_simple(evalresult)) {
3446 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3447 free_tlist(tline);
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3451 start = evalresult->value - 1;
3453 while (tok_type_(tt, TOK_WHITESPACE))
3454 tt = tt->next;
3455 if (!tt) {
3456 count = 1; /* Backwards compatibility: one character */
3457 } else {
3458 tokval.t_type = TOKEN_INVALID;
3459 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3460 pass, error, NULL);
3461 if (!evalresult) {
3462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
3465 } else if (!is_simple(evalresult)) {
3466 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND;
3471 count = evalresult->value;
3474 len = nasm_unquote(t->text, NULL);
3476 /* make start and count being in range */
3477 if (start < 0)
3478 start = 0;
3479 if (count < 0)
3480 count = len + count + 1 - start;
3481 if (start + count > (int64_t)len)
3482 count = len - start;
3483 if (!len || count < 0 || start >=(int64_t)len)
3484 start = -1, count = 0; /* empty string */
3486 macro_start = nasm_malloc(sizeof(*macro_start));
3487 macro_start->next = NULL;
3488 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3489 macro_start->type = TOK_STRING;
3490 macro_start->a.mac = NULL;
3493 * We now have a macro name, an implicit parameter count of
3494 * zero, and a numeric token to use as an expansion. Create
3495 * and store an SMacro.
3497 define_smacro(ctx, mname, casesense, 0, macro_start);
3498 free_tlist(tline);
3499 free_tlist(origline);
3500 return DIRECTIVE_FOUND;
3503 case PP_ASSIGN:
3504 case PP_IASSIGN:
3505 casesense = (i == PP_ASSIGN);
3507 tline = tline->next;
3508 skip_white_(tline);
3509 tline = expand_id(tline);
3510 if (!tline || (tline->type != TOK_ID &&
3511 (tline->type != TOK_PREPROC_ID ||
3512 tline->text[1] != '$'))) {
3513 error(ERR_NONFATAL,
3514 "`%%%sassign' expects a macro identifier",
3515 (i == PP_IASSIGN ? "i" : ""));
3516 free_tlist(origline);
3517 return DIRECTIVE_FOUND;
3519 ctx = get_ctx(tline->text, &mname, false);
3520 last = tline;
3521 tline = expand_smacro(tline->next);
3522 last->next = NULL;
3524 t = tline;
3525 tptr = &t;
3526 tokval.t_type = TOKEN_INVALID;
3527 evalresult =
3528 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3529 free_tlist(tline);
3530 if (!evalresult) {
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
3535 if (tokval.t_type)
3536 error(ERR_WARNING|ERR_PASS1,
3537 "trailing garbage after expression ignored");
3539 if (!is_simple(evalresult)) {
3540 error(ERR_NONFATAL,
3541 "non-constant value given to `%%%sassign'",
3542 (i == PP_IASSIGN ? "i" : ""));
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3547 macro_start = nasm_malloc(sizeof(*macro_start));
3548 macro_start->next = NULL;
3549 make_tok_num(macro_start, reloc_value(evalresult));
3550 macro_start->a.mac = NULL;
3553 * We now have a macro name, an implicit parameter count of
3554 * zero, and a numeric token to use as an expansion. Create
3555 * and store an SMacro.
3557 define_smacro(ctx, mname, casesense, 0, macro_start);
3558 free_tlist(origline);
3559 return DIRECTIVE_FOUND;
3561 case PP_LINE:
3563 * Syntax is `%line nnn[+mmm] [filename]'
3565 tline = tline->next;
3566 skip_white_(tline);
3567 if (!tok_type_(tline, TOK_NUMBER)) {
3568 error(ERR_NONFATAL, "`%%line' expects line number");
3569 free_tlist(origline);
3570 return DIRECTIVE_FOUND;
3572 k = readnum(tline->text, &err);
3573 m = 1;
3574 tline = tline->next;
3575 if (tok_is_(tline, "+")) {
3576 tline = tline->next;
3577 if (!tok_type_(tline, TOK_NUMBER)) {
3578 error(ERR_NONFATAL, "`%%line' expects line increment");
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3582 m = readnum(tline->text, &err);
3583 tline = tline->next;
3585 skip_white_(tline);
3586 src_set_linnum(k);
3587 istk->lineinc = m;
3588 if (tline) {
3589 nasm_free(src_set_fname(detoken(tline, false)));
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
3594 default:
3595 error(ERR_FATAL,
3596 "preprocessor directive `%s' not yet implemented",
3597 pp_directives[i]);
3598 return DIRECTIVE_FOUND;
3603 * Ensure that a macro parameter contains a condition code and
3604 * nothing else. Return the condition code index if so, or -1
3605 * otherwise.
3607 static int find_cc(Token * t)
3609 Token *tt;
3610 int i, j, k, m;
3612 if (!t)
3613 return -1; /* Probably a %+ without a space */
3615 skip_white_(t);
3616 if (t->type != TOK_ID)
3617 return -1;
3618 tt = t->next;
3619 skip_white_(tt);
3620 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3621 return -1;
3623 i = -1;
3624 j = ARRAY_SIZE(conditions);
3625 while (j - i > 1) {
3626 k = (j + i) / 2;
3627 m = nasm_stricmp(t->text, conditions[k]);
3628 if (m == 0) {
3629 i = k;
3630 j = -2;
3631 break;
3632 } else if (m < 0) {
3633 j = k;
3634 } else
3635 i = k;
3637 if (j != -2)
3638 return -1;
3639 return i;
3642 static bool paste_tokens(Token **head, int mask_head, int mask_tail,
3643 bool handle_paste_tokens)
3645 Token **tail, *t, *tt;
3646 Token **paste_head;
3647 bool did_paste = false;
3648 char *tmp;
3650 /* Now handle token pasting... */
3651 paste_head = NULL;
3652 tail = head;
3653 while ((t = *tail) && (tt = t->next)) {
3654 switch (t->type) {
3655 case TOK_WHITESPACE:
3656 if (tt->type == TOK_WHITESPACE) {
3657 /* Zap adjacent whitespace tokens */
3658 t->next = delete_Token(tt);
3659 } else {
3660 /* Do not advance paste_head here */
3661 tail = &t->next;
3663 break;
3664 case TOK_PASTE: /* %+ */
3665 if (handle_paste_tokens) {
3666 /* Zap %+ and whitespace tokens to the right */
3667 while (t && (t->type == TOK_WHITESPACE ||
3668 t->type == TOK_PASTE))
3669 t = *tail = delete_Token(t);
3670 if (!paste_head || !t)
3671 break; /* Nothing to paste with */
3672 tail = paste_head;
3673 t = *tail;
3674 tt = t->next;
3675 while (tok_type_(tt, TOK_WHITESPACE))
3676 tt = t->next = delete_Token(tt);
3677 if (tt) {
3678 tmp = nasm_strcat(t->text, tt->text);
3679 delete_Token(t);
3680 tt = delete_Token(tt);
3681 t = *tail = tokenize(tmp);
3682 nasm_free(tmp);
3683 while (t->next) {
3684 tail = &t->next;
3685 t = t->next;
3687 t->next = tt; /* Attach the remaining token chain */
3688 did_paste = true;
3690 paste_head = tail;
3691 tail = &t->next;
3692 break;
3694 /* else fall through */
3695 default:
3697 * Concatenation of tokens might look nontrivial
3698 * but in real it's pretty simple -- the caller
3699 * prepares the masks of tokens to be concatenated
3700 * and we simply find matched sequences and slip
3701 * them together
3703 if (PP_CONCAT_MASK(t->type) & mask_head) {
3704 size_t len = 0;
3705 char *tmp, *p;
3707 while (tt && (PP_CONCAT_MASK(tt->type) & mask_tail)) {
3708 len += strlen(tt->text);
3709 tt = tt->next;
3713 * Now tt points to the first token after
3714 * the potential paste area...
3716 if (tt != t->next) {
3717 /* We have at least two tokens... */
3718 len += strlen(t->text);
3719 p = tmp = nasm_malloc(len+1);
3720 while (t != tt) {
3721 strcpy(p, t->text);
3722 p = strchr(p, '\0');
3723 t = delete_Token(t);
3725 t = *tail = tokenize(tmp);
3726 nasm_free(tmp);
3727 while (t->next) {
3728 tail = &t->next;
3729 t = t->next;
3731 t->next = tt; /* Attach the remaining token chain */
3732 did_paste = true;
3734 paste_head = tail;
3735 tail = &t->next;
3736 } else {
3737 tail = &t->next;
3738 if (!tok_type_(t->next, TOK_WHITESPACE))
3739 paste_head = tail;
3741 break;
3744 return did_paste;
3748 * expands to a list of tokens from %{x:y}
3750 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3752 Token *t = tline, **tt, *tm, *head;
3753 char *pos;
3754 int fst, lst, j, i;
3756 pos = strchr(tline->text, ':');
3757 nasm_assert(pos);
3759 lst = atoi(pos + 1);
3760 fst = atoi(tline->text + 1);
3763 * only macros params are accounted so
3764 * if someone passes %0 -- we reject such
3765 * value(s)
3767 if (lst == 0 || fst == 0)
3768 goto err;
3770 /* the values should be sane */
3771 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3772 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3773 goto err;
3775 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3776 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3778 /* counted from zero */
3779 fst--, lst--;
3782 * it will be at least one token
3784 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3785 t = new_Token(NULL, tm->type, tm->text, 0);
3786 head = t, tt = &t->next;
3787 if (fst < lst) {
3788 for (i = fst + 1; i <= lst; i++) {
3789 t = new_Token(NULL, TOK_OTHER, ",", 0);
3790 *tt = t, tt = &t->next;
3791 j = (i + mac->rotate) % mac->nparam;
3792 tm = mac->params[j];
3793 t = new_Token(NULL, tm->type, tm->text, 0);
3794 *tt = t, tt = &t->next;
3796 } else {
3797 for (i = fst - 1; i >= lst; i--) {
3798 t = new_Token(NULL, TOK_OTHER, ",", 0);
3799 *tt = t, tt = &t->next;
3800 j = (i + mac->rotate) % mac->nparam;
3801 tm = mac->params[j];
3802 t = new_Token(NULL, tm->type, tm->text, 0);
3803 *tt = t, tt = &t->next;
3807 *last = tt;
3808 return head;
3810 err:
3811 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3812 &tline->text[1]);
3813 return tline;
3817 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3818 * %-n) and MMacro-local identifiers (%%foo) as well as
3819 * macro indirection (%[...]) and range (%{..:..}).
3821 static Token *expand_mmac_params(Token * tline)
3823 Token *t, *tt, **tail, *thead;
3824 bool changed = false;
3825 char *pos;
3827 tail = &thead;
3828 thead = NULL;
3830 while (tline) {
3831 if (tline->type == TOK_PREPROC_ID &&
3832 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3833 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3834 tline->text[1] == '%')) {
3835 char *text = NULL;
3836 int type = 0, cc; /* type = 0 to placate optimisers */
3837 char tmpbuf[30];
3838 unsigned int n;
3839 int i;
3840 MMacro *mac;
3842 t = tline;
3843 tline = tline->next;
3845 mac = istk->mstk;
3846 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3847 mac = mac->next_active;
3848 if (!mac) {
3849 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3850 } else {
3851 pos = strchr(t->text, ':');
3852 if (!pos) {
3853 switch (t->text[1]) {
3855 * We have to make a substitution of one of the
3856 * forms %1, %-1, %+1, %%foo, %0.
3858 case '0':
3859 type = TOK_NUMBER;
3860 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3861 text = nasm_strdup(tmpbuf);
3862 break;
3863 case '%':
3864 type = TOK_ID;
3865 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3866 mac->unique);
3867 text = nasm_strcat(tmpbuf, t->text + 2);
3868 break;
3869 case '-':
3870 n = atoi(t->text + 2) - 1;
3871 if (n >= mac->nparam)
3872 tt = NULL;
3873 else {
3874 if (mac->nparam > 1)
3875 n = (n + mac->rotate) % mac->nparam;
3876 tt = mac->params[n];
3878 cc = find_cc(tt);
3879 if (cc == -1) {
3880 error(ERR_NONFATAL,
3881 "macro parameter %d is not a condition code",
3882 n + 1);
3883 text = NULL;
3884 } else {
3885 type = TOK_ID;
3886 if (inverse_ccs[cc] == -1) {
3887 error(ERR_NONFATAL,
3888 "condition code `%s' is not invertible",
3889 conditions[cc]);
3890 text = NULL;
3891 } else
3892 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3894 break;
3895 case '+':
3896 n = atoi(t->text + 2) - 1;
3897 if (n >= mac->nparam)
3898 tt = NULL;
3899 else {
3900 if (mac->nparam > 1)
3901 n = (n + mac->rotate) % mac->nparam;
3902 tt = mac->params[n];
3904 cc = find_cc(tt);
3905 if (cc == -1) {
3906 error(ERR_NONFATAL,
3907 "macro parameter %d is not a condition code",
3908 n + 1);
3909 text = NULL;
3910 } else {
3911 type = TOK_ID;
3912 text = nasm_strdup(conditions[cc]);
3914 break;
3915 default:
3916 n = atoi(t->text + 1) - 1;
3917 if (n >= mac->nparam)
3918 tt = NULL;
3919 else {
3920 if (mac->nparam > 1)
3921 n = (n + mac->rotate) % mac->nparam;
3922 tt = mac->params[n];
3924 if (tt) {
3925 for (i = 0; i < mac->paramlen[n]; i++) {
3926 *tail = new_Token(NULL, tt->type, tt->text, 0);
3927 tail = &(*tail)->next;
3928 tt = tt->next;
3931 text = NULL; /* we've done it here */
3932 break;
3934 } else {
3936 * seems we have a parameters range here
3938 Token *head, **last;
3939 head = expand_mmac_params_range(mac, t, &last);
3940 if (head != t) {
3941 *tail = head;
3942 *last = tline;
3943 tline = head;
3944 text = NULL;
3948 if (!text) {
3949 delete_Token(t);
3950 } else {
3951 *tail = t;
3952 tail = &t->next;
3953 t->type = type;
3954 nasm_free(t->text);
3955 t->text = text;
3956 t->a.mac = NULL;
3958 changed = true;
3959 continue;
3960 } else if (tline->type == TOK_INDIRECT) {
3961 t = tline;
3962 tline = tline->next;
3963 tt = tokenize(t->text);
3964 tt = expand_mmac_params(tt);
3965 tt = expand_smacro(tt);
3966 *tail = tt;
3967 while (tt) {
3968 tt->a.mac = NULL; /* Necessary? */
3969 tail = &tt->next;
3970 tt = tt->next;
3972 delete_Token(t);
3973 changed = true;
3974 } else {
3975 t = *tail = tline;
3976 tline = tline->next;
3977 t->a.mac = NULL;
3978 tail = &t->next;
3981 *tail = NULL;
3983 if (changed) {
3984 int mask_head = PP_CONCAT_MASK(TOK_ID) |
3985 PP_CONCAT_MASK(TOK_NUMBER) |
3986 PP_CONCAT_MASK(TOK_FLOAT);
3987 int mask_tail = PP_CONCAT_MASK(TOK_ID) |
3988 PP_CONCAT_MASK(TOK_NUMBER) |
3989 PP_CONCAT_MASK(TOK_FLOAT) |
3990 PP_CONCAT_MASK(TOK_OTHER);
3991 paste_tokens(&thead, mask_head, mask_tail, false);
3994 return thead;
3998 * Expand all single-line macro calls made in the given line.
3999 * Return the expanded version of the line. The original is deemed
4000 * to be destroyed in the process. (In reality we'll just move
4001 * Tokens from input to output a lot of the time, rather than
4002 * actually bothering to destroy and replicate.)
4005 static Token *expand_smacro(Token * tline)
4007 Token *t, *tt, *mstart, **tail, *thead;
4008 SMacro *head = NULL, *m;
4009 Token **params;
4010 int *paramsize;
4011 unsigned int nparam, sparam;
4012 int brackets;
4013 Token *org_tline = tline;
4014 Context *ctx;
4015 const char *mname;
4016 int deadman = DEADMAN_LIMIT;
4017 bool expanded;
4020 * Trick: we should avoid changing the start token pointer since it can
4021 * be contained in "next" field of other token. Because of this
4022 * we allocate a copy of first token and work with it; at the end of
4023 * routine we copy it back
4025 if (org_tline) {
4026 tline = new_Token(org_tline->next, org_tline->type,
4027 org_tline->text, 0);
4028 tline->a.mac = org_tline->a.mac;
4029 nasm_free(org_tline->text);
4030 org_tline->text = NULL;
4033 expanded = true; /* Always expand %+ at least once */
4035 again:
4036 thead = NULL;
4037 tail = &thead;
4039 while (tline) { /* main token loop */
4040 if (!--deadman) {
4041 error(ERR_NONFATAL, "interminable macro recursion");
4042 goto err;
4045 if ((mname = tline->text)) {
4046 /* if this token is a local macro, look in local context */
4047 if (tline->type == TOK_ID) {
4048 head = (SMacro *)hash_findix(&smacros, mname);
4049 } else if (tline->type == TOK_PREPROC_ID) {
4050 ctx = get_ctx(mname, &mname, true);
4051 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4052 } else
4053 head = NULL;
4056 * We've hit an identifier. As in is_mmacro below, we first
4057 * check whether the identifier is a single-line macro at
4058 * all, then think about checking for parameters if
4059 * necessary.
4061 list_for_each(m, head)
4062 if (!mstrcmp(m->name, mname, m->casesense))
4063 break;
4064 if (m) {
4065 mstart = tline;
4066 params = NULL;
4067 paramsize = NULL;
4068 if (m->nparam == 0) {
4070 * Simple case: the macro is parameterless. Discard the
4071 * one token that the macro call took, and push the
4072 * expansion back on the to-do stack.
4074 if (!m->expansion) {
4075 if (!strcmp("__FILE__", m->name)) {
4076 int32_t num = 0;
4077 char *file = NULL;
4078 src_get(&num, &file);
4079 tline->text = nasm_quote(file, strlen(file));
4080 tline->type = TOK_STRING;
4081 nasm_free(file);
4082 continue;
4084 if (!strcmp("__LINE__", m->name)) {
4085 nasm_free(tline->text);
4086 make_tok_num(tline, src_get_linnum());
4087 continue;
4089 if (!strcmp("__BITS__", m->name)) {
4090 nasm_free(tline->text);
4091 make_tok_num(tline, globalbits);
4092 continue;
4094 tline = delete_Token(tline);
4095 continue;
4097 } else {
4099 * Complicated case: at least one macro with this name
4100 * exists and takes parameters. We must find the
4101 * parameters in the call, count them, find the SMacro
4102 * that corresponds to that form of the macro call, and
4103 * substitute for the parameters when we expand. What a
4104 * pain.
4106 /*tline = tline->next;
4107 skip_white_(tline); */
4108 do {
4109 t = tline->next;
4110 while (tok_type_(t, TOK_SMAC_END)) {
4111 t->a.mac->in_progress = false;
4112 t->text = NULL;
4113 t = tline->next = delete_Token(t);
4115 tline = t;
4116 } while (tok_type_(tline, TOK_WHITESPACE));
4117 if (!tok_is_(tline, "(")) {
4119 * This macro wasn't called with parameters: ignore
4120 * the call. (Behaviour borrowed from gnu cpp.)
4122 tline = mstart;
4123 m = NULL;
4124 } else {
4125 int paren = 0;
4126 int white = 0;
4127 brackets = 0;
4128 nparam = 0;
4129 sparam = PARAM_DELTA;
4130 params = nasm_malloc(sparam * sizeof(Token *));
4131 params[0] = tline->next;
4132 paramsize = nasm_malloc(sparam * sizeof(int));
4133 paramsize[0] = 0;
4134 while (true) { /* parameter loop */
4136 * For some unusual expansions
4137 * which concatenates function call
4139 t = tline->next;
4140 while (tok_type_(t, TOK_SMAC_END)) {
4141 t->a.mac->in_progress = false;
4142 t->text = NULL;
4143 t = tline->next = delete_Token(t);
4145 tline = t;
4147 if (!tline) {
4148 error(ERR_NONFATAL,
4149 "macro call expects terminating `)'");
4150 break;
4152 if (tline->type == TOK_WHITESPACE
4153 && brackets <= 0) {
4154 if (paramsize[nparam])
4155 white++;
4156 else
4157 params[nparam] = tline->next;
4158 continue; /* parameter loop */
4160 if (tline->type == TOK_OTHER
4161 && tline->text[1] == 0) {
4162 char ch = tline->text[0];
4163 if (ch == ',' && !paren && brackets <= 0) {
4164 if (++nparam >= sparam) {
4165 sparam += PARAM_DELTA;
4166 params = nasm_realloc(params,
4167 sparam * sizeof(Token *));
4168 paramsize = nasm_realloc(paramsize,
4169 sparam * sizeof(int));
4171 params[nparam] = tline->next;
4172 paramsize[nparam] = 0;
4173 white = 0;
4174 continue; /* parameter loop */
4176 if (ch == '{' &&
4177 (brackets > 0 || (brackets == 0 &&
4178 !paramsize[nparam])))
4180 if (!(brackets++)) {
4181 params[nparam] = tline->next;
4182 continue; /* parameter loop */
4185 if (ch == '}' && brackets > 0)
4186 if (--brackets == 0) {
4187 brackets = -1;
4188 continue; /* parameter loop */
4190 if (ch == '(' && !brackets)
4191 paren++;
4192 if (ch == ')' && brackets <= 0)
4193 if (--paren < 0)
4194 break;
4196 if (brackets < 0) {
4197 brackets = 0;
4198 error(ERR_NONFATAL, "braces do not "
4199 "enclose all of macro parameter");
4201 paramsize[nparam] += white + 1;
4202 white = 0;
4203 } /* parameter loop */
4204 nparam++;
4205 while (m && (m->nparam != nparam ||
4206 mstrcmp(m->name, mname,
4207 m->casesense)))
4208 m = m->next;
4209 if (!m)
4210 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4211 "macro `%s' exists, "
4212 "but not taking %d parameters",
4213 mstart->text, nparam);
4216 if (m && m->in_progress)
4217 m = NULL;
4218 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4220 * Design question: should we handle !tline, which
4221 * indicates missing ')' here, or expand those
4222 * macros anyway, which requires the (t) test a few
4223 * lines down?
4225 nasm_free(params);
4226 nasm_free(paramsize);
4227 tline = mstart;
4228 } else {
4230 * Expand the macro: we are placed on the last token of the
4231 * call, so that we can easily split the call from the
4232 * following tokens. We also start by pushing an SMAC_END
4233 * token for the cycle removal.
4235 t = tline;
4236 if (t) {
4237 tline = t->next;
4238 t->next = NULL;
4240 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4241 tt->a.mac = m;
4242 m->in_progress = true;
4243 tline = tt;
4244 list_for_each(t, m->expansion) {
4245 if (t->type >= TOK_SMAC_PARAM) {
4246 Token *pcopy = tline, **ptail = &pcopy;
4247 Token *ttt, *pt;
4248 int i;
4250 ttt = params[t->type - TOK_SMAC_PARAM];
4251 i = paramsize[t->type - TOK_SMAC_PARAM];
4252 while (--i >= 0) {
4253 pt = *ptail = new_Token(tline, ttt->type,
4254 ttt->text, 0);
4255 ptail = &pt->next;
4256 ttt = ttt->next;
4258 tline = pcopy;
4259 } else if (t->type == TOK_PREPROC_Q) {
4260 tt = new_Token(tline, TOK_ID, mname, 0);
4261 tline = tt;
4262 } else if (t->type == TOK_PREPROC_QQ) {
4263 tt = new_Token(tline, TOK_ID, m->name, 0);
4264 tline = tt;
4265 } else {
4266 tt = new_Token(tline, t->type, t->text, 0);
4267 tline = tt;
4272 * Having done that, get rid of the macro call, and clean
4273 * up the parameters.
4275 nasm_free(params);
4276 nasm_free(paramsize);
4277 free_tlist(mstart);
4278 expanded = true;
4279 continue; /* main token loop */
4284 if (tline->type == TOK_SMAC_END) {
4285 tline->a.mac->in_progress = false;
4286 tline = delete_Token(tline);
4287 } else {
4288 t = *tail = tline;
4289 tline = tline->next;
4290 t->a.mac = NULL;
4291 t->next = NULL;
4292 tail = &t->next;
4297 * Now scan the entire line and look for successive TOK_IDs that resulted
4298 * after expansion (they can't be produced by tokenize()). The successive
4299 * TOK_IDs should be concatenated.
4300 * Also we look for %+ tokens and concatenate the tokens before and after
4301 * them (without white spaces in between).
4303 if (expanded) {
4304 int mask_head = PP_CONCAT_MASK(TOK_ID) |
4305 PP_CONCAT_MASK(TOK_PREPROC_ID);
4306 int mask_tail = PP_CONCAT_MASK(TOK_ID) |
4307 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4308 PP_CONCAT_MASK(TOK_NUMBER);
4309 if (paste_tokens(&thead, mask_head, mask_tail, true)) {
4311 * If we concatenated something, *and* we had previously expanded
4312 * an actual macro, scan the lines again for macros...
4314 tline = thead;
4315 expanded = false;
4316 goto again;
4320 err:
4321 if (org_tline) {
4322 if (thead) {
4323 *org_tline = *thead;
4324 /* since we just gave text to org_line, don't free it */
4325 thead->text = NULL;
4326 delete_Token(thead);
4327 } else {
4328 /* the expression expanded to empty line;
4329 we can't return NULL for some reasons
4330 we just set the line to a single WHITESPACE token. */
4331 memset(org_tline, 0, sizeof(*org_tline));
4332 org_tline->text = NULL;
4333 org_tline->type = TOK_WHITESPACE;
4335 thead = org_tline;
4338 return thead;
4342 * Similar to expand_smacro but used exclusively with macro identifiers
4343 * right before they are fetched in. The reason is that there can be
4344 * identifiers consisting of several subparts. We consider that if there
4345 * are more than one element forming the name, user wants a expansion,
4346 * otherwise it will be left as-is. Example:
4348 * %define %$abc cde
4350 * the identifier %$abc will be left as-is so that the handler for %define
4351 * will suck it and define the corresponding value. Other case:
4353 * %define _%$abc cde
4355 * In this case user wants name to be expanded *before* %define starts
4356 * working, so we'll expand %$abc into something (if it has a value;
4357 * otherwise it will be left as-is) then concatenate all successive
4358 * PP_IDs into one.
4360 static Token *expand_id(Token * tline)
4362 Token *cur, *oldnext = NULL;
4364 if (!tline || !tline->next)
4365 return tline;
4367 cur = tline;
4368 while (cur->next &&
4369 (cur->next->type == TOK_ID ||
4370 cur->next->type == TOK_PREPROC_ID
4371 || cur->next->type == TOK_NUMBER))
4372 cur = cur->next;
4374 /* If identifier consists of just one token, don't expand */
4375 if (cur == tline)
4376 return tline;
4378 if (cur) {
4379 oldnext = cur->next; /* Detach the tail past identifier */
4380 cur->next = NULL; /* so that expand_smacro stops here */
4383 tline = expand_smacro(tline);
4385 if (cur) {
4386 /* expand_smacro possibly changhed tline; re-scan for EOL */
4387 cur = tline;
4388 while (cur && cur->next)
4389 cur = cur->next;
4390 if (cur)
4391 cur->next = oldnext;
4394 return tline;
4398 * Determine whether the given line constitutes a multi-line macro
4399 * call, and return the MMacro structure called if so. Doesn't have
4400 * to check for an initial label - that's taken care of in
4401 * expand_mmacro - but must check numbers of parameters. Guaranteed
4402 * to be called with tline->type == TOK_ID, so the putative macro
4403 * name is easy to find.
4405 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4407 MMacro *head, *m;
4408 Token **params;
4409 int nparam;
4411 head = (MMacro *) hash_findix(&mmacros, tline->text);
4414 * Efficiency: first we see if any macro exists with the given
4415 * name. If not, we can return NULL immediately. _Then_ we
4416 * count the parameters, and then we look further along the
4417 * list if necessary to find the proper MMacro.
4419 list_for_each(m, head)
4420 if (!mstrcmp(m->name, tline->text, m->casesense))
4421 break;
4422 if (!m)
4423 return NULL;
4426 * OK, we have a potential macro. Count and demarcate the
4427 * parameters.
4429 count_mmac_params(tline->next, &nparam, &params);
4432 * So we know how many parameters we've got. Find the MMacro
4433 * structure that handles this number.
4435 while (m) {
4436 if (m->nparam_min <= nparam
4437 && (m->plus || nparam <= m->nparam_max)) {
4439 * This one is right. Just check if cycle removal
4440 * prohibits us using it before we actually celebrate...
4442 if (m->in_progress > m->max_depth) {
4443 if (m->max_depth > 0) {
4444 error(ERR_WARNING,
4445 "reached maximum recursion depth of %i",
4446 m->max_depth);
4448 nasm_free(params);
4449 return NULL;
4452 * It's right, and we can use it. Add its default
4453 * parameters to the end of our list if necessary.
4455 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4456 params =
4457 nasm_realloc(params,
4458 ((m->nparam_min + m->ndefs +
4459 1) * sizeof(*params)));
4460 while (nparam < m->nparam_min + m->ndefs) {
4461 params[nparam] = m->defaults[nparam - m->nparam_min];
4462 nparam++;
4466 * If we've gone over the maximum parameter count (and
4467 * we're in Plus mode), ignore parameters beyond
4468 * nparam_max.
4470 if (m->plus && nparam > m->nparam_max)
4471 nparam = m->nparam_max;
4473 * Then terminate the parameter list, and leave.
4475 if (!params) { /* need this special case */
4476 params = nasm_malloc(sizeof(*params));
4477 nparam = 0;
4479 params[nparam] = NULL;
4480 *params_array = params;
4481 return m;
4484 * This one wasn't right: look for the next one with the
4485 * same name.
4487 list_for_each(m, m->next)
4488 if (!mstrcmp(m->name, tline->text, m->casesense))
4489 break;
4493 * After all that, we didn't find one with the right number of
4494 * parameters. Issue a warning, and fail to expand the macro.
4496 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4497 "macro `%s' exists, but not taking %d parameters",
4498 tline->text, nparam);
4499 nasm_free(params);
4500 return NULL;
4505 * Save MMacro invocation specific fields in
4506 * preparation for a recursive macro expansion
4508 static void push_mmacro(MMacro *m)
4510 MMacroInvocation *i;
4512 i = nasm_malloc(sizeof(MMacroInvocation));
4513 i->prev = m->prev;
4514 i->params = m->params;
4515 i->iline = m->iline;
4516 i->nparam = m->nparam;
4517 i->rotate = m->rotate;
4518 i->paramlen = m->paramlen;
4519 i->unique = m->unique;
4520 i->condcnt = m->condcnt;
4521 m->prev = i;
4526 * Restore MMacro invocation specific fields that were
4527 * saved during a previous recursive macro expansion
4529 static void pop_mmacro(MMacro *m)
4531 MMacroInvocation *i;
4533 if (m->prev) {
4534 i = m->prev;
4535 m->prev = i->prev;
4536 m->params = i->params;
4537 m->iline = i->iline;
4538 m->nparam = i->nparam;
4539 m->rotate = i->rotate;
4540 m->paramlen = i->paramlen;
4541 m->unique = i->unique;
4542 m->condcnt = i->condcnt;
4543 nasm_free(i);
4549 * Expand the multi-line macro call made by the given line, if
4550 * there is one to be expanded. If there is, push the expansion on
4551 * istk->expansion and return 1. Otherwise return 0.
4553 static int expand_mmacro(Token * tline)
4555 Token *startline = tline;
4556 Token *label = NULL;
4557 int dont_prepend = 0;
4558 Token **params, *t, *mtok, *tt;
4559 MMacro *m;
4560 Line *l, *ll;
4561 int i, nparam, *paramlen;
4562 const char *mname;
4564 t = tline;
4565 skip_white_(t);
4566 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4567 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4568 return 0;
4569 mtok = t;
4570 m = is_mmacro(t, &params);
4571 if (m) {
4572 mname = t->text;
4573 } else {
4574 Token *last;
4576 * We have an id which isn't a macro call. We'll assume
4577 * it might be a label; we'll also check to see if a
4578 * colon follows it. Then, if there's another id after
4579 * that lot, we'll check it again for macro-hood.
4581 label = last = t;
4582 t = t->next;
4583 if (tok_type_(t, TOK_WHITESPACE))
4584 last = t, t = t->next;
4585 if (tok_is_(t, ":")) {
4586 dont_prepend = 1;
4587 last = t, t = t->next;
4588 if (tok_type_(t, TOK_WHITESPACE))
4589 last = t, t = t->next;
4591 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4592 return 0;
4593 last->next = NULL;
4594 mname = t->text;
4595 tline = t;
4599 * Fix up the parameters: this involves stripping leading and
4600 * trailing whitespace, then stripping braces if they are
4601 * present.
4603 for (nparam = 0; params[nparam]; nparam++) ;
4604 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4606 for (i = 0; params[i]; i++) {
4607 int brace = false;
4608 int comma = (!m->plus || i < nparam - 1);
4610 t = params[i];
4611 skip_white_(t);
4612 if (tok_is_(t, "{"))
4613 t = t->next, brace = true, comma = false;
4614 params[i] = t;
4615 paramlen[i] = 0;
4616 while (t) {
4617 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4618 break; /* ... because we have hit a comma */
4619 if (comma && t->type == TOK_WHITESPACE
4620 && tok_is_(t->next, ","))
4621 break; /* ... or a space then a comma */
4622 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4623 break; /* ... or a brace */
4624 t = t->next;
4625 paramlen[i]++;
4630 * OK, we have a MMacro structure together with a set of
4631 * parameters. We must now go through the expansion and push
4632 * copies of each Line on to istk->expansion. Substitution of
4633 * parameter tokens and macro-local tokens doesn't get done
4634 * until the single-line macro substitution process; this is
4635 * because delaying them allows us to change the semantics
4636 * later through %rotate.
4638 * First, push an end marker on to istk->expansion, mark this
4639 * macro as in progress, and set up its invocation-specific
4640 * variables.
4642 ll = nasm_malloc(sizeof(Line));
4643 ll->next = istk->expansion;
4644 ll->finishes = m;
4645 ll->first = NULL;
4646 istk->expansion = ll;
4649 * Save the previous MMacro expansion in the case of
4650 * macro recursion
4652 if (m->max_depth && m->in_progress)
4653 push_mmacro(m);
4655 m->in_progress ++;
4656 m->params = params;
4657 m->iline = tline;
4658 m->nparam = nparam;
4659 m->rotate = 0;
4660 m->paramlen = paramlen;
4661 m->unique = unique++;
4662 m->lineno = 0;
4663 m->condcnt = 0;
4665 m->next_active = istk->mstk;
4666 istk->mstk = m;
4668 list_for_each(l, m->expansion) {
4669 Token **tail;
4671 ll = nasm_malloc(sizeof(Line));
4672 ll->finishes = NULL;
4673 ll->next = istk->expansion;
4674 istk->expansion = ll;
4675 tail = &ll->first;
4677 list_for_each(t, l->first) {
4678 Token *x = t;
4679 switch (t->type) {
4680 case TOK_PREPROC_Q:
4681 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4682 break;
4683 case TOK_PREPROC_QQ:
4684 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4685 break;
4686 case TOK_PREPROC_ID:
4687 if (t->text[1] == '0' && t->text[2] == '0') {
4688 dont_prepend = -1;
4689 x = label;
4690 if (!x)
4691 continue;
4693 /* fall through */
4694 default:
4695 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4696 break;
4698 tail = &tt->next;
4700 *tail = NULL;
4704 * If we had a label, push it on as the first line of
4705 * the macro expansion.
4707 if (label) {
4708 if (dont_prepend < 0)
4709 free_tlist(startline);
4710 else {
4711 ll = nasm_malloc(sizeof(Line));
4712 ll->finishes = NULL;
4713 ll->next = istk->expansion;
4714 istk->expansion = ll;
4715 ll->first = startline;
4716 if (!dont_prepend) {
4717 while (label->next)
4718 label = label->next;
4719 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4724 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4726 return 1;
4729 /* The function that actually does the error reporting */
4730 static void verror(int severity, const char *fmt, va_list arg)
4732 char buff[1024];
4733 MMacro *mmac = NULL;
4734 int delta = 0;
4736 vsnprintf(buff, sizeof(buff), fmt, arg);
4738 /* get %macro name */
4739 if (istk && istk->mstk) {
4740 mmac = istk->mstk;
4741 /* but %rep blocks should be skipped */
4742 while (mmac && !mmac->name)
4743 mmac = mmac->next_active, delta++;
4746 if (mmac)
4747 nasm_error(severity, "(%s:%d) %s",
4748 mmac->name, mmac->lineno - delta, buff);
4749 else
4750 nasm_error(severity, "%s", buff);
4754 * Since preprocessor always operate only on the line that didn't
4755 * arrived yet, we should always use ERR_OFFBY1.
4757 static void error(int severity, const char *fmt, ...)
4759 va_list arg;
4761 /* If we're in a dead branch of IF or something like it, ignore the error */
4762 if (istk && istk->conds && !emitting(istk->conds->state))
4763 return;
4765 va_start(arg, fmt);
4766 verror(severity, fmt, arg);
4767 va_end(arg);
4771 * Because %else etc are evaluated in the state context
4772 * of the previous branch, errors might get lost with error():
4773 * %if 0 ... %else trailing garbage ... %endif
4774 * So %else etc should report errors with this function.
4776 static void error_precond(int severity, const char *fmt, ...)
4778 va_list arg;
4780 /* Only ignore the error if it's really in a dead branch */
4781 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4782 return;
4784 va_start(arg, fmt);
4785 verror(severity, fmt, arg);
4786 va_end(arg);
4789 static void
4790 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4792 Token *t;
4794 cstk = NULL;
4795 istk = nasm_malloc(sizeof(Include));
4796 istk->next = NULL;
4797 istk->conds = NULL;
4798 istk->expansion = NULL;
4799 istk->mstk = NULL;
4800 istk->fp = fopen(file, "r");
4801 istk->fname = NULL;
4802 src_set_fname(nasm_strdup(file));
4803 src_set_linnum(0);
4804 istk->lineinc = 1;
4805 if (!istk->fp)
4806 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4807 file);
4808 defining = NULL;
4809 nested_mac_count = 0;
4810 nested_rep_count = 0;
4811 init_macros();
4812 unique = 0;
4813 if (tasm_compatible_mode) {
4814 stdmacpos = nasm_stdmac;
4815 } else {
4816 stdmacpos = nasm_stdmac_after_tasm;
4818 any_extrastdmac = extrastdmac && *extrastdmac;
4819 do_predef = true;
4820 list = listgen;
4823 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4824 * The caller, however, will also pass in 3 for preprocess-only so
4825 * we can set __PASS__ accordingly.
4827 pass = apass > 2 ? 2 : apass;
4829 dephead = deptail = deplist;
4830 if (deplist) {
4831 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4832 sl->next = NULL;
4833 strcpy(sl->str, file);
4834 *deptail = sl;
4835 deptail = &sl->next;
4839 * Define the __PASS__ macro. This is defined here unlike
4840 * all the other builtins, because it is special -- it varies between
4841 * passes.
4843 t = nasm_malloc(sizeof(*t));
4844 t->next = NULL;
4845 make_tok_num(t, apass);
4846 t->a.mac = NULL;
4847 define_smacro(NULL, "__PASS__", true, 0, t);
4850 static char *pp_getline(void)
4852 char *line;
4853 Token *tline;
4855 while (1) {
4857 * Fetch a tokenized line, either from the macro-expansion
4858 * buffer or from the input file.
4860 tline = NULL;
4861 while (istk->expansion && istk->expansion->finishes) {
4862 Line *l = istk->expansion;
4863 if (!l->finishes->name && l->finishes->in_progress > 1) {
4864 Line *ll;
4867 * This is a macro-end marker for a macro with no
4868 * name, which means it's not really a macro at all
4869 * but a %rep block, and the `in_progress' field is
4870 * more than 1, meaning that we still need to
4871 * repeat. (1 means the natural last repetition; 0
4872 * means termination by %exitrep.) We have
4873 * therefore expanded up to the %endrep, and must
4874 * push the whole block on to the expansion buffer
4875 * again. We don't bother to remove the macro-end
4876 * marker: we'd only have to generate another one
4877 * if we did.
4879 l->finishes->in_progress--;
4880 list_for_each(l, l->finishes->expansion) {
4881 Token *t, *tt, **tail;
4883 ll = nasm_malloc(sizeof(Line));
4884 ll->next = istk->expansion;
4885 ll->finishes = NULL;
4886 ll->first = NULL;
4887 tail = &ll->first;
4889 list_for_each(t, l->first) {
4890 if (t->text || t->type == TOK_WHITESPACE) {
4891 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4892 tail = &tt->next;
4896 istk->expansion = ll;
4898 } else {
4900 * Check whether a `%rep' was started and not ended
4901 * within this macro expansion. This can happen and
4902 * should be detected. It's a fatal error because
4903 * I'm too confused to work out how to recover
4904 * sensibly from it.
4906 if (defining) {
4907 if (defining->name)
4908 error(ERR_PANIC,
4909 "defining with name in expansion");
4910 else if (istk->mstk->name)
4911 error(ERR_FATAL,
4912 "`%%rep' without `%%endrep' within"
4913 " expansion of macro `%s'",
4914 istk->mstk->name);
4918 * FIXME: investigate the relationship at this point between
4919 * istk->mstk and l->finishes
4922 MMacro *m = istk->mstk;
4923 istk->mstk = m->next_active;
4924 if (m->name) {
4926 * This was a real macro call, not a %rep, and
4927 * therefore the parameter information needs to
4928 * be freed.
4930 if (m->prev) {
4931 pop_mmacro(m);
4932 l->finishes->in_progress --;
4933 } else {
4934 nasm_free(m->params);
4935 free_tlist(m->iline);
4936 nasm_free(m->paramlen);
4937 l->finishes->in_progress = 0;
4939 } else
4940 free_mmacro(m);
4942 istk->expansion = l->next;
4943 nasm_free(l);
4944 list->downlevel(LIST_MACRO);
4947 while (1) { /* until we get a line we can use */
4949 if (istk->expansion) { /* from a macro expansion */
4950 char *p;
4951 Line *l = istk->expansion;
4952 if (istk->mstk)
4953 istk->mstk->lineno++;
4954 tline = l->first;
4955 istk->expansion = l->next;
4956 nasm_free(l);
4957 p = detoken(tline, false);
4958 list->line(LIST_MACRO, p);
4959 nasm_free(p);
4960 break;
4962 line = read_line();
4963 if (line) { /* from the current input file */
4964 line = prepreproc(line);
4965 tline = tokenize(line);
4966 nasm_free(line);
4967 break;
4970 * The current file has ended; work down the istk
4973 Include *i = istk;
4974 fclose(i->fp);
4975 if (i->conds) {
4976 /* nasm_error can't be conditionally suppressed */
4977 nasm_error(ERR_FATAL,
4978 "expected `%%endif' before end of file");
4980 /* only set line and file name if there's a next node */
4981 if (i->next) {
4982 src_set_linnum(i->lineno);
4983 nasm_free(src_set_fname(i->fname));
4985 istk = i->next;
4986 list->downlevel(LIST_INCLUDE);
4987 nasm_free(i);
4988 if (!istk)
4989 return NULL;
4990 if (istk->expansion && istk->expansion->finishes)
4991 break;
4996 * We must expand MMacro parameters and MMacro-local labels
4997 * _before_ we plunge into directive processing, to cope
4998 * with things like `%define something %1' such as STRUC
4999 * uses. Unless we're _defining_ a MMacro, in which case
5000 * those tokens should be left alone to go into the
5001 * definition; and unless we're in a non-emitting
5002 * condition, in which case we don't want to meddle with
5003 * anything.
5005 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5006 && !(istk->mstk && !istk->mstk->in_progress)) {
5007 tline = expand_mmac_params(tline);
5011 * Check the line to see if it's a preprocessor directive.
5013 if (do_directive(tline) == DIRECTIVE_FOUND) {
5014 continue;
5015 } else if (defining) {
5017 * We're defining a multi-line macro. We emit nothing
5018 * at all, and just
5019 * shove the tokenized line on to the macro definition.
5021 Line *l = nasm_malloc(sizeof(Line));
5022 l->next = defining->expansion;
5023 l->first = tline;
5024 l->finishes = NULL;
5025 defining->expansion = l;
5026 continue;
5027 } else if (istk->conds && !emitting(istk->conds->state)) {
5029 * We're in a non-emitting branch of a condition block.
5030 * Emit nothing at all, not even a blank line: when we
5031 * emerge from the condition we'll give a line-number
5032 * directive so we keep our place correctly.
5034 free_tlist(tline);
5035 continue;
5036 } else if (istk->mstk && !istk->mstk->in_progress) {
5038 * We're in a %rep block which has been terminated, so
5039 * we're walking through to the %endrep without
5040 * emitting anything. Emit nothing at all, not even a
5041 * blank line: when we emerge from the %rep block we'll
5042 * give a line-number directive so we keep our place
5043 * correctly.
5045 free_tlist(tline);
5046 continue;
5047 } else {
5048 tline = expand_smacro(tline);
5049 if (!expand_mmacro(tline)) {
5051 * De-tokenize the line again, and emit it.
5053 line = detoken(tline, true);
5054 free_tlist(tline);
5055 break;
5056 } else {
5057 continue; /* expand_mmacro calls free_tlist */
5062 return line;
5065 static void pp_cleanup(int pass)
5067 if (defining) {
5068 if (defining->name) {
5069 error(ERR_NONFATAL,
5070 "end of file while still defining macro `%s'",
5071 defining->name);
5072 } else {
5073 error(ERR_NONFATAL, "end of file while still in %%rep");
5076 free_mmacro(defining);
5077 defining = NULL;
5079 while (cstk)
5080 ctx_pop();
5081 free_macros();
5082 while (istk) {
5083 Include *i = istk;
5084 istk = istk->next;
5085 fclose(i->fp);
5086 nasm_free(i->fname);
5087 nasm_free(i);
5089 while (cstk)
5090 ctx_pop();
5091 nasm_free(src_set_fname(NULL));
5092 if (pass == 0) {
5093 IncPath *i;
5094 free_llist(predef);
5095 delete_Blocks();
5096 while ((i = ipath)) {
5097 ipath = i->next;
5098 if (i->path)
5099 nasm_free(i->path);
5100 nasm_free(i);
5105 void pp_include_path(char *path)
5107 IncPath *i;
5109 i = nasm_malloc(sizeof(IncPath));
5110 i->path = path ? nasm_strdup(path) : NULL;
5111 i->next = NULL;
5113 if (ipath) {
5114 IncPath *j = ipath;
5115 while (j->next)
5116 j = j->next;
5117 j->next = i;
5118 } else {
5119 ipath = i;
5123 void pp_pre_include(char *fname)
5125 Token *inc, *space, *name;
5126 Line *l;
5128 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5129 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5130 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5132 l = nasm_malloc(sizeof(Line));
5133 l->next = predef;
5134 l->first = inc;
5135 l->finishes = NULL;
5136 predef = l;
5139 void pp_pre_define(char *definition)
5141 Token *def, *space;
5142 Line *l;
5143 char *equals;
5145 equals = strchr(definition, '=');
5146 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5147 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5148 if (equals)
5149 *equals = ' ';
5150 space->next = tokenize(definition);
5151 if (equals)
5152 *equals = '=';
5154 l = nasm_malloc(sizeof(Line));
5155 l->next = predef;
5156 l->first = def;
5157 l->finishes = NULL;
5158 predef = l;
5161 void pp_pre_undefine(char *definition)
5163 Token *def, *space;
5164 Line *l;
5166 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5167 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5168 space->next = tokenize(definition);
5170 l = nasm_malloc(sizeof(Line));
5171 l->next = predef;
5172 l->first = def;
5173 l->finishes = NULL;
5174 predef = l;
5178 * Added by Keith Kanios:
5180 * This function is used to assist with "runtime" preprocessor
5181 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5183 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5184 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5187 void pp_runtime(char *definition)
5189 Token *def;
5191 def = tokenize(definition);
5192 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5193 free_tlist(def);
5197 void pp_extra_stdmac(macros_t *macros)
5199 extrastdmac = macros;
5202 static void make_tok_num(Token * tok, int64_t val)
5204 char numbuf[20];
5205 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5206 tok->text = nasm_strdup(numbuf);
5207 tok->type = TOK_NUMBER;
5210 Preproc nasmpp = {
5211 pp_reset,
5212 pp_getline,
5213 pp_cleanup