Merge commit 'nasm-2.05.01'
[nasm/perl-rewrite.git] / preproc.c
blob350b293ccc36c9871c7965575f3a92578bb7f528
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_INDIRECT, /* %[...] */
163 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
164 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
167 struct Token {
168 Token *next;
169 char *text;
170 union {
171 SMacro *mac; /* associated macro for TOK_SMAC_END */
172 size_t len; /* scratch length field */
173 } a; /* Auxiliary data */
174 enum pp_token_type type;
178 * Multi-line macro definitions are stored as a linked list of
179 * these, which is essentially a container to allow several linked
180 * lists of Tokens.
182 * Note that in this module, linked lists are treated as stacks
183 * wherever possible. For this reason, Lines are _pushed_ on to the
184 * `expansion' field in MMacro structures, so that the linked list,
185 * if walked, would give the macro lines in reverse order; this
186 * means that we can walk the list when expanding a macro, and thus
187 * push the lines on to the `expansion' field in _istk_ in reverse
188 * order (so that when popped back off they are in the right
189 * order). It may seem cockeyed, and it relies on my design having
190 * an even number of steps in, but it works...
192 * Some of these structures, rather than being actual lines, are
193 * markers delimiting the end of the expansion of a given macro.
194 * This is for use in the cycle-tracking and %rep-handling code.
195 * Such structures have `finishes' non-NULL, and `first' NULL. All
196 * others have `finishes' NULL, but `first' may still be NULL if
197 * the line is blank.
199 struct Line {
200 Line *next;
201 MMacro *finishes;
202 Token *first;
206 * To handle an arbitrary level of file inclusion, we maintain a
207 * stack (ie linked list) of these things.
209 struct Include {
210 Include *next;
211 FILE *fp;
212 Cond *conds;
213 Line *expansion;
214 char *fname;
215 int lineno, lineinc;
216 MMacro *mstk; /* stack of active macros/reps */
220 * Include search path. This is simply a list of strings which get
221 * prepended, in turn, to the name of an include file, in an
222 * attempt to find the file if it's not in the current directory.
224 struct IncPath {
225 IncPath *next;
226 char *path;
230 * Conditional assembly: we maintain a separate stack of these for
231 * each level of file inclusion. (The only reason we keep the
232 * stacks separate is to ensure that a stray `%endif' in a file
233 * included from within the true branch of a `%if' won't terminate
234 * it and cause confusion: instead, rightly, it'll cause an error.)
236 struct Cond {
237 Cond *next;
238 int state;
240 enum {
242 * These states are for use just after %if or %elif: IF_TRUE
243 * means the condition has evaluated to truth so we are
244 * currently emitting, whereas IF_FALSE means we are not
245 * currently emitting but will start doing so if a %else comes
246 * up. In these states, all directives are admissible: %elif,
247 * %else and %endif. (And of course %if.)
249 COND_IF_TRUE, COND_IF_FALSE,
251 * These states come up after a %else: ELSE_TRUE means we're
252 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
253 * any %elif or %else will cause an error.
255 COND_ELSE_TRUE, COND_ELSE_FALSE,
257 * These states mean that we're not emitting now, and also that
258 * nothing until %endif will be emitted at all. COND_DONE is
259 * used when we've had our moment of emission
260 * and have now started seeing %elifs. COND_NEVER is used when
261 * the condition construct in question is contained within a
262 * non-emitting branch of a larger condition construct,
263 * or if there is an error.
265 COND_DONE, COND_NEVER
267 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
270 * These defines are used as the possible return values for do_directive
272 #define NO_DIRECTIVE_FOUND 0
273 #define DIRECTIVE_FOUND 1
276 * Condition codes. Note that we use c_ prefix not C_ because C_ is
277 * used in nasm.h for the "real" condition codes. At _this_ level,
278 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
279 * ones, so we need a different enum...
281 static const char * const conditions[] = {
282 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
283 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
284 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
286 enum pp_conds {
287 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
288 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
289 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
290 c_none = -1
292 static const enum pp_conds inverse_ccs[] = {
293 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
294 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,
295 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
299 * Directive names.
301 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
302 static int is_condition(enum preproc_token arg)
304 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
307 /* For TASM compatibility we need to be able to recognise TASM compatible
308 * conditional compilation directives. Using the NASM pre-processor does
309 * not work, so we look for them specifically from the following list and
310 * then jam in the equivalent NASM directive into the input stream.
313 enum {
314 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
315 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
318 static const char * const tasm_directives[] = {
319 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
320 "ifndef", "include", "local"
323 static int StackSize = 4;
324 static char *StackPointer = "ebp";
325 static int ArgOffset = 8;
326 static int LocalOffset = 0;
328 static Context *cstk;
329 static Include *istk;
330 static IncPath *ipath = NULL;
332 static efunc _error; /* Pointer to client-provided error reporting function */
333 static evalfunc evaluate;
335 static int pass; /* HACK: pass 0 = generate dependencies only */
336 static StrList **dephead, **deptail; /* Dependency list */
338 static uint64_t unique; /* unique identifier numbers */
340 static Line *predef = NULL;
341 static bool do_predef;
343 static ListGen *list;
346 * The current set of multi-line macros we have defined.
348 static struct hash_table mmacros;
351 * The current set of single-line macros we have defined.
353 static struct hash_table smacros;
356 * The multi-line macro we are currently defining, or the %rep
357 * block we are currently reading, if any.
359 static MMacro *defining;
361 static uint64_t nested_mac_count;
362 static uint64_t nested_rep_count;
365 * The number of macro parameters to allocate space for at a time.
367 #define PARAM_DELTA 16
370 * The standard macro set: defined in macros.c in the array nasm_stdmac.
371 * This gives our position in the macro set, when we're processing it.
373 static macros_t *stdmacpos;
376 * The extra standard macros that come from the object format, if
377 * any.
379 static macros_t *extrastdmac = NULL;
380 static bool any_extrastdmac;
383 * Tokens are allocated in blocks to improve speed
385 #define TOKEN_BLOCKSIZE 4096
386 static Token *freeTokens = NULL;
387 struct Blocks {
388 Blocks *next;
389 void *chunk;
392 static Blocks blocks = { NULL, NULL };
395 * Forward declarations.
397 static Token *expand_mmac_params(Token * tline);
398 static Token *expand_smacro(Token * tline);
399 static Token *expand_id(Token * tline);
400 static Context *get_ctx(const char *name, bool all_contexts);
401 static void make_tok_num(Token * tok, int64_t val);
402 static void error(int severity, const char *fmt, ...);
403 static void error_precond(int severity, const char *fmt, ...);
404 static void *new_Block(size_t size);
405 static void delete_Blocks(void);
406 static Token *new_Token(Token * next, enum pp_token_type type,
407 const char *text, int txtlen);
408 static Token *delete_Token(Token * t);
411 * Macros for safe checking of token pointers, avoid *(NULL)
413 #define tok_type_(x,t) ((x) && (x)->type == (t))
414 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
415 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
416 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
418 /* Handle TASM specific directives, which do not contain a % in
419 * front of them. We do it here because I could not find any other
420 * place to do it for the moment, and it is a hack (ideally it would
421 * be nice to be able to use the NASM pre-processor to do it).
423 static char *check_tasm_directive(char *line)
425 int32_t i, j, k, m, len;
426 char *p = line, *oldline, oldchar;
428 /* Skip whitespace */
429 while (nasm_isspace(*p) && *p != 0)
430 p++;
432 /* Binary search for the directive name */
433 i = -1;
434 j = elements(tasm_directives);
435 len = 0;
436 while (!nasm_isspace(p[len]) && p[len] != 0)
437 len++;
438 if (len) {
439 oldchar = p[len];
440 p[len] = 0;
441 while (j - i > 1) {
442 k = (j + i) / 2;
443 m = nasm_stricmp(p, tasm_directives[k]);
444 if (m == 0) {
445 /* We have found a directive, so jam a % in front of it
446 * so that NASM will then recognise it as one if it's own.
448 p[len] = oldchar;
449 len = strlen(p);
450 oldline = line;
451 line = nasm_malloc(len + 2);
452 line[0] = '%';
453 if (k == TM_IFDIFI) {
454 /* NASM does not recognise IFDIFI, so we convert it to
455 * %ifdef BOGUS. This is not used in NASM comaptible
456 * code, but does need to parse for the TASM macro
457 * package.
459 strcpy(line + 1, "ifdef BOGUS");
460 } else {
461 memcpy(line + 1, p, len + 1);
463 nasm_free(oldline);
464 return line;
465 } else if (m < 0) {
466 j = k;
467 } else
468 i = k;
470 p[len] = oldchar;
472 return line;
476 * The pre-preprocessing stage... This function translates line
477 * number indications as they emerge from GNU cpp (`# lineno "file"
478 * flags') into NASM preprocessor line number indications (`%line
479 * lineno file').
481 static char *prepreproc(char *line)
483 int lineno, fnlen;
484 char *fname, *oldline;
486 if (line[0] == '#' && line[1] == ' ') {
487 oldline = line;
488 fname = oldline + 2;
489 lineno = atoi(fname);
490 fname += strspn(fname, "0123456789 ");
491 if (*fname == '"')
492 fname++;
493 fnlen = strcspn(fname, "\"");
494 line = nasm_malloc(20 + fnlen);
495 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
496 nasm_free(oldline);
498 if (tasm_compatible_mode)
499 return check_tasm_directive(line);
500 return line;
504 * Free a linked list of tokens.
506 static void free_tlist(Token * list)
508 while (list) {
509 list = delete_Token(list);
514 * Free a linked list of lines.
516 static void free_llist(Line * list)
518 Line *l;
519 while (list) {
520 l = list;
521 list = list->next;
522 free_tlist(l->first);
523 nasm_free(l);
528 * Free an MMacro
530 static void free_mmacro(MMacro * m)
532 nasm_free(m->name);
533 free_tlist(m->dlist);
534 nasm_free(m->defaults);
535 free_llist(m->expansion);
536 nasm_free(m);
540 * Free all currently defined macros, and free the hash tables
542 static void free_smacro_table(struct hash_table *smt)
544 SMacro *s;
545 const char *key;
546 struct hash_tbl_node *it = NULL;
548 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
549 nasm_free((void *)key);
550 while (s) {
551 SMacro *ns = s->next;
552 nasm_free(s->name);
553 free_tlist(s->expansion);
554 nasm_free(s);
555 s = ns;
558 hash_free(smt);
561 static void free_mmacro_table(struct hash_table *mmt)
563 MMacro *m;
564 const char *key;
565 struct hash_tbl_node *it = NULL;
567 it = NULL;
568 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
569 nasm_free((void *)key);
570 while (m) {
571 MMacro *nm = m->next;
572 free_mmacro(m);
573 m = nm;
576 hash_free(mmt);
579 static void free_macros(void)
581 free_smacro_table(&smacros);
582 free_mmacro_table(&mmacros);
586 * Initialize the hash tables
588 static void init_macros(void)
590 hash_init(&smacros, HASH_LARGE);
591 hash_init(&mmacros, HASH_LARGE);
595 * Pop the context stack.
597 static void ctx_pop(void)
599 Context *c = cstk;
601 cstk = cstk->next;
602 free_smacro_table(&c->localmac);
603 nasm_free(c->name);
604 nasm_free(c);
608 * Search for a key in the hash index; adding it if necessary
609 * (in which case we initialize the data pointer to NULL.)
611 static void **
612 hash_findi_add(struct hash_table *hash, const char *str)
614 struct hash_insert hi;
615 void **r;
616 char *strx;
618 r = hash_findi(hash, str, &hi);
619 if (r)
620 return r;
622 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
623 return hash_add(&hi, strx, NULL);
627 * Like hash_findi, but returns the data element rather than a pointer
628 * to it. Used only when not adding a new element, hence no third
629 * argument.
631 static void *
632 hash_findix(struct hash_table *hash, const char *str)
634 void **p;
636 p = hash_findi(hash, str, NULL);
637 return p ? *p : NULL;
640 #define BUF_DELTA 512
642 * Read a line from the top file in istk, handling multiple CR/LFs
643 * at the end of the line read, and handling spurious ^Zs. Will
644 * return lines from the standard macro set if this has not already
645 * been done.
647 static char *read_line(void)
649 char *buffer, *p, *q;
650 int bufsize, continued_count;
652 if (stdmacpos) {
653 unsigned char c;
654 const unsigned char *p = stdmacpos;
655 char *ret, *q;
656 size_t len = 0;
657 while ((c = *p++)) {
658 if (c >= 0x80)
659 len += pp_directives_len[c-0x80]+1;
660 else
661 len++;
663 ret = nasm_malloc(len+1);
664 q = ret;
665 while ((c = *stdmacpos++)) {
666 if (c >= 0x80) {
667 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
668 q += pp_directives_len[c-0x80];
669 *q++ = ' ';
670 } else {
671 *q++ = c;
674 stdmacpos = p;
675 *q = '\0';
677 if (!*stdmacpos) {
678 /* This was the last of the standard macro chain... */
679 stdmacpos = NULL;
680 if (any_extrastdmac) {
681 stdmacpos = extrastdmac;
682 any_extrastdmac = false;
683 } else if (do_predef) {
684 Line *pd, *l;
685 Token *head, **tail, *t;
688 * Nasty hack: here we push the contents of
689 * `predef' on to the top-level expansion stack,
690 * since this is the most convenient way to
691 * implement the pre-include and pre-define
692 * features.
694 for (pd = predef; pd; pd = pd->next) {
695 head = NULL;
696 tail = &head;
697 for (t = pd->first; t; t = t->next) {
698 *tail = new_Token(NULL, t->type, t->text, 0);
699 tail = &(*tail)->next;
701 l = nasm_malloc(sizeof(Line));
702 l->next = istk->expansion;
703 l->first = head;
704 l->finishes = NULL;
705 istk->expansion = l;
707 do_predef = false;
710 return ret;
713 bufsize = BUF_DELTA;
714 buffer = nasm_malloc(BUF_DELTA);
715 p = buffer;
716 continued_count = 0;
717 while (1) {
718 q = fgets(p, bufsize - (p - buffer), istk->fp);
719 if (!q)
720 break;
721 p += strlen(p);
722 if (p > buffer && p[-1] == '\n') {
723 /* Convert backslash-CRLF line continuation sequences into
724 nothing at all (for DOS and Windows) */
725 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
726 p -= 3;
727 *p = 0;
728 continued_count++;
730 /* Also convert backslash-LF line continuation sequences into
731 nothing at all (for Unix) */
732 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
733 p -= 2;
734 *p = 0;
735 continued_count++;
736 } else {
737 break;
740 if (p - buffer > bufsize - 10) {
741 int32_t offset = p - buffer;
742 bufsize += BUF_DELTA;
743 buffer = nasm_realloc(buffer, bufsize);
744 p = buffer + offset; /* prevent stale-pointer problems */
748 if (!q && p == buffer) {
749 nasm_free(buffer);
750 return NULL;
753 src_set_linnum(src_get_linnum() + istk->lineinc +
754 (continued_count * istk->lineinc));
757 * Play safe: remove CRs as well as LFs, if any of either are
758 * present at the end of the line.
760 while (--p >= buffer && (*p == '\n' || *p == '\r'))
761 *p = '\0';
764 * Handle spurious ^Z, which may be inserted into source files
765 * by some file transfer utilities.
767 buffer[strcspn(buffer, "\032")] = '\0';
769 list->line(LIST_READ, buffer);
771 return buffer;
775 * Tokenize a line of text. This is a very simple process since we
776 * don't need to parse the value out of e.g. numeric tokens: we
777 * simply split one string into many.
779 static Token *tokenize(char *line)
781 char c, *p = line;
782 enum pp_token_type type;
783 Token *list = NULL;
784 Token *t, **tail = &list;
786 while (*line) {
787 p = line;
788 if (*p == '%') {
789 p++;
790 if (nasm_isdigit(*p) ||
791 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
792 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
793 do {
794 p++;
796 while (nasm_isdigit(*p));
797 type = TOK_PREPROC_ID;
798 } else if (*p == '{') {
799 p++;
800 while (*p && *p != '}') {
801 p[-1] = *p;
802 p++;
804 p[-1] = '\0';
805 if (*p)
806 p++;
807 type = TOK_PREPROC_ID;
808 } else if (*p == '[') {
809 int lvl = 1;
810 line += 2; /* Skip the leading %[ */
811 p++;
812 while (lvl && (c = *p++)) {
813 switch (c) {
814 case ']':
815 lvl--;
816 break;
817 case '%':
818 if (*p == '[')
819 lvl++;
820 break;
821 case '\'':
822 case '\"':
823 case '`':
824 p = nasm_skip_string(p)+1;
825 break;
826 default:
827 break;
830 p--;
831 if (*p)
832 *p++ = '\0';
833 if (lvl)
834 error(ERR_NONFATAL, "unterminated %[ construct");
835 type = TOK_INDIRECT;
836 } else if (*p == '?') {
837 type = TOK_PREPROC_Q; /* %? */
838 p++;
839 if (*p == '?') {
840 type = TOK_PREPROC_QQ; /* %?? */
841 p++;
843 } else if (isidchar(*p) ||
844 ((*p == '!' || *p == '%' || *p == '$') &&
845 isidchar(p[1]))) {
846 do {
847 p++;
849 while (isidchar(*p));
850 type = TOK_PREPROC_ID;
851 } else {
852 type = TOK_OTHER;
853 if (*p == '%')
854 p++;
856 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
857 type = TOK_ID;
858 p++;
859 while (*p && isidchar(*p))
860 p++;
861 } else if (*p == '\'' || *p == '"' || *p == '`') {
863 * A string token.
865 type = TOK_STRING;
866 p = nasm_skip_string(p);
868 if (*p) {
869 p++;
870 } else {
871 error(ERR_WARNING|ERR_PASS1, "unterminated string");
872 /* Handling unterminated strings by UNV */
873 /* type = -1; */
875 } else if (isnumstart(*p)) {
876 bool is_hex = false;
877 bool is_float = false;
878 bool has_e = false;
879 char c, *r;
882 * A numeric token.
885 if (*p == '$') {
886 p++;
887 is_hex = true;
890 for (;;) {
891 c = *p++;
893 if (!is_hex && (c == 'e' || c == 'E')) {
894 has_e = true;
895 if (*p == '+' || *p == '-') {
896 /* e can only be followed by +/- if it is either a
897 prefixed hex number or a floating-point number */
898 p++;
899 is_float = true;
901 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
902 is_hex = true;
903 } else if (c == 'P' || c == 'p') {
904 is_float = true;
905 if (*p == '+' || *p == '-')
906 p++;
907 } else if (isnumchar(c) || c == '_')
908 ; /* just advance */
909 else if (c == '.') {
910 /* we need to deal with consequences of the legacy
911 parser, like "1.nolist" being two tokens
912 (TOK_NUMBER, TOK_ID) here; at least give it
913 a shot for now. In the future, we probably need
914 a flex-based scanner with proper pattern matching
915 to do it as well as it can be done. Nothing in
916 the world is going to help the person who wants
917 0x123.p16 interpreted as two tokens, though. */
918 r = p;
919 while (*r == '_')
920 r++;
922 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
923 (!is_hex && (*r == 'e' || *r == 'E')) ||
924 (*r == 'p' || *r == 'P')) {
925 p = r;
926 is_float = true;
927 } else
928 break; /* Terminate the token */
929 } else
930 break;
932 p--; /* Point to first character beyond number */
934 if (has_e && !is_hex) {
935 /* 1e13 is floating-point, but 1e13h is not */
936 is_float = true;
939 type = is_float ? TOK_FLOAT : TOK_NUMBER;
940 } else if (nasm_isspace(*p)) {
941 type = TOK_WHITESPACE;
942 p++;
943 while (*p && nasm_isspace(*p))
944 p++;
946 * Whitespace just before end-of-line is discarded by
947 * pretending it's a comment; whitespace just before a
948 * comment gets lumped into the comment.
950 if (!*p || *p == ';') {
951 type = TOK_COMMENT;
952 while (*p)
953 p++;
955 } else if (*p == ';') {
956 type = TOK_COMMENT;
957 while (*p)
958 p++;
959 } else {
961 * Anything else is an operator of some kind. We check
962 * for all the double-character operators (>>, <<, //,
963 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
964 * else is a single-character operator.
966 type = TOK_OTHER;
967 if ((p[0] == '>' && p[1] == '>') ||
968 (p[0] == '<' && p[1] == '<') ||
969 (p[0] == '/' && p[1] == '/') ||
970 (p[0] == '<' && p[1] == '=') ||
971 (p[0] == '>' && p[1] == '=') ||
972 (p[0] == '=' && p[1] == '=') ||
973 (p[0] == '!' && p[1] == '=') ||
974 (p[0] == '<' && p[1] == '>') ||
975 (p[0] == '&' && p[1] == '&') ||
976 (p[0] == '|' && p[1] == '|') ||
977 (p[0] == '^' && p[1] == '^')) {
978 p++;
980 p++;
983 /* Handling unterminated string by UNV */
984 /*if (type == -1)
986 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
987 t->text[p-line] = *line;
988 tail = &t->next;
990 else */
991 if (type != TOK_COMMENT) {
992 *tail = t = new_Token(NULL, type, line, p - line);
993 tail = &t->next;
995 line = p;
997 return list;
1001 * this function allocates a new managed block of memory and
1002 * returns a pointer to the block. The managed blocks are
1003 * deleted only all at once by the delete_Blocks function.
1005 static void *new_Block(size_t size)
1007 Blocks *b = &blocks;
1009 /* first, get to the end of the linked list */
1010 while (b->next)
1011 b = b->next;
1012 /* now allocate the requested chunk */
1013 b->chunk = nasm_malloc(size);
1015 /* now allocate a new block for the next request */
1016 b->next = nasm_malloc(sizeof(Blocks));
1017 /* and initialize the contents of the new block */
1018 b->next->next = NULL;
1019 b->next->chunk = NULL;
1020 return b->chunk;
1024 * this function deletes all managed blocks of memory
1026 static void delete_Blocks(void)
1028 Blocks *a, *b = &blocks;
1031 * keep in mind that the first block, pointed to by blocks
1032 * is a static and not dynamically allocated, so we don't
1033 * free it.
1035 while (b) {
1036 if (b->chunk)
1037 nasm_free(b->chunk);
1038 a = b;
1039 b = b->next;
1040 if (a != &blocks)
1041 nasm_free(a);
1046 * this function creates a new Token and passes a pointer to it
1047 * back to the caller. It sets the type and text elements, and
1048 * also the a.mac and next elements to NULL.
1050 static Token *new_Token(Token * next, enum pp_token_type type,
1051 const char *text, int txtlen)
1053 Token *t;
1054 int i;
1056 if (freeTokens == NULL) {
1057 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1058 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1059 freeTokens[i].next = &freeTokens[i + 1];
1060 freeTokens[i].next = NULL;
1062 t = freeTokens;
1063 freeTokens = t->next;
1064 t->next = next;
1065 t->a.mac = NULL;
1066 t->type = type;
1067 if (type == TOK_WHITESPACE || text == NULL) {
1068 t->text = NULL;
1069 } else {
1070 if (txtlen == 0)
1071 txtlen = strlen(text);
1072 t->text = nasm_malloc(txtlen+1);
1073 memcpy(t->text, text, txtlen);
1074 t->text[txtlen] = '\0';
1076 return t;
1079 static Token *delete_Token(Token * t)
1081 Token *next = t->next;
1082 nasm_free(t->text);
1083 t->next = freeTokens;
1084 freeTokens = t;
1085 return next;
1089 * Convert a line of tokens back into text.
1090 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1091 * will be transformed into ..@ctxnum.xxx
1093 static char *detoken(Token * tlist, bool expand_locals)
1095 Token *t;
1096 int len;
1097 char *line, *p;
1098 const char *q;
1100 len = 0;
1101 for (t = tlist; t; t = t->next) {
1102 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1103 char *p = getenv(t->text + 2);
1104 nasm_free(t->text);
1105 if (p)
1106 t->text = nasm_strdup(p);
1107 else
1108 t->text = NULL;
1110 /* Expand local macros here and not during preprocessing */
1111 if (expand_locals &&
1112 t->type == TOK_PREPROC_ID && t->text &&
1113 t->text[0] == '%' && t->text[1] == '$') {
1114 Context *ctx = get_ctx(t->text, false);
1115 if (ctx) {
1116 char buffer[40];
1117 char *p, *q = t->text + 2;
1119 q += strspn(q, "$");
1120 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1121 p = nasm_strcat(buffer, q);
1122 nasm_free(t->text);
1123 t->text = p;
1126 if (t->type == TOK_WHITESPACE) {
1127 len++;
1128 } else if (t->text) {
1129 len += strlen(t->text);
1132 p = line = nasm_malloc(len + 1);
1133 for (t = tlist; t; t = t->next) {
1134 if (t->type == TOK_WHITESPACE) {
1135 *p++ = ' ';
1136 } else if (t->text) {
1137 q = t->text;
1138 while (*q)
1139 *p++ = *q++;
1142 *p = '\0';
1143 return line;
1147 * A scanner, suitable for use by the expression evaluator, which
1148 * operates on a line of Tokens. Expects a pointer to a pointer to
1149 * the first token in the line to be passed in as its private_data
1150 * field.
1152 * FIX: This really needs to be unified with stdscan.
1154 static int ppscan(void *private_data, struct tokenval *tokval)
1156 Token **tlineptr = private_data;
1157 Token *tline;
1158 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1160 do {
1161 tline = *tlineptr;
1162 *tlineptr = tline ? tline->next : NULL;
1164 while (tline && (tline->type == TOK_WHITESPACE ||
1165 tline->type == TOK_COMMENT));
1167 if (!tline)
1168 return tokval->t_type = TOKEN_EOS;
1170 tokval->t_charptr = tline->text;
1172 if (tline->text[0] == '$' && !tline->text[1])
1173 return tokval->t_type = TOKEN_HERE;
1174 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1175 return tokval->t_type = TOKEN_BASE;
1177 if (tline->type == TOK_ID) {
1178 p = tokval->t_charptr = tline->text;
1179 if (p[0] == '$') {
1180 tokval->t_charptr++;
1181 return tokval->t_type = TOKEN_ID;
1184 for (r = p, s = ourcopy; *r; r++) {
1185 if (r >= p+MAX_KEYWORD)
1186 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1187 *s++ = nasm_tolower(*r);
1189 *s = '\0';
1190 /* right, so we have an identifier sitting in temp storage. now,
1191 * is it actually a register or instruction name, or what? */
1192 return nasm_token_hash(ourcopy, tokval);
1195 if (tline->type == TOK_NUMBER) {
1196 bool rn_error;
1197 tokval->t_integer = readnum(tline->text, &rn_error);
1198 tokval->t_charptr = tline->text;
1199 if (rn_error)
1200 return tokval->t_type = TOKEN_ERRNUM;
1201 else
1202 return tokval->t_type = TOKEN_NUM;
1205 if (tline->type == TOK_FLOAT) {
1206 return tokval->t_type = TOKEN_FLOAT;
1209 if (tline->type == TOK_STRING) {
1210 char bq, *ep;
1212 bq = tline->text[0];
1213 tokval->t_charptr = tline->text;
1214 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1216 if (ep[0] != bq || ep[1] != '\0')
1217 return tokval->t_type = TOKEN_ERRSTR;
1218 else
1219 return tokval->t_type = TOKEN_STR;
1222 if (tline->type == TOK_OTHER) {
1223 if (!strcmp(tline->text, "<<"))
1224 return tokval->t_type = TOKEN_SHL;
1225 if (!strcmp(tline->text, ">>"))
1226 return tokval->t_type = TOKEN_SHR;
1227 if (!strcmp(tline->text, "//"))
1228 return tokval->t_type = TOKEN_SDIV;
1229 if (!strcmp(tline->text, "%%"))
1230 return tokval->t_type = TOKEN_SMOD;
1231 if (!strcmp(tline->text, "=="))
1232 return tokval->t_type = TOKEN_EQ;
1233 if (!strcmp(tline->text, "<>"))
1234 return tokval->t_type = TOKEN_NE;
1235 if (!strcmp(tline->text, "!="))
1236 return tokval->t_type = TOKEN_NE;
1237 if (!strcmp(tline->text, "<="))
1238 return tokval->t_type = TOKEN_LE;
1239 if (!strcmp(tline->text, ">="))
1240 return tokval->t_type = TOKEN_GE;
1241 if (!strcmp(tline->text, "&&"))
1242 return tokval->t_type = TOKEN_DBL_AND;
1243 if (!strcmp(tline->text, "^^"))
1244 return tokval->t_type = TOKEN_DBL_XOR;
1245 if (!strcmp(tline->text, "||"))
1246 return tokval->t_type = TOKEN_DBL_OR;
1250 * We have no other options: just return the first character of
1251 * the token text.
1253 return tokval->t_type = tline->text[0];
1257 * Compare a string to the name of an existing macro; this is a
1258 * simple wrapper which calls either strcmp or nasm_stricmp
1259 * depending on the value of the `casesense' parameter.
1261 static int mstrcmp(const char *p, const char *q, bool casesense)
1263 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1267 * Compare a string to the name of an existing macro; this is a
1268 * simple wrapper which calls either strcmp or nasm_stricmp
1269 * depending on the value of the `casesense' parameter.
1271 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1273 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1277 * Return the Context structure associated with a %$ token. Return
1278 * NULL, having _already_ reported an error condition, if the
1279 * context stack isn't deep enough for the supplied number of $
1280 * signs.
1281 * If all_contexts == true, contexts that enclose current are
1282 * also scanned for such smacro, until it is found; if not -
1283 * only the context that directly results from the number of $'s
1284 * in variable's name.
1286 static Context *get_ctx(const char *name, bool all_contexts)
1288 Context *ctx;
1289 SMacro *m;
1290 int i;
1292 if (!name || name[0] != '%' || name[1] != '$')
1293 return NULL;
1295 if (!cstk) {
1296 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1297 return NULL;
1300 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1301 ctx = ctx->next;
1302 /* i--; Lino - 02/25/02 */
1304 if (!ctx) {
1305 error(ERR_NONFATAL, "`%s': context stack is only"
1306 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1307 return NULL;
1309 if (!all_contexts)
1310 return ctx;
1312 do {
1313 /* Search for this smacro in found context */
1314 m = hash_findix(&ctx->localmac, name);
1315 while (m) {
1316 if (!mstrcmp(m->name, name, m->casesense))
1317 return ctx;
1318 m = m->next;
1320 ctx = ctx->next;
1322 while (ctx);
1323 return NULL;
1327 * Check to see if a file is already in a string list
1329 static bool in_list(const StrList *list, const char *str)
1331 while (list) {
1332 if (!strcmp(list->str, str))
1333 return true;
1334 list = list->next;
1336 return false;
1340 * Open an include file. This routine must always return a valid
1341 * file pointer if it returns - it's responsible for throwing an
1342 * ERR_FATAL and bombing out completely if not. It should also try
1343 * the include path one by one until it finds the file or reaches
1344 * the end of the path.
1346 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1347 bool missing_ok)
1349 FILE *fp;
1350 char *prefix = "";
1351 IncPath *ip = ipath;
1352 int len = strlen(file);
1353 size_t prefix_len = 0;
1354 StrList *sl;
1356 while (1) {
1357 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1358 memcpy(sl->str, prefix, prefix_len);
1359 memcpy(sl->str+prefix_len, file, len+1);
1360 fp = fopen(sl->str, "r");
1361 if (fp && dhead && !in_list(*dhead, sl->str)) {
1362 sl->next = NULL;
1363 **dtail = sl;
1364 *dtail = &sl->next;
1365 } else {
1366 nasm_free(sl);
1368 if (fp)
1369 return fp;
1370 if (!ip) {
1371 if (!missing_ok)
1372 break;
1373 prefix = NULL;
1374 } else {
1375 prefix = ip->path;
1376 ip = ip->next;
1378 if (prefix) {
1379 prefix_len = strlen(prefix);
1380 } else {
1381 /* -MG given and file not found */
1382 if (dhead && !in_list(*dhead, file)) {
1383 sl = nasm_malloc(len+1+sizeof sl->next);
1384 sl->next = NULL;
1385 strcpy(sl->str, file);
1386 **dtail = sl;
1387 *dtail = &sl->next;
1389 return NULL;
1393 error(ERR_FATAL, "unable to open include file `%s'", file);
1394 return NULL; /* never reached - placate compilers */
1398 * Determine if we should warn on defining a single-line macro of
1399 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1400 * return true if _any_ single-line macro of that name is defined.
1401 * Otherwise, will return true if a single-line macro with either
1402 * `nparam' or no parameters is defined.
1404 * If a macro with precisely the right number of parameters is
1405 * defined, or nparam is -1, the address of the definition structure
1406 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1407 * is NULL, no action will be taken regarding its contents, and no
1408 * error will occur.
1410 * Note that this is also called with nparam zero to resolve
1411 * `ifdef'.
1413 * If you already know which context macro belongs to, you can pass
1414 * the context pointer as first parameter; if you won't but name begins
1415 * with %$ the context will be automatically computed. If all_contexts
1416 * is true, macro will be searched in outer contexts as well.
1418 static bool
1419 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1420 bool nocase)
1422 struct hash_table *smtbl;
1423 SMacro *m;
1425 if (ctx) {
1426 smtbl = &ctx->localmac;
1427 } else if (name[0] == '%' && name[1] == '$') {
1428 if (cstk)
1429 ctx = get_ctx(name, false);
1430 if (!ctx)
1431 return false; /* got to return _something_ */
1432 smtbl = &ctx->localmac;
1433 } else {
1434 smtbl = &smacros;
1436 m = (SMacro *) hash_findix(smtbl, name);
1438 while (m) {
1439 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1440 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1441 if (defn) {
1442 if (nparam == (int) m->nparam || nparam == -1)
1443 *defn = m;
1444 else
1445 *defn = NULL;
1447 return true;
1449 m = m->next;
1452 return false;
1456 * Count and mark off the parameters in a multi-line macro call.
1457 * This is called both from within the multi-line macro expansion
1458 * code, and also to mark off the default parameters when provided
1459 * in a %macro definition line.
1461 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1463 int paramsize, brace;
1465 *nparam = paramsize = 0;
1466 *params = NULL;
1467 while (t) {
1468 /* +1: we need space for the final NULL */
1469 if (*nparam+1 >= paramsize) {
1470 paramsize += PARAM_DELTA;
1471 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1473 skip_white_(t);
1474 brace = false;
1475 if (tok_is_(t, "{"))
1476 brace = true;
1477 (*params)[(*nparam)++] = t;
1478 while (tok_isnt_(t, brace ? "}" : ","))
1479 t = t->next;
1480 if (t) { /* got a comma/brace */
1481 t = t->next;
1482 if (brace) {
1484 * Now we've found the closing brace, look further
1485 * for the comma.
1487 skip_white_(t);
1488 if (tok_isnt_(t, ",")) {
1489 error(ERR_NONFATAL,
1490 "braces do not enclose all of macro parameter");
1491 while (tok_isnt_(t, ","))
1492 t = t->next;
1494 if (t)
1495 t = t->next; /* eat the comma */
1502 * Determine whether one of the various `if' conditions is true or
1503 * not.
1505 * We must free the tline we get passed.
1507 static bool if_condition(Token * tline, enum preproc_token ct)
1509 enum pp_conditional i = PP_COND(ct);
1510 bool j;
1511 Token *t, *tt, **tptr, *origline;
1512 struct tokenval tokval;
1513 expr *evalresult;
1514 enum pp_token_type needtype;
1516 origline = tline;
1518 switch (i) {
1519 case PPC_IFCTX:
1520 j = false; /* have we matched yet? */
1521 while (true) {
1522 skip_white_(tline);
1523 if (!tline)
1524 break;
1525 if (tline->type != TOK_ID) {
1526 error(ERR_NONFATAL,
1527 "`%s' expects context identifiers", pp_directives[ct]);
1528 free_tlist(origline);
1529 return -1;
1531 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1532 j = true;
1533 tline = tline->next;
1535 break;
1537 case PPC_IFDEF:
1538 j = false; /* have we matched yet? */
1539 while (tline) {
1540 skip_white_(tline);
1541 if (!tline || (tline->type != TOK_ID &&
1542 (tline->type != TOK_PREPROC_ID ||
1543 tline->text[1] != '$'))) {
1544 error(ERR_NONFATAL,
1545 "`%s' expects macro identifiers", pp_directives[ct]);
1546 goto fail;
1548 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1549 j = true;
1550 tline = tline->next;
1552 break;
1554 case PPC_IFIDN:
1555 case PPC_IFIDNI:
1556 tline = expand_smacro(tline);
1557 t = tt = tline;
1558 while (tok_isnt_(tt, ","))
1559 tt = tt->next;
1560 if (!tt) {
1561 error(ERR_NONFATAL,
1562 "`%s' expects two comma-separated arguments",
1563 pp_directives[ct]);
1564 goto fail;
1566 tt = tt->next;
1567 j = true; /* assume equality unless proved not */
1568 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1569 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1570 error(ERR_NONFATAL, "`%s': more than one comma on line",
1571 pp_directives[ct]);
1572 goto fail;
1574 if (t->type == TOK_WHITESPACE) {
1575 t = t->next;
1576 continue;
1578 if (tt->type == TOK_WHITESPACE) {
1579 tt = tt->next;
1580 continue;
1582 if (tt->type != t->type) {
1583 j = false; /* found mismatching tokens */
1584 break;
1586 /* When comparing strings, need to unquote them first */
1587 if (t->type == TOK_STRING) {
1588 size_t l1 = nasm_unquote(t->text, NULL);
1589 size_t l2 = nasm_unquote(tt->text, NULL);
1591 if (l1 != l2) {
1592 j = false;
1593 break;
1595 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1596 j = false;
1597 break;
1599 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1600 j = false; /* found mismatching tokens */
1601 break;
1604 t = t->next;
1605 tt = tt->next;
1607 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1608 j = false; /* trailing gunk on one end or other */
1609 break;
1611 case PPC_IFMACRO:
1613 bool found = false;
1614 MMacro searching, *mmac;
1616 tline = tline->next;
1617 skip_white_(tline);
1618 tline = expand_id(tline);
1619 if (!tok_type_(tline, TOK_ID)) {
1620 error(ERR_NONFATAL,
1621 "`%s' expects a macro name", pp_directives[ct]);
1622 goto fail;
1624 searching.name = nasm_strdup(tline->text);
1625 searching.casesense = true;
1626 searching.plus = false;
1627 searching.nolist = false;
1628 searching.in_progress = 0;
1629 searching.rep_nest = NULL;
1630 searching.nparam_min = 0;
1631 searching.nparam_max = INT_MAX;
1632 tline = expand_smacro(tline->next);
1633 skip_white_(tline);
1634 if (!tline) {
1635 } else if (!tok_type_(tline, TOK_NUMBER)) {
1636 error(ERR_NONFATAL,
1637 "`%s' expects a parameter count or nothing",
1638 pp_directives[ct]);
1639 } else {
1640 searching.nparam_min = searching.nparam_max =
1641 readnum(tline->text, &j);
1642 if (j)
1643 error(ERR_NONFATAL,
1644 "unable to parse parameter count `%s'",
1645 tline->text);
1647 if (tline && tok_is_(tline->next, "-")) {
1648 tline = tline->next->next;
1649 if (tok_is_(tline, "*"))
1650 searching.nparam_max = INT_MAX;
1651 else if (!tok_type_(tline, TOK_NUMBER))
1652 error(ERR_NONFATAL,
1653 "`%s' expects a parameter count after `-'",
1654 pp_directives[ct]);
1655 else {
1656 searching.nparam_max = readnum(tline->text, &j);
1657 if (j)
1658 error(ERR_NONFATAL,
1659 "unable to parse parameter count `%s'",
1660 tline->text);
1661 if (searching.nparam_min > searching.nparam_max)
1662 error(ERR_NONFATAL,
1663 "minimum parameter count exceeds maximum");
1666 if (tline && tok_is_(tline->next, "+")) {
1667 tline = tline->next;
1668 searching.plus = true;
1670 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1671 while (mmac) {
1672 if (!strcmp(mmac->name, searching.name) &&
1673 (mmac->nparam_min <= searching.nparam_max
1674 || searching.plus)
1675 && (searching.nparam_min <= mmac->nparam_max
1676 || mmac->plus)) {
1677 found = true;
1678 break;
1680 mmac = mmac->next;
1682 if(tline && tline->next)
1683 error(ERR_WARNING|ERR_PASS1,
1684 "trailing garbage after %%ifmacro ignored");
1685 nasm_free(searching.name);
1686 j = found;
1687 break;
1690 case PPC_IFID:
1691 needtype = TOK_ID;
1692 goto iftype;
1693 case PPC_IFNUM:
1694 needtype = TOK_NUMBER;
1695 goto iftype;
1696 case PPC_IFSTR:
1697 needtype = TOK_STRING;
1698 goto iftype;
1700 iftype:
1701 t = tline = expand_smacro(tline);
1703 while (tok_type_(t, TOK_WHITESPACE) ||
1704 (needtype == TOK_NUMBER &&
1705 tok_type_(t, TOK_OTHER) &&
1706 (t->text[0] == '-' || t->text[0] == '+') &&
1707 !t->text[1]))
1708 t = t->next;
1710 j = tok_type_(t, needtype);
1711 break;
1713 case PPC_IFTOKEN:
1714 t = tline = expand_smacro(tline);
1715 while (tok_type_(t, TOK_WHITESPACE))
1716 t = t->next;
1718 j = false;
1719 if (t) {
1720 t = t->next; /* Skip the actual token */
1721 while (tok_type_(t, TOK_WHITESPACE))
1722 t = t->next;
1723 j = !t; /* Should be nothing left */
1725 break;
1727 case PPC_IFEMPTY:
1728 t = tline = expand_smacro(tline);
1729 while (tok_type_(t, TOK_WHITESPACE))
1730 t = t->next;
1732 j = !t; /* Should be empty */
1733 break;
1735 case PPC_IF:
1736 t = tline = expand_smacro(tline);
1737 tptr = &t;
1738 tokval.t_type = TOKEN_INVALID;
1739 evalresult = evaluate(ppscan, tptr, &tokval,
1740 NULL, pass | CRITICAL, error, NULL);
1741 if (!evalresult)
1742 return -1;
1743 if (tokval.t_type)
1744 error(ERR_WARNING|ERR_PASS1,
1745 "trailing garbage after expression ignored");
1746 if (!is_simple(evalresult)) {
1747 error(ERR_NONFATAL,
1748 "non-constant value given to `%s'", pp_directives[ct]);
1749 goto fail;
1751 j = reloc_value(evalresult) != 0;
1752 break;
1754 default:
1755 error(ERR_FATAL,
1756 "preprocessor directive `%s' not yet implemented",
1757 pp_directives[ct]);
1758 goto fail;
1761 free_tlist(origline);
1762 return j ^ PP_NEGATIVE(ct);
1764 fail:
1765 free_tlist(origline);
1766 return -1;
1770 * Common code for defining an smacro
1772 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1773 int nparam, Token *expansion)
1775 SMacro *smac, **smhead;
1776 struct hash_table *smtbl;
1778 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1779 if (!smac) {
1780 error(ERR_WARNING|ERR_PASS1,
1781 "single-line macro `%s' defined both with and"
1782 " without parameters", mname);
1784 /* Some instances of the old code considered this a failure,
1785 some others didn't. What is the right thing to do here? */
1786 free_tlist(expansion);
1787 return false; /* Failure */
1788 } else {
1790 * We're redefining, so we have to take over an
1791 * existing SMacro structure. This means freeing
1792 * what was already in it.
1794 nasm_free(smac->name);
1795 free_tlist(smac->expansion);
1797 } else {
1798 smtbl = ctx ? &ctx->localmac : &smacros;
1799 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1800 smac = nasm_malloc(sizeof(SMacro));
1801 smac->next = *smhead;
1802 *smhead = smac;
1804 smac->name = nasm_strdup(mname);
1805 smac->casesense = casesense;
1806 smac->nparam = nparam;
1807 smac->expansion = expansion;
1808 smac->in_progress = false;
1809 return true; /* Success */
1813 * Undefine an smacro
1815 static void undef_smacro(Context *ctx, const char *mname)
1817 SMacro **smhead, *s, **sp;
1818 struct hash_table *smtbl;
1820 smtbl = ctx ? &ctx->localmac : &smacros;
1821 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1823 if (smhead) {
1825 * We now have a macro name... go hunt for it.
1827 sp = smhead;
1828 while ((s = *sp) != NULL) {
1829 if (!mstrcmp(s->name, mname, s->casesense)) {
1830 *sp = s->next;
1831 nasm_free(s->name);
1832 free_tlist(s->expansion);
1833 nasm_free(s);
1834 } else {
1835 sp = &s->next;
1842 * Parse a mmacro specification.
1844 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1846 bool err;
1848 tline = tline->next;
1849 skip_white_(tline);
1850 tline = expand_id(tline);
1851 if (!tok_type_(tline, TOK_ID)) {
1852 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1853 return false;
1856 def->name = nasm_strdup(tline->text);
1857 def->plus = false;
1858 def->nolist = false;
1859 def->in_progress = 0;
1860 def->rep_nest = NULL;
1861 def->nparam_min = 0;
1862 def->nparam_max = 0;
1864 tline = expand_smacro(tline->next);
1865 skip_white_(tline);
1866 if (!tok_type_(tline, TOK_NUMBER)) {
1867 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1868 } else {
1869 def->nparam_min = def->nparam_max =
1870 readnum(tline->text, &err);
1871 if (err)
1872 error(ERR_NONFATAL,
1873 "unable to parse parameter count `%s'", tline->text);
1875 if (tline && tok_is_(tline->next, "-")) {
1876 tline = tline->next->next;
1877 if (tok_is_(tline, "*")) {
1878 def->nparam_max = INT_MAX;
1879 } else if (!tok_type_(tline, TOK_NUMBER)) {
1880 error(ERR_NONFATAL,
1881 "`%s' expects a parameter count after `-'", directive);
1882 } else {
1883 def->nparam_max = readnum(tline->text, &err);
1884 if (err) {
1885 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1886 tline->text);
1888 if (def->nparam_min > def->nparam_max) {
1889 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1893 if (tline && tok_is_(tline->next, "+")) {
1894 tline = tline->next;
1895 def->plus = true;
1897 if (tline && tok_type_(tline->next, TOK_ID) &&
1898 !nasm_stricmp(tline->next->text, ".nolist")) {
1899 tline = tline->next;
1900 def->nolist = true;
1904 * Handle default parameters.
1906 if (tline && tline->next) {
1907 def->dlist = tline->next;
1908 tline->next = NULL;
1909 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1910 } else {
1911 def->dlist = NULL;
1912 def->defaults = NULL;
1914 def->expansion = NULL;
1916 if(def->defaults &&
1917 def->ndefs > def->nparam_max - def->nparam_min &&
1918 !def->plus)
1919 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1920 "too many default macro parameters");
1922 return true;
1927 * Decode a size directive
1929 static int parse_size(const char *str) {
1930 static const char *size_names[] =
1931 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1932 static const int sizes[] =
1933 { 0, 1, 4, 16, 8, 10, 2, 32 };
1935 return sizes[bsii(str, size_names, elements(size_names))+1];
1939 * find and process preprocessor directive in passed line
1940 * Find out if a line contains a preprocessor directive, and deal
1941 * with it if so.
1943 * If a directive _is_ found, it is the responsibility of this routine
1944 * (and not the caller) to free_tlist() the line.
1946 * @param tline a pointer to the current tokeninzed line linked list
1947 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1950 static int do_directive(Token * tline)
1952 enum preproc_token i;
1953 int j;
1954 bool err;
1955 int nparam;
1956 bool nolist;
1957 bool casesense;
1958 int k, m;
1959 int offset;
1960 char *p, *pp, *mname;
1961 Include *inc;
1962 Context *ctx;
1963 Cond *cond;
1964 MMacro *mmac, **mmhead;
1965 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1966 Line *l;
1967 struct tokenval tokval;
1968 expr *evalresult;
1969 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1970 int64_t count;
1971 size_t len;
1972 int severity;
1974 origline = tline;
1976 skip_white_(tline);
1977 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
1978 (tline->text[1] == '%' || tline->text[1] == '$'
1979 || tline->text[1] == '!'))
1980 return NO_DIRECTIVE_FOUND;
1982 i = pp_token_hash(tline->text);
1985 * If we're in a non-emitting branch of a condition construct,
1986 * or walking to the end of an already terminated %rep block,
1987 * we should ignore all directives except for condition
1988 * directives.
1990 if (((istk->conds && !emitting(istk->conds->state)) ||
1991 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1992 return NO_DIRECTIVE_FOUND;
1996 * If we're defining a macro or reading a %rep block, we should
1997 * ignore all directives except for %macro/%imacro (which nest),
1998 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1999 * If we're in a %rep block, another %rep nests, so should be let through.
2001 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2002 i != PP_ENDMACRO && i != PP_ENDM &&
2003 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2004 return NO_DIRECTIVE_FOUND;
2007 if (defining) {
2008 if (i == PP_MACRO || i == PP_IMACRO) {
2009 nested_mac_count++;
2010 return NO_DIRECTIVE_FOUND;
2011 } else if (nested_mac_count > 0) {
2012 if (i == PP_ENDMACRO) {
2013 nested_mac_count--;
2014 return NO_DIRECTIVE_FOUND;
2017 if (!defining->name) {
2018 if (i == PP_REP) {
2019 nested_rep_count++;
2020 return NO_DIRECTIVE_FOUND;
2021 } else if (nested_rep_count > 0) {
2022 if (i == PP_ENDREP) {
2023 nested_rep_count--;
2024 return NO_DIRECTIVE_FOUND;
2030 switch (i) {
2031 case PP_INVALID:
2032 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2033 tline->text);
2034 return NO_DIRECTIVE_FOUND; /* didn't get it */
2036 case PP_STACKSIZE:
2037 /* Directive to tell NASM what the default stack size is. The
2038 * default is for a 16-bit stack, and this can be overriden with
2039 * %stacksize large.
2040 * the following form:
2042 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2044 tline = tline->next;
2045 if (tline && tline->type == TOK_WHITESPACE)
2046 tline = tline->next;
2047 if (!tline || tline->type != TOK_ID) {
2048 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2049 free_tlist(origline);
2050 return DIRECTIVE_FOUND;
2052 if (nasm_stricmp(tline->text, "flat") == 0) {
2053 /* All subsequent ARG directives are for a 32-bit stack */
2054 StackSize = 4;
2055 StackPointer = "ebp";
2056 ArgOffset = 8;
2057 LocalOffset = 0;
2058 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2059 /* All subsequent ARG directives are for a 64-bit stack */
2060 StackSize = 8;
2061 StackPointer = "rbp";
2062 ArgOffset = 8;
2063 LocalOffset = 0;
2064 } else if (nasm_stricmp(tline->text, "large") == 0) {
2065 /* All subsequent ARG directives are for a 16-bit stack,
2066 * far function call.
2068 StackSize = 2;
2069 StackPointer = "bp";
2070 ArgOffset = 4;
2071 LocalOffset = 0;
2072 } else if (nasm_stricmp(tline->text, "small") == 0) {
2073 /* All subsequent ARG directives are for a 16-bit stack,
2074 * far function call. We don't support near functions.
2076 StackSize = 2;
2077 StackPointer = "bp";
2078 ArgOffset = 6;
2079 LocalOffset = 0;
2080 } else {
2081 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2082 free_tlist(origline);
2083 return DIRECTIVE_FOUND;
2085 free_tlist(origline);
2086 return DIRECTIVE_FOUND;
2088 case PP_ARG:
2089 /* TASM like ARG directive to define arguments to functions, in
2090 * the following form:
2092 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2094 offset = ArgOffset;
2095 do {
2096 char *arg, directive[256];
2097 int size = StackSize;
2099 /* Find the argument name */
2100 tline = tline->next;
2101 if (tline && tline->type == TOK_WHITESPACE)
2102 tline = tline->next;
2103 if (!tline || tline->type != TOK_ID) {
2104 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND;
2108 arg = tline->text;
2110 /* Find the argument size type */
2111 tline = tline->next;
2112 if (!tline || tline->type != TOK_OTHER
2113 || tline->text[0] != ':') {
2114 error(ERR_NONFATAL,
2115 "Syntax error processing `%%arg' directive");
2116 free_tlist(origline);
2117 return DIRECTIVE_FOUND;
2119 tline = tline->next;
2120 if (!tline || tline->type != TOK_ID) {
2121 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2122 free_tlist(origline);
2123 return DIRECTIVE_FOUND;
2126 /* Allow macro expansion of type parameter */
2127 tt = tokenize(tline->text);
2128 tt = expand_smacro(tt);
2129 size = parse_size(tt->text);
2130 if (!size) {
2131 error(ERR_NONFATAL,
2132 "Invalid size type for `%%arg' missing directive");
2133 free_tlist(tt);
2134 free_tlist(origline);
2135 return DIRECTIVE_FOUND;
2137 free_tlist(tt);
2139 /* Round up to even stack slots */
2140 size = (size+StackSize-1) & ~(StackSize-1);
2142 /* Now define the macro for the argument */
2143 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2144 arg, StackPointer, offset);
2145 do_directive(tokenize(directive));
2146 offset += size;
2148 /* Move to the next argument in the list */
2149 tline = tline->next;
2150 if (tline && tline->type == TOK_WHITESPACE)
2151 tline = tline->next;
2152 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2153 ArgOffset = offset;
2154 free_tlist(origline);
2155 return DIRECTIVE_FOUND;
2157 case PP_LOCAL:
2158 /* TASM like LOCAL directive to define local variables for a
2159 * function, in the following form:
2161 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2163 * The '= LocalSize' at the end is ignored by NASM, but is
2164 * required by TASM to define the local parameter size (and used
2165 * by the TASM macro package).
2167 offset = LocalOffset;
2168 do {
2169 char *local, directive[256];
2170 int size = StackSize;
2172 /* Find the argument name */
2173 tline = tline->next;
2174 if (tline && tline->type == TOK_WHITESPACE)
2175 tline = tline->next;
2176 if (!tline || tline->type != TOK_ID) {
2177 error(ERR_NONFATAL,
2178 "`%%local' missing argument parameter");
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
2182 local = tline->text;
2184 /* Find the argument size type */
2185 tline = tline->next;
2186 if (!tline || tline->type != TOK_OTHER
2187 || tline->text[0] != ':') {
2188 error(ERR_NONFATAL,
2189 "Syntax error processing `%%local' directive");
2190 free_tlist(origline);
2191 return DIRECTIVE_FOUND;
2193 tline = tline->next;
2194 if (!tline || tline->type != TOK_ID) {
2195 error(ERR_NONFATAL,
2196 "`%%local' missing size type parameter");
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
2201 /* Allow macro expansion of type parameter */
2202 tt = tokenize(tline->text);
2203 tt = expand_smacro(tt);
2204 size = parse_size(tt->text);
2205 if (!size) {
2206 error(ERR_NONFATAL,
2207 "Invalid size type for `%%local' missing directive");
2208 free_tlist(tt);
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
2212 free_tlist(tt);
2214 /* Round up to even stack slots */
2215 size = (size+StackSize-1) & ~(StackSize-1);
2217 offset += size; /* Negative offset, increment before */
2219 /* Now define the macro for the argument */
2220 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2221 local, StackPointer, offset);
2222 do_directive(tokenize(directive));
2224 /* Now define the assign to setup the enter_c macro correctly */
2225 snprintf(directive, sizeof(directive),
2226 "%%assign %%$localsize %%$localsize+%d", size);
2227 do_directive(tokenize(directive));
2229 /* Move to the next argument in the list */
2230 tline = tline->next;
2231 if (tline && tline->type == TOK_WHITESPACE)
2232 tline = tline->next;
2233 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2234 LocalOffset = offset;
2235 free_tlist(origline);
2236 return DIRECTIVE_FOUND;
2238 case PP_CLEAR:
2239 if (tline->next)
2240 error(ERR_WARNING|ERR_PASS1,
2241 "trailing garbage after `%%clear' ignored");
2242 free_macros();
2243 init_macros();
2244 free_tlist(origline);
2245 return DIRECTIVE_FOUND;
2247 case PP_DEPEND:
2248 t = tline->next = expand_smacro(tline->next);
2249 skip_white_(t);
2250 if (!t || (t->type != TOK_STRING &&
2251 t->type != TOK_INTERNAL_STRING)) {
2252 error(ERR_NONFATAL, "`%%depend' expects a file name");
2253 free_tlist(origline);
2254 return DIRECTIVE_FOUND; /* but we did _something_ */
2256 if (t->next)
2257 error(ERR_WARNING|ERR_PASS1,
2258 "trailing garbage after `%%depend' ignored");
2259 p = t->text;
2260 if (t->type != TOK_INTERNAL_STRING)
2261 nasm_unquote(p, NULL);
2262 if (dephead && !in_list(*dephead, p)) {
2263 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2264 sl->next = NULL;
2265 strcpy(sl->str, p);
2266 *deptail = sl;
2267 deptail = &sl->next;
2269 free_tlist(origline);
2270 return DIRECTIVE_FOUND;
2272 case PP_INCLUDE:
2273 t = tline->next = expand_smacro(tline->next);
2274 skip_white_(t);
2276 if (!t || (t->type != TOK_STRING &&
2277 t->type != TOK_INTERNAL_STRING)) {
2278 error(ERR_NONFATAL, "`%%include' expects a file name");
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND; /* but we did _something_ */
2282 if (t->next)
2283 error(ERR_WARNING|ERR_PASS1,
2284 "trailing garbage after `%%include' ignored");
2285 p = t->text;
2286 if (t->type != TOK_INTERNAL_STRING)
2287 nasm_unquote(p, NULL);
2288 inc = nasm_malloc(sizeof(Include));
2289 inc->next = istk;
2290 inc->conds = NULL;
2291 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2292 if (!inc->fp) {
2293 /* -MG given but file not found */
2294 nasm_free(inc);
2295 } else {
2296 inc->fname = src_set_fname(nasm_strdup(p));
2297 inc->lineno = src_set_linnum(0);
2298 inc->lineinc = 1;
2299 inc->expansion = NULL;
2300 inc->mstk = NULL;
2301 istk = inc;
2302 list->uplevel(LIST_INCLUDE);
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
2307 case PP_USE:
2309 static macros_t *use_pkg;
2310 const char *pkg_macro;
2312 tline = tline->next;
2313 skip_white_(tline);
2314 tline = expand_id(tline);
2316 if (!tline || (tline->type != TOK_STRING &&
2317 tline->type != TOK_INTERNAL_STRING &&
2318 tline->type != TOK_ID)) {
2319 error(ERR_NONFATAL, "`%%use' expects a package name");
2320 free_tlist(origline);
2321 return DIRECTIVE_FOUND; /* but we did _something_ */
2323 if (tline->next)
2324 error(ERR_WARNING|ERR_PASS1,
2325 "trailing garbage after `%%use' ignored");
2326 if (tline->type == TOK_STRING)
2327 nasm_unquote(tline->text, NULL);
2328 use_pkg = nasm_stdmac_find_package(tline->text);
2329 if (!use_pkg)
2330 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2331 /* The first string will be <%define>__USE_*__ */
2332 pkg_macro = (char *)use_pkg + 1;
2333 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2334 /* Not already included, go ahead and include it */
2335 stdmacpos = use_pkg;
2337 free_tlist(origline);
2338 return DIRECTIVE_FOUND;
2340 case PP_PUSH:
2341 case PP_REPL:
2342 case PP_POP:
2343 tline = tline->next;
2344 skip_white_(tline);
2345 tline = expand_id(tline);
2346 if (tline) {
2347 if (!tok_type_(tline, TOK_ID)) {
2348 error(ERR_NONFATAL, "`%s' expects a context identifier",
2349 pp_directives[i]);
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND; /* but we did _something_ */
2353 if (tline->next)
2354 error(ERR_WARNING|ERR_PASS1,
2355 "trailing garbage after `%s' ignored",
2356 pp_directives[i]);
2357 p = nasm_strdup(tline->text);
2358 } else {
2359 p = NULL; /* Anonymous */
2362 if (i == PP_PUSH) {
2363 ctx = nasm_malloc(sizeof(Context));
2364 ctx->next = cstk;
2365 hash_init(&ctx->localmac, HASH_SMALL);
2366 ctx->name = p;
2367 ctx->number = unique++;
2368 cstk = ctx;
2369 } else {
2370 /* %pop or %repl */
2371 if (!cstk) {
2372 error(ERR_NONFATAL, "`%s': context stack is empty",
2373 pp_directives[i]);
2374 } else if (i == PP_POP) {
2375 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2376 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2377 "expected %s",
2378 cstk->name ? cstk->name : "anonymous", p);
2379 else
2380 ctx_pop();
2381 } else {
2382 /* i == PP_REPL */
2383 nasm_free(cstk->name);
2384 cstk->name = p;
2385 p = NULL;
2387 nasm_free(p);
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2391 case PP_FATAL:
2392 severity = ERR_FATAL|ERR_NO_SEVERITY;
2393 goto issue_error;
2394 case PP_ERROR:
2395 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2396 goto issue_error;
2397 case PP_WARNING:
2398 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
2399 goto issue_error;
2401 issue_error:
2403 /* Only error out if this is the final pass */
2404 if (pass != 2 && i != PP_FATAL)
2405 return DIRECTIVE_FOUND;
2407 tline->next = expand_smacro(tline->next);
2408 tline = tline->next;
2409 skip_white_(tline);
2410 t = tline ? tline->next : NULL;
2411 skip_white_(t);
2412 if (tok_type_(tline, TOK_STRING) && !t) {
2413 /* The line contains only a quoted string */
2414 p = tline->text;
2415 nasm_unquote(p, NULL);
2416 error(severity, "%s: %s", pp_directives[i], p);
2417 } else {
2418 /* Not a quoted string, or more than a quoted string */
2419 p = detoken(tline, false);
2420 error(severity, "%s: %s", pp_directives[i], p);
2421 nasm_free(p);
2423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
2427 CASE_PP_IF:
2428 if (istk->conds && !emitting(istk->conds->state))
2429 j = COND_NEVER;
2430 else {
2431 j = if_condition(tline->next, i);
2432 tline->next = NULL; /* it got freed */
2433 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2435 cond = nasm_malloc(sizeof(Cond));
2436 cond->next = istk->conds;
2437 cond->state = j;
2438 istk->conds = cond;
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND;
2442 CASE_PP_ELIF:
2443 if (!istk->conds)
2444 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2445 switch(istk->conds->state) {
2446 case COND_IF_TRUE:
2447 istk->conds->state = COND_DONE;
2448 break;
2450 case COND_DONE:
2451 case COND_NEVER:
2452 break;
2454 case COND_ELSE_TRUE:
2455 case COND_ELSE_FALSE:
2456 error_precond(ERR_WARNING|ERR_PASS1,
2457 "`%%elif' after `%%else' ignored");
2458 istk->conds->state = COND_NEVER;
2459 break;
2461 case COND_IF_FALSE:
2463 * IMPORTANT: In the case of %if, we will already have
2464 * called expand_mmac_params(); however, if we're
2465 * processing an %elif we must have been in a
2466 * non-emitting mode, which would have inhibited
2467 * the normal invocation of expand_mmac_params().
2468 * Therefore, we have to do it explicitly here.
2470 j = if_condition(expand_mmac_params(tline->next), i);
2471 tline->next = NULL; /* it got freed */
2472 istk->conds->state =
2473 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2474 break;
2476 free_tlist(origline);
2477 return DIRECTIVE_FOUND;
2479 case PP_ELSE:
2480 if (tline->next)
2481 error_precond(ERR_WARNING|ERR_PASS1,
2482 "trailing garbage after `%%else' ignored");
2483 if (!istk->conds)
2484 error(ERR_FATAL, "`%%else': no matching `%%if'");
2485 switch(istk->conds->state) {
2486 case COND_IF_TRUE:
2487 case COND_DONE:
2488 istk->conds->state = COND_ELSE_FALSE;
2489 break;
2491 case COND_NEVER:
2492 break;
2494 case COND_IF_FALSE:
2495 istk->conds->state = COND_ELSE_TRUE;
2496 break;
2498 case COND_ELSE_TRUE:
2499 case COND_ELSE_FALSE:
2500 error_precond(ERR_WARNING|ERR_PASS1,
2501 "`%%else' after `%%else' ignored.");
2502 istk->conds->state = COND_NEVER;
2503 break;
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2508 case PP_ENDIF:
2509 if (tline->next)
2510 error_precond(ERR_WARNING|ERR_PASS1,
2511 "trailing garbage after `%%endif' ignored");
2512 if (!istk->conds)
2513 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2514 cond = istk->conds;
2515 istk->conds = cond->next;
2516 nasm_free(cond);
2517 free_tlist(origline);
2518 return DIRECTIVE_FOUND;
2520 case PP_MACRO:
2521 case PP_IMACRO:
2522 if (defining) {
2523 error(ERR_FATAL,
2524 "`%%%smacro': already defining a macro",
2525 (i == PP_IMACRO ? "i" : ""));
2526 return DIRECTIVE_FOUND;
2528 defining = nasm_malloc(sizeof(MMacro));
2529 defining->casesense = (i == PP_MACRO);
2530 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2531 nasm_free(defining);
2532 defining = NULL;
2533 return DIRECTIVE_FOUND;
2536 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2537 while (mmac) {
2538 if (!strcmp(mmac->name, defining->name) &&
2539 (mmac->nparam_min <= defining->nparam_max
2540 || defining->plus)
2541 && (defining->nparam_min <= mmac->nparam_max
2542 || mmac->plus)) {
2543 error(ERR_WARNING|ERR_PASS1,
2544 "redefining multi-line macro `%s'", defining->name);
2545 return DIRECTIVE_FOUND;
2547 mmac = mmac->next;
2549 free_tlist(origline);
2550 return DIRECTIVE_FOUND;
2552 case PP_ENDM:
2553 case PP_ENDMACRO:
2554 if (! (defining && defining->name)) {
2555 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2556 return DIRECTIVE_FOUND;
2558 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2559 defining->next = *mmhead;
2560 *mmhead = defining;
2561 defining = NULL;
2562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
2565 case PP_UNMACRO:
2566 case PP_UNIMACRO:
2568 MMacro **mmac_p;
2569 MMacro spec;
2571 spec.casesense = (i == PP_UNMACRO);
2572 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2573 return DIRECTIVE_FOUND;
2575 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2576 while (mmac_p && *mmac_p) {
2577 mmac = *mmac_p;
2578 if (mmac->casesense == spec.casesense &&
2579 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2580 mmac->nparam_min == spec.nparam_min &&
2581 mmac->nparam_max == spec.nparam_max &&
2582 mmac->plus == spec.plus) {
2583 *mmac_p = mmac->next;
2584 free_mmacro(mmac);
2585 } else {
2586 mmac_p = &mmac->next;
2589 free_tlist(origline);
2590 free_tlist(spec.dlist);
2591 return DIRECTIVE_FOUND;
2594 case PP_ROTATE:
2595 if (tline->next && tline->next->type == TOK_WHITESPACE)
2596 tline = tline->next;
2597 if (tline->next == NULL) {
2598 free_tlist(origline);
2599 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2600 return DIRECTIVE_FOUND;
2602 t = expand_smacro(tline->next);
2603 tline->next = NULL;
2604 free_tlist(origline);
2605 tline = t;
2606 tptr = &t;
2607 tokval.t_type = TOKEN_INVALID;
2608 evalresult =
2609 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2610 free_tlist(tline);
2611 if (!evalresult)
2612 return DIRECTIVE_FOUND;
2613 if (tokval.t_type)
2614 error(ERR_WARNING|ERR_PASS1,
2615 "trailing garbage after expression ignored");
2616 if (!is_simple(evalresult)) {
2617 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2618 return DIRECTIVE_FOUND;
2620 mmac = istk->mstk;
2621 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2622 mmac = mmac->next_active;
2623 if (!mmac) {
2624 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2625 } else if (mmac->nparam == 0) {
2626 error(ERR_NONFATAL,
2627 "`%%rotate' invoked within macro without parameters");
2628 } else {
2629 int rotate = mmac->rotate + reloc_value(evalresult);
2631 rotate %= (int)mmac->nparam;
2632 if (rotate < 0)
2633 rotate += mmac->nparam;
2635 mmac->rotate = rotate;
2637 return DIRECTIVE_FOUND;
2639 case PP_REP:
2640 nolist = false;
2641 do {
2642 tline = tline->next;
2643 } while (tok_type_(tline, TOK_WHITESPACE));
2645 if (tok_type_(tline, TOK_ID) &&
2646 nasm_stricmp(tline->text, ".nolist") == 0) {
2647 nolist = true;
2648 do {
2649 tline = tline->next;
2650 } while (tok_type_(tline, TOK_WHITESPACE));
2653 if (tline) {
2654 t = expand_smacro(tline);
2655 tptr = &t;
2656 tokval.t_type = TOKEN_INVALID;
2657 evalresult =
2658 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2659 if (!evalresult) {
2660 free_tlist(origline);
2661 return DIRECTIVE_FOUND;
2663 if (tokval.t_type)
2664 error(ERR_WARNING|ERR_PASS1,
2665 "trailing garbage after expression ignored");
2666 if (!is_simple(evalresult)) {
2667 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2668 return DIRECTIVE_FOUND;
2670 count = reloc_value(evalresult) + 1;
2671 } else {
2672 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2673 count = 0;
2675 free_tlist(origline);
2677 tmp_defining = defining;
2678 defining = nasm_malloc(sizeof(MMacro));
2679 defining->name = NULL; /* flags this macro as a %rep block */
2680 defining->casesense = false;
2681 defining->plus = false;
2682 defining->nolist = nolist;
2683 defining->in_progress = count;
2684 defining->nparam_min = defining->nparam_max = 0;
2685 defining->defaults = NULL;
2686 defining->dlist = NULL;
2687 defining->expansion = NULL;
2688 defining->next_active = istk->mstk;
2689 defining->rep_nest = tmp_defining;
2690 return DIRECTIVE_FOUND;
2692 case PP_ENDREP:
2693 if (!defining || defining->name) {
2694 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2695 return DIRECTIVE_FOUND;
2699 * Now we have a "macro" defined - although it has no name
2700 * and we won't be entering it in the hash tables - we must
2701 * push a macro-end marker for it on to istk->expansion.
2702 * After that, it will take care of propagating itself (a
2703 * macro-end marker line for a macro which is really a %rep
2704 * block will cause the macro to be re-expanded, complete
2705 * with another macro-end marker to ensure the process
2706 * continues) until the whole expansion is forcibly removed
2707 * from istk->expansion by a %exitrep.
2709 l = nasm_malloc(sizeof(Line));
2710 l->next = istk->expansion;
2711 l->finishes = defining;
2712 l->first = NULL;
2713 istk->expansion = l;
2715 istk->mstk = defining;
2717 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2718 tmp_defining = defining;
2719 defining = defining->rep_nest;
2720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
2723 case PP_EXITREP:
2725 * We must search along istk->expansion until we hit a
2726 * macro-end marker for a macro with no name. Then we set
2727 * its `in_progress' flag to 0.
2729 for (l = istk->expansion; l; l = l->next)
2730 if (l->finishes && !l->finishes->name)
2731 break;
2733 if (l)
2734 l->finishes->in_progress = 1;
2735 else
2736 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
2740 case PP_XDEFINE:
2741 case PP_IXDEFINE:
2742 case PP_DEFINE:
2743 case PP_IDEFINE:
2744 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2746 tline = tline->next;
2747 skip_white_(tline);
2748 tline = expand_id(tline);
2749 if (!tline || (tline->type != TOK_ID &&
2750 (tline->type != TOK_PREPROC_ID ||
2751 tline->text[1] != '$'))) {
2752 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2753 pp_directives[i]);
2754 free_tlist(origline);
2755 return DIRECTIVE_FOUND;
2758 ctx = get_ctx(tline->text, false);
2760 mname = tline->text;
2761 last = tline;
2762 param_start = tline = tline->next;
2763 nparam = 0;
2765 /* Expand the macro definition now for %xdefine and %ixdefine */
2766 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2767 tline = expand_smacro(tline);
2769 if (tok_is_(tline, "(")) {
2771 * This macro has parameters.
2774 tline = tline->next;
2775 while (1) {
2776 skip_white_(tline);
2777 if (!tline) {
2778 error(ERR_NONFATAL, "parameter identifier expected");
2779 free_tlist(origline);
2780 return DIRECTIVE_FOUND;
2782 if (tline->type != TOK_ID) {
2783 error(ERR_NONFATAL,
2784 "`%s': parameter identifier expected",
2785 tline->text);
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2789 tline->type = TOK_SMAC_PARAM + nparam++;
2790 tline = tline->next;
2791 skip_white_(tline);
2792 if (tok_is_(tline, ",")) {
2793 tline = tline->next;
2794 } else {
2795 if (!tok_is_(tline, ")")) {
2796 error(ERR_NONFATAL,
2797 "`)' expected to terminate macro template");
2798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
2801 break;
2804 last = tline;
2805 tline = tline->next;
2807 if (tok_type_(tline, TOK_WHITESPACE))
2808 last = tline, tline = tline->next;
2809 macro_start = NULL;
2810 last->next = NULL;
2811 t = tline;
2812 while (t) {
2813 if (t->type == TOK_ID) {
2814 for (tt = param_start; tt; tt = tt->next)
2815 if (tt->type >= TOK_SMAC_PARAM &&
2816 !strcmp(tt->text, t->text))
2817 t->type = tt->type;
2819 tt = t->next;
2820 t->next = macro_start;
2821 macro_start = t;
2822 t = tt;
2825 * Good. We now have a macro name, a parameter count, and a
2826 * token list (in reverse order) for an expansion. We ought
2827 * to be OK just to create an SMacro, store it, and let
2828 * free_tlist have the rest of the line (which we have
2829 * carefully re-terminated after chopping off the expansion
2830 * from the end).
2832 define_smacro(ctx, mname, casesense, nparam, macro_start);
2833 free_tlist(origline);
2834 return DIRECTIVE_FOUND;
2836 case PP_UNDEF:
2837 tline = tline->next;
2838 skip_white_(tline);
2839 tline = expand_id(tline);
2840 if (!tline || (tline->type != TOK_ID &&
2841 (tline->type != TOK_PREPROC_ID ||
2842 tline->text[1] != '$'))) {
2843 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2844 free_tlist(origline);
2845 return DIRECTIVE_FOUND;
2847 if (tline->next) {
2848 error(ERR_WARNING|ERR_PASS1,
2849 "trailing garbage after macro name ignored");
2852 /* Find the context that symbol belongs to */
2853 ctx = get_ctx(tline->text, false);
2854 undef_smacro(ctx, tline->text);
2855 free_tlist(origline);
2856 return DIRECTIVE_FOUND;
2858 case PP_DEFSTR:
2859 case PP_IDEFSTR:
2860 casesense = (i == PP_DEFSTR);
2862 tline = tline->next;
2863 skip_white_(tline);
2864 tline = expand_id(tline);
2865 if (!tline || (tline->type != TOK_ID &&
2866 (tline->type != TOK_PREPROC_ID ||
2867 tline->text[1] != '$'))) {
2868 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2869 pp_directives[i]);
2870 free_tlist(origline);
2871 return DIRECTIVE_FOUND;
2874 ctx = get_ctx(tline->text, false);
2876 mname = tline->text;
2877 last = tline;
2878 tline = expand_smacro(tline->next);
2879 last->next = NULL;
2881 while (tok_type_(tline, TOK_WHITESPACE))
2882 tline = delete_Token(tline);
2884 p = detoken(tline, false);
2885 macro_start = nasm_malloc(sizeof(*macro_start));
2886 macro_start->next = NULL;
2887 macro_start->text = nasm_quote(p, strlen(p));
2888 macro_start->type = TOK_STRING;
2889 macro_start->a.mac = NULL;
2890 nasm_free(p);
2893 * We now have a macro name, an implicit parameter count of
2894 * zero, and a string token to use as an expansion. Create
2895 * and store an SMacro.
2897 define_smacro(ctx, mname, casesense, 0, macro_start);
2898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
2901 case PP_PATHSEARCH:
2903 FILE *fp;
2904 StrList *xsl = NULL;
2905 StrList **xst = &xsl;
2907 casesense = true;
2909 tline = tline->next;
2910 skip_white_(tline);
2911 tline = expand_id(tline);
2912 if (!tline || (tline->type != TOK_ID &&
2913 (tline->type != TOK_PREPROC_ID ||
2914 tline->text[1] != '$'))) {
2915 error(ERR_NONFATAL,
2916 "`%%pathsearch' expects a macro identifier as first parameter");
2917 free_tlist(origline);
2918 return DIRECTIVE_FOUND;
2920 ctx = get_ctx(tline->text, false);
2922 mname = tline->text;
2923 last = tline;
2924 tline = expand_smacro(tline->next);
2925 last->next = NULL;
2927 t = tline;
2928 while (tok_type_(t, TOK_WHITESPACE))
2929 t = t->next;
2931 if (!t || (t->type != TOK_STRING &&
2932 t->type != TOK_INTERNAL_STRING)) {
2933 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2934 free_tlist(tline);
2935 free_tlist(origline);
2936 return DIRECTIVE_FOUND; /* but we did _something_ */
2938 if (t->next)
2939 error(ERR_WARNING|ERR_PASS1,
2940 "trailing garbage after `%%pathsearch' ignored");
2941 p = t->text;
2942 if (t->type != TOK_INTERNAL_STRING)
2943 nasm_unquote(p, NULL);
2945 fp = inc_fopen(p, &xsl, &xst, true);
2946 if (fp) {
2947 p = xsl->str;
2948 fclose(fp); /* Don't actually care about the file */
2950 macro_start = nasm_malloc(sizeof(*macro_start));
2951 macro_start->next = NULL;
2952 macro_start->text = nasm_quote(p, strlen(p));
2953 macro_start->type = TOK_STRING;
2954 macro_start->a.mac = NULL;
2955 if (xsl)
2956 nasm_free(xsl);
2959 * We now have a macro name, an implicit parameter count of
2960 * zero, and a string token to use as an expansion. Create
2961 * and store an SMacro.
2963 define_smacro(ctx, mname, casesense, 0, macro_start);
2964 free_tlist(tline);
2965 free_tlist(origline);
2966 return DIRECTIVE_FOUND;
2969 case PP_STRLEN:
2970 casesense = true;
2972 tline = tline->next;
2973 skip_white_(tline);
2974 tline = expand_id(tline);
2975 if (!tline || (tline->type != TOK_ID &&
2976 (tline->type != TOK_PREPROC_ID ||
2977 tline->text[1] != '$'))) {
2978 error(ERR_NONFATAL,
2979 "`%%strlen' expects a macro identifier as first parameter");
2980 free_tlist(origline);
2981 return DIRECTIVE_FOUND;
2983 ctx = get_ctx(tline->text, false);
2985 mname = tline->text;
2986 last = tline;
2987 tline = expand_smacro(tline->next);
2988 last->next = NULL;
2990 t = tline;
2991 while (tok_type_(t, TOK_WHITESPACE))
2992 t = t->next;
2993 /* t should now point to the string */
2994 if (t->type != TOK_STRING) {
2995 error(ERR_NONFATAL,
2996 "`%%strlen` requires string as second parameter");
2997 free_tlist(tline);
2998 free_tlist(origline);
2999 return DIRECTIVE_FOUND;
3002 macro_start = nasm_malloc(sizeof(*macro_start));
3003 macro_start->next = NULL;
3004 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3005 macro_start->a.mac = NULL;
3008 * We now have a macro name, an implicit parameter count of
3009 * zero, and a numeric token to use as an expansion. Create
3010 * and store an SMacro.
3012 define_smacro(ctx, mname, casesense, 0, macro_start);
3013 free_tlist(tline);
3014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
3017 case PP_STRCAT:
3018 casesense = true;
3020 tline = tline->next;
3021 skip_white_(tline);
3022 tline = expand_id(tline);
3023 if (!tline || (tline->type != TOK_ID &&
3024 (tline->type != TOK_PREPROC_ID ||
3025 tline->text[1] != '$'))) {
3026 error(ERR_NONFATAL,
3027 "`%%strcat' expects a macro identifier as first parameter");
3028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
3031 ctx = get_ctx(tline->text, false);
3033 mname = tline->text;
3034 last = tline;
3035 tline = expand_smacro(tline->next);
3036 last->next = NULL;
3038 len = 0;
3039 for (t = tline; t; t = t->next) {
3040 switch (t->type) {
3041 case TOK_WHITESPACE:
3042 break;
3043 case TOK_STRING:
3044 len += t->a.len = nasm_unquote(t->text, NULL);
3045 break;
3046 case TOK_OTHER:
3047 if (!strcmp(t->text, ",")) /* permit comma separators */
3048 break;
3049 /* else fall through */
3050 default:
3051 error(ERR_NONFATAL,
3052 "non-string passed to `%%strcat' (%d)", t->type);
3053 free_tlist(tline);
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3059 p = pp = nasm_malloc(len);
3060 t = tline;
3061 for (t = tline; t; t = t->next) {
3062 if (t->type == TOK_STRING) {
3063 memcpy(p, t->text, t->a.len);
3064 p += t->a.len;
3069 * We now have a macro name, an implicit parameter count of
3070 * zero, and a numeric token to use as an expansion. Create
3071 * and store an SMacro.
3073 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3074 macro_start->text = nasm_quote(pp, len);
3075 nasm_free(pp);
3076 define_smacro(ctx, mname, casesense, 0, macro_start);
3077 free_tlist(tline);
3078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
3081 case PP_SUBSTR:
3083 int64_t a1, a2;
3084 size_t len;
3086 casesense = true;
3088 tline = tline->next;
3089 skip_white_(tline);
3090 tline = expand_id(tline);
3091 if (!tline || (tline->type != TOK_ID &&
3092 (tline->type != TOK_PREPROC_ID ||
3093 tline->text[1] != '$'))) {
3094 error(ERR_NONFATAL,
3095 "`%%substr' expects a macro identifier as first parameter");
3096 free_tlist(origline);
3097 return DIRECTIVE_FOUND;
3099 ctx = get_ctx(tline->text, false);
3101 mname = tline->text;
3102 last = tline;
3103 tline = expand_smacro(tline->next);
3104 last->next = NULL;
3106 t = tline->next;
3107 while (tok_type_(t, TOK_WHITESPACE))
3108 t = t->next;
3110 /* t should now point to the string */
3111 if (t->type != TOK_STRING) {
3112 error(ERR_NONFATAL,
3113 "`%%substr` requires string as second parameter");
3114 free_tlist(tline);
3115 free_tlist(origline);
3116 return DIRECTIVE_FOUND;
3119 tt = t->next;
3120 tptr = &tt;
3121 tokval.t_type = TOKEN_INVALID;
3122 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3123 pass, error, NULL);
3124 if (!evalresult) {
3125 free_tlist(tline);
3126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
3128 } else if (!is_simple(evalresult)) {
3129 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3130 free_tlist(tline);
3131 free_tlist(origline);
3132 return DIRECTIVE_FOUND;
3134 a1 = evalresult->value-1;
3136 while (tok_type_(tt, TOK_WHITESPACE))
3137 tt = tt->next;
3138 if (!tt) {
3139 a2 = 1; /* Backwards compatibility: one character */
3140 } else {
3141 tokval.t_type = TOKEN_INVALID;
3142 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3143 pass, error, NULL);
3144 if (!evalresult) {
3145 free_tlist(tline);
3146 free_tlist(origline);
3147 return DIRECTIVE_FOUND;
3148 } else if (!is_simple(evalresult)) {
3149 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3150 free_tlist(tline);
3151 free_tlist(origline);
3152 return DIRECTIVE_FOUND;
3154 a2 = evalresult->value;
3157 len = nasm_unquote(t->text, NULL);
3158 if (a2 < 0)
3159 a2 = a2+1+len-a1;
3160 if (a1+a2 > (int64_t)len)
3161 a2 = len-a1;
3163 macro_start = nasm_malloc(sizeof(*macro_start));
3164 macro_start->next = NULL;
3165 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3166 macro_start->type = TOK_STRING;
3167 macro_start->a.mac = NULL;
3170 * We now have a macro name, an implicit parameter count of
3171 * zero, and a numeric token to use as an expansion. Create
3172 * and store an SMacro.
3174 define_smacro(ctx, mname, casesense, 0, macro_start);
3175 free_tlist(tline);
3176 free_tlist(origline);
3177 return DIRECTIVE_FOUND;
3180 case PP_ASSIGN:
3181 case PP_IASSIGN:
3182 casesense = (i == PP_ASSIGN);
3184 tline = tline->next;
3185 skip_white_(tline);
3186 tline = expand_id(tline);
3187 if (!tline || (tline->type != TOK_ID &&
3188 (tline->type != TOK_PREPROC_ID ||
3189 tline->text[1] != '$'))) {
3190 error(ERR_NONFATAL,
3191 "`%%%sassign' expects a macro identifier",
3192 (i == PP_IASSIGN ? "i" : ""));
3193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
3196 ctx = get_ctx(tline->text, false);
3198 mname = tline->text;
3199 last = tline;
3200 tline = expand_smacro(tline->next);
3201 last->next = NULL;
3203 t = tline;
3204 tptr = &t;
3205 tokval.t_type = TOKEN_INVALID;
3206 evalresult =
3207 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3208 free_tlist(tline);
3209 if (!evalresult) {
3210 free_tlist(origline);
3211 return DIRECTIVE_FOUND;
3214 if (tokval.t_type)
3215 error(ERR_WARNING|ERR_PASS1,
3216 "trailing garbage after expression ignored");
3218 if (!is_simple(evalresult)) {
3219 error(ERR_NONFATAL,
3220 "non-constant value given to `%%%sassign'",
3221 (i == PP_IASSIGN ? "i" : ""));
3222 free_tlist(origline);
3223 return DIRECTIVE_FOUND;
3226 macro_start = nasm_malloc(sizeof(*macro_start));
3227 macro_start->next = NULL;
3228 make_tok_num(macro_start, reloc_value(evalresult));
3229 macro_start->a.mac = NULL;
3232 * We now have a macro name, an implicit parameter count of
3233 * zero, and a numeric token to use as an expansion. Create
3234 * and store an SMacro.
3236 define_smacro(ctx, mname, casesense, 0, macro_start);
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
3240 case PP_LINE:
3242 * Syntax is `%line nnn[+mmm] [filename]'
3244 tline = tline->next;
3245 skip_white_(tline);
3246 if (!tok_type_(tline, TOK_NUMBER)) {
3247 error(ERR_NONFATAL, "`%%line' expects line number");
3248 free_tlist(origline);
3249 return DIRECTIVE_FOUND;
3251 k = readnum(tline->text, &err);
3252 m = 1;
3253 tline = tline->next;
3254 if (tok_is_(tline, "+")) {
3255 tline = tline->next;
3256 if (!tok_type_(tline, TOK_NUMBER)) {
3257 error(ERR_NONFATAL, "`%%line' expects line increment");
3258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
3261 m = readnum(tline->text, &err);
3262 tline = tline->next;
3264 skip_white_(tline);
3265 src_set_linnum(k);
3266 istk->lineinc = m;
3267 if (tline) {
3268 nasm_free(src_set_fname(detoken(tline, false)));
3270 free_tlist(origline);
3271 return DIRECTIVE_FOUND;
3273 default:
3274 error(ERR_FATAL,
3275 "preprocessor directive `%s' not yet implemented",
3276 pp_directives[i]);
3277 return DIRECTIVE_FOUND;
3282 * Ensure that a macro parameter contains a condition code and
3283 * nothing else. Return the condition code index if so, or -1
3284 * otherwise.
3286 static int find_cc(Token * t)
3288 Token *tt;
3289 int i, j, k, m;
3291 if (!t)
3292 return -1; /* Probably a %+ without a space */
3294 skip_white_(t);
3295 if (t->type != TOK_ID)
3296 return -1;
3297 tt = t->next;
3298 skip_white_(tt);
3299 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3300 return -1;
3302 i = -1;
3303 j = elements(conditions);
3304 while (j - i > 1) {
3305 k = (j + i) / 2;
3306 m = nasm_stricmp(t->text, conditions[k]);
3307 if (m == 0) {
3308 i = k;
3309 j = -2;
3310 break;
3311 } else if (m < 0) {
3312 j = k;
3313 } else
3314 i = k;
3316 if (j != -2)
3317 return -1;
3318 return i;
3322 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3323 * %-n) and MMacro-local identifiers (%%foo) as well as
3324 * macro indirection (%[...]).
3326 static Token *expand_mmac_params(Token * tline)
3328 Token *t, *tt, **tail, *thead;
3330 tail = &thead;
3331 thead = NULL;
3333 while (tline) {
3334 if (tline->type == TOK_PREPROC_ID &&
3335 (((tline->text[1] == '+' || tline->text[1] == '-')
3336 && tline->text[2]) || tline->text[1] == '%'
3337 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3338 char *text = NULL;
3339 int type = 0, cc; /* type = 0 to placate optimisers */
3340 char tmpbuf[30];
3341 unsigned int n;
3342 int i;
3343 MMacro *mac;
3345 t = tline;
3346 tline = tline->next;
3348 mac = istk->mstk;
3349 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3350 mac = mac->next_active;
3351 if (!mac)
3352 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3353 else
3354 switch (t->text[1]) {
3356 * We have to make a substitution of one of the
3357 * forms %1, %-1, %+1, %%foo, %0.
3359 case '0':
3360 type = TOK_NUMBER;
3361 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3362 text = nasm_strdup(tmpbuf);
3363 break;
3364 case '%':
3365 type = TOK_ID;
3366 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3367 mac->unique);
3368 text = nasm_strcat(tmpbuf, t->text + 2);
3369 break;
3370 case '-':
3371 n = atoi(t->text + 2) - 1;
3372 if (n >= mac->nparam)
3373 tt = NULL;
3374 else {
3375 if (mac->nparam > 1)
3376 n = (n + mac->rotate) % mac->nparam;
3377 tt = mac->params[n];
3379 cc = find_cc(tt);
3380 if (cc == -1) {
3381 error(ERR_NONFATAL,
3382 "macro parameter %d is not a condition code",
3383 n + 1);
3384 text = NULL;
3385 } else {
3386 type = TOK_ID;
3387 if (inverse_ccs[cc] == -1) {
3388 error(ERR_NONFATAL,
3389 "condition code `%s' is not invertible",
3390 conditions[cc]);
3391 text = NULL;
3392 } else
3393 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3395 break;
3396 case '+':
3397 n = atoi(t->text + 2) - 1;
3398 if (n >= mac->nparam)
3399 tt = NULL;
3400 else {
3401 if (mac->nparam > 1)
3402 n = (n + mac->rotate) % mac->nparam;
3403 tt = mac->params[n];
3405 cc = find_cc(tt);
3406 if (cc == -1) {
3407 error(ERR_NONFATAL,
3408 "macro parameter %d is not a condition code",
3409 n + 1);
3410 text = NULL;
3411 } else {
3412 type = TOK_ID;
3413 text = nasm_strdup(conditions[cc]);
3415 break;
3416 default:
3417 n = atoi(t->text + 1) - 1;
3418 if (n >= mac->nparam)
3419 tt = NULL;
3420 else {
3421 if (mac->nparam > 1)
3422 n = (n + mac->rotate) % mac->nparam;
3423 tt = mac->params[n];
3425 if (tt) {
3426 for (i = 0; i < mac->paramlen[n]; i++) {
3427 *tail = new_Token(NULL, tt->type, tt->text, 0);
3428 tail = &(*tail)->next;
3429 tt = tt->next;
3432 text = NULL; /* we've done it here */
3433 break;
3435 if (!text) {
3436 delete_Token(t);
3437 } else {
3438 *tail = t;
3439 tail = &t->next;
3440 t->type = type;
3441 nasm_free(t->text);
3442 t->text = text;
3443 t->a.mac = NULL;
3445 continue;
3446 } else if (tline->type == TOK_INDIRECT) {
3447 t = tline;
3448 tline = tline->next;
3449 tt = tokenize(t->text);
3450 tt = expand_mmac_params(tt);
3451 tt = expand_smacro(tt);
3452 *tail = tt;
3453 while (tt) {
3454 tt->a.mac = NULL; /* Necessary? */
3455 tail = &tt->next;
3456 tt = tt->next;
3458 delete_Token(t);
3459 } else {
3460 t = *tail = tline;
3461 tline = tline->next;
3462 t->a.mac = NULL;
3463 tail = &t->next;
3466 *tail = NULL;
3468 /* Now handle token pasting... */
3469 t = thead;
3470 while (t && (tt = t->next)) {
3471 switch (t->type) {
3472 case TOK_WHITESPACE:
3473 if (tt->type == TOK_WHITESPACE) {
3474 t->next = delete_Token(tt);
3475 } else {
3476 t = tt;
3478 break;
3479 case TOK_ID:
3480 case TOK_NUMBER:
3481 if (tt->type == t->type || tt->type == TOK_NUMBER) {
3482 char *tmp = nasm_strcat(t->text, tt->text);
3483 nasm_free(t->text);
3484 t->text = tmp;
3485 t->next = delete_Token(tt);
3486 } else {
3487 t = tt;
3489 break;
3490 default:
3491 t = tt;
3492 break;
3495 return thead;
3499 * Expand all single-line macro calls made in the given line.
3500 * Return the expanded version of the line. The original is deemed
3501 * to be destroyed in the process. (In reality we'll just move
3502 * Tokens from input to output a lot of the time, rather than
3503 * actually bothering to destroy and replicate.)
3505 #define DEADMAN_LIMIT (1 << 20)
3507 static Token *expand_smacro(Token * tline)
3509 Token *t, *tt, *mstart, **tail, *thead;
3510 struct hash_table *smtbl;
3511 SMacro *head = NULL, *m;
3512 Token **params;
3513 int *paramsize;
3514 unsigned int nparam, sparam;
3515 int brackets, rescan;
3516 Token *org_tline = tline;
3517 Context *ctx;
3518 char *mname;
3519 int deadman = DEADMAN_LIMIT;
3522 * Trick: we should avoid changing the start token pointer since it can
3523 * be contained in "next" field of other token. Because of this
3524 * we allocate a copy of first token and work with it; at the end of
3525 * routine we copy it back
3527 if (org_tline) {
3528 tline =
3529 new_Token(org_tline->next, org_tline->type, org_tline->text,
3531 tline->a.mac = org_tline->a.mac;
3532 nasm_free(org_tline->text);
3533 org_tline->text = NULL;
3536 again:
3537 tail = &thead;
3538 thead = NULL;
3540 while (tline) { /* main token loop */
3541 if (!--deadman) {
3542 error(ERR_NONFATAL, "interminable macro recursion");
3543 break;
3546 if ((mname = tline->text)) {
3547 /* if this token is a local macro, look in local context */
3548 ctx = NULL;
3549 smtbl = &smacros;
3550 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3551 ctx = get_ctx(mname, true);
3552 if (ctx)
3553 smtbl = &ctx->localmac;
3555 head = (SMacro *) hash_findix(smtbl, mname);
3558 * We've hit an identifier. As in is_mmacro below, we first
3559 * check whether the identifier is a single-line macro at
3560 * all, then think about checking for parameters if
3561 * necessary.
3563 for (m = head; m; m = m->next)
3564 if (!mstrcmp(m->name, mname, m->casesense))
3565 break;
3566 if (m) {
3567 mstart = tline;
3568 params = NULL;
3569 paramsize = NULL;
3570 if (m->nparam == 0) {
3572 * Simple case: the macro is parameterless. Discard the
3573 * one token that the macro call took, and push the
3574 * expansion back on the to-do stack.
3576 if (!m->expansion) {
3577 if (!strcmp("__FILE__", m->name)) {
3578 int32_t num = 0;
3579 char *file = NULL;
3580 src_get(&num, &file);
3581 tline->text = nasm_quote(file, strlen(file));
3582 tline->type = TOK_STRING;
3583 nasm_free(file);
3584 continue;
3586 if (!strcmp("__LINE__", m->name)) {
3587 nasm_free(tline->text);
3588 make_tok_num(tline, src_get_linnum());
3589 continue;
3591 if (!strcmp("__BITS__", m->name)) {
3592 nasm_free(tline->text);
3593 make_tok_num(tline, globalbits);
3594 continue;
3596 tline = delete_Token(tline);
3597 continue;
3599 } else {
3601 * Complicated case: at least one macro with this name
3602 * exists and takes parameters. We must find the
3603 * parameters in the call, count them, find the SMacro
3604 * that corresponds to that form of the macro call, and
3605 * substitute for the parameters when we expand. What a
3606 * pain.
3608 /*tline = tline->next;
3609 skip_white_(tline); */
3610 do {
3611 t = tline->next;
3612 while (tok_type_(t, TOK_SMAC_END)) {
3613 t->a.mac->in_progress = false;
3614 t->text = NULL;
3615 t = tline->next = delete_Token(t);
3617 tline = t;
3618 } while (tok_type_(tline, TOK_WHITESPACE));
3619 if (!tok_is_(tline, "(")) {
3621 * This macro wasn't called with parameters: ignore
3622 * the call. (Behaviour borrowed from gnu cpp.)
3624 tline = mstart;
3625 m = NULL;
3626 } else {
3627 int paren = 0;
3628 int white = 0;
3629 brackets = 0;
3630 nparam = 0;
3631 sparam = PARAM_DELTA;
3632 params = nasm_malloc(sparam * sizeof(Token *));
3633 params[0] = tline->next;
3634 paramsize = nasm_malloc(sparam * sizeof(int));
3635 paramsize[0] = 0;
3636 while (true) { /* parameter loop */
3638 * For some unusual expansions
3639 * which concatenates function call
3641 t = tline->next;
3642 while (tok_type_(t, TOK_SMAC_END)) {
3643 t->a.mac->in_progress = false;
3644 t->text = NULL;
3645 t = tline->next = delete_Token(t);
3647 tline = t;
3649 if (!tline) {
3650 error(ERR_NONFATAL,
3651 "macro call expects terminating `)'");
3652 break;
3654 if (tline->type == TOK_WHITESPACE
3655 && brackets <= 0) {
3656 if (paramsize[nparam])
3657 white++;
3658 else
3659 params[nparam] = tline->next;
3660 continue; /* parameter loop */
3662 if (tline->type == TOK_OTHER
3663 && tline->text[1] == 0) {
3664 char ch = tline->text[0];
3665 if (ch == ',' && !paren && brackets <= 0) {
3666 if (++nparam >= sparam) {
3667 sparam += PARAM_DELTA;
3668 params = nasm_realloc(params,
3669 sparam *
3670 sizeof(Token
3671 *));
3672 paramsize =
3673 nasm_realloc(paramsize,
3674 sparam *
3675 sizeof(int));
3677 params[nparam] = tline->next;
3678 paramsize[nparam] = 0;
3679 white = 0;
3680 continue; /* parameter loop */
3682 if (ch == '{' &&
3683 (brackets > 0 || (brackets == 0 &&
3684 !paramsize[nparam])))
3686 if (!(brackets++)) {
3687 params[nparam] = tline->next;
3688 continue; /* parameter loop */
3691 if (ch == '}' && brackets > 0)
3692 if (--brackets == 0) {
3693 brackets = -1;
3694 continue; /* parameter loop */
3696 if (ch == '(' && !brackets)
3697 paren++;
3698 if (ch == ')' && brackets <= 0)
3699 if (--paren < 0)
3700 break;
3702 if (brackets < 0) {
3703 brackets = 0;
3704 error(ERR_NONFATAL, "braces do not "
3705 "enclose all of macro parameter");
3707 paramsize[nparam] += white + 1;
3708 white = 0;
3709 } /* parameter loop */
3710 nparam++;
3711 while (m && (m->nparam != nparam ||
3712 mstrcmp(m->name, mname,
3713 m->casesense)))
3714 m = m->next;
3715 if (!m)
3716 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
3717 "macro `%s' exists, "
3718 "but not taking %d parameters",
3719 mstart->text, nparam);
3722 if (m && m->in_progress)
3723 m = NULL;
3724 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3726 * Design question: should we handle !tline, which
3727 * indicates missing ')' here, or expand those
3728 * macros anyway, which requires the (t) test a few
3729 * lines down?
3731 nasm_free(params);
3732 nasm_free(paramsize);
3733 tline = mstart;
3734 } else {
3736 * Expand the macro: we are placed on the last token of the
3737 * call, so that we can easily split the call from the
3738 * following tokens. We also start by pushing an SMAC_END
3739 * token for the cycle removal.
3741 t = tline;
3742 if (t) {
3743 tline = t->next;
3744 t->next = NULL;
3746 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3747 tt->a.mac = m;
3748 m->in_progress = true;
3749 tline = tt;
3750 for (t = m->expansion; t; t = t->next) {
3751 if (t->type >= TOK_SMAC_PARAM) {
3752 Token *pcopy = tline, **ptail = &pcopy;
3753 Token *ttt, *pt;
3754 int i;
3756 ttt = params[t->type - TOK_SMAC_PARAM];
3757 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3758 --i >= 0;) {
3759 pt = *ptail =
3760 new_Token(tline, ttt->type, ttt->text,
3762 ptail = &pt->next;
3763 ttt = ttt->next;
3765 tline = pcopy;
3766 } else if (t->type == TOK_PREPROC_Q) {
3767 tt = new_Token(tline, TOK_ID, mname, 0);
3768 tline = tt;
3769 } else if (t->type == TOK_PREPROC_QQ) {
3770 tt = new_Token(tline, TOK_ID, m->name, 0);
3771 tline = tt;
3772 } else {
3773 tt = new_Token(tline, t->type, t->text, 0);
3774 tline = tt;
3779 * Having done that, get rid of the macro call, and clean
3780 * up the parameters.
3782 nasm_free(params);
3783 nasm_free(paramsize);
3784 free_tlist(mstart);
3785 continue; /* main token loop */
3790 if (tline->type == TOK_SMAC_END) {
3791 tline->a.mac->in_progress = false;
3792 tline = delete_Token(tline);
3793 } else {
3794 t = *tail = tline;
3795 tline = tline->next;
3796 t->a.mac = NULL;
3797 t->next = NULL;
3798 tail = &t->next;
3803 * Now scan the entire line and look for successive TOK_IDs that resulted
3804 * after expansion (they can't be produced by tokenize()). The successive
3805 * TOK_IDs should be concatenated.
3806 * Also we look for %+ tokens and concatenate the tokens before and after
3807 * them (without white spaces in between).
3809 t = thead;
3810 rescan = 0;
3811 while (t) {
3812 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3813 t = t->next;
3814 if (!t || !t->next)
3815 break;
3816 if (t->next->type == TOK_ID ||
3817 t->next->type == TOK_PREPROC_ID ||
3818 t->next->type == TOK_NUMBER) {
3819 char *p = nasm_strcat(t->text, t->next->text);
3820 nasm_free(t->text);
3821 t->next = delete_Token(t->next);
3822 t->text = p;
3823 rescan = 1;
3824 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3825 t->next->next->type == TOK_PREPROC_ID &&
3826 strcmp(t->next->next->text, "%+") == 0) {
3827 /* free the next whitespace, the %+ token and next whitespace */
3828 int i;
3829 for (i = 1; i <= 3; i++) {
3830 if (!t->next
3831 || (i != 2 && t->next->type != TOK_WHITESPACE))
3832 break;
3833 t->next = delete_Token(t->next);
3834 } /* endfor */
3835 } else
3836 t = t->next;
3838 /* If we concatenaded something, re-scan the line for macros */
3839 if (rescan) {
3840 tline = thead;
3841 goto again;
3844 if (org_tline) {
3845 if (thead) {
3846 *org_tline = *thead;
3847 /* since we just gave text to org_line, don't free it */
3848 thead->text = NULL;
3849 delete_Token(thead);
3850 } else {
3851 /* the expression expanded to empty line;
3852 we can't return NULL for some reasons
3853 we just set the line to a single WHITESPACE token. */
3854 memset(org_tline, 0, sizeof(*org_tline));
3855 org_tline->text = NULL;
3856 org_tline->type = TOK_WHITESPACE;
3858 thead = org_tline;
3861 return thead;
3865 * Similar to expand_smacro but used exclusively with macro identifiers
3866 * right before they are fetched in. The reason is that there can be
3867 * identifiers consisting of several subparts. We consider that if there
3868 * are more than one element forming the name, user wants a expansion,
3869 * otherwise it will be left as-is. Example:
3871 * %define %$abc cde
3873 * the identifier %$abc will be left as-is so that the handler for %define
3874 * will suck it and define the corresponding value. Other case:
3876 * %define _%$abc cde
3878 * In this case user wants name to be expanded *before* %define starts
3879 * working, so we'll expand %$abc into something (if it has a value;
3880 * otherwise it will be left as-is) then concatenate all successive
3881 * PP_IDs into one.
3883 static Token *expand_id(Token * tline)
3885 Token *cur, *oldnext = NULL;
3887 if (!tline || !tline->next)
3888 return tline;
3890 cur = tline;
3891 while (cur->next &&
3892 (cur->next->type == TOK_ID ||
3893 cur->next->type == TOK_PREPROC_ID
3894 || cur->next->type == TOK_NUMBER))
3895 cur = cur->next;
3897 /* If identifier consists of just one token, don't expand */
3898 if (cur == tline)
3899 return tline;
3901 if (cur) {
3902 oldnext = cur->next; /* Detach the tail past identifier */
3903 cur->next = NULL; /* so that expand_smacro stops here */
3906 tline = expand_smacro(tline);
3908 if (cur) {
3909 /* expand_smacro possibly changhed tline; re-scan for EOL */
3910 cur = tline;
3911 while (cur && cur->next)
3912 cur = cur->next;
3913 if (cur)
3914 cur->next = oldnext;
3917 return tline;
3921 * Determine whether the given line constitutes a multi-line macro
3922 * call, and return the MMacro structure called if so. Doesn't have
3923 * to check for an initial label - that's taken care of in
3924 * expand_mmacro - but must check numbers of parameters. Guaranteed
3925 * to be called with tline->type == TOK_ID, so the putative macro
3926 * name is easy to find.
3928 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3930 MMacro *head, *m;
3931 Token **params;
3932 int nparam;
3934 head = (MMacro *) hash_findix(&mmacros, tline->text);
3937 * Efficiency: first we see if any macro exists with the given
3938 * name. If not, we can return NULL immediately. _Then_ we
3939 * count the parameters, and then we look further along the
3940 * list if necessary to find the proper MMacro.
3942 for (m = head; m; m = m->next)
3943 if (!mstrcmp(m->name, tline->text, m->casesense))
3944 break;
3945 if (!m)
3946 return NULL;
3949 * OK, we have a potential macro. Count and demarcate the
3950 * parameters.
3952 count_mmac_params(tline->next, &nparam, &params);
3955 * So we know how many parameters we've got. Find the MMacro
3956 * structure that handles this number.
3958 while (m) {
3959 if (m->nparam_min <= nparam
3960 && (m->plus || nparam <= m->nparam_max)) {
3962 * This one is right. Just check if cycle removal
3963 * prohibits us using it before we actually celebrate...
3965 if (m->in_progress) {
3966 #if 0
3967 error(ERR_NONFATAL,
3968 "self-reference in multi-line macro `%s'", m->name);
3969 #endif
3970 nasm_free(params);
3971 return NULL;
3974 * It's right, and we can use it. Add its default
3975 * parameters to the end of our list if necessary.
3977 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3978 params =
3979 nasm_realloc(params,
3980 ((m->nparam_min + m->ndefs +
3981 1) * sizeof(*params)));
3982 while (nparam < m->nparam_min + m->ndefs) {
3983 params[nparam] = m->defaults[nparam - m->nparam_min];
3984 nparam++;
3988 * If we've gone over the maximum parameter count (and
3989 * we're in Plus mode), ignore parameters beyond
3990 * nparam_max.
3992 if (m->plus && nparam > m->nparam_max)
3993 nparam = m->nparam_max;
3995 * Then terminate the parameter list, and leave.
3997 if (!params) { /* need this special case */
3998 params = nasm_malloc(sizeof(*params));
3999 nparam = 0;
4001 params[nparam] = NULL;
4002 *params_array = params;
4003 return m;
4006 * This one wasn't right: look for the next one with the
4007 * same name.
4009 for (m = m->next; m; m = m->next)
4010 if (!mstrcmp(m->name, tline->text, m->casesense))
4011 break;
4015 * After all that, we didn't find one with the right number of
4016 * parameters. Issue a warning, and fail to expand the macro.
4018 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4019 "macro `%s' exists, but not taking %d parameters",
4020 tline->text, nparam);
4021 nasm_free(params);
4022 return NULL;
4026 * Expand the multi-line macro call made by the given line, if
4027 * there is one to be expanded. If there is, push the expansion on
4028 * istk->expansion and return 1. Otherwise return 0.
4030 static int expand_mmacro(Token * tline)
4032 Token *startline = tline;
4033 Token *label = NULL;
4034 int dont_prepend = 0;
4035 Token **params, *t, *mtok, *tt;
4036 MMacro *m;
4037 Line *l, *ll;
4038 int i, nparam, *paramlen;
4039 const char *mname;
4041 t = tline;
4042 skip_white_(t);
4043 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4044 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4045 return 0;
4046 mtok = t;
4047 m = is_mmacro(t, &params);
4048 if (m) {
4049 mname = t->text;
4050 } else {
4051 Token *last;
4053 * We have an id which isn't a macro call. We'll assume
4054 * it might be a label; we'll also check to see if a
4055 * colon follows it. Then, if there's another id after
4056 * that lot, we'll check it again for macro-hood.
4058 label = last = t;
4059 t = t->next;
4060 if (tok_type_(t, TOK_WHITESPACE))
4061 last = t, t = t->next;
4062 if (tok_is_(t, ":")) {
4063 dont_prepend = 1;
4064 last = t, t = t->next;
4065 if (tok_type_(t, TOK_WHITESPACE))
4066 last = t, t = t->next;
4068 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4069 return 0;
4070 last->next = NULL;
4071 mname = t->text;
4072 tline = t;
4076 * Fix up the parameters: this involves stripping leading and
4077 * trailing whitespace, then stripping braces if they are
4078 * present.
4080 for (nparam = 0; params[nparam]; nparam++) ;
4081 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4083 for (i = 0; params[i]; i++) {
4084 int brace = false;
4085 int comma = (!m->plus || i < nparam - 1);
4087 t = params[i];
4088 skip_white_(t);
4089 if (tok_is_(t, "{"))
4090 t = t->next, brace = true, comma = false;
4091 params[i] = t;
4092 paramlen[i] = 0;
4093 while (t) {
4094 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4095 break; /* ... because we have hit a comma */
4096 if (comma && t->type == TOK_WHITESPACE
4097 && tok_is_(t->next, ","))
4098 break; /* ... or a space then a comma */
4099 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4100 break; /* ... or a brace */
4101 t = t->next;
4102 paramlen[i]++;
4107 * OK, we have a MMacro structure together with a set of
4108 * parameters. We must now go through the expansion and push
4109 * copies of each Line on to istk->expansion. Substitution of
4110 * parameter tokens and macro-local tokens doesn't get done
4111 * until the single-line macro substitution process; this is
4112 * because delaying them allows us to change the semantics
4113 * later through %rotate.
4115 * First, push an end marker on to istk->expansion, mark this
4116 * macro as in progress, and set up its invocation-specific
4117 * variables.
4119 ll = nasm_malloc(sizeof(Line));
4120 ll->next = istk->expansion;
4121 ll->finishes = m;
4122 ll->first = NULL;
4123 istk->expansion = ll;
4125 m->in_progress = true;
4126 m->params = params;
4127 m->iline = tline;
4128 m->nparam = nparam;
4129 m->rotate = 0;
4130 m->paramlen = paramlen;
4131 m->unique = unique++;
4132 m->lineno = 0;
4134 m->next_active = istk->mstk;
4135 istk->mstk = m;
4137 for (l = m->expansion; l; l = l->next) {
4138 Token **tail;
4140 ll = nasm_malloc(sizeof(Line));
4141 ll->finishes = NULL;
4142 ll->next = istk->expansion;
4143 istk->expansion = ll;
4144 tail = &ll->first;
4146 for (t = l->first; t; t = t->next) {
4147 Token *x = t;
4148 switch (t->type) {
4149 case TOK_PREPROC_Q:
4150 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4151 break;
4152 case TOK_PREPROC_QQ:
4153 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4154 break;
4155 case TOK_PREPROC_ID:
4156 if (t->text[1] == '0' && t->text[2] == '0') {
4157 dont_prepend = -1;
4158 x = label;
4159 if (!x)
4160 continue;
4162 /* fall through */
4163 default:
4164 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4165 break;
4167 tail = &tt->next;
4169 *tail = NULL;
4173 * If we had a label, push it on as the first line of
4174 * the macro expansion.
4176 if (label) {
4177 if (dont_prepend < 0)
4178 free_tlist(startline);
4179 else {
4180 ll = nasm_malloc(sizeof(Line));
4181 ll->finishes = NULL;
4182 ll->next = istk->expansion;
4183 istk->expansion = ll;
4184 ll->first = startline;
4185 if (!dont_prepend) {
4186 while (label->next)
4187 label = label->next;
4188 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4193 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4195 return 1;
4198 /* The function that actually does the error reporting */
4199 static void verror(int severity, const char *fmt, va_list arg)
4201 char buff[1024];
4203 vsnprintf(buff, sizeof(buff), fmt, arg);
4205 if (istk && istk->mstk && istk->mstk->name)
4206 _error(severity, "(%s:%d) %s", istk->mstk->name,
4207 istk->mstk->lineno, buff);
4208 else
4209 _error(severity, "%s", buff);
4213 * Since preprocessor always operate only on the line that didn't
4214 * arrived yet, we should always use ERR_OFFBY1.
4216 static void error(int severity, const char *fmt, ...)
4218 va_list arg;
4220 /* If we're in a dead branch of IF or something like it, ignore the error */
4221 if (istk && istk->conds && !emitting(istk->conds->state))
4222 return;
4224 va_start(arg, fmt);
4225 verror(severity, fmt, arg);
4226 va_end(arg);
4230 * Because %else etc are evaluated in the state context
4231 * of the previous branch, errors might get lost with error():
4232 * %if 0 ... %else trailing garbage ... %endif
4233 * So %else etc should report errors with this function.
4235 static void error_precond(int severity, const char *fmt, ...)
4237 va_list arg;
4239 /* Only ignore the error if it's really in a dead branch */
4240 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4241 return;
4243 va_start(arg, fmt);
4244 verror(severity, fmt, arg);
4245 va_end(arg);
4248 static void
4249 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4250 ListGen * listgen, StrList **deplist)
4252 Token *t;
4254 _error = errfunc;
4255 cstk = NULL;
4256 istk = nasm_malloc(sizeof(Include));
4257 istk->next = NULL;
4258 istk->conds = NULL;
4259 istk->expansion = NULL;
4260 istk->mstk = NULL;
4261 istk->fp = fopen(file, "r");
4262 istk->fname = NULL;
4263 src_set_fname(nasm_strdup(file));
4264 src_set_linnum(0);
4265 istk->lineinc = 1;
4266 if (!istk->fp)
4267 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4268 file);
4269 defining = NULL;
4270 nested_mac_count = 0;
4271 nested_rep_count = 0;
4272 init_macros();
4273 unique = 0;
4274 if (tasm_compatible_mode) {
4275 stdmacpos = nasm_stdmac;
4276 } else {
4277 stdmacpos = nasm_stdmac_after_tasm;
4279 any_extrastdmac = extrastdmac && *extrastdmac;
4280 do_predef = true;
4281 list = listgen;
4282 evaluate = eval;
4285 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4286 * The caller, however, will also pass in 3 for preprocess-only so
4287 * we can set __PASS__ accordingly.
4289 pass = apass > 2 ? 2 : apass;
4291 dephead = deptail = deplist;
4292 if (deplist) {
4293 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4294 sl->next = NULL;
4295 strcpy(sl->str, file);
4296 *deptail = sl;
4297 deptail = &sl->next;
4301 * Define the __PASS__ macro. This is defined here unlike
4302 * all the other builtins, because it is special -- it varies between
4303 * passes.
4305 t = nasm_malloc(sizeof(*t));
4306 t->next = NULL;
4307 make_tok_num(t, apass);
4308 t->a.mac = NULL;
4309 define_smacro(NULL, "__PASS__", true, 0, t);
4312 static char *pp_getline(void)
4314 char *line;
4315 Token *tline;
4317 while (1) {
4319 * Fetch a tokenized line, either from the macro-expansion
4320 * buffer or from the input file.
4322 tline = NULL;
4323 while (istk->expansion && istk->expansion->finishes) {
4324 Line *l = istk->expansion;
4325 if (!l->finishes->name && l->finishes->in_progress > 1) {
4326 Line *ll;
4329 * This is a macro-end marker for a macro with no
4330 * name, which means it's not really a macro at all
4331 * but a %rep block, and the `in_progress' field is
4332 * more than 1, meaning that we still need to
4333 * repeat. (1 means the natural last repetition; 0
4334 * means termination by %exitrep.) We have
4335 * therefore expanded up to the %endrep, and must
4336 * push the whole block on to the expansion buffer
4337 * again. We don't bother to remove the macro-end
4338 * marker: we'd only have to generate another one
4339 * if we did.
4341 l->finishes->in_progress--;
4342 for (l = l->finishes->expansion; l; l = l->next) {
4343 Token *t, *tt, **tail;
4345 ll = nasm_malloc(sizeof(Line));
4346 ll->next = istk->expansion;
4347 ll->finishes = NULL;
4348 ll->first = NULL;
4349 tail = &ll->first;
4351 for (t = l->first; t; t = t->next) {
4352 if (t->text || t->type == TOK_WHITESPACE) {
4353 tt = *tail =
4354 new_Token(NULL, t->type, t->text, 0);
4355 tail = &tt->next;
4359 istk->expansion = ll;
4361 } else {
4363 * Check whether a `%rep' was started and not ended
4364 * within this macro expansion. This can happen and
4365 * should be detected. It's a fatal error because
4366 * I'm too confused to work out how to recover
4367 * sensibly from it.
4369 if (defining) {
4370 if (defining->name)
4371 error(ERR_PANIC,
4372 "defining with name in expansion");
4373 else if (istk->mstk->name)
4374 error(ERR_FATAL,
4375 "`%%rep' without `%%endrep' within"
4376 " expansion of macro `%s'",
4377 istk->mstk->name);
4381 * FIXME: investigate the relationship at this point between
4382 * istk->mstk and l->finishes
4385 MMacro *m = istk->mstk;
4386 istk->mstk = m->next_active;
4387 if (m->name) {
4389 * This was a real macro call, not a %rep, and
4390 * therefore the parameter information needs to
4391 * be freed.
4393 nasm_free(m->params);
4394 free_tlist(m->iline);
4395 nasm_free(m->paramlen);
4396 l->finishes->in_progress = false;
4397 } else
4398 free_mmacro(m);
4400 istk->expansion = l->next;
4401 nasm_free(l);
4402 list->downlevel(LIST_MACRO);
4405 while (1) { /* until we get a line we can use */
4407 if (istk->expansion) { /* from a macro expansion */
4408 char *p;
4409 Line *l = istk->expansion;
4410 if (istk->mstk)
4411 istk->mstk->lineno++;
4412 tline = l->first;
4413 istk->expansion = l->next;
4414 nasm_free(l);
4415 p = detoken(tline, false);
4416 list->line(LIST_MACRO, p);
4417 nasm_free(p);
4418 break;
4420 line = read_line();
4421 if (line) { /* from the current input file */
4422 line = prepreproc(line);
4423 tline = tokenize(line);
4424 nasm_free(line);
4425 break;
4428 * The current file has ended; work down the istk
4431 Include *i = istk;
4432 fclose(i->fp);
4433 if (i->conds)
4434 error(ERR_FATAL,
4435 "expected `%%endif' before end of file");
4436 /* only set line and file name if there's a next node */
4437 if (i->next) {
4438 src_set_linnum(i->lineno);
4439 nasm_free(src_set_fname(i->fname));
4441 istk = i->next;
4442 list->downlevel(LIST_INCLUDE);
4443 nasm_free(i);
4444 if (!istk)
4445 return NULL;
4446 if (istk->expansion && istk->expansion->finishes)
4447 break;
4452 * We must expand MMacro parameters and MMacro-local labels
4453 * _before_ we plunge into directive processing, to cope
4454 * with things like `%define something %1' such as STRUC
4455 * uses. Unless we're _defining_ a MMacro, in which case
4456 * those tokens should be left alone to go into the
4457 * definition; and unless we're in a non-emitting
4458 * condition, in which case we don't want to meddle with
4459 * anything.
4461 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4462 && !(istk->mstk && !istk->mstk->in_progress)) {
4463 tline = expand_mmac_params(tline);
4467 * Check the line to see if it's a preprocessor directive.
4469 if (do_directive(tline) == DIRECTIVE_FOUND) {
4470 continue;
4471 } else if (defining) {
4473 * We're defining a multi-line macro. We emit nothing
4474 * at all, and just
4475 * shove the tokenized line on to the macro definition.
4477 Line *l = nasm_malloc(sizeof(Line));
4478 l->next = defining->expansion;
4479 l->first = tline;
4480 l->finishes = NULL;
4481 defining->expansion = l;
4482 continue;
4483 } else if (istk->conds && !emitting(istk->conds->state)) {
4485 * We're in a non-emitting branch of a condition block.
4486 * Emit nothing at all, not even a blank line: when we
4487 * emerge from the condition we'll give a line-number
4488 * directive so we keep our place correctly.
4490 free_tlist(tline);
4491 continue;
4492 } else if (istk->mstk && !istk->mstk->in_progress) {
4494 * We're in a %rep block which has been terminated, so
4495 * we're walking through to the %endrep without
4496 * emitting anything. Emit nothing at all, not even a
4497 * blank line: when we emerge from the %rep block we'll
4498 * give a line-number directive so we keep our place
4499 * correctly.
4501 free_tlist(tline);
4502 continue;
4503 } else {
4504 tline = expand_smacro(tline);
4505 if (!expand_mmacro(tline)) {
4507 * De-tokenize the line again, and emit it.
4509 line = detoken(tline, true);
4510 free_tlist(tline);
4511 break;
4512 } else {
4513 continue; /* expand_mmacro calls free_tlist */
4518 return line;
4521 static void pp_cleanup(int pass)
4523 if (defining) {
4524 if(defining->name) {
4525 error(ERR_NONFATAL,
4526 "end of file while still defining macro `%s'",
4527 defining->name);
4528 } else {
4529 error(ERR_NONFATAL, "end of file while still in %%rep");
4532 free_mmacro(defining);
4534 while (cstk)
4535 ctx_pop();
4536 free_macros();
4537 while (istk) {
4538 Include *i = istk;
4539 istk = istk->next;
4540 fclose(i->fp);
4541 nasm_free(i->fname);
4542 nasm_free(i);
4544 while (cstk)
4545 ctx_pop();
4546 nasm_free(src_set_fname(NULL));
4547 if (pass == 0) {
4548 IncPath *i;
4549 free_llist(predef);
4550 delete_Blocks();
4551 while ((i = ipath)) {
4552 ipath = i->next;
4553 if (i->path)
4554 nasm_free(i->path);
4555 nasm_free(i);
4560 void pp_include_path(char *path)
4562 IncPath *i;
4564 i = nasm_malloc(sizeof(IncPath));
4565 i->path = path ? nasm_strdup(path) : NULL;
4566 i->next = NULL;
4568 if (ipath != NULL) {
4569 IncPath *j = ipath;
4570 while (j->next != NULL)
4571 j = j->next;
4572 j->next = i;
4573 } else {
4574 ipath = i;
4578 void pp_pre_include(char *fname)
4580 Token *inc, *space, *name;
4581 Line *l;
4583 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4584 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4585 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4587 l = nasm_malloc(sizeof(Line));
4588 l->next = predef;
4589 l->first = inc;
4590 l->finishes = NULL;
4591 predef = l;
4594 void pp_pre_define(char *definition)
4596 Token *def, *space;
4597 Line *l;
4598 char *equals;
4600 equals = strchr(definition, '=');
4601 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4602 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4603 if (equals)
4604 *equals = ' ';
4605 space->next = tokenize(definition);
4606 if (equals)
4607 *equals = '=';
4609 l = nasm_malloc(sizeof(Line));
4610 l->next = predef;
4611 l->first = def;
4612 l->finishes = NULL;
4613 predef = l;
4616 void pp_pre_undefine(char *definition)
4618 Token *def, *space;
4619 Line *l;
4621 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4622 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4623 space->next = tokenize(definition);
4625 l = nasm_malloc(sizeof(Line));
4626 l->next = predef;
4627 l->first = def;
4628 l->finishes = NULL;
4629 predef = l;
4633 * Added by Keith Kanios:
4635 * This function is used to assist with "runtime" preprocessor
4636 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4638 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4639 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4642 void pp_runtime(char *definition)
4644 Token *def;
4646 def = tokenize(definition);
4647 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4648 free_tlist(def);
4652 void pp_extra_stdmac(macros_t *macros)
4654 extrastdmac = macros;
4657 static void make_tok_num(Token * tok, int64_t val)
4659 char numbuf[20];
4660 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4661 tok->text = nasm_strdup(numbuf);
4662 tok->type = TOK_NUMBER;
4665 Preproc nasmpp = {
4666 pp_reset,
4667 pp_getline,
4668 pp_cleanup