test: Add br3385573 testcase
[nasm/avx512.git] / preproc.c
blobac39bb1bcb307c49ec913fce91a18ce1ea2c41e5
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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 #define PP_CONCAT_MASK(x) (1 << (x))
217 struct tokseq_match {
218 int mask_head;
219 int mask_tail;
222 struct Token {
223 Token *next;
224 char *text;
225 union {
226 SMacro *mac; /* associated macro for TOK_SMAC_END */
227 size_t len; /* scratch length field */
228 } a; /* Auxiliary data */
229 enum pp_token_type type;
233 * Multi-line macro definitions are stored as a linked list of
234 * these, which is essentially a container to allow several linked
235 * lists of Tokens.
237 * Note that in this module, linked lists are treated as stacks
238 * wherever possible. For this reason, Lines are _pushed_ on to the
239 * `expansion' field in MMacro structures, so that the linked list,
240 * if walked, would give the macro lines in reverse order; this
241 * means that we can walk the list when expanding a macro, and thus
242 * push the lines on to the `expansion' field in _istk_ in reverse
243 * order (so that when popped back off they are in the right
244 * order). It may seem cockeyed, and it relies on my design having
245 * an even number of steps in, but it works...
247 * Some of these structures, rather than being actual lines, are
248 * markers delimiting the end of the expansion of a given macro.
249 * This is for use in the cycle-tracking and %rep-handling code.
250 * Such structures have `finishes' non-NULL, and `first' NULL. All
251 * others have `finishes' NULL, but `first' may still be NULL if
252 * the line is blank.
254 struct Line {
255 Line *next;
256 MMacro *finishes;
257 Token *first;
261 * To handle an arbitrary level of file inclusion, we maintain a
262 * stack (ie linked list) of these things.
264 struct Include {
265 Include *next;
266 FILE *fp;
267 Cond *conds;
268 Line *expansion;
269 char *fname;
270 int lineno, lineinc;
271 MMacro *mstk; /* stack of active macros/reps */
275 * Include search path. This is simply a list of strings which get
276 * prepended, in turn, to the name of an include file, in an
277 * attempt to find the file if it's not in the current directory.
279 struct IncPath {
280 IncPath *next;
281 char *path;
285 * Conditional assembly: we maintain a separate stack of these for
286 * each level of file inclusion. (The only reason we keep the
287 * stacks separate is to ensure that a stray `%endif' in a file
288 * included from within the true branch of a `%if' won't terminate
289 * it and cause confusion: instead, rightly, it'll cause an error.)
291 struct Cond {
292 Cond *next;
293 int state;
295 enum {
297 * These states are for use just after %if or %elif: IF_TRUE
298 * means the condition has evaluated to truth so we are
299 * currently emitting, whereas IF_FALSE means we are not
300 * currently emitting but will start doing so if a %else comes
301 * up. In these states, all directives are admissible: %elif,
302 * %else and %endif. (And of course %if.)
304 COND_IF_TRUE, COND_IF_FALSE,
306 * These states come up after a %else: ELSE_TRUE means we're
307 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
308 * any %elif or %else will cause an error.
310 COND_ELSE_TRUE, COND_ELSE_FALSE,
312 * These states mean that we're not emitting now, and also that
313 * nothing until %endif will be emitted at all. COND_DONE is
314 * used when we've had our moment of emission
315 * and have now started seeing %elifs. COND_NEVER is used when
316 * the condition construct in question is contained within a
317 * non-emitting branch of a larger condition construct,
318 * or if there is an error.
320 COND_DONE, COND_NEVER
322 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325 * These defines are used as the possible return values for do_directive
327 #define NO_DIRECTIVE_FOUND 0
328 #define DIRECTIVE_FOUND 1
331 * This define sets the upper limit for smacro and recursive mmacro
332 * expansions
334 #define DEADMAN_LIMIT (1 << 20)
336 /* max reps */
337 #define REP_LIMIT ((INT64_C(1) << 62))
340 * Condition codes. Note that we use c_ prefix not C_ because C_ is
341 * used in nasm.h for the "real" condition codes. At _this_ level,
342 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
343 * ones, so we need a different enum...
345 static const char * const conditions[] = {
346 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
347 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
348 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
350 enum pp_conds {
351 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
352 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
353 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
354 c_none = -1
356 static const enum pp_conds inverse_ccs[] = {
357 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
358 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,
359 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
363 * Directive names.
365 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
366 static int is_condition(enum preproc_token arg)
368 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
371 /* For TASM compatibility we need to be able to recognise TASM compatible
372 * conditional compilation directives. Using the NASM pre-processor does
373 * not work, so we look for them specifically from the following list and
374 * then jam in the equivalent NASM directive into the input stream.
377 enum {
378 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
379 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
382 static const char * const tasm_directives[] = {
383 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
384 "ifndef", "include", "local"
387 static int StackSize = 4;
388 static char *StackPointer = "ebp";
389 static int ArgOffset = 8;
390 static int LocalOffset = 0;
392 static Context *cstk;
393 static Include *istk;
394 static IncPath *ipath = NULL;
396 static int pass; /* HACK: pass 0 = generate dependencies only */
397 static StrList **dephead, **deptail; /* Dependency list */
399 static uint64_t unique; /* unique identifier numbers */
401 static Line *predef = NULL;
402 static bool do_predef;
404 static ListGen *list;
407 * The current set of multi-line macros we have defined.
409 static struct hash_table mmacros;
412 * The current set of single-line macros we have defined.
414 static struct hash_table smacros;
417 * The multi-line macro we are currently defining, or the %rep
418 * block we are currently reading, if any.
420 static MMacro *defining;
422 static uint64_t nested_mac_count;
423 static uint64_t nested_rep_count;
426 * The number of macro parameters to allocate space for at a time.
428 #define PARAM_DELTA 16
431 * The standard macro set: defined in macros.c in the array nasm_stdmac.
432 * This gives our position in the macro set, when we're processing it.
434 static macros_t *stdmacpos;
437 * The extra standard macros that come from the object format, if
438 * any.
440 static macros_t *extrastdmac = NULL;
441 static bool any_extrastdmac;
444 * Tokens are allocated in blocks to improve speed
446 #define TOKEN_BLOCKSIZE 4096
447 static Token *freeTokens = NULL;
448 struct Blocks {
449 Blocks *next;
450 void *chunk;
453 static Blocks blocks = { NULL, NULL };
456 * Forward declarations.
458 static Token *expand_mmac_params(Token * tline);
459 static Token *expand_smacro(Token * tline);
460 static Token *expand_id(Token * tline);
461 static Context *get_ctx(const char *name, const char **namep,
462 bool all_contexts);
463 static void make_tok_num(Token * tok, int64_t val);
464 static void error(int severity, const char *fmt, ...);
465 static void error_precond(int severity, const char *fmt, ...);
466 static void *new_Block(size_t size);
467 static void delete_Blocks(void);
468 static Token *new_Token(Token * next, enum pp_token_type type,
469 const char *text, int txtlen);
470 static Token *delete_Token(Token * t);
473 * Macros for safe checking of token pointers, avoid *(NULL)
475 #define tok_type_(x,t) ((x) && (x)->type == (t))
476 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
477 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
478 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
481 * nasm_unquote with error if the string contains NUL characters.
482 * If the string contains NUL characters, issue an error and return
483 * the C len, i.e. truncate at the NUL.
485 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
487 size_t len = nasm_unquote(qstr, NULL);
488 size_t clen = strlen(qstr);
490 if (len != clen)
491 error(ERR_NONFATAL, "NUL character in `%s' directive",
492 pp_directives[directive]);
494 return clen;
498 * In-place reverse a list of tokens.
500 static Token *reverse_tokens(Token *t)
502 Token *prev = NULL;
503 Token *next;
505 while (t) {
506 next = t->next;
507 t->next = prev;
508 prev = t;
509 t = next;
512 return prev;
516 * Handle TASM specific directives, which do not contain a % in
517 * front of them. We do it here because I could not find any other
518 * place to do it for the moment, and it is a hack (ideally it would
519 * be nice to be able to use the NASM pre-processor to do it).
521 static char *check_tasm_directive(char *line)
523 int32_t i, j, k, m, len;
524 char *p, *q, *oldline, oldchar;
526 p = nasm_skip_spaces(line);
528 /* Binary search for the directive name */
529 i = -1;
530 j = ARRAY_SIZE(tasm_directives);
531 q = nasm_skip_word(p);
532 len = q - p;
533 if (len) {
534 oldchar = p[len];
535 p[len] = 0;
536 while (j - i > 1) {
537 k = (j + i) / 2;
538 m = nasm_stricmp(p, tasm_directives[k]);
539 if (m == 0) {
540 /* We have found a directive, so jam a % in front of it
541 * so that NASM will then recognise it as one if it's own.
543 p[len] = oldchar;
544 len = strlen(p);
545 oldline = line;
546 line = nasm_malloc(len + 2);
547 line[0] = '%';
548 if (k == TM_IFDIFI) {
550 * NASM does not recognise IFDIFI, so we convert
551 * it to %if 0. This is not used in NASM
552 * compatible code, but does need to parse for the
553 * TASM macro package.
555 strcpy(line + 1, "if 0");
556 } else {
557 memcpy(line + 1, p, len + 1);
559 nasm_free(oldline);
560 return line;
561 } else if (m < 0) {
562 j = k;
563 } else
564 i = k;
566 p[len] = oldchar;
568 return line;
572 * The pre-preprocessing stage... This function translates line
573 * number indications as they emerge from GNU cpp (`# lineno "file"
574 * flags') into NASM preprocessor line number indications (`%line
575 * lineno file').
577 static char *prepreproc(char *line)
579 int lineno, fnlen;
580 char *fname, *oldline;
582 if (line[0] == '#' && line[1] == ' ') {
583 oldline = line;
584 fname = oldline + 2;
585 lineno = atoi(fname);
586 fname += strspn(fname, "0123456789 ");
587 if (*fname == '"')
588 fname++;
589 fnlen = strcspn(fname, "\"");
590 line = nasm_malloc(20 + fnlen);
591 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
592 nasm_free(oldline);
594 if (tasm_compatible_mode)
595 return check_tasm_directive(line);
596 return line;
600 * Free a linked list of tokens.
602 static void free_tlist(Token * list)
604 while (list)
605 list = delete_Token(list);
609 * Free a linked list of lines.
611 static void free_llist(Line * list)
613 Line *l, *tmp;
614 list_for_each_safe(l, tmp, list) {
615 free_tlist(l->first);
616 nasm_free(l);
621 * Free an MMacro
623 static void free_mmacro(MMacro * m)
625 nasm_free(m->name);
626 free_tlist(m->dlist);
627 nasm_free(m->defaults);
628 free_llist(m->expansion);
629 nasm_free(m);
633 * Free all currently defined macros, and free the hash tables
635 static void free_smacro_table(struct hash_table *smt)
637 SMacro *s, *tmp;
638 const char *key;
639 struct hash_tbl_node *it = NULL;
641 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
642 nasm_free((void *)key);
643 list_for_each_safe(s, tmp, s) {
644 nasm_free(s->name);
645 free_tlist(s->expansion);
646 nasm_free(s);
649 hash_free(smt);
652 static void free_mmacro_table(struct hash_table *mmt)
654 MMacro *m, *tmp;
655 const char *key;
656 struct hash_tbl_node *it = NULL;
658 it = NULL;
659 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
660 nasm_free((void *)key);
661 list_for_each_safe(m ,tmp, m)
662 free_mmacro(m);
664 hash_free(mmt);
667 static void free_macros(void)
669 free_smacro_table(&smacros);
670 free_mmacro_table(&mmacros);
674 * Initialize the hash tables
676 static void init_macros(void)
678 hash_init(&smacros, HASH_LARGE);
679 hash_init(&mmacros, HASH_LARGE);
683 * Pop the context stack.
685 static void ctx_pop(void)
687 Context *c = cstk;
689 cstk = cstk->next;
690 free_smacro_table(&c->localmac);
691 nasm_free(c->name);
692 nasm_free(c);
696 * Search for a key in the hash index; adding it if necessary
697 * (in which case we initialize the data pointer to NULL.)
699 static void **
700 hash_findi_add(struct hash_table *hash, const char *str)
702 struct hash_insert hi;
703 void **r;
704 char *strx;
706 r = hash_findi(hash, str, &hi);
707 if (r)
708 return r;
710 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
711 return hash_add(&hi, strx, NULL);
715 * Like hash_findi, but returns the data element rather than a pointer
716 * to it. Used only when not adding a new element, hence no third
717 * argument.
719 static void *
720 hash_findix(struct hash_table *hash, const char *str)
722 void **p;
724 p = hash_findi(hash, str, NULL);
725 return p ? *p : NULL;
729 * read line from standart macros set,
730 * if there no more left -- return NULL
732 static char *line_from_stdmac(void)
734 unsigned char c;
735 const unsigned char *p = stdmacpos;
736 char *line, *q;
737 size_t len = 0;
739 if (!stdmacpos)
740 return NULL;
742 while ((c = *p++)) {
743 if (c >= 0x80)
744 len += pp_directives_len[c - 0x80] + 1;
745 else
746 len++;
749 line = nasm_malloc(len + 1);
750 q = line;
751 while ((c = *stdmacpos++)) {
752 if (c >= 0x80) {
753 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
754 q += pp_directives_len[c - 0x80];
755 *q++ = ' ';
756 } else {
757 *q++ = c;
760 stdmacpos = p;
761 *q = '\0';
763 if (!*stdmacpos) {
764 /* This was the last of the standard macro chain... */
765 stdmacpos = NULL;
766 if (any_extrastdmac) {
767 stdmacpos = extrastdmac;
768 any_extrastdmac = false;
769 } else if (do_predef) {
770 Line *pd, *l;
771 Token *head, **tail, *t;
774 * Nasty hack: here we push the contents of
775 * `predef' on to the top-level expansion stack,
776 * since this is the most convenient way to
777 * implement the pre-include and pre-define
778 * features.
780 list_for_each(pd, predef) {
781 head = NULL;
782 tail = &head;
783 list_for_each(t, pd->first) {
784 *tail = new_Token(NULL, t->type, t->text, 0);
785 tail = &(*tail)->next;
788 l = nasm_malloc(sizeof(Line));
789 l->next = istk->expansion;
790 l->first = head;
791 l->finishes = NULL;
793 istk->expansion = l;
795 do_predef = false;
799 return line;
802 #define BUF_DELTA 512
804 * Read a line from the top file in istk, handling multiple CR/LFs
805 * at the end of the line read, and handling spurious ^Zs. Will
806 * return lines from the standard macro set if this has not already
807 * been done.
809 static char *read_line(void)
811 char *buffer, *p, *q;
812 int bufsize, continued_count;
815 * standart macros set (predefined) goes first
817 p = line_from_stdmac();
818 if (p)
819 return p;
822 * regular read from a file
824 bufsize = BUF_DELTA;
825 buffer = nasm_malloc(BUF_DELTA);
826 p = buffer;
827 continued_count = 0;
828 while (1) {
829 q = fgets(p, bufsize - (p - buffer), istk->fp);
830 if (!q)
831 break;
832 p += strlen(p);
833 if (p > buffer && p[-1] == '\n') {
835 * Convert backslash-CRLF line continuation sequences into
836 * nothing at all (for DOS and Windows)
838 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
839 p -= 3;
840 *p = 0;
841 continued_count++;
844 * Also convert backslash-LF line continuation sequences into
845 * nothing at all (for Unix)
847 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
848 p -= 2;
849 *p = 0;
850 continued_count++;
851 } else {
852 break;
855 if (p - buffer > bufsize - 10) {
856 int32_t offset = p - buffer;
857 bufsize += BUF_DELTA;
858 buffer = nasm_realloc(buffer, bufsize);
859 p = buffer + offset; /* prevent stale-pointer problems */
863 if (!q && p == buffer) {
864 nasm_free(buffer);
865 return NULL;
868 src_set_linnum(src_get_linnum() + istk->lineinc +
869 (continued_count * istk->lineinc));
872 * Play safe: remove CRs as well as LFs, if any of either are
873 * present at the end of the line.
875 while (--p >= buffer && (*p == '\n' || *p == '\r'))
876 *p = '\0';
879 * Handle spurious ^Z, which may be inserted into source files
880 * by some file transfer utilities.
882 buffer[strcspn(buffer, "\032")] = '\0';
884 list->line(LIST_READ, buffer);
886 return buffer;
890 * Tokenize a line of text. This is a very simple process since we
891 * don't need to parse the value out of e.g. numeric tokens: we
892 * simply split one string into many.
894 static Token *tokenize(char *line)
896 char c, *p = line;
897 enum pp_token_type type;
898 Token *list = NULL;
899 Token *t, **tail = &list;
901 while (*line) {
902 p = line;
903 if (*p == '%') {
904 p++;
905 if (*p == '+' && !nasm_isdigit(p[1])) {
906 p++;
907 type = TOK_PASTE;
908 } else if (nasm_isdigit(*p) ||
909 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
910 do {
911 p++;
913 while (nasm_isdigit(*p));
914 type = TOK_PREPROC_ID;
915 } else if (*p == '{') {
916 p++;
917 while (*p) {
918 if (*p == '}')
919 break;
920 p[-1] = *p;
921 p++;
923 if (*p != '}')
924 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
925 p[-1] = '\0';
926 if (*p)
927 p++;
928 type = TOK_PREPROC_ID;
929 } else if (*p == '[') {
930 int lvl = 1;
931 line += 2; /* Skip the leading %[ */
932 p++;
933 while (lvl && (c = *p++)) {
934 switch (c) {
935 case ']':
936 lvl--;
937 break;
938 case '%':
939 if (*p == '[')
940 lvl++;
941 break;
942 case '\'':
943 case '\"':
944 case '`':
945 p = nasm_skip_string(p - 1) + 1;
946 break;
947 default:
948 break;
951 p--;
952 if (*p)
953 *p++ = '\0';
954 if (lvl)
955 error(ERR_NONFATAL, "unterminated %[ construct");
956 type = TOK_INDIRECT;
957 } else if (*p == '?') {
958 type = TOK_PREPROC_Q; /* %? */
959 p++;
960 if (*p == '?') {
961 type = TOK_PREPROC_QQ; /* %?? */
962 p++;
964 } else if (*p == '!') {
965 type = TOK_PREPROC_ID;
966 p++;
967 if (isidchar(*p)) {
968 do {
969 p++;
971 while (isidchar(*p));
972 } else if (*p == '\'' || *p == '\"' || *p == '`') {
973 p = nasm_skip_string(p);
974 if (*p)
975 p++;
976 else
977 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
978 } else {
979 /* %! without string or identifier */
980 type = TOK_OTHER; /* Legacy behavior... */
982 } else if (isidchar(*p) ||
983 ((*p == '!' || *p == '%' || *p == '$') &&
984 isidchar(p[1]))) {
985 do {
986 p++;
988 while (isidchar(*p));
989 type = TOK_PREPROC_ID;
990 } else {
991 type = TOK_OTHER;
992 if (*p == '%')
993 p++;
995 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
996 type = TOK_ID;
997 p++;
998 while (*p && isidchar(*p))
999 p++;
1000 } else if (*p == '\'' || *p == '"' || *p == '`') {
1002 * A string token.
1004 type = TOK_STRING;
1005 p = nasm_skip_string(p);
1007 if (*p) {
1008 p++;
1009 } else {
1010 error(ERR_WARNING|ERR_PASS1, "unterminated string");
1011 /* Handling unterminated strings by UNV */
1012 /* type = -1; */
1014 } else if (p[0] == '$' && p[1] == '$') {
1015 type = TOK_OTHER; /* TOKEN_BASE */
1016 p += 2;
1017 } else if (isnumstart(*p)) {
1018 bool is_hex = false;
1019 bool is_float = false;
1020 bool has_e = false;
1021 char c, *r;
1024 * A numeric token.
1027 if (*p == '$') {
1028 p++;
1029 is_hex = true;
1032 for (;;) {
1033 c = *p++;
1035 if (!is_hex && (c == 'e' || c == 'E')) {
1036 has_e = true;
1037 if (*p == '+' || *p == '-') {
1039 * e can only be followed by +/- if it is either a
1040 * prefixed hex number or a floating-point number
1042 p++;
1043 is_float = true;
1045 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1046 is_hex = true;
1047 } else if (c == 'P' || c == 'p') {
1048 is_float = true;
1049 if (*p == '+' || *p == '-')
1050 p++;
1051 } else if (isnumchar(c) || c == '_')
1052 ; /* just advance */
1053 else if (c == '.') {
1055 * we need to deal with consequences of the legacy
1056 * parser, like "1.nolist" being two tokens
1057 * (TOK_NUMBER, TOK_ID) here; at least give it
1058 * a shot for now. In the future, we probably need
1059 * a flex-based scanner with proper pattern matching
1060 * to do it as well as it can be done. Nothing in
1061 * the world is going to help the person who wants
1062 * 0x123.p16 interpreted as two tokens, though.
1064 r = p;
1065 while (*r == '_')
1066 r++;
1068 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1069 (!is_hex && (*r == 'e' || *r == 'E')) ||
1070 (*r == 'p' || *r == 'P')) {
1071 p = r;
1072 is_float = true;
1073 } else
1074 break; /* Terminate the token */
1075 } else
1076 break;
1078 p--; /* Point to first character beyond number */
1080 if (p == line+1 && *line == '$') {
1081 type = TOK_OTHER; /* TOKEN_HERE */
1082 } else {
1083 if (has_e && !is_hex) {
1084 /* 1e13 is floating-point, but 1e13h is not */
1085 is_float = true;
1088 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1090 } else if (nasm_isspace(*p)) {
1091 type = TOK_WHITESPACE;
1092 p = nasm_skip_spaces(p);
1094 * Whitespace just before end-of-line is discarded by
1095 * pretending it's a comment; whitespace just before a
1096 * comment gets lumped into the comment.
1098 if (!*p || *p == ';') {
1099 type = TOK_COMMENT;
1100 while (*p)
1101 p++;
1103 } else if (*p == ';') {
1104 type = TOK_COMMENT;
1105 while (*p)
1106 p++;
1107 } else {
1109 * Anything else is an operator of some kind. We check
1110 * for all the double-character operators (>>, <<, //,
1111 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1112 * else is a single-character operator.
1114 type = TOK_OTHER;
1115 if ((p[0] == '>' && p[1] == '>') ||
1116 (p[0] == '<' && p[1] == '<') ||
1117 (p[0] == '/' && p[1] == '/') ||
1118 (p[0] == '<' && p[1] == '=') ||
1119 (p[0] == '>' && p[1] == '=') ||
1120 (p[0] == '=' && p[1] == '=') ||
1121 (p[0] == '!' && p[1] == '=') ||
1122 (p[0] == '<' && p[1] == '>') ||
1123 (p[0] == '&' && p[1] == '&') ||
1124 (p[0] == '|' && p[1] == '|') ||
1125 (p[0] == '^' && p[1] == '^')) {
1126 p++;
1128 p++;
1131 /* Handling unterminated string by UNV */
1132 /*if (type == -1)
1134 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1135 t->text[p-line] = *line;
1136 tail = &t->next;
1138 else */
1139 if (type != TOK_COMMENT) {
1140 *tail = t = new_Token(NULL, type, line, p - line);
1141 tail = &t->next;
1143 line = p;
1145 return list;
1149 * this function allocates a new managed block of memory and
1150 * returns a pointer to the block. The managed blocks are
1151 * deleted only all at once by the delete_Blocks function.
1153 static void *new_Block(size_t size)
1155 Blocks *b = &blocks;
1157 /* first, get to the end of the linked list */
1158 while (b->next)
1159 b = b->next;
1160 /* now allocate the requested chunk */
1161 b->chunk = nasm_malloc(size);
1163 /* now allocate a new block for the next request */
1164 b->next = nasm_malloc(sizeof(Blocks));
1165 /* and initialize the contents of the new block */
1166 b->next->next = NULL;
1167 b->next->chunk = NULL;
1168 return b->chunk;
1172 * this function deletes all managed blocks of memory
1174 static void delete_Blocks(void)
1176 Blocks *a, *b = &blocks;
1179 * keep in mind that the first block, pointed to by blocks
1180 * is a static and not dynamically allocated, so we don't
1181 * free it.
1183 while (b) {
1184 if (b->chunk)
1185 nasm_free(b->chunk);
1186 a = b;
1187 b = b->next;
1188 if (a != &blocks)
1189 nasm_free(a);
1194 * this function creates a new Token and passes a pointer to it
1195 * back to the caller. It sets the type and text elements, and
1196 * also the a.mac and next elements to NULL.
1198 static Token *new_Token(Token * next, enum pp_token_type type,
1199 const char *text, int txtlen)
1201 Token *t;
1202 int i;
1204 if (!freeTokens) {
1205 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1206 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1207 freeTokens[i].next = &freeTokens[i + 1];
1208 freeTokens[i].next = NULL;
1210 t = freeTokens;
1211 freeTokens = t->next;
1212 t->next = next;
1213 t->a.mac = NULL;
1214 t->type = type;
1215 if (type == TOK_WHITESPACE || !text) {
1216 t->text = NULL;
1217 } else {
1218 if (txtlen == 0)
1219 txtlen = strlen(text);
1220 t->text = nasm_malloc(txtlen+1);
1221 memcpy(t->text, text, txtlen);
1222 t->text[txtlen] = '\0';
1224 return t;
1227 static Token *delete_Token(Token * t)
1229 Token *next = t->next;
1230 nasm_free(t->text);
1231 t->next = freeTokens;
1232 freeTokens = t;
1233 return next;
1237 * Convert a line of tokens back into text.
1238 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1239 * will be transformed into ..@ctxnum.xxx
1241 static char *detoken(Token * tlist, bool expand_locals)
1243 Token *t;
1244 char *line, *p;
1245 const char *q;
1246 int len = 0;
1248 list_for_each(t, tlist) {
1249 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1250 char *v;
1251 char *q = t->text;
1253 v = t->text + 2;
1254 if (*v == '\'' || *v == '\"' || *v == '`') {
1255 size_t len = nasm_unquote(v, NULL);
1256 size_t clen = strlen(v);
1258 if (len != clen) {
1259 error(ERR_NONFATAL | ERR_PASS1,
1260 "NUL character in %! string");
1261 v = NULL;
1265 if (v) {
1266 char *p = getenv(v);
1267 if (!p) {
1268 error(ERR_NONFATAL | ERR_PASS1,
1269 "nonexistent environment variable `%s'", v);
1270 p = "";
1272 t->text = nasm_strdup(p);
1274 nasm_free(q);
1277 /* Expand local macros here and not during preprocessing */
1278 if (expand_locals &&
1279 t->type == TOK_PREPROC_ID && t->text &&
1280 t->text[0] == '%' && t->text[1] == '$') {
1281 const char *q;
1282 char *p;
1283 Context *ctx = get_ctx(t->text, &q, false);
1284 if (ctx) {
1285 char buffer[40];
1286 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1287 p = nasm_strcat(buffer, q);
1288 nasm_free(t->text);
1289 t->text = p;
1292 if (t->type == TOK_WHITESPACE)
1293 len++;
1294 else if (t->text)
1295 len += strlen(t->text);
1298 p = line = nasm_malloc(len + 1);
1300 list_for_each(t, tlist) {
1301 if (t->type == TOK_WHITESPACE) {
1302 *p++ = ' ';
1303 } else if (t->text) {
1304 q = t->text;
1305 while (*q)
1306 *p++ = *q++;
1309 *p = '\0';
1311 return line;
1315 * A scanner, suitable for use by the expression evaluator, which
1316 * operates on a line of Tokens. Expects a pointer to a pointer to
1317 * the first token in the line to be passed in as its private_data
1318 * field.
1320 * FIX: This really needs to be unified with stdscan.
1322 static int ppscan(void *private_data, struct tokenval *tokval)
1324 Token **tlineptr = private_data;
1325 Token *tline;
1326 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1328 do {
1329 tline = *tlineptr;
1330 *tlineptr = tline ? tline->next : NULL;
1331 } while (tline && (tline->type == TOK_WHITESPACE ||
1332 tline->type == TOK_COMMENT));
1334 if (!tline)
1335 return tokval->t_type = TOKEN_EOS;
1337 tokval->t_charptr = tline->text;
1339 if (tline->text[0] == '$' && !tline->text[1])
1340 return tokval->t_type = TOKEN_HERE;
1341 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1342 return tokval->t_type = TOKEN_BASE;
1344 if (tline->type == TOK_ID) {
1345 p = tokval->t_charptr = tline->text;
1346 if (p[0] == '$') {
1347 tokval->t_charptr++;
1348 return tokval->t_type = TOKEN_ID;
1351 for (r = p, s = ourcopy; *r; r++) {
1352 if (r >= p+MAX_KEYWORD)
1353 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1354 *s++ = nasm_tolower(*r);
1356 *s = '\0';
1357 /* right, so we have an identifier sitting in temp storage. now,
1358 * is it actually a register or instruction name, or what? */
1359 return nasm_token_hash(ourcopy, tokval);
1362 if (tline->type == TOK_NUMBER) {
1363 bool rn_error;
1364 tokval->t_integer = readnum(tline->text, &rn_error);
1365 tokval->t_charptr = tline->text;
1366 if (rn_error)
1367 return tokval->t_type = TOKEN_ERRNUM;
1368 else
1369 return tokval->t_type = TOKEN_NUM;
1372 if (tline->type == TOK_FLOAT) {
1373 return tokval->t_type = TOKEN_FLOAT;
1376 if (tline->type == TOK_STRING) {
1377 char bq, *ep;
1379 bq = tline->text[0];
1380 tokval->t_charptr = tline->text;
1381 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1383 if (ep[0] != bq || ep[1] != '\0')
1384 return tokval->t_type = TOKEN_ERRSTR;
1385 else
1386 return tokval->t_type = TOKEN_STR;
1389 if (tline->type == TOK_OTHER) {
1390 if (!strcmp(tline->text, "<<"))
1391 return tokval->t_type = TOKEN_SHL;
1392 if (!strcmp(tline->text, ">>"))
1393 return tokval->t_type = TOKEN_SHR;
1394 if (!strcmp(tline->text, "//"))
1395 return tokval->t_type = TOKEN_SDIV;
1396 if (!strcmp(tline->text, "%%"))
1397 return tokval->t_type = TOKEN_SMOD;
1398 if (!strcmp(tline->text, "=="))
1399 return tokval->t_type = TOKEN_EQ;
1400 if (!strcmp(tline->text, "<>"))
1401 return tokval->t_type = TOKEN_NE;
1402 if (!strcmp(tline->text, "!="))
1403 return tokval->t_type = TOKEN_NE;
1404 if (!strcmp(tline->text, "<="))
1405 return tokval->t_type = TOKEN_LE;
1406 if (!strcmp(tline->text, ">="))
1407 return tokval->t_type = TOKEN_GE;
1408 if (!strcmp(tline->text, "&&"))
1409 return tokval->t_type = TOKEN_DBL_AND;
1410 if (!strcmp(tline->text, "^^"))
1411 return tokval->t_type = TOKEN_DBL_XOR;
1412 if (!strcmp(tline->text, "||"))
1413 return tokval->t_type = TOKEN_DBL_OR;
1417 * We have no other options: just return the first character of
1418 * the token text.
1420 return tokval->t_type = tline->text[0];
1424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1428 static int mstrcmp(const char *p, const char *q, bool casesense)
1430 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1434 * Compare a string to the name of an existing macro; this is a
1435 * simple wrapper which calls either strcmp or nasm_stricmp
1436 * depending on the value of the `casesense' parameter.
1438 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1440 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1444 * Return the Context structure associated with a %$ token. Return
1445 * NULL, having _already_ reported an error condition, if the
1446 * context stack isn't deep enough for the supplied number of $
1447 * signs.
1448 * If all_contexts == true, contexts that enclose current are
1449 * also scanned for such smacro, until it is found; if not -
1450 * only the context that directly results from the number of $'s
1451 * in variable's name.
1453 * If "namep" is non-NULL, set it to the pointer to the macro name
1454 * tail, i.e. the part beyond %$...
1456 static Context *get_ctx(const char *name, const char **namep,
1457 bool all_contexts)
1459 Context *ctx;
1460 SMacro *m;
1461 int i;
1463 if (namep)
1464 *namep = name;
1466 if (!name || name[0] != '%' || name[1] != '$')
1467 return NULL;
1469 if (!cstk) {
1470 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1471 return NULL;
1474 name += 2;
1475 ctx = cstk;
1476 i = 0;
1477 while (ctx && *name == '$') {
1478 name++;
1479 i++;
1480 ctx = ctx->next;
1482 if (!ctx) {
1483 error(ERR_NONFATAL, "`%s': context stack is only"
1484 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1485 return NULL;
1488 if (namep)
1489 *namep = name;
1491 if (!all_contexts)
1492 return ctx;
1495 * NOTE: In 2.10 we will not need lookup in extarnal
1496 * contexts, so this is a gentle way to inform users
1497 * about their source code need to be updated
1500 /* first round -- check the current context */
1501 m = hash_findix(&ctx->localmac, name);
1502 while (m) {
1503 if (!mstrcmp(m->name, name, m->casesense))
1504 return ctx;
1505 m = m->next;
1508 /* second round - external contexts */
1509 while ((ctx = ctx->next)) {
1510 /* Search for this smacro in found context */
1511 m = hash_findix(&ctx->localmac, name);
1512 while (m) {
1513 if (!mstrcmp(m->name, name, m->casesense)) {
1514 /* NOTE: deprecated as of 2.10 */
1515 static int once = 0;
1516 if (!once) {
1517 error(ERR_WARNING, "context-local macro expansion"
1518 " fall-through (automatic searching of outer"
1519 " contexts) will be deprecated starting in"
1520 " NASM 2.10, please see the NASM Manual for"
1521 " more information");
1522 once = 1;
1524 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
1525 return ctx;
1527 m = m->next;
1531 return NULL;
1535 * Check to see if a file is already in a string list
1537 static bool in_list(const StrList *list, const char *str)
1539 while (list) {
1540 if (!strcmp(list->str, str))
1541 return true;
1542 list = list->next;
1544 return false;
1548 * Open an include file. This routine must always return a valid
1549 * file pointer if it returns - it's responsible for throwing an
1550 * ERR_FATAL and bombing out completely if not. It should also try
1551 * the include path one by one until it finds the file or reaches
1552 * the end of the path.
1554 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1555 bool missing_ok)
1557 FILE *fp;
1558 char *prefix = "";
1559 IncPath *ip = ipath;
1560 int len = strlen(file);
1561 size_t prefix_len = 0;
1562 StrList *sl;
1564 while (1) {
1565 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1566 memcpy(sl->str, prefix, prefix_len);
1567 memcpy(sl->str+prefix_len, file, len+1);
1568 fp = fopen(sl->str, "r");
1569 if (fp && dhead && !in_list(*dhead, sl->str)) {
1570 sl->next = NULL;
1571 **dtail = sl;
1572 *dtail = &sl->next;
1573 } else {
1574 nasm_free(sl);
1576 if (fp)
1577 return fp;
1578 if (!ip) {
1579 if (!missing_ok)
1580 break;
1581 prefix = NULL;
1582 } else {
1583 prefix = ip->path;
1584 ip = ip->next;
1586 if (prefix) {
1587 prefix_len = strlen(prefix);
1588 } else {
1589 /* -MG given and file not found */
1590 if (dhead && !in_list(*dhead, file)) {
1591 sl = nasm_malloc(len+1+sizeof sl->next);
1592 sl->next = NULL;
1593 strcpy(sl->str, file);
1594 **dtail = sl;
1595 *dtail = &sl->next;
1597 return NULL;
1601 error(ERR_FATAL, "unable to open include file `%s'", file);
1602 return NULL;
1606 * Determine if we should warn on defining a single-line macro of
1607 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1608 * return true if _any_ single-line macro of that name is defined.
1609 * Otherwise, will return true if a single-line macro with either
1610 * `nparam' or no parameters is defined.
1612 * If a macro with precisely the right number of parameters is
1613 * defined, or nparam is -1, the address of the definition structure
1614 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1615 * is NULL, no action will be taken regarding its contents, and no
1616 * error will occur.
1618 * Note that this is also called with nparam zero to resolve
1619 * `ifdef'.
1621 * If you already know which context macro belongs to, you can pass
1622 * the context pointer as first parameter; if you won't but name begins
1623 * with %$ the context will be automatically computed. If all_contexts
1624 * is true, macro will be searched in outer contexts as well.
1626 static bool
1627 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1628 bool nocase)
1630 struct hash_table *smtbl;
1631 SMacro *m;
1633 if (ctx) {
1634 smtbl = &ctx->localmac;
1635 } else if (name[0] == '%' && name[1] == '$') {
1636 if (cstk)
1637 ctx = get_ctx(name, &name, false);
1638 if (!ctx)
1639 return false; /* got to return _something_ */
1640 smtbl = &ctx->localmac;
1641 } else {
1642 smtbl = &smacros;
1644 m = (SMacro *) hash_findix(smtbl, name);
1646 while (m) {
1647 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1648 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1649 if (defn) {
1650 if (nparam == (int) m->nparam || nparam == -1)
1651 *defn = m;
1652 else
1653 *defn = NULL;
1655 return true;
1657 m = m->next;
1660 return false;
1664 * Count and mark off the parameters in a multi-line macro call.
1665 * This is called both from within the multi-line macro expansion
1666 * code, and also to mark off the default parameters when provided
1667 * in a %macro definition line.
1669 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1671 int paramsize, brace;
1673 *nparam = paramsize = 0;
1674 *params = NULL;
1675 while (t) {
1676 /* +1: we need space for the final NULL */
1677 if (*nparam+1 >= paramsize) {
1678 paramsize += PARAM_DELTA;
1679 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1681 skip_white_(t);
1682 brace = false;
1683 if (tok_is_(t, "{"))
1684 brace = true;
1685 (*params)[(*nparam)++] = t;
1686 while (tok_isnt_(t, brace ? "}" : ","))
1687 t = t->next;
1688 if (t) { /* got a comma/brace */
1689 t = t->next;
1690 if (brace) {
1692 * Now we've found the closing brace, look further
1693 * for the comma.
1695 skip_white_(t);
1696 if (tok_isnt_(t, ",")) {
1697 error(ERR_NONFATAL,
1698 "braces do not enclose all of macro parameter");
1699 while (tok_isnt_(t, ","))
1700 t = t->next;
1702 if (t)
1703 t = t->next; /* eat the comma */
1710 * Determine whether one of the various `if' conditions is true or
1711 * not.
1713 * We must free the tline we get passed.
1715 static bool if_condition(Token * tline, enum preproc_token ct)
1717 enum pp_conditional i = PP_COND(ct);
1718 bool j;
1719 Token *t, *tt, **tptr, *origline;
1720 struct tokenval tokval;
1721 expr *evalresult;
1722 enum pp_token_type needtype;
1723 char *p;
1725 origline = tline;
1727 switch (i) {
1728 case PPC_IFCTX:
1729 j = false; /* have we matched yet? */
1730 while (true) {
1731 skip_white_(tline);
1732 if (!tline)
1733 break;
1734 if (tline->type != TOK_ID) {
1735 error(ERR_NONFATAL,
1736 "`%s' expects context identifiers", pp_directives[ct]);
1737 free_tlist(origline);
1738 return -1;
1740 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1741 j = true;
1742 tline = tline->next;
1744 break;
1746 case PPC_IFDEF:
1747 j = false; /* have we matched yet? */
1748 while (tline) {
1749 skip_white_(tline);
1750 if (!tline || (tline->type != TOK_ID &&
1751 (tline->type != TOK_PREPROC_ID ||
1752 tline->text[1] != '$'))) {
1753 error(ERR_NONFATAL,
1754 "`%s' expects macro identifiers", pp_directives[ct]);
1755 goto fail;
1757 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1758 j = true;
1759 tline = tline->next;
1761 break;
1763 case PPC_IFENV:
1764 tline = expand_smacro(tline);
1765 j = false; /* have we matched yet? */
1766 while (tline) {
1767 skip_white_(tline);
1768 if (!tline || (tline->type != TOK_ID &&
1769 tline->type != TOK_STRING &&
1770 (tline->type != TOK_PREPROC_ID ||
1771 tline->text[1] != '!'))) {
1772 error(ERR_NONFATAL,
1773 "`%s' expects environment variable names",
1774 pp_directives[ct]);
1775 goto fail;
1777 p = tline->text;
1778 if (tline->type == TOK_PREPROC_ID)
1779 p += 2; /* Skip leading %! */
1780 if (*p == '\'' || *p == '\"' || *p == '`')
1781 nasm_unquote_cstr(p, ct);
1782 if (getenv(p))
1783 j = true;
1784 tline = tline->next;
1786 break;
1788 case PPC_IFIDN:
1789 case PPC_IFIDNI:
1790 tline = expand_smacro(tline);
1791 t = tt = tline;
1792 while (tok_isnt_(tt, ","))
1793 tt = tt->next;
1794 if (!tt) {
1795 error(ERR_NONFATAL,
1796 "`%s' expects two comma-separated arguments",
1797 pp_directives[ct]);
1798 goto fail;
1800 tt = tt->next;
1801 j = true; /* assume equality unless proved not */
1802 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1803 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1804 error(ERR_NONFATAL, "`%s': more than one comma on line",
1805 pp_directives[ct]);
1806 goto fail;
1808 if (t->type == TOK_WHITESPACE) {
1809 t = t->next;
1810 continue;
1812 if (tt->type == TOK_WHITESPACE) {
1813 tt = tt->next;
1814 continue;
1816 if (tt->type != t->type) {
1817 j = false; /* found mismatching tokens */
1818 break;
1820 /* When comparing strings, need to unquote them first */
1821 if (t->type == TOK_STRING) {
1822 size_t l1 = nasm_unquote(t->text, NULL);
1823 size_t l2 = nasm_unquote(tt->text, NULL);
1825 if (l1 != l2) {
1826 j = false;
1827 break;
1829 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1830 j = false;
1831 break;
1833 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1834 j = false; /* found mismatching tokens */
1835 break;
1838 t = t->next;
1839 tt = tt->next;
1841 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1842 j = false; /* trailing gunk on one end or other */
1843 break;
1845 case PPC_IFMACRO:
1847 bool found = false;
1848 MMacro searching, *mmac;
1850 skip_white_(tline);
1851 tline = expand_id(tline);
1852 if (!tok_type_(tline, TOK_ID)) {
1853 error(ERR_NONFATAL,
1854 "`%s' expects a macro name", pp_directives[ct]);
1855 goto fail;
1857 searching.name = nasm_strdup(tline->text);
1858 searching.casesense = true;
1859 searching.plus = false;
1860 searching.nolist = false;
1861 searching.in_progress = 0;
1862 searching.max_depth = 0;
1863 searching.rep_nest = NULL;
1864 searching.nparam_min = 0;
1865 searching.nparam_max = INT_MAX;
1866 tline = expand_smacro(tline->next);
1867 skip_white_(tline);
1868 if (!tline) {
1869 } else if (!tok_type_(tline, TOK_NUMBER)) {
1870 error(ERR_NONFATAL,
1871 "`%s' expects a parameter count or nothing",
1872 pp_directives[ct]);
1873 } else {
1874 searching.nparam_min = searching.nparam_max =
1875 readnum(tline->text, &j);
1876 if (j)
1877 error(ERR_NONFATAL,
1878 "unable to parse parameter count `%s'",
1879 tline->text);
1881 if (tline && tok_is_(tline->next, "-")) {
1882 tline = tline->next->next;
1883 if (tok_is_(tline, "*"))
1884 searching.nparam_max = INT_MAX;
1885 else if (!tok_type_(tline, TOK_NUMBER))
1886 error(ERR_NONFATAL,
1887 "`%s' expects a parameter count after `-'",
1888 pp_directives[ct]);
1889 else {
1890 searching.nparam_max = readnum(tline->text, &j);
1891 if (j)
1892 error(ERR_NONFATAL,
1893 "unable to parse parameter count `%s'",
1894 tline->text);
1895 if (searching.nparam_min > searching.nparam_max)
1896 error(ERR_NONFATAL,
1897 "minimum parameter count exceeds maximum");
1900 if (tline && tok_is_(tline->next, "+")) {
1901 tline = tline->next;
1902 searching.plus = true;
1904 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1905 while (mmac) {
1906 if (!strcmp(mmac->name, searching.name) &&
1907 (mmac->nparam_min <= searching.nparam_max
1908 || searching.plus)
1909 && (searching.nparam_min <= mmac->nparam_max
1910 || mmac->plus)) {
1911 found = true;
1912 break;
1914 mmac = mmac->next;
1916 if (tline && tline->next)
1917 error(ERR_WARNING|ERR_PASS1,
1918 "trailing garbage after %%ifmacro ignored");
1919 nasm_free(searching.name);
1920 j = found;
1921 break;
1924 case PPC_IFID:
1925 needtype = TOK_ID;
1926 goto iftype;
1927 case PPC_IFNUM:
1928 needtype = TOK_NUMBER;
1929 goto iftype;
1930 case PPC_IFSTR:
1931 needtype = TOK_STRING;
1932 goto iftype;
1934 iftype:
1935 t = tline = expand_smacro(tline);
1937 while (tok_type_(t, TOK_WHITESPACE) ||
1938 (needtype == TOK_NUMBER &&
1939 tok_type_(t, TOK_OTHER) &&
1940 (t->text[0] == '-' || t->text[0] == '+') &&
1941 !t->text[1]))
1942 t = t->next;
1944 j = tok_type_(t, needtype);
1945 break;
1947 case PPC_IFTOKEN:
1948 t = tline = expand_smacro(tline);
1949 while (tok_type_(t, TOK_WHITESPACE))
1950 t = t->next;
1952 j = false;
1953 if (t) {
1954 t = t->next; /* Skip the actual token */
1955 while (tok_type_(t, TOK_WHITESPACE))
1956 t = t->next;
1957 j = !t; /* Should be nothing left */
1959 break;
1961 case PPC_IFEMPTY:
1962 t = tline = expand_smacro(tline);
1963 while (tok_type_(t, TOK_WHITESPACE))
1964 t = t->next;
1966 j = !t; /* Should be empty */
1967 break;
1969 case PPC_IF:
1970 t = tline = expand_smacro(tline);
1971 tptr = &t;
1972 tokval.t_type = TOKEN_INVALID;
1973 evalresult = evaluate(ppscan, tptr, &tokval,
1974 NULL, pass | CRITICAL, error, NULL);
1975 if (!evalresult)
1976 return -1;
1977 if (tokval.t_type)
1978 error(ERR_WARNING|ERR_PASS1,
1979 "trailing garbage after expression ignored");
1980 if (!is_simple(evalresult)) {
1981 error(ERR_NONFATAL,
1982 "non-constant value given to `%s'", pp_directives[ct]);
1983 goto fail;
1985 j = reloc_value(evalresult) != 0;
1986 break;
1988 default:
1989 error(ERR_FATAL,
1990 "preprocessor directive `%s' not yet implemented",
1991 pp_directives[ct]);
1992 goto fail;
1995 free_tlist(origline);
1996 return j ^ PP_NEGATIVE(ct);
1998 fail:
1999 free_tlist(origline);
2000 return -1;
2004 * Common code for defining an smacro
2006 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2007 int nparam, Token *expansion)
2009 SMacro *smac, **smhead;
2010 struct hash_table *smtbl;
2012 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2013 if (!smac) {
2014 error(ERR_WARNING|ERR_PASS1,
2015 "single-line macro `%s' defined both with and"
2016 " without parameters", mname);
2018 * Some instances of the old code considered this a failure,
2019 * some others didn't. What is the right thing to do here?
2021 free_tlist(expansion);
2022 return false; /* Failure */
2023 } else {
2025 * We're redefining, so we have to take over an
2026 * existing SMacro structure. This means freeing
2027 * what was already in it.
2029 nasm_free(smac->name);
2030 free_tlist(smac->expansion);
2032 } else {
2033 smtbl = ctx ? &ctx->localmac : &smacros;
2034 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2035 smac = nasm_malloc(sizeof(SMacro));
2036 smac->next = *smhead;
2037 *smhead = smac;
2039 smac->name = nasm_strdup(mname);
2040 smac->casesense = casesense;
2041 smac->nparam = nparam;
2042 smac->expansion = expansion;
2043 smac->in_progress = false;
2044 return true; /* Success */
2048 * Undefine an smacro
2050 static void undef_smacro(Context *ctx, const char *mname)
2052 SMacro **smhead, *s, **sp;
2053 struct hash_table *smtbl;
2055 smtbl = ctx ? &ctx->localmac : &smacros;
2056 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2058 if (smhead) {
2060 * We now have a macro name... go hunt for it.
2062 sp = smhead;
2063 while ((s = *sp) != NULL) {
2064 if (!mstrcmp(s->name, mname, s->casesense)) {
2065 *sp = s->next;
2066 nasm_free(s->name);
2067 free_tlist(s->expansion);
2068 nasm_free(s);
2069 } else {
2070 sp = &s->next;
2077 * Parse a mmacro specification.
2079 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2081 bool err;
2083 tline = tline->next;
2084 skip_white_(tline);
2085 tline = expand_id(tline);
2086 if (!tok_type_(tline, TOK_ID)) {
2087 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2088 return false;
2091 def->prev = NULL;
2092 def->name = nasm_strdup(tline->text);
2093 def->plus = false;
2094 def->nolist = false;
2095 def->in_progress = 0;
2096 def->rep_nest = NULL;
2097 def->nparam_min = 0;
2098 def->nparam_max = 0;
2100 tline = expand_smacro(tline->next);
2101 skip_white_(tline);
2102 if (!tok_type_(tline, TOK_NUMBER)) {
2103 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2104 } else {
2105 def->nparam_min = def->nparam_max =
2106 readnum(tline->text, &err);
2107 if (err)
2108 error(ERR_NONFATAL,
2109 "unable to parse parameter count `%s'", tline->text);
2111 if (tline && tok_is_(tline->next, "-")) {
2112 tline = tline->next->next;
2113 if (tok_is_(tline, "*")) {
2114 def->nparam_max = INT_MAX;
2115 } else if (!tok_type_(tline, TOK_NUMBER)) {
2116 error(ERR_NONFATAL,
2117 "`%s' expects a parameter count after `-'", directive);
2118 } else {
2119 def->nparam_max = readnum(tline->text, &err);
2120 if (err) {
2121 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2122 tline->text);
2124 if (def->nparam_min > def->nparam_max) {
2125 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2129 if (tline && tok_is_(tline->next, "+")) {
2130 tline = tline->next;
2131 def->plus = true;
2133 if (tline && tok_type_(tline->next, TOK_ID) &&
2134 !nasm_stricmp(tline->next->text, ".nolist")) {
2135 tline = tline->next;
2136 def->nolist = true;
2140 * Handle default parameters.
2142 if (tline && tline->next) {
2143 def->dlist = tline->next;
2144 tline->next = NULL;
2145 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2146 } else {
2147 def->dlist = NULL;
2148 def->defaults = NULL;
2150 def->expansion = NULL;
2152 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2153 !def->plus)
2154 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2155 "too many default macro parameters");
2157 return true;
2162 * Decode a size directive
2164 static int parse_size(const char *str) {
2165 static const char *size_names[] =
2166 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2167 static const int sizes[] =
2168 { 0, 1, 4, 16, 8, 10, 2, 32 };
2170 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2174 * find and process preprocessor directive in passed line
2175 * Find out if a line contains a preprocessor directive, and deal
2176 * with it if so.
2178 * If a directive _is_ found, it is the responsibility of this routine
2179 * (and not the caller) to free_tlist() the line.
2181 * @param tline a pointer to the current tokeninzed line linked list
2182 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2185 static int do_directive(Token * tline)
2187 enum preproc_token i;
2188 int j;
2189 bool err;
2190 int nparam;
2191 bool nolist;
2192 bool casesense;
2193 int k, m;
2194 int offset;
2195 char *p, *pp;
2196 const char *mname;
2197 Include *inc;
2198 Context *ctx;
2199 Cond *cond;
2200 MMacro *mmac, **mmhead;
2201 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2202 Line *l;
2203 struct tokenval tokval;
2204 expr *evalresult;
2205 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2206 int64_t count;
2207 size_t len;
2208 int severity;
2210 origline = tline;
2212 skip_white_(tline);
2213 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2214 (tline->text[1] == '%' || tline->text[1] == '$'
2215 || tline->text[1] == '!'))
2216 return NO_DIRECTIVE_FOUND;
2218 i = pp_token_hash(tline->text);
2221 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2222 * since they are known to be buggy at moment, we need to fix them
2223 * in future release (2.09-2.10)
2225 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2226 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2227 tline->text);
2228 return NO_DIRECTIVE_FOUND;
2232 * If we're in a non-emitting branch of a condition construct,
2233 * or walking to the end of an already terminated %rep block,
2234 * we should ignore all directives except for condition
2235 * directives.
2237 if (((istk->conds && !emitting(istk->conds->state)) ||
2238 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2239 return NO_DIRECTIVE_FOUND;
2243 * If we're defining a macro or reading a %rep block, we should
2244 * ignore all directives except for %macro/%imacro (which nest),
2245 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2246 * If we're in a %rep block, another %rep nests, so should be let through.
2248 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2249 i != PP_RMACRO && i != PP_IRMACRO &&
2250 i != PP_ENDMACRO && i != PP_ENDM &&
2251 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2252 return NO_DIRECTIVE_FOUND;
2255 if (defining) {
2256 if (i == PP_MACRO || i == PP_IMACRO ||
2257 i == PP_RMACRO || i == PP_IRMACRO) {
2258 nested_mac_count++;
2259 return NO_DIRECTIVE_FOUND;
2260 } else if (nested_mac_count > 0) {
2261 if (i == PP_ENDMACRO) {
2262 nested_mac_count--;
2263 return NO_DIRECTIVE_FOUND;
2266 if (!defining->name) {
2267 if (i == PP_REP) {
2268 nested_rep_count++;
2269 return NO_DIRECTIVE_FOUND;
2270 } else if (nested_rep_count > 0) {
2271 if (i == PP_ENDREP) {
2272 nested_rep_count--;
2273 return NO_DIRECTIVE_FOUND;
2279 switch (i) {
2280 case PP_INVALID:
2281 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2282 tline->text);
2283 return NO_DIRECTIVE_FOUND; /* didn't get it */
2285 case PP_STACKSIZE:
2286 /* Directive to tell NASM what the default stack size is. The
2287 * default is for a 16-bit stack, and this can be overriden with
2288 * %stacksize large.
2290 tline = tline->next;
2291 if (tline && tline->type == TOK_WHITESPACE)
2292 tline = tline->next;
2293 if (!tline || tline->type != TOK_ID) {
2294 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2298 if (nasm_stricmp(tline->text, "flat") == 0) {
2299 /* All subsequent ARG directives are for a 32-bit stack */
2300 StackSize = 4;
2301 StackPointer = "ebp";
2302 ArgOffset = 8;
2303 LocalOffset = 0;
2304 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2305 /* All subsequent ARG directives are for a 64-bit stack */
2306 StackSize = 8;
2307 StackPointer = "rbp";
2308 ArgOffset = 16;
2309 LocalOffset = 0;
2310 } else if (nasm_stricmp(tline->text, "large") == 0) {
2311 /* All subsequent ARG directives are for a 16-bit stack,
2312 * far function call.
2314 StackSize = 2;
2315 StackPointer = "bp";
2316 ArgOffset = 4;
2317 LocalOffset = 0;
2318 } else if (nasm_stricmp(tline->text, "small") == 0) {
2319 /* All subsequent ARG directives are for a 16-bit stack,
2320 * far function call. We don't support near functions.
2322 StackSize = 2;
2323 StackPointer = "bp";
2324 ArgOffset = 6;
2325 LocalOffset = 0;
2326 } else {
2327 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2328 free_tlist(origline);
2329 return DIRECTIVE_FOUND;
2331 free_tlist(origline);
2332 return DIRECTIVE_FOUND;
2334 case PP_ARG:
2335 /* TASM like ARG directive to define arguments to functions, in
2336 * the following form:
2338 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2340 offset = ArgOffset;
2341 do {
2342 char *arg, directive[256];
2343 int size = StackSize;
2345 /* Find the argument name */
2346 tline = tline->next;
2347 if (tline && tline->type == TOK_WHITESPACE)
2348 tline = tline->next;
2349 if (!tline || tline->type != TOK_ID) {
2350 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2354 arg = tline->text;
2356 /* Find the argument size type */
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_OTHER
2359 || tline->text[0] != ':') {
2360 error(ERR_NONFATAL,
2361 "Syntax error processing `%%arg' directive");
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
2365 tline = tline->next;
2366 if (!tline || tline->type != TOK_ID) {
2367 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2372 /* Allow macro expansion of type parameter */
2373 tt = tokenize(tline->text);
2374 tt = expand_smacro(tt);
2375 size = parse_size(tt->text);
2376 if (!size) {
2377 error(ERR_NONFATAL,
2378 "Invalid size type for `%%arg' missing directive");
2379 free_tlist(tt);
2380 free_tlist(origline);
2381 return DIRECTIVE_FOUND;
2383 free_tlist(tt);
2385 /* Round up to even stack slots */
2386 size = ALIGN(size, StackSize);
2388 /* Now define the macro for the argument */
2389 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2390 arg, StackPointer, offset);
2391 do_directive(tokenize(directive));
2392 offset += size;
2394 /* Move to the next argument in the list */
2395 tline = tline->next;
2396 if (tline && tline->type == TOK_WHITESPACE)
2397 tline = tline->next;
2398 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2399 ArgOffset = offset;
2400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2403 case PP_LOCAL:
2404 /* TASM like LOCAL directive to define local variables for a
2405 * function, in the following form:
2407 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2409 * The '= LocalSize' at the end is ignored by NASM, but is
2410 * required by TASM to define the local parameter size (and used
2411 * by the TASM macro package).
2413 offset = LocalOffset;
2414 do {
2415 char *local, directive[256];
2416 int size = StackSize;
2418 /* Find the argument name */
2419 tline = tline->next;
2420 if (tline && tline->type == TOK_WHITESPACE)
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
2423 error(ERR_NONFATAL,
2424 "`%%local' missing argument parameter");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2428 local = tline->text;
2430 /* Find the argument size type */
2431 tline = tline->next;
2432 if (!tline || tline->type != TOK_OTHER
2433 || tline->text[0] != ':') {
2434 error(ERR_NONFATAL,
2435 "Syntax error processing `%%local' directive");
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2439 tline = tline->next;
2440 if (!tline || tline->type != TOK_ID) {
2441 error(ERR_NONFATAL,
2442 "`%%local' missing size type parameter");
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2447 /* Allow macro expansion of type parameter */
2448 tt = tokenize(tline->text);
2449 tt = expand_smacro(tt);
2450 size = parse_size(tt->text);
2451 if (!size) {
2452 error(ERR_NONFATAL,
2453 "Invalid size type for `%%local' missing directive");
2454 free_tlist(tt);
2455 free_tlist(origline);
2456 return DIRECTIVE_FOUND;
2458 free_tlist(tt);
2460 /* Round up to even stack slots */
2461 size = ALIGN(size, StackSize);
2463 offset += size; /* Negative offset, increment before */
2465 /* Now define the macro for the argument */
2466 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2467 local, StackPointer, offset);
2468 do_directive(tokenize(directive));
2470 /* Now define the assign to setup the enter_c macro correctly */
2471 snprintf(directive, sizeof(directive),
2472 "%%assign %%$localsize %%$localsize+%d", size);
2473 do_directive(tokenize(directive));
2475 /* Move to the next argument in the list */
2476 tline = tline->next;
2477 if (tline && tline->type == TOK_WHITESPACE)
2478 tline = tline->next;
2479 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2480 LocalOffset = offset;
2481 free_tlist(origline);
2482 return DIRECTIVE_FOUND;
2484 case PP_CLEAR:
2485 if (tline->next)
2486 error(ERR_WARNING|ERR_PASS1,
2487 "trailing garbage after `%%clear' ignored");
2488 free_macros();
2489 init_macros();
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2493 case PP_DEPEND:
2494 t = tline->next = expand_smacro(tline->next);
2495 skip_white_(t);
2496 if (!t || (t->type != TOK_STRING &&
2497 t->type != TOK_INTERNAL_STRING)) {
2498 error(ERR_NONFATAL, "`%%depend' expects a file name");
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND; /* but we did _something_ */
2502 if (t->next)
2503 error(ERR_WARNING|ERR_PASS1,
2504 "trailing garbage after `%%depend' ignored");
2505 p = t->text;
2506 if (t->type != TOK_INTERNAL_STRING)
2507 nasm_unquote_cstr(p, i);
2508 if (dephead && !in_list(*dephead, p)) {
2509 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2510 sl->next = NULL;
2511 strcpy(sl->str, p);
2512 *deptail = sl;
2513 deptail = &sl->next;
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
2518 case PP_INCLUDE:
2519 t = tline->next = expand_smacro(tline->next);
2520 skip_white_(t);
2522 if (!t || (t->type != TOK_STRING &&
2523 t->type != TOK_INTERNAL_STRING)) {
2524 error(ERR_NONFATAL, "`%%include' expects a file name");
2525 free_tlist(origline);
2526 return DIRECTIVE_FOUND; /* but we did _something_ */
2528 if (t->next)
2529 error(ERR_WARNING|ERR_PASS1,
2530 "trailing garbage after `%%include' ignored");
2531 p = t->text;
2532 if (t->type != TOK_INTERNAL_STRING)
2533 nasm_unquote_cstr(p, i);
2534 inc = nasm_malloc(sizeof(Include));
2535 inc->next = istk;
2536 inc->conds = NULL;
2537 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2538 if (!inc->fp) {
2539 /* -MG given but file not found */
2540 nasm_free(inc);
2541 } else {
2542 inc->fname = src_set_fname(nasm_strdup(p));
2543 inc->lineno = src_set_linnum(0);
2544 inc->lineinc = 1;
2545 inc->expansion = NULL;
2546 inc->mstk = NULL;
2547 istk = inc;
2548 list->uplevel(LIST_INCLUDE);
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND;
2553 case PP_USE:
2555 static macros_t *use_pkg;
2556 const char *pkg_macro = NULL;
2558 tline = tline->next;
2559 skip_white_(tline);
2560 tline = expand_id(tline);
2562 if (!tline || (tline->type != TOK_STRING &&
2563 tline->type != TOK_INTERNAL_STRING &&
2564 tline->type != TOK_ID)) {
2565 error(ERR_NONFATAL, "`%%use' expects a package name");
2566 free_tlist(origline);
2567 return DIRECTIVE_FOUND; /* but we did _something_ */
2569 if (tline->next)
2570 error(ERR_WARNING|ERR_PASS1,
2571 "trailing garbage after `%%use' ignored");
2572 if (tline->type == TOK_STRING)
2573 nasm_unquote_cstr(tline->text, i);
2574 use_pkg = nasm_stdmac_find_package(tline->text);
2575 if (!use_pkg)
2576 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2577 else
2578 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2579 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2580 /* Not already included, go ahead and include it */
2581 stdmacpos = use_pkg;
2583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
2586 case PP_PUSH:
2587 case PP_REPL:
2588 case PP_POP:
2589 tline = tline->next;
2590 skip_white_(tline);
2591 tline = expand_id(tline);
2592 if (tline) {
2593 if (!tok_type_(tline, TOK_ID)) {
2594 error(ERR_NONFATAL, "`%s' expects a context identifier",
2595 pp_directives[i]);
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND; /* but we did _something_ */
2599 if (tline->next)
2600 error(ERR_WARNING|ERR_PASS1,
2601 "trailing garbage after `%s' ignored",
2602 pp_directives[i]);
2603 p = nasm_strdup(tline->text);
2604 } else {
2605 p = NULL; /* Anonymous */
2608 if (i == PP_PUSH) {
2609 ctx = nasm_malloc(sizeof(Context));
2610 ctx->next = cstk;
2611 hash_init(&ctx->localmac, HASH_SMALL);
2612 ctx->name = p;
2613 ctx->number = unique++;
2614 cstk = ctx;
2615 } else {
2616 /* %pop or %repl */
2617 if (!cstk) {
2618 error(ERR_NONFATAL, "`%s': context stack is empty",
2619 pp_directives[i]);
2620 } else if (i == PP_POP) {
2621 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2622 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2623 "expected %s",
2624 cstk->name ? cstk->name : "anonymous", p);
2625 else
2626 ctx_pop();
2627 } else {
2628 /* i == PP_REPL */
2629 nasm_free(cstk->name);
2630 cstk->name = p;
2631 p = NULL;
2633 nasm_free(p);
2635 free_tlist(origline);
2636 return DIRECTIVE_FOUND;
2637 case PP_FATAL:
2638 severity = ERR_FATAL;
2639 goto issue_error;
2640 case PP_ERROR:
2641 severity = ERR_NONFATAL;
2642 goto issue_error;
2643 case PP_WARNING:
2644 severity = ERR_WARNING|ERR_WARN_USER;
2645 goto issue_error;
2647 issue_error:
2649 /* Only error out if this is the final pass */
2650 if (pass != 2 && i != PP_FATAL)
2651 return DIRECTIVE_FOUND;
2653 tline->next = expand_smacro(tline->next);
2654 tline = tline->next;
2655 skip_white_(tline);
2656 t = tline ? tline->next : NULL;
2657 skip_white_(t);
2658 if (tok_type_(tline, TOK_STRING) && !t) {
2659 /* The line contains only a quoted string */
2660 p = tline->text;
2661 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2662 error(severity, "%s", p);
2663 } else {
2664 /* Not a quoted string, or more than a quoted string */
2665 p = detoken(tline, false);
2666 error(severity, "%s", p);
2667 nasm_free(p);
2669 free_tlist(origline);
2670 return DIRECTIVE_FOUND;
2673 CASE_PP_IF:
2674 if (istk->conds && !emitting(istk->conds->state))
2675 j = COND_NEVER;
2676 else {
2677 j = if_condition(tline->next, i);
2678 tline->next = NULL; /* it got freed */
2679 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2681 cond = nasm_malloc(sizeof(Cond));
2682 cond->next = istk->conds;
2683 cond->state = j;
2684 istk->conds = cond;
2685 if(istk->mstk)
2686 istk->mstk->condcnt ++;
2687 free_tlist(origline);
2688 return DIRECTIVE_FOUND;
2690 CASE_PP_ELIF:
2691 if (!istk->conds)
2692 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2693 switch(istk->conds->state) {
2694 case COND_IF_TRUE:
2695 istk->conds->state = COND_DONE;
2696 break;
2698 case COND_DONE:
2699 case COND_NEVER:
2700 break;
2702 case COND_ELSE_TRUE:
2703 case COND_ELSE_FALSE:
2704 error_precond(ERR_WARNING|ERR_PASS1,
2705 "`%%elif' after `%%else' ignored");
2706 istk->conds->state = COND_NEVER;
2707 break;
2709 case COND_IF_FALSE:
2711 * IMPORTANT: In the case of %if, we will already have
2712 * called expand_mmac_params(); however, if we're
2713 * processing an %elif we must have been in a
2714 * non-emitting mode, which would have inhibited
2715 * the normal invocation of expand_mmac_params().
2716 * Therefore, we have to do it explicitly here.
2718 j = if_condition(expand_mmac_params(tline->next), i);
2719 tline->next = NULL; /* it got freed */
2720 istk->conds->state =
2721 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2722 break;
2724 free_tlist(origline);
2725 return DIRECTIVE_FOUND;
2727 case PP_ELSE:
2728 if (tline->next)
2729 error_precond(ERR_WARNING|ERR_PASS1,
2730 "trailing garbage after `%%else' ignored");
2731 if (!istk->conds)
2732 error(ERR_FATAL, "`%%else': no matching `%%if'");
2733 switch(istk->conds->state) {
2734 case COND_IF_TRUE:
2735 case COND_DONE:
2736 istk->conds->state = COND_ELSE_FALSE;
2737 break;
2739 case COND_NEVER:
2740 break;
2742 case COND_IF_FALSE:
2743 istk->conds->state = COND_ELSE_TRUE;
2744 break;
2746 case COND_ELSE_TRUE:
2747 case COND_ELSE_FALSE:
2748 error_precond(ERR_WARNING|ERR_PASS1,
2749 "`%%else' after `%%else' ignored.");
2750 istk->conds->state = COND_NEVER;
2751 break;
2753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
2756 case PP_ENDIF:
2757 if (tline->next)
2758 error_precond(ERR_WARNING|ERR_PASS1,
2759 "trailing garbage after `%%endif' ignored");
2760 if (!istk->conds)
2761 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2762 cond = istk->conds;
2763 istk->conds = cond->next;
2764 nasm_free(cond);
2765 if(istk->mstk)
2766 istk->mstk->condcnt --;
2767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
2770 case PP_RMACRO:
2771 case PP_IRMACRO:
2772 case PP_MACRO:
2773 case PP_IMACRO:
2774 if (defining) {
2775 error(ERR_FATAL, "`%s': already defining a macro",
2776 pp_directives[i]);
2777 return DIRECTIVE_FOUND;
2779 defining = nasm_malloc(sizeof(MMacro));
2780 defining->max_depth =
2781 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2782 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2783 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2784 nasm_free(defining);
2785 defining = NULL;
2786 return DIRECTIVE_FOUND;
2789 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2790 while (mmac) {
2791 if (!strcmp(mmac->name, defining->name) &&
2792 (mmac->nparam_min <= defining->nparam_max
2793 || defining->plus)
2794 && (defining->nparam_min <= mmac->nparam_max
2795 || mmac->plus)) {
2796 error(ERR_WARNING|ERR_PASS1,
2797 "redefining multi-line macro `%s'", defining->name);
2798 return DIRECTIVE_FOUND;
2800 mmac = mmac->next;
2802 free_tlist(origline);
2803 return DIRECTIVE_FOUND;
2805 case PP_ENDM:
2806 case PP_ENDMACRO:
2807 if (! (defining && defining->name)) {
2808 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2809 return DIRECTIVE_FOUND;
2811 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2812 defining->next = *mmhead;
2813 *mmhead = defining;
2814 defining = NULL;
2815 free_tlist(origline);
2816 return DIRECTIVE_FOUND;
2818 case PP_EXITMACRO:
2820 * We must search along istk->expansion until we hit a
2821 * macro-end marker for a macro with a name. Then we
2822 * bypass all lines between exitmacro and endmacro.
2824 list_for_each(l, istk->expansion)
2825 if (l->finishes && l->finishes->name)
2826 break;
2828 if (l) {
2830 * Remove all conditional entries relative to this
2831 * macro invocation. (safe to do in this context)
2833 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2834 cond = istk->conds;
2835 istk->conds = cond->next;
2836 nasm_free(cond);
2838 istk->expansion = l;
2839 } else {
2840 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2842 free_tlist(origline);
2843 return DIRECTIVE_FOUND;
2845 case PP_UNMACRO:
2846 case PP_UNIMACRO:
2848 MMacro **mmac_p;
2849 MMacro spec;
2851 spec.casesense = (i == PP_UNMACRO);
2852 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2853 return DIRECTIVE_FOUND;
2855 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2856 while (mmac_p && *mmac_p) {
2857 mmac = *mmac_p;
2858 if (mmac->casesense == spec.casesense &&
2859 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2860 mmac->nparam_min == spec.nparam_min &&
2861 mmac->nparam_max == spec.nparam_max &&
2862 mmac->plus == spec.plus) {
2863 *mmac_p = mmac->next;
2864 free_mmacro(mmac);
2865 } else {
2866 mmac_p = &mmac->next;
2869 free_tlist(origline);
2870 free_tlist(spec.dlist);
2871 return DIRECTIVE_FOUND;
2874 case PP_ROTATE:
2875 if (tline->next && tline->next->type == TOK_WHITESPACE)
2876 tline = tline->next;
2877 if (!tline->next) {
2878 free_tlist(origline);
2879 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2880 return DIRECTIVE_FOUND;
2882 t = expand_smacro(tline->next);
2883 tline->next = NULL;
2884 free_tlist(origline);
2885 tline = t;
2886 tptr = &t;
2887 tokval.t_type = TOKEN_INVALID;
2888 evalresult =
2889 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2890 free_tlist(tline);
2891 if (!evalresult)
2892 return DIRECTIVE_FOUND;
2893 if (tokval.t_type)
2894 error(ERR_WARNING|ERR_PASS1,
2895 "trailing garbage after expression ignored");
2896 if (!is_simple(evalresult)) {
2897 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2898 return DIRECTIVE_FOUND;
2900 mmac = istk->mstk;
2901 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2902 mmac = mmac->next_active;
2903 if (!mmac) {
2904 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2905 } else if (mmac->nparam == 0) {
2906 error(ERR_NONFATAL,
2907 "`%%rotate' invoked within macro without parameters");
2908 } else {
2909 int rotate = mmac->rotate + reloc_value(evalresult);
2911 rotate %= (int)mmac->nparam;
2912 if (rotate < 0)
2913 rotate += mmac->nparam;
2915 mmac->rotate = rotate;
2917 return DIRECTIVE_FOUND;
2919 case PP_REP:
2920 nolist = false;
2921 do {
2922 tline = tline->next;
2923 } while (tok_type_(tline, TOK_WHITESPACE));
2925 if (tok_type_(tline, TOK_ID) &&
2926 nasm_stricmp(tline->text, ".nolist") == 0) {
2927 nolist = true;
2928 do {
2929 tline = tline->next;
2930 } while (tok_type_(tline, TOK_WHITESPACE));
2933 if (tline) {
2934 t = expand_smacro(tline);
2935 tptr = &t;
2936 tokval.t_type = TOKEN_INVALID;
2937 evalresult =
2938 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2939 if (!evalresult) {
2940 free_tlist(origline);
2941 return DIRECTIVE_FOUND;
2943 if (tokval.t_type)
2944 error(ERR_WARNING|ERR_PASS1,
2945 "trailing garbage after expression ignored");
2946 if (!is_simple(evalresult)) {
2947 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2948 return DIRECTIVE_FOUND;
2950 count = reloc_value(evalresult);
2951 if (count >= REP_LIMIT) {
2952 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2953 count = 0;
2954 } else
2955 count++;
2956 } else {
2957 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2958 count = 0;
2960 free_tlist(origline);
2962 tmp_defining = defining;
2963 defining = nasm_malloc(sizeof(MMacro));
2964 defining->prev = NULL;
2965 defining->name = NULL; /* flags this macro as a %rep block */
2966 defining->casesense = false;
2967 defining->plus = false;
2968 defining->nolist = nolist;
2969 defining->in_progress = count;
2970 defining->max_depth = 0;
2971 defining->nparam_min = defining->nparam_max = 0;
2972 defining->defaults = NULL;
2973 defining->dlist = NULL;
2974 defining->expansion = NULL;
2975 defining->next_active = istk->mstk;
2976 defining->rep_nest = tmp_defining;
2977 return DIRECTIVE_FOUND;
2979 case PP_ENDREP:
2980 if (!defining || defining->name) {
2981 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2982 return DIRECTIVE_FOUND;
2986 * Now we have a "macro" defined - although it has no name
2987 * and we won't be entering it in the hash tables - we must
2988 * push a macro-end marker for it on to istk->expansion.
2989 * After that, it will take care of propagating itself (a
2990 * macro-end marker line for a macro which is really a %rep
2991 * block will cause the macro to be re-expanded, complete
2992 * with another macro-end marker to ensure the process
2993 * continues) until the whole expansion is forcibly removed
2994 * from istk->expansion by a %exitrep.
2996 l = nasm_malloc(sizeof(Line));
2997 l->next = istk->expansion;
2998 l->finishes = defining;
2999 l->first = NULL;
3000 istk->expansion = l;
3002 istk->mstk = defining;
3004 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3005 tmp_defining = defining;
3006 defining = defining->rep_nest;
3007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
3010 case PP_EXITREP:
3012 * We must search along istk->expansion until we hit a
3013 * macro-end marker for a macro with no name. Then we set
3014 * its `in_progress' flag to 0.
3016 list_for_each(l, istk->expansion)
3017 if (l->finishes && !l->finishes->name)
3018 break;
3020 if (l)
3021 l->finishes->in_progress = 1;
3022 else
3023 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3027 case PP_XDEFINE:
3028 case PP_IXDEFINE:
3029 case PP_DEFINE:
3030 case PP_IDEFINE:
3031 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3033 tline = tline->next;
3034 skip_white_(tline);
3035 tline = expand_id(tline);
3036 if (!tline || (tline->type != TOK_ID &&
3037 (tline->type != TOK_PREPROC_ID ||
3038 tline->text[1] != '$'))) {
3039 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3040 pp_directives[i]);
3041 free_tlist(origline);
3042 return DIRECTIVE_FOUND;
3045 ctx = get_ctx(tline->text, &mname, false);
3046 last = tline;
3047 param_start = tline = tline->next;
3048 nparam = 0;
3050 /* Expand the macro definition now for %xdefine and %ixdefine */
3051 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3052 tline = expand_smacro(tline);
3054 if (tok_is_(tline, "(")) {
3056 * This macro has parameters.
3059 tline = tline->next;
3060 while (1) {
3061 skip_white_(tline);
3062 if (!tline) {
3063 error(ERR_NONFATAL, "parameter identifier expected");
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3067 if (tline->type != TOK_ID) {
3068 error(ERR_NONFATAL,
3069 "`%s': parameter identifier expected",
3070 tline->text);
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3074 tline->type = TOK_SMAC_PARAM + nparam++;
3075 tline = tline->next;
3076 skip_white_(tline);
3077 if (tok_is_(tline, ",")) {
3078 tline = tline->next;
3079 } else {
3080 if (!tok_is_(tline, ")")) {
3081 error(ERR_NONFATAL,
3082 "`)' expected to terminate macro template");
3083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3086 break;
3089 last = tline;
3090 tline = tline->next;
3092 if (tok_type_(tline, TOK_WHITESPACE))
3093 last = tline, tline = tline->next;
3094 macro_start = NULL;
3095 last->next = NULL;
3096 t = tline;
3097 while (t) {
3098 if (t->type == TOK_ID) {
3099 list_for_each(tt, param_start)
3100 if (tt->type >= TOK_SMAC_PARAM &&
3101 !strcmp(tt->text, t->text))
3102 t->type = tt->type;
3104 tt = t->next;
3105 t->next = macro_start;
3106 macro_start = t;
3107 t = tt;
3110 * Good. We now have a macro name, a parameter count, and a
3111 * token list (in reverse order) for an expansion. We ought
3112 * to be OK just to create an SMacro, store it, and let
3113 * free_tlist have the rest of the line (which we have
3114 * carefully re-terminated after chopping off the expansion
3115 * from the end).
3117 define_smacro(ctx, mname, casesense, nparam, macro_start);
3118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
3121 case PP_UNDEF:
3122 tline = tline->next;
3123 skip_white_(tline);
3124 tline = expand_id(tline);
3125 if (!tline || (tline->type != TOK_ID &&
3126 (tline->type != TOK_PREPROC_ID ||
3127 tline->text[1] != '$'))) {
3128 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3132 if (tline->next) {
3133 error(ERR_WARNING|ERR_PASS1,
3134 "trailing garbage after macro name ignored");
3137 /* Find the context that symbol belongs to */
3138 ctx = get_ctx(tline->text, &mname, false);
3139 undef_smacro(ctx, mname);
3140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
3143 case PP_DEFSTR:
3144 case PP_IDEFSTR:
3145 casesense = (i == PP_DEFSTR);
3147 tline = tline->next;
3148 skip_white_(tline);
3149 tline = expand_id(tline);
3150 if (!tline || (tline->type != TOK_ID &&
3151 (tline->type != TOK_PREPROC_ID ||
3152 tline->text[1] != '$'))) {
3153 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3154 pp_directives[i]);
3155 free_tlist(origline);
3156 return DIRECTIVE_FOUND;
3159 ctx = get_ctx(tline->text, &mname, false);
3160 last = tline;
3161 tline = expand_smacro(tline->next);
3162 last->next = NULL;
3164 while (tok_type_(tline, TOK_WHITESPACE))
3165 tline = delete_Token(tline);
3167 p = detoken(tline, false);
3168 macro_start = nasm_malloc(sizeof(*macro_start));
3169 macro_start->next = NULL;
3170 macro_start->text = nasm_quote(p, strlen(p));
3171 macro_start->type = TOK_STRING;
3172 macro_start->a.mac = NULL;
3173 nasm_free(p);
3176 * We now have a macro name, an implicit parameter count of
3177 * zero, and a string token to use as an expansion. Create
3178 * and store an SMacro.
3180 define_smacro(ctx, mname, casesense, 0, macro_start);
3181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3184 case PP_DEFTOK:
3185 case PP_IDEFTOK:
3186 casesense = (i == PP_DEFTOK);
3188 tline = tline->next;
3189 skip_white_(tline);
3190 tline = expand_id(tline);
3191 if (!tline || (tline->type != TOK_ID &&
3192 (tline->type != TOK_PREPROC_ID ||
3193 tline->text[1] != '$'))) {
3194 error(ERR_NONFATAL,
3195 "`%s' expects a macro identifier as first parameter",
3196 pp_directives[i]);
3197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3200 ctx = get_ctx(tline->text, &mname, false);
3201 last = tline;
3202 tline = expand_smacro(tline->next);
3203 last->next = NULL;
3205 t = tline;
3206 while (tok_type_(t, TOK_WHITESPACE))
3207 t = t->next;
3208 /* t should now point to the string */
3209 if (!tok_type_(t, TOK_STRING)) {
3210 error(ERR_NONFATAL,
3211 "`%s` requires string as second parameter",
3212 pp_directives[i]);
3213 free_tlist(tline);
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3219 * Convert the string to a token stream. Note that smacros
3220 * are stored with the token stream reversed, so we have to
3221 * reverse the output of tokenize().
3223 nasm_unquote_cstr(t->text, i);
3224 macro_start = reverse_tokens(tokenize(t->text));
3227 * We now have a macro name, an implicit parameter count of
3228 * zero, and a numeric token to use as an expansion. Create
3229 * and store an SMacro.
3231 define_smacro(ctx, mname, casesense, 0, macro_start);
3232 free_tlist(tline);
3233 free_tlist(origline);
3234 return DIRECTIVE_FOUND;
3236 case PP_PATHSEARCH:
3238 FILE *fp;
3239 StrList *xsl = NULL;
3240 StrList **xst = &xsl;
3242 casesense = true;
3244 tline = tline->next;
3245 skip_white_(tline);
3246 tline = expand_id(tline);
3247 if (!tline || (tline->type != TOK_ID &&
3248 (tline->type != TOK_PREPROC_ID ||
3249 tline->text[1] != '$'))) {
3250 error(ERR_NONFATAL,
3251 "`%%pathsearch' expects a macro identifier as first parameter");
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3255 ctx = get_ctx(tline->text, &mname, false);
3256 last = tline;
3257 tline = expand_smacro(tline->next);
3258 last->next = NULL;
3260 t = tline;
3261 while (tok_type_(t, TOK_WHITESPACE))
3262 t = t->next;
3264 if (!t || (t->type != TOK_STRING &&
3265 t->type != TOK_INTERNAL_STRING)) {
3266 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3267 free_tlist(tline);
3268 free_tlist(origline);
3269 return DIRECTIVE_FOUND; /* but we did _something_ */
3271 if (t->next)
3272 error(ERR_WARNING|ERR_PASS1,
3273 "trailing garbage after `%%pathsearch' ignored");
3274 p = t->text;
3275 if (t->type != TOK_INTERNAL_STRING)
3276 nasm_unquote(p, NULL);
3278 fp = inc_fopen(p, &xsl, &xst, true);
3279 if (fp) {
3280 p = xsl->str;
3281 fclose(fp); /* Don't actually care about the file */
3283 macro_start = nasm_malloc(sizeof(*macro_start));
3284 macro_start->next = NULL;
3285 macro_start->text = nasm_quote(p, strlen(p));
3286 macro_start->type = TOK_STRING;
3287 macro_start->a.mac = NULL;
3288 if (xsl)
3289 nasm_free(xsl);
3292 * We now have a macro name, an implicit parameter count of
3293 * zero, and a string token to use as an expansion. Create
3294 * and store an SMacro.
3296 define_smacro(ctx, mname, casesense, 0, macro_start);
3297 free_tlist(tline);
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3302 case PP_STRLEN:
3303 casesense = true;
3305 tline = tline->next;
3306 skip_white_(tline);
3307 tline = expand_id(tline);
3308 if (!tline || (tline->type != TOK_ID &&
3309 (tline->type != TOK_PREPROC_ID ||
3310 tline->text[1] != '$'))) {
3311 error(ERR_NONFATAL,
3312 "`%%strlen' expects a macro identifier as first parameter");
3313 free_tlist(origline);
3314 return DIRECTIVE_FOUND;
3316 ctx = get_ctx(tline->text, &mname, false);
3317 last = tline;
3318 tline = expand_smacro(tline->next);
3319 last->next = NULL;
3321 t = tline;
3322 while (tok_type_(t, TOK_WHITESPACE))
3323 t = t->next;
3324 /* t should now point to the string */
3325 if (!tok_type_(t, TOK_STRING)) {
3326 error(ERR_NONFATAL,
3327 "`%%strlen` requires string as second parameter");
3328 free_tlist(tline);
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3333 macro_start = nasm_malloc(sizeof(*macro_start));
3334 macro_start->next = NULL;
3335 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3336 macro_start->a.mac = NULL;
3339 * We now have a macro name, an implicit parameter count of
3340 * zero, and a numeric token to use as an expansion. Create
3341 * and store an SMacro.
3343 define_smacro(ctx, mname, casesense, 0, macro_start);
3344 free_tlist(tline);
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3348 case PP_STRCAT:
3349 casesense = true;
3351 tline = tline->next;
3352 skip_white_(tline);
3353 tline = expand_id(tline);
3354 if (!tline || (tline->type != TOK_ID &&
3355 (tline->type != TOK_PREPROC_ID ||
3356 tline->text[1] != '$'))) {
3357 error(ERR_NONFATAL,
3358 "`%%strcat' expects a macro identifier as first parameter");
3359 free_tlist(origline);
3360 return DIRECTIVE_FOUND;
3362 ctx = get_ctx(tline->text, &mname, false);
3363 last = tline;
3364 tline = expand_smacro(tline->next);
3365 last->next = NULL;
3367 len = 0;
3368 list_for_each(t, tline) {
3369 switch (t->type) {
3370 case TOK_WHITESPACE:
3371 break;
3372 case TOK_STRING:
3373 len += t->a.len = nasm_unquote(t->text, NULL);
3374 break;
3375 case TOK_OTHER:
3376 if (!strcmp(t->text, ",")) /* permit comma separators */
3377 break;
3378 /* else fall through */
3379 default:
3380 error(ERR_NONFATAL,
3381 "non-string passed to `%%strcat' (%d)", t->type);
3382 free_tlist(tline);
3383 free_tlist(origline);
3384 return DIRECTIVE_FOUND;
3388 p = pp = nasm_malloc(len);
3389 list_for_each(t, tline) {
3390 if (t->type == TOK_STRING) {
3391 memcpy(p, t->text, t->a.len);
3392 p += t->a.len;
3397 * We now have a macro name, an implicit parameter count of
3398 * zero, and a numeric token to use as an expansion. Create
3399 * and store an SMacro.
3401 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3402 macro_start->text = nasm_quote(pp, len);
3403 nasm_free(pp);
3404 define_smacro(ctx, mname, casesense, 0, macro_start);
3405 free_tlist(tline);
3406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
3409 case PP_SUBSTR:
3411 int64_t start, count;
3412 size_t len;
3414 casesense = true;
3416 tline = tline->next;
3417 skip_white_(tline);
3418 tline = expand_id(tline);
3419 if (!tline || (tline->type != TOK_ID &&
3420 (tline->type != TOK_PREPROC_ID ||
3421 tline->text[1] != '$'))) {
3422 error(ERR_NONFATAL,
3423 "`%%substr' expects a macro identifier as first parameter");
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3427 ctx = get_ctx(tline->text, &mname, false);
3428 last = tline;
3429 tline = expand_smacro(tline->next);
3430 last->next = NULL;
3432 if (tline) /* skip expanded id */
3433 t = tline->next;
3434 while (tok_type_(t, TOK_WHITESPACE))
3435 t = t->next;
3437 /* t should now point to the string */
3438 if (!tok_type_(t, TOK_STRING)) {
3439 error(ERR_NONFATAL,
3440 "`%%substr` requires string as second parameter");
3441 free_tlist(tline);
3442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
3446 tt = t->next;
3447 tptr = &tt;
3448 tokval.t_type = TOKEN_INVALID;
3449 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3450 pass, error, NULL);
3451 if (!evalresult) {
3452 free_tlist(tline);
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
3455 } else if (!is_simple(evalresult)) {
3456 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3457 free_tlist(tline);
3458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
3461 start = evalresult->value - 1;
3463 while (tok_type_(tt, TOK_WHITESPACE))
3464 tt = tt->next;
3465 if (!tt) {
3466 count = 1; /* Backwards compatibility: one character */
3467 } else {
3468 tokval.t_type = TOKEN_INVALID;
3469 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3470 pass, error, NULL);
3471 if (!evalresult) {
3472 free_tlist(tline);
3473 free_tlist(origline);
3474 return DIRECTIVE_FOUND;
3475 } else if (!is_simple(evalresult)) {
3476 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3477 free_tlist(tline);
3478 free_tlist(origline);
3479 return DIRECTIVE_FOUND;
3481 count = evalresult->value;
3484 len = nasm_unquote(t->text, NULL);
3486 /* make start and count being in range */
3487 if (start < 0)
3488 start = 0;
3489 if (count < 0)
3490 count = len + count + 1 - start;
3491 if (start + count > (int64_t)len)
3492 count = len - start;
3493 if (!len || count < 0 || start >=(int64_t)len)
3494 start = -1, count = 0; /* empty string */
3496 macro_start = nasm_malloc(sizeof(*macro_start));
3497 macro_start->next = NULL;
3498 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3499 macro_start->type = TOK_STRING;
3500 macro_start->a.mac = NULL;
3503 * We now have a macro name, an implicit parameter count of
3504 * zero, and a numeric token to use as an expansion. Create
3505 * and store an SMacro.
3507 define_smacro(ctx, mname, casesense, 0, macro_start);
3508 free_tlist(tline);
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3513 case PP_ASSIGN:
3514 case PP_IASSIGN:
3515 casesense = (i == PP_ASSIGN);
3517 tline = tline->next;
3518 skip_white_(tline);
3519 tline = expand_id(tline);
3520 if (!tline || (tline->type != TOK_ID &&
3521 (tline->type != TOK_PREPROC_ID ||
3522 tline->text[1] != '$'))) {
3523 error(ERR_NONFATAL,
3524 "`%%%sassign' expects a macro identifier",
3525 (i == PP_IASSIGN ? "i" : ""));
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3529 ctx = get_ctx(tline->text, &mname, false);
3530 last = tline;
3531 tline = expand_smacro(tline->next);
3532 last->next = NULL;
3534 t = tline;
3535 tptr = &t;
3536 tokval.t_type = TOKEN_INVALID;
3537 evalresult =
3538 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3539 free_tlist(tline);
3540 if (!evalresult) {
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3545 if (tokval.t_type)
3546 error(ERR_WARNING|ERR_PASS1,
3547 "trailing garbage after expression ignored");
3549 if (!is_simple(evalresult)) {
3550 error(ERR_NONFATAL,
3551 "non-constant value given to `%%%sassign'",
3552 (i == PP_IASSIGN ? "i" : ""));
3553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3557 macro_start = nasm_malloc(sizeof(*macro_start));
3558 macro_start->next = NULL;
3559 make_tok_num(macro_start, reloc_value(evalresult));
3560 macro_start->a.mac = NULL;
3563 * We now have a macro name, an implicit parameter count of
3564 * zero, and a numeric token to use as an expansion. Create
3565 * and store an SMacro.
3567 define_smacro(ctx, mname, casesense, 0, macro_start);
3568 free_tlist(origline);
3569 return DIRECTIVE_FOUND;
3571 case PP_LINE:
3573 * Syntax is `%line nnn[+mmm] [filename]'
3575 tline = tline->next;
3576 skip_white_(tline);
3577 if (!tok_type_(tline, TOK_NUMBER)) {
3578 error(ERR_NONFATAL, "`%%line' expects line number");
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3582 k = readnum(tline->text, &err);
3583 m = 1;
3584 tline = tline->next;
3585 if (tok_is_(tline, "+")) {
3586 tline = tline->next;
3587 if (!tok_type_(tline, TOK_NUMBER)) {
3588 error(ERR_NONFATAL, "`%%line' expects line increment");
3589 free_tlist(origline);
3590 return DIRECTIVE_FOUND;
3592 m = readnum(tline->text, &err);
3593 tline = tline->next;
3595 skip_white_(tline);
3596 src_set_linnum(k);
3597 istk->lineinc = m;
3598 if (tline) {
3599 nasm_free(src_set_fname(detoken(tline, false)));
3601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
3604 default:
3605 error(ERR_FATAL,
3606 "preprocessor directive `%s' not yet implemented",
3607 pp_directives[i]);
3608 return DIRECTIVE_FOUND;
3613 * Ensure that a macro parameter contains a condition code and
3614 * nothing else. Return the condition code index if so, or -1
3615 * otherwise.
3617 static int find_cc(Token * t)
3619 Token *tt;
3620 int i, j, k, m;
3622 if (!t)
3623 return -1; /* Probably a %+ without a space */
3625 skip_white_(t);
3626 if (t->type != TOK_ID)
3627 return -1;
3628 tt = t->next;
3629 skip_white_(tt);
3630 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3631 return -1;
3633 i = -1;
3634 j = ARRAY_SIZE(conditions);
3635 while (j - i > 1) {
3636 k = (j + i) / 2;
3637 m = nasm_stricmp(t->text, conditions[k]);
3638 if (m == 0) {
3639 i = k;
3640 j = -2;
3641 break;
3642 } else if (m < 0) {
3643 j = k;
3644 } else
3645 i = k;
3647 if (j != -2)
3648 return -1;
3649 return i;
3652 static bool paste_tokens(Token **head, const struct tokseq_match *m,
3653 int mnum, bool handle_paste_tokens)
3655 Token **tail, *t, *tt;
3656 Token **paste_head;
3657 bool did_paste = false;
3658 char *tmp;
3659 int i;
3661 /* Now handle token pasting... */
3662 paste_head = NULL;
3663 tail = head;
3664 while ((t = *tail) && (tt = t->next)) {
3665 switch (t->type) {
3666 case TOK_WHITESPACE:
3667 if (tt->type == TOK_WHITESPACE) {
3668 /* Zap adjacent whitespace tokens */
3669 t->next = delete_Token(tt);
3670 } else {
3671 /* Do not advance paste_head here */
3672 tail = &t->next;
3674 break;
3675 case TOK_PASTE: /* %+ */
3676 if (handle_paste_tokens) {
3677 /* Zap %+ and whitespace tokens to the right */
3678 while (t && (t->type == TOK_WHITESPACE ||
3679 t->type == TOK_PASTE))
3680 t = *tail = delete_Token(t);
3681 if (!t) { /* Dangling %+ term */
3682 if (paste_head)
3683 (*paste_head)->next = NULL;
3684 else
3685 *head = NULL;
3686 return did_paste;
3688 tail = paste_head;
3689 t = *tail;
3690 tt = t->next;
3691 while (tok_type_(tt, TOK_WHITESPACE))
3692 tt = t->next = delete_Token(tt);
3693 if (tt) {
3694 tmp = nasm_strcat(t->text, tt->text);
3695 delete_Token(t);
3696 tt = delete_Token(tt);
3697 t = *tail = tokenize(tmp);
3698 nasm_free(tmp);
3699 while (t->next) {
3700 tail = &t->next;
3701 t = t->next;
3703 t->next = tt; /* Attach the remaining token chain */
3704 did_paste = true;
3706 paste_head = tail;
3707 tail = &t->next;
3708 break;
3710 /* else fall through */
3711 default:
3713 * Concatenation of tokens might look nontrivial
3714 * but in real it's pretty simple -- the caller
3715 * prepares the masks of token types to be concatenated
3716 * and we simply find matched sequences and slip
3717 * them together
3719 for (i = 0; i < mnum; i++) {
3720 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
3721 size_t len = 0;
3722 char *tmp, *p;
3724 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
3725 len += strlen(tt->text);
3726 tt = tt->next;
3730 * Now tt points to the first token after
3731 * the potential paste area...
3733 if (tt != t->next) {
3734 /* We have at least two tokens... */
3735 len += strlen(t->text);
3736 p = tmp = nasm_malloc(len+1);
3737 while (t != tt) {
3738 strcpy(p, t->text);
3739 p = strchr(p, '\0');
3740 t = delete_Token(t);
3742 t = *tail = tokenize(tmp);
3743 nasm_free(tmp);
3744 while (t->next) {
3745 tail = &t->next;
3746 t = t->next;
3748 t->next = tt; /* Attach the remaining token chain */
3749 did_paste = true;
3751 paste_head = tail;
3752 tail = &t->next;
3753 break;
3756 if (i >= mnum) { /* no match */
3757 tail = &t->next;
3758 if (!tok_type_(t->next, TOK_WHITESPACE))
3759 paste_head = tail;
3761 break;
3764 return did_paste;
3768 * expands to a list of tokens from %{x:y}
3770 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3772 Token *t = tline, **tt, *tm, *head;
3773 char *pos;
3774 int fst, lst, j, i;
3776 pos = strchr(tline->text, ':');
3777 nasm_assert(pos);
3779 lst = atoi(pos + 1);
3780 fst = atoi(tline->text + 1);
3783 * only macros params are accounted so
3784 * if someone passes %0 -- we reject such
3785 * value(s)
3787 if (lst == 0 || fst == 0)
3788 goto err;
3790 /* the values should be sane */
3791 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3792 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3793 goto err;
3795 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3796 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3798 /* counted from zero */
3799 fst--, lst--;
3802 * it will be at least one token
3804 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3805 t = new_Token(NULL, tm->type, tm->text, 0);
3806 head = t, tt = &t->next;
3807 if (fst < lst) {
3808 for (i = fst + 1; i <= lst; i++) {
3809 t = new_Token(NULL, TOK_OTHER, ",", 0);
3810 *tt = t, tt = &t->next;
3811 j = (i + mac->rotate) % mac->nparam;
3812 tm = mac->params[j];
3813 t = new_Token(NULL, tm->type, tm->text, 0);
3814 *tt = t, tt = &t->next;
3816 } else {
3817 for (i = fst - 1; i >= lst; i--) {
3818 t = new_Token(NULL, TOK_OTHER, ",", 0);
3819 *tt = t, tt = &t->next;
3820 j = (i + mac->rotate) % mac->nparam;
3821 tm = mac->params[j];
3822 t = new_Token(NULL, tm->type, tm->text, 0);
3823 *tt = t, tt = &t->next;
3827 *last = tt;
3828 return head;
3830 err:
3831 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3832 &tline->text[1]);
3833 return tline;
3837 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3838 * %-n) and MMacro-local identifiers (%%foo) as well as
3839 * macro indirection (%[...]) and range (%{..:..}).
3841 static Token *expand_mmac_params(Token * tline)
3843 Token *t, *tt, **tail, *thead;
3844 bool changed = false;
3845 char *pos;
3847 tail = &thead;
3848 thead = NULL;
3850 while (tline) {
3851 if (tline->type == TOK_PREPROC_ID &&
3852 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3853 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3854 tline->text[1] == '%')) {
3855 char *text = NULL;
3856 int type = 0, cc; /* type = 0 to placate optimisers */
3857 char tmpbuf[30];
3858 unsigned int n;
3859 int i;
3860 MMacro *mac;
3862 t = tline;
3863 tline = tline->next;
3865 mac = istk->mstk;
3866 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3867 mac = mac->next_active;
3868 if (!mac) {
3869 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3870 } else {
3871 pos = strchr(t->text, ':');
3872 if (!pos) {
3873 switch (t->text[1]) {
3875 * We have to make a substitution of one of the
3876 * forms %1, %-1, %+1, %%foo, %0.
3878 case '0':
3879 type = TOK_NUMBER;
3880 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3881 text = nasm_strdup(tmpbuf);
3882 break;
3883 case '%':
3884 type = TOK_ID;
3885 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3886 mac->unique);
3887 text = nasm_strcat(tmpbuf, t->text + 2);
3888 break;
3889 case '-':
3890 n = atoi(t->text + 2) - 1;
3891 if (n >= mac->nparam)
3892 tt = NULL;
3893 else {
3894 if (mac->nparam > 1)
3895 n = (n + mac->rotate) % mac->nparam;
3896 tt = mac->params[n];
3898 cc = find_cc(tt);
3899 if (cc == -1) {
3900 error(ERR_NONFATAL,
3901 "macro parameter %d is not a condition code",
3902 n + 1);
3903 text = NULL;
3904 } else {
3905 type = TOK_ID;
3906 if (inverse_ccs[cc] == -1) {
3907 error(ERR_NONFATAL,
3908 "condition code `%s' is not invertible",
3909 conditions[cc]);
3910 text = NULL;
3911 } else
3912 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3914 break;
3915 case '+':
3916 n = atoi(t->text + 2) - 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 cc = find_cc(tt);
3925 if (cc == -1) {
3926 error(ERR_NONFATAL,
3927 "macro parameter %d is not a condition code",
3928 n + 1);
3929 text = NULL;
3930 } else {
3931 type = TOK_ID;
3932 text = nasm_strdup(conditions[cc]);
3934 break;
3935 default:
3936 n = atoi(t->text + 1) - 1;
3937 if (n >= mac->nparam)
3938 tt = NULL;
3939 else {
3940 if (mac->nparam > 1)
3941 n = (n + mac->rotate) % mac->nparam;
3942 tt = mac->params[n];
3944 if (tt) {
3945 for (i = 0; i < mac->paramlen[n]; i++) {
3946 *tail = new_Token(NULL, tt->type, tt->text, 0);
3947 tail = &(*tail)->next;
3948 tt = tt->next;
3951 text = NULL; /* we've done it here */
3952 break;
3954 } else {
3956 * seems we have a parameters range here
3958 Token *head, **last;
3959 head = expand_mmac_params_range(mac, t, &last);
3960 if (head != t) {
3961 *tail = head;
3962 *last = tline;
3963 tline = head;
3964 text = NULL;
3968 if (!text) {
3969 delete_Token(t);
3970 } else {
3971 *tail = t;
3972 tail = &t->next;
3973 t->type = type;
3974 nasm_free(t->text);
3975 t->text = text;
3976 t->a.mac = NULL;
3978 changed = true;
3979 continue;
3980 } else if (tline->type == TOK_INDIRECT) {
3981 t = tline;
3982 tline = tline->next;
3983 tt = tokenize(t->text);
3984 tt = expand_mmac_params(tt);
3985 tt = expand_smacro(tt);
3986 *tail = tt;
3987 while (tt) {
3988 tt->a.mac = NULL; /* Necessary? */
3989 tail = &tt->next;
3990 tt = tt->next;
3992 delete_Token(t);
3993 changed = true;
3994 } else {
3995 t = *tail = tline;
3996 tline = tline->next;
3997 t->a.mac = NULL;
3998 tail = &t->next;
4001 *tail = NULL;
4003 if (changed) {
4004 const struct tokseq_match t[] = {
4006 PP_CONCAT_MASK(TOK_ID) |
4007 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4008 PP_CONCAT_MASK(TOK_ID) |
4009 PP_CONCAT_MASK(TOK_NUMBER) |
4010 PP_CONCAT_MASK(TOK_FLOAT) |
4011 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4014 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4015 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4018 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4021 return thead;
4025 * Expand all single-line macro calls made in the given line.
4026 * Return the expanded version of the line. The original is deemed
4027 * to be destroyed in the process. (In reality we'll just move
4028 * Tokens from input to output a lot of the time, rather than
4029 * actually bothering to destroy and replicate.)
4032 static Token *expand_smacro(Token * tline)
4034 Token *t, *tt, *mstart, **tail, *thead;
4035 SMacro *head = NULL, *m;
4036 Token **params;
4037 int *paramsize;
4038 unsigned int nparam, sparam;
4039 int brackets;
4040 Token *org_tline = tline;
4041 Context *ctx;
4042 const char *mname;
4043 int deadman = DEADMAN_LIMIT;
4044 bool expanded;
4047 * Trick: we should avoid changing the start token pointer since it can
4048 * be contained in "next" field of other token. Because of this
4049 * we allocate a copy of first token and work with it; at the end of
4050 * routine we copy it back
4052 if (org_tline) {
4053 tline = new_Token(org_tline->next, org_tline->type,
4054 org_tline->text, 0);
4055 tline->a.mac = org_tline->a.mac;
4056 nasm_free(org_tline->text);
4057 org_tline->text = NULL;
4060 expanded = true; /* Always expand %+ at least once */
4062 again:
4063 thead = NULL;
4064 tail = &thead;
4066 while (tline) { /* main token loop */
4067 if (!--deadman) {
4068 error(ERR_NONFATAL, "interminable macro recursion");
4069 goto err;
4072 if ((mname = tline->text)) {
4073 /* if this token is a local macro, look in local context */
4074 if (tline->type == TOK_ID) {
4075 head = (SMacro *)hash_findix(&smacros, mname);
4076 } else if (tline->type == TOK_PREPROC_ID) {
4077 ctx = get_ctx(mname, &mname, true);
4078 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4079 } else
4080 head = NULL;
4083 * We've hit an identifier. As in is_mmacro below, we first
4084 * check whether the identifier is a single-line macro at
4085 * all, then think about checking for parameters if
4086 * necessary.
4088 list_for_each(m, head)
4089 if (!mstrcmp(m->name, mname, m->casesense))
4090 break;
4091 if (m) {
4092 mstart = tline;
4093 params = NULL;
4094 paramsize = NULL;
4095 if (m->nparam == 0) {
4097 * Simple case: the macro is parameterless. Discard the
4098 * one token that the macro call took, and push the
4099 * expansion back on the to-do stack.
4101 if (!m->expansion) {
4102 if (!strcmp("__FILE__", m->name)) {
4103 int32_t num = 0;
4104 char *file = NULL;
4105 src_get(&num, &file);
4106 tline->text = nasm_quote(file, strlen(file));
4107 tline->type = TOK_STRING;
4108 nasm_free(file);
4109 continue;
4111 if (!strcmp("__LINE__", m->name)) {
4112 nasm_free(tline->text);
4113 make_tok_num(tline, src_get_linnum());
4114 continue;
4116 if (!strcmp("__BITS__", m->name)) {
4117 nasm_free(tline->text);
4118 make_tok_num(tline, globalbits);
4119 continue;
4121 tline = delete_Token(tline);
4122 continue;
4124 } else {
4126 * Complicated case: at least one macro with this name
4127 * exists and takes parameters. We must find the
4128 * parameters in the call, count them, find the SMacro
4129 * that corresponds to that form of the macro call, and
4130 * substitute for the parameters when we expand. What a
4131 * pain.
4133 /*tline = tline->next;
4134 skip_white_(tline); */
4135 do {
4136 t = tline->next;
4137 while (tok_type_(t, TOK_SMAC_END)) {
4138 t->a.mac->in_progress = false;
4139 t->text = NULL;
4140 t = tline->next = delete_Token(t);
4142 tline = t;
4143 } while (tok_type_(tline, TOK_WHITESPACE));
4144 if (!tok_is_(tline, "(")) {
4146 * This macro wasn't called with parameters: ignore
4147 * the call. (Behaviour borrowed from gnu cpp.)
4149 tline = mstart;
4150 m = NULL;
4151 } else {
4152 int paren = 0;
4153 int white = 0;
4154 brackets = 0;
4155 nparam = 0;
4156 sparam = PARAM_DELTA;
4157 params = nasm_malloc(sparam * sizeof(Token *));
4158 params[0] = tline->next;
4159 paramsize = nasm_malloc(sparam * sizeof(int));
4160 paramsize[0] = 0;
4161 while (true) { /* parameter loop */
4163 * For some unusual expansions
4164 * which concatenates function call
4166 t = tline->next;
4167 while (tok_type_(t, TOK_SMAC_END)) {
4168 t->a.mac->in_progress = false;
4169 t->text = NULL;
4170 t = tline->next = delete_Token(t);
4172 tline = t;
4174 if (!tline) {
4175 error(ERR_NONFATAL,
4176 "macro call expects terminating `)'");
4177 break;
4179 if (tline->type == TOK_WHITESPACE
4180 && brackets <= 0) {
4181 if (paramsize[nparam])
4182 white++;
4183 else
4184 params[nparam] = tline->next;
4185 continue; /* parameter loop */
4187 if (tline->type == TOK_OTHER
4188 && tline->text[1] == 0) {
4189 char ch = tline->text[0];
4190 if (ch == ',' && !paren && brackets <= 0) {
4191 if (++nparam >= sparam) {
4192 sparam += PARAM_DELTA;
4193 params = nasm_realloc(params,
4194 sparam * sizeof(Token *));
4195 paramsize = nasm_realloc(paramsize,
4196 sparam * sizeof(int));
4198 params[nparam] = tline->next;
4199 paramsize[nparam] = 0;
4200 white = 0;
4201 continue; /* parameter loop */
4203 if (ch == '{' &&
4204 (brackets > 0 || (brackets == 0 &&
4205 !paramsize[nparam])))
4207 if (!(brackets++)) {
4208 params[nparam] = tline->next;
4209 continue; /* parameter loop */
4212 if (ch == '}' && brackets > 0)
4213 if (--brackets == 0) {
4214 brackets = -1;
4215 continue; /* parameter loop */
4217 if (ch == '(' && !brackets)
4218 paren++;
4219 if (ch == ')' && brackets <= 0)
4220 if (--paren < 0)
4221 break;
4223 if (brackets < 0) {
4224 brackets = 0;
4225 error(ERR_NONFATAL, "braces do not "
4226 "enclose all of macro parameter");
4228 paramsize[nparam] += white + 1;
4229 white = 0;
4230 } /* parameter loop */
4231 nparam++;
4232 while (m && (m->nparam != nparam ||
4233 mstrcmp(m->name, mname,
4234 m->casesense)))
4235 m = m->next;
4236 if (!m)
4237 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4238 "macro `%s' exists, "
4239 "but not taking %d parameters",
4240 mstart->text, nparam);
4243 if (m && m->in_progress)
4244 m = NULL;
4245 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4247 * Design question: should we handle !tline, which
4248 * indicates missing ')' here, or expand those
4249 * macros anyway, which requires the (t) test a few
4250 * lines down?
4252 nasm_free(params);
4253 nasm_free(paramsize);
4254 tline = mstart;
4255 } else {
4257 * Expand the macro: we are placed on the last token of the
4258 * call, so that we can easily split the call from the
4259 * following tokens. We also start by pushing an SMAC_END
4260 * token for the cycle removal.
4262 t = tline;
4263 if (t) {
4264 tline = t->next;
4265 t->next = NULL;
4267 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4268 tt->a.mac = m;
4269 m->in_progress = true;
4270 tline = tt;
4271 list_for_each(t, m->expansion) {
4272 if (t->type >= TOK_SMAC_PARAM) {
4273 Token *pcopy = tline, **ptail = &pcopy;
4274 Token *ttt, *pt;
4275 int i;
4277 ttt = params[t->type - TOK_SMAC_PARAM];
4278 i = paramsize[t->type - TOK_SMAC_PARAM];
4279 while (--i >= 0) {
4280 pt = *ptail = new_Token(tline, ttt->type,
4281 ttt->text, 0);
4282 ptail = &pt->next;
4283 ttt = ttt->next;
4285 tline = pcopy;
4286 } else if (t->type == TOK_PREPROC_Q) {
4287 tt = new_Token(tline, TOK_ID, mname, 0);
4288 tline = tt;
4289 } else if (t->type == TOK_PREPROC_QQ) {
4290 tt = new_Token(tline, TOK_ID, m->name, 0);
4291 tline = tt;
4292 } else {
4293 tt = new_Token(tline, t->type, t->text, 0);
4294 tline = tt;
4299 * Having done that, get rid of the macro call, and clean
4300 * up the parameters.
4302 nasm_free(params);
4303 nasm_free(paramsize);
4304 free_tlist(mstart);
4305 expanded = true;
4306 continue; /* main token loop */
4311 if (tline->type == TOK_SMAC_END) {
4312 tline->a.mac->in_progress = false;
4313 tline = delete_Token(tline);
4314 } else {
4315 t = *tail = tline;
4316 tline = tline->next;
4317 t->a.mac = NULL;
4318 t->next = NULL;
4319 tail = &t->next;
4324 * Now scan the entire line and look for successive TOK_IDs that resulted
4325 * after expansion (they can't be produced by tokenize()). The successive
4326 * TOK_IDs should be concatenated.
4327 * Also we look for %+ tokens and concatenate the tokens before and after
4328 * them (without white spaces in between).
4330 if (expanded) {
4331 const struct tokseq_match t[] = {
4333 PP_CONCAT_MASK(TOK_ID) |
4334 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4335 PP_CONCAT_MASK(TOK_ID) |
4336 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4337 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4340 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4342 * If we concatenated something, *and* we had previously expanded
4343 * an actual macro, scan the lines again for macros...
4345 tline = thead;
4346 expanded = false;
4347 goto again;
4351 err:
4352 if (org_tline) {
4353 if (thead) {
4354 *org_tline = *thead;
4355 /* since we just gave text to org_line, don't free it */
4356 thead->text = NULL;
4357 delete_Token(thead);
4358 } else {
4359 /* the expression expanded to empty line;
4360 we can't return NULL for some reasons
4361 we just set the line to a single WHITESPACE token. */
4362 memset(org_tline, 0, sizeof(*org_tline));
4363 org_tline->text = NULL;
4364 org_tline->type = TOK_WHITESPACE;
4366 thead = org_tline;
4369 return thead;
4373 * Similar to expand_smacro but used exclusively with macro identifiers
4374 * right before they are fetched in. The reason is that there can be
4375 * identifiers consisting of several subparts. We consider that if there
4376 * are more than one element forming the name, user wants a expansion,
4377 * otherwise it will be left as-is. Example:
4379 * %define %$abc cde
4381 * the identifier %$abc will be left as-is so that the handler for %define
4382 * will suck it and define the corresponding value. Other case:
4384 * %define _%$abc cde
4386 * In this case user wants name to be expanded *before* %define starts
4387 * working, so we'll expand %$abc into something (if it has a value;
4388 * otherwise it will be left as-is) then concatenate all successive
4389 * PP_IDs into one.
4391 static Token *expand_id(Token * tline)
4393 Token *cur, *oldnext = NULL;
4395 if (!tline || !tline->next)
4396 return tline;
4398 cur = tline;
4399 while (cur->next &&
4400 (cur->next->type == TOK_ID ||
4401 cur->next->type == TOK_PREPROC_ID
4402 || cur->next->type == TOK_NUMBER))
4403 cur = cur->next;
4405 /* If identifier consists of just one token, don't expand */
4406 if (cur == tline)
4407 return tline;
4409 if (cur) {
4410 oldnext = cur->next; /* Detach the tail past identifier */
4411 cur->next = NULL; /* so that expand_smacro stops here */
4414 tline = expand_smacro(tline);
4416 if (cur) {
4417 /* expand_smacro possibly changhed tline; re-scan for EOL */
4418 cur = tline;
4419 while (cur && cur->next)
4420 cur = cur->next;
4421 if (cur)
4422 cur->next = oldnext;
4425 return tline;
4429 * Determine whether the given line constitutes a multi-line macro
4430 * call, and return the MMacro structure called if so. Doesn't have
4431 * to check for an initial label - that's taken care of in
4432 * expand_mmacro - but must check numbers of parameters. Guaranteed
4433 * to be called with tline->type == TOK_ID, so the putative macro
4434 * name is easy to find.
4436 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4438 MMacro *head, *m;
4439 Token **params;
4440 int nparam;
4442 head = (MMacro *) hash_findix(&mmacros, tline->text);
4445 * Efficiency: first we see if any macro exists with the given
4446 * name. If not, we can return NULL immediately. _Then_ we
4447 * count the parameters, and then we look further along the
4448 * list if necessary to find the proper MMacro.
4450 list_for_each(m, head)
4451 if (!mstrcmp(m->name, tline->text, m->casesense))
4452 break;
4453 if (!m)
4454 return NULL;
4457 * OK, we have a potential macro. Count and demarcate the
4458 * parameters.
4460 count_mmac_params(tline->next, &nparam, &params);
4463 * So we know how many parameters we've got. Find the MMacro
4464 * structure that handles this number.
4466 while (m) {
4467 if (m->nparam_min <= nparam
4468 && (m->plus || nparam <= m->nparam_max)) {
4470 * This one is right. Just check if cycle removal
4471 * prohibits us using it before we actually celebrate...
4473 if (m->in_progress > m->max_depth) {
4474 if (m->max_depth > 0) {
4475 error(ERR_WARNING,
4476 "reached maximum recursion depth of %i",
4477 m->max_depth);
4479 nasm_free(params);
4480 return NULL;
4483 * It's right, and we can use it. Add its default
4484 * parameters to the end of our list if necessary.
4486 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4487 params =
4488 nasm_realloc(params,
4489 ((m->nparam_min + m->ndefs +
4490 1) * sizeof(*params)));
4491 while (nparam < m->nparam_min + m->ndefs) {
4492 params[nparam] = m->defaults[nparam - m->nparam_min];
4493 nparam++;
4497 * If we've gone over the maximum parameter count (and
4498 * we're in Plus mode), ignore parameters beyond
4499 * nparam_max.
4501 if (m->plus && nparam > m->nparam_max)
4502 nparam = m->nparam_max;
4504 * Then terminate the parameter list, and leave.
4506 if (!params) { /* need this special case */
4507 params = nasm_malloc(sizeof(*params));
4508 nparam = 0;
4510 params[nparam] = NULL;
4511 *params_array = params;
4512 return m;
4515 * This one wasn't right: look for the next one with the
4516 * same name.
4518 list_for_each(m, m->next)
4519 if (!mstrcmp(m->name, tline->text, m->casesense))
4520 break;
4524 * After all that, we didn't find one with the right number of
4525 * parameters. Issue a warning, and fail to expand the macro.
4527 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4528 "macro `%s' exists, but not taking %d parameters",
4529 tline->text, nparam);
4530 nasm_free(params);
4531 return NULL;
4536 * Save MMacro invocation specific fields in
4537 * preparation for a recursive macro expansion
4539 static void push_mmacro(MMacro *m)
4541 MMacroInvocation *i;
4543 i = nasm_malloc(sizeof(MMacroInvocation));
4544 i->prev = m->prev;
4545 i->params = m->params;
4546 i->iline = m->iline;
4547 i->nparam = m->nparam;
4548 i->rotate = m->rotate;
4549 i->paramlen = m->paramlen;
4550 i->unique = m->unique;
4551 i->condcnt = m->condcnt;
4552 m->prev = i;
4557 * Restore MMacro invocation specific fields that were
4558 * saved during a previous recursive macro expansion
4560 static void pop_mmacro(MMacro *m)
4562 MMacroInvocation *i;
4564 if (m->prev) {
4565 i = m->prev;
4566 m->prev = i->prev;
4567 m->params = i->params;
4568 m->iline = i->iline;
4569 m->nparam = i->nparam;
4570 m->rotate = i->rotate;
4571 m->paramlen = i->paramlen;
4572 m->unique = i->unique;
4573 m->condcnt = i->condcnt;
4574 nasm_free(i);
4580 * Expand the multi-line macro call made by the given line, if
4581 * there is one to be expanded. If there is, push the expansion on
4582 * istk->expansion and return 1. Otherwise return 0.
4584 static int expand_mmacro(Token * tline)
4586 Token *startline = tline;
4587 Token *label = NULL;
4588 int dont_prepend = 0;
4589 Token **params, *t, *tt;
4590 MMacro *m;
4591 Line *l, *ll;
4592 int i, nparam, *paramlen;
4593 const char *mname;
4595 t = tline;
4596 skip_white_(t);
4597 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4598 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4599 return 0;
4600 m = is_mmacro(t, &params);
4601 if (m) {
4602 mname = t->text;
4603 } else {
4604 Token *last;
4606 * We have an id which isn't a macro call. We'll assume
4607 * it might be a label; we'll also check to see if a
4608 * colon follows it. Then, if there's another id after
4609 * that lot, we'll check it again for macro-hood.
4611 label = last = t;
4612 t = t->next;
4613 if (tok_type_(t, TOK_WHITESPACE))
4614 last = t, t = t->next;
4615 if (tok_is_(t, ":")) {
4616 dont_prepend = 1;
4617 last = t, t = t->next;
4618 if (tok_type_(t, TOK_WHITESPACE))
4619 last = t, t = t->next;
4621 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4622 return 0;
4623 last->next = NULL;
4624 mname = t->text;
4625 tline = t;
4629 * Fix up the parameters: this involves stripping leading and
4630 * trailing whitespace, then stripping braces if they are
4631 * present.
4633 for (nparam = 0; params[nparam]; nparam++) ;
4634 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4636 for (i = 0; params[i]; i++) {
4637 int brace = false;
4638 int comma = (!m->plus || i < nparam - 1);
4640 t = params[i];
4641 skip_white_(t);
4642 if (tok_is_(t, "{"))
4643 t = t->next, brace = true, comma = false;
4644 params[i] = t;
4645 paramlen[i] = 0;
4646 while (t) {
4647 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4648 break; /* ... because we have hit a comma */
4649 if (comma && t->type == TOK_WHITESPACE
4650 && tok_is_(t->next, ","))
4651 break; /* ... or a space then a comma */
4652 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4653 break; /* ... or a brace */
4654 t = t->next;
4655 paramlen[i]++;
4660 * OK, we have a MMacro structure together with a set of
4661 * parameters. We must now go through the expansion and push
4662 * copies of each Line on to istk->expansion. Substitution of
4663 * parameter tokens and macro-local tokens doesn't get done
4664 * until the single-line macro substitution process; this is
4665 * because delaying them allows us to change the semantics
4666 * later through %rotate.
4668 * First, push an end marker on to istk->expansion, mark this
4669 * macro as in progress, and set up its invocation-specific
4670 * variables.
4672 ll = nasm_malloc(sizeof(Line));
4673 ll->next = istk->expansion;
4674 ll->finishes = m;
4675 ll->first = NULL;
4676 istk->expansion = ll;
4679 * Save the previous MMacro expansion in the case of
4680 * macro recursion
4682 if (m->max_depth && m->in_progress)
4683 push_mmacro(m);
4685 m->in_progress ++;
4686 m->params = params;
4687 m->iline = tline;
4688 m->nparam = nparam;
4689 m->rotate = 0;
4690 m->paramlen = paramlen;
4691 m->unique = unique++;
4692 m->lineno = 0;
4693 m->condcnt = 0;
4695 m->next_active = istk->mstk;
4696 istk->mstk = m;
4698 list_for_each(l, m->expansion) {
4699 Token **tail;
4701 ll = nasm_malloc(sizeof(Line));
4702 ll->finishes = NULL;
4703 ll->next = istk->expansion;
4704 istk->expansion = ll;
4705 tail = &ll->first;
4707 list_for_each(t, l->first) {
4708 Token *x = t;
4709 switch (t->type) {
4710 case TOK_PREPROC_Q:
4711 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4712 break;
4713 case TOK_PREPROC_QQ:
4714 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4715 break;
4716 case TOK_PREPROC_ID:
4717 if (t->text[1] == '0' && t->text[2] == '0') {
4718 dont_prepend = -1;
4719 x = label;
4720 if (!x)
4721 continue;
4723 /* fall through */
4724 default:
4725 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4726 break;
4728 tail = &tt->next;
4730 *tail = NULL;
4734 * If we had a label, push it on as the first line of
4735 * the macro expansion.
4737 if (label) {
4738 if (dont_prepend < 0)
4739 free_tlist(startline);
4740 else {
4741 ll = nasm_malloc(sizeof(Line));
4742 ll->finishes = NULL;
4743 ll->next = istk->expansion;
4744 istk->expansion = ll;
4745 ll->first = startline;
4746 if (!dont_prepend) {
4747 while (label->next)
4748 label = label->next;
4749 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4754 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4756 return 1;
4759 /* The function that actually does the error reporting */
4760 static void verror(int severity, const char *fmt, va_list arg)
4762 char buff[1024];
4763 MMacro *mmac = NULL;
4764 int delta = 0;
4766 vsnprintf(buff, sizeof(buff), fmt, arg);
4768 /* get %macro name */
4769 if (istk && istk->mstk) {
4770 mmac = istk->mstk;
4771 /* but %rep blocks should be skipped */
4772 while (mmac && !mmac->name)
4773 mmac = mmac->next_active, delta++;
4776 if (mmac)
4777 nasm_error(severity, "(%s:%d) %s",
4778 mmac->name, mmac->lineno - delta, buff);
4779 else
4780 nasm_error(severity, "%s", buff);
4784 * Since preprocessor always operate only on the line that didn't
4785 * arrived yet, we should always use ERR_OFFBY1.
4787 static void error(int severity, const char *fmt, ...)
4789 va_list arg;
4791 /* If we're in a dead branch of IF or something like it, ignore the error */
4792 if (istk && istk->conds && !emitting(istk->conds->state))
4793 return;
4795 va_start(arg, fmt);
4796 verror(severity, fmt, arg);
4797 va_end(arg);
4801 * Because %else etc are evaluated in the state context
4802 * of the previous branch, errors might get lost with error():
4803 * %if 0 ... %else trailing garbage ... %endif
4804 * So %else etc should report errors with this function.
4806 static void error_precond(int severity, const char *fmt, ...)
4808 va_list arg;
4810 /* Only ignore the error if it's really in a dead branch */
4811 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4812 return;
4814 va_start(arg, fmt);
4815 verror(severity, fmt, arg);
4816 va_end(arg);
4819 static void
4820 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4822 Token *t;
4824 cstk = NULL;
4825 istk = nasm_malloc(sizeof(Include));
4826 istk->next = NULL;
4827 istk->conds = NULL;
4828 istk->expansion = NULL;
4829 istk->mstk = NULL;
4830 istk->fp = fopen(file, "r");
4831 istk->fname = NULL;
4832 src_set_fname(nasm_strdup(file));
4833 src_set_linnum(0);
4834 istk->lineinc = 1;
4835 if (!istk->fp)
4836 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4837 file);
4838 defining = NULL;
4839 nested_mac_count = 0;
4840 nested_rep_count = 0;
4841 init_macros();
4842 unique = 0;
4843 if (tasm_compatible_mode) {
4844 stdmacpos = nasm_stdmac;
4845 } else {
4846 stdmacpos = nasm_stdmac_after_tasm;
4848 any_extrastdmac = extrastdmac && *extrastdmac;
4849 do_predef = true;
4850 list = listgen;
4853 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4854 * The caller, however, will also pass in 3 for preprocess-only so
4855 * we can set __PASS__ accordingly.
4857 pass = apass > 2 ? 2 : apass;
4859 dephead = deptail = deplist;
4860 if (deplist) {
4861 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4862 sl->next = NULL;
4863 strcpy(sl->str, file);
4864 *deptail = sl;
4865 deptail = &sl->next;
4869 * Define the __PASS__ macro. This is defined here unlike
4870 * all the other builtins, because it is special -- it varies between
4871 * passes.
4873 t = nasm_malloc(sizeof(*t));
4874 t->next = NULL;
4875 make_tok_num(t, apass);
4876 t->a.mac = NULL;
4877 define_smacro(NULL, "__PASS__", true, 0, t);
4880 static char *pp_getline(void)
4882 char *line;
4883 Token *tline;
4885 while (1) {
4887 * Fetch a tokenized line, either from the macro-expansion
4888 * buffer or from the input file.
4890 tline = NULL;
4891 while (istk->expansion && istk->expansion->finishes) {
4892 Line *l = istk->expansion;
4893 if (!l->finishes->name && l->finishes->in_progress > 1) {
4894 Line *ll;
4897 * This is a macro-end marker for a macro with no
4898 * name, which means it's not really a macro at all
4899 * but a %rep block, and the `in_progress' field is
4900 * more than 1, meaning that we still need to
4901 * repeat. (1 means the natural last repetition; 0
4902 * means termination by %exitrep.) We have
4903 * therefore expanded up to the %endrep, and must
4904 * push the whole block on to the expansion buffer
4905 * again. We don't bother to remove the macro-end
4906 * marker: we'd only have to generate another one
4907 * if we did.
4909 l->finishes->in_progress--;
4910 list_for_each(l, l->finishes->expansion) {
4911 Token *t, *tt, **tail;
4913 ll = nasm_malloc(sizeof(Line));
4914 ll->next = istk->expansion;
4915 ll->finishes = NULL;
4916 ll->first = NULL;
4917 tail = &ll->first;
4919 list_for_each(t, l->first) {
4920 if (t->text || t->type == TOK_WHITESPACE) {
4921 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4922 tail = &tt->next;
4926 istk->expansion = ll;
4928 } else {
4930 * Check whether a `%rep' was started and not ended
4931 * within this macro expansion. This can happen and
4932 * should be detected. It's a fatal error because
4933 * I'm too confused to work out how to recover
4934 * sensibly from it.
4936 if (defining) {
4937 if (defining->name)
4938 error(ERR_PANIC,
4939 "defining with name in expansion");
4940 else if (istk->mstk->name)
4941 error(ERR_FATAL,
4942 "`%%rep' without `%%endrep' within"
4943 " expansion of macro `%s'",
4944 istk->mstk->name);
4948 * FIXME: investigate the relationship at this point between
4949 * istk->mstk and l->finishes
4952 MMacro *m = istk->mstk;
4953 istk->mstk = m->next_active;
4954 if (m->name) {
4956 * This was a real macro call, not a %rep, and
4957 * therefore the parameter information needs to
4958 * be freed.
4960 if (m->prev) {
4961 pop_mmacro(m);
4962 l->finishes->in_progress --;
4963 } else {
4964 nasm_free(m->params);
4965 free_tlist(m->iline);
4966 nasm_free(m->paramlen);
4967 l->finishes->in_progress = 0;
4969 } else
4970 free_mmacro(m);
4972 istk->expansion = l->next;
4973 nasm_free(l);
4974 list->downlevel(LIST_MACRO);
4977 while (1) { /* until we get a line we can use */
4979 if (istk->expansion) { /* from a macro expansion */
4980 char *p;
4981 Line *l = istk->expansion;
4982 if (istk->mstk)
4983 istk->mstk->lineno++;
4984 tline = l->first;
4985 istk->expansion = l->next;
4986 nasm_free(l);
4987 p = detoken(tline, false);
4988 list->line(LIST_MACRO, p);
4989 nasm_free(p);
4990 break;
4992 line = read_line();
4993 if (line) { /* from the current input file */
4994 line = prepreproc(line);
4995 tline = tokenize(line);
4996 nasm_free(line);
4997 break;
5000 * The current file has ended; work down the istk
5003 Include *i = istk;
5004 fclose(i->fp);
5005 if (i->conds) {
5006 /* nasm_error can't be conditionally suppressed */
5007 nasm_error(ERR_FATAL,
5008 "expected `%%endif' before end of file");
5010 /* only set line and file name if there's a next node */
5011 if (i->next) {
5012 src_set_linnum(i->lineno);
5013 nasm_free(src_set_fname(nasm_strdup(i->fname)));
5015 istk = i->next;
5016 list->downlevel(LIST_INCLUDE);
5017 nasm_free(i);
5018 if (!istk)
5019 return NULL;
5020 if (istk->expansion && istk->expansion->finishes)
5021 break;
5026 * We must expand MMacro parameters and MMacro-local labels
5027 * _before_ we plunge into directive processing, to cope
5028 * with things like `%define something %1' such as STRUC
5029 * uses. Unless we're _defining_ a MMacro, in which case
5030 * those tokens should be left alone to go into the
5031 * definition; and unless we're in a non-emitting
5032 * condition, in which case we don't want to meddle with
5033 * anything.
5035 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5036 && !(istk->mstk && !istk->mstk->in_progress)) {
5037 tline = expand_mmac_params(tline);
5041 * Check the line to see if it's a preprocessor directive.
5043 if (do_directive(tline) == DIRECTIVE_FOUND) {
5044 continue;
5045 } else if (defining) {
5047 * We're defining a multi-line macro. We emit nothing
5048 * at all, and just
5049 * shove the tokenized line on to the macro definition.
5051 Line *l = nasm_malloc(sizeof(Line));
5052 l->next = defining->expansion;
5053 l->first = tline;
5054 l->finishes = NULL;
5055 defining->expansion = l;
5056 continue;
5057 } else if (istk->conds && !emitting(istk->conds->state)) {
5059 * We're in a non-emitting branch of a condition block.
5060 * Emit nothing at all, not even a blank line: when we
5061 * emerge from the condition we'll give a line-number
5062 * directive so we keep our place correctly.
5064 free_tlist(tline);
5065 continue;
5066 } else if (istk->mstk && !istk->mstk->in_progress) {
5068 * We're in a %rep block which has been terminated, so
5069 * we're walking through to the %endrep without
5070 * emitting anything. Emit nothing at all, not even a
5071 * blank line: when we emerge from the %rep block we'll
5072 * give a line-number directive so we keep our place
5073 * correctly.
5075 free_tlist(tline);
5076 continue;
5077 } else {
5078 tline = expand_smacro(tline);
5079 if (!expand_mmacro(tline)) {
5081 * De-tokenize the line again, and emit it.
5083 line = detoken(tline, true);
5084 free_tlist(tline);
5085 break;
5086 } else {
5087 continue; /* expand_mmacro calls free_tlist */
5092 return line;
5095 static void pp_cleanup(int pass)
5097 if (defining) {
5098 if (defining->name) {
5099 error(ERR_NONFATAL,
5100 "end of file while still defining macro `%s'",
5101 defining->name);
5102 } else {
5103 error(ERR_NONFATAL, "end of file while still in %%rep");
5106 free_mmacro(defining);
5107 defining = NULL;
5109 while (cstk)
5110 ctx_pop();
5111 free_macros();
5112 while (istk) {
5113 Include *i = istk;
5114 istk = istk->next;
5115 fclose(i->fp);
5116 nasm_free(i->fname);
5117 nasm_free(i);
5119 while (cstk)
5120 ctx_pop();
5121 nasm_free(src_set_fname(NULL));
5122 if (pass == 0) {
5123 IncPath *i;
5124 free_llist(predef);
5125 delete_Blocks();
5126 while ((i = ipath)) {
5127 ipath = i->next;
5128 if (i->path)
5129 nasm_free(i->path);
5130 nasm_free(i);
5135 void pp_include_path(char *path)
5137 IncPath *i;
5139 i = nasm_malloc(sizeof(IncPath));
5140 i->path = path ? nasm_strdup(path) : NULL;
5141 i->next = NULL;
5143 if (ipath) {
5144 IncPath *j = ipath;
5145 while (j->next)
5146 j = j->next;
5147 j->next = i;
5148 } else {
5149 ipath = i;
5153 void pp_pre_include(char *fname)
5155 Token *inc, *space, *name;
5156 Line *l;
5158 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5159 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5160 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5162 l = nasm_malloc(sizeof(Line));
5163 l->next = predef;
5164 l->first = inc;
5165 l->finishes = NULL;
5166 predef = l;
5169 void pp_pre_define(char *definition)
5171 Token *def, *space;
5172 Line *l;
5173 char *equals;
5175 equals = strchr(definition, '=');
5176 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5177 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5178 if (equals)
5179 *equals = ' ';
5180 space->next = tokenize(definition);
5181 if (equals)
5182 *equals = '=';
5184 l = nasm_malloc(sizeof(Line));
5185 l->next = predef;
5186 l->first = def;
5187 l->finishes = NULL;
5188 predef = l;
5191 void pp_pre_undefine(char *definition)
5193 Token *def, *space;
5194 Line *l;
5196 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5197 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5198 space->next = tokenize(definition);
5200 l = nasm_malloc(sizeof(Line));
5201 l->next = predef;
5202 l->first = def;
5203 l->finishes = NULL;
5204 predef = l;
5208 * Added by Keith Kanios:
5210 * This function is used to assist with "runtime" preprocessor
5211 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5213 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5214 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5217 void pp_runtime(char *definition)
5219 Token *def;
5221 def = tokenize(definition);
5222 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5223 free_tlist(def);
5227 void pp_extra_stdmac(macros_t *macros)
5229 extrastdmac = macros;
5232 static void make_tok_num(Token * tok, int64_t val)
5234 char numbuf[20];
5235 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5236 tok->text = nasm_strdup(numbuf);
5237 tok->type = TOK_NUMBER;
5240 struct preproc_ops nasmpp = {
5241 pp_reset,
5242 pp_getline,
5243 pp_cleanup