preproc.c: get_ctx() can return NULL...
[nasm/avx512.git] / preproc.c
blob482d6af29626231c4a1bf1fd6a4b69a30991daba
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 */
331 static uint64_t unique; /* unique identifier numbers */
333 static Line *predef = NULL;
335 static ListGen *list;
338 * The current set of multi-line macros we have defined.
340 static struct hash_table mmacros;
343 * The current set of single-line macros we have defined.
345 static struct hash_table smacros;
348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
351 static MMacro *defining;
354 * The number of macro parameters to allocate space for at a time.
356 #define PARAM_DELTA 16
359 * The standard macro set: defined in macros.c in the array nasm_stdmac.
360 * This gives our position in the macro set, when we're processing it.
362 static const char * const *stdmacpos;
365 * The extra standard macros that come from the object format, if
366 * any.
368 static const char * const *extrastdmac = NULL;
369 bool any_extrastdmac;
372 * Tokens are allocated in blocks to improve speed
374 #define TOKEN_BLOCKSIZE 4096
375 static Token *freeTokens = NULL;
376 struct Blocks {
377 Blocks *next;
378 void *chunk;
381 static Blocks blocks = { NULL, NULL };
384 * Forward declarations.
386 static Token *expand_mmac_params(Token * tline);
387 static Token *expand_smacro(Token * tline);
388 static Token *expand_id(Token * tline);
389 static Context *get_ctx(char *name, bool all_contexts);
390 static void make_tok_num(Token * tok, int64_t val);
391 static void error(int severity, const char *fmt, ...);
392 static void *new_Block(size_t size);
393 static void delete_Blocks(void);
394 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
395 static Token *delete_Token(Token * t);
398 * Macros for safe checking of token pointers, avoid *(NULL)
400 #define tok_type_(x,t) ((x) && (x)->type == (t))
401 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
402 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
403 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
405 /* Handle TASM specific directives, which do not contain a % in
406 * front of them. We do it here because I could not find any other
407 * place to do it for the moment, and it is a hack (ideally it would
408 * be nice to be able to use the NASM pre-processor to do it).
410 static char *check_tasm_directive(char *line)
412 int32_t i, j, k, m, len;
413 char *p = line, *oldline, oldchar;
415 /* Skip whitespace */
416 while (isspace(*p) && *p != 0)
417 p++;
419 /* Binary search for the directive name */
420 i = -1;
421 j = elements(tasm_directives);
422 len = 0;
423 while (!isspace(p[len]) && p[len] != 0)
424 len++;
425 if (len) {
426 oldchar = p[len];
427 p[len] = 0;
428 while (j - i > 1) {
429 k = (j + i) / 2;
430 m = nasm_stricmp(p, tasm_directives[k]);
431 if (m == 0) {
432 /* We have found a directive, so jam a % in front of it
433 * so that NASM will then recognise it as one if it's own.
435 p[len] = oldchar;
436 len = strlen(p);
437 oldline = line;
438 line = nasm_malloc(len + 2);
439 line[0] = '%';
440 if (k == TM_IFDIFI) {
441 /* NASM does not recognise IFDIFI, so we convert it to
442 * %ifdef BOGUS. This is not used in NASM comaptible
443 * code, but does need to parse for the TASM macro
444 * package.
446 strcpy(line + 1, "ifdef BOGUS");
447 } else {
448 memcpy(line + 1, p, len + 1);
450 nasm_free(oldline);
451 return line;
452 } else if (m < 0) {
453 j = k;
454 } else
455 i = k;
457 p[len] = oldchar;
459 return line;
463 * The pre-preprocessing stage... This function translates line
464 * number indications as they emerge from GNU cpp (`# lineno "file"
465 * flags') into NASM preprocessor line number indications (`%line
466 * lineno file').
468 static char *prepreproc(char *line)
470 int lineno, fnlen;
471 char *fname, *oldline;
473 if (line[0] == '#' && line[1] == ' ') {
474 oldline = line;
475 fname = oldline + 2;
476 lineno = atoi(fname);
477 fname += strspn(fname, "0123456789 ");
478 if (*fname == '"')
479 fname++;
480 fnlen = strcspn(fname, "\"");
481 line = nasm_malloc(20 + fnlen);
482 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
483 nasm_free(oldline);
485 if (tasm_compatible_mode)
486 return check_tasm_directive(line);
487 return line;
491 * Free a linked list of tokens.
493 static void free_tlist(Token * list)
495 while (list) {
496 list = delete_Token(list);
501 * Free a linked list of lines.
503 static void free_llist(Line * list)
505 Line *l;
506 while (list) {
507 l = list;
508 list = list->next;
509 free_tlist(l->first);
510 nasm_free(l);
515 * Free an MMacro
517 static void free_mmacro(MMacro * m)
519 nasm_free(m->name);
520 free_tlist(m->dlist);
521 nasm_free(m->defaults);
522 free_llist(m->expansion);
523 nasm_free(m);
527 * Free all currently defined macros, and free the hash tables
529 static void free_smacro_table(struct hash_table *smt)
531 SMacro *s;
532 const char *key;
533 struct hash_tbl_node *it = NULL;
535 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
536 nasm_free((void *)key);
537 while (s) {
538 SMacro *ns = s->next;
539 nasm_free(s->name);
540 free_tlist(s->expansion);
541 nasm_free(s);
542 s = ns;
545 hash_free(smt);
548 static void free_mmacro_table(struct hash_table *mmt)
550 MMacro *m;
551 const char *key;
552 struct hash_tbl_node *it = NULL;
554 it = NULL;
555 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
556 nasm_free((void *)key);
557 while (m) {
558 MMacro *nm = m->next;
559 free_mmacro(m);
560 m = nm;
563 hash_free(mmt);
566 static void free_macros(void)
568 free_smacro_table(&smacros);
569 free_mmacro_table(&mmacros);
573 * Initialize the hash tables
575 static void init_macros(void)
577 hash_init(&smacros, HASH_LARGE);
578 hash_init(&mmacros, HASH_LARGE);
582 * Pop the context stack.
584 static void ctx_pop(void)
586 Context *c = cstk;
588 cstk = cstk->next;
589 free_smacro_table(&c->localmac);
590 nasm_free(c->name);
591 nasm_free(c);
595 * Search for a key in the hash index; adding it if necessary
596 * (in which case we initialize the data pointer to NULL.)
598 static void **
599 hash_findi_add(struct hash_table *hash, const char *str)
601 struct hash_insert hi;
602 void **r;
603 char *strx;
605 r = hash_findi(hash, str, &hi);
606 if (r)
607 return r;
609 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
610 return hash_add(&hi, strx, NULL);
614 * Like hash_findi, but returns the data element rather than a pointer
615 * to it. Used only when not adding a new element, hence no third
616 * argument.
618 static void *
619 hash_findix(struct hash_table *hash, const char *str)
621 void **p;
623 p = hash_findi(hash, str, NULL);
624 return p ? *p : NULL;
627 #define BUF_DELTA 512
629 * Read a line from the top file in istk, handling multiple CR/LFs
630 * at the end of the line read, and handling spurious ^Zs. Will
631 * return lines from the standard macro set if this has not already
632 * been done.
634 static char *read_line(void)
636 char *buffer, *p, *q;
637 int bufsize, continued_count;
639 if (stdmacpos) {
640 if (*stdmacpos) {
641 char *ret = nasm_strdup(*stdmacpos++);
642 if (!*stdmacpos && any_extrastdmac) {
643 stdmacpos = extrastdmac;
644 any_extrastdmac = false;
645 return ret;
648 * Nasty hack: here we push the contents of `predef' on
649 * to the top-level expansion stack, since this is the
650 * most convenient way to implement the pre-include and
651 * pre-define features.
653 if (!*stdmacpos) {
654 Line *pd, *l;
655 Token *head, **tail, *t;
657 for (pd = predef; pd; pd = pd->next) {
658 head = NULL;
659 tail = &head;
660 for (t = pd->first; t; t = t->next) {
661 *tail = new_Token(NULL, t->type, t->text, 0);
662 tail = &(*tail)->next;
664 l = nasm_malloc(sizeof(Line));
665 l->next = istk->expansion;
666 l->first = head;
667 l->finishes = false;
668 istk->expansion = l;
671 return ret;
672 } else {
673 stdmacpos = NULL;
677 bufsize = BUF_DELTA;
678 buffer = nasm_malloc(BUF_DELTA);
679 p = buffer;
680 continued_count = 0;
681 while (1) {
682 q = fgets(p, bufsize - (p - buffer), istk->fp);
683 if (!q)
684 break;
685 p += strlen(p);
686 if (p > buffer && p[-1] == '\n') {
687 /* Convert backslash-CRLF line continuation sequences into
688 nothing at all (for DOS and Windows) */
689 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
690 p -= 3;
691 *p = 0;
692 continued_count++;
694 /* Also convert backslash-LF line continuation sequences into
695 nothing at all (for Unix) */
696 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
697 p -= 2;
698 *p = 0;
699 continued_count++;
700 } else {
701 break;
704 if (p - buffer > bufsize - 10) {
705 int32_t offset = p - buffer;
706 bufsize += BUF_DELTA;
707 buffer = nasm_realloc(buffer, bufsize);
708 p = buffer + offset; /* prevent stale-pointer problems */
712 if (!q && p == buffer) {
713 nasm_free(buffer);
714 return NULL;
717 src_set_linnum(src_get_linnum() + istk->lineinc +
718 (continued_count * istk->lineinc));
721 * Play safe: remove CRs as well as LFs, if any of either are
722 * present at the end of the line.
724 while (--p >= buffer && (*p == '\n' || *p == '\r'))
725 *p = '\0';
728 * Handle spurious ^Z, which may be inserted into source files
729 * by some file transfer utilities.
731 buffer[strcspn(buffer, "\032")] = '\0';
733 list->line(LIST_READ, buffer);
735 return buffer;
739 * Tokenize a line of text. This is a very simple process since we
740 * don't need to parse the value out of e.g. numeric tokens: we
741 * simply split one string into many.
743 static Token *tokenize(char *line)
745 char *p = line;
746 enum pp_token_type type;
747 Token *list = NULL;
748 Token *t, **tail = &list;
750 while (*line) {
751 p = line;
752 if (*p == '%') {
753 p++;
754 if (isdigit(*p) ||
755 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
756 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
757 do {
758 p++;
760 while (isdigit(*p));
761 type = TOK_PREPROC_ID;
762 } else if (*p == '{') {
763 p++;
764 while (*p && *p != '}') {
765 p[-1] = *p;
766 p++;
768 p[-1] = '\0';
769 if (*p)
770 p++;
771 type = TOK_PREPROC_ID;
772 } else if (*p == '?') {
773 type = TOK_PREPROC_Q; /* %? */
774 p++;
775 if (*p == '?') {
776 type = TOK_PREPROC_QQ; /* %?? */
777 p++;
779 } else if (isidchar(*p) ||
780 ((*p == '!' || *p == '%' || *p == '$') &&
781 isidchar(p[1]))) {
782 do {
783 p++;
785 while (isidchar(*p));
786 type = TOK_PREPROC_ID;
787 } else {
788 type = TOK_OTHER;
789 if (*p == '%')
790 p++;
792 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
793 type = TOK_ID;
794 p++;
795 while (*p && isidchar(*p))
796 p++;
797 } else if (*p == '\'' || *p == '"') {
799 * A string token.
801 char c = *p;
802 p++;
803 type = TOK_STRING;
804 while (*p && *p != c)
805 p++;
807 if (*p) {
808 p++;
809 } else {
810 error(ERR_WARNING, "unterminated string");
811 /* Handling unterminated strings by UNV */
812 /* type = -1; */
814 } else if (isnumstart(*p)) {
815 bool is_hex = false;
816 bool is_float = false;
817 bool has_e = false;
818 char c, *r;
821 * A numeric token.
824 if (*p == '$') {
825 p++;
826 is_hex = true;
829 for (;;) {
830 c = *p++;
832 if (!is_hex && (c == 'e' || c == 'E')) {
833 has_e = true;
834 if (*p == '+' || *p == '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
837 p++;
838 is_float = true;
840 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
841 is_hex = true;
842 } else if (c == 'P' || c == 'p') {
843 is_float = true;
844 if (*p == '+' || *p == '-')
845 p++;
846 } else if (isnumchar(c) || c == '_')
847 ; /* just advance */
848 else if (c == '.') {
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
857 r = p;
858 while (*r == '_')
859 r++;
861 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
862 (!is_hex && (*r == 'e' || *r == 'E')) ||
863 (*r == 'p' || *r == 'P')) {
864 p = r;
865 is_float = true;
866 } else
867 break; /* Terminate the token */
868 } else
869 break;
871 p--; /* Point to first character beyond number */
873 if (has_e && !is_hex) {
874 /* 1e13 is floating-point, but 1e13h is not */
875 is_float = true;
878 type = is_float ? TOK_FLOAT : TOK_NUMBER;
879 } else if (isspace(*p)) {
880 type = TOK_WHITESPACE;
881 p++;
882 while (*p && isspace(*p))
883 p++;
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
889 if (!*p || *p == ';') {
890 type = TOK_COMMENT;
891 while (*p)
892 p++;
894 } else if (*p == ';') {
895 type = TOK_COMMENT;
896 while (*p)
897 p++;
898 } else {
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
903 * else is a single-character operator.
905 type = TOK_OTHER;
906 if ((p[0] == '>' && p[1] == '>') ||
907 (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++;
919 p++;
922 /* Handling unterminated string by UNV */
923 /*if (type == -1)
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
927 tail = &t->next;
929 else */
930 if (type != TOK_COMMENT) {
931 *tail = t = new_Token(NULL, type, line, p - line);
932 tail = &t->next;
934 line = p;
936 return list;
940 * this function allocates a new managed block of memory and
941 * returns a pointer to the block. The managed blocks are
942 * deleted only all at once by the delete_Blocks function.
944 static void *new_Block(size_t size)
946 Blocks *b = &blocks;
948 /* first, get to the end of the linked list */
949 while (b->next)
950 b = b->next;
951 /* now allocate the requested chunk */
952 b->chunk = nasm_malloc(size);
954 /* now allocate a new block for the next request */
955 b->next = nasm_malloc(sizeof(Blocks));
956 /* and initialize the contents of the new block */
957 b->next->next = NULL;
958 b->next->chunk = NULL;
959 return b->chunk;
963 * this function deletes all managed blocks of memory
965 static void delete_Blocks(void)
967 Blocks *a, *b = &blocks;
970 * keep in mind that the first block, pointed to by blocks
971 * is a static and not dynamically allocated, so we don't
972 * free it.
974 while (b) {
975 if (b->chunk)
976 nasm_free(b->chunk);
977 a = b;
978 b = b->next;
979 if (a != &blocks)
980 nasm_free(a);
985 * this function creates a new Token and passes a pointer to it
986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
989 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
991 Token *t;
992 int i;
994 if (freeTokens == NULL) {
995 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
996 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
997 freeTokens[i].next = &freeTokens[i + 1];
998 freeTokens[i].next = NULL;
1000 t = freeTokens;
1001 freeTokens = t->next;
1002 t->next = next;
1003 t->mac = NULL;
1004 t->type = type;
1005 if (type == TOK_WHITESPACE || text == NULL) {
1006 t->text = NULL;
1007 } else {
1008 if (txtlen == 0)
1009 txtlen = strlen(text);
1010 t->text = nasm_malloc(1 + txtlen);
1011 strncpy(t->text, text, txtlen);
1012 t->text[txtlen] = '\0';
1014 return t;
1017 static Token *delete_Token(Token * t)
1019 Token *next = t->next;
1020 nasm_free(t->text);
1021 t->next = freeTokens;
1022 freeTokens = t;
1023 return next;
1027 * Convert a line of tokens back into text.
1028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
1031 static char *detoken(Token * tlist, int expand_locals)
1033 Token *t;
1034 int len;
1035 char *line, *p;
1036 const char *q;
1038 len = 0;
1039 for (t = tlist; t; t = t->next) {
1040 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1041 char *p = getenv(t->text + 2);
1042 nasm_free(t->text);
1043 if (p)
1044 t->text = nasm_strdup(p);
1045 else
1046 t->text = NULL;
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals &&
1050 t->type == TOK_PREPROC_ID && t->text &&
1051 t->text[0] == '%' && t->text[1] == '$') {
1052 Context *ctx = get_ctx(t->text, false);
1053 if (ctx) {
1054 char buffer[40];
1055 char *p, *q = t->text + 2;
1057 q += strspn(q, "$");
1058 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1059 p = nasm_strcat(buffer, q);
1060 nasm_free(t->text);
1061 t->text = p;
1064 if (t->type == TOK_WHITESPACE) {
1065 len++;
1066 } else if (t->text) {
1067 len += strlen(t->text);
1070 p = line = nasm_malloc(len + 1);
1071 for (t = tlist; t; t = t->next) {
1072 if (t->type == TOK_WHITESPACE) {
1073 *p++ = ' ';
1074 } else if (t->text) {
1075 q = t->text;
1076 while (*q)
1077 *p++ = *q++;
1080 *p = '\0';
1081 return line;
1085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1088 * field.
1090 * FIX: This really needs to be unified with stdscan.
1092 static int ppscan(void *private_data, struct tokenval *tokval)
1094 Token **tlineptr = private_data;
1095 Token *tline;
1096 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1098 do {
1099 tline = *tlineptr;
1100 *tlineptr = tline ? tline->next : NULL;
1102 while (tline && (tline->type == TOK_WHITESPACE ||
1103 tline->type == TOK_COMMENT));
1105 if (!tline)
1106 return tokval->t_type = TOKEN_EOS;
1108 tokval->t_charptr = tline->text;
1110 if (tline->text[0] == '$' && !tline->text[1])
1111 return tokval->t_type = TOKEN_HERE;
1112 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1113 return tokval->t_type = TOKEN_BASE;
1115 if (tline->type == TOK_ID) {
1116 p = tokval->t_charptr = tline->text;
1117 if (p[0] == '$') {
1118 tokval->t_charptr++;
1119 return tokval->t_type = TOKEN_ID;
1122 for (r = p, s = ourcopy; *r; r++) {
1123 if (r >= p+MAX_KEYWORD)
1124 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1125 *s++ = tolower(*r);
1127 *s = '\0';
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy, tokval);
1133 if (tline->type == TOK_NUMBER) {
1134 bool rn_error;
1135 tokval->t_integer = readnum(tline->text, &rn_error);
1136 if (rn_error)
1137 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1138 tokval->t_charptr = tline->text;
1139 return tokval->t_type = TOKEN_NUM;
1142 if (tline->type == TOK_FLOAT) {
1143 return tokval->t_type = TOKEN_FLOAT;
1146 if (tline->type == TOK_STRING) {
1147 bool rn_warn;
1148 char q, *r;
1149 int l;
1151 r = tline->text;
1152 q = *r++;
1153 l = strlen(r);
1155 if (l == 0 || r[l - 1] != q)
1156 return tokval->t_type = TOKEN_ERRNUM;
1157 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1158 if (rn_warn)
1159 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1160 tokval->t_charptr = NULL;
1161 return tokval->t_type = TOKEN_NUM;
1164 if (tline->type == TOK_OTHER) {
1165 if (!strcmp(tline->text, "<<"))
1166 return tokval->t_type = TOKEN_SHL;
1167 if (!strcmp(tline->text, ">>"))
1168 return tokval->t_type = TOKEN_SHR;
1169 if (!strcmp(tline->text, "//"))
1170 return tokval->t_type = TOKEN_SDIV;
1171 if (!strcmp(tline->text, "%%"))
1172 return tokval->t_type = TOKEN_SMOD;
1173 if (!strcmp(tline->text, "=="))
1174 return tokval->t_type = TOKEN_EQ;
1175 if (!strcmp(tline->text, "<>"))
1176 return tokval->t_type = TOKEN_NE;
1177 if (!strcmp(tline->text, "!="))
1178 return tokval->t_type = TOKEN_NE;
1179 if (!strcmp(tline->text, "<="))
1180 return tokval->t_type = TOKEN_LE;
1181 if (!strcmp(tline->text, ">="))
1182 return tokval->t_type = TOKEN_GE;
1183 if (!strcmp(tline->text, "&&"))
1184 return tokval->t_type = TOKEN_DBL_AND;
1185 if (!strcmp(tline->text, "^^"))
1186 return tokval->t_type = TOKEN_DBL_XOR;
1187 if (!strcmp(tline->text, "||"))
1188 return tokval->t_type = TOKEN_DBL_OR;
1192 * We have no other options: just return the first character of
1193 * the token text.
1195 return tokval->t_type = tline->text[0];
1199 * Compare a string to the name of an existing macro; this is a
1200 * simple wrapper which calls either strcmp or nasm_stricmp
1201 * depending on the value of the `casesense' parameter.
1203 static int mstrcmp(const char *p, const char *q, bool casesense)
1205 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1209 * Return the Context structure associated with a %$ token. Return
1210 * NULL, having _already_ reported an error condition, if the
1211 * context stack isn't deep enough for the supplied number of $
1212 * signs.
1213 * If all_contexts == true, contexts that enclose current are
1214 * also scanned for such smacro, until it is found; if not -
1215 * only the context that directly results from the number of $'s
1216 * in variable's name.
1218 static Context *get_ctx(char *name, bool all_contexts)
1220 Context *ctx;
1221 SMacro *m;
1222 int i;
1224 if (!name || name[0] != '%' || name[1] != '$')
1225 return NULL;
1227 if (!cstk) {
1228 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1229 return NULL;
1232 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1233 ctx = ctx->next;
1234 /* i--; Lino - 02/25/02 */
1236 if (!ctx) {
1237 error(ERR_NONFATAL, "`%s': context stack is only"
1238 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1239 return NULL;
1241 if (!all_contexts)
1242 return ctx;
1244 do {
1245 /* Search for this smacro in found context */
1246 m = hash_findix(&ctx->localmac, name);
1247 while (m) {
1248 if (!mstrcmp(m->name, name, m->casesense))
1249 return ctx;
1250 m = m->next;
1252 ctx = ctx->next;
1254 while (ctx);
1255 return NULL;
1259 * Open an include file. This routine must always return a valid
1260 * file pointer if it returns - it's responsible for throwing an
1261 * ERR_FATAL and bombing out completely if not. It should also try
1262 * the include path one by one until it finds the file or reaches
1263 * the end of the path.
1265 static FILE *inc_fopen(char *file)
1267 FILE *fp;
1268 char *prefix = "", *combine;
1269 IncPath *ip = ipath;
1270 static int namelen = 0;
1271 int len = strlen(file);
1273 while (1) {
1274 combine = nasm_malloc(strlen(prefix) + len + 1);
1275 strcpy(combine, prefix);
1276 strcat(combine, file);
1277 fp = fopen(combine, "r");
1278 if (pass == 0 && fp) {
1279 namelen += strlen(combine) + 1;
1280 if (namelen > 62) {
1281 printf(" \\\n ");
1282 namelen = 2;
1284 printf(" %s", combine);
1286 nasm_free(combine);
1287 if (fp)
1288 return fp;
1289 if (!ip)
1290 break;
1291 prefix = ip->path;
1292 ip = ip->next;
1294 if (!prefix) {
1295 /* -MG given and file not found */
1296 if (pass == 0) {
1297 namelen += strlen(file) + 1;
1298 if (namelen > 62) {
1299 printf(" \\\n ");
1300 namelen = 2;
1302 printf(" %s", file);
1304 return NULL;
1308 error(ERR_FATAL, "unable to open include file `%s'", file);
1309 return NULL; /* never reached - placate compilers */
1313 * Determine if we should warn on defining a single-line macro of
1314 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1315 * return true if _any_ single-line macro of that name is defined.
1316 * Otherwise, will return true if a single-line macro with either
1317 * `nparam' or no parameters is defined.
1319 * If a macro with precisely the right number of parameters is
1320 * defined, or nparam is -1, the address of the definition structure
1321 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1322 * is NULL, no action will be taken regarding its contents, and no
1323 * error will occur.
1325 * Note that this is also called with nparam zero to resolve
1326 * `ifdef'.
1328 * If you already know which context macro belongs to, you can pass
1329 * the context pointer as first parameter; if you won't but name begins
1330 * with %$ the context will be automatically computed. If all_contexts
1331 * is true, macro will be searched in outer contexts as well.
1333 static bool
1334 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1335 bool nocase)
1337 struct hash_table *smtbl;
1338 SMacro *m;
1340 if (ctx) {
1341 smtbl = &ctx->localmac;
1342 } else if (name[0] == '%' && name[1] == '$') {
1343 if (cstk)
1344 ctx = get_ctx(name, false);
1345 if (!ctx)
1346 return false; /* got to return _something_ */
1347 smtbl = &ctx->localmac;
1348 } else {
1349 smtbl = &smacros;
1351 m = (SMacro *) hash_findix(smtbl, name);
1353 while (m) {
1354 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1355 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1356 if (defn) {
1357 if (nparam == (int) m->nparam || nparam == -1)
1358 *defn = m;
1359 else
1360 *defn = NULL;
1362 return true;
1364 m = m->next;
1367 return false;
1371 * Count and mark off the parameters in a multi-line macro call.
1372 * This is called both from within the multi-line macro expansion
1373 * code, and also to mark off the default parameters when provided
1374 * in a %macro definition line.
1376 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1378 int paramsize, brace;
1380 *nparam = paramsize = 0;
1381 *params = NULL;
1382 while (t) {
1383 if (*nparam >= paramsize) {
1384 paramsize += PARAM_DELTA;
1385 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1387 skip_white_(t);
1388 brace = false;
1389 if (tok_is_(t, "{"))
1390 brace = true;
1391 (*params)[(*nparam)++] = t;
1392 while (tok_isnt_(t, brace ? "}" : ","))
1393 t = t->next;
1394 if (t) { /* got a comma/brace */
1395 t = t->next;
1396 if (brace) {
1398 * Now we've found the closing brace, look further
1399 * for the comma.
1401 skip_white_(t);
1402 if (tok_isnt_(t, ",")) {
1403 error(ERR_NONFATAL,
1404 "braces do not enclose all of macro parameter");
1405 while (tok_isnt_(t, ","))
1406 t = t->next;
1408 if (t)
1409 t = t->next; /* eat the comma */
1416 * Determine whether one of the various `if' conditions is true or
1417 * not.
1419 * We must free the tline we get passed.
1421 static bool if_condition(Token * tline, enum preproc_token ct)
1423 enum pp_conditional i = PP_COND(ct);
1424 bool j;
1425 Token *t, *tt, **tptr, *origline;
1426 struct tokenval tokval;
1427 expr *evalresult;
1428 enum pp_token_type needtype;
1430 origline = tline;
1432 switch (i) {
1433 case PPC_IFCTX:
1434 j = false; /* have we matched yet? */
1435 while (cstk && tline) {
1436 skip_white_(tline);
1437 if (!tline || tline->type != TOK_ID) {
1438 error(ERR_NONFATAL,
1439 "`%s' expects context identifiers", pp_directives[ct]);
1440 free_tlist(origline);
1441 return -1;
1443 if (!nasm_stricmp(tline->text, cstk->name))
1444 j = true;
1445 tline = tline->next;
1447 break;
1449 case PPC_IFDEF:
1450 j = false; /* have we matched yet? */
1451 while (tline) {
1452 skip_white_(tline);
1453 if (!tline || (tline->type != TOK_ID &&
1454 (tline->type != TOK_PREPROC_ID ||
1455 tline->text[1] != '$'))) {
1456 error(ERR_NONFATAL,
1457 "`%s' expects macro identifiers", pp_directives[ct]);
1458 goto fail;
1460 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1461 j = true;
1462 tline = tline->next;
1464 break;
1466 case PPC_IFIDN:
1467 case PPC_IFIDNI:
1468 tline = expand_smacro(tline);
1469 t = tt = tline;
1470 while (tok_isnt_(tt, ","))
1471 tt = tt->next;
1472 if (!tt) {
1473 error(ERR_NONFATAL,
1474 "`%s' expects two comma-separated arguments",
1475 pp_directives[ct]);
1476 goto fail;
1478 tt = tt->next;
1479 j = true; /* assume equality unless proved not */
1480 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1481 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1482 error(ERR_NONFATAL, "`%s': more than one comma on line",
1483 pp_directives[ct]);
1484 goto fail;
1486 if (t->type == TOK_WHITESPACE) {
1487 t = t->next;
1488 continue;
1490 if (tt->type == TOK_WHITESPACE) {
1491 tt = tt->next;
1492 continue;
1494 if (tt->type != t->type) {
1495 j = false; /* found mismatching tokens */
1496 break;
1498 /* Unify surrounding quotes for strings */
1499 if (t->type == TOK_STRING) {
1500 tt->text[0] = t->text[0];
1501 tt->text[strlen(tt->text) - 1] = t->text[0];
1503 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1504 j = false; /* found mismatching tokens */
1505 break;
1508 t = t->next;
1509 tt = tt->next;
1511 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1512 j = false; /* trailing gunk on one end or other */
1513 break;
1515 case PPC_IFMACRO:
1517 bool found = false;
1518 MMacro searching, *mmac;
1520 tline = tline->next;
1521 skip_white_(tline);
1522 tline = expand_id(tline);
1523 if (!tok_type_(tline, TOK_ID)) {
1524 error(ERR_NONFATAL,
1525 "`%s' expects a macro name", pp_directives[ct]);
1526 goto fail;
1528 searching.name = nasm_strdup(tline->text);
1529 searching.casesense = true;
1530 searching.plus = false;
1531 searching.nolist = false;
1532 searching.in_progress = 0;
1533 searching.rep_nest = NULL;
1534 searching.nparam_min = 0;
1535 searching.nparam_max = INT_MAX;
1536 tline = expand_smacro(tline->next);
1537 skip_white_(tline);
1538 if (!tline) {
1539 } else if (!tok_type_(tline, TOK_NUMBER)) {
1540 error(ERR_NONFATAL,
1541 "`%s' expects a parameter count or nothing",
1542 pp_directives[ct]);
1543 } else {
1544 searching.nparam_min = searching.nparam_max =
1545 readnum(tline->text, &j);
1546 if (j)
1547 error(ERR_NONFATAL,
1548 "unable to parse parameter count `%s'",
1549 tline->text);
1551 if (tline && tok_is_(tline->next, "-")) {
1552 tline = tline->next->next;
1553 if (tok_is_(tline, "*"))
1554 searching.nparam_max = INT_MAX;
1555 else if (!tok_type_(tline, TOK_NUMBER))
1556 error(ERR_NONFATAL,
1557 "`%s' expects a parameter count after `-'",
1558 pp_directives[ct]);
1559 else {
1560 searching.nparam_max = readnum(tline->text, &j);
1561 if (j)
1562 error(ERR_NONFATAL,
1563 "unable to parse parameter count `%s'",
1564 tline->text);
1565 if (searching.nparam_min > searching.nparam_max)
1566 error(ERR_NONFATAL,
1567 "minimum parameter count exceeds maximum");
1570 if (tline && tok_is_(tline->next, "+")) {
1571 tline = tline->next;
1572 searching.plus = true;
1574 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1575 while (mmac) {
1576 if (!strcmp(mmac->name, searching.name) &&
1577 (mmac->nparam_min <= searching.nparam_max
1578 || searching.plus)
1579 && (searching.nparam_min <= mmac->nparam_max
1580 || mmac->plus)) {
1581 found = true;
1582 break;
1584 mmac = mmac->next;
1586 nasm_free(searching.name);
1587 j = found;
1588 break;
1591 case PPC_IFID:
1592 needtype = TOK_ID;
1593 goto iftype;
1594 case PPC_IFNUM:
1595 needtype = TOK_NUMBER;
1596 goto iftype;
1597 case PPC_IFSTR:
1598 needtype = TOK_STRING;
1599 goto iftype;
1601 iftype:
1602 t = tline = expand_smacro(tline);
1604 while (tok_type_(t, TOK_WHITESPACE) ||
1605 (needtype == TOK_NUMBER &&
1606 tok_type_(t, TOK_OTHER) &&
1607 (t->text[0] == '-' || t->text[0] == '+') &&
1608 !t->text[1]))
1609 t = t->next;
1611 j = tok_type_(t, needtype);
1612 break;
1614 case PPC_IFTOKEN:
1615 t = tline = expand_smacro(tline);
1616 while (tok_type_(t, TOK_WHITESPACE))
1617 t = t->next;
1619 j = false;
1620 if (t) {
1621 t = t->next; /* Skip the actual token */
1622 while (tok_type_(t, TOK_WHITESPACE))
1623 t = t->next;
1624 j = !t; /* Should be nothing left */
1626 break;
1628 case PPC_IFEMPTY:
1629 t = tline = expand_smacro(tline);
1630 while (tok_type_(t, TOK_WHITESPACE))
1631 t = t->next;
1633 j = !t; /* Should be empty */
1634 break;
1636 case PPC_IF:
1637 t = tline = expand_smacro(tline);
1638 tptr = &t;
1639 tokval.t_type = TOKEN_INVALID;
1640 evalresult = evaluate(ppscan, tptr, &tokval,
1641 NULL, pass | CRITICAL, error, NULL);
1642 if (!evalresult)
1643 return -1;
1644 if (tokval.t_type)
1645 error(ERR_WARNING,
1646 "trailing garbage after expression ignored");
1647 if (!is_simple(evalresult)) {
1648 error(ERR_NONFATAL,
1649 "non-constant value given to `%s'", pp_directives[ct]);
1650 goto fail;
1652 j = reloc_value(evalresult) != 0;
1653 return j;
1655 default:
1656 error(ERR_FATAL,
1657 "preprocessor directive `%s' not yet implemented",
1658 pp_directives[ct]);
1659 goto fail;
1662 free_tlist(origline);
1663 return j ^ PP_NEGATIVE(ct);
1665 fail:
1666 free_tlist(origline);
1667 return -1;
1671 * Expand macros in a string. Used in %error and %include directives.
1672 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1673 * The returned variable should ALWAYS be freed after usage.
1675 void expand_macros_in_string(char **p)
1677 Token *line = tokenize(*p);
1678 line = expand_smacro(line);
1679 *p = detoken(line, false);
1683 * Common code for defining an smacro
1685 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1686 int nparam, Token *expansion)
1688 SMacro *smac, **smhead;
1689 struct hash_table *smtbl;
1691 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1692 if (!smac) {
1693 error(ERR_WARNING,
1694 "single-line macro `%s' defined both with and"
1695 " without parameters", mname);
1697 /* Some instances of the old code considered this a failure,
1698 some others didn't. What is the right thing to do here? */
1699 free_tlist(expansion);
1700 return false; /* Failure */
1701 } else {
1703 * We're redefining, so we have to take over an
1704 * existing SMacro structure. This means freeing
1705 * what was already in it.
1707 nasm_free(smac->name);
1708 free_tlist(smac->expansion);
1710 } else {
1711 smtbl = ctx ? &ctx->localmac : &smacros;
1712 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1713 smac = nasm_malloc(sizeof(SMacro));
1714 smac->next = *smhead;
1715 *smhead = smac;
1717 smac->name = nasm_strdup(mname);
1718 smac->casesense = casesense;
1719 smac->nparam = nparam;
1720 smac->expansion = expansion;
1721 smac->in_progress = false;
1722 return true; /* Success */
1726 * Undefine an smacro
1728 static void undef_smacro(Context *ctx, const char *mname)
1730 SMacro **smhead, *s, **sp;
1731 struct hash_table *smtbl;
1733 smtbl = ctx ? &ctx->localmac : &smacros;
1734 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1736 if (smhead) {
1738 * We now have a macro name... go hunt for it.
1740 sp = smhead;
1741 while ((s = *sp) != NULL) {
1742 if (!mstrcmp(s->name, mname, s->casesense)) {
1743 *sp = s->next;
1744 nasm_free(s->name);
1745 free_tlist(s->expansion);
1746 nasm_free(s);
1747 } else {
1748 sp = &s->next;
1755 * Decode a size directive
1757 static int parse_size(const char *str) {
1758 static const char *size_names[] =
1759 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1760 static const int sizes[] =
1761 { 0, 1, 4, 16, 8, 10, 2, 32 };
1763 return sizes[bsii(str, size_names, elements(size_names))+1];
1767 * find and process preprocessor directive in passed line
1768 * Find out if a line contains a preprocessor directive, and deal
1769 * with it if so.
1771 * If a directive _is_ found, it is the responsibility of this routine
1772 * (and not the caller) to free_tlist() the line.
1774 * @param tline a pointer to the current tokeninzed line linked list
1775 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1778 static int do_directive(Token * tline)
1780 enum preproc_token i;
1781 int j;
1782 bool err;
1783 int nparam;
1784 bool nolist;
1785 bool casesense;
1786 int k, m;
1787 int offset;
1788 char *p, *mname;
1789 Include *inc;
1790 Context *ctx;
1791 Cond *cond;
1792 MMacro *mmac, **mmhead;
1793 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1794 Line *l;
1795 struct tokenval tokval;
1796 expr *evalresult;
1797 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1798 int64_t count;
1800 origline = tline;
1802 skip_white_(tline);
1803 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1804 (tline->text[1] == '%' || tline->text[1] == '$'
1805 || tline->text[1] == '!'))
1806 return NO_DIRECTIVE_FOUND;
1808 i = pp_token_hash(tline->text);
1811 * If we're in a non-emitting branch of a condition construct,
1812 * or walking to the end of an already terminated %rep block,
1813 * we should ignore all directives except for condition
1814 * directives.
1816 if (((istk->conds && !emitting(istk->conds->state)) ||
1817 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1818 return NO_DIRECTIVE_FOUND;
1822 * If we're defining a macro or reading a %rep block, we should
1823 * ignore all directives except for %macro/%imacro (which
1824 * generate an error), %endm/%endmacro, and (only if we're in a
1825 * %rep block) %endrep. If we're in a %rep block, another %rep
1826 * causes an error, so should be let through.
1828 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1829 i != PP_ENDMACRO && i != PP_ENDM &&
1830 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1831 return NO_DIRECTIVE_FOUND;
1834 switch (i) {
1835 case PP_INVALID:
1836 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1837 tline->text);
1838 return NO_DIRECTIVE_FOUND; /* didn't get it */
1840 case PP_STACKSIZE:
1841 /* Directive to tell NASM what the default stack size is. The
1842 * default is for a 16-bit stack, and this can be overriden with
1843 * %stacksize large.
1844 * the following form:
1846 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1848 tline = tline->next;
1849 if (tline && tline->type == TOK_WHITESPACE)
1850 tline = tline->next;
1851 if (!tline || tline->type != TOK_ID) {
1852 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1853 free_tlist(origline);
1854 return DIRECTIVE_FOUND;
1856 if (nasm_stricmp(tline->text, "flat") == 0) {
1857 /* All subsequent ARG directives are for a 32-bit stack */
1858 StackSize = 4;
1859 StackPointer = "ebp";
1860 ArgOffset = 8;
1861 LocalOffset = 0;
1862 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1863 /* All subsequent ARG directives are for a 64-bit stack */
1864 StackSize = 8;
1865 StackPointer = "rbp";
1866 ArgOffset = 8;
1867 LocalOffset = 0;
1868 } else if (nasm_stricmp(tline->text, "large") == 0) {
1869 /* All subsequent ARG directives are for a 16-bit stack,
1870 * far function call.
1872 StackSize = 2;
1873 StackPointer = "bp";
1874 ArgOffset = 4;
1875 LocalOffset = 0;
1876 } else if (nasm_stricmp(tline->text, "small") == 0) {
1877 /* All subsequent ARG directives are for a 16-bit stack,
1878 * far function call. We don't support near functions.
1880 StackSize = 2;
1881 StackPointer = "bp";
1882 ArgOffset = 6;
1883 LocalOffset = 0;
1884 } else {
1885 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1886 free_tlist(origline);
1887 return DIRECTIVE_FOUND;
1889 free_tlist(origline);
1890 return DIRECTIVE_FOUND;
1892 case PP_ARG:
1893 /* TASM like ARG directive to define arguments to functions, in
1894 * the following form:
1896 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1898 offset = ArgOffset;
1899 do {
1900 char *arg, directive[256];
1901 int size = StackSize;
1903 /* Find the argument name */
1904 tline = tline->next;
1905 if (tline && tline->type == TOK_WHITESPACE)
1906 tline = tline->next;
1907 if (!tline || tline->type != TOK_ID) {
1908 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1909 free_tlist(origline);
1910 return DIRECTIVE_FOUND;
1912 arg = tline->text;
1914 /* Find the argument size type */
1915 tline = tline->next;
1916 if (!tline || tline->type != TOK_OTHER
1917 || tline->text[0] != ':') {
1918 error(ERR_NONFATAL,
1919 "Syntax error processing `%%arg' directive");
1920 free_tlist(origline);
1921 return DIRECTIVE_FOUND;
1923 tline = tline->next;
1924 if (!tline || tline->type != TOK_ID) {
1925 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1930 /* Allow macro expansion of type parameter */
1931 tt = tokenize(tline->text);
1932 tt = expand_smacro(tt);
1933 size = parse_size(tt->text);
1934 if (!size) {
1935 error(ERR_NONFATAL,
1936 "Invalid size type for `%%arg' missing directive");
1937 free_tlist(tt);
1938 free_tlist(origline);
1939 return DIRECTIVE_FOUND;
1941 free_tlist(tt);
1943 /* Round up to even stack slots */
1944 size = (size+StackSize-1) & ~(StackSize-1);
1946 /* Now define the macro for the argument */
1947 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1948 arg, StackPointer, offset);
1949 do_directive(tokenize(directive));
1950 offset += size;
1952 /* Move to the next argument in the list */
1953 tline = tline->next;
1954 if (tline && tline->type == TOK_WHITESPACE)
1955 tline = tline->next;
1956 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1957 ArgOffset = offset;
1958 free_tlist(origline);
1959 return DIRECTIVE_FOUND;
1961 case PP_LOCAL:
1962 /* TASM like LOCAL directive to define local variables for a
1963 * function, in the following form:
1965 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1967 * The '= LocalSize' at the end is ignored by NASM, but is
1968 * required by TASM to define the local parameter size (and used
1969 * by the TASM macro package).
1971 offset = LocalOffset;
1972 do {
1973 char *local, directive[256];
1974 int size = StackSize;
1976 /* Find the argument name */
1977 tline = tline->next;
1978 if (tline && tline->type == TOK_WHITESPACE)
1979 tline = tline->next;
1980 if (!tline || tline->type != TOK_ID) {
1981 error(ERR_NONFATAL,
1982 "`%%local' missing argument parameter");
1983 free_tlist(origline);
1984 return DIRECTIVE_FOUND;
1986 local = tline->text;
1988 /* Find the argument size type */
1989 tline = tline->next;
1990 if (!tline || tline->type != TOK_OTHER
1991 || tline->text[0] != ':') {
1992 error(ERR_NONFATAL,
1993 "Syntax error processing `%%local' directive");
1994 free_tlist(origline);
1995 return DIRECTIVE_FOUND;
1997 tline = tline->next;
1998 if (!tline || tline->type != TOK_ID) {
1999 error(ERR_NONFATAL,
2000 "`%%local' missing size type parameter");
2001 free_tlist(origline);
2002 return DIRECTIVE_FOUND;
2005 /* Allow macro expansion of type parameter */
2006 tt = tokenize(tline->text);
2007 tt = expand_smacro(tt);
2008 size = parse_size(tt->text);
2009 if (!size) {
2010 error(ERR_NONFATAL,
2011 "Invalid size type for `%%local' missing directive");
2012 free_tlist(tt);
2013 free_tlist(origline);
2014 return DIRECTIVE_FOUND;
2016 free_tlist(tt);
2018 /* Round up to even stack slots */
2019 size = (size+StackSize-1) & ~(StackSize-1);
2021 offset += size; /* Negative offset, increment before */
2023 /* Now define the macro for the argument */
2024 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2025 local, StackPointer, offset);
2026 do_directive(tokenize(directive));
2028 /* Now define the assign to setup the enter_c macro correctly */
2029 snprintf(directive, sizeof(directive),
2030 "%%assign %%$localsize %%$localsize+%d", size);
2031 do_directive(tokenize(directive));
2033 /* Move to the next argument in the list */
2034 tline = tline->next;
2035 if (tline && tline->type == TOK_WHITESPACE)
2036 tline = tline->next;
2037 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2038 LocalOffset = offset;
2039 free_tlist(origline);
2040 return DIRECTIVE_FOUND;
2042 case PP_CLEAR:
2043 if (tline->next)
2044 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2045 free_macros();
2046 init_macros();
2047 free_tlist(origline);
2048 return DIRECTIVE_FOUND;
2050 case PP_INCLUDE:
2051 tline = tline->next;
2052 skip_white_(tline);
2053 if (!tline || (tline->type != TOK_STRING &&
2054 tline->type != TOK_INTERNAL_STRING)) {
2055 error(ERR_NONFATAL, "`%%include' expects a file name");
2056 free_tlist(origline);
2057 return DIRECTIVE_FOUND; /* but we did _something_ */
2059 if (tline->next)
2060 error(ERR_WARNING,
2061 "trailing garbage after `%%include' ignored");
2062 if (tline->type != TOK_INTERNAL_STRING) {
2063 p = tline->text + 1; /* point past the quote to the name */
2064 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2065 } else
2066 p = tline->text; /* internal_string is easier */
2067 expand_macros_in_string(&p);
2068 inc = nasm_malloc(sizeof(Include));
2069 inc->next = istk;
2070 inc->conds = NULL;
2071 inc->fp = inc_fopen(p);
2072 if (!inc->fp && pass == 0) {
2073 /* -MG given but file not found */
2074 nasm_free(inc);
2075 } else {
2076 inc->fname = src_set_fname(p);
2077 inc->lineno = src_set_linnum(0);
2078 inc->lineinc = 1;
2079 inc->expansion = NULL;
2080 inc->mstk = NULL;
2081 istk = inc;
2082 list->uplevel(LIST_INCLUDE);
2084 free_tlist(origline);
2085 return DIRECTIVE_FOUND;
2087 case PP_PUSH:
2088 tline = tline->next;
2089 skip_white_(tline);
2090 tline = expand_id(tline);
2091 if (!tok_type_(tline, TOK_ID)) {
2092 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2093 free_tlist(origline);
2094 return DIRECTIVE_FOUND; /* but we did _something_ */
2096 if (tline->next)
2097 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2098 ctx = nasm_malloc(sizeof(Context));
2099 ctx->next = cstk;
2100 hash_init(&ctx->localmac, HASH_SMALL);
2101 ctx->name = nasm_strdup(tline->text);
2102 ctx->number = unique++;
2103 cstk = ctx;
2104 free_tlist(origline);
2105 break;
2107 case PP_REPL:
2108 tline = tline->next;
2109 skip_white_(tline);
2110 tline = expand_id(tline);
2111 if (!tok_type_(tline, TOK_ID)) {
2112 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2113 free_tlist(origline);
2114 return DIRECTIVE_FOUND; /* but we did _something_ */
2116 if (tline->next)
2117 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2118 if (!cstk)
2119 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2120 else {
2121 nasm_free(cstk->name);
2122 cstk->name = nasm_strdup(tline->text);
2124 free_tlist(origline);
2125 break;
2127 case PP_POP:
2128 if (tline->next)
2129 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2130 if (!cstk)
2131 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2132 else
2133 ctx_pop();
2134 free_tlist(origline);
2135 break;
2137 case PP_ERROR:
2138 tline->next = expand_smacro(tline->next);
2139 tline = tline->next;
2140 skip_white_(tline);
2141 if (tok_type_(tline, TOK_STRING)) {
2142 p = tline->text + 1; /* point past the quote to the name */
2143 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2144 expand_macros_in_string(&p);
2145 error(ERR_NONFATAL, "%s", p);
2146 nasm_free(p);
2147 } else {
2148 p = detoken(tline, false);
2149 error(ERR_WARNING, "%s", p);
2150 nasm_free(p);
2152 free_tlist(origline);
2153 break;
2155 CASE_PP_IF:
2156 if (istk->conds && !emitting(istk->conds->state))
2157 j = COND_NEVER;
2158 else {
2159 j = if_condition(tline->next, i);
2160 tline->next = NULL; /* it got freed */
2161 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2163 cond = nasm_malloc(sizeof(Cond));
2164 cond->next = istk->conds;
2165 cond->state = j;
2166 istk->conds = cond;
2167 free_tlist(origline);
2168 return DIRECTIVE_FOUND;
2170 CASE_PP_ELIF:
2171 if (!istk->conds)
2172 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2173 if (emitting(istk->conds->state)
2174 || istk->conds->state == COND_NEVER)
2175 istk->conds->state = COND_NEVER;
2176 else {
2178 * IMPORTANT: In the case of %if, we will already have
2179 * called expand_mmac_params(); however, if we're
2180 * processing an %elif we must have been in a
2181 * non-emitting mode, which would have inhibited
2182 * the normal invocation of expand_mmac_params(). Therefore,
2183 * we have to do it explicitly here.
2185 j = if_condition(expand_mmac_params(tline->next), i);
2186 tline->next = NULL; /* it got freed */
2187 istk->conds->state =
2188 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2190 free_tlist(origline);
2191 return DIRECTIVE_FOUND;
2193 case PP_ELSE:
2194 if (tline->next)
2195 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2196 if (!istk->conds)
2197 error(ERR_FATAL, "`%%else': no matching `%%if'");
2198 if (emitting(istk->conds->state)
2199 || istk->conds->state == COND_NEVER)
2200 istk->conds->state = COND_ELSE_FALSE;
2201 else
2202 istk->conds->state = COND_ELSE_TRUE;
2203 free_tlist(origline);
2204 return DIRECTIVE_FOUND;
2206 case PP_ENDIF:
2207 if (tline->next)
2208 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2209 if (!istk->conds)
2210 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2211 cond = istk->conds;
2212 istk->conds = cond->next;
2213 nasm_free(cond);
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
2217 case PP_MACRO:
2218 case PP_IMACRO:
2219 if (defining)
2220 error(ERR_FATAL,
2221 "`%%%smacro': already defining a macro",
2222 (i == PP_IMACRO ? "i" : ""));
2223 tline = tline->next;
2224 skip_white_(tline);
2225 tline = expand_id(tline);
2226 if (!tok_type_(tline, TOK_ID)) {
2227 error(ERR_NONFATAL,
2228 "`%%%smacro' expects a macro name",
2229 (i == PP_IMACRO ? "i" : ""));
2230 return DIRECTIVE_FOUND;
2232 defining = nasm_malloc(sizeof(MMacro));
2233 defining->name = nasm_strdup(tline->text);
2234 defining->casesense = (i == PP_MACRO);
2235 defining->plus = false;
2236 defining->nolist = false;
2237 defining->in_progress = 0;
2238 defining->rep_nest = NULL;
2239 tline = expand_smacro(tline->next);
2240 skip_white_(tline);
2241 if (!tok_type_(tline, TOK_NUMBER)) {
2242 error(ERR_NONFATAL,
2243 "`%%%smacro' expects a parameter count",
2244 (i == PP_IMACRO ? "i" : ""));
2245 defining->nparam_min = defining->nparam_max = 0;
2246 } else {
2247 defining->nparam_min = defining->nparam_max =
2248 readnum(tline->text, &err);
2249 if (err)
2250 error(ERR_NONFATAL,
2251 "unable to parse parameter count `%s'", tline->text);
2253 if (tline && tok_is_(tline->next, "-")) {
2254 tline = tline->next->next;
2255 if (tok_is_(tline, "*"))
2256 defining->nparam_max = INT_MAX;
2257 else if (!tok_type_(tline, TOK_NUMBER))
2258 error(ERR_NONFATAL,
2259 "`%%%smacro' expects a parameter count after `-'",
2260 (i == PP_IMACRO ? "i" : ""));
2261 else {
2262 defining->nparam_max = readnum(tline->text, &err);
2263 if (err)
2264 error(ERR_NONFATAL,
2265 "unable to parse parameter count `%s'",
2266 tline->text);
2267 if (defining->nparam_min > defining->nparam_max)
2268 error(ERR_NONFATAL,
2269 "minimum parameter count exceeds maximum");
2272 if (tline && tok_is_(tline->next, "+")) {
2273 tline = tline->next;
2274 defining->plus = true;
2276 if (tline && tok_type_(tline->next, TOK_ID) &&
2277 !nasm_stricmp(tline->next->text, ".nolist")) {
2278 tline = tline->next;
2279 defining->nolist = true;
2281 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2282 while (mmac) {
2283 if (!strcmp(mmac->name, defining->name) &&
2284 (mmac->nparam_min <= defining->nparam_max
2285 || defining->plus)
2286 && (defining->nparam_min <= mmac->nparam_max
2287 || mmac->plus)) {
2288 error(ERR_WARNING,
2289 "redefining multi-line macro `%s'", defining->name);
2290 break;
2292 mmac = mmac->next;
2295 * Handle default parameters.
2297 if (tline && tline->next) {
2298 defining->dlist = tline->next;
2299 tline->next = NULL;
2300 count_mmac_params(defining->dlist, &defining->ndefs,
2301 &defining->defaults);
2302 } else {
2303 defining->dlist = NULL;
2304 defining->defaults = NULL;
2306 defining->expansion = NULL;
2307 free_tlist(origline);
2308 return DIRECTIVE_FOUND;
2310 case PP_ENDM:
2311 case PP_ENDMACRO:
2312 if (!defining) {
2313 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2314 return DIRECTIVE_FOUND;
2316 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2317 defining->next = *mmhead;
2318 *mmhead = defining;
2319 defining = NULL;
2320 free_tlist(origline);
2321 return DIRECTIVE_FOUND;
2323 case PP_ROTATE:
2324 if (tline->next && tline->next->type == TOK_WHITESPACE)
2325 tline = tline->next;
2326 if (tline->next == NULL) {
2327 free_tlist(origline);
2328 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2329 return DIRECTIVE_FOUND;
2331 t = expand_smacro(tline->next);
2332 tline->next = NULL;
2333 free_tlist(origline);
2334 tline = t;
2335 tptr = &t;
2336 tokval.t_type = TOKEN_INVALID;
2337 evalresult =
2338 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2339 free_tlist(tline);
2340 if (!evalresult)
2341 return DIRECTIVE_FOUND;
2342 if (tokval.t_type)
2343 error(ERR_WARNING,
2344 "trailing garbage after expression ignored");
2345 if (!is_simple(evalresult)) {
2346 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2347 return DIRECTIVE_FOUND;
2349 mmac = istk->mstk;
2350 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2351 mmac = mmac->next_active;
2352 if (!mmac) {
2353 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2354 } else if (mmac->nparam == 0) {
2355 error(ERR_NONFATAL,
2356 "`%%rotate' invoked within macro without parameters");
2357 } else {
2358 int rotate = mmac->rotate + reloc_value(evalresult);
2360 rotate %= (int)mmac->nparam;
2361 if (rotate < 0)
2362 rotate += mmac->nparam;
2364 mmac->rotate = rotate;
2366 return DIRECTIVE_FOUND;
2368 case PP_REP:
2369 nolist = false;
2370 do {
2371 tline = tline->next;
2372 } while (tok_type_(tline, TOK_WHITESPACE));
2374 if (tok_type_(tline, TOK_ID) &&
2375 nasm_stricmp(tline->text, ".nolist") == 0) {
2376 nolist = true;
2377 do {
2378 tline = tline->next;
2379 } while (tok_type_(tline, TOK_WHITESPACE));
2382 if (tline) {
2383 t = expand_smacro(tline);
2384 tptr = &t;
2385 tokval.t_type = TOKEN_INVALID;
2386 evalresult =
2387 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2388 if (!evalresult) {
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2392 if (tokval.t_type)
2393 error(ERR_WARNING,
2394 "trailing garbage after expression ignored");
2395 if (!is_simple(evalresult)) {
2396 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2397 return DIRECTIVE_FOUND;
2399 count = reloc_value(evalresult) + 1;
2400 } else {
2401 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2402 count = 0;
2404 free_tlist(origline);
2406 tmp_defining = defining;
2407 defining = nasm_malloc(sizeof(MMacro));
2408 defining->name = NULL; /* flags this macro as a %rep block */
2409 defining->casesense = false;
2410 defining->plus = false;
2411 defining->nolist = nolist;
2412 defining->in_progress = count;
2413 defining->nparam_min = defining->nparam_max = 0;
2414 defining->defaults = NULL;
2415 defining->dlist = NULL;
2416 defining->expansion = NULL;
2417 defining->next_active = istk->mstk;
2418 defining->rep_nest = tmp_defining;
2419 return DIRECTIVE_FOUND;
2421 case PP_ENDREP:
2422 if (!defining || defining->name) {
2423 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2424 return DIRECTIVE_FOUND;
2428 * Now we have a "macro" defined - although it has no name
2429 * and we won't be entering it in the hash tables - we must
2430 * push a macro-end marker for it on to istk->expansion.
2431 * After that, it will take care of propagating itself (a
2432 * macro-end marker line for a macro which is really a %rep
2433 * block will cause the macro to be re-expanded, complete
2434 * with another macro-end marker to ensure the process
2435 * continues) until the whole expansion is forcibly removed
2436 * from istk->expansion by a %exitrep.
2438 l = nasm_malloc(sizeof(Line));
2439 l->next = istk->expansion;
2440 l->finishes = defining;
2441 l->first = NULL;
2442 istk->expansion = l;
2444 istk->mstk = defining;
2446 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2447 tmp_defining = defining;
2448 defining = defining->rep_nest;
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2452 case PP_EXITREP:
2454 * We must search along istk->expansion until we hit a
2455 * macro-end marker for a macro with no name. Then we set
2456 * its `in_progress' flag to 0.
2458 for (l = istk->expansion; l; l = l->next)
2459 if (l->finishes && !l->finishes->name)
2460 break;
2462 if (l)
2463 l->finishes->in_progress = 0;
2464 else
2465 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2469 case PP_XDEFINE:
2470 case PP_IXDEFINE:
2471 case PP_DEFINE:
2472 case PP_IDEFINE:
2473 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2475 tline = tline->next;
2476 skip_white_(tline);
2477 tline = expand_id(tline);
2478 if (!tline || (tline->type != TOK_ID &&
2479 (tline->type != TOK_PREPROC_ID ||
2480 tline->text[1] != '$'))) {
2481 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2482 pp_directives[i]);
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2487 ctx = get_ctx(tline->text, false);
2489 mname = tline->text;
2490 last = tline;
2491 param_start = tline = tline->next;
2492 nparam = 0;
2494 /* Expand the macro definition now for %xdefine and %ixdefine */
2495 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2496 tline = expand_smacro(tline);
2498 if (tok_is_(tline, "(")) {
2500 * This macro has parameters.
2503 tline = tline->next;
2504 while (1) {
2505 skip_white_(tline);
2506 if (!tline) {
2507 error(ERR_NONFATAL, "parameter identifier expected");
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
2511 if (tline->type != TOK_ID) {
2512 error(ERR_NONFATAL,
2513 "`%s': parameter identifier expected",
2514 tline->text);
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
2518 tline->type = TOK_SMAC_PARAM + nparam++;
2519 tline = tline->next;
2520 skip_white_(tline);
2521 if (tok_is_(tline, ",")) {
2522 tline = tline->next;
2523 continue;
2525 if (!tok_is_(tline, ")")) {
2526 error(ERR_NONFATAL,
2527 "`)' expected to terminate macro template");
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2531 break;
2533 last = tline;
2534 tline = tline->next;
2536 if (tok_type_(tline, TOK_WHITESPACE))
2537 last = tline, tline = tline->next;
2538 macro_start = NULL;
2539 last->next = NULL;
2540 t = tline;
2541 while (t) {
2542 if (t->type == TOK_ID) {
2543 for (tt = param_start; tt; tt = tt->next)
2544 if (tt->type >= TOK_SMAC_PARAM &&
2545 !strcmp(tt->text, t->text))
2546 t->type = tt->type;
2548 tt = t->next;
2549 t->next = macro_start;
2550 macro_start = t;
2551 t = tt;
2554 * Good. We now have a macro name, a parameter count, and a
2555 * token list (in reverse order) for an expansion. We ought
2556 * to be OK just to create an SMacro, store it, and let
2557 * free_tlist have the rest of the line (which we have
2558 * carefully re-terminated after chopping off the expansion
2559 * from the end).
2561 define_smacro(ctx, mname, casesense, nparam, macro_start);
2562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
2565 case PP_UNDEF:
2566 tline = tline->next;
2567 skip_white_(tline);
2568 tline = expand_id(tline);
2569 if (!tline || (tline->type != TOK_ID &&
2570 (tline->type != TOK_PREPROC_ID ||
2571 tline->text[1] != '$'))) {
2572 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
2576 if (tline->next) {
2577 error(ERR_WARNING,
2578 "trailing garbage after macro name ignored");
2581 /* Find the context that symbol belongs to */
2582 ctx = get_ctx(tline->text, false);
2583 undef_smacro(ctx, tline->text);
2584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
2587 case PP_STRLEN:
2588 casesense = true;
2590 tline = tline->next;
2591 skip_white_(tline);
2592 tline = expand_id(tline);
2593 if (!tline || (tline->type != TOK_ID &&
2594 (tline->type != TOK_PREPROC_ID ||
2595 tline->text[1] != '$'))) {
2596 error(ERR_NONFATAL,
2597 "`%%strlen' expects a macro identifier as first parameter");
2598 free_tlist(origline);
2599 return DIRECTIVE_FOUND;
2601 ctx = get_ctx(tline->text, false);
2603 mname = tline->text;
2604 last = tline;
2605 tline = expand_smacro(tline->next);
2606 last->next = NULL;
2608 t = tline;
2609 while (tok_type_(t, TOK_WHITESPACE))
2610 t = t->next;
2611 /* t should now point to the string */
2612 if (t->type != TOK_STRING) {
2613 error(ERR_NONFATAL,
2614 "`%%strlen` requires string as second parameter");
2615 free_tlist(tline);
2616 free_tlist(origline);
2617 return DIRECTIVE_FOUND;
2620 macro_start = nasm_malloc(sizeof(*macro_start));
2621 macro_start->next = NULL;
2622 make_tok_num(macro_start, strlen(t->text) - 2);
2623 macro_start->mac = NULL;
2626 * We now have a macro name, an implicit parameter count of
2627 * zero, and a numeric token to use as an expansion. Create
2628 * and store an SMacro.
2630 define_smacro(ctx, mname, casesense, 0, macro_start);
2631 free_tlist(tline);
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND;
2635 case PP_SUBSTR:
2636 casesense = true;
2638 tline = tline->next;
2639 skip_white_(tline);
2640 tline = expand_id(tline);
2641 if (!tline || (tline->type != TOK_ID &&
2642 (tline->type != TOK_PREPROC_ID ||
2643 tline->text[1] != '$'))) {
2644 error(ERR_NONFATAL,
2645 "`%%substr' expects a macro identifier as first parameter");
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
2649 ctx = get_ctx(tline->text, false);
2651 mname = tline->text;
2652 last = tline;
2653 tline = expand_smacro(tline->next);
2654 last->next = NULL;
2656 t = tline->next;
2657 while (tok_type_(t, TOK_WHITESPACE))
2658 t = t->next;
2660 /* t should now point to the string */
2661 if (t->type != TOK_STRING) {
2662 error(ERR_NONFATAL,
2663 "`%%substr` requires string as second parameter");
2664 free_tlist(tline);
2665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
2669 tt = t->next;
2670 tptr = &tt;
2671 tokval.t_type = TOKEN_INVALID;
2672 evalresult =
2673 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2674 if (!evalresult) {
2675 free_tlist(tline);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND;
2679 if (!is_simple(evalresult)) {
2680 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2681 free_tlist(tline);
2682 free_tlist(origline);
2683 return DIRECTIVE_FOUND;
2686 macro_start = nasm_malloc(sizeof(*macro_start));
2687 macro_start->next = NULL;
2688 macro_start->text = nasm_strdup("'''");
2689 if (evalresult->value > 0
2690 && evalresult->value < (int) strlen(t->text) - 1) {
2691 macro_start->text[1] = t->text[evalresult->value];
2692 } else {
2693 macro_start->text[2] = '\0';
2695 macro_start->type = TOK_STRING;
2696 macro_start->mac = NULL;
2699 * We now have a macro name, an implicit parameter count of
2700 * zero, and a numeric token to use as an expansion. Create
2701 * and store an SMacro.
2703 define_smacro(ctx, mname, casesense, 0, macro_start);
2704 free_tlist(tline);
2705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
2708 case PP_ASSIGN:
2709 case PP_IASSIGN:
2710 casesense = (i == PP_ASSIGN);
2712 tline = tline->next;
2713 skip_white_(tline);
2714 tline = expand_id(tline);
2715 if (!tline || (tline->type != TOK_ID &&
2716 (tline->type != TOK_PREPROC_ID ||
2717 tline->text[1] != '$'))) {
2718 error(ERR_NONFATAL,
2719 "`%%%sassign' expects a macro identifier",
2720 (i == PP_IASSIGN ? "i" : ""));
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
2724 ctx = get_ctx(tline->text, false);
2726 mname = tline->text;
2727 last = tline;
2728 tline = expand_smacro(tline->next);
2729 last->next = NULL;
2731 t = tline;
2732 tptr = &t;
2733 tokval.t_type = TOKEN_INVALID;
2734 evalresult =
2735 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2736 free_tlist(tline);
2737 if (!evalresult) {
2738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
2742 if (tokval.t_type)
2743 error(ERR_WARNING,
2744 "trailing garbage after expression ignored");
2746 if (!is_simple(evalresult)) {
2747 error(ERR_NONFATAL,
2748 "non-constant value given to `%%%sassign'",
2749 (i == PP_IASSIGN ? "i" : ""));
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
2754 macro_start = nasm_malloc(sizeof(*macro_start));
2755 macro_start->next = NULL;
2756 make_tok_num(macro_start, reloc_value(evalresult));
2757 macro_start->mac = NULL;
2760 * We now have a macro name, an implicit parameter count of
2761 * zero, and a numeric token to use as an expansion. Create
2762 * and store an SMacro.
2764 define_smacro(ctx, mname, casesense, 0, macro_start);
2765 free_tlist(origline);
2766 return DIRECTIVE_FOUND;
2768 case PP_LINE:
2770 * Syntax is `%line nnn[+mmm] [filename]'
2772 tline = tline->next;
2773 skip_white_(tline);
2774 if (!tok_type_(tline, TOK_NUMBER)) {
2775 error(ERR_NONFATAL, "`%%line' expects line number");
2776 free_tlist(origline);
2777 return DIRECTIVE_FOUND;
2779 k = readnum(tline->text, &err);
2780 m = 1;
2781 tline = tline->next;
2782 if (tok_is_(tline, "+")) {
2783 tline = tline->next;
2784 if (!tok_type_(tline, TOK_NUMBER)) {
2785 error(ERR_NONFATAL, "`%%line' expects line increment");
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2789 m = readnum(tline->text, &err);
2790 tline = tline->next;
2792 skip_white_(tline);
2793 src_set_linnum(k);
2794 istk->lineinc = m;
2795 if (tline) {
2796 nasm_free(src_set_fname(detoken(tline, false)));
2798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
2801 default:
2802 error(ERR_FATAL,
2803 "preprocessor directive `%s' not yet implemented",
2804 pp_directives[i]);
2805 break;
2807 return DIRECTIVE_FOUND;
2811 * Ensure that a macro parameter contains a condition code and
2812 * nothing else. Return the condition code index if so, or -1
2813 * otherwise.
2815 static int find_cc(Token * t)
2817 Token *tt;
2818 int i, j, k, m;
2820 if (!t)
2821 return -1; /* Probably a %+ without a space */
2823 skip_white_(t);
2824 if (t->type != TOK_ID)
2825 return -1;
2826 tt = t->next;
2827 skip_white_(tt);
2828 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2829 return -1;
2831 i = -1;
2832 j = elements(conditions);
2833 while (j - i > 1) {
2834 k = (j + i) / 2;
2835 m = nasm_stricmp(t->text, conditions[k]);
2836 if (m == 0) {
2837 i = k;
2838 j = -2;
2839 break;
2840 } else if (m < 0) {
2841 j = k;
2842 } else
2843 i = k;
2845 if (j != -2)
2846 return -1;
2847 return i;
2851 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2852 * %-n) and MMacro-local identifiers (%%foo).
2854 static Token *expand_mmac_params(Token * tline)
2856 Token *t, *tt, **tail, *thead;
2858 tail = &thead;
2859 thead = NULL;
2861 while (tline) {
2862 if (tline->type == TOK_PREPROC_ID &&
2863 (((tline->text[1] == '+' || tline->text[1] == '-')
2864 && tline->text[2]) || tline->text[1] == '%'
2865 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2866 char *text = NULL;
2867 int type = 0, cc; /* type = 0 to placate optimisers */
2868 char tmpbuf[30];
2869 unsigned int n;
2870 int i;
2871 MMacro *mac;
2873 t = tline;
2874 tline = tline->next;
2876 mac = istk->mstk;
2877 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2878 mac = mac->next_active;
2879 if (!mac)
2880 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2881 else
2882 switch (t->text[1]) {
2884 * We have to make a substitution of one of the
2885 * forms %1, %-1, %+1, %%foo, %0.
2887 case '0':
2888 type = TOK_NUMBER;
2889 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2890 text = nasm_strdup(tmpbuf);
2891 break;
2892 case '%':
2893 type = TOK_ID;
2894 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2895 mac->unique);
2896 text = nasm_strcat(tmpbuf, t->text + 2);
2897 break;
2898 case '-':
2899 n = atoi(t->text + 2) - 1;
2900 if (n >= mac->nparam)
2901 tt = NULL;
2902 else {
2903 if (mac->nparam > 1)
2904 n = (n + mac->rotate) % mac->nparam;
2905 tt = mac->params[n];
2907 cc = find_cc(tt);
2908 if (cc == -1) {
2909 error(ERR_NONFATAL,
2910 "macro parameter %d is not a condition code",
2911 n + 1);
2912 text = NULL;
2913 } else {
2914 type = TOK_ID;
2915 if (inverse_ccs[cc] == -1) {
2916 error(ERR_NONFATAL,
2917 "condition code `%s' is not invertible",
2918 conditions[cc]);
2919 text = NULL;
2920 } else
2921 text =
2922 nasm_strdup(conditions[inverse_ccs[cc]]);
2924 break;
2925 case '+':
2926 n = atoi(t->text + 2) - 1;
2927 if (n >= mac->nparam)
2928 tt = NULL;
2929 else {
2930 if (mac->nparam > 1)
2931 n = (n + mac->rotate) % mac->nparam;
2932 tt = mac->params[n];
2934 cc = find_cc(tt);
2935 if (cc == -1) {
2936 error(ERR_NONFATAL,
2937 "macro parameter %d is not a condition code",
2938 n + 1);
2939 text = NULL;
2940 } else {
2941 type = TOK_ID;
2942 text = nasm_strdup(conditions[cc]);
2944 break;
2945 default:
2946 n = atoi(t->text + 1) - 1;
2947 if (n >= mac->nparam)
2948 tt = NULL;
2949 else {
2950 if (mac->nparam > 1)
2951 n = (n + mac->rotate) % mac->nparam;
2952 tt = mac->params[n];
2954 if (tt) {
2955 for (i = 0; i < mac->paramlen[n]; i++) {
2956 *tail = new_Token(NULL, tt->type, tt->text, 0);
2957 tail = &(*tail)->next;
2958 tt = tt->next;
2961 text = NULL; /* we've done it here */
2962 break;
2964 if (!text) {
2965 delete_Token(t);
2966 } else {
2967 *tail = t;
2968 tail = &t->next;
2969 t->type = type;
2970 nasm_free(t->text);
2971 t->text = text;
2972 t->mac = NULL;
2974 continue;
2975 } else {
2976 t = *tail = tline;
2977 tline = tline->next;
2978 t->mac = NULL;
2979 tail = &t->next;
2982 *tail = NULL;
2983 t = thead;
2984 for (; t && (tt = t->next) != NULL; t = t->next)
2985 switch (t->type) {
2986 case TOK_WHITESPACE:
2987 if (tt->type == TOK_WHITESPACE) {
2988 t->next = delete_Token(tt);
2990 break;
2991 case TOK_ID:
2992 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2993 char *tmp = nasm_strcat(t->text, tt->text);
2994 nasm_free(t->text);
2995 t->text = tmp;
2996 t->next = delete_Token(tt);
2998 break;
2999 case TOK_NUMBER:
3000 if (tt->type == TOK_NUMBER) {
3001 char *tmp = nasm_strcat(t->text, tt->text);
3002 nasm_free(t->text);
3003 t->text = tmp;
3004 t->next = delete_Token(tt);
3006 break;
3007 default:
3008 break;
3011 return thead;
3015 * Expand all single-line macro calls made in the given line.
3016 * Return the expanded version of the line. The original is deemed
3017 * to be destroyed in the process. (In reality we'll just move
3018 * Tokens from input to output a lot of the time, rather than
3019 * actually bothering to destroy and replicate.)
3021 #define DEADMAN_LIMIT (1 << 20)
3023 static Token *expand_smacro(Token * tline)
3025 Token *t, *tt, *mstart, **tail, *thead;
3026 struct hash_table *smtbl;
3027 SMacro *head = NULL, *m;
3028 Token **params;
3029 int *paramsize;
3030 unsigned int nparam, sparam;
3031 int brackets, rescan;
3032 Token *org_tline = tline;
3033 Context *ctx;
3034 char *mname;
3035 int deadman = DEADMAN_LIMIT;
3038 * Trick: we should avoid changing the start token pointer since it can
3039 * be contained in "next" field of other token. Because of this
3040 * we allocate a copy of first token and work with it; at the end of
3041 * routine we copy it back
3043 if (org_tline) {
3044 tline =
3045 new_Token(org_tline->next, org_tline->type, org_tline->text,
3047 tline->mac = org_tline->mac;
3048 nasm_free(org_tline->text);
3049 org_tline->text = NULL;
3052 again:
3053 tail = &thead;
3054 thead = NULL;
3056 while (tline) { /* main token loop */
3057 if (!--deadman) {
3058 error(ERR_NONFATAL, "interminable macro recursion");
3059 break;
3062 if ((mname = tline->text)) {
3063 /* if this token is a local macro, look in local context */
3064 ctx = NULL;
3065 smtbl = &smacros;
3066 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3067 ctx = get_ctx(mname, true);
3068 if (ctx)
3069 smtbl = &ctx->localmac;
3071 head = (SMacro *) hash_findix(smtbl, mname);
3074 * We've hit an identifier. As in is_mmacro below, we first
3075 * check whether the identifier is a single-line macro at
3076 * all, then think about checking for parameters if
3077 * necessary.
3079 for (m = head; m; m = m->next)
3080 if (!mstrcmp(m->name, mname, m->casesense))
3081 break;
3082 if (m) {
3083 mstart = tline;
3084 params = NULL;
3085 paramsize = NULL;
3086 if (m->nparam == 0) {
3088 * Simple case: the macro is parameterless. Discard the
3089 * one token that the macro call took, and push the
3090 * expansion back on the to-do stack.
3092 if (!m->expansion) {
3093 if (!strcmp("__FILE__", m->name)) {
3094 int32_t num = 0;
3095 src_get(&num, &(tline->text));
3096 nasm_quote(&(tline->text));
3097 tline->type = TOK_STRING;
3098 continue;
3100 if (!strcmp("__LINE__", m->name)) {
3101 nasm_free(tline->text);
3102 make_tok_num(tline, src_get_linnum());
3103 continue;
3105 if (!strcmp("__BITS__", m->name)) {
3106 nasm_free(tline->text);
3107 make_tok_num(tline, globalbits);
3108 continue;
3110 tline = delete_Token(tline);
3111 continue;
3113 } else {
3115 * Complicated case: at least one macro with this name
3116 * exists and takes parameters. We must find the
3117 * parameters in the call, count them, find the SMacro
3118 * that corresponds to that form of the macro call, and
3119 * substitute for the parameters when we expand. What a
3120 * pain.
3122 /*tline = tline->next;
3123 skip_white_(tline); */
3124 do {
3125 t = tline->next;
3126 while (tok_type_(t, TOK_SMAC_END)) {
3127 t->mac->in_progress = false;
3128 t->text = NULL;
3129 t = tline->next = delete_Token(t);
3131 tline = t;
3132 } while (tok_type_(tline, TOK_WHITESPACE));
3133 if (!tok_is_(tline, "(")) {
3135 * This macro wasn't called with parameters: ignore
3136 * the call. (Behaviour borrowed from gnu cpp.)
3138 tline = mstart;
3139 m = NULL;
3140 } else {
3141 int paren = 0;
3142 int white = 0;
3143 brackets = 0;
3144 nparam = 0;
3145 sparam = PARAM_DELTA;
3146 params = nasm_malloc(sparam * sizeof(Token *));
3147 params[0] = tline->next;
3148 paramsize = nasm_malloc(sparam * sizeof(int));
3149 paramsize[0] = 0;
3150 while (true) { /* parameter loop */
3152 * For some unusual expansions
3153 * which concatenates function call
3155 t = tline->next;
3156 while (tok_type_(t, TOK_SMAC_END)) {
3157 t->mac->in_progress = false;
3158 t->text = NULL;
3159 t = tline->next = delete_Token(t);
3161 tline = t;
3163 if (!tline) {
3164 error(ERR_NONFATAL,
3165 "macro call expects terminating `)'");
3166 break;
3168 if (tline->type == TOK_WHITESPACE
3169 && brackets <= 0) {
3170 if (paramsize[nparam])
3171 white++;
3172 else
3173 params[nparam] = tline->next;
3174 continue; /* parameter loop */
3176 if (tline->type == TOK_OTHER
3177 && tline->text[1] == 0) {
3178 char ch = tline->text[0];
3179 if (ch == ',' && !paren && brackets <= 0) {
3180 if (++nparam >= sparam) {
3181 sparam += PARAM_DELTA;
3182 params = nasm_realloc(params,
3183 sparam *
3184 sizeof(Token
3185 *));
3186 paramsize =
3187 nasm_realloc(paramsize,
3188 sparam *
3189 sizeof(int));
3191 params[nparam] = tline->next;
3192 paramsize[nparam] = 0;
3193 white = 0;
3194 continue; /* parameter loop */
3196 if (ch == '{' &&
3197 (brackets > 0 || (brackets == 0 &&
3198 !paramsize[nparam])))
3200 if (!(brackets++)) {
3201 params[nparam] = tline->next;
3202 continue; /* parameter loop */
3205 if (ch == '}' && brackets > 0)
3206 if (--brackets == 0) {
3207 brackets = -1;
3208 continue; /* parameter loop */
3210 if (ch == '(' && !brackets)
3211 paren++;
3212 if (ch == ')' && brackets <= 0)
3213 if (--paren < 0)
3214 break;
3216 if (brackets < 0) {
3217 brackets = 0;
3218 error(ERR_NONFATAL, "braces do not "
3219 "enclose all of macro parameter");
3221 paramsize[nparam] += white + 1;
3222 white = 0;
3223 } /* parameter loop */
3224 nparam++;
3225 while (m && (m->nparam != nparam ||
3226 mstrcmp(m->name, mname,
3227 m->casesense)))
3228 m = m->next;
3229 if (!m)
3230 error(ERR_WARNING | ERR_WARN_MNP,
3231 "macro `%s' exists, "
3232 "but not taking %d parameters",
3233 mstart->text, nparam);
3236 if (m && m->in_progress)
3237 m = NULL;
3238 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3240 * Design question: should we handle !tline, which
3241 * indicates missing ')' here, or expand those
3242 * macros anyway, which requires the (t) test a few
3243 * lines down?
3245 nasm_free(params);
3246 nasm_free(paramsize);
3247 tline = mstart;
3248 } else {
3250 * Expand the macro: we are placed on the last token of the
3251 * call, so that we can easily split the call from the
3252 * following tokens. We also start by pushing an SMAC_END
3253 * token for the cycle removal.
3255 t = tline;
3256 if (t) {
3257 tline = t->next;
3258 t->next = NULL;
3260 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3261 tt->mac = m;
3262 m->in_progress = true;
3263 tline = tt;
3264 for (t = m->expansion; t; t = t->next) {
3265 if (t->type >= TOK_SMAC_PARAM) {
3266 Token *pcopy = tline, **ptail = &pcopy;
3267 Token *ttt, *pt;
3268 int i;
3270 ttt = params[t->type - TOK_SMAC_PARAM];
3271 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3272 --i >= 0;) {
3273 pt = *ptail =
3274 new_Token(tline, ttt->type, ttt->text,
3276 ptail = &pt->next;
3277 ttt = ttt->next;
3279 tline = pcopy;
3280 } else if (t->type == TOK_PREPROC_Q) {
3281 tt = new_Token(tline, TOK_ID, mname, 0);
3282 tline = tt;
3283 } else if (t->type == TOK_PREPROC_QQ) {
3284 tt = new_Token(tline, TOK_ID, m->name, 0);
3285 tline = tt;
3286 } else {
3287 tt = new_Token(tline, t->type, t->text, 0);
3288 tline = tt;
3293 * Having done that, get rid of the macro call, and clean
3294 * up the parameters.
3296 nasm_free(params);
3297 nasm_free(paramsize);
3298 free_tlist(mstart);
3299 continue; /* main token loop */
3304 if (tline->type == TOK_SMAC_END) {
3305 tline->mac->in_progress = false;
3306 tline = delete_Token(tline);
3307 } else {
3308 t = *tail = tline;
3309 tline = tline->next;
3310 t->mac = NULL;
3311 t->next = NULL;
3312 tail = &t->next;
3317 * Now scan the entire line and look for successive TOK_IDs that resulted
3318 * after expansion (they can't be produced by tokenize()). The successive
3319 * TOK_IDs should be concatenated.
3320 * Also we look for %+ tokens and concatenate the tokens before and after
3321 * them (without white spaces in between).
3323 t = thead;
3324 rescan = 0;
3325 while (t) {
3326 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3327 t = t->next;
3328 if (!t || !t->next)
3329 break;
3330 if (t->next->type == TOK_ID ||
3331 t->next->type == TOK_PREPROC_ID ||
3332 t->next->type == TOK_NUMBER) {
3333 char *p = nasm_strcat(t->text, t->next->text);
3334 nasm_free(t->text);
3335 t->next = delete_Token(t->next);
3336 t->text = p;
3337 rescan = 1;
3338 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3339 t->next->next->type == TOK_PREPROC_ID &&
3340 strcmp(t->next->next->text, "%+") == 0) {
3341 /* free the next whitespace, the %+ token and next whitespace */
3342 int i;
3343 for (i = 1; i <= 3; i++) {
3344 if (!t->next
3345 || (i != 2 && t->next->type != TOK_WHITESPACE))
3346 break;
3347 t->next = delete_Token(t->next);
3348 } /* endfor */
3349 } else
3350 t = t->next;
3352 /* If we concatenaded something, re-scan the line for macros */
3353 if (rescan) {
3354 tline = thead;
3355 goto again;
3358 if (org_tline) {
3359 if (thead) {
3360 *org_tline = *thead;
3361 /* since we just gave text to org_line, don't free it */
3362 thead->text = NULL;
3363 delete_Token(thead);
3364 } else {
3365 /* the expression expanded to empty line;
3366 we can't return NULL for some reasons
3367 we just set the line to a single WHITESPACE token. */
3368 memset(org_tline, 0, sizeof(*org_tline));
3369 org_tline->text = NULL;
3370 org_tline->type = TOK_WHITESPACE;
3372 thead = org_tline;
3375 return thead;
3379 * Similar to expand_smacro but used exclusively with macro identifiers
3380 * right before they are fetched in. The reason is that there can be
3381 * identifiers consisting of several subparts. We consider that if there
3382 * are more than one element forming the name, user wants a expansion,
3383 * otherwise it will be left as-is. Example:
3385 * %define %$abc cde
3387 * the identifier %$abc will be left as-is so that the handler for %define
3388 * will suck it and define the corresponding value. Other case:
3390 * %define _%$abc cde
3392 * In this case user wants name to be expanded *before* %define starts
3393 * working, so we'll expand %$abc into something (if it has a value;
3394 * otherwise it will be left as-is) then concatenate all successive
3395 * PP_IDs into one.
3397 static Token *expand_id(Token * tline)
3399 Token *cur, *oldnext = NULL;
3401 if (!tline || !tline->next)
3402 return tline;
3404 cur = tline;
3405 while (cur->next &&
3406 (cur->next->type == TOK_ID ||
3407 cur->next->type == TOK_PREPROC_ID
3408 || cur->next->type == TOK_NUMBER))
3409 cur = cur->next;
3411 /* If identifier consists of just one token, don't expand */
3412 if (cur == tline)
3413 return tline;
3415 if (cur) {
3416 oldnext = cur->next; /* Detach the tail past identifier */
3417 cur->next = NULL; /* so that expand_smacro stops here */
3420 tline = expand_smacro(tline);
3422 if (cur) {
3423 /* expand_smacro possibly changhed tline; re-scan for EOL */
3424 cur = tline;
3425 while (cur && cur->next)
3426 cur = cur->next;
3427 if (cur)
3428 cur->next = oldnext;
3431 return tline;
3435 * Determine whether the given line constitutes a multi-line macro
3436 * call, and return the MMacro structure called if so. Doesn't have
3437 * to check for an initial label - that's taken care of in
3438 * expand_mmacro - but must check numbers of parameters. Guaranteed
3439 * to be called with tline->type == TOK_ID, so the putative macro
3440 * name is easy to find.
3442 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3444 MMacro *head, *m;
3445 Token **params;
3446 int nparam;
3448 head = (MMacro *) hash_findix(&mmacros, tline->text);
3451 * Efficiency: first we see if any macro exists with the given
3452 * name. If not, we can return NULL immediately. _Then_ we
3453 * count the parameters, and then we look further along the
3454 * list if necessary to find the proper MMacro.
3456 for (m = head; m; m = m->next)
3457 if (!mstrcmp(m->name, tline->text, m->casesense))
3458 break;
3459 if (!m)
3460 return NULL;
3463 * OK, we have a potential macro. Count and demarcate the
3464 * parameters.
3466 count_mmac_params(tline->next, &nparam, &params);
3469 * So we know how many parameters we've got. Find the MMacro
3470 * structure that handles this number.
3472 while (m) {
3473 if (m->nparam_min <= nparam
3474 && (m->plus || nparam <= m->nparam_max)) {
3476 * This one is right. Just check if cycle removal
3477 * prohibits us using it before we actually celebrate...
3479 if (m->in_progress) {
3480 #if 0
3481 error(ERR_NONFATAL,
3482 "self-reference in multi-line macro `%s'", m->name);
3483 #endif
3484 nasm_free(params);
3485 return NULL;
3488 * It's right, and we can use it. Add its default
3489 * parameters to the end of our list if necessary.
3491 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3492 params =
3493 nasm_realloc(params,
3494 ((m->nparam_min + m->ndefs +
3495 1) * sizeof(*params)));
3496 while (nparam < m->nparam_min + m->ndefs) {
3497 params[nparam] = m->defaults[nparam - m->nparam_min];
3498 nparam++;
3502 * If we've gone over the maximum parameter count (and
3503 * we're in Plus mode), ignore parameters beyond
3504 * nparam_max.
3506 if (m->plus && nparam > m->nparam_max)
3507 nparam = m->nparam_max;
3509 * Then terminate the parameter list, and leave.
3511 if (!params) { /* need this special case */
3512 params = nasm_malloc(sizeof(*params));
3513 nparam = 0;
3515 params[nparam] = NULL;
3516 *params_array = params;
3517 return m;
3520 * This one wasn't right: look for the next one with the
3521 * same name.
3523 for (m = m->next; m; m = m->next)
3524 if (!mstrcmp(m->name, tline->text, m->casesense))
3525 break;
3529 * After all that, we didn't find one with the right number of
3530 * parameters. Issue a warning, and fail to expand the macro.
3532 error(ERR_WARNING | ERR_WARN_MNP,
3533 "macro `%s' exists, but not taking %d parameters",
3534 tline->text, nparam);
3535 nasm_free(params);
3536 return NULL;
3540 * Expand the multi-line macro call made by the given line, if
3541 * there is one to be expanded. If there is, push the expansion on
3542 * istk->expansion and return 1. Otherwise return 0.
3544 static int expand_mmacro(Token * tline)
3546 Token *startline = tline;
3547 Token *label = NULL;
3548 int dont_prepend = 0;
3549 Token **params, *t, *mtok, *tt;
3550 MMacro *m;
3551 Line *l, *ll;
3552 int i, nparam, *paramlen;
3554 t = tline;
3555 skip_white_(t);
3556 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3557 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3558 return 0;
3559 mtok = t;
3560 m = is_mmacro(t, &params);
3561 if (!m) {
3562 Token *last;
3564 * We have an id which isn't a macro call. We'll assume
3565 * it might be a label; we'll also check to see if a
3566 * colon follows it. Then, if there's another id after
3567 * that lot, we'll check it again for macro-hood.
3569 label = last = t;
3570 t = t->next;
3571 if (tok_type_(t, TOK_WHITESPACE))
3572 last = t, t = t->next;
3573 if (tok_is_(t, ":")) {
3574 dont_prepend = 1;
3575 last = t, t = t->next;
3576 if (tok_type_(t, TOK_WHITESPACE))
3577 last = t, t = t->next;
3579 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3580 return 0;
3581 last->next = NULL;
3582 tline = t;
3586 * Fix up the parameters: this involves stripping leading and
3587 * trailing whitespace, then stripping braces if they are
3588 * present.
3590 for (nparam = 0; params[nparam]; nparam++) ;
3591 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3593 for (i = 0; params[i]; i++) {
3594 int brace = false;
3595 int comma = (!m->plus || i < nparam - 1);
3597 t = params[i];
3598 skip_white_(t);
3599 if (tok_is_(t, "{"))
3600 t = t->next, brace = true, comma = false;
3601 params[i] = t;
3602 paramlen[i] = 0;
3603 while (t) {
3604 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3605 break; /* ... because we have hit a comma */
3606 if (comma && t->type == TOK_WHITESPACE
3607 && tok_is_(t->next, ","))
3608 break; /* ... or a space then a comma */
3609 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3610 break; /* ... or a brace */
3611 t = t->next;
3612 paramlen[i]++;
3617 * OK, we have a MMacro structure together with a set of
3618 * parameters. We must now go through the expansion and push
3619 * copies of each Line on to istk->expansion. Substitution of
3620 * parameter tokens and macro-local tokens doesn't get done
3621 * until the single-line macro substitution process; this is
3622 * because delaying them allows us to change the semantics
3623 * later through %rotate.
3625 * First, push an end marker on to istk->expansion, mark this
3626 * macro as in progress, and set up its invocation-specific
3627 * variables.
3629 ll = nasm_malloc(sizeof(Line));
3630 ll->next = istk->expansion;
3631 ll->finishes = m;
3632 ll->first = NULL;
3633 istk->expansion = ll;
3635 m->in_progress = true;
3636 m->params = params;
3637 m->iline = tline;
3638 m->nparam = nparam;
3639 m->rotate = 0;
3640 m->paramlen = paramlen;
3641 m->unique = unique++;
3642 m->lineno = 0;
3644 m->next_active = istk->mstk;
3645 istk->mstk = m;
3647 for (l = m->expansion; l; l = l->next) {
3648 Token **tail;
3650 ll = nasm_malloc(sizeof(Line));
3651 ll->finishes = NULL;
3652 ll->next = istk->expansion;
3653 istk->expansion = ll;
3654 tail = &ll->first;
3656 for (t = l->first; t; t = t->next) {
3657 Token *x = t;
3658 switch (t->type) {
3659 case TOK_PREPROC_Q:
3660 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3661 break;
3662 case TOK_PREPROC_QQ:
3663 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3664 break;
3665 case TOK_PREPROC_ID:
3666 if (t->text[1] == '0' && t->text[2] == '0') {
3667 dont_prepend = -1;
3668 x = label;
3669 if (!x)
3670 continue;
3672 /* fall through */
3673 default:
3674 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3675 break;
3677 tail = &tt->next;
3679 *tail = NULL;
3683 * If we had a label, push it on as the first line of
3684 * the macro expansion.
3686 if (label) {
3687 if (dont_prepend < 0)
3688 free_tlist(startline);
3689 else {
3690 ll = nasm_malloc(sizeof(Line));
3691 ll->finishes = NULL;
3692 ll->next = istk->expansion;
3693 istk->expansion = ll;
3694 ll->first = startline;
3695 if (!dont_prepend) {
3696 while (label->next)
3697 label = label->next;
3698 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3703 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3705 return 1;
3709 * Since preprocessor always operate only on the line that didn't
3710 * arrived yet, we should always use ERR_OFFBY1. Also since user
3711 * won't want to see same error twice (preprocessing is done once
3712 * per pass) we will want to show errors only during pass one.
3714 static void error(int severity, const char *fmt, ...)
3716 va_list arg;
3717 char buff[1024];
3719 /* If we're in a dead branch of IF or something like it, ignore the error */
3720 if (istk && istk->conds && !emitting(istk->conds->state))
3721 return;
3723 va_start(arg, fmt);
3724 vsnprintf(buff, sizeof(buff), fmt, arg);
3725 va_end(arg);
3727 if (istk && istk->mstk && istk->mstk->name)
3728 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3729 istk->mstk->lineno, buff);
3730 else
3731 _error(severity | ERR_PASS1, "%s", buff);
3734 static void
3735 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3736 ListGen * listgen)
3738 _error = errfunc;
3739 cstk = NULL;
3740 istk = nasm_malloc(sizeof(Include));
3741 istk->next = NULL;
3742 istk->conds = NULL;
3743 istk->expansion = NULL;
3744 istk->mstk = NULL;
3745 istk->fp = fopen(file, "r");
3746 istk->fname = NULL;
3747 src_set_fname(nasm_strdup(file));
3748 src_set_linnum(0);
3749 istk->lineinc = 1;
3750 if (!istk->fp)
3751 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3752 file);
3753 defining = NULL;
3754 init_macros();
3755 unique = 0;
3756 if (tasm_compatible_mode) {
3757 stdmacpos = nasm_stdmac;
3758 } else {
3759 stdmacpos = nasm_stdmac_after_tasm;
3761 any_extrastdmac = (extrastdmac != NULL);
3762 list = listgen;
3763 evaluate = eval;
3764 pass = apass;
3767 static char *pp_getline(void)
3769 char *line;
3770 Token *tline;
3772 while (1) {
3774 * Fetch a tokenized line, either from the macro-expansion
3775 * buffer or from the input file.
3777 tline = NULL;
3778 while (istk->expansion && istk->expansion->finishes) {
3779 Line *l = istk->expansion;
3780 if (!l->finishes->name && l->finishes->in_progress > 1) {
3781 Line *ll;
3784 * This is a macro-end marker for a macro with no
3785 * name, which means it's not really a macro at all
3786 * but a %rep block, and the `in_progress' field is
3787 * more than 1, meaning that we still need to
3788 * repeat. (1 means the natural last repetition; 0
3789 * means termination by %exitrep.) We have
3790 * therefore expanded up to the %endrep, and must
3791 * push the whole block on to the expansion buffer
3792 * again. We don't bother to remove the macro-end
3793 * marker: we'd only have to generate another one
3794 * if we did.
3796 l->finishes->in_progress--;
3797 for (l = l->finishes->expansion; l; l = l->next) {
3798 Token *t, *tt, **tail;
3800 ll = nasm_malloc(sizeof(Line));
3801 ll->next = istk->expansion;
3802 ll->finishes = NULL;
3803 ll->first = NULL;
3804 tail = &ll->first;
3806 for (t = l->first; t; t = t->next) {
3807 if (t->text || t->type == TOK_WHITESPACE) {
3808 tt = *tail =
3809 new_Token(NULL, t->type, t->text, 0);
3810 tail = &tt->next;
3814 istk->expansion = ll;
3816 } else {
3818 * Check whether a `%rep' was started and not ended
3819 * within this macro expansion. This can happen and
3820 * should be detected. It's a fatal error because
3821 * I'm too confused to work out how to recover
3822 * sensibly from it.
3824 if (defining) {
3825 if (defining->name)
3826 error(ERR_PANIC,
3827 "defining with name in expansion");
3828 else if (istk->mstk->name)
3829 error(ERR_FATAL,
3830 "`%%rep' without `%%endrep' within"
3831 " expansion of macro `%s'",
3832 istk->mstk->name);
3836 * FIXME: investigate the relationship at this point between
3837 * istk->mstk and l->finishes
3840 MMacro *m = istk->mstk;
3841 istk->mstk = m->next_active;
3842 if (m->name) {
3844 * This was a real macro call, not a %rep, and
3845 * therefore the parameter information needs to
3846 * be freed.
3848 nasm_free(m->params);
3849 free_tlist(m->iline);
3850 nasm_free(m->paramlen);
3851 l->finishes->in_progress = false;
3852 } else
3853 free_mmacro(m);
3855 istk->expansion = l->next;
3856 nasm_free(l);
3857 list->downlevel(LIST_MACRO);
3860 while (1) { /* until we get a line we can use */
3862 if (istk->expansion) { /* from a macro expansion */
3863 char *p;
3864 Line *l = istk->expansion;
3865 if (istk->mstk)
3866 istk->mstk->lineno++;
3867 tline = l->first;
3868 istk->expansion = l->next;
3869 nasm_free(l);
3870 p = detoken(tline, false);
3871 list->line(LIST_MACRO, p);
3872 nasm_free(p);
3873 break;
3875 line = read_line();
3876 if (line) { /* from the current input file */
3877 line = prepreproc(line);
3878 tline = tokenize(line);
3879 nasm_free(line);
3880 break;
3883 * The current file has ended; work down the istk
3886 Include *i = istk;
3887 fclose(i->fp);
3888 if (i->conds)
3889 error(ERR_FATAL,
3890 "expected `%%endif' before end of file");
3891 /* only set line and file name if there's a next node */
3892 if (i->next) {
3893 src_set_linnum(i->lineno);
3894 nasm_free(src_set_fname(i->fname));
3896 istk = i->next;
3897 list->downlevel(LIST_INCLUDE);
3898 nasm_free(i);
3899 if (!istk)
3900 return NULL;
3905 * We must expand MMacro parameters and MMacro-local labels
3906 * _before_ we plunge into directive processing, to cope
3907 * with things like `%define something %1' such as STRUC
3908 * uses. Unless we're _defining_ a MMacro, in which case
3909 * those tokens should be left alone to go into the
3910 * definition; and unless we're in a non-emitting
3911 * condition, in which case we don't want to meddle with
3912 * anything.
3914 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3915 tline = expand_mmac_params(tline);
3918 * Check the line to see if it's a preprocessor directive.
3920 if (do_directive(tline) == DIRECTIVE_FOUND) {
3921 continue;
3922 } else if (defining) {
3924 * We're defining a multi-line macro. We emit nothing
3925 * at all, and just
3926 * shove the tokenized line on to the macro definition.
3928 Line *l = nasm_malloc(sizeof(Line));
3929 l->next = defining->expansion;
3930 l->first = tline;
3931 l->finishes = false;
3932 defining->expansion = l;
3933 continue;
3934 } else if (istk->conds && !emitting(istk->conds->state)) {
3936 * We're in a non-emitting branch of a condition block.
3937 * Emit nothing at all, not even a blank line: when we
3938 * emerge from the condition we'll give a line-number
3939 * directive so we keep our place correctly.
3941 free_tlist(tline);
3942 continue;
3943 } else if (istk->mstk && !istk->mstk->in_progress) {
3945 * We're in a %rep block which has been terminated, so
3946 * we're walking through to the %endrep without
3947 * emitting anything. Emit nothing at all, not even a
3948 * blank line: when we emerge from the %rep block we'll
3949 * give a line-number directive so we keep our place
3950 * correctly.
3952 free_tlist(tline);
3953 continue;
3954 } else {
3955 tline = expand_smacro(tline);
3956 if (!expand_mmacro(tline)) {
3958 * De-tokenize the line again, and emit it.
3960 line = detoken(tline, true);
3961 free_tlist(tline);
3962 break;
3963 } else {
3964 continue; /* expand_mmacro calls free_tlist */
3969 return line;
3972 static void pp_cleanup(int pass)
3974 if (defining) {
3975 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3976 defining->name);
3977 free_mmacro(defining);
3979 while (cstk)
3980 ctx_pop();
3981 free_macros();
3982 while (istk) {
3983 Include *i = istk;
3984 istk = istk->next;
3985 fclose(i->fp);
3986 nasm_free(i->fname);
3987 nasm_free(i);
3989 while (cstk)
3990 ctx_pop();
3991 if (pass == 0) {
3992 free_llist(predef);
3993 delete_Blocks();
3997 void pp_include_path(char *path)
3999 IncPath *i;
4001 i = nasm_malloc(sizeof(IncPath));
4002 i->path = path ? nasm_strdup(path) : NULL;
4003 i->next = NULL;
4005 if (ipath != NULL) {
4006 IncPath *j = ipath;
4007 while (j->next != NULL)
4008 j = j->next;
4009 j->next = i;
4010 } else {
4011 ipath = i;
4016 * added by alexfru:
4018 * This function is used to "export" the include paths, e.g.
4019 * the paths specified in the '-I' command switch.
4020 * The need for such exporting is due to the 'incbin' directive,
4021 * which includes raw binary files (unlike '%include', which
4022 * includes text source files). It would be real nice to be
4023 * able to specify paths to search for incbin'ned files also.
4024 * So, this is a simple workaround.
4026 * The function use is simple:
4028 * The 1st call (with NULL argument) returns a pointer to the 1st path
4029 * (char** type) or NULL if none include paths available.
4031 * All subsequent calls take as argument the value returned by this
4032 * function last. The return value is either the next path
4033 * (char** type) or NULL if the end of the paths list is reached.
4035 * It is maybe not the best way to do things, but I didn't want
4036 * to export too much, just one or two functions and no types or
4037 * variables exported.
4039 * Can't say I like the current situation with e.g. this path list either,
4040 * it seems to be never deallocated after creation...
4042 char **pp_get_include_path_ptr(char **pPrevPath)
4044 /* This macro returns offset of a member of a structure */
4045 #define GetMemberOffset(StructType,MemberName)\
4046 ((size_t)&((StructType*)0)->MemberName)
4047 IncPath *i;
4049 if (pPrevPath == NULL) {
4050 if (ipath != NULL)
4051 return &ipath->path;
4052 else
4053 return NULL;
4055 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4056 i = i->next;
4057 if (i != NULL)
4058 return &i->path;
4059 else
4060 return NULL;
4061 #undef GetMemberOffset
4064 void pp_pre_include(char *fname)
4066 Token *inc, *space, *name;
4067 Line *l;
4069 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4070 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4071 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4073 l = nasm_malloc(sizeof(Line));
4074 l->next = predef;
4075 l->first = inc;
4076 l->finishes = false;
4077 predef = l;
4080 void pp_pre_define(char *definition)
4082 Token *def, *space;
4083 Line *l;
4084 char *equals;
4086 equals = strchr(definition, '=');
4087 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4088 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4089 if (equals)
4090 *equals = ' ';
4091 space->next = tokenize(definition);
4092 if (equals)
4093 *equals = '=';
4095 l = nasm_malloc(sizeof(Line));
4096 l->next = predef;
4097 l->first = def;
4098 l->finishes = false;
4099 predef = l;
4102 void pp_pre_undefine(char *definition)
4104 Token *def, *space;
4105 Line *l;
4107 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4108 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4109 space->next = tokenize(definition);
4111 l = nasm_malloc(sizeof(Line));
4112 l->next = predef;
4113 l->first = def;
4114 l->finishes = false;
4115 predef = l;
4119 * Added by Keith Kanios:
4121 * This function is used to assist with "runtime" preprocessor
4122 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4124 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4125 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4128 void pp_runtime(char *definition)
4130 Token *def;
4132 def = tokenize(definition);
4133 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4134 free_tlist(def);
4138 void pp_extra_stdmac(const char **macros)
4140 extrastdmac = macros;
4143 static void make_tok_num(Token * tok, int64_t val)
4145 char numbuf[20];
4146 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4147 tok->text = nasm_strdup(numbuf);
4148 tok->type = TOK_NUMBER;
4151 Preproc nasmpp = {
4152 pp_reset,
4153 pp_getline,
4154 pp_cleanup