doc/changes.src: document the RDPID instruction
[nasm.git] / preproc.c
blob228a65aa862be235c38c877c21fc8505068f5745
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>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
83 #include "listing.h"
85 typedef struct SMacro SMacro;
86 typedef struct MMacro MMacro;
87 typedef struct MMacroInvocation MMacroInvocation;
88 typedef struct Context Context;
89 typedef struct Token Token;
90 typedef struct Blocks Blocks;
91 typedef struct Line Line;
92 typedef struct Include Include;
93 typedef struct Cond Cond;
94 typedef struct IncPath IncPath;
97 * Note on the storage of both SMacro and MMacros: the hash table
98 * indexes them case-insensitively, and we then have to go through a
99 * linked list of potential case aliases (and, for MMacros, parameter
100 * ranges); this is to preserve the matching semantics of the earlier
101 * code. If the number of case aliases for a specific macro is a
102 * performance issue, you may want to reconsider your coding style.
106 * Store the definition of a single-line macro.
108 struct SMacro {
109 SMacro *next;
110 char *name;
111 bool casesense;
112 bool in_progress;
113 unsigned int nparam;
114 Token *expansion;
118 * Store the definition of a multi-line macro. This is also used to
119 * store the interiors of `%rep...%endrep' blocks, which are
120 * effectively self-re-invoking multi-line macros which simply
121 * don't have a name or bother to appear in the hash tables. %rep
122 * blocks are signified by having a NULL `name' field.
124 * In a MMacro describing a `%rep' block, the `in_progress' field
125 * isn't merely boolean, but gives the number of repeats left to
126 * run.
128 * The `next' field is used for storing MMacros in hash tables; the
129 * `next_active' field is for stacking them on istk entries.
131 * When a MMacro is being expanded, `params', `iline', `nparam',
132 * `paramlen', `rotate' and `unique' are local to the invocation.
134 struct MMacro {
135 MMacro *next;
136 MMacroInvocation *prev; /* previous invocation */
137 char *name;
138 int nparam_min, nparam_max;
139 bool casesense;
140 bool plus; /* is the last parameter greedy? */
141 bool nolist; /* is this macro listing-inhibited? */
142 int64_t in_progress; /* is this macro currently being expanded? */
143 int32_t max_depth; /* maximum number of recursive expansions allowed */
144 Token *dlist; /* All defaults as one list */
145 Token **defaults; /* Parameter default pointers */
146 int ndefs; /* number of default parameters */
147 Line *expansion;
149 MMacro *next_active;
150 MMacro *rep_nest; /* used for nesting %rep */
151 Token **params; /* actual parameters */
152 Token *iline; /* invocation line */
153 unsigned int nparam, rotate;
154 int *paramlen;
155 uint64_t unique;
156 int lineno; /* Current line number on expansion */
157 uint64_t condcnt; /* number of if blocks... */
159 const char *fname; /* File where defined */
160 int32_t xline; /* First line in macro */
164 /* Store the definition of a multi-line macro, as defined in a
165 * previous recursive macro expansion.
167 struct MMacroInvocation {
168 MMacroInvocation *prev; /* previous invocation */
169 Token **params; /* actual parameters */
170 Token *iline; /* invocation line */
171 unsigned int nparam, rotate;
172 int *paramlen;
173 uint64_t unique;
174 uint64_t condcnt;
179 * The context stack is composed of a linked list of these.
181 struct Context {
182 Context *next;
183 char *name;
184 struct hash_table localmac;
185 uint32_t number;
189 * This is the internal form which we break input lines up into.
190 * Typically stored in linked lists.
192 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
193 * necessarily used as-is, but is intended to denote the number of
194 * the substituted parameter. So in the definition
196 * %define a(x,y) ( (x) & ~(y) )
198 * the token representing `x' will have its type changed to
199 * TOK_SMAC_PARAM, but the one representing `y' will be
200 * TOK_SMAC_PARAM+1.
202 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
203 * which doesn't need quotes around it. Used in the pre-include
204 * mechanism as an alternative to trying to find a sensible type of
205 * quote to use on the filename we were passed.
207 enum pp_token_type {
208 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
209 TOK_PREPROC_ID, TOK_STRING,
210 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
211 TOK_INTERNAL_STRING,
212 TOK_PREPROC_Q, TOK_PREPROC_QQ,
213 TOK_PASTE, /* %+ */
214 TOK_INDIRECT, /* %[...] */
215 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
216 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
219 #define PP_CONCAT_MASK(x) (1 << (x))
220 #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
222 struct tokseq_match {
223 int mask_head;
224 int mask_tail;
227 struct Token {
228 Token *next;
229 char *text;
230 union {
231 SMacro *mac; /* associated macro for TOK_SMAC_END */
232 size_t len; /* scratch length field */
233 } a; /* Auxiliary data */
234 enum pp_token_type type;
238 * Multi-line macro definitions are stored as a linked list of
239 * these, which is essentially a container to allow several linked
240 * lists of Tokens.
242 * Note that in this module, linked lists are treated as stacks
243 * wherever possible. For this reason, Lines are _pushed_ on to the
244 * `expansion' field in MMacro structures, so that the linked list,
245 * if walked, would give the macro lines in reverse order; this
246 * means that we can walk the list when expanding a macro, and thus
247 * push the lines on to the `expansion' field in _istk_ in reverse
248 * order (so that when popped back off they are in the right
249 * order). It may seem cockeyed, and it relies on my design having
250 * an even number of steps in, but it works...
252 * Some of these structures, rather than being actual lines, are
253 * markers delimiting the end of the expansion of a given macro.
254 * This is for use in the cycle-tracking and %rep-handling code.
255 * Such structures have `finishes' non-NULL, and `first' NULL. All
256 * others have `finishes' NULL, but `first' may still be NULL if
257 * the line is blank.
259 struct Line {
260 Line *next;
261 MMacro *finishes;
262 Token *first;
266 * To handle an arbitrary level of file inclusion, we maintain a
267 * stack (ie linked list) of these things.
269 struct Include {
270 Include *next;
271 FILE *fp;
272 Cond *conds;
273 Line *expansion;
274 const char *fname;
275 int lineno, lineinc;
276 MMacro *mstk; /* stack of active macros/reps */
280 * Include search path. This is simply a list of strings which get
281 * prepended, in turn, to the name of an include file, in an
282 * attempt to find the file if it's not in the current directory.
284 struct IncPath {
285 IncPath *next;
286 char *path;
290 * Conditional assembly: we maintain a separate stack of these for
291 * each level of file inclusion. (The only reason we keep the
292 * stacks separate is to ensure that a stray `%endif' in a file
293 * included from within the true branch of a `%if' won't terminate
294 * it and cause confusion: instead, rightly, it'll cause an error.)
296 struct Cond {
297 Cond *next;
298 int state;
300 enum {
302 * These states are for use just after %if or %elif: IF_TRUE
303 * means the condition has evaluated to truth so we are
304 * currently emitting, whereas IF_FALSE means we are not
305 * currently emitting but will start doing so if a %else comes
306 * up. In these states, all directives are admissible: %elif,
307 * %else and %endif. (And of course %if.)
309 COND_IF_TRUE, COND_IF_FALSE,
311 * These states come up after a %else: ELSE_TRUE means we're
312 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
313 * any %elif or %else will cause an error.
315 COND_ELSE_TRUE, COND_ELSE_FALSE,
317 * These states mean that we're not emitting now, and also that
318 * nothing until %endif will be emitted at all. COND_DONE is
319 * used when we've had our moment of emission
320 * and have now started seeing %elifs. COND_NEVER is used when
321 * the condition construct in question is contained within a
322 * non-emitting branch of a larger condition construct,
323 * or if there is an error.
325 COND_DONE, COND_NEVER
327 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
330 * These defines are used as the possible return values for do_directive
332 #define NO_DIRECTIVE_FOUND 0
333 #define DIRECTIVE_FOUND 1
336 * This define sets the upper limit for smacro and recursive mmacro
337 * expansions
339 #define DEADMAN_LIMIT (1 << 20)
341 /* max reps */
342 #define REP_LIMIT ((INT64_C(1) << 62))
345 * Condition codes. Note that we use c_ prefix not C_ because C_ is
346 * used in nasm.h for the "real" condition codes. At _this_ level,
347 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
348 * ones, so we need a different enum...
350 static const char * const conditions[] = {
351 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
352 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
353 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
355 enum pp_conds {
356 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
357 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
358 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
359 c_none = -1
361 static const enum pp_conds inverse_ccs[] = {
362 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
363 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,
364 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
368 * Directive names.
370 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
371 static int is_condition(enum preproc_token arg)
373 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
376 /* For TASM compatibility we need to be able to recognise TASM compatible
377 * conditional compilation directives. Using the NASM pre-processor does
378 * not work, so we look for them specifically from the following list and
379 * then jam in the equivalent NASM directive into the input stream.
382 enum {
383 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
384 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
387 static const char * const tasm_directives[] = {
388 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
389 "ifndef", "include", "local"
392 static int StackSize = 4;
393 static char *StackPointer = "ebp";
394 static int ArgOffset = 8;
395 static int LocalOffset = 0;
397 static Context *cstk;
398 static Include *istk;
399 static IncPath *ipath = NULL;
401 static int pass; /* HACK: pass 0 = generate dependencies only */
402 static StrList **dephead, **deptail; /* Dependency list */
404 static uint64_t unique; /* unique identifier numbers */
406 static Line *predef = NULL;
407 static bool do_predef;
410 * The current set of multi-line macros we have defined.
412 static struct hash_table mmacros;
415 * The current set of single-line macros we have defined.
417 static struct hash_table smacros;
420 * The multi-line macro we are currently defining, or the %rep
421 * block we are currently reading, if any.
423 static MMacro *defining;
425 static uint64_t nested_mac_count;
426 static uint64_t nested_rep_count;
429 * The number of macro parameters to allocate space for at a time.
431 #define PARAM_DELTA 16
434 * The standard macro set: defined in macros.c in the array nasm_stdmac.
435 * This gives our position in the macro set, when we're processing it.
437 static macros_t *stdmacpos;
440 * The extra standard macros that come from the object format, if
441 * any.
443 static macros_t *extrastdmac = NULL;
444 static bool any_extrastdmac;
447 * Tokens are allocated in blocks to improve speed
449 #define TOKEN_BLOCKSIZE 4096
450 static Token *freeTokens = NULL;
451 struct Blocks {
452 Blocks *next;
453 void *chunk;
456 static Blocks blocks = { NULL, NULL };
459 * Forward declarations.
461 static Token *expand_mmac_params(Token * tline);
462 static Token *expand_smacro(Token * tline);
463 static Token *expand_id(Token * tline);
464 static Context *get_ctx(const char *name, const char **namep);
465 static void make_tok_num(Token * tok, int64_t val);
466 static void pp_verror(int severity, const char *fmt, va_list ap);
467 static vefunc real_verror;
468 static void *new_Block(size_t size);
469 static void delete_Blocks(void);
470 static Token *new_Token(Token * next, enum pp_token_type type,
471 const char *text, int txtlen);
472 static Token *delete_Token(Token * t);
475 * Macros for safe checking of token pointers, avoid *(NULL)
477 #define tok_type_(x,t) ((x) && (x)->type == (t))
478 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
479 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
480 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
483 * nasm_unquote with error if the string contains NUL characters.
484 * If the string contains NUL characters, issue an error and return
485 * the C len, i.e. truncate at the NUL.
487 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
489 size_t len = nasm_unquote(qstr, NULL);
490 size_t clen = strlen(qstr);
492 if (len != clen)
493 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
494 pp_directives[directive]);
496 return clen;
500 * In-place reverse a list of tokens.
502 static Token *reverse_tokens(Token *t)
504 Token *prev = NULL;
505 Token *next;
507 while (t) {
508 next = t->next;
509 t->next = prev;
510 prev = t;
511 t = next;
514 return prev;
518 * Handle TASM specific directives, which do not contain a % in
519 * front of them. We do it here because I could not find any other
520 * place to do it for the moment, and it is a hack (ideally it would
521 * be nice to be able to use the NASM pre-processor to do it).
523 static char *check_tasm_directive(char *line)
525 int32_t i, j, k, m, len;
526 char *p, *q, *oldline, oldchar;
528 p = nasm_skip_spaces(line);
530 /* Binary search for the directive name */
531 i = -1;
532 j = ARRAY_SIZE(tasm_directives);
533 q = nasm_skip_word(p);
534 len = q - p;
535 if (len) {
536 oldchar = p[len];
537 p[len] = 0;
538 while (j - i > 1) {
539 k = (j + i) / 2;
540 m = nasm_stricmp(p, tasm_directives[k]);
541 if (m == 0) {
542 /* We have found a directive, so jam a % in front of it
543 * so that NASM will then recognise it as one if it's own.
545 p[len] = oldchar;
546 len = strlen(p);
547 oldline = line;
548 line = nasm_malloc(len + 2);
549 line[0] = '%';
550 if (k == TM_IFDIFI) {
552 * NASM does not recognise IFDIFI, so we convert
553 * it to %if 0. This is not used in NASM
554 * compatible code, but does need to parse for the
555 * TASM macro package.
557 strcpy(line + 1, "if 0");
558 } else {
559 memcpy(line + 1, p, len + 1);
561 nasm_free(oldline);
562 return line;
563 } else if (m < 0) {
564 j = k;
565 } else
566 i = k;
568 p[len] = oldchar;
570 return line;
574 * The pre-preprocessing stage... This function translates line
575 * number indications as they emerge from GNU cpp (`# lineno "file"
576 * flags') into NASM preprocessor line number indications (`%line
577 * lineno file').
579 static char *prepreproc(char *line)
581 int lineno, fnlen;
582 char *fname, *oldline;
584 if (line[0] == '#' && line[1] == ' ') {
585 oldline = line;
586 fname = oldline + 2;
587 lineno = atoi(fname);
588 fname += strspn(fname, "0123456789 ");
589 if (*fname == '"')
590 fname++;
591 fnlen = strcspn(fname, "\"");
592 line = nasm_malloc(20 + fnlen);
593 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
594 nasm_free(oldline);
596 if (tasm_compatible_mode)
597 return check_tasm_directive(line);
598 return line;
602 * Free a linked list of tokens.
604 static void free_tlist(Token * list)
606 while (list)
607 list = delete_Token(list);
611 * Free a linked list of lines.
613 static void free_llist(Line * list)
615 Line *l, *tmp;
616 list_for_each_safe(l, tmp, list) {
617 free_tlist(l->first);
618 nasm_free(l);
623 * Free an MMacro
625 static void free_mmacro(MMacro * m)
627 nasm_free(m->name);
628 free_tlist(m->dlist);
629 nasm_free(m->defaults);
630 free_llist(m->expansion);
631 nasm_free(m);
635 * Free all currently defined macros, and free the hash tables
637 static void free_smacro_table(struct hash_table *smt)
639 SMacro *s, *tmp;
640 const char *key;
641 struct hash_tbl_node *it = NULL;
643 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
644 nasm_free((void *)key);
645 list_for_each_safe(s, tmp, s) {
646 nasm_free(s->name);
647 free_tlist(s->expansion);
648 nasm_free(s);
651 hash_free(smt);
654 static void free_mmacro_table(struct hash_table *mmt)
656 MMacro *m, *tmp;
657 const char *key;
658 struct hash_tbl_node *it = NULL;
660 it = NULL;
661 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
662 nasm_free((void *)key);
663 list_for_each_safe(m ,tmp, m)
664 free_mmacro(m);
666 hash_free(mmt);
669 static void free_macros(void)
671 free_smacro_table(&smacros);
672 free_mmacro_table(&mmacros);
676 * Initialize the hash tables
678 static void init_macros(void)
680 hash_init(&smacros, HASH_LARGE);
681 hash_init(&mmacros, HASH_LARGE);
685 * Pop the context stack.
687 static void ctx_pop(void)
689 Context *c = cstk;
691 cstk = cstk->next;
692 free_smacro_table(&c->localmac);
693 nasm_free(c->name);
694 nasm_free(c);
698 * Search for a key in the hash index; adding it if necessary
699 * (in which case we initialize the data pointer to NULL.)
701 static void **
702 hash_findi_add(struct hash_table *hash, const char *str)
704 struct hash_insert hi;
705 void **r;
706 char *strx;
708 r = hash_findi(hash, str, &hi);
709 if (r)
710 return r;
712 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
713 return hash_add(&hi, strx, NULL);
717 * Like hash_findi, but returns the data element rather than a pointer
718 * to it. Used only when not adding a new element, hence no third
719 * argument.
721 static void *
722 hash_findix(struct hash_table *hash, const char *str)
724 void **p;
726 p = hash_findi(hash, str, NULL);
727 return p ? *p : NULL;
731 * read line from standart macros set,
732 * if there no more left -- return NULL
734 static char *line_from_stdmac(void)
736 unsigned char c;
737 const unsigned char *p = stdmacpos;
738 char *line, *q;
739 size_t len = 0;
741 if (!stdmacpos)
742 return NULL;
744 while ((c = *p++)) {
745 if (c >= 0x80)
746 len += pp_directives_len[c - 0x80] + 1;
747 else
748 len++;
751 line = nasm_malloc(len + 1);
752 q = line;
753 while ((c = *stdmacpos++)) {
754 if (c >= 0x80) {
755 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
756 q += pp_directives_len[c - 0x80];
757 *q++ = ' ';
758 } else {
759 *q++ = c;
762 stdmacpos = p;
763 *q = '\0';
765 if (!*stdmacpos) {
766 /* This was the last of the standard macro chain... */
767 stdmacpos = NULL;
768 if (any_extrastdmac) {
769 stdmacpos = extrastdmac;
770 any_extrastdmac = false;
771 } else if (do_predef) {
772 Line *pd, *l;
773 Token *head, **tail, *t;
776 * Nasty hack: here we push the contents of
777 * `predef' on to the top-level expansion stack,
778 * since this is the most convenient way to
779 * implement the pre-include and pre-define
780 * features.
782 list_for_each(pd, predef) {
783 head = NULL;
784 tail = &head;
785 list_for_each(t, pd->first) {
786 *tail = new_Token(NULL, t->type, t->text, 0);
787 tail = &(*tail)->next;
790 l = nasm_malloc(sizeof(Line));
791 l->next = istk->expansion;
792 l->first = head;
793 l->finishes = NULL;
795 istk->expansion = l;
797 do_predef = false;
801 return line;
804 static char *read_line(void)
806 unsigned int size, c, next;
807 const unsigned int delta = 512;
808 const unsigned int pad = 8;
809 unsigned int nr_cont = 0;
810 bool cont = false;
811 char *buffer, *p;
813 /* Standart macros set (predefined) goes first */
814 p = line_from_stdmac();
815 if (p)
816 return p;
818 size = delta;
819 p = buffer = nasm_malloc(size);
821 for (;;) {
822 c = fgetc(istk->fp);
823 if ((int)(c) == EOF) {
824 p[0] = 0;
825 break;
828 switch (c) {
829 case '\r':
830 next = fgetc(istk->fp);
831 if (next != '\n')
832 ungetc(next, istk->fp);
833 if (cont) {
834 cont = false;
835 continue;
837 break;
839 case '\n':
840 if (cont) {
841 cont = false;
842 continue;
844 break;
846 case '\\':
847 next = fgetc(istk->fp);
848 ungetc(next, istk->fp);
849 if (next == '\r' || next == '\n') {
850 cont = true;
851 nr_cont++;
852 continue;
854 break;
857 if (c == '\r' || c == '\n') {
858 *p++ = 0;
859 break;
862 if (p >= (buffer + size - pad)) {
863 buffer = nasm_realloc(buffer, size + delta);
864 p = buffer + size - pad;
865 size += delta;
868 *p++ = (unsigned char)c;
871 if (p == buffer) {
872 nasm_free(buffer);
873 return NULL;
876 src_set_linnum(src_get_linnum() + istk->lineinc +
877 (nr_cont * istk->lineinc));
880 * Handle spurious ^Z, which may be inserted into source files
881 * by some file transfer utilities.
883 buffer[strcspn(buffer, "\032")] = '\0';
885 lfmt->line(LIST_READ, buffer);
887 return buffer;
891 * Tokenize a line of text. This is a very simple process since we
892 * don't need to parse the value out of e.g. numeric tokens: we
893 * simply split one string into many.
895 static Token *tokenize(char *line)
897 char c, *p = line;
898 enum pp_token_type type;
899 Token *list = NULL;
900 Token *t, **tail = &list;
902 while (*line) {
903 p = line;
904 if (*p == '%') {
905 p++;
906 if (*p == '+' && !nasm_isdigit(p[1])) {
907 p++;
908 type = TOK_PASTE;
909 } else if (nasm_isdigit(*p) ||
910 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
911 do {
912 p++;
914 while (nasm_isdigit(*p));
915 type = TOK_PREPROC_ID;
916 } else if (*p == '{') {
917 p++;
918 while (*p) {
919 if (*p == '}')
920 break;
921 p[-1] = *p;
922 p++;
924 if (*p != '}')
925 nasm_error(ERR_WARNING | ERR_PASS1,
926 "unterminated %%{ construct");
927 p[-1] = '\0';
928 if (*p)
929 p++;
930 type = TOK_PREPROC_ID;
931 } else if (*p == '[') {
932 int lvl = 1;
933 line += 2; /* Skip the leading %[ */
934 p++;
935 while (lvl && (c = *p++)) {
936 switch (c) {
937 case ']':
938 lvl--;
939 break;
940 case '%':
941 if (*p == '[')
942 lvl++;
943 break;
944 case '\'':
945 case '\"':
946 case '`':
947 p = nasm_skip_string(p - 1) + 1;
948 break;
949 default:
950 break;
953 p--;
954 if (*p)
955 *p++ = '\0';
956 if (lvl)
957 nasm_error(ERR_NONFATAL|ERR_PASS1,
958 "unterminated %%[ construct");
959 type = TOK_INDIRECT;
960 } else if (*p == '?') {
961 type = TOK_PREPROC_Q; /* %? */
962 p++;
963 if (*p == '?') {
964 type = TOK_PREPROC_QQ; /* %?? */
965 p++;
967 } else if (*p == '!') {
968 type = TOK_PREPROC_ID;
969 p++;
970 if (isidchar(*p)) {
971 do {
972 p++;
974 while (isidchar(*p));
975 } else if (*p == '\'' || *p == '\"' || *p == '`') {
976 p = nasm_skip_string(p);
977 if (*p)
978 p++;
979 else
980 nasm_error(ERR_NONFATAL|ERR_PASS1,
981 "unterminated %%! string");
982 } else {
983 /* %! without string or identifier */
984 type = TOK_OTHER; /* Legacy behavior... */
986 } else if (isidchar(*p) ||
987 ((*p == '!' || *p == '%' || *p == '$') &&
988 isidchar(p[1]))) {
989 do {
990 p++;
992 while (isidchar(*p));
993 type = TOK_PREPROC_ID;
994 } else {
995 type = TOK_OTHER;
996 if (*p == '%')
997 p++;
999 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1000 type = TOK_ID;
1001 p++;
1002 while (*p && isidchar(*p))
1003 p++;
1004 } else if (*p == '\'' || *p == '"' || *p == '`') {
1006 * A string token.
1008 type = TOK_STRING;
1009 p = nasm_skip_string(p);
1011 if (*p) {
1012 p++;
1013 } else {
1014 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
1015 /* Handling unterminated strings by UNV */
1016 /* type = -1; */
1018 } else if (p[0] == '$' && p[1] == '$') {
1019 type = TOK_OTHER; /* TOKEN_BASE */
1020 p += 2;
1021 } else if (isnumstart(*p)) {
1022 bool is_hex = false;
1023 bool is_float = false;
1024 bool has_e = false;
1025 char c, *r;
1028 * A numeric token.
1031 if (*p == '$') {
1032 p++;
1033 is_hex = true;
1036 for (;;) {
1037 c = *p++;
1039 if (!is_hex && (c == 'e' || c == 'E')) {
1040 has_e = true;
1041 if (*p == '+' || *p == '-') {
1043 * e can only be followed by +/- if it is either a
1044 * prefixed hex number or a floating-point number
1046 p++;
1047 is_float = true;
1049 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1050 is_hex = true;
1051 } else if (c == 'P' || c == 'p') {
1052 is_float = true;
1053 if (*p == '+' || *p == '-')
1054 p++;
1055 } else if (isnumchar(c) || c == '_')
1056 ; /* just advance */
1057 else if (c == '.') {
1059 * we need to deal with consequences of the legacy
1060 * parser, like "1.nolist" being two tokens
1061 * (TOK_NUMBER, TOK_ID) here; at least give it
1062 * a shot for now. In the future, we probably need
1063 * a flex-based scanner with proper pattern matching
1064 * to do it as well as it can be done. Nothing in
1065 * the world is going to help the person who wants
1066 * 0x123.p16 interpreted as two tokens, though.
1068 r = p;
1069 while (*r == '_')
1070 r++;
1072 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1073 (!is_hex && (*r == 'e' || *r == 'E')) ||
1074 (*r == 'p' || *r == 'P')) {
1075 p = r;
1076 is_float = true;
1077 } else
1078 break; /* Terminate the token */
1079 } else
1080 break;
1082 p--; /* Point to first character beyond number */
1084 if (p == line+1 && *line == '$') {
1085 type = TOK_OTHER; /* TOKEN_HERE */
1086 } else {
1087 if (has_e && !is_hex) {
1088 /* 1e13 is floating-point, but 1e13h is not */
1089 is_float = true;
1092 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1094 } else if (nasm_isspace(*p)) {
1095 type = TOK_WHITESPACE;
1096 p = nasm_skip_spaces(p);
1098 * Whitespace just before end-of-line is discarded by
1099 * pretending it's a comment; whitespace just before a
1100 * comment gets lumped into the comment.
1102 if (!*p || *p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1107 } else if (*p == ';') {
1108 type = TOK_COMMENT;
1109 while (*p)
1110 p++;
1111 } else {
1113 * Anything else is an operator of some kind. We check
1114 * for all the double-character operators (>>, <<, //,
1115 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1116 * else is a single-character operator.
1118 type = TOK_OTHER;
1119 if ((p[0] == '>' && p[1] == '>') ||
1120 (p[0] == '<' && p[1] == '<') ||
1121 (p[0] == '/' && p[1] == '/') ||
1122 (p[0] == '<' && p[1] == '=') ||
1123 (p[0] == '>' && p[1] == '=') ||
1124 (p[0] == '=' && p[1] == '=') ||
1125 (p[0] == '!' && p[1] == '=') ||
1126 (p[0] == '<' && p[1] == '>') ||
1127 (p[0] == '&' && p[1] == '&') ||
1128 (p[0] == '|' && p[1] == '|') ||
1129 (p[0] == '^' && p[1] == '^')) {
1130 p++;
1132 p++;
1135 /* Handling unterminated string by UNV */
1136 /*if (type == -1)
1138 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1139 t->text[p-line] = *line;
1140 tail = &t->next;
1142 else */
1143 if (type != TOK_COMMENT) {
1144 *tail = t = new_Token(NULL, type, line, p - line);
1145 tail = &t->next;
1147 line = p;
1149 return list;
1153 * this function allocates a new managed block of memory and
1154 * returns a pointer to the block. The managed blocks are
1155 * deleted only all at once by the delete_Blocks function.
1157 static void *new_Block(size_t size)
1159 Blocks *b = &blocks;
1161 /* first, get to the end of the linked list */
1162 while (b->next)
1163 b = b->next;
1164 /* now allocate the requested chunk */
1165 b->chunk = nasm_malloc(size);
1167 /* now allocate a new block for the next request */
1168 b->next = nasm_zalloc(sizeof(Blocks));
1169 return b->chunk;
1173 * this function deletes all managed blocks of memory
1175 static void delete_Blocks(void)
1177 Blocks *a, *b = &blocks;
1180 * keep in mind that the first block, pointed to by blocks
1181 * is a static and not dynamically allocated, so we don't
1182 * free it.
1184 while (b) {
1185 if (b->chunk)
1186 nasm_free(b->chunk);
1187 a = b;
1188 b = b->next;
1189 if (a != &blocks)
1190 nasm_free(a);
1192 memset(&blocks, 0, sizeof(blocks));
1196 * this function creates a new Token and passes a pointer to it
1197 * back to the caller. It sets the type and text elements, and
1198 * also the a.mac and next elements to NULL.
1200 static Token *new_Token(Token * next, enum pp_token_type type,
1201 const char *text, int txtlen)
1203 Token *t;
1204 int i;
1206 if (!freeTokens) {
1207 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1208 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1209 freeTokens[i].next = &freeTokens[i + 1];
1210 freeTokens[i].next = NULL;
1212 t = freeTokens;
1213 freeTokens = t->next;
1214 t->next = next;
1215 t->a.mac = NULL;
1216 t->type = type;
1217 if (type == TOK_WHITESPACE || !text) {
1218 t->text = NULL;
1219 } else {
1220 if (txtlen == 0)
1221 txtlen = strlen(text);
1222 t->text = nasm_malloc(txtlen+1);
1223 memcpy(t->text, text, txtlen);
1224 t->text[txtlen] = '\0';
1226 return t;
1229 static Token *delete_Token(Token * t)
1231 Token *next = t->next;
1232 nasm_free(t->text);
1233 t->next = freeTokens;
1234 freeTokens = t;
1235 return next;
1239 * Convert a line of tokens back into text.
1240 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1241 * will be transformed into ..@ctxnum.xxx
1243 static char *detoken(Token * tlist, bool expand_locals)
1245 Token *t;
1246 char *line, *p;
1247 const char *q;
1248 int len = 0;
1250 list_for_each(t, tlist) {
1251 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1252 char *v;
1253 char *q = t->text;
1255 v = t->text + 2;
1256 if (*v == '\'' || *v == '\"' || *v == '`') {
1257 size_t len = nasm_unquote(v, NULL);
1258 size_t clen = strlen(v);
1260 if (len != clen) {
1261 nasm_error(ERR_NONFATAL | ERR_PASS1,
1262 "NUL character in %%! string");
1263 v = NULL;
1267 if (v) {
1268 char *p = getenv(v);
1269 if (!p) {
1270 nasm_error(ERR_NONFATAL | ERR_PASS1,
1271 "nonexistent environment variable `%s'", v);
1273 * FIXME We better should investigate if accessing
1274 * ->text[1] without ->text[0] is safe enough.
1276 t->text = nasm_zalloc(2);
1277 } else
1278 t->text = nasm_strdup(p);
1280 nasm_free(q);
1283 /* Expand local macros here and not during preprocessing */
1284 if (expand_locals &&
1285 t->type == TOK_PREPROC_ID && t->text &&
1286 t->text[0] == '%' && t->text[1] == '$') {
1287 const char *q;
1288 char *p;
1289 Context *ctx = get_ctx(t->text, &q);
1290 if (ctx) {
1291 char buffer[40];
1292 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1293 p = nasm_strcat(buffer, q);
1294 nasm_free(t->text);
1295 t->text = p;
1298 if (t->type == TOK_WHITESPACE)
1299 len++;
1300 else if (t->text)
1301 len += strlen(t->text);
1304 p = line = nasm_malloc(len + 1);
1306 list_for_each(t, tlist) {
1307 if (t->type == TOK_WHITESPACE) {
1308 *p++ = ' ';
1309 } else if (t->text) {
1310 q = t->text;
1311 while (*q)
1312 *p++ = *q++;
1315 *p = '\0';
1317 return line;
1321 * A scanner, suitable for use by the expression evaluator, which
1322 * operates on a line of Tokens. Expects a pointer to a pointer to
1323 * the first token in the line to be passed in as its private_data
1324 * field.
1326 * FIX: This really needs to be unified with stdscan.
1328 static int ppscan(void *private_data, struct tokenval *tokval)
1330 Token **tlineptr = private_data;
1331 Token *tline;
1332 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1334 do {
1335 tline = *tlineptr;
1336 *tlineptr = tline ? tline->next : NULL;
1337 } while (tline && (tline->type == TOK_WHITESPACE ||
1338 tline->type == TOK_COMMENT));
1340 if (!tline)
1341 return tokval->t_type = TOKEN_EOS;
1343 tokval->t_charptr = tline->text;
1345 if (tline->text[0] == '$' && !tline->text[1])
1346 return tokval->t_type = TOKEN_HERE;
1347 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1348 return tokval->t_type = TOKEN_BASE;
1350 if (tline->type == TOK_ID) {
1351 p = tokval->t_charptr = tline->text;
1352 if (p[0] == '$') {
1353 tokval->t_charptr++;
1354 return tokval->t_type = TOKEN_ID;
1357 for (r = p, s = ourcopy; *r; r++) {
1358 if (r >= p+MAX_KEYWORD)
1359 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1360 *s++ = nasm_tolower(*r);
1362 *s = '\0';
1363 /* right, so we have an identifier sitting in temp storage. now,
1364 * is it actually a register or instruction name, or what? */
1365 return nasm_token_hash(ourcopy, tokval);
1368 if (tline->type == TOK_NUMBER) {
1369 bool rn_error;
1370 tokval->t_integer = readnum(tline->text, &rn_error);
1371 tokval->t_charptr = tline->text;
1372 if (rn_error)
1373 return tokval->t_type = TOKEN_ERRNUM;
1374 else
1375 return tokval->t_type = TOKEN_NUM;
1378 if (tline->type == TOK_FLOAT) {
1379 return tokval->t_type = TOKEN_FLOAT;
1382 if (tline->type == TOK_STRING) {
1383 char bq, *ep;
1385 bq = tline->text[0];
1386 tokval->t_charptr = tline->text;
1387 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1389 if (ep[0] != bq || ep[1] != '\0')
1390 return tokval->t_type = TOKEN_ERRSTR;
1391 else
1392 return tokval->t_type = TOKEN_STR;
1395 if (tline->type == TOK_OTHER) {
1396 if (!strcmp(tline->text, "<<"))
1397 return tokval->t_type = TOKEN_SHL;
1398 if (!strcmp(tline->text, ">>"))
1399 return tokval->t_type = TOKEN_SHR;
1400 if (!strcmp(tline->text, "//"))
1401 return tokval->t_type = TOKEN_SDIV;
1402 if (!strcmp(tline->text, "%%"))
1403 return tokval->t_type = TOKEN_SMOD;
1404 if (!strcmp(tline->text, "=="))
1405 return tokval->t_type = TOKEN_EQ;
1406 if (!strcmp(tline->text, "<>"))
1407 return tokval->t_type = TOKEN_NE;
1408 if (!strcmp(tline->text, "!="))
1409 return tokval->t_type = TOKEN_NE;
1410 if (!strcmp(tline->text, "<="))
1411 return tokval->t_type = TOKEN_LE;
1412 if (!strcmp(tline->text, ">="))
1413 return tokval->t_type = TOKEN_GE;
1414 if (!strcmp(tline->text, "&&"))
1415 return tokval->t_type = TOKEN_DBL_AND;
1416 if (!strcmp(tline->text, "^^"))
1417 return tokval->t_type = TOKEN_DBL_XOR;
1418 if (!strcmp(tline->text, "||"))
1419 return tokval->t_type = TOKEN_DBL_OR;
1423 * We have no other options: just return the first character of
1424 * the token text.
1426 return tokval->t_type = tline->text[0];
1430 * Compare a string to the name of an existing macro; this is a
1431 * simple wrapper which calls either strcmp or nasm_stricmp
1432 * depending on the value of the `casesense' parameter.
1434 static int mstrcmp(const char *p, const char *q, bool casesense)
1436 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1440 * Compare a string to the name of an existing macro; this is a
1441 * simple wrapper which calls either strcmp or nasm_stricmp
1442 * depending on the value of the `casesense' parameter.
1444 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1446 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1450 * Return the Context structure associated with a %$ token. Return
1451 * NULL, having _already_ reported an error condition, if the
1452 * context stack isn't deep enough for the supplied number of $
1453 * signs.
1455 * If "namep" is non-NULL, set it to the pointer to the macro name
1456 * tail, i.e. the part beyond %$...
1458 static Context *get_ctx(const char *name, const char **namep)
1460 Context *ctx;
1461 int i;
1463 if (namep)
1464 *namep = name;
1466 if (!name || name[0] != '%' || name[1] != '$')
1467 return NULL;
1469 if (!cstk) {
1470 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
1471 return NULL;
1474 name += 2;
1475 ctx = cstk;
1476 i = 0;
1477 while (ctx && *name == '$') {
1478 name++;
1479 i++;
1480 ctx = ctx->next;
1482 if (!ctx) {
1483 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
1484 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1485 return NULL;
1488 if (namep)
1489 *namep = name;
1491 return ctx;
1495 * Check to see if a file is already in a string list
1497 static bool in_list(const StrList *list, const char *str)
1499 while (list) {
1500 if (!strcmp(list->str, str))
1501 return true;
1502 list = list->next;
1504 return false;
1508 * Open an include file. This routine must always return a valid
1509 * file pointer if it returns - it's responsible for throwing an
1510 * ERR_FATAL and bombing out completely if not. It should also try
1511 * the include path one by one until it finds the file or reaches
1512 * the end of the path.
1514 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1515 char **found_path, bool missing_ok, const char *mode)
1517 FILE *fp;
1518 char *prefix = "";
1519 IncPath *ip = ipath;
1520 int len = strlen(file);
1521 size_t prefix_len = 0;
1522 StrList *sl;
1523 size_t path_len;
1525 while (1) {
1526 path_len = prefix_len + len + 1;
1528 sl = nasm_malloc(path_len + sizeof sl->next);
1529 memcpy(sl->str, prefix, prefix_len);
1530 memcpy(sl->str+prefix_len, file, len+1);
1532 if (found_path != NULL) {
1533 *found_path = nasm_malloc(path_len);
1534 memcpy(*found_path, sl->str, path_len);
1537 fp = fopen(sl->str, mode);
1538 if (fp && dhead && !in_list(*dhead, sl->str)) {
1539 sl->next = NULL;
1540 **dtail = sl;
1541 *dtail = &sl->next;
1542 } else {
1543 nasm_free(sl);
1545 if (fp)
1546 return fp;
1548 if (found_path != NULL && *found_path != NULL) {
1549 nasm_free(*found_path);
1550 *found_path = NULL;
1553 if (!ip) {
1554 if (!missing_ok)
1555 break;
1556 prefix = NULL;
1557 } else {
1558 prefix = ip->path;
1559 ip = ip->next;
1561 if (prefix) {
1562 prefix_len = strlen(prefix);
1563 } else {
1564 /* -MG given and file not found */
1565 if (dhead && !in_list(*dhead, file)) {
1566 sl = nasm_malloc(len+1+sizeof sl->next);
1567 sl->next = NULL;
1568 strcpy(sl->str, file);
1569 **dtail = sl;
1570 *dtail = &sl->next;
1572 return NULL;
1576 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
1577 return NULL;
1581 * Opens an include or input file. Public version, for use by modules
1582 * that get a file:lineno pair and need to look at the file again
1583 * (e.g. the CodeView debug backend). Returns NULL on failure.
1585 FILE *pp_input_fopen(const char *filename, const char *mode)
1587 FILE *fp;
1588 StrList *xsl = NULL;
1589 StrList **xst = &xsl;
1591 fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode);
1592 if (xsl)
1593 nasm_free(xsl);
1594 return fp;
1598 * Determine if we should warn on defining a single-line macro of
1599 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1600 * return true if _any_ single-line macro of that name is defined.
1601 * Otherwise, will return true if a single-line macro with either
1602 * `nparam' or no parameters is defined.
1604 * If a macro with precisely the right number of parameters is
1605 * defined, or nparam is -1, the address of the definition structure
1606 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1607 * is NULL, no action will be taken regarding its contents, and no
1608 * error will occur.
1610 * Note that this is also called with nparam zero to resolve
1611 * `ifdef'.
1613 * If you already know which context macro belongs to, you can pass
1614 * the context pointer as first parameter; if you won't but name begins
1615 * with %$ the context will be automatically computed. If all_contexts
1616 * is true, macro will be searched in outer contexts as well.
1618 static bool
1619 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1620 bool nocase)
1622 struct hash_table *smtbl;
1623 SMacro *m;
1625 if (ctx) {
1626 smtbl = &ctx->localmac;
1627 } else if (name[0] == '%' && name[1] == '$') {
1628 if (cstk)
1629 ctx = get_ctx(name, &name);
1630 if (!ctx)
1631 return false; /* got to return _something_ */
1632 smtbl = &ctx->localmac;
1633 } else {
1634 smtbl = &smacros;
1636 m = (SMacro *) hash_findix(smtbl, name);
1638 while (m) {
1639 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1640 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1641 if (defn) {
1642 if (nparam == (int) m->nparam || nparam == -1)
1643 *defn = m;
1644 else
1645 *defn = NULL;
1647 return true;
1649 m = m->next;
1652 return false;
1656 * Count and mark off the parameters in a multi-line macro call.
1657 * This is called both from within the multi-line macro expansion
1658 * code, and also to mark off the default parameters when provided
1659 * in a %macro definition line.
1661 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1663 int paramsize, brace;
1665 *nparam = paramsize = 0;
1666 *params = NULL;
1667 while (t) {
1668 /* +1: we need space for the final NULL */
1669 if (*nparam+1 >= paramsize) {
1670 paramsize += PARAM_DELTA;
1671 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1673 skip_white_(t);
1674 brace = 0;
1675 if (tok_is_(t, "{"))
1676 brace++;
1677 (*params)[(*nparam)++] = t;
1678 if (brace) {
1679 while (brace && (t = t->next) != NULL) {
1680 if (tok_is_(t, "{"))
1681 brace++;
1682 else if (tok_is_(t, "}"))
1683 brace--;
1686 if (t) {
1688 * Now we've found the closing brace, look further
1689 * for the comma.
1691 t = t->next;
1692 skip_white_(t);
1693 if (tok_isnt_(t, ",")) {
1694 nasm_error(ERR_NONFATAL,
1695 "braces do not enclose all of macro parameter");
1696 while (tok_isnt_(t, ","))
1697 t = t->next;
1700 } else {
1701 while (tok_isnt_(t, ","))
1702 t = t->next;
1704 if (t) { /* got a comma/brace */
1705 t = t->next; /* eat the comma */
1711 * Determine whether one of the various `if' conditions is true or
1712 * not.
1714 * We must free the tline we get passed.
1716 static bool if_condition(Token * tline, enum preproc_token ct)
1718 enum pp_conditional i = PP_COND(ct);
1719 bool j;
1720 Token *t, *tt, **tptr, *origline;
1721 struct tokenval tokval;
1722 expr *evalresult;
1723 enum pp_token_type needtype;
1724 char *p;
1726 origline = tline;
1728 switch (i) {
1729 case PPC_IFCTX:
1730 j = false; /* have we matched yet? */
1731 while (true) {
1732 skip_white_(tline);
1733 if (!tline)
1734 break;
1735 if (tline->type != TOK_ID) {
1736 nasm_error(ERR_NONFATAL,
1737 "`%s' expects context identifiers", pp_directives[ct]);
1738 free_tlist(origline);
1739 return -1;
1741 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1742 j = true;
1743 tline = tline->next;
1745 break;
1747 case PPC_IFDEF:
1748 j = false; /* have we matched yet? */
1749 while (tline) {
1750 skip_white_(tline);
1751 if (!tline || (tline->type != TOK_ID &&
1752 (tline->type != TOK_PREPROC_ID ||
1753 tline->text[1] != '$'))) {
1754 nasm_error(ERR_NONFATAL,
1755 "`%s' expects macro identifiers", pp_directives[ct]);
1756 goto fail;
1758 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1759 j = true;
1760 tline = tline->next;
1762 break;
1764 case PPC_IFENV:
1765 tline = expand_smacro(tline);
1766 j = false; /* have we matched yet? */
1767 while (tline) {
1768 skip_white_(tline);
1769 if (!tline || (tline->type != TOK_ID &&
1770 tline->type != TOK_STRING &&
1771 (tline->type != TOK_PREPROC_ID ||
1772 tline->text[1] != '!'))) {
1773 nasm_error(ERR_NONFATAL,
1774 "`%s' expects environment variable names",
1775 pp_directives[ct]);
1776 goto fail;
1778 p = tline->text;
1779 if (tline->type == TOK_PREPROC_ID)
1780 p += 2; /* Skip leading %! */
1781 if (*p == '\'' || *p == '\"' || *p == '`')
1782 nasm_unquote_cstr(p, ct);
1783 if (getenv(p))
1784 j = true;
1785 tline = tline->next;
1787 break;
1789 case PPC_IFIDN:
1790 case PPC_IFIDNI:
1791 tline = expand_smacro(tline);
1792 t = tt = tline;
1793 while (tok_isnt_(tt, ","))
1794 tt = tt->next;
1795 if (!tt) {
1796 nasm_error(ERR_NONFATAL,
1797 "`%s' expects two comma-separated arguments",
1798 pp_directives[ct]);
1799 goto fail;
1801 tt = tt->next;
1802 j = true; /* assume equality unless proved not */
1803 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1804 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1805 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
1806 pp_directives[ct]);
1807 goto fail;
1809 if (t->type == TOK_WHITESPACE) {
1810 t = t->next;
1811 continue;
1813 if (tt->type == TOK_WHITESPACE) {
1814 tt = tt->next;
1815 continue;
1817 if (tt->type != t->type) {
1818 j = false; /* found mismatching tokens */
1819 break;
1821 /* When comparing strings, need to unquote them first */
1822 if (t->type == TOK_STRING) {
1823 size_t l1 = nasm_unquote(t->text, NULL);
1824 size_t l2 = nasm_unquote(tt->text, NULL);
1826 if (l1 != l2) {
1827 j = false;
1828 break;
1830 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1831 j = false;
1832 break;
1834 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1835 j = false; /* found mismatching tokens */
1836 break;
1839 t = t->next;
1840 tt = tt->next;
1842 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1843 j = false; /* trailing gunk on one end or other */
1844 break;
1846 case PPC_IFMACRO:
1848 bool found = false;
1849 MMacro searching, *mmac;
1851 skip_white_(tline);
1852 tline = expand_id(tline);
1853 if (!tok_type_(tline, TOK_ID)) {
1854 nasm_error(ERR_NONFATAL,
1855 "`%s' expects a macro name", pp_directives[ct]);
1856 goto fail;
1858 searching.name = nasm_strdup(tline->text);
1859 searching.casesense = true;
1860 searching.plus = false;
1861 searching.nolist = false;
1862 searching.in_progress = 0;
1863 searching.max_depth = 0;
1864 searching.rep_nest = NULL;
1865 searching.nparam_min = 0;
1866 searching.nparam_max = INT_MAX;
1867 tline = expand_smacro(tline->next);
1868 skip_white_(tline);
1869 if (!tline) {
1870 } else if (!tok_type_(tline, TOK_NUMBER)) {
1871 nasm_error(ERR_NONFATAL,
1872 "`%s' expects a parameter count or nothing",
1873 pp_directives[ct]);
1874 } else {
1875 searching.nparam_min = searching.nparam_max =
1876 readnum(tline->text, &j);
1877 if (j)
1878 nasm_error(ERR_NONFATAL,
1879 "unable to parse parameter count `%s'",
1880 tline->text);
1882 if (tline && tok_is_(tline->next, "-")) {
1883 tline = tline->next->next;
1884 if (tok_is_(tline, "*"))
1885 searching.nparam_max = INT_MAX;
1886 else if (!tok_type_(tline, TOK_NUMBER))
1887 nasm_error(ERR_NONFATAL,
1888 "`%s' expects a parameter count after `-'",
1889 pp_directives[ct]);
1890 else {
1891 searching.nparam_max = readnum(tline->text, &j);
1892 if (j)
1893 nasm_error(ERR_NONFATAL,
1894 "unable to parse parameter count `%s'",
1895 tline->text);
1896 if (searching.nparam_min > searching.nparam_max)
1897 nasm_error(ERR_NONFATAL,
1898 "minimum parameter count exceeds maximum");
1901 if (tline && tok_is_(tline->next, "+")) {
1902 tline = tline->next;
1903 searching.plus = true;
1905 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1906 while (mmac) {
1907 if (!strcmp(mmac->name, searching.name) &&
1908 (mmac->nparam_min <= searching.nparam_max
1909 || searching.plus)
1910 && (searching.nparam_min <= mmac->nparam_max
1911 || mmac->plus)) {
1912 found = true;
1913 break;
1915 mmac = mmac->next;
1917 if (tline && tline->next)
1918 nasm_error(ERR_WARNING|ERR_PASS1,
1919 "trailing garbage after %%ifmacro ignored");
1920 nasm_free(searching.name);
1921 j = found;
1922 break;
1925 case PPC_IFID:
1926 needtype = TOK_ID;
1927 goto iftype;
1928 case PPC_IFNUM:
1929 needtype = TOK_NUMBER;
1930 goto iftype;
1931 case PPC_IFSTR:
1932 needtype = TOK_STRING;
1933 goto iftype;
1935 iftype:
1936 t = tline = expand_smacro(tline);
1938 while (tok_type_(t, TOK_WHITESPACE) ||
1939 (needtype == TOK_NUMBER &&
1940 tok_type_(t, TOK_OTHER) &&
1941 (t->text[0] == '-' || t->text[0] == '+') &&
1942 !t->text[1]))
1943 t = t->next;
1945 j = tok_type_(t, needtype);
1946 break;
1948 case PPC_IFTOKEN:
1949 t = tline = expand_smacro(tline);
1950 while (tok_type_(t, TOK_WHITESPACE))
1951 t = t->next;
1953 j = false;
1954 if (t) {
1955 t = t->next; /* Skip the actual token */
1956 while (tok_type_(t, TOK_WHITESPACE))
1957 t = t->next;
1958 j = !t; /* Should be nothing left */
1960 break;
1962 case PPC_IFEMPTY:
1963 t = tline = expand_smacro(tline);
1964 while (tok_type_(t, TOK_WHITESPACE))
1965 t = t->next;
1967 j = !t; /* Should be empty */
1968 break;
1970 case PPC_IF:
1971 t = tline = expand_smacro(tline);
1972 tptr = &t;
1973 tokval.t_type = TOKEN_INVALID;
1974 evalresult = evaluate(ppscan, tptr, &tokval,
1975 NULL, pass | CRITICAL, NULL);
1976 if (!evalresult)
1977 return -1;
1978 if (tokval.t_type)
1979 nasm_error(ERR_WARNING|ERR_PASS1,
1980 "trailing garbage after expression ignored");
1981 if (!is_simple(evalresult)) {
1982 nasm_error(ERR_NONFATAL,
1983 "non-constant value given to `%s'", pp_directives[ct]);
1984 goto fail;
1986 j = reloc_value(evalresult) != 0;
1987 break;
1989 default:
1990 nasm_error(ERR_FATAL,
1991 "preprocessor directive `%s' not yet implemented",
1992 pp_directives[ct]);
1993 goto fail;
1996 free_tlist(origline);
1997 return j ^ PP_NEGATIVE(ct);
1999 fail:
2000 free_tlist(origline);
2001 return -1;
2005 * Common code for defining an smacro
2007 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2008 int nparam, Token *expansion)
2010 SMacro *smac, **smhead;
2011 struct hash_table *smtbl;
2013 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2014 if (!smac) {
2015 nasm_error(ERR_WARNING|ERR_PASS1,
2016 "single-line macro `%s' defined both with and"
2017 " without parameters", mname);
2019 * Some instances of the old code considered this a failure,
2020 * some others didn't. What is the right thing to do here?
2022 free_tlist(expansion);
2023 return false; /* Failure */
2024 } else {
2026 * We're redefining, so we have to take over an
2027 * existing SMacro structure. This means freeing
2028 * what was already in it.
2030 nasm_free(smac->name);
2031 free_tlist(smac->expansion);
2033 } else {
2034 smtbl = ctx ? &ctx->localmac : &smacros;
2035 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2036 smac = nasm_malloc(sizeof(SMacro));
2037 smac->next = *smhead;
2038 *smhead = smac;
2040 smac->name = nasm_strdup(mname);
2041 smac->casesense = casesense;
2042 smac->nparam = nparam;
2043 smac->expansion = expansion;
2044 smac->in_progress = false;
2045 return true; /* Success */
2049 * Undefine an smacro
2051 static void undef_smacro(Context *ctx, const char *mname)
2053 SMacro **smhead, *s, **sp;
2054 struct hash_table *smtbl;
2056 smtbl = ctx ? &ctx->localmac : &smacros;
2057 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2059 if (smhead) {
2061 * We now have a macro name... go hunt for it.
2063 sp = smhead;
2064 while ((s = *sp) != NULL) {
2065 if (!mstrcmp(s->name, mname, s->casesense)) {
2066 *sp = s->next;
2067 nasm_free(s->name);
2068 free_tlist(s->expansion);
2069 nasm_free(s);
2070 } else {
2071 sp = &s->next;
2078 * Parse a mmacro specification.
2080 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2082 bool err;
2084 tline = tline->next;
2085 skip_white_(tline);
2086 tline = expand_id(tline);
2087 if (!tok_type_(tline, TOK_ID)) {
2088 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2089 return false;
2092 def->prev = NULL;
2093 def->name = nasm_strdup(tline->text);
2094 def->plus = false;
2095 def->nolist = false;
2096 def->in_progress = 0;
2097 def->rep_nest = NULL;
2098 def->nparam_min = 0;
2099 def->nparam_max = 0;
2101 tline = expand_smacro(tline->next);
2102 skip_white_(tline);
2103 if (!tok_type_(tline, TOK_NUMBER)) {
2104 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2105 } else {
2106 def->nparam_min = def->nparam_max =
2107 readnum(tline->text, &err);
2108 if (err)
2109 nasm_error(ERR_NONFATAL,
2110 "unable to parse parameter count `%s'", tline->text);
2112 if (tline && tok_is_(tline->next, "-")) {
2113 tline = tline->next->next;
2114 if (tok_is_(tline, "*")) {
2115 def->nparam_max = INT_MAX;
2116 } else if (!tok_type_(tline, TOK_NUMBER)) {
2117 nasm_error(ERR_NONFATAL,
2118 "`%s' expects a parameter count after `-'", directive);
2119 } else {
2120 def->nparam_max = readnum(tline->text, &err);
2121 if (err) {
2122 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2123 tline->text);
2125 if (def->nparam_min > def->nparam_max) {
2126 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2130 if (tline && tok_is_(tline->next, "+")) {
2131 tline = tline->next;
2132 def->plus = true;
2134 if (tline && tok_type_(tline->next, TOK_ID) &&
2135 !nasm_stricmp(tline->next->text, ".nolist")) {
2136 tline = tline->next;
2137 def->nolist = true;
2141 * Handle default parameters.
2143 if (tline && tline->next) {
2144 def->dlist = tline->next;
2145 tline->next = NULL;
2146 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2147 } else {
2148 def->dlist = NULL;
2149 def->defaults = NULL;
2151 def->expansion = NULL;
2153 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2154 !def->plus)
2155 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2156 "too many default macro parameters");
2158 return true;
2163 * Decode a size directive
2165 static int parse_size(const char *str) {
2166 static const char *size_names[] =
2167 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2168 static const int sizes[] =
2169 { 0, 1, 4, 16, 8, 10, 2, 32 };
2171 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2175 * find and process preprocessor directive in passed line
2176 * Find out if a line contains a preprocessor directive, and deal
2177 * with it if so.
2179 * If a directive _is_ found, it is the responsibility of this routine
2180 * (and not the caller) to free_tlist() the line.
2182 * @param tline a pointer to the current tokeninzed line linked list
2183 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2186 static int do_directive(Token * tline)
2188 enum preproc_token i;
2189 int j;
2190 bool err;
2191 int nparam;
2192 bool nolist;
2193 bool casesense;
2194 int k, m;
2195 int offset;
2196 char *p, *pp, *found_path;
2197 const char *mname;
2198 Include *inc;
2199 Context *ctx;
2200 Cond *cond;
2201 MMacro *mmac, **mmhead;
2202 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2203 Line *l;
2204 struct tokenval tokval;
2205 expr *evalresult;
2206 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2207 int64_t count;
2208 size_t len;
2209 int severity;
2211 origline = tline;
2213 skip_white_(tline);
2214 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2215 (tline->text[1] == '%' || tline->text[1] == '$'
2216 || tline->text[1] == '!'))
2217 return NO_DIRECTIVE_FOUND;
2219 i = pp_token_hash(tline->text);
2222 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2223 * since they are known to be buggy at moment, we need to fix them
2224 * in future release (2.09-2.10)
2226 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
2227 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2228 tline->text);
2229 return NO_DIRECTIVE_FOUND;
2233 * If we're in a non-emitting branch of a condition construct,
2234 * or walking to the end of an already terminated %rep block,
2235 * we should ignore all directives except for condition
2236 * directives.
2238 if (((istk->conds && !emitting(istk->conds->state)) ||
2239 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2240 return NO_DIRECTIVE_FOUND;
2244 * If we're defining a macro or reading a %rep block, we should
2245 * ignore all directives except for %macro/%imacro (which nest),
2246 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2247 * If we're in a %rep block, another %rep nests, so should be let through.
2249 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2250 i != PP_RMACRO && i != PP_IRMACRO &&
2251 i != PP_ENDMACRO && i != PP_ENDM &&
2252 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2253 return NO_DIRECTIVE_FOUND;
2256 if (defining) {
2257 if (i == PP_MACRO || i == PP_IMACRO ||
2258 i == PP_RMACRO || i == PP_IRMACRO) {
2259 nested_mac_count++;
2260 return NO_DIRECTIVE_FOUND;
2261 } else if (nested_mac_count > 0) {
2262 if (i == PP_ENDMACRO) {
2263 nested_mac_count--;
2264 return NO_DIRECTIVE_FOUND;
2267 if (!defining->name) {
2268 if (i == PP_REP) {
2269 nested_rep_count++;
2270 return NO_DIRECTIVE_FOUND;
2271 } else if (nested_rep_count > 0) {
2272 if (i == PP_ENDREP) {
2273 nested_rep_count--;
2274 return NO_DIRECTIVE_FOUND;
2280 switch (i) {
2281 case PP_INVALID:
2282 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2283 tline->text);
2284 return NO_DIRECTIVE_FOUND; /* didn't get it */
2286 case PP_PRAGMA:
2288 * Currently %pragma doesn't do anything; it is here for
2289 * forward compatibility with future versions of NASM.
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2294 case PP_STACKSIZE:
2295 /* Directive to tell NASM what the default stack size is. The
2296 * default is for a 16-bit stack, and this can be overriden with
2297 * %stacksize large.
2299 tline = tline->next;
2300 if (tline && tline->type == TOK_WHITESPACE)
2301 tline = tline->next;
2302 if (!tline || tline->type != TOK_ID) {
2303 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
2307 if (nasm_stricmp(tline->text, "flat") == 0) {
2308 /* All subsequent ARG directives are for a 32-bit stack */
2309 StackSize = 4;
2310 StackPointer = "ebp";
2311 ArgOffset = 8;
2312 LocalOffset = 0;
2313 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2314 /* All subsequent ARG directives are for a 64-bit stack */
2315 StackSize = 8;
2316 StackPointer = "rbp";
2317 ArgOffset = 16;
2318 LocalOffset = 0;
2319 } else if (nasm_stricmp(tline->text, "large") == 0) {
2320 /* All subsequent ARG directives are for a 16-bit stack,
2321 * far function call.
2323 StackSize = 2;
2324 StackPointer = "bp";
2325 ArgOffset = 4;
2326 LocalOffset = 0;
2327 } else if (nasm_stricmp(tline->text, "small") == 0) {
2328 /* All subsequent ARG directives are for a 16-bit stack,
2329 * far function call. We don't support near functions.
2331 StackSize = 2;
2332 StackPointer = "bp";
2333 ArgOffset = 6;
2334 LocalOffset = 0;
2335 } else {
2336 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2337 free_tlist(origline);
2338 return DIRECTIVE_FOUND;
2340 free_tlist(origline);
2341 return DIRECTIVE_FOUND;
2343 case PP_ARG:
2344 /* TASM like ARG directive to define arguments to functions, in
2345 * the following form:
2347 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2349 offset = ArgOffset;
2350 do {
2351 char *arg, directive[256];
2352 int size = StackSize;
2354 /* Find the argument name */
2355 tline = tline->next;
2356 if (tline && tline->type == TOK_WHITESPACE)
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_ID) {
2359 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
2363 arg = tline->text;
2365 /* Find the argument size type */
2366 tline = tline->next;
2367 if (!tline || tline->type != TOK_OTHER
2368 || tline->text[0] != ':') {
2369 nasm_error(ERR_NONFATAL,
2370 "Syntax error processing `%%arg' directive");
2371 free_tlist(origline);
2372 return DIRECTIVE_FOUND;
2374 tline = tline->next;
2375 if (!tline || tline->type != TOK_ID) {
2376 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2377 free_tlist(origline);
2378 return DIRECTIVE_FOUND;
2381 /* Allow macro expansion of type parameter */
2382 tt = tokenize(tline->text);
2383 tt = expand_smacro(tt);
2384 size = parse_size(tt->text);
2385 if (!size) {
2386 nasm_error(ERR_NONFATAL,
2387 "Invalid size type for `%%arg' missing directive");
2388 free_tlist(tt);
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2392 free_tlist(tt);
2394 /* Round up to even stack slots */
2395 size = ALIGN(size, StackSize);
2397 /* Now define the macro for the argument */
2398 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2399 arg, StackPointer, offset);
2400 do_directive(tokenize(directive));
2401 offset += size;
2403 /* Move to the next argument in the list */
2404 tline = tline->next;
2405 if (tline && tline->type == TOK_WHITESPACE)
2406 tline = tline->next;
2407 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2408 ArgOffset = offset;
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2412 case PP_LOCAL:
2413 /* TASM like LOCAL directive to define local variables for a
2414 * function, in the following form:
2416 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2418 * The '= LocalSize' at the end is ignored by NASM, but is
2419 * required by TASM to define the local parameter size (and used
2420 * by the TASM macro package).
2422 offset = LocalOffset;
2423 do {
2424 char *local, directive[256];
2425 int size = StackSize;
2427 /* Find the argument name */
2428 tline = tline->next;
2429 if (tline && tline->type == TOK_WHITESPACE)
2430 tline = tline->next;
2431 if (!tline || tline->type != TOK_ID) {
2432 nasm_error(ERR_NONFATAL,
2433 "`%%local' missing argument parameter");
2434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
2437 local = tline->text;
2439 /* Find the argument size type */
2440 tline = tline->next;
2441 if (!tline || tline->type != TOK_OTHER
2442 || tline->text[0] != ':') {
2443 nasm_error(ERR_NONFATAL,
2444 "Syntax error processing `%%local' directive");
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2448 tline = tline->next;
2449 if (!tline || tline->type != TOK_ID) {
2450 nasm_error(ERR_NONFATAL,
2451 "`%%local' missing size type parameter");
2452 free_tlist(origline);
2453 return DIRECTIVE_FOUND;
2456 /* Allow macro expansion of type parameter */
2457 tt = tokenize(tline->text);
2458 tt = expand_smacro(tt);
2459 size = parse_size(tt->text);
2460 if (!size) {
2461 nasm_error(ERR_NONFATAL,
2462 "Invalid size type for `%%local' missing directive");
2463 free_tlist(tt);
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2467 free_tlist(tt);
2469 /* Round up to even stack slots */
2470 size = ALIGN(size, StackSize);
2472 offset += size; /* Negative offset, increment before */
2474 /* Now define the macro for the argument */
2475 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2476 local, StackPointer, offset);
2477 do_directive(tokenize(directive));
2479 /* Now define the assign to setup the enter_c macro correctly */
2480 snprintf(directive, sizeof(directive),
2481 "%%assign %%$localsize %%$localsize+%d", size);
2482 do_directive(tokenize(directive));
2484 /* Move to the next argument in the list */
2485 tline = tline->next;
2486 if (tline && tline->type == TOK_WHITESPACE)
2487 tline = tline->next;
2488 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2489 LocalOffset = offset;
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2493 case PP_CLEAR:
2494 if (tline->next)
2495 nasm_error(ERR_WARNING|ERR_PASS1,
2496 "trailing garbage after `%%clear' ignored");
2497 free_macros();
2498 init_macros();
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
2502 case PP_DEPEND:
2503 t = tline->next = expand_smacro(tline->next);
2504 skip_white_(t);
2505 if (!t || (t->type != TOK_STRING &&
2506 t->type != TOK_INTERNAL_STRING)) {
2507 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND; /* but we did _something_ */
2511 if (t->next)
2512 nasm_error(ERR_WARNING|ERR_PASS1,
2513 "trailing garbage after `%%depend' ignored");
2514 p = t->text;
2515 if (t->type != TOK_INTERNAL_STRING)
2516 nasm_unquote_cstr(p, i);
2517 if (dephead && !in_list(*dephead, p)) {
2518 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2519 sl->next = NULL;
2520 strcpy(sl->str, p);
2521 *deptail = sl;
2522 deptail = &sl->next;
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
2527 case PP_INCLUDE:
2528 t = tline->next = expand_smacro(tline->next);
2529 skip_white_(t);
2531 if (!t || (t->type != TOK_STRING &&
2532 t->type != TOK_INTERNAL_STRING)) {
2533 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
2534 free_tlist(origline);
2535 return DIRECTIVE_FOUND; /* but we did _something_ */
2537 if (t->next)
2538 nasm_error(ERR_WARNING|ERR_PASS1,
2539 "trailing garbage after `%%include' ignored");
2540 p = t->text;
2541 if (t->type != TOK_INTERNAL_STRING)
2542 nasm_unquote_cstr(p, i);
2543 inc = nasm_malloc(sizeof(Include));
2544 inc->next = istk;
2545 inc->conds = NULL;
2546 found_path = NULL;
2547 inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, "r");
2548 if (!inc->fp) {
2549 /* -MG given but file not found */
2550 nasm_free(inc);
2551 } else {
2552 inc->fname = src_set_fname(found_path ? found_path : p);
2553 inc->lineno = src_set_linnum(0);
2554 inc->lineinc = 1;
2555 inc->expansion = NULL;
2556 inc->mstk = NULL;
2557 istk = inc;
2558 lfmt->uplevel(LIST_INCLUDE);
2560 free_tlist(origline);
2561 return DIRECTIVE_FOUND;
2563 case PP_USE:
2565 static macros_t *use_pkg;
2566 const char *pkg_macro = NULL;
2568 tline = tline->next;
2569 skip_white_(tline);
2570 tline = expand_id(tline);
2572 if (!tline || (tline->type != TOK_STRING &&
2573 tline->type != TOK_INTERNAL_STRING &&
2574 tline->type != TOK_ID)) {
2575 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
2576 free_tlist(origline);
2577 return DIRECTIVE_FOUND; /* but we did _something_ */
2579 if (tline->next)
2580 nasm_error(ERR_WARNING|ERR_PASS1,
2581 "trailing garbage after `%%use' ignored");
2582 if (tline->type == TOK_STRING)
2583 nasm_unquote_cstr(tline->text, i);
2584 use_pkg = nasm_stdmac_find_package(tline->text);
2585 if (!use_pkg)
2586 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2587 else
2588 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2589 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2590 /* Not already included, go ahead and include it */
2591 stdmacpos = use_pkg;
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2596 case PP_PUSH:
2597 case PP_REPL:
2598 case PP_POP:
2599 tline = tline->next;
2600 skip_white_(tline);
2601 tline = expand_id(tline);
2602 if (tline) {
2603 if (!tok_type_(tline, TOK_ID)) {
2604 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
2605 pp_directives[i]);
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND; /* but we did _something_ */
2609 if (tline->next)
2610 nasm_error(ERR_WARNING|ERR_PASS1,
2611 "trailing garbage after `%s' ignored",
2612 pp_directives[i]);
2613 p = nasm_strdup(tline->text);
2614 } else {
2615 p = NULL; /* Anonymous */
2618 if (i == PP_PUSH) {
2619 ctx = nasm_malloc(sizeof(Context));
2620 ctx->next = cstk;
2621 hash_init(&ctx->localmac, HASH_SMALL);
2622 ctx->name = p;
2623 ctx->number = unique++;
2624 cstk = ctx;
2625 } else {
2626 /* %pop or %repl */
2627 if (!cstk) {
2628 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
2629 pp_directives[i]);
2630 } else if (i == PP_POP) {
2631 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2632 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2633 "expected %s",
2634 cstk->name ? cstk->name : "anonymous", p);
2635 else
2636 ctx_pop();
2637 } else {
2638 /* i == PP_REPL */
2639 nasm_free(cstk->name);
2640 cstk->name = p;
2641 p = NULL;
2643 nasm_free(p);
2645 free_tlist(origline);
2646 return DIRECTIVE_FOUND;
2647 case PP_FATAL:
2648 severity = ERR_FATAL;
2649 goto issue_error;
2650 case PP_ERROR:
2651 severity = ERR_NONFATAL;
2652 goto issue_error;
2653 case PP_WARNING:
2654 severity = ERR_WARNING|ERR_WARN_USER;
2655 goto issue_error;
2657 issue_error:
2659 /* Only error out if this is the final pass */
2660 if (pass != 2 && i != PP_FATAL)
2661 return DIRECTIVE_FOUND;
2663 tline->next = expand_smacro(tline->next);
2664 tline = tline->next;
2665 skip_white_(tline);
2666 t = tline ? tline->next : NULL;
2667 skip_white_(t);
2668 if (tok_type_(tline, TOK_STRING) && !t) {
2669 /* The line contains only a quoted string */
2670 p = tline->text;
2671 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2672 nasm_error(severity, "%s", p);
2673 } else {
2674 /* Not a quoted string, or more than a quoted string */
2675 p = detoken(tline, false);
2676 nasm_error(severity, "%s", p);
2677 nasm_free(p);
2679 free_tlist(origline);
2680 return DIRECTIVE_FOUND;
2683 CASE_PP_IF:
2684 if (istk->conds && !emitting(istk->conds->state))
2685 j = COND_NEVER;
2686 else {
2687 j = if_condition(tline->next, i);
2688 tline->next = NULL; /* it got freed */
2689 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2691 cond = nasm_malloc(sizeof(Cond));
2692 cond->next = istk->conds;
2693 cond->state = j;
2694 istk->conds = cond;
2695 if(istk->mstk)
2696 istk->mstk->condcnt ++;
2697 free_tlist(origline);
2698 return DIRECTIVE_FOUND;
2700 CASE_PP_ELIF:
2701 if (!istk->conds)
2702 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2703 switch(istk->conds->state) {
2704 case COND_IF_TRUE:
2705 istk->conds->state = COND_DONE;
2706 break;
2708 case COND_DONE:
2709 case COND_NEVER:
2710 break;
2712 case COND_ELSE_TRUE:
2713 case COND_ELSE_FALSE:
2714 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2715 "`%%elif' after `%%else' ignored");
2716 istk->conds->state = COND_NEVER;
2717 break;
2719 case COND_IF_FALSE:
2721 * IMPORTANT: In the case of %if, we will already have
2722 * called expand_mmac_params(); however, if we're
2723 * processing an %elif we must have been in a
2724 * non-emitting mode, which would have inhibited
2725 * the normal invocation of expand_mmac_params().
2726 * Therefore, we have to do it explicitly here.
2728 j = if_condition(expand_mmac_params(tline->next), i);
2729 tline->next = NULL; /* it got freed */
2730 istk->conds->state =
2731 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2732 break;
2734 free_tlist(origline);
2735 return DIRECTIVE_FOUND;
2737 case PP_ELSE:
2738 if (tline->next)
2739 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2740 "trailing garbage after `%%else' ignored");
2741 if (!istk->conds)
2742 nasm_fatal(0, "`%%else: no matching `%%if'");
2743 switch(istk->conds->state) {
2744 case COND_IF_TRUE:
2745 case COND_DONE:
2746 istk->conds->state = COND_ELSE_FALSE;
2747 break;
2749 case COND_NEVER:
2750 break;
2752 case COND_IF_FALSE:
2753 istk->conds->state = COND_ELSE_TRUE;
2754 break;
2756 case COND_ELSE_TRUE:
2757 case COND_ELSE_FALSE:
2758 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2759 "`%%else' after `%%else' ignored.");
2760 istk->conds->state = COND_NEVER;
2761 break;
2763 free_tlist(origline);
2764 return DIRECTIVE_FOUND;
2766 case PP_ENDIF:
2767 if (tline->next)
2768 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2769 "trailing garbage after `%%endif' ignored");
2770 if (!istk->conds)
2771 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
2772 cond = istk->conds;
2773 istk->conds = cond->next;
2774 nasm_free(cond);
2775 if(istk->mstk)
2776 istk->mstk->condcnt --;
2777 free_tlist(origline);
2778 return DIRECTIVE_FOUND;
2780 case PP_RMACRO:
2781 case PP_IRMACRO:
2782 case PP_MACRO:
2783 case PP_IMACRO:
2784 if (defining) {
2785 nasm_error(ERR_FATAL, "`%s': already defining a macro",
2786 pp_directives[i]);
2787 return DIRECTIVE_FOUND;
2789 defining = nasm_zalloc(sizeof(MMacro));
2790 defining->max_depth =
2791 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2792 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2793 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2794 nasm_free(defining);
2795 defining = NULL;
2796 return DIRECTIVE_FOUND;
2799 src_get(&defining->xline, &defining->fname);
2801 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2802 while (mmac) {
2803 if (!strcmp(mmac->name, defining->name) &&
2804 (mmac->nparam_min <= defining->nparam_max
2805 || defining->plus)
2806 && (defining->nparam_min <= mmac->nparam_max
2807 || mmac->plus)) {
2808 nasm_error(ERR_WARNING|ERR_PASS1,
2809 "redefining multi-line macro `%s'", defining->name);
2810 return DIRECTIVE_FOUND;
2812 mmac = mmac->next;
2814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
2817 case PP_ENDM:
2818 case PP_ENDMACRO:
2819 if (! (defining && defining->name)) {
2820 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2821 return DIRECTIVE_FOUND;
2823 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2824 defining->next = *mmhead;
2825 *mmhead = defining;
2826 defining = NULL;
2827 free_tlist(origline);
2828 return DIRECTIVE_FOUND;
2830 case PP_EXITMACRO:
2832 * We must search along istk->expansion until we hit a
2833 * macro-end marker for a macro with a name. Then we
2834 * bypass all lines between exitmacro and endmacro.
2836 list_for_each(l, istk->expansion)
2837 if (l->finishes && l->finishes->name)
2838 break;
2840 if (l) {
2842 * Remove all conditional entries relative to this
2843 * macro invocation. (safe to do in this context)
2845 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2846 cond = istk->conds;
2847 istk->conds = cond->next;
2848 nasm_free(cond);
2850 istk->expansion = l;
2851 } else {
2852 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
2857 case PP_UNMACRO:
2858 case PP_UNIMACRO:
2860 MMacro **mmac_p;
2861 MMacro spec;
2863 spec.casesense = (i == PP_UNMACRO);
2864 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2865 return DIRECTIVE_FOUND;
2867 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2868 while (mmac_p && *mmac_p) {
2869 mmac = *mmac_p;
2870 if (mmac->casesense == spec.casesense &&
2871 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2872 mmac->nparam_min == spec.nparam_min &&
2873 mmac->nparam_max == spec.nparam_max &&
2874 mmac->plus == spec.plus) {
2875 *mmac_p = mmac->next;
2876 free_mmacro(mmac);
2877 } else {
2878 mmac_p = &mmac->next;
2881 free_tlist(origline);
2882 free_tlist(spec.dlist);
2883 return DIRECTIVE_FOUND;
2886 case PP_ROTATE:
2887 if (tline->next && tline->next->type == TOK_WHITESPACE)
2888 tline = tline->next;
2889 if (!tline->next) {
2890 free_tlist(origline);
2891 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2892 return DIRECTIVE_FOUND;
2894 t = expand_smacro(tline->next);
2895 tline->next = NULL;
2896 free_tlist(origline);
2897 tline = t;
2898 tptr = &t;
2899 tokval.t_type = TOKEN_INVALID;
2900 evalresult =
2901 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
2902 free_tlist(tline);
2903 if (!evalresult)
2904 return DIRECTIVE_FOUND;
2905 if (tokval.t_type)
2906 nasm_error(ERR_WARNING|ERR_PASS1,
2907 "trailing garbage after expression ignored");
2908 if (!is_simple(evalresult)) {
2909 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2910 return DIRECTIVE_FOUND;
2912 mmac = istk->mstk;
2913 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2914 mmac = mmac->next_active;
2915 if (!mmac) {
2916 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2917 } else if (mmac->nparam == 0) {
2918 nasm_error(ERR_NONFATAL,
2919 "`%%rotate' invoked within macro without parameters");
2920 } else {
2921 int rotate = mmac->rotate + reloc_value(evalresult);
2923 rotate %= (int)mmac->nparam;
2924 if (rotate < 0)
2925 rotate += mmac->nparam;
2927 mmac->rotate = rotate;
2929 return DIRECTIVE_FOUND;
2931 case PP_REP:
2932 nolist = false;
2933 do {
2934 tline = tline->next;
2935 } while (tok_type_(tline, TOK_WHITESPACE));
2937 if (tok_type_(tline, TOK_ID) &&
2938 nasm_stricmp(tline->text, ".nolist") == 0) {
2939 nolist = true;
2940 do {
2941 tline = tline->next;
2942 } while (tok_type_(tline, TOK_WHITESPACE));
2945 if (tline) {
2946 t = expand_smacro(tline);
2947 tptr = &t;
2948 tokval.t_type = TOKEN_INVALID;
2949 evalresult =
2950 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
2951 if (!evalresult) {
2952 free_tlist(origline);
2953 return DIRECTIVE_FOUND;
2955 if (tokval.t_type)
2956 nasm_error(ERR_WARNING|ERR_PASS1,
2957 "trailing garbage after expression ignored");
2958 if (!is_simple(evalresult)) {
2959 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2960 return DIRECTIVE_FOUND;
2962 count = reloc_value(evalresult);
2963 if (count >= REP_LIMIT) {
2964 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
2965 count = 0;
2966 } else
2967 count++;
2968 } else {
2969 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2970 count = 0;
2972 free_tlist(origline);
2974 tmp_defining = defining;
2975 defining = nasm_malloc(sizeof(MMacro));
2976 defining->prev = NULL;
2977 defining->name = NULL; /* flags this macro as a %rep block */
2978 defining->casesense = false;
2979 defining->plus = false;
2980 defining->nolist = nolist;
2981 defining->in_progress = count;
2982 defining->max_depth = 0;
2983 defining->nparam_min = defining->nparam_max = 0;
2984 defining->defaults = NULL;
2985 defining->dlist = NULL;
2986 defining->expansion = NULL;
2987 defining->next_active = istk->mstk;
2988 defining->rep_nest = tmp_defining;
2989 return DIRECTIVE_FOUND;
2991 case PP_ENDREP:
2992 if (!defining || defining->name) {
2993 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2994 return DIRECTIVE_FOUND;
2998 * Now we have a "macro" defined - although it has no name
2999 * and we won't be entering it in the hash tables - we must
3000 * push a macro-end marker for it on to istk->expansion.
3001 * After that, it will take care of propagating itself (a
3002 * macro-end marker line for a macro which is really a %rep
3003 * block will cause the macro to be re-expanded, complete
3004 * with another macro-end marker to ensure the process
3005 * continues) until the whole expansion is forcibly removed
3006 * from istk->expansion by a %exitrep.
3008 l = nasm_malloc(sizeof(Line));
3009 l->next = istk->expansion;
3010 l->finishes = defining;
3011 l->first = NULL;
3012 istk->expansion = l;
3014 istk->mstk = defining;
3016 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3017 tmp_defining = defining;
3018 defining = defining->rep_nest;
3019 free_tlist(origline);
3020 return DIRECTIVE_FOUND;
3022 case PP_EXITREP:
3024 * We must search along istk->expansion until we hit a
3025 * macro-end marker for a macro with no name. Then we set
3026 * its `in_progress' flag to 0.
3028 list_for_each(l, istk->expansion)
3029 if (l->finishes && !l->finishes->name)
3030 break;
3032 if (l)
3033 l->finishes->in_progress = 1;
3034 else
3035 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3039 case PP_XDEFINE:
3040 case PP_IXDEFINE:
3041 case PP_DEFINE:
3042 case PP_IDEFINE:
3043 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3045 tline = tline->next;
3046 skip_white_(tline);
3047 tline = expand_id(tline);
3048 if (!tline || (tline->type != TOK_ID &&
3049 (tline->type != TOK_PREPROC_ID ||
3050 tline->text[1] != '$'))) {
3051 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
3052 pp_directives[i]);
3053 free_tlist(origline);
3054 return DIRECTIVE_FOUND;
3057 ctx = get_ctx(tline->text, &mname);
3058 last = tline;
3059 param_start = tline = tline->next;
3060 nparam = 0;
3062 /* Expand the macro definition now for %xdefine and %ixdefine */
3063 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3064 tline = expand_smacro(tline);
3066 if (tok_is_(tline, "(")) {
3068 * This macro has parameters.
3071 tline = tline->next;
3072 while (1) {
3073 skip_white_(tline);
3074 if (!tline) {
3075 nasm_error(ERR_NONFATAL, "parameter identifier expected");
3076 free_tlist(origline);
3077 return DIRECTIVE_FOUND;
3079 if (tline->type != TOK_ID) {
3080 nasm_error(ERR_NONFATAL,
3081 "`%s': parameter identifier expected",
3082 tline->text);
3083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3086 tline->type = TOK_SMAC_PARAM + nparam++;
3087 tline = tline->next;
3088 skip_white_(tline);
3089 if (tok_is_(tline, ",")) {
3090 tline = tline->next;
3091 } else {
3092 if (!tok_is_(tline, ")")) {
3093 nasm_error(ERR_NONFATAL,
3094 "`)' expected to terminate macro template");
3095 free_tlist(origline);
3096 return DIRECTIVE_FOUND;
3098 break;
3101 last = tline;
3102 tline = tline->next;
3104 if (tok_type_(tline, TOK_WHITESPACE))
3105 last = tline, tline = tline->next;
3106 macro_start = NULL;
3107 last->next = NULL;
3108 t = tline;
3109 while (t) {
3110 if (t->type == TOK_ID) {
3111 list_for_each(tt, param_start)
3112 if (tt->type >= TOK_SMAC_PARAM &&
3113 !strcmp(tt->text, t->text))
3114 t->type = tt->type;
3116 tt = t->next;
3117 t->next = macro_start;
3118 macro_start = t;
3119 t = tt;
3122 * Good. We now have a macro name, a parameter count, and a
3123 * token list (in reverse order) for an expansion. We ought
3124 * to be OK just to create an SMacro, store it, and let
3125 * free_tlist have the rest of the line (which we have
3126 * carefully re-terminated after chopping off the expansion
3127 * from the end).
3129 define_smacro(ctx, mname, casesense, nparam, macro_start);
3130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
3133 case PP_UNDEF:
3134 tline = tline->next;
3135 skip_white_(tline);
3136 tline = expand_id(tline);
3137 if (!tline || (tline->type != TOK_ID &&
3138 (tline->type != TOK_PREPROC_ID ||
3139 tline->text[1] != '$'))) {
3140 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
3144 if (tline->next) {
3145 nasm_error(ERR_WARNING|ERR_PASS1,
3146 "trailing garbage after macro name ignored");
3149 /* Find the context that symbol belongs to */
3150 ctx = get_ctx(tline->text, &mname);
3151 undef_smacro(ctx, mname);
3152 free_tlist(origline);
3153 return DIRECTIVE_FOUND;
3155 case PP_DEFSTR:
3156 case PP_IDEFSTR:
3157 casesense = (i == PP_DEFSTR);
3159 tline = tline->next;
3160 skip_white_(tline);
3161 tline = expand_id(tline);
3162 if (!tline || (tline->type != TOK_ID &&
3163 (tline->type != TOK_PREPROC_ID ||
3164 tline->text[1] != '$'))) {
3165 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
3166 pp_directives[i]);
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3171 ctx = get_ctx(tline->text, &mname);
3172 last = tline;
3173 tline = expand_smacro(tline->next);
3174 last->next = NULL;
3176 while (tok_type_(tline, TOK_WHITESPACE))
3177 tline = delete_Token(tline);
3179 p = detoken(tline, false);
3180 macro_start = nasm_malloc(sizeof(*macro_start));
3181 macro_start->next = NULL;
3182 macro_start->text = nasm_quote(p, strlen(p));
3183 macro_start->type = TOK_STRING;
3184 macro_start->a.mac = NULL;
3185 nasm_free(p);
3188 * We now have a macro name, an implicit parameter count of
3189 * zero, and a string token to use as an expansion. Create
3190 * and store an SMacro.
3192 define_smacro(ctx, mname, casesense, 0, macro_start);
3193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
3196 case PP_DEFTOK:
3197 case PP_IDEFTOK:
3198 casesense = (i == PP_DEFTOK);
3200 tline = tline->next;
3201 skip_white_(tline);
3202 tline = expand_id(tline);
3203 if (!tline || (tline->type != TOK_ID &&
3204 (tline->type != TOK_PREPROC_ID ||
3205 tline->text[1] != '$'))) {
3206 nasm_error(ERR_NONFATAL,
3207 "`%s' expects a macro identifier as first parameter",
3208 pp_directives[i]);
3209 free_tlist(origline);
3210 return DIRECTIVE_FOUND;
3212 ctx = get_ctx(tline->text, &mname);
3213 last = tline;
3214 tline = expand_smacro(tline->next);
3215 last->next = NULL;
3217 t = tline;
3218 while (tok_type_(t, TOK_WHITESPACE))
3219 t = t->next;
3220 /* t should now point to the string */
3221 if (!tok_type_(t, TOK_STRING)) {
3222 nasm_error(ERR_NONFATAL,
3223 "`%s` requires string as second parameter",
3224 pp_directives[i]);
3225 free_tlist(tline);
3226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
3231 * Convert the string to a token stream. Note that smacros
3232 * are stored with the token stream reversed, so we have to
3233 * reverse the output of tokenize().
3235 nasm_unquote_cstr(t->text, i);
3236 macro_start = reverse_tokens(tokenize(t->text));
3239 * We now have a macro name, an implicit parameter count of
3240 * zero, and a numeric token to use as an expansion. Create
3241 * and store an SMacro.
3243 define_smacro(ctx, mname, casesense, 0, macro_start);
3244 free_tlist(tline);
3245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3248 case PP_PATHSEARCH:
3250 FILE *fp;
3251 StrList *xsl = NULL;
3252 StrList **xst = &xsl;
3254 casesense = true;
3256 tline = tline->next;
3257 skip_white_(tline);
3258 tline = expand_id(tline);
3259 if (!tline || (tline->type != TOK_ID &&
3260 (tline->type != TOK_PREPROC_ID ||
3261 tline->text[1] != '$'))) {
3262 nasm_error(ERR_NONFATAL,
3263 "`%%pathsearch' expects a macro identifier as first parameter");
3264 free_tlist(origline);
3265 return DIRECTIVE_FOUND;
3267 ctx = get_ctx(tline->text, &mname);
3268 last = tline;
3269 tline = expand_smacro(tline->next);
3270 last->next = NULL;
3272 t = tline;
3273 while (tok_type_(t, TOK_WHITESPACE))
3274 t = t->next;
3276 if (!t || (t->type != TOK_STRING &&
3277 t->type != TOK_INTERNAL_STRING)) {
3278 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3279 free_tlist(tline);
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND; /* but we did _something_ */
3283 if (t->next)
3284 nasm_error(ERR_WARNING|ERR_PASS1,
3285 "trailing garbage after `%%pathsearch' ignored");
3286 p = t->text;
3287 if (t->type != TOK_INTERNAL_STRING)
3288 nasm_unquote(p, NULL);
3290 fp = inc_fopen(p, &xsl, &xst, NULL, true, "r");
3291 if (fp) {
3292 p = xsl->str;
3293 fclose(fp); /* Don't actually care about the file */
3295 macro_start = nasm_malloc(sizeof(*macro_start));
3296 macro_start->next = NULL;
3297 macro_start->text = nasm_quote(p, strlen(p));
3298 macro_start->type = TOK_STRING;
3299 macro_start->a.mac = NULL;
3300 if (xsl)
3301 nasm_free(xsl);
3304 * We now have a macro name, an implicit parameter count of
3305 * zero, and a string token to use as an expansion. Create
3306 * and store an SMacro.
3308 define_smacro(ctx, mname, casesense, 0, macro_start);
3309 free_tlist(tline);
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3314 case PP_STRLEN:
3315 casesense = true;
3317 tline = tline->next;
3318 skip_white_(tline);
3319 tline = expand_id(tline);
3320 if (!tline || (tline->type != TOK_ID &&
3321 (tline->type != TOK_PREPROC_ID ||
3322 tline->text[1] != '$'))) {
3323 nasm_error(ERR_NONFATAL,
3324 "`%%strlen' expects a macro identifier as first parameter");
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3328 ctx = get_ctx(tline->text, &mname);
3329 last = tline;
3330 tline = expand_smacro(tline->next);
3331 last->next = NULL;
3333 t = tline;
3334 while (tok_type_(t, TOK_WHITESPACE))
3335 t = t->next;
3336 /* t should now point to the string */
3337 if (!tok_type_(t, TOK_STRING)) {
3338 nasm_error(ERR_NONFATAL,
3339 "`%%strlen` requires string as second parameter");
3340 free_tlist(tline);
3341 free_tlist(origline);
3342 return DIRECTIVE_FOUND;
3345 macro_start = nasm_malloc(sizeof(*macro_start));
3346 macro_start->next = NULL;
3347 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3348 macro_start->a.mac = NULL;
3351 * We now have a macro name, an implicit parameter count of
3352 * zero, and a numeric token to use as an expansion. Create
3353 * and store an SMacro.
3355 define_smacro(ctx, mname, casesense, 0, macro_start);
3356 free_tlist(tline);
3357 free_tlist(origline);
3358 return DIRECTIVE_FOUND;
3360 case PP_STRCAT:
3361 casesense = true;
3363 tline = tline->next;
3364 skip_white_(tline);
3365 tline = expand_id(tline);
3366 if (!tline || (tline->type != TOK_ID &&
3367 (tline->type != TOK_PREPROC_ID ||
3368 tline->text[1] != '$'))) {
3369 nasm_error(ERR_NONFATAL,
3370 "`%%strcat' expects a macro identifier as first parameter");
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3374 ctx = get_ctx(tline->text, &mname);
3375 last = tline;
3376 tline = expand_smacro(tline->next);
3377 last->next = NULL;
3379 len = 0;
3380 list_for_each(t, tline) {
3381 switch (t->type) {
3382 case TOK_WHITESPACE:
3383 break;
3384 case TOK_STRING:
3385 len += t->a.len = nasm_unquote(t->text, NULL);
3386 break;
3387 case TOK_OTHER:
3388 if (!strcmp(t->text, ",")) /* permit comma separators */
3389 break;
3390 /* else fall through */
3391 default:
3392 nasm_error(ERR_NONFATAL,
3393 "non-string passed to `%%strcat' (%d)", t->type);
3394 free_tlist(tline);
3395 free_tlist(origline);
3396 return DIRECTIVE_FOUND;
3400 p = pp = nasm_malloc(len);
3401 list_for_each(t, tline) {
3402 if (t->type == TOK_STRING) {
3403 memcpy(p, t->text, t->a.len);
3404 p += t->a.len;
3409 * We now have a macro name, an implicit parameter count of
3410 * zero, and a numeric token to use as an expansion. Create
3411 * and store an SMacro.
3413 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3414 macro_start->text = nasm_quote(pp, len);
3415 nasm_free(pp);
3416 define_smacro(ctx, mname, casesense, 0, macro_start);
3417 free_tlist(tline);
3418 free_tlist(origline);
3419 return DIRECTIVE_FOUND;
3421 case PP_SUBSTR:
3423 int64_t start, count;
3424 size_t len;
3426 casesense = true;
3428 tline = tline->next;
3429 skip_white_(tline);
3430 tline = expand_id(tline);
3431 if (!tline || (tline->type != TOK_ID &&
3432 (tline->type != TOK_PREPROC_ID ||
3433 tline->text[1] != '$'))) {
3434 nasm_error(ERR_NONFATAL,
3435 "`%%substr' expects a macro identifier as first parameter");
3436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
3439 ctx = get_ctx(tline->text, &mname);
3440 last = tline;
3441 tline = expand_smacro(tline->next);
3442 last->next = NULL;
3444 if (tline) /* skip expanded id */
3445 t = tline->next;
3446 while (tok_type_(t, TOK_WHITESPACE))
3447 t = t->next;
3449 /* t should now point to the string */
3450 if (!tok_type_(t, TOK_STRING)) {
3451 nasm_error(ERR_NONFATAL,
3452 "`%%substr` requires string as second parameter");
3453 free_tlist(tline);
3454 free_tlist(origline);
3455 return DIRECTIVE_FOUND;
3458 tt = t->next;
3459 tptr = &tt;
3460 tokval.t_type = TOKEN_INVALID;
3461 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3462 if (!evalresult) {
3463 free_tlist(tline);
3464 free_tlist(origline);
3465 return DIRECTIVE_FOUND;
3466 } else if (!is_simple(evalresult)) {
3467 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3468 free_tlist(tline);
3469 free_tlist(origline);
3470 return DIRECTIVE_FOUND;
3472 start = evalresult->value - 1;
3474 while (tok_type_(tt, TOK_WHITESPACE))
3475 tt = tt->next;
3476 if (!tt) {
3477 count = 1; /* Backwards compatibility: one character */
3478 } else {
3479 tokval.t_type = TOKEN_INVALID;
3480 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3481 if (!evalresult) {
3482 free_tlist(tline);
3483 free_tlist(origline);
3484 return DIRECTIVE_FOUND;
3485 } else if (!is_simple(evalresult)) {
3486 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3487 free_tlist(tline);
3488 free_tlist(origline);
3489 return DIRECTIVE_FOUND;
3491 count = evalresult->value;
3494 len = nasm_unquote(t->text, NULL);
3496 /* make start and count being in range */
3497 if (start < 0)
3498 start = 0;
3499 if (count < 0)
3500 count = len + count + 1 - start;
3501 if (start + count > (int64_t)len)
3502 count = len - start;
3503 if (!len || count < 0 || start >=(int64_t)len)
3504 start = -1, count = 0; /* empty string */
3506 macro_start = nasm_malloc(sizeof(*macro_start));
3507 macro_start->next = NULL;
3508 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3509 macro_start->type = TOK_STRING;
3510 macro_start->a.mac = NULL;
3513 * We now have a macro name, an implicit parameter count of
3514 * zero, and a numeric token to use as an expansion. Create
3515 * and store an SMacro.
3517 define_smacro(ctx, mname, casesense, 0, macro_start);
3518 free_tlist(tline);
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3523 case PP_ASSIGN:
3524 case PP_IASSIGN:
3525 casesense = (i == PP_ASSIGN);
3527 tline = tline->next;
3528 skip_white_(tline);
3529 tline = expand_id(tline);
3530 if (!tline || (tline->type != TOK_ID &&
3531 (tline->type != TOK_PREPROC_ID ||
3532 tline->text[1] != '$'))) {
3533 nasm_error(ERR_NONFATAL,
3534 "`%%%sassign' expects a macro identifier",
3535 (i == PP_IASSIGN ? "i" : ""));
3536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
3539 ctx = get_ctx(tline->text, &mname);
3540 last = tline;
3541 tline = expand_smacro(tline->next);
3542 last->next = NULL;
3544 t = tline;
3545 tptr = &t;
3546 tokval.t_type = TOKEN_INVALID;
3547 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
3548 free_tlist(tline);
3549 if (!evalresult) {
3550 free_tlist(origline);
3551 return DIRECTIVE_FOUND;
3554 if (tokval.t_type)
3555 nasm_error(ERR_WARNING|ERR_PASS1,
3556 "trailing garbage after expression ignored");
3558 if (!is_simple(evalresult)) {
3559 nasm_error(ERR_NONFATAL,
3560 "non-constant value given to `%%%sassign'",
3561 (i == PP_IASSIGN ? "i" : ""));
3562 free_tlist(origline);
3563 return DIRECTIVE_FOUND;
3566 macro_start = nasm_malloc(sizeof(*macro_start));
3567 macro_start->next = NULL;
3568 make_tok_num(macro_start, reloc_value(evalresult));
3569 macro_start->a.mac = NULL;
3572 * We now have a macro name, an implicit parameter count of
3573 * zero, and a numeric token to use as an expansion. Create
3574 * and store an SMacro.
3576 define_smacro(ctx, mname, casesense, 0, macro_start);
3577 free_tlist(origline);
3578 return DIRECTIVE_FOUND;
3580 case PP_LINE:
3582 * Syntax is `%line nnn[+mmm] [filename]'
3584 tline = tline->next;
3585 skip_white_(tline);
3586 if (!tok_type_(tline, TOK_NUMBER)) {
3587 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
3588 free_tlist(origline);
3589 return DIRECTIVE_FOUND;
3591 k = readnum(tline->text, &err);
3592 m = 1;
3593 tline = tline->next;
3594 if (tok_is_(tline, "+")) {
3595 tline = tline->next;
3596 if (!tok_type_(tline, TOK_NUMBER)) {
3597 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
3598 free_tlist(origline);
3599 return DIRECTIVE_FOUND;
3601 m = readnum(tline->text, &err);
3602 tline = tline->next;
3604 skip_white_(tline);
3605 src_set_linnum(k);
3606 istk->lineinc = m;
3607 if (tline) {
3608 char *fname = detoken(tline, false);
3609 src_set_fname(fname);
3610 nasm_free(fname);
3612 free_tlist(origline);
3613 return DIRECTIVE_FOUND;
3615 default:
3616 nasm_error(ERR_FATAL,
3617 "preprocessor directive `%s' not yet implemented",
3618 pp_directives[i]);
3619 return DIRECTIVE_FOUND;
3624 * Ensure that a macro parameter contains a condition code and
3625 * nothing else. Return the condition code index if so, or -1
3626 * otherwise.
3628 static int find_cc(Token * t)
3630 Token *tt;
3632 if (!t)
3633 return -1; /* Probably a %+ without a space */
3635 skip_white_(t);
3636 if (t->type != TOK_ID)
3637 return -1;
3638 tt = t->next;
3639 skip_white_(tt);
3640 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3641 return -1;
3643 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
3647 * This routines walks over tokens strem and hadnles tokens
3648 * pasting, if @handle_explicit passed then explicit pasting
3649 * term is handled, otherwise -- implicit pastings only.
3651 static bool paste_tokens(Token **head, const struct tokseq_match *m,
3652 size_t mnum, bool handle_explicit)
3654 Token *tok, *next, **prev_next, **prev_nonspace;
3655 bool pasted = false;
3656 char *buf, *p;
3657 size_t len, i;
3660 * The last token before pasting. We need it
3661 * to be able to connect new handled tokens.
3662 * In other words if there were a tokens stream
3664 * A -> B -> C -> D
3666 * and we've joined tokens B and C, the resulting
3667 * stream should be
3669 * A -> BC -> D
3671 tok = *head;
3672 prev_next = NULL;
3674 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3675 prev_nonspace = head;
3676 else
3677 prev_nonspace = NULL;
3679 while (tok && (next = tok->next)) {
3681 switch (tok->type) {
3682 case TOK_WHITESPACE:
3683 /* Zap redundant whitespaces */
3684 while (tok_type_(next, TOK_WHITESPACE))
3685 next = delete_Token(next);
3686 tok->next = next;
3687 break;
3689 case TOK_PASTE:
3690 /* Explicit pasting */
3691 if (!handle_explicit)
3692 break;
3693 next = delete_Token(tok);
3695 while (tok_type_(next, TOK_WHITESPACE))
3696 next = delete_Token(next);
3698 if (!pasted)
3699 pasted = true;
3701 /* Left pasting token is start of line */
3702 if (!prev_nonspace)
3703 nasm_error(ERR_FATAL, "No lvalue found on pasting");
3706 * No ending token, this might happen in two
3707 * cases
3709 * 1) There indeed no right token at all
3710 * 2) There is a bare "%define ID" statement,
3711 * and @ID does expand to whitespace.
3713 * So technically we need to do a grammar analysis
3714 * in another stage of parsing, but for now lets don't
3715 * change the behaviour people used to. Simply allow
3716 * whitespace after paste token.
3718 if (!next) {
3720 * Zap ending space tokens and that's all.
3722 tok = (*prev_nonspace)->next;
3723 while (tok_type_(tok, TOK_WHITESPACE))
3724 tok = delete_Token(tok);
3725 tok = *prev_nonspace;
3726 tok->next = NULL;
3727 break;
3730 tok = *prev_nonspace;
3731 while (tok_type_(tok, TOK_WHITESPACE))
3732 tok = delete_Token(tok);
3733 len = strlen(tok->text);
3734 len += strlen(next->text);
3736 p = buf = nasm_malloc(len + 1);
3737 strcpy(p, tok->text);
3738 p = strchr(p, '\0');
3739 strcpy(p, next->text);
3741 delete_Token(tok);
3743 tok = tokenize(buf);
3744 nasm_free(buf);
3746 *prev_nonspace = tok;
3747 while (tok && tok->next)
3748 tok = tok->next;
3750 tok->next = delete_Token(next);
3752 /* Restart from pasted tokens head */
3753 tok = *prev_nonspace;
3754 break;
3756 default:
3757 /* implicit pasting */
3758 for (i = 0; i < mnum; i++) {
3759 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3760 continue;
3762 len = 0;
3763 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3764 len += strlen(next->text);
3765 next = next->next;
3768 /* No match */
3769 if (tok == next)
3770 break;
3772 len += strlen(tok->text);
3773 p = buf = nasm_malloc(len + 1);
3775 while (tok != next) {
3776 strcpy(p, tok->text);
3777 p = strchr(p, '\0');
3778 tok = delete_Token(tok);
3781 tok = tokenize(buf);
3782 nasm_free(buf);
3784 if (prev_next)
3785 *prev_next = tok;
3786 else
3787 *head = tok;
3790 * Connect pasted into original stream,
3791 * ie A -> new-tokens -> B
3793 while (tok && tok->next)
3794 tok = tok->next;
3795 tok->next = next;
3797 if (!pasted)
3798 pasted = true;
3800 /* Restart from pasted tokens head */
3801 tok = prev_next ? *prev_next : *head;
3804 break;
3807 prev_next = &tok->next;
3809 if (tok->next &&
3810 !tok_type_(tok->next, TOK_WHITESPACE) &&
3811 !tok_type_(tok->next, TOK_PASTE))
3812 prev_nonspace = prev_next;
3814 tok = tok->next;
3817 return pasted;
3821 * expands to a list of tokens from %{x:y}
3823 static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3825 Token *t = tline, **tt, *tm, *head;
3826 char *pos;
3827 int fst, lst, j, i;
3829 pos = strchr(tline->text, ':');
3830 nasm_assert(pos);
3832 lst = atoi(pos + 1);
3833 fst = atoi(tline->text + 1);
3836 * only macros params are accounted so
3837 * if someone passes %0 -- we reject such
3838 * value(s)
3840 if (lst == 0 || fst == 0)
3841 goto err;
3843 /* the values should be sane */
3844 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3845 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
3846 goto err;
3848 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3849 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
3851 /* counted from zero */
3852 fst--, lst--;
3855 * It will be at least one token. Note we
3856 * need to scan params until separator, otherwise
3857 * only first token will be passed.
3859 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3860 head = new_Token(NULL, tm->type, tm->text, 0);
3861 tt = &head->next, tm = tm->next;
3862 while (tok_isnt_(tm, ",")) {
3863 t = new_Token(NULL, tm->type, tm->text, 0);
3864 *tt = t, tt = &t->next, tm = tm->next;
3867 if (fst < lst) {
3868 for (i = fst + 1; i <= lst; i++) {
3869 t = new_Token(NULL, TOK_OTHER, ",", 0);
3870 *tt = t, tt = &t->next;
3871 j = (i + mac->rotate) % mac->nparam;
3872 tm = mac->params[j];
3873 while (tok_isnt_(tm, ",")) {
3874 t = new_Token(NULL, tm->type, tm->text, 0);
3875 *tt = t, tt = &t->next, tm = tm->next;
3878 } else {
3879 for (i = fst - 1; i >= lst; i--) {
3880 t = new_Token(NULL, TOK_OTHER, ",", 0);
3881 *tt = t, tt = &t->next;
3882 j = (i + mac->rotate) % mac->nparam;
3883 tm = mac->params[j];
3884 while (tok_isnt_(tm, ",")) {
3885 t = new_Token(NULL, tm->type, tm->text, 0);
3886 *tt = t, tt = &t->next, tm = tm->next;
3891 *last = tt;
3892 return head;
3894 err:
3895 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3896 &tline->text[1]);
3897 return tline;
3901 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3902 * %-n) and MMacro-local identifiers (%%foo) as well as
3903 * macro indirection (%[...]) and range (%{..:..}).
3905 static Token *expand_mmac_params(Token * tline)
3907 Token *t, *tt, **tail, *thead;
3908 bool changed = false;
3909 char *pos;
3911 tail = &thead;
3912 thead = NULL;
3914 while (tline) {
3915 if (tline->type == TOK_PREPROC_ID &&
3916 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3917 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3918 tline->text[1] == '%')) {
3919 char *text = NULL;
3920 int type = 0, cc; /* type = 0 to placate optimisers */
3921 char tmpbuf[30];
3922 unsigned int n;
3923 int i;
3924 MMacro *mac;
3926 t = tline;
3927 tline = tline->next;
3929 mac = istk->mstk;
3930 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3931 mac = mac->next_active;
3932 if (!mac) {
3933 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3934 } else {
3935 pos = strchr(t->text, ':');
3936 if (!pos) {
3937 switch (t->text[1]) {
3939 * We have to make a substitution of one of the
3940 * forms %1, %-1, %+1, %%foo, %0.
3942 case '0':
3943 type = TOK_NUMBER;
3944 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3945 text = nasm_strdup(tmpbuf);
3946 break;
3947 case '%':
3948 type = TOK_ID;
3949 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3950 mac->unique);
3951 text = nasm_strcat(tmpbuf, t->text + 2);
3952 break;
3953 case '-':
3954 n = atoi(t->text + 2) - 1;
3955 if (n >= mac->nparam)
3956 tt = NULL;
3957 else {
3958 if (mac->nparam > 1)
3959 n = (n + mac->rotate) % mac->nparam;
3960 tt = mac->params[n];
3962 cc = find_cc(tt);
3963 if (cc == -1) {
3964 nasm_error(ERR_NONFATAL,
3965 "macro parameter %d is not a condition code",
3966 n + 1);
3967 text = NULL;
3968 } else {
3969 type = TOK_ID;
3970 if (inverse_ccs[cc] == -1) {
3971 nasm_error(ERR_NONFATAL,
3972 "condition code `%s' is not invertible",
3973 conditions[cc]);
3974 text = NULL;
3975 } else
3976 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3978 break;
3979 case '+':
3980 n = atoi(t->text + 2) - 1;
3981 if (n >= mac->nparam)
3982 tt = NULL;
3983 else {
3984 if (mac->nparam > 1)
3985 n = (n + mac->rotate) % mac->nparam;
3986 tt = mac->params[n];
3988 cc = find_cc(tt);
3989 if (cc == -1) {
3990 nasm_error(ERR_NONFATAL,
3991 "macro parameter %d is not a condition code",
3992 n + 1);
3993 text = NULL;
3994 } else {
3995 type = TOK_ID;
3996 text = nasm_strdup(conditions[cc]);
3998 break;
3999 default:
4000 n = atoi(t->text + 1) - 1;
4001 if (n >= mac->nparam)
4002 tt = NULL;
4003 else {
4004 if (mac->nparam > 1)
4005 n = (n + mac->rotate) % mac->nparam;
4006 tt = mac->params[n];
4008 if (tt) {
4009 for (i = 0; i < mac->paramlen[n]; i++) {
4010 *tail = new_Token(NULL, tt->type, tt->text, 0);
4011 tail = &(*tail)->next;
4012 tt = tt->next;
4015 text = NULL; /* we've done it here */
4016 break;
4018 } else {
4020 * seems we have a parameters range here
4022 Token *head, **last;
4023 head = expand_mmac_params_range(mac, t, &last);
4024 if (head != t) {
4025 *tail = head;
4026 *last = tline;
4027 tline = head;
4028 text = NULL;
4032 if (!text) {
4033 delete_Token(t);
4034 } else {
4035 *tail = t;
4036 tail = &t->next;
4037 t->type = type;
4038 nasm_free(t->text);
4039 t->text = text;
4040 t->a.mac = NULL;
4042 changed = true;
4043 continue;
4044 } else if (tline->type == TOK_INDIRECT) {
4045 t = tline;
4046 tline = tline->next;
4047 tt = tokenize(t->text);
4048 tt = expand_mmac_params(tt);
4049 tt = expand_smacro(tt);
4050 *tail = tt;
4051 while (tt) {
4052 tt->a.mac = NULL; /* Necessary? */
4053 tail = &tt->next;
4054 tt = tt->next;
4056 delete_Token(t);
4057 changed = true;
4058 } else {
4059 t = *tail = tline;
4060 tline = tline->next;
4061 t->a.mac = NULL;
4062 tail = &t->next;
4065 *tail = NULL;
4067 if (changed) {
4068 const struct tokseq_match t[] = {
4070 PP_CONCAT_MASK(TOK_ID) |
4071 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4072 PP_CONCAT_MASK(TOK_ID) |
4073 PP_CONCAT_MASK(TOK_NUMBER) |
4074 PP_CONCAT_MASK(TOK_FLOAT) |
4075 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4078 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4079 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4082 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4085 return thead;
4089 * Expand all single-line macro calls made in the given line.
4090 * Return the expanded version of the line. The original is deemed
4091 * to be destroyed in the process. (In reality we'll just move
4092 * Tokens from input to output a lot of the time, rather than
4093 * actually bothering to destroy and replicate.)
4096 static Token *expand_smacro(Token * tline)
4098 Token *t, *tt, *mstart, **tail, *thead;
4099 SMacro *head = NULL, *m;
4100 Token **params;
4101 int *paramsize;
4102 unsigned int nparam, sparam;
4103 int brackets;
4104 Token *org_tline = tline;
4105 Context *ctx;
4106 const char *mname;
4107 int deadman = DEADMAN_LIMIT;
4108 bool expanded;
4111 * Trick: we should avoid changing the start token pointer since it can
4112 * be contained in "next" field of other token. Because of this
4113 * we allocate a copy of first token and work with it; at the end of
4114 * routine we copy it back
4116 if (org_tline) {
4117 tline = new_Token(org_tline->next, org_tline->type,
4118 org_tline->text, 0);
4119 tline->a.mac = org_tline->a.mac;
4120 nasm_free(org_tline->text);
4121 org_tline->text = NULL;
4124 expanded = true; /* Always expand %+ at least once */
4126 again:
4127 thead = NULL;
4128 tail = &thead;
4130 while (tline) { /* main token loop */
4131 if (!--deadman) {
4132 nasm_error(ERR_NONFATAL, "interminable macro recursion");
4133 goto err;
4136 if ((mname = tline->text)) {
4137 /* if this token is a local macro, look in local context */
4138 if (tline->type == TOK_ID) {
4139 head = (SMacro *)hash_findix(&smacros, mname);
4140 } else if (tline->type == TOK_PREPROC_ID) {
4141 ctx = get_ctx(mname, &mname);
4142 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4143 } else
4144 head = NULL;
4147 * We've hit an identifier. As in is_mmacro below, we first
4148 * check whether the identifier is a single-line macro at
4149 * all, then think about checking for parameters if
4150 * necessary.
4152 list_for_each(m, head)
4153 if (!mstrcmp(m->name, mname, m->casesense))
4154 break;
4155 if (m) {
4156 mstart = tline;
4157 params = NULL;
4158 paramsize = NULL;
4159 if (m->nparam == 0) {
4161 * Simple case: the macro is parameterless. Discard the
4162 * one token that the macro call took, and push the
4163 * expansion back on the to-do stack.
4165 if (!m->expansion) {
4166 if (!strcmp("__FILE__", m->name)) {
4167 const char *file = src_get_fname();
4168 /* nasm_free(tline->text); here? */
4169 tline->text = nasm_quote(file, strlen(file));
4170 tline->type = TOK_STRING;
4171 continue;
4173 if (!strcmp("__LINE__", m->name)) {
4174 nasm_free(tline->text);
4175 make_tok_num(tline, src_get_linnum());
4176 continue;
4178 if (!strcmp("__BITS__", m->name)) {
4179 nasm_free(tline->text);
4180 make_tok_num(tline, globalbits);
4181 continue;
4183 tline = delete_Token(tline);
4184 continue;
4186 } else {
4188 * Complicated case: at least one macro with this name
4189 * exists and takes parameters. We must find the
4190 * parameters in the call, count them, find the SMacro
4191 * that corresponds to that form of the macro call, and
4192 * substitute for the parameters when we expand. What a
4193 * pain.
4195 /*tline = tline->next;
4196 skip_white_(tline); */
4197 do {
4198 t = tline->next;
4199 while (tok_type_(t, TOK_SMAC_END)) {
4200 t->a.mac->in_progress = false;
4201 t->text = NULL;
4202 t = tline->next = delete_Token(t);
4204 tline = t;
4205 } while (tok_type_(tline, TOK_WHITESPACE));
4206 if (!tok_is_(tline, "(")) {
4208 * This macro wasn't called with parameters: ignore
4209 * the call. (Behaviour borrowed from gnu cpp.)
4211 tline = mstart;
4212 m = NULL;
4213 } else {
4214 int paren = 0;
4215 int white = 0;
4216 brackets = 0;
4217 nparam = 0;
4218 sparam = PARAM_DELTA;
4219 params = nasm_malloc(sparam * sizeof(Token *));
4220 params[0] = tline->next;
4221 paramsize = nasm_malloc(sparam * sizeof(int));
4222 paramsize[0] = 0;
4223 while (true) { /* parameter loop */
4225 * For some unusual expansions
4226 * which concatenates function call
4228 t = tline->next;
4229 while (tok_type_(t, TOK_SMAC_END)) {
4230 t->a.mac->in_progress = false;
4231 t->text = NULL;
4232 t = tline->next = delete_Token(t);
4234 tline = t;
4236 if (!tline) {
4237 nasm_error(ERR_NONFATAL,
4238 "macro call expects terminating `)'");
4239 break;
4241 if (tline->type == TOK_WHITESPACE
4242 && brackets <= 0) {
4243 if (paramsize[nparam])
4244 white++;
4245 else
4246 params[nparam] = tline->next;
4247 continue; /* parameter loop */
4249 if (tline->type == TOK_OTHER
4250 && tline->text[1] == 0) {
4251 char ch = tline->text[0];
4252 if (ch == ',' && !paren && brackets <= 0) {
4253 if (++nparam >= sparam) {
4254 sparam += PARAM_DELTA;
4255 params = nasm_realloc(params,
4256 sparam * sizeof(Token *));
4257 paramsize = nasm_realloc(paramsize,
4258 sparam * sizeof(int));
4260 params[nparam] = tline->next;
4261 paramsize[nparam] = 0;
4262 white = 0;
4263 continue; /* parameter loop */
4265 if (ch == '{' &&
4266 (brackets > 0 || (brackets == 0 &&
4267 !paramsize[nparam])))
4269 if (!(brackets++)) {
4270 params[nparam] = tline->next;
4271 continue; /* parameter loop */
4274 if (ch == '}' && brackets > 0)
4275 if (--brackets == 0) {
4276 brackets = -1;
4277 continue; /* parameter loop */
4279 if (ch == '(' && !brackets)
4280 paren++;
4281 if (ch == ')' && brackets <= 0)
4282 if (--paren < 0)
4283 break;
4285 if (brackets < 0) {
4286 brackets = 0;
4287 nasm_error(ERR_NONFATAL, "braces do not "
4288 "enclose all of macro parameter");
4290 paramsize[nparam] += white + 1;
4291 white = 0;
4292 } /* parameter loop */
4293 nparam++;
4294 while (m && (m->nparam != nparam ||
4295 mstrcmp(m->name, mname,
4296 m->casesense)))
4297 m = m->next;
4298 if (!m)
4299 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4300 "macro `%s' exists, "
4301 "but not taking %d parameters",
4302 mstart->text, nparam);
4305 if (m && m->in_progress)
4306 m = NULL;
4307 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4309 * Design question: should we handle !tline, which
4310 * indicates missing ')' here, or expand those
4311 * macros anyway, which requires the (t) test a few
4312 * lines down?
4314 nasm_free(params);
4315 nasm_free(paramsize);
4316 tline = mstart;
4317 } else {
4319 * Expand the macro: we are placed on the last token of the
4320 * call, so that we can easily split the call from the
4321 * following tokens. We also start by pushing an SMAC_END
4322 * token for the cycle removal.
4324 t = tline;
4325 if (t) {
4326 tline = t->next;
4327 t->next = NULL;
4329 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4330 tt->a.mac = m;
4331 m->in_progress = true;
4332 tline = tt;
4333 list_for_each(t, m->expansion) {
4334 if (t->type >= TOK_SMAC_PARAM) {
4335 Token *pcopy = tline, **ptail = &pcopy;
4336 Token *ttt, *pt;
4337 int i;
4339 ttt = params[t->type - TOK_SMAC_PARAM];
4340 i = paramsize[t->type - TOK_SMAC_PARAM];
4341 while (--i >= 0) {
4342 pt = *ptail = new_Token(tline, ttt->type,
4343 ttt->text, 0);
4344 ptail = &pt->next;
4345 ttt = ttt->next;
4347 tline = pcopy;
4348 } else if (t->type == TOK_PREPROC_Q) {
4349 tt = new_Token(tline, TOK_ID, mname, 0);
4350 tline = tt;
4351 } else if (t->type == TOK_PREPROC_QQ) {
4352 tt = new_Token(tline, TOK_ID, m->name, 0);
4353 tline = tt;
4354 } else {
4355 tt = new_Token(tline, t->type, t->text, 0);
4356 tline = tt;
4361 * Having done that, get rid of the macro call, and clean
4362 * up the parameters.
4364 nasm_free(params);
4365 nasm_free(paramsize);
4366 free_tlist(mstart);
4367 expanded = true;
4368 continue; /* main token loop */
4373 if (tline->type == TOK_SMAC_END) {
4374 tline->a.mac->in_progress = false;
4375 tline = delete_Token(tline);
4376 } else {
4377 t = *tail = tline;
4378 tline = tline->next;
4379 t->a.mac = NULL;
4380 t->next = NULL;
4381 tail = &t->next;
4386 * Now scan the entire line and look for successive TOK_IDs that resulted
4387 * after expansion (they can't be produced by tokenize()). The successive
4388 * TOK_IDs should be concatenated.
4389 * Also we look for %+ tokens and concatenate the tokens before and after
4390 * them (without white spaces in between).
4392 if (expanded) {
4393 const struct tokseq_match t[] = {
4395 PP_CONCAT_MASK(TOK_ID) |
4396 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4397 PP_CONCAT_MASK(TOK_ID) |
4398 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4399 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4402 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4404 * If we concatenated something, *and* we had previously expanded
4405 * an actual macro, scan the lines again for macros...
4407 tline = thead;
4408 expanded = false;
4409 goto again;
4413 err:
4414 if (org_tline) {
4415 if (thead) {
4416 *org_tline = *thead;
4417 /* since we just gave text to org_line, don't free it */
4418 thead->text = NULL;
4419 delete_Token(thead);
4420 } else {
4421 /* the expression expanded to empty line;
4422 we can't return NULL for some reasons
4423 we just set the line to a single WHITESPACE token. */
4424 memset(org_tline, 0, sizeof(*org_tline));
4425 org_tline->text = NULL;
4426 org_tline->type = TOK_WHITESPACE;
4428 thead = org_tline;
4431 return thead;
4435 * Similar to expand_smacro but used exclusively with macro identifiers
4436 * right before they are fetched in. The reason is that there can be
4437 * identifiers consisting of several subparts. We consider that if there
4438 * are more than one element forming the name, user wants a expansion,
4439 * otherwise it will be left as-is. Example:
4441 * %define %$abc cde
4443 * the identifier %$abc will be left as-is so that the handler for %define
4444 * will suck it and define the corresponding value. Other case:
4446 * %define _%$abc cde
4448 * In this case user wants name to be expanded *before* %define starts
4449 * working, so we'll expand %$abc into something (if it has a value;
4450 * otherwise it will be left as-is) then concatenate all successive
4451 * PP_IDs into one.
4453 static Token *expand_id(Token * tline)
4455 Token *cur, *oldnext = NULL;
4457 if (!tline || !tline->next)
4458 return tline;
4460 cur = tline;
4461 while (cur->next &&
4462 (cur->next->type == TOK_ID ||
4463 cur->next->type == TOK_PREPROC_ID
4464 || cur->next->type == TOK_NUMBER))
4465 cur = cur->next;
4467 /* If identifier consists of just one token, don't expand */
4468 if (cur == tline)
4469 return tline;
4471 if (cur) {
4472 oldnext = cur->next; /* Detach the tail past identifier */
4473 cur->next = NULL; /* so that expand_smacro stops here */
4476 tline = expand_smacro(tline);
4478 if (cur) {
4479 /* expand_smacro possibly changhed tline; re-scan for EOL */
4480 cur = tline;
4481 while (cur && cur->next)
4482 cur = cur->next;
4483 if (cur)
4484 cur->next = oldnext;
4487 return tline;
4491 * Determine whether the given line constitutes a multi-line macro
4492 * call, and return the MMacro structure called if so. Doesn't have
4493 * to check for an initial label - that's taken care of in
4494 * expand_mmacro - but must check numbers of parameters. Guaranteed
4495 * to be called with tline->type == TOK_ID, so the putative macro
4496 * name is easy to find.
4498 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4500 MMacro *head, *m;
4501 Token **params;
4502 int nparam;
4504 head = (MMacro *) hash_findix(&mmacros, tline->text);
4507 * Efficiency: first we see if any macro exists with the given
4508 * name. If not, we can return NULL immediately. _Then_ we
4509 * count the parameters, and then we look further along the
4510 * list if necessary to find the proper MMacro.
4512 list_for_each(m, head)
4513 if (!mstrcmp(m->name, tline->text, m->casesense))
4514 break;
4515 if (!m)
4516 return NULL;
4519 * OK, we have a potential macro. Count and demarcate the
4520 * parameters.
4522 count_mmac_params(tline->next, &nparam, &params);
4525 * So we know how many parameters we've got. Find the MMacro
4526 * structure that handles this number.
4528 while (m) {
4529 if (m->nparam_min <= nparam
4530 && (m->plus || nparam <= m->nparam_max)) {
4532 * This one is right. Just check if cycle removal
4533 * prohibits us using it before we actually celebrate...
4535 if (m->in_progress > m->max_depth) {
4536 if (m->max_depth > 0) {
4537 nasm_error(ERR_WARNING,
4538 "reached maximum recursion depth of %i",
4539 m->max_depth);
4541 nasm_free(params);
4542 return NULL;
4545 * It's right, and we can use it. Add its default
4546 * parameters to the end of our list if necessary.
4548 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4549 params =
4550 nasm_realloc(params,
4551 ((m->nparam_min + m->ndefs +
4552 1) * sizeof(*params)));
4553 while (nparam < m->nparam_min + m->ndefs) {
4554 params[nparam] = m->defaults[nparam - m->nparam_min];
4555 nparam++;
4559 * If we've gone over the maximum parameter count (and
4560 * we're in Plus mode), ignore parameters beyond
4561 * nparam_max.
4563 if (m->plus && nparam > m->nparam_max)
4564 nparam = m->nparam_max;
4566 * Then terminate the parameter list, and leave.
4568 if (!params) { /* need this special case */
4569 params = nasm_malloc(sizeof(*params));
4570 nparam = 0;
4572 params[nparam] = NULL;
4573 *params_array = params;
4574 return m;
4577 * This one wasn't right: look for the next one with the
4578 * same name.
4580 list_for_each(m, m->next)
4581 if (!mstrcmp(m->name, tline->text, m->casesense))
4582 break;
4586 * After all that, we didn't find one with the right number of
4587 * parameters. Issue a warning, and fail to expand the macro.
4589 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4590 "macro `%s' exists, but not taking %d parameters",
4591 tline->text, nparam);
4592 nasm_free(params);
4593 return NULL;
4598 * Save MMacro invocation specific fields in
4599 * preparation for a recursive macro expansion
4601 static void push_mmacro(MMacro *m)
4603 MMacroInvocation *i;
4605 i = nasm_malloc(sizeof(MMacroInvocation));
4606 i->prev = m->prev;
4607 i->params = m->params;
4608 i->iline = m->iline;
4609 i->nparam = m->nparam;
4610 i->rotate = m->rotate;
4611 i->paramlen = m->paramlen;
4612 i->unique = m->unique;
4613 i->condcnt = m->condcnt;
4614 m->prev = i;
4619 * Restore MMacro invocation specific fields that were
4620 * saved during a previous recursive macro expansion
4622 static void pop_mmacro(MMacro *m)
4624 MMacroInvocation *i;
4626 if (m->prev) {
4627 i = m->prev;
4628 m->prev = i->prev;
4629 m->params = i->params;
4630 m->iline = i->iline;
4631 m->nparam = i->nparam;
4632 m->rotate = i->rotate;
4633 m->paramlen = i->paramlen;
4634 m->unique = i->unique;
4635 m->condcnt = i->condcnt;
4636 nasm_free(i);
4642 * Expand the multi-line macro call made by the given line, if
4643 * there is one to be expanded. If there is, push the expansion on
4644 * istk->expansion and return 1. Otherwise return 0.
4646 static int expand_mmacro(Token * tline)
4648 Token *startline = tline;
4649 Token *label = NULL;
4650 int dont_prepend = 0;
4651 Token **params, *t, *tt;
4652 MMacro *m;
4653 Line *l, *ll;
4654 int i, nparam, *paramlen;
4655 const char *mname;
4657 t = tline;
4658 skip_white_(t);
4659 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4660 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4661 return 0;
4662 m = is_mmacro(t, &params);
4663 if (m) {
4664 mname = t->text;
4665 } else {
4666 Token *last;
4668 * We have an id which isn't a macro call. We'll assume
4669 * it might be a label; we'll also check to see if a
4670 * colon follows it. Then, if there's another id after
4671 * that lot, we'll check it again for macro-hood.
4673 label = last = t;
4674 t = t->next;
4675 if (tok_type_(t, TOK_WHITESPACE))
4676 last = t, t = t->next;
4677 if (tok_is_(t, ":")) {
4678 dont_prepend = 1;
4679 last = t, t = t->next;
4680 if (tok_type_(t, TOK_WHITESPACE))
4681 last = t, t = t->next;
4683 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4684 return 0;
4685 last->next = NULL;
4686 mname = t->text;
4687 tline = t;
4691 * Fix up the parameters: this involves stripping leading and
4692 * trailing whitespace, then stripping braces if they are
4693 * present.
4695 for (nparam = 0; params[nparam]; nparam++) ;
4696 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4698 for (i = 0; params[i]; i++) {
4699 int brace = 0;
4700 int comma = (!m->plus || i < nparam - 1);
4702 t = params[i];
4703 skip_white_(t);
4704 if (tok_is_(t, "{"))
4705 t = t->next, brace++, comma = false;
4706 params[i] = t;
4707 paramlen[i] = 0;
4708 while (t) {
4709 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4710 break; /* ... because we have hit a comma */
4711 if (comma && t->type == TOK_WHITESPACE
4712 && tok_is_(t->next, ","))
4713 break; /* ... or a space then a comma */
4714 if (brace && t->type == TOK_OTHER) {
4715 if (t->text[0] == '{')
4716 brace++; /* ... or a nested opening brace */
4717 else if (t->text[0] == '}')
4718 if (!--brace)
4719 break; /* ... or a brace */
4721 t = t->next;
4722 paramlen[i]++;
4724 if (brace)
4725 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
4729 * OK, we have a MMacro structure together with a set of
4730 * parameters. We must now go through the expansion and push
4731 * copies of each Line on to istk->expansion. Substitution of
4732 * parameter tokens and macro-local tokens doesn't get done
4733 * until the single-line macro substitution process; this is
4734 * because delaying them allows us to change the semantics
4735 * later through %rotate.
4737 * First, push an end marker on to istk->expansion, mark this
4738 * macro as in progress, and set up its invocation-specific
4739 * variables.
4741 ll = nasm_malloc(sizeof(Line));
4742 ll->next = istk->expansion;
4743 ll->finishes = m;
4744 ll->first = NULL;
4745 istk->expansion = ll;
4748 * Save the previous MMacro expansion in the case of
4749 * macro recursion
4751 if (m->max_depth && m->in_progress)
4752 push_mmacro(m);
4754 m->in_progress ++;
4755 m->params = params;
4756 m->iline = tline;
4757 m->nparam = nparam;
4758 m->rotate = 0;
4759 m->paramlen = paramlen;
4760 m->unique = unique++;
4761 m->lineno = 0;
4762 m->condcnt = 0;
4764 m->next_active = istk->mstk;
4765 istk->mstk = m;
4767 list_for_each(l, m->expansion) {
4768 Token **tail;
4770 ll = nasm_malloc(sizeof(Line));
4771 ll->finishes = NULL;
4772 ll->next = istk->expansion;
4773 istk->expansion = ll;
4774 tail = &ll->first;
4776 list_for_each(t, l->first) {
4777 Token *x = t;
4778 switch (t->type) {
4779 case TOK_PREPROC_Q:
4780 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4781 break;
4782 case TOK_PREPROC_QQ:
4783 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4784 break;
4785 case TOK_PREPROC_ID:
4786 if (t->text[1] == '0' && t->text[2] == '0') {
4787 dont_prepend = -1;
4788 x = label;
4789 if (!x)
4790 continue;
4792 /* fall through */
4793 default:
4794 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4795 break;
4797 tail = &tt->next;
4799 *tail = NULL;
4803 * If we had a label, push it on as the first line of
4804 * the macro expansion.
4806 if (label) {
4807 if (dont_prepend < 0)
4808 free_tlist(startline);
4809 else {
4810 ll = nasm_malloc(sizeof(Line));
4811 ll->finishes = NULL;
4812 ll->next = istk->expansion;
4813 istk->expansion = ll;
4814 ll->first = startline;
4815 if (!dont_prepend) {
4816 while (label->next)
4817 label = label->next;
4818 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4823 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4825 return 1;
4829 * This function adds macro names to error messages, and suppresses
4830 * them if necessary.
4832 static void pp_verror(int severity, const char *fmt, va_list arg)
4834 char buff[BUFSIZ];
4835 MMacro *mmac = NULL;
4836 int delta = 0;
4839 * If we're in a dead branch of IF or something like it, ignore the error.
4840 * However, because %else etc are evaluated in the state context
4841 * of the previous branch, errors might get lost:
4842 * %if 0 ... %else trailing garbage ... %endif
4843 * So %else etc should set the ERR_PP_PRECOND flag.
4845 if ((severity & ERR_MASK) < ERR_FATAL &&
4846 istk && istk->conds &&
4847 ((severity & ERR_PP_PRECOND) ?
4848 istk->conds->state == COND_NEVER :
4849 !emitting(istk->conds->state)))
4850 return;
4852 /* get %macro name */
4853 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
4854 mmac = istk->mstk;
4855 /* but %rep blocks should be skipped */
4856 while (mmac && !mmac->name)
4857 mmac = mmac->next_active, delta++;
4860 if (mmac) {
4861 vsnprintf(buff, sizeof(buff), fmt, arg);
4863 nasm_set_verror(real_verror);
4864 nasm_error(severity, "(%s:%d) %s",
4865 mmac->name, mmac->lineno - delta, buff);
4866 nasm_set_verror(pp_verror);
4867 } else {
4868 real_verror(severity, fmt, arg);
4872 static void
4873 pp_reset(char *file, int apass, StrList **deplist)
4875 Token *t;
4877 cstk = NULL;
4878 istk = nasm_malloc(sizeof(Include));
4879 istk->next = NULL;
4880 istk->conds = NULL;
4881 istk->expansion = NULL;
4882 istk->mstk = NULL;
4883 istk->fp = fopen(file, "r");
4884 istk->fname = NULL;
4885 src_set(0, file);
4886 istk->lineinc = 1;
4887 if (!istk->fp)
4888 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
4889 defining = NULL;
4890 nested_mac_count = 0;
4891 nested_rep_count = 0;
4892 init_macros();
4893 unique = 0;
4894 if (tasm_compatible_mode) {
4895 stdmacpos = nasm_stdmac;
4896 } else {
4897 stdmacpos = nasm_stdmac_after_tasm;
4899 any_extrastdmac = extrastdmac && *extrastdmac;
4900 do_predef = true;
4903 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4904 * The caller, however, will also pass in 3 for preprocess-only so
4905 * we can set __PASS__ accordingly.
4907 pass = apass > 2 ? 2 : apass;
4909 dephead = deptail = deplist;
4910 if (deplist) {
4911 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4912 sl->next = NULL;
4913 strcpy(sl->str, file);
4914 *deptail = sl;
4915 deptail = &sl->next;
4919 * Define the __PASS__ macro. This is defined here unlike
4920 * all the other builtins, because it is special -- it varies between
4921 * passes.
4923 t = nasm_malloc(sizeof(*t));
4924 t->next = NULL;
4925 make_tok_num(t, apass);
4926 t->a.mac = NULL;
4927 define_smacro(NULL, "__PASS__", true, 0, t);
4930 static char *pp_getline(void)
4932 char *line;
4933 Token *tline;
4935 real_verror = nasm_set_verror(pp_verror);
4937 while (1) {
4939 * Fetch a tokenized line, either from the macro-expansion
4940 * buffer or from the input file.
4942 tline = NULL;
4943 while (istk->expansion && istk->expansion->finishes) {
4944 Line *l = istk->expansion;
4945 if (!l->finishes->name && l->finishes->in_progress > 1) {
4946 Line *ll;
4949 * This is a macro-end marker for a macro with no
4950 * name, which means it's not really a macro at all
4951 * but a %rep block, and the `in_progress' field is
4952 * more than 1, meaning that we still need to
4953 * repeat. (1 means the natural last repetition; 0
4954 * means termination by %exitrep.) We have
4955 * therefore expanded up to the %endrep, and must
4956 * push the whole block on to the expansion buffer
4957 * again. We don't bother to remove the macro-end
4958 * marker: we'd only have to generate another one
4959 * if we did.
4961 l->finishes->in_progress--;
4962 list_for_each(l, l->finishes->expansion) {
4963 Token *t, *tt, **tail;
4965 ll = nasm_malloc(sizeof(Line));
4966 ll->next = istk->expansion;
4967 ll->finishes = NULL;
4968 ll->first = NULL;
4969 tail = &ll->first;
4971 list_for_each(t, l->first) {
4972 if (t->text || t->type == TOK_WHITESPACE) {
4973 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4974 tail = &tt->next;
4978 istk->expansion = ll;
4980 } else {
4982 * Check whether a `%rep' was started and not ended
4983 * within this macro expansion. This can happen and
4984 * should be detected. It's a fatal error because
4985 * I'm too confused to work out how to recover
4986 * sensibly from it.
4988 if (defining) {
4989 if (defining->name)
4990 nasm_panic(0, "defining with name in expansion");
4991 else if (istk->mstk->name)
4992 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4993 " expansion of macro `%s'",
4994 istk->mstk->name);
4998 * FIXME: investigate the relationship at this point between
4999 * istk->mstk and l->finishes
5002 MMacro *m = istk->mstk;
5003 istk->mstk = m->next_active;
5004 if (m->name) {
5006 * This was a real macro call, not a %rep, and
5007 * therefore the parameter information needs to
5008 * be freed.
5010 if (m->prev) {
5011 pop_mmacro(m);
5012 l->finishes->in_progress --;
5013 } else {
5014 nasm_free(m->params);
5015 free_tlist(m->iline);
5016 nasm_free(m->paramlen);
5017 l->finishes->in_progress = 0;
5019 } else
5020 free_mmacro(m);
5022 istk->expansion = l->next;
5023 nasm_free(l);
5024 lfmt->downlevel(LIST_MACRO);
5027 while (1) { /* until we get a line we can use */
5029 if (istk->expansion) { /* from a macro expansion */
5030 char *p;
5031 Line *l = istk->expansion;
5032 if (istk->mstk)
5033 istk->mstk->lineno++;
5034 tline = l->first;
5035 istk->expansion = l->next;
5036 nasm_free(l);
5037 p = detoken(tline, false);
5038 lfmt->line(LIST_MACRO, p);
5039 nasm_free(p);
5040 break;
5042 line = read_line();
5043 if (line) { /* from the current input file */
5044 line = prepreproc(line);
5045 tline = tokenize(line);
5046 nasm_free(line);
5047 break;
5050 * The current file has ended; work down the istk
5053 Include *i = istk;
5054 fclose(i->fp);
5055 if (i->conds) {
5056 /* nasm_error can't be conditionally suppressed */
5057 nasm_fatal(0,
5058 "expected `%%endif' before end of file");
5060 /* only set line and file name if there's a next node */
5061 if (i->next)
5062 src_set(i->lineno, i->fname);
5063 istk = i->next;
5064 lfmt->downlevel(LIST_INCLUDE);
5065 nasm_free(i);
5066 if (!istk) {
5067 line = NULL;
5068 goto done;
5070 if (istk->expansion && istk->expansion->finishes)
5071 break;
5076 * We must expand MMacro parameters and MMacro-local labels
5077 * _before_ we plunge into directive processing, to cope
5078 * with things like `%define something %1' such as STRUC
5079 * uses. Unless we're _defining_ a MMacro, in which case
5080 * those tokens should be left alone to go into the
5081 * definition; and unless we're in a non-emitting
5082 * condition, in which case we don't want to meddle with
5083 * anything.
5085 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5086 && !(istk->mstk && !istk->mstk->in_progress)) {
5087 tline = expand_mmac_params(tline);
5091 * Check the line to see if it's a preprocessor directive.
5093 if (do_directive(tline) == DIRECTIVE_FOUND) {
5094 continue;
5095 } else if (defining) {
5097 * We're defining a multi-line macro. We emit nothing
5098 * at all, and just
5099 * shove the tokenized line on to the macro definition.
5101 Line *l = nasm_malloc(sizeof(Line));
5102 l->next = defining->expansion;
5103 l->first = tline;
5104 l->finishes = NULL;
5105 defining->expansion = l;
5106 continue;
5107 } else if (istk->conds && !emitting(istk->conds->state)) {
5109 * We're in a non-emitting branch of a condition block.
5110 * Emit nothing at all, not even a blank line: when we
5111 * emerge from the condition we'll give a line-number
5112 * directive so we keep our place correctly.
5114 free_tlist(tline);
5115 continue;
5116 } else if (istk->mstk && !istk->mstk->in_progress) {
5118 * We're in a %rep block which has been terminated, so
5119 * we're walking through to the %endrep without
5120 * emitting anything. Emit nothing at all, not even a
5121 * blank line: when we emerge from the %rep block we'll
5122 * give a line-number directive so we keep our place
5123 * correctly.
5125 free_tlist(tline);
5126 continue;
5127 } else {
5128 tline = expand_smacro(tline);
5129 if (!expand_mmacro(tline)) {
5131 * De-tokenize the line again, and emit it.
5133 line = detoken(tline, true);
5134 free_tlist(tline);
5135 break;
5136 } else {
5137 continue; /* expand_mmacro calls free_tlist */
5142 done:
5143 nasm_set_verror(real_verror);
5144 return line;
5147 static void pp_cleanup(int pass)
5149 real_verror = nasm_set_verror(pp_verror);
5151 if (defining) {
5152 if (defining->name) {
5153 nasm_error(ERR_NONFATAL,
5154 "end of file while still defining macro `%s'",
5155 defining->name);
5156 } else {
5157 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
5160 free_mmacro(defining);
5161 defining = NULL;
5164 nasm_set_verror(real_verror);
5166 while (cstk)
5167 ctx_pop();
5168 free_macros();
5169 while (istk) {
5170 Include *i = istk;
5171 istk = istk->next;
5172 fclose(i->fp);
5173 nasm_free(i);
5175 while (cstk)
5176 ctx_pop();
5177 src_set_fname(NULL);
5178 if (pass == 0) {
5179 IncPath *i;
5180 free_llist(predef);
5181 predef = NULL;
5182 delete_Blocks();
5183 freeTokens = NULL;
5184 while ((i = ipath)) {
5185 ipath = i->next;
5186 if (i->path)
5187 nasm_free(i->path);
5188 nasm_free(i);
5193 static void pp_include_path(char *path)
5195 IncPath *i;
5197 i = nasm_malloc(sizeof(IncPath));
5198 i->path = path ? nasm_strdup(path) : NULL;
5199 i->next = NULL;
5201 if (ipath) {
5202 IncPath *j = ipath;
5203 while (j->next)
5204 j = j->next;
5205 j->next = i;
5206 } else {
5207 ipath = i;
5211 static void pp_pre_include(char *fname)
5213 Token *inc, *space, *name;
5214 Line *l;
5216 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5217 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5218 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5220 l = nasm_malloc(sizeof(Line));
5221 l->next = predef;
5222 l->first = inc;
5223 l->finishes = NULL;
5224 predef = l;
5227 static void pp_pre_define(char *definition)
5229 Token *def, *space;
5230 Line *l;
5231 char *equals;
5233 real_verror = nasm_set_verror(pp_verror);
5235 equals = strchr(definition, '=');
5236 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5237 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5238 if (equals)
5239 *equals = ' ';
5240 space->next = tokenize(definition);
5241 if (equals)
5242 *equals = '=';
5244 if (space->next->type != TOK_PREPROC_ID &&
5245 space->next->type != TOK_ID)
5246 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
5248 l = nasm_malloc(sizeof(Line));
5249 l->next = predef;
5250 l->first = def;
5251 l->finishes = NULL;
5252 predef = l;
5254 nasm_set_verror(real_verror);
5257 static void pp_pre_undefine(char *definition)
5259 Token *def, *space;
5260 Line *l;
5262 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5263 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5264 space->next = tokenize(definition);
5266 l = nasm_malloc(sizeof(Line));
5267 l->next = predef;
5268 l->first = def;
5269 l->finishes = NULL;
5270 predef = l;
5273 static void pp_extra_stdmac(macros_t *macros)
5275 extrastdmac = macros;
5278 static void make_tok_num(Token * tok, int64_t val)
5280 char numbuf[32];
5281 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5282 tok->text = nasm_strdup(numbuf);
5283 tok->type = TOK_NUMBER;
5286 static void pp_list_one_macro(MMacro *m, int severity)
5288 if (!m)
5289 return;
5291 /* We need to print the next_active list in reverse order */
5292 pp_list_one_macro(m->next_active, severity);
5294 if (m->name && !m->nolist) {
5295 src_set(m->xline + m->lineno, m->fname);
5296 nasm_error(severity, "... from macro `%s' defined here", m->name);
5300 static void pp_error_list_macros(int severity)
5302 int32_t saved_line;
5303 const char *saved_fname = NULL;
5305 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
5306 src_get(&saved_line, &saved_fname);
5308 if (istk)
5309 pp_list_one_macro(istk->mstk, severity);
5311 src_set(saved_line, saved_fname);
5314 const struct preproc_ops nasmpp = {
5315 pp_reset,
5316 pp_getline,
5317 pp_cleanup,
5318 pp_extra_stdmac,
5319 pp_pre_define,
5320 pp_pre_undefine,
5321 pp_pre_include,
5322 pp_include_path,
5323 pp_error_list_macros,