preproc.c: %include: use expand_smacros() not expand_smacros_in_string()
[nasm.git] / preproc.c
blob3b4c12f8a440fae9e584e8ec027fbfda2ab2053c
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 "stdscan.h"
52 #include "tokens.h"
53 #include "tables.h"
55 typedef struct SMacro SMacro;
56 typedef struct MMacro MMacro;
57 typedef struct Context Context;
58 typedef struct Token Token;
59 typedef struct Blocks Blocks;
60 typedef struct Line Line;
61 typedef struct Include Include;
62 typedef struct Cond Cond;
63 typedef struct IncPath IncPath;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
77 struct SMacro {
78 SMacro *next;
79 char *name;
80 bool casesense;
81 bool in_progress;
82 unsigned int nparam;
83 Token *expansion;
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
103 struct MMacro {
104 MMacro *next;
105 char *name;
106 int nparam_min, nparam_max;
107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
110 int64_t in_progress;
111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
114 Line *expansion;
116 MMacro *next_active;
117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
120 unsigned int nparam, rotate;
121 int *paramlen;
122 uint64_t unique;
123 int lineno; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
129 struct Context {
130 Context *next;
131 char *name;
132 struct hash_table localmac;
133 uint32_t number;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
155 enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
161 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
165 struct Token {
166 Token *next;
167 char *text;
168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 enum pp_token_type type;
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
175 * lists of Tokens.
177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
192 * the line is blank.
194 struct Line {
195 Line *next;
196 MMacro *finishes;
197 Token *first;
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
204 struct Include {
205 Include *next;
206 FILE *fp;
207 Cond *conds;
208 Line *expansion;
209 char *fname;
210 int lineno, lineinc;
211 MMacro *mstk; /* stack of active macros/reps */
215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
219 struct IncPath {
220 IncPath *next;
221 char *path;
225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
231 struct Cond {
232 Cond *next;
233 int state;
235 enum {
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
244 COND_IF_TRUE, COND_IF_FALSE,
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
250 COND_ELSE_TRUE, COND_ELSE_FALSE,
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
259 COND_NEVER
261 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
264 * These defines are used as the possible return values for do_directive
266 #define NO_DIRECTIVE_FOUND 0
267 #define DIRECTIVE_FOUND 1
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
275 static const char * const conditions[] = {
276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
280 enum pp_conds {
281 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
282 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
283 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
284 c_none = -1
286 static const enum pp_conds inverse_ccs[] = {
287 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
288 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,
289 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
293 * Directive names.
295 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
296 static int is_condition(enum preproc_token arg)
298 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
301 /* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
307 enum {
308 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
309 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
312 static const char * const tasm_directives[] = {
313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
317 static int StackSize = 4;
318 static char *StackPointer = "ebp";
319 static int ArgOffset = 8;
320 static int LocalOffset = 0;
322 static Context *cstk;
323 static Include *istk;
324 static IncPath *ipath = NULL;
326 static efunc _error; /* Pointer to client-provided error reporting function */
327 static evalfunc evaluate;
329 static int pass; /* HACK: pass 0 = generate dependencies only */
330 static StrList **dephead, **deptail; /* Dependency list */
332 static uint64_t unique; /* unique identifier numbers */
334 static Line *predef = NULL;
336 static ListGen *list;
339 * The current set of multi-line macros we have defined.
341 static struct hash_table mmacros;
344 * The current set of single-line macros we have defined.
346 static struct hash_table smacros;
349 * The multi-line macro we are currently defining, or the %rep
350 * block we are currently reading, if any.
352 static MMacro *defining;
355 * The number of macro parameters to allocate space for at a time.
357 #define PARAM_DELTA 16
360 * The standard macro set: defined in macros.c in the array nasm_stdmac.
361 * This gives our position in the macro set, when we're processing it.
363 static const char * const *stdmacpos;
366 * The extra standard macros that come from the object format, if
367 * any.
369 static const char * const *extrastdmac = NULL;
370 bool any_extrastdmac;
373 * Tokens are allocated in blocks to improve speed
375 #define TOKEN_BLOCKSIZE 4096
376 static Token *freeTokens = NULL;
377 struct Blocks {
378 Blocks *next;
379 void *chunk;
382 static Blocks blocks = { NULL, NULL };
385 * Forward declarations.
387 static Token *expand_mmac_params(Token * tline);
388 static Token *expand_smacro(Token * tline);
389 static Token *expand_id(Token * tline);
390 static Context *get_ctx(char *name, bool all_contexts);
391 static void make_tok_num(Token * tok, int64_t val);
392 static void error(int severity, const char *fmt, ...);
393 static void *new_Block(size_t size);
394 static void delete_Blocks(void);
395 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
396 static Token *delete_Token(Token * t);
399 * Macros for safe checking of token pointers, avoid *(NULL)
401 #define tok_type_(x,t) ((x) && (x)->type == (t))
402 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
403 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
404 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
406 /* Handle TASM specific directives, which do not contain a % in
407 * front of them. We do it here because I could not find any other
408 * place to do it for the moment, and it is a hack (ideally it would
409 * be nice to be able to use the NASM pre-processor to do it).
411 static char *check_tasm_directive(char *line)
413 int32_t i, j, k, m, len;
414 char *p = line, *oldline, oldchar;
416 /* Skip whitespace */
417 while (isspace(*p) && *p != 0)
418 p++;
420 /* Binary search for the directive name */
421 i = -1;
422 j = elements(tasm_directives);
423 len = 0;
424 while (!isspace(p[len]) && p[len] != 0)
425 len++;
426 if (len) {
427 oldchar = p[len];
428 p[len] = 0;
429 while (j - i > 1) {
430 k = (j + i) / 2;
431 m = nasm_stricmp(p, tasm_directives[k]);
432 if (m == 0) {
433 /* We have found a directive, so jam a % in front of it
434 * so that NASM will then recognise it as one if it's own.
436 p[len] = oldchar;
437 len = strlen(p);
438 oldline = line;
439 line = nasm_malloc(len + 2);
440 line[0] = '%';
441 if (k == TM_IFDIFI) {
442 /* NASM does not recognise IFDIFI, so we convert it to
443 * %ifdef BOGUS. This is not used in NASM comaptible
444 * code, but does need to parse for the TASM macro
445 * package.
447 strcpy(line + 1, "ifdef BOGUS");
448 } else {
449 memcpy(line + 1, p, len + 1);
451 nasm_free(oldline);
452 return line;
453 } else if (m < 0) {
454 j = k;
455 } else
456 i = k;
458 p[len] = oldchar;
460 return line;
464 * The pre-preprocessing stage... This function translates line
465 * number indications as they emerge from GNU cpp (`# lineno "file"
466 * flags') into NASM preprocessor line number indications (`%line
467 * lineno file').
469 static char *prepreproc(char *line)
471 int lineno, fnlen;
472 char *fname, *oldline;
474 if (line[0] == '#' && line[1] == ' ') {
475 oldline = line;
476 fname = oldline + 2;
477 lineno = atoi(fname);
478 fname += strspn(fname, "0123456789 ");
479 if (*fname == '"')
480 fname++;
481 fnlen = strcspn(fname, "\"");
482 line = nasm_malloc(20 + fnlen);
483 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
484 nasm_free(oldline);
486 if (tasm_compatible_mode)
487 return check_tasm_directive(line);
488 return line;
492 * Free a linked list of tokens.
494 static void free_tlist(Token * list)
496 while (list) {
497 list = delete_Token(list);
502 * Free a linked list of lines.
504 static void free_llist(Line * list)
506 Line *l;
507 while (list) {
508 l = list;
509 list = list->next;
510 free_tlist(l->first);
511 nasm_free(l);
516 * Free an MMacro
518 static void free_mmacro(MMacro * m)
520 nasm_free(m->name);
521 free_tlist(m->dlist);
522 nasm_free(m->defaults);
523 free_llist(m->expansion);
524 nasm_free(m);
528 * Free all currently defined macros, and free the hash tables
530 static void free_smacro_table(struct hash_table *smt)
532 SMacro *s;
533 const char *key;
534 struct hash_tbl_node *it = NULL;
536 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
537 nasm_free((void *)key);
538 while (s) {
539 SMacro *ns = s->next;
540 nasm_free(s->name);
541 free_tlist(s->expansion);
542 nasm_free(s);
543 s = ns;
546 hash_free(smt);
549 static void free_mmacro_table(struct hash_table *mmt)
551 MMacro *m;
552 const char *key;
553 struct hash_tbl_node *it = NULL;
555 it = NULL;
556 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
557 nasm_free((void *)key);
558 while (m) {
559 MMacro *nm = m->next;
560 free_mmacro(m);
561 m = nm;
564 hash_free(mmt);
567 static void free_macros(void)
569 free_smacro_table(&smacros);
570 free_mmacro_table(&mmacros);
574 * Initialize the hash tables
576 static void init_macros(void)
578 hash_init(&smacros, HASH_LARGE);
579 hash_init(&mmacros, HASH_LARGE);
583 * Pop the context stack.
585 static void ctx_pop(void)
587 Context *c = cstk;
589 cstk = cstk->next;
590 free_smacro_table(&c->localmac);
591 nasm_free(c->name);
592 nasm_free(c);
596 * Search for a key in the hash index; adding it if necessary
597 * (in which case we initialize the data pointer to NULL.)
599 static void **
600 hash_findi_add(struct hash_table *hash, const char *str)
602 struct hash_insert hi;
603 void **r;
604 char *strx;
606 r = hash_findi(hash, str, &hi);
607 if (r)
608 return r;
610 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
611 return hash_add(&hi, strx, NULL);
615 * Like hash_findi, but returns the data element rather than a pointer
616 * to it. Used only when not adding a new element, hence no third
617 * argument.
619 static void *
620 hash_findix(struct hash_table *hash, const char *str)
622 void **p;
624 p = hash_findi(hash, str, NULL);
625 return p ? *p : NULL;
628 #define BUF_DELTA 512
630 * Read a line from the top file in istk, handling multiple CR/LFs
631 * at the end of the line read, and handling spurious ^Zs. Will
632 * return lines from the standard macro set if this has not already
633 * been done.
635 static char *read_line(void)
637 char *buffer, *p, *q;
638 int bufsize, continued_count;
640 if (stdmacpos) {
641 if (*stdmacpos) {
642 char *ret = nasm_strdup(*stdmacpos++);
643 if (!*stdmacpos && any_extrastdmac) {
644 stdmacpos = extrastdmac;
645 any_extrastdmac = false;
646 return ret;
649 * Nasty hack: here we push the contents of `predef' on
650 * to the top-level expansion stack, since this is the
651 * most convenient way to implement the pre-include and
652 * pre-define features.
654 if (!*stdmacpos) {
655 Line *pd, *l;
656 Token *head, **tail, *t;
658 for (pd = predef; pd; pd = pd->next) {
659 head = NULL;
660 tail = &head;
661 for (t = pd->first; t; t = t->next) {
662 *tail = new_Token(NULL, t->type, t->text, 0);
663 tail = &(*tail)->next;
665 l = nasm_malloc(sizeof(Line));
666 l->next = istk->expansion;
667 l->first = head;
668 l->finishes = false;
669 istk->expansion = l;
672 return ret;
673 } else {
674 stdmacpos = NULL;
678 bufsize = BUF_DELTA;
679 buffer = nasm_malloc(BUF_DELTA);
680 p = buffer;
681 continued_count = 0;
682 while (1) {
683 q = fgets(p, bufsize - (p - buffer), istk->fp);
684 if (!q)
685 break;
686 p += strlen(p);
687 if (p > buffer && p[-1] == '\n') {
688 /* Convert backslash-CRLF line continuation sequences into
689 nothing at all (for DOS and Windows) */
690 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
691 p -= 3;
692 *p = 0;
693 continued_count++;
695 /* Also convert backslash-LF line continuation sequences into
696 nothing at all (for Unix) */
697 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
698 p -= 2;
699 *p = 0;
700 continued_count++;
701 } else {
702 break;
705 if (p - buffer > bufsize - 10) {
706 int32_t offset = p - buffer;
707 bufsize += BUF_DELTA;
708 buffer = nasm_realloc(buffer, bufsize);
709 p = buffer + offset; /* prevent stale-pointer problems */
713 if (!q && p == buffer) {
714 nasm_free(buffer);
715 return NULL;
718 src_set_linnum(src_get_linnum() + istk->lineinc +
719 (continued_count * istk->lineinc));
722 * Play safe: remove CRs as well as LFs, if any of either are
723 * present at the end of the line.
725 while (--p >= buffer && (*p == '\n' || *p == '\r'))
726 *p = '\0';
729 * Handle spurious ^Z, which may be inserted into source files
730 * by some file transfer utilities.
732 buffer[strcspn(buffer, "\032")] = '\0';
734 list->line(LIST_READ, buffer);
736 return buffer;
740 * Tokenize a line of text. This is a very simple process since we
741 * don't need to parse the value out of e.g. numeric tokens: we
742 * simply split one string into many.
744 static Token *tokenize(char *line)
746 char *p = line;
747 enum pp_token_type type;
748 Token *list = NULL;
749 Token *t, **tail = &list;
751 while (*line) {
752 p = line;
753 if (*p == '%') {
754 p++;
755 if (isdigit(*p) ||
756 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
757 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
758 do {
759 p++;
761 while (isdigit(*p));
762 type = TOK_PREPROC_ID;
763 } else if (*p == '{') {
764 p++;
765 while (*p && *p != '}') {
766 p[-1] = *p;
767 p++;
769 p[-1] = '\0';
770 if (*p)
771 p++;
772 type = TOK_PREPROC_ID;
773 } else if (*p == '?') {
774 type = TOK_PREPROC_Q; /* %? */
775 p++;
776 if (*p == '?') {
777 type = TOK_PREPROC_QQ; /* %?? */
778 p++;
780 } else if (isidchar(*p) ||
781 ((*p == '!' || *p == '%' || *p == '$') &&
782 isidchar(p[1]))) {
783 do {
784 p++;
786 while (isidchar(*p));
787 type = TOK_PREPROC_ID;
788 } else {
789 type = TOK_OTHER;
790 if (*p == '%')
791 p++;
793 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
794 type = TOK_ID;
795 p++;
796 while (*p && isidchar(*p))
797 p++;
798 } else if (*p == '\'' || *p == '"') {
800 * A string token.
802 char c = *p;
803 p++;
804 type = TOK_STRING;
805 while (*p && *p != c)
806 p++;
808 if (*p) {
809 p++;
810 } else {
811 error(ERR_WARNING, "unterminated string");
812 /* Handling unterminated strings by UNV */
813 /* type = -1; */
815 } else if (isnumstart(*p)) {
816 bool is_hex = false;
817 bool is_float = false;
818 bool has_e = false;
819 char c, *r;
822 * A numeric token.
825 if (*p == '$') {
826 p++;
827 is_hex = true;
830 for (;;) {
831 c = *p++;
833 if (!is_hex && (c == 'e' || c == 'E')) {
834 has_e = true;
835 if (*p == '+' || *p == '-') {
836 /* e can only be followed by +/- if it is either a
837 prefixed hex number or a floating-point number */
838 p++;
839 is_float = true;
841 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
842 is_hex = true;
843 } else if (c == 'P' || c == 'p') {
844 is_float = true;
845 if (*p == '+' || *p == '-')
846 p++;
847 } else if (isnumchar(c) || c == '_')
848 ; /* just advance */
849 else if (c == '.') {
850 /* we need to deal with consequences of the legacy
851 parser, like "1.nolist" being two tokens
852 (TOK_NUMBER, TOK_ID) here; at least give it
853 a shot for now. In the future, we probably need
854 a flex-based scanner with proper pattern matching
855 to do it as well as it can be done. Nothing in
856 the world is going to help the person who wants
857 0x123.p16 interpreted as two tokens, though. */
858 r = p;
859 while (*r == '_')
860 r++;
862 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
863 (!is_hex && (*r == 'e' || *r == 'E')) ||
864 (*r == 'p' || *r == 'P')) {
865 p = r;
866 is_float = true;
867 } else
868 break; /* Terminate the token */
869 } else
870 break;
872 p--; /* Point to first character beyond number */
874 if (has_e && !is_hex) {
875 /* 1e13 is floating-point, but 1e13h is not */
876 is_float = true;
879 type = is_float ? TOK_FLOAT : TOK_NUMBER;
880 } else if (isspace(*p)) {
881 type = TOK_WHITESPACE;
882 p++;
883 while (*p && isspace(*p))
884 p++;
886 * Whitespace just before end-of-line is discarded by
887 * pretending it's a comment; whitespace just before a
888 * comment gets lumped into the comment.
890 if (!*p || *p == ';') {
891 type = TOK_COMMENT;
892 while (*p)
893 p++;
895 } else if (*p == ';') {
896 type = TOK_COMMENT;
897 while (*p)
898 p++;
899 } else {
901 * Anything else is an operator of some kind. We check
902 * for all the double-character operators (>>, <<, //,
903 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
904 * else is a single-character operator.
906 type = TOK_OTHER;
907 if ((p[0] == '>' && p[1] == '>') ||
908 (p[0] == '<' && p[1] == '<') ||
909 (p[0] == '/' && p[1] == '/') ||
910 (p[0] == '<' && p[1] == '=') ||
911 (p[0] == '>' && p[1] == '=') ||
912 (p[0] == '=' && p[1] == '=') ||
913 (p[0] == '!' && p[1] == '=') ||
914 (p[0] == '<' && p[1] == '>') ||
915 (p[0] == '&' && p[1] == '&') ||
916 (p[0] == '|' && p[1] == '|') ||
917 (p[0] == '^' && p[1] == '^')) {
918 p++;
920 p++;
923 /* Handling unterminated string by UNV */
924 /*if (type == -1)
926 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
927 t->text[p-line] = *line;
928 tail = &t->next;
930 else */
931 if (type != TOK_COMMENT) {
932 *tail = t = new_Token(NULL, type, line, p - line);
933 tail = &t->next;
935 line = p;
937 return list;
941 * this function allocates a new managed block of memory and
942 * returns a pointer to the block. The managed blocks are
943 * deleted only all at once by the delete_Blocks function.
945 static void *new_Block(size_t size)
947 Blocks *b = &blocks;
949 /* first, get to the end of the linked list */
950 while (b->next)
951 b = b->next;
952 /* now allocate the requested chunk */
953 b->chunk = nasm_malloc(size);
955 /* now allocate a new block for the next request */
956 b->next = nasm_malloc(sizeof(Blocks));
957 /* and initialize the contents of the new block */
958 b->next->next = NULL;
959 b->next->chunk = NULL;
960 return b->chunk;
964 * this function deletes all managed blocks of memory
966 static void delete_Blocks(void)
968 Blocks *a, *b = &blocks;
971 * keep in mind that the first block, pointed to by blocks
972 * is a static and not dynamically allocated, so we don't
973 * free it.
975 while (b) {
976 if (b->chunk)
977 nasm_free(b->chunk);
978 a = b;
979 b = b->next;
980 if (a != &blocks)
981 nasm_free(a);
986 * this function creates a new Token and passes a pointer to it
987 * back to the caller. It sets the type and text elements, and
988 * also the mac and next elements to NULL.
990 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
992 Token *t;
993 int i;
995 if (freeTokens == NULL) {
996 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
997 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
998 freeTokens[i].next = &freeTokens[i + 1];
999 freeTokens[i].next = NULL;
1001 t = freeTokens;
1002 freeTokens = t->next;
1003 t->next = next;
1004 t->mac = NULL;
1005 t->type = type;
1006 if (type == TOK_WHITESPACE || text == NULL) {
1007 t->text = NULL;
1008 } else {
1009 if (txtlen == 0)
1010 txtlen = strlen(text);
1011 t->text = nasm_malloc(1 + txtlen);
1012 strncpy(t->text, text, txtlen);
1013 t->text[txtlen] = '\0';
1015 return t;
1018 static Token *delete_Token(Token * t)
1020 Token *next = t->next;
1021 nasm_free(t->text);
1022 t->next = freeTokens;
1023 freeTokens = t;
1024 return next;
1028 * Convert a line of tokens back into text.
1029 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1030 * will be transformed into ..@ctxnum.xxx
1032 static char *detoken(Token * tlist, int expand_locals)
1034 Token *t;
1035 int len;
1036 char *line, *p;
1037 const char *q;
1039 len = 0;
1040 for (t = tlist; t; t = t->next) {
1041 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1042 char *p = getenv(t->text + 2);
1043 nasm_free(t->text);
1044 if (p)
1045 t->text = nasm_strdup(p);
1046 else
1047 t->text = NULL;
1049 /* Expand local macros here and not during preprocessing */
1050 if (expand_locals &&
1051 t->type == TOK_PREPROC_ID && t->text &&
1052 t->text[0] == '%' && t->text[1] == '$') {
1053 Context *ctx = get_ctx(t->text, false);
1054 if (ctx) {
1055 char buffer[40];
1056 char *p, *q = t->text + 2;
1058 q += strspn(q, "$");
1059 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1060 p = nasm_strcat(buffer, q);
1061 nasm_free(t->text);
1062 t->text = p;
1065 if (t->type == TOK_WHITESPACE) {
1066 len++;
1067 } else if (t->text) {
1068 len += strlen(t->text);
1071 p = line = nasm_malloc(len + 1);
1072 for (t = tlist; t; t = t->next) {
1073 if (t->type == TOK_WHITESPACE) {
1074 *p++ = ' ';
1075 } else if (t->text) {
1076 q = t->text;
1077 while (*q)
1078 *p++ = *q++;
1081 *p = '\0';
1082 return line;
1086 * A scanner, suitable for use by the expression evaluator, which
1087 * operates on a line of Tokens. Expects a pointer to a pointer to
1088 * the first token in the line to be passed in as its private_data
1089 * field.
1091 * FIX: This really needs to be unified with stdscan.
1093 static int ppscan(void *private_data, struct tokenval *tokval)
1095 Token **tlineptr = private_data;
1096 Token *tline;
1097 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1099 do {
1100 tline = *tlineptr;
1101 *tlineptr = tline ? tline->next : NULL;
1103 while (tline && (tline->type == TOK_WHITESPACE ||
1104 tline->type == TOK_COMMENT));
1106 if (!tline)
1107 return tokval->t_type = TOKEN_EOS;
1109 tokval->t_charptr = tline->text;
1111 if (tline->text[0] == '$' && !tline->text[1])
1112 return tokval->t_type = TOKEN_HERE;
1113 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1114 return tokval->t_type = TOKEN_BASE;
1116 if (tline->type == TOK_ID) {
1117 p = tokval->t_charptr = tline->text;
1118 if (p[0] == '$') {
1119 tokval->t_charptr++;
1120 return tokval->t_type = TOKEN_ID;
1123 for (r = p, s = ourcopy; *r; r++) {
1124 if (r >= p+MAX_KEYWORD)
1125 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1126 *s++ = tolower(*r);
1128 *s = '\0';
1129 /* right, so we have an identifier sitting in temp storage. now,
1130 * is it actually a register or instruction name, or what? */
1131 return nasm_token_hash(ourcopy, tokval);
1134 if (tline->type == TOK_NUMBER) {
1135 bool rn_error;
1136 tokval->t_integer = readnum(tline->text, &rn_error);
1137 if (rn_error)
1138 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1139 tokval->t_charptr = tline->text;
1140 return tokval->t_type = TOKEN_NUM;
1143 if (tline->type == TOK_FLOAT) {
1144 return tokval->t_type = TOKEN_FLOAT;
1147 if (tline->type == TOK_STRING) {
1148 bool rn_warn;
1149 char q, *r;
1150 int l;
1152 r = tline->text;
1153 q = *r++;
1154 l = strlen(r);
1156 if (l == 0 || r[l - 1] != q)
1157 return tokval->t_type = TOKEN_ERRNUM;
1158 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1159 if (rn_warn)
1160 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1161 tokval->t_charptr = NULL;
1162 return tokval->t_type = TOKEN_NUM;
1165 if (tline->type == TOK_OTHER) {
1166 if (!strcmp(tline->text, "<<"))
1167 return tokval->t_type = TOKEN_SHL;
1168 if (!strcmp(tline->text, ">>"))
1169 return tokval->t_type = TOKEN_SHR;
1170 if (!strcmp(tline->text, "//"))
1171 return tokval->t_type = TOKEN_SDIV;
1172 if (!strcmp(tline->text, "%%"))
1173 return tokval->t_type = TOKEN_SMOD;
1174 if (!strcmp(tline->text, "=="))
1175 return tokval->t_type = TOKEN_EQ;
1176 if (!strcmp(tline->text, "<>"))
1177 return tokval->t_type = TOKEN_NE;
1178 if (!strcmp(tline->text, "!="))
1179 return tokval->t_type = TOKEN_NE;
1180 if (!strcmp(tline->text, "<="))
1181 return tokval->t_type = TOKEN_LE;
1182 if (!strcmp(tline->text, ">="))
1183 return tokval->t_type = TOKEN_GE;
1184 if (!strcmp(tline->text, "&&"))
1185 return tokval->t_type = TOKEN_DBL_AND;
1186 if (!strcmp(tline->text, "^^"))
1187 return tokval->t_type = TOKEN_DBL_XOR;
1188 if (!strcmp(tline->text, "||"))
1189 return tokval->t_type = TOKEN_DBL_OR;
1193 * We have no other options: just return the first character of
1194 * the token text.
1196 return tokval->t_type = tline->text[0];
1200 * Compare a string to the name of an existing macro; this is a
1201 * simple wrapper which calls either strcmp or nasm_stricmp
1202 * depending on the value of the `casesense' parameter.
1204 static int mstrcmp(const char *p, const char *q, bool casesense)
1206 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1210 * Return the Context structure associated with a %$ token. Return
1211 * NULL, having _already_ reported an error condition, if the
1212 * context stack isn't deep enough for the supplied number of $
1213 * signs.
1214 * If all_contexts == true, contexts that enclose current are
1215 * also scanned for such smacro, until it is found; if not -
1216 * only the context that directly results from the number of $'s
1217 * in variable's name.
1219 static Context *get_ctx(char *name, bool all_contexts)
1221 Context *ctx;
1222 SMacro *m;
1223 int i;
1225 if (!name || name[0] != '%' || name[1] != '$')
1226 return NULL;
1228 if (!cstk) {
1229 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1230 return NULL;
1233 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1234 ctx = ctx->next;
1235 /* i--; Lino - 02/25/02 */
1237 if (!ctx) {
1238 error(ERR_NONFATAL, "`%s': context stack is only"
1239 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1240 return NULL;
1242 if (!all_contexts)
1243 return ctx;
1245 do {
1246 /* Search for this smacro in found context */
1247 m = hash_findix(&ctx->localmac, name);
1248 while (m) {
1249 if (!mstrcmp(m->name, name, m->casesense))
1250 return ctx;
1251 m = m->next;
1253 ctx = ctx->next;
1255 while (ctx);
1256 return NULL;
1260 * Check to see if a file is already in a string list
1262 static bool in_list(const StrList *list, const char *str)
1264 while (list) {
1265 if (!strcmp(list->str, str))
1266 return true;
1267 list = list->next;
1269 return false;
1273 * Open an include file. This routine must always return a valid
1274 * file pointer if it returns - it's responsible for throwing an
1275 * ERR_FATAL and bombing out completely if not. It should also try
1276 * the include path one by one until it finds the file or reaches
1277 * the end of the path.
1279 static FILE *inc_fopen(const char *file)
1281 FILE *fp;
1282 char *prefix = "";
1283 IncPath *ip = ipath;
1284 int len = strlen(file);
1285 size_t prefix_len = 0;
1286 StrList *sl;
1288 while (1) {
1289 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1290 memcpy(sl->str, prefix, prefix_len);
1291 memcpy(sl->str+prefix_len, file, len+1);
1292 fp = fopen(sl->str, "r");
1293 if (fp && dephead && !in_list(*dephead, sl->str)) {
1294 sl->next = NULL;
1295 *deptail = sl;
1296 deptail = &sl->next;
1297 } else {
1298 nasm_free(sl);
1300 if (fp)
1301 return fp;
1302 if (!ip)
1303 break;
1304 prefix = ip->path;
1305 ip = ip->next;
1306 if (prefix) {
1307 prefix_len = strlen(prefix);
1308 } else {
1309 /* -MG given and file not found */
1310 if (dephead && !in_list(*dephead, file)) {
1311 sl = nasm_malloc(len+1+sizeof sl->next);
1312 sl->next = NULL;
1313 strcpy(sl->str, file);
1314 *deptail = sl;
1315 deptail = &sl->next;
1317 return NULL;
1321 error(ERR_FATAL, "unable to open include file `%s'", file);
1322 return NULL; /* never reached - placate compilers */
1326 * Determine if we should warn on defining a single-line macro of
1327 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1328 * return true if _any_ single-line macro of that name is defined.
1329 * Otherwise, will return true if a single-line macro with either
1330 * `nparam' or no parameters is defined.
1332 * If a macro with precisely the right number of parameters is
1333 * defined, or nparam is -1, the address of the definition structure
1334 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1335 * is NULL, no action will be taken regarding its contents, and no
1336 * error will occur.
1338 * Note that this is also called with nparam zero to resolve
1339 * `ifdef'.
1341 * If you already know which context macro belongs to, you can pass
1342 * the context pointer as first parameter; if you won't but name begins
1343 * with %$ the context will be automatically computed. If all_contexts
1344 * is true, macro will be searched in outer contexts as well.
1346 static bool
1347 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1348 bool nocase)
1350 struct hash_table *smtbl;
1351 SMacro *m;
1353 if (ctx) {
1354 smtbl = &ctx->localmac;
1355 } else if (name[0] == '%' && name[1] == '$') {
1356 if (cstk)
1357 ctx = get_ctx(name, false);
1358 if (!ctx)
1359 return false; /* got to return _something_ */
1360 smtbl = &ctx->localmac;
1361 } else {
1362 smtbl = &smacros;
1364 m = (SMacro *) hash_findix(smtbl, name);
1366 while (m) {
1367 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1368 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1369 if (defn) {
1370 if (nparam == (int) m->nparam || nparam == -1)
1371 *defn = m;
1372 else
1373 *defn = NULL;
1375 return true;
1377 m = m->next;
1380 return false;
1384 * Count and mark off the parameters in a multi-line macro call.
1385 * This is called both from within the multi-line macro expansion
1386 * code, and also to mark off the default parameters when provided
1387 * in a %macro definition line.
1389 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1391 int paramsize, brace;
1393 *nparam = paramsize = 0;
1394 *params = NULL;
1395 while (t) {
1396 if (*nparam >= paramsize) {
1397 paramsize += PARAM_DELTA;
1398 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1400 skip_white_(t);
1401 brace = false;
1402 if (tok_is_(t, "{"))
1403 brace = true;
1404 (*params)[(*nparam)++] = t;
1405 while (tok_isnt_(t, brace ? "}" : ","))
1406 t = t->next;
1407 if (t) { /* got a comma/brace */
1408 t = t->next;
1409 if (brace) {
1411 * Now we've found the closing brace, look further
1412 * for the comma.
1414 skip_white_(t);
1415 if (tok_isnt_(t, ",")) {
1416 error(ERR_NONFATAL,
1417 "braces do not enclose all of macro parameter");
1418 while (tok_isnt_(t, ","))
1419 t = t->next;
1421 if (t)
1422 t = t->next; /* eat the comma */
1429 * Determine whether one of the various `if' conditions is true or
1430 * not.
1432 * We must free the tline we get passed.
1434 static bool if_condition(Token * tline, enum preproc_token ct)
1436 enum pp_conditional i = PP_COND(ct);
1437 bool j;
1438 Token *t, *tt, **tptr, *origline;
1439 struct tokenval tokval;
1440 expr *evalresult;
1441 enum pp_token_type needtype;
1443 origline = tline;
1445 switch (i) {
1446 case PPC_IFCTX:
1447 j = false; /* have we matched yet? */
1448 while (cstk && tline) {
1449 skip_white_(tline);
1450 if (!tline || tline->type != TOK_ID) {
1451 error(ERR_NONFATAL,
1452 "`%s' expects context identifiers", pp_directives[ct]);
1453 free_tlist(origline);
1454 return -1;
1456 if (!nasm_stricmp(tline->text, cstk->name))
1457 j = true;
1458 tline = tline->next;
1460 break;
1462 case PPC_IFDEF:
1463 j = false; /* have we matched yet? */
1464 while (tline) {
1465 skip_white_(tline);
1466 if (!tline || (tline->type != TOK_ID &&
1467 (tline->type != TOK_PREPROC_ID ||
1468 tline->text[1] != '$'))) {
1469 error(ERR_NONFATAL,
1470 "`%s' expects macro identifiers", pp_directives[ct]);
1471 goto fail;
1473 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1474 j = true;
1475 tline = tline->next;
1477 break;
1479 case PPC_IFIDN:
1480 case PPC_IFIDNI:
1481 tline = expand_smacro(tline);
1482 t = tt = tline;
1483 while (tok_isnt_(tt, ","))
1484 tt = tt->next;
1485 if (!tt) {
1486 error(ERR_NONFATAL,
1487 "`%s' expects two comma-separated arguments",
1488 pp_directives[ct]);
1489 goto fail;
1491 tt = tt->next;
1492 j = true; /* assume equality unless proved not */
1493 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1494 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1495 error(ERR_NONFATAL, "`%s': more than one comma on line",
1496 pp_directives[ct]);
1497 goto fail;
1499 if (t->type == TOK_WHITESPACE) {
1500 t = t->next;
1501 continue;
1503 if (tt->type == TOK_WHITESPACE) {
1504 tt = tt->next;
1505 continue;
1507 if (tt->type != t->type) {
1508 j = false; /* found mismatching tokens */
1509 break;
1511 /* Unify surrounding quotes for strings */
1512 if (t->type == TOK_STRING) {
1513 tt->text[0] = t->text[0];
1514 tt->text[strlen(tt->text) - 1] = t->text[0];
1516 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1517 j = false; /* found mismatching tokens */
1518 break;
1521 t = t->next;
1522 tt = tt->next;
1524 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1525 j = false; /* trailing gunk on one end or other */
1526 break;
1528 case PPC_IFMACRO:
1530 bool found = false;
1531 MMacro searching, *mmac;
1533 tline = tline->next;
1534 skip_white_(tline);
1535 tline = expand_id(tline);
1536 if (!tok_type_(tline, TOK_ID)) {
1537 error(ERR_NONFATAL,
1538 "`%s' expects a macro name", pp_directives[ct]);
1539 goto fail;
1541 searching.name = nasm_strdup(tline->text);
1542 searching.casesense = true;
1543 searching.plus = false;
1544 searching.nolist = false;
1545 searching.in_progress = 0;
1546 searching.rep_nest = NULL;
1547 searching.nparam_min = 0;
1548 searching.nparam_max = INT_MAX;
1549 tline = expand_smacro(tline->next);
1550 skip_white_(tline);
1551 if (!tline) {
1552 } else if (!tok_type_(tline, TOK_NUMBER)) {
1553 error(ERR_NONFATAL,
1554 "`%s' expects a parameter count or nothing",
1555 pp_directives[ct]);
1556 } else {
1557 searching.nparam_min = searching.nparam_max =
1558 readnum(tline->text, &j);
1559 if (j)
1560 error(ERR_NONFATAL,
1561 "unable to parse parameter count `%s'",
1562 tline->text);
1564 if (tline && tok_is_(tline->next, "-")) {
1565 tline = tline->next->next;
1566 if (tok_is_(tline, "*"))
1567 searching.nparam_max = INT_MAX;
1568 else if (!tok_type_(tline, TOK_NUMBER))
1569 error(ERR_NONFATAL,
1570 "`%s' expects a parameter count after `-'",
1571 pp_directives[ct]);
1572 else {
1573 searching.nparam_max = readnum(tline->text, &j);
1574 if (j)
1575 error(ERR_NONFATAL,
1576 "unable to parse parameter count `%s'",
1577 tline->text);
1578 if (searching.nparam_min > searching.nparam_max)
1579 error(ERR_NONFATAL,
1580 "minimum parameter count exceeds maximum");
1583 if (tline && tok_is_(tline->next, "+")) {
1584 tline = tline->next;
1585 searching.plus = true;
1587 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1588 while (mmac) {
1589 if (!strcmp(mmac->name, searching.name) &&
1590 (mmac->nparam_min <= searching.nparam_max
1591 || searching.plus)
1592 && (searching.nparam_min <= mmac->nparam_max
1593 || mmac->plus)) {
1594 found = true;
1595 break;
1597 mmac = mmac->next;
1599 nasm_free(searching.name);
1600 j = found;
1601 break;
1604 case PPC_IFID:
1605 needtype = TOK_ID;
1606 goto iftype;
1607 case PPC_IFNUM:
1608 needtype = TOK_NUMBER;
1609 goto iftype;
1610 case PPC_IFSTR:
1611 needtype = TOK_STRING;
1612 goto iftype;
1614 iftype:
1615 t = tline = expand_smacro(tline);
1617 while (tok_type_(t, TOK_WHITESPACE) ||
1618 (needtype == TOK_NUMBER &&
1619 tok_type_(t, TOK_OTHER) &&
1620 (t->text[0] == '-' || t->text[0] == '+') &&
1621 !t->text[1]))
1622 t = t->next;
1624 j = tok_type_(t, needtype);
1625 break;
1627 case PPC_IFTOKEN:
1628 t = tline = expand_smacro(tline);
1629 while (tok_type_(t, TOK_WHITESPACE))
1630 t = t->next;
1632 j = false;
1633 if (t) {
1634 t = t->next; /* Skip the actual token */
1635 while (tok_type_(t, TOK_WHITESPACE))
1636 t = t->next;
1637 j = !t; /* Should be nothing left */
1639 break;
1641 case PPC_IFEMPTY:
1642 t = tline = expand_smacro(tline);
1643 while (tok_type_(t, TOK_WHITESPACE))
1644 t = t->next;
1646 j = !t; /* Should be empty */
1647 break;
1649 case PPC_IF:
1650 t = tline = expand_smacro(tline);
1651 tptr = &t;
1652 tokval.t_type = TOKEN_INVALID;
1653 evalresult = evaluate(ppscan, tptr, &tokval,
1654 NULL, pass | CRITICAL, error, NULL);
1655 if (!evalresult)
1656 return -1;
1657 if (tokval.t_type)
1658 error(ERR_WARNING,
1659 "trailing garbage after expression ignored");
1660 if (!is_simple(evalresult)) {
1661 error(ERR_NONFATAL,
1662 "non-constant value given to `%s'", pp_directives[ct]);
1663 goto fail;
1665 j = reloc_value(evalresult) != 0;
1666 return j;
1668 default:
1669 error(ERR_FATAL,
1670 "preprocessor directive `%s' not yet implemented",
1671 pp_directives[ct]);
1672 goto fail;
1675 free_tlist(origline);
1676 return j ^ PP_NEGATIVE(ct);
1678 fail:
1679 free_tlist(origline);
1680 return -1;
1684 * Expand macros in a string. Used in %error and %include directives.
1685 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1686 * The returned variable should ALWAYS be freed after usage.
1688 void expand_macros_in_string(char **p)
1690 Token *line = tokenize(*p);
1691 line = expand_smacro(line);
1692 *p = detoken(line, false);
1696 * Common code for defining an smacro
1698 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1699 int nparam, Token *expansion)
1701 SMacro *smac, **smhead;
1702 struct hash_table *smtbl;
1704 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1705 if (!smac) {
1706 error(ERR_WARNING,
1707 "single-line macro `%s' defined both with and"
1708 " without parameters", mname);
1710 /* Some instances of the old code considered this a failure,
1711 some others didn't. What is the right thing to do here? */
1712 free_tlist(expansion);
1713 return false; /* Failure */
1714 } else {
1716 * We're redefining, so we have to take over an
1717 * existing SMacro structure. This means freeing
1718 * what was already in it.
1720 nasm_free(smac->name);
1721 free_tlist(smac->expansion);
1723 } else {
1724 smtbl = ctx ? &ctx->localmac : &smacros;
1725 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1726 smac = nasm_malloc(sizeof(SMacro));
1727 smac->next = *smhead;
1728 *smhead = smac;
1730 smac->name = nasm_strdup(mname);
1731 smac->casesense = casesense;
1732 smac->nparam = nparam;
1733 smac->expansion = expansion;
1734 smac->in_progress = false;
1735 return true; /* Success */
1739 * Undefine an smacro
1741 static void undef_smacro(Context *ctx, const char *mname)
1743 SMacro **smhead, *s, **sp;
1744 struct hash_table *smtbl;
1746 smtbl = ctx ? &ctx->localmac : &smacros;
1747 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1749 if (smhead) {
1751 * We now have a macro name... go hunt for it.
1753 sp = smhead;
1754 while ((s = *sp) != NULL) {
1755 if (!mstrcmp(s->name, mname, s->casesense)) {
1756 *sp = s->next;
1757 nasm_free(s->name);
1758 free_tlist(s->expansion);
1759 nasm_free(s);
1760 } else {
1761 sp = &s->next;
1768 * Decode a size directive
1770 static int parse_size(const char *str) {
1771 static const char *size_names[] =
1772 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1773 static const int sizes[] =
1774 { 0, 1, 4, 16, 8, 10, 2, 32 };
1776 return sizes[bsii(str, size_names, elements(size_names))+1];
1780 * find and process preprocessor directive in passed line
1781 * Find out if a line contains a preprocessor directive, and deal
1782 * with it if so.
1784 * If a directive _is_ found, it is the responsibility of this routine
1785 * (and not the caller) to free_tlist() the line.
1787 * @param tline a pointer to the current tokeninzed line linked list
1788 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1791 static int do_directive(Token * tline)
1793 enum preproc_token i;
1794 int j;
1795 bool err;
1796 int nparam;
1797 bool nolist;
1798 bool casesense;
1799 int k, m;
1800 int offset;
1801 char *p, *mname;
1802 Include *inc;
1803 Context *ctx;
1804 Cond *cond;
1805 MMacro *mmac, **mmhead;
1806 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1807 Line *l;
1808 struct tokenval tokval;
1809 expr *evalresult;
1810 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1811 int64_t count;
1813 origline = tline;
1815 skip_white_(tline);
1816 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1817 (tline->text[1] == '%' || tline->text[1] == '$'
1818 || tline->text[1] == '!'))
1819 return NO_DIRECTIVE_FOUND;
1821 i = pp_token_hash(tline->text);
1824 * If we're in a non-emitting branch of a condition construct,
1825 * or walking to the end of an already terminated %rep block,
1826 * we should ignore all directives except for condition
1827 * directives.
1829 if (((istk->conds && !emitting(istk->conds->state)) ||
1830 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1831 return NO_DIRECTIVE_FOUND;
1835 * If we're defining a macro or reading a %rep block, we should
1836 * ignore all directives except for %macro/%imacro (which
1837 * generate an error), %endm/%endmacro, and (only if we're in a
1838 * %rep block) %endrep. If we're in a %rep block, another %rep
1839 * causes an error, so should be let through.
1841 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1842 i != PP_ENDMACRO && i != PP_ENDM &&
1843 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1844 return NO_DIRECTIVE_FOUND;
1847 switch (i) {
1848 case PP_INVALID:
1849 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1850 tline->text);
1851 return NO_DIRECTIVE_FOUND; /* didn't get it */
1853 case PP_STACKSIZE:
1854 /* Directive to tell NASM what the default stack size is. The
1855 * default is for a 16-bit stack, and this can be overriden with
1856 * %stacksize large.
1857 * the following form:
1859 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1861 tline = tline->next;
1862 if (tline && tline->type == TOK_WHITESPACE)
1863 tline = tline->next;
1864 if (!tline || tline->type != TOK_ID) {
1865 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1866 free_tlist(origline);
1867 return DIRECTIVE_FOUND;
1869 if (nasm_stricmp(tline->text, "flat") == 0) {
1870 /* All subsequent ARG directives are for a 32-bit stack */
1871 StackSize = 4;
1872 StackPointer = "ebp";
1873 ArgOffset = 8;
1874 LocalOffset = 0;
1875 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1876 /* All subsequent ARG directives are for a 64-bit stack */
1877 StackSize = 8;
1878 StackPointer = "rbp";
1879 ArgOffset = 8;
1880 LocalOffset = 0;
1881 } else if (nasm_stricmp(tline->text, "large") == 0) {
1882 /* All subsequent ARG directives are for a 16-bit stack,
1883 * far function call.
1885 StackSize = 2;
1886 StackPointer = "bp";
1887 ArgOffset = 4;
1888 LocalOffset = 0;
1889 } else if (nasm_stricmp(tline->text, "small") == 0) {
1890 /* All subsequent ARG directives are for a 16-bit stack,
1891 * far function call. We don't support near functions.
1893 StackSize = 2;
1894 StackPointer = "bp";
1895 ArgOffset = 6;
1896 LocalOffset = 0;
1897 } else {
1898 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1899 free_tlist(origline);
1900 return DIRECTIVE_FOUND;
1902 free_tlist(origline);
1903 return DIRECTIVE_FOUND;
1905 case PP_ARG:
1906 /* TASM like ARG directive to define arguments to functions, in
1907 * the following form:
1909 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1911 offset = ArgOffset;
1912 do {
1913 char *arg, directive[256];
1914 int size = StackSize;
1916 /* Find the argument name */
1917 tline = tline->next;
1918 if (tline && tline->type == TOK_WHITESPACE)
1919 tline = tline->next;
1920 if (!tline || tline->type != TOK_ID) {
1921 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1922 free_tlist(origline);
1923 return DIRECTIVE_FOUND;
1925 arg = tline->text;
1927 /* Find the argument size type */
1928 tline = tline->next;
1929 if (!tline || tline->type != TOK_OTHER
1930 || tline->text[0] != ':') {
1931 error(ERR_NONFATAL,
1932 "Syntax error processing `%%arg' directive");
1933 free_tlist(origline);
1934 return DIRECTIVE_FOUND;
1936 tline = tline->next;
1937 if (!tline || tline->type != TOK_ID) {
1938 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1939 free_tlist(origline);
1940 return DIRECTIVE_FOUND;
1943 /* Allow macro expansion of type parameter */
1944 tt = tokenize(tline->text);
1945 tt = expand_smacro(tt);
1946 size = parse_size(tt->text);
1947 if (!size) {
1948 error(ERR_NONFATAL,
1949 "Invalid size type for `%%arg' missing directive");
1950 free_tlist(tt);
1951 free_tlist(origline);
1952 return DIRECTIVE_FOUND;
1954 free_tlist(tt);
1956 /* Round up to even stack slots */
1957 size = (size+StackSize-1) & ~(StackSize-1);
1959 /* Now define the macro for the argument */
1960 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1961 arg, StackPointer, offset);
1962 do_directive(tokenize(directive));
1963 offset += size;
1965 /* Move to the next argument in the list */
1966 tline = tline->next;
1967 if (tline && tline->type == TOK_WHITESPACE)
1968 tline = tline->next;
1969 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1970 ArgOffset = offset;
1971 free_tlist(origline);
1972 return DIRECTIVE_FOUND;
1974 case PP_LOCAL:
1975 /* TASM like LOCAL directive to define local variables for a
1976 * function, in the following form:
1978 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1980 * The '= LocalSize' at the end is ignored by NASM, but is
1981 * required by TASM to define the local parameter size (and used
1982 * by the TASM macro package).
1984 offset = LocalOffset;
1985 do {
1986 char *local, directive[256];
1987 int size = StackSize;
1989 /* Find the argument name */
1990 tline = tline->next;
1991 if (tline && tline->type == TOK_WHITESPACE)
1992 tline = tline->next;
1993 if (!tline || tline->type != TOK_ID) {
1994 error(ERR_NONFATAL,
1995 "`%%local' missing argument parameter");
1996 free_tlist(origline);
1997 return DIRECTIVE_FOUND;
1999 local = tline->text;
2001 /* Find the argument size type */
2002 tline = tline->next;
2003 if (!tline || tline->type != TOK_OTHER
2004 || tline->text[0] != ':') {
2005 error(ERR_NONFATAL,
2006 "Syntax error processing `%%local' directive");
2007 free_tlist(origline);
2008 return DIRECTIVE_FOUND;
2010 tline = tline->next;
2011 if (!tline || tline->type != TOK_ID) {
2012 error(ERR_NONFATAL,
2013 "`%%local' missing size type parameter");
2014 free_tlist(origline);
2015 return DIRECTIVE_FOUND;
2018 /* Allow macro expansion of type parameter */
2019 tt = tokenize(tline->text);
2020 tt = expand_smacro(tt);
2021 size = parse_size(tt->text);
2022 if (!size) {
2023 error(ERR_NONFATAL,
2024 "Invalid size type for `%%local' missing directive");
2025 free_tlist(tt);
2026 free_tlist(origline);
2027 return DIRECTIVE_FOUND;
2029 free_tlist(tt);
2031 /* Round up to even stack slots */
2032 size = (size+StackSize-1) & ~(StackSize-1);
2034 offset += size; /* Negative offset, increment before */
2036 /* Now define the macro for the argument */
2037 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2038 local, StackPointer, offset);
2039 do_directive(tokenize(directive));
2041 /* Now define the assign to setup the enter_c macro correctly */
2042 snprintf(directive, sizeof(directive),
2043 "%%assign %%$localsize %%$localsize+%d", size);
2044 do_directive(tokenize(directive));
2046 /* Move to the next argument in the list */
2047 tline = tline->next;
2048 if (tline && tline->type == TOK_WHITESPACE)
2049 tline = tline->next;
2050 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2051 LocalOffset = offset;
2052 free_tlist(origline);
2053 return DIRECTIVE_FOUND;
2055 case PP_CLEAR:
2056 if (tline->next)
2057 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2058 free_macros();
2059 init_macros();
2060 free_tlist(origline);
2061 return DIRECTIVE_FOUND;
2063 case PP_INCLUDE:
2064 tline = expand_smacros(tline->next);
2065 skip_white_(tline);
2066 if (!tline || (tline->type != TOK_STRING &&
2067 tline->type != TOK_INTERNAL_STRING)) {
2068 error(ERR_NONFATAL, "`%%include' expects a file name");
2069 free_tlist(origline);
2070 return DIRECTIVE_FOUND; /* but we did _something_ */
2072 if (tline->next)
2073 error(ERR_WARNING,
2074 "trailing garbage after `%%include' ignored");
2075 if (tline->type != TOK_INTERNAL_STRING) {
2076 p = tline->text + 1; /* point past the quote to the name */
2077 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2078 } else
2079 p = tline->text; /* internal_string is easier */
2080 inc = nasm_malloc(sizeof(Include));
2081 inc->next = istk;
2082 inc->conds = NULL;
2083 inc->fp = inc_fopen(p);
2084 if (!inc->fp && pass == 0) {
2085 /* -MG given but file not found */
2086 nasm_free(inc);
2087 } else {
2088 inc->fname = src_set_fname(p);
2089 inc->lineno = src_set_linnum(0);
2090 inc->lineinc = 1;
2091 inc->expansion = NULL;
2092 inc->mstk = NULL;
2093 istk = inc;
2094 list->uplevel(LIST_INCLUDE);
2096 free_tlist(origline);
2097 return DIRECTIVE_FOUND;
2099 case PP_PUSH:
2100 tline = tline->next;
2101 skip_white_(tline);
2102 tline = expand_id(tline);
2103 if (!tok_type_(tline, TOK_ID)) {
2104 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND; /* but we did _something_ */
2108 if (tline->next)
2109 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2110 ctx = nasm_malloc(sizeof(Context));
2111 ctx->next = cstk;
2112 hash_init(&ctx->localmac, HASH_SMALL);
2113 ctx->name = nasm_strdup(tline->text);
2114 ctx->number = unique++;
2115 cstk = ctx;
2116 free_tlist(origline);
2117 break;
2119 case PP_REPL:
2120 tline = tline->next;
2121 skip_white_(tline);
2122 tline = expand_id(tline);
2123 if (!tok_type_(tline, TOK_ID)) {
2124 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2125 free_tlist(origline);
2126 return DIRECTIVE_FOUND; /* but we did _something_ */
2128 if (tline->next)
2129 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2130 if (!cstk)
2131 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2132 else {
2133 nasm_free(cstk->name);
2134 cstk->name = nasm_strdup(tline->text);
2136 free_tlist(origline);
2137 break;
2139 case PP_POP:
2140 if (tline->next)
2141 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2142 if (!cstk)
2143 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2144 else
2145 ctx_pop();
2146 free_tlist(origline);
2147 break;
2149 case PP_ERROR:
2150 tline->next = expand_smacro(tline->next);
2151 tline = tline->next;
2152 skip_white_(tline);
2153 if (tok_type_(tline, TOK_STRING)) {
2154 p = tline->text + 1; /* point past the quote to the name */
2155 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2156 expand_macros_in_string(&p);
2157 error(ERR_NONFATAL, "%s", p);
2158 nasm_free(p);
2159 } else {
2160 p = detoken(tline, false);
2161 error(ERR_WARNING, "%s", p);
2162 nasm_free(p);
2164 free_tlist(origline);
2165 break;
2167 CASE_PP_IF:
2168 if (istk->conds && !emitting(istk->conds->state))
2169 j = COND_NEVER;
2170 else {
2171 j = if_condition(tline->next, i);
2172 tline->next = NULL; /* it got freed */
2173 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2175 cond = nasm_malloc(sizeof(Cond));
2176 cond->next = istk->conds;
2177 cond->state = j;
2178 istk->conds = cond;
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
2182 CASE_PP_ELIF:
2183 if (!istk->conds)
2184 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2185 if (emitting(istk->conds->state)
2186 || istk->conds->state == COND_NEVER)
2187 istk->conds->state = COND_NEVER;
2188 else {
2190 * IMPORTANT: In the case of %if, we will already have
2191 * called expand_mmac_params(); however, if we're
2192 * processing an %elif we must have been in a
2193 * non-emitting mode, which would have inhibited
2194 * the normal invocation of expand_mmac_params(). Therefore,
2195 * we have to do it explicitly here.
2197 j = if_condition(expand_mmac_params(tline->next), i);
2198 tline->next = NULL; /* it got freed */
2199 istk->conds->state =
2200 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2202 free_tlist(origline);
2203 return DIRECTIVE_FOUND;
2205 case PP_ELSE:
2206 if (tline->next)
2207 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2208 if (!istk->conds)
2209 error(ERR_FATAL, "`%%else': no matching `%%if'");
2210 if (emitting(istk->conds->state)
2211 || istk->conds->state == COND_NEVER)
2212 istk->conds->state = COND_ELSE_FALSE;
2213 else
2214 istk->conds->state = COND_ELSE_TRUE;
2215 free_tlist(origline);
2216 return DIRECTIVE_FOUND;
2218 case PP_ENDIF:
2219 if (tline->next)
2220 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2221 if (!istk->conds)
2222 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2223 cond = istk->conds;
2224 istk->conds = cond->next;
2225 nasm_free(cond);
2226 free_tlist(origline);
2227 return DIRECTIVE_FOUND;
2229 case PP_MACRO:
2230 case PP_IMACRO:
2231 if (defining)
2232 error(ERR_FATAL,
2233 "`%%%smacro': already defining a macro",
2234 (i == PP_IMACRO ? "i" : ""));
2235 tline = tline->next;
2236 skip_white_(tline);
2237 tline = expand_id(tline);
2238 if (!tok_type_(tline, TOK_ID)) {
2239 error(ERR_NONFATAL,
2240 "`%%%smacro' expects a macro name",
2241 (i == PP_IMACRO ? "i" : ""));
2242 return DIRECTIVE_FOUND;
2244 defining = nasm_malloc(sizeof(MMacro));
2245 defining->name = nasm_strdup(tline->text);
2246 defining->casesense = (i == PP_MACRO);
2247 defining->plus = false;
2248 defining->nolist = false;
2249 defining->in_progress = 0;
2250 defining->rep_nest = NULL;
2251 tline = expand_smacro(tline->next);
2252 skip_white_(tline);
2253 if (!tok_type_(tline, TOK_NUMBER)) {
2254 error(ERR_NONFATAL,
2255 "`%%%smacro' expects a parameter count",
2256 (i == PP_IMACRO ? "i" : ""));
2257 defining->nparam_min = defining->nparam_max = 0;
2258 } else {
2259 defining->nparam_min = defining->nparam_max =
2260 readnum(tline->text, &err);
2261 if (err)
2262 error(ERR_NONFATAL,
2263 "unable to parse parameter count `%s'", tline->text);
2265 if (tline && tok_is_(tline->next, "-")) {
2266 tline = tline->next->next;
2267 if (tok_is_(tline, "*"))
2268 defining->nparam_max = INT_MAX;
2269 else if (!tok_type_(tline, TOK_NUMBER))
2270 error(ERR_NONFATAL,
2271 "`%%%smacro' expects a parameter count after `-'",
2272 (i == PP_IMACRO ? "i" : ""));
2273 else {
2274 defining->nparam_max = readnum(tline->text, &err);
2275 if (err)
2276 error(ERR_NONFATAL,
2277 "unable to parse parameter count `%s'",
2278 tline->text);
2279 if (defining->nparam_min > defining->nparam_max)
2280 error(ERR_NONFATAL,
2281 "minimum parameter count exceeds maximum");
2284 if (tline && tok_is_(tline->next, "+")) {
2285 tline = tline->next;
2286 defining->plus = true;
2288 if (tline && tok_type_(tline->next, TOK_ID) &&
2289 !nasm_stricmp(tline->next->text, ".nolist")) {
2290 tline = tline->next;
2291 defining->nolist = true;
2293 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2294 while (mmac) {
2295 if (!strcmp(mmac->name, defining->name) &&
2296 (mmac->nparam_min <= defining->nparam_max
2297 || defining->plus)
2298 && (defining->nparam_min <= mmac->nparam_max
2299 || mmac->plus)) {
2300 error(ERR_WARNING,
2301 "redefining multi-line macro `%s'", defining->name);
2302 break;
2304 mmac = mmac->next;
2307 * Handle default parameters.
2309 if (tline && tline->next) {
2310 defining->dlist = tline->next;
2311 tline->next = NULL;
2312 count_mmac_params(defining->dlist, &defining->ndefs,
2313 &defining->defaults);
2314 } else {
2315 defining->dlist = NULL;
2316 defining->defaults = NULL;
2318 defining->expansion = NULL;
2319 free_tlist(origline);
2320 return DIRECTIVE_FOUND;
2322 case PP_ENDM:
2323 case PP_ENDMACRO:
2324 if (!defining) {
2325 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2326 return DIRECTIVE_FOUND;
2328 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2329 defining->next = *mmhead;
2330 *mmhead = defining;
2331 defining = NULL;
2332 free_tlist(origline);
2333 return DIRECTIVE_FOUND;
2335 case PP_ROTATE:
2336 if (tline->next && tline->next->type == TOK_WHITESPACE)
2337 tline = tline->next;
2338 if (tline->next == NULL) {
2339 free_tlist(origline);
2340 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2341 return DIRECTIVE_FOUND;
2343 t = expand_smacro(tline->next);
2344 tline->next = NULL;
2345 free_tlist(origline);
2346 tline = t;
2347 tptr = &t;
2348 tokval.t_type = TOKEN_INVALID;
2349 evalresult =
2350 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2351 free_tlist(tline);
2352 if (!evalresult)
2353 return DIRECTIVE_FOUND;
2354 if (tokval.t_type)
2355 error(ERR_WARNING,
2356 "trailing garbage after expression ignored");
2357 if (!is_simple(evalresult)) {
2358 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2359 return DIRECTIVE_FOUND;
2361 mmac = istk->mstk;
2362 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2363 mmac = mmac->next_active;
2364 if (!mmac) {
2365 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2366 } else if (mmac->nparam == 0) {
2367 error(ERR_NONFATAL,
2368 "`%%rotate' invoked within macro without parameters");
2369 } else {
2370 int rotate = mmac->rotate + reloc_value(evalresult);
2372 rotate %= (int)mmac->nparam;
2373 if (rotate < 0)
2374 rotate += mmac->nparam;
2376 mmac->rotate = rotate;
2378 return DIRECTIVE_FOUND;
2380 case PP_REP:
2381 nolist = false;
2382 do {
2383 tline = tline->next;
2384 } while (tok_type_(tline, TOK_WHITESPACE));
2386 if (tok_type_(tline, TOK_ID) &&
2387 nasm_stricmp(tline->text, ".nolist") == 0) {
2388 nolist = true;
2389 do {
2390 tline = tline->next;
2391 } while (tok_type_(tline, TOK_WHITESPACE));
2394 if (tline) {
2395 t = expand_smacro(tline);
2396 tptr = &t;
2397 tokval.t_type = TOKEN_INVALID;
2398 evalresult =
2399 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2400 if (!evalresult) {
2401 free_tlist(origline);
2402 return DIRECTIVE_FOUND;
2404 if (tokval.t_type)
2405 error(ERR_WARNING,
2406 "trailing garbage after expression ignored");
2407 if (!is_simple(evalresult)) {
2408 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2409 return DIRECTIVE_FOUND;
2411 count = reloc_value(evalresult) + 1;
2412 } else {
2413 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2414 count = 0;
2416 free_tlist(origline);
2418 tmp_defining = defining;
2419 defining = nasm_malloc(sizeof(MMacro));
2420 defining->name = NULL; /* flags this macro as a %rep block */
2421 defining->casesense = false;
2422 defining->plus = false;
2423 defining->nolist = nolist;
2424 defining->in_progress = count;
2425 defining->nparam_min = defining->nparam_max = 0;
2426 defining->defaults = NULL;
2427 defining->dlist = NULL;
2428 defining->expansion = NULL;
2429 defining->next_active = istk->mstk;
2430 defining->rep_nest = tmp_defining;
2431 return DIRECTIVE_FOUND;
2433 case PP_ENDREP:
2434 if (!defining || defining->name) {
2435 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2436 return DIRECTIVE_FOUND;
2440 * Now we have a "macro" defined - although it has no name
2441 * and we won't be entering it in the hash tables - we must
2442 * push a macro-end marker for it on to istk->expansion.
2443 * After that, it will take care of propagating itself (a
2444 * macro-end marker line for a macro which is really a %rep
2445 * block will cause the macro to be re-expanded, complete
2446 * with another macro-end marker to ensure the process
2447 * continues) until the whole expansion is forcibly removed
2448 * from istk->expansion by a %exitrep.
2450 l = nasm_malloc(sizeof(Line));
2451 l->next = istk->expansion;
2452 l->finishes = defining;
2453 l->first = NULL;
2454 istk->expansion = l;
2456 istk->mstk = defining;
2458 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2459 tmp_defining = defining;
2460 defining = defining->rep_nest;
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2464 case PP_EXITREP:
2466 * We must search along istk->expansion until we hit a
2467 * macro-end marker for a macro with no name. Then we set
2468 * its `in_progress' flag to 0.
2470 for (l = istk->expansion; l; l = l->next)
2471 if (l->finishes && !l->finishes->name)
2472 break;
2474 if (l)
2475 l->finishes->in_progress = 0;
2476 else
2477 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2481 case PP_XDEFINE:
2482 case PP_IXDEFINE:
2483 case PP_DEFINE:
2484 case PP_IDEFINE:
2485 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2487 tline = tline->next;
2488 skip_white_(tline);
2489 tline = expand_id(tline);
2490 if (!tline || (tline->type != TOK_ID &&
2491 (tline->type != TOK_PREPROC_ID ||
2492 tline->text[1] != '$'))) {
2493 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2494 pp_directives[i]);
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2499 ctx = get_ctx(tline->text, false);
2501 mname = tline->text;
2502 last = tline;
2503 param_start = tline = tline->next;
2504 nparam = 0;
2506 /* Expand the macro definition now for %xdefine and %ixdefine */
2507 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2508 tline = expand_smacro(tline);
2510 if (tok_is_(tline, "(")) {
2512 * This macro has parameters.
2515 tline = tline->next;
2516 while (1) {
2517 skip_white_(tline);
2518 if (!tline) {
2519 error(ERR_NONFATAL, "parameter identifier expected");
2520 free_tlist(origline);
2521 return DIRECTIVE_FOUND;
2523 if (tline->type != TOK_ID) {
2524 error(ERR_NONFATAL,
2525 "`%s': parameter identifier expected",
2526 tline->text);
2527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
2530 tline->type = TOK_SMAC_PARAM + nparam++;
2531 tline = tline->next;
2532 skip_white_(tline);
2533 if (tok_is_(tline, ",")) {
2534 tline = tline->next;
2535 continue;
2537 if (!tok_is_(tline, ")")) {
2538 error(ERR_NONFATAL,
2539 "`)' expected to terminate macro template");
2540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
2543 break;
2545 last = tline;
2546 tline = tline->next;
2548 if (tok_type_(tline, TOK_WHITESPACE))
2549 last = tline, tline = tline->next;
2550 macro_start = NULL;
2551 last->next = NULL;
2552 t = tline;
2553 while (t) {
2554 if (t->type == TOK_ID) {
2555 for (tt = param_start; tt; tt = tt->next)
2556 if (tt->type >= TOK_SMAC_PARAM &&
2557 !strcmp(tt->text, t->text))
2558 t->type = tt->type;
2560 tt = t->next;
2561 t->next = macro_start;
2562 macro_start = t;
2563 t = tt;
2566 * Good. We now have a macro name, a parameter count, and a
2567 * token list (in reverse order) for an expansion. We ought
2568 * to be OK just to create an SMacro, store it, and let
2569 * free_tlist have the rest of the line (which we have
2570 * carefully re-terminated after chopping off the expansion
2571 * from the end).
2573 define_smacro(ctx, mname, casesense, nparam, macro_start);
2574 free_tlist(origline);
2575 return DIRECTIVE_FOUND;
2577 case PP_UNDEF:
2578 tline = tline->next;
2579 skip_white_(tline);
2580 tline = expand_id(tline);
2581 if (!tline || (tline->type != TOK_ID &&
2582 (tline->type != TOK_PREPROC_ID ||
2583 tline->text[1] != '$'))) {
2584 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2585 free_tlist(origline);
2586 return DIRECTIVE_FOUND;
2588 if (tline->next) {
2589 error(ERR_WARNING,
2590 "trailing garbage after macro name ignored");
2593 /* Find the context that symbol belongs to */
2594 ctx = get_ctx(tline->text, false);
2595 undef_smacro(ctx, tline->text);
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
2599 case PP_STRLEN:
2600 casesense = true;
2602 tline = tline->next;
2603 skip_white_(tline);
2604 tline = expand_id(tline);
2605 if (!tline || (tline->type != TOK_ID &&
2606 (tline->type != TOK_PREPROC_ID ||
2607 tline->text[1] != '$'))) {
2608 error(ERR_NONFATAL,
2609 "`%%strlen' expects a macro identifier as first parameter");
2610 free_tlist(origline);
2611 return DIRECTIVE_FOUND;
2613 ctx = get_ctx(tline->text, false);
2615 mname = tline->text;
2616 last = tline;
2617 tline = expand_smacro(tline->next);
2618 last->next = NULL;
2620 t = tline;
2621 while (tok_type_(t, TOK_WHITESPACE))
2622 t = t->next;
2623 /* t should now point to the string */
2624 if (t->type != TOK_STRING) {
2625 error(ERR_NONFATAL,
2626 "`%%strlen` requires string as second parameter");
2627 free_tlist(tline);
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND;
2632 macro_start = nasm_malloc(sizeof(*macro_start));
2633 macro_start->next = NULL;
2634 make_tok_num(macro_start, strlen(t->text) - 2);
2635 macro_start->mac = NULL;
2638 * We now have a macro name, an implicit parameter count of
2639 * zero, and a numeric token to use as an expansion. Create
2640 * and store an SMacro.
2642 define_smacro(ctx, mname, casesense, 0, macro_start);
2643 free_tlist(tline);
2644 free_tlist(origline);
2645 return DIRECTIVE_FOUND;
2647 case PP_SUBSTR:
2648 casesense = true;
2650 tline = tline->next;
2651 skip_white_(tline);
2652 tline = expand_id(tline);
2653 if (!tline || (tline->type != TOK_ID &&
2654 (tline->type != TOK_PREPROC_ID ||
2655 tline->text[1] != '$'))) {
2656 error(ERR_NONFATAL,
2657 "`%%substr' expects a macro identifier as first parameter");
2658 free_tlist(origline);
2659 return DIRECTIVE_FOUND;
2661 ctx = get_ctx(tline->text, false);
2663 mname = tline->text;
2664 last = tline;
2665 tline = expand_smacro(tline->next);
2666 last->next = NULL;
2668 t = tline->next;
2669 while (tok_type_(t, TOK_WHITESPACE))
2670 t = t->next;
2672 /* t should now point to the string */
2673 if (t->type != TOK_STRING) {
2674 error(ERR_NONFATAL,
2675 "`%%substr` requires string as second parameter");
2676 free_tlist(tline);
2677 free_tlist(origline);
2678 return DIRECTIVE_FOUND;
2681 tt = t->next;
2682 tptr = &tt;
2683 tokval.t_type = TOKEN_INVALID;
2684 evalresult =
2685 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2686 if (!evalresult) {
2687 free_tlist(tline);
2688 free_tlist(origline);
2689 return DIRECTIVE_FOUND;
2691 if (!is_simple(evalresult)) {
2692 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2693 free_tlist(tline);
2694 free_tlist(origline);
2695 return DIRECTIVE_FOUND;
2698 macro_start = nasm_malloc(sizeof(*macro_start));
2699 macro_start->next = NULL;
2700 macro_start->text = nasm_strdup("'''");
2701 if (evalresult->value > 0
2702 && evalresult->value < (int) strlen(t->text) - 1) {
2703 macro_start->text[1] = t->text[evalresult->value];
2704 } else {
2705 macro_start->text[2] = '\0';
2707 macro_start->type = TOK_STRING;
2708 macro_start->mac = NULL;
2711 * We now have a macro name, an implicit parameter count of
2712 * zero, and a numeric token to use as an expansion. Create
2713 * and store an SMacro.
2715 define_smacro(ctx, mname, casesense, 0, macro_start);
2716 free_tlist(tline);
2717 free_tlist(origline);
2718 return DIRECTIVE_FOUND;
2720 case PP_ASSIGN:
2721 case PP_IASSIGN:
2722 casesense = (i == PP_ASSIGN);
2724 tline = tline->next;
2725 skip_white_(tline);
2726 tline = expand_id(tline);
2727 if (!tline || (tline->type != TOK_ID &&
2728 (tline->type != TOK_PREPROC_ID ||
2729 tline->text[1] != '$'))) {
2730 error(ERR_NONFATAL,
2731 "`%%%sassign' expects a macro identifier",
2732 (i == PP_IASSIGN ? "i" : ""));
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
2736 ctx = get_ctx(tline->text, false);
2738 mname = tline->text;
2739 last = tline;
2740 tline = expand_smacro(tline->next);
2741 last->next = NULL;
2743 t = tline;
2744 tptr = &t;
2745 tokval.t_type = TOKEN_INVALID;
2746 evalresult =
2747 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2748 free_tlist(tline);
2749 if (!evalresult) {
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
2754 if (tokval.t_type)
2755 error(ERR_WARNING,
2756 "trailing garbage after expression ignored");
2758 if (!is_simple(evalresult)) {
2759 error(ERR_NONFATAL,
2760 "non-constant value given to `%%%sassign'",
2761 (i == PP_IASSIGN ? "i" : ""));
2762 free_tlist(origline);
2763 return DIRECTIVE_FOUND;
2766 macro_start = nasm_malloc(sizeof(*macro_start));
2767 macro_start->next = NULL;
2768 make_tok_num(macro_start, reloc_value(evalresult));
2769 macro_start->mac = NULL;
2772 * We now have a macro name, an implicit parameter count of
2773 * zero, and a numeric token to use as an expansion. Create
2774 * and store an SMacro.
2776 define_smacro(ctx, mname, casesense, 0, macro_start);
2777 free_tlist(origline);
2778 return DIRECTIVE_FOUND;
2780 case PP_LINE:
2782 * Syntax is `%line nnn[+mmm] [filename]'
2784 tline = tline->next;
2785 skip_white_(tline);
2786 if (!tok_type_(tline, TOK_NUMBER)) {
2787 error(ERR_NONFATAL, "`%%line' expects line number");
2788 free_tlist(origline);
2789 return DIRECTIVE_FOUND;
2791 k = readnum(tline->text, &err);
2792 m = 1;
2793 tline = tline->next;
2794 if (tok_is_(tline, "+")) {
2795 tline = tline->next;
2796 if (!tok_type_(tline, TOK_NUMBER)) {
2797 error(ERR_NONFATAL, "`%%line' expects line increment");
2798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
2801 m = readnum(tline->text, &err);
2802 tline = tline->next;
2804 skip_white_(tline);
2805 src_set_linnum(k);
2806 istk->lineinc = m;
2807 if (tline) {
2808 nasm_free(src_set_fname(detoken(tline, false)));
2810 free_tlist(origline);
2811 return DIRECTIVE_FOUND;
2813 default:
2814 error(ERR_FATAL,
2815 "preprocessor directive `%s' not yet implemented",
2816 pp_directives[i]);
2817 break;
2819 return DIRECTIVE_FOUND;
2823 * Ensure that a macro parameter contains a condition code and
2824 * nothing else. Return the condition code index if so, or -1
2825 * otherwise.
2827 static int find_cc(Token * t)
2829 Token *tt;
2830 int i, j, k, m;
2832 if (!t)
2833 return -1; /* Probably a %+ without a space */
2835 skip_white_(t);
2836 if (t->type != TOK_ID)
2837 return -1;
2838 tt = t->next;
2839 skip_white_(tt);
2840 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2841 return -1;
2843 i = -1;
2844 j = elements(conditions);
2845 while (j - i > 1) {
2846 k = (j + i) / 2;
2847 m = nasm_stricmp(t->text, conditions[k]);
2848 if (m == 0) {
2849 i = k;
2850 j = -2;
2851 break;
2852 } else if (m < 0) {
2853 j = k;
2854 } else
2855 i = k;
2857 if (j != -2)
2858 return -1;
2859 return i;
2863 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2864 * %-n) and MMacro-local identifiers (%%foo).
2866 static Token *expand_mmac_params(Token * tline)
2868 Token *t, *tt, **tail, *thead;
2870 tail = &thead;
2871 thead = NULL;
2873 while (tline) {
2874 if (tline->type == TOK_PREPROC_ID &&
2875 (((tline->text[1] == '+' || tline->text[1] == '-')
2876 && tline->text[2]) || tline->text[1] == '%'
2877 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2878 char *text = NULL;
2879 int type = 0, cc; /* type = 0 to placate optimisers */
2880 char tmpbuf[30];
2881 unsigned int n;
2882 int i;
2883 MMacro *mac;
2885 t = tline;
2886 tline = tline->next;
2888 mac = istk->mstk;
2889 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2890 mac = mac->next_active;
2891 if (!mac)
2892 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2893 else
2894 switch (t->text[1]) {
2896 * We have to make a substitution of one of the
2897 * forms %1, %-1, %+1, %%foo, %0.
2899 case '0':
2900 type = TOK_NUMBER;
2901 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2902 text = nasm_strdup(tmpbuf);
2903 break;
2904 case '%':
2905 type = TOK_ID;
2906 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2907 mac->unique);
2908 text = nasm_strcat(tmpbuf, t->text + 2);
2909 break;
2910 case '-':
2911 n = atoi(t->text + 2) - 1;
2912 if (n >= mac->nparam)
2913 tt = NULL;
2914 else {
2915 if (mac->nparam > 1)
2916 n = (n + mac->rotate) % mac->nparam;
2917 tt = mac->params[n];
2919 cc = find_cc(tt);
2920 if (cc == -1) {
2921 error(ERR_NONFATAL,
2922 "macro parameter %d is not a condition code",
2923 n + 1);
2924 text = NULL;
2925 } else {
2926 type = TOK_ID;
2927 if (inverse_ccs[cc] == -1) {
2928 error(ERR_NONFATAL,
2929 "condition code `%s' is not invertible",
2930 conditions[cc]);
2931 text = NULL;
2932 } else
2933 text =
2934 nasm_strdup(conditions[inverse_ccs[cc]]);
2936 break;
2937 case '+':
2938 n = atoi(t->text + 2) - 1;
2939 if (n >= mac->nparam)
2940 tt = NULL;
2941 else {
2942 if (mac->nparam > 1)
2943 n = (n + mac->rotate) % mac->nparam;
2944 tt = mac->params[n];
2946 cc = find_cc(tt);
2947 if (cc == -1) {
2948 error(ERR_NONFATAL,
2949 "macro parameter %d is not a condition code",
2950 n + 1);
2951 text = NULL;
2952 } else {
2953 type = TOK_ID;
2954 text = nasm_strdup(conditions[cc]);
2956 break;
2957 default:
2958 n = atoi(t->text + 1) - 1;
2959 if (n >= mac->nparam)
2960 tt = NULL;
2961 else {
2962 if (mac->nparam > 1)
2963 n = (n + mac->rotate) % mac->nparam;
2964 tt = mac->params[n];
2966 if (tt) {
2967 for (i = 0; i < mac->paramlen[n]; i++) {
2968 *tail = new_Token(NULL, tt->type, tt->text, 0);
2969 tail = &(*tail)->next;
2970 tt = tt->next;
2973 text = NULL; /* we've done it here */
2974 break;
2976 if (!text) {
2977 delete_Token(t);
2978 } else {
2979 *tail = t;
2980 tail = &t->next;
2981 t->type = type;
2982 nasm_free(t->text);
2983 t->text = text;
2984 t->mac = NULL;
2986 continue;
2987 } else {
2988 t = *tail = tline;
2989 tline = tline->next;
2990 t->mac = NULL;
2991 tail = &t->next;
2994 *tail = NULL;
2995 t = thead;
2996 for (; t && (tt = t->next) != NULL; t = t->next)
2997 switch (t->type) {
2998 case TOK_WHITESPACE:
2999 if (tt->type == TOK_WHITESPACE) {
3000 t->next = delete_Token(tt);
3002 break;
3003 case TOK_ID:
3004 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3005 char *tmp = nasm_strcat(t->text, tt->text);
3006 nasm_free(t->text);
3007 t->text = tmp;
3008 t->next = delete_Token(tt);
3010 break;
3011 case TOK_NUMBER:
3012 if (tt->type == TOK_NUMBER) {
3013 char *tmp = nasm_strcat(t->text, tt->text);
3014 nasm_free(t->text);
3015 t->text = tmp;
3016 t->next = delete_Token(tt);
3018 break;
3019 default:
3020 break;
3023 return thead;
3027 * Expand all single-line macro calls made in the given line.
3028 * Return the expanded version of the line. The original is deemed
3029 * to be destroyed in the process. (In reality we'll just move
3030 * Tokens from input to output a lot of the time, rather than
3031 * actually bothering to destroy and replicate.)
3033 #define DEADMAN_LIMIT (1 << 20)
3035 static Token *expand_smacro(Token * tline)
3037 Token *t, *tt, *mstart, **tail, *thead;
3038 struct hash_table *smtbl;
3039 SMacro *head = NULL, *m;
3040 Token **params;
3041 int *paramsize;
3042 unsigned int nparam, sparam;
3043 int brackets, rescan;
3044 Token *org_tline = tline;
3045 Context *ctx;
3046 char *mname;
3047 int deadman = DEADMAN_LIMIT;
3050 * Trick: we should avoid changing the start token pointer since it can
3051 * be contained in "next" field of other token. Because of this
3052 * we allocate a copy of first token and work with it; at the end of
3053 * routine we copy it back
3055 if (org_tline) {
3056 tline =
3057 new_Token(org_tline->next, org_tline->type, org_tline->text,
3059 tline->mac = org_tline->mac;
3060 nasm_free(org_tline->text);
3061 org_tline->text = NULL;
3064 again:
3065 tail = &thead;
3066 thead = NULL;
3068 while (tline) { /* main token loop */
3069 if (!--deadman) {
3070 error(ERR_NONFATAL, "interminable macro recursion");
3071 break;
3074 if ((mname = tline->text)) {
3075 /* if this token is a local macro, look in local context */
3076 ctx = NULL;
3077 smtbl = &smacros;
3078 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3079 ctx = get_ctx(mname, true);
3080 if (ctx)
3081 smtbl = &ctx->localmac;
3083 head = (SMacro *) hash_findix(smtbl, mname);
3086 * We've hit an identifier. As in is_mmacro below, we first
3087 * check whether the identifier is a single-line macro at
3088 * all, then think about checking for parameters if
3089 * necessary.
3091 for (m = head; m; m = m->next)
3092 if (!mstrcmp(m->name, mname, m->casesense))
3093 break;
3094 if (m) {
3095 mstart = tline;
3096 params = NULL;
3097 paramsize = NULL;
3098 if (m->nparam == 0) {
3100 * Simple case: the macro is parameterless. Discard the
3101 * one token that the macro call took, and push the
3102 * expansion back on the to-do stack.
3104 if (!m->expansion) {
3105 if (!strcmp("__FILE__", m->name)) {
3106 int32_t num = 0;
3107 src_get(&num, &(tline->text));
3108 nasm_quote(&(tline->text));
3109 tline->type = TOK_STRING;
3110 continue;
3112 if (!strcmp("__LINE__", m->name)) {
3113 nasm_free(tline->text);
3114 make_tok_num(tline, src_get_linnum());
3115 continue;
3117 if (!strcmp("__BITS__", m->name)) {
3118 nasm_free(tline->text);
3119 make_tok_num(tline, globalbits);
3120 continue;
3122 tline = delete_Token(tline);
3123 continue;
3125 } else {
3127 * Complicated case: at least one macro with this name
3128 * exists and takes parameters. We must find the
3129 * parameters in the call, count them, find the SMacro
3130 * that corresponds to that form of the macro call, and
3131 * substitute for the parameters when we expand. What a
3132 * pain.
3134 /*tline = tline->next;
3135 skip_white_(tline); */
3136 do {
3137 t = tline->next;
3138 while (tok_type_(t, TOK_SMAC_END)) {
3139 t->mac->in_progress = false;
3140 t->text = NULL;
3141 t = tline->next = delete_Token(t);
3143 tline = t;
3144 } while (tok_type_(tline, TOK_WHITESPACE));
3145 if (!tok_is_(tline, "(")) {
3147 * This macro wasn't called with parameters: ignore
3148 * the call. (Behaviour borrowed from gnu cpp.)
3150 tline = mstart;
3151 m = NULL;
3152 } else {
3153 int paren = 0;
3154 int white = 0;
3155 brackets = 0;
3156 nparam = 0;
3157 sparam = PARAM_DELTA;
3158 params = nasm_malloc(sparam * sizeof(Token *));
3159 params[0] = tline->next;
3160 paramsize = nasm_malloc(sparam * sizeof(int));
3161 paramsize[0] = 0;
3162 while (true) { /* parameter loop */
3164 * For some unusual expansions
3165 * which concatenates function call
3167 t = tline->next;
3168 while (tok_type_(t, TOK_SMAC_END)) {
3169 t->mac->in_progress = false;
3170 t->text = NULL;
3171 t = tline->next = delete_Token(t);
3173 tline = t;
3175 if (!tline) {
3176 error(ERR_NONFATAL,
3177 "macro call expects terminating `)'");
3178 break;
3180 if (tline->type == TOK_WHITESPACE
3181 && brackets <= 0) {
3182 if (paramsize[nparam])
3183 white++;
3184 else
3185 params[nparam] = tline->next;
3186 continue; /* parameter loop */
3188 if (tline->type == TOK_OTHER
3189 && tline->text[1] == 0) {
3190 char ch = tline->text[0];
3191 if (ch == ',' && !paren && brackets <= 0) {
3192 if (++nparam >= sparam) {
3193 sparam += PARAM_DELTA;
3194 params = nasm_realloc(params,
3195 sparam *
3196 sizeof(Token
3197 *));
3198 paramsize =
3199 nasm_realloc(paramsize,
3200 sparam *
3201 sizeof(int));
3203 params[nparam] = tline->next;
3204 paramsize[nparam] = 0;
3205 white = 0;
3206 continue; /* parameter loop */
3208 if (ch == '{' &&
3209 (brackets > 0 || (brackets == 0 &&
3210 !paramsize[nparam])))
3212 if (!(brackets++)) {
3213 params[nparam] = tline->next;
3214 continue; /* parameter loop */
3217 if (ch == '}' && brackets > 0)
3218 if (--brackets == 0) {
3219 brackets = -1;
3220 continue; /* parameter loop */
3222 if (ch == '(' && !brackets)
3223 paren++;
3224 if (ch == ')' && brackets <= 0)
3225 if (--paren < 0)
3226 break;
3228 if (brackets < 0) {
3229 brackets = 0;
3230 error(ERR_NONFATAL, "braces do not "
3231 "enclose all of macro parameter");
3233 paramsize[nparam] += white + 1;
3234 white = 0;
3235 } /* parameter loop */
3236 nparam++;
3237 while (m && (m->nparam != nparam ||
3238 mstrcmp(m->name, mname,
3239 m->casesense)))
3240 m = m->next;
3241 if (!m)
3242 error(ERR_WARNING | ERR_WARN_MNP,
3243 "macro `%s' exists, "
3244 "but not taking %d parameters",
3245 mstart->text, nparam);
3248 if (m && m->in_progress)
3249 m = NULL;
3250 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3252 * Design question: should we handle !tline, which
3253 * indicates missing ')' here, or expand those
3254 * macros anyway, which requires the (t) test a few
3255 * lines down?
3257 nasm_free(params);
3258 nasm_free(paramsize);
3259 tline = mstart;
3260 } else {
3262 * Expand the macro: we are placed on the last token of the
3263 * call, so that we can easily split the call from the
3264 * following tokens. We also start by pushing an SMAC_END
3265 * token for the cycle removal.
3267 t = tline;
3268 if (t) {
3269 tline = t->next;
3270 t->next = NULL;
3272 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3273 tt->mac = m;
3274 m->in_progress = true;
3275 tline = tt;
3276 for (t = m->expansion; t; t = t->next) {
3277 if (t->type >= TOK_SMAC_PARAM) {
3278 Token *pcopy = tline, **ptail = &pcopy;
3279 Token *ttt, *pt;
3280 int i;
3282 ttt = params[t->type - TOK_SMAC_PARAM];
3283 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3284 --i >= 0;) {
3285 pt = *ptail =
3286 new_Token(tline, ttt->type, ttt->text,
3288 ptail = &pt->next;
3289 ttt = ttt->next;
3291 tline = pcopy;
3292 } else if (t->type == TOK_PREPROC_Q) {
3293 tt = new_Token(tline, TOK_ID, mname, 0);
3294 tline = tt;
3295 } else if (t->type == TOK_PREPROC_QQ) {
3296 tt = new_Token(tline, TOK_ID, m->name, 0);
3297 tline = tt;
3298 } else {
3299 tt = new_Token(tline, t->type, t->text, 0);
3300 tline = tt;
3305 * Having done that, get rid of the macro call, and clean
3306 * up the parameters.
3308 nasm_free(params);
3309 nasm_free(paramsize);
3310 free_tlist(mstart);
3311 continue; /* main token loop */
3316 if (tline->type == TOK_SMAC_END) {
3317 tline->mac->in_progress = false;
3318 tline = delete_Token(tline);
3319 } else {
3320 t = *tail = tline;
3321 tline = tline->next;
3322 t->mac = NULL;
3323 t->next = NULL;
3324 tail = &t->next;
3329 * Now scan the entire line and look for successive TOK_IDs that resulted
3330 * after expansion (they can't be produced by tokenize()). The successive
3331 * TOK_IDs should be concatenated.
3332 * Also we look for %+ tokens and concatenate the tokens before and after
3333 * them (without white spaces in between).
3335 t = thead;
3336 rescan = 0;
3337 while (t) {
3338 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3339 t = t->next;
3340 if (!t || !t->next)
3341 break;
3342 if (t->next->type == TOK_ID ||
3343 t->next->type == TOK_PREPROC_ID ||
3344 t->next->type == TOK_NUMBER) {
3345 char *p = nasm_strcat(t->text, t->next->text);
3346 nasm_free(t->text);
3347 t->next = delete_Token(t->next);
3348 t->text = p;
3349 rescan = 1;
3350 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3351 t->next->next->type == TOK_PREPROC_ID &&
3352 strcmp(t->next->next->text, "%+") == 0) {
3353 /* free the next whitespace, the %+ token and next whitespace */
3354 int i;
3355 for (i = 1; i <= 3; i++) {
3356 if (!t->next
3357 || (i != 2 && t->next->type != TOK_WHITESPACE))
3358 break;
3359 t->next = delete_Token(t->next);
3360 } /* endfor */
3361 } else
3362 t = t->next;
3364 /* If we concatenaded something, re-scan the line for macros */
3365 if (rescan) {
3366 tline = thead;
3367 goto again;
3370 if (org_tline) {
3371 if (thead) {
3372 *org_tline = *thead;
3373 /* since we just gave text to org_line, don't free it */
3374 thead->text = NULL;
3375 delete_Token(thead);
3376 } else {
3377 /* the expression expanded to empty line;
3378 we can't return NULL for some reasons
3379 we just set the line to a single WHITESPACE token. */
3380 memset(org_tline, 0, sizeof(*org_tline));
3381 org_tline->text = NULL;
3382 org_tline->type = TOK_WHITESPACE;
3384 thead = org_tline;
3387 return thead;
3391 * Similar to expand_smacro but used exclusively with macro identifiers
3392 * right before they are fetched in. The reason is that there can be
3393 * identifiers consisting of several subparts. We consider that if there
3394 * are more than one element forming the name, user wants a expansion,
3395 * otherwise it will be left as-is. Example:
3397 * %define %$abc cde
3399 * the identifier %$abc will be left as-is so that the handler for %define
3400 * will suck it and define the corresponding value. Other case:
3402 * %define _%$abc cde
3404 * In this case user wants name to be expanded *before* %define starts
3405 * working, so we'll expand %$abc into something (if it has a value;
3406 * otherwise it will be left as-is) then concatenate all successive
3407 * PP_IDs into one.
3409 static Token *expand_id(Token * tline)
3411 Token *cur, *oldnext = NULL;
3413 if (!tline || !tline->next)
3414 return tline;
3416 cur = tline;
3417 while (cur->next &&
3418 (cur->next->type == TOK_ID ||
3419 cur->next->type == TOK_PREPROC_ID
3420 || cur->next->type == TOK_NUMBER))
3421 cur = cur->next;
3423 /* If identifier consists of just one token, don't expand */
3424 if (cur == tline)
3425 return tline;
3427 if (cur) {
3428 oldnext = cur->next; /* Detach the tail past identifier */
3429 cur->next = NULL; /* so that expand_smacro stops here */
3432 tline = expand_smacro(tline);
3434 if (cur) {
3435 /* expand_smacro possibly changhed tline; re-scan for EOL */
3436 cur = tline;
3437 while (cur && cur->next)
3438 cur = cur->next;
3439 if (cur)
3440 cur->next = oldnext;
3443 return tline;
3447 * Determine whether the given line constitutes a multi-line macro
3448 * call, and return the MMacro structure called if so. Doesn't have
3449 * to check for an initial label - that's taken care of in
3450 * expand_mmacro - but must check numbers of parameters. Guaranteed
3451 * to be called with tline->type == TOK_ID, so the putative macro
3452 * name is easy to find.
3454 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3456 MMacro *head, *m;
3457 Token **params;
3458 int nparam;
3460 head = (MMacro *) hash_findix(&mmacros, tline->text);
3463 * Efficiency: first we see if any macro exists with the given
3464 * name. If not, we can return NULL immediately. _Then_ we
3465 * count the parameters, and then we look further along the
3466 * list if necessary to find the proper MMacro.
3468 for (m = head; m; m = m->next)
3469 if (!mstrcmp(m->name, tline->text, m->casesense))
3470 break;
3471 if (!m)
3472 return NULL;
3475 * OK, we have a potential macro. Count and demarcate the
3476 * parameters.
3478 count_mmac_params(tline->next, &nparam, &params);
3481 * So we know how many parameters we've got. Find the MMacro
3482 * structure that handles this number.
3484 while (m) {
3485 if (m->nparam_min <= nparam
3486 && (m->plus || nparam <= m->nparam_max)) {
3488 * This one is right. Just check if cycle removal
3489 * prohibits us using it before we actually celebrate...
3491 if (m->in_progress) {
3492 #if 0
3493 error(ERR_NONFATAL,
3494 "self-reference in multi-line macro `%s'", m->name);
3495 #endif
3496 nasm_free(params);
3497 return NULL;
3500 * It's right, and we can use it. Add its default
3501 * parameters to the end of our list if necessary.
3503 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3504 params =
3505 nasm_realloc(params,
3506 ((m->nparam_min + m->ndefs +
3507 1) * sizeof(*params)));
3508 while (nparam < m->nparam_min + m->ndefs) {
3509 params[nparam] = m->defaults[nparam - m->nparam_min];
3510 nparam++;
3514 * If we've gone over the maximum parameter count (and
3515 * we're in Plus mode), ignore parameters beyond
3516 * nparam_max.
3518 if (m->plus && nparam > m->nparam_max)
3519 nparam = m->nparam_max;
3521 * Then terminate the parameter list, and leave.
3523 if (!params) { /* need this special case */
3524 params = nasm_malloc(sizeof(*params));
3525 nparam = 0;
3527 params[nparam] = NULL;
3528 *params_array = params;
3529 return m;
3532 * This one wasn't right: look for the next one with the
3533 * same name.
3535 for (m = m->next; m; m = m->next)
3536 if (!mstrcmp(m->name, tline->text, m->casesense))
3537 break;
3541 * After all that, we didn't find one with the right number of
3542 * parameters. Issue a warning, and fail to expand the macro.
3544 error(ERR_WARNING | ERR_WARN_MNP,
3545 "macro `%s' exists, but not taking %d parameters",
3546 tline->text, nparam);
3547 nasm_free(params);
3548 return NULL;
3552 * Expand the multi-line macro call made by the given line, if
3553 * there is one to be expanded. If there is, push the expansion on
3554 * istk->expansion and return 1. Otherwise return 0.
3556 static int expand_mmacro(Token * tline)
3558 Token *startline = tline;
3559 Token *label = NULL;
3560 int dont_prepend = 0;
3561 Token **params, *t, *mtok, *tt;
3562 MMacro *m;
3563 Line *l, *ll;
3564 int i, nparam, *paramlen;
3566 t = tline;
3567 skip_white_(t);
3568 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3569 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3570 return 0;
3571 mtok = t;
3572 m = is_mmacro(t, &params);
3573 if (!m) {
3574 Token *last;
3576 * We have an id which isn't a macro call. We'll assume
3577 * it might be a label; we'll also check to see if a
3578 * colon follows it. Then, if there's another id after
3579 * that lot, we'll check it again for macro-hood.
3581 label = last = t;
3582 t = t->next;
3583 if (tok_type_(t, TOK_WHITESPACE))
3584 last = t, t = t->next;
3585 if (tok_is_(t, ":")) {
3586 dont_prepend = 1;
3587 last = t, t = t->next;
3588 if (tok_type_(t, TOK_WHITESPACE))
3589 last = t, t = t->next;
3591 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3592 return 0;
3593 last->next = NULL;
3594 tline = t;
3598 * Fix up the parameters: this involves stripping leading and
3599 * trailing whitespace, then stripping braces if they are
3600 * present.
3602 for (nparam = 0; params[nparam]; nparam++) ;
3603 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3605 for (i = 0; params[i]; i++) {
3606 int brace = false;
3607 int comma = (!m->plus || i < nparam - 1);
3609 t = params[i];
3610 skip_white_(t);
3611 if (tok_is_(t, "{"))
3612 t = t->next, brace = true, comma = false;
3613 params[i] = t;
3614 paramlen[i] = 0;
3615 while (t) {
3616 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3617 break; /* ... because we have hit a comma */
3618 if (comma && t->type == TOK_WHITESPACE
3619 && tok_is_(t->next, ","))
3620 break; /* ... or a space then a comma */
3621 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3622 break; /* ... or a brace */
3623 t = t->next;
3624 paramlen[i]++;
3629 * OK, we have a MMacro structure together with a set of
3630 * parameters. We must now go through the expansion and push
3631 * copies of each Line on to istk->expansion. Substitution of
3632 * parameter tokens and macro-local tokens doesn't get done
3633 * until the single-line macro substitution process; this is
3634 * because delaying them allows us to change the semantics
3635 * later through %rotate.
3637 * First, push an end marker on to istk->expansion, mark this
3638 * macro as in progress, and set up its invocation-specific
3639 * variables.
3641 ll = nasm_malloc(sizeof(Line));
3642 ll->next = istk->expansion;
3643 ll->finishes = m;
3644 ll->first = NULL;
3645 istk->expansion = ll;
3647 m->in_progress = true;
3648 m->params = params;
3649 m->iline = tline;
3650 m->nparam = nparam;
3651 m->rotate = 0;
3652 m->paramlen = paramlen;
3653 m->unique = unique++;
3654 m->lineno = 0;
3656 m->next_active = istk->mstk;
3657 istk->mstk = m;
3659 for (l = m->expansion; l; l = l->next) {
3660 Token **tail;
3662 ll = nasm_malloc(sizeof(Line));
3663 ll->finishes = NULL;
3664 ll->next = istk->expansion;
3665 istk->expansion = ll;
3666 tail = &ll->first;
3668 for (t = l->first; t; t = t->next) {
3669 Token *x = t;
3670 switch (t->type) {
3671 case TOK_PREPROC_Q:
3672 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3673 break;
3674 case TOK_PREPROC_QQ:
3675 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3676 break;
3677 case TOK_PREPROC_ID:
3678 if (t->text[1] == '0' && t->text[2] == '0') {
3679 dont_prepend = -1;
3680 x = label;
3681 if (!x)
3682 continue;
3684 /* fall through */
3685 default:
3686 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3687 break;
3689 tail = &tt->next;
3691 *tail = NULL;
3695 * If we had a label, push it on as the first line of
3696 * the macro expansion.
3698 if (label) {
3699 if (dont_prepend < 0)
3700 free_tlist(startline);
3701 else {
3702 ll = nasm_malloc(sizeof(Line));
3703 ll->finishes = NULL;
3704 ll->next = istk->expansion;
3705 istk->expansion = ll;
3706 ll->first = startline;
3707 if (!dont_prepend) {
3708 while (label->next)
3709 label = label->next;
3710 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3715 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3717 return 1;
3721 * Since preprocessor always operate only on the line that didn't
3722 * arrived yet, we should always use ERR_OFFBY1. Also since user
3723 * won't want to see same error twice (preprocessing is done once
3724 * per pass) we will want to show errors only during pass one.
3726 static void error(int severity, const char *fmt, ...)
3728 va_list arg;
3729 char buff[1024];
3731 /* If we're in a dead branch of IF or something like it, ignore the error */
3732 if (istk && istk->conds && !emitting(istk->conds->state))
3733 return;
3735 va_start(arg, fmt);
3736 vsnprintf(buff, sizeof(buff), fmt, arg);
3737 va_end(arg);
3739 if (istk && istk->mstk && istk->mstk->name)
3740 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3741 istk->mstk->lineno, buff);
3742 else
3743 _error(severity | ERR_PASS1, "%s", buff);
3746 static void
3747 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3748 ListGen * listgen, StrList **deplist)
3750 _error = errfunc;
3751 cstk = NULL;
3752 istk = nasm_malloc(sizeof(Include));
3753 istk->next = NULL;
3754 istk->conds = NULL;
3755 istk->expansion = NULL;
3756 istk->mstk = NULL;
3757 istk->fp = fopen(file, "r");
3758 istk->fname = NULL;
3759 src_set_fname(nasm_strdup(file));
3760 src_set_linnum(0);
3761 istk->lineinc = 1;
3762 if (!istk->fp)
3763 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3764 file);
3765 defining = NULL;
3766 init_macros();
3767 unique = 0;
3768 if (tasm_compatible_mode) {
3769 stdmacpos = nasm_stdmac;
3770 } else {
3771 stdmacpos = nasm_stdmac_after_tasm;
3773 any_extrastdmac = (extrastdmac != NULL);
3774 list = listgen;
3775 evaluate = eval;
3776 pass = apass;
3777 dephead = deptail = deplist;
3778 if (deplist) {
3779 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3780 sl->next = NULL;
3781 strcpy(sl->str, file);
3782 *deptail = sl;
3783 deptail = &sl->next;
3787 static char *pp_getline(void)
3789 char *line;
3790 Token *tline;
3792 while (1) {
3794 * Fetch a tokenized line, either from the macro-expansion
3795 * buffer or from the input file.
3797 tline = NULL;
3798 while (istk->expansion && istk->expansion->finishes) {
3799 Line *l = istk->expansion;
3800 if (!l->finishes->name && l->finishes->in_progress > 1) {
3801 Line *ll;
3804 * This is a macro-end marker for a macro with no
3805 * name, which means it's not really a macro at all
3806 * but a %rep block, and the `in_progress' field is
3807 * more than 1, meaning that we still need to
3808 * repeat. (1 means the natural last repetition; 0
3809 * means termination by %exitrep.) We have
3810 * therefore expanded up to the %endrep, and must
3811 * push the whole block on to the expansion buffer
3812 * again. We don't bother to remove the macro-end
3813 * marker: we'd only have to generate another one
3814 * if we did.
3816 l->finishes->in_progress--;
3817 for (l = l->finishes->expansion; l; l = l->next) {
3818 Token *t, *tt, **tail;
3820 ll = nasm_malloc(sizeof(Line));
3821 ll->next = istk->expansion;
3822 ll->finishes = NULL;
3823 ll->first = NULL;
3824 tail = &ll->first;
3826 for (t = l->first; t; t = t->next) {
3827 if (t->text || t->type == TOK_WHITESPACE) {
3828 tt = *tail =
3829 new_Token(NULL, t->type, t->text, 0);
3830 tail = &tt->next;
3834 istk->expansion = ll;
3836 } else {
3838 * Check whether a `%rep' was started and not ended
3839 * within this macro expansion. This can happen and
3840 * should be detected. It's a fatal error because
3841 * I'm too confused to work out how to recover
3842 * sensibly from it.
3844 if (defining) {
3845 if (defining->name)
3846 error(ERR_PANIC,
3847 "defining with name in expansion");
3848 else if (istk->mstk->name)
3849 error(ERR_FATAL,
3850 "`%%rep' without `%%endrep' within"
3851 " expansion of macro `%s'",
3852 istk->mstk->name);
3856 * FIXME: investigate the relationship at this point between
3857 * istk->mstk and l->finishes
3860 MMacro *m = istk->mstk;
3861 istk->mstk = m->next_active;
3862 if (m->name) {
3864 * This was a real macro call, not a %rep, and
3865 * therefore the parameter information needs to
3866 * be freed.
3868 nasm_free(m->params);
3869 free_tlist(m->iline);
3870 nasm_free(m->paramlen);
3871 l->finishes->in_progress = false;
3872 } else
3873 free_mmacro(m);
3875 istk->expansion = l->next;
3876 nasm_free(l);
3877 list->downlevel(LIST_MACRO);
3880 while (1) { /* until we get a line we can use */
3882 if (istk->expansion) { /* from a macro expansion */
3883 char *p;
3884 Line *l = istk->expansion;
3885 if (istk->mstk)
3886 istk->mstk->lineno++;
3887 tline = l->first;
3888 istk->expansion = l->next;
3889 nasm_free(l);
3890 p = detoken(tline, false);
3891 list->line(LIST_MACRO, p);
3892 nasm_free(p);
3893 break;
3895 line = read_line();
3896 if (line) { /* from the current input file */
3897 line = prepreproc(line);
3898 tline = tokenize(line);
3899 nasm_free(line);
3900 break;
3903 * The current file has ended; work down the istk
3906 Include *i = istk;
3907 fclose(i->fp);
3908 if (i->conds)
3909 error(ERR_FATAL,
3910 "expected `%%endif' before end of file");
3911 /* only set line and file name if there's a next node */
3912 if (i->next) {
3913 src_set_linnum(i->lineno);
3914 nasm_free(src_set_fname(i->fname));
3916 istk = i->next;
3917 list->downlevel(LIST_INCLUDE);
3918 nasm_free(i);
3919 if (!istk)
3920 return NULL;
3925 * We must expand MMacro parameters and MMacro-local labels
3926 * _before_ we plunge into directive processing, to cope
3927 * with things like `%define something %1' such as STRUC
3928 * uses. Unless we're _defining_ a MMacro, in which case
3929 * those tokens should be left alone to go into the
3930 * definition; and unless we're in a non-emitting
3931 * condition, in which case we don't want to meddle with
3932 * anything.
3934 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3935 tline = expand_mmac_params(tline);
3938 * Check the line to see if it's a preprocessor directive.
3940 if (do_directive(tline) == DIRECTIVE_FOUND) {
3941 continue;
3942 } else if (defining) {
3944 * We're defining a multi-line macro. We emit nothing
3945 * at all, and just
3946 * shove the tokenized line on to the macro definition.
3948 Line *l = nasm_malloc(sizeof(Line));
3949 l->next = defining->expansion;
3950 l->first = tline;
3951 l->finishes = false;
3952 defining->expansion = l;
3953 continue;
3954 } else if (istk->conds && !emitting(istk->conds->state)) {
3956 * We're in a non-emitting branch of a condition block.
3957 * Emit nothing at all, not even a blank line: when we
3958 * emerge from the condition we'll give a line-number
3959 * directive so we keep our place correctly.
3961 free_tlist(tline);
3962 continue;
3963 } else if (istk->mstk && !istk->mstk->in_progress) {
3965 * We're in a %rep block which has been terminated, so
3966 * we're walking through to the %endrep without
3967 * emitting anything. Emit nothing at all, not even a
3968 * blank line: when we emerge from the %rep block we'll
3969 * give a line-number directive so we keep our place
3970 * correctly.
3972 free_tlist(tline);
3973 continue;
3974 } else {
3975 tline = expand_smacro(tline);
3976 if (!expand_mmacro(tline)) {
3978 * De-tokenize the line again, and emit it.
3980 line = detoken(tline, true);
3981 free_tlist(tline);
3982 break;
3983 } else {
3984 continue; /* expand_mmacro calls free_tlist */
3989 return line;
3992 static void pp_cleanup(int pass)
3994 if (defining) {
3995 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3996 defining->name);
3997 free_mmacro(defining);
3999 while (cstk)
4000 ctx_pop();
4001 free_macros();
4002 while (istk) {
4003 Include *i = istk;
4004 istk = istk->next;
4005 fclose(i->fp);
4006 nasm_free(i->fname);
4007 nasm_free(i);
4009 while (cstk)
4010 ctx_pop();
4011 if (pass == 0) {
4012 free_llist(predef);
4013 delete_Blocks();
4017 void pp_include_path(char *path)
4019 IncPath *i;
4021 i = nasm_malloc(sizeof(IncPath));
4022 i->path = path ? nasm_strdup(path) : NULL;
4023 i->next = NULL;
4025 if (ipath != NULL) {
4026 IncPath *j = ipath;
4027 while (j->next != NULL)
4028 j = j->next;
4029 j->next = i;
4030 } else {
4031 ipath = i;
4036 * added by alexfru:
4038 * This function is used to "export" the include paths, e.g.
4039 * the paths specified in the '-I' command switch.
4040 * The need for such exporting is due to the 'incbin' directive,
4041 * which includes raw binary files (unlike '%include', which
4042 * includes text source files). It would be real nice to be
4043 * able to specify paths to search for incbin'ned files also.
4044 * So, this is a simple workaround.
4046 * The function use is simple:
4048 * The 1st call (with NULL argument) returns a pointer to the 1st path
4049 * (char** type) or NULL if none include paths available.
4051 * All subsequent calls take as argument the value returned by this
4052 * function last. The return value is either the next path
4053 * (char** type) or NULL if the end of the paths list is reached.
4055 * It is maybe not the best way to do things, but I didn't want
4056 * to export too much, just one or two functions and no types or
4057 * variables exported.
4059 * Can't say I like the current situation with e.g. this path list either,
4060 * it seems to be never deallocated after creation...
4062 char **pp_get_include_path_ptr(char **pPrevPath)
4064 /* This macro returns offset of a member of a structure */
4065 #define GetMemberOffset(StructType,MemberName)\
4066 ((size_t)&((StructType*)0)->MemberName)
4067 IncPath *i;
4069 if (pPrevPath == NULL) {
4070 if (ipath != NULL)
4071 return &ipath->path;
4072 else
4073 return NULL;
4075 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4076 i = i->next;
4077 if (i != NULL)
4078 return &i->path;
4079 else
4080 return NULL;
4081 #undef GetMemberOffset
4084 void pp_pre_include(char *fname)
4086 Token *inc, *space, *name;
4087 Line *l;
4089 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4090 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4091 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4093 l = nasm_malloc(sizeof(Line));
4094 l->next = predef;
4095 l->first = inc;
4096 l->finishes = false;
4097 predef = l;
4100 void pp_pre_define(char *definition)
4102 Token *def, *space;
4103 Line *l;
4104 char *equals;
4106 equals = strchr(definition, '=');
4107 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4108 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4109 if (equals)
4110 *equals = ' ';
4111 space->next = tokenize(definition);
4112 if (equals)
4113 *equals = '=';
4115 l = nasm_malloc(sizeof(Line));
4116 l->next = predef;
4117 l->first = def;
4118 l->finishes = false;
4119 predef = l;
4122 void pp_pre_undefine(char *definition)
4124 Token *def, *space;
4125 Line *l;
4127 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4128 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4129 space->next = tokenize(definition);
4131 l = nasm_malloc(sizeof(Line));
4132 l->next = predef;
4133 l->first = def;
4134 l->finishes = false;
4135 predef = l;
4139 * Added by Keith Kanios:
4141 * This function is used to assist with "runtime" preprocessor
4142 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4144 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4145 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4148 void pp_runtime(char *definition)
4150 Token *def;
4152 def = tokenize(definition);
4153 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4154 free_tlist(def);
4158 void pp_extra_stdmac(const char **macros)
4160 extrastdmac = macros;
4163 static void make_tok_num(Token * tok, int64_t val)
4165 char numbuf[20];
4166 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4167 tok->text = nasm_strdup(numbuf);
4168 tok->type = TOK_NUMBER;
4171 Preproc nasmpp = {
4172 pp_reset,
4173 pp_getline,
4174 pp_cleanup