tokenize: Fix wrong string index in indirect strings
[nasm.git] / preproc.c
blobef4f95c32d3f4e08ca6f1056a375b72cd1215342
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 nasm_free(t->text);
1185 if (p)
1186 t->text = nasm_strdup(p);
1187 else
1188 t->text = NULL;
1190 /* Expand local macros here and not during preprocessing */
1191 if (expand_locals &&
1192 t->type == TOK_PREPROC_ID && t->text &&
1193 t->text[0] == '%' && t->text[1] == '$') {
1194 const char *q;
1195 char *p;
1196 Context *ctx = get_ctx(t->text, &q, false);
1197 if (ctx) {
1198 char buffer[40];
1199 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1200 p = nasm_strcat(buffer, q);
1201 nasm_free(t->text);
1202 t->text = p;
1205 if (t->type == TOK_WHITESPACE)
1206 len++;
1207 else if (t->text)
1208 len += strlen(t->text);
1211 p = line = nasm_malloc(len + 1);
1213 list_for_each(t, tlist) {
1214 if (t->type == TOK_WHITESPACE) {
1215 *p++ = ' ';
1216 } else if (t->text) {
1217 q = t->text;
1218 while (*q)
1219 *p++ = *q++;
1222 *p = '\0';
1224 return line;
1228 * A scanner, suitable for use by the expression evaluator, which
1229 * operates on a line of Tokens. Expects a pointer to a pointer to
1230 * the first token in the line to be passed in as its private_data
1231 * field.
1233 * FIX: This really needs to be unified with stdscan.
1235 static int ppscan(void *private_data, struct tokenval *tokval)
1237 Token **tlineptr = private_data;
1238 Token *tline;
1239 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1241 do {
1242 tline = *tlineptr;
1243 *tlineptr = tline ? tline->next : NULL;
1244 } while (tline && (tline->type == TOK_WHITESPACE ||
1245 tline->type == TOK_COMMENT));
1247 if (!tline)
1248 return tokval->t_type = TOKEN_EOS;
1250 tokval->t_charptr = tline->text;
1252 if (tline->text[0] == '$' && !tline->text[1])
1253 return tokval->t_type = TOKEN_HERE;
1254 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1255 return tokval->t_type = TOKEN_BASE;
1257 if (tline->type == TOK_ID) {
1258 p = tokval->t_charptr = tline->text;
1259 if (p[0] == '$') {
1260 tokval->t_charptr++;
1261 return tokval->t_type = TOKEN_ID;
1264 for (r = p, s = ourcopy; *r; r++) {
1265 if (r >= p+MAX_KEYWORD)
1266 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1267 *s++ = nasm_tolower(*r);
1269 *s = '\0';
1270 /* right, so we have an identifier sitting in temp storage. now,
1271 * is it actually a register or instruction name, or what? */
1272 return nasm_token_hash(ourcopy, tokval);
1275 if (tline->type == TOK_NUMBER) {
1276 bool rn_error;
1277 tokval->t_integer = readnum(tline->text, &rn_error);
1278 tokval->t_charptr = tline->text;
1279 if (rn_error)
1280 return tokval->t_type = TOKEN_ERRNUM;
1281 else
1282 return tokval->t_type = TOKEN_NUM;
1285 if (tline->type == TOK_FLOAT) {
1286 return tokval->t_type = TOKEN_FLOAT;
1289 if (tline->type == TOK_STRING) {
1290 char bq, *ep;
1292 bq = tline->text[0];
1293 tokval->t_charptr = tline->text;
1294 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1296 if (ep[0] != bq || ep[1] != '\0')
1297 return tokval->t_type = TOKEN_ERRSTR;
1298 else
1299 return tokval->t_type = TOKEN_STR;
1302 if (tline->type == TOK_OTHER) {
1303 if (!strcmp(tline->text, "<<"))
1304 return tokval->t_type = TOKEN_SHL;
1305 if (!strcmp(tline->text, ">>"))
1306 return tokval->t_type = TOKEN_SHR;
1307 if (!strcmp(tline->text, "//"))
1308 return tokval->t_type = TOKEN_SDIV;
1309 if (!strcmp(tline->text, "%%"))
1310 return tokval->t_type = TOKEN_SMOD;
1311 if (!strcmp(tline->text, "=="))
1312 return tokval->t_type = TOKEN_EQ;
1313 if (!strcmp(tline->text, "<>"))
1314 return tokval->t_type = TOKEN_NE;
1315 if (!strcmp(tline->text, "!="))
1316 return tokval->t_type = TOKEN_NE;
1317 if (!strcmp(tline->text, "<="))
1318 return tokval->t_type = TOKEN_LE;
1319 if (!strcmp(tline->text, ">="))
1320 return tokval->t_type = TOKEN_GE;
1321 if (!strcmp(tline->text, "&&"))
1322 return tokval->t_type = TOKEN_DBL_AND;
1323 if (!strcmp(tline->text, "^^"))
1324 return tokval->t_type = TOKEN_DBL_XOR;
1325 if (!strcmp(tline->text, "||"))
1326 return tokval->t_type = TOKEN_DBL_OR;
1330 * We have no other options: just return the first character of
1331 * the token text.
1333 return tokval->t_type = tline->text[0];
1337 * Compare a string to the name of an existing macro; this is a
1338 * simple wrapper which calls either strcmp or nasm_stricmp
1339 * depending on the value of the `casesense' parameter.
1341 static int mstrcmp(const char *p, const char *q, bool casesense)
1343 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1347 * Compare a string to the name of an existing macro; this is a
1348 * simple wrapper which calls either strcmp or nasm_stricmp
1349 * depending on the value of the `casesense' parameter.
1351 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1353 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1357 * Return the Context structure associated with a %$ token. Return
1358 * NULL, having _already_ reported an error condition, if the
1359 * context stack isn't deep enough for the supplied number of $
1360 * signs.
1361 * If all_contexts == true, contexts that enclose current are
1362 * also scanned for such smacro, until it is found; if not -
1363 * only the context that directly results from the number of $'s
1364 * in variable's name.
1366 * If "namep" is non-NULL, set it to the pointer to the macro name
1367 * tail, i.e. the part beyond %$...
1369 static Context *get_ctx(const char *name, const char **namep,
1370 bool all_contexts)
1372 Context *ctx;
1373 SMacro *m;
1374 int i;
1376 if (namep)
1377 *namep = name;
1379 if (!name || name[0] != '%' || name[1] != '$')
1380 return NULL;
1382 if (!cstk) {
1383 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1384 return NULL;
1387 name += 2;
1388 ctx = cstk;
1389 i = 0;
1390 while (ctx && *name == '$') {
1391 name++;
1392 i++;
1393 ctx = ctx->next;
1395 if (!ctx) {
1396 error(ERR_NONFATAL, "`%s': context stack is only"
1397 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1398 return NULL;
1401 if (namep)
1402 *namep = name;
1404 if (!all_contexts)
1405 return ctx;
1407 do {
1408 /* Search for this smacro in found context */
1409 m = hash_findix(&ctx->localmac, name);
1410 while (m) {
1411 if (!mstrcmp(m->name, name, m->casesense))
1412 return ctx;
1413 m = m->next;
1415 ctx = ctx->next;
1417 while (ctx);
1418 return NULL;
1422 * Check to see if a file is already in a string list
1424 static bool in_list(const StrList *list, const char *str)
1426 while (list) {
1427 if (!strcmp(list->str, str))
1428 return true;
1429 list = list->next;
1431 return false;
1435 * Open an include file. This routine must always return a valid
1436 * file pointer if it returns - it's responsible for throwing an
1437 * ERR_FATAL and bombing out completely if not. It should also try
1438 * the include path one by one until it finds the file or reaches
1439 * the end of the path.
1441 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1442 bool missing_ok)
1444 FILE *fp;
1445 char *prefix = "";
1446 IncPath *ip = ipath;
1447 int len = strlen(file);
1448 size_t prefix_len = 0;
1449 StrList *sl;
1451 while (1) {
1452 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1453 memcpy(sl->str, prefix, prefix_len);
1454 memcpy(sl->str+prefix_len, file, len+1);
1455 fp = fopen(sl->str, "r");
1456 if (fp && dhead && !in_list(*dhead, sl->str)) {
1457 sl->next = NULL;
1458 **dtail = sl;
1459 *dtail = &sl->next;
1460 } else {
1461 nasm_free(sl);
1463 if (fp)
1464 return fp;
1465 if (!ip) {
1466 if (!missing_ok)
1467 break;
1468 prefix = NULL;
1469 } else {
1470 prefix = ip->path;
1471 ip = ip->next;
1473 if (prefix) {
1474 prefix_len = strlen(prefix);
1475 } else {
1476 /* -MG given and file not found */
1477 if (dhead && !in_list(*dhead, file)) {
1478 sl = nasm_malloc(len+1+sizeof sl->next);
1479 sl->next = NULL;
1480 strcpy(sl->str, file);
1481 **dtail = sl;
1482 *dtail = &sl->next;
1484 return NULL;
1488 error(ERR_FATAL, "unable to open include file `%s'", file);
1489 return NULL;
1493 * Determine if we should warn on defining a single-line macro of
1494 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1495 * return true if _any_ single-line macro of that name is defined.
1496 * Otherwise, will return true if a single-line macro with either
1497 * `nparam' or no parameters is defined.
1499 * If a macro with precisely the right number of parameters is
1500 * defined, or nparam is -1, the address of the definition structure
1501 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1502 * is NULL, no action will be taken regarding its contents, and no
1503 * error will occur.
1505 * Note that this is also called with nparam zero to resolve
1506 * `ifdef'.
1508 * If you already know which context macro belongs to, you can pass
1509 * the context pointer as first parameter; if you won't but name begins
1510 * with %$ the context will be automatically computed. If all_contexts
1511 * is true, macro will be searched in outer contexts as well.
1513 static bool
1514 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1515 bool nocase)
1517 struct hash_table *smtbl;
1518 SMacro *m;
1520 if (ctx) {
1521 smtbl = &ctx->localmac;
1522 } else if (name[0] == '%' && name[1] == '$') {
1523 if (cstk)
1524 ctx = get_ctx(name, &name, false);
1525 if (!ctx)
1526 return false; /* got to return _something_ */
1527 smtbl = &ctx->localmac;
1528 } else {
1529 smtbl = &smacros;
1531 m = (SMacro *) hash_findix(smtbl, name);
1533 while (m) {
1534 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1535 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1536 if (defn) {
1537 if (nparam == (int) m->nparam || nparam == -1)
1538 *defn = m;
1539 else
1540 *defn = NULL;
1542 return true;
1544 m = m->next;
1547 return false;
1551 * Count and mark off the parameters in a multi-line macro call.
1552 * This is called both from within the multi-line macro expansion
1553 * code, and also to mark off the default parameters when provided
1554 * in a %macro definition line.
1556 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1558 int paramsize, brace;
1560 *nparam = paramsize = 0;
1561 *params = NULL;
1562 while (t) {
1563 /* +1: we need space for the final NULL */
1564 if (*nparam+1 >= paramsize) {
1565 paramsize += PARAM_DELTA;
1566 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1568 skip_white_(t);
1569 brace = false;
1570 if (tok_is_(t, "{"))
1571 brace = true;
1572 (*params)[(*nparam)++] = t;
1573 while (tok_isnt_(t, brace ? "}" : ","))
1574 t = t->next;
1575 if (t) { /* got a comma/brace */
1576 t = t->next;
1577 if (brace) {
1579 * Now we've found the closing brace, look further
1580 * for the comma.
1582 skip_white_(t);
1583 if (tok_isnt_(t, ",")) {
1584 error(ERR_NONFATAL,
1585 "braces do not enclose all of macro parameter");
1586 while (tok_isnt_(t, ","))
1587 t = t->next;
1589 if (t)
1590 t = t->next; /* eat the comma */
1597 * Determine whether one of the various `if' conditions is true or
1598 * not.
1600 * We must free the tline we get passed.
1602 static bool if_condition(Token * tline, enum preproc_token ct)
1604 enum pp_conditional i = PP_COND(ct);
1605 bool j;
1606 Token *t, *tt, **tptr, *origline;
1607 struct tokenval tokval;
1608 expr *evalresult;
1609 enum pp_token_type needtype;
1611 origline = tline;
1613 switch (i) {
1614 case PPC_IFCTX:
1615 j = false; /* have we matched yet? */
1616 while (true) {
1617 skip_white_(tline);
1618 if (!tline)
1619 break;
1620 if (tline->type != TOK_ID) {
1621 error(ERR_NONFATAL,
1622 "`%s' expects context identifiers", pp_directives[ct]);
1623 free_tlist(origline);
1624 return -1;
1626 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1627 j = true;
1628 tline = tline->next;
1630 break;
1632 case PPC_IFDEF:
1633 j = false; /* have we matched yet? */
1634 while (tline) {
1635 skip_white_(tline);
1636 if (!tline || (tline->type != TOK_ID &&
1637 (tline->type != TOK_PREPROC_ID ||
1638 tline->text[1] != '$'))) {
1639 error(ERR_NONFATAL,
1640 "`%s' expects macro identifiers", pp_directives[ct]);
1641 goto fail;
1643 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1644 j = true;
1645 tline = tline->next;
1647 break;
1649 case PPC_IFIDN:
1650 case PPC_IFIDNI:
1651 tline = expand_smacro(tline);
1652 t = tt = tline;
1653 while (tok_isnt_(tt, ","))
1654 tt = tt->next;
1655 if (!tt) {
1656 error(ERR_NONFATAL,
1657 "`%s' expects two comma-separated arguments",
1658 pp_directives[ct]);
1659 goto fail;
1661 tt = tt->next;
1662 j = true; /* assume equality unless proved not */
1663 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1664 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1665 error(ERR_NONFATAL, "`%s': more than one comma on line",
1666 pp_directives[ct]);
1667 goto fail;
1669 if (t->type == TOK_WHITESPACE) {
1670 t = t->next;
1671 continue;
1673 if (tt->type == TOK_WHITESPACE) {
1674 tt = tt->next;
1675 continue;
1677 if (tt->type != t->type) {
1678 j = false; /* found mismatching tokens */
1679 break;
1681 /* When comparing strings, need to unquote them first */
1682 if (t->type == TOK_STRING) {
1683 size_t l1 = nasm_unquote(t->text, NULL);
1684 size_t l2 = nasm_unquote(tt->text, NULL);
1686 if (l1 != l2) {
1687 j = false;
1688 break;
1690 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1691 j = false;
1692 break;
1694 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1695 j = false; /* found mismatching tokens */
1696 break;
1699 t = t->next;
1700 tt = tt->next;
1702 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1703 j = false; /* trailing gunk on one end or other */
1704 break;
1706 case PPC_IFMACRO:
1708 bool found = false;
1709 MMacro searching, *mmac;
1711 skip_white_(tline);
1712 tline = expand_id(tline);
1713 if (!tok_type_(tline, TOK_ID)) {
1714 error(ERR_NONFATAL,
1715 "`%s' expects a macro name", pp_directives[ct]);
1716 goto fail;
1718 searching.name = nasm_strdup(tline->text);
1719 searching.casesense = true;
1720 searching.plus = false;
1721 searching.nolist = false;
1722 searching.in_progress = 0;
1723 searching.max_depth = 0;
1724 searching.rep_nest = NULL;
1725 searching.nparam_min = 0;
1726 searching.nparam_max = INT_MAX;
1727 tline = expand_smacro(tline->next);
1728 skip_white_(tline);
1729 if (!tline) {
1730 } else if (!tok_type_(tline, TOK_NUMBER)) {
1731 error(ERR_NONFATAL,
1732 "`%s' expects a parameter count or nothing",
1733 pp_directives[ct]);
1734 } else {
1735 searching.nparam_min = searching.nparam_max =
1736 readnum(tline->text, &j);
1737 if (j)
1738 error(ERR_NONFATAL,
1739 "unable to parse parameter count `%s'",
1740 tline->text);
1742 if (tline && tok_is_(tline->next, "-")) {
1743 tline = tline->next->next;
1744 if (tok_is_(tline, "*"))
1745 searching.nparam_max = INT_MAX;
1746 else if (!tok_type_(tline, TOK_NUMBER))
1747 error(ERR_NONFATAL,
1748 "`%s' expects a parameter count after `-'",
1749 pp_directives[ct]);
1750 else {
1751 searching.nparam_max = readnum(tline->text, &j);
1752 if (j)
1753 error(ERR_NONFATAL,
1754 "unable to parse parameter count `%s'",
1755 tline->text);
1756 if (searching.nparam_min > searching.nparam_max)
1757 error(ERR_NONFATAL,
1758 "minimum parameter count exceeds maximum");
1761 if (tline && tok_is_(tline->next, "+")) {
1762 tline = tline->next;
1763 searching.plus = true;
1765 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1766 while (mmac) {
1767 if (!strcmp(mmac->name, searching.name) &&
1768 (mmac->nparam_min <= searching.nparam_max
1769 || searching.plus)
1770 && (searching.nparam_min <= mmac->nparam_max
1771 || mmac->plus)) {
1772 found = true;
1773 break;
1775 mmac = mmac->next;
1777 if (tline && tline->next)
1778 error(ERR_WARNING|ERR_PASS1,
1779 "trailing garbage after %%ifmacro ignored");
1780 nasm_free(searching.name);
1781 j = found;
1782 break;
1785 case PPC_IFID:
1786 needtype = TOK_ID;
1787 goto iftype;
1788 case PPC_IFNUM:
1789 needtype = TOK_NUMBER;
1790 goto iftype;
1791 case PPC_IFSTR:
1792 needtype = TOK_STRING;
1793 goto iftype;
1795 iftype:
1796 t = tline = expand_smacro(tline);
1798 while (tok_type_(t, TOK_WHITESPACE) ||
1799 (needtype == TOK_NUMBER &&
1800 tok_type_(t, TOK_OTHER) &&
1801 (t->text[0] == '-' || t->text[0] == '+') &&
1802 !t->text[1]))
1803 t = t->next;
1805 j = tok_type_(t, needtype);
1806 break;
1808 case PPC_IFTOKEN:
1809 t = tline = expand_smacro(tline);
1810 while (tok_type_(t, TOK_WHITESPACE))
1811 t = t->next;
1813 j = false;
1814 if (t) {
1815 t = t->next; /* Skip the actual token */
1816 while (tok_type_(t, TOK_WHITESPACE))
1817 t = t->next;
1818 j = !t; /* Should be nothing left */
1820 break;
1822 case PPC_IFEMPTY:
1823 t = tline = expand_smacro(tline);
1824 while (tok_type_(t, TOK_WHITESPACE))
1825 t = t->next;
1827 j = !t; /* Should be empty */
1828 break;
1830 case PPC_IF:
1831 t = tline = expand_smacro(tline);
1832 tptr = &t;
1833 tokval.t_type = TOKEN_INVALID;
1834 evalresult = evaluate(ppscan, tptr, &tokval,
1835 NULL, pass | CRITICAL, error, NULL);
1836 if (!evalresult)
1837 return -1;
1838 if (tokval.t_type)
1839 error(ERR_WARNING|ERR_PASS1,
1840 "trailing garbage after expression ignored");
1841 if (!is_simple(evalresult)) {
1842 error(ERR_NONFATAL,
1843 "non-constant value given to `%s'", pp_directives[ct]);
1844 goto fail;
1846 j = reloc_value(evalresult) != 0;
1847 break;
1849 default:
1850 error(ERR_FATAL,
1851 "preprocessor directive `%s' not yet implemented",
1852 pp_directives[ct]);
1853 goto fail;
1856 free_tlist(origline);
1857 return j ^ PP_NEGATIVE(ct);
1859 fail:
1860 free_tlist(origline);
1861 return -1;
1865 * Common code for defining an smacro
1867 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1868 int nparam, Token *expansion)
1870 SMacro *smac, **smhead;
1871 struct hash_table *smtbl;
1873 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1874 if (!smac) {
1875 error(ERR_WARNING|ERR_PASS1,
1876 "single-line macro `%s' defined both with and"
1877 " without parameters", mname);
1879 * Some instances of the old code considered this a failure,
1880 * some others didn't. What is the right thing to do here?
1882 free_tlist(expansion);
1883 return false; /* Failure */
1884 } else {
1886 * We're redefining, so we have to take over an
1887 * existing SMacro structure. This means freeing
1888 * what was already in it.
1890 nasm_free(smac->name);
1891 free_tlist(smac->expansion);
1893 } else {
1894 smtbl = ctx ? &ctx->localmac : &smacros;
1895 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1896 smac = nasm_malloc(sizeof(SMacro));
1897 smac->next = *smhead;
1898 *smhead = smac;
1900 smac->name = nasm_strdup(mname);
1901 smac->casesense = casesense;
1902 smac->nparam = nparam;
1903 smac->expansion = expansion;
1904 smac->in_progress = false;
1905 return true; /* Success */
1909 * Undefine an smacro
1911 static void undef_smacro(Context *ctx, const char *mname)
1913 SMacro **smhead, *s, **sp;
1914 struct hash_table *smtbl;
1916 smtbl = ctx ? &ctx->localmac : &smacros;
1917 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1919 if (smhead) {
1921 * We now have a macro name... go hunt for it.
1923 sp = smhead;
1924 while ((s = *sp) != NULL) {
1925 if (!mstrcmp(s->name, mname, s->casesense)) {
1926 *sp = s->next;
1927 nasm_free(s->name);
1928 free_tlist(s->expansion);
1929 nasm_free(s);
1930 } else {
1931 sp = &s->next;
1938 * Parse a mmacro specification.
1940 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1942 bool err;
1944 tline = tline->next;
1945 skip_white_(tline);
1946 tline = expand_id(tline);
1947 if (!tok_type_(tline, TOK_ID)) {
1948 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1949 return false;
1952 def->prev = NULL;
1953 def->name = nasm_strdup(tline->text);
1954 def->plus = false;
1955 def->nolist = false;
1956 def->in_progress = 0;
1957 def->rep_nest = NULL;
1958 def->nparam_min = 0;
1959 def->nparam_max = 0;
1961 tline = expand_smacro(tline->next);
1962 skip_white_(tline);
1963 if (!tok_type_(tline, TOK_NUMBER)) {
1964 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1965 } else {
1966 def->nparam_min = def->nparam_max =
1967 readnum(tline->text, &err);
1968 if (err)
1969 error(ERR_NONFATAL,
1970 "unable to parse parameter count `%s'", tline->text);
1972 if (tline && tok_is_(tline->next, "-")) {
1973 tline = tline->next->next;
1974 if (tok_is_(tline, "*")) {
1975 def->nparam_max = INT_MAX;
1976 } else if (!tok_type_(tline, TOK_NUMBER)) {
1977 error(ERR_NONFATAL,
1978 "`%s' expects a parameter count after `-'", directive);
1979 } else {
1980 def->nparam_max = readnum(tline->text, &err);
1981 if (err) {
1982 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1983 tline->text);
1985 if (def->nparam_min > def->nparam_max) {
1986 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1990 if (tline && tok_is_(tline->next, "+")) {
1991 tline = tline->next;
1992 def->plus = true;
1994 if (tline && tok_type_(tline->next, TOK_ID) &&
1995 !nasm_stricmp(tline->next->text, ".nolist")) {
1996 tline = tline->next;
1997 def->nolist = true;
2001 * Handle default parameters.
2003 if (tline && tline->next) {
2004 def->dlist = tline->next;
2005 tline->next = NULL;
2006 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2007 } else {
2008 def->dlist = NULL;
2009 def->defaults = NULL;
2011 def->expansion = NULL;
2013 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2014 !def->plus)
2015 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2016 "too many default macro parameters");
2018 return true;
2023 * Decode a size directive
2025 static int parse_size(const char *str) {
2026 static const char *size_names[] =
2027 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2028 static const int sizes[] =
2029 { 0, 1, 4, 16, 8, 10, 2, 32 };
2031 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2035 * nasm_unquote with error if the string contains NUL characters.
2036 * If the string contains NUL characters, issue an error and return
2037 * the C len, i.e. truncate at the NUL.
2039 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2041 size_t len = nasm_unquote(qstr, NULL);
2042 size_t clen = strlen(qstr);
2044 if (len != clen)
2045 error(ERR_NONFATAL, "NUL character in `%s' directive",
2046 pp_directives[directive]);
2048 return clen;
2052 * find and process preprocessor directive in passed line
2053 * Find out if a line contains a preprocessor directive, and deal
2054 * with it if so.
2056 * If a directive _is_ found, it is the responsibility of this routine
2057 * (and not the caller) to free_tlist() the line.
2059 * @param tline a pointer to the current tokeninzed line linked list
2060 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2063 static int do_directive(Token * tline)
2065 enum preproc_token i;
2066 int j;
2067 bool err;
2068 int nparam;
2069 bool nolist;
2070 bool casesense;
2071 int k, m;
2072 int offset;
2073 char *p, *pp;
2074 const char *mname;
2075 Include *inc;
2076 Context *ctx;
2077 Cond *cond;
2078 MMacro *mmac, **mmhead;
2079 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2080 Line *l;
2081 struct tokenval tokval;
2082 expr *evalresult;
2083 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2084 int64_t count;
2085 size_t len;
2086 int severity;
2088 origline = tline;
2090 skip_white_(tline);
2091 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2092 (tline->text[1] == '%' || tline->text[1] == '$'
2093 || tline->text[1] == '!'))
2094 return NO_DIRECTIVE_FOUND;
2096 i = pp_token_hash(tline->text);
2099 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2100 * since they are known to be buggy at moment, we need to fix them
2101 * in future release (2.09-2.10)
2103 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2104 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2105 tline->text);
2106 return NO_DIRECTIVE_FOUND;
2110 * If we're in a non-emitting branch of a condition construct,
2111 * or walking to the end of an already terminated %rep block,
2112 * we should ignore all directives except for condition
2113 * directives.
2115 if (((istk->conds && !emitting(istk->conds->state)) ||
2116 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2117 return NO_DIRECTIVE_FOUND;
2121 * If we're defining a macro or reading a %rep block, we should
2122 * ignore all directives except for %macro/%imacro (which nest),
2123 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2124 * If we're in a %rep block, another %rep nests, so should be let through.
2126 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2127 i != PP_RMACRO && i != PP_IRMACRO &&
2128 i != PP_ENDMACRO && i != PP_ENDM &&
2129 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2130 return NO_DIRECTIVE_FOUND;
2133 if (defining) {
2134 if (i == PP_MACRO || i == PP_IMACRO ||
2135 i == PP_RMACRO || i == PP_IRMACRO) {
2136 nested_mac_count++;
2137 return NO_DIRECTIVE_FOUND;
2138 } else if (nested_mac_count > 0) {
2139 if (i == PP_ENDMACRO) {
2140 nested_mac_count--;
2141 return NO_DIRECTIVE_FOUND;
2144 if (!defining->name) {
2145 if (i == PP_REP) {
2146 nested_rep_count++;
2147 return NO_DIRECTIVE_FOUND;
2148 } else if (nested_rep_count > 0) {
2149 if (i == PP_ENDREP) {
2150 nested_rep_count--;
2151 return NO_DIRECTIVE_FOUND;
2157 switch (i) {
2158 case PP_INVALID:
2159 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2160 tline->text);
2161 return NO_DIRECTIVE_FOUND; /* didn't get it */
2163 case PP_STACKSIZE:
2164 /* Directive to tell NASM what the default stack size is. The
2165 * default is for a 16-bit stack, and this can be overriden with
2166 * %stacksize large.
2168 tline = tline->next;
2169 if (tline && tline->type == TOK_WHITESPACE)
2170 tline = tline->next;
2171 if (!tline || tline->type != TOK_ID) {
2172 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2173 free_tlist(origline);
2174 return DIRECTIVE_FOUND;
2176 if (nasm_stricmp(tline->text, "flat") == 0) {
2177 /* All subsequent ARG directives are for a 32-bit stack */
2178 StackSize = 4;
2179 StackPointer = "ebp";
2180 ArgOffset = 8;
2181 LocalOffset = 0;
2182 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2183 /* All subsequent ARG directives are for a 64-bit stack */
2184 StackSize = 8;
2185 StackPointer = "rbp";
2186 ArgOffset = 16;
2187 LocalOffset = 0;
2188 } else if (nasm_stricmp(tline->text, "large") == 0) {
2189 /* All subsequent ARG directives are for a 16-bit stack,
2190 * far function call.
2192 StackSize = 2;
2193 StackPointer = "bp";
2194 ArgOffset = 4;
2195 LocalOffset = 0;
2196 } else if (nasm_stricmp(tline->text, "small") == 0) {
2197 /* All subsequent ARG directives are for a 16-bit stack,
2198 * far function call. We don't support near functions.
2200 StackSize = 2;
2201 StackPointer = "bp";
2202 ArgOffset = 6;
2203 LocalOffset = 0;
2204 } else {
2205 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
2212 case PP_ARG:
2213 /* TASM like ARG directive to define arguments to functions, in
2214 * the following form:
2216 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2218 offset = ArgOffset;
2219 do {
2220 char *arg, directive[256];
2221 int size = StackSize;
2223 /* Find the argument name */
2224 tline = tline->next;
2225 if (tline && tline->type == TOK_WHITESPACE)
2226 tline = tline->next;
2227 if (!tline || tline->type != TOK_ID) {
2228 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2229 free_tlist(origline);
2230 return DIRECTIVE_FOUND;
2232 arg = tline->text;
2234 /* Find the argument size type */
2235 tline = tline->next;
2236 if (!tline || tline->type != TOK_OTHER
2237 || tline->text[0] != ':') {
2238 error(ERR_NONFATAL,
2239 "Syntax error processing `%%arg' directive");
2240 free_tlist(origline);
2241 return DIRECTIVE_FOUND;
2243 tline = tline->next;
2244 if (!tline || tline->type != TOK_ID) {
2245 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2246 free_tlist(origline);
2247 return DIRECTIVE_FOUND;
2250 /* Allow macro expansion of type parameter */
2251 tt = tokenize(tline->text);
2252 tt = expand_smacro(tt);
2253 size = parse_size(tt->text);
2254 if (!size) {
2255 error(ERR_NONFATAL,
2256 "Invalid size type for `%%arg' missing directive");
2257 free_tlist(tt);
2258 free_tlist(origline);
2259 return DIRECTIVE_FOUND;
2261 free_tlist(tt);
2263 /* Round up to even stack slots */
2264 size = ALIGN(size, StackSize);
2266 /* Now define the macro for the argument */
2267 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2268 arg, StackPointer, offset);
2269 do_directive(tokenize(directive));
2270 offset += size;
2272 /* Move to the next argument in the list */
2273 tline = tline->next;
2274 if (tline && tline->type == TOK_WHITESPACE)
2275 tline = tline->next;
2276 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2277 ArgOffset = offset;
2278 free_tlist(origline);
2279 return DIRECTIVE_FOUND;
2281 case PP_LOCAL:
2282 /* TASM like LOCAL directive to define local variables for a
2283 * function, in the following form:
2285 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2287 * The '= LocalSize' at the end is ignored by NASM, but is
2288 * required by TASM to define the local parameter size (and used
2289 * by the TASM macro package).
2291 offset = LocalOffset;
2292 do {
2293 char *local, directive[256];
2294 int size = StackSize;
2296 /* Find the argument name */
2297 tline = tline->next;
2298 if (tline && tline->type == TOK_WHITESPACE)
2299 tline = tline->next;
2300 if (!tline || tline->type != TOK_ID) {
2301 error(ERR_NONFATAL,
2302 "`%%local' missing argument parameter");
2303 free_tlist(origline);
2304 return DIRECTIVE_FOUND;
2306 local = tline->text;
2308 /* Find the argument size type */
2309 tline = tline->next;
2310 if (!tline || tline->type != TOK_OTHER
2311 || tline->text[0] != ':') {
2312 error(ERR_NONFATAL,
2313 "Syntax error processing `%%local' directive");
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
2317 tline = tline->next;
2318 if (!tline || tline->type != TOK_ID) {
2319 error(ERR_NONFATAL,
2320 "`%%local' missing size type parameter");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2325 /* Allow macro expansion of type parameter */
2326 tt = tokenize(tline->text);
2327 tt = expand_smacro(tt);
2328 size = parse_size(tt->text);
2329 if (!size) {
2330 error(ERR_NONFATAL,
2331 "Invalid size type for `%%local' missing directive");
2332 free_tlist(tt);
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2336 free_tlist(tt);
2338 /* Round up to even stack slots */
2339 size = ALIGN(size, StackSize);
2341 offset += size; /* Negative offset, increment before */
2343 /* Now define the macro for the argument */
2344 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2345 local, StackPointer, offset);
2346 do_directive(tokenize(directive));
2348 /* Now define the assign to setup the enter_c macro correctly */
2349 snprintf(directive, sizeof(directive),
2350 "%%assign %%$localsize %%$localsize+%d", size);
2351 do_directive(tokenize(directive));
2353 /* Move to the next argument in the list */
2354 tline = tline->next;
2355 if (tline && tline->type == TOK_WHITESPACE)
2356 tline = tline->next;
2357 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2358 LocalOffset = offset;
2359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
2362 case PP_CLEAR:
2363 if (tline->next)
2364 error(ERR_WARNING|ERR_PASS1,
2365 "trailing garbage after `%%clear' ignored");
2366 free_macros();
2367 init_macros();
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2371 case PP_DEPEND:
2372 t = tline->next = expand_smacro(tline->next);
2373 skip_white_(t);
2374 if (!t || (t->type != TOK_STRING &&
2375 t->type != TOK_INTERNAL_STRING)) {
2376 error(ERR_NONFATAL, "`%%depend' expects a file name");
2377 free_tlist(origline);
2378 return DIRECTIVE_FOUND; /* but we did _something_ */
2380 if (t->next)
2381 error(ERR_WARNING|ERR_PASS1,
2382 "trailing garbage after `%%depend' ignored");
2383 p = t->text;
2384 if (t->type != TOK_INTERNAL_STRING)
2385 nasm_unquote_cstr(p, i);
2386 if (dephead && !in_list(*dephead, p)) {
2387 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2388 sl->next = NULL;
2389 strcpy(sl->str, p);
2390 *deptail = sl;
2391 deptail = &sl->next;
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2396 case PP_INCLUDE:
2397 t = tline->next = expand_smacro(tline->next);
2398 skip_white_(t);
2400 if (!t || (t->type != TOK_STRING &&
2401 t->type != TOK_INTERNAL_STRING)) {
2402 error(ERR_NONFATAL, "`%%include' expects a file name");
2403 free_tlist(origline);
2404 return DIRECTIVE_FOUND; /* but we did _something_ */
2406 if (t->next)
2407 error(ERR_WARNING|ERR_PASS1,
2408 "trailing garbage after `%%include' ignored");
2409 p = t->text;
2410 if (t->type != TOK_INTERNAL_STRING)
2411 nasm_unquote_cstr(p, i);
2412 inc = nasm_malloc(sizeof(Include));
2413 inc->next = istk;
2414 inc->conds = NULL;
2415 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2416 if (!inc->fp) {
2417 /* -MG given but file not found */
2418 nasm_free(inc);
2419 } else {
2420 inc->fname = src_set_fname(nasm_strdup(p));
2421 inc->lineno = src_set_linnum(0);
2422 inc->lineinc = 1;
2423 inc->expansion = NULL;
2424 inc->mstk = NULL;
2425 istk = inc;
2426 list->uplevel(LIST_INCLUDE);
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2431 case PP_USE:
2433 static macros_t *use_pkg;
2434 const char *pkg_macro = NULL;
2436 tline = tline->next;
2437 skip_white_(tline);
2438 tline = expand_id(tline);
2440 if (!tline || (tline->type != TOK_STRING &&
2441 tline->type != TOK_INTERNAL_STRING &&
2442 tline->type != TOK_ID)) {
2443 error(ERR_NONFATAL, "`%%use' expects a package name");
2444 free_tlist(origline);
2445 return DIRECTIVE_FOUND; /* but we did _something_ */
2447 if (tline->next)
2448 error(ERR_WARNING|ERR_PASS1,
2449 "trailing garbage after `%%use' ignored");
2450 if (tline->type == TOK_STRING)
2451 nasm_unquote_cstr(tline->text, i);
2452 use_pkg = nasm_stdmac_find_package(tline->text);
2453 if (!use_pkg)
2454 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2455 else
2456 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2457 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2458 /* Not already included, go ahead and include it */
2459 stdmacpos = use_pkg;
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2464 case PP_PUSH:
2465 case PP_REPL:
2466 case PP_POP:
2467 tline = tline->next;
2468 skip_white_(tline);
2469 tline = expand_id(tline);
2470 if (tline) {
2471 if (!tok_type_(tline, TOK_ID)) {
2472 error(ERR_NONFATAL, "`%s' expects a context identifier",
2473 pp_directives[i]);
2474 free_tlist(origline);
2475 return DIRECTIVE_FOUND; /* but we did _something_ */
2477 if (tline->next)
2478 error(ERR_WARNING|ERR_PASS1,
2479 "trailing garbage after `%s' ignored",
2480 pp_directives[i]);
2481 p = nasm_strdup(tline->text);
2482 } else {
2483 p = NULL; /* Anonymous */
2486 if (i == PP_PUSH) {
2487 ctx = nasm_malloc(sizeof(Context));
2488 ctx->next = cstk;
2489 hash_init(&ctx->localmac, HASH_SMALL);
2490 ctx->name = p;
2491 ctx->number = unique++;
2492 cstk = ctx;
2493 } else {
2494 /* %pop or %repl */
2495 if (!cstk) {
2496 error(ERR_NONFATAL, "`%s': context stack is empty",
2497 pp_directives[i]);
2498 } else if (i == PP_POP) {
2499 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2500 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2501 "expected %s",
2502 cstk->name ? cstk->name : "anonymous", p);
2503 else
2504 ctx_pop();
2505 } else {
2506 /* i == PP_REPL */
2507 nasm_free(cstk->name);
2508 cstk->name = p;
2509 p = NULL;
2511 nasm_free(p);
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
2515 case PP_FATAL:
2516 severity = ERR_FATAL;
2517 goto issue_error;
2518 case PP_ERROR:
2519 severity = ERR_NONFATAL;
2520 goto issue_error;
2521 case PP_WARNING:
2522 severity = ERR_WARNING|ERR_WARN_USER;
2523 goto issue_error;
2525 issue_error:
2527 /* Only error out if this is the final pass */
2528 if (pass != 2 && i != PP_FATAL)
2529 return DIRECTIVE_FOUND;
2531 tline->next = expand_smacro(tline->next);
2532 tline = tline->next;
2533 skip_white_(tline);
2534 t = tline ? tline->next : NULL;
2535 skip_white_(t);
2536 if (tok_type_(tline, TOK_STRING) && !t) {
2537 /* The line contains only a quoted string */
2538 p = tline->text;
2539 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2540 error(severity, "%s", p);
2541 } else {
2542 /* Not a quoted string, or more than a quoted string */
2543 p = detoken(tline, false);
2544 error(severity, "%s", p);
2545 nasm_free(p);
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
2551 CASE_PP_IF:
2552 if (istk->conds && !emitting(istk->conds->state))
2553 j = COND_NEVER;
2554 else {
2555 j = if_condition(tline->next, i);
2556 tline->next = NULL; /* it got freed */
2557 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2559 cond = nasm_malloc(sizeof(Cond));
2560 cond->next = istk->conds;
2561 cond->state = j;
2562 istk->conds = cond;
2563 if(istk->mstk)
2564 istk->mstk->condcnt ++;
2565 free_tlist(origline);
2566 return DIRECTIVE_FOUND;
2568 CASE_PP_ELIF:
2569 if (!istk->conds)
2570 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2571 switch(istk->conds->state) {
2572 case COND_IF_TRUE:
2573 istk->conds->state = COND_DONE;
2574 break;
2576 case COND_DONE:
2577 case COND_NEVER:
2578 break;
2580 case COND_ELSE_TRUE:
2581 case COND_ELSE_FALSE:
2582 error_precond(ERR_WARNING|ERR_PASS1,
2583 "`%%elif' after `%%else' ignored");
2584 istk->conds->state = COND_NEVER;
2585 break;
2587 case COND_IF_FALSE:
2589 * IMPORTANT: In the case of %if, we will already have
2590 * called expand_mmac_params(); however, if we're
2591 * processing an %elif we must have been in a
2592 * non-emitting mode, which would have inhibited
2593 * the normal invocation of expand_mmac_params().
2594 * Therefore, we have to do it explicitly here.
2596 j = if_condition(expand_mmac_params(tline->next), i);
2597 tline->next = NULL; /* it got freed */
2598 istk->conds->state =
2599 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2600 break;
2602 free_tlist(origline);
2603 return DIRECTIVE_FOUND;
2605 case PP_ELSE:
2606 if (tline->next)
2607 error_precond(ERR_WARNING|ERR_PASS1,
2608 "trailing garbage after `%%else' ignored");
2609 if (!istk->conds)
2610 error(ERR_FATAL, "`%%else': no matching `%%if'");
2611 switch(istk->conds->state) {
2612 case COND_IF_TRUE:
2613 case COND_DONE:
2614 istk->conds->state = COND_ELSE_FALSE;
2615 break;
2617 case COND_NEVER:
2618 break;
2620 case COND_IF_FALSE:
2621 istk->conds->state = COND_ELSE_TRUE;
2622 break;
2624 case COND_ELSE_TRUE:
2625 case COND_ELSE_FALSE:
2626 error_precond(ERR_WARNING|ERR_PASS1,
2627 "`%%else' after `%%else' ignored.");
2628 istk->conds->state = COND_NEVER;
2629 break;
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
2634 case PP_ENDIF:
2635 if (tline->next)
2636 error_precond(ERR_WARNING|ERR_PASS1,
2637 "trailing garbage after `%%endif' ignored");
2638 if (!istk->conds)
2639 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2640 cond = istk->conds;
2641 istk->conds = cond->next;
2642 nasm_free(cond);
2643 if(istk->mstk)
2644 istk->mstk->condcnt --;
2645 free_tlist(origline);
2646 return DIRECTIVE_FOUND;
2648 case PP_RMACRO:
2649 case PP_IRMACRO:
2650 case PP_MACRO:
2651 case PP_IMACRO:
2652 if (defining) {
2653 error(ERR_FATAL, "`%s': already defining a macro",
2654 pp_directives[i]);
2655 return DIRECTIVE_FOUND;
2657 defining = nasm_malloc(sizeof(MMacro));
2658 defining->max_depth =
2659 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2660 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2661 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2662 nasm_free(defining);
2663 defining = NULL;
2664 return DIRECTIVE_FOUND;
2667 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2668 while (mmac) {
2669 if (!strcmp(mmac->name, defining->name) &&
2670 (mmac->nparam_min <= defining->nparam_max
2671 || defining->plus)
2672 && (defining->nparam_min <= mmac->nparam_max
2673 || mmac->plus)) {
2674 error(ERR_WARNING|ERR_PASS1,
2675 "redefining multi-line macro `%s'", defining->name);
2676 return DIRECTIVE_FOUND;
2678 mmac = mmac->next;
2680 free_tlist(origline);
2681 return DIRECTIVE_FOUND;
2683 case PP_ENDM:
2684 case PP_ENDMACRO:
2685 if (! (defining && defining->name)) {
2686 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2687 return DIRECTIVE_FOUND;
2689 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2690 defining->next = *mmhead;
2691 *mmhead = defining;
2692 defining = NULL;
2693 free_tlist(origline);
2694 return DIRECTIVE_FOUND;
2696 case PP_EXITMACRO:
2698 * We must search along istk->expansion until we hit a
2699 * macro-end marker for a macro with a name. Then we
2700 * bypass all lines between exitmacro and endmacro.
2702 list_for_each(l, istk->expansion)
2703 if (l->finishes && l->finishes->name)
2704 break;
2706 if (l) {
2708 * Remove all conditional entries relative to this
2709 * macro invocation. (safe to do in this context)
2711 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2712 cond = istk->conds;
2713 istk->conds = cond->next;
2714 nasm_free(cond);
2716 istk->expansion = l;
2717 } else {
2718 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
2723 case PP_UNMACRO:
2724 case PP_UNIMACRO:
2726 MMacro **mmac_p;
2727 MMacro spec;
2729 spec.casesense = (i == PP_UNMACRO);
2730 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2731 return DIRECTIVE_FOUND;
2733 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2734 while (mmac_p && *mmac_p) {
2735 mmac = *mmac_p;
2736 if (mmac->casesense == spec.casesense &&
2737 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2738 mmac->nparam_min == spec.nparam_min &&
2739 mmac->nparam_max == spec.nparam_max &&
2740 mmac->plus == spec.plus) {
2741 *mmac_p = mmac->next;
2742 free_mmacro(mmac);
2743 } else {
2744 mmac_p = &mmac->next;
2747 free_tlist(origline);
2748 free_tlist(spec.dlist);
2749 return DIRECTIVE_FOUND;
2752 case PP_ROTATE:
2753 if (tline->next && tline->next->type == TOK_WHITESPACE)
2754 tline = tline->next;
2755 if (!tline->next) {
2756 free_tlist(origline);
2757 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2758 return DIRECTIVE_FOUND;
2760 t = expand_smacro(tline->next);
2761 tline->next = NULL;
2762 free_tlist(origline);
2763 tline = t;
2764 tptr = &t;
2765 tokval.t_type = TOKEN_INVALID;
2766 evalresult =
2767 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2768 free_tlist(tline);
2769 if (!evalresult)
2770 return DIRECTIVE_FOUND;
2771 if (tokval.t_type)
2772 error(ERR_WARNING|ERR_PASS1,
2773 "trailing garbage after expression ignored");
2774 if (!is_simple(evalresult)) {
2775 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2776 return DIRECTIVE_FOUND;
2778 mmac = istk->mstk;
2779 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2780 mmac = mmac->next_active;
2781 if (!mmac) {
2782 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2783 } else if (mmac->nparam == 0) {
2784 error(ERR_NONFATAL,
2785 "`%%rotate' invoked within macro without parameters");
2786 } else {
2787 int rotate = mmac->rotate + reloc_value(evalresult);
2789 rotate %= (int)mmac->nparam;
2790 if (rotate < 0)
2791 rotate += mmac->nparam;
2793 mmac->rotate = rotate;
2795 return DIRECTIVE_FOUND;
2797 case PP_REP:
2798 nolist = false;
2799 do {
2800 tline = tline->next;
2801 } while (tok_type_(tline, TOK_WHITESPACE));
2803 if (tok_type_(tline, TOK_ID) &&
2804 nasm_stricmp(tline->text, ".nolist") == 0) {
2805 nolist = true;
2806 do {
2807 tline = tline->next;
2808 } while (tok_type_(tline, TOK_WHITESPACE));
2811 if (tline) {
2812 t = expand_smacro(tline);
2813 tptr = &t;
2814 tokval.t_type = TOKEN_INVALID;
2815 evalresult =
2816 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2817 if (!evalresult) {
2818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
2821 if (tokval.t_type)
2822 error(ERR_WARNING|ERR_PASS1,
2823 "trailing garbage after expression ignored");
2824 if (!is_simple(evalresult)) {
2825 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2826 return DIRECTIVE_FOUND;
2828 count = reloc_value(evalresult) + 1;
2829 } else {
2830 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2831 count = 0;
2833 free_tlist(origline);
2835 tmp_defining = defining;
2836 defining = nasm_malloc(sizeof(MMacro));
2837 defining->prev = NULL;
2838 defining->name = NULL; /* flags this macro as a %rep block */
2839 defining->casesense = false;
2840 defining->plus = false;
2841 defining->nolist = nolist;
2842 defining->in_progress = count;
2843 defining->max_depth = 0;
2844 defining->nparam_min = defining->nparam_max = 0;
2845 defining->defaults = NULL;
2846 defining->dlist = NULL;
2847 defining->expansion = NULL;
2848 defining->next_active = istk->mstk;
2849 defining->rep_nest = tmp_defining;
2850 return DIRECTIVE_FOUND;
2852 case PP_ENDREP:
2853 if (!defining || defining->name) {
2854 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2855 return DIRECTIVE_FOUND;
2859 * Now we have a "macro" defined - although it has no name
2860 * and we won't be entering it in the hash tables - we must
2861 * push a macro-end marker for it on to istk->expansion.
2862 * After that, it will take care of propagating itself (a
2863 * macro-end marker line for a macro which is really a %rep
2864 * block will cause the macro to be re-expanded, complete
2865 * with another macro-end marker to ensure the process
2866 * continues) until the whole expansion is forcibly removed
2867 * from istk->expansion by a %exitrep.
2869 l = nasm_malloc(sizeof(Line));
2870 l->next = istk->expansion;
2871 l->finishes = defining;
2872 l->first = NULL;
2873 istk->expansion = l;
2875 istk->mstk = defining;
2877 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2878 tmp_defining = defining;
2879 defining = defining->rep_nest;
2880 free_tlist(origline);
2881 return DIRECTIVE_FOUND;
2883 case PP_EXITREP:
2885 * We must search along istk->expansion until we hit a
2886 * macro-end marker for a macro with no name. Then we set
2887 * its `in_progress' flag to 0.
2889 list_for_each(l, istk->expansion)
2890 if (l->finishes && !l->finishes->name)
2891 break;
2893 if (l)
2894 l->finishes->in_progress = 1;
2895 else
2896 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2897 free_tlist(origline);
2898 return DIRECTIVE_FOUND;
2900 case PP_XDEFINE:
2901 case PP_IXDEFINE:
2902 case PP_DEFINE:
2903 case PP_IDEFINE:
2904 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2906 tline = tline->next;
2907 skip_white_(tline);
2908 tline = expand_id(tline);
2909 if (!tline || (tline->type != TOK_ID &&
2910 (tline->type != TOK_PREPROC_ID ||
2911 tline->text[1] != '$'))) {
2912 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2913 pp_directives[i]);
2914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2918 ctx = get_ctx(tline->text, &mname, false);
2919 last = tline;
2920 param_start = tline = tline->next;
2921 nparam = 0;
2923 /* Expand the macro definition now for %xdefine and %ixdefine */
2924 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2925 tline = expand_smacro(tline);
2927 if (tok_is_(tline, "(")) {
2929 * This macro has parameters.
2932 tline = tline->next;
2933 while (1) {
2934 skip_white_(tline);
2935 if (!tline) {
2936 error(ERR_NONFATAL, "parameter identifier expected");
2937 free_tlist(origline);
2938 return DIRECTIVE_FOUND;
2940 if (tline->type != TOK_ID) {
2941 error(ERR_NONFATAL,
2942 "`%s': parameter identifier expected",
2943 tline->text);
2944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
2947 tline->type = TOK_SMAC_PARAM + nparam++;
2948 tline = tline->next;
2949 skip_white_(tline);
2950 if (tok_is_(tline, ",")) {
2951 tline = tline->next;
2952 } else {
2953 if (!tok_is_(tline, ")")) {
2954 error(ERR_NONFATAL,
2955 "`)' expected to terminate macro template");
2956 free_tlist(origline);
2957 return DIRECTIVE_FOUND;
2959 break;
2962 last = tline;
2963 tline = tline->next;
2965 if (tok_type_(tline, TOK_WHITESPACE))
2966 last = tline, tline = tline->next;
2967 macro_start = NULL;
2968 last->next = NULL;
2969 t = tline;
2970 while (t) {
2971 if (t->type == TOK_ID) {
2972 list_for_each(tt, param_start)
2973 if (tt->type >= TOK_SMAC_PARAM &&
2974 !strcmp(tt->text, t->text))
2975 t->type = tt->type;
2977 tt = t->next;
2978 t->next = macro_start;
2979 macro_start = t;
2980 t = tt;
2983 * Good. We now have a macro name, a parameter count, and a
2984 * token list (in reverse order) for an expansion. We ought
2985 * to be OK just to create an SMacro, store it, and let
2986 * free_tlist have the rest of the line (which we have
2987 * carefully re-terminated after chopping off the expansion
2988 * from the end).
2990 define_smacro(ctx, mname, casesense, nparam, macro_start);
2991 free_tlist(origline);
2992 return DIRECTIVE_FOUND;
2994 case PP_UNDEF:
2995 tline = tline->next;
2996 skip_white_(tline);
2997 tline = expand_id(tline);
2998 if (!tline || (tline->type != TOK_ID &&
2999 (tline->type != TOK_PREPROC_ID ||
3000 tline->text[1] != '$'))) {
3001 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
3005 if (tline->next) {
3006 error(ERR_WARNING|ERR_PASS1,
3007 "trailing garbage after macro name ignored");
3010 /* Find the context that symbol belongs to */
3011 ctx = get_ctx(tline->text, &mname, false);
3012 undef_smacro(ctx, mname);
3013 free_tlist(origline);
3014 return DIRECTIVE_FOUND;
3016 case PP_DEFSTR:
3017 case PP_IDEFSTR:
3018 casesense = (i == PP_DEFSTR);
3020 tline = tline->next;
3021 skip_white_(tline);
3022 tline = expand_id(tline);
3023 if (!tline || (tline->type != TOK_ID &&
3024 (tline->type != TOK_PREPROC_ID ||
3025 tline->text[1] != '$'))) {
3026 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3027 pp_directives[i]);
3028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
3032 ctx = get_ctx(tline->text, &mname, false);
3033 last = tline;
3034 tline = expand_smacro(tline->next);
3035 last->next = NULL;
3037 while (tok_type_(tline, TOK_WHITESPACE))
3038 tline = delete_Token(tline);
3040 p = detoken(tline, false);
3041 macro_start = nasm_malloc(sizeof(*macro_start));
3042 macro_start->next = NULL;
3043 macro_start->text = nasm_quote(p, strlen(p));
3044 macro_start->type = TOK_STRING;
3045 macro_start->a.mac = NULL;
3046 nasm_free(p);
3049 * We now have a macro name, an implicit parameter count of
3050 * zero, and a string token to use as an expansion. Create
3051 * and store an SMacro.
3053 define_smacro(ctx, mname, casesense, 0, macro_start);
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3057 case PP_DEFTOK:
3058 case PP_IDEFTOK:
3059 casesense = (i == PP_DEFTOK);
3061 tline = tline->next;
3062 skip_white_(tline);
3063 tline = expand_id(tline);
3064 if (!tline || (tline->type != TOK_ID &&
3065 (tline->type != TOK_PREPROC_ID ||
3066 tline->text[1] != '$'))) {
3067 error(ERR_NONFATAL,
3068 "`%s' expects a macro identifier as first parameter",
3069 pp_directives[i]);
3070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3073 ctx = get_ctx(tline->text, &mname, false);
3074 last = tline;
3075 tline = expand_smacro(tline->next);
3076 last->next = NULL;
3078 t = tline;
3079 while (tok_type_(t, TOK_WHITESPACE))
3080 t = t->next;
3081 /* t should now point to the string */
3082 if (t->type != TOK_STRING) {
3083 error(ERR_NONFATAL,
3084 "`%s` requires string as second parameter",
3085 pp_directives[i]);
3086 free_tlist(tline);
3087 free_tlist(origline);
3088 return DIRECTIVE_FOUND;
3091 nasm_unquote_cstr(t->text, i);
3092 macro_start = tokenize(t->text);
3095 * We now have a macro name, an implicit parameter count of
3096 * zero, and a numeric token to use as an expansion. Create
3097 * and store an SMacro.
3099 define_smacro(ctx, mname, casesense, 0, macro_start);
3100 free_tlist(tline);
3101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
3104 case PP_PATHSEARCH:
3106 FILE *fp;
3107 StrList *xsl = NULL;
3108 StrList **xst = &xsl;
3110 casesense = true;
3112 tline = tline->next;
3113 skip_white_(tline);
3114 tline = expand_id(tline);
3115 if (!tline || (tline->type != TOK_ID &&
3116 (tline->type != TOK_PREPROC_ID ||
3117 tline->text[1] != '$'))) {
3118 error(ERR_NONFATAL,
3119 "`%%pathsearch' expects a macro identifier as first parameter");
3120 free_tlist(origline);
3121 return DIRECTIVE_FOUND;
3123 ctx = get_ctx(tline->text, &mname, false);
3124 last = tline;
3125 tline = expand_smacro(tline->next);
3126 last->next = NULL;
3128 t = tline;
3129 while (tok_type_(t, TOK_WHITESPACE))
3130 t = t->next;
3132 if (!t || (t->type != TOK_STRING &&
3133 t->type != TOK_INTERNAL_STRING)) {
3134 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3135 free_tlist(tline);
3136 free_tlist(origline);
3137 return DIRECTIVE_FOUND; /* but we did _something_ */
3139 if (t->next)
3140 error(ERR_WARNING|ERR_PASS1,
3141 "trailing garbage after `%%pathsearch' ignored");
3142 p = t->text;
3143 if (t->type != TOK_INTERNAL_STRING)
3144 nasm_unquote(p, NULL);
3146 fp = inc_fopen(p, &xsl, &xst, true);
3147 if (fp) {
3148 p = xsl->str;
3149 fclose(fp); /* Don't actually care about the file */
3151 macro_start = nasm_malloc(sizeof(*macro_start));
3152 macro_start->next = NULL;
3153 macro_start->text = nasm_quote(p, strlen(p));
3154 macro_start->type = TOK_STRING;
3155 macro_start->a.mac = NULL;
3156 if (xsl)
3157 nasm_free(xsl);
3160 * We now have a macro name, an implicit parameter count of
3161 * zero, and a string token to use as an expansion. Create
3162 * and store an SMacro.
3164 define_smacro(ctx, mname, casesense, 0, macro_start);
3165 free_tlist(tline);
3166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
3170 case PP_STRLEN:
3171 casesense = true;
3173 tline = tline->next;
3174 skip_white_(tline);
3175 tline = expand_id(tline);
3176 if (!tline || (tline->type != TOK_ID &&
3177 (tline->type != TOK_PREPROC_ID ||
3178 tline->text[1] != '$'))) {
3179 error(ERR_NONFATAL,
3180 "`%%strlen' expects a macro identifier as first parameter");
3181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3184 ctx = get_ctx(tline->text, &mname, false);
3185 last = tline;
3186 tline = expand_smacro(tline->next);
3187 last->next = NULL;
3189 t = tline;
3190 while (tok_type_(t, TOK_WHITESPACE))
3191 t = t->next;
3192 /* t should now point to the string */
3193 if (t->type != TOK_STRING) {
3194 error(ERR_NONFATAL,
3195 "`%%strlen` requires string as second parameter");
3196 free_tlist(tline);
3197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3201 macro_start = nasm_malloc(sizeof(*macro_start));
3202 macro_start->next = NULL;
3203 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3204 macro_start->a.mac = NULL;
3207 * We now have a macro name, an implicit parameter count of
3208 * zero, and a numeric token to use as an expansion. Create
3209 * and store an SMacro.
3211 define_smacro(ctx, mname, casesense, 0, macro_start);
3212 free_tlist(tline);
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3216 case PP_STRCAT:
3217 casesense = true;
3219 tline = tline->next;
3220 skip_white_(tline);
3221 tline = expand_id(tline);
3222 if (!tline || (tline->type != TOK_ID &&
3223 (tline->type != TOK_PREPROC_ID ||
3224 tline->text[1] != '$'))) {
3225 error(ERR_NONFATAL,
3226 "`%%strcat' expects a macro identifier as first parameter");
3227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
3230 ctx = get_ctx(tline->text, &mname, false);
3231 last = tline;
3232 tline = expand_smacro(tline->next);
3233 last->next = NULL;
3235 len = 0;
3236 list_for_each(t, tline) {
3237 switch (t->type) {
3238 case TOK_WHITESPACE:
3239 break;
3240 case TOK_STRING:
3241 len += t->a.len = nasm_unquote(t->text, NULL);
3242 break;
3243 case TOK_OTHER:
3244 if (!strcmp(t->text, ",")) /* permit comma separators */
3245 break;
3246 /* else fall through */
3247 default:
3248 error(ERR_NONFATAL,
3249 "non-string passed to `%%strcat' (%d)", t->type);
3250 free_tlist(tline);
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3256 p = pp = nasm_malloc(len);
3257 list_for_each(t, tline) {
3258 if (t->type == TOK_STRING) {
3259 memcpy(p, t->text, t->a.len);
3260 p += t->a.len;
3265 * We now have a macro name, an implicit parameter count of
3266 * zero, and a numeric token to use as an expansion. Create
3267 * and store an SMacro.
3269 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3270 macro_start->text = nasm_quote(pp, len);
3271 nasm_free(pp);
3272 define_smacro(ctx, mname, casesense, 0, macro_start);
3273 free_tlist(tline);
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3277 case PP_SUBSTR:
3279 int64_t a1, a2;
3280 size_t len;
3282 casesense = true;
3284 tline = tline->next;
3285 skip_white_(tline);
3286 tline = expand_id(tline);
3287 if (!tline || (tline->type != TOK_ID &&
3288 (tline->type != TOK_PREPROC_ID ||
3289 tline->text[1] != '$'))) {
3290 error(ERR_NONFATAL,
3291 "`%%substr' expects a macro identifier as first parameter");
3292 free_tlist(origline);
3293 return DIRECTIVE_FOUND;
3295 ctx = get_ctx(tline->text, &mname, false);
3296 last = tline;
3297 tline = expand_smacro(tline->next);
3298 last->next = NULL;
3300 t = tline->next;
3301 while (tok_type_(t, TOK_WHITESPACE))
3302 t = t->next;
3304 /* t should now point to the string */
3305 if (t->type != TOK_STRING) {
3306 error(ERR_NONFATAL,
3307 "`%%substr` requires string as second parameter");
3308 free_tlist(tline);
3309 free_tlist(origline);
3310 return DIRECTIVE_FOUND;
3313 tt = t->next;
3314 tptr = &tt;
3315 tokval.t_type = TOKEN_INVALID;
3316 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3317 pass, error, NULL);
3318 if (!evalresult) {
3319 free_tlist(tline);
3320 free_tlist(origline);
3321 return DIRECTIVE_FOUND;
3322 } else if (!is_simple(evalresult)) {
3323 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3324 free_tlist(tline);
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3328 a1 = evalresult->value-1;
3330 while (tok_type_(tt, TOK_WHITESPACE))
3331 tt = tt->next;
3332 if (!tt) {
3333 a2 = 1; /* Backwards compatibility: one character */
3334 } else {
3335 tokval.t_type = TOKEN_INVALID;
3336 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3337 pass, error, NULL);
3338 if (!evalresult) {
3339 free_tlist(tline);
3340 free_tlist(origline);
3341 return DIRECTIVE_FOUND;
3342 } else if (!is_simple(evalresult)) {
3343 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3344 free_tlist(tline);
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3348 a2 = evalresult->value;
3351 len = nasm_unquote(t->text, NULL);
3352 if (a2 < 0)
3353 a2 = a2+1+len-a1;
3354 if (a1+a2 > (int64_t)len)
3355 a2 = len-a1;
3357 macro_start = nasm_malloc(sizeof(*macro_start));
3358 macro_start->next = NULL;
3359 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3360 macro_start->type = TOK_STRING;
3361 macro_start->a.mac = NULL;
3364 * We now have a macro name, an implicit parameter count of
3365 * zero, and a numeric token to use as an expansion. Create
3366 * and store an SMacro.
3368 define_smacro(ctx, mname, casesense, 0, macro_start);
3369 free_tlist(tline);
3370 free_tlist(origline);
3371 return DIRECTIVE_FOUND;
3374 case PP_ASSIGN:
3375 case PP_IASSIGN:
3376 casesense = (i == PP_ASSIGN);
3378 tline = tline->next;
3379 skip_white_(tline);
3380 tline = expand_id(tline);
3381 if (!tline || (tline->type != TOK_ID &&
3382 (tline->type != TOK_PREPROC_ID ||
3383 tline->text[1] != '$'))) {
3384 error(ERR_NONFATAL,
3385 "`%%%sassign' expects a macro identifier",
3386 (i == PP_IASSIGN ? "i" : ""));
3387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
3390 ctx = get_ctx(tline->text, &mname, false);
3391 last = tline;
3392 tline = expand_smacro(tline->next);
3393 last->next = NULL;
3395 t = tline;
3396 tptr = &t;
3397 tokval.t_type = TOKEN_INVALID;
3398 evalresult =
3399 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3400 free_tlist(tline);
3401 if (!evalresult) {
3402 free_tlist(origline);
3403 return DIRECTIVE_FOUND;
3406 if (tokval.t_type)
3407 error(ERR_WARNING|ERR_PASS1,
3408 "trailing garbage after expression ignored");
3410 if (!is_simple(evalresult)) {
3411 error(ERR_NONFATAL,
3412 "non-constant value given to `%%%sassign'",
3413 (i == PP_IASSIGN ? "i" : ""));
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3418 macro_start = nasm_malloc(sizeof(*macro_start));
3419 macro_start->next = NULL;
3420 make_tok_num(macro_start, reloc_value(evalresult));
3421 macro_start->a.mac = NULL;
3424 * We now have a macro name, an implicit parameter count of
3425 * zero, and a numeric token to use as an expansion. Create
3426 * and store an SMacro.
3428 define_smacro(ctx, mname, casesense, 0, macro_start);
3429 free_tlist(origline);
3430 return DIRECTIVE_FOUND;
3432 case PP_LINE:
3434 * Syntax is `%line nnn[+mmm] [filename]'
3436 tline = tline->next;
3437 skip_white_(tline);
3438 if (!tok_type_(tline, TOK_NUMBER)) {
3439 error(ERR_NONFATAL, "`%%line' expects line number");
3440 free_tlist(origline);
3441 return DIRECTIVE_FOUND;
3443 k = readnum(tline->text, &err);
3444 m = 1;
3445 tline = tline->next;
3446 if (tok_is_(tline, "+")) {
3447 tline = tline->next;
3448 if (!tok_type_(tline, TOK_NUMBER)) {
3449 error(ERR_NONFATAL, "`%%line' expects line increment");
3450 free_tlist(origline);
3451 return DIRECTIVE_FOUND;
3453 m = readnum(tline->text, &err);
3454 tline = tline->next;
3456 skip_white_(tline);
3457 src_set_linnum(k);
3458 istk->lineinc = m;
3459 if (tline) {
3460 nasm_free(src_set_fname(detoken(tline, false)));
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3465 default:
3466 error(ERR_FATAL,
3467 "preprocessor directive `%s' not yet implemented",
3468 pp_directives[i]);
3469 return DIRECTIVE_FOUND;
3474 * Ensure that a macro parameter contains a condition code and
3475 * nothing else. Return the condition code index if so, or -1
3476 * otherwise.
3478 static int find_cc(Token * t)
3480 Token *tt;
3481 int i, j, k, m;
3483 if (!t)
3484 return -1; /* Probably a %+ without a space */
3486 skip_white_(t);
3487 if (t->type != TOK_ID)
3488 return -1;
3489 tt = t->next;
3490 skip_white_(tt);
3491 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3492 return -1;
3494 i = -1;
3495 j = ARRAY_SIZE(conditions);
3496 while (j - i > 1) {
3497 k = (j + i) / 2;
3498 m = nasm_stricmp(t->text, conditions[k]);
3499 if (m == 0) {
3500 i = k;
3501 j = -2;
3502 break;
3503 } else if (m < 0) {
3504 j = k;
3505 } else
3506 i = k;
3508 if (j != -2)
3509 return -1;
3510 return i;
3513 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3515 Token **tail, *t, *tt;
3516 Token **paste_head;
3517 bool did_paste = false;
3518 char *tmp;
3520 /* Now handle token pasting... */
3521 paste_head = NULL;
3522 tail = head;
3523 while ((t = *tail) && (tt = t->next)) {
3524 switch (t->type) {
3525 case TOK_WHITESPACE:
3526 if (tt->type == TOK_WHITESPACE) {
3527 /* Zap adjacent whitespace tokens */
3528 t->next = delete_Token(tt);
3529 } else {
3530 /* Do not advance paste_head here */
3531 tail = &t->next;
3533 break;
3534 case TOK_ID:
3535 case TOK_NUMBER:
3536 case TOK_FLOAT:
3538 size_t len = 0;
3539 char *tmp, *p;
3541 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3542 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3543 tt->type == TOK_OTHER)) {
3544 len += strlen(tt->text);
3545 tt = tt->next;
3549 * Now tt points to the first token after
3550 * the potential paste area...
3552 if (tt != t->next) {
3553 /* We have at least two tokens... */
3554 len += strlen(t->text);
3555 p = tmp = nasm_malloc(len+1);
3557 while (t != tt) {
3558 strcpy(p, t->text);
3559 p = strchr(p, '\0');
3560 t = delete_Token(t);
3563 t = *tail = tokenize(tmp);
3564 nasm_free(tmp);
3566 while (t->next) {
3567 tail = &t->next;
3568 t = t->next;
3570 t->next = tt; /* Attach the remaining token chain */
3572 did_paste = true;
3574 paste_head = tail;
3575 tail = &t->next;
3576 break;
3578 case TOK_PASTE: /* %+ */
3579 if (handle_paste_tokens) {
3580 /* Zap %+ and whitespace tokens to the right */
3581 while (t && (t->type == TOK_WHITESPACE ||
3582 t->type == TOK_PASTE))
3583 t = *tail = delete_Token(t);
3584 if (!paste_head || !t)
3585 break; /* Nothing to paste with */
3586 tail = paste_head;
3587 t = *tail;
3588 tt = t->next;
3589 while (tok_type_(tt, TOK_WHITESPACE))
3590 tt = t->next = delete_Token(tt);
3592 if (tt) {
3593 tmp = nasm_strcat(t->text, tt->text);
3594 delete_Token(t);
3595 tt = delete_Token(tt);
3596 t = *tail = tokenize(tmp);
3597 nasm_free(tmp);
3598 while (t->next) {
3599 tail = &t->next;
3600 t = t->next;
3602 t->next = tt; /* Attach the remaining token chain */
3603 did_paste = true;
3605 paste_head = tail;
3606 tail = &t->next;
3607 break;
3609 /* else fall through */
3610 default:
3611 tail = &t->next;
3612 if (!tok_type_(t->next, TOK_WHITESPACE))
3613 paste_head = tail;
3614 break;
3617 return did_paste;
3621 * expands to a list of tokens from %{x:y}
3623 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3625 Token *t = tline, **tt, *tm, *head;
3626 char *pos;
3627 int fst, lst, j, i;
3629 pos = strchr(tline->text, ':');
3630 nasm_assert(pos);
3632 lst = atoi(pos + 1);
3633 fst = atoi(tline->text + 1);
3636 * only macros params are accounted so
3637 * if someone passes %0 -- we reject such
3638 * value(s)
3640 if (lst == 0 || fst == 0)
3641 goto err;
3643 /* the values should be sane */
3644 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3645 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3646 goto err;
3648 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3649 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3651 /* counted from zero */
3652 fst--, lst--;
3655 * it will be at least one token
3657 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3658 t = new_Token(NULL, tm->type, tm->text, 0);
3659 head = t, tt = &t->next;
3660 if (fst < lst) {
3661 for (i = fst + 1; i <= lst; i++) {
3662 t = new_Token(NULL, TOK_OTHER, ",", 0);
3663 *tt = t, tt = &t->next;
3664 j = (i + mac->rotate) % mac->nparam;
3665 tm = mac->params[j];
3666 t = new_Token(NULL, tm->type, tm->text, 0);
3667 *tt = t, tt = &t->next;
3669 } else {
3670 for (i = fst - 1; i >= lst; i--) {
3671 t = new_Token(NULL, TOK_OTHER, ",", 0);
3672 *tt = t, tt = &t->next;
3673 j = (i + mac->rotate) % mac->nparam;
3674 tm = mac->params[j];
3675 t = new_Token(NULL, tm->type, tm->text, 0);
3676 *tt = t, tt = &t->next;
3680 *last = tt;
3681 return head;
3683 err:
3684 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3685 &tline->text[1]);
3686 return tline;
3690 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3691 * %-n) and MMacro-local identifiers (%%foo) as well as
3692 * macro indirection (%[...]) and range (%{..:..}).
3694 static Token *expand_mmac_params(Token * tline)
3696 Token *t, *tt, **tail, *thead;
3697 bool changed = false;
3698 char *pos;
3700 tail = &thead;
3701 thead = NULL;
3703 while (tline) {
3704 if (tline->type == TOK_PREPROC_ID &&
3705 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3706 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3707 tline->text[1] == '%')) {
3708 char *text = NULL;
3709 int type = 0, cc; /* type = 0 to placate optimisers */
3710 char tmpbuf[30];
3711 unsigned int n;
3712 int i;
3713 MMacro *mac;
3715 t = tline;
3716 tline = tline->next;
3718 mac = istk->mstk;
3719 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3720 mac = mac->next_active;
3721 if (!mac) {
3722 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3723 } else {
3724 pos = strchr(t->text, ':');
3725 if (!pos) {
3726 switch (t->text[1]) {
3728 * We have to make a substitution of one of the
3729 * forms %1, %-1, %+1, %%foo, %0.
3731 case '0':
3732 type = TOK_NUMBER;
3733 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3734 text = nasm_strdup(tmpbuf);
3735 break;
3736 case '%':
3737 type = TOK_ID;
3738 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3739 mac->unique);
3740 text = nasm_strcat(tmpbuf, t->text + 2);
3741 break;
3742 case '-':
3743 n = atoi(t->text + 2) - 1;
3744 if (n >= mac->nparam)
3745 tt = NULL;
3746 else {
3747 if (mac->nparam > 1)
3748 n = (n + mac->rotate) % mac->nparam;
3749 tt = mac->params[n];
3751 cc = find_cc(tt);
3752 if (cc == -1) {
3753 error(ERR_NONFATAL,
3754 "macro parameter %d is not a condition code",
3755 n + 1);
3756 text = NULL;
3757 } else {
3758 type = TOK_ID;
3759 if (inverse_ccs[cc] == -1) {
3760 error(ERR_NONFATAL,
3761 "condition code `%s' is not invertible",
3762 conditions[cc]);
3763 text = NULL;
3764 } else
3765 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3767 break;
3768 case '+':
3769 n = atoi(t->text + 2) - 1;
3770 if (n >= mac->nparam)
3771 tt = NULL;
3772 else {
3773 if (mac->nparam > 1)
3774 n = (n + mac->rotate) % mac->nparam;
3775 tt = mac->params[n];
3777 cc = find_cc(tt);
3778 if (cc == -1) {
3779 error(ERR_NONFATAL,
3780 "macro parameter %d is not a condition code",
3781 n + 1);
3782 text = NULL;
3783 } else {
3784 type = TOK_ID;
3785 text = nasm_strdup(conditions[cc]);
3787 break;
3788 default:
3789 n = atoi(t->text + 1) - 1;
3790 if (n >= mac->nparam)
3791 tt = NULL;
3792 else {
3793 if (mac->nparam > 1)
3794 n = (n + mac->rotate) % mac->nparam;
3795 tt = mac->params[n];
3797 if (tt) {
3798 for (i = 0; i < mac->paramlen[n]; i++) {
3799 *tail = new_Token(NULL, tt->type, tt->text, 0);
3800 tail = &(*tail)->next;
3801 tt = tt->next;
3804 text = NULL; /* we've done it here */
3805 break;
3807 } else {
3809 * seems we have a parameters range here
3811 Token *head, **last;
3812 head = expand_mmac_params_range(mac, t, &last);
3813 if (head != t) {
3814 *tail = head;
3815 *last = tline;
3816 tline = head;
3817 text = NULL;
3821 if (!text) {
3822 delete_Token(t);
3823 } else {
3824 *tail = t;
3825 tail = &t->next;
3826 t->type = type;
3827 nasm_free(t->text);
3828 t->text = text;
3829 t->a.mac = NULL;
3831 changed = true;
3832 continue;
3833 } else if (tline->type == TOK_INDIRECT) {
3834 t = tline;
3835 tline = tline->next;
3836 tt = tokenize(t->text);
3837 tt = expand_mmac_params(tt);
3838 tt = expand_smacro(tt);
3839 *tail = tt;
3840 while (tt) {
3841 tt->a.mac = NULL; /* Necessary? */
3842 tail = &tt->next;
3843 tt = tt->next;
3845 delete_Token(t);
3846 changed = true;
3847 } else {
3848 t = *tail = tline;
3849 tline = tline->next;
3850 t->a.mac = NULL;
3851 tail = &t->next;
3854 *tail = NULL;
3856 if (changed)
3857 paste_tokens(&thead, false);
3859 return thead;
3863 * Expand all single-line macro calls made in the given line.
3864 * Return the expanded version of the line. The original is deemed
3865 * to be destroyed in the process. (In reality we'll just move
3866 * Tokens from input to output a lot of the time, rather than
3867 * actually bothering to destroy and replicate.)
3870 static Token *expand_smacro(Token * tline)
3872 Token *t, *tt, *mstart, **tail, *thead;
3873 SMacro *head = NULL, *m;
3874 Token **params;
3875 int *paramsize;
3876 unsigned int nparam, sparam;
3877 int brackets;
3878 Token *org_tline = tline;
3879 Context *ctx;
3880 const char *mname;
3881 int deadman = DEADMAN_LIMIT;
3882 bool expanded;
3885 * Trick: we should avoid changing the start token pointer since it can
3886 * be contained in "next" field of other token. Because of this
3887 * we allocate a copy of first token and work with it; at the end of
3888 * routine we copy it back
3890 if (org_tline) {
3891 tline = new_Token(org_tline->next, org_tline->type,
3892 org_tline->text, 0);
3893 tline->a.mac = org_tline->a.mac;
3894 nasm_free(org_tline->text);
3895 org_tline->text = NULL;
3898 expanded = true; /* Always expand %+ at least once */
3900 again:
3901 thead = NULL;
3902 tail = &thead;
3904 while (tline) { /* main token loop */
3905 if (!--deadman) {
3906 error(ERR_NONFATAL, "interminable macro recursion");
3907 goto err;
3910 if ((mname = tline->text)) {
3911 /* if this token is a local macro, look in local context */
3912 if (tline->type == TOK_ID) {
3913 head = (SMacro *)hash_findix(&smacros, mname);
3914 } else if (tline->type == TOK_PREPROC_ID) {
3915 ctx = get_ctx(mname, &mname, true);
3916 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3917 } else
3918 head = NULL;
3921 * We've hit an identifier. As in is_mmacro below, we first
3922 * check whether the identifier is a single-line macro at
3923 * all, then think about checking for parameters if
3924 * necessary.
3926 list_for_each(m, head)
3927 if (!mstrcmp(m->name, mname, m->casesense))
3928 break;
3929 if (m) {
3930 mstart = tline;
3931 params = NULL;
3932 paramsize = NULL;
3933 if (m->nparam == 0) {
3935 * Simple case: the macro is parameterless. Discard the
3936 * one token that the macro call took, and push the
3937 * expansion back on the to-do stack.
3939 if (!m->expansion) {
3940 if (!strcmp("__FILE__", m->name)) {
3941 int32_t num = 0;
3942 char *file = NULL;
3943 src_get(&num, &file);
3944 tline->text = nasm_quote(file, strlen(file));
3945 tline->type = TOK_STRING;
3946 nasm_free(file);
3947 continue;
3949 if (!strcmp("__LINE__", m->name)) {
3950 nasm_free(tline->text);
3951 make_tok_num(tline, src_get_linnum());
3952 continue;
3954 if (!strcmp("__BITS__", m->name)) {
3955 nasm_free(tline->text);
3956 make_tok_num(tline, globalbits);
3957 continue;
3959 tline = delete_Token(tline);
3960 continue;
3962 } else {
3964 * Complicated case: at least one macro with this name
3965 * exists and takes parameters. We must find the
3966 * parameters in the call, count them, find the SMacro
3967 * that corresponds to that form of the macro call, and
3968 * substitute for the parameters when we expand. What a
3969 * pain.
3971 /*tline = tline->next;
3972 skip_white_(tline); */
3973 do {
3974 t = tline->next;
3975 while (tok_type_(t, TOK_SMAC_END)) {
3976 t->a.mac->in_progress = false;
3977 t->text = NULL;
3978 t = tline->next = delete_Token(t);
3980 tline = t;
3981 } while (tok_type_(tline, TOK_WHITESPACE));
3982 if (!tok_is_(tline, "(")) {
3984 * This macro wasn't called with parameters: ignore
3985 * the call. (Behaviour borrowed from gnu cpp.)
3987 tline = mstart;
3988 m = NULL;
3989 } else {
3990 int paren = 0;
3991 int white = 0;
3992 brackets = 0;
3993 nparam = 0;
3994 sparam = PARAM_DELTA;
3995 params = nasm_malloc(sparam * sizeof(Token *));
3996 params[0] = tline->next;
3997 paramsize = nasm_malloc(sparam * sizeof(int));
3998 paramsize[0] = 0;
3999 while (true) { /* parameter loop */
4001 * For some unusual expansions
4002 * which concatenates function call
4004 t = tline->next;
4005 while (tok_type_(t, TOK_SMAC_END)) {
4006 t->a.mac->in_progress = false;
4007 t->text = NULL;
4008 t = tline->next = delete_Token(t);
4010 tline = t;
4012 if (!tline) {
4013 error(ERR_NONFATAL,
4014 "macro call expects terminating `)'");
4015 break;
4017 if (tline->type == TOK_WHITESPACE
4018 && brackets <= 0) {
4019 if (paramsize[nparam])
4020 white++;
4021 else
4022 params[nparam] = tline->next;
4023 continue; /* parameter loop */
4025 if (tline->type == TOK_OTHER
4026 && tline->text[1] == 0) {
4027 char ch = tline->text[0];
4028 if (ch == ',' && !paren && brackets <= 0) {
4029 if (++nparam >= sparam) {
4030 sparam += PARAM_DELTA;
4031 params = nasm_realloc(params,
4032 sparam * sizeof(Token *));
4033 paramsize = nasm_realloc(paramsize,
4034 sparam * sizeof(int));
4036 params[nparam] = tline->next;
4037 paramsize[nparam] = 0;
4038 white = 0;
4039 continue; /* parameter loop */
4041 if (ch == '{' &&
4042 (brackets > 0 || (brackets == 0 &&
4043 !paramsize[nparam])))
4045 if (!(brackets++)) {
4046 params[nparam] = tline->next;
4047 continue; /* parameter loop */
4050 if (ch == '}' && brackets > 0)
4051 if (--brackets == 0) {
4052 brackets = -1;
4053 continue; /* parameter loop */
4055 if (ch == '(' && !brackets)
4056 paren++;
4057 if (ch == ')' && brackets <= 0)
4058 if (--paren < 0)
4059 break;
4061 if (brackets < 0) {
4062 brackets = 0;
4063 error(ERR_NONFATAL, "braces do not "
4064 "enclose all of macro parameter");
4066 paramsize[nparam] += white + 1;
4067 white = 0;
4068 } /* parameter loop */
4069 nparam++;
4070 while (m && (m->nparam != nparam ||
4071 mstrcmp(m->name, mname,
4072 m->casesense)))
4073 m = m->next;
4074 if (!m)
4075 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4076 "macro `%s' exists, "
4077 "but not taking %d parameters",
4078 mstart->text, nparam);
4081 if (m && m->in_progress)
4082 m = NULL;
4083 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4085 * Design question: should we handle !tline, which
4086 * indicates missing ')' here, or expand those
4087 * macros anyway, which requires the (t) test a few
4088 * lines down?
4090 nasm_free(params);
4091 nasm_free(paramsize);
4092 tline = mstart;
4093 } else {
4095 * Expand the macro: we are placed on the last token of the
4096 * call, so that we can easily split the call from the
4097 * following tokens. We also start by pushing an SMAC_END
4098 * token for the cycle removal.
4100 t = tline;
4101 if (t) {
4102 tline = t->next;
4103 t->next = NULL;
4105 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4106 tt->a.mac = m;
4107 m->in_progress = true;
4108 tline = tt;
4109 list_for_each(t, m->expansion) {
4110 if (t->type >= TOK_SMAC_PARAM) {
4111 Token *pcopy = tline, **ptail = &pcopy;
4112 Token *ttt, *pt;
4113 int i;
4115 ttt = params[t->type - TOK_SMAC_PARAM];
4116 i = paramsize[t->type - TOK_SMAC_PARAM];
4117 while (--i >= 0) {
4118 pt = *ptail = new_Token(tline, ttt->type,
4119 ttt->text, 0);
4120 ptail = &pt->next;
4121 ttt = ttt->next;
4123 tline = pcopy;
4124 } else if (t->type == TOK_PREPROC_Q) {
4125 tt = new_Token(tline, TOK_ID, mname, 0);
4126 tline = tt;
4127 } else if (t->type == TOK_PREPROC_QQ) {
4128 tt = new_Token(tline, TOK_ID, m->name, 0);
4129 tline = tt;
4130 } else {
4131 tt = new_Token(tline, t->type, t->text, 0);
4132 tline = tt;
4137 * Having done that, get rid of the macro call, and clean
4138 * up the parameters.
4140 nasm_free(params);
4141 nasm_free(paramsize);
4142 free_tlist(mstart);
4143 expanded = true;
4144 continue; /* main token loop */
4149 if (tline->type == TOK_SMAC_END) {
4150 tline->a.mac->in_progress = false;
4151 tline = delete_Token(tline);
4152 } else {
4153 t = *tail = tline;
4154 tline = tline->next;
4155 t->a.mac = NULL;
4156 t->next = NULL;
4157 tail = &t->next;
4162 * Now scan the entire line and look for successive TOK_IDs that resulted
4163 * after expansion (they can't be produced by tokenize()). The successive
4164 * TOK_IDs should be concatenated.
4165 * Also we look for %+ tokens and concatenate the tokens before and after
4166 * them (without white spaces in between).
4168 if (expanded && paste_tokens(&thead, true)) {
4170 * If we concatenated something, *and* we had previously expanded
4171 * an actual macro, scan the lines again for macros...
4173 tline = thead;
4174 expanded = false;
4175 goto again;
4178 err:
4179 if (org_tline) {
4180 if (thead) {
4181 *org_tline = *thead;
4182 /* since we just gave text to org_line, don't free it */
4183 thead->text = NULL;
4184 delete_Token(thead);
4185 } else {
4186 /* the expression expanded to empty line;
4187 we can't return NULL for some reasons
4188 we just set the line to a single WHITESPACE token. */
4189 memset(org_tline, 0, sizeof(*org_tline));
4190 org_tline->text = NULL;
4191 org_tline->type = TOK_WHITESPACE;
4193 thead = org_tline;
4196 return thead;
4200 * Similar to expand_smacro but used exclusively with macro identifiers
4201 * right before they are fetched in. The reason is that there can be
4202 * identifiers consisting of several subparts. We consider that if there
4203 * are more than one element forming the name, user wants a expansion,
4204 * otherwise it will be left as-is. Example:
4206 * %define %$abc cde
4208 * the identifier %$abc will be left as-is so that the handler for %define
4209 * will suck it and define the corresponding value. Other case:
4211 * %define _%$abc cde
4213 * In this case user wants name to be expanded *before* %define starts
4214 * working, so we'll expand %$abc into something (if it has a value;
4215 * otherwise it will be left as-is) then concatenate all successive
4216 * PP_IDs into one.
4218 static Token *expand_id(Token * tline)
4220 Token *cur, *oldnext = NULL;
4222 if (!tline || !tline->next)
4223 return tline;
4225 cur = tline;
4226 while (cur->next &&
4227 (cur->next->type == TOK_ID ||
4228 cur->next->type == TOK_PREPROC_ID
4229 || cur->next->type == TOK_NUMBER))
4230 cur = cur->next;
4232 /* If identifier consists of just one token, don't expand */
4233 if (cur == tline)
4234 return tline;
4236 if (cur) {
4237 oldnext = cur->next; /* Detach the tail past identifier */
4238 cur->next = NULL; /* so that expand_smacro stops here */
4241 tline = expand_smacro(tline);
4243 if (cur) {
4244 /* expand_smacro possibly changhed tline; re-scan for EOL */
4245 cur = tline;
4246 while (cur && cur->next)
4247 cur = cur->next;
4248 if (cur)
4249 cur->next = oldnext;
4252 return tline;
4256 * Determine whether the given line constitutes a multi-line macro
4257 * call, and return the MMacro structure called if so. Doesn't have
4258 * to check for an initial label - that's taken care of in
4259 * expand_mmacro - but must check numbers of parameters. Guaranteed
4260 * to be called with tline->type == TOK_ID, so the putative macro
4261 * name is easy to find.
4263 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4265 MMacro *head, *m;
4266 Token **params;
4267 int nparam;
4269 head = (MMacro *) hash_findix(&mmacros, tline->text);
4272 * Efficiency: first we see if any macro exists with the given
4273 * name. If not, we can return NULL immediately. _Then_ we
4274 * count the parameters, and then we look further along the
4275 * list if necessary to find the proper MMacro.
4277 list_for_each(m, head)
4278 if (!mstrcmp(m->name, tline->text, m->casesense))
4279 break;
4280 if (!m)
4281 return NULL;
4284 * OK, we have a potential macro. Count and demarcate the
4285 * parameters.
4287 count_mmac_params(tline->next, &nparam, &params);
4290 * So we know how many parameters we've got. Find the MMacro
4291 * structure that handles this number.
4293 while (m) {
4294 if (m->nparam_min <= nparam
4295 && (m->plus || nparam <= m->nparam_max)) {
4297 * This one is right. Just check if cycle removal
4298 * prohibits us using it before we actually celebrate...
4300 if (m->in_progress > m->max_depth) {
4301 if (m->max_depth > 0) {
4302 error(ERR_WARNING,
4303 "reached maximum recursion depth of %i",
4304 m->max_depth);
4306 nasm_free(params);
4307 return NULL;
4310 * It's right, and we can use it. Add its default
4311 * parameters to the end of our list if necessary.
4313 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4314 params =
4315 nasm_realloc(params,
4316 ((m->nparam_min + m->ndefs +
4317 1) * sizeof(*params)));
4318 while (nparam < m->nparam_min + m->ndefs) {
4319 params[nparam] = m->defaults[nparam - m->nparam_min];
4320 nparam++;
4324 * If we've gone over the maximum parameter count (and
4325 * we're in Plus mode), ignore parameters beyond
4326 * nparam_max.
4328 if (m->plus && nparam > m->nparam_max)
4329 nparam = m->nparam_max;
4331 * Then terminate the parameter list, and leave.
4333 if (!params) { /* need this special case */
4334 params = nasm_malloc(sizeof(*params));
4335 nparam = 0;
4337 params[nparam] = NULL;
4338 *params_array = params;
4339 return m;
4342 * This one wasn't right: look for the next one with the
4343 * same name.
4345 list_for_each(m, m->next)
4346 if (!mstrcmp(m->name, tline->text, m->casesense))
4347 break;
4351 * After all that, we didn't find one with the right number of
4352 * parameters. Issue a warning, and fail to expand the macro.
4354 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4355 "macro `%s' exists, but not taking %d parameters",
4356 tline->text, nparam);
4357 nasm_free(params);
4358 return NULL;
4363 * Save MMacro invocation specific fields in
4364 * preparation for a recursive macro expansion
4366 static void push_mmacro(MMacro *m)
4368 MMacroInvocation *i;
4370 i = nasm_malloc(sizeof(MMacroInvocation));
4371 i->prev = m->prev;
4372 i->params = m->params;
4373 i->iline = m->iline;
4374 i->nparam = m->nparam;
4375 i->rotate = m->rotate;
4376 i->paramlen = m->paramlen;
4377 i->unique = m->unique;
4378 i->condcnt = m->condcnt;
4379 m->prev = i;
4384 * Restore MMacro invocation specific fields that were
4385 * saved during a previous recursive macro expansion
4387 static void pop_mmacro(MMacro *m)
4389 MMacroInvocation *i;
4391 if (m->prev) {
4392 i = m->prev;
4393 m->prev = i->prev;
4394 m->params = i->params;
4395 m->iline = i->iline;
4396 m->nparam = i->nparam;
4397 m->rotate = i->rotate;
4398 m->paramlen = i->paramlen;
4399 m->unique = i->unique;
4400 m->condcnt = i->condcnt;
4401 nasm_free(i);
4407 * Expand the multi-line macro call made by the given line, if
4408 * there is one to be expanded. If there is, push the expansion on
4409 * istk->expansion and return 1. Otherwise return 0.
4411 static int expand_mmacro(Token * tline)
4413 Token *startline = tline;
4414 Token *label = NULL;
4415 int dont_prepend = 0;
4416 Token **params, *t, *mtok, *tt;
4417 MMacro *m;
4418 Line *l, *ll;
4419 int i, nparam, *paramlen;
4420 const char *mname;
4422 t = tline;
4423 skip_white_(t);
4424 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4425 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4426 return 0;
4427 mtok = t;
4428 m = is_mmacro(t, &params);
4429 if (m) {
4430 mname = t->text;
4431 } else {
4432 Token *last;
4434 * We have an id which isn't a macro call. We'll assume
4435 * it might be a label; we'll also check to see if a
4436 * colon follows it. Then, if there's another id after
4437 * that lot, we'll check it again for macro-hood.
4439 label = last = t;
4440 t = t->next;
4441 if (tok_type_(t, TOK_WHITESPACE))
4442 last = t, t = t->next;
4443 if (tok_is_(t, ":")) {
4444 dont_prepend = 1;
4445 last = t, t = t->next;
4446 if (tok_type_(t, TOK_WHITESPACE))
4447 last = t, t = t->next;
4449 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4450 return 0;
4451 last->next = NULL;
4452 mname = t->text;
4453 tline = t;
4457 * Fix up the parameters: this involves stripping leading and
4458 * trailing whitespace, then stripping braces if they are
4459 * present.
4461 for (nparam = 0; params[nparam]; nparam++) ;
4462 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4464 for (i = 0; params[i]; i++) {
4465 int brace = false;
4466 int comma = (!m->plus || i < nparam - 1);
4468 t = params[i];
4469 skip_white_(t);
4470 if (tok_is_(t, "{"))
4471 t = t->next, brace = true, comma = false;
4472 params[i] = t;
4473 paramlen[i] = 0;
4474 while (t) {
4475 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4476 break; /* ... because we have hit a comma */
4477 if (comma && t->type == TOK_WHITESPACE
4478 && tok_is_(t->next, ","))
4479 break; /* ... or a space then a comma */
4480 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4481 break; /* ... or a brace */
4482 t = t->next;
4483 paramlen[i]++;
4488 * OK, we have a MMacro structure together with a set of
4489 * parameters. We must now go through the expansion and push
4490 * copies of each Line on to istk->expansion. Substitution of
4491 * parameter tokens and macro-local tokens doesn't get done
4492 * until the single-line macro substitution process; this is
4493 * because delaying them allows us to change the semantics
4494 * later through %rotate.
4496 * First, push an end marker on to istk->expansion, mark this
4497 * macro as in progress, and set up its invocation-specific
4498 * variables.
4500 ll = nasm_malloc(sizeof(Line));
4501 ll->next = istk->expansion;
4502 ll->finishes = m;
4503 ll->first = NULL;
4504 istk->expansion = ll;
4507 * Save the previous MMacro expansion in the case of
4508 * macro recursion
4510 if (m->max_depth && m->in_progress)
4511 push_mmacro(m);
4513 m->in_progress ++;
4514 m->params = params;
4515 m->iline = tline;
4516 m->nparam = nparam;
4517 m->rotate = 0;
4518 m->paramlen = paramlen;
4519 m->unique = unique++;
4520 m->lineno = 0;
4521 m->condcnt = 0;
4523 m->next_active = istk->mstk;
4524 istk->mstk = m;
4526 list_for_each(l, m->expansion) {
4527 Token **tail;
4529 ll = nasm_malloc(sizeof(Line));
4530 ll->finishes = NULL;
4531 ll->next = istk->expansion;
4532 istk->expansion = ll;
4533 tail = &ll->first;
4535 list_for_each(t, l->first) {
4536 Token *x = t;
4537 switch (t->type) {
4538 case TOK_PREPROC_Q:
4539 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4540 break;
4541 case TOK_PREPROC_QQ:
4542 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4543 break;
4544 case TOK_PREPROC_ID:
4545 if (t->text[1] == '0' && t->text[2] == '0') {
4546 dont_prepend = -1;
4547 x = label;
4548 if (!x)
4549 continue;
4551 /* fall through */
4552 default:
4553 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4554 break;
4556 tail = &tt->next;
4558 *tail = NULL;
4562 * If we had a label, push it on as the first line of
4563 * the macro expansion.
4565 if (label) {
4566 if (dont_prepend < 0)
4567 free_tlist(startline);
4568 else {
4569 ll = nasm_malloc(sizeof(Line));
4570 ll->finishes = NULL;
4571 ll->next = istk->expansion;
4572 istk->expansion = ll;
4573 ll->first = startline;
4574 if (!dont_prepend) {
4575 while (label->next)
4576 label = label->next;
4577 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4582 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4584 return 1;
4587 /* The function that actually does the error reporting */
4588 static void verror(int severity, const char *fmt, va_list arg)
4590 char buff[1024];
4592 vsnprintf(buff, sizeof(buff), fmt, arg);
4594 if (istk && istk->mstk && istk->mstk->name)
4595 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4596 istk->mstk->lineno, buff);
4597 else
4598 nasm_error(severity, "%s", buff);
4602 * Since preprocessor always operate only on the line that didn't
4603 * arrived yet, we should always use ERR_OFFBY1.
4605 static void error(int severity, const char *fmt, ...)
4607 va_list arg;
4609 /* If we're in a dead branch of IF or something like it, ignore the error */
4610 if (istk && istk->conds && !emitting(istk->conds->state))
4611 return;
4613 va_start(arg, fmt);
4614 verror(severity, fmt, arg);
4615 va_end(arg);
4619 * Because %else etc are evaluated in the state context
4620 * of the previous branch, errors might get lost with error():
4621 * %if 0 ... %else trailing garbage ... %endif
4622 * So %else etc should report errors with this function.
4624 static void error_precond(int severity, const char *fmt, ...)
4626 va_list arg;
4628 /* Only ignore the error if it's really in a dead branch */
4629 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4630 return;
4632 va_start(arg, fmt);
4633 verror(severity, fmt, arg);
4634 va_end(arg);
4637 static void
4638 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4640 Token *t;
4642 cstk = NULL;
4643 istk = nasm_malloc(sizeof(Include));
4644 istk->next = NULL;
4645 istk->conds = NULL;
4646 istk->expansion = NULL;
4647 istk->mstk = NULL;
4648 istk->fp = fopen(file, "r");
4649 istk->fname = NULL;
4650 src_set_fname(nasm_strdup(file));
4651 src_set_linnum(0);
4652 istk->lineinc = 1;
4653 if (!istk->fp)
4654 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4655 file);
4656 defining = NULL;
4657 nested_mac_count = 0;
4658 nested_rep_count = 0;
4659 init_macros();
4660 unique = 0;
4661 if (tasm_compatible_mode) {
4662 stdmacpos = nasm_stdmac;
4663 } else {
4664 stdmacpos = nasm_stdmac_after_tasm;
4666 any_extrastdmac = extrastdmac && *extrastdmac;
4667 do_predef = true;
4668 list = listgen;
4671 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4672 * The caller, however, will also pass in 3 for preprocess-only so
4673 * we can set __PASS__ accordingly.
4675 pass = apass > 2 ? 2 : apass;
4677 dephead = deptail = deplist;
4678 if (deplist) {
4679 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4680 sl->next = NULL;
4681 strcpy(sl->str, file);
4682 *deptail = sl;
4683 deptail = &sl->next;
4687 * Define the __PASS__ macro. This is defined here unlike
4688 * all the other builtins, because it is special -- it varies between
4689 * passes.
4691 t = nasm_malloc(sizeof(*t));
4692 t->next = NULL;
4693 make_tok_num(t, apass);
4694 t->a.mac = NULL;
4695 define_smacro(NULL, "__PASS__", true, 0, t);
4698 static char *pp_getline(void)
4700 char *line;
4701 Token *tline;
4703 while (1) {
4705 * Fetch a tokenized line, either from the macro-expansion
4706 * buffer or from the input file.
4708 tline = NULL;
4709 while (istk->expansion && istk->expansion->finishes) {
4710 Line *l = istk->expansion;
4711 if (!l->finishes->name && l->finishes->in_progress > 1) {
4712 Line *ll;
4715 * This is a macro-end marker for a macro with no
4716 * name, which means it's not really a macro at all
4717 * but a %rep block, and the `in_progress' field is
4718 * more than 1, meaning that we still need to
4719 * repeat. (1 means the natural last repetition; 0
4720 * means termination by %exitrep.) We have
4721 * therefore expanded up to the %endrep, and must
4722 * push the whole block on to the expansion buffer
4723 * again. We don't bother to remove the macro-end
4724 * marker: we'd only have to generate another one
4725 * if we did.
4727 l->finishes->in_progress--;
4728 list_for_each(l, l->finishes->expansion) {
4729 Token *t, *tt, **tail;
4731 ll = nasm_malloc(sizeof(Line));
4732 ll->next = istk->expansion;
4733 ll->finishes = NULL;
4734 ll->first = NULL;
4735 tail = &ll->first;
4737 list_for_each(t, l->first) {
4738 if (t->text || t->type == TOK_WHITESPACE) {
4739 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4740 tail = &tt->next;
4744 istk->expansion = ll;
4746 } else {
4748 * Check whether a `%rep' was started and not ended
4749 * within this macro expansion. This can happen and
4750 * should be detected. It's a fatal error because
4751 * I'm too confused to work out how to recover
4752 * sensibly from it.
4754 if (defining) {
4755 if (defining->name)
4756 error(ERR_PANIC,
4757 "defining with name in expansion");
4758 else if (istk->mstk->name)
4759 error(ERR_FATAL,
4760 "`%%rep' without `%%endrep' within"
4761 " expansion of macro `%s'",
4762 istk->mstk->name);
4766 * FIXME: investigate the relationship at this point between
4767 * istk->mstk and l->finishes
4770 MMacro *m = istk->mstk;
4771 istk->mstk = m->next_active;
4772 if (m->name) {
4774 * This was a real macro call, not a %rep, and
4775 * therefore the parameter information needs to
4776 * be freed.
4778 if (m->prev) {
4779 pop_mmacro(m);
4780 l->finishes->in_progress --;
4781 } else {
4782 nasm_free(m->params);
4783 free_tlist(m->iline);
4784 nasm_free(m->paramlen);
4785 l->finishes->in_progress = 0;
4787 } else
4788 free_mmacro(m);
4790 istk->expansion = l->next;
4791 nasm_free(l);
4792 list->downlevel(LIST_MACRO);
4795 while (1) { /* until we get a line we can use */
4797 if (istk->expansion) { /* from a macro expansion */
4798 char *p;
4799 Line *l = istk->expansion;
4800 if (istk->mstk)
4801 istk->mstk->lineno++;
4802 tline = l->first;
4803 istk->expansion = l->next;
4804 nasm_free(l);
4805 p = detoken(tline, false);
4806 list->line(LIST_MACRO, p);
4807 nasm_free(p);
4808 break;
4810 line = read_line();
4811 if (line) { /* from the current input file */
4812 line = prepreproc(line);
4813 tline = tokenize(line);
4814 nasm_free(line);
4815 break;
4818 * The current file has ended; work down the istk
4821 Include *i = istk;
4822 fclose(i->fp);
4823 if (i->conds)
4824 error(ERR_FATAL,
4825 "expected `%%endif' before end of file");
4826 /* only set line and file name if there's a next node */
4827 if (i->next) {
4828 src_set_linnum(i->lineno);
4829 nasm_free(src_set_fname(i->fname));
4831 istk = i->next;
4832 list->downlevel(LIST_INCLUDE);
4833 nasm_free(i);
4834 if (!istk)
4835 return NULL;
4836 if (istk->expansion && istk->expansion->finishes)
4837 break;
4842 * We must expand MMacro parameters and MMacro-local labels
4843 * _before_ we plunge into directive processing, to cope
4844 * with things like `%define something %1' such as STRUC
4845 * uses. Unless we're _defining_ a MMacro, in which case
4846 * those tokens should be left alone to go into the
4847 * definition; and unless we're in a non-emitting
4848 * condition, in which case we don't want to meddle with
4849 * anything.
4851 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4852 && !(istk->mstk && !istk->mstk->in_progress)) {
4853 tline = expand_mmac_params(tline);
4857 * Check the line to see if it's a preprocessor directive.
4859 if (do_directive(tline) == DIRECTIVE_FOUND) {
4860 continue;
4861 } else if (defining) {
4863 * We're defining a multi-line macro. We emit nothing
4864 * at all, and just
4865 * shove the tokenized line on to the macro definition.
4867 Line *l = nasm_malloc(sizeof(Line));
4868 l->next = defining->expansion;
4869 l->first = tline;
4870 l->finishes = NULL;
4871 defining->expansion = l;
4872 continue;
4873 } else if (istk->conds && !emitting(istk->conds->state)) {
4875 * We're in a non-emitting branch of a condition block.
4876 * Emit nothing at all, not even a blank line: when we
4877 * emerge from the condition we'll give a line-number
4878 * directive so we keep our place correctly.
4880 free_tlist(tline);
4881 continue;
4882 } else if (istk->mstk && !istk->mstk->in_progress) {
4884 * We're in a %rep block which has been terminated, so
4885 * we're walking through to the %endrep without
4886 * emitting anything. Emit nothing at all, not even a
4887 * blank line: when we emerge from the %rep block we'll
4888 * give a line-number directive so we keep our place
4889 * correctly.
4891 free_tlist(tline);
4892 continue;
4893 } else {
4894 tline = expand_smacro(tline);
4895 if (!expand_mmacro(tline)) {
4897 * De-tokenize the line again, and emit it.
4899 line = detoken(tline, true);
4900 free_tlist(tline);
4901 break;
4902 } else {
4903 continue; /* expand_mmacro calls free_tlist */
4908 return line;
4911 static void pp_cleanup(int pass)
4913 if (defining) {
4914 if (defining->name) {
4915 error(ERR_NONFATAL,
4916 "end of file while still defining macro `%s'",
4917 defining->name);
4918 } else {
4919 error(ERR_NONFATAL, "end of file while still in %%rep");
4922 free_mmacro(defining);
4923 defining = NULL;
4925 while (cstk)
4926 ctx_pop();
4927 free_macros();
4928 while (istk) {
4929 Include *i = istk;
4930 istk = istk->next;
4931 fclose(i->fp);
4932 nasm_free(i->fname);
4933 nasm_free(i);
4935 while (cstk)
4936 ctx_pop();
4937 nasm_free(src_set_fname(NULL));
4938 if (pass == 0) {
4939 IncPath *i;
4940 free_llist(predef);
4941 delete_Blocks();
4942 while ((i = ipath)) {
4943 ipath = i->next;
4944 if (i->path)
4945 nasm_free(i->path);
4946 nasm_free(i);
4951 void pp_include_path(char *path)
4953 IncPath *i;
4955 i = nasm_malloc(sizeof(IncPath));
4956 i->path = path ? nasm_strdup(path) : NULL;
4957 i->next = NULL;
4959 if (ipath) {
4960 IncPath *j = ipath;
4961 while (j->next)
4962 j = j->next;
4963 j->next = i;
4964 } else {
4965 ipath = i;
4969 void pp_pre_include(char *fname)
4971 Token *inc, *space, *name;
4972 Line *l;
4974 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4975 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4976 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4978 l = nasm_malloc(sizeof(Line));
4979 l->next = predef;
4980 l->first = inc;
4981 l->finishes = NULL;
4982 predef = l;
4985 void pp_pre_define(char *definition)
4987 Token *def, *space;
4988 Line *l;
4989 char *equals;
4991 equals = strchr(definition, '=');
4992 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4993 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4994 if (equals)
4995 *equals = ' ';
4996 space->next = tokenize(definition);
4997 if (equals)
4998 *equals = '=';
5000 l = nasm_malloc(sizeof(Line));
5001 l->next = predef;
5002 l->first = def;
5003 l->finishes = NULL;
5004 predef = l;
5007 void pp_pre_undefine(char *definition)
5009 Token *def, *space;
5010 Line *l;
5012 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5013 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5014 space->next = tokenize(definition);
5016 l = nasm_malloc(sizeof(Line));
5017 l->next = predef;
5018 l->first = def;
5019 l->finishes = NULL;
5020 predef = l;
5024 * Added by Keith Kanios:
5026 * This function is used to assist with "runtime" preprocessor
5027 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5029 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5030 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5033 void pp_runtime(char *definition)
5035 Token *def;
5037 def = tokenize(definition);
5038 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5039 free_tlist(def);
5043 void pp_extra_stdmac(macros_t *macros)
5045 extrastdmac = macros;
5048 static void make_tok_num(Token * tok, int64_t val)
5050 char numbuf[20];
5051 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5052 tok->text = nasm_strdup(numbuf);
5053 tok->type = TOK_NUMBER;
5056 Preproc nasmpp = {
5057 pp_reset,
5058 pp_getline,
5059 pp_cleanup