preproc: BR 2222615: fix segfault on bogus %ifmacro
[nasm.git] / preproc.c
blobc89982888ae3bdd2d12f1ff67cf506c3bc88d5c3
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include "compiler.h"
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51 #include "quote.h"
52 #include "stdscan.h"
53 #include "tokens.h"
54 #include "tables.h"
56 typedef struct SMacro SMacro;
57 typedef struct MMacro MMacro;
58 typedef struct Context Context;
59 typedef struct Token Token;
60 typedef struct Blocks Blocks;
61 typedef struct Line Line;
62 typedef struct Include Include;
63 typedef struct Cond Cond;
64 typedef struct IncPath IncPath;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
78 struct SMacro {
79 SMacro *next;
80 char *name;
81 bool casesense;
82 bool in_progress;
83 unsigned int nparam;
84 Token *expansion;
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
104 struct MMacro {
105 MMacro *next;
106 char *name;
107 int nparam_min, nparam_max;
108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
111 int64_t in_progress;
112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
115 Line *expansion;
117 MMacro *next_active;
118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
121 unsigned int nparam, rotate;
122 int *paramlen;
123 uint64_t unique;
124 int lineno; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
130 struct Context {
131 Context *next;
132 char *name;
133 struct hash_table localmac;
134 uint32_t number;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
156 enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
166 struct Token {
167 Token *next;
168 char *text;
169 union {
170 SMacro *mac; /* associated macro for TOK_SMAC_END */
171 size_t len; /* scratch length field */
172 } a; /* Auxiliary data */
173 enum pp_token_type type;
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
179 * lists of Tokens.
181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
196 * the line is blank.
198 struct Line {
199 Line *next;
200 MMacro *finishes;
201 Token *first;
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
208 struct Include {
209 Include *next;
210 FILE *fp;
211 Cond *conds;
212 Line *expansion;
213 char *fname;
214 int lineno, lineinc;
215 MMacro *mstk; /* stack of active macros/reps */
219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
223 struct IncPath {
224 IncPath *next;
225 char *path;
229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
235 struct Cond {
236 Cond *next;
237 int state;
239 enum {
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
248 COND_IF_TRUE, COND_IF_FALSE,
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
254 COND_ELSE_TRUE, COND_ELSE_FALSE,
256 * These states mean that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. COND_DONE is
258 * used when we've had our moment of emission
259 * and have now started seeing %elifs. COND_NEVER is used when
260 * the condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct,
262 * or if there is an error.
264 COND_DONE, COND_NEVER
266 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
269 * These defines are used as the possible return values for do_directive
271 #define NO_DIRECTIVE_FOUND 0
272 #define DIRECTIVE_FOUND 1
275 * Condition codes. Note that we use c_ prefix not C_ because C_ is
276 * used in nasm.h for the "real" condition codes. At _this_ level,
277 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
278 * ones, so we need a different enum...
280 static const char * const conditions[] = {
281 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
282 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
283 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
285 enum pp_conds {
286 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
287 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
288 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
289 c_none = -1
291 static const enum pp_conds inverse_ccs[] = {
292 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
293 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,
294 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
298 * Directive names.
300 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
301 static int is_condition(enum preproc_token arg)
303 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
306 /* For TASM compatibility we need to be able to recognise TASM compatible
307 * conditional compilation directives. Using the NASM pre-processor does
308 * not work, so we look for them specifically from the following list and
309 * then jam in the equivalent NASM directive into the input stream.
312 enum {
313 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
314 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
317 static const char * const tasm_directives[] = {
318 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
319 "ifndef", "include", "local"
322 static int StackSize = 4;
323 static char *StackPointer = "ebp";
324 static int ArgOffset = 8;
325 static int LocalOffset = 0;
327 static Context *cstk;
328 static Include *istk;
329 static IncPath *ipath = NULL;
331 static efunc _error; /* Pointer to client-provided error reporting function */
332 static evalfunc evaluate;
334 static int pass; /* HACK: pass 0 = generate dependencies only */
335 static StrList **dephead, **deptail; /* Dependency list */
337 static uint64_t unique; /* unique identifier numbers */
339 static Line *predef = NULL;
340 static bool do_predef;
342 static ListGen *list;
345 * The current set of multi-line macros we have defined.
347 static struct hash_table mmacros;
350 * The current set of single-line macros we have defined.
352 static struct hash_table smacros;
355 * The multi-line macro we are currently defining, or the %rep
356 * block we are currently reading, if any.
358 static MMacro *defining;
360 static uint64_t nested_mac_count;
361 static uint64_t nested_rep_count;
364 * The number of macro parameters to allocate space for at a time.
366 #define PARAM_DELTA 16
369 * The standard macro set: defined in macros.c in the array nasm_stdmac.
370 * This gives our position in the macro set, when we're processing it.
372 static macros_t *stdmacpos;
375 * The extra standard macros that come from the object format, if
376 * any.
378 static macros_t *extrastdmac = NULL;
379 static bool any_extrastdmac;
382 * Tokens are allocated in blocks to improve speed
384 #define TOKEN_BLOCKSIZE 4096
385 static Token *freeTokens = NULL;
386 struct Blocks {
387 Blocks *next;
388 void *chunk;
391 static Blocks blocks = { NULL, NULL };
394 * Forward declarations.
396 static Token *expand_mmac_params(Token * tline);
397 static Token *expand_smacro(Token * tline);
398 static Token *expand_id(Token * tline);
399 static Context *get_ctx(const char *name, bool all_contexts);
400 static void make_tok_num(Token * tok, int64_t val);
401 static void error(int severity, const char *fmt, ...);
402 static void error_precond(int severity, const char *fmt, ...);
403 static void *new_Block(size_t size);
404 static void delete_Blocks(void);
405 static Token *new_Token(Token * next, enum pp_token_type type,
406 const char *text, int txtlen);
407 static Token *delete_Token(Token * t);
410 * Macros for safe checking of token pointers, avoid *(NULL)
412 #define tok_type_(x,t) ((x) && (x)->type == (t))
413 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
414 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
415 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
417 /* Handle TASM specific directives, which do not contain a % in
418 * front of them. We do it here because I could not find any other
419 * place to do it for the moment, and it is a hack (ideally it would
420 * be nice to be able to use the NASM pre-processor to do it).
422 static char *check_tasm_directive(char *line)
424 int32_t i, j, k, m, len;
425 char *p = line, *oldline, oldchar;
427 /* Skip whitespace */
428 while (nasm_isspace(*p) && *p != 0)
429 p++;
431 /* Binary search for the directive name */
432 i = -1;
433 j = elements(tasm_directives);
434 len = 0;
435 while (!nasm_isspace(p[len]) && p[len] != 0)
436 len++;
437 if (len) {
438 oldchar = p[len];
439 p[len] = 0;
440 while (j - i > 1) {
441 k = (j + i) / 2;
442 m = nasm_stricmp(p, tasm_directives[k]);
443 if (m == 0) {
444 /* We have found a directive, so jam a % in front of it
445 * so that NASM will then recognise it as one if it's own.
447 p[len] = oldchar;
448 len = strlen(p);
449 oldline = line;
450 line = nasm_malloc(len + 2);
451 line[0] = '%';
452 if (k == TM_IFDIFI) {
453 /* NASM does not recognise IFDIFI, so we convert it to
454 * %ifdef BOGUS. This is not used in NASM comaptible
455 * code, but does need to parse for the TASM macro
456 * package.
458 strcpy(line + 1, "ifdef BOGUS");
459 } else {
460 memcpy(line + 1, p, len + 1);
462 nasm_free(oldline);
463 return line;
464 } else if (m < 0) {
465 j = k;
466 } else
467 i = k;
469 p[len] = oldchar;
471 return line;
475 * The pre-preprocessing stage... This function translates line
476 * number indications as they emerge from GNU cpp (`# lineno "file"
477 * flags') into NASM preprocessor line number indications (`%line
478 * lineno file').
480 static char *prepreproc(char *line)
482 int lineno, fnlen;
483 char *fname, *oldline;
485 if (line[0] == '#' && line[1] == ' ') {
486 oldline = line;
487 fname = oldline + 2;
488 lineno = atoi(fname);
489 fname += strspn(fname, "0123456789 ");
490 if (*fname == '"')
491 fname++;
492 fnlen = strcspn(fname, "\"");
493 line = nasm_malloc(20 + fnlen);
494 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
495 nasm_free(oldline);
497 if (tasm_compatible_mode)
498 return check_tasm_directive(line);
499 return line;
503 * Free a linked list of tokens.
505 static void free_tlist(Token * list)
507 while (list) {
508 list = delete_Token(list);
513 * Free a linked list of lines.
515 static void free_llist(Line * list)
517 Line *l;
518 while (list) {
519 l = list;
520 list = list->next;
521 free_tlist(l->first);
522 nasm_free(l);
527 * Free an MMacro
529 static void free_mmacro(MMacro * m)
531 nasm_free(m->name);
532 free_tlist(m->dlist);
533 nasm_free(m->defaults);
534 free_llist(m->expansion);
535 nasm_free(m);
539 * Free all currently defined macros, and free the hash tables
541 static void free_smacro_table(struct hash_table *smt)
543 SMacro *s;
544 const char *key;
545 struct hash_tbl_node *it = NULL;
547 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
548 nasm_free((void *)key);
549 while (s) {
550 SMacro *ns = s->next;
551 nasm_free(s->name);
552 free_tlist(s->expansion);
553 nasm_free(s);
554 s = ns;
557 hash_free(smt);
560 static void free_mmacro_table(struct hash_table *mmt)
562 MMacro *m;
563 const char *key;
564 struct hash_tbl_node *it = NULL;
566 it = NULL;
567 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
568 nasm_free((void *)key);
569 while (m) {
570 MMacro *nm = m->next;
571 free_mmacro(m);
572 m = nm;
575 hash_free(mmt);
578 static void free_macros(void)
580 free_smacro_table(&smacros);
581 free_mmacro_table(&mmacros);
585 * Initialize the hash tables
587 static void init_macros(void)
589 hash_init(&smacros, HASH_LARGE);
590 hash_init(&mmacros, HASH_LARGE);
594 * Pop the context stack.
596 static void ctx_pop(void)
598 Context *c = cstk;
600 cstk = cstk->next;
601 free_smacro_table(&c->localmac);
602 nasm_free(c->name);
603 nasm_free(c);
607 * Search for a key in the hash index; adding it if necessary
608 * (in which case we initialize the data pointer to NULL.)
610 static void **
611 hash_findi_add(struct hash_table *hash, const char *str)
613 struct hash_insert hi;
614 void **r;
615 char *strx;
617 r = hash_findi(hash, str, &hi);
618 if (r)
619 return r;
621 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
622 return hash_add(&hi, strx, NULL);
626 * Like hash_findi, but returns the data element rather than a pointer
627 * to it. Used only when not adding a new element, hence no third
628 * argument.
630 static void *
631 hash_findix(struct hash_table *hash, const char *str)
633 void **p;
635 p = hash_findi(hash, str, NULL);
636 return p ? *p : NULL;
639 #define BUF_DELTA 512
641 * Read a line from the top file in istk, handling multiple CR/LFs
642 * at the end of the line read, and handling spurious ^Zs. Will
643 * return lines from the standard macro set if this has not already
644 * been done.
646 static char *read_line(void)
648 char *buffer, *p, *q;
649 int bufsize, continued_count;
651 if (stdmacpos) {
652 unsigned char c;
653 const unsigned char *p = stdmacpos;
654 char *ret, *q;
655 size_t len = 0;
656 while ((c = *p++)) {
657 if (c >= 0x80)
658 len += pp_directives_len[c-0x80]+1;
659 else
660 len++;
662 ret = nasm_malloc(len+1);
663 q = ret;
664 while ((c = *stdmacpos++)) {
665 if (c >= 0x80) {
666 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
667 q += pp_directives_len[c-0x80];
668 *q++ = ' ';
669 } else {
670 *q++ = c;
673 stdmacpos = p;
674 *q = '\0';
676 if (!*stdmacpos) {
677 /* This was the last of the standard macro chain... */
678 stdmacpos = NULL;
679 if (any_extrastdmac) {
680 stdmacpos = extrastdmac;
681 any_extrastdmac = false;
682 } else if (do_predef) {
683 Line *pd, *l;
684 Token *head, **tail, *t;
687 * Nasty hack: here we push the contents of
688 * `predef' on to the top-level expansion stack,
689 * since this is the most convenient way to
690 * implement the pre-include and pre-define
691 * features.
693 for (pd = predef; pd; pd = pd->next) {
694 head = NULL;
695 tail = &head;
696 for (t = pd->first; t; t = t->next) {
697 *tail = new_Token(NULL, t->type, t->text, 0);
698 tail = &(*tail)->next;
700 l = nasm_malloc(sizeof(Line));
701 l->next = istk->expansion;
702 l->first = head;
703 l->finishes = NULL;
704 istk->expansion = l;
706 do_predef = false;
709 return ret;
712 bufsize = BUF_DELTA;
713 buffer = nasm_malloc(BUF_DELTA);
714 p = buffer;
715 continued_count = 0;
716 while (1) {
717 q = fgets(p, bufsize - (p - buffer), istk->fp);
718 if (!q)
719 break;
720 p += strlen(p);
721 if (p > buffer && p[-1] == '\n') {
722 /* Convert backslash-CRLF line continuation sequences into
723 nothing at all (for DOS and Windows) */
724 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
725 p -= 3;
726 *p = 0;
727 continued_count++;
729 /* Also convert backslash-LF line continuation sequences into
730 nothing at all (for Unix) */
731 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
732 p -= 2;
733 *p = 0;
734 continued_count++;
735 } else {
736 break;
739 if (p - buffer > bufsize - 10) {
740 int32_t offset = p - buffer;
741 bufsize += BUF_DELTA;
742 buffer = nasm_realloc(buffer, bufsize);
743 p = buffer + offset; /* prevent stale-pointer problems */
747 if (!q && p == buffer) {
748 nasm_free(buffer);
749 return NULL;
752 src_set_linnum(src_get_linnum() + istk->lineinc +
753 (continued_count * istk->lineinc));
756 * Play safe: remove CRs as well as LFs, if any of either are
757 * present at the end of the line.
759 while (--p >= buffer && (*p == '\n' || *p == '\r'))
760 *p = '\0';
763 * Handle spurious ^Z, which may be inserted into source files
764 * by some file transfer utilities.
766 buffer[strcspn(buffer, "\032")] = '\0';
768 list->line(LIST_READ, buffer);
770 return buffer;
774 * Tokenize a line of text. This is a very simple process since we
775 * don't need to parse the value out of e.g. numeric tokens: we
776 * simply split one string into many.
778 static Token *tokenize(char *line)
780 char *p = line;
781 enum pp_token_type type;
782 Token *list = NULL;
783 Token *t, **tail = &list;
785 while (*line) {
786 p = line;
787 if (*p == '%') {
788 p++;
789 if (nasm_isdigit(*p) ||
790 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
791 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
792 do {
793 p++;
795 while (nasm_isdigit(*p));
796 type = TOK_PREPROC_ID;
797 } else if (*p == '{') {
798 p++;
799 while (*p && *p != '}') {
800 p[-1] = *p;
801 p++;
803 p[-1] = '\0';
804 if (*p)
805 p++;
806 type = TOK_PREPROC_ID;
807 } else if (*p == '?') {
808 type = TOK_PREPROC_Q; /* %? */
809 p++;
810 if (*p == '?') {
811 type = TOK_PREPROC_QQ; /* %?? */
812 p++;
814 } else if (isidchar(*p) ||
815 ((*p == '!' || *p == '%' || *p == '$') &&
816 isidchar(p[1]))) {
817 do {
818 p++;
820 while (isidchar(*p));
821 type = TOK_PREPROC_ID;
822 } else {
823 type = TOK_OTHER;
824 if (*p == '%')
825 p++;
827 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
828 type = TOK_ID;
829 p++;
830 while (*p && isidchar(*p))
831 p++;
832 } else if (*p == '\'' || *p == '"' || *p == '`') {
834 * A string token.
836 type = TOK_STRING;
837 p = nasm_skip_string(p);
839 if (*p) {
840 p++;
841 } else {
842 error(ERR_WARNING|ERR_PASS1, "unterminated string");
843 /* Handling unterminated strings by UNV */
844 /* type = -1; */
846 } else if (isnumstart(*p)) {
847 bool is_hex = false;
848 bool is_float = false;
849 bool has_e = false;
850 char c, *r;
853 * A numeric token.
856 if (*p == '$') {
857 p++;
858 is_hex = true;
861 for (;;) {
862 c = *p++;
864 if (!is_hex && (c == 'e' || c == 'E')) {
865 has_e = true;
866 if (*p == '+' || *p == '-') {
867 /* e can only be followed by +/- if it is either a
868 prefixed hex number or a floating-point number */
869 p++;
870 is_float = true;
872 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
873 is_hex = true;
874 } else if (c == 'P' || c == 'p') {
875 is_float = true;
876 if (*p == '+' || *p == '-')
877 p++;
878 } else if (isnumchar(c) || c == '_')
879 ; /* just advance */
880 else if (c == '.') {
881 /* we need to deal with consequences of the legacy
882 parser, like "1.nolist" being two tokens
883 (TOK_NUMBER, TOK_ID) here; at least give it
884 a shot for now. In the future, we probably need
885 a flex-based scanner with proper pattern matching
886 to do it as well as it can be done. Nothing in
887 the world is going to help the person who wants
888 0x123.p16 interpreted as two tokens, though. */
889 r = p;
890 while (*r == '_')
891 r++;
893 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
894 (!is_hex && (*r == 'e' || *r == 'E')) ||
895 (*r == 'p' || *r == 'P')) {
896 p = r;
897 is_float = true;
898 } else
899 break; /* Terminate the token */
900 } else
901 break;
903 p--; /* Point to first character beyond number */
905 if (has_e && !is_hex) {
906 /* 1e13 is floating-point, but 1e13h is not */
907 is_float = true;
910 type = is_float ? TOK_FLOAT : TOK_NUMBER;
911 } else if (nasm_isspace(*p)) {
912 type = TOK_WHITESPACE;
913 p++;
914 while (*p && nasm_isspace(*p))
915 p++;
917 * Whitespace just before end-of-line is discarded by
918 * pretending it's a comment; whitespace just before a
919 * comment gets lumped into the comment.
921 if (!*p || *p == ';') {
922 type = TOK_COMMENT;
923 while (*p)
924 p++;
926 } else if (*p == ';') {
927 type = TOK_COMMENT;
928 while (*p)
929 p++;
930 } else {
932 * Anything else is an operator of some kind. We check
933 * for all the double-character operators (>>, <<, //,
934 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
935 * else is a single-character operator.
937 type = TOK_OTHER;
938 if ((p[0] == '>' && p[1] == '>') ||
939 (p[0] == '<' && p[1] == '<') ||
940 (p[0] == '/' && p[1] == '/') ||
941 (p[0] == '<' && p[1] == '=') ||
942 (p[0] == '>' && p[1] == '=') ||
943 (p[0] == '=' && p[1] == '=') ||
944 (p[0] == '!' && p[1] == '=') ||
945 (p[0] == '<' && p[1] == '>') ||
946 (p[0] == '&' && p[1] == '&') ||
947 (p[0] == '|' && p[1] == '|') ||
948 (p[0] == '^' && p[1] == '^')) {
949 p++;
951 p++;
954 /* Handling unterminated string by UNV */
955 /*if (type == -1)
957 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
958 t->text[p-line] = *line;
959 tail = &t->next;
961 else */
962 if (type != TOK_COMMENT) {
963 *tail = t = new_Token(NULL, type, line, p - line);
964 tail = &t->next;
966 line = p;
968 return list;
972 * this function allocates a new managed block of memory and
973 * returns a pointer to the block. The managed blocks are
974 * deleted only all at once by the delete_Blocks function.
976 static void *new_Block(size_t size)
978 Blocks *b = &blocks;
980 /* first, get to the end of the linked list */
981 while (b->next)
982 b = b->next;
983 /* now allocate the requested chunk */
984 b->chunk = nasm_malloc(size);
986 /* now allocate a new block for the next request */
987 b->next = nasm_malloc(sizeof(Blocks));
988 /* and initialize the contents of the new block */
989 b->next->next = NULL;
990 b->next->chunk = NULL;
991 return b->chunk;
995 * this function deletes all managed blocks of memory
997 static void delete_Blocks(void)
999 Blocks *a, *b = &blocks;
1002 * keep in mind that the first block, pointed to by blocks
1003 * is a static and not dynamically allocated, so we don't
1004 * free it.
1006 while (b) {
1007 if (b->chunk)
1008 nasm_free(b->chunk);
1009 a = b;
1010 b = b->next;
1011 if (a != &blocks)
1012 nasm_free(a);
1017 * this function creates a new Token and passes a pointer to it
1018 * back to the caller. It sets the type and text elements, and
1019 * also the a.mac and next elements to NULL.
1021 static Token *new_Token(Token * next, enum pp_token_type type,
1022 const char *text, int txtlen)
1024 Token *t;
1025 int i;
1027 if (freeTokens == NULL) {
1028 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1029 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1030 freeTokens[i].next = &freeTokens[i + 1];
1031 freeTokens[i].next = NULL;
1033 t = freeTokens;
1034 freeTokens = t->next;
1035 t->next = next;
1036 t->a.mac = NULL;
1037 t->type = type;
1038 if (type == TOK_WHITESPACE || text == NULL) {
1039 t->text = NULL;
1040 } else {
1041 if (txtlen == 0)
1042 txtlen = strlen(text);
1043 t->text = nasm_malloc(txtlen+1);
1044 memcpy(t->text, text, txtlen);
1045 t->text[txtlen] = '\0';
1047 return t;
1050 static Token *delete_Token(Token * t)
1052 Token *next = t->next;
1053 nasm_free(t->text);
1054 t->next = freeTokens;
1055 freeTokens = t;
1056 return next;
1060 * Convert a line of tokens back into text.
1061 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1062 * will be transformed into ..@ctxnum.xxx
1064 static char *detoken(Token * tlist, bool expand_locals)
1066 Token *t;
1067 int len;
1068 char *line, *p;
1069 const char *q;
1071 len = 0;
1072 for (t = tlist; t; t = t->next) {
1073 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1074 char *p = getenv(t->text + 2);
1075 nasm_free(t->text);
1076 if (p)
1077 t->text = nasm_strdup(p);
1078 else
1079 t->text = NULL;
1081 /* Expand local macros here and not during preprocessing */
1082 if (expand_locals &&
1083 t->type == TOK_PREPROC_ID && t->text &&
1084 t->text[0] == '%' && t->text[1] == '$') {
1085 Context *ctx = get_ctx(t->text, false);
1086 if (ctx) {
1087 char buffer[40];
1088 char *p, *q = t->text + 2;
1090 q += strspn(q, "$");
1091 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1092 p = nasm_strcat(buffer, q);
1093 nasm_free(t->text);
1094 t->text = p;
1097 if (t->type == TOK_WHITESPACE) {
1098 len++;
1099 } else if (t->text) {
1100 len += strlen(t->text);
1103 p = line = nasm_malloc(len + 1);
1104 for (t = tlist; t; t = t->next) {
1105 if (t->type == TOK_WHITESPACE) {
1106 *p++ = ' ';
1107 } else if (t->text) {
1108 q = t->text;
1109 while (*q)
1110 *p++ = *q++;
1113 *p = '\0';
1114 return line;
1118 * A scanner, suitable for use by the expression evaluator, which
1119 * operates on a line of Tokens. Expects a pointer to a pointer to
1120 * the first token in the line to be passed in as its private_data
1121 * field.
1123 * FIX: This really needs to be unified with stdscan.
1125 static int ppscan(void *private_data, struct tokenval *tokval)
1127 Token **tlineptr = private_data;
1128 Token *tline;
1129 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1131 do {
1132 tline = *tlineptr;
1133 *tlineptr = tline ? tline->next : NULL;
1135 while (tline && (tline->type == TOK_WHITESPACE ||
1136 tline->type == TOK_COMMENT));
1138 if (!tline)
1139 return tokval->t_type = TOKEN_EOS;
1141 tokval->t_charptr = tline->text;
1143 if (tline->text[0] == '$' && !tline->text[1])
1144 return tokval->t_type = TOKEN_HERE;
1145 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1146 return tokval->t_type = TOKEN_BASE;
1148 if (tline->type == TOK_ID) {
1149 p = tokval->t_charptr = tline->text;
1150 if (p[0] == '$') {
1151 tokval->t_charptr++;
1152 return tokval->t_type = TOKEN_ID;
1155 for (r = p, s = ourcopy; *r; r++) {
1156 if (r >= p+MAX_KEYWORD)
1157 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1158 *s++ = nasm_tolower(*r);
1160 *s = '\0';
1161 /* right, so we have an identifier sitting in temp storage. now,
1162 * is it actually a register or instruction name, or what? */
1163 return nasm_token_hash(ourcopy, tokval);
1166 if (tline->type == TOK_NUMBER) {
1167 bool rn_error;
1168 tokval->t_integer = readnum(tline->text, &rn_error);
1169 tokval->t_charptr = tline->text;
1170 if (rn_error)
1171 return tokval->t_type = TOKEN_ERRNUM;
1172 else
1173 return tokval->t_type = TOKEN_NUM;
1176 if (tline->type == TOK_FLOAT) {
1177 return tokval->t_type = TOKEN_FLOAT;
1180 if (tline->type == TOK_STRING) {
1181 char bq, *ep;
1183 bq = tline->text[0];
1184 tokval->t_charptr = tline->text;
1185 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1187 if (ep[0] != bq || ep[1] != '\0')
1188 return tokval->t_type = TOKEN_ERRSTR;
1189 else
1190 return tokval->t_type = TOKEN_STR;
1193 if (tline->type == TOK_OTHER) {
1194 if (!strcmp(tline->text, "<<"))
1195 return tokval->t_type = TOKEN_SHL;
1196 if (!strcmp(tline->text, ">>"))
1197 return tokval->t_type = TOKEN_SHR;
1198 if (!strcmp(tline->text, "//"))
1199 return tokval->t_type = TOKEN_SDIV;
1200 if (!strcmp(tline->text, "%%"))
1201 return tokval->t_type = TOKEN_SMOD;
1202 if (!strcmp(tline->text, "=="))
1203 return tokval->t_type = TOKEN_EQ;
1204 if (!strcmp(tline->text, "<>"))
1205 return tokval->t_type = TOKEN_NE;
1206 if (!strcmp(tline->text, "!="))
1207 return tokval->t_type = TOKEN_NE;
1208 if (!strcmp(tline->text, "<="))
1209 return tokval->t_type = TOKEN_LE;
1210 if (!strcmp(tline->text, ">="))
1211 return tokval->t_type = TOKEN_GE;
1212 if (!strcmp(tline->text, "&&"))
1213 return tokval->t_type = TOKEN_DBL_AND;
1214 if (!strcmp(tline->text, "^^"))
1215 return tokval->t_type = TOKEN_DBL_XOR;
1216 if (!strcmp(tline->text, "||"))
1217 return tokval->t_type = TOKEN_DBL_OR;
1221 * We have no other options: just return the first character of
1222 * the token text.
1224 return tokval->t_type = tline->text[0];
1228 * Compare a string to the name of an existing macro; this is a
1229 * simple wrapper which calls either strcmp or nasm_stricmp
1230 * depending on the value of the `casesense' parameter.
1232 static int mstrcmp(const char *p, const char *q, bool casesense)
1234 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1238 * Compare a string to the name of an existing macro; this is a
1239 * simple wrapper which calls either strcmp or nasm_stricmp
1240 * depending on the value of the `casesense' parameter.
1242 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1244 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1248 * Return the Context structure associated with a %$ token. Return
1249 * NULL, having _already_ reported an error condition, if the
1250 * context stack isn't deep enough for the supplied number of $
1251 * signs.
1252 * If all_contexts == true, contexts that enclose current are
1253 * also scanned for such smacro, until it is found; if not -
1254 * only the context that directly results from the number of $'s
1255 * in variable's name.
1257 static Context *get_ctx(const char *name, bool all_contexts)
1259 Context *ctx;
1260 SMacro *m;
1261 int i;
1263 if (!name || name[0] != '%' || name[1] != '$')
1264 return NULL;
1266 if (!cstk) {
1267 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1268 return NULL;
1271 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1272 ctx = ctx->next;
1273 /* i--; Lino - 02/25/02 */
1275 if (!ctx) {
1276 error(ERR_NONFATAL, "`%s': context stack is only"
1277 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1278 return NULL;
1280 if (!all_contexts)
1281 return ctx;
1283 do {
1284 /* Search for this smacro in found context */
1285 m = hash_findix(&ctx->localmac, name);
1286 while (m) {
1287 if (!mstrcmp(m->name, name, m->casesense))
1288 return ctx;
1289 m = m->next;
1291 ctx = ctx->next;
1293 while (ctx);
1294 return NULL;
1298 * Check to see if a file is already in a string list
1300 static bool in_list(const StrList *list, const char *str)
1302 while (list) {
1303 if (!strcmp(list->str, str))
1304 return true;
1305 list = list->next;
1307 return false;
1311 * Open an include file. This routine must always return a valid
1312 * file pointer if it returns - it's responsible for throwing an
1313 * ERR_FATAL and bombing out completely if not. It should also try
1314 * the include path one by one until it finds the file or reaches
1315 * the end of the path.
1317 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1318 bool missing_ok)
1320 FILE *fp;
1321 char *prefix = "";
1322 IncPath *ip = ipath;
1323 int len = strlen(file);
1324 size_t prefix_len = 0;
1325 StrList *sl;
1327 while (1) {
1328 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1329 memcpy(sl->str, prefix, prefix_len);
1330 memcpy(sl->str+prefix_len, file, len+1);
1331 fp = fopen(sl->str, "r");
1332 if (fp && dhead && !in_list(*dhead, sl->str)) {
1333 sl->next = NULL;
1334 **dtail = sl;
1335 *dtail = &sl->next;
1336 } else {
1337 nasm_free(sl);
1339 if (fp)
1340 return fp;
1341 if (!ip) {
1342 if (!missing_ok)
1343 break;
1344 prefix = NULL;
1345 } else {
1346 prefix = ip->path;
1347 ip = ip->next;
1349 if (prefix) {
1350 prefix_len = strlen(prefix);
1351 } else {
1352 /* -MG given and file not found */
1353 if (dhead && !in_list(*dhead, file)) {
1354 sl = nasm_malloc(len+1+sizeof sl->next);
1355 sl->next = NULL;
1356 strcpy(sl->str, file);
1357 **dtail = sl;
1358 *dtail = &sl->next;
1360 return NULL;
1364 error(ERR_FATAL, "unable to open include file `%s'", file);
1365 return NULL; /* never reached - placate compilers */
1369 * Determine if we should warn on defining a single-line macro of
1370 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1371 * return true if _any_ single-line macro of that name is defined.
1372 * Otherwise, will return true if a single-line macro with either
1373 * `nparam' or no parameters is defined.
1375 * If a macro with precisely the right number of parameters is
1376 * defined, or nparam is -1, the address of the definition structure
1377 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1378 * is NULL, no action will be taken regarding its contents, and no
1379 * error will occur.
1381 * Note that this is also called with nparam zero to resolve
1382 * `ifdef'.
1384 * If you already know which context macro belongs to, you can pass
1385 * the context pointer as first parameter; if you won't but name begins
1386 * with %$ the context will be automatically computed. If all_contexts
1387 * is true, macro will be searched in outer contexts as well.
1389 static bool
1390 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1391 bool nocase)
1393 struct hash_table *smtbl;
1394 SMacro *m;
1396 if (ctx) {
1397 smtbl = &ctx->localmac;
1398 } else if (name[0] == '%' && name[1] == '$') {
1399 if (cstk)
1400 ctx = get_ctx(name, false);
1401 if (!ctx)
1402 return false; /* got to return _something_ */
1403 smtbl = &ctx->localmac;
1404 } else {
1405 smtbl = &smacros;
1407 m = (SMacro *) hash_findix(smtbl, name);
1409 while (m) {
1410 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1411 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1412 if (defn) {
1413 if (nparam == (int) m->nparam || nparam == -1)
1414 *defn = m;
1415 else
1416 *defn = NULL;
1418 return true;
1420 m = m->next;
1423 return false;
1427 * Count and mark off the parameters in a multi-line macro call.
1428 * This is called both from within the multi-line macro expansion
1429 * code, and also to mark off the default parameters when provided
1430 * in a %macro definition line.
1432 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1434 int paramsize, brace;
1436 *nparam = paramsize = 0;
1437 *params = NULL;
1438 while (t) {
1439 /* +1: we need space for the final NULL */
1440 if (*nparam+1 >= paramsize) {
1441 paramsize += PARAM_DELTA;
1442 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1444 skip_white_(t);
1445 brace = false;
1446 if (tok_is_(t, "{"))
1447 brace = true;
1448 (*params)[(*nparam)++] = t;
1449 while (tok_isnt_(t, brace ? "}" : ","))
1450 t = t->next;
1451 if (t) { /* got a comma/brace */
1452 t = t->next;
1453 if (brace) {
1455 * Now we've found the closing brace, look further
1456 * for the comma.
1458 skip_white_(t);
1459 if (tok_isnt_(t, ",")) {
1460 error(ERR_NONFATAL,
1461 "braces do not enclose all of macro parameter");
1462 while (tok_isnt_(t, ","))
1463 t = t->next;
1465 if (t)
1466 t = t->next; /* eat the comma */
1473 * Determine whether one of the various `if' conditions is true or
1474 * not.
1476 * We must free the tline we get passed.
1478 static bool if_condition(Token * tline, enum preproc_token ct)
1480 enum pp_conditional i = PP_COND(ct);
1481 bool j;
1482 Token *t, *tt, **tptr, *origline;
1483 struct tokenval tokval;
1484 expr *evalresult;
1485 enum pp_token_type needtype;
1487 origline = tline;
1489 switch (i) {
1490 case PPC_IFCTX:
1491 j = false; /* have we matched yet? */
1492 while (true) {
1493 skip_white_(tline);
1494 if (!tline)
1495 break;
1496 if (tline->type != TOK_ID) {
1497 error(ERR_NONFATAL,
1498 "`%s' expects context identifiers", pp_directives[ct]);
1499 free_tlist(origline);
1500 return -1;
1502 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1503 j = true;
1504 tline = tline->next;
1506 break;
1508 case PPC_IFDEF:
1509 j = false; /* have we matched yet? */
1510 while (tline) {
1511 skip_white_(tline);
1512 if (!tline || (tline->type != TOK_ID &&
1513 (tline->type != TOK_PREPROC_ID ||
1514 tline->text[1] != '$'))) {
1515 error(ERR_NONFATAL,
1516 "`%s' expects macro identifiers", pp_directives[ct]);
1517 goto fail;
1519 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1520 j = true;
1521 tline = tline->next;
1523 break;
1525 case PPC_IFIDN:
1526 case PPC_IFIDNI:
1527 tline = expand_smacro(tline);
1528 t = tt = tline;
1529 while (tok_isnt_(tt, ","))
1530 tt = tt->next;
1531 if (!tt) {
1532 error(ERR_NONFATAL,
1533 "`%s' expects two comma-separated arguments",
1534 pp_directives[ct]);
1535 goto fail;
1537 tt = tt->next;
1538 j = true; /* assume equality unless proved not */
1539 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1540 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1541 error(ERR_NONFATAL, "`%s': more than one comma on line",
1542 pp_directives[ct]);
1543 goto fail;
1545 if (t->type == TOK_WHITESPACE) {
1546 t = t->next;
1547 continue;
1549 if (tt->type == TOK_WHITESPACE) {
1550 tt = tt->next;
1551 continue;
1553 if (tt->type != t->type) {
1554 j = false; /* found mismatching tokens */
1555 break;
1557 /* When comparing strings, need to unquote them first */
1558 if (t->type == TOK_STRING) {
1559 size_t l1 = nasm_unquote(t->text, NULL);
1560 size_t l2 = nasm_unquote(tt->text, NULL);
1562 if (l1 != l2) {
1563 j = false;
1564 break;
1566 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1567 j = false;
1568 break;
1570 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1571 j = false; /* found mismatching tokens */
1572 break;
1575 t = t->next;
1576 tt = tt->next;
1578 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1579 j = false; /* trailing gunk on one end or other */
1580 break;
1582 case PPC_IFMACRO:
1584 bool found = false;
1585 MMacro searching, *mmac;
1587 skip_white_(tline);
1588 tline = expand_id(tline);
1589 if (!tok_type_(tline, TOK_ID)) {
1590 error(ERR_NONFATAL,
1591 "`%s' expects a macro name", pp_directives[ct]);
1592 goto fail;
1594 searching.name = nasm_strdup(tline->text);
1595 searching.casesense = true;
1596 searching.plus = false;
1597 searching.nolist = false;
1598 searching.in_progress = 0;
1599 searching.rep_nest = NULL;
1600 searching.nparam_min = 0;
1601 searching.nparam_max = INT_MAX;
1602 tline = expand_smacro(tline->next);
1603 skip_white_(tline);
1604 if (!tline) {
1605 } else if (!tok_type_(tline, TOK_NUMBER)) {
1606 error(ERR_NONFATAL,
1607 "`%s' expects a parameter count or nothing",
1608 pp_directives[ct]);
1609 } else {
1610 searching.nparam_min = searching.nparam_max =
1611 readnum(tline->text, &j);
1612 if (j)
1613 error(ERR_NONFATAL,
1614 "unable to parse parameter count `%s'",
1615 tline->text);
1617 if (tline && tok_is_(tline->next, "-")) {
1618 tline = tline->next->next;
1619 if (tok_is_(tline, "*"))
1620 searching.nparam_max = INT_MAX;
1621 else if (!tok_type_(tline, TOK_NUMBER))
1622 error(ERR_NONFATAL,
1623 "`%s' expects a parameter count after `-'",
1624 pp_directives[ct]);
1625 else {
1626 searching.nparam_max = readnum(tline->text, &j);
1627 if (j)
1628 error(ERR_NONFATAL,
1629 "unable to parse parameter count `%s'",
1630 tline->text);
1631 if (searching.nparam_min > searching.nparam_max)
1632 error(ERR_NONFATAL,
1633 "minimum parameter count exceeds maximum");
1636 if (tline && tok_is_(tline->next, "+")) {
1637 tline = tline->next;
1638 searching.plus = true;
1640 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1641 while (mmac) {
1642 if (!strcmp(mmac->name, searching.name) &&
1643 (mmac->nparam_min <= searching.nparam_max
1644 || searching.plus)
1645 && (searching.nparam_min <= mmac->nparam_max
1646 || mmac->plus)) {
1647 found = true;
1648 break;
1650 mmac = mmac->next;
1652 if(tline && tline->next)
1653 error(ERR_WARNING|ERR_PASS1,
1654 "trailing garbage after %%ifmacro ignored");
1655 nasm_free(searching.name);
1656 j = found;
1657 break;
1660 case PPC_IFID:
1661 needtype = TOK_ID;
1662 goto iftype;
1663 case PPC_IFNUM:
1664 needtype = TOK_NUMBER;
1665 goto iftype;
1666 case PPC_IFSTR:
1667 needtype = TOK_STRING;
1668 goto iftype;
1670 iftype:
1671 t = tline = expand_smacro(tline);
1673 while (tok_type_(t, TOK_WHITESPACE) ||
1674 (needtype == TOK_NUMBER &&
1675 tok_type_(t, TOK_OTHER) &&
1676 (t->text[0] == '-' || t->text[0] == '+') &&
1677 !t->text[1]))
1678 t = t->next;
1680 j = tok_type_(t, needtype);
1681 break;
1683 case PPC_IFTOKEN:
1684 t = tline = expand_smacro(tline);
1685 while (tok_type_(t, TOK_WHITESPACE))
1686 t = t->next;
1688 j = false;
1689 if (t) {
1690 t = t->next; /* Skip the actual token */
1691 while (tok_type_(t, TOK_WHITESPACE))
1692 t = t->next;
1693 j = !t; /* Should be nothing left */
1695 break;
1697 case PPC_IFEMPTY:
1698 t = tline = expand_smacro(tline);
1699 while (tok_type_(t, TOK_WHITESPACE))
1700 t = t->next;
1702 j = !t; /* Should be empty */
1703 break;
1705 case PPC_IF:
1706 t = tline = expand_smacro(tline);
1707 tptr = &t;
1708 tokval.t_type = TOKEN_INVALID;
1709 evalresult = evaluate(ppscan, tptr, &tokval,
1710 NULL, pass | CRITICAL, error, NULL);
1711 if (!evalresult)
1712 return -1;
1713 if (tokval.t_type)
1714 error(ERR_WARNING|ERR_PASS1,
1715 "trailing garbage after expression ignored");
1716 if (!is_simple(evalresult)) {
1717 error(ERR_NONFATAL,
1718 "non-constant value given to `%s'", pp_directives[ct]);
1719 goto fail;
1721 j = reloc_value(evalresult) != 0;
1722 break;
1724 default:
1725 error(ERR_FATAL,
1726 "preprocessor directive `%s' not yet implemented",
1727 pp_directives[ct]);
1728 goto fail;
1731 free_tlist(origline);
1732 return j ^ PP_NEGATIVE(ct);
1734 fail:
1735 free_tlist(origline);
1736 return -1;
1740 * Common code for defining an smacro
1742 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1743 int nparam, Token *expansion)
1745 SMacro *smac, **smhead;
1746 struct hash_table *smtbl;
1748 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1749 if (!smac) {
1750 error(ERR_WARNING|ERR_PASS1,
1751 "single-line macro `%s' defined both with and"
1752 " without parameters", mname);
1754 /* Some instances of the old code considered this a failure,
1755 some others didn't. What is the right thing to do here? */
1756 free_tlist(expansion);
1757 return false; /* Failure */
1758 } else {
1760 * We're redefining, so we have to take over an
1761 * existing SMacro structure. This means freeing
1762 * what was already in it.
1764 nasm_free(smac->name);
1765 free_tlist(smac->expansion);
1767 } else {
1768 smtbl = ctx ? &ctx->localmac : &smacros;
1769 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1770 smac = nasm_malloc(sizeof(SMacro));
1771 smac->next = *smhead;
1772 *smhead = smac;
1774 smac->name = nasm_strdup(mname);
1775 smac->casesense = casesense;
1776 smac->nparam = nparam;
1777 smac->expansion = expansion;
1778 smac->in_progress = false;
1779 return true; /* Success */
1783 * Undefine an smacro
1785 static void undef_smacro(Context *ctx, const char *mname)
1787 SMacro **smhead, *s, **sp;
1788 struct hash_table *smtbl;
1790 smtbl = ctx ? &ctx->localmac : &smacros;
1791 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1793 if (smhead) {
1795 * We now have a macro name... go hunt for it.
1797 sp = smhead;
1798 while ((s = *sp) != NULL) {
1799 if (!mstrcmp(s->name, mname, s->casesense)) {
1800 *sp = s->next;
1801 nasm_free(s->name);
1802 free_tlist(s->expansion);
1803 nasm_free(s);
1804 } else {
1805 sp = &s->next;
1812 * Parse a mmacro specification.
1814 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1816 bool err;
1818 tline = tline->next;
1819 skip_white_(tline);
1820 tline = expand_id(tline);
1821 if (!tok_type_(tline, TOK_ID)) {
1822 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1823 return false;
1826 def->name = nasm_strdup(tline->text);
1827 def->plus = false;
1828 def->nolist = false;
1829 def->in_progress = 0;
1830 def->rep_nest = NULL;
1831 def->nparam_min = 0;
1832 def->nparam_max = 0;
1834 tline = expand_smacro(tline->next);
1835 skip_white_(tline);
1836 if (!tok_type_(tline, TOK_NUMBER)) {
1837 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1838 } else {
1839 def->nparam_min = def->nparam_max =
1840 readnum(tline->text, &err);
1841 if (err)
1842 error(ERR_NONFATAL,
1843 "unable to parse parameter count `%s'", tline->text);
1845 if (tline && tok_is_(tline->next, "-")) {
1846 tline = tline->next->next;
1847 if (tok_is_(tline, "*")) {
1848 def->nparam_max = INT_MAX;
1849 } else if (!tok_type_(tline, TOK_NUMBER)) {
1850 error(ERR_NONFATAL,
1851 "`%s' expects a parameter count after `-'", directive);
1852 } else {
1853 def->nparam_max = readnum(tline->text, &err);
1854 if (err) {
1855 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1856 tline->text);
1858 if (def->nparam_min > def->nparam_max) {
1859 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1863 if (tline && tok_is_(tline->next, "+")) {
1864 tline = tline->next;
1865 def->plus = true;
1867 if (tline && tok_type_(tline->next, TOK_ID) &&
1868 !nasm_stricmp(tline->next->text, ".nolist")) {
1869 tline = tline->next;
1870 def->nolist = true;
1874 * Handle default parameters.
1876 if (tline && tline->next) {
1877 def->dlist = tline->next;
1878 tline->next = NULL;
1879 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1880 } else {
1881 def->dlist = NULL;
1882 def->defaults = NULL;
1884 def->expansion = NULL;
1886 if(def->defaults &&
1887 def->ndefs > def->nparam_max - def->nparam_min &&
1888 !def->plus)
1889 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1890 "too many default macro parameters");
1892 return true;
1897 * Decode a size directive
1899 static int parse_size(const char *str) {
1900 static const char *size_names[] =
1901 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1902 static const int sizes[] =
1903 { 0, 1, 4, 16, 8, 10, 2, 32 };
1905 return sizes[bsii(str, size_names, elements(size_names))+1];
1909 * find and process preprocessor directive in passed line
1910 * Find out if a line contains a preprocessor directive, and deal
1911 * with it if so.
1913 * If a directive _is_ found, it is the responsibility of this routine
1914 * (and not the caller) to free_tlist() the line.
1916 * @param tline a pointer to the current tokeninzed line linked list
1917 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1920 static int do_directive(Token * tline)
1922 enum preproc_token i;
1923 int j;
1924 bool err;
1925 int nparam;
1926 bool nolist;
1927 bool casesense;
1928 int k, m;
1929 int offset;
1930 char *p, *pp, *mname;
1931 Include *inc;
1932 Context *ctx;
1933 Cond *cond;
1934 MMacro *mmac, **mmhead;
1935 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1936 Line *l;
1937 struct tokenval tokval;
1938 expr *evalresult;
1939 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1940 int64_t count;
1941 size_t len;
1942 int severity;
1944 origline = tline;
1946 skip_white_(tline);
1947 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
1948 (tline->text[1] == '%' || tline->text[1] == '$'
1949 || tline->text[1] == '!'))
1950 return NO_DIRECTIVE_FOUND;
1952 i = pp_token_hash(tline->text);
1955 * If we're in a non-emitting branch of a condition construct,
1956 * or walking to the end of an already terminated %rep block,
1957 * we should ignore all directives except for condition
1958 * directives.
1960 if (((istk->conds && !emitting(istk->conds->state)) ||
1961 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1962 return NO_DIRECTIVE_FOUND;
1966 * If we're defining a macro or reading a %rep block, we should
1967 * ignore all directives except for %macro/%imacro (which nest),
1968 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1969 * If we're in a %rep block, another %rep nests, so should be let through.
1971 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1972 i != PP_ENDMACRO && i != PP_ENDM &&
1973 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1974 return NO_DIRECTIVE_FOUND;
1977 if (defining) {
1978 if (i == PP_MACRO || i == PP_IMACRO) {
1979 nested_mac_count++;
1980 return NO_DIRECTIVE_FOUND;
1981 } else if (nested_mac_count > 0) {
1982 if (i == PP_ENDMACRO) {
1983 nested_mac_count--;
1984 return NO_DIRECTIVE_FOUND;
1987 if (!defining->name) {
1988 if (i == PP_REP) {
1989 nested_rep_count++;
1990 return NO_DIRECTIVE_FOUND;
1991 } else if (nested_rep_count > 0) {
1992 if (i == PP_ENDREP) {
1993 nested_rep_count--;
1994 return NO_DIRECTIVE_FOUND;
2000 switch (i) {
2001 case PP_INVALID:
2002 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2003 tline->text);
2004 return NO_DIRECTIVE_FOUND; /* didn't get it */
2006 case PP_STACKSIZE:
2007 /* Directive to tell NASM what the default stack size is. The
2008 * default is for a 16-bit stack, and this can be overriden with
2009 * %stacksize large.
2010 * the following form:
2012 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2014 tline = tline->next;
2015 if (tline && tline->type == TOK_WHITESPACE)
2016 tline = tline->next;
2017 if (!tline || tline->type != TOK_ID) {
2018 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2019 free_tlist(origline);
2020 return DIRECTIVE_FOUND;
2022 if (nasm_stricmp(tline->text, "flat") == 0) {
2023 /* All subsequent ARG directives are for a 32-bit stack */
2024 StackSize = 4;
2025 StackPointer = "ebp";
2026 ArgOffset = 8;
2027 LocalOffset = 0;
2028 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2029 /* All subsequent ARG directives are for a 64-bit stack */
2030 StackSize = 8;
2031 StackPointer = "rbp";
2032 ArgOffset = 8;
2033 LocalOffset = 0;
2034 } else if (nasm_stricmp(tline->text, "large") == 0) {
2035 /* All subsequent ARG directives are for a 16-bit stack,
2036 * far function call.
2038 StackSize = 2;
2039 StackPointer = "bp";
2040 ArgOffset = 4;
2041 LocalOffset = 0;
2042 } else if (nasm_stricmp(tline->text, "small") == 0) {
2043 /* All subsequent ARG directives are for a 16-bit stack,
2044 * far function call. We don't support near functions.
2046 StackSize = 2;
2047 StackPointer = "bp";
2048 ArgOffset = 6;
2049 LocalOffset = 0;
2050 } else {
2051 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2052 free_tlist(origline);
2053 return DIRECTIVE_FOUND;
2055 free_tlist(origline);
2056 return DIRECTIVE_FOUND;
2058 case PP_ARG:
2059 /* TASM like ARG directive to define arguments to functions, in
2060 * the following form:
2062 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2064 offset = ArgOffset;
2065 do {
2066 char *arg, directive[256];
2067 int size = StackSize;
2069 /* Find the argument name */
2070 tline = tline->next;
2071 if (tline && tline->type == TOK_WHITESPACE)
2072 tline = tline->next;
2073 if (!tline || tline->type != TOK_ID) {
2074 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2075 free_tlist(origline);
2076 return DIRECTIVE_FOUND;
2078 arg = tline->text;
2080 /* Find the argument size type */
2081 tline = tline->next;
2082 if (!tline || tline->type != TOK_OTHER
2083 || tline->text[0] != ':') {
2084 error(ERR_NONFATAL,
2085 "Syntax error processing `%%arg' directive");
2086 free_tlist(origline);
2087 return DIRECTIVE_FOUND;
2089 tline = tline->next;
2090 if (!tline || tline->type != TOK_ID) {
2091 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2092 free_tlist(origline);
2093 return DIRECTIVE_FOUND;
2096 /* Allow macro expansion of type parameter */
2097 tt = tokenize(tline->text);
2098 tt = expand_smacro(tt);
2099 size = parse_size(tt->text);
2100 if (!size) {
2101 error(ERR_NONFATAL,
2102 "Invalid size type for `%%arg' missing directive");
2103 free_tlist(tt);
2104 free_tlist(origline);
2105 return DIRECTIVE_FOUND;
2107 free_tlist(tt);
2109 /* Round up to even stack slots */
2110 size = (size+StackSize-1) & ~(StackSize-1);
2112 /* Now define the macro for the argument */
2113 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2114 arg, StackPointer, offset);
2115 do_directive(tokenize(directive));
2116 offset += size;
2118 /* Move to the next argument in the list */
2119 tline = tline->next;
2120 if (tline && tline->type == TOK_WHITESPACE)
2121 tline = tline->next;
2122 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2123 ArgOffset = offset;
2124 free_tlist(origline);
2125 return DIRECTIVE_FOUND;
2127 case PP_LOCAL:
2128 /* TASM like LOCAL directive to define local variables for a
2129 * function, in the following form:
2131 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2133 * The '= LocalSize' at the end is ignored by NASM, but is
2134 * required by TASM to define the local parameter size (and used
2135 * by the TASM macro package).
2137 offset = LocalOffset;
2138 do {
2139 char *local, directive[256];
2140 int size = StackSize;
2142 /* Find the argument name */
2143 tline = tline->next;
2144 if (tline && tline->type == TOK_WHITESPACE)
2145 tline = tline->next;
2146 if (!tline || tline->type != TOK_ID) {
2147 error(ERR_NONFATAL,
2148 "`%%local' missing argument parameter");
2149 free_tlist(origline);
2150 return DIRECTIVE_FOUND;
2152 local = tline->text;
2154 /* Find the argument size type */
2155 tline = tline->next;
2156 if (!tline || tline->type != TOK_OTHER
2157 || tline->text[0] != ':') {
2158 error(ERR_NONFATAL,
2159 "Syntax error processing `%%local' directive");
2160 free_tlist(origline);
2161 return DIRECTIVE_FOUND;
2163 tline = tline->next;
2164 if (!tline || tline->type != TOK_ID) {
2165 error(ERR_NONFATAL,
2166 "`%%local' missing size type parameter");
2167 free_tlist(origline);
2168 return DIRECTIVE_FOUND;
2171 /* Allow macro expansion of type parameter */
2172 tt = tokenize(tline->text);
2173 tt = expand_smacro(tt);
2174 size = parse_size(tt->text);
2175 if (!size) {
2176 error(ERR_NONFATAL,
2177 "Invalid size type for `%%local' missing directive");
2178 free_tlist(tt);
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
2182 free_tlist(tt);
2184 /* Round up to even stack slots */
2185 size = (size+StackSize-1) & ~(StackSize-1);
2187 offset += size; /* Negative offset, increment before */
2189 /* Now define the macro for the argument */
2190 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2191 local, StackPointer, offset);
2192 do_directive(tokenize(directive));
2194 /* Now define the assign to setup the enter_c macro correctly */
2195 snprintf(directive, sizeof(directive),
2196 "%%assign %%$localsize %%$localsize+%d", size);
2197 do_directive(tokenize(directive));
2199 /* Move to the next argument in the list */
2200 tline = tline->next;
2201 if (tline && tline->type == TOK_WHITESPACE)
2202 tline = tline->next;
2203 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2204 LocalOffset = offset;
2205 free_tlist(origline);
2206 return DIRECTIVE_FOUND;
2208 case PP_CLEAR:
2209 if (tline->next)
2210 error(ERR_WARNING|ERR_PASS1,
2211 "trailing garbage after `%%clear' ignored");
2212 free_macros();
2213 init_macros();
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
2217 case PP_DEPEND:
2218 t = tline->next = expand_smacro(tline->next);
2219 skip_white_(t);
2220 if (!t || (t->type != TOK_STRING &&
2221 t->type != TOK_INTERNAL_STRING)) {
2222 error(ERR_NONFATAL, "`%%depend' expects a file name");
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND; /* but we did _something_ */
2226 if (t->next)
2227 error(ERR_WARNING|ERR_PASS1,
2228 "trailing garbage after `%%depend' ignored");
2229 p = t->text;
2230 if (t->type != TOK_INTERNAL_STRING)
2231 nasm_unquote(p, NULL);
2232 if (dephead && !in_list(*dephead, p)) {
2233 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2234 sl->next = NULL;
2235 strcpy(sl->str, p);
2236 *deptail = sl;
2237 deptail = &sl->next;
2239 free_tlist(origline);
2240 return DIRECTIVE_FOUND;
2242 case PP_INCLUDE:
2243 t = tline->next = expand_smacro(tline->next);
2244 skip_white_(t);
2246 if (!t || (t->type != TOK_STRING &&
2247 t->type != TOK_INTERNAL_STRING)) {
2248 error(ERR_NONFATAL, "`%%include' expects a file name");
2249 free_tlist(origline);
2250 return DIRECTIVE_FOUND; /* but we did _something_ */
2252 if (t->next)
2253 error(ERR_WARNING|ERR_PASS1,
2254 "trailing garbage after `%%include' ignored");
2255 p = t->text;
2256 if (t->type != TOK_INTERNAL_STRING)
2257 nasm_unquote(p, NULL);
2258 inc = nasm_malloc(sizeof(Include));
2259 inc->next = istk;
2260 inc->conds = NULL;
2261 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2262 if (!inc->fp) {
2263 /* -MG given but file not found */
2264 nasm_free(inc);
2265 } else {
2266 inc->fname = src_set_fname(nasm_strdup(p));
2267 inc->lineno = src_set_linnum(0);
2268 inc->lineinc = 1;
2269 inc->expansion = NULL;
2270 inc->mstk = NULL;
2271 istk = inc;
2272 list->uplevel(LIST_INCLUDE);
2274 free_tlist(origline);
2275 return DIRECTIVE_FOUND;
2277 case PP_USE:
2279 static macros_t *use_pkg;
2280 const char *pkg_macro;
2282 t = tline->next = expand_smacro(tline->next);
2283 skip_white_(t);
2285 if (!t || (t->type != TOK_STRING &&
2286 t->type != TOK_INTERNAL_STRING &&
2287 t->type != TOK_ID)) {
2288 error(ERR_NONFATAL, "`%%use' expects a package name");
2289 free_tlist(origline);
2290 return DIRECTIVE_FOUND; /* but we did _something_ */
2292 if (t->next)
2293 error(ERR_WARNING|ERR_PASS1,
2294 "trailing garbage after `%%use' ignored");
2295 if (t->type == TOK_STRING)
2296 nasm_unquote(t->text, NULL);
2297 use_pkg = nasm_stdmac_find_package(t->text);
2298 if (!use_pkg)
2299 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
2300 /* The first string will be <%define>__USE_*__ */
2301 pkg_macro = (char *)use_pkg + 1;
2302 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2303 /* Not already included, go ahead and include it */
2304 stdmacpos = use_pkg;
2306 free_tlist(origline);
2307 return DIRECTIVE_FOUND;
2309 case PP_PUSH:
2310 tline = tline->next;
2311 skip_white_(tline);
2312 tline = expand_id(tline);
2313 if (tline) {
2314 if (!tok_type_(tline, TOK_ID)) {
2315 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2316 free_tlist(origline);
2317 return DIRECTIVE_FOUND; /* but we did _something_ */
2319 if (tline->next)
2320 error(ERR_WARNING|ERR_PASS1,
2321 "trailing garbage after `%%push' ignored");
2322 p = nasm_strdup(tline->text);
2323 } else {
2324 p = NULL; /* Anonymous context */
2326 ctx = nasm_malloc(sizeof(Context));
2327 ctx->next = cstk;
2328 hash_init(&ctx->localmac, HASH_SMALL);
2329 ctx->name = p;
2330 ctx->number = unique++;
2331 cstk = ctx;
2332 free_tlist(origline);
2333 return DIRECTIVE_FOUND;
2335 case PP_REPL:
2336 tline = tline->next;
2337 skip_white_(tline);
2338 tline = expand_id(tline);
2339 if (tline) {
2340 if (!tok_type_(tline, TOK_ID)) {
2341 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2342 free_tlist(origline);
2343 return DIRECTIVE_FOUND; /* but we did _something_ */
2345 if (tline->next)
2346 error(ERR_WARNING|ERR_PASS1,
2347 "trailing garbage after `%%repl' ignored");
2348 p = nasm_strdup(tline->text);
2349 } else {
2350 p = NULL;
2352 if (!cstk)
2353 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2354 else {
2355 nasm_free(cstk->name);
2356 cstk->name = p;
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2361 case PP_POP:
2362 if (tline->next)
2363 error(ERR_WARNING|ERR_PASS1,
2364 "trailing garbage after `%%pop' ignored");
2365 if (!cstk)
2366 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2367 else
2368 ctx_pop();
2369 free_tlist(origline);
2370 return DIRECTIVE_FOUND;
2372 case PP_FATAL:
2373 severity = ERR_FATAL|ERR_NO_SEVERITY;
2374 goto issue_error;
2375 case PP_ERROR:
2376 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2377 goto issue_error;
2378 case PP_WARNING:
2379 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
2380 goto issue_error;
2382 issue_error:
2384 /* Only error out if this is the final pass */
2385 if (pass != 2 && i != PP_FATAL)
2386 return DIRECTIVE_FOUND;
2388 tline->next = expand_smacro(tline->next);
2389 tline = tline->next;
2390 skip_white_(tline);
2391 t = tline ? tline->next : NULL;
2392 skip_white_(t);
2393 if (tok_type_(tline, TOK_STRING) && !t) {
2394 /* The line contains only a quoted string */
2395 p = tline->text;
2396 nasm_unquote(p, NULL);
2397 error(severity, "%s: %s", pp_directives[i], p);
2398 } else {
2399 /* Not a quoted string, or more than a quoted string */
2400 p = detoken(tline, false);
2401 error(severity, "%s: %s", pp_directives[i], p);
2402 nasm_free(p);
2404 free_tlist(origline);
2405 return DIRECTIVE_FOUND;
2408 CASE_PP_IF:
2409 if (istk->conds && !emitting(istk->conds->state))
2410 j = COND_NEVER;
2411 else {
2412 j = if_condition(tline->next, i);
2413 tline->next = NULL; /* it got freed */
2414 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2416 cond = nasm_malloc(sizeof(Cond));
2417 cond->next = istk->conds;
2418 cond->state = j;
2419 istk->conds = cond;
2420 free_tlist(origline);
2421 return DIRECTIVE_FOUND;
2423 CASE_PP_ELIF:
2424 if (!istk->conds)
2425 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2426 switch(istk->conds->state) {
2427 case COND_IF_TRUE:
2428 istk->conds->state = COND_DONE;
2429 break;
2431 case COND_DONE:
2432 case COND_NEVER:
2433 break;
2435 case COND_ELSE_TRUE:
2436 case COND_ELSE_FALSE:
2437 error_precond(ERR_WARNING|ERR_PASS1,
2438 "`%%elif' after `%%else' ignored");
2439 istk->conds->state = COND_NEVER;
2440 break;
2442 case COND_IF_FALSE:
2444 * IMPORTANT: In the case of %if, we will already have
2445 * called expand_mmac_params(); however, if we're
2446 * processing an %elif we must have been in a
2447 * non-emitting mode, which would have inhibited
2448 * the normal invocation of expand_mmac_params(). Therefore,
2449 * we have to do it explicitly here.
2451 j = if_condition(expand_mmac_params(tline->next), i);
2452 tline->next = NULL; /* it got freed */
2453 istk->conds->state =
2454 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2455 break;
2457 free_tlist(origline);
2458 return DIRECTIVE_FOUND;
2460 case PP_ELSE:
2461 if (tline->next)
2462 error_precond(ERR_WARNING|ERR_PASS1,
2463 "trailing garbage after `%%else' ignored");
2464 if (!istk->conds)
2465 error(ERR_FATAL, "`%%else': no matching `%%if'");
2466 switch(istk->conds->state) {
2467 case COND_IF_TRUE:
2468 case COND_DONE:
2469 istk->conds->state = COND_ELSE_FALSE;
2470 break;
2472 case COND_NEVER:
2473 break;
2475 case COND_IF_FALSE:
2476 istk->conds->state = COND_ELSE_TRUE;
2477 break;
2479 case COND_ELSE_TRUE:
2480 case COND_ELSE_FALSE:
2481 error_precond(ERR_WARNING|ERR_PASS1,
2482 "`%%else' after `%%else' ignored.");
2483 istk->conds->state = COND_NEVER;
2484 break;
2486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
2489 case PP_ENDIF:
2490 if (tline->next)
2491 error_precond(ERR_WARNING|ERR_PASS1,
2492 "trailing garbage after `%%endif' ignored");
2493 if (!istk->conds)
2494 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2495 cond = istk->conds;
2496 istk->conds = cond->next;
2497 nasm_free(cond);
2498 free_tlist(origline);
2499 return DIRECTIVE_FOUND;
2501 case PP_MACRO:
2502 case PP_IMACRO:
2503 if (defining) {
2504 error(ERR_FATAL,
2505 "`%%%smacro': already defining a macro",
2506 (i == PP_IMACRO ? "i" : ""));
2507 return DIRECTIVE_FOUND;
2509 defining = nasm_malloc(sizeof(MMacro));
2510 defining->casesense = (i == PP_MACRO);
2511 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2512 nasm_free(defining);
2513 defining = NULL;
2514 return DIRECTIVE_FOUND;
2517 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2518 while (mmac) {
2519 if (!strcmp(mmac->name, defining->name) &&
2520 (mmac->nparam_min <= defining->nparam_max
2521 || defining->plus)
2522 && (defining->nparam_min <= mmac->nparam_max
2523 || mmac->plus)) {
2524 error(ERR_WARNING|ERR_PASS1,
2525 "redefining multi-line macro `%s'", defining->name);
2526 return DIRECTIVE_FOUND;
2528 mmac = mmac->next;
2530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
2533 case PP_ENDM:
2534 case PP_ENDMACRO:
2535 if (! (defining && defining->name)) {
2536 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2537 return DIRECTIVE_FOUND;
2539 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2540 defining->next = *mmhead;
2541 *mmhead = defining;
2542 defining = NULL;
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
2546 case PP_UNMACRO:
2547 case PP_UNIMACRO:
2549 MMacro **mmac_p;
2550 MMacro spec;
2552 spec.casesense = (i == PP_UNMACRO);
2553 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2554 return DIRECTIVE_FOUND;
2556 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2557 while (mmac_p && *mmac_p) {
2558 mmac = *mmac_p;
2559 if (mmac->casesense == spec.casesense &&
2560 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2561 mmac->nparam_min == spec.nparam_min &&
2562 mmac->nparam_max == spec.nparam_max &&
2563 mmac->plus == spec.plus) {
2564 *mmac_p = mmac->next;
2565 free_mmacro(mmac);
2566 } else {
2567 mmac_p = &mmac->next;
2570 free_tlist(origline);
2571 free_tlist(spec.dlist);
2572 return DIRECTIVE_FOUND;
2575 case PP_ROTATE:
2576 if (tline->next && tline->next->type == TOK_WHITESPACE)
2577 tline = tline->next;
2578 if (tline->next == NULL) {
2579 free_tlist(origline);
2580 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2581 return DIRECTIVE_FOUND;
2583 t = expand_smacro(tline->next);
2584 tline->next = NULL;
2585 free_tlist(origline);
2586 tline = t;
2587 tptr = &t;
2588 tokval.t_type = TOKEN_INVALID;
2589 evalresult =
2590 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2591 free_tlist(tline);
2592 if (!evalresult)
2593 return DIRECTIVE_FOUND;
2594 if (tokval.t_type)
2595 error(ERR_WARNING|ERR_PASS1,
2596 "trailing garbage after expression ignored");
2597 if (!is_simple(evalresult)) {
2598 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2599 return DIRECTIVE_FOUND;
2601 mmac = istk->mstk;
2602 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2603 mmac = mmac->next_active;
2604 if (!mmac) {
2605 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2606 } else if (mmac->nparam == 0) {
2607 error(ERR_NONFATAL,
2608 "`%%rotate' invoked within macro without parameters");
2609 } else {
2610 int rotate = mmac->rotate + reloc_value(evalresult);
2612 rotate %= (int)mmac->nparam;
2613 if (rotate < 0)
2614 rotate += mmac->nparam;
2616 mmac->rotate = rotate;
2618 return DIRECTIVE_FOUND;
2620 case PP_REP:
2621 nolist = false;
2622 do {
2623 tline = tline->next;
2624 } while (tok_type_(tline, TOK_WHITESPACE));
2626 if (tok_type_(tline, TOK_ID) &&
2627 nasm_stricmp(tline->text, ".nolist") == 0) {
2628 nolist = true;
2629 do {
2630 tline = tline->next;
2631 } while (tok_type_(tline, TOK_WHITESPACE));
2634 if (tline) {
2635 t = expand_smacro(tline);
2636 tptr = &t;
2637 tokval.t_type = TOKEN_INVALID;
2638 evalresult =
2639 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2640 if (!evalresult) {
2641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
2644 if (tokval.t_type)
2645 error(ERR_WARNING|ERR_PASS1,
2646 "trailing garbage after expression ignored");
2647 if (!is_simple(evalresult)) {
2648 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2649 return DIRECTIVE_FOUND;
2651 count = reloc_value(evalresult) + 1;
2652 } else {
2653 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2654 count = 0;
2656 free_tlist(origline);
2658 tmp_defining = defining;
2659 defining = nasm_malloc(sizeof(MMacro));
2660 defining->name = NULL; /* flags this macro as a %rep block */
2661 defining->casesense = false;
2662 defining->plus = false;
2663 defining->nolist = nolist;
2664 defining->in_progress = count;
2665 defining->nparam_min = defining->nparam_max = 0;
2666 defining->defaults = NULL;
2667 defining->dlist = NULL;
2668 defining->expansion = NULL;
2669 defining->next_active = istk->mstk;
2670 defining->rep_nest = tmp_defining;
2671 return DIRECTIVE_FOUND;
2673 case PP_ENDREP:
2674 if (!defining || defining->name) {
2675 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2676 return DIRECTIVE_FOUND;
2680 * Now we have a "macro" defined - although it has no name
2681 * and we won't be entering it in the hash tables - we must
2682 * push a macro-end marker for it on to istk->expansion.
2683 * After that, it will take care of propagating itself (a
2684 * macro-end marker line for a macro which is really a %rep
2685 * block will cause the macro to be re-expanded, complete
2686 * with another macro-end marker to ensure the process
2687 * continues) until the whole expansion is forcibly removed
2688 * from istk->expansion by a %exitrep.
2690 l = nasm_malloc(sizeof(Line));
2691 l->next = istk->expansion;
2692 l->finishes = defining;
2693 l->first = NULL;
2694 istk->expansion = l;
2696 istk->mstk = defining;
2698 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2699 tmp_defining = defining;
2700 defining = defining->rep_nest;
2701 free_tlist(origline);
2702 return DIRECTIVE_FOUND;
2704 case PP_EXITREP:
2706 * We must search along istk->expansion until we hit a
2707 * macro-end marker for a macro with no name. Then we set
2708 * its `in_progress' flag to 0.
2710 for (l = istk->expansion; l; l = l->next)
2711 if (l->finishes && !l->finishes->name)
2712 break;
2714 if (l)
2715 l->finishes->in_progress = 1;
2716 else
2717 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
2721 case PP_XDEFINE:
2722 case PP_IXDEFINE:
2723 case PP_DEFINE:
2724 case PP_IDEFINE:
2725 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2727 tline = tline->next;
2728 skip_white_(tline);
2729 tline = expand_id(tline);
2730 if (!tline || (tline->type != TOK_ID &&
2731 (tline->type != TOK_PREPROC_ID ||
2732 tline->text[1] != '$'))) {
2733 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2734 pp_directives[i]);
2735 free_tlist(origline);
2736 return DIRECTIVE_FOUND;
2739 ctx = get_ctx(tline->text, false);
2741 mname = tline->text;
2742 last = tline;
2743 param_start = tline = tline->next;
2744 nparam = 0;
2746 /* Expand the macro definition now for %xdefine and %ixdefine */
2747 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2748 tline = expand_smacro(tline);
2750 if (tok_is_(tline, "(")) {
2752 * This macro has parameters.
2755 tline = tline->next;
2756 while (1) {
2757 skip_white_(tline);
2758 if (!tline) {
2759 error(ERR_NONFATAL, "parameter identifier expected");
2760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
2763 if (tline->type != TOK_ID) {
2764 error(ERR_NONFATAL,
2765 "`%s': parameter identifier expected",
2766 tline->text);
2767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
2770 tline->type = TOK_SMAC_PARAM + nparam++;
2771 tline = tline->next;
2772 skip_white_(tline);
2773 if (tok_is_(tline, ",")) {
2774 tline = tline->next;
2775 } else {
2776 if (!tok_is_(tline, ")")) {
2777 error(ERR_NONFATAL,
2778 "`)' expected to terminate macro template");
2779 free_tlist(origline);
2780 return DIRECTIVE_FOUND;
2782 break;
2785 last = tline;
2786 tline = tline->next;
2788 if (tok_type_(tline, TOK_WHITESPACE))
2789 last = tline, tline = tline->next;
2790 macro_start = NULL;
2791 last->next = NULL;
2792 t = tline;
2793 while (t) {
2794 if (t->type == TOK_ID) {
2795 for (tt = param_start; tt; tt = tt->next)
2796 if (tt->type >= TOK_SMAC_PARAM &&
2797 !strcmp(tt->text, t->text))
2798 t->type = tt->type;
2800 tt = t->next;
2801 t->next = macro_start;
2802 macro_start = t;
2803 t = tt;
2806 * Good. We now have a macro name, a parameter count, and a
2807 * token list (in reverse order) for an expansion. We ought
2808 * to be OK just to create an SMacro, store it, and let
2809 * free_tlist have the rest of the line (which we have
2810 * carefully re-terminated after chopping off the expansion
2811 * from the end).
2813 define_smacro(ctx, mname, casesense, nparam, macro_start);
2814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
2817 case PP_UNDEF:
2818 tline = tline->next;
2819 skip_white_(tline);
2820 tline = expand_id(tline);
2821 if (!tline || (tline->type != TOK_ID &&
2822 (tline->type != TOK_PREPROC_ID ||
2823 tline->text[1] != '$'))) {
2824 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2825 free_tlist(origline);
2826 return DIRECTIVE_FOUND;
2828 if (tline->next) {
2829 error(ERR_WARNING|ERR_PASS1,
2830 "trailing garbage after macro name ignored");
2833 /* Find the context that symbol belongs to */
2834 ctx = get_ctx(tline->text, false);
2835 undef_smacro(ctx, tline->text);
2836 free_tlist(origline);
2837 return DIRECTIVE_FOUND;
2839 case PP_DEFSTR:
2840 case PP_IDEFSTR:
2841 casesense = (i == PP_DEFSTR);
2843 tline = tline->next;
2844 skip_white_(tline);
2845 tline = expand_id(tline);
2846 if (!tline || (tline->type != TOK_ID &&
2847 (tline->type != TOK_PREPROC_ID ||
2848 tline->text[1] != '$'))) {
2849 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2850 pp_directives[i]);
2851 free_tlist(origline);
2852 return DIRECTIVE_FOUND;
2855 ctx = get_ctx(tline->text, false);
2857 mname = tline->text;
2858 last = tline;
2859 tline = expand_smacro(tline->next);
2860 last->next = NULL;
2862 while (tok_type_(tline, TOK_WHITESPACE))
2863 tline = delete_Token(tline);
2865 p = detoken(tline, false);
2866 macro_start = nasm_malloc(sizeof(*macro_start));
2867 macro_start->next = NULL;
2868 macro_start->text = nasm_quote(p, strlen(p));
2869 macro_start->type = TOK_STRING;
2870 macro_start->a.mac = NULL;
2871 nasm_free(p);
2874 * We now have a macro name, an implicit parameter count of
2875 * zero, and a string token to use as an expansion. Create
2876 * and store an SMacro.
2878 define_smacro(ctx, mname, casesense, 0, macro_start);
2879 free_tlist(origline);
2880 return DIRECTIVE_FOUND;
2882 case PP_PATHSEARCH:
2884 FILE *fp;
2885 StrList *xsl = NULL;
2886 StrList **xst = &xsl;
2888 casesense = true;
2890 tline = tline->next;
2891 skip_white_(tline);
2892 tline = expand_id(tline);
2893 if (!tline || (tline->type != TOK_ID &&
2894 (tline->type != TOK_PREPROC_ID ||
2895 tline->text[1] != '$'))) {
2896 error(ERR_NONFATAL,
2897 "`%%pathsearch' expects a macro identifier as first parameter");
2898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
2901 ctx = get_ctx(tline->text, false);
2903 mname = tline->text;
2904 last = tline;
2905 tline = expand_smacro(tline->next);
2906 last->next = NULL;
2908 t = tline;
2909 while (tok_type_(t, TOK_WHITESPACE))
2910 t = t->next;
2912 if (!t || (t->type != TOK_STRING &&
2913 t->type != TOK_INTERNAL_STRING)) {
2914 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2915 free_tlist(tline);
2916 free_tlist(origline);
2917 return DIRECTIVE_FOUND; /* but we did _something_ */
2919 if (t->next)
2920 error(ERR_WARNING|ERR_PASS1,
2921 "trailing garbage after `%%pathsearch' ignored");
2922 p = t->text;
2923 if (t->type != TOK_INTERNAL_STRING)
2924 nasm_unquote(p, NULL);
2926 fp = inc_fopen(p, &xsl, &xst, true);
2927 if (fp) {
2928 p = xsl->str;
2929 fclose(fp); /* Don't actually care about the file */
2931 macro_start = nasm_malloc(sizeof(*macro_start));
2932 macro_start->next = NULL;
2933 macro_start->text = nasm_quote(p, strlen(p));
2934 macro_start->type = TOK_STRING;
2935 macro_start->a.mac = NULL;
2936 if (xsl)
2937 nasm_free(xsl);
2940 * We now have a macro name, an implicit parameter count of
2941 * zero, and a string token to use as an expansion. Create
2942 * and store an SMacro.
2944 define_smacro(ctx, mname, casesense, 0, macro_start);
2945 free_tlist(tline);
2946 free_tlist(origline);
2947 return DIRECTIVE_FOUND;
2950 case PP_STRLEN:
2951 casesense = true;
2953 tline = tline->next;
2954 skip_white_(tline);
2955 tline = expand_id(tline);
2956 if (!tline || (tline->type != TOK_ID &&
2957 (tline->type != TOK_PREPROC_ID ||
2958 tline->text[1] != '$'))) {
2959 error(ERR_NONFATAL,
2960 "`%%strlen' expects a macro identifier as first parameter");
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2964 ctx = get_ctx(tline->text, false);
2966 mname = tline->text;
2967 last = tline;
2968 tline = expand_smacro(tline->next);
2969 last->next = NULL;
2971 t = tline;
2972 while (tok_type_(t, TOK_WHITESPACE))
2973 t = t->next;
2974 /* t should now point to the string */
2975 if (t->type != TOK_STRING) {
2976 error(ERR_NONFATAL,
2977 "`%%strlen` requires string as second parameter");
2978 free_tlist(tline);
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2983 macro_start = nasm_malloc(sizeof(*macro_start));
2984 macro_start->next = NULL;
2985 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
2986 macro_start->a.mac = NULL;
2989 * We now have a macro name, an implicit parameter count of
2990 * zero, and a numeric token to use as an expansion. Create
2991 * and store an SMacro.
2993 define_smacro(ctx, mname, casesense, 0, macro_start);
2994 free_tlist(tline);
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
2998 case PP_STRCAT:
2999 casesense = true;
3001 tline = tline->next;
3002 skip_white_(tline);
3003 tline = expand_id(tline);
3004 if (!tline || (tline->type != TOK_ID &&
3005 (tline->type != TOK_PREPROC_ID ||
3006 tline->text[1] != '$'))) {
3007 error(ERR_NONFATAL,
3008 "`%%strcat' expects a macro identifier as first parameter");
3009 free_tlist(origline);
3010 return DIRECTIVE_FOUND;
3012 ctx = get_ctx(tline->text, false);
3014 mname = tline->text;
3015 last = tline;
3016 tline = expand_smacro(tline->next);
3017 last->next = NULL;
3019 len = 0;
3020 for (t = tline; t; t = t->next) {
3021 switch (t->type) {
3022 case TOK_WHITESPACE:
3023 break;
3024 case TOK_STRING:
3025 len += t->a.len = nasm_unquote(t->text, NULL);
3026 break;
3027 case TOK_OTHER:
3028 if (!strcmp(t->text, ",")) /* permit comma separators */
3029 break;
3030 /* else fall through */
3031 default:
3032 error(ERR_NONFATAL,
3033 "non-string passed to `%%strcat' (%d)", t->type);
3034 free_tlist(tline);
3035 free_tlist(origline);
3036 return DIRECTIVE_FOUND;
3040 p = pp = nasm_malloc(len);
3041 t = tline;
3042 for (t = tline; t; t = t->next) {
3043 if (t->type == TOK_STRING) {
3044 memcpy(p, t->text, t->a.len);
3045 p += t->a.len;
3050 * We now have a macro name, an implicit parameter count of
3051 * zero, and a numeric token to use as an expansion. Create
3052 * and store an SMacro.
3054 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3055 macro_start->text = nasm_quote(pp, len);
3056 nasm_free(pp);
3057 define_smacro(ctx, mname, casesense, 0, macro_start);
3058 free_tlist(tline);
3059 free_tlist(origline);
3060 return DIRECTIVE_FOUND;
3062 case PP_SUBSTR:
3064 int64_t a1, a2;
3065 size_t len;
3067 casesense = true;
3069 tline = tline->next;
3070 skip_white_(tline);
3071 tline = expand_id(tline);
3072 if (!tline || (tline->type != TOK_ID &&
3073 (tline->type != TOK_PREPROC_ID ||
3074 tline->text[1] != '$'))) {
3075 error(ERR_NONFATAL,
3076 "`%%substr' expects a macro identifier as first parameter");
3077 free_tlist(origline);
3078 return DIRECTIVE_FOUND;
3080 ctx = get_ctx(tline->text, false);
3082 mname = tline->text;
3083 last = tline;
3084 tline = expand_smacro(tline->next);
3085 last->next = NULL;
3087 t = tline->next;
3088 while (tok_type_(t, TOK_WHITESPACE))
3089 t = t->next;
3091 /* t should now point to the string */
3092 if (t->type != TOK_STRING) {
3093 error(ERR_NONFATAL,
3094 "`%%substr` requires string as second parameter");
3095 free_tlist(tline);
3096 free_tlist(origline);
3097 return DIRECTIVE_FOUND;
3100 tt = t->next;
3101 tptr = &tt;
3102 tokval.t_type = TOKEN_INVALID;
3103 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3104 pass, error, NULL);
3105 if (!evalresult) {
3106 free_tlist(tline);
3107 free_tlist(origline);
3108 return DIRECTIVE_FOUND;
3109 } else if (!is_simple(evalresult)) {
3110 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3111 free_tlist(tline);
3112 free_tlist(origline);
3113 return DIRECTIVE_FOUND;
3115 a1 = evalresult->value-1;
3117 while (tok_type_(tt, TOK_WHITESPACE))
3118 tt = tt->next;
3119 if (!tt) {
3120 a2 = 1; /* Backwards compatibility: one character */
3121 } else {
3122 tokval.t_type = TOKEN_INVALID;
3123 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3124 pass, error, NULL);
3125 if (!evalresult) {
3126 free_tlist(tline);
3127 free_tlist(origline);
3128 return DIRECTIVE_FOUND;
3129 } else if (!is_simple(evalresult)) {
3130 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3131 free_tlist(tline);
3132 free_tlist(origline);
3133 return DIRECTIVE_FOUND;
3135 a2 = evalresult->value;
3138 len = nasm_unquote(t->text, NULL);
3139 if (a2 < 0)
3140 a2 = a2+1+len-a1;
3141 if (a1+a2 > (int64_t)len)
3142 a2 = len-a1;
3144 macro_start = nasm_malloc(sizeof(*macro_start));
3145 macro_start->next = NULL;
3146 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3147 macro_start->type = TOK_STRING;
3148 macro_start->a.mac = NULL;
3151 * We now have a macro name, an implicit parameter count of
3152 * zero, and a numeric token to use as an expansion. Create
3153 * and store an SMacro.
3155 define_smacro(ctx, mname, casesense, 0, macro_start);
3156 free_tlist(tline);
3157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
3161 case PP_ASSIGN:
3162 case PP_IASSIGN:
3163 casesense = (i == PP_ASSIGN);
3165 tline = tline->next;
3166 skip_white_(tline);
3167 tline = expand_id(tline);
3168 if (!tline || (tline->type != TOK_ID &&
3169 (tline->type != TOK_PREPROC_ID ||
3170 tline->text[1] != '$'))) {
3171 error(ERR_NONFATAL,
3172 "`%%%sassign' expects a macro identifier",
3173 (i == PP_IASSIGN ? "i" : ""));
3174 free_tlist(origline);
3175 return DIRECTIVE_FOUND;
3177 ctx = get_ctx(tline->text, false);
3179 mname = tline->text;
3180 last = tline;
3181 tline = expand_smacro(tline->next);
3182 last->next = NULL;
3184 t = tline;
3185 tptr = &t;
3186 tokval.t_type = TOKEN_INVALID;
3187 evalresult =
3188 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3189 free_tlist(tline);
3190 if (!evalresult) {
3191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
3195 if (tokval.t_type)
3196 error(ERR_WARNING|ERR_PASS1,
3197 "trailing garbage after expression ignored");
3199 if (!is_simple(evalresult)) {
3200 error(ERR_NONFATAL,
3201 "non-constant value given to `%%%sassign'",
3202 (i == PP_IASSIGN ? "i" : ""));
3203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
3207 macro_start = nasm_malloc(sizeof(*macro_start));
3208 macro_start->next = NULL;
3209 make_tok_num(macro_start, reloc_value(evalresult));
3210 macro_start->a.mac = NULL;
3213 * We now have a macro name, an implicit parameter count of
3214 * zero, and a numeric token to use as an expansion. Create
3215 * and store an SMacro.
3217 define_smacro(ctx, mname, casesense, 0, macro_start);
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3221 case PP_LINE:
3223 * Syntax is `%line nnn[+mmm] [filename]'
3225 tline = tline->next;
3226 skip_white_(tline);
3227 if (!tok_type_(tline, TOK_NUMBER)) {
3228 error(ERR_NONFATAL, "`%%line' expects line number");
3229 free_tlist(origline);
3230 return DIRECTIVE_FOUND;
3232 k = readnum(tline->text, &err);
3233 m = 1;
3234 tline = tline->next;
3235 if (tok_is_(tline, "+")) {
3236 tline = tline->next;
3237 if (!tok_type_(tline, TOK_NUMBER)) {
3238 error(ERR_NONFATAL, "`%%line' expects line increment");
3239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
3242 m = readnum(tline->text, &err);
3243 tline = tline->next;
3245 skip_white_(tline);
3246 src_set_linnum(k);
3247 istk->lineinc = m;
3248 if (tline) {
3249 nasm_free(src_set_fname(detoken(tline, false)));
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3254 default:
3255 error(ERR_FATAL,
3256 "preprocessor directive `%s' not yet implemented",
3257 pp_directives[i]);
3258 return DIRECTIVE_FOUND;
3263 * Ensure that a macro parameter contains a condition code and
3264 * nothing else. Return the condition code index if so, or -1
3265 * otherwise.
3267 static int find_cc(Token * t)
3269 Token *tt;
3270 int i, j, k, m;
3272 if (!t)
3273 return -1; /* Probably a %+ without a space */
3275 skip_white_(t);
3276 if (t->type != TOK_ID)
3277 return -1;
3278 tt = t->next;
3279 skip_white_(tt);
3280 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3281 return -1;
3283 i = -1;
3284 j = elements(conditions);
3285 while (j - i > 1) {
3286 k = (j + i) / 2;
3287 m = nasm_stricmp(t->text, conditions[k]);
3288 if (m == 0) {
3289 i = k;
3290 j = -2;
3291 break;
3292 } else if (m < 0) {
3293 j = k;
3294 } else
3295 i = k;
3297 if (j != -2)
3298 return -1;
3299 return i;
3303 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3304 * %-n) and MMacro-local identifiers (%%foo).
3306 static Token *expand_mmac_params(Token * tline)
3308 Token *t, *tt, **tail, *thead;
3310 tail = &thead;
3311 thead = NULL;
3313 while (tline) {
3314 if (tline->type == TOK_PREPROC_ID &&
3315 (((tline->text[1] == '+' || tline->text[1] == '-')
3316 && tline->text[2]) || tline->text[1] == '%'
3317 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3318 char *text = NULL;
3319 int type = 0, cc; /* type = 0 to placate optimisers */
3320 char tmpbuf[30];
3321 unsigned int n;
3322 int i;
3323 MMacro *mac;
3325 t = tline;
3326 tline = tline->next;
3328 mac = istk->mstk;
3329 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3330 mac = mac->next_active;
3331 if (!mac)
3332 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3333 else
3334 switch (t->text[1]) {
3336 * We have to make a substitution of one of the
3337 * forms %1, %-1, %+1, %%foo, %0.
3339 case '0':
3340 type = TOK_NUMBER;
3341 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3342 text = nasm_strdup(tmpbuf);
3343 break;
3344 case '%':
3345 type = TOK_ID;
3346 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3347 mac->unique);
3348 text = nasm_strcat(tmpbuf, t->text + 2);
3349 break;
3350 case '-':
3351 n = atoi(t->text + 2) - 1;
3352 if (n >= mac->nparam)
3353 tt = NULL;
3354 else {
3355 if (mac->nparam > 1)
3356 n = (n + mac->rotate) % mac->nparam;
3357 tt = mac->params[n];
3359 cc = find_cc(tt);
3360 if (cc == -1) {
3361 error(ERR_NONFATAL,
3362 "macro parameter %d is not a condition code",
3363 n + 1);
3364 text = NULL;
3365 } else {
3366 type = TOK_ID;
3367 if (inverse_ccs[cc] == -1) {
3368 error(ERR_NONFATAL,
3369 "condition code `%s' is not invertible",
3370 conditions[cc]);
3371 text = NULL;
3372 } else
3373 text =
3374 nasm_strdup(conditions[inverse_ccs[cc]]);
3376 break;
3377 case '+':
3378 n = atoi(t->text + 2) - 1;
3379 if (n >= mac->nparam)
3380 tt = NULL;
3381 else {
3382 if (mac->nparam > 1)
3383 n = (n + mac->rotate) % mac->nparam;
3384 tt = mac->params[n];
3386 cc = find_cc(tt);
3387 if (cc == -1) {
3388 error(ERR_NONFATAL,
3389 "macro parameter %d is not a condition code",
3390 n + 1);
3391 text = NULL;
3392 } else {
3393 type = TOK_ID;
3394 text = nasm_strdup(conditions[cc]);
3396 break;
3397 default:
3398 n = atoi(t->text + 1) - 1;
3399 if (n >= mac->nparam)
3400 tt = NULL;
3401 else {
3402 if (mac->nparam > 1)
3403 n = (n + mac->rotate) % mac->nparam;
3404 tt = mac->params[n];
3406 if (tt) {
3407 for (i = 0; i < mac->paramlen[n]; i++) {
3408 *tail = new_Token(NULL, tt->type, tt->text, 0);
3409 tail = &(*tail)->next;
3410 tt = tt->next;
3413 text = NULL; /* we've done it here */
3414 break;
3416 if (!text) {
3417 delete_Token(t);
3418 } else {
3419 *tail = t;
3420 tail = &t->next;
3421 t->type = type;
3422 nasm_free(t->text);
3423 t->text = text;
3424 t->a.mac = NULL;
3426 continue;
3427 } else {
3428 t = *tail = tline;
3429 tline = tline->next;
3430 t->a.mac = NULL;
3431 tail = &t->next;
3434 *tail = NULL;
3435 t = thead;
3436 for (; t && (tt = t->next) != NULL; t = t->next)
3437 switch (t->type) {
3438 case TOK_WHITESPACE:
3439 if (tt->type == TOK_WHITESPACE) {
3440 t->next = delete_Token(tt);
3442 break;
3443 case TOK_ID:
3444 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3445 char *tmp = nasm_strcat(t->text, tt->text);
3446 nasm_free(t->text);
3447 t->text = tmp;
3448 t->next = delete_Token(tt);
3450 break;
3451 case TOK_NUMBER:
3452 if (tt->type == TOK_NUMBER) {
3453 char *tmp = nasm_strcat(t->text, tt->text);
3454 nasm_free(t->text);
3455 t->text = tmp;
3456 t->next = delete_Token(tt);
3458 break;
3459 default:
3460 break;
3463 return thead;
3467 * Expand all single-line macro calls made in the given line.
3468 * Return the expanded version of the line. The original is deemed
3469 * to be destroyed in the process. (In reality we'll just move
3470 * Tokens from input to output a lot of the time, rather than
3471 * actually bothering to destroy and replicate.)
3473 #define DEADMAN_LIMIT (1 << 20)
3475 static Token *expand_smacro(Token * tline)
3477 Token *t, *tt, *mstart, **tail, *thead;
3478 struct hash_table *smtbl;
3479 SMacro *head = NULL, *m;
3480 Token **params;
3481 int *paramsize;
3482 unsigned int nparam, sparam;
3483 int brackets, rescan;
3484 Token *org_tline = tline;
3485 Context *ctx;
3486 char *mname;
3487 int deadman = DEADMAN_LIMIT;
3490 * Trick: we should avoid changing the start token pointer since it can
3491 * be contained in "next" field of other token. Because of this
3492 * we allocate a copy of first token and work with it; at the end of
3493 * routine we copy it back
3495 if (org_tline) {
3496 tline =
3497 new_Token(org_tline->next, org_tline->type, org_tline->text,
3499 tline->a.mac = org_tline->a.mac;
3500 nasm_free(org_tline->text);
3501 org_tline->text = NULL;
3504 again:
3505 tail = &thead;
3506 thead = NULL;
3508 while (tline) { /* main token loop */
3509 if (!--deadman) {
3510 error(ERR_NONFATAL, "interminable macro recursion");
3511 break;
3514 if ((mname = tline->text)) {
3515 /* if this token is a local macro, look in local context */
3516 ctx = NULL;
3517 smtbl = &smacros;
3518 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3519 ctx = get_ctx(mname, true);
3520 if (ctx)
3521 smtbl = &ctx->localmac;
3523 head = (SMacro *) hash_findix(smtbl, mname);
3526 * We've hit an identifier. As in is_mmacro below, we first
3527 * check whether the identifier is a single-line macro at
3528 * all, then think about checking for parameters if
3529 * necessary.
3531 for (m = head; m; m = m->next)
3532 if (!mstrcmp(m->name, mname, m->casesense))
3533 break;
3534 if (m) {
3535 mstart = tline;
3536 params = NULL;
3537 paramsize = NULL;
3538 if (m->nparam == 0) {
3540 * Simple case: the macro is parameterless. Discard the
3541 * one token that the macro call took, and push the
3542 * expansion back on the to-do stack.
3544 if (!m->expansion) {
3545 if (!strcmp("__FILE__", m->name)) {
3546 int32_t num = 0;
3547 char *file = NULL;
3548 src_get(&num, &file);
3549 tline->text = nasm_quote(file, strlen(file));
3550 tline->type = TOK_STRING;
3551 nasm_free(file);
3552 continue;
3554 if (!strcmp("__LINE__", m->name)) {
3555 nasm_free(tline->text);
3556 make_tok_num(tline, src_get_linnum());
3557 continue;
3559 if (!strcmp("__BITS__", m->name)) {
3560 nasm_free(tline->text);
3561 make_tok_num(tline, globalbits);
3562 continue;
3564 tline = delete_Token(tline);
3565 continue;
3567 } else {
3569 * Complicated case: at least one macro with this name
3570 * exists and takes parameters. We must find the
3571 * parameters in the call, count them, find the SMacro
3572 * that corresponds to that form of the macro call, and
3573 * substitute for the parameters when we expand. What a
3574 * pain.
3576 /*tline = tline->next;
3577 skip_white_(tline); */
3578 do {
3579 t = tline->next;
3580 while (tok_type_(t, TOK_SMAC_END)) {
3581 t->a.mac->in_progress = false;
3582 t->text = NULL;
3583 t = tline->next = delete_Token(t);
3585 tline = t;
3586 } while (tok_type_(tline, TOK_WHITESPACE));
3587 if (!tok_is_(tline, "(")) {
3589 * This macro wasn't called with parameters: ignore
3590 * the call. (Behaviour borrowed from gnu cpp.)
3592 tline = mstart;
3593 m = NULL;
3594 } else {
3595 int paren = 0;
3596 int white = 0;
3597 brackets = 0;
3598 nparam = 0;
3599 sparam = PARAM_DELTA;
3600 params = nasm_malloc(sparam * sizeof(Token *));
3601 params[0] = tline->next;
3602 paramsize = nasm_malloc(sparam * sizeof(int));
3603 paramsize[0] = 0;
3604 while (true) { /* parameter loop */
3606 * For some unusual expansions
3607 * which concatenates function call
3609 t = tline->next;
3610 while (tok_type_(t, TOK_SMAC_END)) {
3611 t->a.mac->in_progress = false;
3612 t->text = NULL;
3613 t = tline->next = delete_Token(t);
3615 tline = t;
3617 if (!tline) {
3618 error(ERR_NONFATAL,
3619 "macro call expects terminating `)'");
3620 break;
3622 if (tline->type == TOK_WHITESPACE
3623 && brackets <= 0) {
3624 if (paramsize[nparam])
3625 white++;
3626 else
3627 params[nparam] = tline->next;
3628 continue; /* parameter loop */
3630 if (tline->type == TOK_OTHER
3631 && tline->text[1] == 0) {
3632 char ch = tline->text[0];
3633 if (ch == ',' && !paren && brackets <= 0) {
3634 if (++nparam >= sparam) {
3635 sparam += PARAM_DELTA;
3636 params = nasm_realloc(params,
3637 sparam *
3638 sizeof(Token
3639 *));
3640 paramsize =
3641 nasm_realloc(paramsize,
3642 sparam *
3643 sizeof(int));
3645 params[nparam] = tline->next;
3646 paramsize[nparam] = 0;
3647 white = 0;
3648 continue; /* parameter loop */
3650 if (ch == '{' &&
3651 (brackets > 0 || (brackets == 0 &&
3652 !paramsize[nparam])))
3654 if (!(brackets++)) {
3655 params[nparam] = tline->next;
3656 continue; /* parameter loop */
3659 if (ch == '}' && brackets > 0)
3660 if (--brackets == 0) {
3661 brackets = -1;
3662 continue; /* parameter loop */
3664 if (ch == '(' && !brackets)
3665 paren++;
3666 if (ch == ')' && brackets <= 0)
3667 if (--paren < 0)
3668 break;
3670 if (brackets < 0) {
3671 brackets = 0;
3672 error(ERR_NONFATAL, "braces do not "
3673 "enclose all of macro parameter");
3675 paramsize[nparam] += white + 1;
3676 white = 0;
3677 } /* parameter loop */
3678 nparam++;
3679 while (m && (m->nparam != nparam ||
3680 mstrcmp(m->name, mname,
3681 m->casesense)))
3682 m = m->next;
3683 if (!m)
3684 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
3685 "macro `%s' exists, "
3686 "but not taking %d parameters",
3687 mstart->text, nparam);
3690 if (m && m->in_progress)
3691 m = NULL;
3692 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3694 * Design question: should we handle !tline, which
3695 * indicates missing ')' here, or expand those
3696 * macros anyway, which requires the (t) test a few
3697 * lines down?
3699 nasm_free(params);
3700 nasm_free(paramsize);
3701 tline = mstart;
3702 } else {
3704 * Expand the macro: we are placed on the last token of the
3705 * call, so that we can easily split the call from the
3706 * following tokens. We also start by pushing an SMAC_END
3707 * token for the cycle removal.
3709 t = tline;
3710 if (t) {
3711 tline = t->next;
3712 t->next = NULL;
3714 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3715 tt->a.mac = m;
3716 m->in_progress = true;
3717 tline = tt;
3718 for (t = m->expansion; t; t = t->next) {
3719 if (t->type >= TOK_SMAC_PARAM) {
3720 Token *pcopy = tline, **ptail = &pcopy;
3721 Token *ttt, *pt;
3722 int i;
3724 ttt = params[t->type - TOK_SMAC_PARAM];
3725 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3726 --i >= 0;) {
3727 pt = *ptail =
3728 new_Token(tline, ttt->type, ttt->text,
3730 ptail = &pt->next;
3731 ttt = ttt->next;
3733 tline = pcopy;
3734 } else if (t->type == TOK_PREPROC_Q) {
3735 tt = new_Token(tline, TOK_ID, mname, 0);
3736 tline = tt;
3737 } else if (t->type == TOK_PREPROC_QQ) {
3738 tt = new_Token(tline, TOK_ID, m->name, 0);
3739 tline = tt;
3740 } else {
3741 tt = new_Token(tline, t->type, t->text, 0);
3742 tline = tt;
3747 * Having done that, get rid of the macro call, and clean
3748 * up the parameters.
3750 nasm_free(params);
3751 nasm_free(paramsize);
3752 free_tlist(mstart);
3753 continue; /* main token loop */
3758 if (tline->type == TOK_SMAC_END) {
3759 tline->a.mac->in_progress = false;
3760 tline = delete_Token(tline);
3761 } else {
3762 t = *tail = tline;
3763 tline = tline->next;
3764 t->a.mac = NULL;
3765 t->next = NULL;
3766 tail = &t->next;
3771 * Now scan the entire line and look for successive TOK_IDs that resulted
3772 * after expansion (they can't be produced by tokenize()). The successive
3773 * TOK_IDs should be concatenated.
3774 * Also we look for %+ tokens and concatenate the tokens before and after
3775 * them (without white spaces in between).
3777 t = thead;
3778 rescan = 0;
3779 while (t) {
3780 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3781 t = t->next;
3782 if (!t || !t->next)
3783 break;
3784 if (t->next->type == TOK_ID ||
3785 t->next->type == TOK_PREPROC_ID ||
3786 t->next->type == TOK_NUMBER) {
3787 char *p = nasm_strcat(t->text, t->next->text);
3788 nasm_free(t->text);
3789 t->next = delete_Token(t->next);
3790 t->text = p;
3791 rescan = 1;
3792 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3793 t->next->next->type == TOK_PREPROC_ID &&
3794 strcmp(t->next->next->text, "%+") == 0) {
3795 /* free the next whitespace, the %+ token and next whitespace */
3796 int i;
3797 for (i = 1; i <= 3; i++) {
3798 if (!t->next
3799 || (i != 2 && t->next->type != TOK_WHITESPACE))
3800 break;
3801 t->next = delete_Token(t->next);
3802 } /* endfor */
3803 } else
3804 t = t->next;
3806 /* If we concatenaded something, re-scan the line for macros */
3807 if (rescan) {
3808 tline = thead;
3809 goto again;
3812 if (org_tline) {
3813 if (thead) {
3814 *org_tline = *thead;
3815 /* since we just gave text to org_line, don't free it */
3816 thead->text = NULL;
3817 delete_Token(thead);
3818 } else {
3819 /* the expression expanded to empty line;
3820 we can't return NULL for some reasons
3821 we just set the line to a single WHITESPACE token. */
3822 memset(org_tline, 0, sizeof(*org_tline));
3823 org_tline->text = NULL;
3824 org_tline->type = TOK_WHITESPACE;
3826 thead = org_tline;
3829 return thead;
3833 * Similar to expand_smacro but used exclusively with macro identifiers
3834 * right before they are fetched in. The reason is that there can be
3835 * identifiers consisting of several subparts. We consider that if there
3836 * are more than one element forming the name, user wants a expansion,
3837 * otherwise it will be left as-is. Example:
3839 * %define %$abc cde
3841 * the identifier %$abc will be left as-is so that the handler for %define
3842 * will suck it and define the corresponding value. Other case:
3844 * %define _%$abc cde
3846 * In this case user wants name to be expanded *before* %define starts
3847 * working, so we'll expand %$abc into something (if it has a value;
3848 * otherwise it will be left as-is) then concatenate all successive
3849 * PP_IDs into one.
3851 static Token *expand_id(Token * tline)
3853 Token *cur, *oldnext = NULL;
3855 if (!tline || !tline->next)
3856 return tline;
3858 cur = tline;
3859 while (cur->next &&
3860 (cur->next->type == TOK_ID ||
3861 cur->next->type == TOK_PREPROC_ID
3862 || cur->next->type == TOK_NUMBER))
3863 cur = cur->next;
3865 /* If identifier consists of just one token, don't expand */
3866 if (cur == tline)
3867 return tline;
3869 if (cur) {
3870 oldnext = cur->next; /* Detach the tail past identifier */
3871 cur->next = NULL; /* so that expand_smacro stops here */
3874 tline = expand_smacro(tline);
3876 if (cur) {
3877 /* expand_smacro possibly changhed tline; re-scan for EOL */
3878 cur = tline;
3879 while (cur && cur->next)
3880 cur = cur->next;
3881 if (cur)
3882 cur->next = oldnext;
3885 return tline;
3889 * Determine whether the given line constitutes a multi-line macro
3890 * call, and return the MMacro structure called if so. Doesn't have
3891 * to check for an initial label - that's taken care of in
3892 * expand_mmacro - but must check numbers of parameters. Guaranteed
3893 * to be called with tline->type == TOK_ID, so the putative macro
3894 * name is easy to find.
3896 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3898 MMacro *head, *m;
3899 Token **params;
3900 int nparam;
3902 head = (MMacro *) hash_findix(&mmacros, tline->text);
3905 * Efficiency: first we see if any macro exists with the given
3906 * name. If not, we can return NULL immediately. _Then_ we
3907 * count the parameters, and then we look further along the
3908 * list if necessary to find the proper MMacro.
3910 for (m = head; m; m = m->next)
3911 if (!mstrcmp(m->name, tline->text, m->casesense))
3912 break;
3913 if (!m)
3914 return NULL;
3917 * OK, we have a potential macro. Count and demarcate the
3918 * parameters.
3920 count_mmac_params(tline->next, &nparam, &params);
3923 * So we know how many parameters we've got. Find the MMacro
3924 * structure that handles this number.
3926 while (m) {
3927 if (m->nparam_min <= nparam
3928 && (m->plus || nparam <= m->nparam_max)) {
3930 * This one is right. Just check if cycle removal
3931 * prohibits us using it before we actually celebrate...
3933 if (m->in_progress) {
3934 #if 0
3935 error(ERR_NONFATAL,
3936 "self-reference in multi-line macro `%s'", m->name);
3937 #endif
3938 nasm_free(params);
3939 return NULL;
3942 * It's right, and we can use it. Add its default
3943 * parameters to the end of our list if necessary.
3945 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3946 params =
3947 nasm_realloc(params,
3948 ((m->nparam_min + m->ndefs +
3949 1) * sizeof(*params)));
3950 while (nparam < m->nparam_min + m->ndefs) {
3951 params[nparam] = m->defaults[nparam - m->nparam_min];
3952 nparam++;
3956 * If we've gone over the maximum parameter count (and
3957 * we're in Plus mode), ignore parameters beyond
3958 * nparam_max.
3960 if (m->plus && nparam > m->nparam_max)
3961 nparam = m->nparam_max;
3963 * Then terminate the parameter list, and leave.
3965 if (!params) { /* need this special case */
3966 params = nasm_malloc(sizeof(*params));
3967 nparam = 0;
3969 params[nparam] = NULL;
3970 *params_array = params;
3971 return m;
3974 * This one wasn't right: look for the next one with the
3975 * same name.
3977 for (m = m->next; m; m = m->next)
3978 if (!mstrcmp(m->name, tline->text, m->casesense))
3979 break;
3983 * After all that, we didn't find one with the right number of
3984 * parameters. Issue a warning, and fail to expand the macro.
3986 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
3987 "macro `%s' exists, but not taking %d parameters",
3988 tline->text, nparam);
3989 nasm_free(params);
3990 return NULL;
3994 * Expand the multi-line macro call made by the given line, if
3995 * there is one to be expanded. If there is, push the expansion on
3996 * istk->expansion and return 1. Otherwise return 0.
3998 static int expand_mmacro(Token * tline)
4000 Token *startline = tline;
4001 Token *label = NULL;
4002 int dont_prepend = 0;
4003 Token **params, *t, *mtok, *tt;
4004 MMacro *m;
4005 Line *l, *ll;
4006 int i, nparam, *paramlen;
4007 const char *mname;
4009 t = tline;
4010 skip_white_(t);
4011 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4012 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4013 return 0;
4014 mtok = t;
4015 m = is_mmacro(t, &params);
4016 if (m) {
4017 mname = t->text;
4018 } else {
4019 Token *last;
4021 * We have an id which isn't a macro call. We'll assume
4022 * it might be a label; we'll also check to see if a
4023 * colon follows it. Then, if there's another id after
4024 * that lot, we'll check it again for macro-hood.
4026 label = last = t;
4027 t = t->next;
4028 if (tok_type_(t, TOK_WHITESPACE))
4029 last = t, t = t->next;
4030 if (tok_is_(t, ":")) {
4031 dont_prepend = 1;
4032 last = t, t = t->next;
4033 if (tok_type_(t, TOK_WHITESPACE))
4034 last = t, t = t->next;
4036 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4037 return 0;
4038 last->next = NULL;
4039 mname = t->text;
4040 tline = t;
4044 * Fix up the parameters: this involves stripping leading and
4045 * trailing whitespace, then stripping braces if they are
4046 * present.
4048 for (nparam = 0; params[nparam]; nparam++) ;
4049 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4051 for (i = 0; params[i]; i++) {
4052 int brace = false;
4053 int comma = (!m->plus || i < nparam - 1);
4055 t = params[i];
4056 skip_white_(t);
4057 if (tok_is_(t, "{"))
4058 t = t->next, brace = true, comma = false;
4059 params[i] = t;
4060 paramlen[i] = 0;
4061 while (t) {
4062 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4063 break; /* ... because we have hit a comma */
4064 if (comma && t->type == TOK_WHITESPACE
4065 && tok_is_(t->next, ","))
4066 break; /* ... or a space then a comma */
4067 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4068 break; /* ... or a brace */
4069 t = t->next;
4070 paramlen[i]++;
4075 * OK, we have a MMacro structure together with a set of
4076 * parameters. We must now go through the expansion and push
4077 * copies of each Line on to istk->expansion. Substitution of
4078 * parameter tokens and macro-local tokens doesn't get done
4079 * until the single-line macro substitution process; this is
4080 * because delaying them allows us to change the semantics
4081 * later through %rotate.
4083 * First, push an end marker on to istk->expansion, mark this
4084 * macro as in progress, and set up its invocation-specific
4085 * variables.
4087 ll = nasm_malloc(sizeof(Line));
4088 ll->next = istk->expansion;
4089 ll->finishes = m;
4090 ll->first = NULL;
4091 istk->expansion = ll;
4093 m->in_progress = true;
4094 m->params = params;
4095 m->iline = tline;
4096 m->nparam = nparam;
4097 m->rotate = 0;
4098 m->paramlen = paramlen;
4099 m->unique = unique++;
4100 m->lineno = 0;
4102 m->next_active = istk->mstk;
4103 istk->mstk = m;
4105 for (l = m->expansion; l; l = l->next) {
4106 Token **tail;
4108 ll = nasm_malloc(sizeof(Line));
4109 ll->finishes = NULL;
4110 ll->next = istk->expansion;
4111 istk->expansion = ll;
4112 tail = &ll->first;
4114 for (t = l->first; t; t = t->next) {
4115 Token *x = t;
4116 switch (t->type) {
4117 case TOK_PREPROC_Q:
4118 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4119 break;
4120 case TOK_PREPROC_QQ:
4121 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4122 break;
4123 case TOK_PREPROC_ID:
4124 if (t->text[1] == '0' && t->text[2] == '0') {
4125 dont_prepend = -1;
4126 x = label;
4127 if (!x)
4128 continue;
4130 /* fall through */
4131 default:
4132 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4133 break;
4135 tail = &tt->next;
4137 *tail = NULL;
4141 * If we had a label, push it on as the first line of
4142 * the macro expansion.
4144 if (label) {
4145 if (dont_prepend < 0)
4146 free_tlist(startline);
4147 else {
4148 ll = nasm_malloc(sizeof(Line));
4149 ll->finishes = NULL;
4150 ll->next = istk->expansion;
4151 istk->expansion = ll;
4152 ll->first = startline;
4153 if (!dont_prepend) {
4154 while (label->next)
4155 label = label->next;
4156 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4161 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4163 return 1;
4166 /* The function that actually does the error reporting */
4167 static void verror(int severity, const char *fmt, va_list arg)
4169 char buff[1024];
4171 vsnprintf(buff, sizeof(buff), fmt, arg);
4173 if (istk && istk->mstk && istk->mstk->name)
4174 _error(severity, "(%s:%d) %s", istk->mstk->name,
4175 istk->mstk->lineno, buff);
4176 else
4177 _error(severity, "%s", buff);
4181 * Since preprocessor always operate only on the line that didn't
4182 * arrived yet, we should always use ERR_OFFBY1.
4184 static void error(int severity, const char *fmt, ...)
4186 va_list arg;
4188 /* If we're in a dead branch of IF or something like it, ignore the error */
4189 if (istk && istk->conds && !emitting(istk->conds->state))
4190 return;
4192 va_start(arg, fmt);
4193 verror(severity, fmt, arg);
4194 va_end(arg);
4198 * Because %else etc are evaluated in the state context
4199 * of the previous branch, errors might get lost with error():
4200 * %if 0 ... %else trailing garbage ... %endif
4201 * So %else etc should report errors with this function.
4203 static void error_precond(int severity, const char *fmt, ...)
4205 va_list arg;
4207 /* Only ignore the error if it's really in a dead branch */
4208 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4209 return;
4211 va_start(arg, fmt);
4212 verror(severity, fmt, arg);
4213 va_end(arg);
4216 static void
4217 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4218 ListGen * listgen, StrList **deplist)
4220 Token *t;
4222 _error = errfunc;
4223 cstk = NULL;
4224 istk = nasm_malloc(sizeof(Include));
4225 istk->next = NULL;
4226 istk->conds = NULL;
4227 istk->expansion = NULL;
4228 istk->mstk = NULL;
4229 istk->fp = fopen(file, "r");
4230 istk->fname = NULL;
4231 src_set_fname(nasm_strdup(file));
4232 src_set_linnum(0);
4233 istk->lineinc = 1;
4234 if (!istk->fp)
4235 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4236 file);
4237 defining = NULL;
4238 nested_mac_count = 0;
4239 nested_rep_count = 0;
4240 init_macros();
4241 unique = 0;
4242 if (tasm_compatible_mode) {
4243 stdmacpos = nasm_stdmac;
4244 } else {
4245 stdmacpos = nasm_stdmac_after_tasm;
4247 any_extrastdmac = extrastdmac && *extrastdmac;
4248 do_predef = true;
4249 list = listgen;
4250 evaluate = eval;
4253 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4254 * The caller, however, will also pass in 3 for preprocess-only so
4255 * we can set __PASS__ accordingly.
4257 pass = apass > 2 ? 2 : apass;
4259 dephead = deptail = deplist;
4260 if (deplist) {
4261 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4262 sl->next = NULL;
4263 strcpy(sl->str, file);
4264 *deptail = sl;
4265 deptail = &sl->next;
4269 * Define the __PASS__ macro. This is defined here unlike
4270 * all the other builtins, because it is special -- it varies between
4271 * passes.
4273 t = nasm_malloc(sizeof(*t));
4274 t->next = NULL;
4275 make_tok_num(t, apass);
4276 t->a.mac = NULL;
4277 define_smacro(NULL, "__PASS__", true, 0, t);
4280 static char *pp_getline(void)
4282 char *line;
4283 Token *tline;
4285 while (1) {
4287 * Fetch a tokenized line, either from the macro-expansion
4288 * buffer or from the input file.
4290 tline = NULL;
4291 while (istk->expansion && istk->expansion->finishes) {
4292 Line *l = istk->expansion;
4293 if (!l->finishes->name && l->finishes->in_progress > 1) {
4294 Line *ll;
4297 * This is a macro-end marker for a macro with no
4298 * name, which means it's not really a macro at all
4299 * but a %rep block, and the `in_progress' field is
4300 * more than 1, meaning that we still need to
4301 * repeat. (1 means the natural last repetition; 0
4302 * means termination by %exitrep.) We have
4303 * therefore expanded up to the %endrep, and must
4304 * push the whole block on to the expansion buffer
4305 * again. We don't bother to remove the macro-end
4306 * marker: we'd only have to generate another one
4307 * if we did.
4309 l->finishes->in_progress--;
4310 for (l = l->finishes->expansion; l; l = l->next) {
4311 Token *t, *tt, **tail;
4313 ll = nasm_malloc(sizeof(Line));
4314 ll->next = istk->expansion;
4315 ll->finishes = NULL;
4316 ll->first = NULL;
4317 tail = &ll->first;
4319 for (t = l->first; t; t = t->next) {
4320 if (t->text || t->type == TOK_WHITESPACE) {
4321 tt = *tail =
4322 new_Token(NULL, t->type, t->text, 0);
4323 tail = &tt->next;
4327 istk->expansion = ll;
4329 } else {
4331 * Check whether a `%rep' was started and not ended
4332 * within this macro expansion. This can happen and
4333 * should be detected. It's a fatal error because
4334 * I'm too confused to work out how to recover
4335 * sensibly from it.
4337 if (defining) {
4338 if (defining->name)
4339 error(ERR_PANIC,
4340 "defining with name in expansion");
4341 else if (istk->mstk->name)
4342 error(ERR_FATAL,
4343 "`%%rep' without `%%endrep' within"
4344 " expansion of macro `%s'",
4345 istk->mstk->name);
4349 * FIXME: investigate the relationship at this point between
4350 * istk->mstk and l->finishes
4353 MMacro *m = istk->mstk;
4354 istk->mstk = m->next_active;
4355 if (m->name) {
4357 * This was a real macro call, not a %rep, and
4358 * therefore the parameter information needs to
4359 * be freed.
4361 nasm_free(m->params);
4362 free_tlist(m->iline);
4363 nasm_free(m->paramlen);
4364 l->finishes->in_progress = false;
4365 } else
4366 free_mmacro(m);
4368 istk->expansion = l->next;
4369 nasm_free(l);
4370 list->downlevel(LIST_MACRO);
4373 while (1) { /* until we get a line we can use */
4375 if (istk->expansion) { /* from a macro expansion */
4376 char *p;
4377 Line *l = istk->expansion;
4378 if (istk->mstk)
4379 istk->mstk->lineno++;
4380 tline = l->first;
4381 istk->expansion = l->next;
4382 nasm_free(l);
4383 p = detoken(tline, false);
4384 list->line(LIST_MACRO, p);
4385 nasm_free(p);
4386 break;
4388 line = read_line();
4389 if (line) { /* from the current input file */
4390 line = prepreproc(line);
4391 tline = tokenize(line);
4392 nasm_free(line);
4393 break;
4396 * The current file has ended; work down the istk
4399 Include *i = istk;
4400 fclose(i->fp);
4401 if (i->conds)
4402 error(ERR_FATAL,
4403 "expected `%%endif' before end of file");
4404 /* only set line and file name if there's a next node */
4405 if (i->next) {
4406 src_set_linnum(i->lineno);
4407 nasm_free(src_set_fname(i->fname));
4409 istk = i->next;
4410 list->downlevel(LIST_INCLUDE);
4411 nasm_free(i);
4412 if (!istk)
4413 return NULL;
4414 if (istk->expansion && istk->expansion->finishes)
4415 break;
4420 * We must expand MMacro parameters and MMacro-local labels
4421 * _before_ we plunge into directive processing, to cope
4422 * with things like `%define something %1' such as STRUC
4423 * uses. Unless we're _defining_ a MMacro, in which case
4424 * those tokens should be left alone to go into the
4425 * definition; and unless we're in a non-emitting
4426 * condition, in which case we don't want to meddle with
4427 * anything.
4429 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4430 && !(istk->mstk && !istk->mstk->in_progress))
4431 tline = expand_mmac_params(tline);
4434 * Check the line to see if it's a preprocessor directive.
4436 if (do_directive(tline) == DIRECTIVE_FOUND) {
4437 continue;
4438 } else if (defining) {
4440 * We're defining a multi-line macro. We emit nothing
4441 * at all, and just
4442 * shove the tokenized line on to the macro definition.
4444 Line *l = nasm_malloc(sizeof(Line));
4445 l->next = defining->expansion;
4446 l->first = tline;
4447 l->finishes = NULL;
4448 defining->expansion = l;
4449 continue;
4450 } else if (istk->conds && !emitting(istk->conds->state)) {
4452 * We're in a non-emitting branch of a condition block.
4453 * Emit nothing at all, not even a blank line: when we
4454 * emerge from the condition we'll give a line-number
4455 * directive so we keep our place correctly.
4457 free_tlist(tline);
4458 continue;
4459 } else if (istk->mstk && !istk->mstk->in_progress) {
4461 * We're in a %rep block which has been terminated, so
4462 * we're walking through to the %endrep without
4463 * emitting anything. Emit nothing at all, not even a
4464 * blank line: when we emerge from the %rep block we'll
4465 * give a line-number directive so we keep our place
4466 * correctly.
4468 free_tlist(tline);
4469 continue;
4470 } else {
4471 tline = expand_smacro(tline);
4472 if (!expand_mmacro(tline)) {
4474 * De-tokenize the line again, and emit it.
4476 line = detoken(tline, true);
4477 free_tlist(tline);
4478 break;
4479 } else {
4480 continue; /* expand_mmacro calls free_tlist */
4485 return line;
4488 static void pp_cleanup(int pass)
4490 if (defining) {
4491 if(defining->name) {
4492 error(ERR_NONFATAL,
4493 "end of file while still defining macro `%s'",
4494 defining->name);
4495 } else {
4496 error(ERR_NONFATAL, "end of file while still in %%rep");
4499 free_mmacro(defining);
4501 while (cstk)
4502 ctx_pop();
4503 free_macros();
4504 while (istk) {
4505 Include *i = istk;
4506 istk = istk->next;
4507 fclose(i->fp);
4508 nasm_free(i->fname);
4509 nasm_free(i);
4511 while (cstk)
4512 ctx_pop();
4513 nasm_free(src_set_fname(NULL));
4514 if (pass == 0) {
4515 IncPath *i;
4516 free_llist(predef);
4517 delete_Blocks();
4518 while ((i = ipath)) {
4519 ipath = i->next;
4520 if (i->path)
4521 nasm_free(i->path);
4522 nasm_free(i);
4527 void pp_include_path(char *path)
4529 IncPath *i;
4531 i = nasm_malloc(sizeof(IncPath));
4532 i->path = path ? nasm_strdup(path) : NULL;
4533 i->next = NULL;
4535 if (ipath != NULL) {
4536 IncPath *j = ipath;
4537 while (j->next != NULL)
4538 j = j->next;
4539 j->next = i;
4540 } else {
4541 ipath = i;
4545 void pp_pre_include(char *fname)
4547 Token *inc, *space, *name;
4548 Line *l;
4550 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4551 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4552 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4554 l = nasm_malloc(sizeof(Line));
4555 l->next = predef;
4556 l->first = inc;
4557 l->finishes = NULL;
4558 predef = l;
4561 void pp_pre_define(char *definition)
4563 Token *def, *space;
4564 Line *l;
4565 char *equals;
4567 equals = strchr(definition, '=');
4568 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4569 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4570 if (equals)
4571 *equals = ' ';
4572 space->next = tokenize(definition);
4573 if (equals)
4574 *equals = '=';
4576 l = nasm_malloc(sizeof(Line));
4577 l->next = predef;
4578 l->first = def;
4579 l->finishes = NULL;
4580 predef = l;
4583 void pp_pre_undefine(char *definition)
4585 Token *def, *space;
4586 Line *l;
4588 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4589 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4590 space->next = tokenize(definition);
4592 l = nasm_malloc(sizeof(Line));
4593 l->next = predef;
4594 l->first = def;
4595 l->finishes = NULL;
4596 predef = l;
4600 * Added by Keith Kanios:
4602 * This function is used to assist with "runtime" preprocessor
4603 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4605 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4606 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4609 void pp_runtime(char *definition)
4611 Token *def;
4613 def = tokenize(definition);
4614 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4615 free_tlist(def);
4619 void pp_extra_stdmac(macros_t *macros)
4621 extrastdmac = macros;
4624 static void make_tok_num(Token * tok, int64_t val)
4626 char numbuf[20];
4627 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4628 tok->text = nasm_strdup(numbuf);
4629 tok->type = TOK_NUMBER;
4632 Preproc nasmpp = {
4633 pp_reset,
4634 pp_getline,
4635 pp_cleanup