BR3026808: Assign to local preprocessor variable does not work in 2.09
[nasm/nasm.git] / preproc.c
blob9d560b9d6760813b8b76168ed0fc24a22aa5a1c6
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
48 * }
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
63 #include "compiler.h"
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <string.h>
70 #include <ctype.h>
71 #include <limits.h>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
84 typedef struct SMacro SMacro;
85 typedef struct MMacro MMacro;
86 typedef struct MMacroInvocation MMacroInvocation;
87 typedef struct Context Context;
88 typedef struct Token Token;
89 typedef struct Blocks Blocks;
90 typedef struct Line Line;
91 typedef struct Include Include;
92 typedef struct Cond Cond;
93 typedef struct IncPath IncPath;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
107 struct SMacro {
108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
133 struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
160 /* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
163 struct MMacroInvocation {
164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
170 uint64_t condcnt;
175 * The context stack is composed of a linked list of these.
177 struct Context {
178 Context *next;
179 char *name;
180 struct hash_table localmac;
181 uint32_t number;
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
192 * %define a(x,y) ( (x) & ~(y) )
194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
203 enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
215 struct Token {
216 Token *next;
217 char *text;
218 union {
219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
222 enum pp_token_type type;
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
247 struct Line {
248 Line *next;
249 MMacro *finishes;
250 Token *first;
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
257 struct Include {
258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
262 char *fname;
263 int lineno, lineinc;
264 MMacro *mstk; /* stack of active macros/reps */
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
272 struct IncPath {
273 IncPath *next;
274 char *path;
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
284 struct Cond {
285 Cond *next;
286 int state;
288 enum {
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
297 COND_IF_TRUE, COND_IF_FALSE,
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
313 COND_DONE, COND_NEVER
315 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318 * These defines are used as the possible return values for do_directive
320 #define NO_DIRECTIVE_FOUND 0
321 #define DIRECTIVE_FOUND 1
324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
327 #define DEADMAN_LIMIT (1 << 20)
330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
335 static const char * const conditions[] = {
336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
340 enum pp_conds {
341 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
342 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
343 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
344 c_none = -1
346 static const enum pp_conds inverse_ccs[] = {
347 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
348 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,
349 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
353 * Directive names.
355 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
356 static int is_condition(enum preproc_token arg)
358 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
361 /* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
367 enum {
368 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
369 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372 static const char * const tasm_directives[] = {
373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
377 static int StackSize = 4;
378 static char *StackPointer = "ebp";
379 static int ArgOffset = 8;
380 static int LocalOffset = 0;
382 static Context *cstk;
383 static Include *istk;
384 static IncPath *ipath = NULL;
386 static int pass; /* HACK: pass 0 = generate dependencies only */
387 static StrList **dephead, **deptail; /* Dependency list */
389 static uint64_t unique; /* unique identifier numbers */
391 static Line *predef = NULL;
392 static bool do_predef;
394 static ListGen *list;
397 * The current set of multi-line macros we have defined.
399 static struct hash_table mmacros;
402 * The current set of single-line macros we have defined.
404 static struct hash_table smacros;
407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
410 static MMacro *defining;
412 static uint64_t nested_mac_count;
413 static uint64_t nested_rep_count;
416 * The number of macro parameters to allocate space for at a time.
418 #define PARAM_DELTA 16
421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
424 static macros_t *stdmacpos;
427 * The extra standard macros that come from the object format, if
428 * any.
430 static macros_t *extrastdmac = NULL;
431 static bool any_extrastdmac;
434 * Tokens are allocated in blocks to improve speed
436 #define TOKEN_BLOCKSIZE 4096
437 static Token *freeTokens = NULL;
438 struct Blocks {
439 Blocks *next;
440 void *chunk;
443 static Blocks blocks = { NULL, NULL };
446 * Forward declarations.
448 static Token *expand_mmac_params(Token * tline);
449 static Token *expand_smacro(Token * tline);
450 static Token *expand_id(Token * tline);
451 static Context *get_ctx(const char *name, const char **namep,
452 bool all_contexts);
453 static void make_tok_num(Token * tok, int64_t val);
454 static void error(int severity, const char *fmt, ...);
455 static void error_precond(int severity, const char *fmt, ...);
456 static void *new_Block(size_t size);
457 static void delete_Blocks(void);
458 static Token *new_Token(Token * next, enum pp_token_type type,
459 const char *text, int txtlen);
460 static Token *delete_Token(Token * t);
463 * Macros for safe checking of token pointers, avoid *(NULL)
465 #define tok_type_(x,t) ((x) && (x)->type == (t))
466 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
471 * Handle TASM specific directives, which do not contain a % in
472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
476 static char *check_tasm_directive(char *line)
478 int32_t i, j, k, m, len;
479 char *p, *q, *oldline, oldchar;
481 p = nasm_skip_spaces(line);
483 /* Binary search for the directive name */
484 i = -1;
485 j = ARRAY_SIZE(tasm_directives);
486 q = nasm_skip_word(p);
487 len = q - p;
488 if (len) {
489 oldchar = p[len];
490 p[len] = 0;
491 while (j - i > 1) {
492 k = (j + i) / 2;
493 m = nasm_stricmp(p, tasm_directives[k]);
494 if (m == 0) {
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
498 p[len] = oldchar;
499 len = strlen(p);
500 oldline = line;
501 line = nasm_malloc(len + 2);
502 line[0] = '%';
503 if (k == TM_IFDIFI) {
505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
510 strcpy(line + 1, "if 0");
511 } else {
512 memcpy(line + 1, p, len + 1);
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
521 p[len] = oldchar;
523 return line;
527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
532 static char *prepreproc(char *line)
534 int lineno, fnlen;
535 char *fname, *oldline;
537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
549 if (tasm_compatible_mode)
550 return check_tasm_directive(line);
551 return line;
555 * Free a linked list of tokens.
557 static void free_tlist(Token * list)
559 while (list)
560 list = delete_Token(list);
564 * Free a linked list of lines.
566 static void free_llist(Line * list)
568 Line *l, *tmp;
569 list_for_each_safe(l, tmp, list) {
570 free_tlist(l->first);
571 nasm_free(l);
576 * Free an MMacro
578 static void free_mmacro(MMacro * m)
580 nasm_free(m->name);
581 free_tlist(m->dlist);
582 nasm_free(m->defaults);
583 free_llist(m->expansion);
584 nasm_free(m);
588 * Free all currently defined macros, and free the hash tables
590 static void free_smacro_table(struct hash_table *smt)
592 SMacro *s, *tmp;
593 const char *key;
594 struct hash_tbl_node *it = NULL;
596 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
597 nasm_free((void *)key);
598 list_for_each_safe(s, tmp, s) {
599 nasm_free(s->name);
600 free_tlist(s->expansion);
601 nasm_free(s);
604 hash_free(smt);
607 static void free_mmacro_table(struct hash_table *mmt)
609 MMacro *m, *tmp;
610 const char *key;
611 struct hash_tbl_node *it = NULL;
613 it = NULL;
614 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
615 nasm_free((void *)key);
616 list_for_each_safe(m ,tmp, m)
617 free_mmacro(m);
619 hash_free(mmt);
622 static void free_macros(void)
624 free_smacro_table(&smacros);
625 free_mmacro_table(&mmacros);
629 * Initialize the hash tables
631 static void init_macros(void)
633 hash_init(&smacros, HASH_LARGE);
634 hash_init(&mmacros, HASH_LARGE);
638 * Pop the context stack.
640 static void ctx_pop(void)
642 Context *c = cstk;
644 cstk = cstk->next;
645 free_smacro_table(&c->localmac);
646 nasm_free(c->name);
647 nasm_free(c);
651 * Search for a key in the hash index; adding it if necessary
652 * (in which case we initialize the data pointer to NULL.)
654 static void **
655 hash_findi_add(struct hash_table *hash, const char *str)
657 struct hash_insert hi;
658 void **r;
659 char *strx;
661 r = hash_findi(hash, str, &hi);
662 if (r)
663 return r;
665 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
666 return hash_add(&hi, strx, NULL);
670 * Like hash_findi, but returns the data element rather than a pointer
671 * to it. Used only when not adding a new element, hence no third
672 * argument.
674 static void *
675 hash_findix(struct hash_table *hash, const char *str)
677 void **p;
679 p = hash_findi(hash, str, NULL);
680 return p ? *p : NULL;
683 #define BUF_DELTA 512
685 * Read a line from the top file in istk, handling multiple CR/LFs
686 * at the end of the line read, and handling spurious ^Zs. Will
687 * return lines from the standard macro set if this has not already
688 * been done.
690 static char *read_line(void)
692 char *buffer, *p, *q;
693 int bufsize, continued_count;
695 if (stdmacpos) {
696 unsigned char c;
697 const unsigned char *p = stdmacpos;
698 char *ret, *q;
699 size_t len = 0;
700 while ((c = *p++)) {
701 if (c >= 0x80)
702 len += pp_directives_len[c-0x80]+1;
703 else
704 len++;
706 ret = nasm_malloc(len+1);
707 q = ret;
708 while ((c = *stdmacpos++)) {
709 if (c >= 0x80) {
710 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
711 q += pp_directives_len[c-0x80];
712 *q++ = ' ';
713 } else {
714 *q++ = c;
717 stdmacpos = p;
718 *q = '\0';
720 if (!*stdmacpos) {
721 /* This was the last of the standard macro chain... */
722 stdmacpos = NULL;
723 if (any_extrastdmac) {
724 stdmacpos = extrastdmac;
725 any_extrastdmac = false;
726 } else if (do_predef) {
727 Line *pd, *l;
728 Token *head, **tail, *t;
731 * Nasty hack: here we push the contents of
732 * `predef' on to the top-level expansion stack,
733 * since this is the most convenient way to
734 * implement the pre-include and pre-define
735 * features.
737 list_for_each(pd, predef) {
738 head = NULL;
739 tail = &head;
740 list_for_each(t, pd->first) {
741 *tail = new_Token(NULL, t->type, t->text, 0);
742 tail = &(*tail)->next;
744 l = nasm_malloc(sizeof(Line));
745 l->next = istk->expansion;
746 l->first = head;
747 l->finishes = NULL;
748 istk->expansion = l;
750 do_predef = false;
753 return ret;
756 bufsize = BUF_DELTA;
757 buffer = nasm_malloc(BUF_DELTA);
758 p = buffer;
759 continued_count = 0;
760 while (1) {
761 q = fgets(p, bufsize - (p - buffer), istk->fp);
762 if (!q)
763 break;
764 p += strlen(p);
765 if (p > buffer && p[-1] == '\n') {
767 * Convert backslash-CRLF line continuation sequences into
768 * nothing at all (for DOS and Windows)
770 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
771 p -= 3;
772 *p = 0;
773 continued_count++;
776 * Also convert backslash-LF line continuation sequences into
777 * nothing at all (for Unix)
779 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
780 p -= 2;
781 *p = 0;
782 continued_count++;
783 } else {
784 break;
787 if (p - buffer > bufsize - 10) {
788 int32_t offset = p - buffer;
789 bufsize += BUF_DELTA;
790 buffer = nasm_realloc(buffer, bufsize);
791 p = buffer + offset; /* prevent stale-pointer problems */
795 if (!q && p == buffer) {
796 nasm_free(buffer);
797 return NULL;
800 src_set_linnum(src_get_linnum() + istk->lineinc +
801 (continued_count * istk->lineinc));
804 * Play safe: remove CRs as well as LFs, if any of either are
805 * present at the end of the line.
807 while (--p >= buffer && (*p == '\n' || *p == '\r'))
808 *p = '\0';
811 * Handle spurious ^Z, which may be inserted into source files
812 * by some file transfer utilities.
814 buffer[strcspn(buffer, "\032")] = '\0';
816 list->line(LIST_READ, buffer);
818 return buffer;
822 * Tokenize a line of text. This is a very simple process since we
823 * don't need to parse the value out of e.g. numeric tokens: we
824 * simply split one string into many.
826 static Token *tokenize(char *line)
828 char c, *p = line;
829 enum pp_token_type type;
830 Token *list = NULL;
831 Token *t, **tail = &list;
833 while (*line) {
834 p = line;
835 if (*p == '%') {
836 p++;
837 if (*p == '+' && !nasm_isdigit(p[1])) {
838 p++;
839 type = TOK_PASTE;
840 } else if (nasm_isdigit(*p) ||
841 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
842 do {
843 p++;
845 while (nasm_isdigit(*p));
846 type = TOK_PREPROC_ID;
847 } else if (*p == '{') {
848 p++;
849 while (*p && *p != '}') {
850 p[-1] = *p;
851 p++;
853 p[-1] = '\0';
854 if (*p)
855 p++;
856 type = TOK_PREPROC_ID;
857 } else if (*p == '[') {
858 int lvl = 1;
859 line += 2; /* Skip the leading %[ */
860 p++;
861 while (lvl && (c = *p++)) {
862 switch (c) {
863 case ']':
864 lvl--;
865 break;
866 case '%':
867 if (*p == '[')
868 lvl++;
869 break;
870 case '\'':
871 case '\"':
872 case '`':
873 p = nasm_skip_string(p)+1;
874 break;
875 default:
876 break;
879 p--;
880 if (*p)
881 *p++ = '\0';
882 if (lvl)
883 error(ERR_NONFATAL, "unterminated %[ construct");
884 type = TOK_INDIRECT;
885 } else if (*p == '?') {
886 type = TOK_PREPROC_Q; /* %? */
887 p++;
888 if (*p == '?') {
889 type = TOK_PREPROC_QQ; /* %?? */
890 p++;
892 } else if (isidchar(*p) ||
893 ((*p == '!' || *p == '%' || *p == '$') &&
894 isidchar(p[1]))) {
895 do {
896 p++;
898 while (isidchar(*p));
899 type = TOK_PREPROC_ID;
900 } else {
901 type = TOK_OTHER;
902 if (*p == '%')
903 p++;
905 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
906 type = TOK_ID;
907 p++;
908 while (*p && isidchar(*p))
909 p++;
910 } else if (*p == '\'' || *p == '"' || *p == '`') {
912 * A string token.
914 type = TOK_STRING;
915 p = nasm_skip_string(p);
917 if (*p) {
918 p++;
919 } else {
920 error(ERR_WARNING|ERR_PASS1, "unterminated string");
921 /* Handling unterminated strings by UNV */
922 /* type = -1; */
924 } else if (p[0] == '$' && p[1] == '$') {
925 type = TOK_OTHER; /* TOKEN_BASE */
926 p += 2;
927 } else if (isnumstart(*p)) {
928 bool is_hex = false;
929 bool is_float = false;
930 bool has_e = false;
931 char c, *r;
934 * A numeric token.
937 if (*p == '$') {
938 p++;
939 is_hex = true;
942 for (;;) {
943 c = *p++;
945 if (!is_hex && (c == 'e' || c == 'E')) {
946 has_e = true;
947 if (*p == '+' || *p == '-') {
949 * e can only be followed by +/- if it is either a
950 * prefixed hex number or a floating-point number
952 p++;
953 is_float = true;
955 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
956 is_hex = true;
957 } else if (c == 'P' || c == 'p') {
958 is_float = true;
959 if (*p == '+' || *p == '-')
960 p++;
961 } else if (isnumchar(c) || c == '_')
962 ; /* just advance */
963 else if (c == '.') {
965 * we need to deal with consequences of the legacy
966 * parser, like "1.nolist" being two tokens
967 * (TOK_NUMBER, TOK_ID) here; at least give it
968 * a shot for now. In the future, we probably need
969 * a flex-based scanner with proper pattern matching
970 * to do it as well as it can be done. Nothing in
971 * the world is going to help the person who wants
972 * 0x123.p16 interpreted as two tokens, though.
974 r = p;
975 while (*r == '_')
976 r++;
978 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
979 (!is_hex && (*r == 'e' || *r == 'E')) ||
980 (*r == 'p' || *r == 'P')) {
981 p = r;
982 is_float = true;
983 } else
984 break; /* Terminate the token */
985 } else
986 break;
988 p--; /* Point to first character beyond number */
990 if (p == line+1 && *line == '$') {
991 type = TOK_OTHER; /* TOKEN_HERE */
992 } else {
993 if (has_e && !is_hex) {
994 /* 1e13 is floating-point, but 1e13h is not */
995 is_float = true;
998 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1000 } else if (nasm_isspace(*p)) {
1001 type = TOK_WHITESPACE;
1002 p = nasm_skip_spaces(p);
1004 * Whitespace just before end-of-line is discarded by
1005 * pretending it's a comment; whitespace just before a
1006 * comment gets lumped into the comment.
1008 if (!*p || *p == ';') {
1009 type = TOK_COMMENT;
1010 while (*p)
1011 p++;
1013 } else if (*p == ';') {
1014 type = TOK_COMMENT;
1015 while (*p)
1016 p++;
1017 } else {
1019 * Anything else is an operator of some kind. We check
1020 * for all the double-character operators (>>, <<, //,
1021 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1022 * else is a single-character operator.
1024 type = TOK_OTHER;
1025 if ((p[0] == '>' && p[1] == '>') ||
1026 (p[0] == '<' && p[1] == '<') ||
1027 (p[0] == '/' && p[1] == '/') ||
1028 (p[0] == '<' && p[1] == '=') ||
1029 (p[0] == '>' && p[1] == '=') ||
1030 (p[0] == '=' && p[1] == '=') ||
1031 (p[0] == '!' && p[1] == '=') ||
1032 (p[0] == '<' && p[1] == '>') ||
1033 (p[0] == '&' && p[1] == '&') ||
1034 (p[0] == '|' && p[1] == '|') ||
1035 (p[0] == '^' && p[1] == '^')) {
1036 p++;
1038 p++;
1041 /* Handling unterminated string by UNV */
1042 /*if (type == -1)
1044 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1045 t->text[p-line] = *line;
1046 tail = &t->next;
1048 else */
1049 if (type != TOK_COMMENT) {
1050 *tail = t = new_Token(NULL, type, line, p - line);
1051 tail = &t->next;
1053 line = p;
1055 return list;
1059 * this function allocates a new managed block of memory and
1060 * returns a pointer to the block. The managed blocks are
1061 * deleted only all at once by the delete_Blocks function.
1063 static void *new_Block(size_t size)
1065 Blocks *b = &blocks;
1067 /* first, get to the end of the linked list */
1068 while (b->next)
1069 b = b->next;
1070 /* now allocate the requested chunk */
1071 b->chunk = nasm_malloc(size);
1073 /* now allocate a new block for the next request */
1074 b->next = nasm_malloc(sizeof(Blocks));
1075 /* and initialize the contents of the new block */
1076 b->next->next = NULL;
1077 b->next->chunk = NULL;
1078 return b->chunk;
1082 * this function deletes all managed blocks of memory
1084 static void delete_Blocks(void)
1086 Blocks *a, *b = &blocks;
1089 * keep in mind that the first block, pointed to by blocks
1090 * is a static and not dynamically allocated, so we don't
1091 * free it.
1093 while (b) {
1094 if (b->chunk)
1095 nasm_free(b->chunk);
1096 a = b;
1097 b = b->next;
1098 if (a != &blocks)
1099 nasm_free(a);
1104 * this function creates a new Token and passes a pointer to it
1105 * back to the caller. It sets the type and text elements, and
1106 * also the a.mac and next elements to NULL.
1108 static Token *new_Token(Token * next, enum pp_token_type type,
1109 const char *text, int txtlen)
1111 Token *t;
1112 int i;
1114 if (!freeTokens) {
1115 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1116 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1117 freeTokens[i].next = &freeTokens[i + 1];
1118 freeTokens[i].next = NULL;
1120 t = freeTokens;
1121 freeTokens = t->next;
1122 t->next = next;
1123 t->a.mac = NULL;
1124 t->type = type;
1125 if (type == TOK_WHITESPACE || !text) {
1126 t->text = NULL;
1127 } else {
1128 if (txtlen == 0)
1129 txtlen = strlen(text);
1130 t->text = nasm_malloc(txtlen+1);
1131 memcpy(t->text, text, txtlen);
1132 t->text[txtlen] = '\0';
1134 return t;
1137 static Token *delete_Token(Token * t)
1139 Token *next = t->next;
1140 nasm_free(t->text);
1141 t->next = freeTokens;
1142 freeTokens = t;
1143 return next;
1147 * Convert a line of tokens back into text.
1148 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1149 * will be transformed into ..@ctxnum.xxx
1151 static char *detoken(Token * tlist, bool expand_locals)
1153 Token *t;
1154 char *line, *p;
1155 const char *q;
1156 int len = 0;
1158 list_for_each(t, tlist) {
1159 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1160 char *p = getenv(t->text + 2);
1161 nasm_free(t->text);
1162 if (p)
1163 t->text = nasm_strdup(p);
1164 else
1165 t->text = NULL;
1167 /* Expand local macros here and not during preprocessing */
1168 if (expand_locals &&
1169 t->type == TOK_PREPROC_ID && t->text &&
1170 t->text[0] == '%' && t->text[1] == '$') {
1171 const char *q;
1172 char *p;
1173 Context *ctx = get_ctx(t->text, &q, false);
1174 if (ctx) {
1175 char buffer[40];
1176 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1177 p = nasm_strcat(buffer, q);
1178 nasm_free(t->text);
1179 t->text = p;
1182 if (t->type == TOK_WHITESPACE)
1183 len++;
1184 else if (t->text)
1185 len += strlen(t->text);
1188 p = line = nasm_malloc(len + 1);
1190 list_for_each(t, tlist) {
1191 if (t->type == TOK_WHITESPACE) {
1192 *p++ = ' ';
1193 } else if (t->text) {
1194 q = t->text;
1195 while (*q)
1196 *p++ = *q++;
1199 *p = '\0';
1201 return line;
1205 * A scanner, suitable for use by the expression evaluator, which
1206 * operates on a line of Tokens. Expects a pointer to a pointer to
1207 * the first token in the line to be passed in as its private_data
1208 * field.
1210 * FIX: This really needs to be unified with stdscan.
1212 static int ppscan(void *private_data, struct tokenval *tokval)
1214 Token **tlineptr = private_data;
1215 Token *tline;
1216 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1218 do {
1219 tline = *tlineptr;
1220 *tlineptr = tline ? tline->next : NULL;
1221 } while (tline && (tline->type == TOK_WHITESPACE ||
1222 tline->type == TOK_COMMENT));
1224 if (!tline)
1225 return tokval->t_type = TOKEN_EOS;
1227 tokval->t_charptr = tline->text;
1229 if (tline->text[0] == '$' && !tline->text[1])
1230 return tokval->t_type = TOKEN_HERE;
1231 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1232 return tokval->t_type = TOKEN_BASE;
1234 if (tline->type == TOK_ID) {
1235 p = tokval->t_charptr = tline->text;
1236 if (p[0] == '$') {
1237 tokval->t_charptr++;
1238 return tokval->t_type = TOKEN_ID;
1241 for (r = p, s = ourcopy; *r; r++) {
1242 if (r >= p+MAX_KEYWORD)
1243 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1244 *s++ = nasm_tolower(*r);
1246 *s = '\0';
1247 /* right, so we have an identifier sitting in temp storage. now,
1248 * is it actually a register or instruction name, or what? */
1249 return nasm_token_hash(ourcopy, tokval);
1252 if (tline->type == TOK_NUMBER) {
1253 bool rn_error;
1254 tokval->t_integer = readnum(tline->text, &rn_error);
1255 tokval->t_charptr = tline->text;
1256 if (rn_error)
1257 return tokval->t_type = TOKEN_ERRNUM;
1258 else
1259 return tokval->t_type = TOKEN_NUM;
1262 if (tline->type == TOK_FLOAT) {
1263 return tokval->t_type = TOKEN_FLOAT;
1266 if (tline->type == TOK_STRING) {
1267 char bq, *ep;
1269 bq = tline->text[0];
1270 tokval->t_charptr = tline->text;
1271 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1273 if (ep[0] != bq || ep[1] != '\0')
1274 return tokval->t_type = TOKEN_ERRSTR;
1275 else
1276 return tokval->t_type = TOKEN_STR;
1279 if (tline->type == TOK_OTHER) {
1280 if (!strcmp(tline->text, "<<"))
1281 return tokval->t_type = TOKEN_SHL;
1282 if (!strcmp(tline->text, ">>"))
1283 return tokval->t_type = TOKEN_SHR;
1284 if (!strcmp(tline->text, "//"))
1285 return tokval->t_type = TOKEN_SDIV;
1286 if (!strcmp(tline->text, "%%"))
1287 return tokval->t_type = TOKEN_SMOD;
1288 if (!strcmp(tline->text, "=="))
1289 return tokval->t_type = TOKEN_EQ;
1290 if (!strcmp(tline->text, "<>"))
1291 return tokval->t_type = TOKEN_NE;
1292 if (!strcmp(tline->text, "!="))
1293 return tokval->t_type = TOKEN_NE;
1294 if (!strcmp(tline->text, "<="))
1295 return tokval->t_type = TOKEN_LE;
1296 if (!strcmp(tline->text, ">="))
1297 return tokval->t_type = TOKEN_GE;
1298 if (!strcmp(tline->text, "&&"))
1299 return tokval->t_type = TOKEN_DBL_AND;
1300 if (!strcmp(tline->text, "^^"))
1301 return tokval->t_type = TOKEN_DBL_XOR;
1302 if (!strcmp(tline->text, "||"))
1303 return tokval->t_type = TOKEN_DBL_OR;
1307 * We have no other options: just return the first character of
1308 * the token text.
1310 return tokval->t_type = tline->text[0];
1314 * Compare a string to the name of an existing macro; this is a
1315 * simple wrapper which calls either strcmp or nasm_stricmp
1316 * depending on the value of the `casesense' parameter.
1318 static int mstrcmp(const char *p, const char *q, bool casesense)
1320 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1324 * Compare a string to the name of an existing macro; this is a
1325 * simple wrapper which calls either strcmp or nasm_stricmp
1326 * depending on the value of the `casesense' parameter.
1328 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1330 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1334 * Return the Context structure associated with a %$ token. Return
1335 * NULL, having _already_ reported an error condition, if the
1336 * context stack isn't deep enough for the supplied number of $
1337 * signs.
1338 * If all_contexts == true, contexts that enclose current are
1339 * also scanned for such smacro, until it is found; if not -
1340 * only the context that directly results from the number of $'s
1341 * in variable's name.
1343 * If "namep" is non-NULL, set it to the pointer to the macro name
1344 * tail, i.e. the part beyond %$...
1346 static Context *get_ctx(const char *name, const char **namep,
1347 bool all_contexts)
1349 Context *ctx;
1350 SMacro *m;
1351 int i;
1353 if (namep)
1354 *namep = name;
1356 if (!name || name[0] != '%' || name[1] != '$')
1357 return NULL;
1359 if (!cstk) {
1360 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1361 return NULL;
1364 name += 2;
1365 ctx = cstk;
1366 i = 0;
1367 while (ctx && *name == '$') {
1368 name++;
1369 i++;
1370 ctx = ctx->next;
1372 if (!ctx) {
1373 error(ERR_NONFATAL, "`%s': context stack is only"
1374 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1375 return NULL;
1378 if (namep)
1379 *namep = name;
1381 if (!all_contexts)
1382 return ctx;
1384 do {
1385 /* Search for this smacro in found context */
1386 m = hash_findix(&ctx->localmac, name);
1387 while (m) {
1388 if (!mstrcmp(m->name, name, m->casesense))
1389 return ctx;
1390 m = m->next;
1392 ctx = ctx->next;
1394 while (ctx);
1395 return NULL;
1399 * Check to see if a file is already in a string list
1401 static bool in_list(const StrList *list, const char *str)
1403 while (list) {
1404 if (!strcmp(list->str, str))
1405 return true;
1406 list = list->next;
1408 return false;
1412 * Open an include file. This routine must always return a valid
1413 * file pointer if it returns - it's responsible for throwing an
1414 * ERR_FATAL and bombing out completely if not. It should also try
1415 * the include path one by one until it finds the file or reaches
1416 * the end of the path.
1418 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1419 bool missing_ok)
1421 FILE *fp;
1422 char *prefix = "";
1423 IncPath *ip = ipath;
1424 int len = strlen(file);
1425 size_t prefix_len = 0;
1426 StrList *sl;
1428 while (1) {
1429 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1430 memcpy(sl->str, prefix, prefix_len);
1431 memcpy(sl->str+prefix_len, file, len+1);
1432 fp = fopen(sl->str, "r");
1433 if (fp && dhead && !in_list(*dhead, sl->str)) {
1434 sl->next = NULL;
1435 **dtail = sl;
1436 *dtail = &sl->next;
1437 } else {
1438 nasm_free(sl);
1440 if (fp)
1441 return fp;
1442 if (!ip) {
1443 if (!missing_ok)
1444 break;
1445 prefix = NULL;
1446 } else {
1447 prefix = ip->path;
1448 ip = ip->next;
1450 if (prefix) {
1451 prefix_len = strlen(prefix);
1452 } else {
1453 /* -MG given and file not found */
1454 if (dhead && !in_list(*dhead, file)) {
1455 sl = nasm_malloc(len+1+sizeof sl->next);
1456 sl->next = NULL;
1457 strcpy(sl->str, file);
1458 **dtail = sl;
1459 *dtail = &sl->next;
1461 return NULL;
1465 error(ERR_FATAL, "unable to open include file `%s'", file);
1466 return NULL;
1470 * Determine if we should warn on defining a single-line macro of
1471 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1472 * return true if _any_ single-line macro of that name is defined.
1473 * Otherwise, will return true if a single-line macro with either
1474 * `nparam' or no parameters is defined.
1476 * If a macro with precisely the right number of parameters is
1477 * defined, or nparam is -1, the address of the definition structure
1478 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1479 * is NULL, no action will be taken regarding its contents, and no
1480 * error will occur.
1482 * Note that this is also called with nparam zero to resolve
1483 * `ifdef'.
1485 * If you already know which context macro belongs to, you can pass
1486 * the context pointer as first parameter; if you won't but name begins
1487 * with %$ the context will be automatically computed. If all_contexts
1488 * is true, macro will be searched in outer contexts as well.
1490 static bool
1491 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1492 bool nocase)
1494 struct hash_table *smtbl;
1495 SMacro *m;
1497 if (ctx) {
1498 smtbl = &ctx->localmac;
1499 } else if (name[0] == '%' && name[1] == '$') {
1500 if (cstk)
1501 ctx = get_ctx(name, &name, false);
1502 if (!ctx)
1503 return false; /* got to return _something_ */
1504 smtbl = &ctx->localmac;
1505 } else {
1506 smtbl = &smacros;
1508 m = (SMacro *) hash_findix(smtbl, name);
1510 while (m) {
1511 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1512 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1513 if (defn) {
1514 if (nparam == (int) m->nparam || nparam == -1)
1515 *defn = m;
1516 else
1517 *defn = NULL;
1519 return true;
1521 m = m->next;
1524 return false;
1528 * Count and mark off the parameters in a multi-line macro call.
1529 * This is called both from within the multi-line macro expansion
1530 * code, and also to mark off the default parameters when provided
1531 * in a %macro definition line.
1533 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1535 int paramsize, brace;
1537 *nparam = paramsize = 0;
1538 *params = NULL;
1539 while (t) {
1540 /* +1: we need space for the final NULL */
1541 if (*nparam+1 >= paramsize) {
1542 paramsize += PARAM_DELTA;
1543 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1545 skip_white_(t);
1546 brace = false;
1547 if (tok_is_(t, "{"))
1548 brace = true;
1549 (*params)[(*nparam)++] = t;
1550 while (tok_isnt_(t, brace ? "}" : ","))
1551 t = t->next;
1552 if (t) { /* got a comma/brace */
1553 t = t->next;
1554 if (brace) {
1556 * Now we've found the closing brace, look further
1557 * for the comma.
1559 skip_white_(t);
1560 if (tok_isnt_(t, ",")) {
1561 error(ERR_NONFATAL,
1562 "braces do not enclose all of macro parameter");
1563 while (tok_isnt_(t, ","))
1564 t = t->next;
1566 if (t)
1567 t = t->next; /* eat the comma */
1574 * Determine whether one of the various `if' conditions is true or
1575 * not.
1577 * We must free the tline we get passed.
1579 static bool if_condition(Token * tline, enum preproc_token ct)
1581 enum pp_conditional i = PP_COND(ct);
1582 bool j;
1583 Token *t, *tt, **tptr, *origline;
1584 struct tokenval tokval;
1585 expr *evalresult;
1586 enum pp_token_type needtype;
1588 origline = tline;
1590 switch (i) {
1591 case PPC_IFCTX:
1592 j = false; /* have we matched yet? */
1593 while (true) {
1594 skip_white_(tline);
1595 if (!tline)
1596 break;
1597 if (tline->type != TOK_ID) {
1598 error(ERR_NONFATAL,
1599 "`%s' expects context identifiers", pp_directives[ct]);
1600 free_tlist(origline);
1601 return -1;
1603 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1604 j = true;
1605 tline = tline->next;
1607 break;
1609 case PPC_IFDEF:
1610 j = false; /* have we matched yet? */
1611 while (tline) {
1612 skip_white_(tline);
1613 if (!tline || (tline->type != TOK_ID &&
1614 (tline->type != TOK_PREPROC_ID ||
1615 tline->text[1] != '$'))) {
1616 error(ERR_NONFATAL,
1617 "`%s' expects macro identifiers", pp_directives[ct]);
1618 goto fail;
1620 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1621 j = true;
1622 tline = tline->next;
1624 break;
1626 case PPC_IFIDN:
1627 case PPC_IFIDNI:
1628 tline = expand_smacro(tline);
1629 t = tt = tline;
1630 while (tok_isnt_(tt, ","))
1631 tt = tt->next;
1632 if (!tt) {
1633 error(ERR_NONFATAL,
1634 "`%s' expects two comma-separated arguments",
1635 pp_directives[ct]);
1636 goto fail;
1638 tt = tt->next;
1639 j = true; /* assume equality unless proved not */
1640 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1641 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1642 error(ERR_NONFATAL, "`%s': more than one comma on line",
1643 pp_directives[ct]);
1644 goto fail;
1646 if (t->type == TOK_WHITESPACE) {
1647 t = t->next;
1648 continue;
1650 if (tt->type == TOK_WHITESPACE) {
1651 tt = tt->next;
1652 continue;
1654 if (tt->type != t->type) {
1655 j = false; /* found mismatching tokens */
1656 break;
1658 /* When comparing strings, need to unquote them first */
1659 if (t->type == TOK_STRING) {
1660 size_t l1 = nasm_unquote(t->text, NULL);
1661 size_t l2 = nasm_unquote(tt->text, NULL);
1663 if (l1 != l2) {
1664 j = false;
1665 break;
1667 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1668 j = false;
1669 break;
1671 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1672 j = false; /* found mismatching tokens */
1673 break;
1676 t = t->next;
1677 tt = tt->next;
1679 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1680 j = false; /* trailing gunk on one end or other */
1681 break;
1683 case PPC_IFMACRO:
1685 bool found = false;
1686 MMacro searching, *mmac;
1688 skip_white_(tline);
1689 tline = expand_id(tline);
1690 if (!tok_type_(tline, TOK_ID)) {
1691 error(ERR_NONFATAL,
1692 "`%s' expects a macro name", pp_directives[ct]);
1693 goto fail;
1695 searching.name = nasm_strdup(tline->text);
1696 searching.casesense = true;
1697 searching.plus = false;
1698 searching.nolist = false;
1699 searching.in_progress = 0;
1700 searching.max_depth = 0;
1701 searching.rep_nest = NULL;
1702 searching.nparam_min = 0;
1703 searching.nparam_max = INT_MAX;
1704 tline = expand_smacro(tline->next);
1705 skip_white_(tline);
1706 if (!tline) {
1707 } else if (!tok_type_(tline, TOK_NUMBER)) {
1708 error(ERR_NONFATAL,
1709 "`%s' expects a parameter count or nothing",
1710 pp_directives[ct]);
1711 } else {
1712 searching.nparam_min = searching.nparam_max =
1713 readnum(tline->text, &j);
1714 if (j)
1715 error(ERR_NONFATAL,
1716 "unable to parse parameter count `%s'",
1717 tline->text);
1719 if (tline && tok_is_(tline->next, "-")) {
1720 tline = tline->next->next;
1721 if (tok_is_(tline, "*"))
1722 searching.nparam_max = INT_MAX;
1723 else if (!tok_type_(tline, TOK_NUMBER))
1724 error(ERR_NONFATAL,
1725 "`%s' expects a parameter count after `-'",
1726 pp_directives[ct]);
1727 else {
1728 searching.nparam_max = readnum(tline->text, &j);
1729 if (j)
1730 error(ERR_NONFATAL,
1731 "unable to parse parameter count `%s'",
1732 tline->text);
1733 if (searching.nparam_min > searching.nparam_max)
1734 error(ERR_NONFATAL,
1735 "minimum parameter count exceeds maximum");
1738 if (tline && tok_is_(tline->next, "+")) {
1739 tline = tline->next;
1740 searching.plus = true;
1742 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1743 while (mmac) {
1744 if (!strcmp(mmac->name, searching.name) &&
1745 (mmac->nparam_min <= searching.nparam_max
1746 || searching.plus)
1747 && (searching.nparam_min <= mmac->nparam_max
1748 || mmac->plus)) {
1749 found = true;
1750 break;
1752 mmac = mmac->next;
1754 if (tline && tline->next)
1755 error(ERR_WARNING|ERR_PASS1,
1756 "trailing garbage after %%ifmacro ignored");
1757 nasm_free(searching.name);
1758 j = found;
1759 break;
1762 case PPC_IFID:
1763 needtype = TOK_ID;
1764 goto iftype;
1765 case PPC_IFNUM:
1766 needtype = TOK_NUMBER;
1767 goto iftype;
1768 case PPC_IFSTR:
1769 needtype = TOK_STRING;
1770 goto iftype;
1772 iftype:
1773 t = tline = expand_smacro(tline);
1775 while (tok_type_(t, TOK_WHITESPACE) ||
1776 (needtype == TOK_NUMBER &&
1777 tok_type_(t, TOK_OTHER) &&
1778 (t->text[0] == '-' || t->text[0] == '+') &&
1779 !t->text[1]))
1780 t = t->next;
1782 j = tok_type_(t, needtype);
1783 break;
1785 case PPC_IFTOKEN:
1786 t = tline = expand_smacro(tline);
1787 while (tok_type_(t, TOK_WHITESPACE))
1788 t = t->next;
1790 j = false;
1791 if (t) {
1792 t = t->next; /* Skip the actual token */
1793 while (tok_type_(t, TOK_WHITESPACE))
1794 t = t->next;
1795 j = !t; /* Should be nothing left */
1797 break;
1799 case PPC_IFEMPTY:
1800 t = tline = expand_smacro(tline);
1801 while (tok_type_(t, TOK_WHITESPACE))
1802 t = t->next;
1804 j = !t; /* Should be empty */
1805 break;
1807 case PPC_IF:
1808 t = tline = expand_smacro(tline);
1809 tptr = &t;
1810 tokval.t_type = TOKEN_INVALID;
1811 evalresult = evaluate(ppscan, tptr, &tokval,
1812 NULL, pass | CRITICAL, error, NULL);
1813 if (!evalresult)
1814 return -1;
1815 if (tokval.t_type)
1816 error(ERR_WARNING|ERR_PASS1,
1817 "trailing garbage after expression ignored");
1818 if (!is_simple(evalresult)) {
1819 error(ERR_NONFATAL,
1820 "non-constant value given to `%s'", pp_directives[ct]);
1821 goto fail;
1823 j = reloc_value(evalresult) != 0;
1824 break;
1826 default:
1827 error(ERR_FATAL,
1828 "preprocessor directive `%s' not yet implemented",
1829 pp_directives[ct]);
1830 goto fail;
1833 free_tlist(origline);
1834 return j ^ PP_NEGATIVE(ct);
1836 fail:
1837 free_tlist(origline);
1838 return -1;
1842 * Common code for defining an smacro
1844 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1845 int nparam, Token *expansion)
1847 SMacro *smac, **smhead;
1848 struct hash_table *smtbl;
1850 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1851 if (!smac) {
1852 error(ERR_WARNING|ERR_PASS1,
1853 "single-line macro `%s' defined both with and"
1854 " without parameters", mname);
1856 * Some instances of the old code considered this a failure,
1857 * some others didn't. What is the right thing to do here?
1859 free_tlist(expansion);
1860 return false; /* Failure */
1861 } else {
1863 * We're redefining, so we have to take over an
1864 * existing SMacro structure. This means freeing
1865 * what was already in it.
1867 nasm_free(smac->name);
1868 free_tlist(smac->expansion);
1870 } else {
1871 smtbl = ctx ? &ctx->localmac : &smacros;
1872 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1873 smac = nasm_malloc(sizeof(SMacro));
1874 smac->next = *smhead;
1875 *smhead = smac;
1877 smac->name = nasm_strdup(mname);
1878 smac->casesense = casesense;
1879 smac->nparam = nparam;
1880 smac->expansion = expansion;
1881 smac->in_progress = false;
1882 return true; /* Success */
1886 * Undefine an smacro
1888 static void undef_smacro(Context *ctx, const char *mname)
1890 SMacro **smhead, *s, **sp;
1891 struct hash_table *smtbl;
1893 smtbl = ctx ? &ctx->localmac : &smacros;
1894 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1896 if (smhead) {
1898 * We now have a macro name... go hunt for it.
1900 sp = smhead;
1901 while ((s = *sp) != NULL) {
1902 if (!mstrcmp(s->name, mname, s->casesense)) {
1903 *sp = s->next;
1904 nasm_free(s->name);
1905 free_tlist(s->expansion);
1906 nasm_free(s);
1907 } else {
1908 sp = &s->next;
1915 * Parse a mmacro specification.
1917 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1919 bool err;
1921 tline = tline->next;
1922 skip_white_(tline);
1923 tline = expand_id(tline);
1924 if (!tok_type_(tline, TOK_ID)) {
1925 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1926 return false;
1929 def->prev = NULL;
1930 def->name = nasm_strdup(tline->text);
1931 def->plus = false;
1932 def->nolist = false;
1933 def->in_progress = 0;
1934 def->rep_nest = NULL;
1935 def->nparam_min = 0;
1936 def->nparam_max = 0;
1938 tline = expand_smacro(tline->next);
1939 skip_white_(tline);
1940 if (!tok_type_(tline, TOK_NUMBER)) {
1941 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1942 } else {
1943 def->nparam_min = def->nparam_max =
1944 readnum(tline->text, &err);
1945 if (err)
1946 error(ERR_NONFATAL,
1947 "unable to parse parameter count `%s'", tline->text);
1949 if (tline && tok_is_(tline->next, "-")) {
1950 tline = tline->next->next;
1951 if (tok_is_(tline, "*")) {
1952 def->nparam_max = INT_MAX;
1953 } else if (!tok_type_(tline, TOK_NUMBER)) {
1954 error(ERR_NONFATAL,
1955 "`%s' expects a parameter count after `-'", directive);
1956 } else {
1957 def->nparam_max = readnum(tline->text, &err);
1958 if (err) {
1959 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1960 tline->text);
1962 if (def->nparam_min > def->nparam_max) {
1963 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1967 if (tline && tok_is_(tline->next, "+")) {
1968 tline = tline->next;
1969 def->plus = true;
1971 if (tline && tok_type_(tline->next, TOK_ID) &&
1972 !nasm_stricmp(tline->next->text, ".nolist")) {
1973 tline = tline->next;
1974 def->nolist = true;
1978 * Handle default parameters.
1980 if (tline && tline->next) {
1981 def->dlist = tline->next;
1982 tline->next = NULL;
1983 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1984 } else {
1985 def->dlist = NULL;
1986 def->defaults = NULL;
1988 def->expansion = NULL;
1990 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
1991 !def->plus)
1992 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1993 "too many default macro parameters");
1995 return true;
2000 * Decode a size directive
2002 static int parse_size(const char *str) {
2003 static const char *size_names[] =
2004 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2005 static const int sizes[] =
2006 { 0, 1, 4, 16, 8, 10, 2, 32 };
2008 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2012 * nasm_unquote with error if the string contains NUL characters.
2013 * If the string contains NUL characters, issue an error and return
2014 * the C len, i.e. truncate at the NUL.
2016 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2018 size_t len = nasm_unquote(qstr, NULL);
2019 size_t clen = strlen(qstr);
2021 if (len != clen)
2022 error(ERR_NONFATAL, "NUL character in `%s' directive",
2023 pp_directives[directive]);
2025 return clen;
2029 * find and process preprocessor directive in passed line
2030 * Find out if a line contains a preprocessor directive, and deal
2031 * with it if so.
2033 * If a directive _is_ found, it is the responsibility of this routine
2034 * (and not the caller) to free_tlist() the line.
2036 * @param tline a pointer to the current tokeninzed line linked list
2037 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2040 static int do_directive(Token * tline)
2042 enum preproc_token i;
2043 int j;
2044 bool err;
2045 int nparam;
2046 bool nolist;
2047 bool casesense;
2048 int k, m;
2049 int offset;
2050 char *p, *pp;
2051 const char *mname;
2052 Include *inc;
2053 Context *ctx;
2054 Cond *cond;
2055 MMacro *mmac, **mmhead;
2056 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2057 Line *l;
2058 struct tokenval tokval;
2059 expr *evalresult;
2060 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2061 int64_t count;
2062 size_t len;
2063 int severity;
2065 origline = tline;
2067 skip_white_(tline);
2068 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2069 (tline->text[1] == '%' || tline->text[1] == '$'
2070 || tline->text[1] == '!'))
2071 return NO_DIRECTIVE_FOUND;
2073 i = pp_token_hash(tline->text);
2076 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2077 * since they are known to be buggy at moment, we need to fix them
2078 * in future release (2.09-2.10)
2080 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2081 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2082 tline->text);
2083 return NO_DIRECTIVE_FOUND;
2087 * If we're in a non-emitting branch of a condition construct,
2088 * or walking to the end of an already terminated %rep block,
2089 * we should ignore all directives except for condition
2090 * directives.
2092 if (((istk->conds && !emitting(istk->conds->state)) ||
2093 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2094 return NO_DIRECTIVE_FOUND;
2098 * If we're defining a macro or reading a %rep block, we should
2099 * ignore all directives except for %macro/%imacro (which nest),
2100 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2101 * If we're in a %rep block, another %rep nests, so should be let through.
2103 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2104 i != PP_RMACRO && i != PP_IRMACRO &&
2105 i != PP_ENDMACRO && i != PP_ENDM &&
2106 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2107 return NO_DIRECTIVE_FOUND;
2110 if (defining) {
2111 if (i == PP_MACRO || i == PP_IMACRO ||
2112 i == PP_RMACRO || i == PP_IRMACRO) {
2113 nested_mac_count++;
2114 return NO_DIRECTIVE_FOUND;
2115 } else if (nested_mac_count > 0) {
2116 if (i == PP_ENDMACRO) {
2117 nested_mac_count--;
2118 return NO_DIRECTIVE_FOUND;
2121 if (!defining->name) {
2122 if (i == PP_REP) {
2123 nested_rep_count++;
2124 return NO_DIRECTIVE_FOUND;
2125 } else if (nested_rep_count > 0) {
2126 if (i == PP_ENDREP) {
2127 nested_rep_count--;
2128 return NO_DIRECTIVE_FOUND;
2134 switch (i) {
2135 case PP_INVALID:
2136 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2137 tline->text);
2138 return NO_DIRECTIVE_FOUND; /* didn't get it */
2140 case PP_STACKSIZE:
2141 /* Directive to tell NASM what the default stack size is. The
2142 * default is for a 16-bit stack, and this can be overriden with
2143 * %stacksize large.
2145 tline = tline->next;
2146 if (tline && tline->type == TOK_WHITESPACE)
2147 tline = tline->next;
2148 if (!tline || tline->type != TOK_ID) {
2149 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2150 free_tlist(origline);
2151 return DIRECTIVE_FOUND;
2153 if (nasm_stricmp(tline->text, "flat") == 0) {
2154 /* All subsequent ARG directives are for a 32-bit stack */
2155 StackSize = 4;
2156 StackPointer = "ebp";
2157 ArgOffset = 8;
2158 LocalOffset = 0;
2159 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2160 /* All subsequent ARG directives are for a 64-bit stack */
2161 StackSize = 8;
2162 StackPointer = "rbp";
2163 ArgOffset = 16;
2164 LocalOffset = 0;
2165 } else if (nasm_stricmp(tline->text, "large") == 0) {
2166 /* All subsequent ARG directives are for a 16-bit stack,
2167 * far function call.
2169 StackSize = 2;
2170 StackPointer = "bp";
2171 ArgOffset = 4;
2172 LocalOffset = 0;
2173 } else if (nasm_stricmp(tline->text, "small") == 0) {
2174 /* All subsequent ARG directives are for a 16-bit stack,
2175 * far function call. We don't support near functions.
2177 StackSize = 2;
2178 StackPointer = "bp";
2179 ArgOffset = 6;
2180 LocalOffset = 0;
2181 } else {
2182 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2183 free_tlist(origline);
2184 return DIRECTIVE_FOUND;
2186 free_tlist(origline);
2187 return DIRECTIVE_FOUND;
2189 case PP_ARG:
2190 /* TASM like ARG directive to define arguments to functions, in
2191 * the following form:
2193 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2195 offset = ArgOffset;
2196 do {
2197 char *arg, directive[256];
2198 int size = StackSize;
2200 /* Find the argument name */
2201 tline = tline->next;
2202 if (tline && tline->type == TOK_WHITESPACE)
2203 tline = tline->next;
2204 if (!tline || tline->type != TOK_ID) {
2205 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
2209 arg = tline->text;
2211 /* Find the argument size type */
2212 tline = tline->next;
2213 if (!tline || tline->type != TOK_OTHER
2214 || tline->text[0] != ':') {
2215 error(ERR_NONFATAL,
2216 "Syntax error processing `%%arg' directive");
2217 free_tlist(origline);
2218 return DIRECTIVE_FOUND;
2220 tline = tline->next;
2221 if (!tline || tline->type != TOK_ID) {
2222 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND;
2227 /* Allow macro expansion of type parameter */
2228 tt = tokenize(tline->text);
2229 tt = expand_smacro(tt);
2230 size = parse_size(tt->text);
2231 if (!size) {
2232 error(ERR_NONFATAL,
2233 "Invalid size type for `%%arg' missing directive");
2234 free_tlist(tt);
2235 free_tlist(origline);
2236 return DIRECTIVE_FOUND;
2238 free_tlist(tt);
2240 /* Round up to even stack slots */
2241 size = ALIGN(size, StackSize);
2243 /* Now define the macro for the argument */
2244 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2245 arg, StackPointer, offset);
2246 do_directive(tokenize(directive));
2247 offset += size;
2249 /* Move to the next argument in the list */
2250 tline = tline->next;
2251 if (tline && tline->type == TOK_WHITESPACE)
2252 tline = tline->next;
2253 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2254 ArgOffset = offset;
2255 free_tlist(origline);
2256 return DIRECTIVE_FOUND;
2258 case PP_LOCAL:
2259 /* TASM like LOCAL directive to define local variables for a
2260 * function, in the following form:
2262 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2264 * The '= LocalSize' at the end is ignored by NASM, but is
2265 * required by TASM to define the local parameter size (and used
2266 * by the TASM macro package).
2268 offset = LocalOffset;
2269 do {
2270 char *local, directive[256];
2271 int size = StackSize;
2273 /* Find the argument name */
2274 tline = tline->next;
2275 if (tline && tline->type == TOK_WHITESPACE)
2276 tline = tline->next;
2277 if (!tline || tline->type != TOK_ID) {
2278 error(ERR_NONFATAL,
2279 "`%%local' missing argument parameter");
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND;
2283 local = tline->text;
2285 /* Find the argument size type */
2286 tline = tline->next;
2287 if (!tline || tline->type != TOK_OTHER
2288 || tline->text[0] != ':') {
2289 error(ERR_NONFATAL,
2290 "Syntax error processing `%%local' directive");
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2294 tline = tline->next;
2295 if (!tline || tline->type != TOK_ID) {
2296 error(ERR_NONFATAL,
2297 "`%%local' missing size type parameter");
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND;
2302 /* Allow macro expansion of type parameter */
2303 tt = tokenize(tline->text);
2304 tt = expand_smacro(tt);
2305 size = parse_size(tt->text);
2306 if (!size) {
2307 error(ERR_NONFATAL,
2308 "Invalid size type for `%%local' missing directive");
2309 free_tlist(tt);
2310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2313 free_tlist(tt);
2315 /* Round up to even stack slots */
2316 size = ALIGN(size, StackSize);
2318 offset += size; /* Negative offset, increment before */
2320 /* Now define the macro for the argument */
2321 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2322 local, StackPointer, offset);
2323 do_directive(tokenize(directive));
2325 /* Now define the assign to setup the enter_c macro correctly */
2326 snprintf(directive, sizeof(directive),
2327 "%%assign %%$localsize %%$localsize+%d", size);
2328 do_directive(tokenize(directive));
2330 /* Move to the next argument in the list */
2331 tline = tline->next;
2332 if (tline && tline->type == TOK_WHITESPACE)
2333 tline = tline->next;
2334 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2335 LocalOffset = offset;
2336 free_tlist(origline);
2337 return DIRECTIVE_FOUND;
2339 case PP_CLEAR:
2340 if (tline->next)
2341 error(ERR_WARNING|ERR_PASS1,
2342 "trailing garbage after `%%clear' ignored");
2343 free_macros();
2344 init_macros();
2345 free_tlist(origline);
2346 return DIRECTIVE_FOUND;
2348 case PP_DEPEND:
2349 t = tline->next = expand_smacro(tline->next);
2350 skip_white_(t);
2351 if (!t || (t->type != TOK_STRING &&
2352 t->type != TOK_INTERNAL_STRING)) {
2353 error(ERR_NONFATAL, "`%%depend' expects a file name");
2354 free_tlist(origline);
2355 return DIRECTIVE_FOUND; /* but we did _something_ */
2357 if (t->next)
2358 error(ERR_WARNING|ERR_PASS1,
2359 "trailing garbage after `%%depend' ignored");
2360 p = t->text;
2361 if (t->type != TOK_INTERNAL_STRING)
2362 nasm_unquote_cstr(p, i);
2363 if (dephead && !in_list(*dephead, p)) {
2364 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2365 sl->next = NULL;
2366 strcpy(sl->str, p);
2367 *deptail = sl;
2368 deptail = &sl->next;
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2373 case PP_INCLUDE:
2374 t = tline->next = expand_smacro(tline->next);
2375 skip_white_(t);
2377 if (!t || (t->type != TOK_STRING &&
2378 t->type != TOK_INTERNAL_STRING)) {
2379 error(ERR_NONFATAL, "`%%include' expects a file name");
2380 free_tlist(origline);
2381 return DIRECTIVE_FOUND; /* but we did _something_ */
2383 if (t->next)
2384 error(ERR_WARNING|ERR_PASS1,
2385 "trailing garbage after `%%include' ignored");
2386 p = t->text;
2387 if (t->type != TOK_INTERNAL_STRING)
2388 nasm_unquote_cstr(p, i);
2389 inc = nasm_malloc(sizeof(Include));
2390 inc->next = istk;
2391 inc->conds = NULL;
2392 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2393 if (!inc->fp) {
2394 /* -MG given but file not found */
2395 nasm_free(inc);
2396 } else {
2397 inc->fname = src_set_fname(nasm_strdup(p));
2398 inc->lineno = src_set_linnum(0);
2399 inc->lineinc = 1;
2400 inc->expansion = NULL;
2401 inc->mstk = NULL;
2402 istk = inc;
2403 list->uplevel(LIST_INCLUDE);
2405 free_tlist(origline);
2406 return DIRECTIVE_FOUND;
2408 case PP_USE:
2410 static macros_t *use_pkg;
2411 const char *pkg_macro = NULL;
2413 tline = tline->next;
2414 skip_white_(tline);
2415 tline = expand_id(tline);
2417 if (!tline || (tline->type != TOK_STRING &&
2418 tline->type != TOK_INTERNAL_STRING &&
2419 tline->type != TOK_ID)) {
2420 error(ERR_NONFATAL, "`%%use' expects a package name");
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND; /* but we did _something_ */
2424 if (tline->next)
2425 error(ERR_WARNING|ERR_PASS1,
2426 "trailing garbage after `%%use' ignored");
2427 if (tline->type == TOK_STRING)
2428 nasm_unquote_cstr(tline->text, i);
2429 use_pkg = nasm_stdmac_find_package(tline->text);
2430 if (!use_pkg)
2431 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2432 else
2433 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2434 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2435 /* Not already included, go ahead and include it */
2436 stdmacpos = use_pkg;
2438 free_tlist(origline);
2439 return DIRECTIVE_FOUND;
2441 case PP_PUSH:
2442 case PP_REPL:
2443 case PP_POP:
2444 tline = tline->next;
2445 skip_white_(tline);
2446 tline = expand_id(tline);
2447 if (tline) {
2448 if (!tok_type_(tline, TOK_ID)) {
2449 error(ERR_NONFATAL, "`%s' expects a context identifier",
2450 pp_directives[i]);
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND; /* but we did _something_ */
2454 if (tline->next)
2455 error(ERR_WARNING|ERR_PASS1,
2456 "trailing garbage after `%s' ignored",
2457 pp_directives[i]);
2458 p = nasm_strdup(tline->text);
2459 } else {
2460 p = NULL; /* Anonymous */
2463 if (i == PP_PUSH) {
2464 ctx = nasm_malloc(sizeof(Context));
2465 ctx->next = cstk;
2466 hash_init(&ctx->localmac, HASH_SMALL);
2467 ctx->name = p;
2468 ctx->number = unique++;
2469 cstk = ctx;
2470 } else {
2471 /* %pop or %repl */
2472 if (!cstk) {
2473 error(ERR_NONFATAL, "`%s': context stack is empty",
2474 pp_directives[i]);
2475 } else if (i == PP_POP) {
2476 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2477 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2478 "expected %s",
2479 cstk->name ? cstk->name : "anonymous", p);
2480 else
2481 ctx_pop();
2482 } else {
2483 /* i == PP_REPL */
2484 nasm_free(cstk->name);
2485 cstk->name = p;
2486 p = NULL;
2488 nasm_free(p);
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2492 case PP_FATAL:
2493 severity = ERR_FATAL;
2494 goto issue_error;
2495 case PP_ERROR:
2496 severity = ERR_NONFATAL;
2497 goto issue_error;
2498 case PP_WARNING:
2499 severity = ERR_WARNING|ERR_WARN_USER;
2500 goto issue_error;
2502 issue_error:
2504 /* Only error out if this is the final pass */
2505 if (pass != 2 && i != PP_FATAL)
2506 return DIRECTIVE_FOUND;
2508 tline->next = expand_smacro(tline->next);
2509 tline = tline->next;
2510 skip_white_(tline);
2511 t = tline ? tline->next : NULL;
2512 skip_white_(t);
2513 if (tok_type_(tline, TOK_STRING) && !t) {
2514 /* The line contains only a quoted string */
2515 p = tline->text;
2516 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2517 error(severity, "%s", p);
2518 } else {
2519 /* Not a quoted string, or more than a quoted string */
2520 p = detoken(tline, false);
2521 error(severity, "%s", p);
2522 nasm_free(p);
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
2528 CASE_PP_IF:
2529 if (istk->conds && !emitting(istk->conds->state))
2530 j = COND_NEVER;
2531 else {
2532 j = if_condition(tline->next, i);
2533 tline->next = NULL; /* it got freed */
2534 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2536 cond = nasm_malloc(sizeof(Cond));
2537 cond->next = istk->conds;
2538 cond->state = j;
2539 istk->conds = cond;
2540 if(istk->mstk)
2541 istk->mstk->condcnt ++;
2542 free_tlist(origline);
2543 return DIRECTIVE_FOUND;
2545 CASE_PP_ELIF:
2546 if (!istk->conds)
2547 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2548 switch(istk->conds->state) {
2549 case COND_IF_TRUE:
2550 istk->conds->state = COND_DONE;
2551 break;
2553 case COND_DONE:
2554 case COND_NEVER:
2555 break;
2557 case COND_ELSE_TRUE:
2558 case COND_ELSE_FALSE:
2559 error_precond(ERR_WARNING|ERR_PASS1,
2560 "`%%elif' after `%%else' ignored");
2561 istk->conds->state = COND_NEVER;
2562 break;
2564 case COND_IF_FALSE:
2566 * IMPORTANT: In the case of %if, we will already have
2567 * called expand_mmac_params(); however, if we're
2568 * processing an %elif we must have been in a
2569 * non-emitting mode, which would have inhibited
2570 * the normal invocation of expand_mmac_params().
2571 * Therefore, we have to do it explicitly here.
2573 j = if_condition(expand_mmac_params(tline->next), i);
2574 tline->next = NULL; /* it got freed */
2575 istk->conds->state =
2576 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2577 break;
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2582 case PP_ELSE:
2583 if (tline->next)
2584 error_precond(ERR_WARNING|ERR_PASS1,
2585 "trailing garbage after `%%else' ignored");
2586 if (!istk->conds)
2587 error(ERR_FATAL, "`%%else': no matching `%%if'");
2588 switch(istk->conds->state) {
2589 case COND_IF_TRUE:
2590 case COND_DONE:
2591 istk->conds->state = COND_ELSE_FALSE;
2592 break;
2594 case COND_NEVER:
2595 break;
2597 case COND_IF_FALSE:
2598 istk->conds->state = COND_ELSE_TRUE;
2599 break;
2601 case COND_ELSE_TRUE:
2602 case COND_ELSE_FALSE:
2603 error_precond(ERR_WARNING|ERR_PASS1,
2604 "`%%else' after `%%else' ignored.");
2605 istk->conds->state = COND_NEVER;
2606 break;
2608 free_tlist(origline);
2609 return DIRECTIVE_FOUND;
2611 case PP_ENDIF:
2612 if (tline->next)
2613 error_precond(ERR_WARNING|ERR_PASS1,
2614 "trailing garbage after `%%endif' ignored");
2615 if (!istk->conds)
2616 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2617 cond = istk->conds;
2618 istk->conds = cond->next;
2619 nasm_free(cond);
2620 if(istk->mstk)
2621 istk->mstk->condcnt --;
2622 free_tlist(origline);
2623 return DIRECTIVE_FOUND;
2625 case PP_RMACRO:
2626 case PP_IRMACRO:
2627 case PP_MACRO:
2628 case PP_IMACRO:
2629 if (defining) {
2630 error(ERR_FATAL, "`%s': already defining a macro",
2631 pp_directives[i]);
2632 return DIRECTIVE_FOUND;
2634 defining = nasm_malloc(sizeof(MMacro));
2635 defining->max_depth =
2636 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2637 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2638 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2639 nasm_free(defining);
2640 defining = NULL;
2641 return DIRECTIVE_FOUND;
2644 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2645 while (mmac) {
2646 if (!strcmp(mmac->name, defining->name) &&
2647 (mmac->nparam_min <= defining->nparam_max
2648 || defining->plus)
2649 && (defining->nparam_min <= mmac->nparam_max
2650 || mmac->plus)) {
2651 error(ERR_WARNING|ERR_PASS1,
2652 "redefining multi-line macro `%s'", defining->name);
2653 return DIRECTIVE_FOUND;
2655 mmac = mmac->next;
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
2660 case PP_ENDM:
2661 case PP_ENDMACRO:
2662 if (! (defining && defining->name)) {
2663 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2664 return DIRECTIVE_FOUND;
2666 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2667 defining->next = *mmhead;
2668 *mmhead = defining;
2669 defining = NULL;
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2673 case PP_EXITMACRO:
2675 * We must search along istk->expansion until we hit a
2676 * macro-end marker for a macro with a name. Then we
2677 * bypass all lines between exitmacro and endmacro.
2679 list_for_each(l, istk->expansion)
2680 if (l->finishes && l->finishes->name)
2681 break;
2683 if (l) {
2685 * Remove all conditional entries relative to this
2686 * macro invocation. (safe to do in this context)
2688 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2689 cond = istk->conds;
2690 istk->conds = cond->next;
2691 nasm_free(cond);
2693 istk->expansion = l;
2694 } else {
2695 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2697 free_tlist(origline);
2698 return DIRECTIVE_FOUND;
2700 case PP_UNMACRO:
2701 case PP_UNIMACRO:
2703 MMacro **mmac_p;
2704 MMacro spec;
2706 spec.casesense = (i == PP_UNMACRO);
2707 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2708 return DIRECTIVE_FOUND;
2710 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2711 while (mmac_p && *mmac_p) {
2712 mmac = *mmac_p;
2713 if (mmac->casesense == spec.casesense &&
2714 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2715 mmac->nparam_min == spec.nparam_min &&
2716 mmac->nparam_max == spec.nparam_max &&
2717 mmac->plus == spec.plus) {
2718 *mmac_p = mmac->next;
2719 free_mmacro(mmac);
2720 } else {
2721 mmac_p = &mmac->next;
2724 free_tlist(origline);
2725 free_tlist(spec.dlist);
2726 return DIRECTIVE_FOUND;
2729 case PP_ROTATE:
2730 if (tline->next && tline->next->type == TOK_WHITESPACE)
2731 tline = tline->next;
2732 if (!tline->next) {
2733 free_tlist(origline);
2734 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2735 return DIRECTIVE_FOUND;
2737 t = expand_smacro(tline->next);
2738 tline->next = NULL;
2739 free_tlist(origline);
2740 tline = t;
2741 tptr = &t;
2742 tokval.t_type = TOKEN_INVALID;
2743 evalresult =
2744 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2745 free_tlist(tline);
2746 if (!evalresult)
2747 return DIRECTIVE_FOUND;
2748 if (tokval.t_type)
2749 error(ERR_WARNING|ERR_PASS1,
2750 "trailing garbage after expression ignored");
2751 if (!is_simple(evalresult)) {
2752 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2753 return DIRECTIVE_FOUND;
2755 mmac = istk->mstk;
2756 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2757 mmac = mmac->next_active;
2758 if (!mmac) {
2759 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2760 } else if (mmac->nparam == 0) {
2761 error(ERR_NONFATAL,
2762 "`%%rotate' invoked within macro without parameters");
2763 } else {
2764 int rotate = mmac->rotate + reloc_value(evalresult);
2766 rotate %= (int)mmac->nparam;
2767 if (rotate < 0)
2768 rotate += mmac->nparam;
2770 mmac->rotate = rotate;
2772 return DIRECTIVE_FOUND;
2774 case PP_REP:
2775 nolist = false;
2776 do {
2777 tline = tline->next;
2778 } while (tok_type_(tline, TOK_WHITESPACE));
2780 if (tok_type_(tline, TOK_ID) &&
2781 nasm_stricmp(tline->text, ".nolist") == 0) {
2782 nolist = true;
2783 do {
2784 tline = tline->next;
2785 } while (tok_type_(tline, TOK_WHITESPACE));
2788 if (tline) {
2789 t = expand_smacro(tline);
2790 tptr = &t;
2791 tokval.t_type = TOKEN_INVALID;
2792 evalresult =
2793 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2794 if (!evalresult) {
2795 free_tlist(origline);
2796 return DIRECTIVE_FOUND;
2798 if (tokval.t_type)
2799 error(ERR_WARNING|ERR_PASS1,
2800 "trailing garbage after expression ignored");
2801 if (!is_simple(evalresult)) {
2802 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2803 return DIRECTIVE_FOUND;
2805 count = reloc_value(evalresult) + 1;
2806 } else {
2807 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2808 count = 0;
2810 free_tlist(origline);
2812 tmp_defining = defining;
2813 defining = nasm_malloc(sizeof(MMacro));
2814 defining->prev = NULL;
2815 defining->name = NULL; /* flags this macro as a %rep block */
2816 defining->casesense = false;
2817 defining->plus = false;
2818 defining->nolist = nolist;
2819 defining->in_progress = count;
2820 defining->max_depth = 0;
2821 defining->nparam_min = defining->nparam_max = 0;
2822 defining->defaults = NULL;
2823 defining->dlist = NULL;
2824 defining->expansion = NULL;
2825 defining->next_active = istk->mstk;
2826 defining->rep_nest = tmp_defining;
2827 return DIRECTIVE_FOUND;
2829 case PP_ENDREP:
2830 if (!defining || defining->name) {
2831 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2832 return DIRECTIVE_FOUND;
2836 * Now we have a "macro" defined - although it has no name
2837 * and we won't be entering it in the hash tables - we must
2838 * push a macro-end marker for it on to istk->expansion.
2839 * After that, it will take care of propagating itself (a
2840 * macro-end marker line for a macro which is really a %rep
2841 * block will cause the macro to be re-expanded, complete
2842 * with another macro-end marker to ensure the process
2843 * continues) until the whole expansion is forcibly removed
2844 * from istk->expansion by a %exitrep.
2846 l = nasm_malloc(sizeof(Line));
2847 l->next = istk->expansion;
2848 l->finishes = defining;
2849 l->first = NULL;
2850 istk->expansion = l;
2852 istk->mstk = defining;
2854 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2855 tmp_defining = defining;
2856 defining = defining->rep_nest;
2857 free_tlist(origline);
2858 return DIRECTIVE_FOUND;
2860 case PP_EXITREP:
2862 * We must search along istk->expansion until we hit a
2863 * macro-end marker for a macro with no name. Then we set
2864 * its `in_progress' flag to 0.
2866 list_for_each(l, istk->expansion)
2867 if (l->finishes && !l->finishes->name)
2868 break;
2870 if (l)
2871 l->finishes->in_progress = 1;
2872 else
2873 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
2877 case PP_XDEFINE:
2878 case PP_IXDEFINE:
2879 case PP_DEFINE:
2880 case PP_IDEFINE:
2881 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2883 tline = tline->next;
2884 skip_white_(tline);
2885 tline = expand_id(tline);
2886 if (!tline || (tline->type != TOK_ID &&
2887 (tline->type != TOK_PREPROC_ID ||
2888 tline->text[1] != '$'))) {
2889 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2890 pp_directives[i]);
2891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
2895 ctx = get_ctx(tline->text, &mname, false);
2896 last = tline;
2897 param_start = tline = tline->next;
2898 nparam = 0;
2900 /* Expand the macro definition now for %xdefine and %ixdefine */
2901 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2902 tline = expand_smacro(tline);
2904 if (tok_is_(tline, "(")) {
2906 * This macro has parameters.
2909 tline = tline->next;
2910 while (1) {
2911 skip_white_(tline);
2912 if (!tline) {
2913 error(ERR_NONFATAL, "parameter identifier expected");
2914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2917 if (tline->type != TOK_ID) {
2918 error(ERR_NONFATAL,
2919 "`%s': parameter identifier expected",
2920 tline->text);
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
2924 tline->type = TOK_SMAC_PARAM + nparam++;
2925 tline = tline->next;
2926 skip_white_(tline);
2927 if (tok_is_(tline, ",")) {
2928 tline = tline->next;
2929 } else {
2930 if (!tok_is_(tline, ")")) {
2931 error(ERR_NONFATAL,
2932 "`)' expected to terminate macro template");
2933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
2936 break;
2939 last = tline;
2940 tline = tline->next;
2942 if (tok_type_(tline, TOK_WHITESPACE))
2943 last = tline, tline = tline->next;
2944 macro_start = NULL;
2945 last->next = NULL;
2946 t = tline;
2947 while (t) {
2948 if (t->type == TOK_ID) {
2949 list_for_each(tt, param_start)
2950 if (tt->type >= TOK_SMAC_PARAM &&
2951 !strcmp(tt->text, t->text))
2952 t->type = tt->type;
2954 tt = t->next;
2955 t->next = macro_start;
2956 macro_start = t;
2957 t = tt;
2960 * Good. We now have a macro name, a parameter count, and a
2961 * token list (in reverse order) for an expansion. We ought
2962 * to be OK just to create an SMacro, store it, and let
2963 * free_tlist have the rest of the line (which we have
2964 * carefully re-terminated after chopping off the expansion
2965 * from the end).
2967 define_smacro(ctx, mname, casesense, nparam, macro_start);
2968 free_tlist(origline);
2969 return DIRECTIVE_FOUND;
2971 case PP_UNDEF:
2972 tline = tline->next;
2973 skip_white_(tline);
2974 tline = expand_id(tline);
2975 if (!tline || (tline->type != TOK_ID &&
2976 (tline->type != TOK_PREPROC_ID ||
2977 tline->text[1] != '$'))) {
2978 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2982 if (tline->next) {
2983 error(ERR_WARNING|ERR_PASS1,
2984 "trailing garbage after macro name ignored");
2987 /* Find the context that symbol belongs to */
2988 ctx = get_ctx(tline->text, &mname, false);
2989 undef_smacro(ctx, mname);
2990 free_tlist(origline);
2991 return DIRECTIVE_FOUND;
2993 case PP_DEFSTR:
2994 case PP_IDEFSTR:
2995 casesense = (i == PP_DEFSTR);
2997 tline = tline->next;
2998 skip_white_(tline);
2999 tline = expand_id(tline);
3000 if (!tline || (tline->type != TOK_ID &&
3001 (tline->type != TOK_PREPROC_ID ||
3002 tline->text[1] != '$'))) {
3003 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3004 pp_directives[i]);
3005 free_tlist(origline);
3006 return DIRECTIVE_FOUND;
3009 ctx = get_ctx(tline->text, &mname, false);
3010 last = tline;
3011 tline = expand_smacro(tline->next);
3012 last->next = NULL;
3014 while (tok_type_(tline, TOK_WHITESPACE))
3015 tline = delete_Token(tline);
3017 p = detoken(tline, false);
3018 macro_start = nasm_malloc(sizeof(*macro_start));
3019 macro_start->next = NULL;
3020 macro_start->text = nasm_quote(p, strlen(p));
3021 macro_start->type = TOK_STRING;
3022 macro_start->a.mac = NULL;
3023 nasm_free(p);
3026 * We now have a macro name, an implicit parameter count of
3027 * zero, and a string token to use as an expansion. Create
3028 * and store an SMacro.
3030 define_smacro(ctx, mname, casesense, 0, macro_start);
3031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
3034 case PP_DEFTOK:
3035 case PP_IDEFTOK:
3036 casesense = (i == PP_DEFTOK);
3038 tline = tline->next;
3039 skip_white_(tline);
3040 tline = expand_id(tline);
3041 if (!tline || (tline->type != TOK_ID &&
3042 (tline->type != TOK_PREPROC_ID ||
3043 tline->text[1] != '$'))) {
3044 error(ERR_NONFATAL,
3045 "`%s' expects a macro identifier as first parameter",
3046 pp_directives[i]);
3047 free_tlist(origline);
3048 return DIRECTIVE_FOUND;
3050 ctx = get_ctx(tline->text, &mname, false);
3051 last = tline;
3052 tline = expand_smacro(tline->next);
3053 last->next = NULL;
3055 t = tline;
3056 while (tok_type_(t, TOK_WHITESPACE))
3057 t = t->next;
3058 /* t should now point to the string */
3059 if (t->type != TOK_STRING) {
3060 error(ERR_NONFATAL,
3061 "`%s` requires string as second parameter",
3062 pp_directives[i]);
3063 free_tlist(tline);
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3068 nasm_unquote_cstr(t->text, i);
3069 macro_start = tokenize(t->text);
3072 * We now have a macro name, an implicit parameter count of
3073 * zero, and a numeric token to use as an expansion. Create
3074 * and store an SMacro.
3076 define_smacro(ctx, mname, casesense, 0, macro_start);
3077 free_tlist(tline);
3078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
3081 case PP_PATHSEARCH:
3083 FILE *fp;
3084 StrList *xsl = NULL;
3085 StrList **xst = &xsl;
3087 casesense = true;
3089 tline = tline->next;
3090 skip_white_(tline);
3091 tline = expand_id(tline);
3092 if (!tline || (tline->type != TOK_ID &&
3093 (tline->type != TOK_PREPROC_ID ||
3094 tline->text[1] != '$'))) {
3095 error(ERR_NONFATAL,
3096 "`%%pathsearch' expects a macro identifier as first parameter");
3097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
3100 ctx = get_ctx(tline->text, &mname, false);
3101 last = tline;
3102 tline = expand_smacro(tline->next);
3103 last->next = NULL;
3105 t = tline;
3106 while (tok_type_(t, TOK_WHITESPACE))
3107 t = t->next;
3109 if (!t || (t->type != TOK_STRING &&
3110 t->type != TOK_INTERNAL_STRING)) {
3111 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3112 free_tlist(tline);
3113 free_tlist(origline);
3114 return DIRECTIVE_FOUND; /* but we did _something_ */
3116 if (t->next)
3117 error(ERR_WARNING|ERR_PASS1,
3118 "trailing garbage after `%%pathsearch' ignored");
3119 p = t->text;
3120 if (t->type != TOK_INTERNAL_STRING)
3121 nasm_unquote(p, NULL);
3123 fp = inc_fopen(p, &xsl, &xst, true);
3124 if (fp) {
3125 p = xsl->str;
3126 fclose(fp); /* Don't actually care about the file */
3128 macro_start = nasm_malloc(sizeof(*macro_start));
3129 macro_start->next = NULL;
3130 macro_start->text = nasm_quote(p, strlen(p));
3131 macro_start->type = TOK_STRING;
3132 macro_start->a.mac = NULL;
3133 if (xsl)
3134 nasm_free(xsl);
3137 * We now have a macro name, an implicit parameter count of
3138 * zero, and a string token to use as an expansion. Create
3139 * and store an SMacro.
3141 define_smacro(ctx, mname, casesense, 0, macro_start);
3142 free_tlist(tline);
3143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3147 case PP_STRLEN:
3148 casesense = true;
3150 tline = tline->next;
3151 skip_white_(tline);
3152 tline = expand_id(tline);
3153 if (!tline || (tline->type != TOK_ID &&
3154 (tline->type != TOK_PREPROC_ID ||
3155 tline->text[1] != '$'))) {
3156 error(ERR_NONFATAL,
3157 "`%%strlen' expects a macro identifier as first parameter");
3158 free_tlist(origline);
3159 return DIRECTIVE_FOUND;
3161 ctx = get_ctx(tline->text, &mname, false);
3162 last = tline;
3163 tline = expand_smacro(tline->next);
3164 last->next = NULL;
3166 t = tline;
3167 while (tok_type_(t, TOK_WHITESPACE))
3168 t = t->next;
3169 /* t should now point to the string */
3170 if (t->type != TOK_STRING) {
3171 error(ERR_NONFATAL,
3172 "`%%strlen` requires string as second parameter");
3173 free_tlist(tline);
3174 free_tlist(origline);
3175 return DIRECTIVE_FOUND;
3178 macro_start = nasm_malloc(sizeof(*macro_start));
3179 macro_start->next = NULL;
3180 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3181 macro_start->a.mac = NULL;
3184 * We now have a macro name, an implicit parameter count of
3185 * zero, and a numeric token to use as an expansion. Create
3186 * and store an SMacro.
3188 define_smacro(ctx, mname, casesense, 0, macro_start);
3189 free_tlist(tline);
3190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
3193 case PP_STRCAT:
3194 casesense = true;
3196 tline = tline->next;
3197 skip_white_(tline);
3198 tline = expand_id(tline);
3199 if (!tline || (tline->type != TOK_ID &&
3200 (tline->type != TOK_PREPROC_ID ||
3201 tline->text[1] != '$'))) {
3202 error(ERR_NONFATAL,
3203 "`%%strcat' expects a macro identifier as first parameter");
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3207 ctx = get_ctx(tline->text, &mname, false);
3208 last = tline;
3209 tline = expand_smacro(tline->next);
3210 last->next = NULL;
3212 len = 0;
3213 list_for_each(t, tline) {
3214 switch (t->type) {
3215 case TOK_WHITESPACE:
3216 break;
3217 case TOK_STRING:
3218 len += t->a.len = nasm_unquote(t->text, NULL);
3219 break;
3220 case TOK_OTHER:
3221 if (!strcmp(t->text, ",")) /* permit comma separators */
3222 break;
3223 /* else fall through */
3224 default:
3225 error(ERR_NONFATAL,
3226 "non-string passed to `%%strcat' (%d)", t->type);
3227 free_tlist(tline);
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
3233 p = pp = nasm_malloc(len);
3234 list_for_each(t, tline) {
3235 if (t->type == TOK_STRING) {
3236 memcpy(p, t->text, t->a.len);
3237 p += t->a.len;
3242 * We now have a macro name, an implicit parameter count of
3243 * zero, and a numeric token to use as an expansion. Create
3244 * and store an SMacro.
3246 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3247 macro_start->text = nasm_quote(pp, len);
3248 nasm_free(pp);
3249 define_smacro(ctx, mname, casesense, 0, macro_start);
3250 free_tlist(tline);
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3254 case PP_SUBSTR:
3256 int64_t a1, a2;
3257 size_t len;
3259 casesense = true;
3261 tline = tline->next;
3262 skip_white_(tline);
3263 tline = expand_id(tline);
3264 if (!tline || (tline->type != TOK_ID &&
3265 (tline->type != TOK_PREPROC_ID ||
3266 tline->text[1] != '$'))) {
3267 error(ERR_NONFATAL,
3268 "`%%substr' expects a macro identifier as first parameter");
3269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3272 ctx = get_ctx(tline->text, &mname, false);
3273 last = tline;
3274 tline = expand_smacro(tline->next);
3275 last->next = NULL;
3277 t = tline->next;
3278 while (tok_type_(t, TOK_WHITESPACE))
3279 t = t->next;
3281 /* t should now point to the string */
3282 if (t->type != TOK_STRING) {
3283 error(ERR_NONFATAL,
3284 "`%%substr` requires string as second parameter");
3285 free_tlist(tline);
3286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3290 tt = t->next;
3291 tptr = &tt;
3292 tokval.t_type = TOKEN_INVALID;
3293 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3294 pass, error, NULL);
3295 if (!evalresult) {
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 } else if (!is_simple(evalresult)) {
3300 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3301 free_tlist(tline);
3302 free_tlist(origline);
3303 return DIRECTIVE_FOUND;
3305 a1 = evalresult->value-1;
3307 while (tok_type_(tt, TOK_WHITESPACE))
3308 tt = tt->next;
3309 if (!tt) {
3310 a2 = 1; /* Backwards compatibility: one character */
3311 } else {
3312 tokval.t_type = TOKEN_INVALID;
3313 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3314 pass, error, NULL);
3315 if (!evalresult) {
3316 free_tlist(tline);
3317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 } else if (!is_simple(evalresult)) {
3320 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3321 free_tlist(tline);
3322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
3325 a2 = evalresult->value;
3328 len = nasm_unquote(t->text, NULL);
3329 if (a2 < 0)
3330 a2 = a2+1+len-a1;
3331 if (a1+a2 > (int64_t)len)
3332 a2 = len-a1;
3334 macro_start = nasm_malloc(sizeof(*macro_start));
3335 macro_start->next = NULL;
3336 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3337 macro_start->type = TOK_STRING;
3338 macro_start->a.mac = NULL;
3341 * We now have a macro name, an implicit parameter count of
3342 * zero, and a numeric token to use as an expansion. Create
3343 * and store an SMacro.
3345 define_smacro(ctx, mname, casesense, 0, macro_start);
3346 free_tlist(tline);
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
3351 case PP_ASSIGN:
3352 case PP_IASSIGN:
3353 casesense = (i == PP_ASSIGN);
3355 tline = tline->next;
3356 skip_white_(tline);
3357 tline = expand_id(tline);
3358 if (!tline || (tline->type != TOK_ID &&
3359 (tline->type != TOK_PREPROC_ID ||
3360 tline->text[1] != '$'))) {
3361 error(ERR_NONFATAL,
3362 "`%%%sassign' expects a macro identifier",
3363 (i == PP_IASSIGN ? "i" : ""));
3364 free_tlist(origline);
3365 return DIRECTIVE_FOUND;
3367 ctx = get_ctx(tline->text, &mname, false);
3368 last = tline;
3369 tline = expand_smacro(tline->next);
3370 last->next = NULL;
3372 t = tline;
3373 tptr = &t;
3374 tokval.t_type = TOKEN_INVALID;
3375 evalresult =
3376 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3377 free_tlist(tline);
3378 if (!evalresult) {
3379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
3383 if (tokval.t_type)
3384 error(ERR_WARNING|ERR_PASS1,
3385 "trailing garbage after expression ignored");
3387 if (!is_simple(evalresult)) {
3388 error(ERR_NONFATAL,
3389 "non-constant value given to `%%%sassign'",
3390 (i == PP_IASSIGN ? "i" : ""));
3391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
3395 macro_start = nasm_malloc(sizeof(*macro_start));
3396 macro_start->next = NULL;
3397 make_tok_num(macro_start, reloc_value(evalresult));
3398 macro_start->a.mac = NULL;
3401 * We now have a macro name, an implicit parameter count of
3402 * zero, and a numeric token to use as an expansion. Create
3403 * and store an SMacro.
3405 define_smacro(ctx, mname, casesense, 0, macro_start);
3406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
3409 case PP_LINE:
3411 * Syntax is `%line nnn[+mmm] [filename]'
3413 tline = tline->next;
3414 skip_white_(tline);
3415 if (!tok_type_(tline, TOK_NUMBER)) {
3416 error(ERR_NONFATAL, "`%%line' expects line number");
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3420 k = readnum(tline->text, &err);
3421 m = 1;
3422 tline = tline->next;
3423 if (tok_is_(tline, "+")) {
3424 tline = tline->next;
3425 if (!tok_type_(tline, TOK_NUMBER)) {
3426 error(ERR_NONFATAL, "`%%line' expects line increment");
3427 free_tlist(origline);
3428 return DIRECTIVE_FOUND;
3430 m = readnum(tline->text, &err);
3431 tline = tline->next;
3433 skip_white_(tline);
3434 src_set_linnum(k);
3435 istk->lineinc = m;
3436 if (tline) {
3437 nasm_free(src_set_fname(detoken(tline, false)));
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
3442 default:
3443 error(ERR_FATAL,
3444 "preprocessor directive `%s' not yet implemented",
3445 pp_directives[i]);
3446 return DIRECTIVE_FOUND;
3451 * Ensure that a macro parameter contains a condition code and
3452 * nothing else. Return the condition code index if so, or -1
3453 * otherwise.
3455 static int find_cc(Token * t)
3457 Token *tt;
3458 int i, j, k, m;
3460 if (!t)
3461 return -1; /* Probably a %+ without a space */
3463 skip_white_(t);
3464 if (t->type != TOK_ID)
3465 return -1;
3466 tt = t->next;
3467 skip_white_(tt);
3468 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3469 return -1;
3471 i = -1;
3472 j = ARRAY_SIZE(conditions);
3473 while (j - i > 1) {
3474 k = (j + i) / 2;
3475 m = nasm_stricmp(t->text, conditions[k]);
3476 if (m == 0) {
3477 i = k;
3478 j = -2;
3479 break;
3480 } else if (m < 0) {
3481 j = k;
3482 } else
3483 i = k;
3485 if (j != -2)
3486 return -1;
3487 return i;
3490 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3492 Token **tail, *t, *tt;
3493 Token **paste_head;
3494 bool did_paste = false;
3495 char *tmp;
3497 /* Now handle token pasting... */
3498 paste_head = NULL;
3499 tail = head;
3500 while ((t = *tail) && (tt = t->next)) {
3501 switch (t->type) {
3502 case TOK_WHITESPACE:
3503 if (tt->type == TOK_WHITESPACE) {
3504 /* Zap adjacent whitespace tokens */
3505 t->next = delete_Token(tt);
3506 } else {
3507 /* Do not advance paste_head here */
3508 tail = &t->next;
3510 break;
3511 case TOK_ID:
3512 case TOK_NUMBER:
3513 case TOK_FLOAT:
3515 size_t len = 0;
3516 char *tmp, *p;
3518 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3519 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3520 tt->type == TOK_OTHER)) {
3521 len += strlen(tt->text);
3522 tt = tt->next;
3526 * Now tt points to the first token after
3527 * the potential paste area...
3529 if (tt != t->next) {
3530 /* We have at least two tokens... */
3531 len += strlen(t->text);
3532 p = tmp = nasm_malloc(len+1);
3534 while (t != tt) {
3535 strcpy(p, t->text);
3536 p = strchr(p, '\0');
3537 t = delete_Token(t);
3540 t = *tail = tokenize(tmp);
3541 nasm_free(tmp);
3543 while (t->next) {
3544 tail = &t->next;
3545 t = t->next;
3547 t->next = tt; /* Attach the remaining token chain */
3549 did_paste = true;
3551 paste_head = tail;
3552 tail = &t->next;
3553 break;
3555 case TOK_PASTE: /* %+ */
3556 if (handle_paste_tokens) {
3557 /* Zap %+ and whitespace tokens to the right */
3558 while (t && (t->type == TOK_WHITESPACE ||
3559 t->type == TOK_PASTE))
3560 t = *tail = delete_Token(t);
3561 if (!paste_head || !t)
3562 break; /* Nothing to paste with */
3563 tail = paste_head;
3564 t = *tail;
3565 tt = t->next;
3566 while (tok_type_(tt, TOK_WHITESPACE))
3567 tt = t->next = delete_Token(tt);
3569 if (tt) {
3570 tmp = nasm_strcat(t->text, tt->text);
3571 delete_Token(t);
3572 tt = delete_Token(tt);
3573 t = *tail = tokenize(tmp);
3574 nasm_free(tmp);
3575 while (t->next) {
3576 tail = &t->next;
3577 t = t->next;
3579 t->next = tt; /* Attach the remaining token chain */
3580 did_paste = true;
3582 paste_head = tail;
3583 tail = &t->next;
3584 break;
3586 /* else fall through */
3587 default:
3588 tail = &t->next;
3589 if (!tok_type_(t->next, TOK_WHITESPACE))
3590 paste_head = tail;
3591 break;
3594 return did_paste;
3598 * expands to a list of tokens from %{x:y}
3600 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3602 Token *t = tline, **tt, *tm, *head;
3603 char *pos;
3604 int fst, lst, j, i;
3606 pos = strchr(tline->text, ':');
3607 nasm_assert(pos);
3609 lst = atoi(pos + 1);
3610 fst = atoi(tline->text + 1);
3613 * only macros params are accounted so
3614 * if someone passes %0 -- we reject such
3615 * value(s)
3617 if (lst == 0 || fst == 0)
3618 goto err;
3620 /* the values should be sane */
3621 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3622 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3623 goto err;
3625 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3626 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3628 /* counted from zero */
3629 fst--, lst--;
3632 * it will be at least one token
3634 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3635 t = new_Token(NULL, tm->type, tm->text, 0);
3636 head = t, tt = &t->next;
3637 if (fst < lst) {
3638 for (i = fst + 1; i <= lst; i++) {
3639 t = new_Token(NULL, TOK_OTHER, ",", 0);
3640 *tt = t, tt = &t->next;
3641 j = (i + mac->rotate) % mac->nparam;
3642 tm = mac->params[j];
3643 t = new_Token(NULL, tm->type, tm->text, 0);
3644 *tt = t, tt = &t->next;
3646 } else {
3647 for (i = fst - 1; i >= lst; i--) {
3648 t = new_Token(NULL, TOK_OTHER, ",", 0);
3649 *tt = t, tt = &t->next;
3650 j = (i + mac->rotate) % mac->nparam;
3651 tm = mac->params[j];
3652 t = new_Token(NULL, tm->type, tm->text, 0);
3653 *tt = t, tt = &t->next;
3657 *last = tt;
3658 return head;
3660 err:
3661 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3662 &tline->text[1]);
3663 return tline;
3667 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3668 * %-n) and MMacro-local identifiers (%%foo) as well as
3669 * macro indirection (%[...]) and range (%{..:..}).
3671 static Token *expand_mmac_params(Token * tline)
3673 Token *t, *tt, **tail, *thead;
3674 bool changed = false;
3675 char *pos;
3677 tail = &thead;
3678 thead = NULL;
3680 while (tline) {
3681 if (tline->type == TOK_PREPROC_ID &&
3682 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3683 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3684 tline->text[1] == '%')) {
3685 char *text = NULL;
3686 int type = 0, cc; /* type = 0 to placate optimisers */
3687 char tmpbuf[30];
3688 unsigned int n;
3689 int i;
3690 MMacro *mac;
3692 t = tline;
3693 tline = tline->next;
3695 mac = istk->mstk;
3696 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3697 mac = mac->next_active;
3698 if (!mac) {
3699 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3700 } else {
3701 pos = strchr(t->text, ':');
3702 if (!pos) {
3703 switch (t->text[1]) {
3705 * We have to make a substitution of one of the
3706 * forms %1, %-1, %+1, %%foo, %0.
3708 case '0':
3709 type = TOK_NUMBER;
3710 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3711 text = nasm_strdup(tmpbuf);
3712 break;
3713 case '%':
3714 type = TOK_ID;
3715 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3716 mac->unique);
3717 text = nasm_strcat(tmpbuf, t->text + 2);
3718 break;
3719 case '-':
3720 n = atoi(t->text + 2) - 1;
3721 if (n >= mac->nparam)
3722 tt = NULL;
3723 else {
3724 if (mac->nparam > 1)
3725 n = (n + mac->rotate) % mac->nparam;
3726 tt = mac->params[n];
3728 cc = find_cc(tt);
3729 if (cc == -1) {
3730 error(ERR_NONFATAL,
3731 "macro parameter %d is not a condition code",
3732 n + 1);
3733 text = NULL;
3734 } else {
3735 type = TOK_ID;
3736 if (inverse_ccs[cc] == -1) {
3737 error(ERR_NONFATAL,
3738 "condition code `%s' is not invertible",
3739 conditions[cc]);
3740 text = NULL;
3741 } else
3742 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3744 break;
3745 case '+':
3746 n = atoi(t->text + 2) - 1;
3747 if (n >= mac->nparam)
3748 tt = NULL;
3749 else {
3750 if (mac->nparam > 1)
3751 n = (n + mac->rotate) % mac->nparam;
3752 tt = mac->params[n];
3754 cc = find_cc(tt);
3755 if (cc == -1) {
3756 error(ERR_NONFATAL,
3757 "macro parameter %d is not a condition code",
3758 n + 1);
3759 text = NULL;
3760 } else {
3761 type = TOK_ID;
3762 text = nasm_strdup(conditions[cc]);
3764 break;
3765 default:
3766 n = atoi(t->text + 1) - 1;
3767 if (n >= mac->nparam)
3768 tt = NULL;
3769 else {
3770 if (mac->nparam > 1)
3771 n = (n + mac->rotate) % mac->nparam;
3772 tt = mac->params[n];
3774 if (tt) {
3775 for (i = 0; i < mac->paramlen[n]; i++) {
3776 *tail = new_Token(NULL, tt->type, tt->text, 0);
3777 tail = &(*tail)->next;
3778 tt = tt->next;
3781 text = NULL; /* we've done it here */
3782 break;
3784 } else {
3786 * seems we have a parameters range here
3788 Token *head, **last;
3789 head = expand_mmac_params_range(mac, t, &last);
3790 if (head != t) {
3791 *tail = head;
3792 *last = tline;
3793 tline = head;
3794 text = NULL;
3798 if (!text) {
3799 delete_Token(t);
3800 } else {
3801 *tail = t;
3802 tail = &t->next;
3803 t->type = type;
3804 nasm_free(t->text);
3805 t->text = text;
3806 t->a.mac = NULL;
3808 changed = true;
3809 continue;
3810 } else if (tline->type == TOK_INDIRECT) {
3811 t = tline;
3812 tline = tline->next;
3813 tt = tokenize(t->text);
3814 tt = expand_mmac_params(tt);
3815 tt = expand_smacro(tt);
3816 *tail = tt;
3817 while (tt) {
3818 tt->a.mac = NULL; /* Necessary? */
3819 tail = &tt->next;
3820 tt = tt->next;
3822 delete_Token(t);
3823 changed = true;
3824 } else {
3825 t = *tail = tline;
3826 tline = tline->next;
3827 t->a.mac = NULL;
3828 tail = &t->next;
3831 *tail = NULL;
3833 if (changed)
3834 paste_tokens(&thead, false);
3836 return thead;
3840 * Expand all single-line macro calls made in the given line.
3841 * Return the expanded version of the line. The original is deemed
3842 * to be destroyed in the process. (In reality we'll just move
3843 * Tokens from input to output a lot of the time, rather than
3844 * actually bothering to destroy and replicate.)
3847 static Token *expand_smacro(Token * tline)
3849 Token *t, *tt, *mstart, **tail, *thead;
3850 SMacro *head = NULL, *m;
3851 Token **params;
3852 int *paramsize;
3853 unsigned int nparam, sparam;
3854 int brackets;
3855 Token *org_tline = tline;
3856 Context *ctx;
3857 const char *mname;
3858 int deadman = DEADMAN_LIMIT;
3859 bool expanded;
3862 * Trick: we should avoid changing the start token pointer since it can
3863 * be contained in "next" field of other token. Because of this
3864 * we allocate a copy of first token and work with it; at the end of
3865 * routine we copy it back
3867 if (org_tline) {
3868 tline = new_Token(org_tline->next, org_tline->type,
3869 org_tline->text, 0);
3870 tline->a.mac = org_tline->a.mac;
3871 nasm_free(org_tline->text);
3872 org_tline->text = NULL;
3875 expanded = true; /* Always expand %+ at least once */
3877 again:
3878 thead = NULL;
3879 tail = &thead;
3881 while (tline) { /* main token loop */
3882 if (!--deadman) {
3883 error(ERR_NONFATAL, "interminable macro recursion");
3884 goto err;
3887 if ((mname = tline->text)) {
3888 /* if this token is a local macro, look in local context */
3889 if (tline->type == TOK_ID) {
3890 head = (SMacro *)hash_findix(&smacros, mname);
3891 } else if (tline->type == TOK_PREPROC_ID) {
3892 ctx = get_ctx(mname, &mname, true);
3893 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3894 } else
3895 head = NULL;
3898 * We've hit an identifier. As in is_mmacro below, we first
3899 * check whether the identifier is a single-line macro at
3900 * all, then think about checking for parameters if
3901 * necessary.
3903 list_for_each(m, head)
3904 if (!mstrcmp(m->name, mname, m->casesense))
3905 break;
3906 if (m) {
3907 mstart = tline;
3908 params = NULL;
3909 paramsize = NULL;
3910 if (m->nparam == 0) {
3912 * Simple case: the macro is parameterless. Discard the
3913 * one token that the macro call took, and push the
3914 * expansion back on the to-do stack.
3916 if (!m->expansion) {
3917 if (!strcmp("__FILE__", m->name)) {
3918 int32_t num = 0;
3919 char *file = NULL;
3920 src_get(&num, &file);
3921 tline->text = nasm_quote(file, strlen(file));
3922 tline->type = TOK_STRING;
3923 nasm_free(file);
3924 continue;
3926 if (!strcmp("__LINE__", m->name)) {
3927 nasm_free(tline->text);
3928 make_tok_num(tline, src_get_linnum());
3929 continue;
3931 if (!strcmp("__BITS__", m->name)) {
3932 nasm_free(tline->text);
3933 make_tok_num(tline, globalbits);
3934 continue;
3936 tline = delete_Token(tline);
3937 continue;
3939 } else {
3941 * Complicated case: at least one macro with this name
3942 * exists and takes parameters. We must find the
3943 * parameters in the call, count them, find the SMacro
3944 * that corresponds to that form of the macro call, and
3945 * substitute for the parameters when we expand. What a
3946 * pain.
3948 /*tline = tline->next;
3949 skip_white_(tline); */
3950 do {
3951 t = tline->next;
3952 while (tok_type_(t, TOK_SMAC_END)) {
3953 t->a.mac->in_progress = false;
3954 t->text = NULL;
3955 t = tline->next = delete_Token(t);
3957 tline = t;
3958 } while (tok_type_(tline, TOK_WHITESPACE));
3959 if (!tok_is_(tline, "(")) {
3961 * This macro wasn't called with parameters: ignore
3962 * the call. (Behaviour borrowed from gnu cpp.)
3964 tline = mstart;
3965 m = NULL;
3966 } else {
3967 int paren = 0;
3968 int white = 0;
3969 brackets = 0;
3970 nparam = 0;
3971 sparam = PARAM_DELTA;
3972 params = nasm_malloc(sparam * sizeof(Token *));
3973 params[0] = tline->next;
3974 paramsize = nasm_malloc(sparam * sizeof(int));
3975 paramsize[0] = 0;
3976 while (true) { /* parameter loop */
3978 * For some unusual expansions
3979 * which concatenates function call
3981 t = tline->next;
3982 while (tok_type_(t, TOK_SMAC_END)) {
3983 t->a.mac->in_progress = false;
3984 t->text = NULL;
3985 t = tline->next = delete_Token(t);
3987 tline = t;
3989 if (!tline) {
3990 error(ERR_NONFATAL,
3991 "macro call expects terminating `)'");
3992 break;
3994 if (tline->type == TOK_WHITESPACE
3995 && brackets <= 0) {
3996 if (paramsize[nparam])
3997 white++;
3998 else
3999 params[nparam] = tline->next;
4000 continue; /* parameter loop */
4002 if (tline->type == TOK_OTHER
4003 && tline->text[1] == 0) {
4004 char ch = tline->text[0];
4005 if (ch == ',' && !paren && brackets <= 0) {
4006 if (++nparam >= sparam) {
4007 sparam += PARAM_DELTA;
4008 params = nasm_realloc(params,
4009 sparam * sizeof(Token *));
4010 paramsize = nasm_realloc(paramsize,
4011 sparam * sizeof(int));
4013 params[nparam] = tline->next;
4014 paramsize[nparam] = 0;
4015 white = 0;
4016 continue; /* parameter loop */
4018 if (ch == '{' &&
4019 (brackets > 0 || (brackets == 0 &&
4020 !paramsize[nparam])))
4022 if (!(brackets++)) {
4023 params[nparam] = tline->next;
4024 continue; /* parameter loop */
4027 if (ch == '}' && brackets > 0)
4028 if (--brackets == 0) {
4029 brackets = -1;
4030 continue; /* parameter loop */
4032 if (ch == '(' && !brackets)
4033 paren++;
4034 if (ch == ')' && brackets <= 0)
4035 if (--paren < 0)
4036 break;
4038 if (brackets < 0) {
4039 brackets = 0;
4040 error(ERR_NONFATAL, "braces do not "
4041 "enclose all of macro parameter");
4043 paramsize[nparam] += white + 1;
4044 white = 0;
4045 } /* parameter loop */
4046 nparam++;
4047 while (m && (m->nparam != nparam ||
4048 mstrcmp(m->name, mname,
4049 m->casesense)))
4050 m = m->next;
4051 if (!m)
4052 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4053 "macro `%s' exists, "
4054 "but not taking %d parameters",
4055 mstart->text, nparam);
4058 if (m && m->in_progress)
4059 m = NULL;
4060 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4062 * Design question: should we handle !tline, which
4063 * indicates missing ')' here, or expand those
4064 * macros anyway, which requires the (t) test a few
4065 * lines down?
4067 nasm_free(params);
4068 nasm_free(paramsize);
4069 tline = mstart;
4070 } else {
4072 * Expand the macro: we are placed on the last token of the
4073 * call, so that we can easily split the call from the
4074 * following tokens. We also start by pushing an SMAC_END
4075 * token for the cycle removal.
4077 t = tline;
4078 if (t) {
4079 tline = t->next;
4080 t->next = NULL;
4082 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4083 tt->a.mac = m;
4084 m->in_progress = true;
4085 tline = tt;
4086 list_for_each(t, m->expansion) {
4087 if (t->type >= TOK_SMAC_PARAM) {
4088 Token *pcopy = tline, **ptail = &pcopy;
4089 Token *ttt, *pt;
4090 int i;
4092 ttt = params[t->type - TOK_SMAC_PARAM];
4093 i = paramsize[t->type - TOK_SMAC_PARAM];
4094 while (--i >= 0) {
4095 pt = *ptail = new_Token(tline, ttt->type,
4096 ttt->text, 0);
4097 ptail = &pt->next;
4098 ttt = ttt->next;
4100 tline = pcopy;
4101 } else if (t->type == TOK_PREPROC_Q) {
4102 tt = new_Token(tline, TOK_ID, mname, 0);
4103 tline = tt;
4104 } else if (t->type == TOK_PREPROC_QQ) {
4105 tt = new_Token(tline, TOK_ID, m->name, 0);
4106 tline = tt;
4107 } else {
4108 tt = new_Token(tline, t->type, t->text, 0);
4109 tline = tt;
4114 * Having done that, get rid of the macro call, and clean
4115 * up the parameters.
4117 nasm_free(params);
4118 nasm_free(paramsize);
4119 free_tlist(mstart);
4120 expanded = true;
4121 continue; /* main token loop */
4126 if (tline->type == TOK_SMAC_END) {
4127 tline->a.mac->in_progress = false;
4128 tline = delete_Token(tline);
4129 } else {
4130 t = *tail = tline;
4131 tline = tline->next;
4132 t->a.mac = NULL;
4133 t->next = NULL;
4134 tail = &t->next;
4139 * Now scan the entire line and look for successive TOK_IDs that resulted
4140 * after expansion (they can't be produced by tokenize()). The successive
4141 * TOK_IDs should be concatenated.
4142 * Also we look for %+ tokens and concatenate the tokens before and after
4143 * them (without white spaces in between).
4145 if (expanded && paste_tokens(&thead, true)) {
4147 * If we concatenated something, *and* we had previously expanded
4148 * an actual macro, scan the lines again for macros...
4150 tline = thead;
4151 expanded = false;
4152 goto again;
4155 err:
4156 if (org_tline) {
4157 if (thead) {
4158 *org_tline = *thead;
4159 /* since we just gave text to org_line, don't free it */
4160 thead->text = NULL;
4161 delete_Token(thead);
4162 } else {
4163 /* the expression expanded to empty line;
4164 we can't return NULL for some reasons
4165 we just set the line to a single WHITESPACE token. */
4166 memset(org_tline, 0, sizeof(*org_tline));
4167 org_tline->text = NULL;
4168 org_tline->type = TOK_WHITESPACE;
4170 thead = org_tline;
4173 return thead;
4177 * Similar to expand_smacro but used exclusively with macro identifiers
4178 * right before they are fetched in. The reason is that there can be
4179 * identifiers consisting of several subparts. We consider that if there
4180 * are more than one element forming the name, user wants a expansion,
4181 * otherwise it will be left as-is. Example:
4183 * %define %$abc cde
4185 * the identifier %$abc will be left as-is so that the handler for %define
4186 * will suck it and define the corresponding value. Other case:
4188 * %define _%$abc cde
4190 * In this case user wants name to be expanded *before* %define starts
4191 * working, so we'll expand %$abc into something (if it has a value;
4192 * otherwise it will be left as-is) then concatenate all successive
4193 * PP_IDs into one.
4195 static Token *expand_id(Token * tline)
4197 Token *cur, *oldnext = NULL;
4199 if (!tline || !tline->next)
4200 return tline;
4202 cur = tline;
4203 while (cur->next &&
4204 (cur->next->type == TOK_ID ||
4205 cur->next->type == TOK_PREPROC_ID
4206 || cur->next->type == TOK_NUMBER))
4207 cur = cur->next;
4209 /* If identifier consists of just one token, don't expand */
4210 if (cur == tline)
4211 return tline;
4213 if (cur) {
4214 oldnext = cur->next; /* Detach the tail past identifier */
4215 cur->next = NULL; /* so that expand_smacro stops here */
4218 tline = expand_smacro(tline);
4220 if (cur) {
4221 /* expand_smacro possibly changhed tline; re-scan for EOL */
4222 cur = tline;
4223 while (cur && cur->next)
4224 cur = cur->next;
4225 if (cur)
4226 cur->next = oldnext;
4229 return tline;
4233 * Determine whether the given line constitutes a multi-line macro
4234 * call, and return the MMacro structure called if so. Doesn't have
4235 * to check for an initial label - that's taken care of in
4236 * expand_mmacro - but must check numbers of parameters. Guaranteed
4237 * to be called with tline->type == TOK_ID, so the putative macro
4238 * name is easy to find.
4240 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4242 MMacro *head, *m;
4243 Token **params;
4244 int nparam;
4246 head = (MMacro *) hash_findix(&mmacros, tline->text);
4249 * Efficiency: first we see if any macro exists with the given
4250 * name. If not, we can return NULL immediately. _Then_ we
4251 * count the parameters, and then we look further along the
4252 * list if necessary to find the proper MMacro.
4254 list_for_each(m, head)
4255 if (!mstrcmp(m->name, tline->text, m->casesense))
4256 break;
4257 if (!m)
4258 return NULL;
4261 * OK, we have a potential macro. Count and demarcate the
4262 * parameters.
4264 count_mmac_params(tline->next, &nparam, &params);
4267 * So we know how many parameters we've got. Find the MMacro
4268 * structure that handles this number.
4270 while (m) {
4271 if (m->nparam_min <= nparam
4272 && (m->plus || nparam <= m->nparam_max)) {
4274 * This one is right. Just check if cycle removal
4275 * prohibits us using it before we actually celebrate...
4277 if (m->in_progress > m->max_depth) {
4278 if (m->max_depth > 0) {
4279 error(ERR_WARNING,
4280 "reached maximum recursion depth of %i",
4281 m->max_depth);
4283 nasm_free(params);
4284 return NULL;
4287 * It's right, and we can use it. Add its default
4288 * parameters to the end of our list if necessary.
4290 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4291 params =
4292 nasm_realloc(params,
4293 ((m->nparam_min + m->ndefs +
4294 1) * sizeof(*params)));
4295 while (nparam < m->nparam_min + m->ndefs) {
4296 params[nparam] = m->defaults[nparam - m->nparam_min];
4297 nparam++;
4301 * If we've gone over the maximum parameter count (and
4302 * we're in Plus mode), ignore parameters beyond
4303 * nparam_max.
4305 if (m->plus && nparam > m->nparam_max)
4306 nparam = m->nparam_max;
4308 * Then terminate the parameter list, and leave.
4310 if (!params) { /* need this special case */
4311 params = nasm_malloc(sizeof(*params));
4312 nparam = 0;
4314 params[nparam] = NULL;
4315 *params_array = params;
4316 return m;
4319 * This one wasn't right: look for the next one with the
4320 * same name.
4322 list_for_each(m, m->next)
4323 if (!mstrcmp(m->name, tline->text, m->casesense))
4324 break;
4328 * After all that, we didn't find one with the right number of
4329 * parameters. Issue a warning, and fail to expand the macro.
4331 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4332 "macro `%s' exists, but not taking %d parameters",
4333 tline->text, nparam);
4334 nasm_free(params);
4335 return NULL;
4340 * Save MMacro invocation specific fields in
4341 * preparation for a recursive macro expansion
4343 static void push_mmacro(MMacro *m)
4345 MMacroInvocation *i;
4347 i = nasm_malloc(sizeof(MMacroInvocation));
4348 i->prev = m->prev;
4349 i->params = m->params;
4350 i->iline = m->iline;
4351 i->nparam = m->nparam;
4352 i->rotate = m->rotate;
4353 i->paramlen = m->paramlen;
4354 i->unique = m->unique;
4355 i->condcnt = m->condcnt;
4356 m->prev = i;
4361 * Restore MMacro invocation specific fields that were
4362 * saved during a previous recursive macro expansion
4364 static void pop_mmacro(MMacro *m)
4366 MMacroInvocation *i;
4368 if (m->prev) {
4369 i = m->prev;
4370 m->prev = i->prev;
4371 m->params = i->params;
4372 m->iline = i->iline;
4373 m->nparam = i->nparam;
4374 m->rotate = i->rotate;
4375 m->paramlen = i->paramlen;
4376 m->unique = i->unique;
4377 m->condcnt = i->condcnt;
4378 nasm_free(i);
4384 * Expand the multi-line macro call made by the given line, if
4385 * there is one to be expanded. If there is, push the expansion on
4386 * istk->expansion and return 1. Otherwise return 0.
4388 static int expand_mmacro(Token * tline)
4390 Token *startline = tline;
4391 Token *label = NULL;
4392 int dont_prepend = 0;
4393 Token **params, *t, *mtok, *tt;
4394 MMacro *m;
4395 Line *l, *ll;
4396 int i, nparam, *paramlen;
4397 const char *mname;
4399 t = tline;
4400 skip_white_(t);
4401 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4402 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4403 return 0;
4404 mtok = t;
4405 m = is_mmacro(t, &params);
4406 if (m) {
4407 mname = t->text;
4408 } else {
4409 Token *last;
4411 * We have an id which isn't a macro call. We'll assume
4412 * it might be a label; we'll also check to see if a
4413 * colon follows it. Then, if there's another id after
4414 * that lot, we'll check it again for macro-hood.
4416 label = last = t;
4417 t = t->next;
4418 if (tok_type_(t, TOK_WHITESPACE))
4419 last = t, t = t->next;
4420 if (tok_is_(t, ":")) {
4421 dont_prepend = 1;
4422 last = t, t = t->next;
4423 if (tok_type_(t, TOK_WHITESPACE))
4424 last = t, t = t->next;
4426 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4427 return 0;
4428 last->next = NULL;
4429 mname = t->text;
4430 tline = t;
4434 * Fix up the parameters: this involves stripping leading and
4435 * trailing whitespace, then stripping braces if they are
4436 * present.
4438 for (nparam = 0; params[nparam]; nparam++) ;
4439 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4441 for (i = 0; params[i]; i++) {
4442 int brace = false;
4443 int comma = (!m->plus || i < nparam - 1);
4445 t = params[i];
4446 skip_white_(t);
4447 if (tok_is_(t, "{"))
4448 t = t->next, brace = true, comma = false;
4449 params[i] = t;
4450 paramlen[i] = 0;
4451 while (t) {
4452 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4453 break; /* ... because we have hit a comma */
4454 if (comma && t->type == TOK_WHITESPACE
4455 && tok_is_(t->next, ","))
4456 break; /* ... or a space then a comma */
4457 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4458 break; /* ... or a brace */
4459 t = t->next;
4460 paramlen[i]++;
4465 * OK, we have a MMacro structure together with a set of
4466 * parameters. We must now go through the expansion and push
4467 * copies of each Line on to istk->expansion. Substitution of
4468 * parameter tokens and macro-local tokens doesn't get done
4469 * until the single-line macro substitution process; this is
4470 * because delaying them allows us to change the semantics
4471 * later through %rotate.
4473 * First, push an end marker on to istk->expansion, mark this
4474 * macro as in progress, and set up its invocation-specific
4475 * variables.
4477 ll = nasm_malloc(sizeof(Line));
4478 ll->next = istk->expansion;
4479 ll->finishes = m;
4480 ll->first = NULL;
4481 istk->expansion = ll;
4484 * Save the previous MMacro expansion in the case of
4485 * macro recursion
4487 if (m->max_depth && m->in_progress)
4488 push_mmacro(m);
4490 m->in_progress ++;
4491 m->params = params;
4492 m->iline = tline;
4493 m->nparam = nparam;
4494 m->rotate = 0;
4495 m->paramlen = paramlen;
4496 m->unique = unique++;
4497 m->lineno = 0;
4498 m->condcnt = 0;
4500 m->next_active = istk->mstk;
4501 istk->mstk = m;
4503 list_for_each(l, m->expansion) {
4504 Token **tail;
4506 ll = nasm_malloc(sizeof(Line));
4507 ll->finishes = NULL;
4508 ll->next = istk->expansion;
4509 istk->expansion = ll;
4510 tail = &ll->first;
4512 list_for_each(t, l->first) {
4513 Token *x = t;
4514 switch (t->type) {
4515 case TOK_PREPROC_Q:
4516 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4517 break;
4518 case TOK_PREPROC_QQ:
4519 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4520 break;
4521 case TOK_PREPROC_ID:
4522 if (t->text[1] == '0' && t->text[2] == '0') {
4523 dont_prepend = -1;
4524 x = label;
4525 if (!x)
4526 continue;
4528 /* fall through */
4529 default:
4530 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4531 break;
4533 tail = &tt->next;
4535 *tail = NULL;
4539 * If we had a label, push it on as the first line of
4540 * the macro expansion.
4542 if (label) {
4543 if (dont_prepend < 0)
4544 free_tlist(startline);
4545 else {
4546 ll = nasm_malloc(sizeof(Line));
4547 ll->finishes = NULL;
4548 ll->next = istk->expansion;
4549 istk->expansion = ll;
4550 ll->first = startline;
4551 if (!dont_prepend) {
4552 while (label->next)
4553 label = label->next;
4554 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4559 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4561 return 1;
4564 /* The function that actually does the error reporting */
4565 static void verror(int severity, const char *fmt, va_list arg)
4567 char buff[1024];
4569 vsnprintf(buff, sizeof(buff), fmt, arg);
4571 if (istk && istk->mstk && istk->mstk->name)
4572 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4573 istk->mstk->lineno, buff);
4574 else
4575 nasm_error(severity, "%s", buff);
4579 * Since preprocessor always operate only on the line that didn't
4580 * arrived yet, we should always use ERR_OFFBY1.
4582 static void error(int severity, const char *fmt, ...)
4584 va_list arg;
4586 /* If we're in a dead branch of IF or something like it, ignore the error */
4587 if (istk && istk->conds && !emitting(istk->conds->state))
4588 return;
4590 va_start(arg, fmt);
4591 verror(severity, fmt, arg);
4592 va_end(arg);
4596 * Because %else etc are evaluated in the state context
4597 * of the previous branch, errors might get lost with error():
4598 * %if 0 ... %else trailing garbage ... %endif
4599 * So %else etc should report errors with this function.
4601 static void error_precond(int severity, const char *fmt, ...)
4603 va_list arg;
4605 /* Only ignore the error if it's really in a dead branch */
4606 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4607 return;
4609 va_start(arg, fmt);
4610 verror(severity, fmt, arg);
4611 va_end(arg);
4614 static void
4615 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4617 Token *t;
4619 cstk = NULL;
4620 istk = nasm_malloc(sizeof(Include));
4621 istk->next = NULL;
4622 istk->conds = NULL;
4623 istk->expansion = NULL;
4624 istk->mstk = NULL;
4625 istk->fp = fopen(file, "r");
4626 istk->fname = NULL;
4627 src_set_fname(nasm_strdup(file));
4628 src_set_linnum(0);
4629 istk->lineinc = 1;
4630 if (!istk->fp)
4631 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4632 file);
4633 defining = NULL;
4634 nested_mac_count = 0;
4635 nested_rep_count = 0;
4636 init_macros();
4637 unique = 0;
4638 if (tasm_compatible_mode) {
4639 stdmacpos = nasm_stdmac;
4640 } else {
4641 stdmacpos = nasm_stdmac_after_tasm;
4643 any_extrastdmac = extrastdmac && *extrastdmac;
4644 do_predef = true;
4645 list = listgen;
4648 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4649 * The caller, however, will also pass in 3 for preprocess-only so
4650 * we can set __PASS__ accordingly.
4652 pass = apass > 2 ? 2 : apass;
4654 dephead = deptail = deplist;
4655 if (deplist) {
4656 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4657 sl->next = NULL;
4658 strcpy(sl->str, file);
4659 *deptail = sl;
4660 deptail = &sl->next;
4664 * Define the __PASS__ macro. This is defined here unlike
4665 * all the other builtins, because it is special -- it varies between
4666 * passes.
4668 t = nasm_malloc(sizeof(*t));
4669 t->next = NULL;
4670 make_tok_num(t, apass);
4671 t->a.mac = NULL;
4672 define_smacro(NULL, "__PASS__", true, 0, t);
4675 static char *pp_getline(void)
4677 char *line;
4678 Token *tline;
4680 while (1) {
4682 * Fetch a tokenized line, either from the macro-expansion
4683 * buffer or from the input file.
4685 tline = NULL;
4686 while (istk->expansion && istk->expansion->finishes) {
4687 Line *l = istk->expansion;
4688 if (!l->finishes->name && l->finishes->in_progress > 1) {
4689 Line *ll;
4692 * This is a macro-end marker for a macro with no
4693 * name, which means it's not really a macro at all
4694 * but a %rep block, and the `in_progress' field is
4695 * more than 1, meaning that we still need to
4696 * repeat. (1 means the natural last repetition; 0
4697 * means termination by %exitrep.) We have
4698 * therefore expanded up to the %endrep, and must
4699 * push the whole block on to the expansion buffer
4700 * again. We don't bother to remove the macro-end
4701 * marker: we'd only have to generate another one
4702 * if we did.
4704 l->finishes->in_progress--;
4705 list_for_each(l, l->finishes->expansion) {
4706 Token *t, *tt, **tail;
4708 ll = nasm_malloc(sizeof(Line));
4709 ll->next = istk->expansion;
4710 ll->finishes = NULL;
4711 ll->first = NULL;
4712 tail = &ll->first;
4714 list_for_each(t, l->first) {
4715 if (t->text || t->type == TOK_WHITESPACE) {
4716 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4717 tail = &tt->next;
4721 istk->expansion = ll;
4723 } else {
4725 * Check whether a `%rep' was started and not ended
4726 * within this macro expansion. This can happen and
4727 * should be detected. It's a fatal error because
4728 * I'm too confused to work out how to recover
4729 * sensibly from it.
4731 if (defining) {
4732 if (defining->name)
4733 error(ERR_PANIC,
4734 "defining with name in expansion");
4735 else if (istk->mstk->name)
4736 error(ERR_FATAL,
4737 "`%%rep' without `%%endrep' within"
4738 " expansion of macro `%s'",
4739 istk->mstk->name);
4743 * FIXME: investigate the relationship at this point between
4744 * istk->mstk and l->finishes
4747 MMacro *m = istk->mstk;
4748 istk->mstk = m->next_active;
4749 if (m->name) {
4751 * This was a real macro call, not a %rep, and
4752 * therefore the parameter information needs to
4753 * be freed.
4755 if (m->prev) {
4756 pop_mmacro(m);
4757 l->finishes->in_progress --;
4758 } else {
4759 nasm_free(m->params);
4760 free_tlist(m->iline);
4761 nasm_free(m->paramlen);
4762 l->finishes->in_progress = 0;
4764 } else
4765 free_mmacro(m);
4767 istk->expansion = l->next;
4768 nasm_free(l);
4769 list->downlevel(LIST_MACRO);
4772 while (1) { /* until we get a line we can use */
4774 if (istk->expansion) { /* from a macro expansion */
4775 char *p;
4776 Line *l = istk->expansion;
4777 if (istk->mstk)
4778 istk->mstk->lineno++;
4779 tline = l->first;
4780 istk->expansion = l->next;
4781 nasm_free(l);
4782 p = detoken(tline, false);
4783 list->line(LIST_MACRO, p);
4784 nasm_free(p);
4785 break;
4787 line = read_line();
4788 if (line) { /* from the current input file */
4789 line = prepreproc(line);
4790 tline = tokenize(line);
4791 nasm_free(line);
4792 break;
4795 * The current file has ended; work down the istk
4798 Include *i = istk;
4799 fclose(i->fp);
4800 if (i->conds)
4801 error(ERR_FATAL,
4802 "expected `%%endif' before end of file");
4803 /* only set line and file name if there's a next node */
4804 if (i->next) {
4805 src_set_linnum(i->lineno);
4806 nasm_free(src_set_fname(i->fname));
4808 istk = i->next;
4809 list->downlevel(LIST_INCLUDE);
4810 nasm_free(i);
4811 if (!istk)
4812 return NULL;
4813 if (istk->expansion && istk->expansion->finishes)
4814 break;
4819 * We must expand MMacro parameters and MMacro-local labels
4820 * _before_ we plunge into directive processing, to cope
4821 * with things like `%define something %1' such as STRUC
4822 * uses. Unless we're _defining_ a MMacro, in which case
4823 * those tokens should be left alone to go into the
4824 * definition; and unless we're in a non-emitting
4825 * condition, in which case we don't want to meddle with
4826 * anything.
4828 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4829 && !(istk->mstk && !istk->mstk->in_progress)) {
4830 tline = expand_mmac_params(tline);
4834 * Check the line to see if it's a preprocessor directive.
4836 if (do_directive(tline) == DIRECTIVE_FOUND) {
4837 continue;
4838 } else if (defining) {
4840 * We're defining a multi-line macro. We emit nothing
4841 * at all, and just
4842 * shove the tokenized line on to the macro definition.
4844 Line *l = nasm_malloc(sizeof(Line));
4845 l->next = defining->expansion;
4846 l->first = tline;
4847 l->finishes = NULL;
4848 defining->expansion = l;
4849 continue;
4850 } else if (istk->conds && !emitting(istk->conds->state)) {
4852 * We're in a non-emitting branch of a condition block.
4853 * Emit nothing at all, not even a blank line: when we
4854 * emerge from the condition we'll give a line-number
4855 * directive so we keep our place correctly.
4857 free_tlist(tline);
4858 continue;
4859 } else if (istk->mstk && !istk->mstk->in_progress) {
4861 * We're in a %rep block which has been terminated, so
4862 * we're walking through to the %endrep without
4863 * emitting anything. Emit nothing at all, not even a
4864 * blank line: when we emerge from the %rep block we'll
4865 * give a line-number directive so we keep our place
4866 * correctly.
4868 free_tlist(tline);
4869 continue;
4870 } else {
4871 tline = expand_smacro(tline);
4872 if (!expand_mmacro(tline)) {
4874 * De-tokenize the line again, and emit it.
4876 line = detoken(tline, true);
4877 free_tlist(tline);
4878 break;
4879 } else {
4880 continue; /* expand_mmacro calls free_tlist */
4885 return line;
4888 static void pp_cleanup(int pass)
4890 if (defining) {
4891 if (defining->name) {
4892 error(ERR_NONFATAL,
4893 "end of file while still defining macro `%s'",
4894 defining->name);
4895 } else {
4896 error(ERR_NONFATAL, "end of file while still in %%rep");
4899 free_mmacro(defining);
4900 defining = NULL;
4902 while (cstk)
4903 ctx_pop();
4904 free_macros();
4905 while (istk) {
4906 Include *i = istk;
4907 istk = istk->next;
4908 fclose(i->fp);
4909 nasm_free(i->fname);
4910 nasm_free(i);
4912 while (cstk)
4913 ctx_pop();
4914 nasm_free(src_set_fname(NULL));
4915 if (pass == 0) {
4916 IncPath *i;
4917 free_llist(predef);
4918 delete_Blocks();
4919 while ((i = ipath)) {
4920 ipath = i->next;
4921 if (i->path)
4922 nasm_free(i->path);
4923 nasm_free(i);
4928 void pp_include_path(char *path)
4930 IncPath *i;
4932 i = nasm_malloc(sizeof(IncPath));
4933 i->path = path ? nasm_strdup(path) : NULL;
4934 i->next = NULL;
4936 if (ipath) {
4937 IncPath *j = ipath;
4938 while (j->next)
4939 j = j->next;
4940 j->next = i;
4941 } else {
4942 ipath = i;
4946 void pp_pre_include(char *fname)
4948 Token *inc, *space, *name;
4949 Line *l;
4951 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4952 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4953 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4955 l = nasm_malloc(sizeof(Line));
4956 l->next = predef;
4957 l->first = inc;
4958 l->finishes = NULL;
4959 predef = l;
4962 void pp_pre_define(char *definition)
4964 Token *def, *space;
4965 Line *l;
4966 char *equals;
4968 equals = strchr(definition, '=');
4969 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4970 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4971 if (equals)
4972 *equals = ' ';
4973 space->next = tokenize(definition);
4974 if (equals)
4975 *equals = '=';
4977 l = nasm_malloc(sizeof(Line));
4978 l->next = predef;
4979 l->first = def;
4980 l->finishes = NULL;
4981 predef = l;
4984 void pp_pre_undefine(char *definition)
4986 Token *def, *space;
4987 Line *l;
4989 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4990 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4991 space->next = tokenize(definition);
4993 l = nasm_malloc(sizeof(Line));
4994 l->next = predef;
4995 l->first = def;
4996 l->finishes = NULL;
4997 predef = l;
5001 * Added by Keith Kanios:
5003 * This function is used to assist with "runtime" preprocessor
5004 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5006 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5007 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5010 void pp_runtime(char *definition)
5012 Token *def;
5014 def = tokenize(definition);
5015 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5016 free_tlist(def);
5020 void pp_extra_stdmac(macros_t *macros)
5022 extrastdmac = macros;
5025 static void make_tok_num(Token * tok, int64_t val)
5027 char numbuf[20];
5028 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5029 tok->text = nasm_strdup(numbuf);
5030 tok->type = TOK_NUMBER;
5033 Preproc nasmpp = {
5034 pp_reset,
5035 pp_getline,
5036 pp_cleanup