Fix NULL dereferences on %substr missing operands
[nasm.git] / preproc.c
blob22d30ca9cacea7a082ce95e903f5cb5aa9967a40
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
48 * }
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
63 #include "compiler.h"
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <string.h>
70 #include <ctype.h>
71 #include <limits.h>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
84 typedef struct SMacro SMacro;
85 typedef struct MMacro MMacro;
86 typedef struct MMacroInvocation MMacroInvocation;
87 typedef struct Context Context;
88 typedef struct Token Token;
89 typedef struct Blocks Blocks;
90 typedef struct Line Line;
91 typedef struct Include Include;
92 typedef struct Cond Cond;
93 typedef struct IncPath IncPath;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
107 struct SMacro {
108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
133 struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
160 /* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
163 struct MMacroInvocation {
164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
170 uint64_t condcnt;
175 * The context stack is composed of a linked list of these.
177 struct Context {
178 Context *next;
179 char *name;
180 struct hash_table localmac;
181 uint32_t number;
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
192 * %define a(x,y) ( (x) & ~(y) )
194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
203 enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
215 struct Token {
216 Token *next;
217 char *text;
218 union {
219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
222 enum pp_token_type type;
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
247 struct Line {
248 Line *next;
249 MMacro *finishes;
250 Token *first;
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
257 struct Include {
258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
262 char *fname;
263 int lineno, lineinc;
264 MMacro *mstk; /* stack of active macros/reps */
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
272 struct IncPath {
273 IncPath *next;
274 char *path;
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
284 struct Cond {
285 Cond *next;
286 int state;
288 enum {
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
297 COND_IF_TRUE, COND_IF_FALSE,
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
313 COND_DONE, COND_NEVER
315 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318 * These defines are used as the possible return values for do_directive
320 #define NO_DIRECTIVE_FOUND 0
321 #define DIRECTIVE_FOUND 1
324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
327 #define DEADMAN_LIMIT (1 << 20)
329 /* max reps */
330 #define REP_LIMIT ((INT64_C(1) << 62))
333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
338 static const char * const conditions[] = {
339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
343 enum pp_conds {
344 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
345 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
346 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
347 c_none = -1
349 static const enum pp_conds inverse_ccs[] = {
350 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
351 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
352 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
356 * Directive names.
358 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
359 static int is_condition(enum preproc_token arg)
361 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
364 /* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
370 enum {
371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375 static const char * const tasm_directives[] = {
376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
380 static int StackSize = 4;
381 static char *StackPointer = "ebp";
382 static int ArgOffset = 8;
383 static int LocalOffset = 0;
385 static Context *cstk;
386 static Include *istk;
387 static IncPath *ipath = NULL;
389 static int pass; /* HACK: pass 0 = generate dependencies only */
390 static StrList **dephead, **deptail; /* Dependency list */
392 static uint64_t unique; /* unique identifier numbers */
394 static Line *predef = NULL;
395 static bool do_predef;
397 static ListGen *list;
400 * The current set of multi-line macros we have defined.
402 static struct hash_table mmacros;
405 * The current set of single-line macros we have defined.
407 static struct hash_table smacros;
410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
413 static MMacro *defining;
415 static uint64_t nested_mac_count;
416 static uint64_t nested_rep_count;
419 * The number of macro parameters to allocate space for at a time.
421 #define PARAM_DELTA 16
424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
427 static macros_t *stdmacpos;
430 * The extra standard macros that come from the object format, if
431 * any.
433 static macros_t *extrastdmac = NULL;
434 static bool any_extrastdmac;
437 * Tokens are allocated in blocks to improve speed
439 #define TOKEN_BLOCKSIZE 4096
440 static Token *freeTokens = NULL;
441 struct Blocks {
442 Blocks *next;
443 void *chunk;
446 static Blocks blocks = { NULL, NULL };
449 * Forward declarations.
451 static Token *expand_mmac_params(Token * tline);
452 static Token *expand_smacro(Token * tline);
453 static Token *expand_id(Token * tline);
454 static Context *get_ctx(const char *name, const char **namep,
455 bool all_contexts);
456 static void make_tok_num(Token * tok, int64_t val);
457 static void error(int severity, const char *fmt, ...);
458 static void error_precond(int severity, const char *fmt, ...);
459 static void *new_Block(size_t size);
460 static void delete_Blocks(void);
461 static Token *new_Token(Token * next, enum pp_token_type type,
462 const char *text, int txtlen);
463 static Token *delete_Token(Token * t);
466 * Macros for safe checking of token pointers, avoid *(NULL)
468 #define tok_type_(x,t) ((x) && (x)->type == (t))
469 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
478 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
480 size_t len = nasm_unquote(qstr, NULL);
481 size_t clen = strlen(qstr);
483 if (len != clen)
484 error(ERR_NONFATAL, "NUL character in `%s' directive",
485 pp_directives[directive]);
487 return clen;
491 * Handle TASM specific directives, which do not contain a % in
492 * front of them. We do it here because I could not find any other
493 * place to do it for the moment, and it is a hack (ideally it would
494 * be nice to be able to use the NASM pre-processor to do it).
496 static char *check_tasm_directive(char *line)
498 int32_t i, j, k, m, len;
499 char *p, *q, *oldline, oldchar;
501 p = nasm_skip_spaces(line);
503 /* Binary search for the directive name */
504 i = -1;
505 j = ARRAY_SIZE(tasm_directives);
506 q = nasm_skip_word(p);
507 len = q - p;
508 if (len) {
509 oldchar = p[len];
510 p[len] = 0;
511 while (j - i > 1) {
512 k = (j + i) / 2;
513 m = nasm_stricmp(p, tasm_directives[k]);
514 if (m == 0) {
515 /* We have found a directive, so jam a % in front of it
516 * so that NASM will then recognise it as one if it's own.
518 p[len] = oldchar;
519 len = strlen(p);
520 oldline = line;
521 line = nasm_malloc(len + 2);
522 line[0] = '%';
523 if (k == TM_IFDIFI) {
525 * NASM does not recognise IFDIFI, so we convert
526 * it to %if 0. This is not used in NASM
527 * compatible code, but does need to parse for the
528 * TASM macro package.
530 strcpy(line + 1, "if 0");
531 } else {
532 memcpy(line + 1, p, len + 1);
534 nasm_free(oldline);
535 return line;
536 } else if (m < 0) {
537 j = k;
538 } else
539 i = k;
541 p[len] = oldchar;
543 return line;
547 * The pre-preprocessing stage... This function translates line
548 * number indications as they emerge from GNU cpp (`# lineno "file"
549 * flags') into NASM preprocessor line number indications (`%line
550 * lineno file').
552 static char *prepreproc(char *line)
554 int lineno, fnlen;
555 char *fname, *oldline;
557 if (line[0] == '#' && line[1] == ' ') {
558 oldline = line;
559 fname = oldline + 2;
560 lineno = atoi(fname);
561 fname += strspn(fname, "0123456789 ");
562 if (*fname == '"')
563 fname++;
564 fnlen = strcspn(fname, "\"");
565 line = nasm_malloc(20 + fnlen);
566 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
567 nasm_free(oldline);
569 if (tasm_compatible_mode)
570 return check_tasm_directive(line);
571 return line;
575 * Free a linked list of tokens.
577 static void free_tlist(Token * list)
579 while (list)
580 list = delete_Token(list);
584 * Free a linked list of lines.
586 static void free_llist(Line * list)
588 Line *l, *tmp;
589 list_for_each_safe(l, tmp, list) {
590 free_tlist(l->first);
591 nasm_free(l);
596 * Free an MMacro
598 static void free_mmacro(MMacro * m)
600 nasm_free(m->name);
601 free_tlist(m->dlist);
602 nasm_free(m->defaults);
603 free_llist(m->expansion);
604 nasm_free(m);
608 * Free all currently defined macros, and free the hash tables
610 static void free_smacro_table(struct hash_table *smt)
612 SMacro *s, *tmp;
613 const char *key;
614 struct hash_tbl_node *it = NULL;
616 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
617 nasm_free((void *)key);
618 list_for_each_safe(s, tmp, s) {
619 nasm_free(s->name);
620 free_tlist(s->expansion);
621 nasm_free(s);
624 hash_free(smt);
627 static void free_mmacro_table(struct hash_table *mmt)
629 MMacro *m, *tmp;
630 const char *key;
631 struct hash_tbl_node *it = NULL;
633 it = NULL;
634 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
635 nasm_free((void *)key);
636 list_for_each_safe(m ,tmp, m)
637 free_mmacro(m);
639 hash_free(mmt);
642 static void free_macros(void)
644 free_smacro_table(&smacros);
645 free_mmacro_table(&mmacros);
649 * Initialize the hash tables
651 static void init_macros(void)
653 hash_init(&smacros, HASH_LARGE);
654 hash_init(&mmacros, HASH_LARGE);
658 * Pop the context stack.
660 static void ctx_pop(void)
662 Context *c = cstk;
664 cstk = cstk->next;
665 free_smacro_table(&c->localmac);
666 nasm_free(c->name);
667 nasm_free(c);
671 * Search for a key in the hash index; adding it if necessary
672 * (in which case we initialize the data pointer to NULL.)
674 static void **
675 hash_findi_add(struct hash_table *hash, const char *str)
677 struct hash_insert hi;
678 void **r;
679 char *strx;
681 r = hash_findi(hash, str, &hi);
682 if (r)
683 return r;
685 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
686 return hash_add(&hi, strx, NULL);
690 * Like hash_findi, but returns the data element rather than a pointer
691 * to it. Used only when not adding a new element, hence no third
692 * argument.
694 static void *
695 hash_findix(struct hash_table *hash, const char *str)
697 void **p;
699 p = hash_findi(hash, str, NULL);
700 return p ? *p : NULL;
704 * read line from standart macros set,
705 * if there no more left -- return NULL
707 static char *line_from_stdmac(void)
709 unsigned char c;
710 const unsigned char *p = stdmacpos;
711 char *line, *q;
712 size_t len = 0;
714 if (!stdmacpos)
715 return NULL;
717 while ((c = *p++)) {
718 if (c >= 0x80)
719 len += pp_directives_len[c - 0x80] + 1;
720 else
721 len++;
724 line = nasm_malloc(len + 1);
725 q = line;
726 while ((c = *stdmacpos++)) {
727 if (c >= 0x80) {
728 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
729 q += pp_directives_len[c - 0x80];
730 *q++ = ' ';
731 } else {
732 *q++ = c;
735 stdmacpos = p;
736 *q = '\0';
738 if (!*stdmacpos) {
739 /* This was the last of the standard macro chain... */
740 stdmacpos = NULL;
741 if (any_extrastdmac) {
742 stdmacpos = extrastdmac;
743 any_extrastdmac = false;
744 } else if (do_predef) {
745 Line *pd, *l;
746 Token *head, **tail, *t;
749 * Nasty hack: here we push the contents of
750 * `predef' on to the top-level expansion stack,
751 * since this is the most convenient way to
752 * implement the pre-include and pre-define
753 * features.
755 list_for_each(pd, predef) {
756 head = NULL;
757 tail = &head;
758 list_for_each(t, pd->first) {
759 *tail = new_Token(NULL, t->type, t->text, 0);
760 tail = &(*tail)->next;
763 l = nasm_malloc(sizeof(Line));
764 l->next = istk->expansion;
765 l->first = head;
766 l->finishes = NULL;
768 istk->expansion = l;
770 do_predef = false;
774 return line;
777 #define BUF_DELTA 512
779 * Read a line from the top file in istk, handling multiple CR/LFs
780 * at the end of the line read, and handling spurious ^Zs. Will
781 * return lines from the standard macro set if this has not already
782 * been done.
784 static char *read_line(void)
786 char *buffer, *p, *q;
787 int bufsize, continued_count;
790 * standart macros set (predefined) goes first
792 p = line_from_stdmac();
793 if (p)
794 return p;
797 * regular read from a file
799 bufsize = BUF_DELTA;
800 buffer = nasm_malloc(BUF_DELTA);
801 p = buffer;
802 continued_count = 0;
803 while (1) {
804 q = fgets(p, bufsize - (p - buffer), istk->fp);
805 if (!q)
806 break;
807 p += strlen(p);
808 if (p > buffer && p[-1] == '\n') {
810 * Convert backslash-CRLF line continuation sequences into
811 * nothing at all (for DOS and Windows)
813 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
814 p -= 3;
815 *p = 0;
816 continued_count++;
819 * Also convert backslash-LF line continuation sequences into
820 * nothing at all (for Unix)
822 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
823 p -= 2;
824 *p = 0;
825 continued_count++;
826 } else {
827 break;
830 if (p - buffer > bufsize - 10) {
831 int32_t offset = p - buffer;
832 bufsize += BUF_DELTA;
833 buffer = nasm_realloc(buffer, bufsize);
834 p = buffer + offset; /* prevent stale-pointer problems */
838 if (!q && p == buffer) {
839 nasm_free(buffer);
840 return NULL;
843 src_set_linnum(src_get_linnum() + istk->lineinc +
844 (continued_count * istk->lineinc));
847 * Play safe: remove CRs as well as LFs, if any of either are
848 * present at the end of the line.
850 while (--p >= buffer && (*p == '\n' || *p == '\r'))
851 *p = '\0';
854 * Handle spurious ^Z, which may be inserted into source files
855 * by some file transfer utilities.
857 buffer[strcspn(buffer, "\032")] = '\0';
859 list->line(LIST_READ, buffer);
861 return buffer;
865 * Tokenize a line of text. This is a very simple process since we
866 * don't need to parse the value out of e.g. numeric tokens: we
867 * simply split one string into many.
869 static Token *tokenize(char *line)
871 char c, *p = line;
872 enum pp_token_type type;
873 Token *list = NULL;
874 Token *t, **tail = &list;
876 while (*line) {
877 p = line;
878 if (*p == '%') {
879 p++;
880 if (*p == '+' && !nasm_isdigit(p[1])) {
881 p++;
882 type = TOK_PASTE;
883 } else if (nasm_isdigit(*p) ||
884 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
885 do {
886 p++;
888 while (nasm_isdigit(*p));
889 type = TOK_PREPROC_ID;
890 } else if (*p == '{') {
891 p++;
892 while (*p && *p != '}') {
893 p[-1] = *p;
894 p++;
896 p[-1] = '\0';
897 if (*p)
898 p++;
899 type = TOK_PREPROC_ID;
900 } else if (*p == '[') {
901 int lvl = 1;
902 line += 2; /* Skip the leading %[ */
903 p++;
904 while (lvl && (c = *p++)) {
905 switch (c) {
906 case ']':
907 lvl--;
908 break;
909 case '%':
910 if (*p == '[')
911 lvl++;
912 break;
913 case '\'':
914 case '\"':
915 case '`':
916 p = nasm_skip_string(p - 1) + 1;
917 break;
918 default:
919 break;
922 p--;
923 if (*p)
924 *p++ = '\0';
925 if (lvl)
926 error(ERR_NONFATAL, "unterminated %[ construct");
927 type = TOK_INDIRECT;
928 } else if (*p == '?') {
929 type = TOK_PREPROC_Q; /* %? */
930 p++;
931 if (*p == '?') {
932 type = TOK_PREPROC_QQ; /* %?? */
933 p++;
935 } else if (*p == '!') {
936 type = TOK_PREPROC_ID;
937 p++;
938 if (isidchar(*p)) {
939 do {
940 p++;
942 while (isidchar(*p));
943 } else if (*p == '\'' || *p == '\"' || *p == '`') {
944 p = nasm_skip_string(p);
945 if (*p)
946 p++;
947 else
948 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
949 } else {
950 /* %! without string or identifier */
951 type = TOK_OTHER; /* Legacy behavior... */
953 } else if (isidchar(*p) ||
954 ((*p == '!' || *p == '%' || *p == '$') &&
955 isidchar(p[1]))) {
956 do {
957 p++;
959 while (isidchar(*p));
960 type = TOK_PREPROC_ID;
961 } else {
962 type = TOK_OTHER;
963 if (*p == '%')
964 p++;
966 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
967 type = TOK_ID;
968 p++;
969 while (*p && isidchar(*p))
970 p++;
971 } else if (*p == '\'' || *p == '"' || *p == '`') {
973 * A string token.
975 type = TOK_STRING;
976 p = nasm_skip_string(p);
978 if (*p) {
979 p++;
980 } else {
981 error(ERR_WARNING|ERR_PASS1, "unterminated string");
982 /* Handling unterminated strings by UNV */
983 /* type = -1; */
985 } else if (p[0] == '$' && p[1] == '$') {
986 type = TOK_OTHER; /* TOKEN_BASE */
987 p += 2;
988 } else if (isnumstart(*p)) {
989 bool is_hex = false;
990 bool is_float = false;
991 bool has_e = false;
992 char c, *r;
995 * A numeric token.
998 if (*p == '$') {
999 p++;
1000 is_hex = true;
1003 for (;;) {
1004 c = *p++;
1006 if (!is_hex && (c == 'e' || c == 'E')) {
1007 has_e = true;
1008 if (*p == '+' || *p == '-') {
1010 * e can only be followed by +/- if it is either a
1011 * prefixed hex number or a floating-point number
1013 p++;
1014 is_float = true;
1016 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1017 is_hex = true;
1018 } else if (c == 'P' || c == 'p') {
1019 is_float = true;
1020 if (*p == '+' || *p == '-')
1021 p++;
1022 } else if (isnumchar(c) || c == '_')
1023 ; /* just advance */
1024 else if (c == '.') {
1026 * we need to deal with consequences of the legacy
1027 * parser, like "1.nolist" being two tokens
1028 * (TOK_NUMBER, TOK_ID) here; at least give it
1029 * a shot for now. In the future, we probably need
1030 * a flex-based scanner with proper pattern matching
1031 * to do it as well as it can be done. Nothing in
1032 * the world is going to help the person who wants
1033 * 0x123.p16 interpreted as two tokens, though.
1035 r = p;
1036 while (*r == '_')
1037 r++;
1039 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1040 (!is_hex && (*r == 'e' || *r == 'E')) ||
1041 (*r == 'p' || *r == 'P')) {
1042 p = r;
1043 is_float = true;
1044 } else
1045 break; /* Terminate the token */
1046 } else
1047 break;
1049 p--; /* Point to first character beyond number */
1051 if (p == line+1 && *line == '$') {
1052 type = TOK_OTHER; /* TOKEN_HERE */
1053 } else {
1054 if (has_e && !is_hex) {
1055 /* 1e13 is floating-point, but 1e13h is not */
1056 is_float = true;
1059 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1061 } else if (nasm_isspace(*p)) {
1062 type = TOK_WHITESPACE;
1063 p = nasm_skip_spaces(p);
1065 * Whitespace just before end-of-line is discarded by
1066 * pretending it's a comment; whitespace just before a
1067 * comment gets lumped into the comment.
1069 if (!*p || *p == ';') {
1070 type = TOK_COMMENT;
1071 while (*p)
1072 p++;
1074 } else if (*p == ';') {
1075 type = TOK_COMMENT;
1076 while (*p)
1077 p++;
1078 } else {
1080 * Anything else is an operator of some kind. We check
1081 * for all the double-character operators (>>, <<, //,
1082 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1083 * else is a single-character operator.
1085 type = TOK_OTHER;
1086 if ((p[0] == '>' && p[1] == '>') ||
1087 (p[0] == '<' && p[1] == '<') ||
1088 (p[0] == '/' && p[1] == '/') ||
1089 (p[0] == '<' && p[1] == '=') ||
1090 (p[0] == '>' && p[1] == '=') ||
1091 (p[0] == '=' && p[1] == '=') ||
1092 (p[0] == '!' && p[1] == '=') ||
1093 (p[0] == '<' && p[1] == '>') ||
1094 (p[0] == '&' && p[1] == '&') ||
1095 (p[0] == '|' && p[1] == '|') ||
1096 (p[0] == '^' && p[1] == '^')) {
1097 p++;
1099 p++;
1102 /* Handling unterminated string by UNV */
1103 /*if (type == -1)
1105 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1106 t->text[p-line] = *line;
1107 tail = &t->next;
1109 else */
1110 if (type != TOK_COMMENT) {
1111 *tail = t = new_Token(NULL, type, line, p - line);
1112 tail = &t->next;
1114 line = p;
1116 return list;
1120 * this function allocates a new managed block of memory and
1121 * returns a pointer to the block. The managed blocks are
1122 * deleted only all at once by the delete_Blocks function.
1124 static void *new_Block(size_t size)
1126 Blocks *b = &blocks;
1128 /* first, get to the end of the linked list */
1129 while (b->next)
1130 b = b->next;
1131 /* now allocate the requested chunk */
1132 b->chunk = nasm_malloc(size);
1134 /* now allocate a new block for the next request */
1135 b->next = nasm_malloc(sizeof(Blocks));
1136 /* and initialize the contents of the new block */
1137 b->next->next = NULL;
1138 b->next->chunk = NULL;
1139 return b->chunk;
1143 * this function deletes all managed blocks of memory
1145 static void delete_Blocks(void)
1147 Blocks *a, *b = &blocks;
1150 * keep in mind that the first block, pointed to by blocks
1151 * is a static and not dynamically allocated, so we don't
1152 * free it.
1154 while (b) {
1155 if (b->chunk)
1156 nasm_free(b->chunk);
1157 a = b;
1158 b = b->next;
1159 if (a != &blocks)
1160 nasm_free(a);
1165 * this function creates a new Token and passes a pointer to it
1166 * back to the caller. It sets the type and text elements, and
1167 * also the a.mac and next elements to NULL.
1169 static Token *new_Token(Token * next, enum pp_token_type type,
1170 const char *text, int txtlen)
1172 Token *t;
1173 int i;
1175 if (!freeTokens) {
1176 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1177 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1178 freeTokens[i].next = &freeTokens[i + 1];
1179 freeTokens[i].next = NULL;
1181 t = freeTokens;
1182 freeTokens = t->next;
1183 t->next = next;
1184 t->a.mac = NULL;
1185 t->type = type;
1186 if (type == TOK_WHITESPACE || !text) {
1187 t->text = NULL;
1188 } else {
1189 if (txtlen == 0)
1190 txtlen = strlen(text);
1191 t->text = nasm_malloc(txtlen+1);
1192 memcpy(t->text, text, txtlen);
1193 t->text[txtlen] = '\0';
1195 return t;
1198 static Token *delete_Token(Token * t)
1200 Token *next = t->next;
1201 nasm_free(t->text);
1202 t->next = freeTokens;
1203 freeTokens = t;
1204 return next;
1208 * Convert a line of tokens back into text.
1209 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1210 * will be transformed into ..@ctxnum.xxx
1212 static char *detoken(Token * tlist, bool expand_locals)
1214 Token *t;
1215 char *line, *p;
1216 const char *q;
1217 int len = 0;
1219 list_for_each(t, tlist) {
1220 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1221 char *v;
1222 char *q = t->text;
1224 v = t->text + 2;
1225 if (*v == '\'' || *v == '\"' || *v == '`') {
1226 size_t len = nasm_unquote(v, NULL);
1227 size_t clen = strlen(v);
1229 if (len != clen) {
1230 error(ERR_NONFATAL | ERR_PASS1,
1231 "NUL character in %! string");
1232 v = NULL;
1236 if (v) {
1237 char *p = getenv(v);
1238 if (!p) {
1239 error(ERR_NONFATAL | ERR_PASS1,
1240 "nonexistent environment variable `%s'", v);
1241 p = "";
1243 t->text = nasm_strdup(p);
1245 nasm_free(q);
1248 /* Expand local macros here and not during preprocessing */
1249 if (expand_locals &&
1250 t->type == TOK_PREPROC_ID && t->text &&
1251 t->text[0] == '%' && t->text[1] == '$') {
1252 const char *q;
1253 char *p;
1254 Context *ctx = get_ctx(t->text, &q, false);
1255 if (ctx) {
1256 char buffer[40];
1257 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1258 p = nasm_strcat(buffer, q);
1259 nasm_free(t->text);
1260 t->text = p;
1263 if (t->type == TOK_WHITESPACE)
1264 len++;
1265 else if (t->text)
1266 len += strlen(t->text);
1269 p = line = nasm_malloc(len + 1);
1271 list_for_each(t, tlist) {
1272 if (t->type == TOK_WHITESPACE) {
1273 *p++ = ' ';
1274 } else if (t->text) {
1275 q = t->text;
1276 while (*q)
1277 *p++ = *q++;
1280 *p = '\0';
1282 return line;
1286 * A scanner, suitable for use by the expression evaluator, which
1287 * operates on a line of Tokens. Expects a pointer to a pointer to
1288 * the first token in the line to be passed in as its private_data
1289 * field.
1291 * FIX: This really needs to be unified with stdscan.
1293 static int ppscan(void *private_data, struct tokenval *tokval)
1295 Token **tlineptr = private_data;
1296 Token *tline;
1297 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1299 do {
1300 tline = *tlineptr;
1301 *tlineptr = tline ? tline->next : NULL;
1302 } while (tline && (tline->type == TOK_WHITESPACE ||
1303 tline->type == TOK_COMMENT));
1305 if (!tline)
1306 return tokval->t_type = TOKEN_EOS;
1308 tokval->t_charptr = tline->text;
1310 if (tline->text[0] == '$' && !tline->text[1])
1311 return tokval->t_type = TOKEN_HERE;
1312 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1313 return tokval->t_type = TOKEN_BASE;
1315 if (tline->type == TOK_ID) {
1316 p = tokval->t_charptr = tline->text;
1317 if (p[0] == '$') {
1318 tokval->t_charptr++;
1319 return tokval->t_type = TOKEN_ID;
1322 for (r = p, s = ourcopy; *r; r++) {
1323 if (r >= p+MAX_KEYWORD)
1324 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1325 *s++ = nasm_tolower(*r);
1327 *s = '\0';
1328 /* right, so we have an identifier sitting in temp storage. now,
1329 * is it actually a register or instruction name, or what? */
1330 return nasm_token_hash(ourcopy, tokval);
1333 if (tline->type == TOK_NUMBER) {
1334 bool rn_error;
1335 tokval->t_integer = readnum(tline->text, &rn_error);
1336 tokval->t_charptr = tline->text;
1337 if (rn_error)
1338 return tokval->t_type = TOKEN_ERRNUM;
1339 else
1340 return tokval->t_type = TOKEN_NUM;
1343 if (tline->type == TOK_FLOAT) {
1344 return tokval->t_type = TOKEN_FLOAT;
1347 if (tline->type == TOK_STRING) {
1348 char bq, *ep;
1350 bq = tline->text[0];
1351 tokval->t_charptr = tline->text;
1352 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1354 if (ep[0] != bq || ep[1] != '\0')
1355 return tokval->t_type = TOKEN_ERRSTR;
1356 else
1357 return tokval->t_type = TOKEN_STR;
1360 if (tline->type == TOK_OTHER) {
1361 if (!strcmp(tline->text, "<<"))
1362 return tokval->t_type = TOKEN_SHL;
1363 if (!strcmp(tline->text, ">>"))
1364 return tokval->t_type = TOKEN_SHR;
1365 if (!strcmp(tline->text, "//"))
1366 return tokval->t_type = TOKEN_SDIV;
1367 if (!strcmp(tline->text, "%%"))
1368 return tokval->t_type = TOKEN_SMOD;
1369 if (!strcmp(tline->text, "=="))
1370 return tokval->t_type = TOKEN_EQ;
1371 if (!strcmp(tline->text, "<>"))
1372 return tokval->t_type = TOKEN_NE;
1373 if (!strcmp(tline->text, "!="))
1374 return tokval->t_type = TOKEN_NE;
1375 if (!strcmp(tline->text, "<="))
1376 return tokval->t_type = TOKEN_LE;
1377 if (!strcmp(tline->text, ">="))
1378 return tokval->t_type = TOKEN_GE;
1379 if (!strcmp(tline->text, "&&"))
1380 return tokval->t_type = TOKEN_DBL_AND;
1381 if (!strcmp(tline->text, "^^"))
1382 return tokval->t_type = TOKEN_DBL_XOR;
1383 if (!strcmp(tline->text, "||"))
1384 return tokval->t_type = TOKEN_DBL_OR;
1388 * We have no other options: just return the first character of
1389 * the token text.
1391 return tokval->t_type = tline->text[0];
1395 * Compare a string to the name of an existing macro; this is a
1396 * simple wrapper which calls either strcmp or nasm_stricmp
1397 * depending on the value of the `casesense' parameter.
1399 static int mstrcmp(const char *p, const char *q, bool casesense)
1401 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1405 * Compare a string to the name of an existing macro; this is a
1406 * simple wrapper which calls either strcmp or nasm_stricmp
1407 * depending on the value of the `casesense' parameter.
1409 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1411 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1415 * Return the Context structure associated with a %$ token. Return
1416 * NULL, having _already_ reported an error condition, if the
1417 * context stack isn't deep enough for the supplied number of $
1418 * signs.
1419 * If all_contexts == true, contexts that enclose current are
1420 * also scanned for such smacro, until it is found; if not -
1421 * only the context that directly results from the number of $'s
1422 * in variable's name.
1424 * If "namep" is non-NULL, set it to the pointer to the macro name
1425 * tail, i.e. the part beyond %$...
1427 static Context *get_ctx(const char *name, const char **namep,
1428 bool all_contexts)
1430 Context *ctx;
1431 SMacro *m;
1432 int i;
1434 if (namep)
1435 *namep = name;
1437 if (!name || name[0] != '%' || name[1] != '$')
1438 return NULL;
1440 if (!cstk) {
1441 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1442 return NULL;
1445 name += 2;
1446 ctx = cstk;
1447 i = 0;
1448 while (ctx && *name == '$') {
1449 name++;
1450 i++;
1451 ctx = ctx->next;
1453 if (!ctx) {
1454 error(ERR_NONFATAL, "`%s': context stack is only"
1455 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1456 return NULL;
1459 if (namep)
1460 *namep = name;
1462 if (!all_contexts)
1463 return ctx;
1466 * NOTE: In 2.10 we will not need lookup in extarnal
1467 * contexts, so this is a gentle way to inform users
1468 * about their source code need to be updated
1471 /* first round -- check the current context */
1472 m = hash_findix(&ctx->localmac, name);
1473 while (m) {
1474 if (!mstrcmp(m->name, name, m->casesense))
1475 return ctx;
1476 m = m->next;
1479 /* second round - external contexts */
1480 while ((ctx = ctx->next)) {
1481 /* Search for this smacro in found context */
1482 m = hash_findix(&ctx->localmac, name);
1483 while (m) {
1484 if (!mstrcmp(m->name, name, m->casesense)) {
1485 /* NOTE: deprecated as of 2.10 */
1486 static int once = 0;
1487 if (!once) {
1488 error(ERR_WARNING, "context-local macro expansion"
1489 " fall-through (automatic searching of outer"
1490 " contexts) will be deprecated starting in"
1491 " NASM 2.10, please see the NASM Manual for"
1492 " more information");
1493 once = 1;
1495 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
1496 return ctx;
1498 m = m->next;
1502 return NULL;
1506 * Check to see if a file is already in a string list
1508 static bool in_list(const StrList *list, const char *str)
1510 while (list) {
1511 if (!strcmp(list->str, str))
1512 return true;
1513 list = list->next;
1515 return false;
1519 * Open an include file. This routine must always return a valid
1520 * file pointer if it returns - it's responsible for throwing an
1521 * ERR_FATAL and bombing out completely if not. It should also try
1522 * the include path one by one until it finds the file or reaches
1523 * the end of the path.
1525 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1526 bool missing_ok)
1528 FILE *fp;
1529 char *prefix = "";
1530 IncPath *ip = ipath;
1531 int len = strlen(file);
1532 size_t prefix_len = 0;
1533 StrList *sl;
1535 while (1) {
1536 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1537 memcpy(sl->str, prefix, prefix_len);
1538 memcpy(sl->str+prefix_len, file, len+1);
1539 fp = fopen(sl->str, "r");
1540 if (fp && dhead && !in_list(*dhead, sl->str)) {
1541 sl->next = NULL;
1542 **dtail = sl;
1543 *dtail = &sl->next;
1544 } else {
1545 nasm_free(sl);
1547 if (fp)
1548 return fp;
1549 if (!ip) {
1550 if (!missing_ok)
1551 break;
1552 prefix = NULL;
1553 } else {
1554 prefix = ip->path;
1555 ip = ip->next;
1557 if (prefix) {
1558 prefix_len = strlen(prefix);
1559 } else {
1560 /* -MG given and file not found */
1561 if (dhead && !in_list(*dhead, file)) {
1562 sl = nasm_malloc(len+1+sizeof sl->next);
1563 sl->next = NULL;
1564 strcpy(sl->str, file);
1565 **dtail = sl;
1566 *dtail = &sl->next;
1568 return NULL;
1572 error(ERR_FATAL, "unable to open include file `%s'", file);
1573 return NULL;
1577 * Determine if we should warn on defining a single-line macro of
1578 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1579 * return true if _any_ single-line macro of that name is defined.
1580 * Otherwise, will return true if a single-line macro with either
1581 * `nparam' or no parameters is defined.
1583 * If a macro with precisely the right number of parameters is
1584 * defined, or nparam is -1, the address of the definition structure
1585 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1586 * is NULL, no action will be taken regarding its contents, and no
1587 * error will occur.
1589 * Note that this is also called with nparam zero to resolve
1590 * `ifdef'.
1592 * If you already know which context macro belongs to, you can pass
1593 * the context pointer as first parameter; if you won't but name begins
1594 * with %$ the context will be automatically computed. If all_contexts
1595 * is true, macro will be searched in outer contexts as well.
1597 static bool
1598 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1599 bool nocase)
1601 struct hash_table *smtbl;
1602 SMacro *m;
1604 if (ctx) {
1605 smtbl = &ctx->localmac;
1606 } else if (name[0] == '%' && name[1] == '$') {
1607 if (cstk)
1608 ctx = get_ctx(name, &name, false);
1609 if (!ctx)
1610 return false; /* got to return _something_ */
1611 smtbl = &ctx->localmac;
1612 } else {
1613 smtbl = &smacros;
1615 m = (SMacro *) hash_findix(smtbl, name);
1617 while (m) {
1618 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1619 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1620 if (defn) {
1621 if (nparam == (int) m->nparam || nparam == -1)
1622 *defn = m;
1623 else
1624 *defn = NULL;
1626 return true;
1628 m = m->next;
1631 return false;
1635 * Count and mark off the parameters in a multi-line macro call.
1636 * This is called both from within the multi-line macro expansion
1637 * code, and also to mark off the default parameters when provided
1638 * in a %macro definition line.
1640 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1642 int paramsize, brace;
1644 *nparam = paramsize = 0;
1645 *params = NULL;
1646 while (t) {
1647 /* +1: we need space for the final NULL */
1648 if (*nparam+1 >= paramsize) {
1649 paramsize += PARAM_DELTA;
1650 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1652 skip_white_(t);
1653 brace = false;
1654 if (tok_is_(t, "{"))
1655 brace = true;
1656 (*params)[(*nparam)++] = t;
1657 while (tok_isnt_(t, brace ? "}" : ","))
1658 t = t->next;
1659 if (t) { /* got a comma/brace */
1660 t = t->next;
1661 if (brace) {
1663 * Now we've found the closing brace, look further
1664 * for the comma.
1666 skip_white_(t);
1667 if (tok_isnt_(t, ",")) {
1668 error(ERR_NONFATAL,
1669 "braces do not enclose all of macro parameter");
1670 while (tok_isnt_(t, ","))
1671 t = t->next;
1673 if (t)
1674 t = t->next; /* eat the comma */
1681 * Determine whether one of the various `if' conditions is true or
1682 * not.
1684 * We must free the tline we get passed.
1686 static bool if_condition(Token * tline, enum preproc_token ct)
1688 enum pp_conditional i = PP_COND(ct);
1689 bool j;
1690 Token *t, *tt, **tptr, *origline;
1691 struct tokenval tokval;
1692 expr *evalresult;
1693 enum pp_token_type needtype;
1694 char *p;
1696 origline = tline;
1698 switch (i) {
1699 case PPC_IFCTX:
1700 j = false; /* have we matched yet? */
1701 while (true) {
1702 skip_white_(tline);
1703 if (!tline)
1704 break;
1705 if (tline->type != TOK_ID) {
1706 error(ERR_NONFATAL,
1707 "`%s' expects context identifiers", pp_directives[ct]);
1708 free_tlist(origline);
1709 return -1;
1711 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1712 j = true;
1713 tline = tline->next;
1715 break;
1717 case PPC_IFDEF:
1718 j = false; /* have we matched yet? */
1719 while (tline) {
1720 skip_white_(tline);
1721 if (!tline || (tline->type != TOK_ID &&
1722 (tline->type != TOK_PREPROC_ID ||
1723 tline->text[1] != '$'))) {
1724 error(ERR_NONFATAL,
1725 "`%s' expects macro identifiers", pp_directives[ct]);
1726 goto fail;
1728 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1729 j = true;
1730 tline = tline->next;
1732 break;
1734 case PPC_IFENV:
1735 tline = expand_smacro(tline);
1736 j = false; /* have we matched yet? */
1737 while (tline) {
1738 skip_white_(tline);
1739 if (!tline || (tline->type != TOK_ID &&
1740 tline->type != TOK_STRING &&
1741 (tline->type != TOK_PREPROC_ID ||
1742 tline->text[1] != '!'))) {
1743 error(ERR_NONFATAL,
1744 "`%s' expects environment variable names",
1745 pp_directives[ct]);
1746 goto fail;
1748 p = tline->text;
1749 if (tline->type == TOK_PREPROC_ID)
1750 p += 2; /* Skip leading %! */
1751 if (*p == '\'' || *p == '\"' || *p == '`')
1752 nasm_unquote_cstr(p, ct);
1753 if (getenv(p))
1754 j = true;
1755 tline = tline->next;
1757 break;
1759 case PPC_IFIDN:
1760 case PPC_IFIDNI:
1761 tline = expand_smacro(tline);
1762 t = tt = tline;
1763 while (tok_isnt_(tt, ","))
1764 tt = tt->next;
1765 if (!tt) {
1766 error(ERR_NONFATAL,
1767 "`%s' expects two comma-separated arguments",
1768 pp_directives[ct]);
1769 goto fail;
1771 tt = tt->next;
1772 j = true; /* assume equality unless proved not */
1773 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1774 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1775 error(ERR_NONFATAL, "`%s': more than one comma on line",
1776 pp_directives[ct]);
1777 goto fail;
1779 if (t->type == TOK_WHITESPACE) {
1780 t = t->next;
1781 continue;
1783 if (tt->type == TOK_WHITESPACE) {
1784 tt = tt->next;
1785 continue;
1787 if (tt->type != t->type) {
1788 j = false; /* found mismatching tokens */
1789 break;
1791 /* When comparing strings, need to unquote them first */
1792 if (t->type == TOK_STRING) {
1793 size_t l1 = nasm_unquote(t->text, NULL);
1794 size_t l2 = nasm_unquote(tt->text, NULL);
1796 if (l1 != l2) {
1797 j = false;
1798 break;
1800 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1801 j = false;
1802 break;
1804 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1805 j = false; /* found mismatching tokens */
1806 break;
1809 t = t->next;
1810 tt = tt->next;
1812 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1813 j = false; /* trailing gunk on one end or other */
1814 break;
1816 case PPC_IFMACRO:
1818 bool found = false;
1819 MMacro searching, *mmac;
1821 skip_white_(tline);
1822 tline = expand_id(tline);
1823 if (!tok_type_(tline, TOK_ID)) {
1824 error(ERR_NONFATAL,
1825 "`%s' expects a macro name", pp_directives[ct]);
1826 goto fail;
1828 searching.name = nasm_strdup(tline->text);
1829 searching.casesense = true;
1830 searching.plus = false;
1831 searching.nolist = false;
1832 searching.in_progress = 0;
1833 searching.max_depth = 0;
1834 searching.rep_nest = NULL;
1835 searching.nparam_min = 0;
1836 searching.nparam_max = INT_MAX;
1837 tline = expand_smacro(tline->next);
1838 skip_white_(tline);
1839 if (!tline) {
1840 } else if (!tok_type_(tline, TOK_NUMBER)) {
1841 error(ERR_NONFATAL,
1842 "`%s' expects a parameter count or nothing",
1843 pp_directives[ct]);
1844 } else {
1845 searching.nparam_min = searching.nparam_max =
1846 readnum(tline->text, &j);
1847 if (j)
1848 error(ERR_NONFATAL,
1849 "unable to parse parameter count `%s'",
1850 tline->text);
1852 if (tline && tok_is_(tline->next, "-")) {
1853 tline = tline->next->next;
1854 if (tok_is_(tline, "*"))
1855 searching.nparam_max = INT_MAX;
1856 else if (!tok_type_(tline, TOK_NUMBER))
1857 error(ERR_NONFATAL,
1858 "`%s' expects a parameter count after `-'",
1859 pp_directives[ct]);
1860 else {
1861 searching.nparam_max = readnum(tline->text, &j);
1862 if (j)
1863 error(ERR_NONFATAL,
1864 "unable to parse parameter count `%s'",
1865 tline->text);
1866 if (searching.nparam_min > searching.nparam_max)
1867 error(ERR_NONFATAL,
1868 "minimum parameter count exceeds maximum");
1871 if (tline && tok_is_(tline->next, "+")) {
1872 tline = tline->next;
1873 searching.plus = true;
1875 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1876 while (mmac) {
1877 if (!strcmp(mmac->name, searching.name) &&
1878 (mmac->nparam_min <= searching.nparam_max
1879 || searching.plus)
1880 && (searching.nparam_min <= mmac->nparam_max
1881 || mmac->plus)) {
1882 found = true;
1883 break;
1885 mmac = mmac->next;
1887 if (tline && tline->next)
1888 error(ERR_WARNING|ERR_PASS1,
1889 "trailing garbage after %%ifmacro ignored");
1890 nasm_free(searching.name);
1891 j = found;
1892 break;
1895 case PPC_IFID:
1896 needtype = TOK_ID;
1897 goto iftype;
1898 case PPC_IFNUM:
1899 needtype = TOK_NUMBER;
1900 goto iftype;
1901 case PPC_IFSTR:
1902 needtype = TOK_STRING;
1903 goto iftype;
1905 iftype:
1906 t = tline = expand_smacro(tline);
1908 while (tok_type_(t, TOK_WHITESPACE) ||
1909 (needtype == TOK_NUMBER &&
1910 tok_type_(t, TOK_OTHER) &&
1911 (t->text[0] == '-' || t->text[0] == '+') &&
1912 !t->text[1]))
1913 t = t->next;
1915 j = tok_type_(t, needtype);
1916 break;
1918 case PPC_IFTOKEN:
1919 t = tline = expand_smacro(tline);
1920 while (tok_type_(t, TOK_WHITESPACE))
1921 t = t->next;
1923 j = false;
1924 if (t) {
1925 t = t->next; /* Skip the actual token */
1926 while (tok_type_(t, TOK_WHITESPACE))
1927 t = t->next;
1928 j = !t; /* Should be nothing left */
1930 break;
1932 case PPC_IFEMPTY:
1933 t = tline = expand_smacro(tline);
1934 while (tok_type_(t, TOK_WHITESPACE))
1935 t = t->next;
1937 j = !t; /* Should be empty */
1938 break;
1940 case PPC_IF:
1941 t = tline = expand_smacro(tline);
1942 tptr = &t;
1943 tokval.t_type = TOKEN_INVALID;
1944 evalresult = evaluate(ppscan, tptr, &tokval,
1945 NULL, pass | CRITICAL, error, NULL);
1946 if (!evalresult)
1947 return -1;
1948 if (tokval.t_type)
1949 error(ERR_WARNING|ERR_PASS1,
1950 "trailing garbage after expression ignored");
1951 if (!is_simple(evalresult)) {
1952 error(ERR_NONFATAL,
1953 "non-constant value given to `%s'", pp_directives[ct]);
1954 goto fail;
1956 j = reloc_value(evalresult) != 0;
1957 break;
1959 default:
1960 error(ERR_FATAL,
1961 "preprocessor directive `%s' not yet implemented",
1962 pp_directives[ct]);
1963 goto fail;
1966 free_tlist(origline);
1967 return j ^ PP_NEGATIVE(ct);
1969 fail:
1970 free_tlist(origline);
1971 return -1;
1975 * Common code for defining an smacro
1977 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1978 int nparam, Token *expansion)
1980 SMacro *smac, **smhead;
1981 struct hash_table *smtbl;
1983 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1984 if (!smac) {
1985 error(ERR_WARNING|ERR_PASS1,
1986 "single-line macro `%s' defined both with and"
1987 " without parameters", mname);
1989 * Some instances of the old code considered this a failure,
1990 * some others didn't. What is the right thing to do here?
1992 free_tlist(expansion);
1993 return false; /* Failure */
1994 } else {
1996 * We're redefining, so we have to take over an
1997 * existing SMacro structure. This means freeing
1998 * what was already in it.
2000 nasm_free(smac->name);
2001 free_tlist(smac->expansion);
2003 } else {
2004 smtbl = ctx ? &ctx->localmac : &smacros;
2005 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2006 smac = nasm_malloc(sizeof(SMacro));
2007 smac->next = *smhead;
2008 *smhead = smac;
2010 smac->name = nasm_strdup(mname);
2011 smac->casesense = casesense;
2012 smac->nparam = nparam;
2013 smac->expansion = expansion;
2014 smac->in_progress = false;
2015 return true; /* Success */
2019 * Undefine an smacro
2021 static void undef_smacro(Context *ctx, const char *mname)
2023 SMacro **smhead, *s, **sp;
2024 struct hash_table *smtbl;
2026 smtbl = ctx ? &ctx->localmac : &smacros;
2027 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2029 if (smhead) {
2031 * We now have a macro name... go hunt for it.
2033 sp = smhead;
2034 while ((s = *sp) != NULL) {
2035 if (!mstrcmp(s->name, mname, s->casesense)) {
2036 *sp = s->next;
2037 nasm_free(s->name);
2038 free_tlist(s->expansion);
2039 nasm_free(s);
2040 } else {
2041 sp = &s->next;
2048 * Parse a mmacro specification.
2050 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2052 bool err;
2054 tline = tline->next;
2055 skip_white_(tline);
2056 tline = expand_id(tline);
2057 if (!tok_type_(tline, TOK_ID)) {
2058 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2059 return false;
2062 def->prev = NULL;
2063 def->name = nasm_strdup(tline->text);
2064 def->plus = false;
2065 def->nolist = false;
2066 def->in_progress = 0;
2067 def->rep_nest = NULL;
2068 def->nparam_min = 0;
2069 def->nparam_max = 0;
2071 tline = expand_smacro(tline->next);
2072 skip_white_(tline);
2073 if (!tok_type_(tline, TOK_NUMBER)) {
2074 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2075 } else {
2076 def->nparam_min = def->nparam_max =
2077 readnum(tline->text, &err);
2078 if (err)
2079 error(ERR_NONFATAL,
2080 "unable to parse parameter count `%s'", tline->text);
2082 if (tline && tok_is_(tline->next, "-")) {
2083 tline = tline->next->next;
2084 if (tok_is_(tline, "*")) {
2085 def->nparam_max = INT_MAX;
2086 } else if (!tok_type_(tline, TOK_NUMBER)) {
2087 error(ERR_NONFATAL,
2088 "`%s' expects a parameter count after `-'", directive);
2089 } else {
2090 def->nparam_max = readnum(tline->text, &err);
2091 if (err) {
2092 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2093 tline->text);
2095 if (def->nparam_min > def->nparam_max) {
2096 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2100 if (tline && tok_is_(tline->next, "+")) {
2101 tline = tline->next;
2102 def->plus = true;
2104 if (tline && tok_type_(tline->next, TOK_ID) &&
2105 !nasm_stricmp(tline->next->text, ".nolist")) {
2106 tline = tline->next;
2107 def->nolist = true;
2111 * Handle default parameters.
2113 if (tline && tline->next) {
2114 def->dlist = tline->next;
2115 tline->next = NULL;
2116 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2117 } else {
2118 def->dlist = NULL;
2119 def->defaults = NULL;
2121 def->expansion = NULL;
2123 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2124 !def->plus)
2125 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2126 "too many default macro parameters");
2128 return true;
2133 * Decode a size directive
2135 static int parse_size(const char *str) {
2136 static const char *size_names[] =
2137 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2138 static const int sizes[] =
2139 { 0, 1, 4, 16, 8, 10, 2, 32 };
2141 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2145 * find and process preprocessor directive in passed line
2146 * Find out if a line contains a preprocessor directive, and deal
2147 * with it if so.
2149 * If a directive _is_ found, it is the responsibility of this routine
2150 * (and not the caller) to free_tlist() the line.
2152 * @param tline a pointer to the current tokeninzed line linked list
2153 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2156 static int do_directive(Token * tline)
2158 enum preproc_token i;
2159 int j;
2160 bool err;
2161 int nparam;
2162 bool nolist;
2163 bool casesense;
2164 int k, m;
2165 int offset;
2166 char *p, *pp;
2167 const char *mname;
2168 Include *inc;
2169 Context *ctx;
2170 Cond *cond;
2171 MMacro *mmac, **mmhead;
2172 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2173 Line *l;
2174 struct tokenval tokval;
2175 expr *evalresult;
2176 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2177 int64_t count;
2178 size_t len;
2179 int severity;
2181 origline = tline;
2183 skip_white_(tline);
2184 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2185 (tline->text[1] == '%' || tline->text[1] == '$'
2186 || tline->text[1] == '!'))
2187 return NO_DIRECTIVE_FOUND;
2189 i = pp_token_hash(tline->text);
2192 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2193 * since they are known to be buggy at moment, we need to fix them
2194 * in future release (2.09-2.10)
2196 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2197 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2198 tline->text);
2199 return NO_DIRECTIVE_FOUND;
2203 * If we're in a non-emitting branch of a condition construct,
2204 * or walking to the end of an already terminated %rep block,
2205 * we should ignore all directives except for condition
2206 * directives.
2208 if (((istk->conds && !emitting(istk->conds->state)) ||
2209 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2210 return NO_DIRECTIVE_FOUND;
2214 * If we're defining a macro or reading a %rep block, we should
2215 * ignore all directives except for %macro/%imacro (which nest),
2216 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2217 * If we're in a %rep block, another %rep nests, so should be let through.
2219 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2220 i != PP_RMACRO && i != PP_IRMACRO &&
2221 i != PP_ENDMACRO && i != PP_ENDM &&
2222 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2223 return NO_DIRECTIVE_FOUND;
2226 if (defining) {
2227 if (i == PP_MACRO || i == PP_IMACRO ||
2228 i == PP_RMACRO || i == PP_IRMACRO) {
2229 nested_mac_count++;
2230 return NO_DIRECTIVE_FOUND;
2231 } else if (nested_mac_count > 0) {
2232 if (i == PP_ENDMACRO) {
2233 nested_mac_count--;
2234 return NO_DIRECTIVE_FOUND;
2237 if (!defining->name) {
2238 if (i == PP_REP) {
2239 nested_rep_count++;
2240 return NO_DIRECTIVE_FOUND;
2241 } else if (nested_rep_count > 0) {
2242 if (i == PP_ENDREP) {
2243 nested_rep_count--;
2244 return NO_DIRECTIVE_FOUND;
2250 switch (i) {
2251 case PP_INVALID:
2252 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2253 tline->text);
2254 return NO_DIRECTIVE_FOUND; /* didn't get it */
2256 case PP_STACKSIZE:
2257 /* Directive to tell NASM what the default stack size is. The
2258 * default is for a 16-bit stack, and this can be overriden with
2259 * %stacksize large.
2261 tline = tline->next;
2262 if (tline && tline->type == TOK_WHITESPACE)
2263 tline = tline->next;
2264 if (!tline || tline->type != TOK_ID) {
2265 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2266 free_tlist(origline);
2267 return DIRECTIVE_FOUND;
2269 if (nasm_stricmp(tline->text, "flat") == 0) {
2270 /* All subsequent ARG directives are for a 32-bit stack */
2271 StackSize = 4;
2272 StackPointer = "ebp";
2273 ArgOffset = 8;
2274 LocalOffset = 0;
2275 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2276 /* All subsequent ARG directives are for a 64-bit stack */
2277 StackSize = 8;
2278 StackPointer = "rbp";
2279 ArgOffset = 16;
2280 LocalOffset = 0;
2281 } else if (nasm_stricmp(tline->text, "large") == 0) {
2282 /* All subsequent ARG directives are for a 16-bit stack,
2283 * far function call.
2285 StackSize = 2;
2286 StackPointer = "bp";
2287 ArgOffset = 4;
2288 LocalOffset = 0;
2289 } else if (nasm_stricmp(tline->text, "small") == 0) {
2290 /* All subsequent ARG directives are for a 16-bit stack,
2291 * far function call. We don't support near functions.
2293 StackSize = 2;
2294 StackPointer = "bp";
2295 ArgOffset = 6;
2296 LocalOffset = 0;
2297 } else {
2298 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2299 free_tlist(origline);
2300 return DIRECTIVE_FOUND;
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
2305 case PP_ARG:
2306 /* TASM like ARG directive to define arguments to functions, in
2307 * the following form:
2309 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2311 offset = ArgOffset;
2312 do {
2313 char *arg, directive[256];
2314 int size = StackSize;
2316 /* Find the argument name */
2317 tline = tline->next;
2318 if (tline && tline->type == TOK_WHITESPACE)
2319 tline = tline->next;
2320 if (!tline || tline->type != TOK_ID) {
2321 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2325 arg = tline->text;
2327 /* Find the argument size type */
2328 tline = tline->next;
2329 if (!tline || tline->type != TOK_OTHER
2330 || tline->text[0] != ':') {
2331 error(ERR_NONFATAL,
2332 "Syntax error processing `%%arg' directive");
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2336 tline = tline->next;
2337 if (!tline || tline->type != TOK_ID) {
2338 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2339 free_tlist(origline);
2340 return DIRECTIVE_FOUND;
2343 /* Allow macro expansion of type parameter */
2344 tt = tokenize(tline->text);
2345 tt = expand_smacro(tt);
2346 size = parse_size(tt->text);
2347 if (!size) {
2348 error(ERR_NONFATAL,
2349 "Invalid size type for `%%arg' missing directive");
2350 free_tlist(tt);
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2354 free_tlist(tt);
2356 /* Round up to even stack slots */
2357 size = ALIGN(size, StackSize);
2359 /* Now define the macro for the argument */
2360 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2361 arg, StackPointer, offset);
2362 do_directive(tokenize(directive));
2363 offset += size;
2365 /* Move to the next argument in the list */
2366 tline = tline->next;
2367 if (tline && tline->type == TOK_WHITESPACE)
2368 tline = tline->next;
2369 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2370 ArgOffset = offset;
2371 free_tlist(origline);
2372 return DIRECTIVE_FOUND;
2374 case PP_LOCAL:
2375 /* TASM like LOCAL directive to define local variables for a
2376 * function, in the following form:
2378 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2380 * The '= LocalSize' at the end is ignored by NASM, but is
2381 * required by TASM to define the local parameter size (and used
2382 * by the TASM macro package).
2384 offset = LocalOffset;
2385 do {
2386 char *local, directive[256];
2387 int size = StackSize;
2389 /* Find the argument name */
2390 tline = tline->next;
2391 if (tline && tline->type == TOK_WHITESPACE)
2392 tline = tline->next;
2393 if (!tline || tline->type != TOK_ID) {
2394 error(ERR_NONFATAL,
2395 "`%%local' missing argument parameter");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2399 local = tline->text;
2401 /* Find the argument size type */
2402 tline = tline->next;
2403 if (!tline || tline->type != TOK_OTHER
2404 || tline->text[0] != ':') {
2405 error(ERR_NONFATAL,
2406 "Syntax error processing `%%local' directive");
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2410 tline = tline->next;
2411 if (!tline || tline->type != TOK_ID) {
2412 error(ERR_NONFATAL,
2413 "`%%local' missing size type parameter");
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2418 /* Allow macro expansion of type parameter */
2419 tt = tokenize(tline->text);
2420 tt = expand_smacro(tt);
2421 size = parse_size(tt->text);
2422 if (!size) {
2423 error(ERR_NONFATAL,
2424 "Invalid size type for `%%local' missing directive");
2425 free_tlist(tt);
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2429 free_tlist(tt);
2431 /* Round up to even stack slots */
2432 size = ALIGN(size, StackSize);
2434 offset += size; /* Negative offset, increment before */
2436 /* Now define the macro for the argument */
2437 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2438 local, StackPointer, offset);
2439 do_directive(tokenize(directive));
2441 /* Now define the assign to setup the enter_c macro correctly */
2442 snprintf(directive, sizeof(directive),
2443 "%%assign %%$localsize %%$localsize+%d", size);
2444 do_directive(tokenize(directive));
2446 /* Move to the next argument in the list */
2447 tline = tline->next;
2448 if (tline && tline->type == TOK_WHITESPACE)
2449 tline = tline->next;
2450 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2451 LocalOffset = offset;
2452 free_tlist(origline);
2453 return DIRECTIVE_FOUND;
2455 case PP_CLEAR:
2456 if (tline->next)
2457 error(ERR_WARNING|ERR_PASS1,
2458 "trailing garbage after `%%clear' ignored");
2459 free_macros();
2460 init_macros();
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2464 case PP_DEPEND:
2465 t = tline->next = expand_smacro(tline->next);
2466 skip_white_(t);
2467 if (!t || (t->type != TOK_STRING &&
2468 t->type != TOK_INTERNAL_STRING)) {
2469 error(ERR_NONFATAL, "`%%depend' expects a file name");
2470 free_tlist(origline);
2471 return DIRECTIVE_FOUND; /* but we did _something_ */
2473 if (t->next)
2474 error(ERR_WARNING|ERR_PASS1,
2475 "trailing garbage after `%%depend' ignored");
2476 p = t->text;
2477 if (t->type != TOK_INTERNAL_STRING)
2478 nasm_unquote_cstr(p, i);
2479 if (dephead && !in_list(*dephead, p)) {
2480 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2481 sl->next = NULL;
2482 strcpy(sl->str, p);
2483 *deptail = sl;
2484 deptail = &sl->next;
2486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
2489 case PP_INCLUDE:
2490 t = tline->next = expand_smacro(tline->next);
2491 skip_white_(t);
2493 if (!t || (t->type != TOK_STRING &&
2494 t->type != TOK_INTERNAL_STRING)) {
2495 error(ERR_NONFATAL, "`%%include' expects a file name");
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND; /* but we did _something_ */
2499 if (t->next)
2500 error(ERR_WARNING|ERR_PASS1,
2501 "trailing garbage after `%%include' ignored");
2502 p = t->text;
2503 if (t->type != TOK_INTERNAL_STRING)
2504 nasm_unquote_cstr(p, i);
2505 inc = nasm_malloc(sizeof(Include));
2506 inc->next = istk;
2507 inc->conds = NULL;
2508 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2509 if (!inc->fp) {
2510 /* -MG given but file not found */
2511 nasm_free(inc);
2512 } else {
2513 inc->fname = src_set_fname(nasm_strdup(p));
2514 inc->lineno = src_set_linnum(0);
2515 inc->lineinc = 1;
2516 inc->expansion = NULL;
2517 inc->mstk = NULL;
2518 istk = inc;
2519 list->uplevel(LIST_INCLUDE);
2521 free_tlist(origline);
2522 return DIRECTIVE_FOUND;
2524 case PP_USE:
2526 static macros_t *use_pkg;
2527 const char *pkg_macro = NULL;
2529 tline = tline->next;
2530 skip_white_(tline);
2531 tline = expand_id(tline);
2533 if (!tline || (tline->type != TOK_STRING &&
2534 tline->type != TOK_INTERNAL_STRING &&
2535 tline->type != TOK_ID)) {
2536 error(ERR_NONFATAL, "`%%use' expects a package name");
2537 free_tlist(origline);
2538 return DIRECTIVE_FOUND; /* but we did _something_ */
2540 if (tline->next)
2541 error(ERR_WARNING|ERR_PASS1,
2542 "trailing garbage after `%%use' ignored");
2543 if (tline->type == TOK_STRING)
2544 nasm_unquote_cstr(tline->text, i);
2545 use_pkg = nasm_stdmac_find_package(tline->text);
2546 if (!use_pkg)
2547 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2548 else
2549 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2550 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2551 /* Not already included, go ahead and include it */
2552 stdmacpos = use_pkg;
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2557 case PP_PUSH:
2558 case PP_REPL:
2559 case PP_POP:
2560 tline = tline->next;
2561 skip_white_(tline);
2562 tline = expand_id(tline);
2563 if (tline) {
2564 if (!tok_type_(tline, TOK_ID)) {
2565 error(ERR_NONFATAL, "`%s' expects a context identifier",
2566 pp_directives[i]);
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND; /* but we did _something_ */
2570 if (tline->next)
2571 error(ERR_WARNING|ERR_PASS1,
2572 "trailing garbage after `%s' ignored",
2573 pp_directives[i]);
2574 p = nasm_strdup(tline->text);
2575 } else {
2576 p = NULL; /* Anonymous */
2579 if (i == PP_PUSH) {
2580 ctx = nasm_malloc(sizeof(Context));
2581 ctx->next = cstk;
2582 hash_init(&ctx->localmac, HASH_SMALL);
2583 ctx->name = p;
2584 ctx->number = unique++;
2585 cstk = ctx;
2586 } else {
2587 /* %pop or %repl */
2588 if (!cstk) {
2589 error(ERR_NONFATAL, "`%s': context stack is empty",
2590 pp_directives[i]);
2591 } else if (i == PP_POP) {
2592 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2593 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2594 "expected %s",
2595 cstk->name ? cstk->name : "anonymous", p);
2596 else
2597 ctx_pop();
2598 } else {
2599 /* i == PP_REPL */
2600 nasm_free(cstk->name);
2601 cstk->name = p;
2602 p = NULL;
2604 nasm_free(p);
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND;
2608 case PP_FATAL:
2609 severity = ERR_FATAL;
2610 goto issue_error;
2611 case PP_ERROR:
2612 severity = ERR_NONFATAL;
2613 goto issue_error;
2614 case PP_WARNING:
2615 severity = ERR_WARNING|ERR_WARN_USER;
2616 goto issue_error;
2618 issue_error:
2620 /* Only error out if this is the final pass */
2621 if (pass != 2 && i != PP_FATAL)
2622 return DIRECTIVE_FOUND;
2624 tline->next = expand_smacro(tline->next);
2625 tline = tline->next;
2626 skip_white_(tline);
2627 t = tline ? tline->next : NULL;
2628 skip_white_(t);
2629 if (tok_type_(tline, TOK_STRING) && !t) {
2630 /* The line contains only a quoted string */
2631 p = tline->text;
2632 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2633 error(severity, "%s", p);
2634 } else {
2635 /* Not a quoted string, or more than a quoted string */
2636 p = detoken(tline, false);
2637 error(severity, "%s", p);
2638 nasm_free(p);
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2644 CASE_PP_IF:
2645 if (istk->conds && !emitting(istk->conds->state))
2646 j = COND_NEVER;
2647 else {
2648 j = if_condition(tline->next, i);
2649 tline->next = NULL; /* it got freed */
2650 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2652 cond = nasm_malloc(sizeof(Cond));
2653 cond->next = istk->conds;
2654 cond->state = j;
2655 istk->conds = cond;
2656 if(istk->mstk)
2657 istk->mstk->condcnt ++;
2658 free_tlist(origline);
2659 return DIRECTIVE_FOUND;
2661 CASE_PP_ELIF:
2662 if (!istk->conds)
2663 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2664 switch(istk->conds->state) {
2665 case COND_IF_TRUE:
2666 istk->conds->state = COND_DONE;
2667 break;
2669 case COND_DONE:
2670 case COND_NEVER:
2671 break;
2673 case COND_ELSE_TRUE:
2674 case COND_ELSE_FALSE:
2675 error_precond(ERR_WARNING|ERR_PASS1,
2676 "`%%elif' after `%%else' ignored");
2677 istk->conds->state = COND_NEVER;
2678 break;
2680 case COND_IF_FALSE:
2682 * IMPORTANT: In the case of %if, we will already have
2683 * called expand_mmac_params(); however, if we're
2684 * processing an %elif we must have been in a
2685 * non-emitting mode, which would have inhibited
2686 * the normal invocation of expand_mmac_params().
2687 * Therefore, we have to do it explicitly here.
2689 j = if_condition(expand_mmac_params(tline->next), i);
2690 tline->next = NULL; /* it got freed */
2691 istk->conds->state =
2692 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2693 break;
2695 free_tlist(origline);
2696 return DIRECTIVE_FOUND;
2698 case PP_ELSE:
2699 if (tline->next)
2700 error_precond(ERR_WARNING|ERR_PASS1,
2701 "trailing garbage after `%%else' ignored");
2702 if (!istk->conds)
2703 error(ERR_FATAL, "`%%else': no matching `%%if'");
2704 switch(istk->conds->state) {
2705 case COND_IF_TRUE:
2706 case COND_DONE:
2707 istk->conds->state = COND_ELSE_FALSE;
2708 break;
2710 case COND_NEVER:
2711 break;
2713 case COND_IF_FALSE:
2714 istk->conds->state = COND_ELSE_TRUE;
2715 break;
2717 case COND_ELSE_TRUE:
2718 case COND_ELSE_FALSE:
2719 error_precond(ERR_WARNING|ERR_PASS1,
2720 "`%%else' after `%%else' ignored.");
2721 istk->conds->state = COND_NEVER;
2722 break;
2724 free_tlist(origline);
2725 return DIRECTIVE_FOUND;
2727 case PP_ENDIF:
2728 if (tline->next)
2729 error_precond(ERR_WARNING|ERR_PASS1,
2730 "trailing garbage after `%%endif' ignored");
2731 if (!istk->conds)
2732 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2733 cond = istk->conds;
2734 istk->conds = cond->next;
2735 nasm_free(cond);
2736 if(istk->mstk)
2737 istk->mstk->condcnt --;
2738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
2741 case PP_RMACRO:
2742 case PP_IRMACRO:
2743 case PP_MACRO:
2744 case PP_IMACRO:
2745 if (defining) {
2746 error(ERR_FATAL, "`%s': already defining a macro",
2747 pp_directives[i]);
2748 return DIRECTIVE_FOUND;
2750 defining = nasm_malloc(sizeof(MMacro));
2751 defining->max_depth =
2752 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2753 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2754 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2755 nasm_free(defining);
2756 defining = NULL;
2757 return DIRECTIVE_FOUND;
2760 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2761 while (mmac) {
2762 if (!strcmp(mmac->name, defining->name) &&
2763 (mmac->nparam_min <= defining->nparam_max
2764 || defining->plus)
2765 && (defining->nparam_min <= mmac->nparam_max
2766 || mmac->plus)) {
2767 error(ERR_WARNING|ERR_PASS1,
2768 "redefining multi-line macro `%s'", defining->name);
2769 return DIRECTIVE_FOUND;
2771 mmac = mmac->next;
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
2776 case PP_ENDM:
2777 case PP_ENDMACRO:
2778 if (! (defining && defining->name)) {
2779 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2780 return DIRECTIVE_FOUND;
2782 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2783 defining->next = *mmhead;
2784 *mmhead = defining;
2785 defining = NULL;
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2789 case PP_EXITMACRO:
2791 * We must search along istk->expansion until we hit a
2792 * macro-end marker for a macro with a name. Then we
2793 * bypass all lines between exitmacro and endmacro.
2795 list_for_each(l, istk->expansion)
2796 if (l->finishes && l->finishes->name)
2797 break;
2799 if (l) {
2801 * Remove all conditional entries relative to this
2802 * macro invocation. (safe to do in this context)
2804 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2805 cond = istk->conds;
2806 istk->conds = cond->next;
2807 nasm_free(cond);
2809 istk->expansion = l;
2810 } else {
2811 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2813 free_tlist(origline);
2814 return DIRECTIVE_FOUND;
2816 case PP_UNMACRO:
2817 case PP_UNIMACRO:
2819 MMacro **mmac_p;
2820 MMacro spec;
2822 spec.casesense = (i == PP_UNMACRO);
2823 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2824 return DIRECTIVE_FOUND;
2826 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2827 while (mmac_p && *mmac_p) {
2828 mmac = *mmac_p;
2829 if (mmac->casesense == spec.casesense &&
2830 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2831 mmac->nparam_min == spec.nparam_min &&
2832 mmac->nparam_max == spec.nparam_max &&
2833 mmac->plus == spec.plus) {
2834 *mmac_p = mmac->next;
2835 free_mmacro(mmac);
2836 } else {
2837 mmac_p = &mmac->next;
2840 free_tlist(origline);
2841 free_tlist(spec.dlist);
2842 return DIRECTIVE_FOUND;
2845 case PP_ROTATE:
2846 if (tline->next && tline->next->type == TOK_WHITESPACE)
2847 tline = tline->next;
2848 if (!tline->next) {
2849 free_tlist(origline);
2850 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2851 return DIRECTIVE_FOUND;
2853 t = expand_smacro(tline->next);
2854 tline->next = NULL;
2855 free_tlist(origline);
2856 tline = t;
2857 tptr = &t;
2858 tokval.t_type = TOKEN_INVALID;
2859 evalresult =
2860 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2861 free_tlist(tline);
2862 if (!evalresult)
2863 return DIRECTIVE_FOUND;
2864 if (tokval.t_type)
2865 error(ERR_WARNING|ERR_PASS1,
2866 "trailing garbage after expression ignored");
2867 if (!is_simple(evalresult)) {
2868 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2869 return DIRECTIVE_FOUND;
2871 mmac = istk->mstk;
2872 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2873 mmac = mmac->next_active;
2874 if (!mmac) {
2875 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2876 } else if (mmac->nparam == 0) {
2877 error(ERR_NONFATAL,
2878 "`%%rotate' invoked within macro without parameters");
2879 } else {
2880 int rotate = mmac->rotate + reloc_value(evalresult);
2882 rotate %= (int)mmac->nparam;
2883 if (rotate < 0)
2884 rotate += mmac->nparam;
2886 mmac->rotate = rotate;
2888 return DIRECTIVE_FOUND;
2890 case PP_REP:
2891 nolist = false;
2892 do {
2893 tline = tline->next;
2894 } while (tok_type_(tline, TOK_WHITESPACE));
2896 if (tok_type_(tline, TOK_ID) &&
2897 nasm_stricmp(tline->text, ".nolist") == 0) {
2898 nolist = true;
2899 do {
2900 tline = tline->next;
2901 } while (tok_type_(tline, TOK_WHITESPACE));
2904 if (tline) {
2905 t = expand_smacro(tline);
2906 tptr = &t;
2907 tokval.t_type = TOKEN_INVALID;
2908 evalresult =
2909 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2910 if (!evalresult) {
2911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
2914 if (tokval.t_type)
2915 error(ERR_WARNING|ERR_PASS1,
2916 "trailing garbage after expression ignored");
2917 if (!is_simple(evalresult)) {
2918 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2919 return DIRECTIVE_FOUND;
2921 count = reloc_value(evalresult);
2922 if (count >= REP_LIMIT) {
2923 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2924 count = 0;
2925 } else
2926 count++;
2927 } else {
2928 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2929 count = 0;
2931 free_tlist(origline);
2933 tmp_defining = defining;
2934 defining = nasm_malloc(sizeof(MMacro));
2935 defining->prev = NULL;
2936 defining->name = NULL; /* flags this macro as a %rep block */
2937 defining->casesense = false;
2938 defining->plus = false;
2939 defining->nolist = nolist;
2940 defining->in_progress = count;
2941 defining->max_depth = 0;
2942 defining->nparam_min = defining->nparam_max = 0;
2943 defining->defaults = NULL;
2944 defining->dlist = NULL;
2945 defining->expansion = NULL;
2946 defining->next_active = istk->mstk;
2947 defining->rep_nest = tmp_defining;
2948 return DIRECTIVE_FOUND;
2950 case PP_ENDREP:
2951 if (!defining || defining->name) {
2952 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2953 return DIRECTIVE_FOUND;
2957 * Now we have a "macro" defined - although it has no name
2958 * and we won't be entering it in the hash tables - we must
2959 * push a macro-end marker for it on to istk->expansion.
2960 * After that, it will take care of propagating itself (a
2961 * macro-end marker line for a macro which is really a %rep
2962 * block will cause the macro to be re-expanded, complete
2963 * with another macro-end marker to ensure the process
2964 * continues) until the whole expansion is forcibly removed
2965 * from istk->expansion by a %exitrep.
2967 l = nasm_malloc(sizeof(Line));
2968 l->next = istk->expansion;
2969 l->finishes = defining;
2970 l->first = NULL;
2971 istk->expansion = l;
2973 istk->mstk = defining;
2975 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2976 tmp_defining = defining;
2977 defining = defining->rep_nest;
2978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
2981 case PP_EXITREP:
2983 * We must search along istk->expansion until we hit a
2984 * macro-end marker for a macro with no name. Then we set
2985 * its `in_progress' flag to 0.
2987 list_for_each(l, istk->expansion)
2988 if (l->finishes && !l->finishes->name)
2989 break;
2991 if (l)
2992 l->finishes->in_progress = 1;
2993 else
2994 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
2998 case PP_XDEFINE:
2999 case PP_IXDEFINE:
3000 case PP_DEFINE:
3001 case PP_IDEFINE:
3002 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3004 tline = tline->next;
3005 skip_white_(tline);
3006 tline = expand_id(tline);
3007 if (!tline || (tline->type != TOK_ID &&
3008 (tline->type != TOK_PREPROC_ID ||
3009 tline->text[1] != '$'))) {
3010 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3011 pp_directives[i]);
3012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
3016 ctx = get_ctx(tline->text, &mname, false);
3017 last = tline;
3018 param_start = tline = tline->next;
3019 nparam = 0;
3021 /* Expand the macro definition now for %xdefine and %ixdefine */
3022 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3023 tline = expand_smacro(tline);
3025 if (tok_is_(tline, "(")) {
3027 * This macro has parameters.
3030 tline = tline->next;
3031 while (1) {
3032 skip_white_(tline);
3033 if (!tline) {
3034 error(ERR_NONFATAL, "parameter identifier expected");
3035 free_tlist(origline);
3036 return DIRECTIVE_FOUND;
3038 if (tline->type != TOK_ID) {
3039 error(ERR_NONFATAL,
3040 "`%s': parameter identifier expected",
3041 tline->text);
3042 free_tlist(origline);
3043 return DIRECTIVE_FOUND;
3045 tline->type = TOK_SMAC_PARAM + nparam++;
3046 tline = tline->next;
3047 skip_white_(tline);
3048 if (tok_is_(tline, ",")) {
3049 tline = tline->next;
3050 } else {
3051 if (!tok_is_(tline, ")")) {
3052 error(ERR_NONFATAL,
3053 "`)' expected to terminate macro template");
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3057 break;
3060 last = tline;
3061 tline = tline->next;
3063 if (tok_type_(tline, TOK_WHITESPACE))
3064 last = tline, tline = tline->next;
3065 macro_start = NULL;
3066 last->next = NULL;
3067 t = tline;
3068 while (t) {
3069 if (t->type == TOK_ID) {
3070 list_for_each(tt, param_start)
3071 if (tt->type >= TOK_SMAC_PARAM &&
3072 !strcmp(tt->text, t->text))
3073 t->type = tt->type;
3075 tt = t->next;
3076 t->next = macro_start;
3077 macro_start = t;
3078 t = tt;
3081 * Good. We now have a macro name, a parameter count, and a
3082 * token list (in reverse order) for an expansion. We ought
3083 * to be OK just to create an SMacro, store it, and let
3084 * free_tlist have the rest of the line (which we have
3085 * carefully re-terminated after chopping off the expansion
3086 * from the end).
3088 define_smacro(ctx, mname, casesense, nparam, macro_start);
3089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
3092 case PP_UNDEF:
3093 tline = tline->next;
3094 skip_white_(tline);
3095 tline = expand_id(tline);
3096 if (!tline || (tline->type != TOK_ID &&
3097 (tline->type != TOK_PREPROC_ID ||
3098 tline->text[1] != '$'))) {
3099 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3100 free_tlist(origline);
3101 return DIRECTIVE_FOUND;
3103 if (tline->next) {
3104 error(ERR_WARNING|ERR_PASS1,
3105 "trailing garbage after macro name ignored");
3108 /* Find the context that symbol belongs to */
3109 ctx = get_ctx(tline->text, &mname, false);
3110 undef_smacro(ctx, mname);
3111 free_tlist(origline);
3112 return DIRECTIVE_FOUND;
3114 case PP_DEFSTR:
3115 case PP_IDEFSTR:
3116 casesense = (i == PP_DEFSTR);
3118 tline = tline->next;
3119 skip_white_(tline);
3120 tline = expand_id(tline);
3121 if (!tline || (tline->type != TOK_ID &&
3122 (tline->type != TOK_PREPROC_ID ||
3123 tline->text[1] != '$'))) {
3124 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3125 pp_directives[i]);
3126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
3130 ctx = get_ctx(tline->text, &mname, false);
3131 last = tline;
3132 tline = expand_smacro(tline->next);
3133 last->next = NULL;
3135 while (tok_type_(tline, TOK_WHITESPACE))
3136 tline = delete_Token(tline);
3138 p = detoken(tline, false);
3139 macro_start = nasm_malloc(sizeof(*macro_start));
3140 macro_start->next = NULL;
3141 macro_start->text = nasm_quote(p, strlen(p));
3142 macro_start->type = TOK_STRING;
3143 macro_start->a.mac = NULL;
3144 nasm_free(p);
3147 * We now have a macro name, an implicit parameter count of
3148 * zero, and a string token to use as an expansion. Create
3149 * and store an SMacro.
3151 define_smacro(ctx, mname, casesense, 0, macro_start);
3152 free_tlist(origline);
3153 return DIRECTIVE_FOUND;
3155 case PP_DEFTOK:
3156 case PP_IDEFTOK:
3157 casesense = (i == PP_DEFTOK);
3159 tline = tline->next;
3160 skip_white_(tline);
3161 tline = expand_id(tline);
3162 if (!tline || (tline->type != TOK_ID &&
3163 (tline->type != TOK_PREPROC_ID ||
3164 tline->text[1] != '$'))) {
3165 error(ERR_NONFATAL,
3166 "`%s' expects a macro identifier as first parameter",
3167 pp_directives[i]);
3168 free_tlist(origline);
3169 return DIRECTIVE_FOUND;
3171 ctx = get_ctx(tline->text, &mname, false);
3172 last = tline;
3173 tline = expand_smacro(tline->next);
3174 last->next = NULL;
3176 t = tline;
3177 while (tok_type_(t, TOK_WHITESPACE))
3178 t = t->next;
3179 /* t should now point to the string */
3180 if (!tok_type_(t, TOK_STRING)) {
3181 error(ERR_NONFATAL,
3182 "`%s` requires string as second parameter",
3183 pp_directives[i]);
3184 free_tlist(tline);
3185 free_tlist(origline);
3186 return DIRECTIVE_FOUND;
3189 nasm_unquote_cstr(t->text, i);
3190 macro_start = tokenize(t->text);
3193 * We now have a macro name, an implicit parameter count of
3194 * zero, and a numeric token to use as an expansion. Create
3195 * and store an SMacro.
3197 define_smacro(ctx, mname, casesense, 0, macro_start);
3198 free_tlist(tline);
3199 free_tlist(origline);
3200 return DIRECTIVE_FOUND;
3202 case PP_PATHSEARCH:
3204 FILE *fp;
3205 StrList *xsl = NULL;
3206 StrList **xst = &xsl;
3208 casesense = true;
3210 tline = tline->next;
3211 skip_white_(tline);
3212 tline = expand_id(tline);
3213 if (!tline || (tline->type != TOK_ID &&
3214 (tline->type != TOK_PREPROC_ID ||
3215 tline->text[1] != '$'))) {
3216 error(ERR_NONFATAL,
3217 "`%%pathsearch' expects a macro identifier as first parameter");
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3221 ctx = get_ctx(tline->text, &mname, false);
3222 last = tline;
3223 tline = expand_smacro(tline->next);
3224 last->next = NULL;
3226 t = tline;
3227 while (tok_type_(t, TOK_WHITESPACE))
3228 t = t->next;
3230 if (!t || (t->type != TOK_STRING &&
3231 t->type != TOK_INTERNAL_STRING)) {
3232 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3233 free_tlist(tline);
3234 free_tlist(origline);
3235 return DIRECTIVE_FOUND; /* but we did _something_ */
3237 if (t->next)
3238 error(ERR_WARNING|ERR_PASS1,
3239 "trailing garbage after `%%pathsearch' ignored");
3240 p = t->text;
3241 if (t->type != TOK_INTERNAL_STRING)
3242 nasm_unquote(p, NULL);
3244 fp = inc_fopen(p, &xsl, &xst, true);
3245 if (fp) {
3246 p = xsl->str;
3247 fclose(fp); /* Don't actually care about the file */
3249 macro_start = nasm_malloc(sizeof(*macro_start));
3250 macro_start->next = NULL;
3251 macro_start->text = nasm_quote(p, strlen(p));
3252 macro_start->type = TOK_STRING;
3253 macro_start->a.mac = NULL;
3254 if (xsl)
3255 nasm_free(xsl);
3258 * We now have a macro name, an implicit parameter count of
3259 * zero, and a string token to use as an expansion. Create
3260 * and store an SMacro.
3262 define_smacro(ctx, mname, casesense, 0, macro_start);
3263 free_tlist(tline);
3264 free_tlist(origline);
3265 return DIRECTIVE_FOUND;
3268 case PP_STRLEN:
3269 casesense = true;
3271 tline = tline->next;
3272 skip_white_(tline);
3273 tline = expand_id(tline);
3274 if (!tline || (tline->type != TOK_ID &&
3275 (tline->type != TOK_PREPROC_ID ||
3276 tline->text[1] != '$'))) {
3277 error(ERR_NONFATAL,
3278 "`%%strlen' expects a macro identifier as first parameter");
3279 free_tlist(origline);
3280 return DIRECTIVE_FOUND;
3282 ctx = get_ctx(tline->text, &mname, false);
3283 last = tline;
3284 tline = expand_smacro(tline->next);
3285 last->next = NULL;
3287 t = tline;
3288 while (tok_type_(t, TOK_WHITESPACE))
3289 t = t->next;
3290 /* t should now point to the string */
3291 if (!tok_type_(t, TOK_STRING)) {
3292 error(ERR_NONFATAL,
3293 "`%%strlen` requires string as second parameter");
3294 free_tlist(tline);
3295 free_tlist(origline);
3296 return DIRECTIVE_FOUND;
3299 macro_start = nasm_malloc(sizeof(*macro_start));
3300 macro_start->next = NULL;
3301 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3302 macro_start->a.mac = NULL;
3305 * We now have a macro name, an implicit parameter count of
3306 * zero, and a numeric token to use as an expansion. Create
3307 * and store an SMacro.
3309 define_smacro(ctx, mname, casesense, 0, macro_start);
3310 free_tlist(tline);
3311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
3314 case PP_STRCAT:
3315 casesense = true;
3317 tline = tline->next;
3318 skip_white_(tline);
3319 tline = expand_id(tline);
3320 if (!tline || (tline->type != TOK_ID &&
3321 (tline->type != TOK_PREPROC_ID ||
3322 tline->text[1] != '$'))) {
3323 error(ERR_NONFATAL,
3324 "`%%strcat' expects a macro identifier as first parameter");
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3328 ctx = get_ctx(tline->text, &mname, false);
3329 last = tline;
3330 tline = expand_smacro(tline->next);
3331 last->next = NULL;
3333 len = 0;
3334 list_for_each(t, tline) {
3335 switch (t->type) {
3336 case TOK_WHITESPACE:
3337 break;
3338 case TOK_STRING:
3339 len += t->a.len = nasm_unquote(t->text, NULL);
3340 break;
3341 case TOK_OTHER:
3342 if (!strcmp(t->text, ",")) /* permit comma separators */
3343 break;
3344 /* else fall through */
3345 default:
3346 error(ERR_NONFATAL,
3347 "non-string passed to `%%strcat' (%d)", t->type);
3348 free_tlist(tline);
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
3354 p = pp = nasm_malloc(len);
3355 list_for_each(t, tline) {
3356 if (t->type == TOK_STRING) {
3357 memcpy(p, t->text, t->a.len);
3358 p += t->a.len;
3363 * We now have a macro name, an implicit parameter count of
3364 * zero, and a numeric token to use as an expansion. Create
3365 * and store an SMacro.
3367 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3368 macro_start->text = nasm_quote(pp, len);
3369 nasm_free(pp);
3370 define_smacro(ctx, mname, casesense, 0, macro_start);
3371 free_tlist(tline);
3372 free_tlist(origline);
3373 return DIRECTIVE_FOUND;
3375 case PP_SUBSTR:
3377 int64_t a1, a2;
3378 size_t len;
3380 casesense = true;
3382 tline = tline->next;
3383 skip_white_(tline);
3384 tline = expand_id(tline);
3385 if (!tline || (tline->type != TOK_ID &&
3386 (tline->type != TOK_PREPROC_ID ||
3387 tline->text[1] != '$'))) {
3388 error(ERR_NONFATAL,
3389 "`%%substr' expects a macro identifier as first parameter");
3390 free_tlist(origline);
3391 return DIRECTIVE_FOUND;
3393 ctx = get_ctx(tline->text, &mname, false);
3394 last = tline;
3395 tline = expand_smacro(tline->next);
3396 last->next = NULL;
3398 if (tline) /* skip expanded id */
3399 t = tline->next;
3400 while (tok_type_(t, TOK_WHITESPACE))
3401 t = t->next;
3403 /* t should now point to the string */
3404 if (!tok_type_(t, TOK_STRING)) {
3405 error(ERR_NONFATAL,
3406 "`%%substr` requires string as second parameter");
3407 free_tlist(tline);
3408 free_tlist(origline);
3409 return DIRECTIVE_FOUND;
3412 tt = t->next;
3413 tptr = &tt;
3414 tokval.t_type = TOKEN_INVALID;
3415 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3416 pass, error, NULL);
3417 if (!evalresult) {
3418 free_tlist(tline);
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421 } else if (!is_simple(evalresult)) {
3422 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3423 free_tlist(tline);
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3427 a1 = evalresult->value-1;
3429 while (tok_type_(tt, TOK_WHITESPACE))
3430 tt = tt->next;
3431 if (!tt) {
3432 a2 = 1; /* Backwards compatibility: one character */
3433 } else {
3434 tokval.t_type = TOKEN_INVALID;
3435 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3436 pass, error, NULL);
3437 if (!evalresult) {
3438 free_tlist(tline);
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
3441 } else if (!is_simple(evalresult)) {
3442 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3443 free_tlist(tline);
3444 free_tlist(origline);
3445 return DIRECTIVE_FOUND;
3447 a2 = evalresult->value;
3450 len = nasm_unquote(t->text, NULL);
3451 if (a2 < 0)
3452 a2 = a2+1+len-a1;
3453 if (a1+a2 > (int64_t)len)
3454 a2 = len-a1;
3456 macro_start = nasm_malloc(sizeof(*macro_start));
3457 macro_start->next = NULL;
3458 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3459 macro_start->type = TOK_STRING;
3460 macro_start->a.mac = NULL;
3463 * We now have a macro name, an implicit parameter count of
3464 * zero, and a numeric token to use as an expansion. Create
3465 * and store an SMacro.
3467 define_smacro(ctx, mname, casesense, 0, macro_start);
3468 free_tlist(tline);
3469 free_tlist(origline);
3470 return DIRECTIVE_FOUND;
3473 case PP_ASSIGN:
3474 case PP_IASSIGN:
3475 casesense = (i == PP_ASSIGN);
3477 tline = tline->next;
3478 skip_white_(tline);
3479 tline = expand_id(tline);
3480 if (!tline || (tline->type != TOK_ID &&
3481 (tline->type != TOK_PREPROC_ID ||
3482 tline->text[1] != '$'))) {
3483 error(ERR_NONFATAL,
3484 "`%%%sassign' expects a macro identifier",
3485 (i == PP_IASSIGN ? "i" : ""));
3486 free_tlist(origline);
3487 return DIRECTIVE_FOUND;
3489 ctx = get_ctx(tline->text, &mname, false);
3490 last = tline;
3491 tline = expand_smacro(tline->next);
3492 last->next = NULL;
3494 t = tline;
3495 tptr = &t;
3496 tokval.t_type = TOKEN_INVALID;
3497 evalresult =
3498 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3499 free_tlist(tline);
3500 if (!evalresult) {
3501 free_tlist(origline);
3502 return DIRECTIVE_FOUND;
3505 if (tokval.t_type)
3506 error(ERR_WARNING|ERR_PASS1,
3507 "trailing garbage after expression ignored");
3509 if (!is_simple(evalresult)) {
3510 error(ERR_NONFATAL,
3511 "non-constant value given to `%%%sassign'",
3512 (i == PP_IASSIGN ? "i" : ""));
3513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
3517 macro_start = nasm_malloc(sizeof(*macro_start));
3518 macro_start->next = NULL;
3519 make_tok_num(macro_start, reloc_value(evalresult));
3520 macro_start->a.mac = NULL;
3523 * We now have a macro name, an implicit parameter count of
3524 * zero, and a numeric token to use as an expansion. Create
3525 * and store an SMacro.
3527 define_smacro(ctx, mname, casesense, 0, macro_start);
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3531 case PP_LINE:
3533 * Syntax is `%line nnn[+mmm] [filename]'
3535 tline = tline->next;
3536 skip_white_(tline);
3537 if (!tok_type_(tline, TOK_NUMBER)) {
3538 error(ERR_NONFATAL, "`%%line' expects line number");
3539 free_tlist(origline);
3540 return DIRECTIVE_FOUND;
3542 k = readnum(tline->text, &err);
3543 m = 1;
3544 tline = tline->next;
3545 if (tok_is_(tline, "+")) {
3546 tline = tline->next;
3547 if (!tok_type_(tline, TOK_NUMBER)) {
3548 error(ERR_NONFATAL, "`%%line' expects line increment");
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3552 m = readnum(tline->text, &err);
3553 tline = tline->next;
3555 skip_white_(tline);
3556 src_set_linnum(k);
3557 istk->lineinc = m;
3558 if (tline) {
3559 nasm_free(src_set_fname(detoken(tline, false)));
3561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
3564 default:
3565 error(ERR_FATAL,
3566 "preprocessor directive `%s' not yet implemented",
3567 pp_directives[i]);
3568 return DIRECTIVE_FOUND;
3573 * Ensure that a macro parameter contains a condition code and
3574 * nothing else. Return the condition code index if so, or -1
3575 * otherwise.
3577 static int find_cc(Token * t)
3579 Token *tt;
3580 int i, j, k, m;
3582 if (!t)
3583 return -1; /* Probably a %+ without a space */
3585 skip_white_(t);
3586 if (t->type != TOK_ID)
3587 return -1;
3588 tt = t->next;
3589 skip_white_(tt);
3590 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3591 return -1;
3593 i = -1;
3594 j = ARRAY_SIZE(conditions);
3595 while (j - i > 1) {
3596 k = (j + i) / 2;
3597 m = nasm_stricmp(t->text, conditions[k]);
3598 if (m == 0) {
3599 i = k;
3600 j = -2;
3601 break;
3602 } else if (m < 0) {
3603 j = k;
3604 } else
3605 i = k;
3607 if (j != -2)
3608 return -1;
3609 return i;
3612 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3614 Token **tail, *t, *tt;
3615 Token **paste_head;
3616 bool did_paste = false;
3617 char *tmp;
3619 /* Now handle token pasting... */
3620 paste_head = NULL;
3621 tail = head;
3622 while ((t = *tail) && (tt = t->next)) {
3623 switch (t->type) {
3624 case TOK_WHITESPACE:
3625 if (tt->type == TOK_WHITESPACE) {
3626 /* Zap adjacent whitespace tokens */
3627 t->next = delete_Token(tt);
3628 } else {
3629 /* Do not advance paste_head here */
3630 tail = &t->next;
3632 break;
3633 case TOK_ID:
3634 case TOK_NUMBER:
3635 case TOK_FLOAT:
3637 size_t len = 0;
3638 char *tmp, *p;
3640 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3641 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3642 tt->type == TOK_OTHER)) {
3643 len += strlen(tt->text);
3644 tt = tt->next;
3648 * Now tt points to the first token after
3649 * the potential paste area...
3651 if (tt != t->next) {
3652 /* We have at least two tokens... */
3653 len += strlen(t->text);
3654 p = tmp = nasm_malloc(len+1);
3656 while (t != tt) {
3657 strcpy(p, t->text);
3658 p = strchr(p, '\0');
3659 t = delete_Token(t);
3662 t = *tail = tokenize(tmp);
3663 nasm_free(tmp);
3665 while (t->next) {
3666 tail = &t->next;
3667 t = t->next;
3669 t->next = tt; /* Attach the remaining token chain */
3671 did_paste = true;
3673 paste_head = tail;
3674 tail = &t->next;
3675 break;
3677 case TOK_PASTE: /* %+ */
3678 if (handle_paste_tokens) {
3679 /* Zap %+ and whitespace tokens to the right */
3680 while (t && (t->type == TOK_WHITESPACE ||
3681 t->type == TOK_PASTE))
3682 t = *tail = delete_Token(t);
3683 if (!paste_head || !t)
3684 break; /* Nothing to paste with */
3685 tail = paste_head;
3686 t = *tail;
3687 tt = t->next;
3688 while (tok_type_(tt, TOK_WHITESPACE))
3689 tt = t->next = delete_Token(tt);
3691 if (tt) {
3692 tmp = nasm_strcat(t->text, tt->text);
3693 delete_Token(t);
3694 tt = delete_Token(tt);
3695 t = *tail = tokenize(tmp);
3696 nasm_free(tmp);
3697 while (t->next) {
3698 tail = &t->next;
3699 t = t->next;
3701 t->next = tt; /* Attach the remaining token chain */
3702 did_paste = true;
3704 paste_head = tail;
3705 tail = &t->next;
3706 break;
3708 /* else fall through */
3709 default:
3710 tail = &t->next;
3711 if (!tok_type_(t->next, TOK_WHITESPACE))
3712 paste_head = tail;
3713 break;
3716 return did_paste;
3720 * expands to a list of tokens from %{x:y}
3722 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3724 Token *t = tline, **tt, *tm, *head;
3725 char *pos;
3726 int fst, lst, j, i;
3728 pos = strchr(tline->text, ':');
3729 nasm_assert(pos);
3731 lst = atoi(pos + 1);
3732 fst = atoi(tline->text + 1);
3735 * only macros params are accounted so
3736 * if someone passes %0 -- we reject such
3737 * value(s)
3739 if (lst == 0 || fst == 0)
3740 goto err;
3742 /* the values should be sane */
3743 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3744 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3745 goto err;
3747 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3748 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3750 /* counted from zero */
3751 fst--, lst--;
3754 * it will be at least one token
3756 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3757 t = new_Token(NULL, tm->type, tm->text, 0);
3758 head = t, tt = &t->next;
3759 if (fst < lst) {
3760 for (i = fst + 1; i <= lst; i++) {
3761 t = new_Token(NULL, TOK_OTHER, ",", 0);
3762 *tt = t, tt = &t->next;
3763 j = (i + mac->rotate) % mac->nparam;
3764 tm = mac->params[j];
3765 t = new_Token(NULL, tm->type, tm->text, 0);
3766 *tt = t, tt = &t->next;
3768 } else {
3769 for (i = fst - 1; i >= lst; i--) {
3770 t = new_Token(NULL, TOK_OTHER, ",", 0);
3771 *tt = t, tt = &t->next;
3772 j = (i + mac->rotate) % mac->nparam;
3773 tm = mac->params[j];
3774 t = new_Token(NULL, tm->type, tm->text, 0);
3775 *tt = t, tt = &t->next;
3779 *last = tt;
3780 return head;
3782 err:
3783 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3784 &tline->text[1]);
3785 return tline;
3789 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3790 * %-n) and MMacro-local identifiers (%%foo) as well as
3791 * macro indirection (%[...]) and range (%{..:..}).
3793 static Token *expand_mmac_params(Token * tline)
3795 Token *t, *tt, **tail, *thead;
3796 bool changed = false;
3797 char *pos;
3799 tail = &thead;
3800 thead = NULL;
3802 while (tline) {
3803 if (tline->type == TOK_PREPROC_ID &&
3804 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3805 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3806 tline->text[1] == '%')) {
3807 char *text = NULL;
3808 int type = 0, cc; /* type = 0 to placate optimisers */
3809 char tmpbuf[30];
3810 unsigned int n;
3811 int i;
3812 MMacro *mac;
3814 t = tline;
3815 tline = tline->next;
3817 mac = istk->mstk;
3818 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3819 mac = mac->next_active;
3820 if (!mac) {
3821 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3822 } else {
3823 pos = strchr(t->text, ':');
3824 if (!pos) {
3825 switch (t->text[1]) {
3827 * We have to make a substitution of one of the
3828 * forms %1, %-1, %+1, %%foo, %0.
3830 case '0':
3831 type = TOK_NUMBER;
3832 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3833 text = nasm_strdup(tmpbuf);
3834 break;
3835 case '%':
3836 type = TOK_ID;
3837 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3838 mac->unique);
3839 text = nasm_strcat(tmpbuf, t->text + 2);
3840 break;
3841 case '-':
3842 n = atoi(t->text + 2) - 1;
3843 if (n >= mac->nparam)
3844 tt = NULL;
3845 else {
3846 if (mac->nparam > 1)
3847 n = (n + mac->rotate) % mac->nparam;
3848 tt = mac->params[n];
3850 cc = find_cc(tt);
3851 if (cc == -1) {
3852 error(ERR_NONFATAL,
3853 "macro parameter %d is not a condition code",
3854 n + 1);
3855 text = NULL;
3856 } else {
3857 type = TOK_ID;
3858 if (inverse_ccs[cc] == -1) {
3859 error(ERR_NONFATAL,
3860 "condition code `%s' is not invertible",
3861 conditions[cc]);
3862 text = NULL;
3863 } else
3864 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3866 break;
3867 case '+':
3868 n = atoi(t->text + 2) - 1;
3869 if (n >= mac->nparam)
3870 tt = NULL;
3871 else {
3872 if (mac->nparam > 1)
3873 n = (n + mac->rotate) % mac->nparam;
3874 tt = mac->params[n];
3876 cc = find_cc(tt);
3877 if (cc == -1) {
3878 error(ERR_NONFATAL,
3879 "macro parameter %d is not a condition code",
3880 n + 1);
3881 text = NULL;
3882 } else {
3883 type = TOK_ID;
3884 text = nasm_strdup(conditions[cc]);
3886 break;
3887 default:
3888 n = atoi(t->text + 1) - 1;
3889 if (n >= mac->nparam)
3890 tt = NULL;
3891 else {
3892 if (mac->nparam > 1)
3893 n = (n + mac->rotate) % mac->nparam;
3894 tt = mac->params[n];
3896 if (tt) {
3897 for (i = 0; i < mac->paramlen[n]; i++) {
3898 *tail = new_Token(NULL, tt->type, tt->text, 0);
3899 tail = &(*tail)->next;
3900 tt = tt->next;
3903 text = NULL; /* we've done it here */
3904 break;
3906 } else {
3908 * seems we have a parameters range here
3910 Token *head, **last;
3911 head = expand_mmac_params_range(mac, t, &last);
3912 if (head != t) {
3913 *tail = head;
3914 *last = tline;
3915 tline = head;
3916 text = NULL;
3920 if (!text) {
3921 delete_Token(t);
3922 } else {
3923 *tail = t;
3924 tail = &t->next;
3925 t->type = type;
3926 nasm_free(t->text);
3927 t->text = text;
3928 t->a.mac = NULL;
3930 changed = true;
3931 continue;
3932 } else if (tline->type == TOK_INDIRECT) {
3933 t = tline;
3934 tline = tline->next;
3935 tt = tokenize(t->text);
3936 tt = expand_mmac_params(tt);
3937 tt = expand_smacro(tt);
3938 *tail = tt;
3939 while (tt) {
3940 tt->a.mac = NULL; /* Necessary? */
3941 tail = &tt->next;
3942 tt = tt->next;
3944 delete_Token(t);
3945 changed = true;
3946 } else {
3947 t = *tail = tline;
3948 tline = tline->next;
3949 t->a.mac = NULL;
3950 tail = &t->next;
3953 *tail = NULL;
3955 if (changed)
3956 paste_tokens(&thead, false);
3958 return thead;
3962 * Expand all single-line macro calls made in the given line.
3963 * Return the expanded version of the line. The original is deemed
3964 * to be destroyed in the process. (In reality we'll just move
3965 * Tokens from input to output a lot of the time, rather than
3966 * actually bothering to destroy and replicate.)
3969 static Token *expand_smacro(Token * tline)
3971 Token *t, *tt, *mstart, **tail, *thead;
3972 SMacro *head = NULL, *m;
3973 Token **params;
3974 int *paramsize;
3975 unsigned int nparam, sparam;
3976 int brackets;
3977 Token *org_tline = tline;
3978 Context *ctx;
3979 const char *mname;
3980 int deadman = DEADMAN_LIMIT;
3981 bool expanded;
3984 * Trick: we should avoid changing the start token pointer since it can
3985 * be contained in "next" field of other token. Because of this
3986 * we allocate a copy of first token and work with it; at the end of
3987 * routine we copy it back
3989 if (org_tline) {
3990 tline = new_Token(org_tline->next, org_tline->type,
3991 org_tline->text, 0);
3992 tline->a.mac = org_tline->a.mac;
3993 nasm_free(org_tline->text);
3994 org_tline->text = NULL;
3997 expanded = true; /* Always expand %+ at least once */
3999 again:
4000 thead = NULL;
4001 tail = &thead;
4003 while (tline) { /* main token loop */
4004 if (!--deadman) {
4005 error(ERR_NONFATAL, "interminable macro recursion");
4006 goto err;
4009 if ((mname = tline->text)) {
4010 /* if this token is a local macro, look in local context */
4011 if (tline->type == TOK_ID) {
4012 head = (SMacro *)hash_findix(&smacros, mname);
4013 } else if (tline->type == TOK_PREPROC_ID) {
4014 ctx = get_ctx(mname, &mname, true);
4015 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4016 } else
4017 head = NULL;
4020 * We've hit an identifier. As in is_mmacro below, we first
4021 * check whether the identifier is a single-line macro at
4022 * all, then think about checking for parameters if
4023 * necessary.
4025 list_for_each(m, head)
4026 if (!mstrcmp(m->name, mname, m->casesense))
4027 break;
4028 if (m) {
4029 mstart = tline;
4030 params = NULL;
4031 paramsize = NULL;
4032 if (m->nparam == 0) {
4034 * Simple case: the macro is parameterless. Discard the
4035 * one token that the macro call took, and push the
4036 * expansion back on the to-do stack.
4038 if (!m->expansion) {
4039 if (!strcmp("__FILE__", m->name)) {
4040 int32_t num = 0;
4041 char *file = NULL;
4042 src_get(&num, &file);
4043 tline->text = nasm_quote(file, strlen(file));
4044 tline->type = TOK_STRING;
4045 nasm_free(file);
4046 continue;
4048 if (!strcmp("__LINE__", m->name)) {
4049 nasm_free(tline->text);
4050 make_tok_num(tline, src_get_linnum());
4051 continue;
4053 if (!strcmp("__BITS__", m->name)) {
4054 nasm_free(tline->text);
4055 make_tok_num(tline, globalbits);
4056 continue;
4058 tline = delete_Token(tline);
4059 continue;
4061 } else {
4063 * Complicated case: at least one macro with this name
4064 * exists and takes parameters. We must find the
4065 * parameters in the call, count them, find the SMacro
4066 * that corresponds to that form of the macro call, and
4067 * substitute for the parameters when we expand. What a
4068 * pain.
4070 /*tline = tline->next;
4071 skip_white_(tline); */
4072 do {
4073 t = tline->next;
4074 while (tok_type_(t, TOK_SMAC_END)) {
4075 t->a.mac->in_progress = false;
4076 t->text = NULL;
4077 t = tline->next = delete_Token(t);
4079 tline = t;
4080 } while (tok_type_(tline, TOK_WHITESPACE));
4081 if (!tok_is_(tline, "(")) {
4083 * This macro wasn't called with parameters: ignore
4084 * the call. (Behaviour borrowed from gnu cpp.)
4086 tline = mstart;
4087 m = NULL;
4088 } else {
4089 int paren = 0;
4090 int white = 0;
4091 brackets = 0;
4092 nparam = 0;
4093 sparam = PARAM_DELTA;
4094 params = nasm_malloc(sparam * sizeof(Token *));
4095 params[0] = tline->next;
4096 paramsize = nasm_malloc(sparam * sizeof(int));
4097 paramsize[0] = 0;
4098 while (true) { /* parameter loop */
4100 * For some unusual expansions
4101 * which concatenates function call
4103 t = tline->next;
4104 while (tok_type_(t, TOK_SMAC_END)) {
4105 t->a.mac->in_progress = false;
4106 t->text = NULL;
4107 t = tline->next = delete_Token(t);
4109 tline = t;
4111 if (!tline) {
4112 error(ERR_NONFATAL,
4113 "macro call expects terminating `)'");
4114 break;
4116 if (tline->type == TOK_WHITESPACE
4117 && brackets <= 0) {
4118 if (paramsize[nparam])
4119 white++;
4120 else
4121 params[nparam] = tline->next;
4122 continue; /* parameter loop */
4124 if (tline->type == TOK_OTHER
4125 && tline->text[1] == 0) {
4126 char ch = tline->text[0];
4127 if (ch == ',' && !paren && brackets <= 0) {
4128 if (++nparam >= sparam) {
4129 sparam += PARAM_DELTA;
4130 params = nasm_realloc(params,
4131 sparam * sizeof(Token *));
4132 paramsize = nasm_realloc(paramsize,
4133 sparam * sizeof(int));
4135 params[nparam] = tline->next;
4136 paramsize[nparam] = 0;
4137 white = 0;
4138 continue; /* parameter loop */
4140 if (ch == '{' &&
4141 (brackets > 0 || (brackets == 0 &&
4142 !paramsize[nparam])))
4144 if (!(brackets++)) {
4145 params[nparam] = tline->next;
4146 continue; /* parameter loop */
4149 if (ch == '}' && brackets > 0)
4150 if (--brackets == 0) {
4151 brackets = -1;
4152 continue; /* parameter loop */
4154 if (ch == '(' && !brackets)
4155 paren++;
4156 if (ch == ')' && brackets <= 0)
4157 if (--paren < 0)
4158 break;
4160 if (brackets < 0) {
4161 brackets = 0;
4162 error(ERR_NONFATAL, "braces do not "
4163 "enclose all of macro parameter");
4165 paramsize[nparam] += white + 1;
4166 white = 0;
4167 } /* parameter loop */
4168 nparam++;
4169 while (m && (m->nparam != nparam ||
4170 mstrcmp(m->name, mname,
4171 m->casesense)))
4172 m = m->next;
4173 if (!m)
4174 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4175 "macro `%s' exists, "
4176 "but not taking %d parameters",
4177 mstart->text, nparam);
4180 if (m && m->in_progress)
4181 m = NULL;
4182 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4184 * Design question: should we handle !tline, which
4185 * indicates missing ')' here, or expand those
4186 * macros anyway, which requires the (t) test a few
4187 * lines down?
4189 nasm_free(params);
4190 nasm_free(paramsize);
4191 tline = mstart;
4192 } else {
4194 * Expand the macro: we are placed on the last token of the
4195 * call, so that we can easily split the call from the
4196 * following tokens. We also start by pushing an SMAC_END
4197 * token for the cycle removal.
4199 t = tline;
4200 if (t) {
4201 tline = t->next;
4202 t->next = NULL;
4204 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4205 tt->a.mac = m;
4206 m->in_progress = true;
4207 tline = tt;
4208 list_for_each(t, m->expansion) {
4209 if (t->type >= TOK_SMAC_PARAM) {
4210 Token *pcopy = tline, **ptail = &pcopy;
4211 Token *ttt, *pt;
4212 int i;
4214 ttt = params[t->type - TOK_SMAC_PARAM];
4215 i = paramsize[t->type - TOK_SMAC_PARAM];
4216 while (--i >= 0) {
4217 pt = *ptail = new_Token(tline, ttt->type,
4218 ttt->text, 0);
4219 ptail = &pt->next;
4220 ttt = ttt->next;
4222 tline = pcopy;
4223 } else if (t->type == TOK_PREPROC_Q) {
4224 tt = new_Token(tline, TOK_ID, mname, 0);
4225 tline = tt;
4226 } else if (t->type == TOK_PREPROC_QQ) {
4227 tt = new_Token(tline, TOK_ID, m->name, 0);
4228 tline = tt;
4229 } else {
4230 tt = new_Token(tline, t->type, t->text, 0);
4231 tline = tt;
4236 * Having done that, get rid of the macro call, and clean
4237 * up the parameters.
4239 nasm_free(params);
4240 nasm_free(paramsize);
4241 free_tlist(mstart);
4242 expanded = true;
4243 continue; /* main token loop */
4248 if (tline->type == TOK_SMAC_END) {
4249 tline->a.mac->in_progress = false;
4250 tline = delete_Token(tline);
4251 } else {
4252 t = *tail = tline;
4253 tline = tline->next;
4254 t->a.mac = NULL;
4255 t->next = NULL;
4256 tail = &t->next;
4261 * Now scan the entire line and look for successive TOK_IDs that resulted
4262 * after expansion (they can't be produced by tokenize()). The successive
4263 * TOK_IDs should be concatenated.
4264 * Also we look for %+ tokens and concatenate the tokens before and after
4265 * them (without white spaces in between).
4267 if (expanded && paste_tokens(&thead, true)) {
4269 * If we concatenated something, *and* we had previously expanded
4270 * an actual macro, scan the lines again for macros...
4272 tline = thead;
4273 expanded = false;
4274 goto again;
4277 err:
4278 if (org_tline) {
4279 if (thead) {
4280 *org_tline = *thead;
4281 /* since we just gave text to org_line, don't free it */
4282 thead->text = NULL;
4283 delete_Token(thead);
4284 } else {
4285 /* the expression expanded to empty line;
4286 we can't return NULL for some reasons
4287 we just set the line to a single WHITESPACE token. */
4288 memset(org_tline, 0, sizeof(*org_tline));
4289 org_tline->text = NULL;
4290 org_tline->type = TOK_WHITESPACE;
4292 thead = org_tline;
4295 return thead;
4299 * Similar to expand_smacro but used exclusively with macro identifiers
4300 * right before they are fetched in. The reason is that there can be
4301 * identifiers consisting of several subparts. We consider that if there
4302 * are more than one element forming the name, user wants a expansion,
4303 * otherwise it will be left as-is. Example:
4305 * %define %$abc cde
4307 * the identifier %$abc will be left as-is so that the handler for %define
4308 * will suck it and define the corresponding value. Other case:
4310 * %define _%$abc cde
4312 * In this case user wants name to be expanded *before* %define starts
4313 * working, so we'll expand %$abc into something (if it has a value;
4314 * otherwise it will be left as-is) then concatenate all successive
4315 * PP_IDs into one.
4317 static Token *expand_id(Token * tline)
4319 Token *cur, *oldnext = NULL;
4321 if (!tline || !tline->next)
4322 return tline;
4324 cur = tline;
4325 while (cur->next &&
4326 (cur->next->type == TOK_ID ||
4327 cur->next->type == TOK_PREPROC_ID
4328 || cur->next->type == TOK_NUMBER))
4329 cur = cur->next;
4331 /* If identifier consists of just one token, don't expand */
4332 if (cur == tline)
4333 return tline;
4335 if (cur) {
4336 oldnext = cur->next; /* Detach the tail past identifier */
4337 cur->next = NULL; /* so that expand_smacro stops here */
4340 tline = expand_smacro(tline);
4342 if (cur) {
4343 /* expand_smacro possibly changhed tline; re-scan for EOL */
4344 cur = tline;
4345 while (cur && cur->next)
4346 cur = cur->next;
4347 if (cur)
4348 cur->next = oldnext;
4351 return tline;
4355 * Determine whether the given line constitutes a multi-line macro
4356 * call, and return the MMacro structure called if so. Doesn't have
4357 * to check for an initial label - that's taken care of in
4358 * expand_mmacro - but must check numbers of parameters. Guaranteed
4359 * to be called with tline->type == TOK_ID, so the putative macro
4360 * name is easy to find.
4362 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4364 MMacro *head, *m;
4365 Token **params;
4366 int nparam;
4368 head = (MMacro *) hash_findix(&mmacros, tline->text);
4371 * Efficiency: first we see if any macro exists with the given
4372 * name. If not, we can return NULL immediately. _Then_ we
4373 * count the parameters, and then we look further along the
4374 * list if necessary to find the proper MMacro.
4376 list_for_each(m, head)
4377 if (!mstrcmp(m->name, tline->text, m->casesense))
4378 break;
4379 if (!m)
4380 return NULL;
4383 * OK, we have a potential macro. Count and demarcate the
4384 * parameters.
4386 count_mmac_params(tline->next, &nparam, &params);
4389 * So we know how many parameters we've got. Find the MMacro
4390 * structure that handles this number.
4392 while (m) {
4393 if (m->nparam_min <= nparam
4394 && (m->plus || nparam <= m->nparam_max)) {
4396 * This one is right. Just check if cycle removal
4397 * prohibits us using it before we actually celebrate...
4399 if (m->in_progress > m->max_depth) {
4400 if (m->max_depth > 0) {
4401 error(ERR_WARNING,
4402 "reached maximum recursion depth of %i",
4403 m->max_depth);
4405 nasm_free(params);
4406 return NULL;
4409 * It's right, and we can use it. Add its default
4410 * parameters to the end of our list if necessary.
4412 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4413 params =
4414 nasm_realloc(params,
4415 ((m->nparam_min + m->ndefs +
4416 1) * sizeof(*params)));
4417 while (nparam < m->nparam_min + m->ndefs) {
4418 params[nparam] = m->defaults[nparam - m->nparam_min];
4419 nparam++;
4423 * If we've gone over the maximum parameter count (and
4424 * we're in Plus mode), ignore parameters beyond
4425 * nparam_max.
4427 if (m->plus && nparam > m->nparam_max)
4428 nparam = m->nparam_max;
4430 * Then terminate the parameter list, and leave.
4432 if (!params) { /* need this special case */
4433 params = nasm_malloc(sizeof(*params));
4434 nparam = 0;
4436 params[nparam] = NULL;
4437 *params_array = params;
4438 return m;
4441 * This one wasn't right: look for the next one with the
4442 * same name.
4444 list_for_each(m, m->next)
4445 if (!mstrcmp(m->name, tline->text, m->casesense))
4446 break;
4450 * After all that, we didn't find one with the right number of
4451 * parameters. Issue a warning, and fail to expand the macro.
4453 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4454 "macro `%s' exists, but not taking %d parameters",
4455 tline->text, nparam);
4456 nasm_free(params);
4457 return NULL;
4462 * Save MMacro invocation specific fields in
4463 * preparation for a recursive macro expansion
4465 static void push_mmacro(MMacro *m)
4467 MMacroInvocation *i;
4469 i = nasm_malloc(sizeof(MMacroInvocation));
4470 i->prev = m->prev;
4471 i->params = m->params;
4472 i->iline = m->iline;
4473 i->nparam = m->nparam;
4474 i->rotate = m->rotate;
4475 i->paramlen = m->paramlen;
4476 i->unique = m->unique;
4477 i->condcnt = m->condcnt;
4478 m->prev = i;
4483 * Restore MMacro invocation specific fields that were
4484 * saved during a previous recursive macro expansion
4486 static void pop_mmacro(MMacro *m)
4488 MMacroInvocation *i;
4490 if (m->prev) {
4491 i = m->prev;
4492 m->prev = i->prev;
4493 m->params = i->params;
4494 m->iline = i->iline;
4495 m->nparam = i->nparam;
4496 m->rotate = i->rotate;
4497 m->paramlen = i->paramlen;
4498 m->unique = i->unique;
4499 m->condcnt = i->condcnt;
4500 nasm_free(i);
4506 * Expand the multi-line macro call made by the given line, if
4507 * there is one to be expanded. If there is, push the expansion on
4508 * istk->expansion and return 1. Otherwise return 0.
4510 static int expand_mmacro(Token * tline)
4512 Token *startline = tline;
4513 Token *label = NULL;
4514 int dont_prepend = 0;
4515 Token **params, *t, *mtok, *tt;
4516 MMacro *m;
4517 Line *l, *ll;
4518 int i, nparam, *paramlen;
4519 const char *mname;
4521 t = tline;
4522 skip_white_(t);
4523 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4524 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4525 return 0;
4526 mtok = t;
4527 m = is_mmacro(t, &params);
4528 if (m) {
4529 mname = t->text;
4530 } else {
4531 Token *last;
4533 * We have an id which isn't a macro call. We'll assume
4534 * it might be a label; we'll also check to see if a
4535 * colon follows it. Then, if there's another id after
4536 * that lot, we'll check it again for macro-hood.
4538 label = last = t;
4539 t = t->next;
4540 if (tok_type_(t, TOK_WHITESPACE))
4541 last = t, t = t->next;
4542 if (tok_is_(t, ":")) {
4543 dont_prepend = 1;
4544 last = t, t = t->next;
4545 if (tok_type_(t, TOK_WHITESPACE))
4546 last = t, t = t->next;
4548 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4549 return 0;
4550 last->next = NULL;
4551 mname = t->text;
4552 tline = t;
4556 * Fix up the parameters: this involves stripping leading and
4557 * trailing whitespace, then stripping braces if they are
4558 * present.
4560 for (nparam = 0; params[nparam]; nparam++) ;
4561 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4563 for (i = 0; params[i]; i++) {
4564 int brace = false;
4565 int comma = (!m->plus || i < nparam - 1);
4567 t = params[i];
4568 skip_white_(t);
4569 if (tok_is_(t, "{"))
4570 t = t->next, brace = true, comma = false;
4571 params[i] = t;
4572 paramlen[i] = 0;
4573 while (t) {
4574 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4575 break; /* ... because we have hit a comma */
4576 if (comma && t->type == TOK_WHITESPACE
4577 && tok_is_(t->next, ","))
4578 break; /* ... or a space then a comma */
4579 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4580 break; /* ... or a brace */
4581 t = t->next;
4582 paramlen[i]++;
4587 * OK, we have a MMacro structure together with a set of
4588 * parameters. We must now go through the expansion and push
4589 * copies of each Line on to istk->expansion. Substitution of
4590 * parameter tokens and macro-local tokens doesn't get done
4591 * until the single-line macro substitution process; this is
4592 * because delaying them allows us to change the semantics
4593 * later through %rotate.
4595 * First, push an end marker on to istk->expansion, mark this
4596 * macro as in progress, and set up its invocation-specific
4597 * variables.
4599 ll = nasm_malloc(sizeof(Line));
4600 ll->next = istk->expansion;
4601 ll->finishes = m;
4602 ll->first = NULL;
4603 istk->expansion = ll;
4606 * Save the previous MMacro expansion in the case of
4607 * macro recursion
4609 if (m->max_depth && m->in_progress)
4610 push_mmacro(m);
4612 m->in_progress ++;
4613 m->params = params;
4614 m->iline = tline;
4615 m->nparam = nparam;
4616 m->rotate = 0;
4617 m->paramlen = paramlen;
4618 m->unique = unique++;
4619 m->lineno = 0;
4620 m->condcnt = 0;
4622 m->next_active = istk->mstk;
4623 istk->mstk = m;
4625 list_for_each(l, m->expansion) {
4626 Token **tail;
4628 ll = nasm_malloc(sizeof(Line));
4629 ll->finishes = NULL;
4630 ll->next = istk->expansion;
4631 istk->expansion = ll;
4632 tail = &ll->first;
4634 list_for_each(t, l->first) {
4635 Token *x = t;
4636 switch (t->type) {
4637 case TOK_PREPROC_Q:
4638 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4639 break;
4640 case TOK_PREPROC_QQ:
4641 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4642 break;
4643 case TOK_PREPROC_ID:
4644 if (t->text[1] == '0' && t->text[2] == '0') {
4645 dont_prepend = -1;
4646 x = label;
4647 if (!x)
4648 continue;
4650 /* fall through */
4651 default:
4652 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4653 break;
4655 tail = &tt->next;
4657 *tail = NULL;
4661 * If we had a label, push it on as the first line of
4662 * the macro expansion.
4664 if (label) {
4665 if (dont_prepend < 0)
4666 free_tlist(startline);
4667 else {
4668 ll = nasm_malloc(sizeof(Line));
4669 ll->finishes = NULL;
4670 ll->next = istk->expansion;
4671 istk->expansion = ll;
4672 ll->first = startline;
4673 if (!dont_prepend) {
4674 while (label->next)
4675 label = label->next;
4676 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4681 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4683 return 1;
4686 /* The function that actually does the error reporting */
4687 static void verror(int severity, const char *fmt, va_list arg)
4689 char buff[1024];
4691 vsnprintf(buff, sizeof(buff), fmt, arg);
4693 if (istk && istk->mstk && istk->mstk->name)
4694 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4695 istk->mstk->lineno, buff);
4696 else
4697 nasm_error(severity, "%s", buff);
4701 * Since preprocessor always operate only on the line that didn't
4702 * arrived yet, we should always use ERR_OFFBY1.
4704 static void error(int severity, const char *fmt, ...)
4706 va_list arg;
4708 /* If we're in a dead branch of IF or something like it, ignore the error */
4709 if (istk && istk->conds && !emitting(istk->conds->state))
4710 return;
4712 va_start(arg, fmt);
4713 verror(severity, fmt, arg);
4714 va_end(arg);
4718 * Because %else etc are evaluated in the state context
4719 * of the previous branch, errors might get lost with error():
4720 * %if 0 ... %else trailing garbage ... %endif
4721 * So %else etc should report errors with this function.
4723 static void error_precond(int severity, const char *fmt, ...)
4725 va_list arg;
4727 /* Only ignore the error if it's really in a dead branch */
4728 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4729 return;
4731 va_start(arg, fmt);
4732 verror(severity, fmt, arg);
4733 va_end(arg);
4736 static void
4737 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4739 Token *t;
4741 cstk = NULL;
4742 istk = nasm_malloc(sizeof(Include));
4743 istk->next = NULL;
4744 istk->conds = NULL;
4745 istk->expansion = NULL;
4746 istk->mstk = NULL;
4747 istk->fp = fopen(file, "r");
4748 istk->fname = NULL;
4749 src_set_fname(nasm_strdup(file));
4750 src_set_linnum(0);
4751 istk->lineinc = 1;
4752 if (!istk->fp)
4753 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4754 file);
4755 defining = NULL;
4756 nested_mac_count = 0;
4757 nested_rep_count = 0;
4758 init_macros();
4759 unique = 0;
4760 if (tasm_compatible_mode) {
4761 stdmacpos = nasm_stdmac;
4762 } else {
4763 stdmacpos = nasm_stdmac_after_tasm;
4765 any_extrastdmac = extrastdmac && *extrastdmac;
4766 do_predef = true;
4767 list = listgen;
4770 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4771 * The caller, however, will also pass in 3 for preprocess-only so
4772 * we can set __PASS__ accordingly.
4774 pass = apass > 2 ? 2 : apass;
4776 dephead = deptail = deplist;
4777 if (deplist) {
4778 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4779 sl->next = NULL;
4780 strcpy(sl->str, file);
4781 *deptail = sl;
4782 deptail = &sl->next;
4786 * Define the __PASS__ macro. This is defined here unlike
4787 * all the other builtins, because it is special -- it varies between
4788 * passes.
4790 t = nasm_malloc(sizeof(*t));
4791 t->next = NULL;
4792 make_tok_num(t, apass);
4793 t->a.mac = NULL;
4794 define_smacro(NULL, "__PASS__", true, 0, t);
4797 static char *pp_getline(void)
4799 char *line;
4800 Token *tline;
4802 while (1) {
4804 * Fetch a tokenized line, either from the macro-expansion
4805 * buffer or from the input file.
4807 tline = NULL;
4808 while (istk->expansion && istk->expansion->finishes) {
4809 Line *l = istk->expansion;
4810 if (!l->finishes->name && l->finishes->in_progress > 1) {
4811 Line *ll;
4814 * This is a macro-end marker for a macro with no
4815 * name, which means it's not really a macro at all
4816 * but a %rep block, and the `in_progress' field is
4817 * more than 1, meaning that we still need to
4818 * repeat. (1 means the natural last repetition; 0
4819 * means termination by %exitrep.) We have
4820 * therefore expanded up to the %endrep, and must
4821 * push the whole block on to the expansion buffer
4822 * again. We don't bother to remove the macro-end
4823 * marker: we'd only have to generate another one
4824 * if we did.
4826 l->finishes->in_progress--;
4827 list_for_each(l, l->finishes->expansion) {
4828 Token *t, *tt, **tail;
4830 ll = nasm_malloc(sizeof(Line));
4831 ll->next = istk->expansion;
4832 ll->finishes = NULL;
4833 ll->first = NULL;
4834 tail = &ll->first;
4836 list_for_each(t, l->first) {
4837 if (t->text || t->type == TOK_WHITESPACE) {
4838 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4839 tail = &tt->next;
4843 istk->expansion = ll;
4845 } else {
4847 * Check whether a `%rep' was started and not ended
4848 * within this macro expansion. This can happen and
4849 * should be detected. It's a fatal error because
4850 * I'm too confused to work out how to recover
4851 * sensibly from it.
4853 if (defining) {
4854 if (defining->name)
4855 error(ERR_PANIC,
4856 "defining with name in expansion");
4857 else if (istk->mstk->name)
4858 error(ERR_FATAL,
4859 "`%%rep' without `%%endrep' within"
4860 " expansion of macro `%s'",
4861 istk->mstk->name);
4865 * FIXME: investigate the relationship at this point between
4866 * istk->mstk and l->finishes
4869 MMacro *m = istk->mstk;
4870 istk->mstk = m->next_active;
4871 if (m->name) {
4873 * This was a real macro call, not a %rep, and
4874 * therefore the parameter information needs to
4875 * be freed.
4877 if (m->prev) {
4878 pop_mmacro(m);
4879 l->finishes->in_progress --;
4880 } else {
4881 nasm_free(m->params);
4882 free_tlist(m->iline);
4883 nasm_free(m->paramlen);
4884 l->finishes->in_progress = 0;
4886 } else
4887 free_mmacro(m);
4889 istk->expansion = l->next;
4890 nasm_free(l);
4891 list->downlevel(LIST_MACRO);
4894 while (1) { /* until we get a line we can use */
4896 if (istk->expansion) { /* from a macro expansion */
4897 char *p;
4898 Line *l = istk->expansion;
4899 if (istk->mstk)
4900 istk->mstk->lineno++;
4901 tline = l->first;
4902 istk->expansion = l->next;
4903 nasm_free(l);
4904 p = detoken(tline, false);
4905 list->line(LIST_MACRO, p);
4906 nasm_free(p);
4907 break;
4909 line = read_line();
4910 if (line) { /* from the current input file */
4911 line = prepreproc(line);
4912 tline = tokenize(line);
4913 nasm_free(line);
4914 break;
4917 * The current file has ended; work down the istk
4920 Include *i = istk;
4921 fclose(i->fp);
4922 if (i->conds)
4923 error(ERR_FATAL,
4924 "expected `%%endif' before end of file");
4925 /* only set line and file name if there's a next node */
4926 if (i->next) {
4927 src_set_linnum(i->lineno);
4928 nasm_free(src_set_fname(i->fname));
4930 istk = i->next;
4931 list->downlevel(LIST_INCLUDE);
4932 nasm_free(i);
4933 if (!istk)
4934 return NULL;
4935 if (istk->expansion && istk->expansion->finishes)
4936 break;
4941 * We must expand MMacro parameters and MMacro-local labels
4942 * _before_ we plunge into directive processing, to cope
4943 * with things like `%define something %1' such as STRUC
4944 * uses. Unless we're _defining_ a MMacro, in which case
4945 * those tokens should be left alone to go into the
4946 * definition; and unless we're in a non-emitting
4947 * condition, in which case we don't want to meddle with
4948 * anything.
4950 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4951 && !(istk->mstk && !istk->mstk->in_progress)) {
4952 tline = expand_mmac_params(tline);
4956 * Check the line to see if it's a preprocessor directive.
4958 if (do_directive(tline) == DIRECTIVE_FOUND) {
4959 continue;
4960 } else if (defining) {
4962 * We're defining a multi-line macro. We emit nothing
4963 * at all, and just
4964 * shove the tokenized line on to the macro definition.
4966 Line *l = nasm_malloc(sizeof(Line));
4967 l->next = defining->expansion;
4968 l->first = tline;
4969 l->finishes = NULL;
4970 defining->expansion = l;
4971 continue;
4972 } else if (istk->conds && !emitting(istk->conds->state)) {
4974 * We're in a non-emitting branch of a condition block.
4975 * Emit nothing at all, not even a blank line: when we
4976 * emerge from the condition we'll give a line-number
4977 * directive so we keep our place correctly.
4979 free_tlist(tline);
4980 continue;
4981 } else if (istk->mstk && !istk->mstk->in_progress) {
4983 * We're in a %rep block which has been terminated, so
4984 * we're walking through to the %endrep without
4985 * emitting anything. Emit nothing at all, not even a
4986 * blank line: when we emerge from the %rep block we'll
4987 * give a line-number directive so we keep our place
4988 * correctly.
4990 free_tlist(tline);
4991 continue;
4992 } else {
4993 tline = expand_smacro(tline);
4994 if (!expand_mmacro(tline)) {
4996 * De-tokenize the line again, and emit it.
4998 line = detoken(tline, true);
4999 free_tlist(tline);
5000 break;
5001 } else {
5002 continue; /* expand_mmacro calls free_tlist */
5007 return line;
5010 static void pp_cleanup(int pass)
5012 if (defining) {
5013 if (defining->name) {
5014 error(ERR_NONFATAL,
5015 "end of file while still defining macro `%s'",
5016 defining->name);
5017 } else {
5018 error(ERR_NONFATAL, "end of file while still in %%rep");
5021 free_mmacro(defining);
5022 defining = NULL;
5024 while (cstk)
5025 ctx_pop();
5026 free_macros();
5027 while (istk) {
5028 Include *i = istk;
5029 istk = istk->next;
5030 fclose(i->fp);
5031 nasm_free(i->fname);
5032 nasm_free(i);
5034 while (cstk)
5035 ctx_pop();
5036 nasm_free(src_set_fname(NULL));
5037 if (pass == 0) {
5038 IncPath *i;
5039 free_llist(predef);
5040 delete_Blocks();
5041 while ((i = ipath)) {
5042 ipath = i->next;
5043 if (i->path)
5044 nasm_free(i->path);
5045 nasm_free(i);
5050 void pp_include_path(char *path)
5052 IncPath *i;
5054 i = nasm_malloc(sizeof(IncPath));
5055 i->path = path ? nasm_strdup(path) : NULL;
5056 i->next = NULL;
5058 if (ipath) {
5059 IncPath *j = ipath;
5060 while (j->next)
5061 j = j->next;
5062 j->next = i;
5063 } else {
5064 ipath = i;
5068 void pp_pre_include(char *fname)
5070 Token *inc, *space, *name;
5071 Line *l;
5073 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5074 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5075 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5077 l = nasm_malloc(sizeof(Line));
5078 l->next = predef;
5079 l->first = inc;
5080 l->finishes = NULL;
5081 predef = l;
5084 void pp_pre_define(char *definition)
5086 Token *def, *space;
5087 Line *l;
5088 char *equals;
5090 equals = strchr(definition, '=');
5091 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5092 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5093 if (equals)
5094 *equals = ' ';
5095 space->next = tokenize(definition);
5096 if (equals)
5097 *equals = '=';
5099 l = nasm_malloc(sizeof(Line));
5100 l->next = predef;
5101 l->first = def;
5102 l->finishes = NULL;
5103 predef = l;
5106 void pp_pre_undefine(char *definition)
5108 Token *def, *space;
5109 Line *l;
5111 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5112 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5113 space->next = tokenize(definition);
5115 l = nasm_malloc(sizeof(Line));
5116 l->next = predef;
5117 l->first = def;
5118 l->finishes = NULL;
5119 predef = l;
5123 * Added by Keith Kanios:
5125 * This function is used to assist with "runtime" preprocessor
5126 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5128 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5129 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5132 void pp_runtime(char *definition)
5134 Token *def;
5136 def = tokenize(definition);
5137 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5138 free_tlist(def);
5142 void pp_extra_stdmac(macros_t *macros)
5144 extrastdmac = macros;
5147 static void make_tok_num(Token * tok, int64_t val)
5149 char numbuf[20];
5150 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5151 tok->text = nasm_strdup(numbuf);
5152 tok->type = TOK_NUMBER;
5155 Preproc nasmpp = {
5156 pp_reset,
5157 pp_getline,
5158 pp_cleanup