BR3028880: Make nonexistent environment variable being fatal error
[nasm/sigaren-mirror.git] / preproc.c
blob570f9d1192b03e5b5aff298692cc96b983133e53
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;
684 * read line from standart macros set,
685 * if there no more left -- return NULL
687 static char *line_from_stdmac(void)
689 unsigned char c;
690 const unsigned char *p = stdmacpos;
691 char *line, *q;
692 size_t len = 0;
694 if (!stdmacpos)
695 return NULL;
697 while ((c = *p++)) {
698 if (c >= 0x80)
699 len += pp_directives_len[c - 0x80] + 1;
700 else
701 len++;
704 line = nasm_malloc(len + 1);
705 q = line;
706 while ((c = *stdmacpos++)) {
707 if (c >= 0x80) {
708 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
709 q += pp_directives_len[c - 0x80];
710 *q++ = ' ';
711 } else {
712 *q++ = c;
715 stdmacpos = p;
716 *q = '\0';
718 if (!*stdmacpos) {
719 /* This was the last of the standard macro chain... */
720 stdmacpos = NULL;
721 if (any_extrastdmac) {
722 stdmacpos = extrastdmac;
723 any_extrastdmac = false;
724 } else if (do_predef) {
725 Line *pd, *l;
726 Token *head, **tail, *t;
729 * Nasty hack: here we push the contents of
730 * `predef' on to the top-level expansion stack,
731 * since this is the most convenient way to
732 * implement the pre-include and pre-define
733 * features.
735 list_for_each(pd, predef) {
736 head = NULL;
737 tail = &head;
738 list_for_each(t, pd->first) {
739 *tail = new_Token(NULL, t->type, t->text, 0);
740 tail = &(*tail)->next;
743 l = nasm_malloc(sizeof(Line));
744 l->next = istk->expansion;
745 l->first = head;
746 l->finishes = NULL;
748 istk->expansion = l;
750 do_predef = false;
754 return line;
757 #define BUF_DELTA 512
759 * Read a line from the top file in istk, handling multiple CR/LFs
760 * at the end of the line read, and handling spurious ^Zs. Will
761 * return lines from the standard macro set if this has not already
762 * been done.
764 static char *read_line(void)
766 char *buffer, *p, *q;
767 int bufsize, continued_count;
770 * standart macros set (predefined) goes first
772 p = line_from_stdmac();
773 if (p)
774 return p;
777 * regular read from a file
779 bufsize = BUF_DELTA;
780 buffer = nasm_malloc(BUF_DELTA);
781 p = buffer;
782 continued_count = 0;
783 while (1) {
784 q = fgets(p, bufsize - (p - buffer), istk->fp);
785 if (!q)
786 break;
787 p += strlen(p);
788 if (p > buffer && p[-1] == '\n') {
790 * Convert backslash-CRLF line continuation sequences into
791 * nothing at all (for DOS and Windows)
793 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
794 p -= 3;
795 *p = 0;
796 continued_count++;
799 * Also convert backslash-LF line continuation sequences into
800 * nothing at all (for Unix)
802 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
803 p -= 2;
804 *p = 0;
805 continued_count++;
806 } else {
807 break;
810 if (p - buffer > bufsize - 10) {
811 int32_t offset = p - buffer;
812 bufsize += BUF_DELTA;
813 buffer = nasm_realloc(buffer, bufsize);
814 p = buffer + offset; /* prevent stale-pointer problems */
818 if (!q && p == buffer) {
819 nasm_free(buffer);
820 return NULL;
823 src_set_linnum(src_get_linnum() + istk->lineinc +
824 (continued_count * istk->lineinc));
827 * Play safe: remove CRs as well as LFs, if any of either are
828 * present at the end of the line.
830 while (--p >= buffer && (*p == '\n' || *p == '\r'))
831 *p = '\0';
834 * Handle spurious ^Z, which may be inserted into source files
835 * by some file transfer utilities.
837 buffer[strcspn(buffer, "\032")] = '\0';
839 list->line(LIST_READ, buffer);
841 return buffer;
845 * Tokenize a line of text. This is a very simple process since we
846 * don't need to parse the value out of e.g. numeric tokens: we
847 * simply split one string into many.
849 static Token *tokenize(char *line)
851 char c, *p = line;
852 enum pp_token_type type;
853 Token *list = NULL;
854 Token *t, **tail = &list;
856 while (*line) {
857 p = line;
858 if (*p == '%') {
859 p++;
860 if (*p == '+' && !nasm_isdigit(p[1])) {
861 p++;
862 type = TOK_PASTE;
863 } else if (nasm_isdigit(*p) ||
864 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
865 do {
866 p++;
868 while (nasm_isdigit(*p));
869 type = TOK_PREPROC_ID;
870 } else if (*p == '{') {
871 p++;
872 while (*p && *p != '}') {
873 p[-1] = *p;
874 p++;
876 p[-1] = '\0';
877 if (*p)
878 p++;
879 type = TOK_PREPROC_ID;
880 } else if (*p == '[') {
881 int lvl = 1;
882 line += 2; /* Skip the leading %[ */
883 p++;
884 while (lvl && (c = *p++)) {
885 switch (c) {
886 case ']':
887 lvl--;
888 break;
889 case '%':
890 if (*p == '[')
891 lvl++;
892 break;
893 case '\'':
894 case '\"':
895 case '`':
896 p = nasm_skip_string(p - 1) + 1;
897 break;
898 default:
899 break;
902 p--;
903 if (*p)
904 *p++ = '\0';
905 if (lvl)
906 error(ERR_NONFATAL, "unterminated %[ construct");
907 type = TOK_INDIRECT;
908 } else if (*p == '?') {
909 type = TOK_PREPROC_Q; /* %? */
910 p++;
911 if (*p == '?') {
912 type = TOK_PREPROC_QQ; /* %?? */
913 p++;
915 } else if (isidchar(*p) ||
916 ((*p == '!' || *p == '%' || *p == '$') &&
917 isidchar(p[1]))) {
918 do {
919 p++;
921 while (isidchar(*p));
922 type = TOK_PREPROC_ID;
923 } else {
924 type = TOK_OTHER;
925 if (*p == '%')
926 p++;
928 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
929 type = TOK_ID;
930 p++;
931 while (*p && isidchar(*p))
932 p++;
933 } else if (*p == '\'' || *p == '"' || *p == '`') {
935 * A string token.
937 type = TOK_STRING;
938 p = nasm_skip_string(p);
940 if (*p) {
941 p++;
942 } else {
943 error(ERR_WARNING|ERR_PASS1, "unterminated string");
944 /* Handling unterminated strings by UNV */
945 /* type = -1; */
947 } else if (p[0] == '$' && p[1] == '$') {
948 type = TOK_OTHER; /* TOKEN_BASE */
949 p += 2;
950 } else if (isnumstart(*p)) {
951 bool is_hex = false;
952 bool is_float = false;
953 bool has_e = false;
954 char c, *r;
957 * A numeric token.
960 if (*p == '$') {
961 p++;
962 is_hex = true;
965 for (;;) {
966 c = *p++;
968 if (!is_hex && (c == 'e' || c == 'E')) {
969 has_e = true;
970 if (*p == '+' || *p == '-') {
972 * e can only be followed by +/- if it is either a
973 * prefixed hex number or a floating-point number
975 p++;
976 is_float = true;
978 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
979 is_hex = true;
980 } else if (c == 'P' || c == 'p') {
981 is_float = true;
982 if (*p == '+' || *p == '-')
983 p++;
984 } else if (isnumchar(c) || c == '_')
985 ; /* just advance */
986 else if (c == '.') {
988 * we need to deal with consequences of the legacy
989 * parser, like "1.nolist" being two tokens
990 * (TOK_NUMBER, TOK_ID) here; at least give it
991 * a shot for now. In the future, we probably need
992 * a flex-based scanner with proper pattern matching
993 * to do it as well as it can be done. Nothing in
994 * the world is going to help the person who wants
995 * 0x123.p16 interpreted as two tokens, though.
997 r = p;
998 while (*r == '_')
999 r++;
1001 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1002 (!is_hex && (*r == 'e' || *r == 'E')) ||
1003 (*r == 'p' || *r == 'P')) {
1004 p = r;
1005 is_float = true;
1006 } else
1007 break; /* Terminate the token */
1008 } else
1009 break;
1011 p--; /* Point to first character beyond number */
1013 if (p == line+1 && *line == '$') {
1014 type = TOK_OTHER; /* TOKEN_HERE */
1015 } else {
1016 if (has_e && !is_hex) {
1017 /* 1e13 is floating-point, but 1e13h is not */
1018 is_float = true;
1021 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1023 } else if (nasm_isspace(*p)) {
1024 type = TOK_WHITESPACE;
1025 p = nasm_skip_spaces(p);
1027 * Whitespace just before end-of-line is discarded by
1028 * pretending it's a comment; whitespace just before a
1029 * comment gets lumped into the comment.
1031 if (!*p || *p == ';') {
1032 type = TOK_COMMENT;
1033 while (*p)
1034 p++;
1036 } else if (*p == ';') {
1037 type = TOK_COMMENT;
1038 while (*p)
1039 p++;
1040 } else {
1042 * Anything else is an operator of some kind. We check
1043 * for all the double-character operators (>>, <<, //,
1044 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1045 * else is a single-character operator.
1047 type = TOK_OTHER;
1048 if ((p[0] == '>' && p[1] == '>') ||
1049 (p[0] == '<' && p[1] == '<') ||
1050 (p[0] == '/' && p[1] == '/') ||
1051 (p[0] == '<' && p[1] == '=') ||
1052 (p[0] == '>' && p[1] == '=') ||
1053 (p[0] == '=' && p[1] == '=') ||
1054 (p[0] == '!' && p[1] == '=') ||
1055 (p[0] == '<' && p[1] == '>') ||
1056 (p[0] == '&' && p[1] == '&') ||
1057 (p[0] == '|' && p[1] == '|') ||
1058 (p[0] == '^' && p[1] == '^')) {
1059 p++;
1061 p++;
1064 /* Handling unterminated string by UNV */
1065 /*if (type == -1)
1067 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1068 t->text[p-line] = *line;
1069 tail = &t->next;
1071 else */
1072 if (type != TOK_COMMENT) {
1073 *tail = t = new_Token(NULL, type, line, p - line);
1074 tail = &t->next;
1076 line = p;
1078 return list;
1082 * this function allocates a new managed block of memory and
1083 * returns a pointer to the block. The managed blocks are
1084 * deleted only all at once by the delete_Blocks function.
1086 static void *new_Block(size_t size)
1088 Blocks *b = &blocks;
1090 /* first, get to the end of the linked list */
1091 while (b->next)
1092 b = b->next;
1093 /* now allocate the requested chunk */
1094 b->chunk = nasm_malloc(size);
1096 /* now allocate a new block for the next request */
1097 b->next = nasm_malloc(sizeof(Blocks));
1098 /* and initialize the contents of the new block */
1099 b->next->next = NULL;
1100 b->next->chunk = NULL;
1101 return b->chunk;
1105 * this function deletes all managed blocks of memory
1107 static void delete_Blocks(void)
1109 Blocks *a, *b = &blocks;
1112 * keep in mind that the first block, pointed to by blocks
1113 * is a static and not dynamically allocated, so we don't
1114 * free it.
1116 while (b) {
1117 if (b->chunk)
1118 nasm_free(b->chunk);
1119 a = b;
1120 b = b->next;
1121 if (a != &blocks)
1122 nasm_free(a);
1127 * this function creates a new Token and passes a pointer to it
1128 * back to the caller. It sets the type and text elements, and
1129 * also the a.mac and next elements to NULL.
1131 static Token *new_Token(Token * next, enum pp_token_type type,
1132 const char *text, int txtlen)
1134 Token *t;
1135 int i;
1137 if (!freeTokens) {
1138 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1139 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1140 freeTokens[i].next = &freeTokens[i + 1];
1141 freeTokens[i].next = NULL;
1143 t = freeTokens;
1144 freeTokens = t->next;
1145 t->next = next;
1146 t->a.mac = NULL;
1147 t->type = type;
1148 if (type == TOK_WHITESPACE || !text) {
1149 t->text = NULL;
1150 } else {
1151 if (txtlen == 0)
1152 txtlen = strlen(text);
1153 t->text = nasm_malloc(txtlen+1);
1154 memcpy(t->text, text, txtlen);
1155 t->text[txtlen] = '\0';
1157 return t;
1160 static Token *delete_Token(Token * t)
1162 Token *next = t->next;
1163 nasm_free(t->text);
1164 t->next = freeTokens;
1165 freeTokens = t;
1166 return next;
1170 * Convert a line of tokens back into text.
1171 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1172 * will be transformed into ..@ctxnum.xxx
1174 static char *detoken(Token * tlist, bool expand_locals)
1176 Token *t;
1177 char *line, *p;
1178 const char *q;
1179 int len = 0;
1181 list_for_each(t, tlist) {
1182 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1183 char *p = getenv(t->text + 2);
1184 char *q = t->text;
1185 if (p)
1186 t->text = nasm_strdup(p);
1187 else
1188 error(ERR_FATAL, "`%s' is empty", q + 2);
1189 nasm_free(q);
1191 /* Expand local macros here and not during preprocessing */
1192 if (expand_locals &&
1193 t->type == TOK_PREPROC_ID && t->text &&
1194 t->text[0] == '%' && t->text[1] == '$') {
1195 const char *q;
1196 char *p;
1197 Context *ctx = get_ctx(t->text, &q, false);
1198 if (ctx) {
1199 char buffer[40];
1200 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1201 p = nasm_strcat(buffer, q);
1202 nasm_free(t->text);
1203 t->text = p;
1206 if (t->type == TOK_WHITESPACE)
1207 len++;
1208 else if (t->text)
1209 len += strlen(t->text);
1212 p = line = nasm_malloc(len + 1);
1214 list_for_each(t, tlist) {
1215 if (t->type == TOK_WHITESPACE) {
1216 *p++ = ' ';
1217 } else if (t->text) {
1218 q = t->text;
1219 while (*q)
1220 *p++ = *q++;
1223 *p = '\0';
1225 return line;
1229 * A scanner, suitable for use by the expression evaluator, which
1230 * operates on a line of Tokens. Expects a pointer to a pointer to
1231 * the first token in the line to be passed in as its private_data
1232 * field.
1234 * FIX: This really needs to be unified with stdscan.
1236 static int ppscan(void *private_data, struct tokenval *tokval)
1238 Token **tlineptr = private_data;
1239 Token *tline;
1240 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1242 do {
1243 tline = *tlineptr;
1244 *tlineptr = tline ? tline->next : NULL;
1245 } while (tline && (tline->type == TOK_WHITESPACE ||
1246 tline->type == TOK_COMMENT));
1248 if (!tline)
1249 return tokval->t_type = TOKEN_EOS;
1251 tokval->t_charptr = tline->text;
1253 if (tline->text[0] == '$' && !tline->text[1])
1254 return tokval->t_type = TOKEN_HERE;
1255 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1256 return tokval->t_type = TOKEN_BASE;
1258 if (tline->type == TOK_ID) {
1259 p = tokval->t_charptr = tline->text;
1260 if (p[0] == '$') {
1261 tokval->t_charptr++;
1262 return tokval->t_type = TOKEN_ID;
1265 for (r = p, s = ourcopy; *r; r++) {
1266 if (r >= p+MAX_KEYWORD)
1267 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1268 *s++ = nasm_tolower(*r);
1270 *s = '\0';
1271 /* right, so we have an identifier sitting in temp storage. now,
1272 * is it actually a register or instruction name, or what? */
1273 return nasm_token_hash(ourcopy, tokval);
1276 if (tline->type == TOK_NUMBER) {
1277 bool rn_error;
1278 tokval->t_integer = readnum(tline->text, &rn_error);
1279 tokval->t_charptr = tline->text;
1280 if (rn_error)
1281 return tokval->t_type = TOKEN_ERRNUM;
1282 else
1283 return tokval->t_type = TOKEN_NUM;
1286 if (tline->type == TOK_FLOAT) {
1287 return tokval->t_type = TOKEN_FLOAT;
1290 if (tline->type == TOK_STRING) {
1291 char bq, *ep;
1293 bq = tline->text[0];
1294 tokval->t_charptr = tline->text;
1295 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1297 if (ep[0] != bq || ep[1] != '\0')
1298 return tokval->t_type = TOKEN_ERRSTR;
1299 else
1300 return tokval->t_type = TOKEN_STR;
1303 if (tline->type == TOK_OTHER) {
1304 if (!strcmp(tline->text, "<<"))
1305 return tokval->t_type = TOKEN_SHL;
1306 if (!strcmp(tline->text, ">>"))
1307 return tokval->t_type = TOKEN_SHR;
1308 if (!strcmp(tline->text, "//"))
1309 return tokval->t_type = TOKEN_SDIV;
1310 if (!strcmp(tline->text, "%%"))
1311 return tokval->t_type = TOKEN_SMOD;
1312 if (!strcmp(tline->text, "=="))
1313 return tokval->t_type = TOKEN_EQ;
1314 if (!strcmp(tline->text, "<>"))
1315 return tokval->t_type = TOKEN_NE;
1316 if (!strcmp(tline->text, "!="))
1317 return tokval->t_type = TOKEN_NE;
1318 if (!strcmp(tline->text, "<="))
1319 return tokval->t_type = TOKEN_LE;
1320 if (!strcmp(tline->text, ">="))
1321 return tokval->t_type = TOKEN_GE;
1322 if (!strcmp(tline->text, "&&"))
1323 return tokval->t_type = TOKEN_DBL_AND;
1324 if (!strcmp(tline->text, "^^"))
1325 return tokval->t_type = TOKEN_DBL_XOR;
1326 if (!strcmp(tline->text, "||"))
1327 return tokval->t_type = TOKEN_DBL_OR;
1331 * We have no other options: just return the first character of
1332 * the token text.
1334 return tokval->t_type = tline->text[0];
1338 * Compare a string to the name of an existing macro; this is a
1339 * simple wrapper which calls either strcmp or nasm_stricmp
1340 * depending on the value of the `casesense' parameter.
1342 static int mstrcmp(const char *p, const char *q, bool casesense)
1344 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1348 * Compare a string to the name of an existing macro; this is a
1349 * simple wrapper which calls either strcmp or nasm_stricmp
1350 * depending on the value of the `casesense' parameter.
1352 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1354 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1358 * Return the Context structure associated with a %$ token. Return
1359 * NULL, having _already_ reported an error condition, if the
1360 * context stack isn't deep enough for the supplied number of $
1361 * signs.
1362 * If all_contexts == true, contexts that enclose current are
1363 * also scanned for such smacro, until it is found; if not -
1364 * only the context that directly results from the number of $'s
1365 * in variable's name.
1367 * If "namep" is non-NULL, set it to the pointer to the macro name
1368 * tail, i.e. the part beyond %$...
1370 static Context *get_ctx(const char *name, const char **namep,
1371 bool all_contexts)
1373 Context *ctx;
1374 SMacro *m;
1375 int i;
1377 if (namep)
1378 *namep = name;
1380 if (!name || name[0] != '%' || name[1] != '$')
1381 return NULL;
1383 if (!cstk) {
1384 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1385 return NULL;
1388 name += 2;
1389 ctx = cstk;
1390 i = 0;
1391 while (ctx && *name == '$') {
1392 name++;
1393 i++;
1394 ctx = ctx->next;
1396 if (!ctx) {
1397 error(ERR_NONFATAL, "`%s': context stack is only"
1398 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1399 return NULL;
1402 if (namep)
1403 *namep = name;
1405 if (!all_contexts)
1406 return ctx;
1408 do {
1409 /* Search for this smacro in found context */
1410 m = hash_findix(&ctx->localmac, name);
1411 while (m) {
1412 if (!mstrcmp(m->name, name, m->casesense))
1413 return ctx;
1414 m = m->next;
1416 ctx = ctx->next;
1418 while (ctx);
1419 return NULL;
1423 * Check to see if a file is already in a string list
1425 static bool in_list(const StrList *list, const char *str)
1427 while (list) {
1428 if (!strcmp(list->str, str))
1429 return true;
1430 list = list->next;
1432 return false;
1436 * Open an include file. This routine must always return a valid
1437 * file pointer if it returns - it's responsible for throwing an
1438 * ERR_FATAL and bombing out completely if not. It should also try
1439 * the include path one by one until it finds the file or reaches
1440 * the end of the path.
1442 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1443 bool missing_ok)
1445 FILE *fp;
1446 char *prefix = "";
1447 IncPath *ip = ipath;
1448 int len = strlen(file);
1449 size_t prefix_len = 0;
1450 StrList *sl;
1452 while (1) {
1453 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1454 memcpy(sl->str, prefix, prefix_len);
1455 memcpy(sl->str+prefix_len, file, len+1);
1456 fp = fopen(sl->str, "r");
1457 if (fp && dhead && !in_list(*dhead, sl->str)) {
1458 sl->next = NULL;
1459 **dtail = sl;
1460 *dtail = &sl->next;
1461 } else {
1462 nasm_free(sl);
1464 if (fp)
1465 return fp;
1466 if (!ip) {
1467 if (!missing_ok)
1468 break;
1469 prefix = NULL;
1470 } else {
1471 prefix = ip->path;
1472 ip = ip->next;
1474 if (prefix) {
1475 prefix_len = strlen(prefix);
1476 } else {
1477 /* -MG given and file not found */
1478 if (dhead && !in_list(*dhead, file)) {
1479 sl = nasm_malloc(len+1+sizeof sl->next);
1480 sl->next = NULL;
1481 strcpy(sl->str, file);
1482 **dtail = sl;
1483 *dtail = &sl->next;
1485 return NULL;
1489 error(ERR_FATAL, "unable to open include file `%s'", file);
1490 return NULL;
1494 * Determine if we should warn on defining a single-line macro of
1495 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1496 * return true if _any_ single-line macro of that name is defined.
1497 * Otherwise, will return true if a single-line macro with either
1498 * `nparam' or no parameters is defined.
1500 * If a macro with precisely the right number of parameters is
1501 * defined, or nparam is -1, the address of the definition structure
1502 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1503 * is NULL, no action will be taken regarding its contents, and no
1504 * error will occur.
1506 * Note that this is also called with nparam zero to resolve
1507 * `ifdef'.
1509 * If you already know which context macro belongs to, you can pass
1510 * the context pointer as first parameter; if you won't but name begins
1511 * with %$ the context will be automatically computed. If all_contexts
1512 * is true, macro will be searched in outer contexts as well.
1514 static bool
1515 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1516 bool nocase)
1518 struct hash_table *smtbl;
1519 SMacro *m;
1521 if (ctx) {
1522 smtbl = &ctx->localmac;
1523 } else if (name[0] == '%' && name[1] == '$') {
1524 if (cstk)
1525 ctx = get_ctx(name, &name, false);
1526 if (!ctx)
1527 return false; /* got to return _something_ */
1528 smtbl = &ctx->localmac;
1529 } else {
1530 smtbl = &smacros;
1532 m = (SMacro *) hash_findix(smtbl, name);
1534 while (m) {
1535 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1536 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1537 if (defn) {
1538 if (nparam == (int) m->nparam || nparam == -1)
1539 *defn = m;
1540 else
1541 *defn = NULL;
1543 return true;
1545 m = m->next;
1548 return false;
1552 * Count and mark off the parameters in a multi-line macro call.
1553 * This is called both from within the multi-line macro expansion
1554 * code, and also to mark off the default parameters when provided
1555 * in a %macro definition line.
1557 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1559 int paramsize, brace;
1561 *nparam = paramsize = 0;
1562 *params = NULL;
1563 while (t) {
1564 /* +1: we need space for the final NULL */
1565 if (*nparam+1 >= paramsize) {
1566 paramsize += PARAM_DELTA;
1567 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1569 skip_white_(t);
1570 brace = false;
1571 if (tok_is_(t, "{"))
1572 brace = true;
1573 (*params)[(*nparam)++] = t;
1574 while (tok_isnt_(t, brace ? "}" : ","))
1575 t = t->next;
1576 if (t) { /* got a comma/brace */
1577 t = t->next;
1578 if (brace) {
1580 * Now we've found the closing brace, look further
1581 * for the comma.
1583 skip_white_(t);
1584 if (tok_isnt_(t, ",")) {
1585 error(ERR_NONFATAL,
1586 "braces do not enclose all of macro parameter");
1587 while (tok_isnt_(t, ","))
1588 t = t->next;
1590 if (t)
1591 t = t->next; /* eat the comma */
1598 * Determine whether one of the various `if' conditions is true or
1599 * not.
1601 * We must free the tline we get passed.
1603 static bool if_condition(Token * tline, enum preproc_token ct)
1605 enum pp_conditional i = PP_COND(ct);
1606 bool j;
1607 Token *t, *tt, **tptr, *origline;
1608 struct tokenval tokval;
1609 expr *evalresult;
1610 enum pp_token_type needtype;
1612 origline = tline;
1614 switch (i) {
1615 case PPC_IFCTX:
1616 j = false; /* have we matched yet? */
1617 while (true) {
1618 skip_white_(tline);
1619 if (!tline)
1620 break;
1621 if (tline->type != TOK_ID) {
1622 error(ERR_NONFATAL,
1623 "`%s' expects context identifiers", pp_directives[ct]);
1624 free_tlist(origline);
1625 return -1;
1627 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1628 j = true;
1629 tline = tline->next;
1631 break;
1633 case PPC_IFDEF:
1634 j = false; /* have we matched yet? */
1635 while (tline) {
1636 skip_white_(tline);
1637 if (!tline || (tline->type != TOK_ID &&
1638 (tline->type != TOK_PREPROC_ID ||
1639 tline->text[1] != '$'))) {
1640 error(ERR_NONFATAL,
1641 "`%s' expects macro identifiers", pp_directives[ct]);
1642 goto fail;
1644 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1645 j = true;
1646 tline = tline->next;
1648 break;
1650 case PPC_IFIDN:
1651 case PPC_IFIDNI:
1652 tline = expand_smacro(tline);
1653 t = tt = tline;
1654 while (tok_isnt_(tt, ","))
1655 tt = tt->next;
1656 if (!tt) {
1657 error(ERR_NONFATAL,
1658 "`%s' expects two comma-separated arguments",
1659 pp_directives[ct]);
1660 goto fail;
1662 tt = tt->next;
1663 j = true; /* assume equality unless proved not */
1664 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1665 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1666 error(ERR_NONFATAL, "`%s': more than one comma on line",
1667 pp_directives[ct]);
1668 goto fail;
1670 if (t->type == TOK_WHITESPACE) {
1671 t = t->next;
1672 continue;
1674 if (tt->type == TOK_WHITESPACE) {
1675 tt = tt->next;
1676 continue;
1678 if (tt->type != t->type) {
1679 j = false; /* found mismatching tokens */
1680 break;
1682 /* When comparing strings, need to unquote them first */
1683 if (t->type == TOK_STRING) {
1684 size_t l1 = nasm_unquote(t->text, NULL);
1685 size_t l2 = nasm_unquote(tt->text, NULL);
1687 if (l1 != l2) {
1688 j = false;
1689 break;
1691 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1692 j = false;
1693 break;
1695 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1696 j = false; /* found mismatching tokens */
1697 break;
1700 t = t->next;
1701 tt = tt->next;
1703 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1704 j = false; /* trailing gunk on one end or other */
1705 break;
1707 case PPC_IFMACRO:
1709 bool found = false;
1710 MMacro searching, *mmac;
1712 skip_white_(tline);
1713 tline = expand_id(tline);
1714 if (!tok_type_(tline, TOK_ID)) {
1715 error(ERR_NONFATAL,
1716 "`%s' expects a macro name", pp_directives[ct]);
1717 goto fail;
1719 searching.name = nasm_strdup(tline->text);
1720 searching.casesense = true;
1721 searching.plus = false;
1722 searching.nolist = false;
1723 searching.in_progress = 0;
1724 searching.max_depth = 0;
1725 searching.rep_nest = NULL;
1726 searching.nparam_min = 0;
1727 searching.nparam_max = INT_MAX;
1728 tline = expand_smacro(tline->next);
1729 skip_white_(tline);
1730 if (!tline) {
1731 } else if (!tok_type_(tline, TOK_NUMBER)) {
1732 error(ERR_NONFATAL,
1733 "`%s' expects a parameter count or nothing",
1734 pp_directives[ct]);
1735 } else {
1736 searching.nparam_min = searching.nparam_max =
1737 readnum(tline->text, &j);
1738 if (j)
1739 error(ERR_NONFATAL,
1740 "unable to parse parameter count `%s'",
1741 tline->text);
1743 if (tline && tok_is_(tline->next, "-")) {
1744 tline = tline->next->next;
1745 if (tok_is_(tline, "*"))
1746 searching.nparam_max = INT_MAX;
1747 else if (!tok_type_(tline, TOK_NUMBER))
1748 error(ERR_NONFATAL,
1749 "`%s' expects a parameter count after `-'",
1750 pp_directives[ct]);
1751 else {
1752 searching.nparam_max = readnum(tline->text, &j);
1753 if (j)
1754 error(ERR_NONFATAL,
1755 "unable to parse parameter count `%s'",
1756 tline->text);
1757 if (searching.nparam_min > searching.nparam_max)
1758 error(ERR_NONFATAL,
1759 "minimum parameter count exceeds maximum");
1762 if (tline && tok_is_(tline->next, "+")) {
1763 tline = tline->next;
1764 searching.plus = true;
1766 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1767 while (mmac) {
1768 if (!strcmp(mmac->name, searching.name) &&
1769 (mmac->nparam_min <= searching.nparam_max
1770 || searching.plus)
1771 && (searching.nparam_min <= mmac->nparam_max
1772 || mmac->plus)) {
1773 found = true;
1774 break;
1776 mmac = mmac->next;
1778 if (tline && tline->next)
1779 error(ERR_WARNING|ERR_PASS1,
1780 "trailing garbage after %%ifmacro ignored");
1781 nasm_free(searching.name);
1782 j = found;
1783 break;
1786 case PPC_IFID:
1787 needtype = TOK_ID;
1788 goto iftype;
1789 case PPC_IFNUM:
1790 needtype = TOK_NUMBER;
1791 goto iftype;
1792 case PPC_IFSTR:
1793 needtype = TOK_STRING;
1794 goto iftype;
1796 iftype:
1797 t = tline = expand_smacro(tline);
1799 while (tok_type_(t, TOK_WHITESPACE) ||
1800 (needtype == TOK_NUMBER &&
1801 tok_type_(t, TOK_OTHER) &&
1802 (t->text[0] == '-' || t->text[0] == '+') &&
1803 !t->text[1]))
1804 t = t->next;
1806 j = tok_type_(t, needtype);
1807 break;
1809 case PPC_IFTOKEN:
1810 t = tline = expand_smacro(tline);
1811 while (tok_type_(t, TOK_WHITESPACE))
1812 t = t->next;
1814 j = false;
1815 if (t) {
1816 t = t->next; /* Skip the actual token */
1817 while (tok_type_(t, TOK_WHITESPACE))
1818 t = t->next;
1819 j = !t; /* Should be nothing left */
1821 break;
1823 case PPC_IFEMPTY:
1824 t = tline = expand_smacro(tline);
1825 while (tok_type_(t, TOK_WHITESPACE))
1826 t = t->next;
1828 j = !t; /* Should be empty */
1829 break;
1831 case PPC_IF:
1832 t = tline = expand_smacro(tline);
1833 tptr = &t;
1834 tokval.t_type = TOKEN_INVALID;
1835 evalresult = evaluate(ppscan, tptr, &tokval,
1836 NULL, pass | CRITICAL, error, NULL);
1837 if (!evalresult)
1838 return -1;
1839 if (tokval.t_type)
1840 error(ERR_WARNING|ERR_PASS1,
1841 "trailing garbage after expression ignored");
1842 if (!is_simple(evalresult)) {
1843 error(ERR_NONFATAL,
1844 "non-constant value given to `%s'", pp_directives[ct]);
1845 goto fail;
1847 j = reloc_value(evalresult) != 0;
1848 break;
1850 default:
1851 error(ERR_FATAL,
1852 "preprocessor directive `%s' not yet implemented",
1853 pp_directives[ct]);
1854 goto fail;
1857 free_tlist(origline);
1858 return j ^ PP_NEGATIVE(ct);
1860 fail:
1861 free_tlist(origline);
1862 return -1;
1866 * Common code for defining an smacro
1868 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1869 int nparam, Token *expansion)
1871 SMacro *smac, **smhead;
1872 struct hash_table *smtbl;
1874 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1875 if (!smac) {
1876 error(ERR_WARNING|ERR_PASS1,
1877 "single-line macro `%s' defined both with and"
1878 " without parameters", mname);
1880 * Some instances of the old code considered this a failure,
1881 * some others didn't. What is the right thing to do here?
1883 free_tlist(expansion);
1884 return false; /* Failure */
1885 } else {
1887 * We're redefining, so we have to take over an
1888 * existing SMacro structure. This means freeing
1889 * what was already in it.
1891 nasm_free(smac->name);
1892 free_tlist(smac->expansion);
1894 } else {
1895 smtbl = ctx ? &ctx->localmac : &smacros;
1896 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1897 smac = nasm_malloc(sizeof(SMacro));
1898 smac->next = *smhead;
1899 *smhead = smac;
1901 smac->name = nasm_strdup(mname);
1902 smac->casesense = casesense;
1903 smac->nparam = nparam;
1904 smac->expansion = expansion;
1905 smac->in_progress = false;
1906 return true; /* Success */
1910 * Undefine an smacro
1912 static void undef_smacro(Context *ctx, const char *mname)
1914 SMacro **smhead, *s, **sp;
1915 struct hash_table *smtbl;
1917 smtbl = ctx ? &ctx->localmac : &smacros;
1918 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1920 if (smhead) {
1922 * We now have a macro name... go hunt for it.
1924 sp = smhead;
1925 while ((s = *sp) != NULL) {
1926 if (!mstrcmp(s->name, mname, s->casesense)) {
1927 *sp = s->next;
1928 nasm_free(s->name);
1929 free_tlist(s->expansion);
1930 nasm_free(s);
1931 } else {
1932 sp = &s->next;
1939 * Parse a mmacro specification.
1941 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1943 bool err;
1945 tline = tline->next;
1946 skip_white_(tline);
1947 tline = expand_id(tline);
1948 if (!tok_type_(tline, TOK_ID)) {
1949 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1950 return false;
1953 def->prev = NULL;
1954 def->name = nasm_strdup(tline->text);
1955 def->plus = false;
1956 def->nolist = false;
1957 def->in_progress = 0;
1958 def->rep_nest = NULL;
1959 def->nparam_min = 0;
1960 def->nparam_max = 0;
1962 tline = expand_smacro(tline->next);
1963 skip_white_(tline);
1964 if (!tok_type_(tline, TOK_NUMBER)) {
1965 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1966 } else {
1967 def->nparam_min = def->nparam_max =
1968 readnum(tline->text, &err);
1969 if (err)
1970 error(ERR_NONFATAL,
1971 "unable to parse parameter count `%s'", tline->text);
1973 if (tline && tok_is_(tline->next, "-")) {
1974 tline = tline->next->next;
1975 if (tok_is_(tline, "*")) {
1976 def->nparam_max = INT_MAX;
1977 } else if (!tok_type_(tline, TOK_NUMBER)) {
1978 error(ERR_NONFATAL,
1979 "`%s' expects a parameter count after `-'", directive);
1980 } else {
1981 def->nparam_max = readnum(tline->text, &err);
1982 if (err) {
1983 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1984 tline->text);
1986 if (def->nparam_min > def->nparam_max) {
1987 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1991 if (tline && tok_is_(tline->next, "+")) {
1992 tline = tline->next;
1993 def->plus = true;
1995 if (tline && tok_type_(tline->next, TOK_ID) &&
1996 !nasm_stricmp(tline->next->text, ".nolist")) {
1997 tline = tline->next;
1998 def->nolist = true;
2002 * Handle default parameters.
2004 if (tline && tline->next) {
2005 def->dlist = tline->next;
2006 tline->next = NULL;
2007 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2008 } else {
2009 def->dlist = NULL;
2010 def->defaults = NULL;
2012 def->expansion = NULL;
2014 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2015 !def->plus)
2016 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2017 "too many default macro parameters");
2019 return true;
2024 * Decode a size directive
2026 static int parse_size(const char *str) {
2027 static const char *size_names[] =
2028 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2029 static const int sizes[] =
2030 { 0, 1, 4, 16, 8, 10, 2, 32 };
2032 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2036 * nasm_unquote with error if the string contains NUL characters.
2037 * If the string contains NUL characters, issue an error and return
2038 * the C len, i.e. truncate at the NUL.
2040 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2042 size_t len = nasm_unquote(qstr, NULL);
2043 size_t clen = strlen(qstr);
2045 if (len != clen)
2046 error(ERR_NONFATAL, "NUL character in `%s' directive",
2047 pp_directives[directive]);
2049 return clen;
2053 * find and process preprocessor directive in passed line
2054 * Find out if a line contains a preprocessor directive, and deal
2055 * with it if so.
2057 * If a directive _is_ found, it is the responsibility of this routine
2058 * (and not the caller) to free_tlist() the line.
2060 * @param tline a pointer to the current tokeninzed line linked list
2061 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2064 static int do_directive(Token * tline)
2066 enum preproc_token i;
2067 int j;
2068 bool err;
2069 int nparam;
2070 bool nolist;
2071 bool casesense;
2072 int k, m;
2073 int offset;
2074 char *p, *pp;
2075 const char *mname;
2076 Include *inc;
2077 Context *ctx;
2078 Cond *cond;
2079 MMacro *mmac, **mmhead;
2080 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2081 Line *l;
2082 struct tokenval tokval;
2083 expr *evalresult;
2084 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2085 int64_t count;
2086 size_t len;
2087 int severity;
2089 origline = tline;
2091 skip_white_(tline);
2092 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2093 (tline->text[1] == '%' || tline->text[1] == '$'
2094 || tline->text[1] == '!'))
2095 return NO_DIRECTIVE_FOUND;
2097 i = pp_token_hash(tline->text);
2100 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2101 * since they are known to be buggy at moment, we need to fix them
2102 * in future release (2.09-2.10)
2104 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2105 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2106 tline->text);
2107 return NO_DIRECTIVE_FOUND;
2111 * If we're in a non-emitting branch of a condition construct,
2112 * or walking to the end of an already terminated %rep block,
2113 * we should ignore all directives except for condition
2114 * directives.
2116 if (((istk->conds && !emitting(istk->conds->state)) ||
2117 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2118 return NO_DIRECTIVE_FOUND;
2122 * If we're defining a macro or reading a %rep block, we should
2123 * ignore all directives except for %macro/%imacro (which nest),
2124 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2125 * If we're in a %rep block, another %rep nests, so should be let through.
2127 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2128 i != PP_RMACRO && i != PP_IRMACRO &&
2129 i != PP_ENDMACRO && i != PP_ENDM &&
2130 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2131 return NO_DIRECTIVE_FOUND;
2134 if (defining) {
2135 if (i == PP_MACRO || i == PP_IMACRO ||
2136 i == PP_RMACRO || i == PP_IRMACRO) {
2137 nested_mac_count++;
2138 return NO_DIRECTIVE_FOUND;
2139 } else if (nested_mac_count > 0) {
2140 if (i == PP_ENDMACRO) {
2141 nested_mac_count--;
2142 return NO_DIRECTIVE_FOUND;
2145 if (!defining->name) {
2146 if (i == PP_REP) {
2147 nested_rep_count++;
2148 return NO_DIRECTIVE_FOUND;
2149 } else if (nested_rep_count > 0) {
2150 if (i == PP_ENDREP) {
2151 nested_rep_count--;
2152 return NO_DIRECTIVE_FOUND;
2158 switch (i) {
2159 case PP_INVALID:
2160 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2161 tline->text);
2162 return NO_DIRECTIVE_FOUND; /* didn't get it */
2164 case PP_STACKSIZE:
2165 /* Directive to tell NASM what the default stack size is. The
2166 * default is for a 16-bit stack, and this can be overriden with
2167 * %stacksize large.
2169 tline = tline->next;
2170 if (tline && tline->type == TOK_WHITESPACE)
2171 tline = tline->next;
2172 if (!tline || tline->type != TOK_ID) {
2173 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2174 free_tlist(origline);
2175 return DIRECTIVE_FOUND;
2177 if (nasm_stricmp(tline->text, "flat") == 0) {
2178 /* All subsequent ARG directives are for a 32-bit stack */
2179 StackSize = 4;
2180 StackPointer = "ebp";
2181 ArgOffset = 8;
2182 LocalOffset = 0;
2183 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2184 /* All subsequent ARG directives are for a 64-bit stack */
2185 StackSize = 8;
2186 StackPointer = "rbp";
2187 ArgOffset = 16;
2188 LocalOffset = 0;
2189 } else if (nasm_stricmp(tline->text, "large") == 0) {
2190 /* All subsequent ARG directives are for a 16-bit stack,
2191 * far function call.
2193 StackSize = 2;
2194 StackPointer = "bp";
2195 ArgOffset = 4;
2196 LocalOffset = 0;
2197 } else if (nasm_stricmp(tline->text, "small") == 0) {
2198 /* All subsequent ARG directives are for a 16-bit stack,
2199 * far function call. We don't support near functions.
2201 StackSize = 2;
2202 StackPointer = "bp";
2203 ArgOffset = 6;
2204 LocalOffset = 0;
2205 } else {
2206 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2207 free_tlist(origline);
2208 return DIRECTIVE_FOUND;
2210 free_tlist(origline);
2211 return DIRECTIVE_FOUND;
2213 case PP_ARG:
2214 /* TASM like ARG directive to define arguments to functions, in
2215 * the following form:
2217 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2219 offset = ArgOffset;
2220 do {
2221 char *arg, directive[256];
2222 int size = StackSize;
2224 /* Find the argument name */
2225 tline = tline->next;
2226 if (tline && tline->type == TOK_WHITESPACE)
2227 tline = tline->next;
2228 if (!tline || tline->type != TOK_ID) {
2229 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2230 free_tlist(origline);
2231 return DIRECTIVE_FOUND;
2233 arg = tline->text;
2235 /* Find the argument size type */
2236 tline = tline->next;
2237 if (!tline || tline->type != TOK_OTHER
2238 || tline->text[0] != ':') {
2239 error(ERR_NONFATAL,
2240 "Syntax error processing `%%arg' directive");
2241 free_tlist(origline);
2242 return DIRECTIVE_FOUND;
2244 tline = tline->next;
2245 if (!tline || tline->type != TOK_ID) {
2246 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2247 free_tlist(origline);
2248 return DIRECTIVE_FOUND;
2251 /* Allow macro expansion of type parameter */
2252 tt = tokenize(tline->text);
2253 tt = expand_smacro(tt);
2254 size = parse_size(tt->text);
2255 if (!size) {
2256 error(ERR_NONFATAL,
2257 "Invalid size type for `%%arg' missing directive");
2258 free_tlist(tt);
2259 free_tlist(origline);
2260 return DIRECTIVE_FOUND;
2262 free_tlist(tt);
2264 /* Round up to even stack slots */
2265 size = ALIGN(size, StackSize);
2267 /* Now define the macro for the argument */
2268 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2269 arg, StackPointer, offset);
2270 do_directive(tokenize(directive));
2271 offset += size;
2273 /* Move to the next argument in the list */
2274 tline = tline->next;
2275 if (tline && tline->type == TOK_WHITESPACE)
2276 tline = tline->next;
2277 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2278 ArgOffset = offset;
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND;
2282 case PP_LOCAL:
2283 /* TASM like LOCAL directive to define local variables for a
2284 * function, in the following form:
2286 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2288 * The '= LocalSize' at the end is ignored by NASM, but is
2289 * required by TASM to define the local parameter size (and used
2290 * by the TASM macro package).
2292 offset = LocalOffset;
2293 do {
2294 char *local, directive[256];
2295 int size = StackSize;
2297 /* Find the argument name */
2298 tline = tline->next;
2299 if (tline && tline->type == TOK_WHITESPACE)
2300 tline = tline->next;
2301 if (!tline || tline->type != TOK_ID) {
2302 error(ERR_NONFATAL,
2303 "`%%local' missing argument parameter");
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
2307 local = tline->text;
2309 /* Find the argument size type */
2310 tline = tline->next;
2311 if (!tline || tline->type != TOK_OTHER
2312 || tline->text[0] != ':') {
2313 error(ERR_NONFATAL,
2314 "Syntax error processing `%%local' directive");
2315 free_tlist(origline);
2316 return DIRECTIVE_FOUND;
2318 tline = tline->next;
2319 if (!tline || tline->type != TOK_ID) {
2320 error(ERR_NONFATAL,
2321 "`%%local' missing size type parameter");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2326 /* Allow macro expansion of type parameter */
2327 tt = tokenize(tline->text);
2328 tt = expand_smacro(tt);
2329 size = parse_size(tt->text);
2330 if (!size) {
2331 error(ERR_NONFATAL,
2332 "Invalid size type for `%%local' missing directive");
2333 free_tlist(tt);
2334 free_tlist(origline);
2335 return DIRECTIVE_FOUND;
2337 free_tlist(tt);
2339 /* Round up to even stack slots */
2340 size = ALIGN(size, StackSize);
2342 offset += size; /* Negative offset, increment before */
2344 /* Now define the macro for the argument */
2345 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2346 local, StackPointer, offset);
2347 do_directive(tokenize(directive));
2349 /* Now define the assign to setup the enter_c macro correctly */
2350 snprintf(directive, sizeof(directive),
2351 "%%assign %%$localsize %%$localsize+%d", size);
2352 do_directive(tokenize(directive));
2354 /* Move to the next argument in the list */
2355 tline = tline->next;
2356 if (tline && tline->type == TOK_WHITESPACE)
2357 tline = tline->next;
2358 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2359 LocalOffset = offset;
2360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
2363 case PP_CLEAR:
2364 if (tline->next)
2365 error(ERR_WARNING|ERR_PASS1,
2366 "trailing garbage after `%%clear' ignored");
2367 free_macros();
2368 init_macros();
2369 free_tlist(origline);
2370 return DIRECTIVE_FOUND;
2372 case PP_DEPEND:
2373 t = tline->next = expand_smacro(tline->next);
2374 skip_white_(t);
2375 if (!t || (t->type != TOK_STRING &&
2376 t->type != TOK_INTERNAL_STRING)) {
2377 error(ERR_NONFATAL, "`%%depend' expects a file name");
2378 free_tlist(origline);
2379 return DIRECTIVE_FOUND; /* but we did _something_ */
2381 if (t->next)
2382 error(ERR_WARNING|ERR_PASS1,
2383 "trailing garbage after `%%depend' ignored");
2384 p = t->text;
2385 if (t->type != TOK_INTERNAL_STRING)
2386 nasm_unquote_cstr(p, i);
2387 if (dephead && !in_list(*dephead, p)) {
2388 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2389 sl->next = NULL;
2390 strcpy(sl->str, p);
2391 *deptail = sl;
2392 deptail = &sl->next;
2394 free_tlist(origline);
2395 return DIRECTIVE_FOUND;
2397 case PP_INCLUDE:
2398 t = tline->next = expand_smacro(tline->next);
2399 skip_white_(t);
2401 if (!t || (t->type != TOK_STRING &&
2402 t->type != TOK_INTERNAL_STRING)) {
2403 error(ERR_NONFATAL, "`%%include' expects a file name");
2404 free_tlist(origline);
2405 return DIRECTIVE_FOUND; /* but we did _something_ */
2407 if (t->next)
2408 error(ERR_WARNING|ERR_PASS1,
2409 "trailing garbage after `%%include' ignored");
2410 p = t->text;
2411 if (t->type != TOK_INTERNAL_STRING)
2412 nasm_unquote_cstr(p, i);
2413 inc = nasm_malloc(sizeof(Include));
2414 inc->next = istk;
2415 inc->conds = NULL;
2416 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2417 if (!inc->fp) {
2418 /* -MG given but file not found */
2419 nasm_free(inc);
2420 } else {
2421 inc->fname = src_set_fname(nasm_strdup(p));
2422 inc->lineno = src_set_linnum(0);
2423 inc->lineinc = 1;
2424 inc->expansion = NULL;
2425 inc->mstk = NULL;
2426 istk = inc;
2427 list->uplevel(LIST_INCLUDE);
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2432 case PP_USE:
2434 static macros_t *use_pkg;
2435 const char *pkg_macro = NULL;
2437 tline = tline->next;
2438 skip_white_(tline);
2439 tline = expand_id(tline);
2441 if (!tline || (tline->type != TOK_STRING &&
2442 tline->type != TOK_INTERNAL_STRING &&
2443 tline->type != TOK_ID)) {
2444 error(ERR_NONFATAL, "`%%use' expects a package name");
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND; /* but we did _something_ */
2448 if (tline->next)
2449 error(ERR_WARNING|ERR_PASS1,
2450 "trailing garbage after `%%use' ignored");
2451 if (tline->type == TOK_STRING)
2452 nasm_unquote_cstr(tline->text, i);
2453 use_pkg = nasm_stdmac_find_package(tline->text);
2454 if (!use_pkg)
2455 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2456 else
2457 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2458 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2459 /* Not already included, go ahead and include it */
2460 stdmacpos = use_pkg;
2462 free_tlist(origline);
2463 return DIRECTIVE_FOUND;
2465 case PP_PUSH:
2466 case PP_REPL:
2467 case PP_POP:
2468 tline = tline->next;
2469 skip_white_(tline);
2470 tline = expand_id(tline);
2471 if (tline) {
2472 if (!tok_type_(tline, TOK_ID)) {
2473 error(ERR_NONFATAL, "`%s' expects a context identifier",
2474 pp_directives[i]);
2475 free_tlist(origline);
2476 return DIRECTIVE_FOUND; /* but we did _something_ */
2478 if (tline->next)
2479 error(ERR_WARNING|ERR_PASS1,
2480 "trailing garbage after `%s' ignored",
2481 pp_directives[i]);
2482 p = nasm_strdup(tline->text);
2483 } else {
2484 p = NULL; /* Anonymous */
2487 if (i == PP_PUSH) {
2488 ctx = nasm_malloc(sizeof(Context));
2489 ctx->next = cstk;
2490 hash_init(&ctx->localmac, HASH_SMALL);
2491 ctx->name = p;
2492 ctx->number = unique++;
2493 cstk = ctx;
2494 } else {
2495 /* %pop or %repl */
2496 if (!cstk) {
2497 error(ERR_NONFATAL, "`%s': context stack is empty",
2498 pp_directives[i]);
2499 } else if (i == PP_POP) {
2500 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2501 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2502 "expected %s",
2503 cstk->name ? cstk->name : "anonymous", p);
2504 else
2505 ctx_pop();
2506 } else {
2507 /* i == PP_REPL */
2508 nasm_free(cstk->name);
2509 cstk->name = p;
2510 p = NULL;
2512 nasm_free(p);
2514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
2516 case PP_FATAL:
2517 severity = ERR_FATAL;
2518 goto issue_error;
2519 case PP_ERROR:
2520 severity = ERR_NONFATAL;
2521 goto issue_error;
2522 case PP_WARNING:
2523 severity = ERR_WARNING|ERR_WARN_USER;
2524 goto issue_error;
2526 issue_error:
2528 /* Only error out if this is the final pass */
2529 if (pass != 2 && i != PP_FATAL)
2530 return DIRECTIVE_FOUND;
2532 tline->next = expand_smacro(tline->next);
2533 tline = tline->next;
2534 skip_white_(tline);
2535 t = tline ? tline->next : NULL;
2536 skip_white_(t);
2537 if (tok_type_(tline, TOK_STRING) && !t) {
2538 /* The line contains only a quoted string */
2539 p = tline->text;
2540 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2541 error(severity, "%s", p);
2542 } else {
2543 /* Not a quoted string, or more than a quoted string */
2544 p = detoken(tline, false);
2545 error(severity, "%s", p);
2546 nasm_free(p);
2548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
2552 CASE_PP_IF:
2553 if (istk->conds && !emitting(istk->conds->state))
2554 j = COND_NEVER;
2555 else {
2556 j = if_condition(tline->next, i);
2557 tline->next = NULL; /* it got freed */
2558 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2560 cond = nasm_malloc(sizeof(Cond));
2561 cond->next = istk->conds;
2562 cond->state = j;
2563 istk->conds = cond;
2564 if(istk->mstk)
2565 istk->mstk->condcnt ++;
2566 free_tlist(origline);
2567 return DIRECTIVE_FOUND;
2569 CASE_PP_ELIF:
2570 if (!istk->conds)
2571 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2572 switch(istk->conds->state) {
2573 case COND_IF_TRUE:
2574 istk->conds->state = COND_DONE;
2575 break;
2577 case COND_DONE:
2578 case COND_NEVER:
2579 break;
2581 case COND_ELSE_TRUE:
2582 case COND_ELSE_FALSE:
2583 error_precond(ERR_WARNING|ERR_PASS1,
2584 "`%%elif' after `%%else' ignored");
2585 istk->conds->state = COND_NEVER;
2586 break;
2588 case COND_IF_FALSE:
2590 * IMPORTANT: In the case of %if, we will already have
2591 * called expand_mmac_params(); however, if we're
2592 * processing an %elif we must have been in a
2593 * non-emitting mode, which would have inhibited
2594 * the normal invocation of expand_mmac_params().
2595 * Therefore, we have to do it explicitly here.
2597 j = if_condition(expand_mmac_params(tline->next), i);
2598 tline->next = NULL; /* it got freed */
2599 istk->conds->state =
2600 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2601 break;
2603 free_tlist(origline);
2604 return DIRECTIVE_FOUND;
2606 case PP_ELSE:
2607 if (tline->next)
2608 error_precond(ERR_WARNING|ERR_PASS1,
2609 "trailing garbage after `%%else' ignored");
2610 if (!istk->conds)
2611 error(ERR_FATAL, "`%%else': no matching `%%if'");
2612 switch(istk->conds->state) {
2613 case COND_IF_TRUE:
2614 case COND_DONE:
2615 istk->conds->state = COND_ELSE_FALSE;
2616 break;
2618 case COND_NEVER:
2619 break;
2621 case COND_IF_FALSE:
2622 istk->conds->state = COND_ELSE_TRUE;
2623 break;
2625 case COND_ELSE_TRUE:
2626 case COND_ELSE_FALSE:
2627 error_precond(ERR_WARNING|ERR_PASS1,
2628 "`%%else' after `%%else' ignored.");
2629 istk->conds->state = COND_NEVER;
2630 break;
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND;
2635 case PP_ENDIF:
2636 if (tline->next)
2637 error_precond(ERR_WARNING|ERR_PASS1,
2638 "trailing garbage after `%%endif' ignored");
2639 if (!istk->conds)
2640 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2641 cond = istk->conds;
2642 istk->conds = cond->next;
2643 nasm_free(cond);
2644 if(istk->mstk)
2645 istk->mstk->condcnt --;
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
2649 case PP_RMACRO:
2650 case PP_IRMACRO:
2651 case PP_MACRO:
2652 case PP_IMACRO:
2653 if (defining) {
2654 error(ERR_FATAL, "`%s': already defining a macro",
2655 pp_directives[i]);
2656 return DIRECTIVE_FOUND;
2658 defining = nasm_malloc(sizeof(MMacro));
2659 defining->max_depth =
2660 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2661 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2662 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2663 nasm_free(defining);
2664 defining = NULL;
2665 return DIRECTIVE_FOUND;
2668 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2669 while (mmac) {
2670 if (!strcmp(mmac->name, defining->name) &&
2671 (mmac->nparam_min <= defining->nparam_max
2672 || defining->plus)
2673 && (defining->nparam_min <= mmac->nparam_max
2674 || mmac->plus)) {
2675 error(ERR_WARNING|ERR_PASS1,
2676 "redefining multi-line macro `%s'", defining->name);
2677 return DIRECTIVE_FOUND;
2679 mmac = mmac->next;
2681 free_tlist(origline);
2682 return DIRECTIVE_FOUND;
2684 case PP_ENDM:
2685 case PP_ENDMACRO:
2686 if (! (defining && defining->name)) {
2687 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2688 return DIRECTIVE_FOUND;
2690 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2691 defining->next = *mmhead;
2692 *mmhead = defining;
2693 defining = NULL;
2694 free_tlist(origline);
2695 return DIRECTIVE_FOUND;
2697 case PP_EXITMACRO:
2699 * We must search along istk->expansion until we hit a
2700 * macro-end marker for a macro with a name. Then we
2701 * bypass all lines between exitmacro and endmacro.
2703 list_for_each(l, istk->expansion)
2704 if (l->finishes && l->finishes->name)
2705 break;
2707 if (l) {
2709 * Remove all conditional entries relative to this
2710 * macro invocation. (safe to do in this context)
2712 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2713 cond = istk->conds;
2714 istk->conds = cond->next;
2715 nasm_free(cond);
2717 istk->expansion = l;
2718 } else {
2719 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
2724 case PP_UNMACRO:
2725 case PP_UNIMACRO:
2727 MMacro **mmac_p;
2728 MMacro spec;
2730 spec.casesense = (i == PP_UNMACRO);
2731 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2732 return DIRECTIVE_FOUND;
2734 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2735 while (mmac_p && *mmac_p) {
2736 mmac = *mmac_p;
2737 if (mmac->casesense == spec.casesense &&
2738 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2739 mmac->nparam_min == spec.nparam_min &&
2740 mmac->nparam_max == spec.nparam_max &&
2741 mmac->plus == spec.plus) {
2742 *mmac_p = mmac->next;
2743 free_mmacro(mmac);
2744 } else {
2745 mmac_p = &mmac->next;
2748 free_tlist(origline);
2749 free_tlist(spec.dlist);
2750 return DIRECTIVE_FOUND;
2753 case PP_ROTATE:
2754 if (tline->next && tline->next->type == TOK_WHITESPACE)
2755 tline = tline->next;
2756 if (!tline->next) {
2757 free_tlist(origline);
2758 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2759 return DIRECTIVE_FOUND;
2761 t = expand_smacro(tline->next);
2762 tline->next = NULL;
2763 free_tlist(origline);
2764 tline = t;
2765 tptr = &t;
2766 tokval.t_type = TOKEN_INVALID;
2767 evalresult =
2768 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2769 free_tlist(tline);
2770 if (!evalresult)
2771 return DIRECTIVE_FOUND;
2772 if (tokval.t_type)
2773 error(ERR_WARNING|ERR_PASS1,
2774 "trailing garbage after expression ignored");
2775 if (!is_simple(evalresult)) {
2776 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2777 return DIRECTIVE_FOUND;
2779 mmac = istk->mstk;
2780 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2781 mmac = mmac->next_active;
2782 if (!mmac) {
2783 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2784 } else if (mmac->nparam == 0) {
2785 error(ERR_NONFATAL,
2786 "`%%rotate' invoked within macro without parameters");
2787 } else {
2788 int rotate = mmac->rotate + reloc_value(evalresult);
2790 rotate %= (int)mmac->nparam;
2791 if (rotate < 0)
2792 rotate += mmac->nparam;
2794 mmac->rotate = rotate;
2796 return DIRECTIVE_FOUND;
2798 case PP_REP:
2799 nolist = false;
2800 do {
2801 tline = tline->next;
2802 } while (tok_type_(tline, TOK_WHITESPACE));
2804 if (tok_type_(tline, TOK_ID) &&
2805 nasm_stricmp(tline->text, ".nolist") == 0) {
2806 nolist = true;
2807 do {
2808 tline = tline->next;
2809 } while (tok_type_(tline, TOK_WHITESPACE));
2812 if (tline) {
2813 t = expand_smacro(tline);
2814 tptr = &t;
2815 tokval.t_type = TOKEN_INVALID;
2816 evalresult =
2817 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2818 if (!evalresult) {
2819 free_tlist(origline);
2820 return DIRECTIVE_FOUND;
2822 if (tokval.t_type)
2823 error(ERR_WARNING|ERR_PASS1,
2824 "trailing garbage after expression ignored");
2825 if (!is_simple(evalresult)) {
2826 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2827 return DIRECTIVE_FOUND;
2829 count = reloc_value(evalresult) + 1;
2830 } else {
2831 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2832 count = 0;
2834 free_tlist(origline);
2836 tmp_defining = defining;
2837 defining = nasm_malloc(sizeof(MMacro));
2838 defining->prev = NULL;
2839 defining->name = NULL; /* flags this macro as a %rep block */
2840 defining->casesense = false;
2841 defining->plus = false;
2842 defining->nolist = nolist;
2843 defining->in_progress = count;
2844 defining->max_depth = 0;
2845 defining->nparam_min = defining->nparam_max = 0;
2846 defining->defaults = NULL;
2847 defining->dlist = NULL;
2848 defining->expansion = NULL;
2849 defining->next_active = istk->mstk;
2850 defining->rep_nest = tmp_defining;
2851 return DIRECTIVE_FOUND;
2853 case PP_ENDREP:
2854 if (!defining || defining->name) {
2855 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2856 return DIRECTIVE_FOUND;
2860 * Now we have a "macro" defined - although it has no name
2861 * and we won't be entering it in the hash tables - we must
2862 * push a macro-end marker for it on to istk->expansion.
2863 * After that, it will take care of propagating itself (a
2864 * macro-end marker line for a macro which is really a %rep
2865 * block will cause the macro to be re-expanded, complete
2866 * with another macro-end marker to ensure the process
2867 * continues) until the whole expansion is forcibly removed
2868 * from istk->expansion by a %exitrep.
2870 l = nasm_malloc(sizeof(Line));
2871 l->next = istk->expansion;
2872 l->finishes = defining;
2873 l->first = NULL;
2874 istk->expansion = l;
2876 istk->mstk = defining;
2878 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2879 tmp_defining = defining;
2880 defining = defining->rep_nest;
2881 free_tlist(origline);
2882 return DIRECTIVE_FOUND;
2884 case PP_EXITREP:
2886 * We must search along istk->expansion until we hit a
2887 * macro-end marker for a macro with no name. Then we set
2888 * its `in_progress' flag to 0.
2890 list_for_each(l, istk->expansion)
2891 if (l->finishes && !l->finishes->name)
2892 break;
2894 if (l)
2895 l->finishes->in_progress = 1;
2896 else
2897 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
2901 case PP_XDEFINE:
2902 case PP_IXDEFINE:
2903 case PP_DEFINE:
2904 case PP_IDEFINE:
2905 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2907 tline = tline->next;
2908 skip_white_(tline);
2909 tline = expand_id(tline);
2910 if (!tline || (tline->type != TOK_ID &&
2911 (tline->type != TOK_PREPROC_ID ||
2912 tline->text[1] != '$'))) {
2913 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2914 pp_directives[i]);
2915 free_tlist(origline);
2916 return DIRECTIVE_FOUND;
2919 ctx = get_ctx(tline->text, &mname, false);
2920 last = tline;
2921 param_start = tline = tline->next;
2922 nparam = 0;
2924 /* Expand the macro definition now for %xdefine and %ixdefine */
2925 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2926 tline = expand_smacro(tline);
2928 if (tok_is_(tline, "(")) {
2930 * This macro has parameters.
2933 tline = tline->next;
2934 while (1) {
2935 skip_white_(tline);
2936 if (!tline) {
2937 error(ERR_NONFATAL, "parameter identifier expected");
2938 free_tlist(origline);
2939 return DIRECTIVE_FOUND;
2941 if (tline->type != TOK_ID) {
2942 error(ERR_NONFATAL,
2943 "`%s': parameter identifier expected",
2944 tline->text);
2945 free_tlist(origline);
2946 return DIRECTIVE_FOUND;
2948 tline->type = TOK_SMAC_PARAM + nparam++;
2949 tline = tline->next;
2950 skip_white_(tline);
2951 if (tok_is_(tline, ",")) {
2952 tline = tline->next;
2953 } else {
2954 if (!tok_is_(tline, ")")) {
2955 error(ERR_NONFATAL,
2956 "`)' expected to terminate macro template");
2957 free_tlist(origline);
2958 return DIRECTIVE_FOUND;
2960 break;
2963 last = tline;
2964 tline = tline->next;
2966 if (tok_type_(tline, TOK_WHITESPACE))
2967 last = tline, tline = tline->next;
2968 macro_start = NULL;
2969 last->next = NULL;
2970 t = tline;
2971 while (t) {
2972 if (t->type == TOK_ID) {
2973 list_for_each(tt, param_start)
2974 if (tt->type >= TOK_SMAC_PARAM &&
2975 !strcmp(tt->text, t->text))
2976 t->type = tt->type;
2978 tt = t->next;
2979 t->next = macro_start;
2980 macro_start = t;
2981 t = tt;
2984 * Good. We now have a macro name, a parameter count, and a
2985 * token list (in reverse order) for an expansion. We ought
2986 * to be OK just to create an SMacro, store it, and let
2987 * free_tlist have the rest of the line (which we have
2988 * carefully re-terminated after chopping off the expansion
2989 * from the end).
2991 define_smacro(ctx, mname, casesense, nparam, macro_start);
2992 free_tlist(origline);
2993 return DIRECTIVE_FOUND;
2995 case PP_UNDEF:
2996 tline = tline->next;
2997 skip_white_(tline);
2998 tline = expand_id(tline);
2999 if (!tline || (tline->type != TOK_ID &&
3000 (tline->type != TOK_PREPROC_ID ||
3001 tline->text[1] != '$'))) {
3002 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3003 free_tlist(origline);
3004 return DIRECTIVE_FOUND;
3006 if (tline->next) {
3007 error(ERR_WARNING|ERR_PASS1,
3008 "trailing garbage after macro name ignored");
3011 /* Find the context that symbol belongs to */
3012 ctx = get_ctx(tline->text, &mname, false);
3013 undef_smacro(ctx, mname);
3014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
3017 case PP_DEFSTR:
3018 case PP_IDEFSTR:
3019 casesense = (i == PP_DEFSTR);
3021 tline = tline->next;
3022 skip_white_(tline);
3023 tline = expand_id(tline);
3024 if (!tline || (tline->type != TOK_ID &&
3025 (tline->type != TOK_PREPROC_ID ||
3026 tline->text[1] != '$'))) {
3027 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3028 pp_directives[i]);
3029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3033 ctx = get_ctx(tline->text, &mname, false);
3034 last = tline;
3035 tline = expand_smacro(tline->next);
3036 last->next = NULL;
3038 while (tok_type_(tline, TOK_WHITESPACE))
3039 tline = delete_Token(tline);
3041 p = detoken(tline, false);
3042 macro_start = nasm_malloc(sizeof(*macro_start));
3043 macro_start->next = NULL;
3044 macro_start->text = nasm_quote(p, strlen(p));
3045 macro_start->type = TOK_STRING;
3046 macro_start->a.mac = NULL;
3047 nasm_free(p);
3050 * We now have a macro name, an implicit parameter count of
3051 * zero, and a string token to use as an expansion. Create
3052 * and store an SMacro.
3054 define_smacro(ctx, mname, casesense, 0, macro_start);
3055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
3058 case PP_DEFTOK:
3059 case PP_IDEFTOK:
3060 casesense = (i == PP_DEFTOK);
3062 tline = tline->next;
3063 skip_white_(tline);
3064 tline = expand_id(tline);
3065 if (!tline || (tline->type != TOK_ID &&
3066 (tline->type != TOK_PREPROC_ID ||
3067 tline->text[1] != '$'))) {
3068 error(ERR_NONFATAL,
3069 "`%s' expects a macro identifier as first parameter",
3070 pp_directives[i]);
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3074 ctx = get_ctx(tline->text, &mname, false);
3075 last = tline;
3076 tline = expand_smacro(tline->next);
3077 last->next = NULL;
3079 t = tline;
3080 while (tok_type_(t, TOK_WHITESPACE))
3081 t = t->next;
3082 /* t should now point to the string */
3083 if (t->type != TOK_STRING) {
3084 error(ERR_NONFATAL,
3085 "`%s` requires string as second parameter",
3086 pp_directives[i]);
3087 free_tlist(tline);
3088 free_tlist(origline);
3089 return DIRECTIVE_FOUND;
3092 nasm_unquote_cstr(t->text, i);
3093 macro_start = tokenize(t->text);
3096 * We now have a macro name, an implicit parameter count of
3097 * zero, and a numeric token to use as an expansion. Create
3098 * and store an SMacro.
3100 define_smacro(ctx, mname, casesense, 0, macro_start);
3101 free_tlist(tline);
3102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
3105 case PP_PATHSEARCH:
3107 FILE *fp;
3108 StrList *xsl = NULL;
3109 StrList **xst = &xsl;
3111 casesense = true;
3113 tline = tline->next;
3114 skip_white_(tline);
3115 tline = expand_id(tline);
3116 if (!tline || (tline->type != TOK_ID &&
3117 (tline->type != TOK_PREPROC_ID ||
3118 tline->text[1] != '$'))) {
3119 error(ERR_NONFATAL,
3120 "`%%pathsearch' expects a macro identifier as first parameter");
3121 free_tlist(origline);
3122 return DIRECTIVE_FOUND;
3124 ctx = get_ctx(tline->text, &mname, false);
3125 last = tline;
3126 tline = expand_smacro(tline->next);
3127 last->next = NULL;
3129 t = tline;
3130 while (tok_type_(t, TOK_WHITESPACE))
3131 t = t->next;
3133 if (!t || (t->type != TOK_STRING &&
3134 t->type != TOK_INTERNAL_STRING)) {
3135 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3136 free_tlist(tline);
3137 free_tlist(origline);
3138 return DIRECTIVE_FOUND; /* but we did _something_ */
3140 if (t->next)
3141 error(ERR_WARNING|ERR_PASS1,
3142 "trailing garbage after `%%pathsearch' ignored");
3143 p = t->text;
3144 if (t->type != TOK_INTERNAL_STRING)
3145 nasm_unquote(p, NULL);
3147 fp = inc_fopen(p, &xsl, &xst, true);
3148 if (fp) {
3149 p = xsl->str;
3150 fclose(fp); /* Don't actually care about the file */
3152 macro_start = nasm_malloc(sizeof(*macro_start));
3153 macro_start->next = NULL;
3154 macro_start->text = nasm_quote(p, strlen(p));
3155 macro_start->type = TOK_STRING;
3156 macro_start->a.mac = NULL;
3157 if (xsl)
3158 nasm_free(xsl);
3161 * We now have a macro name, an implicit parameter count of
3162 * zero, and a string token to use as an expansion. Create
3163 * and store an SMacro.
3165 define_smacro(ctx, mname, casesense, 0, macro_start);
3166 free_tlist(tline);
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3171 case PP_STRLEN:
3172 casesense = true;
3174 tline = tline->next;
3175 skip_white_(tline);
3176 tline = expand_id(tline);
3177 if (!tline || (tline->type != TOK_ID &&
3178 (tline->type != TOK_PREPROC_ID ||
3179 tline->text[1] != '$'))) {
3180 error(ERR_NONFATAL,
3181 "`%%strlen' expects a macro identifier as first parameter");
3182 free_tlist(origline);
3183 return DIRECTIVE_FOUND;
3185 ctx = get_ctx(tline->text, &mname, false);
3186 last = tline;
3187 tline = expand_smacro(tline->next);
3188 last->next = NULL;
3190 t = tline;
3191 while (tok_type_(t, TOK_WHITESPACE))
3192 t = t->next;
3193 /* t should now point to the string */
3194 if (t->type != TOK_STRING) {
3195 error(ERR_NONFATAL,
3196 "`%%strlen` requires string as second parameter");
3197 free_tlist(tline);
3198 free_tlist(origline);
3199 return DIRECTIVE_FOUND;
3202 macro_start = nasm_malloc(sizeof(*macro_start));
3203 macro_start->next = NULL;
3204 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3205 macro_start->a.mac = NULL;
3208 * We now have a macro name, an implicit parameter count of
3209 * zero, and a numeric token to use as an expansion. Create
3210 * and store an SMacro.
3212 define_smacro(ctx, mname, casesense, 0, macro_start);
3213 free_tlist(tline);
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3217 case PP_STRCAT:
3218 casesense = true;
3220 tline = tline->next;
3221 skip_white_(tline);
3222 tline = expand_id(tline);
3223 if (!tline || (tline->type != TOK_ID &&
3224 (tline->type != TOK_PREPROC_ID ||
3225 tline->text[1] != '$'))) {
3226 error(ERR_NONFATAL,
3227 "`%%strcat' expects a macro identifier as first parameter");
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
3231 ctx = get_ctx(tline->text, &mname, false);
3232 last = tline;
3233 tline = expand_smacro(tline->next);
3234 last->next = NULL;
3236 len = 0;
3237 list_for_each(t, tline) {
3238 switch (t->type) {
3239 case TOK_WHITESPACE:
3240 break;
3241 case TOK_STRING:
3242 len += t->a.len = nasm_unquote(t->text, NULL);
3243 break;
3244 case TOK_OTHER:
3245 if (!strcmp(t->text, ",")) /* permit comma separators */
3246 break;
3247 /* else fall through */
3248 default:
3249 error(ERR_NONFATAL,
3250 "non-string passed to `%%strcat' (%d)", t->type);
3251 free_tlist(tline);
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3257 p = pp = nasm_malloc(len);
3258 list_for_each(t, tline) {
3259 if (t->type == TOK_STRING) {
3260 memcpy(p, t->text, t->a.len);
3261 p += t->a.len;
3266 * We now have a macro name, an implicit parameter count of
3267 * zero, and a numeric token to use as an expansion. Create
3268 * and store an SMacro.
3270 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3271 macro_start->text = nasm_quote(pp, len);
3272 nasm_free(pp);
3273 define_smacro(ctx, mname, casesense, 0, macro_start);
3274 free_tlist(tline);
3275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3278 case PP_SUBSTR:
3280 int64_t a1, a2;
3281 size_t len;
3283 casesense = true;
3285 tline = tline->next;
3286 skip_white_(tline);
3287 tline = expand_id(tline);
3288 if (!tline || (tline->type != TOK_ID &&
3289 (tline->type != TOK_PREPROC_ID ||
3290 tline->text[1] != '$'))) {
3291 error(ERR_NONFATAL,
3292 "`%%substr' expects a macro identifier as first parameter");
3293 free_tlist(origline);
3294 return DIRECTIVE_FOUND;
3296 ctx = get_ctx(tline->text, &mname, false);
3297 last = tline;
3298 tline = expand_smacro(tline->next);
3299 last->next = NULL;
3301 t = tline->next;
3302 while (tok_type_(t, TOK_WHITESPACE))
3303 t = t->next;
3305 /* t should now point to the string */
3306 if (t->type != TOK_STRING) {
3307 error(ERR_NONFATAL,
3308 "`%%substr` requires string as second parameter");
3309 free_tlist(tline);
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3314 tt = t->next;
3315 tptr = &tt;
3316 tokval.t_type = TOKEN_INVALID;
3317 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3318 pass, error, NULL);
3319 if (!evalresult) {
3320 free_tlist(tline);
3321 free_tlist(origline);
3322 return DIRECTIVE_FOUND;
3323 } else if (!is_simple(evalresult)) {
3324 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3325 free_tlist(tline);
3326 free_tlist(origline);
3327 return DIRECTIVE_FOUND;
3329 a1 = evalresult->value-1;
3331 while (tok_type_(tt, TOK_WHITESPACE))
3332 tt = tt->next;
3333 if (!tt) {
3334 a2 = 1; /* Backwards compatibility: one character */
3335 } else {
3336 tokval.t_type = TOKEN_INVALID;
3337 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3338 pass, error, NULL);
3339 if (!evalresult) {
3340 free_tlist(tline);
3341 free_tlist(origline);
3342 return DIRECTIVE_FOUND;
3343 } else if (!is_simple(evalresult)) {
3344 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3345 free_tlist(tline);
3346 free_tlist(origline);
3347 return DIRECTIVE_FOUND;
3349 a2 = evalresult->value;
3352 len = nasm_unquote(t->text, NULL);
3353 if (a2 < 0)
3354 a2 = a2+1+len-a1;
3355 if (a1+a2 > (int64_t)len)
3356 a2 = len-a1;
3358 macro_start = nasm_malloc(sizeof(*macro_start));
3359 macro_start->next = NULL;
3360 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3361 macro_start->type = TOK_STRING;
3362 macro_start->a.mac = NULL;
3365 * We now have a macro name, an implicit parameter count of
3366 * zero, and a numeric token to use as an expansion. Create
3367 * and store an SMacro.
3369 define_smacro(ctx, mname, casesense, 0, macro_start);
3370 free_tlist(tline);
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3375 case PP_ASSIGN:
3376 case PP_IASSIGN:
3377 casesense = (i == PP_ASSIGN);
3379 tline = tline->next;
3380 skip_white_(tline);
3381 tline = expand_id(tline);
3382 if (!tline || (tline->type != TOK_ID &&
3383 (tline->type != TOK_PREPROC_ID ||
3384 tline->text[1] != '$'))) {
3385 error(ERR_NONFATAL,
3386 "`%%%sassign' expects a macro identifier",
3387 (i == PP_IASSIGN ? "i" : ""));
3388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3391 ctx = get_ctx(tline->text, &mname, false);
3392 last = tline;
3393 tline = expand_smacro(tline->next);
3394 last->next = NULL;
3396 t = tline;
3397 tptr = &t;
3398 tokval.t_type = TOKEN_INVALID;
3399 evalresult =
3400 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3401 free_tlist(tline);
3402 if (!evalresult) {
3403 free_tlist(origline);
3404 return DIRECTIVE_FOUND;
3407 if (tokval.t_type)
3408 error(ERR_WARNING|ERR_PASS1,
3409 "trailing garbage after expression ignored");
3411 if (!is_simple(evalresult)) {
3412 error(ERR_NONFATAL,
3413 "non-constant value given to `%%%sassign'",
3414 (i == PP_IASSIGN ? "i" : ""));
3415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3419 macro_start = nasm_malloc(sizeof(*macro_start));
3420 macro_start->next = NULL;
3421 make_tok_num(macro_start, reloc_value(evalresult));
3422 macro_start->a.mac = NULL;
3425 * We now have a macro name, an implicit parameter count of
3426 * zero, and a numeric token to use as an expansion. Create
3427 * and store an SMacro.
3429 define_smacro(ctx, mname, casesense, 0, macro_start);
3430 free_tlist(origline);
3431 return DIRECTIVE_FOUND;
3433 case PP_LINE:
3435 * Syntax is `%line nnn[+mmm] [filename]'
3437 tline = tline->next;
3438 skip_white_(tline);
3439 if (!tok_type_(tline, TOK_NUMBER)) {
3440 error(ERR_NONFATAL, "`%%line' expects line number");
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3444 k = readnum(tline->text, &err);
3445 m = 1;
3446 tline = tline->next;
3447 if (tok_is_(tline, "+")) {
3448 tline = tline->next;
3449 if (!tok_type_(tline, TOK_NUMBER)) {
3450 error(ERR_NONFATAL, "`%%line' expects line increment");
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
3454 m = readnum(tline->text, &err);
3455 tline = tline->next;
3457 skip_white_(tline);
3458 src_set_linnum(k);
3459 istk->lineinc = m;
3460 if (tline) {
3461 nasm_free(src_set_fname(detoken(tline, false)));
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
3466 default:
3467 error(ERR_FATAL,
3468 "preprocessor directive `%s' not yet implemented",
3469 pp_directives[i]);
3470 return DIRECTIVE_FOUND;
3475 * Ensure that a macro parameter contains a condition code and
3476 * nothing else. Return the condition code index if so, or -1
3477 * otherwise.
3479 static int find_cc(Token * t)
3481 Token *tt;
3482 int i, j, k, m;
3484 if (!t)
3485 return -1; /* Probably a %+ without a space */
3487 skip_white_(t);
3488 if (t->type != TOK_ID)
3489 return -1;
3490 tt = t->next;
3491 skip_white_(tt);
3492 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3493 return -1;
3495 i = -1;
3496 j = ARRAY_SIZE(conditions);
3497 while (j - i > 1) {
3498 k = (j + i) / 2;
3499 m = nasm_stricmp(t->text, conditions[k]);
3500 if (m == 0) {
3501 i = k;
3502 j = -2;
3503 break;
3504 } else if (m < 0) {
3505 j = k;
3506 } else
3507 i = k;
3509 if (j != -2)
3510 return -1;
3511 return i;
3514 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3516 Token **tail, *t, *tt;
3517 Token **paste_head;
3518 bool did_paste = false;
3519 char *tmp;
3521 /* Now handle token pasting... */
3522 paste_head = NULL;
3523 tail = head;
3524 while ((t = *tail) && (tt = t->next)) {
3525 switch (t->type) {
3526 case TOK_WHITESPACE:
3527 if (tt->type == TOK_WHITESPACE) {
3528 /* Zap adjacent whitespace tokens */
3529 t->next = delete_Token(tt);
3530 } else {
3531 /* Do not advance paste_head here */
3532 tail = &t->next;
3534 break;
3535 case TOK_ID:
3536 case TOK_NUMBER:
3537 case TOK_FLOAT:
3539 size_t len = 0;
3540 char *tmp, *p;
3542 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3543 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3544 tt->type == TOK_OTHER)) {
3545 len += strlen(tt->text);
3546 tt = tt->next;
3550 * Now tt points to the first token after
3551 * the potential paste area...
3553 if (tt != t->next) {
3554 /* We have at least two tokens... */
3555 len += strlen(t->text);
3556 p = tmp = nasm_malloc(len+1);
3558 while (t != tt) {
3559 strcpy(p, t->text);
3560 p = strchr(p, '\0');
3561 t = delete_Token(t);
3564 t = *tail = tokenize(tmp);
3565 nasm_free(tmp);
3567 while (t->next) {
3568 tail = &t->next;
3569 t = t->next;
3571 t->next = tt; /* Attach the remaining token chain */
3573 did_paste = true;
3575 paste_head = tail;
3576 tail = &t->next;
3577 break;
3579 case TOK_PASTE: /* %+ */
3580 if (handle_paste_tokens) {
3581 /* Zap %+ and whitespace tokens to the right */
3582 while (t && (t->type == TOK_WHITESPACE ||
3583 t->type == TOK_PASTE))
3584 t = *tail = delete_Token(t);
3585 if (!paste_head || !t)
3586 break; /* Nothing to paste with */
3587 tail = paste_head;
3588 t = *tail;
3589 tt = t->next;
3590 while (tok_type_(tt, TOK_WHITESPACE))
3591 tt = t->next = delete_Token(tt);
3593 if (tt) {
3594 tmp = nasm_strcat(t->text, tt->text);
3595 delete_Token(t);
3596 tt = delete_Token(tt);
3597 t = *tail = tokenize(tmp);
3598 nasm_free(tmp);
3599 while (t->next) {
3600 tail = &t->next;
3601 t = t->next;
3603 t->next = tt; /* Attach the remaining token chain */
3604 did_paste = true;
3606 paste_head = tail;
3607 tail = &t->next;
3608 break;
3610 /* else fall through */
3611 default:
3612 tail = &t->next;
3613 if (!tok_type_(t->next, TOK_WHITESPACE))
3614 paste_head = tail;
3615 break;
3618 return did_paste;
3622 * expands to a list of tokens from %{x:y}
3624 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3626 Token *t = tline, **tt, *tm, *head;
3627 char *pos;
3628 int fst, lst, j, i;
3630 pos = strchr(tline->text, ':');
3631 nasm_assert(pos);
3633 lst = atoi(pos + 1);
3634 fst = atoi(tline->text + 1);
3637 * only macros params are accounted so
3638 * if someone passes %0 -- we reject such
3639 * value(s)
3641 if (lst == 0 || fst == 0)
3642 goto err;
3644 /* the values should be sane */
3645 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3646 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3647 goto err;
3649 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3650 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3652 /* counted from zero */
3653 fst--, lst--;
3656 * it will be at least one token
3658 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3659 t = new_Token(NULL, tm->type, tm->text, 0);
3660 head = t, tt = &t->next;
3661 if (fst < lst) {
3662 for (i = fst + 1; i <= lst; i++) {
3663 t = new_Token(NULL, TOK_OTHER, ",", 0);
3664 *tt = t, tt = &t->next;
3665 j = (i + mac->rotate) % mac->nparam;
3666 tm = mac->params[j];
3667 t = new_Token(NULL, tm->type, tm->text, 0);
3668 *tt = t, tt = &t->next;
3670 } else {
3671 for (i = fst - 1; i >= lst; i--) {
3672 t = new_Token(NULL, TOK_OTHER, ",", 0);
3673 *tt = t, tt = &t->next;
3674 j = (i + mac->rotate) % mac->nparam;
3675 tm = mac->params[j];
3676 t = new_Token(NULL, tm->type, tm->text, 0);
3677 *tt = t, tt = &t->next;
3681 *last = tt;
3682 return head;
3684 err:
3685 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3686 &tline->text[1]);
3687 return tline;
3691 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3692 * %-n) and MMacro-local identifiers (%%foo) as well as
3693 * macro indirection (%[...]) and range (%{..:..}).
3695 static Token *expand_mmac_params(Token * tline)
3697 Token *t, *tt, **tail, *thead;
3698 bool changed = false;
3699 char *pos;
3701 tail = &thead;
3702 thead = NULL;
3704 while (tline) {
3705 if (tline->type == TOK_PREPROC_ID &&
3706 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3707 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3708 tline->text[1] == '%')) {
3709 char *text = NULL;
3710 int type = 0, cc; /* type = 0 to placate optimisers */
3711 char tmpbuf[30];
3712 unsigned int n;
3713 int i;
3714 MMacro *mac;
3716 t = tline;
3717 tline = tline->next;
3719 mac = istk->mstk;
3720 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3721 mac = mac->next_active;
3722 if (!mac) {
3723 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3724 } else {
3725 pos = strchr(t->text, ':');
3726 if (!pos) {
3727 switch (t->text[1]) {
3729 * We have to make a substitution of one of the
3730 * forms %1, %-1, %+1, %%foo, %0.
3732 case '0':
3733 type = TOK_NUMBER;
3734 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3735 text = nasm_strdup(tmpbuf);
3736 break;
3737 case '%':
3738 type = TOK_ID;
3739 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3740 mac->unique);
3741 text = nasm_strcat(tmpbuf, t->text + 2);
3742 break;
3743 case '-':
3744 n = atoi(t->text + 2) - 1;
3745 if (n >= mac->nparam)
3746 tt = NULL;
3747 else {
3748 if (mac->nparam > 1)
3749 n = (n + mac->rotate) % mac->nparam;
3750 tt = mac->params[n];
3752 cc = find_cc(tt);
3753 if (cc == -1) {
3754 error(ERR_NONFATAL,
3755 "macro parameter %d is not a condition code",
3756 n + 1);
3757 text = NULL;
3758 } else {
3759 type = TOK_ID;
3760 if (inverse_ccs[cc] == -1) {
3761 error(ERR_NONFATAL,
3762 "condition code `%s' is not invertible",
3763 conditions[cc]);
3764 text = NULL;
3765 } else
3766 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3768 break;
3769 case '+':
3770 n = atoi(t->text + 2) - 1;
3771 if (n >= mac->nparam)
3772 tt = NULL;
3773 else {
3774 if (mac->nparam > 1)
3775 n = (n + mac->rotate) % mac->nparam;
3776 tt = mac->params[n];
3778 cc = find_cc(tt);
3779 if (cc == -1) {
3780 error(ERR_NONFATAL,
3781 "macro parameter %d is not a condition code",
3782 n + 1);
3783 text = NULL;
3784 } else {
3785 type = TOK_ID;
3786 text = nasm_strdup(conditions[cc]);
3788 break;
3789 default:
3790 n = atoi(t->text + 1) - 1;
3791 if (n >= mac->nparam)
3792 tt = NULL;
3793 else {
3794 if (mac->nparam > 1)
3795 n = (n + mac->rotate) % mac->nparam;
3796 tt = mac->params[n];
3798 if (tt) {
3799 for (i = 0; i < mac->paramlen[n]; i++) {
3800 *tail = new_Token(NULL, tt->type, tt->text, 0);
3801 tail = &(*tail)->next;
3802 tt = tt->next;
3805 text = NULL; /* we've done it here */
3806 break;
3808 } else {
3810 * seems we have a parameters range here
3812 Token *head, **last;
3813 head = expand_mmac_params_range(mac, t, &last);
3814 if (head != t) {
3815 *tail = head;
3816 *last = tline;
3817 tline = head;
3818 text = NULL;
3822 if (!text) {
3823 delete_Token(t);
3824 } else {
3825 *tail = t;
3826 tail = &t->next;
3827 t->type = type;
3828 nasm_free(t->text);
3829 t->text = text;
3830 t->a.mac = NULL;
3832 changed = true;
3833 continue;
3834 } else if (tline->type == TOK_INDIRECT) {
3835 t = tline;
3836 tline = tline->next;
3837 tt = tokenize(t->text);
3838 tt = expand_mmac_params(tt);
3839 tt = expand_smacro(tt);
3840 *tail = tt;
3841 while (tt) {
3842 tt->a.mac = NULL; /* Necessary? */
3843 tail = &tt->next;
3844 tt = tt->next;
3846 delete_Token(t);
3847 changed = true;
3848 } else {
3849 t = *tail = tline;
3850 tline = tline->next;
3851 t->a.mac = NULL;
3852 tail = &t->next;
3855 *tail = NULL;
3857 if (changed)
3858 paste_tokens(&thead, false);
3860 return thead;
3864 * Expand all single-line macro calls made in the given line.
3865 * Return the expanded version of the line. The original is deemed
3866 * to be destroyed in the process. (In reality we'll just move
3867 * Tokens from input to output a lot of the time, rather than
3868 * actually bothering to destroy and replicate.)
3871 static Token *expand_smacro(Token * tline)
3873 Token *t, *tt, *mstart, **tail, *thead;
3874 SMacro *head = NULL, *m;
3875 Token **params;
3876 int *paramsize;
3877 unsigned int nparam, sparam;
3878 int brackets;
3879 Token *org_tline = tline;
3880 Context *ctx;
3881 const char *mname;
3882 int deadman = DEADMAN_LIMIT;
3883 bool expanded;
3886 * Trick: we should avoid changing the start token pointer since it can
3887 * be contained in "next" field of other token. Because of this
3888 * we allocate a copy of first token and work with it; at the end of
3889 * routine we copy it back
3891 if (org_tline) {
3892 tline = new_Token(org_tline->next, org_tline->type,
3893 org_tline->text, 0);
3894 tline->a.mac = org_tline->a.mac;
3895 nasm_free(org_tline->text);
3896 org_tline->text = NULL;
3899 expanded = true; /* Always expand %+ at least once */
3901 again:
3902 thead = NULL;
3903 tail = &thead;
3905 while (tline) { /* main token loop */
3906 if (!--deadman) {
3907 error(ERR_NONFATAL, "interminable macro recursion");
3908 goto err;
3911 if ((mname = tline->text)) {
3912 /* if this token is a local macro, look in local context */
3913 if (tline->type == TOK_ID) {
3914 head = (SMacro *)hash_findix(&smacros, mname);
3915 } else if (tline->type == TOK_PREPROC_ID) {
3916 ctx = get_ctx(mname, &mname, true);
3917 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3918 } else
3919 head = NULL;
3922 * We've hit an identifier. As in is_mmacro below, we first
3923 * check whether the identifier is a single-line macro at
3924 * all, then think about checking for parameters if
3925 * necessary.
3927 list_for_each(m, head)
3928 if (!mstrcmp(m->name, mname, m->casesense))
3929 break;
3930 if (m) {
3931 mstart = tline;
3932 params = NULL;
3933 paramsize = NULL;
3934 if (m->nparam == 0) {
3936 * Simple case: the macro is parameterless. Discard the
3937 * one token that the macro call took, and push the
3938 * expansion back on the to-do stack.
3940 if (!m->expansion) {
3941 if (!strcmp("__FILE__", m->name)) {
3942 int32_t num = 0;
3943 char *file = NULL;
3944 src_get(&num, &file);
3945 tline->text = nasm_quote(file, strlen(file));
3946 tline->type = TOK_STRING;
3947 nasm_free(file);
3948 continue;
3950 if (!strcmp("__LINE__", m->name)) {
3951 nasm_free(tline->text);
3952 make_tok_num(tline, src_get_linnum());
3953 continue;
3955 if (!strcmp("__BITS__", m->name)) {
3956 nasm_free(tline->text);
3957 make_tok_num(tline, globalbits);
3958 continue;
3960 tline = delete_Token(tline);
3961 continue;
3963 } else {
3965 * Complicated case: at least one macro with this name
3966 * exists and takes parameters. We must find the
3967 * parameters in the call, count them, find the SMacro
3968 * that corresponds to that form of the macro call, and
3969 * substitute for the parameters when we expand. What a
3970 * pain.
3972 /*tline = tline->next;
3973 skip_white_(tline); */
3974 do {
3975 t = tline->next;
3976 while (tok_type_(t, TOK_SMAC_END)) {
3977 t->a.mac->in_progress = false;
3978 t->text = NULL;
3979 t = tline->next = delete_Token(t);
3981 tline = t;
3982 } while (tok_type_(tline, TOK_WHITESPACE));
3983 if (!tok_is_(tline, "(")) {
3985 * This macro wasn't called with parameters: ignore
3986 * the call. (Behaviour borrowed from gnu cpp.)
3988 tline = mstart;
3989 m = NULL;
3990 } else {
3991 int paren = 0;
3992 int white = 0;
3993 brackets = 0;
3994 nparam = 0;
3995 sparam = PARAM_DELTA;
3996 params = nasm_malloc(sparam * sizeof(Token *));
3997 params[0] = tline->next;
3998 paramsize = nasm_malloc(sparam * sizeof(int));
3999 paramsize[0] = 0;
4000 while (true) { /* parameter loop */
4002 * For some unusual expansions
4003 * which concatenates function call
4005 t = tline->next;
4006 while (tok_type_(t, TOK_SMAC_END)) {
4007 t->a.mac->in_progress = false;
4008 t->text = NULL;
4009 t = tline->next = delete_Token(t);
4011 tline = t;
4013 if (!tline) {
4014 error(ERR_NONFATAL,
4015 "macro call expects terminating `)'");
4016 break;
4018 if (tline->type == TOK_WHITESPACE
4019 && brackets <= 0) {
4020 if (paramsize[nparam])
4021 white++;
4022 else
4023 params[nparam] = tline->next;
4024 continue; /* parameter loop */
4026 if (tline->type == TOK_OTHER
4027 && tline->text[1] == 0) {
4028 char ch = tline->text[0];
4029 if (ch == ',' && !paren && brackets <= 0) {
4030 if (++nparam >= sparam) {
4031 sparam += PARAM_DELTA;
4032 params = nasm_realloc(params,
4033 sparam * sizeof(Token *));
4034 paramsize = nasm_realloc(paramsize,
4035 sparam * sizeof(int));
4037 params[nparam] = tline->next;
4038 paramsize[nparam] = 0;
4039 white = 0;
4040 continue; /* parameter loop */
4042 if (ch == '{' &&
4043 (brackets > 0 || (brackets == 0 &&
4044 !paramsize[nparam])))
4046 if (!(brackets++)) {
4047 params[nparam] = tline->next;
4048 continue; /* parameter loop */
4051 if (ch == '}' && brackets > 0)
4052 if (--brackets == 0) {
4053 brackets = -1;
4054 continue; /* parameter loop */
4056 if (ch == '(' && !brackets)
4057 paren++;
4058 if (ch == ')' && brackets <= 0)
4059 if (--paren < 0)
4060 break;
4062 if (brackets < 0) {
4063 brackets = 0;
4064 error(ERR_NONFATAL, "braces do not "
4065 "enclose all of macro parameter");
4067 paramsize[nparam] += white + 1;
4068 white = 0;
4069 } /* parameter loop */
4070 nparam++;
4071 while (m && (m->nparam != nparam ||
4072 mstrcmp(m->name, mname,
4073 m->casesense)))
4074 m = m->next;
4075 if (!m)
4076 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4077 "macro `%s' exists, "
4078 "but not taking %d parameters",
4079 mstart->text, nparam);
4082 if (m && m->in_progress)
4083 m = NULL;
4084 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4086 * Design question: should we handle !tline, which
4087 * indicates missing ')' here, or expand those
4088 * macros anyway, which requires the (t) test a few
4089 * lines down?
4091 nasm_free(params);
4092 nasm_free(paramsize);
4093 tline = mstart;
4094 } else {
4096 * Expand the macro: we are placed on the last token of the
4097 * call, so that we can easily split the call from the
4098 * following tokens. We also start by pushing an SMAC_END
4099 * token for the cycle removal.
4101 t = tline;
4102 if (t) {
4103 tline = t->next;
4104 t->next = NULL;
4106 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4107 tt->a.mac = m;
4108 m->in_progress = true;
4109 tline = tt;
4110 list_for_each(t, m->expansion) {
4111 if (t->type >= TOK_SMAC_PARAM) {
4112 Token *pcopy = tline, **ptail = &pcopy;
4113 Token *ttt, *pt;
4114 int i;
4116 ttt = params[t->type - TOK_SMAC_PARAM];
4117 i = paramsize[t->type - TOK_SMAC_PARAM];
4118 while (--i >= 0) {
4119 pt = *ptail = new_Token(tline, ttt->type,
4120 ttt->text, 0);
4121 ptail = &pt->next;
4122 ttt = ttt->next;
4124 tline = pcopy;
4125 } else if (t->type == TOK_PREPROC_Q) {
4126 tt = new_Token(tline, TOK_ID, mname, 0);
4127 tline = tt;
4128 } else if (t->type == TOK_PREPROC_QQ) {
4129 tt = new_Token(tline, TOK_ID, m->name, 0);
4130 tline = tt;
4131 } else {
4132 tt = new_Token(tline, t->type, t->text, 0);
4133 tline = tt;
4138 * Having done that, get rid of the macro call, and clean
4139 * up the parameters.
4141 nasm_free(params);
4142 nasm_free(paramsize);
4143 free_tlist(mstart);
4144 expanded = true;
4145 continue; /* main token loop */
4150 if (tline->type == TOK_SMAC_END) {
4151 tline->a.mac->in_progress = false;
4152 tline = delete_Token(tline);
4153 } else {
4154 t = *tail = tline;
4155 tline = tline->next;
4156 t->a.mac = NULL;
4157 t->next = NULL;
4158 tail = &t->next;
4163 * Now scan the entire line and look for successive TOK_IDs that resulted
4164 * after expansion (they can't be produced by tokenize()). The successive
4165 * TOK_IDs should be concatenated.
4166 * Also we look for %+ tokens and concatenate the tokens before and after
4167 * them (without white spaces in between).
4169 if (expanded && paste_tokens(&thead, true)) {
4171 * If we concatenated something, *and* we had previously expanded
4172 * an actual macro, scan the lines again for macros...
4174 tline = thead;
4175 expanded = false;
4176 goto again;
4179 err:
4180 if (org_tline) {
4181 if (thead) {
4182 *org_tline = *thead;
4183 /* since we just gave text to org_line, don't free it */
4184 thead->text = NULL;
4185 delete_Token(thead);
4186 } else {
4187 /* the expression expanded to empty line;
4188 we can't return NULL for some reasons
4189 we just set the line to a single WHITESPACE token. */
4190 memset(org_tline, 0, sizeof(*org_tline));
4191 org_tline->text = NULL;
4192 org_tline->type = TOK_WHITESPACE;
4194 thead = org_tline;
4197 return thead;
4201 * Similar to expand_smacro but used exclusively with macro identifiers
4202 * right before they are fetched in. The reason is that there can be
4203 * identifiers consisting of several subparts. We consider that if there
4204 * are more than one element forming the name, user wants a expansion,
4205 * otherwise it will be left as-is. Example:
4207 * %define %$abc cde
4209 * the identifier %$abc will be left as-is so that the handler for %define
4210 * will suck it and define the corresponding value. Other case:
4212 * %define _%$abc cde
4214 * In this case user wants name to be expanded *before* %define starts
4215 * working, so we'll expand %$abc into something (if it has a value;
4216 * otherwise it will be left as-is) then concatenate all successive
4217 * PP_IDs into one.
4219 static Token *expand_id(Token * tline)
4221 Token *cur, *oldnext = NULL;
4223 if (!tline || !tline->next)
4224 return tline;
4226 cur = tline;
4227 while (cur->next &&
4228 (cur->next->type == TOK_ID ||
4229 cur->next->type == TOK_PREPROC_ID
4230 || cur->next->type == TOK_NUMBER))
4231 cur = cur->next;
4233 /* If identifier consists of just one token, don't expand */
4234 if (cur == tline)
4235 return tline;
4237 if (cur) {
4238 oldnext = cur->next; /* Detach the tail past identifier */
4239 cur->next = NULL; /* so that expand_smacro stops here */
4242 tline = expand_smacro(tline);
4244 if (cur) {
4245 /* expand_smacro possibly changhed tline; re-scan for EOL */
4246 cur = tline;
4247 while (cur && cur->next)
4248 cur = cur->next;
4249 if (cur)
4250 cur->next = oldnext;
4253 return tline;
4257 * Determine whether the given line constitutes a multi-line macro
4258 * call, and return the MMacro structure called if so. Doesn't have
4259 * to check for an initial label - that's taken care of in
4260 * expand_mmacro - but must check numbers of parameters. Guaranteed
4261 * to be called with tline->type == TOK_ID, so the putative macro
4262 * name is easy to find.
4264 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4266 MMacro *head, *m;
4267 Token **params;
4268 int nparam;
4270 head = (MMacro *) hash_findix(&mmacros, tline->text);
4273 * Efficiency: first we see if any macro exists with the given
4274 * name. If not, we can return NULL immediately. _Then_ we
4275 * count the parameters, and then we look further along the
4276 * list if necessary to find the proper MMacro.
4278 list_for_each(m, head)
4279 if (!mstrcmp(m->name, tline->text, m->casesense))
4280 break;
4281 if (!m)
4282 return NULL;
4285 * OK, we have a potential macro. Count and demarcate the
4286 * parameters.
4288 count_mmac_params(tline->next, &nparam, &params);
4291 * So we know how many parameters we've got. Find the MMacro
4292 * structure that handles this number.
4294 while (m) {
4295 if (m->nparam_min <= nparam
4296 && (m->plus || nparam <= m->nparam_max)) {
4298 * This one is right. Just check if cycle removal
4299 * prohibits us using it before we actually celebrate...
4301 if (m->in_progress > m->max_depth) {
4302 if (m->max_depth > 0) {
4303 error(ERR_WARNING,
4304 "reached maximum recursion depth of %i",
4305 m->max_depth);
4307 nasm_free(params);
4308 return NULL;
4311 * It's right, and we can use it. Add its default
4312 * parameters to the end of our list if necessary.
4314 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4315 params =
4316 nasm_realloc(params,
4317 ((m->nparam_min + m->ndefs +
4318 1) * sizeof(*params)));
4319 while (nparam < m->nparam_min + m->ndefs) {
4320 params[nparam] = m->defaults[nparam - m->nparam_min];
4321 nparam++;
4325 * If we've gone over the maximum parameter count (and
4326 * we're in Plus mode), ignore parameters beyond
4327 * nparam_max.
4329 if (m->plus && nparam > m->nparam_max)
4330 nparam = m->nparam_max;
4332 * Then terminate the parameter list, and leave.
4334 if (!params) { /* need this special case */
4335 params = nasm_malloc(sizeof(*params));
4336 nparam = 0;
4338 params[nparam] = NULL;
4339 *params_array = params;
4340 return m;
4343 * This one wasn't right: look for the next one with the
4344 * same name.
4346 list_for_each(m, m->next)
4347 if (!mstrcmp(m->name, tline->text, m->casesense))
4348 break;
4352 * After all that, we didn't find one with the right number of
4353 * parameters. Issue a warning, and fail to expand the macro.
4355 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4356 "macro `%s' exists, but not taking %d parameters",
4357 tline->text, nparam);
4358 nasm_free(params);
4359 return NULL;
4364 * Save MMacro invocation specific fields in
4365 * preparation for a recursive macro expansion
4367 static void push_mmacro(MMacro *m)
4369 MMacroInvocation *i;
4371 i = nasm_malloc(sizeof(MMacroInvocation));
4372 i->prev = m->prev;
4373 i->params = m->params;
4374 i->iline = m->iline;
4375 i->nparam = m->nparam;
4376 i->rotate = m->rotate;
4377 i->paramlen = m->paramlen;
4378 i->unique = m->unique;
4379 i->condcnt = m->condcnt;
4380 m->prev = i;
4385 * Restore MMacro invocation specific fields that were
4386 * saved during a previous recursive macro expansion
4388 static void pop_mmacro(MMacro *m)
4390 MMacroInvocation *i;
4392 if (m->prev) {
4393 i = m->prev;
4394 m->prev = i->prev;
4395 m->params = i->params;
4396 m->iline = i->iline;
4397 m->nparam = i->nparam;
4398 m->rotate = i->rotate;
4399 m->paramlen = i->paramlen;
4400 m->unique = i->unique;
4401 m->condcnt = i->condcnt;
4402 nasm_free(i);
4408 * Expand the multi-line macro call made by the given line, if
4409 * there is one to be expanded. If there is, push the expansion on
4410 * istk->expansion and return 1. Otherwise return 0.
4412 static int expand_mmacro(Token * tline)
4414 Token *startline = tline;
4415 Token *label = NULL;
4416 int dont_prepend = 0;
4417 Token **params, *t, *mtok, *tt;
4418 MMacro *m;
4419 Line *l, *ll;
4420 int i, nparam, *paramlen;
4421 const char *mname;
4423 t = tline;
4424 skip_white_(t);
4425 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4426 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4427 return 0;
4428 mtok = t;
4429 m = is_mmacro(t, &params);
4430 if (m) {
4431 mname = t->text;
4432 } else {
4433 Token *last;
4435 * We have an id which isn't a macro call. We'll assume
4436 * it might be a label; we'll also check to see if a
4437 * colon follows it. Then, if there's another id after
4438 * that lot, we'll check it again for macro-hood.
4440 label = last = t;
4441 t = t->next;
4442 if (tok_type_(t, TOK_WHITESPACE))
4443 last = t, t = t->next;
4444 if (tok_is_(t, ":")) {
4445 dont_prepend = 1;
4446 last = t, t = t->next;
4447 if (tok_type_(t, TOK_WHITESPACE))
4448 last = t, t = t->next;
4450 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4451 return 0;
4452 last->next = NULL;
4453 mname = t->text;
4454 tline = t;
4458 * Fix up the parameters: this involves stripping leading and
4459 * trailing whitespace, then stripping braces if they are
4460 * present.
4462 for (nparam = 0; params[nparam]; nparam++) ;
4463 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4465 for (i = 0; params[i]; i++) {
4466 int brace = false;
4467 int comma = (!m->plus || i < nparam - 1);
4469 t = params[i];
4470 skip_white_(t);
4471 if (tok_is_(t, "{"))
4472 t = t->next, brace = true, comma = false;
4473 params[i] = t;
4474 paramlen[i] = 0;
4475 while (t) {
4476 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4477 break; /* ... because we have hit a comma */
4478 if (comma && t->type == TOK_WHITESPACE
4479 && tok_is_(t->next, ","))
4480 break; /* ... or a space then a comma */
4481 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4482 break; /* ... or a brace */
4483 t = t->next;
4484 paramlen[i]++;
4489 * OK, we have a MMacro structure together with a set of
4490 * parameters. We must now go through the expansion and push
4491 * copies of each Line on to istk->expansion. Substitution of
4492 * parameter tokens and macro-local tokens doesn't get done
4493 * until the single-line macro substitution process; this is
4494 * because delaying them allows us to change the semantics
4495 * later through %rotate.
4497 * First, push an end marker on to istk->expansion, mark this
4498 * macro as in progress, and set up its invocation-specific
4499 * variables.
4501 ll = nasm_malloc(sizeof(Line));
4502 ll->next = istk->expansion;
4503 ll->finishes = m;
4504 ll->first = NULL;
4505 istk->expansion = ll;
4508 * Save the previous MMacro expansion in the case of
4509 * macro recursion
4511 if (m->max_depth && m->in_progress)
4512 push_mmacro(m);
4514 m->in_progress ++;
4515 m->params = params;
4516 m->iline = tline;
4517 m->nparam = nparam;
4518 m->rotate = 0;
4519 m->paramlen = paramlen;
4520 m->unique = unique++;
4521 m->lineno = 0;
4522 m->condcnt = 0;
4524 m->next_active = istk->mstk;
4525 istk->mstk = m;
4527 list_for_each(l, m->expansion) {
4528 Token **tail;
4530 ll = nasm_malloc(sizeof(Line));
4531 ll->finishes = NULL;
4532 ll->next = istk->expansion;
4533 istk->expansion = ll;
4534 tail = &ll->first;
4536 list_for_each(t, l->first) {
4537 Token *x = t;
4538 switch (t->type) {
4539 case TOK_PREPROC_Q:
4540 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4541 break;
4542 case TOK_PREPROC_QQ:
4543 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4544 break;
4545 case TOK_PREPROC_ID:
4546 if (t->text[1] == '0' && t->text[2] == '0') {
4547 dont_prepend = -1;
4548 x = label;
4549 if (!x)
4550 continue;
4552 /* fall through */
4553 default:
4554 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4555 break;
4557 tail = &tt->next;
4559 *tail = NULL;
4563 * If we had a label, push it on as the first line of
4564 * the macro expansion.
4566 if (label) {
4567 if (dont_prepend < 0)
4568 free_tlist(startline);
4569 else {
4570 ll = nasm_malloc(sizeof(Line));
4571 ll->finishes = NULL;
4572 ll->next = istk->expansion;
4573 istk->expansion = ll;
4574 ll->first = startline;
4575 if (!dont_prepend) {
4576 while (label->next)
4577 label = label->next;
4578 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4583 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4585 return 1;
4588 /* The function that actually does the error reporting */
4589 static void verror(int severity, const char *fmt, va_list arg)
4591 char buff[1024];
4593 vsnprintf(buff, sizeof(buff), fmt, arg);
4595 if (istk && istk->mstk && istk->mstk->name)
4596 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4597 istk->mstk->lineno, buff);
4598 else
4599 nasm_error(severity, "%s", buff);
4603 * Since preprocessor always operate only on the line that didn't
4604 * arrived yet, we should always use ERR_OFFBY1.
4606 static void error(int severity, const char *fmt, ...)
4608 va_list arg;
4610 /* If we're in a dead branch of IF or something like it, ignore the error */
4611 if (istk && istk->conds && !emitting(istk->conds->state))
4612 return;
4614 va_start(arg, fmt);
4615 verror(severity, fmt, arg);
4616 va_end(arg);
4620 * Because %else etc are evaluated in the state context
4621 * of the previous branch, errors might get lost with error():
4622 * %if 0 ... %else trailing garbage ... %endif
4623 * So %else etc should report errors with this function.
4625 static void error_precond(int severity, const char *fmt, ...)
4627 va_list arg;
4629 /* Only ignore the error if it's really in a dead branch */
4630 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4631 return;
4633 va_start(arg, fmt);
4634 verror(severity, fmt, arg);
4635 va_end(arg);
4638 static void
4639 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4641 Token *t;
4643 cstk = NULL;
4644 istk = nasm_malloc(sizeof(Include));
4645 istk->next = NULL;
4646 istk->conds = NULL;
4647 istk->expansion = NULL;
4648 istk->mstk = NULL;
4649 istk->fp = fopen(file, "r");
4650 istk->fname = NULL;
4651 src_set_fname(nasm_strdup(file));
4652 src_set_linnum(0);
4653 istk->lineinc = 1;
4654 if (!istk->fp)
4655 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4656 file);
4657 defining = NULL;
4658 nested_mac_count = 0;
4659 nested_rep_count = 0;
4660 init_macros();
4661 unique = 0;
4662 if (tasm_compatible_mode) {
4663 stdmacpos = nasm_stdmac;
4664 } else {
4665 stdmacpos = nasm_stdmac_after_tasm;
4667 any_extrastdmac = extrastdmac && *extrastdmac;
4668 do_predef = true;
4669 list = listgen;
4672 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4673 * The caller, however, will also pass in 3 for preprocess-only so
4674 * we can set __PASS__ accordingly.
4676 pass = apass > 2 ? 2 : apass;
4678 dephead = deptail = deplist;
4679 if (deplist) {
4680 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4681 sl->next = NULL;
4682 strcpy(sl->str, file);
4683 *deptail = sl;
4684 deptail = &sl->next;
4688 * Define the __PASS__ macro. This is defined here unlike
4689 * all the other builtins, because it is special -- it varies between
4690 * passes.
4692 t = nasm_malloc(sizeof(*t));
4693 t->next = NULL;
4694 make_tok_num(t, apass);
4695 t->a.mac = NULL;
4696 define_smacro(NULL, "__PASS__", true, 0, t);
4699 static char *pp_getline(void)
4701 char *line;
4702 Token *tline;
4704 while (1) {
4706 * Fetch a tokenized line, either from the macro-expansion
4707 * buffer or from the input file.
4709 tline = NULL;
4710 while (istk->expansion && istk->expansion->finishes) {
4711 Line *l = istk->expansion;
4712 if (!l->finishes->name && l->finishes->in_progress > 1) {
4713 Line *ll;
4716 * This is a macro-end marker for a macro with no
4717 * name, which means it's not really a macro at all
4718 * but a %rep block, and the `in_progress' field is
4719 * more than 1, meaning that we still need to
4720 * repeat. (1 means the natural last repetition; 0
4721 * means termination by %exitrep.) We have
4722 * therefore expanded up to the %endrep, and must
4723 * push the whole block on to the expansion buffer
4724 * again. We don't bother to remove the macro-end
4725 * marker: we'd only have to generate another one
4726 * if we did.
4728 l->finishes->in_progress--;
4729 list_for_each(l, l->finishes->expansion) {
4730 Token *t, *tt, **tail;
4732 ll = nasm_malloc(sizeof(Line));
4733 ll->next = istk->expansion;
4734 ll->finishes = NULL;
4735 ll->first = NULL;
4736 tail = &ll->first;
4738 list_for_each(t, l->first) {
4739 if (t->text || t->type == TOK_WHITESPACE) {
4740 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4741 tail = &tt->next;
4745 istk->expansion = ll;
4747 } else {
4749 * Check whether a `%rep' was started and not ended
4750 * within this macro expansion. This can happen and
4751 * should be detected. It's a fatal error because
4752 * I'm too confused to work out how to recover
4753 * sensibly from it.
4755 if (defining) {
4756 if (defining->name)
4757 error(ERR_PANIC,
4758 "defining with name in expansion");
4759 else if (istk->mstk->name)
4760 error(ERR_FATAL,
4761 "`%%rep' without `%%endrep' within"
4762 " expansion of macro `%s'",
4763 istk->mstk->name);
4767 * FIXME: investigate the relationship at this point between
4768 * istk->mstk and l->finishes
4771 MMacro *m = istk->mstk;
4772 istk->mstk = m->next_active;
4773 if (m->name) {
4775 * This was a real macro call, not a %rep, and
4776 * therefore the parameter information needs to
4777 * be freed.
4779 if (m->prev) {
4780 pop_mmacro(m);
4781 l->finishes->in_progress --;
4782 } else {
4783 nasm_free(m->params);
4784 free_tlist(m->iline);
4785 nasm_free(m->paramlen);
4786 l->finishes->in_progress = 0;
4788 } else
4789 free_mmacro(m);
4791 istk->expansion = l->next;
4792 nasm_free(l);
4793 list->downlevel(LIST_MACRO);
4796 while (1) { /* until we get a line we can use */
4798 if (istk->expansion) { /* from a macro expansion */
4799 char *p;
4800 Line *l = istk->expansion;
4801 if (istk->mstk)
4802 istk->mstk->lineno++;
4803 tline = l->first;
4804 istk->expansion = l->next;
4805 nasm_free(l);
4806 p = detoken(tline, false);
4807 list->line(LIST_MACRO, p);
4808 nasm_free(p);
4809 break;
4811 line = read_line();
4812 if (line) { /* from the current input file */
4813 line = prepreproc(line);
4814 tline = tokenize(line);
4815 nasm_free(line);
4816 break;
4819 * The current file has ended; work down the istk
4822 Include *i = istk;
4823 fclose(i->fp);
4824 if (i->conds)
4825 error(ERR_FATAL,
4826 "expected `%%endif' before end of file");
4827 /* only set line and file name if there's a next node */
4828 if (i->next) {
4829 src_set_linnum(i->lineno);
4830 nasm_free(src_set_fname(i->fname));
4832 istk = i->next;
4833 list->downlevel(LIST_INCLUDE);
4834 nasm_free(i);
4835 if (!istk)
4836 return NULL;
4837 if (istk->expansion && istk->expansion->finishes)
4838 break;
4843 * We must expand MMacro parameters and MMacro-local labels
4844 * _before_ we plunge into directive processing, to cope
4845 * with things like `%define something %1' such as STRUC
4846 * uses. Unless we're _defining_ a MMacro, in which case
4847 * those tokens should be left alone to go into the
4848 * definition; and unless we're in a non-emitting
4849 * condition, in which case we don't want to meddle with
4850 * anything.
4852 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4853 && !(istk->mstk && !istk->mstk->in_progress)) {
4854 tline = expand_mmac_params(tline);
4858 * Check the line to see if it's a preprocessor directive.
4860 if (do_directive(tline) == DIRECTIVE_FOUND) {
4861 continue;
4862 } else if (defining) {
4864 * We're defining a multi-line macro. We emit nothing
4865 * at all, and just
4866 * shove the tokenized line on to the macro definition.
4868 Line *l = nasm_malloc(sizeof(Line));
4869 l->next = defining->expansion;
4870 l->first = tline;
4871 l->finishes = NULL;
4872 defining->expansion = l;
4873 continue;
4874 } else if (istk->conds && !emitting(istk->conds->state)) {
4876 * We're in a non-emitting branch of a condition block.
4877 * Emit nothing at all, not even a blank line: when we
4878 * emerge from the condition we'll give a line-number
4879 * directive so we keep our place correctly.
4881 free_tlist(tline);
4882 continue;
4883 } else if (istk->mstk && !istk->mstk->in_progress) {
4885 * We're in a %rep block which has been terminated, so
4886 * we're walking through to the %endrep without
4887 * emitting anything. Emit nothing at all, not even a
4888 * blank line: when we emerge from the %rep block we'll
4889 * give a line-number directive so we keep our place
4890 * correctly.
4892 free_tlist(tline);
4893 continue;
4894 } else {
4895 tline = expand_smacro(tline);
4896 if (!expand_mmacro(tline)) {
4898 * De-tokenize the line again, and emit it.
4900 line = detoken(tline, true);
4901 free_tlist(tline);
4902 break;
4903 } else {
4904 continue; /* expand_mmacro calls free_tlist */
4909 return line;
4912 static void pp_cleanup(int pass)
4914 if (defining) {
4915 if (defining->name) {
4916 error(ERR_NONFATAL,
4917 "end of file while still defining macro `%s'",
4918 defining->name);
4919 } else {
4920 error(ERR_NONFATAL, "end of file while still in %%rep");
4923 free_mmacro(defining);
4924 defining = NULL;
4926 while (cstk)
4927 ctx_pop();
4928 free_macros();
4929 while (istk) {
4930 Include *i = istk;
4931 istk = istk->next;
4932 fclose(i->fp);
4933 nasm_free(i->fname);
4934 nasm_free(i);
4936 while (cstk)
4937 ctx_pop();
4938 nasm_free(src_set_fname(NULL));
4939 if (pass == 0) {
4940 IncPath *i;
4941 free_llist(predef);
4942 delete_Blocks();
4943 while ((i = ipath)) {
4944 ipath = i->next;
4945 if (i->path)
4946 nasm_free(i->path);
4947 nasm_free(i);
4952 void pp_include_path(char *path)
4954 IncPath *i;
4956 i = nasm_malloc(sizeof(IncPath));
4957 i->path = path ? nasm_strdup(path) : NULL;
4958 i->next = NULL;
4960 if (ipath) {
4961 IncPath *j = ipath;
4962 while (j->next)
4963 j = j->next;
4964 j->next = i;
4965 } else {
4966 ipath = i;
4970 void pp_pre_include(char *fname)
4972 Token *inc, *space, *name;
4973 Line *l;
4975 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4976 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4977 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4979 l = nasm_malloc(sizeof(Line));
4980 l->next = predef;
4981 l->first = inc;
4982 l->finishes = NULL;
4983 predef = l;
4986 void pp_pre_define(char *definition)
4988 Token *def, *space;
4989 Line *l;
4990 char *equals;
4992 equals = strchr(definition, '=');
4993 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4994 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4995 if (equals)
4996 *equals = ' ';
4997 space->next = tokenize(definition);
4998 if (equals)
4999 *equals = '=';
5001 l = nasm_malloc(sizeof(Line));
5002 l->next = predef;
5003 l->first = def;
5004 l->finishes = NULL;
5005 predef = l;
5008 void pp_pre_undefine(char *definition)
5010 Token *def, *space;
5011 Line *l;
5013 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5014 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5015 space->next = tokenize(definition);
5017 l = nasm_malloc(sizeof(Line));
5018 l->next = predef;
5019 l->first = def;
5020 l->finishes = NULL;
5021 predef = l;
5025 * Added by Keith Kanios:
5027 * This function is used to assist with "runtime" preprocessor
5028 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5030 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5031 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5034 void pp_runtime(char *definition)
5036 Token *def;
5038 def = tokenize(definition);
5039 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5040 free_tlist(def);
5044 void pp_extra_stdmac(macros_t *macros)
5046 extrastdmac = macros;
5049 static void make_tok_num(Token * tok, int64_t val)
5051 char numbuf[20];
5052 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5053 tok->text = nasm_strdup(numbuf);
5054 tok->type = TOK_NUMBER;
5057 Preproc nasmpp = {
5058 pp_reset,
5059 pp_getline,
5060 pp_cleanup