BR 2034542: fix crash when touching __FILE__
[nasm.git] / preproc.c
blobfeb1164cc0c2da81fa51aaea1e34c2bfe6a65428
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include "compiler.h"
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51 #include "quote.h"
52 #include "stdscan.h"
53 #include "tokens.h"
54 #include "tables.h"
56 typedef struct SMacro SMacro;
57 typedef struct MMacro MMacro;
58 typedef struct Context Context;
59 typedef struct Token Token;
60 typedef struct Blocks Blocks;
61 typedef struct Line Line;
62 typedef struct Include Include;
63 typedef struct Cond Cond;
64 typedef struct IncPath IncPath;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
78 struct SMacro {
79 SMacro *next;
80 char *name;
81 bool casesense;
82 bool in_progress;
83 unsigned int nparam;
84 Token *expansion;
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
104 struct MMacro {
105 MMacro *next;
106 char *name;
107 int nparam_min, nparam_max;
108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
111 int64_t in_progress;
112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
115 Line *expansion;
117 MMacro *next_active;
118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
121 unsigned int nparam, rotate;
122 int *paramlen;
123 uint64_t unique;
124 int lineno; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
130 struct Context {
131 Context *next;
132 char *name;
133 struct hash_table localmac;
134 uint32_t number;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
156 enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
166 struct Token {
167 Token *next;
168 char *text;
169 union {
170 SMacro *mac; /* associated macro for TOK_SMAC_END */
171 size_t len; /* scratch length field */
172 } a; /* Auxiliary data */
173 enum pp_token_type type;
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
179 * lists of Tokens.
181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
196 * the line is blank.
198 struct Line {
199 Line *next;
200 MMacro *finishes;
201 Token *first;
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
208 struct Include {
209 Include *next;
210 FILE *fp;
211 Cond *conds;
212 Line *expansion;
213 char *fname;
214 int lineno, lineinc;
215 MMacro *mstk; /* stack of active macros/reps */
219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
223 struct IncPath {
224 IncPath *next;
225 char *path;
229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
235 struct Cond {
236 Cond *next;
237 int state;
239 enum {
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
248 COND_IF_TRUE, COND_IF_FALSE,
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
254 COND_ELSE_TRUE, COND_ELSE_FALSE,
256 * This state means that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. It's for use in
258 * two circumstances: (i) when we've had our moment of emission
259 * and have now started seeing %elifs, and (ii) when the
260 * condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct.
263 COND_NEVER
265 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
268 * These defines are used as the possible return values for do_directive
270 #define NO_DIRECTIVE_FOUND 0
271 #define DIRECTIVE_FOUND 1
274 * Condition codes. Note that we use c_ prefix not C_ because C_ is
275 * used in nasm.h for the "real" condition codes. At _this_ level,
276 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
277 * ones, so we need a different enum...
279 static const char * const conditions[] = {
280 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
281 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
282 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
284 enum pp_conds {
285 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
286 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
287 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
288 c_none = -1
290 static const enum pp_conds inverse_ccs[] = {
291 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
292 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,
293 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
297 * Directive names.
299 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
300 static int is_condition(enum preproc_token arg)
302 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
305 /* For TASM compatibility we need to be able to recognise TASM compatible
306 * conditional compilation directives. Using the NASM pre-processor does
307 * not work, so we look for them specifically from the following list and
308 * then jam in the equivalent NASM directive into the input stream.
311 enum {
312 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
313 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
316 static const char * const tasm_directives[] = {
317 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
318 "ifndef", "include", "local"
321 static int StackSize = 4;
322 static char *StackPointer = "ebp";
323 static int ArgOffset = 8;
324 static int LocalOffset = 0;
326 static Context *cstk;
327 static Include *istk;
328 static IncPath *ipath = NULL;
330 static efunc _error; /* Pointer to client-provided error reporting function */
331 static evalfunc evaluate;
333 static int pass; /* HACK: pass 0 = generate dependencies only */
334 static StrList **dephead, **deptail; /* Dependency list */
336 static uint64_t unique; /* unique identifier numbers */
338 static Line *predef = NULL;
339 static bool do_predef;
341 static ListGen *list;
344 * The current set of multi-line macros we have defined.
346 static struct hash_table mmacros;
349 * The current set of single-line macros we have defined.
351 static struct hash_table smacros;
354 * The multi-line macro we are currently defining, or the %rep
355 * block we are currently reading, if any.
357 static MMacro *defining;
359 static uint64_t nested_mac_count;
360 static uint64_t nested_rep_count;
363 * The number of macro parameters to allocate space for at a time.
365 #define PARAM_DELTA 16
368 * The standard macro set: defined in macros.c in the array nasm_stdmac.
369 * This gives our position in the macro set, when we're processing it.
371 static macros_t *stdmacpos;
374 * The extra standard macros that come from the object format, if
375 * any.
377 static macros_t *extrastdmac = NULL;
378 static bool any_extrastdmac;
381 * Tokens are allocated in blocks to improve speed
383 #define TOKEN_BLOCKSIZE 4096
384 static Token *freeTokens = NULL;
385 struct Blocks {
386 Blocks *next;
387 void *chunk;
390 static Blocks blocks = { NULL, NULL };
393 * Forward declarations.
395 static Token *expand_mmac_params(Token * tline);
396 static Token *expand_smacro(Token * tline);
397 static Token *expand_id(Token * tline);
398 static Context *get_ctx(const char *name, bool all_contexts);
399 static void make_tok_num(Token * tok, int64_t val);
400 static void error(int severity, const char *fmt, ...);
401 static void *new_Block(size_t size);
402 static void delete_Blocks(void);
403 static Token *new_Token(Token * next, enum pp_token_type type,
404 const char *text, int txtlen);
405 static Token *delete_Token(Token * t);
408 * Macros for safe checking of token pointers, avoid *(NULL)
410 #define tok_type_(x,t) ((x) && (x)->type == (t))
411 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
412 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
413 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
415 /* Handle TASM specific directives, which do not contain a % in
416 * front of them. We do it here because I could not find any other
417 * place to do it for the moment, and it is a hack (ideally it would
418 * be nice to be able to use the NASM pre-processor to do it).
420 static char *check_tasm_directive(char *line)
422 int32_t i, j, k, m, len;
423 char *p = line, *oldline, oldchar;
425 /* Skip whitespace */
426 while (nasm_isspace(*p) && *p != 0)
427 p++;
429 /* Binary search for the directive name */
430 i = -1;
431 j = elements(tasm_directives);
432 len = 0;
433 while (!nasm_isspace(p[len]) && p[len] != 0)
434 len++;
435 if (len) {
436 oldchar = p[len];
437 p[len] = 0;
438 while (j - i > 1) {
439 k = (j + i) / 2;
440 m = nasm_stricmp(p, tasm_directives[k]);
441 if (m == 0) {
442 /* We have found a directive, so jam a % in front of it
443 * so that NASM will then recognise it as one if it's own.
445 p[len] = oldchar;
446 len = strlen(p);
447 oldline = line;
448 line = nasm_malloc(len + 2);
449 line[0] = '%';
450 if (k == TM_IFDIFI) {
451 /* NASM does not recognise IFDIFI, so we convert it to
452 * %ifdef BOGUS. This is not used in NASM comaptible
453 * code, but does need to parse for the TASM macro
454 * package.
456 strcpy(line + 1, "ifdef BOGUS");
457 } else {
458 memcpy(line + 1, p, len + 1);
460 nasm_free(oldline);
461 return line;
462 } else if (m < 0) {
463 j = k;
464 } else
465 i = k;
467 p[len] = oldchar;
469 return line;
473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
476 * lineno file').
478 static char *prepreproc(char *line)
480 int lineno, fnlen;
481 char *fname, *oldline;
483 if (line[0] == '#' && line[1] == ' ') {
484 oldline = line;
485 fname = oldline + 2;
486 lineno = atoi(fname);
487 fname += strspn(fname, "0123456789 ");
488 if (*fname == '"')
489 fname++;
490 fnlen = strcspn(fname, "\"");
491 line = nasm_malloc(20 + fnlen);
492 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
493 nasm_free(oldline);
495 if (tasm_compatible_mode)
496 return check_tasm_directive(line);
497 return line;
501 * Free a linked list of tokens.
503 static void free_tlist(Token * list)
505 while (list) {
506 list = delete_Token(list);
511 * Free a linked list of lines.
513 static void free_llist(Line * list)
515 Line *l;
516 while (list) {
517 l = list;
518 list = list->next;
519 free_tlist(l->first);
520 nasm_free(l);
525 * Free an MMacro
527 static void free_mmacro(MMacro * m)
529 nasm_free(m->name);
530 free_tlist(m->dlist);
531 nasm_free(m->defaults);
532 free_llist(m->expansion);
533 nasm_free(m);
537 * Free all currently defined macros, and free the hash tables
539 static void free_smacro_table(struct hash_table *smt)
541 SMacro *s;
542 const char *key;
543 struct hash_tbl_node *it = NULL;
545 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
546 nasm_free((void *)key);
547 while (s) {
548 SMacro *ns = s->next;
549 nasm_free(s->name);
550 free_tlist(s->expansion);
551 nasm_free(s);
552 s = ns;
555 hash_free(smt);
558 static void free_mmacro_table(struct hash_table *mmt)
560 MMacro *m;
561 const char *key;
562 struct hash_tbl_node *it = NULL;
564 it = NULL;
565 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
566 nasm_free((void *)key);
567 while (m) {
568 MMacro *nm = m->next;
569 free_mmacro(m);
570 m = nm;
573 hash_free(mmt);
576 static void free_macros(void)
578 free_smacro_table(&smacros);
579 free_mmacro_table(&mmacros);
583 * Initialize the hash tables
585 static void init_macros(void)
587 hash_init(&smacros, HASH_LARGE);
588 hash_init(&mmacros, HASH_LARGE);
592 * Pop the context stack.
594 static void ctx_pop(void)
596 Context *c = cstk;
598 cstk = cstk->next;
599 free_smacro_table(&c->localmac);
600 nasm_free(c->name);
601 nasm_free(c);
605 * Search for a key in the hash index; adding it if necessary
606 * (in which case we initialize the data pointer to NULL.)
608 static void **
609 hash_findi_add(struct hash_table *hash, const char *str)
611 struct hash_insert hi;
612 void **r;
613 char *strx;
615 r = hash_findi(hash, str, &hi);
616 if (r)
617 return r;
619 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
620 return hash_add(&hi, strx, NULL);
624 * Like hash_findi, but returns the data element rather than a pointer
625 * to it. Used only when not adding a new element, hence no third
626 * argument.
628 static void *
629 hash_findix(struct hash_table *hash, const char *str)
631 void **p;
633 p = hash_findi(hash, str, NULL);
634 return p ? *p : NULL;
637 #define BUF_DELTA 512
639 * Read a line from the top file in istk, handling multiple CR/LFs
640 * at the end of the line read, and handling spurious ^Zs. Will
641 * return lines from the standard macro set if this has not already
642 * been done.
644 static char *read_line(void)
646 char *buffer, *p, *q;
647 int bufsize, continued_count;
649 if (stdmacpos) {
650 unsigned char c;
651 const unsigned char *p = stdmacpos;
652 char *ret, *q;
653 size_t len = 0;
654 while ((c = *p++)) {
655 if (c >= 0x80)
656 len += pp_directives_len[c-0x80]+1;
657 else
658 len++;
660 ret = nasm_malloc(len+1);
661 q = ret;
662 while ((c = *stdmacpos++)) {
663 if (c >= 0x80) {
664 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
665 q += pp_directives_len[c-0x80];
666 *q++ = ' ';
667 } else {
668 *q++ = c;
671 stdmacpos = p;
672 *q = '\0';
674 if (!*stdmacpos) {
675 /* This was the last of the standard macro chain... */
676 stdmacpos = NULL;
677 if (any_extrastdmac) {
678 stdmacpos = extrastdmac;
679 any_extrastdmac = false;
680 } else if (do_predef) {
681 Line *pd, *l;
682 Token *head, **tail, *t;
685 * Nasty hack: here we push the contents of
686 * `predef' on to the top-level expansion stack,
687 * since this is the most convenient way to
688 * implement the pre-include and pre-define
689 * features.
691 for (pd = predef; pd; pd = pd->next) {
692 head = NULL;
693 tail = &head;
694 for (t = pd->first; t; t = t->next) {
695 *tail = new_Token(NULL, t->type, t->text, 0);
696 tail = &(*tail)->next;
698 l = nasm_malloc(sizeof(Line));
699 l->next = istk->expansion;
700 l->first = head;
701 l->finishes = NULL;
702 istk->expansion = l;
704 do_predef = false;
707 return ret;
710 bufsize = BUF_DELTA;
711 buffer = nasm_malloc(BUF_DELTA);
712 p = buffer;
713 continued_count = 0;
714 while (1) {
715 q = fgets(p, bufsize - (p - buffer), istk->fp);
716 if (!q)
717 break;
718 p += strlen(p);
719 if (p > buffer && p[-1] == '\n') {
720 /* Convert backslash-CRLF line continuation sequences into
721 nothing at all (for DOS and Windows) */
722 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
723 p -= 3;
724 *p = 0;
725 continued_count++;
727 /* Also convert backslash-LF line continuation sequences into
728 nothing at all (for Unix) */
729 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
730 p -= 2;
731 *p = 0;
732 continued_count++;
733 } else {
734 break;
737 if (p - buffer > bufsize - 10) {
738 int32_t offset = p - buffer;
739 bufsize += BUF_DELTA;
740 buffer = nasm_realloc(buffer, bufsize);
741 p = buffer + offset; /* prevent stale-pointer problems */
745 if (!q && p == buffer) {
746 nasm_free(buffer);
747 return NULL;
750 src_set_linnum(src_get_linnum() + istk->lineinc +
751 (continued_count * istk->lineinc));
754 * Play safe: remove CRs as well as LFs, if any of either are
755 * present at the end of the line.
757 while (--p >= buffer && (*p == '\n' || *p == '\r'))
758 *p = '\0';
761 * Handle spurious ^Z, which may be inserted into source files
762 * by some file transfer utilities.
764 buffer[strcspn(buffer, "\032")] = '\0';
766 list->line(LIST_READ, buffer);
768 return buffer;
772 * Tokenize a line of text. This is a very simple process since we
773 * don't need to parse the value out of e.g. numeric tokens: we
774 * simply split one string into many.
776 static Token *tokenize(char *line)
778 char *p = line;
779 enum pp_token_type type;
780 Token *list = NULL;
781 Token *t, **tail = &list;
783 while (*line) {
784 p = line;
785 if (*p == '%') {
786 p++;
787 if (nasm_isdigit(*p) ||
788 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
789 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
790 do {
791 p++;
793 while (nasm_isdigit(*p));
794 type = TOK_PREPROC_ID;
795 } else if (*p == '{') {
796 p++;
797 while (*p && *p != '}') {
798 p[-1] = *p;
799 p++;
801 p[-1] = '\0';
802 if (*p)
803 p++;
804 type = TOK_PREPROC_ID;
805 } else if (*p == '?') {
806 type = TOK_PREPROC_Q; /* %? */
807 p++;
808 if (*p == '?') {
809 type = TOK_PREPROC_QQ; /* %?? */
810 p++;
812 } else if (isidchar(*p) ||
813 ((*p == '!' || *p == '%' || *p == '$') &&
814 isidchar(p[1]))) {
815 do {
816 p++;
818 while (isidchar(*p));
819 type = TOK_PREPROC_ID;
820 } else {
821 type = TOK_OTHER;
822 if (*p == '%')
823 p++;
825 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
826 type = TOK_ID;
827 p++;
828 while (*p && isidchar(*p))
829 p++;
830 } else if (*p == '\'' || *p == '"' || *p == '`') {
832 * A string token.
834 type = TOK_STRING;
835 p = nasm_skip_string(p);
837 if (*p) {
838 p++;
839 } else {
840 error(ERR_WARNING, "unterminated string");
841 /* Handling unterminated strings by UNV */
842 /* type = -1; */
844 } else if (isnumstart(*p)) {
845 bool is_hex = false;
846 bool is_float = false;
847 bool has_e = false;
848 char c, *r;
851 * A numeric token.
854 if (*p == '$') {
855 p++;
856 is_hex = true;
859 for (;;) {
860 c = *p++;
862 if (!is_hex && (c == 'e' || c == 'E')) {
863 has_e = true;
864 if (*p == '+' || *p == '-') {
865 /* e can only be followed by +/- if it is either a
866 prefixed hex number or a floating-point number */
867 p++;
868 is_float = true;
870 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
871 is_hex = true;
872 } else if (c == 'P' || c == 'p') {
873 is_float = true;
874 if (*p == '+' || *p == '-')
875 p++;
876 } else if (isnumchar(c) || c == '_')
877 ; /* just advance */
878 else if (c == '.') {
879 /* we need to deal with consequences of the legacy
880 parser, like "1.nolist" being two tokens
881 (TOK_NUMBER, TOK_ID) here; at least give it
882 a shot for now. In the future, we probably need
883 a flex-based scanner with proper pattern matching
884 to do it as well as it can be done. Nothing in
885 the world is going to help the person who wants
886 0x123.p16 interpreted as two tokens, though. */
887 r = p;
888 while (*r == '_')
889 r++;
891 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
892 (!is_hex && (*r == 'e' || *r == 'E')) ||
893 (*r == 'p' || *r == 'P')) {
894 p = r;
895 is_float = true;
896 } else
897 break; /* Terminate the token */
898 } else
899 break;
901 p--; /* Point to first character beyond number */
903 if (has_e && !is_hex) {
904 /* 1e13 is floating-point, but 1e13h is not */
905 is_float = true;
908 type = is_float ? TOK_FLOAT : TOK_NUMBER;
909 } else if (nasm_isspace(*p)) {
910 type = TOK_WHITESPACE;
911 p++;
912 while (*p && nasm_isspace(*p))
913 p++;
915 * Whitespace just before end-of-line is discarded by
916 * pretending it's a comment; whitespace just before a
917 * comment gets lumped into the comment.
919 if (!*p || *p == ';') {
920 type = TOK_COMMENT;
921 while (*p)
922 p++;
924 } else if (*p == ';') {
925 type = TOK_COMMENT;
926 while (*p)
927 p++;
928 } else {
930 * Anything else is an operator of some kind. We check
931 * for all the double-character operators (>>, <<, //,
932 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
933 * else is a single-character operator.
935 type = TOK_OTHER;
936 if ((p[0] == '>' && p[1] == '>') ||
937 (p[0] == '<' && p[1] == '<') ||
938 (p[0] == '/' && p[1] == '/') ||
939 (p[0] == '<' && p[1] == '=') ||
940 (p[0] == '>' && p[1] == '=') ||
941 (p[0] == '=' && p[1] == '=') ||
942 (p[0] == '!' && p[1] == '=') ||
943 (p[0] == '<' && p[1] == '>') ||
944 (p[0] == '&' && p[1] == '&') ||
945 (p[0] == '|' && p[1] == '|') ||
946 (p[0] == '^' && p[1] == '^')) {
947 p++;
949 p++;
952 /* Handling unterminated string by UNV */
953 /*if (type == -1)
955 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
956 t->text[p-line] = *line;
957 tail = &t->next;
959 else */
960 if (type != TOK_COMMENT) {
961 *tail = t = new_Token(NULL, type, line, p - line);
962 tail = &t->next;
964 line = p;
966 return list;
970 * this function allocates a new managed block of memory and
971 * returns a pointer to the block. The managed blocks are
972 * deleted only all at once by the delete_Blocks function.
974 static void *new_Block(size_t size)
976 Blocks *b = &blocks;
978 /* first, get to the end of the linked list */
979 while (b->next)
980 b = b->next;
981 /* now allocate the requested chunk */
982 b->chunk = nasm_malloc(size);
984 /* now allocate a new block for the next request */
985 b->next = nasm_malloc(sizeof(Blocks));
986 /* and initialize the contents of the new block */
987 b->next->next = NULL;
988 b->next->chunk = NULL;
989 return b->chunk;
993 * this function deletes all managed blocks of memory
995 static void delete_Blocks(void)
997 Blocks *a, *b = &blocks;
1000 * keep in mind that the first block, pointed to by blocks
1001 * is a static and not dynamically allocated, so we don't
1002 * free it.
1004 while (b) {
1005 if (b->chunk)
1006 nasm_free(b->chunk);
1007 a = b;
1008 b = b->next;
1009 if (a != &blocks)
1010 nasm_free(a);
1015 * this function creates a new Token and passes a pointer to it
1016 * back to the caller. It sets the type and text elements, and
1017 * also the a.mac and next elements to NULL.
1019 static Token *new_Token(Token * next, enum pp_token_type type,
1020 const char *text, int txtlen)
1022 Token *t;
1023 int i;
1025 if (freeTokens == NULL) {
1026 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1027 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1028 freeTokens[i].next = &freeTokens[i + 1];
1029 freeTokens[i].next = NULL;
1031 t = freeTokens;
1032 freeTokens = t->next;
1033 t->next = next;
1034 t->a.mac = NULL;
1035 t->type = type;
1036 if (type == TOK_WHITESPACE || text == NULL) {
1037 t->text = NULL;
1038 } else {
1039 if (txtlen == 0)
1040 txtlen = strlen(text);
1041 t->text = nasm_malloc(txtlen+1);
1042 memcpy(t->text, text, txtlen);
1043 t->text[txtlen] = '\0';
1045 return t;
1048 static Token *delete_Token(Token * t)
1050 Token *next = t->next;
1051 nasm_free(t->text);
1052 t->next = freeTokens;
1053 freeTokens = t;
1054 return next;
1058 * Convert a line of tokens back into text.
1059 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1060 * will be transformed into ..@ctxnum.xxx
1062 static char *detoken(Token * tlist, bool expand_locals)
1064 Token *t;
1065 int len;
1066 char *line, *p;
1067 const char *q;
1069 len = 0;
1070 for (t = tlist; t; t = t->next) {
1071 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1072 char *p = getenv(t->text + 2);
1073 nasm_free(t->text);
1074 if (p)
1075 t->text = nasm_strdup(p);
1076 else
1077 t->text = NULL;
1079 /* Expand local macros here and not during preprocessing */
1080 if (expand_locals &&
1081 t->type == TOK_PREPROC_ID && t->text &&
1082 t->text[0] == '%' && t->text[1] == '$') {
1083 Context *ctx = get_ctx(t->text, false);
1084 if (ctx) {
1085 char buffer[40];
1086 char *p, *q = t->text + 2;
1088 q += strspn(q, "$");
1089 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1090 p = nasm_strcat(buffer, q);
1091 nasm_free(t->text);
1092 t->text = p;
1095 if (t->type == TOK_WHITESPACE) {
1096 len++;
1097 } else if (t->text) {
1098 len += strlen(t->text);
1101 p = line = nasm_malloc(len + 1);
1102 for (t = tlist; t; t = t->next) {
1103 if (t->type == TOK_WHITESPACE) {
1104 *p++ = ' ';
1105 } else if (t->text) {
1106 q = t->text;
1107 while (*q)
1108 *p++ = *q++;
1111 *p = '\0';
1112 return line;
1116 * A scanner, suitable for use by the expression evaluator, which
1117 * operates on a line of Tokens. Expects a pointer to a pointer to
1118 * the first token in the line to be passed in as its private_data
1119 * field.
1121 * FIX: This really needs to be unified with stdscan.
1123 static int ppscan(void *private_data, struct tokenval *tokval)
1125 Token **tlineptr = private_data;
1126 Token *tline;
1127 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1129 do {
1130 tline = *tlineptr;
1131 *tlineptr = tline ? tline->next : NULL;
1133 while (tline && (tline->type == TOK_WHITESPACE ||
1134 tline->type == TOK_COMMENT));
1136 if (!tline)
1137 return tokval->t_type = TOKEN_EOS;
1139 tokval->t_charptr = tline->text;
1141 if (tline->text[0] == '$' && !tline->text[1])
1142 return tokval->t_type = TOKEN_HERE;
1143 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1144 return tokval->t_type = TOKEN_BASE;
1146 if (tline->type == TOK_ID) {
1147 p = tokval->t_charptr = tline->text;
1148 if (p[0] == '$') {
1149 tokval->t_charptr++;
1150 return tokval->t_type = TOKEN_ID;
1153 for (r = p, s = ourcopy; *r; r++) {
1154 if (r >= p+MAX_KEYWORD)
1155 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1156 *s++ = nasm_tolower(*r);
1158 *s = '\0';
1159 /* right, so we have an identifier sitting in temp storage. now,
1160 * is it actually a register or instruction name, or what? */
1161 return nasm_token_hash(ourcopy, tokval);
1164 if (tline->type == TOK_NUMBER) {
1165 bool rn_error;
1166 tokval->t_integer = readnum(tline->text, &rn_error);
1167 tokval->t_charptr = tline->text;
1168 if (rn_error)
1169 return tokval->t_type = TOKEN_ERRNUM;
1170 else
1171 return tokval->t_type = TOKEN_NUM;
1174 if (tline->type == TOK_FLOAT) {
1175 return tokval->t_type = TOKEN_FLOAT;
1178 if (tline->type == TOK_STRING) {
1179 char bq, *ep;
1181 bq = tline->text[0];
1182 tokval->t_charptr = tline->text;
1183 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1185 if (ep[0] != bq || ep[1] != '\0')
1186 return tokval->t_type = TOKEN_ERRSTR;
1187 else
1188 return tokval->t_type = TOKEN_STR;
1191 if (tline->type == TOK_OTHER) {
1192 if (!strcmp(tline->text, "<<"))
1193 return tokval->t_type = TOKEN_SHL;
1194 if (!strcmp(tline->text, ">>"))
1195 return tokval->t_type = TOKEN_SHR;
1196 if (!strcmp(tline->text, "//"))
1197 return tokval->t_type = TOKEN_SDIV;
1198 if (!strcmp(tline->text, "%%"))
1199 return tokval->t_type = TOKEN_SMOD;
1200 if (!strcmp(tline->text, "=="))
1201 return tokval->t_type = TOKEN_EQ;
1202 if (!strcmp(tline->text, "<>"))
1203 return tokval->t_type = TOKEN_NE;
1204 if (!strcmp(tline->text, "!="))
1205 return tokval->t_type = TOKEN_NE;
1206 if (!strcmp(tline->text, "<="))
1207 return tokval->t_type = TOKEN_LE;
1208 if (!strcmp(tline->text, ">="))
1209 return tokval->t_type = TOKEN_GE;
1210 if (!strcmp(tline->text, "&&"))
1211 return tokval->t_type = TOKEN_DBL_AND;
1212 if (!strcmp(tline->text, "^^"))
1213 return tokval->t_type = TOKEN_DBL_XOR;
1214 if (!strcmp(tline->text, "||"))
1215 return tokval->t_type = TOKEN_DBL_OR;
1219 * We have no other options: just return the first character of
1220 * the token text.
1222 return tokval->t_type = tline->text[0];
1226 * Compare a string to the name of an existing macro; this is a
1227 * simple wrapper which calls either strcmp or nasm_stricmp
1228 * depending on the value of the `casesense' parameter.
1230 static int mstrcmp(const char *p, const char *q, bool casesense)
1232 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1236 * Compare a string to the name of an existing macro; this is a
1237 * simple wrapper which calls either strcmp or nasm_stricmp
1238 * depending on the value of the `casesense' parameter.
1240 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1242 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1246 * Return the Context structure associated with a %$ token. Return
1247 * NULL, having _already_ reported an error condition, if the
1248 * context stack isn't deep enough for the supplied number of $
1249 * signs.
1250 * If all_contexts == true, contexts that enclose current are
1251 * also scanned for such smacro, until it is found; if not -
1252 * only the context that directly results from the number of $'s
1253 * in variable's name.
1255 static Context *get_ctx(const char *name, bool all_contexts)
1257 Context *ctx;
1258 SMacro *m;
1259 int i;
1261 if (!name || name[0] != '%' || name[1] != '$')
1262 return NULL;
1264 if (!cstk) {
1265 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1266 return NULL;
1269 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1270 ctx = ctx->next;
1271 /* i--; Lino - 02/25/02 */
1273 if (!ctx) {
1274 error(ERR_NONFATAL, "`%s': context stack is only"
1275 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1276 return NULL;
1278 if (!all_contexts)
1279 return ctx;
1281 do {
1282 /* Search for this smacro in found context */
1283 m = hash_findix(&ctx->localmac, name);
1284 while (m) {
1285 if (!mstrcmp(m->name, name, m->casesense))
1286 return ctx;
1287 m = m->next;
1289 ctx = ctx->next;
1291 while (ctx);
1292 return NULL;
1296 * Check to see if a file is already in a string list
1298 static bool in_list(const StrList *list, const char *str)
1300 while (list) {
1301 if (!strcmp(list->str, str))
1302 return true;
1303 list = list->next;
1305 return false;
1309 * Open an include file. This routine must always return a valid
1310 * file pointer if it returns - it's responsible for throwing an
1311 * ERR_FATAL and bombing out completely if not. It should also try
1312 * the include path one by one until it finds the file or reaches
1313 * the end of the path.
1315 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1316 bool missing_ok)
1318 FILE *fp;
1319 char *prefix = "";
1320 IncPath *ip = ipath;
1321 int len = strlen(file);
1322 size_t prefix_len = 0;
1323 StrList *sl;
1325 while (1) {
1326 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1327 memcpy(sl->str, prefix, prefix_len);
1328 memcpy(sl->str+prefix_len, file, len+1);
1329 fp = fopen(sl->str, "r");
1330 if (fp && dhead && !in_list(*dhead, sl->str)) {
1331 sl->next = NULL;
1332 **dtail = sl;
1333 *dtail = &sl->next;
1334 } else {
1335 nasm_free(sl);
1337 if (fp)
1338 return fp;
1339 if (!ip) {
1340 if (!missing_ok)
1341 break;
1342 prefix = NULL;
1343 } else {
1344 prefix = ip->path;
1345 ip = ip->next;
1347 if (prefix) {
1348 prefix_len = strlen(prefix);
1349 } else {
1350 /* -MG given and file not found */
1351 if (dhead && !in_list(*dhead, file)) {
1352 sl = nasm_malloc(len+1+sizeof sl->next);
1353 sl->next = NULL;
1354 strcpy(sl->str, file);
1355 **dtail = sl;
1356 *dtail = &sl->next;
1358 return NULL;
1362 error(ERR_FATAL, "unable to open include file `%s'", file);
1363 return NULL; /* never reached - placate compilers */
1367 * Determine if we should warn on defining a single-line macro of
1368 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1369 * return true if _any_ single-line macro of that name is defined.
1370 * Otherwise, will return true if a single-line macro with either
1371 * `nparam' or no parameters is defined.
1373 * If a macro with precisely the right number of parameters is
1374 * defined, or nparam is -1, the address of the definition structure
1375 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1376 * is NULL, no action will be taken regarding its contents, and no
1377 * error will occur.
1379 * Note that this is also called with nparam zero to resolve
1380 * `ifdef'.
1382 * If you already know which context macro belongs to, you can pass
1383 * the context pointer as first parameter; if you won't but name begins
1384 * with %$ the context will be automatically computed. If all_contexts
1385 * is true, macro will be searched in outer contexts as well.
1387 static bool
1388 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1389 bool nocase)
1391 struct hash_table *smtbl;
1392 SMacro *m;
1394 if (ctx) {
1395 smtbl = &ctx->localmac;
1396 } else if (name[0] == '%' && name[1] == '$') {
1397 if (cstk)
1398 ctx = get_ctx(name, false);
1399 if (!ctx)
1400 return false; /* got to return _something_ */
1401 smtbl = &ctx->localmac;
1402 } else {
1403 smtbl = &smacros;
1405 m = (SMacro *) hash_findix(smtbl, name);
1407 while (m) {
1408 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1409 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1410 if (defn) {
1411 if (nparam == (int) m->nparam || nparam == -1)
1412 *defn = m;
1413 else
1414 *defn = NULL;
1416 return true;
1418 m = m->next;
1421 return false;
1425 * Count and mark off the parameters in a multi-line macro call.
1426 * This is called both from within the multi-line macro expansion
1427 * code, and also to mark off the default parameters when provided
1428 * in a %macro definition line.
1430 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1432 int paramsize, brace;
1434 *nparam = paramsize = 0;
1435 *params = NULL;
1436 while (t) {
1437 if (*nparam >= paramsize) {
1438 paramsize += PARAM_DELTA;
1439 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1441 skip_white_(t);
1442 brace = false;
1443 if (tok_is_(t, "{"))
1444 brace = true;
1445 (*params)[(*nparam)++] = t;
1446 while (tok_isnt_(t, brace ? "}" : ","))
1447 t = t->next;
1448 if (t) { /* got a comma/brace */
1449 t = t->next;
1450 if (brace) {
1452 * Now we've found the closing brace, look further
1453 * for the comma.
1455 skip_white_(t);
1456 if (tok_isnt_(t, ",")) {
1457 error(ERR_NONFATAL,
1458 "braces do not enclose all of macro parameter");
1459 while (tok_isnt_(t, ","))
1460 t = t->next;
1462 if (t)
1463 t = t->next; /* eat the comma */
1470 * Determine whether one of the various `if' conditions is true or
1471 * not.
1473 * We must free the tline we get passed.
1475 static bool if_condition(Token * tline, enum preproc_token ct)
1477 enum pp_conditional i = PP_COND(ct);
1478 bool j;
1479 Token *t, *tt, **tptr, *origline;
1480 struct tokenval tokval;
1481 expr *evalresult;
1482 enum pp_token_type needtype;
1484 origline = tline;
1486 switch (i) {
1487 case PPC_IFCTX:
1488 j = false; /* have we matched yet? */
1489 while (true) {
1490 skip_white_(tline);
1491 if (!tline)
1492 break;
1493 if (tline->type != TOK_ID) {
1494 error(ERR_NONFATAL,
1495 "`%s' expects context identifiers", pp_directives[ct]);
1496 free_tlist(origline);
1497 return -1;
1499 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1500 j = true;
1501 tline = tline->next;
1503 break;
1505 case PPC_IFDEF:
1506 j = false; /* have we matched yet? */
1507 while (tline) {
1508 skip_white_(tline);
1509 if (!tline || (tline->type != TOK_ID &&
1510 (tline->type != TOK_PREPROC_ID ||
1511 tline->text[1] != '$'))) {
1512 error(ERR_NONFATAL,
1513 "`%s' expects macro identifiers", pp_directives[ct]);
1514 goto fail;
1516 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1517 j = true;
1518 tline = tline->next;
1520 break;
1522 case PPC_IFIDN:
1523 case PPC_IFIDNI:
1524 tline = expand_smacro(tline);
1525 t = tt = tline;
1526 while (tok_isnt_(tt, ","))
1527 tt = tt->next;
1528 if (!tt) {
1529 error(ERR_NONFATAL,
1530 "`%s' expects two comma-separated arguments",
1531 pp_directives[ct]);
1532 goto fail;
1534 tt = tt->next;
1535 j = true; /* assume equality unless proved not */
1536 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1537 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1538 error(ERR_NONFATAL, "`%s': more than one comma on line",
1539 pp_directives[ct]);
1540 goto fail;
1542 if (t->type == TOK_WHITESPACE) {
1543 t = t->next;
1544 continue;
1546 if (tt->type == TOK_WHITESPACE) {
1547 tt = tt->next;
1548 continue;
1550 if (tt->type != t->type) {
1551 j = false; /* found mismatching tokens */
1552 break;
1554 /* When comparing strings, need to unquote them first */
1555 if (t->type == TOK_STRING) {
1556 size_t l1 = nasm_unquote(t->text, NULL);
1557 size_t l2 = nasm_unquote(tt->text, NULL);
1559 if (l1 != l2) {
1560 j = false;
1561 break;
1563 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1564 j = false;
1565 break;
1567 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1568 j = false; /* found mismatching tokens */
1569 break;
1572 t = t->next;
1573 tt = tt->next;
1575 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1576 j = false; /* trailing gunk on one end or other */
1577 break;
1579 case PPC_IFMACRO:
1581 bool found = false;
1582 MMacro searching, *mmac;
1584 tline = tline->next;
1585 skip_white_(tline);
1586 tline = expand_id(tline);
1587 if (!tok_type_(tline, TOK_ID)) {
1588 error(ERR_NONFATAL,
1589 "`%s' expects a macro name", pp_directives[ct]);
1590 goto fail;
1592 searching.name = nasm_strdup(tline->text);
1593 searching.casesense = true;
1594 searching.plus = false;
1595 searching.nolist = false;
1596 searching.in_progress = 0;
1597 searching.rep_nest = NULL;
1598 searching.nparam_min = 0;
1599 searching.nparam_max = INT_MAX;
1600 tline = expand_smacro(tline->next);
1601 skip_white_(tline);
1602 if (!tline) {
1603 } else if (!tok_type_(tline, TOK_NUMBER)) {
1604 error(ERR_NONFATAL,
1605 "`%s' expects a parameter count or nothing",
1606 pp_directives[ct]);
1607 } else {
1608 searching.nparam_min = searching.nparam_max =
1609 readnum(tline->text, &j);
1610 if (j)
1611 error(ERR_NONFATAL,
1612 "unable to parse parameter count `%s'",
1613 tline->text);
1615 if (tline && tok_is_(tline->next, "-")) {
1616 tline = tline->next->next;
1617 if (tok_is_(tline, "*"))
1618 searching.nparam_max = INT_MAX;
1619 else if (!tok_type_(tline, TOK_NUMBER))
1620 error(ERR_NONFATAL,
1621 "`%s' expects a parameter count after `-'",
1622 pp_directives[ct]);
1623 else {
1624 searching.nparam_max = readnum(tline->text, &j);
1625 if (j)
1626 error(ERR_NONFATAL,
1627 "unable to parse parameter count `%s'",
1628 tline->text);
1629 if (searching.nparam_min > searching.nparam_max)
1630 error(ERR_NONFATAL,
1631 "minimum parameter count exceeds maximum");
1634 if (tline && tok_is_(tline->next, "+")) {
1635 tline = tline->next;
1636 searching.plus = true;
1638 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1639 while (mmac) {
1640 if (!strcmp(mmac->name, searching.name) &&
1641 (mmac->nparam_min <= searching.nparam_max
1642 || searching.plus)
1643 && (searching.nparam_min <= mmac->nparam_max
1644 || mmac->plus)) {
1645 found = true;
1646 break;
1648 mmac = mmac->next;
1650 if(tline && tline->next)
1651 error(ERR_WARNING, "trailing garbage after %%ifmacro ignored");
1652 nasm_free(searching.name);
1653 j = found;
1654 break;
1657 case PPC_IFID:
1658 needtype = TOK_ID;
1659 goto iftype;
1660 case PPC_IFNUM:
1661 needtype = TOK_NUMBER;
1662 goto iftype;
1663 case PPC_IFSTR:
1664 needtype = TOK_STRING;
1665 goto iftype;
1667 iftype:
1668 t = tline = expand_smacro(tline);
1670 while (tok_type_(t, TOK_WHITESPACE) ||
1671 (needtype == TOK_NUMBER &&
1672 tok_type_(t, TOK_OTHER) &&
1673 (t->text[0] == '-' || t->text[0] == '+') &&
1674 !t->text[1]))
1675 t = t->next;
1677 j = tok_type_(t, needtype);
1678 break;
1680 case PPC_IFTOKEN:
1681 t = tline = expand_smacro(tline);
1682 while (tok_type_(t, TOK_WHITESPACE))
1683 t = t->next;
1685 j = false;
1686 if (t) {
1687 t = t->next; /* Skip the actual token */
1688 while (tok_type_(t, TOK_WHITESPACE))
1689 t = t->next;
1690 j = !t; /* Should be nothing left */
1692 break;
1694 case PPC_IFEMPTY:
1695 t = tline = expand_smacro(tline);
1696 while (tok_type_(t, TOK_WHITESPACE))
1697 t = t->next;
1699 j = !t; /* Should be empty */
1700 break;
1702 case PPC_IF:
1703 t = tline = expand_smacro(tline);
1704 tptr = &t;
1705 tokval.t_type = TOKEN_INVALID;
1706 evalresult = evaluate(ppscan, tptr, &tokval,
1707 NULL, pass | CRITICAL, error, NULL);
1708 if (!evalresult)
1709 return -1;
1710 if (tokval.t_type)
1711 error(ERR_WARNING,
1712 "trailing garbage after expression ignored");
1713 if (!is_simple(evalresult)) {
1714 error(ERR_NONFATAL,
1715 "non-constant value given to `%s'", pp_directives[ct]);
1716 goto fail;
1718 j = reloc_value(evalresult) != 0;
1719 break;
1721 default:
1722 error(ERR_FATAL,
1723 "preprocessor directive `%s' not yet implemented",
1724 pp_directives[ct]);
1725 goto fail;
1728 free_tlist(origline);
1729 return j ^ PP_NEGATIVE(ct);
1731 fail:
1732 free_tlist(origline);
1733 return -1;
1737 * Common code for defining an smacro
1739 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1740 int nparam, Token *expansion)
1742 SMacro *smac, **smhead;
1743 struct hash_table *smtbl;
1745 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1746 if (!smac) {
1747 error(ERR_WARNING,
1748 "single-line macro `%s' defined both with and"
1749 " without parameters", mname);
1751 /* Some instances of the old code considered this a failure,
1752 some others didn't. What is the right thing to do here? */
1753 free_tlist(expansion);
1754 return false; /* Failure */
1755 } else {
1757 * We're redefining, so we have to take over an
1758 * existing SMacro structure. This means freeing
1759 * what was already in it.
1761 nasm_free(smac->name);
1762 free_tlist(smac->expansion);
1764 } else {
1765 smtbl = ctx ? &ctx->localmac : &smacros;
1766 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1767 smac = nasm_malloc(sizeof(SMacro));
1768 smac->next = *smhead;
1769 *smhead = smac;
1771 smac->name = nasm_strdup(mname);
1772 smac->casesense = casesense;
1773 smac->nparam = nparam;
1774 smac->expansion = expansion;
1775 smac->in_progress = false;
1776 return true; /* Success */
1780 * Undefine an smacro
1782 static void undef_smacro(Context *ctx, const char *mname)
1784 SMacro **smhead, *s, **sp;
1785 struct hash_table *smtbl;
1787 smtbl = ctx ? &ctx->localmac : &smacros;
1788 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1790 if (smhead) {
1792 * We now have a macro name... go hunt for it.
1794 sp = smhead;
1795 while ((s = *sp) != NULL) {
1796 if (!mstrcmp(s->name, mname, s->casesense)) {
1797 *sp = s->next;
1798 nasm_free(s->name);
1799 free_tlist(s->expansion);
1800 nasm_free(s);
1801 } else {
1802 sp = &s->next;
1809 * Parse a mmacro specification.
1811 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1813 bool err;
1815 tline = tline->next;
1816 skip_white_(tline);
1817 tline = expand_id(tline);
1818 if (!tok_type_(tline, TOK_ID)) {
1819 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1820 return false;
1823 def->name = nasm_strdup(tline->text);
1824 def->plus = false;
1825 def->nolist = false;
1826 def->in_progress = 0;
1827 def->rep_nest = NULL;
1828 def->nparam_min = 0;
1829 def->nparam_max = 0;
1831 tline = expand_smacro(tline->next);
1832 skip_white_(tline);
1833 if (!tok_type_(tline, TOK_NUMBER)) {
1834 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1835 } else {
1836 def->nparam_min = def->nparam_max =
1837 readnum(tline->text, &err);
1838 if (err)
1839 error(ERR_NONFATAL,
1840 "unable to parse parameter count `%s'", tline->text);
1842 if (tline && tok_is_(tline->next, "-")) {
1843 tline = tline->next->next;
1844 if (tok_is_(tline, "*")) {
1845 def->nparam_max = INT_MAX;
1846 } else if (!tok_type_(tline, TOK_NUMBER)) {
1847 error(ERR_NONFATAL,
1848 "`%s' expects a parameter count after `-'", directive);
1849 } else {
1850 def->nparam_max = readnum(tline->text, &err);
1851 if (err) {
1852 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1853 tline->text);
1855 if (def->nparam_min > def->nparam_max) {
1856 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1860 if (tline && tok_is_(tline->next, "+")) {
1861 tline = tline->next;
1862 def->plus = true;
1864 if (tline && tok_type_(tline->next, TOK_ID) &&
1865 !nasm_stricmp(tline->next->text, ".nolist")) {
1866 tline = tline->next;
1867 def->nolist = true;
1871 * Handle default parameters.
1873 if (tline && tline->next) {
1874 def->dlist = tline->next;
1875 tline->next = NULL;
1876 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1877 } else {
1878 def->dlist = NULL;
1879 def->defaults = NULL;
1881 def->expansion = NULL;
1883 if(def->defaults && def->ndefs > def->nparam_max - def->nparam_min)
1884 error(ERR_WARNING, "too much default macro parameters");
1886 return true;
1891 * Decode a size directive
1893 static int parse_size(const char *str) {
1894 static const char *size_names[] =
1895 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1896 static const int sizes[] =
1897 { 0, 1, 4, 16, 8, 10, 2, 32 };
1899 return sizes[bsii(str, size_names, elements(size_names))+1];
1903 * find and process preprocessor directive in passed line
1904 * Find out if a line contains a preprocessor directive, and deal
1905 * with it if so.
1907 * If a directive _is_ found, it is the responsibility of this routine
1908 * (and not the caller) to free_tlist() the line.
1910 * @param tline a pointer to the current tokeninzed line linked list
1911 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1914 static int do_directive(Token * tline)
1916 enum preproc_token i;
1917 int j;
1918 bool err;
1919 int nparam;
1920 bool nolist;
1921 bool casesense;
1922 int k, m;
1923 int offset;
1924 char *p, *pp, *mname;
1925 Include *inc;
1926 Context *ctx;
1927 Cond *cond;
1928 MMacro *mmac, **mmhead;
1929 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1930 Line *l;
1931 struct tokenval tokval;
1932 expr *evalresult;
1933 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1934 int64_t count;
1935 size_t len;
1937 origline = tline;
1939 skip_white_(tline);
1940 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
1941 (tline->text[1] == '%' || tline->text[1] == '$'
1942 || tline->text[1] == '!'))
1943 return NO_DIRECTIVE_FOUND;
1945 i = pp_token_hash(tline->text);
1948 * If we're in a non-emitting branch of a condition construct,
1949 * or walking to the end of an already terminated %rep block,
1950 * we should ignore all directives except for condition
1951 * directives.
1953 if (((istk->conds && !emitting(istk->conds->state)) ||
1954 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1955 return NO_DIRECTIVE_FOUND;
1959 * If we're defining a macro or reading a %rep block, we should
1960 * ignore all directives except for %macro/%imacro (which nest),
1961 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1962 * If we're in a %rep block, another %rep nests, so should be let through.
1964 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1965 i != PP_ENDMACRO && i != PP_ENDM &&
1966 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1967 return NO_DIRECTIVE_FOUND;
1970 if (defining) {
1971 if (i == PP_MACRO || i == PP_IMACRO) {
1972 nested_mac_count++;
1973 return NO_DIRECTIVE_FOUND;
1974 } else if (nested_mac_count > 0) {
1975 if (i == PP_ENDMACRO) {
1976 nested_mac_count--;
1977 return NO_DIRECTIVE_FOUND;
1980 if (!defining->name) {
1981 if (i == PP_REP) {
1982 nested_rep_count++;
1983 return NO_DIRECTIVE_FOUND;
1984 } else if (nested_rep_count > 0) {
1985 if (i == PP_ENDREP) {
1986 nested_rep_count--;
1987 return NO_DIRECTIVE_FOUND;
1993 switch (i) {
1994 case PP_INVALID:
1995 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1996 tline->text);
1997 return NO_DIRECTIVE_FOUND; /* didn't get it */
1999 case PP_STACKSIZE:
2000 /* Directive to tell NASM what the default stack size is. The
2001 * default is for a 16-bit stack, and this can be overriden with
2002 * %stacksize large.
2003 * the following form:
2005 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2007 tline = tline->next;
2008 if (tline && tline->type == TOK_WHITESPACE)
2009 tline = tline->next;
2010 if (!tline || tline->type != TOK_ID) {
2011 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2012 free_tlist(origline);
2013 return DIRECTIVE_FOUND;
2015 if (nasm_stricmp(tline->text, "flat") == 0) {
2016 /* All subsequent ARG directives are for a 32-bit stack */
2017 StackSize = 4;
2018 StackPointer = "ebp";
2019 ArgOffset = 8;
2020 LocalOffset = 0;
2021 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2022 /* All subsequent ARG directives are for a 64-bit stack */
2023 StackSize = 8;
2024 StackPointer = "rbp";
2025 ArgOffset = 8;
2026 LocalOffset = 0;
2027 } else if (nasm_stricmp(tline->text, "large") == 0) {
2028 /* All subsequent ARG directives are for a 16-bit stack,
2029 * far function call.
2031 StackSize = 2;
2032 StackPointer = "bp";
2033 ArgOffset = 4;
2034 LocalOffset = 0;
2035 } else if (nasm_stricmp(tline->text, "small") == 0) {
2036 /* All subsequent ARG directives are for a 16-bit stack,
2037 * far function call. We don't support near functions.
2039 StackSize = 2;
2040 StackPointer = "bp";
2041 ArgOffset = 6;
2042 LocalOffset = 0;
2043 } else {
2044 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2045 free_tlist(origline);
2046 return DIRECTIVE_FOUND;
2048 free_tlist(origline);
2049 return DIRECTIVE_FOUND;
2051 case PP_ARG:
2052 /* TASM like ARG directive to define arguments to functions, in
2053 * the following form:
2055 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2057 offset = ArgOffset;
2058 do {
2059 char *arg, directive[256];
2060 int size = StackSize;
2062 /* Find the argument name */
2063 tline = tline->next;
2064 if (tline && tline->type == TOK_WHITESPACE)
2065 tline = tline->next;
2066 if (!tline || tline->type != TOK_ID) {
2067 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2068 free_tlist(origline);
2069 return DIRECTIVE_FOUND;
2071 arg = tline->text;
2073 /* Find the argument size type */
2074 tline = tline->next;
2075 if (!tline || tline->type != TOK_OTHER
2076 || tline->text[0] != ':') {
2077 error(ERR_NONFATAL,
2078 "Syntax error processing `%%arg' directive");
2079 free_tlist(origline);
2080 return DIRECTIVE_FOUND;
2082 tline = tline->next;
2083 if (!tline || tline->type != TOK_ID) {
2084 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2085 free_tlist(origline);
2086 return DIRECTIVE_FOUND;
2089 /* Allow macro expansion of type parameter */
2090 tt = tokenize(tline->text);
2091 tt = expand_smacro(tt);
2092 size = parse_size(tt->text);
2093 if (!size) {
2094 error(ERR_NONFATAL,
2095 "Invalid size type for `%%arg' missing directive");
2096 free_tlist(tt);
2097 free_tlist(origline);
2098 return DIRECTIVE_FOUND;
2100 free_tlist(tt);
2102 /* Round up to even stack slots */
2103 size = (size+StackSize-1) & ~(StackSize-1);
2105 /* Now define the macro for the argument */
2106 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2107 arg, StackPointer, offset);
2108 do_directive(tokenize(directive));
2109 offset += size;
2111 /* Move to the next argument in the list */
2112 tline = tline->next;
2113 if (tline && tline->type == TOK_WHITESPACE)
2114 tline = tline->next;
2115 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2116 ArgOffset = offset;
2117 free_tlist(origline);
2118 return DIRECTIVE_FOUND;
2120 case PP_LOCAL:
2121 /* TASM like LOCAL directive to define local variables for a
2122 * function, in the following form:
2124 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2126 * The '= LocalSize' at the end is ignored by NASM, but is
2127 * required by TASM to define the local parameter size (and used
2128 * by the TASM macro package).
2130 offset = LocalOffset;
2131 do {
2132 char *local, directive[256];
2133 int size = StackSize;
2135 /* Find the argument name */
2136 tline = tline->next;
2137 if (tline && tline->type == TOK_WHITESPACE)
2138 tline = tline->next;
2139 if (!tline || tline->type != TOK_ID) {
2140 error(ERR_NONFATAL,
2141 "`%%local' missing argument parameter");
2142 free_tlist(origline);
2143 return DIRECTIVE_FOUND;
2145 local = tline->text;
2147 /* Find the argument size type */
2148 tline = tline->next;
2149 if (!tline || tline->type != TOK_OTHER
2150 || tline->text[0] != ':') {
2151 error(ERR_NONFATAL,
2152 "Syntax error processing `%%local' directive");
2153 free_tlist(origline);
2154 return DIRECTIVE_FOUND;
2156 tline = tline->next;
2157 if (!tline || tline->type != TOK_ID) {
2158 error(ERR_NONFATAL,
2159 "`%%local' missing size type parameter");
2160 free_tlist(origline);
2161 return DIRECTIVE_FOUND;
2164 /* Allow macro expansion of type parameter */
2165 tt = tokenize(tline->text);
2166 tt = expand_smacro(tt);
2167 size = parse_size(tt->text);
2168 if (!size) {
2169 error(ERR_NONFATAL,
2170 "Invalid size type for `%%local' missing directive");
2171 free_tlist(tt);
2172 free_tlist(origline);
2173 return DIRECTIVE_FOUND;
2175 free_tlist(tt);
2177 /* Round up to even stack slots */
2178 size = (size+StackSize-1) & ~(StackSize-1);
2180 offset += size; /* Negative offset, increment before */
2182 /* Now define the macro for the argument */
2183 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2184 local, StackPointer, offset);
2185 do_directive(tokenize(directive));
2187 /* Now define the assign to setup the enter_c macro correctly */
2188 snprintf(directive, sizeof(directive),
2189 "%%assign %%$localsize %%$localsize+%d", size);
2190 do_directive(tokenize(directive));
2192 /* Move to the next argument in the list */
2193 tline = tline->next;
2194 if (tline && tline->type == TOK_WHITESPACE)
2195 tline = tline->next;
2196 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2197 LocalOffset = offset;
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND;
2201 case PP_CLEAR:
2202 if (tline->next)
2203 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2204 free_macros();
2205 init_macros();
2206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
2209 case PP_DEPEND:
2210 t = tline->next = expand_smacro(tline->next);
2211 skip_white_(t);
2212 if (!t || (t->type != TOK_STRING &&
2213 t->type != TOK_INTERNAL_STRING)) {
2214 error(ERR_NONFATAL, "`%%depend' expects a file name");
2215 free_tlist(origline);
2216 return DIRECTIVE_FOUND; /* but we did _something_ */
2218 if (t->next)
2219 error(ERR_WARNING,
2220 "trailing garbage after `%%depend' ignored");
2221 p = t->text;
2222 if (t->type != TOK_INTERNAL_STRING)
2223 nasm_unquote(p, NULL);
2224 if (dephead && !in_list(*dephead, p)) {
2225 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2226 sl->next = NULL;
2227 strcpy(sl->str, p);
2228 *deptail = sl;
2229 deptail = &sl->next;
2231 free_tlist(origline);
2232 return DIRECTIVE_FOUND;
2234 case PP_INCLUDE:
2235 t = tline->next = expand_smacro(tline->next);
2236 skip_white_(t);
2238 if (!t || (t->type != TOK_STRING &&
2239 t->type != TOK_INTERNAL_STRING)) {
2240 error(ERR_NONFATAL, "`%%include' expects a file name");
2241 free_tlist(origline);
2242 return DIRECTIVE_FOUND; /* but we did _something_ */
2244 if (t->next)
2245 error(ERR_WARNING,
2246 "trailing garbage after `%%include' ignored");
2247 p = t->text;
2248 if (t->type != TOK_INTERNAL_STRING)
2249 nasm_unquote(p, NULL);
2250 inc = nasm_malloc(sizeof(Include));
2251 inc->next = istk;
2252 inc->conds = NULL;
2253 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2254 if (!inc->fp) {
2255 /* -MG given but file not found */
2256 nasm_free(inc);
2257 } else {
2258 inc->fname = src_set_fname(nasm_strdup(p));
2259 inc->lineno = src_set_linnum(0);
2260 inc->lineinc = 1;
2261 inc->expansion = NULL;
2262 inc->mstk = NULL;
2263 istk = inc;
2264 list->uplevel(LIST_INCLUDE);
2266 free_tlist(origline);
2267 return DIRECTIVE_FOUND;
2269 case PP_USE:
2271 static macros_t *use_pkg;
2272 const char *pkg_macro;
2274 t = tline->next = expand_smacro(tline->next);
2275 skip_white_(t);
2277 if (!t || (t->type != TOK_STRING &&
2278 t->type != TOK_INTERNAL_STRING &&
2279 t->type != TOK_ID)) {
2280 error(ERR_NONFATAL, "`%%use' expects a package name");
2281 free_tlist(origline);
2282 return DIRECTIVE_FOUND; /* but we did _something_ */
2284 if (t->next)
2285 error(ERR_WARNING,
2286 "trailing garbage after `%%use' ignored");
2287 if (t->type == TOK_STRING)
2288 nasm_unquote(t->text, NULL);
2289 use_pkg = nasm_stdmac_find_package(t->text);
2290 if (!use_pkg)
2291 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
2292 /* The first string will be <%define>__USE_*__ */
2293 pkg_macro = (char *)use_pkg + 1;
2294 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2295 /* Not already included, go ahead and include it */
2296 stdmacpos = use_pkg;
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND;
2301 case PP_PUSH:
2302 tline = tline->next;
2303 skip_white_(tline);
2304 tline = expand_id(tline);
2305 if (tline) {
2306 if (!tok_type_(tline, TOK_ID)) {
2307 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2308 free_tlist(origline);
2309 return DIRECTIVE_FOUND; /* but we did _something_ */
2311 if (tline->next)
2312 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2313 p = nasm_strdup(tline->text);
2314 } else {
2315 p = NULL; /* Anonymous context */
2317 ctx = nasm_malloc(sizeof(Context));
2318 ctx->next = cstk;
2319 hash_init(&ctx->localmac, HASH_SMALL);
2320 ctx->name = p;
2321 ctx->number = unique++;
2322 cstk = ctx;
2323 free_tlist(origline);
2324 return DIRECTIVE_FOUND;
2326 case PP_REPL:
2327 tline = tline->next;
2328 skip_white_(tline);
2329 tline = expand_id(tline);
2330 if (tline) {
2331 if (!tok_type_(tline, TOK_ID)) {
2332 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND; /* but we did _something_ */
2336 if (tline->next)
2337 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2338 p = nasm_strdup(tline->text);
2339 } else {
2340 p = NULL;
2342 if (!cstk)
2343 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2344 else {
2345 nasm_free(cstk->name);
2346 cstk->name = p;
2348 free_tlist(origline);
2349 return DIRECTIVE_FOUND;
2351 case PP_POP:
2352 if (tline->next)
2353 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2354 if (!cstk)
2355 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2356 else
2357 ctx_pop();
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2361 case PP_ERROR:
2362 case PP_WARNING:
2364 int severity = (i == PP_ERROR)
2365 ? ERR_NONFATAL|ERR_NO_SEVERITY
2366 : ERR_WARNING|ERR_NO_SEVERITY;
2368 tline->next = expand_smacro(tline->next);
2369 tline = tline->next;
2370 skip_white_(tline);
2371 t = tline ? tline->next : NULL;
2372 skip_white_(t);
2373 if (tok_type_(tline, TOK_STRING) && !t) {
2374 /* The line contains only a quoted string */
2375 p = tline->text;
2376 nasm_unquote(p, NULL);
2377 error(severity, "%s: %s", pp_directives[i], p);
2378 } else {
2379 /* Not a quoted string, or more than a quoted string */
2380 p = detoken(tline, false);
2381 error(severity, "%s: %s", pp_directives[i], p);
2382 nasm_free(p);
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2388 CASE_PP_IF:
2389 if (istk->conds && !emitting(istk->conds->state))
2390 j = COND_NEVER;
2391 else {
2392 j = if_condition(tline->next, i);
2393 tline->next = NULL; /* it got freed */
2394 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2396 cond = nasm_malloc(sizeof(Cond));
2397 cond->next = istk->conds;
2398 cond->state = j;
2399 istk->conds = cond;
2400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2403 CASE_PP_ELIF:
2404 if (!istk->conds)
2405 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2406 if (emitting(istk->conds->state)
2407 || istk->conds->state == COND_NEVER)
2408 istk->conds->state = COND_NEVER;
2409 else {
2411 * IMPORTANT: In the case of %if, we will already have
2412 * called expand_mmac_params(); however, if we're
2413 * processing an %elif we must have been in a
2414 * non-emitting mode, which would have inhibited
2415 * the normal invocation of expand_mmac_params(). Therefore,
2416 * we have to do it explicitly here.
2418 j = if_condition(expand_mmac_params(tline->next), i);
2419 tline->next = NULL; /* it got freed */
2420 istk->conds->state =
2421 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
2426 case PP_ELSE:
2427 if (tline->next)
2428 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2429 if (!istk->conds)
2430 error(ERR_FATAL, "`%%else': no matching `%%if'");
2431 if (emitting(istk->conds->state)
2432 || istk->conds->state == COND_NEVER)
2433 istk->conds->state = COND_ELSE_FALSE;
2434 else
2435 istk->conds->state = COND_ELSE_TRUE;
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2439 case PP_ENDIF:
2440 if (tline->next)
2441 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2442 if (!istk->conds)
2443 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2444 cond = istk->conds;
2445 istk->conds = cond->next;
2446 nasm_free(cond);
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2450 case PP_MACRO:
2451 case PP_IMACRO:
2452 if (defining) {
2453 error(ERR_FATAL,
2454 "`%%%smacro': already defining a macro",
2455 (i == PP_IMACRO ? "i" : ""));
2456 return DIRECTIVE_FOUND;
2458 defining = nasm_malloc(sizeof(MMacro));
2459 defining->casesense = (i == PP_MACRO);
2460 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2461 nasm_free(defining);
2462 defining = NULL;
2463 return DIRECTIVE_FOUND;
2466 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2467 while (mmac) {
2468 if (!strcmp(mmac->name, defining->name) &&
2469 (mmac->nparam_min <= defining->nparam_max
2470 || defining->plus)
2471 && (defining->nparam_min <= mmac->nparam_max
2472 || mmac->plus)) {
2473 error(ERR_WARNING,
2474 "redefining multi-line macro `%s'", defining->name);
2475 return DIRECTIVE_FOUND;
2477 mmac = mmac->next;
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2482 case PP_ENDM:
2483 case PP_ENDMACRO:
2484 if (! (defining && defining->name)) {
2485 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2486 return DIRECTIVE_FOUND;
2488 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2489 defining->next = *mmhead;
2490 *mmhead = defining;
2491 defining = NULL;
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2495 case PP_UNMACRO:
2496 case PP_UNIMACRO:
2498 MMacro **mmac_p;
2499 MMacro spec;
2501 spec.casesense = (i == PP_UNMACRO);
2502 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2503 return DIRECTIVE_FOUND;
2505 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2506 while (mmac_p && *mmac_p) {
2507 mmac = *mmac_p;
2508 if (mmac->casesense == spec.casesense &&
2509 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2510 mmac->nparam_min == spec.nparam_min &&
2511 mmac->nparam_max == spec.nparam_max &&
2512 mmac->plus == spec.plus) {
2513 *mmac_p = mmac->next;
2514 free_mmacro(mmac);
2515 } else {
2516 mmac_p = &mmac->next;
2519 free_tlist(origline);
2520 free_tlist(spec.dlist);
2521 return DIRECTIVE_FOUND;
2524 case PP_ROTATE:
2525 if (tline->next && tline->next->type == TOK_WHITESPACE)
2526 tline = tline->next;
2527 if (tline->next == NULL) {
2528 free_tlist(origline);
2529 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2530 return DIRECTIVE_FOUND;
2532 t = expand_smacro(tline->next);
2533 tline->next = NULL;
2534 free_tlist(origline);
2535 tline = t;
2536 tptr = &t;
2537 tokval.t_type = TOKEN_INVALID;
2538 evalresult =
2539 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2540 free_tlist(tline);
2541 if (!evalresult)
2542 return DIRECTIVE_FOUND;
2543 if (tokval.t_type)
2544 error(ERR_WARNING,
2545 "trailing garbage after expression ignored");
2546 if (!is_simple(evalresult)) {
2547 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2548 return DIRECTIVE_FOUND;
2550 mmac = istk->mstk;
2551 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2552 mmac = mmac->next_active;
2553 if (!mmac) {
2554 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2555 } else if (mmac->nparam == 0) {
2556 error(ERR_NONFATAL,
2557 "`%%rotate' invoked within macro without parameters");
2558 } else {
2559 int rotate = mmac->rotate + reloc_value(evalresult);
2561 rotate %= (int)mmac->nparam;
2562 if (rotate < 0)
2563 rotate += mmac->nparam;
2565 mmac->rotate = rotate;
2567 return DIRECTIVE_FOUND;
2569 case PP_REP:
2570 nolist = false;
2571 do {
2572 tline = tline->next;
2573 } while (tok_type_(tline, TOK_WHITESPACE));
2575 if (tok_type_(tline, TOK_ID) &&
2576 nasm_stricmp(tline->text, ".nolist") == 0) {
2577 nolist = true;
2578 do {
2579 tline = tline->next;
2580 } while (tok_type_(tline, TOK_WHITESPACE));
2583 if (tline) {
2584 t = expand_smacro(tline);
2585 tptr = &t;
2586 tokval.t_type = TOKEN_INVALID;
2587 evalresult =
2588 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2589 if (!evalresult) {
2590 free_tlist(origline);
2591 return DIRECTIVE_FOUND;
2593 if (tokval.t_type)
2594 error(ERR_WARNING,
2595 "trailing garbage after expression ignored");
2596 if (!is_simple(evalresult)) {
2597 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2598 return DIRECTIVE_FOUND;
2600 count = reloc_value(evalresult) + 1;
2601 } else {
2602 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2603 count = 0;
2605 free_tlist(origline);
2607 tmp_defining = defining;
2608 defining = nasm_malloc(sizeof(MMacro));
2609 defining->name = NULL; /* flags this macro as a %rep block */
2610 defining->casesense = false;
2611 defining->plus = false;
2612 defining->nolist = nolist;
2613 defining->in_progress = count;
2614 defining->nparam_min = defining->nparam_max = 0;
2615 defining->defaults = NULL;
2616 defining->dlist = NULL;
2617 defining->expansion = NULL;
2618 defining->next_active = istk->mstk;
2619 defining->rep_nest = tmp_defining;
2620 return DIRECTIVE_FOUND;
2622 case PP_ENDREP:
2623 if (!defining || defining->name) {
2624 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2625 return DIRECTIVE_FOUND;
2629 * Now we have a "macro" defined - although it has no name
2630 * and we won't be entering it in the hash tables - we must
2631 * push a macro-end marker for it on to istk->expansion.
2632 * After that, it will take care of propagating itself (a
2633 * macro-end marker line for a macro which is really a %rep
2634 * block will cause the macro to be re-expanded, complete
2635 * with another macro-end marker to ensure the process
2636 * continues) until the whole expansion is forcibly removed
2637 * from istk->expansion by a %exitrep.
2639 l = nasm_malloc(sizeof(Line));
2640 l->next = istk->expansion;
2641 l->finishes = defining;
2642 l->first = NULL;
2643 istk->expansion = l;
2645 istk->mstk = defining;
2647 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2648 tmp_defining = defining;
2649 defining = defining->rep_nest;
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2653 case PP_EXITREP:
2655 * We must search along istk->expansion until we hit a
2656 * macro-end marker for a macro with no name. Then we set
2657 * its `in_progress' flag to 0.
2659 for (l = istk->expansion; l; l = l->next)
2660 if (l->finishes && !l->finishes->name)
2661 break;
2663 if (l)
2664 l->finishes->in_progress = 1;
2665 else
2666 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2670 case PP_XDEFINE:
2671 case PP_IXDEFINE:
2672 case PP_DEFINE:
2673 case PP_IDEFINE:
2674 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2676 tline = tline->next;
2677 skip_white_(tline);
2678 tline = expand_id(tline);
2679 if (!tline || (tline->type != TOK_ID &&
2680 (tline->type != TOK_PREPROC_ID ||
2681 tline->text[1] != '$'))) {
2682 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2683 pp_directives[i]);
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND;
2688 ctx = get_ctx(tline->text, false);
2690 mname = tline->text;
2691 last = tline;
2692 param_start = tline = tline->next;
2693 nparam = 0;
2695 /* Expand the macro definition now for %xdefine and %ixdefine */
2696 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2697 tline = expand_smacro(tline);
2699 if (tok_is_(tline, "(")) {
2701 * This macro has parameters.
2704 tline = tline->next;
2705 while (1) {
2706 skip_white_(tline);
2707 if (!tline) {
2708 error(ERR_NONFATAL, "parameter identifier expected");
2709 free_tlist(origline);
2710 return DIRECTIVE_FOUND;
2712 if (tline->type != TOK_ID) {
2713 error(ERR_NONFATAL,
2714 "`%s': parameter identifier expected",
2715 tline->text);
2716 free_tlist(origline);
2717 return DIRECTIVE_FOUND;
2719 tline->type = TOK_SMAC_PARAM + nparam++;
2720 tline = tline->next;
2721 skip_white_(tline);
2722 if (tok_is_(tline, ",")) {
2723 tline = tline->next;
2724 } else {
2725 if (!tok_is_(tline, ")")) {
2726 error(ERR_NONFATAL,
2727 "`)' expected to terminate macro template");
2728 free_tlist(origline);
2729 return DIRECTIVE_FOUND;
2731 break;
2734 last = tline;
2735 tline = tline->next;
2737 if (tok_type_(tline, TOK_WHITESPACE))
2738 last = tline, tline = tline->next;
2739 macro_start = NULL;
2740 last->next = NULL;
2741 t = tline;
2742 while (t) {
2743 if (t->type == TOK_ID) {
2744 for (tt = param_start; tt; tt = tt->next)
2745 if (tt->type >= TOK_SMAC_PARAM &&
2746 !strcmp(tt->text, t->text))
2747 t->type = tt->type;
2749 tt = t->next;
2750 t->next = macro_start;
2751 macro_start = t;
2752 t = tt;
2755 * Good. We now have a macro name, a parameter count, and a
2756 * token list (in reverse order) for an expansion. We ought
2757 * to be OK just to create an SMacro, store it, and let
2758 * free_tlist have the rest of the line (which we have
2759 * carefully re-terminated after chopping off the expansion
2760 * from the end).
2762 define_smacro(ctx, mname, casesense, nparam, macro_start);
2763 free_tlist(origline);
2764 return DIRECTIVE_FOUND;
2766 case PP_UNDEF:
2767 tline = tline->next;
2768 skip_white_(tline);
2769 tline = expand_id(tline);
2770 if (!tline || (tline->type != TOK_ID &&
2771 (tline->type != TOK_PREPROC_ID ||
2772 tline->text[1] != '$'))) {
2773 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
2777 if (tline->next) {
2778 error(ERR_WARNING,
2779 "trailing garbage after macro name ignored");
2782 /* Find the context that symbol belongs to */
2783 ctx = get_ctx(tline->text, false);
2784 undef_smacro(ctx, tline->text);
2785 free_tlist(origline);
2786 return DIRECTIVE_FOUND;
2788 case PP_DEFSTR:
2789 case PP_IDEFSTR:
2790 casesense = (i == PP_DEFSTR);
2792 tline = tline->next;
2793 skip_white_(tline);
2794 tline = expand_id(tline);
2795 if (!tline || (tline->type != TOK_ID &&
2796 (tline->type != TOK_PREPROC_ID ||
2797 tline->text[1] != '$'))) {
2798 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2799 pp_directives[i]);
2800 free_tlist(origline);
2801 return DIRECTIVE_FOUND;
2804 ctx = get_ctx(tline->text, false);
2806 mname = tline->text;
2807 last = tline;
2808 tline = expand_smacro(tline->next);
2809 last->next = NULL;
2811 while (tok_type_(tline, TOK_WHITESPACE))
2812 tline = delete_Token(tline);
2814 p = detoken(tline, false);
2815 macro_start = nasm_malloc(sizeof(*macro_start));
2816 macro_start->next = NULL;
2817 macro_start->text = nasm_quote(p, strlen(p));
2818 macro_start->type = TOK_STRING;
2819 macro_start->a.mac = NULL;
2820 nasm_free(p);
2823 * We now have a macro name, an implicit parameter count of
2824 * zero, and a string token to use as an expansion. Create
2825 * and store an SMacro.
2827 define_smacro(ctx, mname, casesense, 0, macro_start);
2828 free_tlist(origline);
2829 return DIRECTIVE_FOUND;
2831 case PP_PATHSEARCH:
2833 FILE *fp;
2834 StrList *xsl = NULL;
2835 StrList **xst = &xsl;
2837 casesense = true;
2839 tline = tline->next;
2840 skip_white_(tline);
2841 tline = expand_id(tline);
2842 if (!tline || (tline->type != TOK_ID &&
2843 (tline->type != TOK_PREPROC_ID ||
2844 tline->text[1] != '$'))) {
2845 error(ERR_NONFATAL,
2846 "`%%pathsearch' expects a macro identifier as first parameter");
2847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
2850 ctx = get_ctx(tline->text, false);
2852 mname = tline->text;
2853 last = tline;
2854 tline = expand_smacro(tline->next);
2855 last->next = NULL;
2857 t = tline;
2858 while (tok_type_(t, TOK_WHITESPACE))
2859 t = t->next;
2861 if (!t || (t->type != TOK_STRING &&
2862 t->type != TOK_INTERNAL_STRING)) {
2863 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2864 free_tlist(tline);
2865 free_tlist(origline);
2866 return DIRECTIVE_FOUND; /* but we did _something_ */
2868 if (t->next)
2869 error(ERR_WARNING,
2870 "trailing garbage after `%%pathsearch' ignored");
2871 p = t->text;
2872 if (t->type != TOK_INTERNAL_STRING)
2873 nasm_unquote(p, NULL);
2875 fp = inc_fopen(p, &xsl, &xst, true);
2876 if (fp) {
2877 p = xsl->str;
2878 fclose(fp); /* Don't actually care about the file */
2880 macro_start = nasm_malloc(sizeof(*macro_start));
2881 macro_start->next = NULL;
2882 macro_start->text = nasm_quote(p, strlen(p));
2883 macro_start->type = TOK_STRING;
2884 macro_start->a.mac = NULL;
2885 if (xsl)
2886 nasm_free(xsl);
2889 * We now have a macro name, an implicit parameter count of
2890 * zero, and a string token to use as an expansion. Create
2891 * and store an SMacro.
2893 define_smacro(ctx, mname, casesense, 0, macro_start);
2894 free_tlist(tline);
2895 free_tlist(origline);
2896 return DIRECTIVE_FOUND;
2899 case PP_STRLEN:
2900 casesense = true;
2902 tline = tline->next;
2903 skip_white_(tline);
2904 tline = expand_id(tline);
2905 if (!tline || (tline->type != TOK_ID &&
2906 (tline->type != TOK_PREPROC_ID ||
2907 tline->text[1] != '$'))) {
2908 error(ERR_NONFATAL,
2909 "`%%strlen' expects a macro identifier as first parameter");
2910 free_tlist(origline);
2911 return DIRECTIVE_FOUND;
2913 ctx = get_ctx(tline->text, false);
2915 mname = tline->text;
2916 last = tline;
2917 tline = expand_smacro(tline->next);
2918 last->next = NULL;
2920 t = tline;
2921 while (tok_type_(t, TOK_WHITESPACE))
2922 t = t->next;
2923 /* t should now point to the string */
2924 if (t->type != TOK_STRING) {
2925 error(ERR_NONFATAL,
2926 "`%%strlen` requires string as second parameter");
2927 free_tlist(tline);
2928 free_tlist(origline);
2929 return DIRECTIVE_FOUND;
2932 macro_start = nasm_malloc(sizeof(*macro_start));
2933 macro_start->next = NULL;
2934 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
2935 macro_start->a.mac = NULL;
2938 * We now have a macro name, an implicit parameter count of
2939 * zero, and a numeric token to use as an expansion. Create
2940 * and store an SMacro.
2942 define_smacro(ctx, mname, casesense, 0, macro_start);
2943 free_tlist(tline);
2944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
2947 case PP_STRCAT:
2948 casesense = true;
2950 tline = tline->next;
2951 skip_white_(tline);
2952 tline = expand_id(tline);
2953 if (!tline || (tline->type != TOK_ID &&
2954 (tline->type != TOK_PREPROC_ID ||
2955 tline->text[1] != '$'))) {
2956 error(ERR_NONFATAL,
2957 "`%%strcat' expects a macro identifier as first parameter");
2958 free_tlist(origline);
2959 return DIRECTIVE_FOUND;
2961 ctx = get_ctx(tline->text, false);
2963 mname = tline->text;
2964 last = tline;
2965 tline = expand_smacro(tline->next);
2966 last->next = NULL;
2968 len = 0;
2969 for (t = tline; t; t = t->next) {
2970 switch (t->type) {
2971 case TOK_WHITESPACE:
2972 break;
2973 case TOK_STRING:
2974 len += t->a.len = nasm_unquote(t->text, NULL);
2975 break;
2976 case TOK_OTHER:
2977 if (!strcmp(t->text, ",")) /* permit comma separators */
2978 break;
2979 /* else fall through */
2980 default:
2981 error(ERR_NONFATAL,
2982 "non-string passed to `%%strcat' (%d)", t->type);
2983 free_tlist(tline);
2984 free_tlist(origline);
2985 return DIRECTIVE_FOUND;
2989 p = pp = nasm_malloc(len);
2990 t = tline;
2991 for (t = tline; t; t = t->next) {
2992 if (t->type == TOK_STRING) {
2993 memcpy(p, t->text, t->a.len);
2994 p += t->a.len;
2999 * We now have a macro name, an implicit parameter count of
3000 * zero, and a numeric token to use as an expansion. Create
3001 * and store an SMacro.
3003 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3004 macro_start->text = nasm_quote(pp, len);
3005 nasm_free(pp);
3006 define_smacro(ctx, mname, casesense, 0, macro_start);
3007 free_tlist(tline);
3008 free_tlist(origline);
3009 return DIRECTIVE_FOUND;
3011 case PP_SUBSTR:
3013 int64_t a1, a2;
3014 size_t len;
3016 casesense = true;
3018 tline = tline->next;
3019 skip_white_(tline);
3020 tline = expand_id(tline);
3021 if (!tline || (tline->type != TOK_ID &&
3022 (tline->type != TOK_PREPROC_ID ||
3023 tline->text[1] != '$'))) {
3024 error(ERR_NONFATAL,
3025 "`%%substr' expects a macro identifier as first parameter");
3026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
3029 ctx = get_ctx(tline->text, false);
3031 mname = tline->text;
3032 last = tline;
3033 tline = expand_smacro(tline->next);
3034 last->next = NULL;
3036 t = tline->next;
3037 while (tok_type_(t, TOK_WHITESPACE))
3038 t = t->next;
3040 /* t should now point to the string */
3041 if (t->type != TOK_STRING) {
3042 error(ERR_NONFATAL,
3043 "`%%substr` requires string as second parameter");
3044 free_tlist(tline);
3045 free_tlist(origline);
3046 return DIRECTIVE_FOUND;
3049 tt = t->next;
3050 tptr = &tt;
3051 tokval.t_type = TOKEN_INVALID;
3052 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3053 pass, error, NULL);
3054 if (!evalresult) {
3055 free_tlist(tline);
3056 free_tlist(origline);
3057 return DIRECTIVE_FOUND;
3058 } else if (!is_simple(evalresult)) {
3059 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3060 free_tlist(tline);
3061 free_tlist(origline);
3062 return DIRECTIVE_FOUND;
3064 a1 = evalresult->value-1;
3066 while (tok_type_(tt, TOK_WHITESPACE))
3067 tt = tt->next;
3068 if (!tt) {
3069 a2 = 1; /* Backwards compatibility: one character */
3070 } else {
3071 tokval.t_type = TOKEN_INVALID;
3072 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3073 pass, error, NULL);
3074 if (!evalresult) {
3075 free_tlist(tline);
3076 free_tlist(origline);
3077 return DIRECTIVE_FOUND;
3078 } else if (!is_simple(evalresult)) {
3079 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3080 free_tlist(tline);
3081 free_tlist(origline);
3082 return DIRECTIVE_FOUND;
3084 a2 = evalresult->value;
3087 len = nasm_unquote(t->text, NULL);
3088 if (a2 < 0)
3089 a2 = a2+1+len-a1;
3090 if (a1+a2 > (int64_t)len)
3091 a2 = len-a1;
3093 macro_start = nasm_malloc(sizeof(*macro_start));
3094 macro_start->next = NULL;
3095 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3096 macro_start->type = TOK_STRING;
3097 macro_start->a.mac = NULL;
3100 * We now have a macro name, an implicit parameter count of
3101 * zero, and a numeric token to use as an expansion. Create
3102 * and store an SMacro.
3104 define_smacro(ctx, mname, casesense, 0, macro_start);
3105 free_tlist(tline);
3106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
3110 case PP_ASSIGN:
3111 case PP_IASSIGN:
3112 casesense = (i == PP_ASSIGN);
3114 tline = tline->next;
3115 skip_white_(tline);
3116 tline = expand_id(tline);
3117 if (!tline || (tline->type != TOK_ID &&
3118 (tline->type != TOK_PREPROC_ID ||
3119 tline->text[1] != '$'))) {
3120 error(ERR_NONFATAL,
3121 "`%%%sassign' expects a macro identifier",
3122 (i == PP_IASSIGN ? "i" : ""));
3123 free_tlist(origline);
3124 return DIRECTIVE_FOUND;
3126 ctx = get_ctx(tline->text, false);
3128 mname = tline->text;
3129 last = tline;
3130 tline = expand_smacro(tline->next);
3131 last->next = NULL;
3133 t = tline;
3134 tptr = &t;
3135 tokval.t_type = TOKEN_INVALID;
3136 evalresult =
3137 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3138 free_tlist(tline);
3139 if (!evalresult) {
3140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
3144 if (tokval.t_type)
3145 error(ERR_WARNING,
3146 "trailing garbage after expression ignored");
3148 if (!is_simple(evalresult)) {
3149 error(ERR_NONFATAL,
3150 "non-constant value given to `%%%sassign'",
3151 (i == PP_IASSIGN ? "i" : ""));
3152 free_tlist(origline);
3153 return DIRECTIVE_FOUND;
3156 macro_start = nasm_malloc(sizeof(*macro_start));
3157 macro_start->next = NULL;
3158 make_tok_num(macro_start, reloc_value(evalresult));
3159 macro_start->a.mac = NULL;
3162 * We now have a macro name, an implicit parameter count of
3163 * zero, and a numeric token to use as an expansion. Create
3164 * and store an SMacro.
3166 define_smacro(ctx, mname, casesense, 0, macro_start);
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3170 case PP_LINE:
3172 * Syntax is `%line nnn[+mmm] [filename]'
3174 tline = tline->next;
3175 skip_white_(tline);
3176 if (!tok_type_(tline, TOK_NUMBER)) {
3177 error(ERR_NONFATAL, "`%%line' expects line number");
3178 free_tlist(origline);
3179 return DIRECTIVE_FOUND;
3181 k = readnum(tline->text, &err);
3182 m = 1;
3183 tline = tline->next;
3184 if (tok_is_(tline, "+")) {
3185 tline = tline->next;
3186 if (!tok_type_(tline, TOK_NUMBER)) {
3187 error(ERR_NONFATAL, "`%%line' expects line increment");
3188 free_tlist(origline);
3189 return DIRECTIVE_FOUND;
3191 m = readnum(tline->text, &err);
3192 tline = tline->next;
3194 skip_white_(tline);
3195 src_set_linnum(k);
3196 istk->lineinc = m;
3197 if (tline) {
3198 nasm_free(src_set_fname(detoken(tline, false)));
3200 free_tlist(origline);
3201 return DIRECTIVE_FOUND;
3203 default:
3204 error(ERR_FATAL,
3205 "preprocessor directive `%s' not yet implemented",
3206 pp_directives[i]);
3207 return DIRECTIVE_FOUND;
3212 * Ensure that a macro parameter contains a condition code and
3213 * nothing else. Return the condition code index if so, or -1
3214 * otherwise.
3216 static int find_cc(Token * t)
3218 Token *tt;
3219 int i, j, k, m;
3221 if (!t)
3222 return -1; /* Probably a %+ without a space */
3224 skip_white_(t);
3225 if (t->type != TOK_ID)
3226 return -1;
3227 tt = t->next;
3228 skip_white_(tt);
3229 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3230 return -1;
3232 i = -1;
3233 j = elements(conditions);
3234 while (j - i > 1) {
3235 k = (j + i) / 2;
3236 m = nasm_stricmp(t->text, conditions[k]);
3237 if (m == 0) {
3238 i = k;
3239 j = -2;
3240 break;
3241 } else if (m < 0) {
3242 j = k;
3243 } else
3244 i = k;
3246 if (j != -2)
3247 return -1;
3248 return i;
3252 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3253 * %-n) and MMacro-local identifiers (%%foo).
3255 static Token *expand_mmac_params(Token * tline)
3257 Token *t, *tt, **tail, *thead;
3259 tail = &thead;
3260 thead = NULL;
3262 while (tline) {
3263 if (tline->type == TOK_PREPROC_ID &&
3264 (((tline->text[1] == '+' || tline->text[1] == '-')
3265 && tline->text[2]) || tline->text[1] == '%'
3266 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3267 char *text = NULL;
3268 int type = 0, cc; /* type = 0 to placate optimisers */
3269 char tmpbuf[30];
3270 unsigned int n;
3271 int i;
3272 MMacro *mac;
3274 t = tline;
3275 tline = tline->next;
3277 mac = istk->mstk;
3278 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3279 mac = mac->next_active;
3280 if (!mac)
3281 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3282 else
3283 switch (t->text[1]) {
3285 * We have to make a substitution of one of the
3286 * forms %1, %-1, %+1, %%foo, %0.
3288 case '0':
3289 type = TOK_NUMBER;
3290 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3291 text = nasm_strdup(tmpbuf);
3292 break;
3293 case '%':
3294 type = TOK_ID;
3295 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3296 mac->unique);
3297 text = nasm_strcat(tmpbuf, t->text + 2);
3298 break;
3299 case '-':
3300 n = atoi(t->text + 2) - 1;
3301 if (n >= mac->nparam)
3302 tt = NULL;
3303 else {
3304 if (mac->nparam > 1)
3305 n = (n + mac->rotate) % mac->nparam;
3306 tt = mac->params[n];
3308 cc = find_cc(tt);
3309 if (cc == -1) {
3310 error(ERR_NONFATAL,
3311 "macro parameter %d is not a condition code",
3312 n + 1);
3313 text = NULL;
3314 } else {
3315 type = TOK_ID;
3316 if (inverse_ccs[cc] == -1) {
3317 error(ERR_NONFATAL,
3318 "condition code `%s' is not invertible",
3319 conditions[cc]);
3320 text = NULL;
3321 } else
3322 text =
3323 nasm_strdup(conditions[inverse_ccs[cc]]);
3325 break;
3326 case '+':
3327 n = atoi(t->text + 2) - 1;
3328 if (n >= mac->nparam)
3329 tt = NULL;
3330 else {
3331 if (mac->nparam > 1)
3332 n = (n + mac->rotate) % mac->nparam;
3333 tt = mac->params[n];
3335 cc = find_cc(tt);
3336 if (cc == -1) {
3337 error(ERR_NONFATAL,
3338 "macro parameter %d is not a condition code",
3339 n + 1);
3340 text = NULL;
3341 } else {
3342 type = TOK_ID;
3343 text = nasm_strdup(conditions[cc]);
3345 break;
3346 default:
3347 n = atoi(t->text + 1) - 1;
3348 if (n >= mac->nparam)
3349 tt = NULL;
3350 else {
3351 if (mac->nparam > 1)
3352 n = (n + mac->rotate) % mac->nparam;
3353 tt = mac->params[n];
3355 if (tt) {
3356 for (i = 0; i < mac->paramlen[n]; i++) {
3357 *tail = new_Token(NULL, tt->type, tt->text, 0);
3358 tail = &(*tail)->next;
3359 tt = tt->next;
3362 text = NULL; /* we've done it here */
3363 break;
3365 if (!text) {
3366 delete_Token(t);
3367 } else {
3368 *tail = t;
3369 tail = &t->next;
3370 t->type = type;
3371 nasm_free(t->text);
3372 t->text = text;
3373 t->a.mac = NULL;
3375 continue;
3376 } else {
3377 t = *tail = tline;
3378 tline = tline->next;
3379 t->a.mac = NULL;
3380 tail = &t->next;
3383 *tail = NULL;
3384 t = thead;
3385 for (; t && (tt = t->next) != NULL; t = t->next)
3386 switch (t->type) {
3387 case TOK_WHITESPACE:
3388 if (tt->type == TOK_WHITESPACE) {
3389 t->next = delete_Token(tt);
3391 break;
3392 case TOK_ID:
3393 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3394 char *tmp = nasm_strcat(t->text, tt->text);
3395 nasm_free(t->text);
3396 t->text = tmp;
3397 t->next = delete_Token(tt);
3399 break;
3400 case TOK_NUMBER:
3401 if (tt->type == TOK_NUMBER) {
3402 char *tmp = nasm_strcat(t->text, tt->text);
3403 nasm_free(t->text);
3404 t->text = tmp;
3405 t->next = delete_Token(tt);
3407 break;
3408 default:
3409 break;
3412 return thead;
3416 * Expand all single-line macro calls made in the given line.
3417 * Return the expanded version of the line. The original is deemed
3418 * to be destroyed in the process. (In reality we'll just move
3419 * Tokens from input to output a lot of the time, rather than
3420 * actually bothering to destroy and replicate.)
3422 #define DEADMAN_LIMIT (1 << 20)
3424 static Token *expand_smacro(Token * tline)
3426 Token *t, *tt, *mstart, **tail, *thead;
3427 struct hash_table *smtbl;
3428 SMacro *head = NULL, *m;
3429 Token **params;
3430 int *paramsize;
3431 unsigned int nparam, sparam;
3432 int brackets, rescan;
3433 Token *org_tline = tline;
3434 Context *ctx;
3435 char *mname;
3436 int deadman = DEADMAN_LIMIT;
3439 * Trick: we should avoid changing the start token pointer since it can
3440 * be contained in "next" field of other token. Because of this
3441 * we allocate a copy of first token and work with it; at the end of
3442 * routine we copy it back
3444 if (org_tline) {
3445 tline =
3446 new_Token(org_tline->next, org_tline->type, org_tline->text,
3448 tline->a.mac = org_tline->a.mac;
3449 nasm_free(org_tline->text);
3450 org_tline->text = NULL;
3453 again:
3454 tail = &thead;
3455 thead = NULL;
3457 while (tline) { /* main token loop */
3458 if (!--deadman) {
3459 error(ERR_NONFATAL, "interminable macro recursion");
3460 break;
3463 if ((mname = tline->text)) {
3464 /* if this token is a local macro, look in local context */
3465 ctx = NULL;
3466 smtbl = &smacros;
3467 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3468 ctx = get_ctx(mname, true);
3469 if (ctx)
3470 smtbl = &ctx->localmac;
3472 head = (SMacro *) hash_findix(smtbl, mname);
3475 * We've hit an identifier. As in is_mmacro below, we first
3476 * check whether the identifier is a single-line macro at
3477 * all, then think about checking for parameters if
3478 * necessary.
3480 for (m = head; m; m = m->next)
3481 if (!mstrcmp(m->name, mname, m->casesense))
3482 break;
3483 if (m) {
3484 mstart = tline;
3485 params = NULL;
3486 paramsize = NULL;
3487 if (m->nparam == 0) {
3489 * Simple case: the macro is parameterless. Discard the
3490 * one token that the macro call took, and push the
3491 * expansion back on the to-do stack.
3493 if (!m->expansion) {
3494 if (!strcmp("__FILE__", m->name)) {
3495 int32_t num = 0;
3496 char *file = NULL;
3497 src_get(&num, &file);
3498 tline->text = nasm_quote(file, strlen(file));
3499 tline->type = TOK_STRING;
3500 nasm_free(file);
3501 continue;
3503 if (!strcmp("__LINE__", m->name)) {
3504 nasm_free(tline->text);
3505 make_tok_num(tline, src_get_linnum());
3506 continue;
3508 if (!strcmp("__BITS__", m->name)) {
3509 nasm_free(tline->text);
3510 make_tok_num(tline, globalbits);
3511 continue;
3513 tline = delete_Token(tline);
3514 continue;
3516 } else {
3518 * Complicated case: at least one macro with this name
3519 * exists and takes parameters. We must find the
3520 * parameters in the call, count them, find the SMacro
3521 * that corresponds to that form of the macro call, and
3522 * substitute for the parameters when we expand. What a
3523 * pain.
3525 /*tline = tline->next;
3526 skip_white_(tline); */
3527 do {
3528 t = tline->next;
3529 while (tok_type_(t, TOK_SMAC_END)) {
3530 t->a.mac->in_progress = false;
3531 t->text = NULL;
3532 t = tline->next = delete_Token(t);
3534 tline = t;
3535 } while (tok_type_(tline, TOK_WHITESPACE));
3536 if (!tok_is_(tline, "(")) {
3538 * This macro wasn't called with parameters: ignore
3539 * the call. (Behaviour borrowed from gnu cpp.)
3541 tline = mstart;
3542 m = NULL;
3543 } else {
3544 int paren = 0;
3545 int white = 0;
3546 brackets = 0;
3547 nparam = 0;
3548 sparam = PARAM_DELTA;
3549 params = nasm_malloc(sparam * sizeof(Token *));
3550 params[0] = tline->next;
3551 paramsize = nasm_malloc(sparam * sizeof(int));
3552 paramsize[0] = 0;
3553 while (true) { /* parameter loop */
3555 * For some unusual expansions
3556 * which concatenates function call
3558 t = tline->next;
3559 while (tok_type_(t, TOK_SMAC_END)) {
3560 t->a.mac->in_progress = false;
3561 t->text = NULL;
3562 t = tline->next = delete_Token(t);
3564 tline = t;
3566 if (!tline) {
3567 error(ERR_NONFATAL,
3568 "macro call expects terminating `)'");
3569 break;
3571 if (tline->type == TOK_WHITESPACE
3572 && brackets <= 0) {
3573 if (paramsize[nparam])
3574 white++;
3575 else
3576 params[nparam] = tline->next;
3577 continue; /* parameter loop */
3579 if (tline->type == TOK_OTHER
3580 && tline->text[1] == 0) {
3581 char ch = tline->text[0];
3582 if (ch == ',' && !paren && brackets <= 0) {
3583 if (++nparam >= sparam) {
3584 sparam += PARAM_DELTA;
3585 params = nasm_realloc(params,
3586 sparam *
3587 sizeof(Token
3588 *));
3589 paramsize =
3590 nasm_realloc(paramsize,
3591 sparam *
3592 sizeof(int));
3594 params[nparam] = tline->next;
3595 paramsize[nparam] = 0;
3596 white = 0;
3597 continue; /* parameter loop */
3599 if (ch == '{' &&
3600 (brackets > 0 || (brackets == 0 &&
3601 !paramsize[nparam])))
3603 if (!(brackets++)) {
3604 params[nparam] = tline->next;
3605 continue; /* parameter loop */
3608 if (ch == '}' && brackets > 0)
3609 if (--brackets == 0) {
3610 brackets = -1;
3611 continue; /* parameter loop */
3613 if (ch == '(' && !brackets)
3614 paren++;
3615 if (ch == ')' && brackets <= 0)
3616 if (--paren < 0)
3617 break;
3619 if (brackets < 0) {
3620 brackets = 0;
3621 error(ERR_NONFATAL, "braces do not "
3622 "enclose all of macro parameter");
3624 paramsize[nparam] += white + 1;
3625 white = 0;
3626 } /* parameter loop */
3627 nparam++;
3628 while (m && (m->nparam != nparam ||
3629 mstrcmp(m->name, mname,
3630 m->casesense)))
3631 m = m->next;
3632 if (!m)
3633 error(ERR_WARNING | ERR_WARN_MNP,
3634 "macro `%s' exists, "
3635 "but not taking %d parameters",
3636 mstart->text, nparam);
3639 if (m && m->in_progress)
3640 m = NULL;
3641 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3643 * Design question: should we handle !tline, which
3644 * indicates missing ')' here, or expand those
3645 * macros anyway, which requires the (t) test a few
3646 * lines down?
3648 nasm_free(params);
3649 nasm_free(paramsize);
3650 tline = mstart;
3651 } else {
3653 * Expand the macro: we are placed on the last token of the
3654 * call, so that we can easily split the call from the
3655 * following tokens. We also start by pushing an SMAC_END
3656 * token for the cycle removal.
3658 t = tline;
3659 if (t) {
3660 tline = t->next;
3661 t->next = NULL;
3663 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3664 tt->a.mac = m;
3665 m->in_progress = true;
3666 tline = tt;
3667 for (t = m->expansion; t; t = t->next) {
3668 if (t->type >= TOK_SMAC_PARAM) {
3669 Token *pcopy = tline, **ptail = &pcopy;
3670 Token *ttt, *pt;
3671 int i;
3673 ttt = params[t->type - TOK_SMAC_PARAM];
3674 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3675 --i >= 0;) {
3676 pt = *ptail =
3677 new_Token(tline, ttt->type, ttt->text,
3679 ptail = &pt->next;
3680 ttt = ttt->next;
3682 tline = pcopy;
3683 } else if (t->type == TOK_PREPROC_Q) {
3684 tt = new_Token(tline, TOK_ID, mname, 0);
3685 tline = tt;
3686 } else if (t->type == TOK_PREPROC_QQ) {
3687 tt = new_Token(tline, TOK_ID, m->name, 0);
3688 tline = tt;
3689 } else {
3690 tt = new_Token(tline, t->type, t->text, 0);
3691 tline = tt;
3696 * Having done that, get rid of the macro call, and clean
3697 * up the parameters.
3699 nasm_free(params);
3700 nasm_free(paramsize);
3701 free_tlist(mstart);
3702 continue; /* main token loop */
3707 if (tline->type == TOK_SMAC_END) {
3708 tline->a.mac->in_progress = false;
3709 tline = delete_Token(tline);
3710 } else {
3711 t = *tail = tline;
3712 tline = tline->next;
3713 t->a.mac = NULL;
3714 t->next = NULL;
3715 tail = &t->next;
3720 * Now scan the entire line and look for successive TOK_IDs that resulted
3721 * after expansion (they can't be produced by tokenize()). The successive
3722 * TOK_IDs should be concatenated.
3723 * Also we look for %+ tokens and concatenate the tokens before and after
3724 * them (without white spaces in between).
3726 t = thead;
3727 rescan = 0;
3728 while (t) {
3729 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3730 t = t->next;
3731 if (!t || !t->next)
3732 break;
3733 if (t->next->type == TOK_ID ||
3734 t->next->type == TOK_PREPROC_ID ||
3735 t->next->type == TOK_NUMBER) {
3736 char *p = nasm_strcat(t->text, t->next->text);
3737 nasm_free(t->text);
3738 t->next = delete_Token(t->next);
3739 t->text = p;
3740 rescan = 1;
3741 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3742 t->next->next->type == TOK_PREPROC_ID &&
3743 strcmp(t->next->next->text, "%+") == 0) {
3744 /* free the next whitespace, the %+ token and next whitespace */
3745 int i;
3746 for (i = 1; i <= 3; i++) {
3747 if (!t->next
3748 || (i != 2 && t->next->type != TOK_WHITESPACE))
3749 break;
3750 t->next = delete_Token(t->next);
3751 } /* endfor */
3752 } else
3753 t = t->next;
3755 /* If we concatenaded something, re-scan the line for macros */
3756 if (rescan) {
3757 tline = thead;
3758 goto again;
3761 if (org_tline) {
3762 if (thead) {
3763 *org_tline = *thead;
3764 /* since we just gave text to org_line, don't free it */
3765 thead->text = NULL;
3766 delete_Token(thead);
3767 } else {
3768 /* the expression expanded to empty line;
3769 we can't return NULL for some reasons
3770 we just set the line to a single WHITESPACE token. */
3771 memset(org_tline, 0, sizeof(*org_tline));
3772 org_tline->text = NULL;
3773 org_tline->type = TOK_WHITESPACE;
3775 thead = org_tline;
3778 return thead;
3782 * Similar to expand_smacro but used exclusively with macro identifiers
3783 * right before they are fetched in. The reason is that there can be
3784 * identifiers consisting of several subparts. We consider that if there
3785 * are more than one element forming the name, user wants a expansion,
3786 * otherwise it will be left as-is. Example:
3788 * %define %$abc cde
3790 * the identifier %$abc will be left as-is so that the handler for %define
3791 * will suck it and define the corresponding value. Other case:
3793 * %define _%$abc cde
3795 * In this case user wants name to be expanded *before* %define starts
3796 * working, so we'll expand %$abc into something (if it has a value;
3797 * otherwise it will be left as-is) then concatenate all successive
3798 * PP_IDs into one.
3800 static Token *expand_id(Token * tline)
3802 Token *cur, *oldnext = NULL;
3804 if (!tline || !tline->next)
3805 return tline;
3807 cur = tline;
3808 while (cur->next &&
3809 (cur->next->type == TOK_ID ||
3810 cur->next->type == TOK_PREPROC_ID
3811 || cur->next->type == TOK_NUMBER))
3812 cur = cur->next;
3814 /* If identifier consists of just one token, don't expand */
3815 if (cur == tline)
3816 return tline;
3818 if (cur) {
3819 oldnext = cur->next; /* Detach the tail past identifier */
3820 cur->next = NULL; /* so that expand_smacro stops here */
3823 tline = expand_smacro(tline);
3825 if (cur) {
3826 /* expand_smacro possibly changhed tline; re-scan for EOL */
3827 cur = tline;
3828 while (cur && cur->next)
3829 cur = cur->next;
3830 if (cur)
3831 cur->next = oldnext;
3834 return tline;
3838 * Determine whether the given line constitutes a multi-line macro
3839 * call, and return the MMacro structure called if so. Doesn't have
3840 * to check for an initial label - that's taken care of in
3841 * expand_mmacro - but must check numbers of parameters. Guaranteed
3842 * to be called with tline->type == TOK_ID, so the putative macro
3843 * name is easy to find.
3845 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3847 MMacro *head, *m;
3848 Token **params;
3849 int nparam;
3851 head = (MMacro *) hash_findix(&mmacros, tline->text);
3854 * Efficiency: first we see if any macro exists with the given
3855 * name. If not, we can return NULL immediately. _Then_ we
3856 * count the parameters, and then we look further along the
3857 * list if necessary to find the proper MMacro.
3859 for (m = head; m; m = m->next)
3860 if (!mstrcmp(m->name, tline->text, m->casesense))
3861 break;
3862 if (!m)
3863 return NULL;
3866 * OK, we have a potential macro. Count and demarcate the
3867 * parameters.
3869 count_mmac_params(tline->next, &nparam, &params);
3872 * So we know how many parameters we've got. Find the MMacro
3873 * structure that handles this number.
3875 while (m) {
3876 if (m->nparam_min <= nparam
3877 && (m->plus || nparam <= m->nparam_max)) {
3879 * This one is right. Just check if cycle removal
3880 * prohibits us using it before we actually celebrate...
3882 if (m->in_progress) {
3883 #if 0
3884 error(ERR_NONFATAL,
3885 "self-reference in multi-line macro `%s'", m->name);
3886 #endif
3887 nasm_free(params);
3888 return NULL;
3891 * It's right, and we can use it. Add its default
3892 * parameters to the end of our list if necessary.
3894 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3895 params =
3896 nasm_realloc(params,
3897 ((m->nparam_min + m->ndefs +
3898 1) * sizeof(*params)));
3899 while (nparam < m->nparam_min + m->ndefs) {
3900 params[nparam] = m->defaults[nparam - m->nparam_min];
3901 nparam++;
3905 * If we've gone over the maximum parameter count (and
3906 * we're in Plus mode), ignore parameters beyond
3907 * nparam_max.
3909 if (m->plus && nparam > m->nparam_max)
3910 nparam = m->nparam_max;
3912 * Then terminate the parameter list, and leave.
3914 if (!params) { /* need this special case */
3915 params = nasm_malloc(sizeof(*params));
3916 nparam = 0;
3918 params[nparam] = NULL;
3919 *params_array = params;
3920 return m;
3923 * This one wasn't right: look for the next one with the
3924 * same name.
3926 for (m = m->next; m; m = m->next)
3927 if (!mstrcmp(m->name, tline->text, m->casesense))
3928 break;
3932 * After all that, we didn't find one with the right number of
3933 * parameters. Issue a warning, and fail to expand the macro.
3935 error(ERR_WARNING | ERR_WARN_MNP,
3936 "macro `%s' exists, but not taking %d parameters",
3937 tline->text, nparam);
3938 nasm_free(params);
3939 return NULL;
3943 * Expand the multi-line macro call made by the given line, if
3944 * there is one to be expanded. If there is, push the expansion on
3945 * istk->expansion and return 1. Otherwise return 0.
3947 static int expand_mmacro(Token * tline)
3949 Token *startline = tline;
3950 Token *label = NULL;
3951 int dont_prepend = 0;
3952 Token **params, *t, *mtok, *tt;
3953 MMacro *m;
3954 Line *l, *ll;
3955 int i, nparam, *paramlen;
3956 const char *mname;
3958 t = tline;
3959 skip_white_(t);
3960 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3961 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3962 return 0;
3963 mtok = t;
3964 m = is_mmacro(t, &params);
3965 if (m) {
3966 mname = t->text;
3967 } else {
3968 Token *last;
3970 * We have an id which isn't a macro call. We'll assume
3971 * it might be a label; we'll also check to see if a
3972 * colon follows it. Then, if there's another id after
3973 * that lot, we'll check it again for macro-hood.
3975 label = last = t;
3976 t = t->next;
3977 if (tok_type_(t, TOK_WHITESPACE))
3978 last = t, t = t->next;
3979 if (tok_is_(t, ":")) {
3980 dont_prepend = 1;
3981 last = t, t = t->next;
3982 if (tok_type_(t, TOK_WHITESPACE))
3983 last = t, t = t->next;
3985 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3986 return 0;
3987 last->next = NULL;
3988 mname = t->text;
3989 tline = t;
3993 * Fix up the parameters: this involves stripping leading and
3994 * trailing whitespace, then stripping braces if they are
3995 * present.
3997 for (nparam = 0; params[nparam]; nparam++) ;
3998 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4000 for (i = 0; params[i]; i++) {
4001 int brace = false;
4002 int comma = (!m->plus || i < nparam - 1);
4004 t = params[i];
4005 skip_white_(t);
4006 if (tok_is_(t, "{"))
4007 t = t->next, brace = true, comma = false;
4008 params[i] = t;
4009 paramlen[i] = 0;
4010 while (t) {
4011 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4012 break; /* ... because we have hit a comma */
4013 if (comma && t->type == TOK_WHITESPACE
4014 && tok_is_(t->next, ","))
4015 break; /* ... or a space then a comma */
4016 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4017 break; /* ... or a brace */
4018 t = t->next;
4019 paramlen[i]++;
4024 * OK, we have a MMacro structure together with a set of
4025 * parameters. We must now go through the expansion and push
4026 * copies of each Line on to istk->expansion. Substitution of
4027 * parameter tokens and macro-local tokens doesn't get done
4028 * until the single-line macro substitution process; this is
4029 * because delaying them allows us to change the semantics
4030 * later through %rotate.
4032 * First, push an end marker on to istk->expansion, mark this
4033 * macro as in progress, and set up its invocation-specific
4034 * variables.
4036 ll = nasm_malloc(sizeof(Line));
4037 ll->next = istk->expansion;
4038 ll->finishes = m;
4039 ll->first = NULL;
4040 istk->expansion = ll;
4042 m->in_progress = true;
4043 m->params = params;
4044 m->iline = tline;
4045 m->nparam = nparam;
4046 m->rotate = 0;
4047 m->paramlen = paramlen;
4048 m->unique = unique++;
4049 m->lineno = 0;
4051 m->next_active = istk->mstk;
4052 istk->mstk = m;
4054 for (l = m->expansion; l; l = l->next) {
4055 Token **tail;
4057 ll = nasm_malloc(sizeof(Line));
4058 ll->finishes = NULL;
4059 ll->next = istk->expansion;
4060 istk->expansion = ll;
4061 tail = &ll->first;
4063 for (t = l->first; t; t = t->next) {
4064 Token *x = t;
4065 switch (t->type) {
4066 case TOK_PREPROC_Q:
4067 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4068 break;
4069 case TOK_PREPROC_QQ:
4070 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4071 break;
4072 case TOK_PREPROC_ID:
4073 if (t->text[1] == '0' && t->text[2] == '0') {
4074 dont_prepend = -1;
4075 x = label;
4076 if (!x)
4077 continue;
4079 /* fall through */
4080 default:
4081 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4082 break;
4084 tail = &tt->next;
4086 *tail = NULL;
4090 * If we had a label, push it on as the first line of
4091 * the macro expansion.
4093 if (label) {
4094 if (dont_prepend < 0)
4095 free_tlist(startline);
4096 else {
4097 ll = nasm_malloc(sizeof(Line));
4098 ll->finishes = NULL;
4099 ll->next = istk->expansion;
4100 istk->expansion = ll;
4101 ll->first = startline;
4102 if (!dont_prepend) {
4103 while (label->next)
4104 label = label->next;
4105 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4110 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4112 return 1;
4116 * Since preprocessor always operate only on the line that didn't
4117 * arrived yet, we should always use ERR_OFFBY1. Also since user
4118 * won't want to see same error twice (preprocessing is done once
4119 * per pass) we will want to show errors only during pass one.
4121 static void error(int severity, const char *fmt, ...)
4123 va_list arg;
4124 char buff[1024];
4126 /* If we're in a dead branch of IF or something like it, ignore the error */
4127 if (istk && istk->conds && !emitting(istk->conds->state))
4128 return;
4130 va_start(arg, fmt);
4131 vsnprintf(buff, sizeof(buff), fmt, arg);
4132 va_end(arg);
4134 if (istk && istk->mstk && istk->mstk->name)
4135 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
4136 istk->mstk->lineno, buff);
4137 else
4138 _error(severity | ERR_PASS1, "%s", buff);
4141 static void
4142 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4143 ListGen * listgen, StrList **deplist)
4145 _error = errfunc;
4146 cstk = NULL;
4147 istk = nasm_malloc(sizeof(Include));
4148 istk->next = NULL;
4149 istk->conds = NULL;
4150 istk->expansion = NULL;
4151 istk->mstk = NULL;
4152 istk->fp = fopen(file, "r");
4153 istk->fname = NULL;
4154 src_set_fname(nasm_strdup(file));
4155 src_set_linnum(0);
4156 istk->lineinc = 1;
4157 if (!istk->fp)
4158 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4159 file);
4160 defining = NULL;
4161 nested_mac_count = 0;
4162 nested_rep_count = 0;
4163 init_macros();
4164 unique = 0;
4165 if (tasm_compatible_mode) {
4166 stdmacpos = nasm_stdmac;
4167 } else {
4168 stdmacpos = nasm_stdmac_after_tasm;
4170 any_extrastdmac = extrastdmac && *extrastdmac;
4171 do_predef = true;
4172 list = listgen;
4173 evaluate = eval;
4174 pass = apass;
4175 dephead = deptail = deplist;
4176 if (deplist) {
4177 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4178 sl->next = NULL;
4179 strcpy(sl->str, file);
4180 *deptail = sl;
4181 deptail = &sl->next;
4185 static char *pp_getline(void)
4187 char *line;
4188 Token *tline;
4190 while (1) {
4192 * Fetch a tokenized line, either from the macro-expansion
4193 * buffer or from the input file.
4195 tline = NULL;
4196 while (istk->expansion && istk->expansion->finishes) {
4197 Line *l = istk->expansion;
4198 if (!l->finishes->name && l->finishes->in_progress > 1) {
4199 Line *ll;
4202 * This is a macro-end marker for a macro with no
4203 * name, which means it's not really a macro at all
4204 * but a %rep block, and the `in_progress' field is
4205 * more than 1, meaning that we still need to
4206 * repeat. (1 means the natural last repetition; 0
4207 * means termination by %exitrep.) We have
4208 * therefore expanded up to the %endrep, and must
4209 * push the whole block on to the expansion buffer
4210 * again. We don't bother to remove the macro-end
4211 * marker: we'd only have to generate another one
4212 * if we did.
4214 l->finishes->in_progress--;
4215 for (l = l->finishes->expansion; l; l = l->next) {
4216 Token *t, *tt, **tail;
4218 ll = nasm_malloc(sizeof(Line));
4219 ll->next = istk->expansion;
4220 ll->finishes = NULL;
4221 ll->first = NULL;
4222 tail = &ll->first;
4224 for (t = l->first; t; t = t->next) {
4225 if (t->text || t->type == TOK_WHITESPACE) {
4226 tt = *tail =
4227 new_Token(NULL, t->type, t->text, 0);
4228 tail = &tt->next;
4232 istk->expansion = ll;
4234 } else {
4236 * Check whether a `%rep' was started and not ended
4237 * within this macro expansion. This can happen and
4238 * should be detected. It's a fatal error because
4239 * I'm too confused to work out how to recover
4240 * sensibly from it.
4242 if (defining) {
4243 if (defining->name)
4244 error(ERR_PANIC,
4245 "defining with name in expansion");
4246 else if (istk->mstk->name)
4247 error(ERR_FATAL,
4248 "`%%rep' without `%%endrep' within"
4249 " expansion of macro `%s'",
4250 istk->mstk->name);
4254 * FIXME: investigate the relationship at this point between
4255 * istk->mstk and l->finishes
4258 MMacro *m = istk->mstk;
4259 istk->mstk = m->next_active;
4260 if (m->name) {
4262 * This was a real macro call, not a %rep, and
4263 * therefore the parameter information needs to
4264 * be freed.
4266 nasm_free(m->params);
4267 free_tlist(m->iline);
4268 nasm_free(m->paramlen);
4269 l->finishes->in_progress = false;
4270 } else
4271 free_mmacro(m);
4273 istk->expansion = l->next;
4274 nasm_free(l);
4275 list->downlevel(LIST_MACRO);
4278 while (1) { /* until we get a line we can use */
4280 if (istk->expansion) { /* from a macro expansion */
4281 char *p;
4282 Line *l = istk->expansion;
4283 if (istk->mstk)
4284 istk->mstk->lineno++;
4285 tline = l->first;
4286 istk->expansion = l->next;
4287 nasm_free(l);
4288 p = detoken(tline, false);
4289 list->line(LIST_MACRO, p);
4290 nasm_free(p);
4291 break;
4293 line = read_line();
4294 if (line) { /* from the current input file */
4295 line = prepreproc(line);
4296 tline = tokenize(line);
4297 nasm_free(line);
4298 break;
4301 * The current file has ended; work down the istk
4304 Include *i = istk;
4305 fclose(i->fp);
4306 if (i->conds)
4307 error(ERR_FATAL,
4308 "expected `%%endif' before end of file");
4309 /* only set line and file name if there's a next node */
4310 if (i->next) {
4311 src_set_linnum(i->lineno);
4312 nasm_free(src_set_fname(i->fname));
4314 istk = i->next;
4315 list->downlevel(LIST_INCLUDE);
4316 nasm_free(i);
4317 if (!istk)
4318 return NULL;
4323 * We must expand MMacro parameters and MMacro-local labels
4324 * _before_ we plunge into directive processing, to cope
4325 * with things like `%define something %1' such as STRUC
4326 * uses. Unless we're _defining_ a MMacro, in which case
4327 * those tokens should be left alone to go into the
4328 * definition; and unless we're in a non-emitting
4329 * condition, in which case we don't want to meddle with
4330 * anything.
4332 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4333 && !(istk->mstk && !istk->mstk->in_progress))
4334 tline = expand_mmac_params(tline);
4337 * Check the line to see if it's a preprocessor directive.
4339 if (do_directive(tline) == DIRECTIVE_FOUND) {
4340 continue;
4341 } else if (defining) {
4343 * We're defining a multi-line macro. We emit nothing
4344 * at all, and just
4345 * shove the tokenized line on to the macro definition.
4347 Line *l = nasm_malloc(sizeof(Line));
4348 l->next = defining->expansion;
4349 l->first = tline;
4350 l->finishes = NULL;
4351 defining->expansion = l;
4352 continue;
4353 } else if (istk->conds && !emitting(istk->conds->state)) {
4355 * We're in a non-emitting branch of a condition block.
4356 * Emit nothing at all, not even a blank line: when we
4357 * emerge from the condition we'll give a line-number
4358 * directive so we keep our place correctly.
4360 free_tlist(tline);
4361 continue;
4362 } else if (istk->mstk && !istk->mstk->in_progress) {
4364 * We're in a %rep block which has been terminated, so
4365 * we're walking through to the %endrep without
4366 * emitting anything. Emit nothing at all, not even a
4367 * blank line: when we emerge from the %rep block we'll
4368 * give a line-number directive so we keep our place
4369 * correctly.
4371 free_tlist(tline);
4372 continue;
4373 } else {
4374 tline = expand_smacro(tline);
4375 if (!expand_mmacro(tline)) {
4377 * De-tokenize the line again, and emit it.
4379 line = detoken(tline, true);
4380 free_tlist(tline);
4381 break;
4382 } else {
4383 continue; /* expand_mmacro calls free_tlist */
4388 return line;
4391 static void pp_cleanup(int pass)
4393 if (defining) {
4394 if(defining->name) {
4395 error(ERR_NONFATAL,
4396 "end of file while still defining macro `%s'",
4397 defining->name);
4398 } else {
4399 error(ERR_NONFATAL, "end of file while still in %%rep");
4402 free_mmacro(defining);
4404 while (cstk)
4405 ctx_pop();
4406 free_macros();
4407 while (istk) {
4408 Include *i = istk;
4409 istk = istk->next;
4410 fclose(i->fp);
4411 nasm_free(i->fname);
4412 nasm_free(i);
4414 while (cstk)
4415 ctx_pop();
4416 nasm_free(src_set_fname(NULL));
4417 if (pass == 0) {
4418 IncPath *i;
4419 free_llist(predef);
4420 delete_Blocks();
4421 while ((i = ipath)) {
4422 ipath = i->next;
4423 if (i->path)
4424 nasm_free(i->path);
4425 nasm_free(i);
4430 void pp_include_path(char *path)
4432 IncPath *i;
4434 i = nasm_malloc(sizeof(IncPath));
4435 i->path = path ? nasm_strdup(path) : NULL;
4436 i->next = NULL;
4438 if (ipath != NULL) {
4439 IncPath *j = ipath;
4440 while (j->next != NULL)
4441 j = j->next;
4442 j->next = i;
4443 } else {
4444 ipath = i;
4448 void pp_pre_include(char *fname)
4450 Token *inc, *space, *name;
4451 Line *l;
4453 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4454 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4455 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4457 l = nasm_malloc(sizeof(Line));
4458 l->next = predef;
4459 l->first = inc;
4460 l->finishes = NULL;
4461 predef = l;
4464 void pp_pre_define(char *definition)
4466 Token *def, *space;
4467 Line *l;
4468 char *equals;
4470 equals = strchr(definition, '=');
4471 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4472 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4473 if (equals)
4474 *equals = ' ';
4475 space->next = tokenize(definition);
4476 if (equals)
4477 *equals = '=';
4479 l = nasm_malloc(sizeof(Line));
4480 l->next = predef;
4481 l->first = def;
4482 l->finishes = NULL;
4483 predef = l;
4486 void pp_pre_undefine(char *definition)
4488 Token *def, *space;
4489 Line *l;
4491 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4492 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4493 space->next = tokenize(definition);
4495 l = nasm_malloc(sizeof(Line));
4496 l->next = predef;
4497 l->first = def;
4498 l->finishes = NULL;
4499 predef = l;
4503 * Added by Keith Kanios:
4505 * This function is used to assist with "runtime" preprocessor
4506 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4508 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4509 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4512 void pp_runtime(char *definition)
4514 Token *def;
4516 def = tokenize(definition);
4517 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4518 free_tlist(def);
4522 void pp_extra_stdmac(macros_t *macros)
4524 extrastdmac = macros;
4527 static void make_tok_num(Token * tok, int64_t val)
4529 char numbuf[20];
4530 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4531 tok->text = nasm_strdup(numbuf);
4532 tok->type = TOK_NUMBER;
4535 Preproc nasmpp = {
4536 pp_reset,
4537 pp_getline,
4538 pp_cleanup