macros.pl: be a bit more careful with open()
[nasm.git] / preproc.c
blob99e89289a3835e88c43468ff541c1072beed1aa9
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 /* +1: we need space for the final NULL */
1438 if (*nparam+1 >= paramsize) {
1439 paramsize += PARAM_DELTA;
1440 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1442 skip_white_(t);
1443 brace = false;
1444 if (tok_is_(t, "{"))
1445 brace = true;
1446 (*params)[(*nparam)++] = t;
1447 while (tok_isnt_(t, brace ? "}" : ","))
1448 t = t->next;
1449 if (t) { /* got a comma/brace */
1450 t = t->next;
1451 if (brace) {
1453 * Now we've found the closing brace, look further
1454 * for the comma.
1456 skip_white_(t);
1457 if (tok_isnt_(t, ",")) {
1458 error(ERR_NONFATAL,
1459 "braces do not enclose all of macro parameter");
1460 while (tok_isnt_(t, ","))
1461 t = t->next;
1463 if (t)
1464 t = t->next; /* eat the comma */
1471 * Determine whether one of the various `if' conditions is true or
1472 * not.
1474 * We must free the tline we get passed.
1476 static bool if_condition(Token * tline, enum preproc_token ct)
1478 enum pp_conditional i = PP_COND(ct);
1479 bool j;
1480 Token *t, *tt, **tptr, *origline;
1481 struct tokenval tokval;
1482 expr *evalresult;
1483 enum pp_token_type needtype;
1485 origline = tline;
1487 switch (i) {
1488 case PPC_IFCTX:
1489 j = false; /* have we matched yet? */
1490 while (true) {
1491 skip_white_(tline);
1492 if (!tline)
1493 break;
1494 if (tline->type != TOK_ID) {
1495 error(ERR_NONFATAL,
1496 "`%s' expects context identifiers", pp_directives[ct]);
1497 free_tlist(origline);
1498 return -1;
1500 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1501 j = true;
1502 tline = tline->next;
1504 break;
1506 case PPC_IFDEF:
1507 j = false; /* have we matched yet? */
1508 while (tline) {
1509 skip_white_(tline);
1510 if (!tline || (tline->type != TOK_ID &&
1511 (tline->type != TOK_PREPROC_ID ||
1512 tline->text[1] != '$'))) {
1513 error(ERR_NONFATAL,
1514 "`%s' expects macro identifiers", pp_directives[ct]);
1515 goto fail;
1517 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1518 j = true;
1519 tline = tline->next;
1521 break;
1523 case PPC_IFIDN:
1524 case PPC_IFIDNI:
1525 tline = expand_smacro(tline);
1526 t = tt = tline;
1527 while (tok_isnt_(tt, ","))
1528 tt = tt->next;
1529 if (!tt) {
1530 error(ERR_NONFATAL,
1531 "`%s' expects two comma-separated arguments",
1532 pp_directives[ct]);
1533 goto fail;
1535 tt = tt->next;
1536 j = true; /* assume equality unless proved not */
1537 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1538 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1539 error(ERR_NONFATAL, "`%s': more than one comma on line",
1540 pp_directives[ct]);
1541 goto fail;
1543 if (t->type == TOK_WHITESPACE) {
1544 t = t->next;
1545 continue;
1547 if (tt->type == TOK_WHITESPACE) {
1548 tt = tt->next;
1549 continue;
1551 if (tt->type != t->type) {
1552 j = false; /* found mismatching tokens */
1553 break;
1555 /* When comparing strings, need to unquote them first */
1556 if (t->type == TOK_STRING) {
1557 size_t l1 = nasm_unquote(t->text, NULL);
1558 size_t l2 = nasm_unquote(tt->text, NULL);
1560 if (l1 != l2) {
1561 j = false;
1562 break;
1564 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1565 j = false;
1566 break;
1568 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1569 j = false; /* found mismatching tokens */
1570 break;
1573 t = t->next;
1574 tt = tt->next;
1576 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1577 j = false; /* trailing gunk on one end or other */
1578 break;
1580 case PPC_IFMACRO:
1582 bool found = false;
1583 MMacro searching, *mmac;
1585 tline = tline->next;
1586 skip_white_(tline);
1587 tline = expand_id(tline);
1588 if (!tok_type_(tline, TOK_ID)) {
1589 error(ERR_NONFATAL,
1590 "`%s' expects a macro name", pp_directives[ct]);
1591 goto fail;
1593 searching.name = nasm_strdup(tline->text);
1594 searching.casesense = true;
1595 searching.plus = false;
1596 searching.nolist = false;
1597 searching.in_progress = 0;
1598 searching.rep_nest = NULL;
1599 searching.nparam_min = 0;
1600 searching.nparam_max = INT_MAX;
1601 tline = expand_smacro(tline->next);
1602 skip_white_(tline);
1603 if (!tline) {
1604 } else if (!tok_type_(tline, TOK_NUMBER)) {
1605 error(ERR_NONFATAL,
1606 "`%s' expects a parameter count or nothing",
1607 pp_directives[ct]);
1608 } else {
1609 searching.nparam_min = searching.nparam_max =
1610 readnum(tline->text, &j);
1611 if (j)
1612 error(ERR_NONFATAL,
1613 "unable to parse parameter count `%s'",
1614 tline->text);
1616 if (tline && tok_is_(tline->next, "-")) {
1617 tline = tline->next->next;
1618 if (tok_is_(tline, "*"))
1619 searching.nparam_max = INT_MAX;
1620 else if (!tok_type_(tline, TOK_NUMBER))
1621 error(ERR_NONFATAL,
1622 "`%s' expects a parameter count after `-'",
1623 pp_directives[ct]);
1624 else {
1625 searching.nparam_max = readnum(tline->text, &j);
1626 if (j)
1627 error(ERR_NONFATAL,
1628 "unable to parse parameter count `%s'",
1629 tline->text);
1630 if (searching.nparam_min > searching.nparam_max)
1631 error(ERR_NONFATAL,
1632 "minimum parameter count exceeds maximum");
1635 if (tline && tok_is_(tline->next, "+")) {
1636 tline = tline->next;
1637 searching.plus = true;
1639 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1640 while (mmac) {
1641 if (!strcmp(mmac->name, searching.name) &&
1642 (mmac->nparam_min <= searching.nparam_max
1643 || searching.plus)
1644 && (searching.nparam_min <= mmac->nparam_max
1645 || mmac->plus)) {
1646 found = true;
1647 break;
1649 mmac = mmac->next;
1651 if(tline && tline->next)
1652 error(ERR_WARNING, "trailing garbage after %%ifmacro ignored");
1653 nasm_free(searching.name);
1654 j = found;
1655 break;
1658 case PPC_IFID:
1659 needtype = TOK_ID;
1660 goto iftype;
1661 case PPC_IFNUM:
1662 needtype = TOK_NUMBER;
1663 goto iftype;
1664 case PPC_IFSTR:
1665 needtype = TOK_STRING;
1666 goto iftype;
1668 iftype:
1669 t = tline = expand_smacro(tline);
1671 while (tok_type_(t, TOK_WHITESPACE) ||
1672 (needtype == TOK_NUMBER &&
1673 tok_type_(t, TOK_OTHER) &&
1674 (t->text[0] == '-' || t->text[0] == '+') &&
1675 !t->text[1]))
1676 t = t->next;
1678 j = tok_type_(t, needtype);
1679 break;
1681 case PPC_IFTOKEN:
1682 t = tline = expand_smacro(tline);
1683 while (tok_type_(t, TOK_WHITESPACE))
1684 t = t->next;
1686 j = false;
1687 if (t) {
1688 t = t->next; /* Skip the actual token */
1689 while (tok_type_(t, TOK_WHITESPACE))
1690 t = t->next;
1691 j = !t; /* Should be nothing left */
1693 break;
1695 case PPC_IFEMPTY:
1696 t = tline = expand_smacro(tline);
1697 while (tok_type_(t, TOK_WHITESPACE))
1698 t = t->next;
1700 j = !t; /* Should be empty */
1701 break;
1703 case PPC_IF:
1704 t = tline = expand_smacro(tline);
1705 tptr = &t;
1706 tokval.t_type = TOKEN_INVALID;
1707 evalresult = evaluate(ppscan, tptr, &tokval,
1708 NULL, pass | CRITICAL, error, NULL);
1709 if (!evalresult)
1710 return -1;
1711 if (tokval.t_type)
1712 error(ERR_WARNING,
1713 "trailing garbage after expression ignored");
1714 if (!is_simple(evalresult)) {
1715 error(ERR_NONFATAL,
1716 "non-constant value given to `%s'", pp_directives[ct]);
1717 goto fail;
1719 j = reloc_value(evalresult) != 0;
1720 break;
1722 default:
1723 error(ERR_FATAL,
1724 "preprocessor directive `%s' not yet implemented",
1725 pp_directives[ct]);
1726 goto fail;
1729 free_tlist(origline);
1730 return j ^ PP_NEGATIVE(ct);
1732 fail:
1733 free_tlist(origline);
1734 return -1;
1738 * Common code for defining an smacro
1740 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1741 int nparam, Token *expansion)
1743 SMacro *smac, **smhead;
1744 struct hash_table *smtbl;
1746 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1747 if (!smac) {
1748 error(ERR_WARNING,
1749 "single-line macro `%s' defined both with and"
1750 " without parameters", mname);
1752 /* Some instances of the old code considered this a failure,
1753 some others didn't. What is the right thing to do here? */
1754 free_tlist(expansion);
1755 return false; /* Failure */
1756 } else {
1758 * We're redefining, so we have to take over an
1759 * existing SMacro structure. This means freeing
1760 * what was already in it.
1762 nasm_free(smac->name);
1763 free_tlist(smac->expansion);
1765 } else {
1766 smtbl = ctx ? &ctx->localmac : &smacros;
1767 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1768 smac = nasm_malloc(sizeof(SMacro));
1769 smac->next = *smhead;
1770 *smhead = smac;
1772 smac->name = nasm_strdup(mname);
1773 smac->casesense = casesense;
1774 smac->nparam = nparam;
1775 smac->expansion = expansion;
1776 smac->in_progress = false;
1777 return true; /* Success */
1781 * Undefine an smacro
1783 static void undef_smacro(Context *ctx, const char *mname)
1785 SMacro **smhead, *s, **sp;
1786 struct hash_table *smtbl;
1788 smtbl = ctx ? &ctx->localmac : &smacros;
1789 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1791 if (smhead) {
1793 * We now have a macro name... go hunt for it.
1795 sp = smhead;
1796 while ((s = *sp) != NULL) {
1797 if (!mstrcmp(s->name, mname, s->casesense)) {
1798 *sp = s->next;
1799 nasm_free(s->name);
1800 free_tlist(s->expansion);
1801 nasm_free(s);
1802 } else {
1803 sp = &s->next;
1810 * Parse a mmacro specification.
1812 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1814 bool err;
1816 tline = tline->next;
1817 skip_white_(tline);
1818 tline = expand_id(tline);
1819 if (!tok_type_(tline, TOK_ID)) {
1820 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1821 return false;
1824 def->name = nasm_strdup(tline->text);
1825 def->plus = false;
1826 def->nolist = false;
1827 def->in_progress = 0;
1828 def->rep_nest = NULL;
1829 def->nparam_min = 0;
1830 def->nparam_max = 0;
1832 tline = expand_smacro(tline->next);
1833 skip_white_(tline);
1834 if (!tok_type_(tline, TOK_NUMBER)) {
1835 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1836 } else {
1837 def->nparam_min = def->nparam_max =
1838 readnum(tline->text, &err);
1839 if (err)
1840 error(ERR_NONFATAL,
1841 "unable to parse parameter count `%s'", tline->text);
1843 if (tline && tok_is_(tline->next, "-")) {
1844 tline = tline->next->next;
1845 if (tok_is_(tline, "*")) {
1846 def->nparam_max = INT_MAX;
1847 } else if (!tok_type_(tline, TOK_NUMBER)) {
1848 error(ERR_NONFATAL,
1849 "`%s' expects a parameter count after `-'", directive);
1850 } else {
1851 def->nparam_max = readnum(tline->text, &err);
1852 if (err) {
1853 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1854 tline->text);
1856 if (def->nparam_min > def->nparam_max) {
1857 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1861 if (tline && tok_is_(tline->next, "+")) {
1862 tline = tline->next;
1863 def->plus = true;
1865 if (tline && tok_type_(tline->next, TOK_ID) &&
1866 !nasm_stricmp(tline->next->text, ".nolist")) {
1867 tline = tline->next;
1868 def->nolist = true;
1872 * Handle default parameters.
1874 if (tline && tline->next) {
1875 def->dlist = tline->next;
1876 tline->next = NULL;
1877 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1878 } else {
1879 def->dlist = NULL;
1880 def->defaults = NULL;
1882 def->expansion = NULL;
1884 if(def->defaults &&
1885 def->ndefs > def->nparam_max - def->nparam_min &&
1886 !def->plus)
1887 error(ERR_WARNING | ERR_WARN_MDP, "too many default macro parameters");
1889 return true;
1894 * Decode a size directive
1896 static int parse_size(const char *str) {
1897 static const char *size_names[] =
1898 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1899 static const int sizes[] =
1900 { 0, 1, 4, 16, 8, 10, 2, 32 };
1902 return sizes[bsii(str, size_names, elements(size_names))+1];
1906 * find and process preprocessor directive in passed line
1907 * Find out if a line contains a preprocessor directive, and deal
1908 * with it if so.
1910 * If a directive _is_ found, it is the responsibility of this routine
1911 * (and not the caller) to free_tlist() the line.
1913 * @param tline a pointer to the current tokeninzed line linked list
1914 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1917 static int do_directive(Token * tline)
1919 enum preproc_token i;
1920 int j;
1921 bool err;
1922 int nparam;
1923 bool nolist;
1924 bool casesense;
1925 int k, m;
1926 int offset;
1927 char *p, *pp, *mname;
1928 Include *inc;
1929 Context *ctx;
1930 Cond *cond;
1931 MMacro *mmac, **mmhead;
1932 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1933 Line *l;
1934 struct tokenval tokval;
1935 expr *evalresult;
1936 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1937 int64_t count;
1938 size_t len;
1940 origline = tline;
1942 skip_white_(tline);
1943 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
1944 (tline->text[1] == '%' || tline->text[1] == '$'
1945 || tline->text[1] == '!'))
1946 return NO_DIRECTIVE_FOUND;
1948 i = pp_token_hash(tline->text);
1951 * If we're in a non-emitting branch of a condition construct,
1952 * or walking to the end of an already terminated %rep block,
1953 * we should ignore all directives except for condition
1954 * directives.
1956 if (((istk->conds && !emitting(istk->conds->state)) ||
1957 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1958 return NO_DIRECTIVE_FOUND;
1962 * If we're defining a macro or reading a %rep block, we should
1963 * ignore all directives except for %macro/%imacro (which nest),
1964 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1965 * If we're in a %rep block, another %rep nests, so should be let through.
1967 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1968 i != PP_ENDMACRO && i != PP_ENDM &&
1969 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1970 return NO_DIRECTIVE_FOUND;
1973 if (defining) {
1974 if (i == PP_MACRO || i == PP_IMACRO) {
1975 nested_mac_count++;
1976 return NO_DIRECTIVE_FOUND;
1977 } else if (nested_mac_count > 0) {
1978 if (i == PP_ENDMACRO) {
1979 nested_mac_count--;
1980 return NO_DIRECTIVE_FOUND;
1983 if (!defining->name) {
1984 if (i == PP_REP) {
1985 nested_rep_count++;
1986 return NO_DIRECTIVE_FOUND;
1987 } else if (nested_rep_count > 0) {
1988 if (i == PP_ENDREP) {
1989 nested_rep_count--;
1990 return NO_DIRECTIVE_FOUND;
1996 switch (i) {
1997 case PP_INVALID:
1998 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1999 tline->text);
2000 return NO_DIRECTIVE_FOUND; /* didn't get it */
2002 case PP_STACKSIZE:
2003 /* Directive to tell NASM what the default stack size is. The
2004 * default is for a 16-bit stack, and this can be overriden with
2005 * %stacksize large.
2006 * the following form:
2008 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2010 tline = tline->next;
2011 if (tline && tline->type == TOK_WHITESPACE)
2012 tline = tline->next;
2013 if (!tline || tline->type != TOK_ID) {
2014 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2015 free_tlist(origline);
2016 return DIRECTIVE_FOUND;
2018 if (nasm_stricmp(tline->text, "flat") == 0) {
2019 /* All subsequent ARG directives are for a 32-bit stack */
2020 StackSize = 4;
2021 StackPointer = "ebp";
2022 ArgOffset = 8;
2023 LocalOffset = 0;
2024 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2025 /* All subsequent ARG directives are for a 64-bit stack */
2026 StackSize = 8;
2027 StackPointer = "rbp";
2028 ArgOffset = 8;
2029 LocalOffset = 0;
2030 } else if (nasm_stricmp(tline->text, "large") == 0) {
2031 /* All subsequent ARG directives are for a 16-bit stack,
2032 * far function call.
2034 StackSize = 2;
2035 StackPointer = "bp";
2036 ArgOffset = 4;
2037 LocalOffset = 0;
2038 } else if (nasm_stricmp(tline->text, "small") == 0) {
2039 /* All subsequent ARG directives are for a 16-bit stack,
2040 * far function call. We don't support near functions.
2042 StackSize = 2;
2043 StackPointer = "bp";
2044 ArgOffset = 6;
2045 LocalOffset = 0;
2046 } else {
2047 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2048 free_tlist(origline);
2049 return DIRECTIVE_FOUND;
2051 free_tlist(origline);
2052 return DIRECTIVE_FOUND;
2054 case PP_ARG:
2055 /* TASM like ARG directive to define arguments to functions, in
2056 * the following form:
2058 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2060 offset = ArgOffset;
2061 do {
2062 char *arg, directive[256];
2063 int size = StackSize;
2065 /* Find the argument name */
2066 tline = tline->next;
2067 if (tline && tline->type == TOK_WHITESPACE)
2068 tline = tline->next;
2069 if (!tline || tline->type != TOK_ID) {
2070 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2071 free_tlist(origline);
2072 return DIRECTIVE_FOUND;
2074 arg = tline->text;
2076 /* Find the argument size type */
2077 tline = tline->next;
2078 if (!tline || tline->type != TOK_OTHER
2079 || tline->text[0] != ':') {
2080 error(ERR_NONFATAL,
2081 "Syntax error processing `%%arg' directive");
2082 free_tlist(origline);
2083 return DIRECTIVE_FOUND;
2085 tline = tline->next;
2086 if (!tline || tline->type != TOK_ID) {
2087 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2088 free_tlist(origline);
2089 return DIRECTIVE_FOUND;
2092 /* Allow macro expansion of type parameter */
2093 tt = tokenize(tline->text);
2094 tt = expand_smacro(tt);
2095 size = parse_size(tt->text);
2096 if (!size) {
2097 error(ERR_NONFATAL,
2098 "Invalid size type for `%%arg' missing directive");
2099 free_tlist(tt);
2100 free_tlist(origline);
2101 return DIRECTIVE_FOUND;
2103 free_tlist(tt);
2105 /* Round up to even stack slots */
2106 size = (size+StackSize-1) & ~(StackSize-1);
2108 /* Now define the macro for the argument */
2109 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2110 arg, StackPointer, offset);
2111 do_directive(tokenize(directive));
2112 offset += size;
2114 /* Move to the next argument in the list */
2115 tline = tline->next;
2116 if (tline && tline->type == TOK_WHITESPACE)
2117 tline = tline->next;
2118 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2119 ArgOffset = offset;
2120 free_tlist(origline);
2121 return DIRECTIVE_FOUND;
2123 case PP_LOCAL:
2124 /* TASM like LOCAL directive to define local variables for a
2125 * function, in the following form:
2127 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2129 * The '= LocalSize' at the end is ignored by NASM, but is
2130 * required by TASM to define the local parameter size (and used
2131 * by the TASM macro package).
2133 offset = LocalOffset;
2134 do {
2135 char *local, directive[256];
2136 int size = StackSize;
2138 /* Find the argument name */
2139 tline = tline->next;
2140 if (tline && tline->type == TOK_WHITESPACE)
2141 tline = tline->next;
2142 if (!tline || tline->type != TOK_ID) {
2143 error(ERR_NONFATAL,
2144 "`%%local' missing argument parameter");
2145 free_tlist(origline);
2146 return DIRECTIVE_FOUND;
2148 local = tline->text;
2150 /* Find the argument size type */
2151 tline = tline->next;
2152 if (!tline || tline->type != TOK_OTHER
2153 || tline->text[0] != ':') {
2154 error(ERR_NONFATAL,
2155 "Syntax error processing `%%local' directive");
2156 free_tlist(origline);
2157 return DIRECTIVE_FOUND;
2159 tline = tline->next;
2160 if (!tline || tline->type != TOK_ID) {
2161 error(ERR_NONFATAL,
2162 "`%%local' missing size type parameter");
2163 free_tlist(origline);
2164 return DIRECTIVE_FOUND;
2167 /* Allow macro expansion of type parameter */
2168 tt = tokenize(tline->text);
2169 tt = expand_smacro(tt);
2170 size = parse_size(tt->text);
2171 if (!size) {
2172 error(ERR_NONFATAL,
2173 "Invalid size type for `%%local' missing directive");
2174 free_tlist(tt);
2175 free_tlist(origline);
2176 return DIRECTIVE_FOUND;
2178 free_tlist(tt);
2180 /* Round up to even stack slots */
2181 size = (size+StackSize-1) & ~(StackSize-1);
2183 offset += size; /* Negative offset, increment before */
2185 /* Now define the macro for the argument */
2186 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2187 local, StackPointer, offset);
2188 do_directive(tokenize(directive));
2190 /* Now define the assign to setup the enter_c macro correctly */
2191 snprintf(directive, sizeof(directive),
2192 "%%assign %%$localsize %%$localsize+%d", size);
2193 do_directive(tokenize(directive));
2195 /* Move to the next argument in the list */
2196 tline = tline->next;
2197 if (tline && tline->type == TOK_WHITESPACE)
2198 tline = tline->next;
2199 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2200 LocalOffset = offset;
2201 free_tlist(origline);
2202 return DIRECTIVE_FOUND;
2204 case PP_CLEAR:
2205 if (tline->next)
2206 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2207 free_macros();
2208 init_macros();
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
2212 case PP_DEPEND:
2213 t = tline->next = expand_smacro(tline->next);
2214 skip_white_(t);
2215 if (!t || (t->type != TOK_STRING &&
2216 t->type != TOK_INTERNAL_STRING)) {
2217 error(ERR_NONFATAL, "`%%depend' expects a file name");
2218 free_tlist(origline);
2219 return DIRECTIVE_FOUND; /* but we did _something_ */
2221 if (t->next)
2222 error(ERR_WARNING,
2223 "trailing garbage after `%%depend' ignored");
2224 p = t->text;
2225 if (t->type != TOK_INTERNAL_STRING)
2226 nasm_unquote(p, NULL);
2227 if (dephead && !in_list(*dephead, p)) {
2228 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2229 sl->next = NULL;
2230 strcpy(sl->str, p);
2231 *deptail = sl;
2232 deptail = &sl->next;
2234 free_tlist(origline);
2235 return DIRECTIVE_FOUND;
2237 case PP_INCLUDE:
2238 t = tline->next = expand_smacro(tline->next);
2239 skip_white_(t);
2241 if (!t || (t->type != TOK_STRING &&
2242 t->type != TOK_INTERNAL_STRING)) {
2243 error(ERR_NONFATAL, "`%%include' expects a file name");
2244 free_tlist(origline);
2245 return DIRECTIVE_FOUND; /* but we did _something_ */
2247 if (t->next)
2248 error(ERR_WARNING,
2249 "trailing garbage after `%%include' ignored");
2250 p = t->text;
2251 if (t->type != TOK_INTERNAL_STRING)
2252 nasm_unquote(p, NULL);
2253 inc = nasm_malloc(sizeof(Include));
2254 inc->next = istk;
2255 inc->conds = NULL;
2256 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2257 if (!inc->fp) {
2258 /* -MG given but file not found */
2259 nasm_free(inc);
2260 } else {
2261 inc->fname = src_set_fname(nasm_strdup(p));
2262 inc->lineno = src_set_linnum(0);
2263 inc->lineinc = 1;
2264 inc->expansion = NULL;
2265 inc->mstk = NULL;
2266 istk = inc;
2267 list->uplevel(LIST_INCLUDE);
2269 free_tlist(origline);
2270 return DIRECTIVE_FOUND;
2272 case PP_USE:
2274 static macros_t *use_pkg;
2275 const char *pkg_macro;
2277 t = tline->next = expand_smacro(tline->next);
2278 skip_white_(t);
2280 if (!t || (t->type != TOK_STRING &&
2281 t->type != TOK_INTERNAL_STRING &&
2282 t->type != TOK_ID)) {
2283 error(ERR_NONFATAL, "`%%use' expects a package name");
2284 free_tlist(origline);
2285 return DIRECTIVE_FOUND; /* but we did _something_ */
2287 if (t->next)
2288 error(ERR_WARNING,
2289 "trailing garbage after `%%use' ignored");
2290 if (t->type == TOK_STRING)
2291 nasm_unquote(t->text, NULL);
2292 use_pkg = nasm_stdmac_find_package(t->text);
2293 if (!use_pkg)
2294 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
2295 /* The first string will be <%define>__USE_*__ */
2296 pkg_macro = (char *)use_pkg + 1;
2297 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2298 /* Not already included, go ahead and include it */
2299 stdmacpos = use_pkg;
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
2304 case PP_PUSH:
2305 tline = tline->next;
2306 skip_white_(tline);
2307 tline = expand_id(tline);
2308 if (tline) {
2309 if (!tok_type_(tline, TOK_ID)) {
2310 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2311 free_tlist(origline);
2312 return DIRECTIVE_FOUND; /* but we did _something_ */
2314 if (tline->next)
2315 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2316 p = nasm_strdup(tline->text);
2317 } else {
2318 p = NULL; /* Anonymous context */
2320 ctx = nasm_malloc(sizeof(Context));
2321 ctx->next = cstk;
2322 hash_init(&ctx->localmac, HASH_SMALL);
2323 ctx->name = p;
2324 ctx->number = unique++;
2325 cstk = ctx;
2326 free_tlist(origline);
2327 return DIRECTIVE_FOUND;
2329 case PP_REPL:
2330 tline = tline->next;
2331 skip_white_(tline);
2332 tline = expand_id(tline);
2333 if (tline) {
2334 if (!tok_type_(tline, TOK_ID)) {
2335 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2336 free_tlist(origline);
2337 return DIRECTIVE_FOUND; /* but we did _something_ */
2339 if (tline->next)
2340 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2341 p = nasm_strdup(tline->text);
2342 } else {
2343 p = NULL;
2345 if (!cstk)
2346 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2347 else {
2348 nasm_free(cstk->name);
2349 cstk->name = p;
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2354 case PP_POP:
2355 if (tline->next)
2356 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2357 if (!cstk)
2358 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2359 else
2360 ctx_pop();
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2364 case PP_ERROR:
2365 case PP_WARNING:
2367 int severity = (i == PP_ERROR)
2368 ? ERR_NONFATAL|ERR_NO_SEVERITY
2369 : ERR_WARNING|ERR_NO_SEVERITY;
2371 tline->next = expand_smacro(tline->next);
2372 tline = tline->next;
2373 skip_white_(tline);
2374 t = tline ? tline->next : NULL;
2375 skip_white_(t);
2376 if (tok_type_(tline, TOK_STRING) && !t) {
2377 /* The line contains only a quoted string */
2378 p = tline->text;
2379 nasm_unquote(p, NULL);
2380 error(severity, "%s: %s", pp_directives[i], p);
2381 } else {
2382 /* Not a quoted string, or more than a quoted string */
2383 p = detoken(tline, false);
2384 error(severity, "%s: %s", pp_directives[i], p);
2385 nasm_free(p);
2387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
2391 CASE_PP_IF:
2392 if (istk->conds && !emitting(istk->conds->state))
2393 j = COND_NEVER;
2394 else {
2395 j = if_condition(tline->next, i);
2396 tline->next = NULL; /* it got freed */
2397 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2399 cond = nasm_malloc(sizeof(Cond));
2400 cond->next = istk->conds;
2401 cond->state = j;
2402 istk->conds = cond;
2403 free_tlist(origline);
2404 return DIRECTIVE_FOUND;
2406 CASE_PP_ELIF:
2407 if (!istk->conds)
2408 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2409 if (emitting(istk->conds->state)
2410 || istk->conds->state == COND_NEVER)
2411 istk->conds->state = COND_NEVER;
2412 else {
2414 * IMPORTANT: In the case of %if, we will already have
2415 * called expand_mmac_params(); however, if we're
2416 * processing an %elif we must have been in a
2417 * non-emitting mode, which would have inhibited
2418 * the normal invocation of expand_mmac_params(). Therefore,
2419 * we have to do it explicitly here.
2421 j = if_condition(expand_mmac_params(tline->next), i);
2422 tline->next = NULL; /* it got freed */
2423 istk->conds->state =
2424 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2429 case PP_ELSE:
2430 if (tline->next)
2431 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2432 if (!istk->conds)
2433 error(ERR_FATAL, "`%%else': no matching `%%if'");
2434 if (emitting(istk->conds->state)
2435 || istk->conds->state == COND_NEVER)
2436 istk->conds->state = COND_ELSE_FALSE;
2437 else
2438 istk->conds->state = COND_ELSE_TRUE;
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND;
2442 case PP_ENDIF:
2443 if (tline->next)
2444 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2445 if (!istk->conds)
2446 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2447 cond = istk->conds;
2448 istk->conds = cond->next;
2449 nasm_free(cond);
2450 free_tlist(origline);
2451 return DIRECTIVE_FOUND;
2453 case PP_MACRO:
2454 case PP_IMACRO:
2455 if (defining) {
2456 error(ERR_FATAL,
2457 "`%%%smacro': already defining a macro",
2458 (i == PP_IMACRO ? "i" : ""));
2459 return DIRECTIVE_FOUND;
2461 defining = nasm_malloc(sizeof(MMacro));
2462 defining->casesense = (i == PP_MACRO);
2463 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2464 nasm_free(defining);
2465 defining = NULL;
2466 return DIRECTIVE_FOUND;
2469 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2470 while (mmac) {
2471 if (!strcmp(mmac->name, defining->name) &&
2472 (mmac->nparam_min <= defining->nparam_max
2473 || defining->plus)
2474 && (defining->nparam_min <= mmac->nparam_max
2475 || mmac->plus)) {
2476 error(ERR_WARNING,
2477 "redefining multi-line macro `%s'", defining->name);
2478 return DIRECTIVE_FOUND;
2480 mmac = mmac->next;
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
2485 case PP_ENDM:
2486 case PP_ENDMACRO:
2487 if (! (defining && defining->name)) {
2488 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2489 return DIRECTIVE_FOUND;
2491 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2492 defining->next = *mmhead;
2493 *mmhead = defining;
2494 defining = NULL;
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2498 case PP_UNMACRO:
2499 case PP_UNIMACRO:
2501 MMacro **mmac_p;
2502 MMacro spec;
2504 spec.casesense = (i == PP_UNMACRO);
2505 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2506 return DIRECTIVE_FOUND;
2508 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2509 while (mmac_p && *mmac_p) {
2510 mmac = *mmac_p;
2511 if (mmac->casesense == spec.casesense &&
2512 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2513 mmac->nparam_min == spec.nparam_min &&
2514 mmac->nparam_max == spec.nparam_max &&
2515 mmac->plus == spec.plus) {
2516 *mmac_p = mmac->next;
2517 free_mmacro(mmac);
2518 } else {
2519 mmac_p = &mmac->next;
2522 free_tlist(origline);
2523 free_tlist(spec.dlist);
2524 return DIRECTIVE_FOUND;
2527 case PP_ROTATE:
2528 if (tline->next && tline->next->type == TOK_WHITESPACE)
2529 tline = tline->next;
2530 if (tline->next == NULL) {
2531 free_tlist(origline);
2532 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2533 return DIRECTIVE_FOUND;
2535 t = expand_smacro(tline->next);
2536 tline->next = NULL;
2537 free_tlist(origline);
2538 tline = t;
2539 tptr = &t;
2540 tokval.t_type = TOKEN_INVALID;
2541 evalresult =
2542 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2543 free_tlist(tline);
2544 if (!evalresult)
2545 return DIRECTIVE_FOUND;
2546 if (tokval.t_type)
2547 error(ERR_WARNING,
2548 "trailing garbage after expression ignored");
2549 if (!is_simple(evalresult)) {
2550 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2551 return DIRECTIVE_FOUND;
2553 mmac = istk->mstk;
2554 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2555 mmac = mmac->next_active;
2556 if (!mmac) {
2557 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2558 } else if (mmac->nparam == 0) {
2559 error(ERR_NONFATAL,
2560 "`%%rotate' invoked within macro without parameters");
2561 } else {
2562 int rotate = mmac->rotate + reloc_value(evalresult);
2564 rotate %= (int)mmac->nparam;
2565 if (rotate < 0)
2566 rotate += mmac->nparam;
2568 mmac->rotate = rotate;
2570 return DIRECTIVE_FOUND;
2572 case PP_REP:
2573 nolist = false;
2574 do {
2575 tline = tline->next;
2576 } while (tok_type_(tline, TOK_WHITESPACE));
2578 if (tok_type_(tline, TOK_ID) &&
2579 nasm_stricmp(tline->text, ".nolist") == 0) {
2580 nolist = true;
2581 do {
2582 tline = tline->next;
2583 } while (tok_type_(tline, TOK_WHITESPACE));
2586 if (tline) {
2587 t = expand_smacro(tline);
2588 tptr = &t;
2589 tokval.t_type = TOKEN_INVALID;
2590 evalresult =
2591 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2592 if (!evalresult) {
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2596 if (tokval.t_type)
2597 error(ERR_WARNING,
2598 "trailing garbage after expression ignored");
2599 if (!is_simple(evalresult)) {
2600 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2601 return DIRECTIVE_FOUND;
2603 count = reloc_value(evalresult) + 1;
2604 } else {
2605 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2606 count = 0;
2608 free_tlist(origline);
2610 tmp_defining = defining;
2611 defining = nasm_malloc(sizeof(MMacro));
2612 defining->name = NULL; /* flags this macro as a %rep block */
2613 defining->casesense = false;
2614 defining->plus = false;
2615 defining->nolist = nolist;
2616 defining->in_progress = count;
2617 defining->nparam_min = defining->nparam_max = 0;
2618 defining->defaults = NULL;
2619 defining->dlist = NULL;
2620 defining->expansion = NULL;
2621 defining->next_active = istk->mstk;
2622 defining->rep_nest = tmp_defining;
2623 return DIRECTIVE_FOUND;
2625 case PP_ENDREP:
2626 if (!defining || defining->name) {
2627 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2628 return DIRECTIVE_FOUND;
2632 * Now we have a "macro" defined - although it has no name
2633 * and we won't be entering it in the hash tables - we must
2634 * push a macro-end marker for it on to istk->expansion.
2635 * After that, it will take care of propagating itself (a
2636 * macro-end marker line for a macro which is really a %rep
2637 * block will cause the macro to be re-expanded, complete
2638 * with another macro-end marker to ensure the process
2639 * continues) until the whole expansion is forcibly removed
2640 * from istk->expansion by a %exitrep.
2642 l = nasm_malloc(sizeof(Line));
2643 l->next = istk->expansion;
2644 l->finishes = defining;
2645 l->first = NULL;
2646 istk->expansion = l;
2648 istk->mstk = defining;
2650 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2651 tmp_defining = defining;
2652 defining = defining->rep_nest;
2653 free_tlist(origline);
2654 return DIRECTIVE_FOUND;
2656 case PP_EXITREP:
2658 * We must search along istk->expansion until we hit a
2659 * macro-end marker for a macro with no name. Then we set
2660 * its `in_progress' flag to 0.
2662 for (l = istk->expansion; l; l = l->next)
2663 if (l->finishes && !l->finishes->name)
2664 break;
2666 if (l)
2667 l->finishes->in_progress = 1;
2668 else
2669 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2673 case PP_XDEFINE:
2674 case PP_IXDEFINE:
2675 case PP_DEFINE:
2676 case PP_IDEFINE:
2677 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2679 tline = tline->next;
2680 skip_white_(tline);
2681 tline = expand_id(tline);
2682 if (!tline || (tline->type != TOK_ID &&
2683 (tline->type != TOK_PREPROC_ID ||
2684 tline->text[1] != '$'))) {
2685 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2686 pp_directives[i]);
2687 free_tlist(origline);
2688 return DIRECTIVE_FOUND;
2691 ctx = get_ctx(tline->text, false);
2693 mname = tline->text;
2694 last = tline;
2695 param_start = tline = tline->next;
2696 nparam = 0;
2698 /* Expand the macro definition now for %xdefine and %ixdefine */
2699 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2700 tline = expand_smacro(tline);
2702 if (tok_is_(tline, "(")) {
2704 * This macro has parameters.
2707 tline = tline->next;
2708 while (1) {
2709 skip_white_(tline);
2710 if (!tline) {
2711 error(ERR_NONFATAL, "parameter identifier expected");
2712 free_tlist(origline);
2713 return DIRECTIVE_FOUND;
2715 if (tline->type != TOK_ID) {
2716 error(ERR_NONFATAL,
2717 "`%s': parameter identifier expected",
2718 tline->text);
2719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
2722 tline->type = TOK_SMAC_PARAM + nparam++;
2723 tline = tline->next;
2724 skip_white_(tline);
2725 if (tok_is_(tline, ",")) {
2726 tline = tline->next;
2727 } else {
2728 if (!tok_is_(tline, ")")) {
2729 error(ERR_NONFATAL,
2730 "`)' expected to terminate macro template");
2731 free_tlist(origline);
2732 return DIRECTIVE_FOUND;
2734 break;
2737 last = tline;
2738 tline = tline->next;
2740 if (tok_type_(tline, TOK_WHITESPACE))
2741 last = tline, tline = tline->next;
2742 macro_start = NULL;
2743 last->next = NULL;
2744 t = tline;
2745 while (t) {
2746 if (t->type == TOK_ID) {
2747 for (tt = param_start; tt; tt = tt->next)
2748 if (tt->type >= TOK_SMAC_PARAM &&
2749 !strcmp(tt->text, t->text))
2750 t->type = tt->type;
2752 tt = t->next;
2753 t->next = macro_start;
2754 macro_start = t;
2755 t = tt;
2758 * Good. We now have a macro name, a parameter count, and a
2759 * token list (in reverse order) for an expansion. We ought
2760 * to be OK just to create an SMacro, store it, and let
2761 * free_tlist have the rest of the line (which we have
2762 * carefully re-terminated after chopping off the expansion
2763 * from the end).
2765 define_smacro(ctx, mname, casesense, nparam, macro_start);
2766 free_tlist(origline);
2767 return DIRECTIVE_FOUND;
2769 case PP_UNDEF:
2770 tline = tline->next;
2771 skip_white_(tline);
2772 tline = expand_id(tline);
2773 if (!tline || (tline->type != TOK_ID &&
2774 (tline->type != TOK_PREPROC_ID ||
2775 tline->text[1] != '$'))) {
2776 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2777 free_tlist(origline);
2778 return DIRECTIVE_FOUND;
2780 if (tline->next) {
2781 error(ERR_WARNING,
2782 "trailing garbage after macro name ignored");
2785 /* Find the context that symbol belongs to */
2786 ctx = get_ctx(tline->text, false);
2787 undef_smacro(ctx, tline->text);
2788 free_tlist(origline);
2789 return DIRECTIVE_FOUND;
2791 case PP_DEFSTR:
2792 case PP_IDEFSTR:
2793 casesense = (i == PP_DEFSTR);
2795 tline = tline->next;
2796 skip_white_(tline);
2797 tline = expand_id(tline);
2798 if (!tline || (tline->type != TOK_ID &&
2799 (tline->type != TOK_PREPROC_ID ||
2800 tline->text[1] != '$'))) {
2801 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2802 pp_directives[i]);
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
2807 ctx = get_ctx(tline->text, false);
2809 mname = tline->text;
2810 last = tline;
2811 tline = expand_smacro(tline->next);
2812 last->next = NULL;
2814 while (tok_type_(tline, TOK_WHITESPACE))
2815 tline = delete_Token(tline);
2817 p = detoken(tline, false);
2818 macro_start = nasm_malloc(sizeof(*macro_start));
2819 macro_start->next = NULL;
2820 macro_start->text = nasm_quote(p, strlen(p));
2821 macro_start->type = TOK_STRING;
2822 macro_start->a.mac = NULL;
2823 nasm_free(p);
2826 * We now have a macro name, an implicit parameter count of
2827 * zero, and a string token to use as an expansion. Create
2828 * and store an SMacro.
2830 define_smacro(ctx, mname, casesense, 0, macro_start);
2831 free_tlist(origline);
2832 return DIRECTIVE_FOUND;
2834 case PP_PATHSEARCH:
2836 FILE *fp;
2837 StrList *xsl = NULL;
2838 StrList **xst = &xsl;
2840 casesense = true;
2842 tline = tline->next;
2843 skip_white_(tline);
2844 tline = expand_id(tline);
2845 if (!tline || (tline->type != TOK_ID &&
2846 (tline->type != TOK_PREPROC_ID ||
2847 tline->text[1] != '$'))) {
2848 error(ERR_NONFATAL,
2849 "`%%pathsearch' expects a macro identifier as first parameter");
2850 free_tlist(origline);
2851 return DIRECTIVE_FOUND;
2853 ctx = get_ctx(tline->text, false);
2855 mname = tline->text;
2856 last = tline;
2857 tline = expand_smacro(tline->next);
2858 last->next = NULL;
2860 t = tline;
2861 while (tok_type_(t, TOK_WHITESPACE))
2862 t = t->next;
2864 if (!t || (t->type != TOK_STRING &&
2865 t->type != TOK_INTERNAL_STRING)) {
2866 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2867 free_tlist(tline);
2868 free_tlist(origline);
2869 return DIRECTIVE_FOUND; /* but we did _something_ */
2871 if (t->next)
2872 error(ERR_WARNING,
2873 "trailing garbage after `%%pathsearch' ignored");
2874 p = t->text;
2875 if (t->type != TOK_INTERNAL_STRING)
2876 nasm_unquote(p, NULL);
2878 fp = inc_fopen(p, &xsl, &xst, true);
2879 if (fp) {
2880 p = xsl->str;
2881 fclose(fp); /* Don't actually care about the file */
2883 macro_start = nasm_malloc(sizeof(*macro_start));
2884 macro_start->next = NULL;
2885 macro_start->text = nasm_quote(p, strlen(p));
2886 macro_start->type = TOK_STRING;
2887 macro_start->a.mac = NULL;
2888 if (xsl)
2889 nasm_free(xsl);
2892 * We now have a macro name, an implicit parameter count of
2893 * zero, and a string token to use as an expansion. Create
2894 * and store an SMacro.
2896 define_smacro(ctx, mname, casesense, 0, macro_start);
2897 free_tlist(tline);
2898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
2902 case PP_STRLEN:
2903 casesense = true;
2905 tline = tline->next;
2906 skip_white_(tline);
2907 tline = expand_id(tline);
2908 if (!tline || (tline->type != TOK_ID &&
2909 (tline->type != TOK_PREPROC_ID ||
2910 tline->text[1] != '$'))) {
2911 error(ERR_NONFATAL,
2912 "`%%strlen' expects a macro identifier as first parameter");
2913 free_tlist(origline);
2914 return DIRECTIVE_FOUND;
2916 ctx = get_ctx(tline->text, false);
2918 mname = tline->text;
2919 last = tline;
2920 tline = expand_smacro(tline->next);
2921 last->next = NULL;
2923 t = tline;
2924 while (tok_type_(t, TOK_WHITESPACE))
2925 t = t->next;
2926 /* t should now point to the string */
2927 if (t->type != TOK_STRING) {
2928 error(ERR_NONFATAL,
2929 "`%%strlen` requires string as second parameter");
2930 free_tlist(tline);
2931 free_tlist(origline);
2932 return DIRECTIVE_FOUND;
2935 macro_start = nasm_malloc(sizeof(*macro_start));
2936 macro_start->next = NULL;
2937 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
2938 macro_start->a.mac = NULL;
2941 * We now have a macro name, an implicit parameter count of
2942 * zero, and a numeric token to use as an expansion. Create
2943 * and store an SMacro.
2945 define_smacro(ctx, mname, casesense, 0, macro_start);
2946 free_tlist(tline);
2947 free_tlist(origline);
2948 return DIRECTIVE_FOUND;
2950 case PP_STRCAT:
2951 casesense = true;
2953 tline = tline->next;
2954 skip_white_(tline);
2955 tline = expand_id(tline);
2956 if (!tline || (tline->type != TOK_ID &&
2957 (tline->type != TOK_PREPROC_ID ||
2958 tline->text[1] != '$'))) {
2959 error(ERR_NONFATAL,
2960 "`%%strcat' expects a macro identifier as first parameter");
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2964 ctx = get_ctx(tline->text, false);
2966 mname = tline->text;
2967 last = tline;
2968 tline = expand_smacro(tline->next);
2969 last->next = NULL;
2971 len = 0;
2972 for (t = tline; t; t = t->next) {
2973 switch (t->type) {
2974 case TOK_WHITESPACE:
2975 break;
2976 case TOK_STRING:
2977 len += t->a.len = nasm_unquote(t->text, NULL);
2978 break;
2979 case TOK_OTHER:
2980 if (!strcmp(t->text, ",")) /* permit comma separators */
2981 break;
2982 /* else fall through */
2983 default:
2984 error(ERR_NONFATAL,
2985 "non-string passed to `%%strcat' (%d)", t->type);
2986 free_tlist(tline);
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2992 p = pp = nasm_malloc(len);
2993 t = tline;
2994 for (t = tline; t; t = t->next) {
2995 if (t->type == TOK_STRING) {
2996 memcpy(p, t->text, t->a.len);
2997 p += t->a.len;
3002 * We now have a macro name, an implicit parameter count of
3003 * zero, and a numeric token to use as an expansion. Create
3004 * and store an SMacro.
3006 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3007 macro_start->text = nasm_quote(pp, len);
3008 nasm_free(pp);
3009 define_smacro(ctx, mname, casesense, 0, macro_start);
3010 free_tlist(tline);
3011 free_tlist(origline);
3012 return DIRECTIVE_FOUND;
3014 case PP_SUBSTR:
3016 int64_t a1, a2;
3017 size_t len;
3019 casesense = true;
3021 tline = tline->next;
3022 skip_white_(tline);
3023 tline = expand_id(tline);
3024 if (!tline || (tline->type != TOK_ID &&
3025 (tline->type != TOK_PREPROC_ID ||
3026 tline->text[1] != '$'))) {
3027 error(ERR_NONFATAL,
3028 "`%%substr' expects a macro identifier as first parameter");
3029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3032 ctx = get_ctx(tline->text, false);
3034 mname = tline->text;
3035 last = tline;
3036 tline = expand_smacro(tline->next);
3037 last->next = NULL;
3039 t = tline->next;
3040 while (tok_type_(t, TOK_WHITESPACE))
3041 t = t->next;
3043 /* t should now point to the string */
3044 if (t->type != TOK_STRING) {
3045 error(ERR_NONFATAL,
3046 "`%%substr` requires string as second parameter");
3047 free_tlist(tline);
3048 free_tlist(origline);
3049 return DIRECTIVE_FOUND;
3052 tt = t->next;
3053 tptr = &tt;
3054 tokval.t_type = TOKEN_INVALID;
3055 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3056 pass, error, NULL);
3057 if (!evalresult) {
3058 free_tlist(tline);
3059 free_tlist(origline);
3060 return DIRECTIVE_FOUND;
3061 } else if (!is_simple(evalresult)) {
3062 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3063 free_tlist(tline);
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3067 a1 = evalresult->value-1;
3069 while (tok_type_(tt, TOK_WHITESPACE))
3070 tt = tt->next;
3071 if (!tt) {
3072 a2 = 1; /* Backwards compatibility: one character */
3073 } else {
3074 tokval.t_type = TOKEN_INVALID;
3075 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3076 pass, error, NULL);
3077 if (!evalresult) {
3078 free_tlist(tline);
3079 free_tlist(origline);
3080 return DIRECTIVE_FOUND;
3081 } else if (!is_simple(evalresult)) {
3082 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3083 free_tlist(tline);
3084 free_tlist(origline);
3085 return DIRECTIVE_FOUND;
3087 a2 = evalresult->value;
3090 len = nasm_unquote(t->text, NULL);
3091 if (a2 < 0)
3092 a2 = a2+1+len-a1;
3093 if (a1+a2 > (int64_t)len)
3094 a2 = len-a1;
3096 macro_start = nasm_malloc(sizeof(*macro_start));
3097 macro_start->next = NULL;
3098 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3099 macro_start->type = TOK_STRING;
3100 macro_start->a.mac = NULL;
3103 * We now have a macro name, an implicit parameter count of
3104 * zero, and a numeric token to use as an expansion. Create
3105 * and store an SMacro.
3107 define_smacro(ctx, mname, casesense, 0, macro_start);
3108 free_tlist(tline);
3109 free_tlist(origline);
3110 return DIRECTIVE_FOUND;
3113 case PP_ASSIGN:
3114 case PP_IASSIGN:
3115 casesense = (i == PP_ASSIGN);
3117 tline = tline->next;
3118 skip_white_(tline);
3119 tline = expand_id(tline);
3120 if (!tline || (tline->type != TOK_ID &&
3121 (tline->type != TOK_PREPROC_ID ||
3122 tline->text[1] != '$'))) {
3123 error(ERR_NONFATAL,
3124 "`%%%sassign' expects a macro identifier",
3125 (i == PP_IASSIGN ? "i" : ""));
3126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
3129 ctx = get_ctx(tline->text, false);
3131 mname = tline->text;
3132 last = tline;
3133 tline = expand_smacro(tline->next);
3134 last->next = NULL;
3136 t = tline;
3137 tptr = &t;
3138 tokval.t_type = TOKEN_INVALID;
3139 evalresult =
3140 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3141 free_tlist(tline);
3142 if (!evalresult) {
3143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3147 if (tokval.t_type)
3148 error(ERR_WARNING,
3149 "trailing garbage after expression ignored");
3151 if (!is_simple(evalresult)) {
3152 error(ERR_NONFATAL,
3153 "non-constant value given to `%%%sassign'",
3154 (i == PP_IASSIGN ? "i" : ""));
3155 free_tlist(origline);
3156 return DIRECTIVE_FOUND;
3159 macro_start = nasm_malloc(sizeof(*macro_start));
3160 macro_start->next = NULL;
3161 make_tok_num(macro_start, reloc_value(evalresult));
3162 macro_start->a.mac = NULL;
3165 * We now have a macro name, an implicit parameter count of
3166 * zero, and a numeric token to use as an expansion. Create
3167 * and store an SMacro.
3169 define_smacro(ctx, mname, casesense, 0, macro_start);
3170 free_tlist(origline);
3171 return DIRECTIVE_FOUND;
3173 case PP_LINE:
3175 * Syntax is `%line nnn[+mmm] [filename]'
3177 tline = tline->next;
3178 skip_white_(tline);
3179 if (!tok_type_(tline, TOK_NUMBER)) {
3180 error(ERR_NONFATAL, "`%%line' expects line number");
3181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3184 k = readnum(tline->text, &err);
3185 m = 1;
3186 tline = tline->next;
3187 if (tok_is_(tline, "+")) {
3188 tline = tline->next;
3189 if (!tok_type_(tline, TOK_NUMBER)) {
3190 error(ERR_NONFATAL, "`%%line' expects line increment");
3191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
3194 m = readnum(tline->text, &err);
3195 tline = tline->next;
3197 skip_white_(tline);
3198 src_set_linnum(k);
3199 istk->lineinc = m;
3200 if (tline) {
3201 nasm_free(src_set_fname(detoken(tline, false)));
3203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
3206 default:
3207 error(ERR_FATAL,
3208 "preprocessor directive `%s' not yet implemented",
3209 pp_directives[i]);
3210 return DIRECTIVE_FOUND;
3215 * Ensure that a macro parameter contains a condition code and
3216 * nothing else. Return the condition code index if so, or -1
3217 * otherwise.
3219 static int find_cc(Token * t)
3221 Token *tt;
3222 int i, j, k, m;
3224 if (!t)
3225 return -1; /* Probably a %+ without a space */
3227 skip_white_(t);
3228 if (t->type != TOK_ID)
3229 return -1;
3230 tt = t->next;
3231 skip_white_(tt);
3232 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3233 return -1;
3235 i = -1;
3236 j = elements(conditions);
3237 while (j - i > 1) {
3238 k = (j + i) / 2;
3239 m = nasm_stricmp(t->text, conditions[k]);
3240 if (m == 0) {
3241 i = k;
3242 j = -2;
3243 break;
3244 } else if (m < 0) {
3245 j = k;
3246 } else
3247 i = k;
3249 if (j != -2)
3250 return -1;
3251 return i;
3255 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3256 * %-n) and MMacro-local identifiers (%%foo).
3258 static Token *expand_mmac_params(Token * tline)
3260 Token *t, *tt, **tail, *thead;
3262 tail = &thead;
3263 thead = NULL;
3265 while (tline) {
3266 if (tline->type == TOK_PREPROC_ID &&
3267 (((tline->text[1] == '+' || tline->text[1] == '-')
3268 && tline->text[2]) || tline->text[1] == '%'
3269 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3270 char *text = NULL;
3271 int type = 0, cc; /* type = 0 to placate optimisers */
3272 char tmpbuf[30];
3273 unsigned int n;
3274 int i;
3275 MMacro *mac;
3277 t = tline;
3278 tline = tline->next;
3280 mac = istk->mstk;
3281 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3282 mac = mac->next_active;
3283 if (!mac)
3284 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3285 else
3286 switch (t->text[1]) {
3288 * We have to make a substitution of one of the
3289 * forms %1, %-1, %+1, %%foo, %0.
3291 case '0':
3292 type = TOK_NUMBER;
3293 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3294 text = nasm_strdup(tmpbuf);
3295 break;
3296 case '%':
3297 type = TOK_ID;
3298 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3299 mac->unique);
3300 text = nasm_strcat(tmpbuf, t->text + 2);
3301 break;
3302 case '-':
3303 n = atoi(t->text + 2) - 1;
3304 if (n >= mac->nparam)
3305 tt = NULL;
3306 else {
3307 if (mac->nparam > 1)
3308 n = (n + mac->rotate) % mac->nparam;
3309 tt = mac->params[n];
3311 cc = find_cc(tt);
3312 if (cc == -1) {
3313 error(ERR_NONFATAL,
3314 "macro parameter %d is not a condition code",
3315 n + 1);
3316 text = NULL;
3317 } else {
3318 type = TOK_ID;
3319 if (inverse_ccs[cc] == -1) {
3320 error(ERR_NONFATAL,
3321 "condition code `%s' is not invertible",
3322 conditions[cc]);
3323 text = NULL;
3324 } else
3325 text =
3326 nasm_strdup(conditions[inverse_ccs[cc]]);
3328 break;
3329 case '+':
3330 n = atoi(t->text + 2) - 1;
3331 if (n >= mac->nparam)
3332 tt = NULL;
3333 else {
3334 if (mac->nparam > 1)
3335 n = (n + mac->rotate) % mac->nparam;
3336 tt = mac->params[n];
3338 cc = find_cc(tt);
3339 if (cc == -1) {
3340 error(ERR_NONFATAL,
3341 "macro parameter %d is not a condition code",
3342 n + 1);
3343 text = NULL;
3344 } else {
3345 type = TOK_ID;
3346 text = nasm_strdup(conditions[cc]);
3348 break;
3349 default:
3350 n = atoi(t->text + 1) - 1;
3351 if (n >= mac->nparam)
3352 tt = NULL;
3353 else {
3354 if (mac->nparam > 1)
3355 n = (n + mac->rotate) % mac->nparam;
3356 tt = mac->params[n];
3358 if (tt) {
3359 for (i = 0; i < mac->paramlen[n]; i++) {
3360 *tail = new_Token(NULL, tt->type, tt->text, 0);
3361 tail = &(*tail)->next;
3362 tt = tt->next;
3365 text = NULL; /* we've done it here */
3366 break;
3368 if (!text) {
3369 delete_Token(t);
3370 } else {
3371 *tail = t;
3372 tail = &t->next;
3373 t->type = type;
3374 nasm_free(t->text);
3375 t->text = text;
3376 t->a.mac = NULL;
3378 continue;
3379 } else {
3380 t = *tail = tline;
3381 tline = tline->next;
3382 t->a.mac = NULL;
3383 tail = &t->next;
3386 *tail = NULL;
3387 t = thead;
3388 for (; t && (tt = t->next) != NULL; t = t->next)
3389 switch (t->type) {
3390 case TOK_WHITESPACE:
3391 if (tt->type == TOK_WHITESPACE) {
3392 t->next = delete_Token(tt);
3394 break;
3395 case TOK_ID:
3396 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3397 char *tmp = nasm_strcat(t->text, tt->text);
3398 nasm_free(t->text);
3399 t->text = tmp;
3400 t->next = delete_Token(tt);
3402 break;
3403 case TOK_NUMBER:
3404 if (tt->type == TOK_NUMBER) {
3405 char *tmp = nasm_strcat(t->text, tt->text);
3406 nasm_free(t->text);
3407 t->text = tmp;
3408 t->next = delete_Token(tt);
3410 break;
3411 default:
3412 break;
3415 return thead;
3419 * Expand all single-line macro calls made in the given line.
3420 * Return the expanded version of the line. The original is deemed
3421 * to be destroyed in the process. (In reality we'll just move
3422 * Tokens from input to output a lot of the time, rather than
3423 * actually bothering to destroy and replicate.)
3425 #define DEADMAN_LIMIT (1 << 20)
3427 static Token *expand_smacro(Token * tline)
3429 Token *t, *tt, *mstart, **tail, *thead;
3430 struct hash_table *smtbl;
3431 SMacro *head = NULL, *m;
3432 Token **params;
3433 int *paramsize;
3434 unsigned int nparam, sparam;
3435 int brackets, rescan;
3436 Token *org_tline = tline;
3437 Context *ctx;
3438 char *mname;
3439 int deadman = DEADMAN_LIMIT;
3442 * Trick: we should avoid changing the start token pointer since it can
3443 * be contained in "next" field of other token. Because of this
3444 * we allocate a copy of first token and work with it; at the end of
3445 * routine we copy it back
3447 if (org_tline) {
3448 tline =
3449 new_Token(org_tline->next, org_tline->type, org_tline->text,
3451 tline->a.mac = org_tline->a.mac;
3452 nasm_free(org_tline->text);
3453 org_tline->text = NULL;
3456 again:
3457 tail = &thead;
3458 thead = NULL;
3460 while (tline) { /* main token loop */
3461 if (!--deadman) {
3462 error(ERR_NONFATAL, "interminable macro recursion");
3463 break;
3466 if ((mname = tline->text)) {
3467 /* if this token is a local macro, look in local context */
3468 ctx = NULL;
3469 smtbl = &smacros;
3470 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3471 ctx = get_ctx(mname, true);
3472 if (ctx)
3473 smtbl = &ctx->localmac;
3475 head = (SMacro *) hash_findix(smtbl, mname);
3478 * We've hit an identifier. As in is_mmacro below, we first
3479 * check whether the identifier is a single-line macro at
3480 * all, then think about checking for parameters if
3481 * necessary.
3483 for (m = head; m; m = m->next)
3484 if (!mstrcmp(m->name, mname, m->casesense))
3485 break;
3486 if (m) {
3487 mstart = tline;
3488 params = NULL;
3489 paramsize = NULL;
3490 if (m->nparam == 0) {
3492 * Simple case: the macro is parameterless. Discard the
3493 * one token that the macro call took, and push the
3494 * expansion back on the to-do stack.
3496 if (!m->expansion) {
3497 if (!strcmp("__FILE__", m->name)) {
3498 int32_t num = 0;
3499 char *file = NULL;
3500 src_get(&num, &file);
3501 tline->text = nasm_quote(file, strlen(file));
3502 tline->type = TOK_STRING;
3503 nasm_free(file);
3504 continue;
3506 if (!strcmp("__LINE__", m->name)) {
3507 nasm_free(tline->text);
3508 make_tok_num(tline, src_get_linnum());
3509 continue;
3511 if (!strcmp("__BITS__", m->name)) {
3512 nasm_free(tline->text);
3513 make_tok_num(tline, globalbits);
3514 continue;
3516 tline = delete_Token(tline);
3517 continue;
3519 } else {
3521 * Complicated case: at least one macro with this name
3522 * exists and takes parameters. We must find the
3523 * parameters in the call, count them, find the SMacro
3524 * that corresponds to that form of the macro call, and
3525 * substitute for the parameters when we expand. What a
3526 * pain.
3528 /*tline = tline->next;
3529 skip_white_(tline); */
3530 do {
3531 t = tline->next;
3532 while (tok_type_(t, TOK_SMAC_END)) {
3533 t->a.mac->in_progress = false;
3534 t->text = NULL;
3535 t = tline->next = delete_Token(t);
3537 tline = t;
3538 } while (tok_type_(tline, TOK_WHITESPACE));
3539 if (!tok_is_(tline, "(")) {
3541 * This macro wasn't called with parameters: ignore
3542 * the call. (Behaviour borrowed from gnu cpp.)
3544 tline = mstart;
3545 m = NULL;
3546 } else {
3547 int paren = 0;
3548 int white = 0;
3549 brackets = 0;
3550 nparam = 0;
3551 sparam = PARAM_DELTA;
3552 params = nasm_malloc(sparam * sizeof(Token *));
3553 params[0] = tline->next;
3554 paramsize = nasm_malloc(sparam * sizeof(int));
3555 paramsize[0] = 0;
3556 while (true) { /* parameter loop */
3558 * For some unusual expansions
3559 * which concatenates function call
3561 t = tline->next;
3562 while (tok_type_(t, TOK_SMAC_END)) {
3563 t->a.mac->in_progress = false;
3564 t->text = NULL;
3565 t = tline->next = delete_Token(t);
3567 tline = t;
3569 if (!tline) {
3570 error(ERR_NONFATAL,
3571 "macro call expects terminating `)'");
3572 break;
3574 if (tline->type == TOK_WHITESPACE
3575 && brackets <= 0) {
3576 if (paramsize[nparam])
3577 white++;
3578 else
3579 params[nparam] = tline->next;
3580 continue; /* parameter loop */
3582 if (tline->type == TOK_OTHER
3583 && tline->text[1] == 0) {
3584 char ch = tline->text[0];
3585 if (ch == ',' && !paren && brackets <= 0) {
3586 if (++nparam >= sparam) {
3587 sparam += PARAM_DELTA;
3588 params = nasm_realloc(params,
3589 sparam *
3590 sizeof(Token
3591 *));
3592 paramsize =
3593 nasm_realloc(paramsize,
3594 sparam *
3595 sizeof(int));
3597 params[nparam] = tline->next;
3598 paramsize[nparam] = 0;
3599 white = 0;
3600 continue; /* parameter loop */
3602 if (ch == '{' &&
3603 (brackets > 0 || (brackets == 0 &&
3604 !paramsize[nparam])))
3606 if (!(brackets++)) {
3607 params[nparam] = tline->next;
3608 continue; /* parameter loop */
3611 if (ch == '}' && brackets > 0)
3612 if (--brackets == 0) {
3613 brackets = -1;
3614 continue; /* parameter loop */
3616 if (ch == '(' && !brackets)
3617 paren++;
3618 if (ch == ')' && brackets <= 0)
3619 if (--paren < 0)
3620 break;
3622 if (brackets < 0) {
3623 brackets = 0;
3624 error(ERR_NONFATAL, "braces do not "
3625 "enclose all of macro parameter");
3627 paramsize[nparam] += white + 1;
3628 white = 0;
3629 } /* parameter loop */
3630 nparam++;
3631 while (m && (m->nparam != nparam ||
3632 mstrcmp(m->name, mname,
3633 m->casesense)))
3634 m = m->next;
3635 if (!m)
3636 error(ERR_WARNING | ERR_WARN_MNP,
3637 "macro `%s' exists, "
3638 "but not taking %d parameters",
3639 mstart->text, nparam);
3642 if (m && m->in_progress)
3643 m = NULL;
3644 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3646 * Design question: should we handle !tline, which
3647 * indicates missing ')' here, or expand those
3648 * macros anyway, which requires the (t) test a few
3649 * lines down?
3651 nasm_free(params);
3652 nasm_free(paramsize);
3653 tline = mstart;
3654 } else {
3656 * Expand the macro: we are placed on the last token of the
3657 * call, so that we can easily split the call from the
3658 * following tokens. We also start by pushing an SMAC_END
3659 * token for the cycle removal.
3661 t = tline;
3662 if (t) {
3663 tline = t->next;
3664 t->next = NULL;
3666 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3667 tt->a.mac = m;
3668 m->in_progress = true;
3669 tline = tt;
3670 for (t = m->expansion; t; t = t->next) {
3671 if (t->type >= TOK_SMAC_PARAM) {
3672 Token *pcopy = tline, **ptail = &pcopy;
3673 Token *ttt, *pt;
3674 int i;
3676 ttt = params[t->type - TOK_SMAC_PARAM];
3677 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3678 --i >= 0;) {
3679 pt = *ptail =
3680 new_Token(tline, ttt->type, ttt->text,
3682 ptail = &pt->next;
3683 ttt = ttt->next;
3685 tline = pcopy;
3686 } else if (t->type == TOK_PREPROC_Q) {
3687 tt = new_Token(tline, TOK_ID, mname, 0);
3688 tline = tt;
3689 } else if (t->type == TOK_PREPROC_QQ) {
3690 tt = new_Token(tline, TOK_ID, m->name, 0);
3691 tline = tt;
3692 } else {
3693 tt = new_Token(tline, t->type, t->text, 0);
3694 tline = tt;
3699 * Having done that, get rid of the macro call, and clean
3700 * up the parameters.
3702 nasm_free(params);
3703 nasm_free(paramsize);
3704 free_tlist(mstart);
3705 continue; /* main token loop */
3710 if (tline->type == TOK_SMAC_END) {
3711 tline->a.mac->in_progress = false;
3712 tline = delete_Token(tline);
3713 } else {
3714 t = *tail = tline;
3715 tline = tline->next;
3716 t->a.mac = NULL;
3717 t->next = NULL;
3718 tail = &t->next;
3723 * Now scan the entire line and look for successive TOK_IDs that resulted
3724 * after expansion (they can't be produced by tokenize()). The successive
3725 * TOK_IDs should be concatenated.
3726 * Also we look for %+ tokens and concatenate the tokens before and after
3727 * them (without white spaces in between).
3729 t = thead;
3730 rescan = 0;
3731 while (t) {
3732 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3733 t = t->next;
3734 if (!t || !t->next)
3735 break;
3736 if (t->next->type == TOK_ID ||
3737 t->next->type == TOK_PREPROC_ID ||
3738 t->next->type == TOK_NUMBER) {
3739 char *p = nasm_strcat(t->text, t->next->text);
3740 nasm_free(t->text);
3741 t->next = delete_Token(t->next);
3742 t->text = p;
3743 rescan = 1;
3744 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3745 t->next->next->type == TOK_PREPROC_ID &&
3746 strcmp(t->next->next->text, "%+") == 0) {
3747 /* free the next whitespace, the %+ token and next whitespace */
3748 int i;
3749 for (i = 1; i <= 3; i++) {
3750 if (!t->next
3751 || (i != 2 && t->next->type != TOK_WHITESPACE))
3752 break;
3753 t->next = delete_Token(t->next);
3754 } /* endfor */
3755 } else
3756 t = t->next;
3758 /* If we concatenaded something, re-scan the line for macros */
3759 if (rescan) {
3760 tline = thead;
3761 goto again;
3764 if (org_tline) {
3765 if (thead) {
3766 *org_tline = *thead;
3767 /* since we just gave text to org_line, don't free it */
3768 thead->text = NULL;
3769 delete_Token(thead);
3770 } else {
3771 /* the expression expanded to empty line;
3772 we can't return NULL for some reasons
3773 we just set the line to a single WHITESPACE token. */
3774 memset(org_tline, 0, sizeof(*org_tline));
3775 org_tline->text = NULL;
3776 org_tline->type = TOK_WHITESPACE;
3778 thead = org_tline;
3781 return thead;
3785 * Similar to expand_smacro but used exclusively with macro identifiers
3786 * right before they are fetched in. The reason is that there can be
3787 * identifiers consisting of several subparts. We consider that if there
3788 * are more than one element forming the name, user wants a expansion,
3789 * otherwise it will be left as-is. Example:
3791 * %define %$abc cde
3793 * the identifier %$abc will be left as-is so that the handler for %define
3794 * will suck it and define the corresponding value. Other case:
3796 * %define _%$abc cde
3798 * In this case user wants name to be expanded *before* %define starts
3799 * working, so we'll expand %$abc into something (if it has a value;
3800 * otherwise it will be left as-is) then concatenate all successive
3801 * PP_IDs into one.
3803 static Token *expand_id(Token * tline)
3805 Token *cur, *oldnext = NULL;
3807 if (!tline || !tline->next)
3808 return tline;
3810 cur = tline;
3811 while (cur->next &&
3812 (cur->next->type == TOK_ID ||
3813 cur->next->type == TOK_PREPROC_ID
3814 || cur->next->type == TOK_NUMBER))
3815 cur = cur->next;
3817 /* If identifier consists of just one token, don't expand */
3818 if (cur == tline)
3819 return tline;
3821 if (cur) {
3822 oldnext = cur->next; /* Detach the tail past identifier */
3823 cur->next = NULL; /* so that expand_smacro stops here */
3826 tline = expand_smacro(tline);
3828 if (cur) {
3829 /* expand_smacro possibly changhed tline; re-scan for EOL */
3830 cur = tline;
3831 while (cur && cur->next)
3832 cur = cur->next;
3833 if (cur)
3834 cur->next = oldnext;
3837 return tline;
3841 * Determine whether the given line constitutes a multi-line macro
3842 * call, and return the MMacro structure called if so. Doesn't have
3843 * to check for an initial label - that's taken care of in
3844 * expand_mmacro - but must check numbers of parameters. Guaranteed
3845 * to be called with tline->type == TOK_ID, so the putative macro
3846 * name is easy to find.
3848 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3850 MMacro *head, *m;
3851 Token **params;
3852 int nparam;
3854 head = (MMacro *) hash_findix(&mmacros, tline->text);
3857 * Efficiency: first we see if any macro exists with the given
3858 * name. If not, we can return NULL immediately. _Then_ we
3859 * count the parameters, and then we look further along the
3860 * list if necessary to find the proper MMacro.
3862 for (m = head; m; m = m->next)
3863 if (!mstrcmp(m->name, tline->text, m->casesense))
3864 break;
3865 if (!m)
3866 return NULL;
3869 * OK, we have a potential macro. Count and demarcate the
3870 * parameters.
3872 count_mmac_params(tline->next, &nparam, &params);
3875 * So we know how many parameters we've got. Find the MMacro
3876 * structure that handles this number.
3878 while (m) {
3879 if (m->nparam_min <= nparam
3880 && (m->plus || nparam <= m->nparam_max)) {
3882 * This one is right. Just check if cycle removal
3883 * prohibits us using it before we actually celebrate...
3885 if (m->in_progress) {
3886 #if 0
3887 error(ERR_NONFATAL,
3888 "self-reference in multi-line macro `%s'", m->name);
3889 #endif
3890 nasm_free(params);
3891 return NULL;
3894 * It's right, and we can use it. Add its default
3895 * parameters to the end of our list if necessary.
3897 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3898 params =
3899 nasm_realloc(params,
3900 ((m->nparam_min + m->ndefs +
3901 1) * sizeof(*params)));
3902 while (nparam < m->nparam_min + m->ndefs) {
3903 params[nparam] = m->defaults[nparam - m->nparam_min];
3904 nparam++;
3908 * If we've gone over the maximum parameter count (and
3909 * we're in Plus mode), ignore parameters beyond
3910 * nparam_max.
3912 if (m->plus && nparam > m->nparam_max)
3913 nparam = m->nparam_max;
3915 * Then terminate the parameter list, and leave.
3917 if (!params) { /* need this special case */
3918 params = nasm_malloc(sizeof(*params));
3919 nparam = 0;
3921 params[nparam] = NULL;
3922 *params_array = params;
3923 return m;
3926 * This one wasn't right: look for the next one with the
3927 * same name.
3929 for (m = m->next; m; m = m->next)
3930 if (!mstrcmp(m->name, tline->text, m->casesense))
3931 break;
3935 * After all that, we didn't find one with the right number of
3936 * parameters. Issue a warning, and fail to expand the macro.
3938 error(ERR_WARNING | ERR_WARN_MNP,
3939 "macro `%s' exists, but not taking %d parameters",
3940 tline->text, nparam);
3941 nasm_free(params);
3942 return NULL;
3946 * Expand the multi-line macro call made by the given line, if
3947 * there is one to be expanded. If there is, push the expansion on
3948 * istk->expansion and return 1. Otherwise return 0.
3950 static int expand_mmacro(Token * tline)
3952 Token *startline = tline;
3953 Token *label = NULL;
3954 int dont_prepend = 0;
3955 Token **params, *t, *mtok, *tt;
3956 MMacro *m;
3957 Line *l, *ll;
3958 int i, nparam, *paramlen;
3959 const char *mname;
3961 t = tline;
3962 skip_white_(t);
3963 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3964 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3965 return 0;
3966 mtok = t;
3967 m = is_mmacro(t, &params);
3968 if (m) {
3969 mname = t->text;
3970 } else {
3971 Token *last;
3973 * We have an id which isn't a macro call. We'll assume
3974 * it might be a label; we'll also check to see if a
3975 * colon follows it. Then, if there's another id after
3976 * that lot, we'll check it again for macro-hood.
3978 label = last = t;
3979 t = t->next;
3980 if (tok_type_(t, TOK_WHITESPACE))
3981 last = t, t = t->next;
3982 if (tok_is_(t, ":")) {
3983 dont_prepend = 1;
3984 last = t, t = t->next;
3985 if (tok_type_(t, TOK_WHITESPACE))
3986 last = t, t = t->next;
3988 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3989 return 0;
3990 last->next = NULL;
3991 mname = t->text;
3992 tline = t;
3996 * Fix up the parameters: this involves stripping leading and
3997 * trailing whitespace, then stripping braces if they are
3998 * present.
4000 for (nparam = 0; params[nparam]; nparam++) ;
4001 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4003 for (i = 0; params[i]; i++) {
4004 int brace = false;
4005 int comma = (!m->plus || i < nparam - 1);
4007 t = params[i];
4008 skip_white_(t);
4009 if (tok_is_(t, "{"))
4010 t = t->next, brace = true, comma = false;
4011 params[i] = t;
4012 paramlen[i] = 0;
4013 while (t) {
4014 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4015 break; /* ... because we have hit a comma */
4016 if (comma && t->type == TOK_WHITESPACE
4017 && tok_is_(t->next, ","))
4018 break; /* ... or a space then a comma */
4019 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4020 break; /* ... or a brace */
4021 t = t->next;
4022 paramlen[i]++;
4027 * OK, we have a MMacro structure together with a set of
4028 * parameters. We must now go through the expansion and push
4029 * copies of each Line on to istk->expansion. Substitution of
4030 * parameter tokens and macro-local tokens doesn't get done
4031 * until the single-line macro substitution process; this is
4032 * because delaying them allows us to change the semantics
4033 * later through %rotate.
4035 * First, push an end marker on to istk->expansion, mark this
4036 * macro as in progress, and set up its invocation-specific
4037 * variables.
4039 ll = nasm_malloc(sizeof(Line));
4040 ll->next = istk->expansion;
4041 ll->finishes = m;
4042 ll->first = NULL;
4043 istk->expansion = ll;
4045 m->in_progress = true;
4046 m->params = params;
4047 m->iline = tline;
4048 m->nparam = nparam;
4049 m->rotate = 0;
4050 m->paramlen = paramlen;
4051 m->unique = unique++;
4052 m->lineno = 0;
4054 m->next_active = istk->mstk;
4055 istk->mstk = m;
4057 for (l = m->expansion; l; l = l->next) {
4058 Token **tail;
4060 ll = nasm_malloc(sizeof(Line));
4061 ll->finishes = NULL;
4062 ll->next = istk->expansion;
4063 istk->expansion = ll;
4064 tail = &ll->first;
4066 for (t = l->first; t; t = t->next) {
4067 Token *x = t;
4068 switch (t->type) {
4069 case TOK_PREPROC_Q:
4070 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4071 break;
4072 case TOK_PREPROC_QQ:
4073 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4074 break;
4075 case TOK_PREPROC_ID:
4076 if (t->text[1] == '0' && t->text[2] == '0') {
4077 dont_prepend = -1;
4078 x = label;
4079 if (!x)
4080 continue;
4082 /* fall through */
4083 default:
4084 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4085 break;
4087 tail = &tt->next;
4089 *tail = NULL;
4093 * If we had a label, push it on as the first line of
4094 * the macro expansion.
4096 if (label) {
4097 if (dont_prepend < 0)
4098 free_tlist(startline);
4099 else {
4100 ll = nasm_malloc(sizeof(Line));
4101 ll->finishes = NULL;
4102 ll->next = istk->expansion;
4103 istk->expansion = ll;
4104 ll->first = startline;
4105 if (!dont_prepend) {
4106 while (label->next)
4107 label = label->next;
4108 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4113 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4115 return 1;
4119 * Since preprocessor always operate only on the line that didn't
4120 * arrived yet, we should always use ERR_OFFBY1. Also since user
4121 * won't want to see same error twice (preprocessing is done once
4122 * per pass) we will want to show errors only during pass one.
4124 static void error(int severity, const char *fmt, ...)
4126 va_list arg;
4127 char buff[1024];
4129 /* If we're in a dead branch of IF or something like it, ignore the error */
4130 if (istk && istk->conds && !emitting(istk->conds->state))
4131 return;
4133 va_start(arg, fmt);
4134 vsnprintf(buff, sizeof(buff), fmt, arg);
4135 va_end(arg);
4137 if (istk && istk->mstk && istk->mstk->name)
4138 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
4139 istk->mstk->lineno, buff);
4140 else
4141 _error(severity | ERR_PASS1, "%s", buff);
4144 static void
4145 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4146 ListGen * listgen, StrList **deplist)
4148 _error = errfunc;
4149 cstk = NULL;
4150 istk = nasm_malloc(sizeof(Include));
4151 istk->next = NULL;
4152 istk->conds = NULL;
4153 istk->expansion = NULL;
4154 istk->mstk = NULL;
4155 istk->fp = fopen(file, "r");
4156 istk->fname = NULL;
4157 src_set_fname(nasm_strdup(file));
4158 src_set_linnum(0);
4159 istk->lineinc = 1;
4160 if (!istk->fp)
4161 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4162 file);
4163 defining = NULL;
4164 nested_mac_count = 0;
4165 nested_rep_count = 0;
4166 init_macros();
4167 unique = 0;
4168 if (tasm_compatible_mode) {
4169 stdmacpos = nasm_stdmac;
4170 } else {
4171 stdmacpos = nasm_stdmac_after_tasm;
4173 any_extrastdmac = extrastdmac && *extrastdmac;
4174 do_predef = true;
4175 list = listgen;
4176 evaluate = eval;
4177 pass = apass;
4178 dephead = deptail = deplist;
4179 if (deplist) {
4180 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4181 sl->next = NULL;
4182 strcpy(sl->str, file);
4183 *deptail = sl;
4184 deptail = &sl->next;
4188 static char *pp_getline(void)
4190 char *line;
4191 Token *tline;
4193 while (1) {
4195 * Fetch a tokenized line, either from the macro-expansion
4196 * buffer or from the input file.
4198 tline = NULL;
4199 while (istk->expansion && istk->expansion->finishes) {
4200 Line *l = istk->expansion;
4201 if (!l->finishes->name && l->finishes->in_progress > 1) {
4202 Line *ll;
4205 * This is a macro-end marker for a macro with no
4206 * name, which means it's not really a macro at all
4207 * but a %rep block, and the `in_progress' field is
4208 * more than 1, meaning that we still need to
4209 * repeat. (1 means the natural last repetition; 0
4210 * means termination by %exitrep.) We have
4211 * therefore expanded up to the %endrep, and must
4212 * push the whole block on to the expansion buffer
4213 * again. We don't bother to remove the macro-end
4214 * marker: we'd only have to generate another one
4215 * if we did.
4217 l->finishes->in_progress--;
4218 for (l = l->finishes->expansion; l; l = l->next) {
4219 Token *t, *tt, **tail;
4221 ll = nasm_malloc(sizeof(Line));
4222 ll->next = istk->expansion;
4223 ll->finishes = NULL;
4224 ll->first = NULL;
4225 tail = &ll->first;
4227 for (t = l->first; t; t = t->next) {
4228 if (t->text || t->type == TOK_WHITESPACE) {
4229 tt = *tail =
4230 new_Token(NULL, t->type, t->text, 0);
4231 tail = &tt->next;
4235 istk->expansion = ll;
4237 } else {
4239 * Check whether a `%rep' was started and not ended
4240 * within this macro expansion. This can happen and
4241 * should be detected. It's a fatal error because
4242 * I'm too confused to work out how to recover
4243 * sensibly from it.
4245 if (defining) {
4246 if (defining->name)
4247 error(ERR_PANIC,
4248 "defining with name in expansion");
4249 else if (istk->mstk->name)
4250 error(ERR_FATAL,
4251 "`%%rep' without `%%endrep' within"
4252 " expansion of macro `%s'",
4253 istk->mstk->name);
4257 * FIXME: investigate the relationship at this point between
4258 * istk->mstk and l->finishes
4261 MMacro *m = istk->mstk;
4262 istk->mstk = m->next_active;
4263 if (m->name) {
4265 * This was a real macro call, not a %rep, and
4266 * therefore the parameter information needs to
4267 * be freed.
4269 nasm_free(m->params);
4270 free_tlist(m->iline);
4271 nasm_free(m->paramlen);
4272 l->finishes->in_progress = false;
4273 } else
4274 free_mmacro(m);
4276 istk->expansion = l->next;
4277 nasm_free(l);
4278 list->downlevel(LIST_MACRO);
4281 while (1) { /* until we get a line we can use */
4283 if (istk->expansion) { /* from a macro expansion */
4284 char *p;
4285 Line *l = istk->expansion;
4286 if (istk->mstk)
4287 istk->mstk->lineno++;
4288 tline = l->first;
4289 istk->expansion = l->next;
4290 nasm_free(l);
4291 p = detoken(tline, false);
4292 list->line(LIST_MACRO, p);
4293 nasm_free(p);
4294 break;
4296 line = read_line();
4297 if (line) { /* from the current input file */
4298 line = prepreproc(line);
4299 tline = tokenize(line);
4300 nasm_free(line);
4301 break;
4304 * The current file has ended; work down the istk
4307 Include *i = istk;
4308 fclose(i->fp);
4309 if (i->conds)
4310 error(ERR_FATAL,
4311 "expected `%%endif' before end of file");
4312 /* only set line and file name if there's a next node */
4313 if (i->next) {
4314 src_set_linnum(i->lineno);
4315 nasm_free(src_set_fname(i->fname));
4317 istk = i->next;
4318 list->downlevel(LIST_INCLUDE);
4319 nasm_free(i);
4320 if (!istk)
4321 return NULL;
4326 * We must expand MMacro parameters and MMacro-local labels
4327 * _before_ we plunge into directive processing, to cope
4328 * with things like `%define something %1' such as STRUC
4329 * uses. Unless we're _defining_ a MMacro, in which case
4330 * those tokens should be left alone to go into the
4331 * definition; and unless we're in a non-emitting
4332 * condition, in which case we don't want to meddle with
4333 * anything.
4335 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4336 && !(istk->mstk && !istk->mstk->in_progress))
4337 tline = expand_mmac_params(tline);
4340 * Check the line to see if it's a preprocessor directive.
4342 if (do_directive(tline) == DIRECTIVE_FOUND) {
4343 continue;
4344 } else if (defining) {
4346 * We're defining a multi-line macro. We emit nothing
4347 * at all, and just
4348 * shove the tokenized line on to the macro definition.
4350 Line *l = nasm_malloc(sizeof(Line));
4351 l->next = defining->expansion;
4352 l->first = tline;
4353 l->finishes = NULL;
4354 defining->expansion = l;
4355 continue;
4356 } else if (istk->conds && !emitting(istk->conds->state)) {
4358 * We're in a non-emitting branch of a condition block.
4359 * Emit nothing at all, not even a blank line: when we
4360 * emerge from the condition we'll give a line-number
4361 * directive so we keep our place correctly.
4363 free_tlist(tline);
4364 continue;
4365 } else if (istk->mstk && !istk->mstk->in_progress) {
4367 * We're in a %rep block which has been terminated, so
4368 * we're walking through to the %endrep without
4369 * emitting anything. Emit nothing at all, not even a
4370 * blank line: when we emerge from the %rep block we'll
4371 * give a line-number directive so we keep our place
4372 * correctly.
4374 free_tlist(tline);
4375 continue;
4376 } else {
4377 tline = expand_smacro(tline);
4378 if (!expand_mmacro(tline)) {
4380 * De-tokenize the line again, and emit it.
4382 line = detoken(tline, true);
4383 free_tlist(tline);
4384 break;
4385 } else {
4386 continue; /* expand_mmacro calls free_tlist */
4391 return line;
4394 static void pp_cleanup(int pass)
4396 if (defining) {
4397 if(defining->name) {
4398 error(ERR_NONFATAL,
4399 "end of file while still defining macro `%s'",
4400 defining->name);
4401 } else {
4402 error(ERR_NONFATAL, "end of file while still in %%rep");
4405 free_mmacro(defining);
4407 while (cstk)
4408 ctx_pop();
4409 free_macros();
4410 while (istk) {
4411 Include *i = istk;
4412 istk = istk->next;
4413 fclose(i->fp);
4414 nasm_free(i->fname);
4415 nasm_free(i);
4417 while (cstk)
4418 ctx_pop();
4419 nasm_free(src_set_fname(NULL));
4420 if (pass == 0) {
4421 IncPath *i;
4422 free_llist(predef);
4423 delete_Blocks();
4424 while ((i = ipath)) {
4425 ipath = i->next;
4426 if (i->path)
4427 nasm_free(i->path);
4428 nasm_free(i);
4433 void pp_include_path(char *path)
4435 IncPath *i;
4437 i = nasm_malloc(sizeof(IncPath));
4438 i->path = path ? nasm_strdup(path) : NULL;
4439 i->next = NULL;
4441 if (ipath != NULL) {
4442 IncPath *j = ipath;
4443 while (j->next != NULL)
4444 j = j->next;
4445 j->next = i;
4446 } else {
4447 ipath = i;
4451 void pp_pre_include(char *fname)
4453 Token *inc, *space, *name;
4454 Line *l;
4456 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4457 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4458 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4460 l = nasm_malloc(sizeof(Line));
4461 l->next = predef;
4462 l->first = inc;
4463 l->finishes = NULL;
4464 predef = l;
4467 void pp_pre_define(char *definition)
4469 Token *def, *space;
4470 Line *l;
4471 char *equals;
4473 equals = strchr(definition, '=');
4474 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4475 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4476 if (equals)
4477 *equals = ' ';
4478 space->next = tokenize(definition);
4479 if (equals)
4480 *equals = '=';
4482 l = nasm_malloc(sizeof(Line));
4483 l->next = predef;
4484 l->first = def;
4485 l->finishes = NULL;
4486 predef = l;
4489 void pp_pre_undefine(char *definition)
4491 Token *def, *space;
4492 Line *l;
4494 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4495 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4496 space->next = tokenize(definition);
4498 l = nasm_malloc(sizeof(Line));
4499 l->next = predef;
4500 l->first = def;
4501 l->finishes = NULL;
4502 predef = l;
4506 * Added by Keith Kanios:
4508 * This function is used to assist with "runtime" preprocessor
4509 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4511 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4512 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4515 void pp_runtime(char *definition)
4517 Token *def;
4519 def = tokenize(definition);
4520 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4521 free_tlist(def);
4525 void pp_extra_stdmac(macros_t *macros)
4527 extrastdmac = macros;
4530 static void make_tok_num(Token * tok, int64_t val)
4532 char numbuf[20];
4533 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4534 tok->text = nasm_strdup(numbuf);
4535 tok->type = TOK_NUMBER;
4538 Preproc nasmpp = {
4539 pp_reset,
4540 pp_getline,
4541 pp_cleanup