Merge branch 'nasm-2.09.xx'
[nasm/sigaren-mirror.git] / preproc.c
blob88c288ef4a9ae1f211e851beb848c45dac13943e
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 * In-place reverse a list of tokens.
493 static Token *reverse_tokens(Token *t)
495 Token *prev = NULL;
496 Token *next;
498 while (t) {
499 next = t->next;
500 t->next = prev;
501 prev = t;
502 t = next;
505 return prev;
509 * Handle TASM specific directives, which do not contain a % in
510 * front of them. We do it here because I could not find any other
511 * place to do it for the moment, and it is a hack (ideally it would
512 * be nice to be able to use the NASM pre-processor to do it).
514 static char *check_tasm_directive(char *line)
516 int32_t i, j, k, m, len;
517 char *p, *q, *oldline, oldchar;
519 p = nasm_skip_spaces(line);
521 /* Binary search for the directive name */
522 i = -1;
523 j = ARRAY_SIZE(tasm_directives);
524 q = nasm_skip_word(p);
525 len = q - p;
526 if (len) {
527 oldchar = p[len];
528 p[len] = 0;
529 while (j - i > 1) {
530 k = (j + i) / 2;
531 m = nasm_stricmp(p, tasm_directives[k]);
532 if (m == 0) {
533 /* We have found a directive, so jam a % in front of it
534 * so that NASM will then recognise it as one if it's own.
536 p[len] = oldchar;
537 len = strlen(p);
538 oldline = line;
539 line = nasm_malloc(len + 2);
540 line[0] = '%';
541 if (k == TM_IFDIFI) {
543 * NASM does not recognise IFDIFI, so we convert
544 * it to %if 0. This is not used in NASM
545 * compatible code, but does need to parse for the
546 * TASM macro package.
548 strcpy(line + 1, "if 0");
549 } else {
550 memcpy(line + 1, p, len + 1);
552 nasm_free(oldline);
553 return line;
554 } else if (m < 0) {
555 j = k;
556 } else
557 i = k;
559 p[len] = oldchar;
561 return line;
565 * The pre-preprocessing stage... This function translates line
566 * number indications as they emerge from GNU cpp (`# lineno "file"
567 * flags') into NASM preprocessor line number indications (`%line
568 * lineno file').
570 static char *prepreproc(char *line)
572 int lineno, fnlen;
573 char *fname, *oldline;
575 if (line[0] == '#' && line[1] == ' ') {
576 oldline = line;
577 fname = oldline + 2;
578 lineno = atoi(fname);
579 fname += strspn(fname, "0123456789 ");
580 if (*fname == '"')
581 fname++;
582 fnlen = strcspn(fname, "\"");
583 line = nasm_malloc(20 + fnlen);
584 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
585 nasm_free(oldline);
587 if (tasm_compatible_mode)
588 return check_tasm_directive(line);
589 return line;
593 * Free a linked list of tokens.
595 static void free_tlist(Token * list)
597 while (list)
598 list = delete_Token(list);
602 * Free a linked list of lines.
604 static void free_llist(Line * list)
606 Line *l, *tmp;
607 list_for_each_safe(l, tmp, list) {
608 free_tlist(l->first);
609 nasm_free(l);
614 * Free an MMacro
616 static void free_mmacro(MMacro * m)
618 nasm_free(m->name);
619 free_tlist(m->dlist);
620 nasm_free(m->defaults);
621 free_llist(m->expansion);
622 nasm_free(m);
626 * Free all currently defined macros, and free the hash tables
628 static void free_smacro_table(struct hash_table *smt)
630 SMacro *s, *tmp;
631 const char *key;
632 struct hash_tbl_node *it = NULL;
634 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
635 nasm_free((void *)key);
636 list_for_each_safe(s, tmp, s) {
637 nasm_free(s->name);
638 free_tlist(s->expansion);
639 nasm_free(s);
642 hash_free(smt);
645 static void free_mmacro_table(struct hash_table *mmt)
647 MMacro *m, *tmp;
648 const char *key;
649 struct hash_tbl_node *it = NULL;
651 it = NULL;
652 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
653 nasm_free((void *)key);
654 list_for_each_safe(m ,tmp, m)
655 free_mmacro(m);
657 hash_free(mmt);
660 static void free_macros(void)
662 free_smacro_table(&smacros);
663 free_mmacro_table(&mmacros);
667 * Initialize the hash tables
669 static void init_macros(void)
671 hash_init(&smacros, HASH_LARGE);
672 hash_init(&mmacros, HASH_LARGE);
676 * Pop the context stack.
678 static void ctx_pop(void)
680 Context *c = cstk;
682 cstk = cstk->next;
683 free_smacro_table(&c->localmac);
684 nasm_free(c->name);
685 nasm_free(c);
689 * Search for a key in the hash index; adding it if necessary
690 * (in which case we initialize the data pointer to NULL.)
692 static void **
693 hash_findi_add(struct hash_table *hash, const char *str)
695 struct hash_insert hi;
696 void **r;
697 char *strx;
699 r = hash_findi(hash, str, &hi);
700 if (r)
701 return r;
703 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
704 return hash_add(&hi, strx, NULL);
708 * Like hash_findi, but returns the data element rather than a pointer
709 * to it. Used only when not adding a new element, hence no third
710 * argument.
712 static void *
713 hash_findix(struct hash_table *hash, const char *str)
715 void **p;
717 p = hash_findi(hash, str, NULL);
718 return p ? *p : NULL;
722 * read line from standart macros set,
723 * if there no more left -- return NULL
725 static char *line_from_stdmac(void)
727 unsigned char c;
728 const unsigned char *p = stdmacpos;
729 char *line, *q;
730 size_t len = 0;
732 if (!stdmacpos)
733 return NULL;
735 while ((c = *p++)) {
736 if (c >= 0x80)
737 len += pp_directives_len[c - 0x80] + 1;
738 else
739 len++;
742 line = nasm_malloc(len + 1);
743 q = line;
744 while ((c = *stdmacpos++)) {
745 if (c >= 0x80) {
746 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
747 q += pp_directives_len[c - 0x80];
748 *q++ = ' ';
749 } else {
750 *q++ = c;
753 stdmacpos = p;
754 *q = '\0';
756 if (!*stdmacpos) {
757 /* This was the last of the standard macro chain... */
758 stdmacpos = NULL;
759 if (any_extrastdmac) {
760 stdmacpos = extrastdmac;
761 any_extrastdmac = false;
762 } else if (do_predef) {
763 Line *pd, *l;
764 Token *head, **tail, *t;
767 * Nasty hack: here we push the contents of
768 * `predef' on to the top-level expansion stack,
769 * since this is the most convenient way to
770 * implement the pre-include and pre-define
771 * features.
773 list_for_each(pd, predef) {
774 head = NULL;
775 tail = &head;
776 list_for_each(t, pd->first) {
777 *tail = new_Token(NULL, t->type, t->text, 0);
778 tail = &(*tail)->next;
781 l = nasm_malloc(sizeof(Line));
782 l->next = istk->expansion;
783 l->first = head;
784 l->finishes = NULL;
786 istk->expansion = l;
788 do_predef = false;
792 return line;
795 #define BUF_DELTA 512
797 * Read a line from the top file in istk, handling multiple CR/LFs
798 * at the end of the line read, and handling spurious ^Zs. Will
799 * return lines from the standard macro set if this has not already
800 * been done.
802 static char *read_line(void)
804 char *buffer, *p, *q;
805 int bufsize, continued_count;
808 * standart macros set (predefined) goes first
810 p = line_from_stdmac();
811 if (p)
812 return p;
815 * regular read from a file
817 bufsize = BUF_DELTA;
818 buffer = nasm_malloc(BUF_DELTA);
819 p = buffer;
820 continued_count = 0;
821 while (1) {
822 q = fgets(p, bufsize - (p - buffer), istk->fp);
823 if (!q)
824 break;
825 p += strlen(p);
826 if (p > buffer && p[-1] == '\n') {
828 * Convert backslash-CRLF line continuation sequences into
829 * nothing at all (for DOS and Windows)
831 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
832 p -= 3;
833 *p = 0;
834 continued_count++;
837 * Also convert backslash-LF line continuation sequences into
838 * nothing at all (for Unix)
840 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
841 p -= 2;
842 *p = 0;
843 continued_count++;
844 } else {
845 break;
848 if (p - buffer > bufsize - 10) {
849 int32_t offset = p - buffer;
850 bufsize += BUF_DELTA;
851 buffer = nasm_realloc(buffer, bufsize);
852 p = buffer + offset; /* prevent stale-pointer problems */
856 if (!q && p == buffer) {
857 nasm_free(buffer);
858 return NULL;
861 src_set_linnum(src_get_linnum() + istk->lineinc +
862 (continued_count * istk->lineinc));
865 * Play safe: remove CRs as well as LFs, if any of either are
866 * present at the end of the line.
868 while (--p >= buffer && (*p == '\n' || *p == '\r'))
869 *p = '\0';
872 * Handle spurious ^Z, which may be inserted into source files
873 * by some file transfer utilities.
875 buffer[strcspn(buffer, "\032")] = '\0';
877 list->line(LIST_READ, buffer);
879 return buffer;
883 * Tokenize a line of text. This is a very simple process since we
884 * don't need to parse the value out of e.g. numeric tokens: we
885 * simply split one string into many.
887 static Token *tokenize(char *line)
889 char c, *p = line;
890 enum pp_token_type type;
891 Token *list = NULL;
892 Token *t, **tail = &list;
894 while (*line) {
895 p = line;
896 if (*p == '%') {
897 p++;
898 if (*p == '+' && !nasm_isdigit(p[1])) {
899 p++;
900 type = TOK_PASTE;
901 } else if (nasm_isdigit(*p) ||
902 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
903 do {
904 p++;
906 while (nasm_isdigit(*p));
907 type = TOK_PREPROC_ID;
908 } else if (*p == '{') {
909 p++;
910 while (*p && *p != '}') {
911 p[-1] = *p;
912 p++;
914 p[-1] = '\0';
915 if (*p)
916 p++;
917 type = TOK_PREPROC_ID;
918 } else if (*p == '[') {
919 int lvl = 1;
920 line += 2; /* Skip the leading %[ */
921 p++;
922 while (lvl && (c = *p++)) {
923 switch (c) {
924 case ']':
925 lvl--;
926 break;
927 case '%':
928 if (*p == '[')
929 lvl++;
930 break;
931 case '\'':
932 case '\"':
933 case '`':
934 p = nasm_skip_string(p - 1) + 1;
935 break;
936 default:
937 break;
940 p--;
941 if (*p)
942 *p++ = '\0';
943 if (lvl)
944 error(ERR_NONFATAL, "unterminated %[ construct");
945 type = TOK_INDIRECT;
946 } else if (*p == '?') {
947 type = TOK_PREPROC_Q; /* %? */
948 p++;
949 if (*p == '?') {
950 type = TOK_PREPROC_QQ; /* %?? */
951 p++;
953 } else if (*p == '!') {
954 type = TOK_PREPROC_ID;
955 p++;
956 if (isidchar(*p)) {
957 do {
958 p++;
959 } while (isidchar(*p));
960 } else if (*p == '\'' || *p == '\"' || *p == '`') {
961 p = nasm_skip_string(p);
962 if (*p)
963 p++;
964 else
965 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
966 } else {
967 /* %! without string or identifier */
968 type = TOK_OTHER; /* Legacy behavior... */
970 } else if (isidchar(*p) ||
971 ((*p == '!' || *p == '%' || *p == '$') &&
972 isidchar(p[1]))) {
973 do {
974 p++;
976 while (isidchar(*p));
977 type = TOK_PREPROC_ID;
978 } else {
979 type = TOK_OTHER;
980 if (*p == '%')
981 p++;
983 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
984 type = TOK_ID;
985 p++;
986 while (*p && isidchar(*p))
987 p++;
988 } else if (*p == '\'' || *p == '"' || *p == '`') {
990 * A string token.
992 type = TOK_STRING;
993 p = nasm_skip_string(p);
995 if (*p) {
996 p++;
997 } else {
998 error(ERR_WARNING|ERR_PASS1, "unterminated string");
999 /* Handling unterminated strings by UNV */
1000 /* type = -1; */
1002 } else if (p[0] == '$' && p[1] == '$') {
1003 type = TOK_OTHER; /* TOKEN_BASE */
1004 p += 2;
1005 } else if (isnumstart(*p)) {
1006 bool is_hex = false;
1007 bool is_float = false;
1008 bool has_e = false;
1009 char c, *r;
1012 * A numeric token.
1015 if (*p == '$') {
1016 p++;
1017 is_hex = true;
1020 for (;;) {
1021 c = *p++;
1023 if (!is_hex && (c == 'e' || c == 'E')) {
1024 has_e = true;
1025 if (*p == '+' || *p == '-') {
1027 * e can only be followed by +/- if it is either a
1028 * prefixed hex number or a floating-point number
1030 p++;
1031 is_float = true;
1033 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1034 is_hex = true;
1035 } else if (c == 'P' || c == 'p') {
1036 is_float = true;
1037 if (*p == '+' || *p == '-')
1038 p++;
1039 } else if (isnumchar(c) || c == '_')
1040 ; /* just advance */
1041 else if (c == '.') {
1043 * we need to deal with consequences of the legacy
1044 * parser, like "1.nolist" being two tokens
1045 * (TOK_NUMBER, TOK_ID) here; at least give it
1046 * a shot for now. In the future, we probably need
1047 * a flex-based scanner with proper pattern matching
1048 * to do it as well as it can be done. Nothing in
1049 * the world is going to help the person who wants
1050 * 0x123.p16 interpreted as two tokens, though.
1052 r = p;
1053 while (*r == '_')
1054 r++;
1056 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1057 (!is_hex && (*r == 'e' || *r == 'E')) ||
1058 (*r == 'p' || *r == 'P')) {
1059 p = r;
1060 is_float = true;
1061 } else
1062 break; /* Terminate the token */
1063 } else
1064 break;
1066 p--; /* Point to first character beyond number */
1068 if (p == line+1 && *line == '$') {
1069 type = TOK_OTHER; /* TOKEN_HERE */
1070 } else {
1071 if (has_e && !is_hex) {
1072 /* 1e13 is floating-point, but 1e13h is not */
1073 is_float = true;
1076 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1078 } else if (nasm_isspace(*p)) {
1079 type = TOK_WHITESPACE;
1080 p = nasm_skip_spaces(p);
1082 * Whitespace just before end-of-line is discarded by
1083 * pretending it's a comment; whitespace just before a
1084 * comment gets lumped into the comment.
1086 if (!*p || *p == ';') {
1087 type = TOK_COMMENT;
1088 while (*p)
1089 p++;
1091 } else if (*p == ';') {
1092 type = TOK_COMMENT;
1093 while (*p)
1094 p++;
1095 } else {
1097 * Anything else is an operator of some kind. We check
1098 * for all the double-character operators (>>, <<, //,
1099 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1100 * else is a single-character operator.
1102 type = TOK_OTHER;
1103 if ((p[0] == '>' && p[1] == '>') ||
1104 (p[0] == '<' && p[1] == '<') ||
1105 (p[0] == '/' && p[1] == '/') ||
1106 (p[0] == '<' && p[1] == '=') ||
1107 (p[0] == '>' && p[1] == '=') ||
1108 (p[0] == '=' && p[1] == '=') ||
1109 (p[0] == '!' && p[1] == '=') ||
1110 (p[0] == '<' && p[1] == '>') ||
1111 (p[0] == '&' && p[1] == '&') ||
1112 (p[0] == '|' && p[1] == '|') ||
1113 (p[0] == '^' && p[1] == '^')) {
1114 p++;
1116 p++;
1119 /* Handling unterminated string by UNV */
1120 /*if (type == -1)
1122 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1123 t->text[p-line] = *line;
1124 tail = &t->next;
1126 else */
1127 if (type != TOK_COMMENT) {
1128 *tail = t = new_Token(NULL, type, line, p - line);
1129 tail = &t->next;
1131 line = p;
1133 return list;
1137 * this function allocates a new managed block of memory and
1138 * returns a pointer to the block. The managed blocks are
1139 * deleted only all at once by the delete_Blocks function.
1141 static void *new_Block(size_t size)
1143 Blocks *b = &blocks;
1145 /* first, get to the end of the linked list */
1146 while (b->next)
1147 b = b->next;
1148 /* now allocate the requested chunk */
1149 b->chunk = nasm_malloc(size);
1151 /* now allocate a new block for the next request */
1152 b->next = nasm_malloc(sizeof(Blocks));
1153 /* and initialize the contents of the new block */
1154 b->next->next = NULL;
1155 b->next->chunk = NULL;
1156 return b->chunk;
1160 * this function deletes all managed blocks of memory
1162 static void delete_Blocks(void)
1164 Blocks *a, *b = &blocks;
1167 * keep in mind that the first block, pointed to by blocks
1168 * is a static and not dynamically allocated, so we don't
1169 * free it.
1171 while (b) {
1172 if (b->chunk)
1173 nasm_free(b->chunk);
1174 a = b;
1175 b = b->next;
1176 if (a != &blocks)
1177 nasm_free(a);
1182 * this function creates a new Token and passes a pointer to it
1183 * back to the caller. It sets the type and text elements, and
1184 * also the a.mac and next elements to NULL.
1186 static Token *new_Token(Token * next, enum pp_token_type type,
1187 const char *text, int txtlen)
1189 Token *t;
1190 int i;
1192 if (!freeTokens) {
1193 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1194 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1195 freeTokens[i].next = &freeTokens[i + 1];
1196 freeTokens[i].next = NULL;
1198 t = freeTokens;
1199 freeTokens = t->next;
1200 t->next = next;
1201 t->a.mac = NULL;
1202 t->type = type;
1203 if (type == TOK_WHITESPACE || !text) {
1204 t->text = NULL;
1205 } else {
1206 if (txtlen == 0)
1207 txtlen = strlen(text);
1208 t->text = nasm_malloc(txtlen+1);
1209 memcpy(t->text, text, txtlen);
1210 t->text[txtlen] = '\0';
1212 return t;
1215 static Token *delete_Token(Token * t)
1217 Token *next = t->next;
1218 nasm_free(t->text);
1219 t->next = freeTokens;
1220 freeTokens = t;
1221 return next;
1225 * Convert a line of tokens back into text.
1226 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1227 * will be transformed into ..@ctxnum.xxx
1229 static char *detoken(Token * tlist, bool expand_locals)
1231 Token *t;
1232 char *line, *p;
1233 const char *q;
1234 int len = 0;
1236 list_for_each(t, tlist) {
1237 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1238 char *v;
1239 char *q = t->text;
1241 v = t->text + 2;
1242 if (*v == '\'' || *v == '\"' || *v == '`') {
1243 size_t len = nasm_unquote(v, NULL);
1244 size_t clen = strlen(v);
1246 if (len != clen) {
1247 error(ERR_NONFATAL | ERR_PASS1,
1248 "NUL character in %! string");
1249 v = NULL;
1253 if (v) {
1254 char *p = getenv(v);
1255 if (!p) {
1256 error(ERR_NONFATAL | ERR_PASS1,
1257 "nonexistent environment variable `%s'", v);
1258 p = "";
1260 t->text = nasm_strdup(p);
1262 nasm_free(q);
1265 /* Expand local macros here and not during preprocessing */
1266 if (expand_locals &&
1267 t->type == TOK_PREPROC_ID && t->text &&
1268 t->text[0] == '%' && t->text[1] == '$') {
1269 const char *q;
1270 char *p;
1271 Context *ctx = get_ctx(t->text, &q, false);
1272 if (ctx) {
1273 char buffer[40];
1274 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1275 p = nasm_strcat(buffer, q);
1276 nasm_free(t->text);
1277 t->text = p;
1280 if (t->type == TOK_WHITESPACE)
1281 len++;
1282 else if (t->text)
1283 len += strlen(t->text);
1286 p = line = nasm_malloc(len + 1);
1288 list_for_each(t, tlist) {
1289 if (t->type == TOK_WHITESPACE) {
1290 *p++ = ' ';
1291 } else if (t->text) {
1292 q = t->text;
1293 while (*q)
1294 *p++ = *q++;
1297 *p = '\0';
1299 return line;
1303 * A scanner, suitable for use by the expression evaluator, which
1304 * operates on a line of Tokens. Expects a pointer to a pointer to
1305 * the first token in the line to be passed in as its private_data
1306 * field.
1308 * FIX: This really needs to be unified with stdscan.
1310 static int ppscan(void *private_data, struct tokenval *tokval)
1312 Token **tlineptr = private_data;
1313 Token *tline;
1314 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1316 do {
1317 tline = *tlineptr;
1318 *tlineptr = tline ? tline->next : NULL;
1319 } while (tline && (tline->type == TOK_WHITESPACE ||
1320 tline->type == TOK_COMMENT));
1322 if (!tline)
1323 return tokval->t_type = TOKEN_EOS;
1325 tokval->t_charptr = tline->text;
1327 if (tline->text[0] == '$' && !tline->text[1])
1328 return tokval->t_type = TOKEN_HERE;
1329 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1330 return tokval->t_type = TOKEN_BASE;
1332 if (tline->type == TOK_ID) {
1333 p = tokval->t_charptr = tline->text;
1334 if (p[0] == '$') {
1335 tokval->t_charptr++;
1336 return tokval->t_type = TOKEN_ID;
1339 for (r = p, s = ourcopy; *r; r++) {
1340 if (r >= p+MAX_KEYWORD)
1341 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1342 *s++ = nasm_tolower(*r);
1344 *s = '\0';
1345 /* right, so we have an identifier sitting in temp storage. now,
1346 * is it actually a register or instruction name, or what? */
1347 return nasm_token_hash(ourcopy, tokval);
1350 if (tline->type == TOK_NUMBER) {
1351 bool rn_error;
1352 tokval->t_integer = readnum(tline->text, &rn_error);
1353 tokval->t_charptr = tline->text;
1354 if (rn_error)
1355 return tokval->t_type = TOKEN_ERRNUM;
1356 else
1357 return tokval->t_type = TOKEN_NUM;
1360 if (tline->type == TOK_FLOAT) {
1361 return tokval->t_type = TOKEN_FLOAT;
1364 if (tline->type == TOK_STRING) {
1365 char bq, *ep;
1367 bq = tline->text[0];
1368 tokval->t_charptr = tline->text;
1369 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1371 if (ep[0] != bq || ep[1] != '\0')
1372 return tokval->t_type = TOKEN_ERRSTR;
1373 else
1374 return tokval->t_type = TOKEN_STR;
1377 if (tline->type == TOK_OTHER) {
1378 if (!strcmp(tline->text, "<<"))
1379 return tokval->t_type = TOKEN_SHL;
1380 if (!strcmp(tline->text, ">>"))
1381 return tokval->t_type = TOKEN_SHR;
1382 if (!strcmp(tline->text, "//"))
1383 return tokval->t_type = TOKEN_SDIV;
1384 if (!strcmp(tline->text, "%%"))
1385 return tokval->t_type = TOKEN_SMOD;
1386 if (!strcmp(tline->text, "=="))
1387 return tokval->t_type = TOKEN_EQ;
1388 if (!strcmp(tline->text, "<>"))
1389 return tokval->t_type = TOKEN_NE;
1390 if (!strcmp(tline->text, "!="))
1391 return tokval->t_type = TOKEN_NE;
1392 if (!strcmp(tline->text, "<="))
1393 return tokval->t_type = TOKEN_LE;
1394 if (!strcmp(tline->text, ">="))
1395 return tokval->t_type = TOKEN_GE;
1396 if (!strcmp(tline->text, "&&"))
1397 return tokval->t_type = TOKEN_DBL_AND;
1398 if (!strcmp(tline->text, "^^"))
1399 return tokval->t_type = TOKEN_DBL_XOR;
1400 if (!strcmp(tline->text, "||"))
1401 return tokval->t_type = TOKEN_DBL_OR;
1405 * We have no other options: just return the first character of
1406 * the token text.
1408 return tokval->t_type = tline->text[0];
1412 * Compare a string to the name of an existing macro; this is a
1413 * simple wrapper which calls either strcmp or nasm_stricmp
1414 * depending on the value of the `casesense' parameter.
1416 static int mstrcmp(const char *p, const char *q, bool casesense)
1418 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1422 * Compare a string to the name of an existing macro; this is a
1423 * simple wrapper which calls either strcmp or nasm_stricmp
1424 * depending on the value of the `casesense' parameter.
1426 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1428 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1432 * Return the Context structure associated with a %$ token. Return
1433 * NULL, having _already_ reported an error condition, if the
1434 * context stack isn't deep enough for the supplied number of $
1435 * signs.
1436 * If all_contexts == true, contexts that enclose current are
1437 * also scanned for such smacro, until it is found; if not -
1438 * only the context that directly results from the number of $'s
1439 * in variable's name.
1441 * If "namep" is non-NULL, set it to the pointer to the macro name
1442 * tail, i.e. the part beyond %$...
1444 static Context *get_ctx(const char *name, const char **namep,
1445 bool all_contexts)
1447 Context *ctx;
1448 SMacro *m;
1449 int i;
1451 if (namep)
1452 *namep = name;
1454 if (!name || name[0] != '%' || name[1] != '$')
1455 return NULL;
1457 if (!cstk) {
1458 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1459 return NULL;
1462 name += 2;
1463 ctx = cstk;
1464 i = 0;
1465 while (ctx && *name == '$') {
1466 name++;
1467 i++;
1468 ctx = ctx->next;
1470 if (!ctx) {
1471 error(ERR_NONFATAL, "`%s': context stack is only"
1472 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1473 return NULL;
1476 if (namep)
1477 *namep = name;
1479 if (!all_contexts)
1480 return ctx;
1483 * NOTE: In 2.10 we will not need lookup in extarnal
1484 * contexts, so this is a gentle way to inform users
1485 * about their source code need to be updated
1488 /* first round -- check the current context */
1489 m = hash_findix(&ctx->localmac, name);
1490 while (m) {
1491 if (!mstrcmp(m->name, name, m->casesense))
1492 return ctx;
1493 m = m->next;
1496 /* second round - external contexts */
1497 while ((ctx = ctx->next)) {
1498 /* Search for this smacro in found context */
1499 m = hash_findix(&ctx->localmac, name);
1500 while (m) {
1501 if (!mstrcmp(m->name, name, m->casesense)) {
1502 /* NOTE: deprecated as of 2.10 */
1503 static int once = 0;
1504 if (!once) {
1505 error(ERR_WARNING, "context-local macro expansion"
1506 " fall-through (automatic searching of outer"
1507 " contexts) will be deprecated starting in"
1508 " NASM 2.10, please see the NASM Manual for"
1509 " more information");
1510 once = 1;
1512 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
1513 return ctx;
1515 m = m->next;
1519 return NULL;
1523 * Check to see if a file is already in a string list
1525 static bool in_list(const StrList *list, const char *str)
1527 while (list) {
1528 if (!strcmp(list->str, str))
1529 return true;
1530 list = list->next;
1532 return false;
1536 * Open an include file. This routine must always return a valid
1537 * file pointer if it returns - it's responsible for throwing an
1538 * ERR_FATAL and bombing out completely if not. It should also try
1539 * the include path one by one until it finds the file or reaches
1540 * the end of the path.
1542 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1543 bool missing_ok)
1545 FILE *fp;
1546 char *prefix = "";
1547 IncPath *ip = ipath;
1548 int len = strlen(file);
1549 size_t prefix_len = 0;
1550 StrList *sl;
1552 while (1) {
1553 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1554 memcpy(sl->str, prefix, prefix_len);
1555 memcpy(sl->str+prefix_len, file, len+1);
1556 fp = fopen(sl->str, "r");
1557 if (fp && dhead && !in_list(*dhead, sl->str)) {
1558 sl->next = NULL;
1559 **dtail = sl;
1560 *dtail = &sl->next;
1561 } else {
1562 nasm_free(sl);
1564 if (fp)
1565 return fp;
1566 if (!ip) {
1567 if (!missing_ok)
1568 break;
1569 prefix = NULL;
1570 } else {
1571 prefix = ip->path;
1572 ip = ip->next;
1574 if (prefix) {
1575 prefix_len = strlen(prefix);
1576 } else {
1577 /* -MG given and file not found */
1578 if (dhead && !in_list(*dhead, file)) {
1579 sl = nasm_malloc(len+1+sizeof sl->next);
1580 sl->next = NULL;
1581 strcpy(sl->str, file);
1582 **dtail = sl;
1583 *dtail = &sl->next;
1585 return NULL;
1589 error(ERR_FATAL, "unable to open include file `%s'", file);
1590 return NULL;
1594 * Determine if we should warn on defining a single-line macro of
1595 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1596 * return true if _any_ single-line macro of that name is defined.
1597 * Otherwise, will return true if a single-line macro with either
1598 * `nparam' or no parameters is defined.
1600 * If a macro with precisely the right number of parameters is
1601 * defined, or nparam is -1, the address of the definition structure
1602 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1603 * is NULL, no action will be taken regarding its contents, and no
1604 * error will occur.
1606 * Note that this is also called with nparam zero to resolve
1607 * `ifdef'.
1609 * If you already know which context macro belongs to, you can pass
1610 * the context pointer as first parameter; if you won't but name begins
1611 * with %$ the context will be automatically computed. If all_contexts
1612 * is true, macro will be searched in outer contexts as well.
1614 static bool
1615 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1616 bool nocase)
1618 struct hash_table *smtbl;
1619 SMacro *m;
1621 if (ctx) {
1622 smtbl = &ctx->localmac;
1623 } else if (name[0] == '%' && name[1] == '$') {
1624 if (cstk)
1625 ctx = get_ctx(name, &name, false);
1626 if (!ctx)
1627 return false; /* got to return _something_ */
1628 smtbl = &ctx->localmac;
1629 } else {
1630 smtbl = &smacros;
1632 m = (SMacro *) hash_findix(smtbl, name);
1634 while (m) {
1635 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1636 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1637 if (defn) {
1638 if (nparam == (int) m->nparam || nparam == -1)
1639 *defn = m;
1640 else
1641 *defn = NULL;
1643 return true;
1645 m = m->next;
1648 return false;
1652 * Count and mark off the parameters in a multi-line macro call.
1653 * This is called both from within the multi-line macro expansion
1654 * code, and also to mark off the default parameters when provided
1655 * in a %macro definition line.
1657 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1659 int paramsize, brace;
1661 *nparam = paramsize = 0;
1662 *params = NULL;
1663 while (t) {
1664 /* +1: we need space for the final NULL */
1665 if (*nparam+1 >= paramsize) {
1666 paramsize += PARAM_DELTA;
1667 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1669 skip_white_(t);
1670 brace = false;
1671 if (tok_is_(t, "{"))
1672 brace = true;
1673 (*params)[(*nparam)++] = t;
1674 while (tok_isnt_(t, brace ? "}" : ","))
1675 t = t->next;
1676 if (t) { /* got a comma/brace */
1677 t = t->next;
1678 if (brace) {
1680 * Now we've found the closing brace, look further
1681 * for the comma.
1683 skip_white_(t);
1684 if (tok_isnt_(t, ",")) {
1685 error(ERR_NONFATAL,
1686 "braces do not enclose all of macro parameter");
1687 while (tok_isnt_(t, ","))
1688 t = t->next;
1690 if (t)
1691 t = t->next; /* eat the comma */
1698 * Determine whether one of the various `if' conditions is true or
1699 * not.
1701 * We must free the tline we get passed.
1703 static bool if_condition(Token * tline, enum preproc_token ct)
1705 enum pp_conditional i = PP_COND(ct);
1706 bool j;
1707 Token *t, *tt, **tptr, *origline;
1708 struct tokenval tokval;
1709 expr *evalresult;
1710 enum pp_token_type needtype;
1711 char *p;
1713 origline = tline;
1715 switch (i) {
1716 case PPC_IFCTX:
1717 j = false; /* have we matched yet? */
1718 while (true) {
1719 skip_white_(tline);
1720 if (!tline)
1721 break;
1722 if (tline->type != TOK_ID) {
1723 error(ERR_NONFATAL,
1724 "`%s' expects context identifiers", pp_directives[ct]);
1725 free_tlist(origline);
1726 return -1;
1728 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1729 j = true;
1730 tline = tline->next;
1732 break;
1734 case PPC_IFDEF:
1735 j = false; /* have we matched yet? */
1736 while (tline) {
1737 skip_white_(tline);
1738 if (!tline || (tline->type != TOK_ID &&
1739 (tline->type != TOK_PREPROC_ID ||
1740 tline->text[1] != '$'))) {
1741 error(ERR_NONFATAL,
1742 "`%s' expects macro identifiers", pp_directives[ct]);
1743 goto fail;
1745 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1746 j = true;
1747 tline = tline->next;
1749 break;
1751 case PPC_IFENV:
1752 tline = expand_smacro(tline);
1753 j = false; /* have we matched yet? */
1754 while (tline) {
1755 skip_white_(tline);
1756 if (!tline || (tline->type != TOK_ID &&
1757 tline->type != TOK_STRING &&
1758 (tline->type != TOK_PREPROC_ID ||
1759 tline->text[1] != '!'))) {
1760 error(ERR_NONFATAL,
1761 "`%s' expects environment variable names",
1762 pp_directives[ct]);
1763 goto fail;
1765 p = tline->text;
1766 if (tline->type == TOK_PREPROC_ID)
1767 p += 2; /* Skip leading %! */
1768 if (*p == '\'' || *p == '\"' || *p == '`')
1769 nasm_unquote_cstr(p, ct);
1770 if (getenv(p))
1771 j = true;
1772 tline = tline->next;
1774 break;
1776 case PPC_IFIDN:
1777 case PPC_IFIDNI:
1778 tline = expand_smacro(tline);
1779 t = tt = tline;
1780 while (tok_isnt_(tt, ","))
1781 tt = tt->next;
1782 if (!tt) {
1783 error(ERR_NONFATAL,
1784 "`%s' expects two comma-separated arguments",
1785 pp_directives[ct]);
1786 goto fail;
1788 tt = tt->next;
1789 j = true; /* assume equality unless proved not */
1790 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1791 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1792 error(ERR_NONFATAL, "`%s': more than one comma on line",
1793 pp_directives[ct]);
1794 goto fail;
1796 if (t->type == TOK_WHITESPACE) {
1797 t = t->next;
1798 continue;
1800 if (tt->type == TOK_WHITESPACE) {
1801 tt = tt->next;
1802 continue;
1804 if (tt->type != t->type) {
1805 j = false; /* found mismatching tokens */
1806 break;
1808 /* When comparing strings, need to unquote them first */
1809 if (t->type == TOK_STRING) {
1810 size_t l1 = nasm_unquote(t->text, NULL);
1811 size_t l2 = nasm_unquote(tt->text, NULL);
1813 if (l1 != l2) {
1814 j = false;
1815 break;
1817 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1818 j = false;
1819 break;
1821 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1822 j = false; /* found mismatching tokens */
1823 break;
1826 t = t->next;
1827 tt = tt->next;
1829 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1830 j = false; /* trailing gunk on one end or other */
1831 break;
1833 case PPC_IFMACRO:
1835 bool found = false;
1836 MMacro searching, *mmac;
1838 skip_white_(tline);
1839 tline = expand_id(tline);
1840 if (!tok_type_(tline, TOK_ID)) {
1841 error(ERR_NONFATAL,
1842 "`%s' expects a macro name", pp_directives[ct]);
1843 goto fail;
1845 searching.name = nasm_strdup(tline->text);
1846 searching.casesense = true;
1847 searching.plus = false;
1848 searching.nolist = false;
1849 searching.in_progress = 0;
1850 searching.max_depth = 0;
1851 searching.rep_nest = NULL;
1852 searching.nparam_min = 0;
1853 searching.nparam_max = INT_MAX;
1854 tline = expand_smacro(tline->next);
1855 skip_white_(tline);
1856 if (!tline) {
1857 } else if (!tok_type_(tline, TOK_NUMBER)) {
1858 error(ERR_NONFATAL,
1859 "`%s' expects a parameter count or nothing",
1860 pp_directives[ct]);
1861 } else {
1862 searching.nparam_min = searching.nparam_max =
1863 readnum(tline->text, &j);
1864 if (j)
1865 error(ERR_NONFATAL,
1866 "unable to parse parameter count `%s'",
1867 tline->text);
1869 if (tline && tok_is_(tline->next, "-")) {
1870 tline = tline->next->next;
1871 if (tok_is_(tline, "*"))
1872 searching.nparam_max = INT_MAX;
1873 else if (!tok_type_(tline, TOK_NUMBER))
1874 error(ERR_NONFATAL,
1875 "`%s' expects a parameter count after `-'",
1876 pp_directives[ct]);
1877 else {
1878 searching.nparam_max = readnum(tline->text, &j);
1879 if (j)
1880 error(ERR_NONFATAL,
1881 "unable to parse parameter count `%s'",
1882 tline->text);
1883 if (searching.nparam_min > searching.nparam_max)
1884 error(ERR_NONFATAL,
1885 "minimum parameter count exceeds maximum");
1888 if (tline && tok_is_(tline->next, "+")) {
1889 tline = tline->next;
1890 searching.plus = true;
1892 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1893 while (mmac) {
1894 if (!strcmp(mmac->name, searching.name) &&
1895 (mmac->nparam_min <= searching.nparam_max
1896 || searching.plus)
1897 && (searching.nparam_min <= mmac->nparam_max
1898 || mmac->plus)) {
1899 found = true;
1900 break;
1902 mmac = mmac->next;
1904 if (tline && tline->next)
1905 error(ERR_WARNING|ERR_PASS1,
1906 "trailing garbage after %%ifmacro ignored");
1907 nasm_free(searching.name);
1908 j = found;
1909 break;
1912 case PPC_IFID:
1913 needtype = TOK_ID;
1914 goto iftype;
1915 case PPC_IFNUM:
1916 needtype = TOK_NUMBER;
1917 goto iftype;
1918 case PPC_IFSTR:
1919 needtype = TOK_STRING;
1920 goto iftype;
1922 iftype:
1923 t = tline = expand_smacro(tline);
1925 while (tok_type_(t, TOK_WHITESPACE) ||
1926 (needtype == TOK_NUMBER &&
1927 tok_type_(t, TOK_OTHER) &&
1928 (t->text[0] == '-' || t->text[0] == '+') &&
1929 !t->text[1]))
1930 t = t->next;
1932 j = tok_type_(t, needtype);
1933 break;
1935 case PPC_IFTOKEN:
1936 t = tline = expand_smacro(tline);
1937 while (tok_type_(t, TOK_WHITESPACE))
1938 t = t->next;
1940 j = false;
1941 if (t) {
1942 t = t->next; /* Skip the actual token */
1943 while (tok_type_(t, TOK_WHITESPACE))
1944 t = t->next;
1945 j = !t; /* Should be nothing left */
1947 break;
1949 case PPC_IFEMPTY:
1950 t = tline = expand_smacro(tline);
1951 while (tok_type_(t, TOK_WHITESPACE))
1952 t = t->next;
1954 j = !t; /* Should be empty */
1955 break;
1957 case PPC_IF:
1958 t = tline = expand_smacro(tline);
1959 tptr = &t;
1960 tokval.t_type = TOKEN_INVALID;
1961 evalresult = evaluate(ppscan, tptr, &tokval,
1962 NULL, pass | CRITICAL, error, NULL);
1963 if (!evalresult)
1964 return -1;
1965 if (tokval.t_type)
1966 error(ERR_WARNING|ERR_PASS1,
1967 "trailing garbage after expression ignored");
1968 if (!is_simple(evalresult)) {
1969 error(ERR_NONFATAL,
1970 "non-constant value given to `%s'", pp_directives[ct]);
1971 goto fail;
1973 j = reloc_value(evalresult) != 0;
1974 break;
1976 default:
1977 error(ERR_FATAL,
1978 "preprocessor directive `%s' not yet implemented",
1979 pp_directives[ct]);
1980 goto fail;
1983 free_tlist(origline);
1984 return j ^ PP_NEGATIVE(ct);
1986 fail:
1987 free_tlist(origline);
1988 return -1;
1992 * Common code for defining an smacro
1994 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1995 int nparam, Token *expansion)
1997 SMacro *smac, **smhead;
1998 struct hash_table *smtbl;
2000 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2001 if (!smac) {
2002 error(ERR_WARNING|ERR_PASS1,
2003 "single-line macro `%s' defined both with and"
2004 " without parameters", mname);
2006 * Some instances of the old code considered this a failure,
2007 * some others didn't. What is the right thing to do here?
2009 free_tlist(expansion);
2010 return false; /* Failure */
2011 } else {
2013 * We're redefining, so we have to take over an
2014 * existing SMacro structure. This means freeing
2015 * what was already in it.
2017 nasm_free(smac->name);
2018 free_tlist(smac->expansion);
2020 } else {
2021 smtbl = ctx ? &ctx->localmac : &smacros;
2022 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2023 smac = nasm_malloc(sizeof(SMacro));
2024 smac->next = *smhead;
2025 *smhead = smac;
2027 smac->name = nasm_strdup(mname);
2028 smac->casesense = casesense;
2029 smac->nparam = nparam;
2030 smac->expansion = expansion;
2031 smac->in_progress = false;
2032 return true; /* Success */
2036 * Undefine an smacro
2038 static void undef_smacro(Context *ctx, const char *mname)
2040 SMacro **smhead, *s, **sp;
2041 struct hash_table *smtbl;
2043 smtbl = ctx ? &ctx->localmac : &smacros;
2044 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2046 if (smhead) {
2048 * We now have a macro name... go hunt for it.
2050 sp = smhead;
2051 while ((s = *sp) != NULL) {
2052 if (!mstrcmp(s->name, mname, s->casesense)) {
2053 *sp = s->next;
2054 nasm_free(s->name);
2055 free_tlist(s->expansion);
2056 nasm_free(s);
2057 } else {
2058 sp = &s->next;
2065 * Parse a mmacro specification.
2067 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2069 bool err;
2071 tline = tline->next;
2072 skip_white_(tline);
2073 tline = expand_id(tline);
2074 if (!tok_type_(tline, TOK_ID)) {
2075 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2076 return false;
2079 def->prev = NULL;
2080 def->name = nasm_strdup(tline->text);
2081 def->plus = false;
2082 def->nolist = false;
2083 def->in_progress = 0;
2084 def->rep_nest = NULL;
2085 def->nparam_min = 0;
2086 def->nparam_max = 0;
2088 tline = expand_smacro(tline->next);
2089 skip_white_(tline);
2090 if (!tok_type_(tline, TOK_NUMBER)) {
2091 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2092 } else {
2093 def->nparam_min = def->nparam_max =
2094 readnum(tline->text, &err);
2095 if (err)
2096 error(ERR_NONFATAL,
2097 "unable to parse parameter count `%s'", tline->text);
2099 if (tline && tok_is_(tline->next, "-")) {
2100 tline = tline->next->next;
2101 if (tok_is_(tline, "*")) {
2102 def->nparam_max = INT_MAX;
2103 } else if (!tok_type_(tline, TOK_NUMBER)) {
2104 error(ERR_NONFATAL,
2105 "`%s' expects a parameter count after `-'", directive);
2106 } else {
2107 def->nparam_max = readnum(tline->text, &err);
2108 if (err) {
2109 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2110 tline->text);
2112 if (def->nparam_min > def->nparam_max) {
2113 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2117 if (tline && tok_is_(tline->next, "+")) {
2118 tline = tline->next;
2119 def->plus = true;
2121 if (tline && tok_type_(tline->next, TOK_ID) &&
2122 !nasm_stricmp(tline->next->text, ".nolist")) {
2123 tline = tline->next;
2124 def->nolist = true;
2128 * Handle default parameters.
2130 if (tline && tline->next) {
2131 def->dlist = tline->next;
2132 tline->next = NULL;
2133 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2134 } else {
2135 def->dlist = NULL;
2136 def->defaults = NULL;
2138 def->expansion = NULL;
2140 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2141 !def->plus)
2142 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2143 "too many default macro parameters");
2145 return true;
2150 * Decode a size directive
2152 static int parse_size(const char *str) {
2153 static const char *size_names[] =
2154 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2155 static const int sizes[] =
2156 { 0, 1, 4, 16, 8, 10, 2, 32 };
2158 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2162 * find and process preprocessor directive in passed line
2163 * Find out if a line contains a preprocessor directive, and deal
2164 * with it if so.
2166 * If a directive _is_ found, it is the responsibility of this routine
2167 * (and not the caller) to free_tlist() the line.
2169 * @param tline a pointer to the current tokeninzed line linked list
2170 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2173 static int do_directive(Token * tline)
2175 enum preproc_token i;
2176 int j;
2177 bool err;
2178 int nparam;
2179 bool nolist;
2180 bool casesense;
2181 int k, m;
2182 int offset;
2183 char *p, *pp;
2184 const char *mname;
2185 Include *inc;
2186 Context *ctx;
2187 Cond *cond;
2188 MMacro *mmac, **mmhead;
2189 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2190 Line *l;
2191 struct tokenval tokval;
2192 expr *evalresult;
2193 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2194 int64_t count;
2195 size_t len;
2196 int severity;
2198 origline = tline;
2200 skip_white_(tline);
2201 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2202 (tline->text[1] == '%' || tline->text[1] == '$'
2203 || tline->text[1] == '!'))
2204 return NO_DIRECTIVE_FOUND;
2206 i = pp_token_hash(tline->text);
2209 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2210 * since they are known to be buggy at moment, we need to fix them
2211 * in future release (2.09-2.10)
2213 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2214 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2215 tline->text);
2216 return NO_DIRECTIVE_FOUND;
2220 * If we're in a non-emitting branch of a condition construct,
2221 * or walking to the end of an already terminated %rep block,
2222 * we should ignore all directives except for condition
2223 * directives.
2225 if (((istk->conds && !emitting(istk->conds->state)) ||
2226 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2227 return NO_DIRECTIVE_FOUND;
2231 * If we're defining a macro or reading a %rep block, we should
2232 * ignore all directives except for %macro/%imacro (which nest),
2233 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2234 * If we're in a %rep block, another %rep nests, so should be let through.
2236 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2237 i != PP_RMACRO && i != PP_IRMACRO &&
2238 i != PP_ENDMACRO && i != PP_ENDM &&
2239 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2240 return NO_DIRECTIVE_FOUND;
2243 if (defining) {
2244 if (i == PP_MACRO || i == PP_IMACRO ||
2245 i == PP_RMACRO || i == PP_IRMACRO) {
2246 nested_mac_count++;
2247 return NO_DIRECTIVE_FOUND;
2248 } else if (nested_mac_count > 0) {
2249 if (i == PP_ENDMACRO) {
2250 nested_mac_count--;
2251 return NO_DIRECTIVE_FOUND;
2254 if (!defining->name) {
2255 if (i == PP_REP) {
2256 nested_rep_count++;
2257 return NO_DIRECTIVE_FOUND;
2258 } else if (nested_rep_count > 0) {
2259 if (i == PP_ENDREP) {
2260 nested_rep_count--;
2261 return NO_DIRECTIVE_FOUND;
2267 switch (i) {
2268 case PP_INVALID:
2269 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2270 tline->text);
2271 return NO_DIRECTIVE_FOUND; /* didn't get it */
2273 case PP_STACKSIZE:
2274 /* Directive to tell NASM what the default stack size is. The
2275 * default is for a 16-bit stack, and this can be overriden with
2276 * %stacksize large.
2278 tline = tline->next;
2279 if (tline && tline->type == TOK_WHITESPACE)
2280 tline = tline->next;
2281 if (!tline || tline->type != TOK_ID) {
2282 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2283 free_tlist(origline);
2284 return DIRECTIVE_FOUND;
2286 if (nasm_stricmp(tline->text, "flat") == 0) {
2287 /* All subsequent ARG directives are for a 32-bit stack */
2288 StackSize = 4;
2289 StackPointer = "ebp";
2290 ArgOffset = 8;
2291 LocalOffset = 0;
2292 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2293 /* All subsequent ARG directives are for a 64-bit stack */
2294 StackSize = 8;
2295 StackPointer = "rbp";
2296 ArgOffset = 16;
2297 LocalOffset = 0;
2298 } else if (nasm_stricmp(tline->text, "large") == 0) {
2299 /* All subsequent ARG directives are for a 16-bit stack,
2300 * far function call.
2302 StackSize = 2;
2303 StackPointer = "bp";
2304 ArgOffset = 4;
2305 LocalOffset = 0;
2306 } else if (nasm_stricmp(tline->text, "small") == 0) {
2307 /* All subsequent ARG directives are for a 16-bit stack,
2308 * far function call. We don't support near functions.
2310 StackSize = 2;
2311 StackPointer = "bp";
2312 ArgOffset = 6;
2313 LocalOffset = 0;
2314 } else {
2315 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2316 free_tlist(origline);
2317 return DIRECTIVE_FOUND;
2319 free_tlist(origline);
2320 return DIRECTIVE_FOUND;
2322 case PP_ARG:
2323 /* TASM like ARG directive to define arguments to functions, in
2324 * the following form:
2326 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2328 offset = ArgOffset;
2329 do {
2330 char *arg, directive[256];
2331 int size = StackSize;
2333 /* Find the argument name */
2334 tline = tline->next;
2335 if (tline && tline->type == TOK_WHITESPACE)
2336 tline = tline->next;
2337 if (!tline || tline->type != TOK_ID) {
2338 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2339 free_tlist(origline);
2340 return DIRECTIVE_FOUND;
2342 arg = tline->text;
2344 /* Find the argument size type */
2345 tline = tline->next;
2346 if (!tline || tline->type != TOK_OTHER
2347 || tline->text[0] != ':') {
2348 error(ERR_NONFATAL,
2349 "Syntax error processing `%%arg' directive");
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
2353 tline = tline->next;
2354 if (!tline || tline->type != TOK_ID) {
2355 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2356 free_tlist(origline);
2357 return DIRECTIVE_FOUND;
2360 /* Allow macro expansion of type parameter */
2361 tt = tokenize(tline->text);
2362 tt = expand_smacro(tt);
2363 size = parse_size(tt->text);
2364 if (!size) {
2365 error(ERR_NONFATAL,
2366 "Invalid size type for `%%arg' missing directive");
2367 free_tlist(tt);
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2371 free_tlist(tt);
2373 /* Round up to even stack slots */
2374 size = ALIGN(size, StackSize);
2376 /* Now define the macro for the argument */
2377 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2378 arg, StackPointer, offset);
2379 do_directive(tokenize(directive));
2380 offset += size;
2382 /* Move to the next argument in the list */
2383 tline = tline->next;
2384 if (tline && tline->type == TOK_WHITESPACE)
2385 tline = tline->next;
2386 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2387 ArgOffset = offset;
2388 free_tlist(origline);
2389 return DIRECTIVE_FOUND;
2391 case PP_LOCAL:
2392 /* TASM like LOCAL directive to define local variables for a
2393 * function, in the following form:
2395 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2397 * The '= LocalSize' at the end is ignored by NASM, but is
2398 * required by TASM to define the local parameter size (and used
2399 * by the TASM macro package).
2401 offset = LocalOffset;
2402 do {
2403 char *local, directive[256];
2404 int size = StackSize;
2406 /* Find the argument name */
2407 tline = tline->next;
2408 if (tline && tline->type == TOK_WHITESPACE)
2409 tline = tline->next;
2410 if (!tline || tline->type != TOK_ID) {
2411 error(ERR_NONFATAL,
2412 "`%%local' missing argument parameter");
2413 free_tlist(origline);
2414 return DIRECTIVE_FOUND;
2416 local = tline->text;
2418 /* Find the argument size type */
2419 tline = tline->next;
2420 if (!tline || tline->type != TOK_OTHER
2421 || tline->text[0] != ':') {
2422 error(ERR_NONFATAL,
2423 "Syntax error processing `%%local' directive");
2424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2427 tline = tline->next;
2428 if (!tline || tline->type != TOK_ID) {
2429 error(ERR_NONFATAL,
2430 "`%%local' missing size type parameter");
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
2435 /* Allow macro expansion of type parameter */
2436 tt = tokenize(tline->text);
2437 tt = expand_smacro(tt);
2438 size = parse_size(tt->text);
2439 if (!size) {
2440 error(ERR_NONFATAL,
2441 "Invalid size type for `%%local' missing directive");
2442 free_tlist(tt);
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2446 free_tlist(tt);
2448 /* Round up to even stack slots */
2449 size = ALIGN(size, StackSize);
2451 offset += size; /* Negative offset, increment before */
2453 /* Now define the macro for the argument */
2454 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2455 local, StackPointer, offset);
2456 do_directive(tokenize(directive));
2458 /* Now define the assign to setup the enter_c macro correctly */
2459 snprintf(directive, sizeof(directive),
2460 "%%assign %%$localsize %%$localsize+%d", size);
2461 do_directive(tokenize(directive));
2463 /* Move to the next argument in the list */
2464 tline = tline->next;
2465 if (tline && tline->type == TOK_WHITESPACE)
2466 tline = tline->next;
2467 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2468 LocalOffset = offset;
2469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2472 case PP_CLEAR:
2473 if (tline->next)
2474 error(ERR_WARNING|ERR_PASS1,
2475 "trailing garbage after `%%clear' ignored");
2476 free_macros();
2477 init_macros();
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2481 case PP_DEPEND:
2482 t = tline->next = expand_smacro(tline->next);
2483 skip_white_(t);
2484 if (!t || (t->type != TOK_STRING &&
2485 t->type != TOK_INTERNAL_STRING)) {
2486 error(ERR_NONFATAL, "`%%depend' expects a file name");
2487 free_tlist(origline);
2488 return DIRECTIVE_FOUND; /* but we did _something_ */
2490 if (t->next)
2491 error(ERR_WARNING|ERR_PASS1,
2492 "trailing garbage after `%%depend' ignored");
2493 p = t->text;
2494 if (t->type != TOK_INTERNAL_STRING)
2495 nasm_unquote_cstr(p, i);
2496 if (dephead && !in_list(*dephead, p)) {
2497 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2498 sl->next = NULL;
2499 strcpy(sl->str, p);
2500 *deptail = sl;
2501 deptail = &sl->next;
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2506 case PP_INCLUDE:
2507 t = tline->next = expand_smacro(tline->next);
2508 skip_white_(t);
2510 if (!t || (t->type != TOK_STRING &&
2511 t->type != TOK_INTERNAL_STRING)) {
2512 error(ERR_NONFATAL, "`%%include' expects a file name");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND; /* but we did _something_ */
2516 if (t->next)
2517 error(ERR_WARNING|ERR_PASS1,
2518 "trailing garbage after `%%include' ignored");
2519 p = t->text;
2520 if (t->type != TOK_INTERNAL_STRING)
2521 nasm_unquote_cstr(p, i);
2522 inc = nasm_malloc(sizeof(Include));
2523 inc->next = istk;
2524 inc->conds = NULL;
2525 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2526 if (!inc->fp) {
2527 /* -MG given but file not found */
2528 nasm_free(inc);
2529 } else {
2530 inc->fname = src_set_fname(nasm_strdup(p));
2531 inc->lineno = src_set_linnum(0);
2532 inc->lineinc = 1;
2533 inc->expansion = NULL;
2534 inc->mstk = NULL;
2535 istk = inc;
2536 list->uplevel(LIST_INCLUDE);
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2541 case PP_USE:
2543 static macros_t *use_pkg;
2544 const char *pkg_macro = NULL;
2546 tline = tline->next;
2547 skip_white_(tline);
2548 tline = expand_id(tline);
2550 if (!tline || (tline->type != TOK_STRING &&
2551 tline->type != TOK_INTERNAL_STRING &&
2552 tline->type != TOK_ID)) {
2553 error(ERR_NONFATAL, "`%%use' expects a package name");
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND; /* but we did _something_ */
2557 if (tline->next)
2558 error(ERR_WARNING|ERR_PASS1,
2559 "trailing garbage after `%%use' ignored");
2560 if (tline->type == TOK_STRING)
2561 nasm_unquote_cstr(tline->text, i);
2562 use_pkg = nasm_stdmac_find_package(tline->text);
2563 if (!use_pkg)
2564 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2565 else
2566 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2567 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2568 /* Not already included, go ahead and include it */
2569 stdmacpos = use_pkg;
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2574 case PP_PUSH:
2575 case PP_REPL:
2576 case PP_POP:
2577 tline = tline->next;
2578 skip_white_(tline);
2579 tline = expand_id(tline);
2580 if (tline) {
2581 if (!tok_type_(tline, TOK_ID)) {
2582 error(ERR_NONFATAL, "`%s' expects a context identifier",
2583 pp_directives[i]);
2584 free_tlist(origline);
2585 return DIRECTIVE_FOUND; /* but we did _something_ */
2587 if (tline->next)
2588 error(ERR_WARNING|ERR_PASS1,
2589 "trailing garbage after `%s' ignored",
2590 pp_directives[i]);
2591 p = nasm_strdup(tline->text);
2592 } else {
2593 p = NULL; /* Anonymous */
2596 if (i == PP_PUSH) {
2597 ctx = nasm_malloc(sizeof(Context));
2598 ctx->next = cstk;
2599 hash_init(&ctx->localmac, HASH_SMALL);
2600 ctx->name = p;
2601 ctx->number = unique++;
2602 cstk = ctx;
2603 } else {
2604 /* %pop or %repl */
2605 if (!cstk) {
2606 error(ERR_NONFATAL, "`%s': context stack is empty",
2607 pp_directives[i]);
2608 } else if (i == PP_POP) {
2609 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2610 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2611 "expected %s",
2612 cstk->name ? cstk->name : "anonymous", p);
2613 else
2614 ctx_pop();
2615 } else {
2616 /* i == PP_REPL */
2617 nasm_free(cstk->name);
2618 cstk->name = p;
2619 p = NULL;
2621 nasm_free(p);
2623 free_tlist(origline);
2624 return DIRECTIVE_FOUND;
2625 case PP_FATAL:
2626 severity = ERR_FATAL;
2627 goto issue_error;
2628 case PP_ERROR:
2629 severity = ERR_NONFATAL;
2630 goto issue_error;
2631 case PP_WARNING:
2632 severity = ERR_WARNING|ERR_WARN_USER;
2633 goto issue_error;
2635 issue_error:
2637 /* Only error out if this is the final pass */
2638 if (pass != 2 && i != PP_FATAL)
2639 return DIRECTIVE_FOUND;
2641 tline->next = expand_smacro(tline->next);
2642 tline = tline->next;
2643 skip_white_(tline);
2644 t = tline ? tline->next : NULL;
2645 skip_white_(t);
2646 if (tok_type_(tline, TOK_STRING) && !t) {
2647 /* The line contains only a quoted string */
2648 p = tline->text;
2649 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2650 error(severity, "%s", p);
2651 } else {
2652 /* Not a quoted string, or more than a quoted string */
2653 p = detoken(tline, false);
2654 error(severity, "%s", p);
2655 nasm_free(p);
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
2661 CASE_PP_IF:
2662 if (istk->conds && !emitting(istk->conds->state))
2663 j = COND_NEVER;
2664 else {
2665 j = if_condition(tline->next, i);
2666 tline->next = NULL; /* it got freed */
2667 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2669 cond = nasm_malloc(sizeof(Cond));
2670 cond->next = istk->conds;
2671 cond->state = j;
2672 istk->conds = cond;
2673 if(istk->mstk)
2674 istk->mstk->condcnt ++;
2675 free_tlist(origline);
2676 return DIRECTIVE_FOUND;
2678 CASE_PP_ELIF:
2679 if (!istk->conds)
2680 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2681 switch(istk->conds->state) {
2682 case COND_IF_TRUE:
2683 istk->conds->state = COND_DONE;
2684 break;
2686 case COND_DONE:
2687 case COND_NEVER:
2688 break;
2690 case COND_ELSE_TRUE:
2691 case COND_ELSE_FALSE:
2692 error_precond(ERR_WARNING|ERR_PASS1,
2693 "`%%elif' after `%%else' ignored");
2694 istk->conds->state = COND_NEVER;
2695 break;
2697 case COND_IF_FALSE:
2699 * IMPORTANT: In the case of %if, we will already have
2700 * called expand_mmac_params(); however, if we're
2701 * processing an %elif we must have been in a
2702 * non-emitting mode, which would have inhibited
2703 * the normal invocation of expand_mmac_params().
2704 * Therefore, we have to do it explicitly here.
2706 j = if_condition(expand_mmac_params(tline->next), i);
2707 tline->next = NULL; /* it got freed */
2708 istk->conds->state =
2709 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2710 break;
2712 free_tlist(origline);
2713 return DIRECTIVE_FOUND;
2715 case PP_ELSE:
2716 if (tline->next)
2717 error_precond(ERR_WARNING|ERR_PASS1,
2718 "trailing garbage after `%%else' ignored");
2719 if (!istk->conds)
2720 error(ERR_FATAL, "`%%else': no matching `%%if'");
2721 switch(istk->conds->state) {
2722 case COND_IF_TRUE:
2723 case COND_DONE:
2724 istk->conds->state = COND_ELSE_FALSE;
2725 break;
2727 case COND_NEVER:
2728 break;
2730 case COND_IF_FALSE:
2731 istk->conds->state = COND_ELSE_TRUE;
2732 break;
2734 case COND_ELSE_TRUE:
2735 case COND_ELSE_FALSE:
2736 error_precond(ERR_WARNING|ERR_PASS1,
2737 "`%%else' after `%%else' ignored.");
2738 istk->conds->state = COND_NEVER;
2739 break;
2741 free_tlist(origline);
2742 return DIRECTIVE_FOUND;
2744 case PP_ENDIF:
2745 if (tline->next)
2746 error_precond(ERR_WARNING|ERR_PASS1,
2747 "trailing garbage after `%%endif' ignored");
2748 if (!istk->conds)
2749 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2750 cond = istk->conds;
2751 istk->conds = cond->next;
2752 nasm_free(cond);
2753 if(istk->mstk)
2754 istk->mstk->condcnt --;
2755 free_tlist(origline);
2756 return DIRECTIVE_FOUND;
2758 case PP_RMACRO:
2759 case PP_IRMACRO:
2760 case PP_MACRO:
2761 case PP_IMACRO:
2762 if (defining) {
2763 error(ERR_FATAL, "`%s': already defining a macro",
2764 pp_directives[i]);
2765 return DIRECTIVE_FOUND;
2767 defining = nasm_malloc(sizeof(MMacro));
2768 defining->max_depth =
2769 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2770 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2771 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2772 nasm_free(defining);
2773 defining = NULL;
2774 return DIRECTIVE_FOUND;
2777 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2778 while (mmac) {
2779 if (!strcmp(mmac->name, defining->name) &&
2780 (mmac->nparam_min <= defining->nparam_max
2781 || defining->plus)
2782 && (defining->nparam_min <= mmac->nparam_max
2783 || mmac->plus)) {
2784 error(ERR_WARNING|ERR_PASS1,
2785 "redefining multi-line macro `%s'", defining->name);
2786 return DIRECTIVE_FOUND;
2788 mmac = mmac->next;
2790 free_tlist(origline);
2791 return DIRECTIVE_FOUND;
2793 case PP_ENDM:
2794 case PP_ENDMACRO:
2795 if (! (defining && defining->name)) {
2796 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2797 return DIRECTIVE_FOUND;
2799 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2800 defining->next = *mmhead;
2801 *mmhead = defining;
2802 defining = NULL;
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
2806 case PP_EXITMACRO:
2808 * We must search along istk->expansion until we hit a
2809 * macro-end marker for a macro with a name. Then we
2810 * bypass all lines between exitmacro and endmacro.
2812 list_for_each(l, istk->expansion)
2813 if (l->finishes && l->finishes->name)
2814 break;
2816 if (l) {
2818 * Remove all conditional entries relative to this
2819 * macro invocation. (safe to do in this context)
2821 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2822 cond = istk->conds;
2823 istk->conds = cond->next;
2824 nasm_free(cond);
2826 istk->expansion = l;
2827 } else {
2828 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2830 free_tlist(origline);
2831 return DIRECTIVE_FOUND;
2833 case PP_UNMACRO:
2834 case PP_UNIMACRO:
2836 MMacro **mmac_p;
2837 MMacro spec;
2839 spec.casesense = (i == PP_UNMACRO);
2840 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2841 return DIRECTIVE_FOUND;
2843 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2844 while (mmac_p && *mmac_p) {
2845 mmac = *mmac_p;
2846 if (mmac->casesense == spec.casesense &&
2847 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2848 mmac->nparam_min == spec.nparam_min &&
2849 mmac->nparam_max == spec.nparam_max &&
2850 mmac->plus == spec.plus) {
2851 *mmac_p = mmac->next;
2852 free_mmacro(mmac);
2853 } else {
2854 mmac_p = &mmac->next;
2857 free_tlist(origline);
2858 free_tlist(spec.dlist);
2859 return DIRECTIVE_FOUND;
2862 case PP_ROTATE:
2863 if (tline->next && tline->next->type == TOK_WHITESPACE)
2864 tline = tline->next;
2865 if (!tline->next) {
2866 free_tlist(origline);
2867 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2868 return DIRECTIVE_FOUND;
2870 t = expand_smacro(tline->next);
2871 tline->next = NULL;
2872 free_tlist(origline);
2873 tline = t;
2874 tptr = &t;
2875 tokval.t_type = TOKEN_INVALID;
2876 evalresult =
2877 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2878 free_tlist(tline);
2879 if (!evalresult)
2880 return DIRECTIVE_FOUND;
2881 if (tokval.t_type)
2882 error(ERR_WARNING|ERR_PASS1,
2883 "trailing garbage after expression ignored");
2884 if (!is_simple(evalresult)) {
2885 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2886 return DIRECTIVE_FOUND;
2888 mmac = istk->mstk;
2889 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2890 mmac = mmac->next_active;
2891 if (!mmac) {
2892 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2893 } else if (mmac->nparam == 0) {
2894 error(ERR_NONFATAL,
2895 "`%%rotate' invoked within macro without parameters");
2896 } else {
2897 int rotate = mmac->rotate + reloc_value(evalresult);
2899 rotate %= (int)mmac->nparam;
2900 if (rotate < 0)
2901 rotate += mmac->nparam;
2903 mmac->rotate = rotate;
2905 return DIRECTIVE_FOUND;
2907 case PP_REP:
2908 nolist = false;
2909 do {
2910 tline = tline->next;
2911 } while (tok_type_(tline, TOK_WHITESPACE));
2913 if (tok_type_(tline, TOK_ID) &&
2914 nasm_stricmp(tline->text, ".nolist") == 0) {
2915 nolist = true;
2916 do {
2917 tline = tline->next;
2918 } while (tok_type_(tline, TOK_WHITESPACE));
2921 if (tline) {
2922 t = expand_smacro(tline);
2923 tptr = &t;
2924 tokval.t_type = TOKEN_INVALID;
2925 evalresult =
2926 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2927 if (!evalresult) {
2928 free_tlist(origline);
2929 return DIRECTIVE_FOUND;
2931 if (tokval.t_type)
2932 error(ERR_WARNING|ERR_PASS1,
2933 "trailing garbage after expression ignored");
2934 if (!is_simple(evalresult)) {
2935 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2936 return DIRECTIVE_FOUND;
2938 count = reloc_value(evalresult);
2939 if (count >= REP_LIMIT) {
2940 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2941 count = 0;
2942 } else
2943 count++;
2944 } else {
2945 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2946 count = 0;
2948 free_tlist(origline);
2950 tmp_defining = defining;
2951 defining = nasm_malloc(sizeof(MMacro));
2952 defining->prev = NULL;
2953 defining->name = NULL; /* flags this macro as a %rep block */
2954 defining->casesense = false;
2955 defining->plus = false;
2956 defining->nolist = nolist;
2957 defining->in_progress = count;
2958 defining->max_depth = 0;
2959 defining->nparam_min = defining->nparam_max = 0;
2960 defining->defaults = NULL;
2961 defining->dlist = NULL;
2962 defining->expansion = NULL;
2963 defining->next_active = istk->mstk;
2964 defining->rep_nest = tmp_defining;
2965 return DIRECTIVE_FOUND;
2967 case PP_ENDREP:
2968 if (!defining || defining->name) {
2969 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2970 return DIRECTIVE_FOUND;
2974 * Now we have a "macro" defined - although it has no name
2975 * and we won't be entering it in the hash tables - we must
2976 * push a macro-end marker for it on to istk->expansion.
2977 * After that, it will take care of propagating itself (a
2978 * macro-end marker line for a macro which is really a %rep
2979 * block will cause the macro to be re-expanded, complete
2980 * with another macro-end marker to ensure the process
2981 * continues) until the whole expansion is forcibly removed
2982 * from istk->expansion by a %exitrep.
2984 l = nasm_malloc(sizeof(Line));
2985 l->next = istk->expansion;
2986 l->finishes = defining;
2987 l->first = NULL;
2988 istk->expansion = l;
2990 istk->mstk = defining;
2992 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2993 tmp_defining = defining;
2994 defining = defining->rep_nest;
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
2998 case PP_EXITREP:
3000 * We must search along istk->expansion until we hit a
3001 * macro-end marker for a macro with no name. Then we set
3002 * its `in_progress' flag to 0.
3004 list_for_each(l, istk->expansion)
3005 if (l->finishes && !l->finishes->name)
3006 break;
3008 if (l)
3009 l->finishes->in_progress = 1;
3010 else
3011 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
3015 case PP_XDEFINE:
3016 case PP_IXDEFINE:
3017 case PP_DEFINE:
3018 case PP_IDEFINE:
3019 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3021 tline = tline->next;
3022 skip_white_(tline);
3023 tline = expand_id(tline);
3024 if (!tline || (tline->type != TOK_ID &&
3025 (tline->type != TOK_PREPROC_ID ||
3026 tline->text[1] != '$'))) {
3027 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3028 pp_directives[i]);
3029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3033 ctx = get_ctx(tline->text, &mname, false);
3034 last = tline;
3035 param_start = tline = tline->next;
3036 nparam = 0;
3038 /* Expand the macro definition now for %xdefine and %ixdefine */
3039 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3040 tline = expand_smacro(tline);
3042 if (tok_is_(tline, "(")) {
3044 * This macro has parameters.
3047 tline = tline->next;
3048 while (1) {
3049 skip_white_(tline);
3050 if (!tline) {
3051 error(ERR_NONFATAL, "parameter identifier expected");
3052 free_tlist(origline);
3053 return DIRECTIVE_FOUND;
3055 if (tline->type != TOK_ID) {
3056 error(ERR_NONFATAL,
3057 "`%s': parameter identifier expected",
3058 tline->text);
3059 free_tlist(origline);
3060 return DIRECTIVE_FOUND;
3062 tline->type = TOK_SMAC_PARAM + nparam++;
3063 tline = tline->next;
3064 skip_white_(tline);
3065 if (tok_is_(tline, ",")) {
3066 tline = tline->next;
3067 } else {
3068 if (!tok_is_(tline, ")")) {
3069 error(ERR_NONFATAL,
3070 "`)' expected to terminate macro template");
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3074 break;
3077 last = tline;
3078 tline = tline->next;
3080 if (tok_type_(tline, TOK_WHITESPACE))
3081 last = tline, tline = tline->next;
3082 macro_start = NULL;
3083 last->next = NULL;
3084 t = tline;
3085 while (t) {
3086 if (t->type == TOK_ID) {
3087 list_for_each(tt, param_start)
3088 if (tt->type >= TOK_SMAC_PARAM &&
3089 !strcmp(tt->text, t->text))
3090 t->type = tt->type;
3092 tt = t->next;
3093 t->next = macro_start;
3094 macro_start = t;
3095 t = tt;
3098 * Good. We now have a macro name, a parameter count, and a
3099 * token list (in reverse order) for an expansion. We ought
3100 * to be OK just to create an SMacro, store it, and let
3101 * free_tlist have the rest of the line (which we have
3102 * carefully re-terminated after chopping off the expansion
3103 * from the end).
3105 define_smacro(ctx, mname, casesense, nparam, macro_start);
3106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
3109 case PP_UNDEF:
3110 tline = tline->next;
3111 skip_white_(tline);
3112 tline = expand_id(tline);
3113 if (!tline || (tline->type != TOK_ID &&
3114 (tline->type != TOK_PREPROC_ID ||
3115 tline->text[1] != '$'))) {
3116 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
3120 if (tline->next) {
3121 error(ERR_WARNING|ERR_PASS1,
3122 "trailing garbage after macro name ignored");
3125 /* Find the context that symbol belongs to */
3126 ctx = get_ctx(tline->text, &mname, false);
3127 undef_smacro(ctx, mname);
3128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3131 case PP_DEFSTR:
3132 case PP_IDEFSTR:
3133 casesense = (i == PP_DEFSTR);
3135 tline = tline->next;
3136 skip_white_(tline);
3137 tline = expand_id(tline);
3138 if (!tline || (tline->type != TOK_ID &&
3139 (tline->type != TOK_PREPROC_ID ||
3140 tline->text[1] != '$'))) {
3141 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3142 pp_directives[i]);
3143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3147 ctx = get_ctx(tline->text, &mname, false);
3148 last = tline;
3149 tline = expand_smacro(tline->next);
3150 last->next = NULL;
3152 while (tok_type_(tline, TOK_WHITESPACE))
3153 tline = delete_Token(tline);
3155 p = detoken(tline, false);
3156 macro_start = nasm_malloc(sizeof(*macro_start));
3157 macro_start->next = NULL;
3158 macro_start->text = nasm_quote(p, strlen(p));
3159 macro_start->type = TOK_STRING;
3160 macro_start->a.mac = NULL;
3161 nasm_free(p);
3164 * We now have a macro name, an implicit parameter count of
3165 * zero, and a string token to use as an expansion. Create
3166 * and store an SMacro.
3168 define_smacro(ctx, mname, casesense, 0, macro_start);
3169 free_tlist(origline);
3170 return DIRECTIVE_FOUND;
3172 case PP_DEFTOK:
3173 case PP_IDEFTOK:
3174 casesense = (i == PP_DEFTOK);
3176 tline = tline->next;
3177 skip_white_(tline);
3178 tline = expand_id(tline);
3179 if (!tline || (tline->type != TOK_ID &&
3180 (tline->type != TOK_PREPROC_ID ||
3181 tline->text[1] != '$'))) {
3182 error(ERR_NONFATAL,
3183 "`%s' expects a macro identifier as first parameter",
3184 pp_directives[i]);
3185 free_tlist(origline);
3186 return DIRECTIVE_FOUND;
3188 ctx = get_ctx(tline->text, &mname, false);
3189 last = tline;
3190 tline = expand_smacro(tline->next);
3191 last->next = NULL;
3193 t = tline;
3194 while (tok_type_(t, TOK_WHITESPACE))
3195 t = t->next;
3196 /* t should now point to the string */
3197 if (!tok_type_(t, TOK_STRING)) {
3198 error(ERR_NONFATAL,
3199 "`%s` requires string as second parameter",
3200 pp_directives[i]);
3201 free_tlist(tline);
3202 free_tlist(origline);
3203 return DIRECTIVE_FOUND;
3207 * Convert the string to a token stream. Note that smacros
3208 * are stored with the token stream reversed, so we have to
3209 * reverse the output of tokenize().
3211 nasm_unquote_cstr(t->text, i);
3212 macro_start = reverse_tokens(tokenize(t->text));
3215 * We now have a macro name, an implicit parameter count of
3216 * zero, and a numeric token to use as an expansion. Create
3217 * and store an SMacro.
3219 define_smacro(ctx, mname, casesense, 0, macro_start);
3220 free_tlist(tline);
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3224 case PP_PATHSEARCH:
3226 FILE *fp;
3227 StrList *xsl = NULL;
3228 StrList **xst = &xsl;
3230 casesense = true;
3232 tline = tline->next;
3233 skip_white_(tline);
3234 tline = expand_id(tline);
3235 if (!tline || (tline->type != TOK_ID &&
3236 (tline->type != TOK_PREPROC_ID ||
3237 tline->text[1] != '$'))) {
3238 error(ERR_NONFATAL,
3239 "`%%pathsearch' expects a macro identifier as first parameter");
3240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
3243 ctx = get_ctx(tline->text, &mname, false);
3244 last = tline;
3245 tline = expand_smacro(tline->next);
3246 last->next = NULL;
3248 t = tline;
3249 while (tok_type_(t, TOK_WHITESPACE))
3250 t = t->next;
3252 if (!t || (t->type != TOK_STRING &&
3253 t->type != TOK_INTERNAL_STRING)) {
3254 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3255 free_tlist(tline);
3256 free_tlist(origline);
3257 return DIRECTIVE_FOUND; /* but we did _something_ */
3259 if (t->next)
3260 error(ERR_WARNING|ERR_PASS1,
3261 "trailing garbage after `%%pathsearch' ignored");
3262 p = t->text;
3263 if (t->type != TOK_INTERNAL_STRING)
3264 nasm_unquote(p, NULL);
3266 fp = inc_fopen(p, &xsl, &xst, true);
3267 if (fp) {
3268 p = xsl->str;
3269 fclose(fp); /* Don't actually care about the file */
3271 macro_start = nasm_malloc(sizeof(*macro_start));
3272 macro_start->next = NULL;
3273 macro_start->text = nasm_quote(p, strlen(p));
3274 macro_start->type = TOK_STRING;
3275 macro_start->a.mac = NULL;
3276 if (xsl)
3277 nasm_free(xsl);
3280 * We now have a macro name, an implicit parameter count of
3281 * zero, and a string token to use as an expansion. Create
3282 * and store an SMacro.
3284 define_smacro(ctx, mname, casesense, 0, macro_start);
3285 free_tlist(tline);
3286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3290 case PP_STRLEN:
3291 casesense = true;
3293 tline = tline->next;
3294 skip_white_(tline);
3295 tline = expand_id(tline);
3296 if (!tline || (tline->type != TOK_ID &&
3297 (tline->type != TOK_PREPROC_ID ||
3298 tline->text[1] != '$'))) {
3299 error(ERR_NONFATAL,
3300 "`%%strlen' expects a macro identifier as first parameter");
3301 free_tlist(origline);
3302 return DIRECTIVE_FOUND;
3304 ctx = get_ctx(tline->text, &mname, false);
3305 last = tline;
3306 tline = expand_smacro(tline->next);
3307 last->next = NULL;
3309 t = tline;
3310 while (tok_type_(t, TOK_WHITESPACE))
3311 t = t->next;
3312 /* t should now point to the string */
3313 if (!tok_type_(t, TOK_STRING)) {
3314 error(ERR_NONFATAL,
3315 "`%%strlen` requires string as second parameter");
3316 free_tlist(tline);
3317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3321 macro_start = nasm_malloc(sizeof(*macro_start));
3322 macro_start->next = NULL;
3323 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3324 macro_start->a.mac = NULL;
3327 * We now have a macro name, an implicit parameter count of
3328 * zero, and a numeric token to use as an expansion. Create
3329 * and store an SMacro.
3331 define_smacro(ctx, mname, casesense, 0, macro_start);
3332 free_tlist(tline);
3333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
3336 case PP_STRCAT:
3337 casesense = true;
3339 tline = tline->next;
3340 skip_white_(tline);
3341 tline = expand_id(tline);
3342 if (!tline || (tline->type != TOK_ID &&
3343 (tline->type != TOK_PREPROC_ID ||
3344 tline->text[1] != '$'))) {
3345 error(ERR_NONFATAL,
3346 "`%%strcat' expects a macro identifier as first parameter");
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
3350 ctx = get_ctx(tline->text, &mname, false);
3351 last = tline;
3352 tline = expand_smacro(tline->next);
3353 last->next = NULL;
3355 len = 0;
3356 list_for_each(t, tline) {
3357 switch (t->type) {
3358 case TOK_WHITESPACE:
3359 break;
3360 case TOK_STRING:
3361 len += t->a.len = nasm_unquote(t->text, NULL);
3362 break;
3363 case TOK_OTHER:
3364 if (!strcmp(t->text, ",")) /* permit comma separators */
3365 break;
3366 /* else fall through */
3367 default:
3368 error(ERR_NONFATAL,
3369 "non-string passed to `%%strcat' (%d)", t->type);
3370 free_tlist(tline);
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3376 p = pp = nasm_malloc(len);
3377 list_for_each(t, tline) {
3378 if (t->type == TOK_STRING) {
3379 memcpy(p, t->text, t->a.len);
3380 p += t->a.len;
3385 * We now have a macro name, an implicit parameter count of
3386 * zero, and a numeric token to use as an expansion. Create
3387 * and store an SMacro.
3389 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3390 macro_start->text = nasm_quote(pp, len);
3391 nasm_free(pp);
3392 define_smacro(ctx, mname, casesense, 0, macro_start);
3393 free_tlist(tline);
3394 free_tlist(origline);
3395 return DIRECTIVE_FOUND;
3397 case PP_SUBSTR:
3399 int64_t start, count;
3400 size_t len;
3402 casesense = true;
3404 tline = tline->next;
3405 skip_white_(tline);
3406 tline = expand_id(tline);
3407 if (!tline || (tline->type != TOK_ID &&
3408 (tline->type != TOK_PREPROC_ID ||
3409 tline->text[1] != '$'))) {
3410 error(ERR_NONFATAL,
3411 "`%%substr' expects a macro identifier as first parameter");
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
3415 ctx = get_ctx(tline->text, &mname, false);
3416 last = tline;
3417 tline = expand_smacro(tline->next);
3418 last->next = NULL;
3420 if (tline) /* skip expanded id */
3421 t = tline->next;
3422 while (tok_type_(t, TOK_WHITESPACE))
3423 t = t->next;
3425 /* t should now point to the string */
3426 if (!tok_type_(t, TOK_STRING)) {
3427 error(ERR_NONFATAL,
3428 "`%%substr` requires string as second parameter");
3429 free_tlist(tline);
3430 free_tlist(origline);
3431 return DIRECTIVE_FOUND;
3434 tt = t->next;
3435 tptr = &tt;
3436 tokval.t_type = TOKEN_INVALID;
3437 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3438 pass, error, NULL);
3439 if (!evalresult) {
3440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3443 } else if (!is_simple(evalresult)) {
3444 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3445 free_tlist(tline);
3446 free_tlist(origline);
3447 return DIRECTIVE_FOUND;
3449 start = evalresult->value - 1;
3451 while (tok_type_(tt, TOK_WHITESPACE))
3452 tt = tt->next;
3453 if (!tt) {
3454 count = 1; /* Backwards compatibility: one character */
3455 } else {
3456 tokval.t_type = TOKEN_INVALID;
3457 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3458 pass, error, NULL);
3459 if (!evalresult) {
3460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463 } else if (!is_simple(evalresult)) {
3464 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3469 count = evalresult->value;
3472 len = nasm_unquote(t->text, NULL);
3474 /* make start and count being in range */
3475 if (start < 0)
3476 start = 0;
3477 if (count < 0)
3478 count = len + count + 1 - start;
3479 if (start + count > (int64_t)len)
3480 count = len - start;
3481 if (!len || count < 0 || start >=(int64_t)len)
3482 start = -1, count = 0; /* empty string */
3484 macro_start = nasm_malloc(sizeof(*macro_start));
3485 macro_start->next = NULL;
3486 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3487 macro_start->type = TOK_STRING;
3488 macro_start->a.mac = NULL;
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a numeric token to use as an expansion. Create
3493 * and store an SMacro.
3495 define_smacro(ctx, mname, casesense, 0, macro_start);
3496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
3501 case PP_ASSIGN:
3502 case PP_IASSIGN:
3503 casesense = (i == PP_ASSIGN);
3505 tline = tline->next;
3506 skip_white_(tline);
3507 tline = expand_id(tline);
3508 if (!tline || (tline->type != TOK_ID &&
3509 (tline->type != TOK_PREPROC_ID ||
3510 tline->text[1] != '$'))) {
3511 error(ERR_NONFATAL,
3512 "`%%%sassign' expects a macro identifier",
3513 (i == PP_IASSIGN ? "i" : ""));
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3517 ctx = get_ctx(tline->text, &mname, false);
3518 last = tline;
3519 tline = expand_smacro(tline->next);
3520 last->next = NULL;
3522 t = tline;
3523 tptr = &t;
3524 tokval.t_type = TOKEN_INVALID;
3525 evalresult =
3526 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3527 free_tlist(tline);
3528 if (!evalresult) {
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3533 if (tokval.t_type)
3534 error(ERR_WARNING|ERR_PASS1,
3535 "trailing garbage after expression ignored");
3537 if (!is_simple(evalresult)) {
3538 error(ERR_NONFATAL,
3539 "non-constant value given to `%%%sassign'",
3540 (i == PP_IASSIGN ? "i" : ""));
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3545 macro_start = nasm_malloc(sizeof(*macro_start));
3546 macro_start->next = NULL;
3547 make_tok_num(macro_start, reloc_value(evalresult));
3548 macro_start->a.mac = NULL;
3551 * We now have a macro name, an implicit parameter count of
3552 * zero, and a numeric token to use as an expansion. Create
3553 * and store an SMacro.
3555 define_smacro(ctx, mname, casesense, 0, macro_start);
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3559 case PP_LINE:
3561 * Syntax is `%line nnn[+mmm] [filename]'
3563 tline = tline->next;
3564 skip_white_(tline);
3565 if (!tok_type_(tline, TOK_NUMBER)) {
3566 error(ERR_NONFATAL, "`%%line' expects line number");
3567 free_tlist(origline);
3568 return DIRECTIVE_FOUND;
3570 k = readnum(tline->text, &err);
3571 m = 1;
3572 tline = tline->next;
3573 if (tok_is_(tline, "+")) {
3574 tline = tline->next;
3575 if (!tok_type_(tline, TOK_NUMBER)) {
3576 error(ERR_NONFATAL, "`%%line' expects line increment");
3577 free_tlist(origline);
3578 return DIRECTIVE_FOUND;
3580 m = readnum(tline->text, &err);
3581 tline = tline->next;
3583 skip_white_(tline);
3584 src_set_linnum(k);
3585 istk->lineinc = m;
3586 if (tline) {
3587 nasm_free(src_set_fname(detoken(tline, false)));
3589 free_tlist(origline);
3590 return DIRECTIVE_FOUND;
3592 default:
3593 error(ERR_FATAL,
3594 "preprocessor directive `%s' not yet implemented",
3595 pp_directives[i]);
3596 return DIRECTIVE_FOUND;
3601 * Ensure that a macro parameter contains a condition code and
3602 * nothing else. Return the condition code index if so, or -1
3603 * otherwise.
3605 static int find_cc(Token * t)
3607 Token *tt;
3608 int i, j, k, m;
3610 if (!t)
3611 return -1; /* Probably a %+ without a space */
3613 skip_white_(t);
3614 if (t->type != TOK_ID)
3615 return -1;
3616 tt = t->next;
3617 skip_white_(tt);
3618 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3619 return -1;
3621 i = -1;
3622 j = ARRAY_SIZE(conditions);
3623 while (j - i > 1) {
3624 k = (j + i) / 2;
3625 m = nasm_stricmp(t->text, conditions[k]);
3626 if (m == 0) {
3627 i = k;
3628 j = -2;
3629 break;
3630 } else if (m < 0) {
3631 j = k;
3632 } else
3633 i = k;
3635 if (j != -2)
3636 return -1;
3637 return i;
3640 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3642 Token **tail, *t, *tt;
3643 Token **paste_head;
3644 bool did_paste = false;
3645 char *tmp;
3647 /* Now handle token pasting... */
3648 paste_head = NULL;
3649 tail = head;
3650 while ((t = *tail) && (tt = t->next)) {
3651 switch (t->type) {
3652 case TOK_WHITESPACE:
3653 if (tt->type == TOK_WHITESPACE) {
3654 /* Zap adjacent whitespace tokens */
3655 t->next = delete_Token(tt);
3656 } else {
3657 /* Do not advance paste_head here */
3658 tail = &t->next;
3660 break;
3661 case TOK_ID:
3662 case TOK_NUMBER:
3663 case TOK_FLOAT:
3665 size_t len = 0;
3666 char *tmp, *p;
3668 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3669 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3670 tt->type == TOK_OTHER)) {
3671 len += strlen(tt->text);
3672 tt = tt->next;
3676 * Now tt points to the first token after
3677 * the potential paste area...
3679 if (tt != t->next) {
3680 /* We have at least two tokens... */
3681 len += strlen(t->text);
3682 p = tmp = nasm_malloc(len+1);
3684 while (t != tt) {
3685 strcpy(p, t->text);
3686 p = strchr(p, '\0');
3687 t = delete_Token(t);
3690 t = *tail = tokenize(tmp);
3691 nasm_free(tmp);
3693 while (t->next) {
3694 tail = &t->next;
3695 t = t->next;
3697 t->next = tt; /* Attach the remaining token chain */
3699 did_paste = true;
3701 paste_head = tail;
3702 tail = &t->next;
3703 break;
3705 case TOK_PASTE: /* %+ */
3706 if (handle_paste_tokens) {
3707 /* Zap %+ and whitespace tokens to the right */
3708 while (t && (t->type == TOK_WHITESPACE ||
3709 t->type == TOK_PASTE))
3710 t = *tail = delete_Token(t);
3711 if (!paste_head || !t)
3712 break; /* Nothing to paste with */
3713 tail = paste_head;
3714 t = *tail;
3715 tt = t->next;
3716 while (tok_type_(tt, TOK_WHITESPACE))
3717 tt = t->next = delete_Token(tt);
3719 if (tt) {
3720 tmp = nasm_strcat(t->text, tt->text);
3721 delete_Token(t);
3722 tt = delete_Token(tt);
3723 t = *tail = tokenize(tmp);
3724 nasm_free(tmp);
3725 while (t->next) {
3726 tail = &t->next;
3727 t = t->next;
3729 t->next = tt; /* Attach the remaining token chain */
3730 did_paste = true;
3732 paste_head = tail;
3733 tail = &t->next;
3734 break;
3736 /* else fall through */
3737 default:
3738 tail = &t->next;
3739 if (!tok_type_(t->next, TOK_WHITESPACE))
3740 paste_head = tail;
3741 break;
3744 return did_paste;
3748 * expands to a list of tokens from %{x:y}
3750 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3752 Token *t = tline, **tt, *tm, *head;
3753 char *pos;
3754 int fst, lst, j, i;
3756 pos = strchr(tline->text, ':');
3757 nasm_assert(pos);
3759 lst = atoi(pos + 1);
3760 fst = atoi(tline->text + 1);
3763 * only macros params are accounted so
3764 * if someone passes %0 -- we reject such
3765 * value(s)
3767 if (lst == 0 || fst == 0)
3768 goto err;
3770 /* the values should be sane */
3771 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3772 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3773 goto err;
3775 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3776 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3778 /* counted from zero */
3779 fst--, lst--;
3782 * it will be at least one token
3784 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3785 t = new_Token(NULL, tm->type, tm->text, 0);
3786 head = t, tt = &t->next;
3787 if (fst < lst) {
3788 for (i = fst + 1; i <= lst; i++) {
3789 t = new_Token(NULL, TOK_OTHER, ",", 0);
3790 *tt = t, tt = &t->next;
3791 j = (i + mac->rotate) % mac->nparam;
3792 tm = mac->params[j];
3793 t = new_Token(NULL, tm->type, tm->text, 0);
3794 *tt = t, tt = &t->next;
3796 } else {
3797 for (i = fst - 1; i >= lst; i--) {
3798 t = new_Token(NULL, TOK_OTHER, ",", 0);
3799 *tt = t, tt = &t->next;
3800 j = (i + mac->rotate) % mac->nparam;
3801 tm = mac->params[j];
3802 t = new_Token(NULL, tm->type, tm->text, 0);
3803 *tt = t, tt = &t->next;
3807 *last = tt;
3808 return head;
3810 err:
3811 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3812 &tline->text[1]);
3813 return tline;
3817 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3818 * %-n) and MMacro-local identifiers (%%foo) as well as
3819 * macro indirection (%[...]) and range (%{..:..}).
3821 static Token *expand_mmac_params(Token * tline)
3823 Token *t, *tt, **tail, *thead;
3824 bool changed = false;
3825 char *pos;
3827 tail = &thead;
3828 thead = NULL;
3830 while (tline) {
3831 if (tline->type == TOK_PREPROC_ID &&
3832 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3833 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3834 tline->text[1] == '%')) {
3835 char *text = NULL;
3836 int type = 0, cc; /* type = 0 to placate optimisers */
3837 char tmpbuf[30];
3838 unsigned int n;
3839 int i;
3840 MMacro *mac;
3842 t = tline;
3843 tline = tline->next;
3845 mac = istk->mstk;
3846 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3847 mac = mac->next_active;
3848 if (!mac) {
3849 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3850 } else {
3851 pos = strchr(t->text, ':');
3852 if (!pos) {
3853 switch (t->text[1]) {
3855 * We have to make a substitution of one of the
3856 * forms %1, %-1, %+1, %%foo, %0.
3858 case '0':
3859 type = TOK_NUMBER;
3860 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3861 text = nasm_strdup(tmpbuf);
3862 break;
3863 case '%':
3864 type = TOK_ID;
3865 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3866 mac->unique);
3867 text = nasm_strcat(tmpbuf, t->text + 2);
3868 break;
3869 case '-':
3870 n = atoi(t->text + 2) - 1;
3871 if (n >= mac->nparam)
3872 tt = NULL;
3873 else {
3874 if (mac->nparam > 1)
3875 n = (n + mac->rotate) % mac->nparam;
3876 tt = mac->params[n];
3878 cc = find_cc(tt);
3879 if (cc == -1) {
3880 error(ERR_NONFATAL,
3881 "macro parameter %d is not a condition code",
3882 n + 1);
3883 text = NULL;
3884 } else {
3885 type = TOK_ID;
3886 if (inverse_ccs[cc] == -1) {
3887 error(ERR_NONFATAL,
3888 "condition code `%s' is not invertible",
3889 conditions[cc]);
3890 text = NULL;
3891 } else
3892 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3894 break;
3895 case '+':
3896 n = atoi(t->text + 2) - 1;
3897 if (n >= mac->nparam)
3898 tt = NULL;
3899 else {
3900 if (mac->nparam > 1)
3901 n = (n + mac->rotate) % mac->nparam;
3902 tt = mac->params[n];
3904 cc = find_cc(tt);
3905 if (cc == -1) {
3906 error(ERR_NONFATAL,
3907 "macro parameter %d is not a condition code",
3908 n + 1);
3909 text = NULL;
3910 } else {
3911 type = TOK_ID;
3912 text = nasm_strdup(conditions[cc]);
3914 break;
3915 default:
3916 n = atoi(t->text + 1) - 1;
3917 if (n >= mac->nparam)
3918 tt = NULL;
3919 else {
3920 if (mac->nparam > 1)
3921 n = (n + mac->rotate) % mac->nparam;
3922 tt = mac->params[n];
3924 if (tt) {
3925 for (i = 0; i < mac->paramlen[n]; i++) {
3926 *tail = new_Token(NULL, tt->type, tt->text, 0);
3927 tail = &(*tail)->next;
3928 tt = tt->next;
3931 text = NULL; /* we've done it here */
3932 break;
3934 } else {
3936 * seems we have a parameters range here
3938 Token *head, **last;
3939 head = expand_mmac_params_range(mac, t, &last);
3940 if (head != t) {
3941 *tail = head;
3942 *last = tline;
3943 tline = head;
3944 text = NULL;
3948 if (!text) {
3949 delete_Token(t);
3950 } else {
3951 *tail = t;
3952 tail = &t->next;
3953 t->type = type;
3954 nasm_free(t->text);
3955 t->text = text;
3956 t->a.mac = NULL;
3958 changed = true;
3959 continue;
3960 } else if (tline->type == TOK_INDIRECT) {
3961 t = tline;
3962 tline = tline->next;
3963 tt = tokenize(t->text);
3964 tt = expand_mmac_params(tt);
3965 tt = expand_smacro(tt);
3966 *tail = tt;
3967 while (tt) {
3968 tt->a.mac = NULL; /* Necessary? */
3969 tail = &tt->next;
3970 tt = tt->next;
3972 delete_Token(t);
3973 changed = true;
3974 } else {
3975 t = *tail = tline;
3976 tline = tline->next;
3977 t->a.mac = NULL;
3978 tail = &t->next;
3981 *tail = NULL;
3983 if (changed)
3984 paste_tokens(&thead, false);
3986 return thead;
3990 * Expand all single-line macro calls made in the given line.
3991 * Return the expanded version of the line. The original is deemed
3992 * to be destroyed in the process. (In reality we'll just move
3993 * Tokens from input to output a lot of the time, rather than
3994 * actually bothering to destroy and replicate.)
3997 static Token *expand_smacro(Token * tline)
3999 Token *t, *tt, *mstart, **tail, *thead;
4000 SMacro *head = NULL, *m;
4001 Token **params;
4002 int *paramsize;
4003 unsigned int nparam, sparam;
4004 int brackets;
4005 Token *org_tline = tline;
4006 Context *ctx;
4007 const char *mname;
4008 int deadman = DEADMAN_LIMIT;
4009 bool expanded;
4012 * Trick: we should avoid changing the start token pointer since it can
4013 * be contained in "next" field of other token. Because of this
4014 * we allocate a copy of first token and work with it; at the end of
4015 * routine we copy it back
4017 if (org_tline) {
4018 tline = new_Token(org_tline->next, org_tline->type,
4019 org_tline->text, 0);
4020 tline->a.mac = org_tline->a.mac;
4021 nasm_free(org_tline->text);
4022 org_tline->text = NULL;
4025 expanded = true; /* Always expand %+ at least once */
4027 again:
4028 thead = NULL;
4029 tail = &thead;
4031 while (tline) { /* main token loop */
4032 if (!--deadman) {
4033 error(ERR_NONFATAL, "interminable macro recursion");
4034 goto err;
4037 if ((mname = tline->text)) {
4038 /* if this token is a local macro, look in local context */
4039 if (tline->type == TOK_ID) {
4040 head = (SMacro *)hash_findix(&smacros, mname);
4041 } else if (tline->type == TOK_PREPROC_ID) {
4042 ctx = get_ctx(mname, &mname, true);
4043 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4044 } else
4045 head = NULL;
4048 * We've hit an identifier. As in is_mmacro below, we first
4049 * check whether the identifier is a single-line macro at
4050 * all, then think about checking for parameters if
4051 * necessary.
4053 list_for_each(m, head)
4054 if (!mstrcmp(m->name, mname, m->casesense))
4055 break;
4056 if (m) {
4057 mstart = tline;
4058 params = NULL;
4059 paramsize = NULL;
4060 if (m->nparam == 0) {
4062 * Simple case: the macro is parameterless. Discard the
4063 * one token that the macro call took, and push the
4064 * expansion back on the to-do stack.
4066 if (!m->expansion) {
4067 if (!strcmp("__FILE__", m->name)) {
4068 int32_t num = 0;
4069 char *file = NULL;
4070 src_get(&num, &file);
4071 tline->text = nasm_quote(file, strlen(file));
4072 tline->type = TOK_STRING;
4073 nasm_free(file);
4074 continue;
4076 if (!strcmp("__LINE__", m->name)) {
4077 nasm_free(tline->text);
4078 make_tok_num(tline, src_get_linnum());
4079 continue;
4081 if (!strcmp("__BITS__", m->name)) {
4082 nasm_free(tline->text);
4083 make_tok_num(tline, globalbits);
4084 continue;
4086 tline = delete_Token(tline);
4087 continue;
4089 } else {
4091 * Complicated case: at least one macro with this name
4092 * exists and takes parameters. We must find the
4093 * parameters in the call, count them, find the SMacro
4094 * that corresponds to that form of the macro call, and
4095 * substitute for the parameters when we expand. What a
4096 * pain.
4098 /*tline = tline->next;
4099 skip_white_(tline); */
4100 do {
4101 t = tline->next;
4102 while (tok_type_(t, TOK_SMAC_END)) {
4103 t->a.mac->in_progress = false;
4104 t->text = NULL;
4105 t = tline->next = delete_Token(t);
4107 tline = t;
4108 } while (tok_type_(tline, TOK_WHITESPACE));
4109 if (!tok_is_(tline, "(")) {
4111 * This macro wasn't called with parameters: ignore
4112 * the call. (Behaviour borrowed from gnu cpp.)
4114 tline = mstart;
4115 m = NULL;
4116 } else {
4117 int paren = 0;
4118 int white = 0;
4119 brackets = 0;
4120 nparam = 0;
4121 sparam = PARAM_DELTA;
4122 params = nasm_malloc(sparam * sizeof(Token *));
4123 params[0] = tline->next;
4124 paramsize = nasm_malloc(sparam * sizeof(int));
4125 paramsize[0] = 0;
4126 while (true) { /* parameter loop */
4128 * For some unusual expansions
4129 * which concatenates function call
4131 t = tline->next;
4132 while (tok_type_(t, TOK_SMAC_END)) {
4133 t->a.mac->in_progress = false;
4134 t->text = NULL;
4135 t = tline->next = delete_Token(t);
4137 tline = t;
4139 if (!tline) {
4140 error(ERR_NONFATAL,
4141 "macro call expects terminating `)'");
4142 break;
4144 if (tline->type == TOK_WHITESPACE
4145 && brackets <= 0) {
4146 if (paramsize[nparam])
4147 white++;
4148 else
4149 params[nparam] = tline->next;
4150 continue; /* parameter loop */
4152 if (tline->type == TOK_OTHER
4153 && tline->text[1] == 0) {
4154 char ch = tline->text[0];
4155 if (ch == ',' && !paren && brackets <= 0) {
4156 if (++nparam >= sparam) {
4157 sparam += PARAM_DELTA;
4158 params = nasm_realloc(params,
4159 sparam * sizeof(Token *));
4160 paramsize = nasm_realloc(paramsize,
4161 sparam * sizeof(int));
4163 params[nparam] = tline->next;
4164 paramsize[nparam] = 0;
4165 white = 0;
4166 continue; /* parameter loop */
4168 if (ch == '{' &&
4169 (brackets > 0 || (brackets == 0 &&
4170 !paramsize[nparam])))
4172 if (!(brackets++)) {
4173 params[nparam] = tline->next;
4174 continue; /* parameter loop */
4177 if (ch == '}' && brackets > 0)
4178 if (--brackets == 0) {
4179 brackets = -1;
4180 continue; /* parameter loop */
4182 if (ch == '(' && !brackets)
4183 paren++;
4184 if (ch == ')' && brackets <= 0)
4185 if (--paren < 0)
4186 break;
4188 if (brackets < 0) {
4189 brackets = 0;
4190 error(ERR_NONFATAL, "braces do not "
4191 "enclose all of macro parameter");
4193 paramsize[nparam] += white + 1;
4194 white = 0;
4195 } /* parameter loop */
4196 nparam++;
4197 while (m && (m->nparam != nparam ||
4198 mstrcmp(m->name, mname,
4199 m->casesense)))
4200 m = m->next;
4201 if (!m)
4202 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4203 "macro `%s' exists, "
4204 "but not taking %d parameters",
4205 mstart->text, nparam);
4208 if (m && m->in_progress)
4209 m = NULL;
4210 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4212 * Design question: should we handle !tline, which
4213 * indicates missing ')' here, or expand those
4214 * macros anyway, which requires the (t) test a few
4215 * lines down?
4217 nasm_free(params);
4218 nasm_free(paramsize);
4219 tline = mstart;
4220 } else {
4222 * Expand the macro: we are placed on the last token of the
4223 * call, so that we can easily split the call from the
4224 * following tokens. We also start by pushing an SMAC_END
4225 * token for the cycle removal.
4227 t = tline;
4228 if (t) {
4229 tline = t->next;
4230 t->next = NULL;
4232 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4233 tt->a.mac = m;
4234 m->in_progress = true;
4235 tline = tt;
4236 list_for_each(t, m->expansion) {
4237 if (t->type >= TOK_SMAC_PARAM) {
4238 Token *pcopy = tline, **ptail = &pcopy;
4239 Token *ttt, *pt;
4240 int i;
4242 ttt = params[t->type - TOK_SMAC_PARAM];
4243 i = paramsize[t->type - TOK_SMAC_PARAM];
4244 while (--i >= 0) {
4245 pt = *ptail = new_Token(tline, ttt->type,
4246 ttt->text, 0);
4247 ptail = &pt->next;
4248 ttt = ttt->next;
4250 tline = pcopy;
4251 } else if (t->type == TOK_PREPROC_Q) {
4252 tt = new_Token(tline, TOK_ID, mname, 0);
4253 tline = tt;
4254 } else if (t->type == TOK_PREPROC_QQ) {
4255 tt = new_Token(tline, TOK_ID, m->name, 0);
4256 tline = tt;
4257 } else {
4258 tt = new_Token(tline, t->type, t->text, 0);
4259 tline = tt;
4264 * Having done that, get rid of the macro call, and clean
4265 * up the parameters.
4267 nasm_free(params);
4268 nasm_free(paramsize);
4269 free_tlist(mstart);
4270 expanded = true;
4271 continue; /* main token loop */
4276 if (tline->type == TOK_SMAC_END) {
4277 tline->a.mac->in_progress = false;
4278 tline = delete_Token(tline);
4279 } else {
4280 t = *tail = tline;
4281 tline = tline->next;
4282 t->a.mac = NULL;
4283 t->next = NULL;
4284 tail = &t->next;
4289 * Now scan the entire line and look for successive TOK_IDs that resulted
4290 * after expansion (they can't be produced by tokenize()). The successive
4291 * TOK_IDs should be concatenated.
4292 * Also we look for %+ tokens and concatenate the tokens before and after
4293 * them (without white spaces in between).
4295 if (expanded && paste_tokens(&thead, true)) {
4297 * If we concatenated something, *and* we had previously expanded
4298 * an actual macro, scan the lines again for macros...
4300 tline = thead;
4301 expanded = false;
4302 goto again;
4305 err:
4306 if (org_tline) {
4307 if (thead) {
4308 *org_tline = *thead;
4309 /* since we just gave text to org_line, don't free it */
4310 thead->text = NULL;
4311 delete_Token(thead);
4312 } else {
4313 /* the expression expanded to empty line;
4314 we can't return NULL for some reasons
4315 we just set the line to a single WHITESPACE token. */
4316 memset(org_tline, 0, sizeof(*org_tline));
4317 org_tline->text = NULL;
4318 org_tline->type = TOK_WHITESPACE;
4320 thead = org_tline;
4323 return thead;
4327 * Similar to expand_smacro but used exclusively with macro identifiers
4328 * right before they are fetched in. The reason is that there can be
4329 * identifiers consisting of several subparts. We consider that if there
4330 * are more than one element forming the name, user wants a expansion,
4331 * otherwise it will be left as-is. Example:
4333 * %define %$abc cde
4335 * the identifier %$abc will be left as-is so that the handler for %define
4336 * will suck it and define the corresponding value. Other case:
4338 * %define _%$abc cde
4340 * In this case user wants name to be expanded *before* %define starts
4341 * working, so we'll expand %$abc into something (if it has a value;
4342 * otherwise it will be left as-is) then concatenate all successive
4343 * PP_IDs into one.
4345 static Token *expand_id(Token * tline)
4347 Token *cur, *oldnext = NULL;
4349 if (!tline || !tline->next)
4350 return tline;
4352 cur = tline;
4353 while (cur->next &&
4354 (cur->next->type == TOK_ID ||
4355 cur->next->type == TOK_PREPROC_ID
4356 || cur->next->type == TOK_NUMBER))
4357 cur = cur->next;
4359 /* If identifier consists of just one token, don't expand */
4360 if (cur == tline)
4361 return tline;
4363 if (cur) {
4364 oldnext = cur->next; /* Detach the tail past identifier */
4365 cur->next = NULL; /* so that expand_smacro stops here */
4368 tline = expand_smacro(tline);
4370 if (cur) {
4371 /* expand_smacro possibly changhed tline; re-scan for EOL */
4372 cur = tline;
4373 while (cur && cur->next)
4374 cur = cur->next;
4375 if (cur)
4376 cur->next = oldnext;
4379 return tline;
4383 * Determine whether the given line constitutes a multi-line macro
4384 * call, and return the MMacro structure called if so. Doesn't have
4385 * to check for an initial label - that's taken care of in
4386 * expand_mmacro - but must check numbers of parameters. Guaranteed
4387 * to be called with tline->type == TOK_ID, so the putative macro
4388 * name is easy to find.
4390 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4392 MMacro *head, *m;
4393 Token **params;
4394 int nparam;
4396 head = (MMacro *) hash_findix(&mmacros, tline->text);
4399 * Efficiency: first we see if any macro exists with the given
4400 * name. If not, we can return NULL immediately. _Then_ we
4401 * count the parameters, and then we look further along the
4402 * list if necessary to find the proper MMacro.
4404 list_for_each(m, head)
4405 if (!mstrcmp(m->name, tline->text, m->casesense))
4406 break;
4407 if (!m)
4408 return NULL;
4411 * OK, we have a potential macro. Count and demarcate the
4412 * parameters.
4414 count_mmac_params(tline->next, &nparam, &params);
4417 * So we know how many parameters we've got. Find the MMacro
4418 * structure that handles this number.
4420 while (m) {
4421 if (m->nparam_min <= nparam
4422 && (m->plus || nparam <= m->nparam_max)) {
4424 * This one is right. Just check if cycle removal
4425 * prohibits us using it before we actually celebrate...
4427 if (m->in_progress > m->max_depth) {
4428 if (m->max_depth > 0) {
4429 error(ERR_WARNING,
4430 "reached maximum recursion depth of %i",
4431 m->max_depth);
4433 nasm_free(params);
4434 return NULL;
4437 * It's right, and we can use it. Add its default
4438 * parameters to the end of our list if necessary.
4440 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4441 params =
4442 nasm_realloc(params,
4443 ((m->nparam_min + m->ndefs +
4444 1) * sizeof(*params)));
4445 while (nparam < m->nparam_min + m->ndefs) {
4446 params[nparam] = m->defaults[nparam - m->nparam_min];
4447 nparam++;
4451 * If we've gone over the maximum parameter count (and
4452 * we're in Plus mode), ignore parameters beyond
4453 * nparam_max.
4455 if (m->plus && nparam > m->nparam_max)
4456 nparam = m->nparam_max;
4458 * Then terminate the parameter list, and leave.
4460 if (!params) { /* need this special case */
4461 params = nasm_malloc(sizeof(*params));
4462 nparam = 0;
4464 params[nparam] = NULL;
4465 *params_array = params;
4466 return m;
4469 * This one wasn't right: look for the next one with the
4470 * same name.
4472 list_for_each(m, m->next)
4473 if (!mstrcmp(m->name, tline->text, m->casesense))
4474 break;
4478 * After all that, we didn't find one with the right number of
4479 * parameters. Issue a warning, and fail to expand the macro.
4481 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4482 "macro `%s' exists, but not taking %d parameters",
4483 tline->text, nparam);
4484 nasm_free(params);
4485 return NULL;
4490 * Save MMacro invocation specific fields in
4491 * preparation for a recursive macro expansion
4493 static void push_mmacro(MMacro *m)
4495 MMacroInvocation *i;
4497 i = nasm_malloc(sizeof(MMacroInvocation));
4498 i->prev = m->prev;
4499 i->params = m->params;
4500 i->iline = m->iline;
4501 i->nparam = m->nparam;
4502 i->rotate = m->rotate;
4503 i->paramlen = m->paramlen;
4504 i->unique = m->unique;
4505 i->condcnt = m->condcnt;
4506 m->prev = i;
4511 * Restore MMacro invocation specific fields that were
4512 * saved during a previous recursive macro expansion
4514 static void pop_mmacro(MMacro *m)
4516 MMacroInvocation *i;
4518 if (m->prev) {
4519 i = m->prev;
4520 m->prev = i->prev;
4521 m->params = i->params;
4522 m->iline = i->iline;
4523 m->nparam = i->nparam;
4524 m->rotate = i->rotate;
4525 m->paramlen = i->paramlen;
4526 m->unique = i->unique;
4527 m->condcnt = i->condcnt;
4528 nasm_free(i);
4534 * Expand the multi-line macro call made by the given line, if
4535 * there is one to be expanded. If there is, push the expansion on
4536 * istk->expansion and return 1. Otherwise return 0.
4538 static int expand_mmacro(Token * tline)
4540 Token *startline = tline;
4541 Token *label = NULL;
4542 int dont_prepend = 0;
4543 Token **params, *t, *mtok, *tt;
4544 MMacro *m;
4545 Line *l, *ll;
4546 int i, nparam, *paramlen;
4547 const char *mname;
4549 t = tline;
4550 skip_white_(t);
4551 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4552 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4553 return 0;
4554 mtok = t;
4555 m = is_mmacro(t, &params);
4556 if (m) {
4557 mname = t->text;
4558 } else {
4559 Token *last;
4561 * We have an id which isn't a macro call. We'll assume
4562 * it might be a label; we'll also check to see if a
4563 * colon follows it. Then, if there's another id after
4564 * that lot, we'll check it again for macro-hood.
4566 label = last = t;
4567 t = t->next;
4568 if (tok_type_(t, TOK_WHITESPACE))
4569 last = t, t = t->next;
4570 if (tok_is_(t, ":")) {
4571 dont_prepend = 1;
4572 last = t, t = t->next;
4573 if (tok_type_(t, TOK_WHITESPACE))
4574 last = t, t = t->next;
4576 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4577 return 0;
4578 last->next = NULL;
4579 mname = t->text;
4580 tline = t;
4584 * Fix up the parameters: this involves stripping leading and
4585 * trailing whitespace, then stripping braces if they are
4586 * present.
4588 for (nparam = 0; params[nparam]; nparam++) ;
4589 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4591 for (i = 0; params[i]; i++) {
4592 int brace = false;
4593 int comma = (!m->plus || i < nparam - 1);
4595 t = params[i];
4596 skip_white_(t);
4597 if (tok_is_(t, "{"))
4598 t = t->next, brace = true, comma = false;
4599 params[i] = t;
4600 paramlen[i] = 0;
4601 while (t) {
4602 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4603 break; /* ... because we have hit a comma */
4604 if (comma && t->type == TOK_WHITESPACE
4605 && tok_is_(t->next, ","))
4606 break; /* ... or a space then a comma */
4607 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4608 break; /* ... or a brace */
4609 t = t->next;
4610 paramlen[i]++;
4615 * OK, we have a MMacro structure together with a set of
4616 * parameters. We must now go through the expansion and push
4617 * copies of each Line on to istk->expansion. Substitution of
4618 * parameter tokens and macro-local tokens doesn't get done
4619 * until the single-line macro substitution process; this is
4620 * because delaying them allows us to change the semantics
4621 * later through %rotate.
4623 * First, push an end marker on to istk->expansion, mark this
4624 * macro as in progress, and set up its invocation-specific
4625 * variables.
4627 ll = nasm_malloc(sizeof(Line));
4628 ll->next = istk->expansion;
4629 ll->finishes = m;
4630 ll->first = NULL;
4631 istk->expansion = ll;
4634 * Save the previous MMacro expansion in the case of
4635 * macro recursion
4637 if (m->max_depth && m->in_progress)
4638 push_mmacro(m);
4640 m->in_progress ++;
4641 m->params = params;
4642 m->iline = tline;
4643 m->nparam = nparam;
4644 m->rotate = 0;
4645 m->paramlen = paramlen;
4646 m->unique = unique++;
4647 m->lineno = 0;
4648 m->condcnt = 0;
4650 m->next_active = istk->mstk;
4651 istk->mstk = m;
4653 list_for_each(l, m->expansion) {
4654 Token **tail;
4656 ll = nasm_malloc(sizeof(Line));
4657 ll->finishes = NULL;
4658 ll->next = istk->expansion;
4659 istk->expansion = ll;
4660 tail = &ll->first;
4662 list_for_each(t, l->first) {
4663 Token *x = t;
4664 switch (t->type) {
4665 case TOK_PREPROC_Q:
4666 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4667 break;
4668 case TOK_PREPROC_QQ:
4669 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4670 break;
4671 case TOK_PREPROC_ID:
4672 if (t->text[1] == '0' && t->text[2] == '0') {
4673 dont_prepend = -1;
4674 x = label;
4675 if (!x)
4676 continue;
4678 /* fall through */
4679 default:
4680 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4681 break;
4683 tail = &tt->next;
4685 *tail = NULL;
4689 * If we had a label, push it on as the first line of
4690 * the macro expansion.
4692 if (label) {
4693 if (dont_prepend < 0)
4694 free_tlist(startline);
4695 else {
4696 ll = nasm_malloc(sizeof(Line));
4697 ll->finishes = NULL;
4698 ll->next = istk->expansion;
4699 istk->expansion = ll;
4700 ll->first = startline;
4701 if (!dont_prepend) {
4702 while (label->next)
4703 label = label->next;
4704 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4709 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4711 return 1;
4714 /* The function that actually does the error reporting */
4715 static void verror(int severity, const char *fmt, va_list arg)
4717 char buff[1024];
4719 vsnprintf(buff, sizeof(buff), fmt, arg);
4721 if (istk && istk->mstk && istk->mstk->name)
4722 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4723 istk->mstk->lineno, buff);
4724 else
4725 nasm_error(severity, "%s", buff);
4729 * Since preprocessor always operate only on the line that didn't
4730 * arrived yet, we should always use ERR_OFFBY1.
4732 static void error(int severity, const char *fmt, ...)
4734 va_list arg;
4736 /* If we're in a dead branch of IF or something like it, ignore the error */
4737 if (istk && istk->conds && !emitting(istk->conds->state))
4738 return;
4740 va_start(arg, fmt);
4741 verror(severity, fmt, arg);
4742 va_end(arg);
4746 * Because %else etc are evaluated in the state context
4747 * of the previous branch, errors might get lost with error():
4748 * %if 0 ... %else trailing garbage ... %endif
4749 * So %else etc should report errors with this function.
4751 static void error_precond(int severity, const char *fmt, ...)
4753 va_list arg;
4755 /* Only ignore the error if it's really in a dead branch */
4756 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4757 return;
4759 va_start(arg, fmt);
4760 verror(severity, fmt, arg);
4761 va_end(arg);
4764 static void
4765 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4767 Token *t;
4769 cstk = NULL;
4770 istk = nasm_malloc(sizeof(Include));
4771 istk->next = NULL;
4772 istk->conds = NULL;
4773 istk->expansion = NULL;
4774 istk->mstk = NULL;
4775 istk->fp = fopen(file, "r");
4776 istk->fname = NULL;
4777 src_set_fname(nasm_strdup(file));
4778 src_set_linnum(0);
4779 istk->lineinc = 1;
4780 if (!istk->fp)
4781 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4782 file);
4783 defining = NULL;
4784 nested_mac_count = 0;
4785 nested_rep_count = 0;
4786 init_macros();
4787 unique = 0;
4788 if (tasm_compatible_mode) {
4789 stdmacpos = nasm_stdmac;
4790 } else {
4791 stdmacpos = nasm_stdmac_after_tasm;
4793 any_extrastdmac = extrastdmac && *extrastdmac;
4794 do_predef = true;
4795 list = listgen;
4798 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4799 * The caller, however, will also pass in 3 for preprocess-only so
4800 * we can set __PASS__ accordingly.
4802 pass = apass > 2 ? 2 : apass;
4804 dephead = deptail = deplist;
4805 if (deplist) {
4806 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4807 sl->next = NULL;
4808 strcpy(sl->str, file);
4809 *deptail = sl;
4810 deptail = &sl->next;
4814 * Define the __PASS__ macro. This is defined here unlike
4815 * all the other builtins, because it is special -- it varies between
4816 * passes.
4818 t = nasm_malloc(sizeof(*t));
4819 t->next = NULL;
4820 make_tok_num(t, apass);
4821 t->a.mac = NULL;
4822 define_smacro(NULL, "__PASS__", true, 0, t);
4825 static char *pp_getline(void)
4827 char *line;
4828 Token *tline;
4830 while (1) {
4832 * Fetch a tokenized line, either from the macro-expansion
4833 * buffer or from the input file.
4835 tline = NULL;
4836 while (istk->expansion && istk->expansion->finishes) {
4837 Line *l = istk->expansion;
4838 if (!l->finishes->name && l->finishes->in_progress > 1) {
4839 Line *ll;
4842 * This is a macro-end marker for a macro with no
4843 * name, which means it's not really a macro at all
4844 * but a %rep block, and the `in_progress' field is
4845 * more than 1, meaning that we still need to
4846 * repeat. (1 means the natural last repetition; 0
4847 * means termination by %exitrep.) We have
4848 * therefore expanded up to the %endrep, and must
4849 * push the whole block on to the expansion buffer
4850 * again. We don't bother to remove the macro-end
4851 * marker: we'd only have to generate another one
4852 * if we did.
4854 l->finishes->in_progress--;
4855 list_for_each(l, l->finishes->expansion) {
4856 Token *t, *tt, **tail;
4858 ll = nasm_malloc(sizeof(Line));
4859 ll->next = istk->expansion;
4860 ll->finishes = NULL;
4861 ll->first = NULL;
4862 tail = &ll->first;
4864 list_for_each(t, l->first) {
4865 if (t->text || t->type == TOK_WHITESPACE) {
4866 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4867 tail = &tt->next;
4871 istk->expansion = ll;
4873 } else {
4875 * Check whether a `%rep' was started and not ended
4876 * within this macro expansion. This can happen and
4877 * should be detected. It's a fatal error because
4878 * I'm too confused to work out how to recover
4879 * sensibly from it.
4881 if (defining) {
4882 if (defining->name)
4883 error(ERR_PANIC,
4884 "defining with name in expansion");
4885 else if (istk->mstk->name)
4886 error(ERR_FATAL,
4887 "`%%rep' without `%%endrep' within"
4888 " expansion of macro `%s'",
4889 istk->mstk->name);
4893 * FIXME: investigate the relationship at this point between
4894 * istk->mstk and l->finishes
4897 MMacro *m = istk->mstk;
4898 istk->mstk = m->next_active;
4899 if (m->name) {
4901 * This was a real macro call, not a %rep, and
4902 * therefore the parameter information needs to
4903 * be freed.
4905 if (m->prev) {
4906 pop_mmacro(m);
4907 l->finishes->in_progress --;
4908 } else {
4909 nasm_free(m->params);
4910 free_tlist(m->iline);
4911 nasm_free(m->paramlen);
4912 l->finishes->in_progress = 0;
4914 } else
4915 free_mmacro(m);
4917 istk->expansion = l->next;
4918 nasm_free(l);
4919 list->downlevel(LIST_MACRO);
4922 while (1) { /* until we get a line we can use */
4924 if (istk->expansion) { /* from a macro expansion */
4925 char *p;
4926 Line *l = istk->expansion;
4927 if (istk->mstk)
4928 istk->mstk->lineno++;
4929 tline = l->first;
4930 istk->expansion = l->next;
4931 nasm_free(l);
4932 p = detoken(tline, false);
4933 list->line(LIST_MACRO, p);
4934 nasm_free(p);
4935 break;
4937 line = read_line();
4938 if (line) { /* from the current input file */
4939 line = prepreproc(line);
4940 tline = tokenize(line);
4941 nasm_free(line);
4942 break;
4945 * The current file has ended; work down the istk
4948 Include *i = istk;
4949 fclose(i->fp);
4950 if (i->conds) {
4951 /* nasm_error can't be conditionally suppressed */
4952 nasm_error(ERR_FATAL,
4953 "expected `%%endif' before end of file");
4955 /* only set line and file name if there's a next node */
4956 if (i->next) {
4957 src_set_linnum(i->lineno);
4958 nasm_free(src_set_fname(i->fname));
4960 istk = i->next;
4961 list->downlevel(LIST_INCLUDE);
4962 nasm_free(i);
4963 if (!istk)
4964 return NULL;
4965 if (istk->expansion && istk->expansion->finishes)
4966 break;
4971 * We must expand MMacro parameters and MMacro-local labels
4972 * _before_ we plunge into directive processing, to cope
4973 * with things like `%define something %1' such as STRUC
4974 * uses. Unless we're _defining_ a MMacro, in which case
4975 * those tokens should be left alone to go into the
4976 * definition; and unless we're in a non-emitting
4977 * condition, in which case we don't want to meddle with
4978 * anything.
4980 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4981 && !(istk->mstk && !istk->mstk->in_progress)) {
4982 tline = expand_mmac_params(tline);
4986 * Check the line to see if it's a preprocessor directive.
4988 if (do_directive(tline) == DIRECTIVE_FOUND) {
4989 continue;
4990 } else if (defining) {
4992 * We're defining a multi-line macro. We emit nothing
4993 * at all, and just
4994 * shove the tokenized line on to the macro definition.
4996 Line *l = nasm_malloc(sizeof(Line));
4997 l->next = defining->expansion;
4998 l->first = tline;
4999 l->finishes = NULL;
5000 defining->expansion = l;
5001 continue;
5002 } else if (istk->conds && !emitting(istk->conds->state)) {
5004 * We're in a non-emitting branch of a condition block.
5005 * Emit nothing at all, not even a blank line: when we
5006 * emerge from the condition we'll give a line-number
5007 * directive so we keep our place correctly.
5009 free_tlist(tline);
5010 continue;
5011 } else if (istk->mstk && !istk->mstk->in_progress) {
5013 * We're in a %rep block which has been terminated, so
5014 * we're walking through to the %endrep without
5015 * emitting anything. Emit nothing at all, not even a
5016 * blank line: when we emerge from the %rep block we'll
5017 * give a line-number directive so we keep our place
5018 * correctly.
5020 free_tlist(tline);
5021 continue;
5022 } else {
5023 tline = expand_smacro(tline);
5024 if (!expand_mmacro(tline)) {
5026 * De-tokenize the line again, and emit it.
5028 line = detoken(tline, true);
5029 free_tlist(tline);
5030 break;
5031 } else {
5032 continue; /* expand_mmacro calls free_tlist */
5037 return line;
5040 static void pp_cleanup(int pass)
5042 if (defining) {
5043 if (defining->name) {
5044 error(ERR_NONFATAL,
5045 "end of file while still defining macro `%s'",
5046 defining->name);
5047 } else {
5048 error(ERR_NONFATAL, "end of file while still in %%rep");
5051 free_mmacro(defining);
5052 defining = NULL;
5054 while (cstk)
5055 ctx_pop();
5056 free_macros();
5057 while (istk) {
5058 Include *i = istk;
5059 istk = istk->next;
5060 fclose(i->fp);
5061 nasm_free(i->fname);
5062 nasm_free(i);
5064 while (cstk)
5065 ctx_pop();
5066 nasm_free(src_set_fname(NULL));
5067 if (pass == 0) {
5068 IncPath *i;
5069 free_llist(predef);
5070 delete_Blocks();
5071 while ((i = ipath)) {
5072 ipath = i->next;
5073 if (i->path)
5074 nasm_free(i->path);
5075 nasm_free(i);
5080 void pp_include_path(char *path)
5082 IncPath *i;
5084 i = nasm_malloc(sizeof(IncPath));
5085 i->path = path ? nasm_strdup(path) : NULL;
5086 i->next = NULL;
5088 if (ipath) {
5089 IncPath *j = ipath;
5090 while (j->next)
5091 j = j->next;
5092 j->next = i;
5093 } else {
5094 ipath = i;
5098 void pp_pre_include(char *fname)
5100 Token *inc, *space, *name;
5101 Line *l;
5103 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5104 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5105 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5107 l = nasm_malloc(sizeof(Line));
5108 l->next = predef;
5109 l->first = inc;
5110 l->finishes = NULL;
5111 predef = l;
5114 void pp_pre_define(char *definition)
5116 Token *def, *space;
5117 Line *l;
5118 char *equals;
5120 equals = strchr(definition, '=');
5121 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5122 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5123 if (equals)
5124 *equals = ' ';
5125 space->next = tokenize(definition);
5126 if (equals)
5127 *equals = '=';
5129 l = nasm_malloc(sizeof(Line));
5130 l->next = predef;
5131 l->first = def;
5132 l->finishes = NULL;
5133 predef = l;
5136 void pp_pre_undefine(char *definition)
5138 Token *def, *space;
5139 Line *l;
5141 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5142 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5143 space->next = tokenize(definition);
5145 l = nasm_malloc(sizeof(Line));
5146 l->next = predef;
5147 l->first = def;
5148 l->finishes = NULL;
5149 predef = l;
5153 * Added by Keith Kanios:
5155 * This function is used to assist with "runtime" preprocessor
5156 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5158 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5159 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5162 void pp_runtime(char *definition)
5164 Token *def;
5166 def = tokenize(definition);
5167 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5168 free_tlist(def);
5172 void pp_extra_stdmac(macros_t *macros)
5174 extrastdmac = macros;
5177 static void make_tok_num(Token * tok, int64_t val)
5179 char numbuf[20];
5180 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5181 tok->text = nasm_strdup(numbuf);
5182 tok->type = TOK_NUMBER;
5185 Preproc nasmpp = {
5186 pp_reset,
5187 pp_getline,
5188 pp_cleanup