Correct /is4 encoding for EVEX instructions
[nasm.git] / asm / preproc.c
blob5f37b00b2371ff381b651ad3e41c313914891783
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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>
73 #include "nasm.h"
74 #include "nasmlib.h"
75 #include "preproc.h"
76 #include "hashtbl.h"
77 #include "quote.h"
78 #include "stdscan.h"
79 #include "eval.h"
80 #include "tokens.h"
81 #include "tables.h"
82 #include "listing.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... */
158 const char *fname; /* File where defined */
159 int32_t xline; /* First line in macro */
163 /* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
166 struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
178 * The context stack is composed of a linked list of these.
180 struct Context {
181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
195 * %define a(x,y) ( (x) & ~(y) )
197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
206 enum pp_token_type {
207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
210 TOK_INTERNAL_STRING,
211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
218 #define PP_CONCAT_MASK(x) (1 << (x))
219 #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
221 struct tokseq_match {
222 int mask_head;
223 int mask_tail;
226 struct Token {
227 Token *next;
228 char *text;
229 union {
230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
237 * Multi-line macro definitions are stored as a linked list of
238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
258 struct Line {
259 Line *next;
260 MMacro *finishes;
261 Token *first;
265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
268 struct Include {
269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
273 const char *fname;
274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
279 * Include search path. This is simply a list of strings which get
280 * prepended, in turn, to the name of an include file, in an
281 * attempt to find the file if it's not in the current directory.
283 struct IncPath {
284 IncPath *next;
285 char *path;
289 * Conditional assembly: we maintain a separate stack of these for
290 * each level of file inclusion. (The only reason we keep the
291 * stacks separate is to ensure that a stray `%endif' in a file
292 * included from within the true branch of a `%if' won't terminate
293 * it and cause confusion: instead, rightly, it'll cause an error.)
295 struct Cond {
296 Cond *next;
297 int state;
299 enum {
301 * These states are for use just after %if or %elif: IF_TRUE
302 * means the condition has evaluated to truth so we are
303 * currently emitting, whereas IF_FALSE means we are not
304 * currently emitting but will start doing so if a %else comes
305 * up. In these states, all directives are admissible: %elif,
306 * %else and %endif. (And of course %if.)
308 COND_IF_TRUE, COND_IF_FALSE,
310 * These states come up after a %else: ELSE_TRUE means we're
311 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
312 * any %elif or %else will cause an error.
314 COND_ELSE_TRUE, COND_ELSE_FALSE,
316 * These states mean that we're not emitting now, and also that
317 * nothing until %endif will be emitted at all. COND_DONE is
318 * used when we've had our moment of emission
319 * and have now started seeing %elifs. COND_NEVER is used when
320 * the condition construct in question is contained within a
321 * non-emitting branch of a larger condition construct,
322 * or if there is an error.
324 COND_DONE, COND_NEVER
326 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
329 * These defines are used as the possible return values for do_directive
331 #define NO_DIRECTIVE_FOUND 0
332 #define DIRECTIVE_FOUND 1
335 * This define sets the upper limit for smacro and recursive mmacro
336 * expansions
338 #define DEADMAN_LIMIT (1 << 20)
340 /* max reps */
341 #define REP_LIMIT ((INT64_C(1) << 62))
344 * Condition codes. Note that we use c_ prefix not C_ because C_ is
345 * used in nasm.h for the "real" condition codes. At _this_ level,
346 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
347 * ones, so we need a different enum...
349 static const char * const conditions[] = {
350 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
351 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
352 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
354 enum pp_conds {
355 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
356 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
357 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
358 c_none = -1
360 static const enum pp_conds inverse_ccs[] = {
361 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
362 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,
363 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
367 * Directive names.
369 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
370 static int is_condition(enum preproc_token arg)
372 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
375 /* For TASM compatibility we need to be able to recognise TASM compatible
376 * conditional compilation directives. Using the NASM pre-processor does
377 * not work, so we look for them specifically from the following list and
378 * then jam in the equivalent NASM directive into the input stream.
381 enum {
382 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
383 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
386 static const char * const tasm_directives[] = {
387 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
388 "ifndef", "include", "local"
391 static int StackSize = 4;
392 static const char *StackPointer = "ebp";
393 static int ArgOffset = 8;
394 static int LocalOffset = 0;
396 static Context *cstk;
397 static Include *istk;
398 static IncPath *ipath = NULL;
400 static int pass; /* HACK: pass 0 = generate dependencies only */
401 static StrList **dephead, **deptail; /* Dependency list */
403 static uint64_t unique; /* unique identifier numbers */
405 static Line *predef = NULL;
406 static bool do_predef;
409 * The current set of multi-line macros we have defined.
411 static struct hash_table mmacros;
414 * The current set of single-line macros we have defined.
416 static struct hash_table smacros;
419 * The multi-line macro we are currently defining, or the %rep
420 * block we are currently reading, if any.
422 static MMacro *defining;
424 static uint64_t nested_mac_count;
425 static uint64_t nested_rep_count;
428 * The number of macro parameters to allocate space for at a time.
430 #define PARAM_DELTA 16
433 * The standard macro set: defined in macros.c in a set of arrays.
434 * This gives our position in any macro set, while we are processing it.
435 * The stdmacset is an array of such macro sets.
437 static macros_t *stdmacpos;
438 static macros_t **stdmacnext;
439 static macros_t *stdmacros[8];
442 * Tokens are allocated in blocks to improve speed
444 #define TOKEN_BLOCKSIZE 4096
445 static Token *freeTokens = NULL;
446 struct Blocks {
447 Blocks *next;
448 void *chunk;
451 static Blocks blocks = { NULL, NULL };
454 * Forward declarations.
456 static void pp_add_stdmac(macros_t *macros);
457 static Token *expand_mmac_params(Token * tline);
458 static Token *expand_smacro(Token * tline);
459 static Token *expand_id(Token * tline);
460 static Context *get_ctx(const char *name, const char **namep);
461 static void make_tok_num(Token * tok, int64_t val);
462 static void pp_verror(int severity, const char *fmt, va_list ap);
463 static vefunc real_verror;
464 static void *new_Block(size_t size);
465 static void delete_Blocks(void);
466 static Token *new_Token(Token * next, enum pp_token_type type,
467 const char *text, int txtlen);
468 static Token *delete_Token(Token * t);
471 * Macros for safe checking of token pointers, avoid *(NULL)
473 #define tok_type_(x,t) ((x) && (x)->type == (t))
474 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
475 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
476 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
479 * nasm_unquote with error if the string contains NUL characters.
480 * If the string contains NUL characters, issue an error and return
481 * the C len, i.e. truncate at the NUL.
483 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
485 size_t len = nasm_unquote(qstr, NULL);
486 size_t clen = strlen(qstr);
488 if (len != clen)
489 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
490 pp_directives[directive]);
492 return clen;
496 * In-place reverse a list of tokens.
498 static Token *reverse_tokens(Token *t)
500 Token *prev = NULL;
501 Token *next;
503 while (t) {
504 next = t->next;
505 t->next = prev;
506 prev = t;
507 t = next;
510 return prev;
514 * Handle TASM specific directives, which do not contain a % in
515 * front of them. We do it here because I could not find any other
516 * place to do it for the moment, and it is a hack (ideally it would
517 * be nice to be able to use the NASM pre-processor to do it).
519 static char *check_tasm_directive(char *line)
521 int32_t i, j, k, m, len;
522 char *p, *q, *oldline, oldchar;
524 p = nasm_skip_spaces(line);
526 /* Binary search for the directive name */
527 i = -1;
528 j = ARRAY_SIZE(tasm_directives);
529 q = nasm_skip_word(p);
530 len = q - p;
531 if (len) {
532 oldchar = p[len];
533 p[len] = 0;
534 while (j - i > 1) {
535 k = (j + i) / 2;
536 m = nasm_stricmp(p, tasm_directives[k]);
537 if (m == 0) {
538 /* We have found a directive, so jam a % in front of it
539 * so that NASM will then recognise it as one if it's own.
541 p[len] = oldchar;
542 len = strlen(p);
543 oldline = line;
544 line = nasm_malloc(len + 2);
545 line[0] = '%';
546 if (k == TM_IFDIFI) {
548 * NASM does not recognise IFDIFI, so we convert
549 * it to %if 0. This is not used in NASM
550 * compatible code, but does need to parse for the
551 * TASM macro package.
553 strcpy(line + 1, "if 0");
554 } else {
555 memcpy(line + 1, p, len + 1);
557 nasm_free(oldline);
558 return line;
559 } else if (m < 0) {
560 j = k;
561 } else
562 i = k;
564 p[len] = oldchar;
566 return line;
570 * The pre-preprocessing stage... This function translates line
571 * number indications as they emerge from GNU cpp (`# lineno "file"
572 * flags') into NASM preprocessor line number indications (`%line
573 * lineno file').
575 static char *prepreproc(char *line)
577 int lineno, fnlen;
578 char *fname, *oldline;
580 if (line[0] == '#' && line[1] == ' ') {
581 oldline = line;
582 fname = oldline + 2;
583 lineno = atoi(fname);
584 fname += strspn(fname, "0123456789 ");
585 if (*fname == '"')
586 fname++;
587 fnlen = strcspn(fname, "\"");
588 line = nasm_malloc(20 + fnlen);
589 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
590 nasm_free(oldline);
592 if (tasm_compatible_mode)
593 return check_tasm_directive(line);
594 return line;
598 * Free a linked list of tokens.
600 static void free_tlist(Token * list)
602 while (list)
603 list = delete_Token(list);
607 * Free a linked list of lines.
609 static void free_llist(Line * list)
611 Line *l, *tmp;
612 list_for_each_safe(l, tmp, list) {
613 free_tlist(l->first);
614 nasm_free(l);
619 * Free an MMacro
621 static void free_mmacro(MMacro * m)
623 nasm_free(m->name);
624 free_tlist(m->dlist);
625 nasm_free(m->defaults);
626 free_llist(m->expansion);
627 nasm_free(m);
631 * Free all currently defined macros, and free the hash tables
633 static void free_smacro_table(struct hash_table *smt)
635 SMacro *s, *tmp;
636 const char *key;
637 struct hash_tbl_node *it = NULL;
639 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
640 nasm_free((void *)key);
641 list_for_each_safe(s, tmp, s) {
642 nasm_free(s->name);
643 free_tlist(s->expansion);
644 nasm_free(s);
647 hash_free(smt);
650 static void free_mmacro_table(struct hash_table *mmt)
652 MMacro *m, *tmp;
653 const char *key;
654 struct hash_tbl_node *it = NULL;
656 it = NULL;
657 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
658 nasm_free((void *)key);
659 list_for_each_safe(m ,tmp, m)
660 free_mmacro(m);
662 hash_free(mmt);
665 static void free_macros(void)
667 free_smacro_table(&smacros);
668 free_mmacro_table(&mmacros);
672 * Initialize the hash tables
674 static void init_macros(void)
676 hash_init(&smacros, HASH_LARGE);
677 hash_init(&mmacros, HASH_LARGE);
681 * Pop the context stack.
683 static void ctx_pop(void)
685 Context *c = cstk;
687 cstk = cstk->next;
688 free_smacro_table(&c->localmac);
689 nasm_free(c->name);
690 nasm_free(c);
694 * Search for a key in the hash index; adding it if necessary
695 * (in which case we initialize the data pointer to NULL.)
697 static void **
698 hash_findi_add(struct hash_table *hash, const char *str)
700 struct hash_insert hi;
701 void **r;
702 char *strx;
704 r = hash_findi(hash, str, &hi);
705 if (r)
706 return r;
708 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
709 return hash_add(&hi, strx, NULL);
713 * Like hash_findi, but returns the data element rather than a pointer
714 * to it. Used only when not adding a new element, hence no third
715 * argument.
717 static void *
718 hash_findix(struct hash_table *hash, const char *str)
720 void **p;
722 p = hash_findi(hash, str, NULL);
723 return p ? *p : NULL;
727 * read line from standart macros set,
728 * if there no more left -- return NULL
730 static char *line_from_stdmac(void)
732 unsigned char c;
733 const unsigned char *p = stdmacpos;
734 char *line, *q;
735 size_t len = 0;
737 if (!stdmacpos)
738 return NULL;
740 while ((c = *p++)) {
741 if (c >= 0x80)
742 len += pp_directives_len[c - 0x80] + 1;
743 else
744 len++;
747 line = nasm_malloc(len + 1);
748 q = line;
749 while ((c = *stdmacpos++)) {
750 if (c >= 0x80) {
751 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
752 q += pp_directives_len[c - 0x80];
753 *q++ = ' ';
754 } else {
755 *q++ = c;
758 stdmacpos = p;
759 *q = '\0';
761 if (!*stdmacpos) {
762 /* This was the last of this particular macro set */
763 stdmacpos = NULL;
764 if (*stdmacnext) {
765 stdmacpos = *stdmacnext++;
766 } else if (do_predef) {
767 Line *pd, *l;
768 Token *head, **tail, *t;
771 * Nasty hack: here we push the contents of
772 * `predef' on to the top-level expansion stack,
773 * since this is the most convenient way to
774 * implement the pre-include and pre-define
775 * features.
777 list_for_each(pd, predef) {
778 head = NULL;
779 tail = &head;
780 list_for_each(t, pd->first) {
781 *tail = new_Token(NULL, t->type, t->text, 0);
782 tail = &(*tail)->next;
785 l = nasm_malloc(sizeof(Line));
786 l->next = istk->expansion;
787 l->first = head;
788 l->finishes = NULL;
790 istk->expansion = l;
792 do_predef = false;
796 return line;
799 static char *read_line(void)
801 unsigned int size, c, next;
802 const unsigned int delta = 512;
803 const unsigned int pad = 8;
804 unsigned int nr_cont = 0;
805 bool cont = false;
806 char *buffer, *p;
808 /* Standart macros set (predefined) goes first */
809 p = line_from_stdmac();
810 if (p)
811 return p;
813 size = delta;
814 p = buffer = nasm_malloc(size);
816 for (;;) {
817 c = fgetc(istk->fp);
818 if ((int)(c) == EOF) {
819 p[0] = 0;
820 break;
823 switch (c) {
824 case '\r':
825 next = fgetc(istk->fp);
826 if (next != '\n')
827 ungetc(next, istk->fp);
828 if (cont) {
829 cont = false;
830 continue;
832 break;
834 case '\n':
835 if (cont) {
836 cont = false;
837 continue;
839 break;
841 case '\\':
842 next = fgetc(istk->fp);
843 ungetc(next, istk->fp);
844 if (next == '\r' || next == '\n') {
845 cont = true;
846 nr_cont++;
847 continue;
849 break;
852 if (c == '\r' || c == '\n') {
853 *p++ = 0;
854 break;
857 if (p >= (buffer + size - pad)) {
858 buffer = nasm_realloc(buffer, size + delta);
859 p = buffer + size - pad;
860 size += delta;
863 *p++ = (unsigned char)c;
866 if (p == buffer) {
867 nasm_free(buffer);
868 return NULL;
871 src_set_linnum(src_get_linnum() + istk->lineinc +
872 (nr_cont * istk->lineinc));
875 * Handle spurious ^Z, which may be inserted into source files
876 * by some file transfer utilities.
878 buffer[strcspn(buffer, "\032")] = '\0';
880 lfmt->line(LIST_READ, buffer);
882 return buffer;
886 * Tokenize a line of text. This is a very simple process since we
887 * don't need to parse the value out of e.g. numeric tokens: we
888 * simply split one string into many.
890 static Token *tokenize(char *line)
892 char c, *p = line;
893 enum pp_token_type type;
894 Token *list = NULL;
895 Token *t, **tail = &list;
897 while (*line) {
898 p = line;
899 if (*p == '%') {
900 p++;
901 if (*p == '+' && !nasm_isdigit(p[1])) {
902 p++;
903 type = TOK_PASTE;
904 } else if (nasm_isdigit(*p) ||
905 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
906 do {
907 p++;
909 while (nasm_isdigit(*p));
910 type = TOK_PREPROC_ID;
911 } else if (*p == '{') {
912 p++;
913 while (*p) {
914 if (*p == '}')
915 break;
916 p[-1] = *p;
917 p++;
919 if (*p != '}')
920 nasm_error(ERR_WARNING | ERR_PASS1,
921 "unterminated %%{ construct");
922 p[-1] = '\0';
923 if (*p)
924 p++;
925 type = TOK_PREPROC_ID;
926 } else if (*p == '[') {
927 int lvl = 1;
928 line += 2; /* Skip the leading %[ */
929 p++;
930 while (lvl && (c = *p++)) {
931 switch (c) {
932 case ']':
933 lvl--;
934 break;
935 case '%':
936 if (*p == '[')
937 lvl++;
938 break;
939 case '\'':
940 case '\"':
941 case '`':
942 p = nasm_skip_string(p - 1) + 1;
943 break;
944 default:
945 break;
948 p--;
949 if (*p)
950 *p++ = '\0';
951 if (lvl)
952 nasm_error(ERR_NONFATAL|ERR_PASS1,
953 "unterminated %%[ construct");
954 type = TOK_INDIRECT;
955 } else if (*p == '?') {
956 type = TOK_PREPROC_Q; /* %? */
957 p++;
958 if (*p == '?') {
959 type = TOK_PREPROC_QQ; /* %?? */
960 p++;
962 } else if (*p == '!') {
963 type = TOK_PREPROC_ID;
964 p++;
965 if (isidchar(*p)) {
966 do {
967 p++;
969 while (isidchar(*p));
970 } else if (*p == '\'' || *p == '\"' || *p == '`') {
971 p = nasm_skip_string(p);
972 if (*p)
973 p++;
974 else
975 nasm_error(ERR_NONFATAL|ERR_PASS1,
976 "unterminated %%! string");
977 } else {
978 /* %! without string or identifier */
979 type = TOK_OTHER; /* Legacy behavior... */
981 } else if (isidchar(*p) ||
982 ((*p == '!' || *p == '%' || *p == '$') &&
983 isidchar(p[1]))) {
984 do {
985 p++;
987 while (isidchar(*p));
988 type = TOK_PREPROC_ID;
989 } else {
990 type = TOK_OTHER;
991 if (*p == '%')
992 p++;
994 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
995 type = TOK_ID;
996 p++;
997 while (*p && isidchar(*p))
998 p++;
999 } else if (*p == '\'' || *p == '"' || *p == '`') {
1001 * A string token.
1003 type = TOK_STRING;
1004 p = nasm_skip_string(p);
1006 if (*p) {
1007 p++;
1008 } else {
1009 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
1010 /* Handling unterminated strings by UNV */
1011 /* type = -1; */
1013 } else if (p[0] == '$' && p[1] == '$') {
1014 type = TOK_OTHER; /* TOKEN_BASE */
1015 p += 2;
1016 } else if (isnumstart(*p)) {
1017 bool is_hex = false;
1018 bool is_float = false;
1019 bool has_e = false;
1020 char c, *r;
1023 * A numeric token.
1026 if (*p == '$') {
1027 p++;
1028 is_hex = true;
1031 for (;;) {
1032 c = *p++;
1034 if (!is_hex && (c == 'e' || c == 'E')) {
1035 has_e = true;
1036 if (*p == '+' || *p == '-') {
1038 * e can only be followed by +/- if it is either a
1039 * prefixed hex number or a floating-point number
1041 p++;
1042 is_float = true;
1044 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1045 is_hex = true;
1046 } else if (c == 'P' || c == 'p') {
1047 is_float = true;
1048 if (*p == '+' || *p == '-')
1049 p++;
1050 } else if (isnumchar(c) || c == '_')
1051 ; /* just advance */
1052 else if (c == '.') {
1054 * we need to deal with consequences of the legacy
1055 * parser, like "1.nolist" being two tokens
1056 * (TOK_NUMBER, TOK_ID) here; at least give it
1057 * a shot for now. In the future, we probably need
1058 * a flex-based scanner with proper pattern matching
1059 * to do it as well as it can be done. Nothing in
1060 * the world is going to help the person who wants
1061 * 0x123.p16 interpreted as two tokens, though.
1063 r = p;
1064 while (*r == '_')
1065 r++;
1067 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1068 (!is_hex && (*r == 'e' || *r == 'E')) ||
1069 (*r == 'p' || *r == 'P')) {
1070 p = r;
1071 is_float = true;
1072 } else
1073 break; /* Terminate the token */
1074 } else
1075 break;
1077 p--; /* Point to first character beyond number */
1079 if (p == line+1 && *line == '$') {
1080 type = TOK_OTHER; /* TOKEN_HERE */
1081 } else {
1082 if (has_e && !is_hex) {
1083 /* 1e13 is floating-point, but 1e13h is not */
1084 is_float = true;
1087 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1089 } else if (nasm_isspace(*p)) {
1090 type = TOK_WHITESPACE;
1091 p = nasm_skip_spaces(p);
1093 * Whitespace just before end-of-line is discarded by
1094 * pretending it's a comment; whitespace just before a
1095 * comment gets lumped into the comment.
1097 if (!*p || *p == ';') {
1098 type = TOK_COMMENT;
1099 while (*p)
1100 p++;
1102 } else if (*p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1106 } else {
1108 * Anything else is an operator of some kind. We check
1109 * for all the double-character operators (>>, <<, //,
1110 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1111 * else is a single-character operator.
1113 type = TOK_OTHER;
1114 if ((p[0] == '>' && p[1] == '>') ||
1115 (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++;
1127 p++;
1130 /* Handling unterminated string by UNV */
1131 /*if (type == -1)
1133 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1134 t->text[p-line] = *line;
1135 tail = &t->next;
1137 else */
1138 if (type != TOK_COMMENT) {
1139 *tail = t = new_Token(NULL, type, line, p - line);
1140 tail = &t->next;
1142 line = p;
1144 return list;
1148 * this function allocates a new managed block of memory and
1149 * returns a pointer to the block. The managed blocks are
1150 * deleted only all at once by the delete_Blocks function.
1152 static void *new_Block(size_t size)
1154 Blocks *b = &blocks;
1156 /* first, get to the end of the linked list */
1157 while (b->next)
1158 b = b->next;
1159 /* now allocate the requested chunk */
1160 b->chunk = nasm_malloc(size);
1162 /* now allocate a new block for the next request */
1163 b->next = nasm_zalloc(sizeof(Blocks));
1164 return b->chunk;
1168 * this function deletes all managed blocks of memory
1170 static void delete_Blocks(void)
1172 Blocks *a, *b = &blocks;
1175 * keep in mind that the first block, pointed to by blocks
1176 * is a static and not dynamically allocated, so we don't
1177 * free it.
1179 while (b) {
1180 if (b->chunk)
1181 nasm_free(b->chunk);
1182 a = b;
1183 b = b->next;
1184 if (a != &blocks)
1185 nasm_free(a);
1187 memset(&blocks, 0, sizeof(blocks));
1191 * this function creates a new Token and passes a pointer to it
1192 * back to the caller. It sets the type and text elements, and
1193 * also the a.mac and next elements to NULL.
1195 static Token *new_Token(Token * next, enum pp_token_type type,
1196 const char *text, int txtlen)
1198 Token *t;
1199 int i;
1201 if (!freeTokens) {
1202 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1203 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1204 freeTokens[i].next = &freeTokens[i + 1];
1205 freeTokens[i].next = NULL;
1207 t = freeTokens;
1208 freeTokens = t->next;
1209 t->next = next;
1210 t->a.mac = NULL;
1211 t->type = type;
1212 if (type == TOK_WHITESPACE || !text) {
1213 t->text = NULL;
1214 } else {
1215 if (txtlen == 0)
1216 txtlen = strlen(text);
1217 t->text = nasm_malloc(txtlen+1);
1218 memcpy(t->text, text, txtlen);
1219 t->text[txtlen] = '\0';
1221 return t;
1224 static Token *delete_Token(Token * t)
1226 Token *next = t->next;
1227 nasm_free(t->text);
1228 t->next = freeTokens;
1229 freeTokens = t;
1230 return next;
1234 * Convert a line of tokens back into text.
1235 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1236 * will be transformed into ..@ctxnum.xxx
1238 static char *detoken(Token * tlist, bool expand_locals)
1240 Token *t;
1241 char *line, *p;
1242 const char *q;
1243 int len = 0;
1245 list_for_each(t, tlist) {
1246 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1247 char *v;
1248 char *q = t->text;
1250 v = t->text + 2;
1251 if (*v == '\'' || *v == '\"' || *v == '`') {
1252 size_t len = nasm_unquote(v, NULL);
1253 size_t clen = strlen(v);
1255 if (len != clen) {
1256 nasm_error(ERR_NONFATAL | ERR_PASS1,
1257 "NUL character in %%! string");
1258 v = NULL;
1262 if (v) {
1263 char *p = getenv(v);
1264 if (!p) {
1265 nasm_error(ERR_NONFATAL | ERR_PASS1,
1266 "nonexistent environment variable `%s'", v);
1268 * FIXME We better should investigate if accessing
1269 * ->text[1] without ->text[0] is safe enough.
1271 t->text = nasm_zalloc(2);
1272 } else
1273 t->text = nasm_strdup(p);
1275 nasm_free(q);
1278 /* Expand local macros here and not during preprocessing */
1279 if (expand_locals &&
1280 t->type == TOK_PREPROC_ID && t->text &&
1281 t->text[0] == '%' && t->text[1] == '$') {
1282 const char *q;
1283 char *p;
1284 Context *ctx = get_ctx(t->text, &q);
1285 if (ctx) {
1286 char buffer[40];
1287 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1288 p = nasm_strcat(buffer, q);
1289 nasm_free(t->text);
1290 t->text = p;
1293 if (t->type == TOK_WHITESPACE)
1294 len++;
1295 else if (t->text)
1296 len += strlen(t->text);
1299 p = line = nasm_malloc(len + 1);
1301 list_for_each(t, tlist) {
1302 if (t->type == TOK_WHITESPACE) {
1303 *p++ = ' ';
1304 } else if (t->text) {
1305 q = t->text;
1306 while (*q)
1307 *p++ = *q++;
1310 *p = '\0';
1312 return line;
1316 * A scanner, suitable for use by the expression evaluator, which
1317 * operates on a line of Tokens. Expects a pointer to a pointer to
1318 * the first token in the line to be passed in as its private_data
1319 * field.
1321 * FIX: This really needs to be unified with stdscan.
1323 static int ppscan(void *private_data, struct tokenval *tokval)
1325 Token **tlineptr = private_data;
1326 Token *tline;
1327 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1329 do {
1330 tline = *tlineptr;
1331 *tlineptr = tline ? tline->next : NULL;
1332 } while (tline && (tline->type == TOK_WHITESPACE ||
1333 tline->type == TOK_COMMENT));
1335 if (!tline)
1336 return tokval->t_type = TOKEN_EOS;
1338 tokval->t_charptr = tline->text;
1340 if (tline->text[0] == '$' && !tline->text[1])
1341 return tokval->t_type = TOKEN_HERE;
1342 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1343 return tokval->t_type = TOKEN_BASE;
1345 if (tline->type == TOK_ID) {
1346 p = tokval->t_charptr = tline->text;
1347 if (p[0] == '$') {
1348 tokval->t_charptr++;
1349 return tokval->t_type = TOKEN_ID;
1352 for (r = p, s = ourcopy; *r; r++) {
1353 if (r >= p+MAX_KEYWORD)
1354 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1355 *s++ = nasm_tolower(*r);
1357 *s = '\0';
1358 /* right, so we have an identifier sitting in temp storage. now,
1359 * is it actually a register or instruction name, or what? */
1360 return nasm_token_hash(ourcopy, tokval);
1363 if (tline->type == TOK_NUMBER) {
1364 bool rn_error;
1365 tokval->t_integer = readnum(tline->text, &rn_error);
1366 tokval->t_charptr = tline->text;
1367 if (rn_error)
1368 return tokval->t_type = TOKEN_ERRNUM;
1369 else
1370 return tokval->t_type = TOKEN_NUM;
1373 if (tline->type == TOK_FLOAT) {
1374 return tokval->t_type = TOKEN_FLOAT;
1377 if (tline->type == TOK_STRING) {
1378 char bq, *ep;
1380 bq = tline->text[0];
1381 tokval->t_charptr = tline->text;
1382 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1384 if (ep[0] != bq || ep[1] != '\0')
1385 return tokval->t_type = TOKEN_ERRSTR;
1386 else
1387 return tokval->t_type = TOKEN_STR;
1390 if (tline->type == TOK_OTHER) {
1391 if (!strcmp(tline->text, "<<"))
1392 return tokval->t_type = TOKEN_SHL;
1393 if (!strcmp(tline->text, ">>"))
1394 return tokval->t_type = TOKEN_SHR;
1395 if (!strcmp(tline->text, "//"))
1396 return tokval->t_type = TOKEN_SDIV;
1397 if (!strcmp(tline->text, "%%"))
1398 return tokval->t_type = TOKEN_SMOD;
1399 if (!strcmp(tline->text, "=="))
1400 return tokval->t_type = TOKEN_EQ;
1401 if (!strcmp(tline->text, "<>"))
1402 return tokval->t_type = TOKEN_NE;
1403 if (!strcmp(tline->text, "!="))
1404 return tokval->t_type = TOKEN_NE;
1405 if (!strcmp(tline->text, "<="))
1406 return tokval->t_type = TOKEN_LE;
1407 if (!strcmp(tline->text, ">="))
1408 return tokval->t_type = TOKEN_GE;
1409 if (!strcmp(tline->text, "&&"))
1410 return tokval->t_type = TOKEN_DBL_AND;
1411 if (!strcmp(tline->text, "^^"))
1412 return tokval->t_type = TOKEN_DBL_XOR;
1413 if (!strcmp(tline->text, "||"))
1414 return tokval->t_type = TOKEN_DBL_OR;
1418 * We have no other options: just return the first character of
1419 * the token text.
1421 return tokval->t_type = tline->text[0];
1425 * Compare a string to the name of an existing macro; this is a
1426 * simple wrapper which calls either strcmp or nasm_stricmp
1427 * depending on the value of the `casesense' parameter.
1429 static int mstrcmp(const char *p, const char *q, bool casesense)
1431 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1435 * Compare a string to the name of an existing macro; this is a
1436 * simple wrapper which calls either strcmp or nasm_stricmp
1437 * depending on the value of the `casesense' parameter.
1439 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1441 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1445 * Return the Context structure associated with a %$ token. Return
1446 * NULL, having _already_ reported an error condition, if the
1447 * context stack isn't deep enough for the supplied number of $
1448 * signs.
1450 * If "namep" is non-NULL, set it to the pointer to the macro name
1451 * tail, i.e. the part beyond %$...
1453 static Context *get_ctx(const char *name, const char **namep)
1455 Context *ctx;
1456 int i;
1458 if (namep)
1459 *namep = name;
1461 if (!name || name[0] != '%' || name[1] != '$')
1462 return NULL;
1464 if (!cstk) {
1465 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
1466 return NULL;
1469 name += 2;
1470 ctx = cstk;
1471 i = 0;
1472 while (ctx && *name == '$') {
1473 name++;
1474 i++;
1475 ctx = ctx->next;
1477 if (!ctx) {
1478 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
1479 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1480 return NULL;
1483 if (namep)
1484 *namep = name;
1486 return ctx;
1490 * Check to see if a file is already in a string list
1492 static bool in_list(const StrList *list, const char *str)
1494 while (list) {
1495 if (!strcmp(list->str, str))
1496 return true;
1497 list = list->next;
1499 return false;
1503 * Open an include file. This routine must always return a valid
1504 * file pointer if it returns - it's responsible for throwing an
1505 * ERR_FATAL and bombing out completely if not. It should also try
1506 * the include path one by one until it finds the file or reaches
1507 * the end of the path.
1509 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1510 char **found_path, bool missing_ok, enum file_flags mode)
1512 FILE *fp;
1513 char *prefix = "";
1514 IncPath *ip = ipath;
1515 int len = strlen(file);
1516 size_t prefix_len = 0;
1517 StrList *sl;
1518 size_t path_len;
1520 while (1) {
1521 path_len = prefix_len + len + 1;
1523 sl = nasm_malloc(path_len + sizeof sl->next);
1524 memcpy(sl->str, prefix, prefix_len);
1525 memcpy(sl->str+prefix_len, file, len+1);
1527 if (found_path != NULL) {
1528 *found_path = nasm_malloc(path_len);
1529 memcpy(*found_path, sl->str, path_len);
1532 fp = nasm_open_read(sl->str, mode);
1533 if (fp && dhead && !in_list(*dhead, sl->str)) {
1534 sl->next = NULL;
1535 **dtail = sl;
1536 *dtail = &sl->next;
1537 } else {
1538 nasm_free(sl);
1540 if (fp)
1541 return fp;
1543 if (found_path != NULL && *found_path != NULL) {
1544 nasm_free(*found_path);
1545 *found_path = NULL;
1548 if (!ip) {
1549 if (!missing_ok)
1550 break;
1551 prefix = NULL;
1552 } else {
1553 prefix = ip->path;
1554 ip = ip->next;
1556 if (prefix) {
1557 prefix_len = strlen(prefix);
1558 } else {
1559 /* -MG given and file not found */
1560 if (dhead && !in_list(*dhead, file)) {
1561 sl = nasm_malloc(len+1+sizeof sl->next);
1562 sl->next = NULL;
1563 strcpy(sl->str, file);
1564 **dtail = sl;
1565 *dtail = &sl->next;
1567 return NULL;
1571 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
1572 return NULL;
1576 * Opens an include or input file. Public version, for use by modules
1577 * that get a file:lineno pair and need to look at the file again
1578 * (e.g. the CodeView debug backend). Returns NULL on failure.
1580 FILE *pp_input_fopen(const char *filename, enum file_flags mode)
1582 FILE *fp;
1583 StrList *xsl = NULL;
1584 StrList **xst = &xsl;
1586 fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode);
1587 if (xsl)
1588 nasm_free(xsl);
1589 return fp;
1593 * Determine if we should warn on defining a single-line macro of
1594 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1595 * return true if _any_ single-line macro of that name is defined.
1596 * Otherwise, will return true if a single-line macro with either
1597 * `nparam' or no parameters is defined.
1599 * If a macro with precisely the right number of parameters is
1600 * defined, or nparam is -1, the address of the definition structure
1601 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1602 * is NULL, no action will be taken regarding its contents, and no
1603 * error will occur.
1605 * Note that this is also called with nparam zero to resolve
1606 * `ifdef'.
1608 * If you already know which context macro belongs to, you can pass
1609 * the context pointer as first parameter; if you won't but name begins
1610 * with %$ the context will be automatically computed. If all_contexts
1611 * is true, macro will be searched in outer contexts as well.
1613 static bool
1614 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1615 bool nocase)
1617 struct hash_table *smtbl;
1618 SMacro *m;
1620 if (ctx) {
1621 smtbl = &ctx->localmac;
1622 } else if (name[0] == '%' && name[1] == '$') {
1623 if (cstk)
1624 ctx = get_ctx(name, &name);
1625 if (!ctx)
1626 return false; /* got to return _something_ */
1627 smtbl = &ctx->localmac;
1628 } else {
1629 smtbl = &smacros;
1631 m = (SMacro *) hash_findix(smtbl, name);
1633 while (m) {
1634 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1635 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1636 if (defn) {
1637 if (nparam == (int) m->nparam || nparam == -1)
1638 *defn = m;
1639 else
1640 *defn = NULL;
1642 return true;
1644 m = m->next;
1647 return false;
1651 * Count and mark off the parameters in a multi-line macro call.
1652 * This is called both from within the multi-line macro expansion
1653 * code, and also to mark off the default parameters when provided
1654 * in a %macro definition line.
1656 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1658 int paramsize, brace;
1660 *nparam = paramsize = 0;
1661 *params = NULL;
1662 while (t) {
1663 /* +1: we need space for the final NULL */
1664 if (*nparam+1 >= paramsize) {
1665 paramsize += PARAM_DELTA;
1666 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1668 skip_white_(t);
1669 brace = 0;
1670 if (tok_is_(t, "{"))
1671 brace++;
1672 (*params)[(*nparam)++] = t;
1673 if (brace) {
1674 while (brace && (t = t->next) != NULL) {
1675 if (tok_is_(t, "{"))
1676 brace++;
1677 else if (tok_is_(t, "}"))
1678 brace--;
1681 if (t) {
1683 * Now we've found the closing brace, look further
1684 * for the comma.
1686 t = t->next;
1687 skip_white_(t);
1688 if (tok_isnt_(t, ",")) {
1689 nasm_error(ERR_NONFATAL,
1690 "braces do not enclose all of macro parameter");
1691 while (tok_isnt_(t, ","))
1692 t = t->next;
1695 } else {
1696 while (tok_isnt_(t, ","))
1697 t = t->next;
1699 if (t) { /* got a comma/brace */
1700 t = t->next; /* eat the comma */
1706 * Determine whether one of the various `if' conditions is true or
1707 * not.
1709 * We must free the tline we get passed.
1711 static bool if_condition(Token * tline, enum preproc_token ct)
1713 enum pp_conditional i = PP_COND(ct);
1714 bool j;
1715 Token *t, *tt, **tptr, *origline;
1716 struct tokenval tokval;
1717 expr *evalresult;
1718 enum pp_token_type needtype;
1719 char *p;
1721 origline = tline;
1723 switch (i) {
1724 case PPC_IFCTX:
1725 j = false; /* have we matched yet? */
1726 while (true) {
1727 skip_white_(tline);
1728 if (!tline)
1729 break;
1730 if (tline->type != TOK_ID) {
1731 nasm_error(ERR_NONFATAL,
1732 "`%s' expects context identifiers", pp_directives[ct]);
1733 free_tlist(origline);
1734 return -1;
1736 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1737 j = true;
1738 tline = tline->next;
1740 break;
1742 case PPC_IFDEF:
1743 j = false; /* have we matched yet? */
1744 while (tline) {
1745 skip_white_(tline);
1746 if (!tline || (tline->type != TOK_ID &&
1747 (tline->type != TOK_PREPROC_ID ||
1748 tline->text[1] != '$'))) {
1749 nasm_error(ERR_NONFATAL,
1750 "`%s' expects macro identifiers", pp_directives[ct]);
1751 goto fail;
1753 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1754 j = true;
1755 tline = tline->next;
1757 break;
1759 case PPC_IFENV:
1760 tline = expand_smacro(tline);
1761 j = false; /* have we matched yet? */
1762 while (tline) {
1763 skip_white_(tline);
1764 if (!tline || (tline->type != TOK_ID &&
1765 tline->type != TOK_STRING &&
1766 (tline->type != TOK_PREPROC_ID ||
1767 tline->text[1] != '!'))) {
1768 nasm_error(ERR_NONFATAL,
1769 "`%s' expects environment variable names",
1770 pp_directives[ct]);
1771 goto fail;
1773 p = tline->text;
1774 if (tline->type == TOK_PREPROC_ID)
1775 p += 2; /* Skip leading %! */
1776 if (*p == '\'' || *p == '\"' || *p == '`')
1777 nasm_unquote_cstr(p, ct);
1778 if (getenv(p))
1779 j = true;
1780 tline = tline->next;
1782 break;
1784 case PPC_IFIDN:
1785 case PPC_IFIDNI:
1786 tline = expand_smacro(tline);
1787 t = tt = tline;
1788 while (tok_isnt_(tt, ","))
1789 tt = tt->next;
1790 if (!tt) {
1791 nasm_error(ERR_NONFATAL,
1792 "`%s' expects two comma-separated arguments",
1793 pp_directives[ct]);
1794 goto fail;
1796 tt = tt->next;
1797 j = true; /* assume equality unless proved not */
1798 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1799 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1800 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
1801 pp_directives[ct]);
1802 goto fail;
1804 if (t->type == TOK_WHITESPACE) {
1805 t = t->next;
1806 continue;
1808 if (tt->type == TOK_WHITESPACE) {
1809 tt = tt->next;
1810 continue;
1812 if (tt->type != t->type) {
1813 j = false; /* found mismatching tokens */
1814 break;
1816 /* When comparing strings, need to unquote them first */
1817 if (t->type == TOK_STRING) {
1818 size_t l1 = nasm_unquote(t->text, NULL);
1819 size_t l2 = nasm_unquote(tt->text, NULL);
1821 if (l1 != l2) {
1822 j = false;
1823 break;
1825 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1826 j = false;
1827 break;
1829 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1830 j = false; /* found mismatching tokens */
1831 break;
1834 t = t->next;
1835 tt = tt->next;
1837 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1838 j = false; /* trailing gunk on one end or other */
1839 break;
1841 case PPC_IFMACRO:
1843 bool found = false;
1844 MMacro searching, *mmac;
1846 skip_white_(tline);
1847 tline = expand_id(tline);
1848 if (!tok_type_(tline, TOK_ID)) {
1849 nasm_error(ERR_NONFATAL,
1850 "`%s' expects a macro name", pp_directives[ct]);
1851 goto fail;
1853 searching.name = nasm_strdup(tline->text);
1854 searching.casesense = true;
1855 searching.plus = false;
1856 searching.nolist = false;
1857 searching.in_progress = 0;
1858 searching.max_depth = 0;
1859 searching.rep_nest = NULL;
1860 searching.nparam_min = 0;
1861 searching.nparam_max = INT_MAX;
1862 tline = expand_smacro(tline->next);
1863 skip_white_(tline);
1864 if (!tline) {
1865 } else if (!tok_type_(tline, TOK_NUMBER)) {
1866 nasm_error(ERR_NONFATAL,
1867 "`%s' expects a parameter count or nothing",
1868 pp_directives[ct]);
1869 } else {
1870 searching.nparam_min = searching.nparam_max =
1871 readnum(tline->text, &j);
1872 if (j)
1873 nasm_error(ERR_NONFATAL,
1874 "unable to parse parameter count `%s'",
1875 tline->text);
1877 if (tline && tok_is_(tline->next, "-")) {
1878 tline = tline->next->next;
1879 if (tok_is_(tline, "*"))
1880 searching.nparam_max = INT_MAX;
1881 else if (!tok_type_(tline, TOK_NUMBER))
1882 nasm_error(ERR_NONFATAL,
1883 "`%s' expects a parameter count after `-'",
1884 pp_directives[ct]);
1885 else {
1886 searching.nparam_max = readnum(tline->text, &j);
1887 if (j)
1888 nasm_error(ERR_NONFATAL,
1889 "unable to parse parameter count `%s'",
1890 tline->text);
1891 if (searching.nparam_min > searching.nparam_max)
1892 nasm_error(ERR_NONFATAL,
1893 "minimum parameter count exceeds maximum");
1896 if (tline && tok_is_(tline->next, "+")) {
1897 tline = tline->next;
1898 searching.plus = true;
1900 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1901 while (mmac) {
1902 if (!strcmp(mmac->name, searching.name) &&
1903 (mmac->nparam_min <= searching.nparam_max
1904 || searching.plus)
1905 && (searching.nparam_min <= mmac->nparam_max
1906 || mmac->plus)) {
1907 found = true;
1908 break;
1910 mmac = mmac->next;
1912 if (tline && tline->next)
1913 nasm_error(ERR_WARNING|ERR_PASS1,
1914 "trailing garbage after %%ifmacro ignored");
1915 nasm_free(searching.name);
1916 j = found;
1917 break;
1920 case PPC_IFID:
1921 needtype = TOK_ID;
1922 goto iftype;
1923 case PPC_IFNUM:
1924 needtype = TOK_NUMBER;
1925 goto iftype;
1926 case PPC_IFSTR:
1927 needtype = TOK_STRING;
1928 goto iftype;
1930 iftype:
1931 t = tline = expand_smacro(tline);
1933 while (tok_type_(t, TOK_WHITESPACE) ||
1934 (needtype == TOK_NUMBER &&
1935 tok_type_(t, TOK_OTHER) &&
1936 (t->text[0] == '-' || t->text[0] == '+') &&
1937 !t->text[1]))
1938 t = t->next;
1940 j = tok_type_(t, needtype);
1941 break;
1943 case PPC_IFTOKEN:
1944 t = tline = expand_smacro(tline);
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
1948 j = false;
1949 if (t) {
1950 t = t->next; /* Skip the actual token */
1951 while (tok_type_(t, TOK_WHITESPACE))
1952 t = t->next;
1953 j = !t; /* Should be nothing left */
1955 break;
1957 case PPC_IFEMPTY:
1958 t = tline = expand_smacro(tline);
1959 while (tok_type_(t, TOK_WHITESPACE))
1960 t = t->next;
1962 j = !t; /* Should be empty */
1963 break;
1965 case PPC_IF:
1966 t = tline = expand_smacro(tline);
1967 tptr = &t;
1968 tokval.t_type = TOKEN_INVALID;
1969 evalresult = evaluate(ppscan, tptr, &tokval,
1970 NULL, pass | CRITICAL, NULL);
1971 if (!evalresult)
1972 return -1;
1973 if (tokval.t_type)
1974 nasm_error(ERR_WARNING|ERR_PASS1,
1975 "trailing garbage after expression ignored");
1976 if (!is_simple(evalresult)) {
1977 nasm_error(ERR_NONFATAL,
1978 "non-constant value given to `%s'", pp_directives[ct]);
1979 goto fail;
1981 j = reloc_value(evalresult) != 0;
1982 break;
1984 default:
1985 nasm_error(ERR_FATAL,
1986 "preprocessor directive `%s' not yet implemented",
1987 pp_directives[ct]);
1988 goto fail;
1991 free_tlist(origline);
1992 return j ^ PP_NEGATIVE(ct);
1994 fail:
1995 free_tlist(origline);
1996 return -1;
2000 * Common code for defining an smacro
2002 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2003 int nparam, Token *expansion)
2005 SMacro *smac, **smhead;
2006 struct hash_table *smtbl;
2008 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2009 if (!smac) {
2010 nasm_error(ERR_WARNING|ERR_PASS1,
2011 "single-line macro `%s' defined both with and"
2012 " without parameters", mname);
2014 * Some instances of the old code considered this a failure,
2015 * some others didn't. What is the right thing to do here?
2017 free_tlist(expansion);
2018 return false; /* Failure */
2019 } else {
2021 * We're redefining, so we have to take over an
2022 * existing SMacro structure. This means freeing
2023 * what was already in it.
2025 nasm_free(smac->name);
2026 free_tlist(smac->expansion);
2028 } else {
2029 smtbl = ctx ? &ctx->localmac : &smacros;
2030 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2031 smac = nasm_malloc(sizeof(SMacro));
2032 smac->next = *smhead;
2033 *smhead = smac;
2035 smac->name = nasm_strdup(mname);
2036 smac->casesense = casesense;
2037 smac->nparam = nparam;
2038 smac->expansion = expansion;
2039 smac->in_progress = false;
2040 return true; /* Success */
2044 * Undefine an smacro
2046 static void undef_smacro(Context *ctx, const char *mname)
2048 SMacro **smhead, *s, **sp;
2049 struct hash_table *smtbl;
2051 smtbl = ctx ? &ctx->localmac : &smacros;
2052 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2054 if (smhead) {
2056 * We now have a macro name... go hunt for it.
2058 sp = smhead;
2059 while ((s = *sp) != NULL) {
2060 if (!mstrcmp(s->name, mname, s->casesense)) {
2061 *sp = s->next;
2062 nasm_free(s->name);
2063 free_tlist(s->expansion);
2064 nasm_free(s);
2065 } else {
2066 sp = &s->next;
2073 * Parse a mmacro specification.
2075 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2077 bool err;
2079 tline = tline->next;
2080 skip_white_(tline);
2081 tline = expand_id(tline);
2082 if (!tok_type_(tline, TOK_ID)) {
2083 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2084 return false;
2087 def->prev = NULL;
2088 def->name = nasm_strdup(tline->text);
2089 def->plus = false;
2090 def->nolist = false;
2091 def->in_progress = 0;
2092 def->rep_nest = NULL;
2093 def->nparam_min = 0;
2094 def->nparam_max = 0;
2096 tline = expand_smacro(tline->next);
2097 skip_white_(tline);
2098 if (!tok_type_(tline, TOK_NUMBER)) {
2099 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2100 } else {
2101 def->nparam_min = def->nparam_max =
2102 readnum(tline->text, &err);
2103 if (err)
2104 nasm_error(ERR_NONFATAL,
2105 "unable to parse parameter count `%s'", tline->text);
2107 if (tline && tok_is_(tline->next, "-")) {
2108 tline = tline->next->next;
2109 if (tok_is_(tline, "*")) {
2110 def->nparam_max = INT_MAX;
2111 } else if (!tok_type_(tline, TOK_NUMBER)) {
2112 nasm_error(ERR_NONFATAL,
2113 "`%s' expects a parameter count after `-'", directive);
2114 } else {
2115 def->nparam_max = readnum(tline->text, &err);
2116 if (err) {
2117 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2118 tline->text);
2120 if (def->nparam_min > def->nparam_max) {
2121 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2125 if (tline && tok_is_(tline->next, "+")) {
2126 tline = tline->next;
2127 def->plus = true;
2129 if (tline && tok_type_(tline->next, TOK_ID) &&
2130 !nasm_stricmp(tline->next->text, ".nolist")) {
2131 tline = tline->next;
2132 def->nolist = true;
2136 * Handle default parameters.
2138 if (tline && tline->next) {
2139 def->dlist = tline->next;
2140 tline->next = NULL;
2141 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2142 } else {
2143 def->dlist = NULL;
2144 def->defaults = NULL;
2146 def->expansion = NULL;
2148 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2149 !def->plus)
2150 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2151 "too many default macro parameters");
2153 return true;
2158 * Decode a size directive
2160 static int parse_size(const char *str) {
2161 static const char *size_names[] =
2162 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2163 static const int sizes[] =
2164 { 0, 1, 4, 16, 8, 10, 2, 32 };
2166 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2170 * find and process preprocessor directive in passed line
2171 * Find out if a line contains a preprocessor directive, and deal
2172 * with it if so.
2174 * If a directive _is_ found, it is the responsibility of this routine
2175 * (and not the caller) to free_tlist() the line.
2177 * @param tline a pointer to the current tokeninzed line linked list
2178 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2181 static int do_directive(Token * tline)
2183 enum preproc_token i;
2184 int j;
2185 bool err;
2186 int nparam;
2187 bool nolist;
2188 bool casesense;
2189 int k, m;
2190 int offset;
2191 char *p, *pp, *found_path;
2192 const char *mname;
2193 Include *inc;
2194 Context *ctx;
2195 Cond *cond;
2196 MMacro *mmac, **mmhead;
2197 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2198 Line *l;
2199 struct tokenval tokval;
2200 expr *evalresult;
2201 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2202 int64_t count;
2203 size_t len;
2204 int severity;
2206 origline = tline;
2208 skip_white_(tline);
2209 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2210 (tline->text[1] == '%' || tline->text[1] == '$'
2211 || tline->text[1] == '!'))
2212 return NO_DIRECTIVE_FOUND;
2214 i = pp_token_hash(tline->text);
2217 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2218 * since they are known to be buggy at moment, we need to fix them
2219 * in future release (2.09-2.10)
2221 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
2222 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2223 tline->text);
2224 return NO_DIRECTIVE_FOUND;
2228 * If we're in a non-emitting branch of a condition construct,
2229 * or walking to the end of an already terminated %rep block,
2230 * we should ignore all directives except for condition
2231 * directives.
2233 if (((istk->conds && !emitting(istk->conds->state)) ||
2234 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2235 return NO_DIRECTIVE_FOUND;
2239 * If we're defining a macro or reading a %rep block, we should
2240 * ignore all directives except for %macro/%imacro (which nest),
2241 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2242 * If we're in a %rep block, another %rep nests, so should be let through.
2244 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2245 i != PP_RMACRO && i != PP_IRMACRO &&
2246 i != PP_ENDMACRO && i != PP_ENDM &&
2247 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2248 return NO_DIRECTIVE_FOUND;
2251 if (defining) {
2252 if (i == PP_MACRO || i == PP_IMACRO ||
2253 i == PP_RMACRO || i == PP_IRMACRO) {
2254 nested_mac_count++;
2255 return NO_DIRECTIVE_FOUND;
2256 } else if (nested_mac_count > 0) {
2257 if (i == PP_ENDMACRO) {
2258 nested_mac_count--;
2259 return NO_DIRECTIVE_FOUND;
2262 if (!defining->name) {
2263 if (i == PP_REP) {
2264 nested_rep_count++;
2265 return NO_DIRECTIVE_FOUND;
2266 } else if (nested_rep_count > 0) {
2267 if (i == PP_ENDREP) {
2268 nested_rep_count--;
2269 return NO_DIRECTIVE_FOUND;
2275 switch (i) {
2276 case PP_INVALID:
2277 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2278 tline->text);
2279 return NO_DIRECTIVE_FOUND; /* didn't get it */
2281 case PP_STACKSIZE:
2282 /* Directive to tell NASM what the default stack size is. The
2283 * default is for a 16-bit stack, and this can be overriden with
2284 * %stacksize large.
2286 tline = tline->next;
2287 if (tline && tline->type == TOK_WHITESPACE)
2288 tline = tline->next;
2289 if (!tline || tline->type != TOK_ID) {
2290 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2294 if (nasm_stricmp(tline->text, "flat") == 0) {
2295 /* All subsequent ARG directives are for a 32-bit stack */
2296 StackSize = 4;
2297 StackPointer = "ebp";
2298 ArgOffset = 8;
2299 LocalOffset = 0;
2300 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2301 /* All subsequent ARG directives are for a 64-bit stack */
2302 StackSize = 8;
2303 StackPointer = "rbp";
2304 ArgOffset = 16;
2305 LocalOffset = 0;
2306 } else if (nasm_stricmp(tline->text, "large") == 0) {
2307 /* All subsequent ARG directives are for a 16-bit stack,
2308 * far function call.
2310 StackSize = 2;
2311 StackPointer = "bp";
2312 ArgOffset = 4;
2313 LocalOffset = 0;
2314 } else if (nasm_stricmp(tline->text, "small") == 0) {
2315 /* All subsequent ARG directives are for a 16-bit stack,
2316 * far function call. We don't support near functions.
2318 StackSize = 2;
2319 StackPointer = "bp";
2320 ArgOffset = 6;
2321 LocalOffset = 0;
2322 } else {
2323 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2324 free_tlist(origline);
2325 return DIRECTIVE_FOUND;
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
2330 case PP_ARG:
2331 /* TASM like ARG directive to define arguments to functions, in
2332 * the following form:
2334 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2336 offset = ArgOffset;
2337 do {
2338 char *arg, directive[256];
2339 int size = StackSize;
2341 /* Find the argument name */
2342 tline = tline->next;
2343 if (tline && tline->type == TOK_WHITESPACE)
2344 tline = tline->next;
2345 if (!tline || tline->type != TOK_ID) {
2346 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2347 free_tlist(origline);
2348 return DIRECTIVE_FOUND;
2350 arg = tline->text;
2352 /* Find the argument size type */
2353 tline = tline->next;
2354 if (!tline || tline->type != TOK_OTHER
2355 || tline->text[0] != ':') {
2356 nasm_error(ERR_NONFATAL,
2357 "Syntax error processing `%%arg' directive");
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2361 tline = tline->next;
2362 if (!tline || tline->type != TOK_ID) {
2363 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2364 free_tlist(origline);
2365 return DIRECTIVE_FOUND;
2368 /* Allow macro expansion of type parameter */
2369 tt = tokenize(tline->text);
2370 tt = expand_smacro(tt);
2371 size = parse_size(tt->text);
2372 if (!size) {
2373 nasm_error(ERR_NONFATAL,
2374 "Invalid size type for `%%arg' missing directive");
2375 free_tlist(tt);
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2379 free_tlist(tt);
2381 /* Round up to even stack slots */
2382 size = ALIGN(size, StackSize);
2384 /* Now define the macro for the argument */
2385 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2386 arg, StackPointer, offset);
2387 do_directive(tokenize(directive));
2388 offset += size;
2390 /* Move to the next argument in the list */
2391 tline = tline->next;
2392 if (tline && tline->type == TOK_WHITESPACE)
2393 tline = tline->next;
2394 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2395 ArgOffset = offset;
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2399 case PP_LOCAL:
2400 /* TASM like LOCAL directive to define local variables for a
2401 * function, in the following form:
2403 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2405 * The '= LocalSize' at the end is ignored by NASM, but is
2406 * required by TASM to define the local parameter size (and used
2407 * by the TASM macro package).
2409 offset = LocalOffset;
2410 do {
2411 char *local, directive[256];
2412 int size = StackSize;
2414 /* Find the argument name */
2415 tline = tline->next;
2416 if (tline && tline->type == TOK_WHITESPACE)
2417 tline = tline->next;
2418 if (!tline || tline->type != TOK_ID) {
2419 nasm_error(ERR_NONFATAL,
2420 "`%%local' missing argument parameter");
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2424 local = tline->text;
2426 /* Find the argument size type */
2427 tline = tline->next;
2428 if (!tline || tline->type != TOK_OTHER
2429 || tline->text[0] != ':') {
2430 nasm_error(ERR_NONFATAL,
2431 "Syntax error processing `%%local' directive");
2432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
2435 tline = tline->next;
2436 if (!tline || tline->type != TOK_ID) {
2437 nasm_error(ERR_NONFATAL,
2438 "`%%local' missing size type parameter");
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND;
2443 /* Allow macro expansion of type parameter */
2444 tt = tokenize(tline->text);
2445 tt = expand_smacro(tt);
2446 size = parse_size(tt->text);
2447 if (!size) {
2448 nasm_error(ERR_NONFATAL,
2449 "Invalid size type for `%%local' missing directive");
2450 free_tlist(tt);
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
2454 free_tlist(tt);
2456 /* Round up to even stack slots */
2457 size = ALIGN(size, StackSize);
2459 offset += size; /* Negative offset, increment before */
2461 /* Now define the macro for the argument */
2462 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2463 local, StackPointer, offset);
2464 do_directive(tokenize(directive));
2466 /* Now define the assign to setup the enter_c macro correctly */
2467 snprintf(directive, sizeof(directive),
2468 "%%assign %%$localsize %%$localsize+%d", size);
2469 do_directive(tokenize(directive));
2471 /* Move to the next argument in the list */
2472 tline = tline->next;
2473 if (tline && tline->type == TOK_WHITESPACE)
2474 tline = tline->next;
2475 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2476 LocalOffset = offset;
2477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
2480 case PP_CLEAR:
2481 if (tline->next)
2482 nasm_error(ERR_WARNING|ERR_PASS1,
2483 "trailing garbage after `%%clear' ignored");
2484 free_macros();
2485 init_macros();
2486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
2489 case PP_DEPEND:
2490 t = tline->next = expand_smacro(tline->next);
2491 skip_white_(t);
2492 if (!t || (t->type != TOK_STRING &&
2493 t->type != TOK_INTERNAL_STRING)) {
2494 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND; /* but we did _something_ */
2498 if (t->next)
2499 nasm_error(ERR_WARNING|ERR_PASS1,
2500 "trailing garbage after `%%depend' ignored");
2501 p = t->text;
2502 if (t->type != TOK_INTERNAL_STRING)
2503 nasm_unquote_cstr(p, i);
2504 if (dephead && !in_list(*dephead, p)) {
2505 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2506 sl->next = NULL;
2507 strcpy(sl->str, p);
2508 *deptail = sl;
2509 deptail = &sl->next;
2511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
2514 case PP_INCLUDE:
2515 t = tline->next = expand_smacro(tline->next);
2516 skip_white_(t);
2518 if (!t || (t->type != TOK_STRING &&
2519 t->type != TOK_INTERNAL_STRING)) {
2520 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
2521 free_tlist(origline);
2522 return DIRECTIVE_FOUND; /* but we did _something_ */
2524 if (t->next)
2525 nasm_error(ERR_WARNING|ERR_PASS1,
2526 "trailing garbage after `%%include' ignored");
2527 p = t->text;
2528 if (t->type != TOK_INTERNAL_STRING)
2529 nasm_unquote_cstr(p, i);
2530 inc = nasm_malloc(sizeof(Include));
2531 inc->next = istk;
2532 inc->conds = NULL;
2533 found_path = NULL;
2534 inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, NF_TEXT);
2535 if (!inc->fp) {
2536 /* -MG given but file not found */
2537 nasm_free(inc);
2538 } else {
2539 inc->fname = src_set_fname(found_path ? found_path : p);
2540 inc->lineno = src_set_linnum(0);
2541 inc->lineinc = 1;
2542 inc->expansion = NULL;
2543 inc->mstk = NULL;
2544 istk = inc;
2545 lfmt->uplevel(LIST_INCLUDE);
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
2550 case PP_USE:
2552 static macros_t *use_pkg;
2553 const char *pkg_macro = NULL;
2555 tline = tline->next;
2556 skip_white_(tline);
2557 tline = expand_id(tline);
2559 if (!tline || (tline->type != TOK_STRING &&
2560 tline->type != TOK_INTERNAL_STRING &&
2561 tline->type != TOK_ID)) {
2562 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
2563 free_tlist(origline);
2564 return DIRECTIVE_FOUND; /* but we did _something_ */
2566 if (tline->next)
2567 nasm_error(ERR_WARNING|ERR_PASS1,
2568 "trailing garbage after `%%use' ignored");
2569 if (tline->type == TOK_STRING)
2570 nasm_unquote_cstr(tline->text, i);
2571 use_pkg = nasm_stdmac_find_package(tline->text);
2572 if (!use_pkg)
2573 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2574 else
2575 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2576 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2577 /* Not already included, go ahead and include it */
2578 stdmacpos = use_pkg;
2580 free_tlist(origline);
2581 return DIRECTIVE_FOUND;
2583 case PP_PUSH:
2584 case PP_REPL:
2585 case PP_POP:
2586 tline = tline->next;
2587 skip_white_(tline);
2588 tline = expand_id(tline);
2589 if (tline) {
2590 if (!tok_type_(tline, TOK_ID)) {
2591 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
2592 pp_directives[i]);
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND; /* but we did _something_ */
2596 if (tline->next)
2597 nasm_error(ERR_WARNING|ERR_PASS1,
2598 "trailing garbage after `%s' ignored",
2599 pp_directives[i]);
2600 p = nasm_strdup(tline->text);
2601 } else {
2602 p = NULL; /* Anonymous */
2605 if (i == PP_PUSH) {
2606 ctx = nasm_malloc(sizeof(Context));
2607 ctx->next = cstk;
2608 hash_init(&ctx->localmac, HASH_SMALL);
2609 ctx->name = p;
2610 ctx->number = unique++;
2611 cstk = ctx;
2612 } else {
2613 /* %pop or %repl */
2614 if (!cstk) {
2615 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
2616 pp_directives[i]);
2617 } else if (i == PP_POP) {
2618 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2619 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2620 "expected %s",
2621 cstk->name ? cstk->name : "anonymous", p);
2622 else
2623 ctx_pop();
2624 } else {
2625 /* i == PP_REPL */
2626 nasm_free(cstk->name);
2627 cstk->name = p;
2628 p = NULL;
2630 nasm_free(p);
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND;
2634 case PP_FATAL:
2635 severity = ERR_FATAL;
2636 goto issue_error;
2637 case PP_ERROR:
2638 severity = ERR_NONFATAL;
2639 goto issue_error;
2640 case PP_WARNING:
2641 severity = ERR_WARNING|ERR_WARN_USER;
2642 goto issue_error;
2644 issue_error:
2646 /* Only error out if this is the final pass */
2647 if (pass != 2 && i != PP_FATAL)
2648 return DIRECTIVE_FOUND;
2650 tline->next = expand_smacro(tline->next);
2651 tline = tline->next;
2652 skip_white_(tline);
2653 t = tline ? tline->next : NULL;
2654 skip_white_(t);
2655 if (tok_type_(tline, TOK_STRING) && !t) {
2656 /* The line contains only a quoted string */
2657 p = tline->text;
2658 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2659 nasm_error(severity, "%s", p);
2660 } else {
2661 /* Not a quoted string, or more than a quoted string */
2662 p = detoken(tline, false);
2663 nasm_error(severity, "%s", p);
2664 nasm_free(p);
2666 free_tlist(origline);
2667 return DIRECTIVE_FOUND;
2670 CASE_PP_IF:
2671 if (istk->conds && !emitting(istk->conds->state))
2672 j = COND_NEVER;
2673 else {
2674 j = if_condition(tline->next, i);
2675 tline->next = NULL; /* it got freed */
2676 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2678 cond = nasm_malloc(sizeof(Cond));
2679 cond->next = istk->conds;
2680 cond->state = j;
2681 istk->conds = cond;
2682 if(istk->mstk)
2683 istk->mstk->condcnt ++;
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND;
2687 CASE_PP_ELIF:
2688 if (!istk->conds)
2689 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2690 switch(istk->conds->state) {
2691 case COND_IF_TRUE:
2692 istk->conds->state = COND_DONE;
2693 break;
2695 case COND_DONE:
2696 case COND_NEVER:
2697 break;
2699 case COND_ELSE_TRUE:
2700 case COND_ELSE_FALSE:
2701 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2702 "`%%elif' after `%%else' ignored");
2703 istk->conds->state = COND_NEVER;
2704 break;
2706 case COND_IF_FALSE:
2708 * IMPORTANT: In the case of %if, we will already have
2709 * called expand_mmac_params(); however, if we're
2710 * processing an %elif we must have been in a
2711 * non-emitting mode, which would have inhibited
2712 * the normal invocation of expand_mmac_params().
2713 * Therefore, we have to do it explicitly here.
2715 j = if_condition(expand_mmac_params(tline->next), i);
2716 tline->next = NULL; /* it got freed */
2717 istk->conds->state =
2718 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2719 break;
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
2724 case PP_ELSE:
2725 if (tline->next)
2726 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2727 "trailing garbage after `%%else' ignored");
2728 if (!istk->conds)
2729 nasm_fatal(0, "`%%else: no matching `%%if'");
2730 switch(istk->conds->state) {
2731 case COND_IF_TRUE:
2732 case COND_DONE:
2733 istk->conds->state = COND_ELSE_FALSE;
2734 break;
2736 case COND_NEVER:
2737 break;
2739 case COND_IF_FALSE:
2740 istk->conds->state = COND_ELSE_TRUE;
2741 break;
2743 case COND_ELSE_TRUE:
2744 case COND_ELSE_FALSE:
2745 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2746 "`%%else' after `%%else' ignored.");
2747 istk->conds->state = COND_NEVER;
2748 break;
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
2753 case PP_ENDIF:
2754 if (tline->next)
2755 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2756 "trailing garbage after `%%endif' ignored");
2757 if (!istk->conds)
2758 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
2759 cond = istk->conds;
2760 istk->conds = cond->next;
2761 nasm_free(cond);
2762 if(istk->mstk)
2763 istk->mstk->condcnt --;
2764 free_tlist(origline);
2765 return DIRECTIVE_FOUND;
2767 case PP_RMACRO:
2768 case PP_IRMACRO:
2769 case PP_MACRO:
2770 case PP_IMACRO:
2771 if (defining) {
2772 nasm_error(ERR_FATAL, "`%s': already defining a macro",
2773 pp_directives[i]);
2774 return DIRECTIVE_FOUND;
2776 defining = nasm_zalloc(sizeof(MMacro));
2777 defining->max_depth =
2778 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2779 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2780 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2781 nasm_free(defining);
2782 defining = NULL;
2783 return DIRECTIVE_FOUND;
2786 src_get(&defining->xline, &defining->fname);
2788 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2789 while (mmac) {
2790 if (!strcmp(mmac->name, defining->name) &&
2791 (mmac->nparam_min <= defining->nparam_max
2792 || defining->plus)
2793 && (defining->nparam_min <= mmac->nparam_max
2794 || mmac->plus)) {
2795 nasm_error(ERR_WARNING|ERR_PASS1,
2796 "redefining multi-line macro `%s'", defining->name);
2797 return DIRECTIVE_FOUND;
2799 mmac = mmac->next;
2801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
2804 case PP_ENDM:
2805 case PP_ENDMACRO:
2806 if (! (defining && defining->name)) {
2807 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2808 return DIRECTIVE_FOUND;
2810 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2811 defining->next = *mmhead;
2812 *mmhead = defining;
2813 defining = NULL;
2814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
2817 case PP_EXITMACRO:
2819 * We must search along istk->expansion until we hit a
2820 * macro-end marker for a macro with a name. Then we
2821 * bypass all lines between exitmacro and endmacro.
2823 list_for_each(l, istk->expansion)
2824 if (l->finishes && l->finishes->name)
2825 break;
2827 if (l) {
2829 * Remove all conditional entries relative to this
2830 * macro invocation. (safe to do in this context)
2832 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2833 cond = istk->conds;
2834 istk->conds = cond->next;
2835 nasm_free(cond);
2837 istk->expansion = l;
2838 } else {
2839 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
2844 case PP_UNMACRO:
2845 case PP_UNIMACRO:
2847 MMacro **mmac_p;
2848 MMacro spec;
2850 spec.casesense = (i == PP_UNMACRO);
2851 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2852 return DIRECTIVE_FOUND;
2854 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2855 while (mmac_p && *mmac_p) {
2856 mmac = *mmac_p;
2857 if (mmac->casesense == spec.casesense &&
2858 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2859 mmac->nparam_min == spec.nparam_min &&
2860 mmac->nparam_max == spec.nparam_max &&
2861 mmac->plus == spec.plus) {
2862 *mmac_p = mmac->next;
2863 free_mmacro(mmac);
2864 } else {
2865 mmac_p = &mmac->next;
2868 free_tlist(origline);
2869 free_tlist(spec.dlist);
2870 return DIRECTIVE_FOUND;
2873 case PP_ROTATE:
2874 if (tline->next && tline->next->type == TOK_WHITESPACE)
2875 tline = tline->next;
2876 if (!tline->next) {
2877 free_tlist(origline);
2878 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2879 return DIRECTIVE_FOUND;
2881 t = expand_smacro(tline->next);
2882 tline->next = NULL;
2883 free_tlist(origline);
2884 tline = t;
2885 tptr = &t;
2886 tokval.t_type = TOKEN_INVALID;
2887 evalresult =
2888 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
2889 free_tlist(tline);
2890 if (!evalresult)
2891 return DIRECTIVE_FOUND;
2892 if (tokval.t_type)
2893 nasm_error(ERR_WARNING|ERR_PASS1,
2894 "trailing garbage after expression ignored");
2895 if (!is_simple(evalresult)) {
2896 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2897 return DIRECTIVE_FOUND;
2899 mmac = istk->mstk;
2900 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2901 mmac = mmac->next_active;
2902 if (!mmac) {
2903 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2904 } else if (mmac->nparam == 0) {
2905 nasm_error(ERR_NONFATAL,
2906 "`%%rotate' invoked within macro without parameters");
2907 } else {
2908 int rotate = mmac->rotate + reloc_value(evalresult);
2910 rotate %= (int)mmac->nparam;
2911 if (rotate < 0)
2912 rotate += mmac->nparam;
2914 mmac->rotate = rotate;
2916 return DIRECTIVE_FOUND;
2918 case PP_REP:
2919 nolist = false;
2920 do {
2921 tline = tline->next;
2922 } while (tok_type_(tline, TOK_WHITESPACE));
2924 if (tok_type_(tline, TOK_ID) &&
2925 nasm_stricmp(tline->text, ".nolist") == 0) {
2926 nolist = true;
2927 do {
2928 tline = tline->next;
2929 } while (tok_type_(tline, TOK_WHITESPACE));
2932 if (tline) {
2933 t = expand_smacro(tline);
2934 tptr = &t;
2935 tokval.t_type = TOKEN_INVALID;
2936 evalresult =
2937 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
2938 if (!evalresult) {
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND;
2942 if (tokval.t_type)
2943 nasm_error(ERR_WARNING|ERR_PASS1,
2944 "trailing garbage after expression ignored");
2945 if (!is_simple(evalresult)) {
2946 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2947 return DIRECTIVE_FOUND;
2949 count = reloc_value(evalresult);
2950 if (count >= REP_LIMIT) {
2951 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2952 count = 0;
2953 } else
2954 count++;
2955 } else {
2956 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2957 count = 0;
2959 free_tlist(origline);
2961 tmp_defining = defining;
2962 defining = nasm_malloc(sizeof(MMacro));
2963 defining->prev = NULL;
2964 defining->name = NULL; /* flags this macro as a %rep block */
2965 defining->casesense = false;
2966 defining->plus = false;
2967 defining->nolist = nolist;
2968 defining->in_progress = count;
2969 defining->max_depth = 0;
2970 defining->nparam_min = defining->nparam_max = 0;
2971 defining->defaults = NULL;
2972 defining->dlist = NULL;
2973 defining->expansion = NULL;
2974 defining->next_active = istk->mstk;
2975 defining->rep_nest = tmp_defining;
2976 return DIRECTIVE_FOUND;
2978 case PP_ENDREP:
2979 if (!defining || defining->name) {
2980 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2981 return DIRECTIVE_FOUND;
2985 * Now we have a "macro" defined - although it has no name
2986 * and we won't be entering it in the hash tables - we must
2987 * push a macro-end marker for it on to istk->expansion.
2988 * After that, it will take care of propagating itself (a
2989 * macro-end marker line for a macro which is really a %rep
2990 * block will cause the macro to be re-expanded, complete
2991 * with another macro-end marker to ensure the process
2992 * continues) until the whole expansion is forcibly removed
2993 * from istk->expansion by a %exitrep.
2995 l = nasm_malloc(sizeof(Line));
2996 l->next = istk->expansion;
2997 l->finishes = defining;
2998 l->first = NULL;
2999 istk->expansion = l;
3001 istk->mstk = defining;
3003 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3004 tmp_defining = defining;
3005 defining = defining->rep_nest;
3006 free_tlist(origline);
3007 return DIRECTIVE_FOUND;
3009 case PP_EXITREP:
3011 * We must search along istk->expansion until we hit a
3012 * macro-end marker for a macro with no name. Then we set
3013 * its `in_progress' flag to 0.
3015 list_for_each(l, istk->expansion)
3016 if (l->finishes && !l->finishes->name)
3017 break;
3019 if (l)
3020 l->finishes->in_progress = 1;
3021 else
3022 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
3026 case PP_XDEFINE:
3027 case PP_IXDEFINE:
3028 case PP_DEFINE:
3029 case PP_IDEFINE:
3030 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3032 tline = tline->next;
3033 skip_white_(tline);
3034 tline = expand_id(tline);
3035 if (!tline || (tline->type != TOK_ID &&
3036 (tline->type != TOK_PREPROC_ID ||
3037 tline->text[1] != '$'))) {
3038 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
3039 pp_directives[i]);
3040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
3044 ctx = get_ctx(tline->text, &mname);
3045 last = tline;
3046 param_start = tline = tline->next;
3047 nparam = 0;
3049 /* Expand the macro definition now for %xdefine and %ixdefine */
3050 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3051 tline = expand_smacro(tline);
3053 if (tok_is_(tline, "(")) {
3055 * This macro has parameters.
3058 tline = tline->next;
3059 while (1) {
3060 skip_white_(tline);
3061 if (!tline) {
3062 nasm_error(ERR_NONFATAL, "parameter identifier expected");
3063 free_tlist(origline);
3064 return DIRECTIVE_FOUND;
3066 if (tline->type != TOK_ID) {
3067 nasm_error(ERR_NONFATAL,
3068 "`%s': parameter identifier expected",
3069 tline->text);
3070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3073 tline->type = TOK_SMAC_PARAM + nparam++;
3074 tline = tline->next;
3075 skip_white_(tline);
3076 if (tok_is_(tline, ",")) {
3077 tline = tline->next;
3078 } else {
3079 if (!tok_is_(tline, ")")) {
3080 nasm_error(ERR_NONFATAL,
3081 "`)' expected to terminate macro template");
3082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
3085 break;
3088 last = tline;
3089 tline = tline->next;
3091 if (tok_type_(tline, TOK_WHITESPACE))
3092 last = tline, tline = tline->next;
3093 macro_start = NULL;
3094 last->next = NULL;
3095 t = tline;
3096 while (t) {
3097 if (t->type == TOK_ID) {
3098 list_for_each(tt, param_start)
3099 if (tt->type >= TOK_SMAC_PARAM &&
3100 !strcmp(tt->text, t->text))
3101 t->type = tt->type;
3103 tt = t->next;
3104 t->next = macro_start;
3105 macro_start = t;
3106 t = tt;
3109 * Good. We now have a macro name, a parameter count, and a
3110 * token list (in reverse order) for an expansion. We ought
3111 * to be OK just to create an SMacro, store it, and let
3112 * free_tlist have the rest of the line (which we have
3113 * carefully re-terminated after chopping off the expansion
3114 * from the end).
3116 define_smacro(ctx, mname, casesense, nparam, macro_start);
3117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
3120 case PP_UNDEF:
3121 tline = tline->next;
3122 skip_white_(tline);
3123 tline = expand_id(tline);
3124 if (!tline || (tline->type != TOK_ID &&
3125 (tline->type != TOK_PREPROC_ID ||
3126 tline->text[1] != '$'))) {
3127 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3131 if (tline->next) {
3132 nasm_error(ERR_WARNING|ERR_PASS1,
3133 "trailing garbage after macro name ignored");
3136 /* Find the context that symbol belongs to */
3137 ctx = get_ctx(tline->text, &mname);
3138 undef_smacro(ctx, mname);
3139 free_tlist(origline);
3140 return DIRECTIVE_FOUND;
3142 case PP_DEFSTR:
3143 case PP_IDEFSTR:
3144 casesense = (i == PP_DEFSTR);
3146 tline = tline->next;
3147 skip_white_(tline);
3148 tline = expand_id(tline);
3149 if (!tline || (tline->type != TOK_ID &&
3150 (tline->type != TOK_PREPROC_ID ||
3151 tline->text[1] != '$'))) {
3152 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
3153 pp_directives[i]);
3154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3158 ctx = get_ctx(tline->text, &mname);
3159 last = tline;
3160 tline = expand_smacro(tline->next);
3161 last->next = NULL;
3163 while (tok_type_(tline, TOK_WHITESPACE))
3164 tline = delete_Token(tline);
3166 p = detoken(tline, false);
3167 macro_start = nasm_malloc(sizeof(*macro_start));
3168 macro_start->next = NULL;
3169 macro_start->text = nasm_quote(p, strlen(p));
3170 macro_start->type = TOK_STRING;
3171 macro_start->a.mac = NULL;
3172 nasm_free(p);
3175 * We now have a macro name, an implicit parameter count of
3176 * zero, and a string token to use as an expansion. Create
3177 * and store an SMacro.
3179 define_smacro(ctx, mname, casesense, 0, macro_start);
3180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
3183 case PP_DEFTOK:
3184 case PP_IDEFTOK:
3185 casesense = (i == PP_DEFTOK);
3187 tline = tline->next;
3188 skip_white_(tline);
3189 tline = expand_id(tline);
3190 if (!tline || (tline->type != TOK_ID &&
3191 (tline->type != TOK_PREPROC_ID ||
3192 tline->text[1] != '$'))) {
3193 nasm_error(ERR_NONFATAL,
3194 "`%s' expects a macro identifier as first parameter",
3195 pp_directives[i]);
3196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
3199 ctx = get_ctx(tline->text, &mname);
3200 last = tline;
3201 tline = expand_smacro(tline->next);
3202 last->next = NULL;
3204 t = tline;
3205 while (tok_type_(t, TOK_WHITESPACE))
3206 t = t->next;
3207 /* t should now point to the string */
3208 if (!tok_type_(t, TOK_STRING)) {
3209 nasm_error(ERR_NONFATAL,
3210 "`%s` requires string as second parameter",
3211 pp_directives[i]);
3212 free_tlist(tline);
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3218 * Convert the string to a token stream. Note that smacros
3219 * are stored with the token stream reversed, so we have to
3220 * reverse the output of tokenize().
3222 nasm_unquote_cstr(t->text, i);
3223 macro_start = reverse_tokens(tokenize(t->text));
3226 * We now have a macro name, an implicit parameter count of
3227 * zero, and a numeric token to use as an expansion. Create
3228 * and store an SMacro.
3230 define_smacro(ctx, mname, casesense, 0, macro_start);
3231 free_tlist(tline);
3232 free_tlist(origline);
3233 return DIRECTIVE_FOUND;
3235 case PP_PATHSEARCH:
3237 FILE *fp;
3238 StrList *xsl = NULL;
3239 StrList **xst = &xsl;
3241 casesense = true;
3243 tline = tline->next;
3244 skip_white_(tline);
3245 tline = expand_id(tline);
3246 if (!tline || (tline->type != TOK_ID &&
3247 (tline->type != TOK_PREPROC_ID ||
3248 tline->text[1] != '$'))) {
3249 nasm_error(ERR_NONFATAL,
3250 "`%%pathsearch' expects a macro identifier as first parameter");
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3254 ctx = get_ctx(tline->text, &mname);
3255 last = tline;
3256 tline = expand_smacro(tline->next);
3257 last->next = NULL;
3259 t = tline;
3260 while (tok_type_(t, TOK_WHITESPACE))
3261 t = t->next;
3263 if (!t || (t->type != TOK_STRING &&
3264 t->type != TOK_INTERNAL_STRING)) {
3265 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3266 free_tlist(tline);
3267 free_tlist(origline);
3268 return DIRECTIVE_FOUND; /* but we did _something_ */
3270 if (t->next)
3271 nasm_error(ERR_WARNING|ERR_PASS1,
3272 "trailing garbage after `%%pathsearch' ignored");
3273 p = t->text;
3274 if (t->type != TOK_INTERNAL_STRING)
3275 nasm_unquote(p, NULL);
3277 fp = inc_fopen(p, &xsl, &xst, NULL, true, NF_TEXT);
3278 if (fp) {
3279 p = xsl->str;
3280 fclose(fp); /* Don't actually care about the file */
3282 macro_start = nasm_malloc(sizeof(*macro_start));
3283 macro_start->next = NULL;
3284 macro_start->text = nasm_quote(p, strlen(p));
3285 macro_start->type = TOK_STRING;
3286 macro_start->a.mac = NULL;
3287 if (xsl)
3288 nasm_free(xsl);
3291 * We now have a macro name, an implicit parameter count of
3292 * zero, and a string token to use as an expansion. Create
3293 * and store an SMacro.
3295 define_smacro(ctx, mname, casesense, 0, macro_start);
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3301 case PP_STRLEN:
3302 casesense = true;
3304 tline = tline->next;
3305 skip_white_(tline);
3306 tline = expand_id(tline);
3307 if (!tline || (tline->type != TOK_ID &&
3308 (tline->type != TOK_PREPROC_ID ||
3309 tline->text[1] != '$'))) {
3310 nasm_error(ERR_NONFATAL,
3311 "`%%strlen' expects a macro identifier as first parameter");
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3315 ctx = get_ctx(tline->text, &mname);
3316 last = tline;
3317 tline = expand_smacro(tline->next);
3318 last->next = NULL;
3320 t = tline;
3321 while (tok_type_(t, TOK_WHITESPACE))
3322 t = t->next;
3323 /* t should now point to the string */
3324 if (!tok_type_(t, TOK_STRING)) {
3325 nasm_error(ERR_NONFATAL,
3326 "`%%strlen` requires string as second parameter");
3327 free_tlist(tline);
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3332 macro_start = nasm_malloc(sizeof(*macro_start));
3333 macro_start->next = NULL;
3334 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3335 macro_start->a.mac = NULL;
3338 * We now have a macro name, an implicit parameter count of
3339 * zero, and a numeric token to use as an expansion. Create
3340 * and store an SMacro.
3342 define_smacro(ctx, mname, casesense, 0, macro_start);
3343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
3347 case PP_STRCAT:
3348 casesense = true;
3350 tline = tline->next;
3351 skip_white_(tline);
3352 tline = expand_id(tline);
3353 if (!tline || (tline->type != TOK_ID &&
3354 (tline->type != TOK_PREPROC_ID ||
3355 tline->text[1] != '$'))) {
3356 nasm_error(ERR_NONFATAL,
3357 "`%%strcat' expects a macro identifier as first parameter");
3358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
3361 ctx = get_ctx(tline->text, &mname);
3362 last = tline;
3363 tline = expand_smacro(tline->next);
3364 last->next = NULL;
3366 len = 0;
3367 list_for_each(t, tline) {
3368 switch (t->type) {
3369 case TOK_WHITESPACE:
3370 break;
3371 case TOK_STRING:
3372 len += t->a.len = nasm_unquote(t->text, NULL);
3373 break;
3374 case TOK_OTHER:
3375 if (!strcmp(t->text, ",")) /* permit comma separators */
3376 break;
3377 /* else fall through */
3378 default:
3379 nasm_error(ERR_NONFATAL,
3380 "non-string passed to `%%strcat' (%d)", t->type);
3381 free_tlist(tline);
3382 free_tlist(origline);
3383 return DIRECTIVE_FOUND;
3387 p = pp = nasm_malloc(len);
3388 list_for_each(t, tline) {
3389 if (t->type == TOK_STRING) {
3390 memcpy(p, t->text, t->a.len);
3391 p += t->a.len;
3396 * We now have a macro name, an implicit parameter count of
3397 * zero, and a numeric token to use as an expansion. Create
3398 * and store an SMacro.
3400 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3401 macro_start->text = nasm_quote(pp, len);
3402 nasm_free(pp);
3403 define_smacro(ctx, mname, casesense, 0, macro_start);
3404 free_tlist(tline);
3405 free_tlist(origline);
3406 return DIRECTIVE_FOUND;
3408 case PP_SUBSTR:
3410 int64_t start, count;
3411 size_t len;
3413 casesense = true;
3415 tline = tline->next;
3416 skip_white_(tline);
3417 tline = expand_id(tline);
3418 if (!tline || (tline->type != TOK_ID &&
3419 (tline->type != TOK_PREPROC_ID ||
3420 tline->text[1] != '$'))) {
3421 nasm_error(ERR_NONFATAL,
3422 "`%%substr' expects a macro identifier as first parameter");
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3426 ctx = get_ctx(tline->text, &mname);
3427 last = tline;
3428 tline = expand_smacro(tline->next);
3429 last->next = NULL;
3431 if (tline) /* skip expanded id */
3432 t = tline->next;
3433 while (tok_type_(t, TOK_WHITESPACE))
3434 t = t->next;
3436 /* t should now point to the string */
3437 if (!tok_type_(t, TOK_STRING)) {
3438 nasm_error(ERR_NONFATAL,
3439 "`%%substr` requires string as second parameter");
3440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3445 tt = t->next;
3446 tptr = &tt;
3447 tokval.t_type = TOKEN_INVALID;
3448 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3449 if (!evalresult) {
3450 free_tlist(tline);
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
3453 } else if (!is_simple(evalresult)) {
3454 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3455 free_tlist(tline);
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3459 start = evalresult->value - 1;
3461 while (tok_type_(tt, TOK_WHITESPACE))
3462 tt = tt->next;
3463 if (!tt) {
3464 count = 1; /* Backwards compatibility: one character */
3465 } else {
3466 tokval.t_type = TOKEN_INVALID;
3467 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3468 if (!evalresult) {
3469 free_tlist(tline);
3470 free_tlist(origline);
3471 return DIRECTIVE_FOUND;
3472 } else if (!is_simple(evalresult)) {
3473 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3474 free_tlist(tline);
3475 free_tlist(origline);
3476 return DIRECTIVE_FOUND;
3478 count = evalresult->value;
3481 len = nasm_unquote(t->text, NULL);
3483 /* make start and count being in range */
3484 if (start < 0)
3485 start = 0;
3486 if (count < 0)
3487 count = len + count + 1 - start;
3488 if (start + count > (int64_t)len)
3489 count = len - start;
3490 if (!len || count < 0 || start >=(int64_t)len)
3491 start = -1, count = 0; /* empty string */
3493 macro_start = nasm_malloc(sizeof(*macro_start));
3494 macro_start->next = NULL;
3495 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3496 macro_start->type = TOK_STRING;
3497 macro_start->a.mac = NULL;
3500 * We now have a macro name, an implicit parameter count of
3501 * zero, and a numeric token to use as an expansion. Create
3502 * and store an SMacro.
3504 define_smacro(ctx, mname, casesense, 0, macro_start);
3505 free_tlist(tline);
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3510 case PP_ASSIGN:
3511 case PP_IASSIGN:
3512 casesense = (i == PP_ASSIGN);
3514 tline = tline->next;
3515 skip_white_(tline);
3516 tline = expand_id(tline);
3517 if (!tline || (tline->type != TOK_ID &&
3518 (tline->type != TOK_PREPROC_ID ||
3519 tline->text[1] != '$'))) {
3520 nasm_error(ERR_NONFATAL,
3521 "`%%%sassign' expects a macro identifier",
3522 (i == PP_IASSIGN ? "i" : ""));
3523 free_tlist(origline);
3524 return DIRECTIVE_FOUND;
3526 ctx = get_ctx(tline->text, &mname);
3527 last = tline;
3528 tline = expand_smacro(tline->next);
3529 last->next = NULL;
3531 t = tline;
3532 tptr = &t;
3533 tokval.t_type = TOKEN_INVALID;
3534 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3535 free_tlist(tline);
3536 if (!evalresult) {
3537 free_tlist(origline);
3538 return DIRECTIVE_FOUND;
3541 if (tokval.t_type)
3542 nasm_error(ERR_WARNING|ERR_PASS1,
3543 "trailing garbage after expression ignored");
3545 if (!is_simple(evalresult)) {
3546 nasm_error(ERR_NONFATAL,
3547 "non-constant value given to `%%%sassign'",
3548 (i == PP_IASSIGN ? "i" : ""));
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3553 macro_start = nasm_malloc(sizeof(*macro_start));
3554 macro_start->next = NULL;
3555 make_tok_num(macro_start, reloc_value(evalresult));
3556 macro_start->a.mac = NULL;
3559 * We now have a macro name, an implicit parameter count of
3560 * zero, and a numeric token to use as an expansion. Create
3561 * and store an SMacro.
3563 define_smacro(ctx, mname, casesense, 0, macro_start);
3564 free_tlist(origline);
3565 return DIRECTIVE_FOUND;
3567 case PP_LINE:
3569 * Syntax is `%line nnn[+mmm] [filename]'
3571 tline = tline->next;
3572 skip_white_(tline);
3573 if (!tok_type_(tline, TOK_NUMBER)) {
3574 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3578 k = readnum(tline->text, &err);
3579 m = 1;
3580 tline = tline->next;
3581 if (tok_is_(tline, "+")) {
3582 tline = tline->next;
3583 if (!tok_type_(tline, TOK_NUMBER)) {
3584 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
3585 free_tlist(origline);
3586 return DIRECTIVE_FOUND;
3588 m = readnum(tline->text, &err);
3589 tline = tline->next;
3591 skip_white_(tline);
3592 src_set_linnum(k);
3593 istk->lineinc = m;
3594 if (tline) {
3595 char *fname = detoken(tline, false);
3596 src_set_fname(fname);
3597 nasm_free(fname);
3599 free_tlist(origline);
3600 return DIRECTIVE_FOUND;
3602 default:
3603 nasm_error(ERR_FATAL,
3604 "preprocessor directive `%s' not yet implemented",
3605 pp_directives[i]);
3606 return DIRECTIVE_FOUND;
3611 * Ensure that a macro parameter contains a condition code and
3612 * nothing else. Return the condition code index if so, or -1
3613 * otherwise.
3615 static int find_cc(Token * t)
3617 Token *tt;
3619 if (!t)
3620 return -1; /* Probably a %+ without a space */
3622 skip_white_(t);
3623 if (t->type != TOK_ID)
3624 return -1;
3625 tt = t->next;
3626 skip_white_(tt);
3627 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3628 return -1;
3630 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
3634 * This routines walks over tokens strem and hadnles tokens
3635 * pasting, if @handle_explicit passed then explicit pasting
3636 * term is handled, otherwise -- implicit pastings only.
3638 static bool paste_tokens(Token **head, const struct tokseq_match *m,
3639 size_t mnum, bool handle_explicit)
3641 Token *tok, *next, **prev_next, **prev_nonspace;
3642 bool pasted = false;
3643 char *buf, *p;
3644 size_t len, i;
3647 * The last token before pasting. We need it
3648 * to be able to connect new handled tokens.
3649 * In other words if there were a tokens stream
3651 * A -> B -> C -> D
3653 * and we've joined tokens B and C, the resulting
3654 * stream should be
3656 * A -> BC -> D
3658 tok = *head;
3659 prev_next = NULL;
3661 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3662 prev_nonspace = head;
3663 else
3664 prev_nonspace = NULL;
3666 while (tok && (next = tok->next)) {
3668 switch (tok->type) {
3669 case TOK_WHITESPACE:
3670 /* Zap redundant whitespaces */
3671 while (tok_type_(next, TOK_WHITESPACE))
3672 next = delete_Token(next);
3673 tok->next = next;
3674 break;
3676 case TOK_PASTE:
3677 /* Explicit pasting */
3678 if (!handle_explicit)
3679 break;
3680 next = delete_Token(tok);
3682 while (tok_type_(next, TOK_WHITESPACE))
3683 next = delete_Token(next);
3685 if (!pasted)
3686 pasted = true;
3688 /* Left pasting token is start of line */
3689 if (!prev_nonspace)
3690 nasm_error(ERR_FATAL, "No lvalue found on pasting");
3693 * No ending token, this might happen in two
3694 * cases
3696 * 1) There indeed no right token at all
3697 * 2) There is a bare "%define ID" statement,
3698 * and @ID does expand to whitespace.
3700 * So technically we need to do a grammar analysis
3701 * in another stage of parsing, but for now lets don't
3702 * change the behaviour people used to. Simply allow
3703 * whitespace after paste token.
3705 if (!next) {
3707 * Zap ending space tokens and that's all.
3709 tok = (*prev_nonspace)->next;
3710 while (tok_type_(tok, TOK_WHITESPACE))
3711 tok = delete_Token(tok);
3712 tok = *prev_nonspace;
3713 tok->next = NULL;
3714 break;
3717 tok = *prev_nonspace;
3718 while (tok_type_(tok, TOK_WHITESPACE))
3719 tok = delete_Token(tok);
3720 len = strlen(tok->text);
3721 len += strlen(next->text);
3723 p = buf = nasm_malloc(len + 1);
3724 strcpy(p, tok->text);
3725 p = strchr(p, '\0');
3726 strcpy(p, next->text);
3728 delete_Token(tok);
3730 tok = tokenize(buf);
3731 nasm_free(buf);
3733 *prev_nonspace = tok;
3734 while (tok && tok->next)
3735 tok = tok->next;
3737 tok->next = delete_Token(next);
3739 /* Restart from pasted tokens head */
3740 tok = *prev_nonspace;
3741 break;
3743 default:
3744 /* implicit pasting */
3745 for (i = 0; i < mnum; i++) {
3746 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3747 continue;
3749 len = 0;
3750 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3751 len += strlen(next->text);
3752 next = next->next;
3755 /* No match */
3756 if (tok == next)
3757 break;
3759 len += strlen(tok->text);
3760 p = buf = nasm_malloc(len + 1);
3762 while (tok != next) {
3763 strcpy(p, tok->text);
3764 p = strchr(p, '\0');
3765 tok = delete_Token(tok);
3768 tok = tokenize(buf);
3769 nasm_free(buf);
3771 if (prev_next)
3772 *prev_next = tok;
3773 else
3774 *head = tok;
3777 * Connect pasted into original stream,
3778 * ie A -> new-tokens -> B
3780 while (tok && tok->next)
3781 tok = tok->next;
3782 tok->next = next;
3784 if (!pasted)
3785 pasted = true;
3787 /* Restart from pasted tokens head */
3788 tok = prev_next ? *prev_next : *head;
3791 break;
3794 prev_next = &tok->next;
3796 if (tok->next &&
3797 !tok_type_(tok->next, TOK_WHITESPACE) &&
3798 !tok_type_(tok->next, TOK_PASTE))
3799 prev_nonspace = prev_next;
3801 tok = tok->next;
3804 return pasted;
3808 * expands to a list of tokens from %{x:y}
3810 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3812 Token *t = tline, **tt, *tm, *head;
3813 char *pos;
3814 int fst, lst, j, i;
3816 pos = strchr(tline->text, ':');
3817 nasm_assert(pos);
3819 lst = atoi(pos + 1);
3820 fst = atoi(tline->text + 1);
3823 * only macros params are accounted so
3824 * if someone passes %0 -- we reject such
3825 * value(s)
3827 if (lst == 0 || fst == 0)
3828 goto err;
3830 /* the values should be sane */
3831 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3832 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3833 goto err;
3835 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3836 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3838 /* counted from zero */
3839 fst--, lst--;
3842 * It will be at least one token. Note we
3843 * need to scan params until separator, otherwise
3844 * only first token will be passed.
3846 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3847 head = new_Token(NULL, tm->type, tm->text, 0);
3848 tt = &head->next, tm = tm->next;
3849 while (tok_isnt_(tm, ",")) {
3850 t = new_Token(NULL, tm->type, tm->text, 0);
3851 *tt = t, tt = &t->next, tm = tm->next;
3854 if (fst < lst) {
3855 for (i = fst + 1; i <= lst; i++) {
3856 t = new_Token(NULL, TOK_OTHER, ",", 0);
3857 *tt = t, tt = &t->next;
3858 j = (i + mac->rotate) % mac->nparam;
3859 tm = mac->params[j];
3860 while (tok_isnt_(tm, ",")) {
3861 t = new_Token(NULL, tm->type, tm->text, 0);
3862 *tt = t, tt = &t->next, tm = tm->next;
3865 } else {
3866 for (i = fst - 1; i >= lst; i--) {
3867 t = new_Token(NULL, TOK_OTHER, ",", 0);
3868 *tt = t, tt = &t->next;
3869 j = (i + mac->rotate) % mac->nparam;
3870 tm = mac->params[j];
3871 while (tok_isnt_(tm, ",")) {
3872 t = new_Token(NULL, tm->type, tm->text, 0);
3873 *tt = t, tt = &t->next, tm = tm->next;
3878 *last = tt;
3879 return head;
3881 err:
3882 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3883 &tline->text[1]);
3884 return tline;
3888 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3889 * %-n) and MMacro-local identifiers (%%foo) as well as
3890 * macro indirection (%[...]) and range (%{..:..}).
3892 static Token *expand_mmac_params(Token * tline)
3894 Token *t, *tt, **tail, *thead;
3895 bool changed = false;
3896 char *pos;
3898 tail = &thead;
3899 thead = NULL;
3901 while (tline) {
3902 if (tline->type == TOK_PREPROC_ID &&
3903 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3904 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3905 tline->text[1] == '%')) {
3906 char *text = NULL;
3907 int type = 0, cc; /* type = 0 to placate optimisers */
3908 char tmpbuf[30];
3909 unsigned int n;
3910 int i;
3911 MMacro *mac;
3913 t = tline;
3914 tline = tline->next;
3916 mac = istk->mstk;
3917 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3918 mac = mac->next_active;
3919 if (!mac) {
3920 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3921 } else {
3922 pos = strchr(t->text, ':');
3923 if (!pos) {
3924 switch (t->text[1]) {
3926 * We have to make a substitution of one of the
3927 * forms %1, %-1, %+1, %%foo, %0.
3929 case '0':
3930 type = TOK_NUMBER;
3931 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3932 text = nasm_strdup(tmpbuf);
3933 break;
3934 case '%':
3935 type = TOK_ID;
3936 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3937 mac->unique);
3938 text = nasm_strcat(tmpbuf, t->text + 2);
3939 break;
3940 case '-':
3941 n = atoi(t->text + 2) - 1;
3942 if (n >= mac->nparam)
3943 tt = NULL;
3944 else {
3945 if (mac->nparam > 1)
3946 n = (n + mac->rotate) % mac->nparam;
3947 tt = mac->params[n];
3949 cc = find_cc(tt);
3950 if (cc == -1) {
3951 nasm_error(ERR_NONFATAL,
3952 "macro parameter %d is not a condition code",
3953 n + 1);
3954 text = NULL;
3955 } else {
3956 type = TOK_ID;
3957 if (inverse_ccs[cc] == -1) {
3958 nasm_error(ERR_NONFATAL,
3959 "condition code `%s' is not invertible",
3960 conditions[cc]);
3961 text = NULL;
3962 } else
3963 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3965 break;
3966 case '+':
3967 n = atoi(t->text + 2) - 1;
3968 if (n >= mac->nparam)
3969 tt = NULL;
3970 else {
3971 if (mac->nparam > 1)
3972 n = (n + mac->rotate) % mac->nparam;
3973 tt = mac->params[n];
3975 cc = find_cc(tt);
3976 if (cc == -1) {
3977 nasm_error(ERR_NONFATAL,
3978 "macro parameter %d is not a condition code",
3979 n + 1);
3980 text = NULL;
3981 } else {
3982 type = TOK_ID;
3983 text = nasm_strdup(conditions[cc]);
3985 break;
3986 default:
3987 n = atoi(t->text + 1) - 1;
3988 if (n >= mac->nparam)
3989 tt = NULL;
3990 else {
3991 if (mac->nparam > 1)
3992 n = (n + mac->rotate) % mac->nparam;
3993 tt = mac->params[n];
3995 if (tt) {
3996 for (i = 0; i < mac->paramlen[n]; i++) {
3997 *tail = new_Token(NULL, tt->type, tt->text, 0);
3998 tail = &(*tail)->next;
3999 tt = tt->next;
4002 text = NULL; /* we've done it here */
4003 break;
4005 } else {
4007 * seems we have a parameters range here
4009 Token *head, **last;
4010 head = expand_mmac_params_range(mac, t, &last);
4011 if (head != t) {
4012 *tail = head;
4013 *last = tline;
4014 tline = head;
4015 text = NULL;
4019 if (!text) {
4020 delete_Token(t);
4021 } else {
4022 *tail = t;
4023 tail = &t->next;
4024 t->type = type;
4025 nasm_free(t->text);
4026 t->text = text;
4027 t->a.mac = NULL;
4029 changed = true;
4030 continue;
4031 } else if (tline->type == TOK_INDIRECT) {
4032 t = tline;
4033 tline = tline->next;
4034 tt = tokenize(t->text);
4035 tt = expand_mmac_params(tt);
4036 tt = expand_smacro(tt);
4037 *tail = tt;
4038 while (tt) {
4039 tt->a.mac = NULL; /* Necessary? */
4040 tail = &tt->next;
4041 tt = tt->next;
4043 delete_Token(t);
4044 changed = true;
4045 } else {
4046 t = *tail = tline;
4047 tline = tline->next;
4048 t->a.mac = NULL;
4049 tail = &t->next;
4052 *tail = NULL;
4054 if (changed) {
4055 const struct tokseq_match t[] = {
4057 PP_CONCAT_MASK(TOK_ID) |
4058 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4059 PP_CONCAT_MASK(TOK_ID) |
4060 PP_CONCAT_MASK(TOK_NUMBER) |
4061 PP_CONCAT_MASK(TOK_FLOAT) |
4062 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4065 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4066 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4069 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4072 return thead;
4076 * Expand all single-line macro calls made in the given line.
4077 * Return the expanded version of the line. The original is deemed
4078 * to be destroyed in the process. (In reality we'll just move
4079 * Tokens from input to output a lot of the time, rather than
4080 * actually bothering to destroy and replicate.)
4083 static Token *expand_smacro(Token * tline)
4085 Token *t, *tt, *mstart, **tail, *thead;
4086 SMacro *head = NULL, *m;
4087 Token **params;
4088 int *paramsize;
4089 unsigned int nparam, sparam;
4090 int brackets;
4091 Token *org_tline = tline;
4092 Context *ctx;
4093 const char *mname;
4094 int deadman = DEADMAN_LIMIT;
4095 bool expanded;
4098 * Trick: we should avoid changing the start token pointer since it can
4099 * be contained in "next" field of other token. Because of this
4100 * we allocate a copy of first token and work with it; at the end of
4101 * routine we copy it back
4103 if (org_tline) {
4104 tline = new_Token(org_tline->next, org_tline->type,
4105 org_tline->text, 0);
4106 tline->a.mac = org_tline->a.mac;
4107 nasm_free(org_tline->text);
4108 org_tline->text = NULL;
4111 expanded = true; /* Always expand %+ at least once */
4113 again:
4114 thead = NULL;
4115 tail = &thead;
4117 while (tline) { /* main token loop */
4118 if (!--deadman) {
4119 nasm_error(ERR_NONFATAL, "interminable macro recursion");
4120 goto err;
4123 if ((mname = tline->text)) {
4124 /* if this token is a local macro, look in local context */
4125 if (tline->type == TOK_ID) {
4126 head = (SMacro *)hash_findix(&smacros, mname);
4127 } else if (tline->type == TOK_PREPROC_ID) {
4128 ctx = get_ctx(mname, &mname);
4129 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4130 } else
4131 head = NULL;
4134 * We've hit an identifier. As in is_mmacro below, we first
4135 * check whether the identifier is a single-line macro at
4136 * all, then think about checking for parameters if
4137 * necessary.
4139 list_for_each(m, head)
4140 if (!mstrcmp(m->name, mname, m->casesense))
4141 break;
4142 if (m) {
4143 mstart = tline;
4144 params = NULL;
4145 paramsize = NULL;
4146 if (m->nparam == 0) {
4148 * Simple case: the macro is parameterless. Discard the
4149 * one token that the macro call took, and push the
4150 * expansion back on the to-do stack.
4152 if (!m->expansion) {
4153 if (!strcmp("__FILE__", m->name)) {
4154 const char *file = src_get_fname();
4155 /* nasm_free(tline->text); here? */
4156 tline->text = nasm_quote(file, strlen(file));
4157 tline->type = TOK_STRING;
4158 continue;
4160 if (!strcmp("__LINE__", m->name)) {
4161 nasm_free(tline->text);
4162 make_tok_num(tline, src_get_linnum());
4163 continue;
4165 if (!strcmp("__BITS__", m->name)) {
4166 nasm_free(tline->text);
4167 make_tok_num(tline, globalbits);
4168 continue;
4170 tline = delete_Token(tline);
4171 continue;
4173 } else {
4175 * Complicated case: at least one macro with this name
4176 * exists and takes parameters. We must find the
4177 * parameters in the call, count them, find the SMacro
4178 * that corresponds to that form of the macro call, and
4179 * substitute for the parameters when we expand. What a
4180 * pain.
4182 /*tline = tline->next;
4183 skip_white_(tline); */
4184 do {
4185 t = tline->next;
4186 while (tok_type_(t, TOK_SMAC_END)) {
4187 t->a.mac->in_progress = false;
4188 t->text = NULL;
4189 t = tline->next = delete_Token(t);
4191 tline = t;
4192 } while (tok_type_(tline, TOK_WHITESPACE));
4193 if (!tok_is_(tline, "(")) {
4195 * This macro wasn't called with parameters: ignore
4196 * the call. (Behaviour borrowed from gnu cpp.)
4198 tline = mstart;
4199 m = NULL;
4200 } else {
4201 int paren = 0;
4202 int white = 0;
4203 brackets = 0;
4204 nparam = 0;
4205 sparam = PARAM_DELTA;
4206 params = nasm_malloc(sparam * sizeof(Token *));
4207 params[0] = tline->next;
4208 paramsize = nasm_malloc(sparam * sizeof(int));
4209 paramsize[0] = 0;
4210 while (true) { /* parameter loop */
4212 * For some unusual expansions
4213 * which concatenates function call
4215 t = tline->next;
4216 while (tok_type_(t, TOK_SMAC_END)) {
4217 t->a.mac->in_progress = false;
4218 t->text = NULL;
4219 t = tline->next = delete_Token(t);
4221 tline = t;
4223 if (!tline) {
4224 nasm_error(ERR_NONFATAL,
4225 "macro call expects terminating `)'");
4226 break;
4228 if (tline->type == TOK_WHITESPACE
4229 && brackets <= 0) {
4230 if (paramsize[nparam])
4231 white++;
4232 else
4233 params[nparam] = tline->next;
4234 continue; /* parameter loop */
4236 if (tline->type == TOK_OTHER
4237 && tline->text[1] == 0) {
4238 char ch = tline->text[0];
4239 if (ch == ',' && !paren && brackets <= 0) {
4240 if (++nparam >= sparam) {
4241 sparam += PARAM_DELTA;
4242 params = nasm_realloc(params,
4243 sparam * sizeof(Token *));
4244 paramsize = nasm_realloc(paramsize,
4245 sparam * sizeof(int));
4247 params[nparam] = tline->next;
4248 paramsize[nparam] = 0;
4249 white = 0;
4250 continue; /* parameter loop */
4252 if (ch == '{' &&
4253 (brackets > 0 || (brackets == 0 &&
4254 !paramsize[nparam])))
4256 if (!(brackets++)) {
4257 params[nparam] = tline->next;
4258 continue; /* parameter loop */
4261 if (ch == '}' && brackets > 0)
4262 if (--brackets == 0) {
4263 brackets = -1;
4264 continue; /* parameter loop */
4266 if (ch == '(' && !brackets)
4267 paren++;
4268 if (ch == ')' && brackets <= 0)
4269 if (--paren < 0)
4270 break;
4272 if (brackets < 0) {
4273 brackets = 0;
4274 nasm_error(ERR_NONFATAL, "braces do not "
4275 "enclose all of macro parameter");
4277 paramsize[nparam] += white + 1;
4278 white = 0;
4279 } /* parameter loop */
4280 nparam++;
4281 while (m && (m->nparam != nparam ||
4282 mstrcmp(m->name, mname,
4283 m->casesense)))
4284 m = m->next;
4285 if (!m)
4286 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4287 "macro `%s' exists, "
4288 "but not taking %d parameters",
4289 mstart->text, nparam);
4292 if (m && m->in_progress)
4293 m = NULL;
4294 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4296 * Design question: should we handle !tline, which
4297 * indicates missing ')' here, or expand those
4298 * macros anyway, which requires the (t) test a few
4299 * lines down?
4301 nasm_free(params);
4302 nasm_free(paramsize);
4303 tline = mstart;
4304 } else {
4306 * Expand the macro: we are placed on the last token of the
4307 * call, so that we can easily split the call from the
4308 * following tokens. We also start by pushing an SMAC_END
4309 * token for the cycle removal.
4311 t = tline;
4312 if (t) {
4313 tline = t->next;
4314 t->next = NULL;
4316 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4317 tt->a.mac = m;
4318 m->in_progress = true;
4319 tline = tt;
4320 list_for_each(t, m->expansion) {
4321 if (t->type >= TOK_SMAC_PARAM) {
4322 Token *pcopy = tline, **ptail = &pcopy;
4323 Token *ttt, *pt;
4324 int i;
4326 ttt = params[t->type - TOK_SMAC_PARAM];
4327 i = paramsize[t->type - TOK_SMAC_PARAM];
4328 while (--i >= 0) {
4329 pt = *ptail = new_Token(tline, ttt->type,
4330 ttt->text, 0);
4331 ptail = &pt->next;
4332 ttt = ttt->next;
4334 tline = pcopy;
4335 } else if (t->type == TOK_PREPROC_Q) {
4336 tt = new_Token(tline, TOK_ID, mname, 0);
4337 tline = tt;
4338 } else if (t->type == TOK_PREPROC_QQ) {
4339 tt = new_Token(tline, TOK_ID, m->name, 0);
4340 tline = tt;
4341 } else {
4342 tt = new_Token(tline, t->type, t->text, 0);
4343 tline = tt;
4348 * Having done that, get rid of the macro call, and clean
4349 * up the parameters.
4351 nasm_free(params);
4352 nasm_free(paramsize);
4353 free_tlist(mstart);
4354 expanded = true;
4355 continue; /* main token loop */
4360 if (tline->type == TOK_SMAC_END) {
4361 tline->a.mac->in_progress = false;
4362 tline = delete_Token(tline);
4363 } else {
4364 t = *tail = tline;
4365 tline = tline->next;
4366 t->a.mac = NULL;
4367 t->next = NULL;
4368 tail = &t->next;
4373 * Now scan the entire line and look for successive TOK_IDs that resulted
4374 * after expansion (they can't be produced by tokenize()). The successive
4375 * TOK_IDs should be concatenated.
4376 * Also we look for %+ tokens and concatenate the tokens before and after
4377 * them (without white spaces in between).
4379 if (expanded) {
4380 const struct tokseq_match t[] = {
4382 PP_CONCAT_MASK(TOK_ID) |
4383 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4384 PP_CONCAT_MASK(TOK_ID) |
4385 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4386 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4389 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4391 * If we concatenated something, *and* we had previously expanded
4392 * an actual macro, scan the lines again for macros...
4394 tline = thead;
4395 expanded = false;
4396 goto again;
4400 err:
4401 if (org_tline) {
4402 if (thead) {
4403 *org_tline = *thead;
4404 /* since we just gave text to org_line, don't free it */
4405 thead->text = NULL;
4406 delete_Token(thead);
4407 } else {
4408 /* the expression expanded to empty line;
4409 we can't return NULL for some reasons
4410 we just set the line to a single WHITESPACE token. */
4411 memset(org_tline, 0, sizeof(*org_tline));
4412 org_tline->text = NULL;
4413 org_tline->type = TOK_WHITESPACE;
4415 thead = org_tline;
4418 return thead;
4422 * Similar to expand_smacro but used exclusively with macro identifiers
4423 * right before they are fetched in. The reason is that there can be
4424 * identifiers consisting of several subparts. We consider that if there
4425 * are more than one element forming the name, user wants a expansion,
4426 * otherwise it will be left as-is. Example:
4428 * %define %$abc cde
4430 * the identifier %$abc will be left as-is so that the handler for %define
4431 * will suck it and define the corresponding value. Other case:
4433 * %define _%$abc cde
4435 * In this case user wants name to be expanded *before* %define starts
4436 * working, so we'll expand %$abc into something (if it has a value;
4437 * otherwise it will be left as-is) then concatenate all successive
4438 * PP_IDs into one.
4440 static Token *expand_id(Token * tline)
4442 Token *cur, *oldnext = NULL;
4444 if (!tline || !tline->next)
4445 return tline;
4447 cur = tline;
4448 while (cur->next &&
4449 (cur->next->type == TOK_ID ||
4450 cur->next->type == TOK_PREPROC_ID
4451 || cur->next->type == TOK_NUMBER))
4452 cur = cur->next;
4454 /* If identifier consists of just one token, don't expand */
4455 if (cur == tline)
4456 return tline;
4458 if (cur) {
4459 oldnext = cur->next; /* Detach the tail past identifier */
4460 cur->next = NULL; /* so that expand_smacro stops here */
4463 tline = expand_smacro(tline);
4465 if (cur) {
4466 /* expand_smacro possibly changhed tline; re-scan for EOL */
4467 cur = tline;
4468 while (cur && cur->next)
4469 cur = cur->next;
4470 if (cur)
4471 cur->next = oldnext;
4474 return tline;
4478 * Determine whether the given line constitutes a multi-line macro
4479 * call, and return the MMacro structure called if so. Doesn't have
4480 * to check for an initial label - that's taken care of in
4481 * expand_mmacro - but must check numbers of parameters. Guaranteed
4482 * to be called with tline->type == TOK_ID, so the putative macro
4483 * name is easy to find.
4485 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4487 MMacro *head, *m;
4488 Token **params;
4489 int nparam;
4491 head = (MMacro *) hash_findix(&mmacros, tline->text);
4494 * Efficiency: first we see if any macro exists with the given
4495 * name. If not, we can return NULL immediately. _Then_ we
4496 * count the parameters, and then we look further along the
4497 * list if necessary to find the proper MMacro.
4499 list_for_each(m, head)
4500 if (!mstrcmp(m->name, tline->text, m->casesense))
4501 break;
4502 if (!m)
4503 return NULL;
4506 * OK, we have a potential macro. Count and demarcate the
4507 * parameters.
4509 count_mmac_params(tline->next, &nparam, &params);
4512 * So we know how many parameters we've got. Find the MMacro
4513 * structure that handles this number.
4515 while (m) {
4516 if (m->nparam_min <= nparam
4517 && (m->plus || nparam <= m->nparam_max)) {
4519 * This one is right. Just check if cycle removal
4520 * prohibits us using it before we actually celebrate...
4522 if (m->in_progress > m->max_depth) {
4523 if (m->max_depth > 0) {
4524 nasm_error(ERR_WARNING,
4525 "reached maximum recursion depth of %i",
4526 m->max_depth);
4528 nasm_free(params);
4529 return NULL;
4532 * It's right, and we can use it. Add its default
4533 * parameters to the end of our list if necessary.
4535 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4536 params =
4537 nasm_realloc(params,
4538 ((m->nparam_min + m->ndefs +
4539 1) * sizeof(*params)));
4540 while (nparam < m->nparam_min + m->ndefs) {
4541 params[nparam] = m->defaults[nparam - m->nparam_min];
4542 nparam++;
4546 * If we've gone over the maximum parameter count (and
4547 * we're in Plus mode), ignore parameters beyond
4548 * nparam_max.
4550 if (m->plus && nparam > m->nparam_max)
4551 nparam = m->nparam_max;
4553 * Then terminate the parameter list, and leave.
4555 if (!params) { /* need this special case */
4556 params = nasm_malloc(sizeof(*params));
4557 nparam = 0;
4559 params[nparam] = NULL;
4560 *params_array = params;
4561 return m;
4564 * This one wasn't right: look for the next one with the
4565 * same name.
4567 list_for_each(m, m->next)
4568 if (!mstrcmp(m->name, tline->text, m->casesense))
4569 break;
4573 * After all that, we didn't find one with the right number of
4574 * parameters. Issue a warning, and fail to expand the macro.
4576 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4577 "macro `%s' exists, but not taking %d parameters",
4578 tline->text, nparam);
4579 nasm_free(params);
4580 return NULL;
4585 * Save MMacro invocation specific fields in
4586 * preparation for a recursive macro expansion
4588 static void push_mmacro(MMacro *m)
4590 MMacroInvocation *i;
4592 i = nasm_malloc(sizeof(MMacroInvocation));
4593 i->prev = m->prev;
4594 i->params = m->params;
4595 i->iline = m->iline;
4596 i->nparam = m->nparam;
4597 i->rotate = m->rotate;
4598 i->paramlen = m->paramlen;
4599 i->unique = m->unique;
4600 i->condcnt = m->condcnt;
4601 m->prev = i;
4606 * Restore MMacro invocation specific fields that were
4607 * saved during a previous recursive macro expansion
4609 static void pop_mmacro(MMacro *m)
4611 MMacroInvocation *i;
4613 if (m->prev) {
4614 i = m->prev;
4615 m->prev = i->prev;
4616 m->params = i->params;
4617 m->iline = i->iline;
4618 m->nparam = i->nparam;
4619 m->rotate = i->rotate;
4620 m->paramlen = i->paramlen;
4621 m->unique = i->unique;
4622 m->condcnt = i->condcnt;
4623 nasm_free(i);
4629 * Expand the multi-line macro call made by the given line, if
4630 * there is one to be expanded. If there is, push the expansion on
4631 * istk->expansion and return 1. Otherwise return 0.
4633 static int expand_mmacro(Token * tline)
4635 Token *startline = tline;
4636 Token *label = NULL;
4637 int dont_prepend = 0;
4638 Token **params, *t, *tt;
4639 MMacro *m;
4640 Line *l, *ll;
4641 int i, nparam, *paramlen;
4642 const char *mname;
4644 t = tline;
4645 skip_white_(t);
4646 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4647 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4648 return 0;
4649 m = is_mmacro(t, &params);
4650 if (m) {
4651 mname = t->text;
4652 } else {
4653 Token *last;
4655 * We have an id which isn't a macro call. We'll assume
4656 * it might be a label; we'll also check to see if a
4657 * colon follows it. Then, if there's another id after
4658 * that lot, we'll check it again for macro-hood.
4660 label = last = t;
4661 t = t->next;
4662 if (tok_type_(t, TOK_WHITESPACE))
4663 last = t, t = t->next;
4664 if (tok_is_(t, ":")) {
4665 dont_prepend = 1;
4666 last = t, t = t->next;
4667 if (tok_type_(t, TOK_WHITESPACE))
4668 last = t, t = t->next;
4670 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4671 return 0;
4672 last->next = NULL;
4673 mname = t->text;
4674 tline = t;
4678 * Fix up the parameters: this involves stripping leading and
4679 * trailing whitespace, then stripping braces if they are
4680 * present.
4682 for (nparam = 0; params[nparam]; nparam++) ;
4683 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4685 for (i = 0; params[i]; i++) {
4686 int brace = 0;
4687 int comma = (!m->plus || i < nparam - 1);
4689 t = params[i];
4690 skip_white_(t);
4691 if (tok_is_(t, "{"))
4692 t = t->next, brace++, comma = false;
4693 params[i] = t;
4694 paramlen[i] = 0;
4695 while (t) {
4696 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4697 break; /* ... because we have hit a comma */
4698 if (comma && t->type == TOK_WHITESPACE
4699 && tok_is_(t->next, ","))
4700 break; /* ... or a space then a comma */
4701 if (brace && t->type == TOK_OTHER) {
4702 if (t->text[0] == '{')
4703 brace++; /* ... or a nested opening brace */
4704 else if (t->text[0] == '}')
4705 if (!--brace)
4706 break; /* ... or a brace */
4708 t = t->next;
4709 paramlen[i]++;
4711 if (brace)
4712 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
4716 * OK, we have a MMacro structure together with a set of
4717 * parameters. We must now go through the expansion and push
4718 * copies of each Line on to istk->expansion. Substitution of
4719 * parameter tokens and macro-local tokens doesn't get done
4720 * until the single-line macro substitution process; this is
4721 * because delaying them allows us to change the semantics
4722 * later through %rotate.
4724 * First, push an end marker on to istk->expansion, mark this
4725 * macro as in progress, and set up its invocation-specific
4726 * variables.
4728 ll = nasm_malloc(sizeof(Line));
4729 ll->next = istk->expansion;
4730 ll->finishes = m;
4731 ll->first = NULL;
4732 istk->expansion = ll;
4735 * Save the previous MMacro expansion in the case of
4736 * macro recursion
4738 if (m->max_depth && m->in_progress)
4739 push_mmacro(m);
4741 m->in_progress ++;
4742 m->params = params;
4743 m->iline = tline;
4744 m->nparam = nparam;
4745 m->rotate = 0;
4746 m->paramlen = paramlen;
4747 m->unique = unique++;
4748 m->lineno = 0;
4749 m->condcnt = 0;
4751 m->next_active = istk->mstk;
4752 istk->mstk = m;
4754 list_for_each(l, m->expansion) {
4755 Token **tail;
4757 ll = nasm_malloc(sizeof(Line));
4758 ll->finishes = NULL;
4759 ll->next = istk->expansion;
4760 istk->expansion = ll;
4761 tail = &ll->first;
4763 list_for_each(t, l->first) {
4764 Token *x = t;
4765 switch (t->type) {
4766 case TOK_PREPROC_Q:
4767 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4768 break;
4769 case TOK_PREPROC_QQ:
4770 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4771 break;
4772 case TOK_PREPROC_ID:
4773 if (t->text[1] == '0' && t->text[2] == '0') {
4774 dont_prepend = -1;
4775 x = label;
4776 if (!x)
4777 continue;
4779 /* fall through */
4780 default:
4781 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4782 break;
4784 tail = &tt->next;
4786 *tail = NULL;
4790 * If we had a label, push it on as the first line of
4791 * the macro expansion.
4793 if (label) {
4794 if (dont_prepend < 0)
4795 free_tlist(startline);
4796 else {
4797 ll = nasm_malloc(sizeof(Line));
4798 ll->finishes = NULL;
4799 ll->next = istk->expansion;
4800 istk->expansion = ll;
4801 ll->first = startline;
4802 if (!dont_prepend) {
4803 while (label->next)
4804 label = label->next;
4805 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4810 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4812 return 1;
4816 * This function adds macro names to error messages, and suppresses
4817 * them if necessary.
4819 static void pp_verror(int severity, const char *fmt, va_list arg)
4821 char buff[BUFSIZ];
4822 MMacro *mmac = NULL;
4823 int delta = 0;
4826 * If we're in a dead branch of IF or something like it, ignore the error.
4827 * However, because %else etc are evaluated in the state context
4828 * of the previous branch, errors might get lost:
4829 * %if 0 ... %else trailing garbage ... %endif
4830 * So %else etc should set the ERR_PP_PRECOND flag.
4832 if ((severity & ERR_MASK) < ERR_FATAL &&
4833 istk && istk->conds &&
4834 ((severity & ERR_PP_PRECOND) ?
4835 istk->conds->state == COND_NEVER :
4836 !emitting(istk->conds->state)))
4837 return;
4839 /* get %macro name */
4840 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
4841 mmac = istk->mstk;
4842 /* but %rep blocks should be skipped */
4843 while (mmac && !mmac->name)
4844 mmac = mmac->next_active, delta++;
4847 if (mmac) {
4848 vsnprintf(buff, sizeof(buff), fmt, arg);
4850 nasm_set_verror(real_verror);
4851 nasm_error(severity, "(%s:%d) %s",
4852 mmac->name, mmac->lineno - delta, buff);
4853 nasm_set_verror(pp_verror);
4854 } else {
4855 real_verror(severity, fmt, arg);
4859 static void
4860 pp_reset(char *file, int apass, StrList **deplist)
4862 Token *t;
4864 cstk = NULL;
4865 istk = nasm_malloc(sizeof(Include));
4866 istk->next = NULL;
4867 istk->conds = NULL;
4868 istk->expansion = NULL;
4869 istk->mstk = NULL;
4870 istk->fp = nasm_open_read(file, NF_TEXT);
4871 istk->fname = NULL;
4872 src_set(0, file);
4873 istk->lineinc = 1;
4874 if (!istk->fp)
4875 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
4876 defining = NULL;
4877 nested_mac_count = 0;
4878 nested_rep_count = 0;
4879 init_macros();
4880 unique = 0;
4882 if (tasm_compatible_mode)
4883 pp_add_stdmac(nasm_stdmac_tasm);
4885 pp_add_stdmac(nasm_stdmac_nasm);
4886 pp_add_stdmac(nasm_stdmac_version);
4888 stdmacpos = stdmacros[0];
4889 stdmacnext = &stdmacros[1];
4891 do_predef = true;
4894 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4895 * The caller, however, will also pass in 3 for preprocess-only so
4896 * we can set __PASS__ accordingly.
4898 pass = apass > 2 ? 2 : apass;
4900 dephead = deptail = deplist;
4901 if (deplist) {
4902 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4903 sl->next = NULL;
4904 strcpy(sl->str, file);
4905 *deptail = sl;
4906 deptail = &sl->next;
4910 * Define the __PASS__ macro. This is defined here unlike
4911 * all the other builtins, because it is special -- it varies between
4912 * passes.
4914 t = nasm_malloc(sizeof(*t));
4915 t->next = NULL;
4916 make_tok_num(t, apass);
4917 t->a.mac = NULL;
4918 define_smacro(NULL, "__PASS__", true, 0, t);
4921 static char *pp_getline(void)
4923 char *line;
4924 Token *tline;
4926 real_verror = nasm_set_verror(pp_verror);
4928 while (1) {
4930 * Fetch a tokenized line, either from the macro-expansion
4931 * buffer or from the input file.
4933 tline = NULL;
4934 while (istk->expansion && istk->expansion->finishes) {
4935 Line *l = istk->expansion;
4936 if (!l->finishes->name && l->finishes->in_progress > 1) {
4937 Line *ll;
4940 * This is a macro-end marker for a macro with no
4941 * name, which means it's not really a macro at all
4942 * but a %rep block, and the `in_progress' field is
4943 * more than 1, meaning that we still need to
4944 * repeat. (1 means the natural last repetition; 0
4945 * means termination by %exitrep.) We have
4946 * therefore expanded up to the %endrep, and must
4947 * push the whole block on to the expansion buffer
4948 * again. We don't bother to remove the macro-end
4949 * marker: we'd only have to generate another one
4950 * if we did.
4952 l->finishes->in_progress--;
4953 list_for_each(l, l->finishes->expansion) {
4954 Token *t, *tt, **tail;
4956 ll = nasm_malloc(sizeof(Line));
4957 ll->next = istk->expansion;
4958 ll->finishes = NULL;
4959 ll->first = NULL;
4960 tail = &ll->first;
4962 list_for_each(t, l->first) {
4963 if (t->text || t->type == TOK_WHITESPACE) {
4964 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4965 tail = &tt->next;
4969 istk->expansion = ll;
4971 } else {
4973 * Check whether a `%rep' was started and not ended
4974 * within this macro expansion. This can happen and
4975 * should be detected. It's a fatal error because
4976 * I'm too confused to work out how to recover
4977 * sensibly from it.
4979 if (defining) {
4980 if (defining->name)
4981 nasm_panic(0, "defining with name in expansion");
4982 else if (istk->mstk->name)
4983 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4984 " expansion of macro `%s'",
4985 istk->mstk->name);
4989 * FIXME: investigate the relationship at this point between
4990 * istk->mstk and l->finishes
4993 MMacro *m = istk->mstk;
4994 istk->mstk = m->next_active;
4995 if (m->name) {
4997 * This was a real macro call, not a %rep, and
4998 * therefore the parameter information needs to
4999 * be freed.
5001 if (m->prev) {
5002 pop_mmacro(m);
5003 l->finishes->in_progress --;
5004 } else {
5005 nasm_free(m->params);
5006 free_tlist(m->iline);
5007 nasm_free(m->paramlen);
5008 l->finishes->in_progress = 0;
5010 } else
5011 free_mmacro(m);
5013 istk->expansion = l->next;
5014 nasm_free(l);
5015 lfmt->downlevel(LIST_MACRO);
5018 while (1) { /* until we get a line we can use */
5020 if (istk->expansion) { /* from a macro expansion */
5021 char *p;
5022 Line *l = istk->expansion;
5023 if (istk->mstk)
5024 istk->mstk->lineno++;
5025 tline = l->first;
5026 istk->expansion = l->next;
5027 nasm_free(l);
5028 p = detoken(tline, false);
5029 lfmt->line(LIST_MACRO, p);
5030 nasm_free(p);
5031 break;
5033 line = read_line();
5034 if (line) { /* from the current input file */
5035 line = prepreproc(line);
5036 tline = tokenize(line);
5037 nasm_free(line);
5038 break;
5041 * The current file has ended; work down the istk
5044 Include *i = istk;
5045 fclose(i->fp);
5046 if (i->conds) {
5047 /* nasm_error can't be conditionally suppressed */
5048 nasm_fatal(0,
5049 "expected `%%endif' before end of file");
5051 /* only set line and file name if there's a next node */
5052 if (i->next)
5053 src_set(i->lineno, i->fname);
5054 istk = i->next;
5055 lfmt->downlevel(LIST_INCLUDE);
5056 nasm_free(i);
5057 if (!istk) {
5058 line = NULL;
5059 goto done;
5061 if (istk->expansion && istk->expansion->finishes)
5062 break;
5067 * We must expand MMacro parameters and MMacro-local labels
5068 * _before_ we plunge into directive processing, to cope
5069 * with things like `%define something %1' such as STRUC
5070 * uses. Unless we're _defining_ a MMacro, in which case
5071 * those tokens should be left alone to go into the
5072 * definition; and unless we're in a non-emitting
5073 * condition, in which case we don't want to meddle with
5074 * anything.
5076 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5077 && !(istk->mstk && !istk->mstk->in_progress)) {
5078 tline = expand_mmac_params(tline);
5082 * Check the line to see if it's a preprocessor directive.
5084 if (do_directive(tline) == DIRECTIVE_FOUND) {
5085 continue;
5086 } else if (defining) {
5088 * We're defining a multi-line macro. We emit nothing
5089 * at all, and just
5090 * shove the tokenized line on to the macro definition.
5092 Line *l = nasm_malloc(sizeof(Line));
5093 l->next = defining->expansion;
5094 l->first = tline;
5095 l->finishes = NULL;
5096 defining->expansion = l;
5097 continue;
5098 } else if (istk->conds && !emitting(istk->conds->state)) {
5100 * We're in a non-emitting branch of a condition block.
5101 * Emit nothing at all, not even a blank line: when we
5102 * emerge from the condition we'll give a line-number
5103 * directive so we keep our place correctly.
5105 free_tlist(tline);
5106 continue;
5107 } else if (istk->mstk && !istk->mstk->in_progress) {
5109 * We're in a %rep block which has been terminated, so
5110 * we're walking through to the %endrep without
5111 * emitting anything. Emit nothing at all, not even a
5112 * blank line: when we emerge from the %rep block we'll
5113 * give a line-number directive so we keep our place
5114 * correctly.
5116 free_tlist(tline);
5117 continue;
5118 } else {
5119 tline = expand_smacro(tline);
5120 if (!expand_mmacro(tline)) {
5122 * De-tokenize the line again, and emit it.
5124 line = detoken(tline, true);
5125 free_tlist(tline);
5126 break;
5127 } else {
5128 continue; /* expand_mmacro calls free_tlist */
5133 done:
5134 nasm_set_verror(real_verror);
5135 return line;
5138 static void pp_cleanup(int pass)
5140 real_verror = nasm_set_verror(pp_verror);
5142 if (defining) {
5143 if (defining->name) {
5144 nasm_error(ERR_NONFATAL,
5145 "end of file while still defining macro `%s'",
5146 defining->name);
5147 } else {
5148 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
5151 free_mmacro(defining);
5152 defining = NULL;
5155 nasm_set_verror(real_verror);
5157 while (cstk)
5158 ctx_pop();
5159 free_macros();
5160 while (istk) {
5161 Include *i = istk;
5162 istk = istk->next;
5163 fclose(i->fp);
5164 nasm_free(i);
5166 while (cstk)
5167 ctx_pop();
5168 src_set_fname(NULL);
5169 if (pass == 0) {
5170 IncPath *i;
5171 free_llist(predef);
5172 predef = NULL;
5173 delete_Blocks();
5174 freeTokens = NULL;
5175 while ((i = ipath)) {
5176 ipath = i->next;
5177 if (i->path)
5178 nasm_free(i->path);
5179 nasm_free(i);
5184 static void pp_include_path(char *path)
5186 IncPath *i;
5188 i = nasm_malloc(sizeof(IncPath));
5189 i->path = path ? nasm_strdup(path) : NULL;
5190 i->next = NULL;
5192 if (ipath) {
5193 IncPath *j = ipath;
5194 while (j->next)
5195 j = j->next;
5196 j->next = i;
5197 } else {
5198 ipath = i;
5202 static void pp_pre_include(char *fname)
5204 Token *inc, *space, *name;
5205 Line *l;
5207 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5208 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5209 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5211 l = nasm_malloc(sizeof(Line));
5212 l->next = predef;
5213 l->first = inc;
5214 l->finishes = NULL;
5215 predef = l;
5218 static void pp_pre_define(char *definition)
5220 Token *def, *space;
5221 Line *l;
5222 char *equals;
5224 real_verror = nasm_set_verror(pp_verror);
5226 equals = strchr(definition, '=');
5227 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5228 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5229 if (equals)
5230 *equals = ' ';
5231 space->next = tokenize(definition);
5232 if (equals)
5233 *equals = '=';
5235 if (space->next->type != TOK_PREPROC_ID &&
5236 space->next->type != TOK_ID)
5237 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
5239 l = nasm_malloc(sizeof(Line));
5240 l->next = predef;
5241 l->first = def;
5242 l->finishes = NULL;
5243 predef = l;
5245 nasm_set_verror(real_verror);
5248 static void pp_pre_undefine(char *definition)
5250 Token *def, *space;
5251 Line *l;
5253 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5254 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5255 space->next = tokenize(definition);
5257 l = nasm_malloc(sizeof(Line));
5258 l->next = predef;
5259 l->first = def;
5260 l->finishes = NULL;
5261 predef = l;
5264 static void pp_add_stdmac(macros_t *macros)
5266 macros_t **mp;
5268 /* Find the end of the list and avoid duplicates */
5269 for (mp = stdmacros; *mp; mp++) {
5270 if (*mp == macros)
5271 return; /* Nothing to do */
5274 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5276 *mp = macros;
5279 static void make_tok_num(Token * tok, int64_t val)
5281 char numbuf[32];
5282 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5283 tok->text = nasm_strdup(numbuf);
5284 tok->type = TOK_NUMBER;
5287 static void pp_list_one_macro(MMacro *m, int severity)
5289 if (!m)
5290 return;
5292 /* We need to print the next_active list in reverse order */
5293 pp_list_one_macro(m->next_active, severity);
5295 if (m->name && !m->nolist) {
5296 src_set(m->xline + m->lineno, m->fname);
5297 nasm_error(severity, "... from macro `%s' defined here", m->name);
5301 static void pp_error_list_macros(int severity)
5303 int32_t saved_line;
5304 const char *saved_fname = NULL;
5306 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
5307 src_get(&saved_line, &saved_fname);
5309 if (istk)
5310 pp_list_one_macro(istk->mstk, severity);
5312 src_set(saved_line, saved_fname);
5315 const struct preproc_ops nasmpp = {
5316 pp_reset,
5317 pp_getline,
5318 pp_cleanup,
5319 pp_add_stdmac,
5320 pp_pre_define,
5321 pp_pre_undefine,
5322 pp_pre_include,
5323 pp_include_path,
5324 pp_error_list_macros,