The AVX R,X,B, and V fields (but not W) are inverted.
[nasm/autotest.git] / preproc.c
blobf3ef2729d2aed373b1fad9ecd20a671531ed8bc1
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"
54 typedef struct SMacro SMacro;
55 typedef struct MMacro MMacro;
56 typedef struct Context Context;
57 typedef struct Token Token;
58 typedef struct Blocks Blocks;
59 typedef struct Line Line;
60 typedef struct Include Include;
61 typedef struct Cond Cond;
62 typedef struct IncPath IncPath;
65 * Note on the storage of both SMacro and MMacros: the hash table
66 * indexes them case-insensitively, and we then have to go through a
67 * linked list of potential case aliases (and, for MMacros, parameter
68 * ranges); this is to preserve the matching semantics of the earlier
69 * code. If the number of case aliases for a specific macro is a
70 * performance issue, you may want to reconsider your coding style.
74 * Store the definition of a single-line macro.
76 struct SMacro {
77 SMacro *next;
78 char *name;
79 bool casesense;
80 bool in_progress;
81 unsigned int nparam;
82 Token *expansion;
86 * Store the definition of a multi-line macro. This is also used to
87 * store the interiors of `%rep...%endrep' blocks, which are
88 * effectively self-re-invoking multi-line macros which simply
89 * don't have a name or bother to appear in the hash tables. %rep
90 * blocks are signified by having a NULL `name' field.
92 * In a MMacro describing a `%rep' block, the `in_progress' field
93 * isn't merely boolean, but gives the number of repeats left to
94 * run.
96 * The `next' field is used for storing MMacros in hash tables; the
97 * `next_active' field is for stacking them on istk entries.
99 * When a MMacro is being expanded, `params', `iline', `nparam',
100 * `paramlen', `rotate' and `unique' are local to the invocation.
102 struct MMacro {
103 MMacro *next;
104 char *name;
105 int nparam_min, nparam_max;
106 bool casesense;
107 bool plus; /* is the last parameter greedy? */
108 bool nolist; /* is this macro listing-inhibited? */
109 int64_t in_progress;
110 Token *dlist; /* All defaults as one list */
111 Token **defaults; /* Parameter default pointers */
112 int ndefs; /* number of default parameters */
113 Line *expansion;
115 MMacro *next_active;
116 MMacro *rep_nest; /* used for nesting %rep */
117 Token **params; /* actual parameters */
118 Token *iline; /* invocation line */
119 unsigned int nparam, rotate;
120 int *paramlen;
121 uint64_t unique;
122 int lineno; /* Current line number on expansion */
126 * The context stack is composed of a linked list of these.
128 struct Context {
129 Context *next;
130 SMacro *localmac;
131 char *name;
132 uint32_t number;
136 * This is the internal form which we break input lines up into.
137 * Typically stored in linked lists.
139 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
140 * necessarily used as-is, but is intended to denote the number of
141 * the substituted parameter. So in the definition
143 * %define a(x,y) ( (x) & ~(y) )
145 * the token representing `x' will have its type changed to
146 * TOK_SMAC_PARAM, but the one representing `y' will be
147 * TOK_SMAC_PARAM+1.
149 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
150 * which doesn't need quotes around it. Used in the pre-include
151 * mechanism as an alternative to trying to find a sensible type of
152 * quote to use on the filename we were passed.
154 enum pp_token_type {
155 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
156 TOK_PREPROC_ID, TOK_STRING,
157 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
158 TOK_INTERNAL_STRING
161 struct Token {
162 Token *next;
163 char *text;
164 SMacro *mac; /* associated macro for TOK_SMAC_END */
165 enum pp_token_type type;
169 * Multi-line macro definitions are stored as a linked list of
170 * these, which is essentially a container to allow several linked
171 * lists of Tokens.
173 * Note that in this module, linked lists are treated as stacks
174 * wherever possible. For this reason, Lines are _pushed_ on to the
175 * `expansion' field in MMacro structures, so that the linked list,
176 * if walked, would give the macro lines in reverse order; this
177 * means that we can walk the list when expanding a macro, and thus
178 * push the lines on to the `expansion' field in _istk_ in reverse
179 * order (so that when popped back off they are in the right
180 * order). It may seem cockeyed, and it relies on my design having
181 * an even number of steps in, but it works...
183 * Some of these structures, rather than being actual lines, are
184 * markers delimiting the end of the expansion of a given macro.
185 * This is for use in the cycle-tracking and %rep-handling code.
186 * Such structures have `finishes' non-NULL, and `first' NULL. All
187 * others have `finishes' NULL, but `first' may still be NULL if
188 * the line is blank.
190 struct Line {
191 Line *next;
192 MMacro *finishes;
193 Token *first;
197 * To handle an arbitrary level of file inclusion, we maintain a
198 * stack (ie linked list) of these things.
200 struct Include {
201 Include *next;
202 FILE *fp;
203 Cond *conds;
204 Line *expansion;
205 char *fname;
206 int lineno, lineinc;
207 MMacro *mstk; /* stack of active macros/reps */
211 * Include search path. This is simply a list of strings which get
212 * prepended, in turn, to the name of an include file, in an
213 * attempt to find the file if it's not in the current directory.
215 struct IncPath {
216 IncPath *next;
217 char *path;
221 * Conditional assembly: we maintain a separate stack of these for
222 * each level of file inclusion. (The only reason we keep the
223 * stacks separate is to ensure that a stray `%endif' in a file
224 * included from within the true branch of a `%if' won't terminate
225 * it and cause confusion: instead, rightly, it'll cause an error.)
227 struct Cond {
228 Cond *next;
229 int state;
231 enum {
233 * These states are for use just after %if or %elif: IF_TRUE
234 * means the condition has evaluated to truth so we are
235 * currently emitting, whereas IF_FALSE means we are not
236 * currently emitting but will start doing so if a %else comes
237 * up. In these states, all directives are admissible: %elif,
238 * %else and %endif. (And of course %if.)
240 COND_IF_TRUE, COND_IF_FALSE,
242 * These states come up after a %else: ELSE_TRUE means we're
243 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
244 * any %elif or %else will cause an error.
246 COND_ELSE_TRUE, COND_ELSE_FALSE,
248 * This state means that we're not emitting now, and also that
249 * nothing until %endif will be emitted at all. It's for use in
250 * two circumstances: (i) when we've had our moment of emission
251 * and have now started seeing %elifs, and (ii) when the
252 * condition construct in question is contained within a
253 * non-emitting branch of a larger condition construct.
255 COND_NEVER
257 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
260 * These defines are used as the possible return values for do_directive
262 #define NO_DIRECTIVE_FOUND 0
263 #define DIRECTIVE_FOUND 1
266 * Condition codes. Note that we use c_ prefix not C_ because C_ is
267 * used in nasm.h for the "real" condition codes. At _this_ level,
268 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
269 * ones, so we need a different enum...
271 static const char * const conditions[] = {
272 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
273 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
274 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
276 enum pp_conds {
277 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
278 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
279 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
280 c_none = -1
282 static const enum pp_conds inverse_ccs[] = {
283 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
284 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,
285 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
289 * Directive names.
291 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
292 static int is_condition(enum preproc_token arg)
294 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
297 /* For TASM compatibility we need to be able to recognise TASM compatible
298 * conditional compilation directives. Using the NASM pre-processor does
299 * not work, so we look for them specifically from the following list and
300 * then jam in the equivalent NASM directive into the input stream.
303 enum {
304 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
305 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
308 static const char * const tasm_directives[] = {
309 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
310 "ifndef", "include", "local"
313 static int StackSize = 4;
314 static char *StackPointer = "ebp";
315 static int ArgOffset = 8;
316 static int LocalOffset = 0;
318 static Context *cstk;
319 static Include *istk;
320 static IncPath *ipath = NULL;
322 static efunc _error; /* Pointer to client-provided error reporting function */
323 static evalfunc evaluate;
325 static int pass; /* HACK: pass 0 = generate dependencies only */
327 static uint64_t unique; /* unique identifier numbers */
329 static Line *predef = NULL;
331 static ListGen *list;
334 * The current set of multi-line macros we have defined.
336 static struct hash_table *mmacros;
339 * The current set of single-line macros we have defined.
341 static struct hash_table *smacros;
344 * The multi-line macro we are currently defining, or the %rep
345 * block we are currently reading, if any.
347 static MMacro *defining;
350 * The number of macro parameters to allocate space for at a time.
352 #define PARAM_DELTA 16
355 * The standard macro set: defined as `static char *stdmac[]'. Also
356 * gives our position in the macro set, when we're processing it.
358 #include "macros.c"
359 static const char * const *stdmacpos;
362 * The extra standard macros that come from the object format, if
363 * any.
365 static const char * const *extrastdmac = NULL;
366 bool any_extrastdmac;
369 * Tokens are allocated in blocks to improve speed
371 #define TOKEN_BLOCKSIZE 4096
372 static Token *freeTokens = NULL;
373 struct Blocks {
374 Blocks *next;
375 void *chunk;
378 static Blocks blocks = { NULL, NULL };
381 * Forward declarations.
383 static Token *expand_mmac_params(Token * tline);
384 static Token *expand_smacro(Token * tline);
385 static Token *expand_id(Token * tline);
386 static Context *get_ctx(char *name, bool all_contexts);
387 static void make_tok_num(Token * tok, int64_t val);
388 static void error(int severity, const char *fmt, ...);
389 static void *new_Block(size_t size);
390 static void delete_Blocks(void);
391 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
392 static Token *delete_Token(Token * t);
395 * Macros for safe checking of token pointers, avoid *(NULL)
397 #define tok_type_(x,t) ((x) && (x)->type == (t))
398 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
402 /* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
407 static char *check_tasm_directive(char *line)
409 int32_t i, j, k, m, len;
410 char *p = line, *oldline, oldchar;
412 /* Skip whitespace */
413 while (isspace(*p) && *p != 0)
414 p++;
416 /* Binary search for the directive name */
417 i = -1;
418 j = elements(tasm_directives);
419 len = 0;
420 while (!isspace(p[len]) && p[len] != 0)
421 len++;
422 if (len) {
423 oldchar = p[len];
424 p[len] = 0;
425 while (j - i > 1) {
426 k = (j + i) / 2;
427 m = nasm_stricmp(p, tasm_directives[k]);
428 if (m == 0) {
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
432 p[len] = oldchar;
433 len = strlen(p);
434 oldline = line;
435 line = nasm_malloc(len + 2);
436 line[0] = '%';
437 if (k == TM_IFDIFI) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
441 * package.
443 strcpy(line + 1, "ifdef BOGUS");
444 } else {
445 memcpy(line + 1, p, len + 1);
447 nasm_free(oldline);
448 return line;
449 } else if (m < 0) {
450 j = k;
451 } else
452 i = k;
454 p[len] = oldchar;
456 return line;
460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
463 * lineno file').
465 static char *prepreproc(char *line)
467 int lineno, fnlen;
468 char *fname, *oldline;
470 if (line[0] == '#' && line[1] == ' ') {
471 oldline = line;
472 fname = oldline + 2;
473 lineno = atoi(fname);
474 fname += strspn(fname, "0123456789 ");
475 if (*fname == '"')
476 fname++;
477 fnlen = strcspn(fname, "\"");
478 line = nasm_malloc(20 + fnlen);
479 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
480 nasm_free(oldline);
482 if (tasm_compatible_mode)
483 return check_tasm_directive(line);
484 return line;
488 * Free a linked list of tokens.
490 static void free_tlist(Token * list)
492 while (list) {
493 list = delete_Token(list);
498 * Free a linked list of lines.
500 static void free_llist(Line * list)
502 Line *l;
503 while (list) {
504 l = list;
505 list = list->next;
506 free_tlist(l->first);
507 nasm_free(l);
512 * Free an MMacro
514 static void free_mmacro(MMacro * m)
516 nasm_free(m->name);
517 free_tlist(m->dlist);
518 nasm_free(m->defaults);
519 free_llist(m->expansion);
520 nasm_free(m);
524 * Free all currently defined macros, and free the hash tables
526 static void free_macros(void)
528 struct hash_tbl_node *it;
529 const char *key;
530 SMacro *s;
531 MMacro *m;
533 it = NULL;
534 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
535 nasm_free((void *)key);
536 while (s) {
537 SMacro *ns = s->next;
538 nasm_free(s->name);
539 free_tlist(s->expansion);
540 nasm_free(s);
541 s = ns;
544 hash_free(smacros);
546 it = NULL;
547 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
548 nasm_free((void *)key);
549 while (m) {
550 MMacro *nm = m->next;
551 free_mmacro(m);
552 m = nm;
555 hash_free(mmacros);
559 * Initialize the hash tables
561 static void init_macros(void)
563 smacros = hash_init();
564 mmacros = hash_init();
568 * Pop the context stack.
570 static void ctx_pop(void)
572 Context *c = cstk;
573 SMacro *smac, *s;
575 cstk = cstk->next;
576 smac = c->localmac;
577 while (smac) {
578 s = smac;
579 smac = smac->next;
580 nasm_free(s->name);
581 free_tlist(s->expansion);
582 nasm_free(s);
584 nasm_free(c->name);
585 nasm_free(c);
588 #define BUF_DELTA 512
590 * Read a line from the top file in istk, handling multiple CR/LFs
591 * at the end of the line read, and handling spurious ^Zs. Will
592 * return lines from the standard macro set if this has not already
593 * been done.
595 static char *read_line(void)
597 char *buffer, *p, *q;
598 int bufsize, continued_count;
600 if (stdmacpos) {
601 if (*stdmacpos) {
602 char *ret = nasm_strdup(*stdmacpos++);
603 if (!*stdmacpos && any_extrastdmac) {
604 stdmacpos = extrastdmac;
605 any_extrastdmac = false;
606 return ret;
609 * Nasty hack: here we push the contents of `predef' on
610 * to the top-level expansion stack, since this is the
611 * most convenient way to implement the pre-include and
612 * pre-define features.
614 if (!*stdmacpos) {
615 Line *pd, *l;
616 Token *head, **tail, *t;
618 for (pd = predef; pd; pd = pd->next) {
619 head = NULL;
620 tail = &head;
621 for (t = pd->first; t; t = t->next) {
622 *tail = new_Token(NULL, t->type, t->text, 0);
623 tail = &(*tail)->next;
625 l = nasm_malloc(sizeof(Line));
626 l->next = istk->expansion;
627 l->first = head;
628 l->finishes = false;
629 istk->expansion = l;
632 return ret;
633 } else {
634 stdmacpos = NULL;
638 bufsize = BUF_DELTA;
639 buffer = nasm_malloc(BUF_DELTA);
640 p = buffer;
641 continued_count = 0;
642 while (1) {
643 q = fgets(p, bufsize - (p - buffer), istk->fp);
644 if (!q)
645 break;
646 p += strlen(p);
647 if (p > buffer && p[-1] == '\n') {
648 /* Convert backslash-CRLF line continuation sequences into
649 nothing at all (for DOS and Windows) */
650 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
651 p -= 3;
652 *p = 0;
653 continued_count++;
655 /* Also convert backslash-LF line continuation sequences into
656 nothing at all (for Unix) */
657 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
658 p -= 2;
659 *p = 0;
660 continued_count++;
661 } else {
662 break;
665 if (p - buffer > bufsize - 10) {
666 int32_t offset = p - buffer;
667 bufsize += BUF_DELTA;
668 buffer = nasm_realloc(buffer, bufsize);
669 p = buffer + offset; /* prevent stale-pointer problems */
673 if (!q && p == buffer) {
674 nasm_free(buffer);
675 return NULL;
678 src_set_linnum(src_get_linnum() + istk->lineinc +
679 (continued_count * istk->lineinc));
682 * Play safe: remove CRs as well as LFs, if any of either are
683 * present at the end of the line.
685 while (--p >= buffer && (*p == '\n' || *p == '\r'))
686 *p = '\0';
689 * Handle spurious ^Z, which may be inserted into source files
690 * by some file transfer utilities.
692 buffer[strcspn(buffer, "\032")] = '\0';
694 list->line(LIST_READ, buffer);
696 return buffer;
700 * Tokenize a line of text. This is a very simple process since we
701 * don't need to parse the value out of e.g. numeric tokens: we
702 * simply split one string into many.
704 static Token *tokenize(char *line)
706 char *p = line;
707 enum pp_token_type type;
708 Token *list = NULL;
709 Token *t, **tail = &list;
711 while (*line) {
712 p = line;
713 if (*p == '%') {
714 p++;
715 if (isdigit(*p) ||
716 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
717 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
718 do {
719 p++;
721 while (isdigit(*p));
722 type = TOK_PREPROC_ID;
723 } else if (*p == '{') {
724 p++;
725 while (*p && *p != '}') {
726 p[-1] = *p;
727 p++;
729 p[-1] = '\0';
730 if (*p)
731 p++;
732 type = TOK_PREPROC_ID;
733 } else if (isidchar(*p) ||
734 ((*p == '!' || *p == '%' || *p == '$') &&
735 isidchar(p[1]))) {
736 do {
737 p++;
739 while (isidchar(*p));
740 type = TOK_PREPROC_ID;
741 } else {
742 type = TOK_OTHER;
743 if (*p == '%')
744 p++;
746 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
747 type = TOK_ID;
748 p++;
749 while (*p && isidchar(*p))
750 p++;
751 } else if (*p == '\'' || *p == '"') {
753 * A string token.
755 char c = *p;
756 p++;
757 type = TOK_STRING;
758 while (*p && *p != c)
759 p++;
761 if (*p) {
762 p++;
763 } else {
764 error(ERR_WARNING, "unterminated string");
765 /* Handling unterminated strings by UNV */
766 /* type = -1; */
768 } else if (isnumstart(*p)) {
769 bool is_hex = false;
770 bool is_float = false;
771 bool has_e = false;
772 char c, *r;
775 * A numeric token.
778 if (*p == '$') {
779 p++;
780 is_hex = true;
783 for (;;) {
784 c = *p++;
786 if (!is_hex && (c == 'e' || c == 'E')) {
787 has_e = true;
788 if (*p == '+' || *p == '-') {
789 /* e can only be followed by +/- if it is either a
790 prefixed hex number or a floating-point number */
791 p++;
792 is_float = true;
794 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
795 is_hex = true;
796 } else if (c == 'P' || c == 'p') {
797 is_float = true;
798 if (*p == '+' || *p == '-')
799 p++;
800 } else if (isnumchar(c) || c == '_')
801 ; /* just advance */
802 else if (c == '.') {
803 /* we need to deal with consequences of the legacy
804 parser, like "1.nolist" being two tokens
805 (TOK_NUMBER, TOK_ID) here; at least give it
806 a shot for now. In the future, we probably need
807 a flex-based scanner with proper pattern matching
808 to do it as well as it can be done. Nothing in
809 the world is going to help the person who wants
810 0x123.p16 interpreted as two tokens, though. */
811 r = p;
812 while (*r == '_')
813 r++;
815 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
816 (!is_hex && (*r == 'e' || *r == 'E')) ||
817 (*r == 'p' || *r == 'P')) {
818 p = r;
819 is_float = true;
820 } else
821 break; /* Terminate the token */
822 } else
823 break;
825 p--; /* Point to first character beyond number */
827 if (has_e && !is_hex) {
828 /* 1e13 is floating-point, but 1e13h is not */
829 is_float = true;
832 type = is_float ? TOK_FLOAT : TOK_NUMBER;
833 } else if (isspace(*p)) {
834 type = TOK_WHITESPACE;
835 p++;
836 while (*p && isspace(*p))
837 p++;
839 * Whitespace just before end-of-line is discarded by
840 * pretending it's a comment; whitespace just before a
841 * comment gets lumped into the comment.
843 if (!*p || *p == ';') {
844 type = TOK_COMMENT;
845 while (*p)
846 p++;
848 } else if (*p == ';') {
849 type = TOK_COMMENT;
850 while (*p)
851 p++;
852 } else {
854 * Anything else is an operator of some kind. We check
855 * for all the double-character operators (>>, <<, //,
856 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
857 * else is a single-character operator.
859 type = TOK_OTHER;
860 if ((p[0] == '>' && p[1] == '>') ||
861 (p[0] == '<' && p[1] == '<') ||
862 (p[0] == '/' && p[1] == '/') ||
863 (p[0] == '<' && p[1] == '=') ||
864 (p[0] == '>' && p[1] == '=') ||
865 (p[0] == '=' && p[1] == '=') ||
866 (p[0] == '!' && p[1] == '=') ||
867 (p[0] == '<' && p[1] == '>') ||
868 (p[0] == '&' && p[1] == '&') ||
869 (p[0] == '|' && p[1] == '|') ||
870 (p[0] == '^' && p[1] == '^')) {
871 p++;
873 p++;
876 /* Handling unterminated string by UNV */
877 /*if (type == -1)
879 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
880 t->text[p-line] = *line;
881 tail = &t->next;
883 else */
884 if (type != TOK_COMMENT) {
885 *tail = t = new_Token(NULL, type, line, p - line);
886 tail = &t->next;
888 line = p;
890 return list;
894 * this function allocates a new managed block of memory and
895 * returns a pointer to the block. The managed blocks are
896 * deleted only all at once by the delete_Blocks function.
898 static void *new_Block(size_t size)
900 Blocks *b = &blocks;
902 /* first, get to the end of the linked list */
903 while (b->next)
904 b = b->next;
905 /* now allocate the requested chunk */
906 b->chunk = nasm_malloc(size);
908 /* now allocate a new block for the next request */
909 b->next = nasm_malloc(sizeof(Blocks));
910 /* and initialize the contents of the new block */
911 b->next->next = NULL;
912 b->next->chunk = NULL;
913 return b->chunk;
917 * this function deletes all managed blocks of memory
919 static void delete_Blocks(void)
921 Blocks *a, *b = &blocks;
924 * keep in mind that the first block, pointed to by blocks
925 * is a static and not dynamically allocated, so we don't
926 * free it.
928 while (b) {
929 if (b->chunk)
930 nasm_free(b->chunk);
931 a = b;
932 b = b->next;
933 if (a != &blocks)
934 nasm_free(a);
939 * this function creates a new Token and passes a pointer to it
940 * back to the caller. It sets the type and text elements, and
941 * also the mac and next elements to NULL.
943 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
945 Token *t;
946 int i;
948 if (freeTokens == NULL) {
949 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
950 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
951 freeTokens[i].next = &freeTokens[i + 1];
952 freeTokens[i].next = NULL;
954 t = freeTokens;
955 freeTokens = t->next;
956 t->next = next;
957 t->mac = NULL;
958 t->type = type;
959 if (type == TOK_WHITESPACE || text == NULL) {
960 t->text = NULL;
961 } else {
962 if (txtlen == 0)
963 txtlen = strlen(text);
964 t->text = nasm_malloc(1 + txtlen);
965 strncpy(t->text, text, txtlen);
966 t->text[txtlen] = '\0';
968 return t;
971 static Token *delete_Token(Token * t)
973 Token *next = t->next;
974 nasm_free(t->text);
975 t->next = freeTokens;
976 freeTokens = t;
977 return next;
981 * Convert a line of tokens back into text.
982 * If expand_locals is not zero, identifiers of the form "%$*xxx"
983 * will be transformed into ..@ctxnum.xxx
985 static char *detoken(Token * tlist, int expand_locals)
987 Token *t;
988 int len;
989 char *line, *p;
990 const char *q;
992 len = 0;
993 for (t = tlist; t; t = t->next) {
994 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
995 char *p = getenv(t->text + 2);
996 nasm_free(t->text);
997 if (p)
998 t->text = nasm_strdup(p);
999 else
1000 t->text = NULL;
1002 /* Expand local macros here and not during preprocessing */
1003 if (expand_locals &&
1004 t->type == TOK_PREPROC_ID && t->text &&
1005 t->text[0] == '%' && t->text[1] == '$') {
1006 Context *ctx = get_ctx(t->text, false);
1007 if (ctx) {
1008 char buffer[40];
1009 char *p, *q = t->text + 2;
1011 q += strspn(q, "$");
1012 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1013 p = nasm_strcat(buffer, q);
1014 nasm_free(t->text);
1015 t->text = p;
1018 if (t->type == TOK_WHITESPACE) {
1019 len++;
1020 } else if (t->text) {
1021 len += strlen(t->text);
1024 p = line = nasm_malloc(len + 1);
1025 for (t = tlist; t; t = t->next) {
1026 if (t->type == TOK_WHITESPACE) {
1027 *p++ = ' ';
1028 } else if (t->text) {
1029 q = t->text;
1030 while (*q)
1031 *p++ = *q++;
1034 *p = '\0';
1035 return line;
1039 * A scanner, suitable for use by the expression evaluator, which
1040 * operates on a line of Tokens. Expects a pointer to a pointer to
1041 * the first token in the line to be passed in as its private_data
1042 * field.
1044 * FIX: This really needs to be unified with stdscan.
1046 static int ppscan(void *private_data, struct tokenval *tokval)
1048 Token **tlineptr = private_data;
1049 Token *tline;
1050 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1052 do {
1053 tline = *tlineptr;
1054 *tlineptr = tline ? tline->next : NULL;
1056 while (tline && (tline->type == TOK_WHITESPACE ||
1057 tline->type == TOK_COMMENT));
1059 if (!tline)
1060 return tokval->t_type = TOKEN_EOS;
1062 tokval->t_charptr = tline->text;
1064 if (tline->text[0] == '$' && !tline->text[1])
1065 return tokval->t_type = TOKEN_HERE;
1066 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1067 return tokval->t_type = TOKEN_BASE;
1069 if (tline->type == TOK_ID) {
1070 p = tokval->t_charptr = tline->text;
1071 if (p[0] == '$') {
1072 tokval->t_charptr++;
1073 return tokval->t_type = TOKEN_ID;
1076 for (r = p, s = ourcopy; *r; r++) {
1077 if (r > p+MAX_KEYWORD)
1078 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1079 *s++ = tolower(*r);
1081 *s = '\0';
1082 /* right, so we have an identifier sitting in temp storage. now,
1083 * is it actually a register or instruction name, or what? */
1084 return nasm_token_hash(ourcopy, tokval);
1087 if (tline->type == TOK_NUMBER) {
1088 bool rn_error;
1089 tokval->t_integer = readnum(tline->text, &rn_error);
1090 if (rn_error)
1091 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1092 tokval->t_charptr = tline->text;
1093 return tokval->t_type = TOKEN_NUM;
1096 if (tline->type == TOK_FLOAT) {
1097 return tokval->t_type = TOKEN_FLOAT;
1100 if (tline->type == TOK_STRING) {
1101 bool rn_warn;
1102 char q, *r;
1103 int l;
1105 r = tline->text;
1106 q = *r++;
1107 l = strlen(r);
1109 if (l == 0 || r[l - 1] != q)
1110 return tokval->t_type = TOKEN_ERRNUM;
1111 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1112 if (rn_warn)
1113 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1114 tokval->t_charptr = NULL;
1115 return tokval->t_type = TOKEN_NUM;
1118 if (tline->type == TOK_OTHER) {
1119 if (!strcmp(tline->text, "<<"))
1120 return tokval->t_type = TOKEN_SHL;
1121 if (!strcmp(tline->text, ">>"))
1122 return tokval->t_type = TOKEN_SHR;
1123 if (!strcmp(tline->text, "//"))
1124 return tokval->t_type = TOKEN_SDIV;
1125 if (!strcmp(tline->text, "%%"))
1126 return tokval->t_type = TOKEN_SMOD;
1127 if (!strcmp(tline->text, "=="))
1128 return tokval->t_type = TOKEN_EQ;
1129 if (!strcmp(tline->text, "<>"))
1130 return tokval->t_type = TOKEN_NE;
1131 if (!strcmp(tline->text, "!="))
1132 return tokval->t_type = TOKEN_NE;
1133 if (!strcmp(tline->text, "<="))
1134 return tokval->t_type = TOKEN_LE;
1135 if (!strcmp(tline->text, ">="))
1136 return tokval->t_type = TOKEN_GE;
1137 if (!strcmp(tline->text, "&&"))
1138 return tokval->t_type = TOKEN_DBL_AND;
1139 if (!strcmp(tline->text, "^^"))
1140 return tokval->t_type = TOKEN_DBL_XOR;
1141 if (!strcmp(tline->text, "||"))
1142 return tokval->t_type = TOKEN_DBL_OR;
1146 * We have no other options: just return the first character of
1147 * the token text.
1149 return tokval->t_type = tline->text[0];
1153 * Compare a string to the name of an existing macro; this is a
1154 * simple wrapper which calls either strcmp or nasm_stricmp
1155 * depending on the value of the `casesense' parameter.
1157 static int mstrcmp(const char *p, const char *q, bool casesense)
1159 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1163 * Return the Context structure associated with a %$ token. Return
1164 * NULL, having _already_ reported an error condition, if the
1165 * context stack isn't deep enough for the supplied number of $
1166 * signs.
1167 * If all_contexts == true, contexts that enclose current are
1168 * also scanned for such smacro, until it is found; if not -
1169 * only the context that directly results from the number of $'s
1170 * in variable's name.
1172 static Context *get_ctx(char *name, bool all_contexts)
1174 Context *ctx;
1175 SMacro *m;
1176 int i;
1178 if (!name || name[0] != '%' || name[1] != '$')
1179 return NULL;
1181 if (!cstk) {
1182 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1183 return NULL;
1186 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1187 ctx = ctx->next;
1188 /* i--; Lino - 02/25/02 */
1190 if (!ctx) {
1191 error(ERR_NONFATAL, "`%s': context stack is only"
1192 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1193 return NULL;
1195 if (!all_contexts)
1196 return ctx;
1198 do {
1199 /* Search for this smacro in found context */
1200 m = ctx->localmac;
1201 while (m) {
1202 if (!mstrcmp(m->name, name, m->casesense))
1203 return ctx;
1204 m = m->next;
1206 ctx = ctx->next;
1208 while (ctx);
1209 return NULL;
1213 * Open an include file. This routine must always return a valid
1214 * file pointer if it returns - it's responsible for throwing an
1215 * ERR_FATAL and bombing out completely if not. It should also try
1216 * the include path one by one until it finds the file or reaches
1217 * the end of the path.
1219 static FILE *inc_fopen(char *file)
1221 FILE *fp;
1222 char *prefix = "", *combine;
1223 IncPath *ip = ipath;
1224 static int namelen = 0;
1225 int len = strlen(file);
1227 while (1) {
1228 combine = nasm_malloc(strlen(prefix) + len + 1);
1229 strcpy(combine, prefix);
1230 strcat(combine, file);
1231 fp = fopen(combine, "r");
1232 if (pass == 0 && fp) {
1233 namelen += strlen(combine) + 1;
1234 if (namelen > 62) {
1235 printf(" \\\n ");
1236 namelen = 2;
1238 printf(" %s", combine);
1240 nasm_free(combine);
1241 if (fp)
1242 return fp;
1243 if (!ip)
1244 break;
1245 prefix = ip->path;
1246 ip = ip->next;
1248 if (!prefix) {
1249 /* -MG given and file not found */
1250 if (pass == 0) {
1251 namelen += strlen(file) + 1;
1252 if (namelen > 62) {
1253 printf(" \\\n ");
1254 namelen = 2;
1256 printf(" %s", file);
1258 return NULL;
1262 error(ERR_FATAL, "unable to open include file `%s'", file);
1263 return NULL; /* never reached - placate compilers */
1267 * Search for a key in the hash index; adding it if necessary
1268 * (in which case we initialize the data pointer to NULL.)
1270 static void **
1271 hash_findi_add(struct hash_table *hash, const char *str)
1273 struct hash_insert hi;
1274 void **r;
1275 char *strx;
1277 r = hash_findi(hash, str, &hi);
1278 if (r)
1279 return r;
1281 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1282 return hash_add(&hi, strx, NULL);
1286 * Like hash_findi, but returns the data element rather than a pointer
1287 * to it. Used only when not adding a new element, hence no third
1288 * argument.
1290 static void *
1291 hash_findix(struct hash_table *hash, const char *str)
1293 void **p;
1295 p = hash_findi(hash, str, NULL);
1296 return p ? *p : NULL;
1300 * Determine if we should warn on defining a single-line macro of
1301 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1302 * return true if _any_ single-line macro of that name is defined.
1303 * Otherwise, will return true if a single-line macro with either
1304 * `nparam' or no parameters is defined.
1306 * If a macro with precisely the right number of parameters is
1307 * defined, or nparam is -1, the address of the definition structure
1308 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1309 * is NULL, no action will be taken regarding its contents, and no
1310 * error will occur.
1312 * Note that this is also called with nparam zero to resolve
1313 * `ifdef'.
1315 * If you already know which context macro belongs to, you can pass
1316 * the context pointer as first parameter; if you won't but name begins
1317 * with %$ the context will be automatically computed. If all_contexts
1318 * is true, macro will be searched in outer contexts as well.
1320 static bool
1321 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1322 bool nocase)
1324 SMacro *m;
1326 if (ctx) {
1327 m = ctx->localmac;
1328 } else if (name[0] == '%' && name[1] == '$') {
1329 if (cstk)
1330 ctx = get_ctx(name, false);
1331 if (!ctx)
1332 return false; /* got to return _something_ */
1333 m = ctx->localmac;
1334 } else {
1335 m = (SMacro *) hash_findix(smacros, name);
1338 while (m) {
1339 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1340 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1341 if (defn) {
1342 if (nparam == (int) m->nparam || nparam == -1)
1343 *defn = m;
1344 else
1345 *defn = NULL;
1347 return true;
1349 m = m->next;
1352 return false;
1356 * Count and mark off the parameters in a multi-line macro call.
1357 * This is called both from within the multi-line macro expansion
1358 * code, and also to mark off the default parameters when provided
1359 * in a %macro definition line.
1361 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1363 int paramsize, brace;
1365 *nparam = paramsize = 0;
1366 *params = NULL;
1367 while (t) {
1368 if (*nparam >= paramsize) {
1369 paramsize += PARAM_DELTA;
1370 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1372 skip_white_(t);
1373 brace = false;
1374 if (tok_is_(t, "{"))
1375 brace = true;
1376 (*params)[(*nparam)++] = t;
1377 while (tok_isnt_(t, brace ? "}" : ","))
1378 t = t->next;
1379 if (t) { /* got a comma/brace */
1380 t = t->next;
1381 if (brace) {
1383 * Now we've found the closing brace, look further
1384 * for the comma.
1386 skip_white_(t);
1387 if (tok_isnt_(t, ",")) {
1388 error(ERR_NONFATAL,
1389 "braces do not enclose all of macro parameter");
1390 while (tok_isnt_(t, ","))
1391 t = t->next;
1393 if (t)
1394 t = t->next; /* eat the comma */
1401 * Determine whether one of the various `if' conditions is true or
1402 * not.
1404 * We must free the tline we get passed.
1406 static bool if_condition(Token * tline, enum preproc_token ct)
1408 enum pp_conditional i = PP_COND(ct);
1409 bool j;
1410 Token *t, *tt, **tptr, *origline;
1411 struct tokenval tokval;
1412 expr *evalresult;
1413 enum pp_token_type needtype;
1415 origline = tline;
1417 switch (i) {
1418 case PPC_IFCTX:
1419 j = false; /* have we matched yet? */
1420 while (cstk && tline) {
1421 skip_white_(tline);
1422 if (!tline || tline->type != TOK_ID) {
1423 error(ERR_NONFATAL,
1424 "`%s' expects context identifiers", pp_directives[ct]);
1425 free_tlist(origline);
1426 return -1;
1428 if (!nasm_stricmp(tline->text, cstk->name))
1429 j = true;
1430 tline = tline->next;
1432 break;
1434 case PPC_IFDEF:
1435 j = false; /* have we matched yet? */
1436 while (tline) {
1437 skip_white_(tline);
1438 if (!tline || (tline->type != TOK_ID &&
1439 (tline->type != TOK_PREPROC_ID ||
1440 tline->text[1] != '$'))) {
1441 error(ERR_NONFATAL,
1442 "`%s' expects macro identifiers", pp_directives[ct]);
1443 goto fail;
1445 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1446 j = true;
1447 tline = tline->next;
1449 break;
1451 case PPC_IFIDN:
1452 case PPC_IFIDNI:
1453 tline = expand_smacro(tline);
1454 t = tt = tline;
1455 while (tok_isnt_(tt, ","))
1456 tt = tt->next;
1457 if (!tt) {
1458 error(ERR_NONFATAL,
1459 "`%s' expects two comma-separated arguments",
1460 pp_directives[ct]);
1461 goto fail;
1463 tt = tt->next;
1464 j = true; /* assume equality unless proved not */
1465 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1466 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1467 error(ERR_NONFATAL, "`%s': more than one comma on line",
1468 pp_directives[ct]);
1469 goto fail;
1471 if (t->type == TOK_WHITESPACE) {
1472 t = t->next;
1473 continue;
1475 if (tt->type == TOK_WHITESPACE) {
1476 tt = tt->next;
1477 continue;
1479 if (tt->type != t->type) {
1480 j = false; /* found mismatching tokens */
1481 break;
1483 /* Unify surrounding quotes for strings */
1484 if (t->type == TOK_STRING) {
1485 tt->text[0] = t->text[0];
1486 tt->text[strlen(tt->text) - 1] = t->text[0];
1488 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1489 j = false; /* found mismatching tokens */
1490 break;
1493 t = t->next;
1494 tt = tt->next;
1496 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1497 j = false; /* trailing gunk on one end or other */
1498 break;
1500 case PPC_IFMACRO:
1502 bool found = false;
1503 MMacro searching, *mmac;
1505 tline = tline->next;
1506 skip_white_(tline);
1507 tline = expand_id(tline);
1508 if (!tok_type_(tline, TOK_ID)) {
1509 error(ERR_NONFATAL,
1510 "`%s' expects a macro name", pp_directives[ct]);
1511 goto fail;
1513 searching.name = nasm_strdup(tline->text);
1514 searching.casesense = true;
1515 searching.plus = false;
1516 searching.nolist = false;
1517 searching.in_progress = 0;
1518 searching.rep_nest = NULL;
1519 searching.nparam_min = 0;
1520 searching.nparam_max = INT_MAX;
1521 tline = expand_smacro(tline->next);
1522 skip_white_(tline);
1523 if (!tline) {
1524 } else if (!tok_type_(tline, TOK_NUMBER)) {
1525 error(ERR_NONFATAL,
1526 "`%s' expects a parameter count or nothing",
1527 pp_directives[ct]);
1528 } else {
1529 searching.nparam_min = searching.nparam_max =
1530 readnum(tline->text, &j);
1531 if (j)
1532 error(ERR_NONFATAL,
1533 "unable to parse parameter count `%s'",
1534 tline->text);
1536 if (tline && tok_is_(tline->next, "-")) {
1537 tline = tline->next->next;
1538 if (tok_is_(tline, "*"))
1539 searching.nparam_max = INT_MAX;
1540 else if (!tok_type_(tline, TOK_NUMBER))
1541 error(ERR_NONFATAL,
1542 "`%s' expects a parameter count after `-'",
1543 pp_directives[ct]);
1544 else {
1545 searching.nparam_max = readnum(tline->text, &j);
1546 if (j)
1547 error(ERR_NONFATAL,
1548 "unable to parse parameter count `%s'",
1549 tline->text);
1550 if (searching.nparam_min > searching.nparam_max)
1551 error(ERR_NONFATAL,
1552 "minimum parameter count exceeds maximum");
1555 if (tline && tok_is_(tline->next, "+")) {
1556 tline = tline->next;
1557 searching.plus = true;
1559 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1560 while (mmac) {
1561 if (!strcmp(mmac->name, searching.name) &&
1562 (mmac->nparam_min <= searching.nparam_max
1563 || searching.plus)
1564 && (searching.nparam_min <= mmac->nparam_max
1565 || mmac->plus)) {
1566 found = true;
1567 break;
1569 mmac = mmac->next;
1571 nasm_free(searching.name);
1572 j = found;
1573 break;
1576 case PPC_IFID:
1577 needtype = TOK_ID;
1578 goto iftype;
1579 case PPC_IFNUM:
1580 needtype = TOK_NUMBER;
1581 goto iftype;
1582 case PPC_IFSTR:
1583 needtype = TOK_STRING;
1584 goto iftype;
1586 iftype:
1587 t = tline = expand_smacro(tline);
1589 while (tok_type_(t, TOK_WHITESPACE) ||
1590 (needtype == TOK_NUMBER &&
1591 tok_type_(t, TOK_OTHER) &&
1592 (t->text[0] == '-' || t->text[0] == '+') &&
1593 !t->text[1]))
1594 t = t->next;
1596 j = tok_type_(t, needtype);
1597 break;
1599 case PPC_IFTOKEN:
1600 t = tline = expand_smacro(tline);
1601 while (tok_type_(t, TOK_WHITESPACE))
1602 t = t->next;
1604 j = false;
1605 if (t) {
1606 t = t->next; /* Skip the actual token */
1607 while (tok_type_(t, TOK_WHITESPACE))
1608 t = t->next;
1609 j = !t; /* Should be nothing left */
1611 break;
1613 case PPC_IFEMPTY:
1614 t = tline = expand_smacro(tline);
1615 while (tok_type_(t, TOK_WHITESPACE))
1616 t = t->next;
1618 j = !t; /* Should be empty */
1619 break;
1621 case PPC_IF:
1622 t = tline = expand_smacro(tline);
1623 tptr = &t;
1624 tokval.t_type = TOKEN_INVALID;
1625 evalresult = evaluate(ppscan, tptr, &tokval,
1626 NULL, pass | CRITICAL, error, NULL);
1627 if (!evalresult)
1628 return -1;
1629 if (tokval.t_type)
1630 error(ERR_WARNING,
1631 "trailing garbage after expression ignored");
1632 if (!is_simple(evalresult)) {
1633 error(ERR_NONFATAL,
1634 "non-constant value given to `%s'", pp_directives[ct]);
1635 goto fail;
1637 j = reloc_value(evalresult) != 0;
1638 return j;
1640 default:
1641 error(ERR_FATAL,
1642 "preprocessor directive `%s' not yet implemented",
1643 pp_directives[ct]);
1644 goto fail;
1647 free_tlist(origline);
1648 return j ^ PP_NEGATIVE(ct);
1650 fail:
1651 free_tlist(origline);
1652 return -1;
1656 * Expand macros in a string. Used in %error and %include directives.
1657 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1658 * The returned variable should ALWAYS be freed after usage.
1660 void expand_macros_in_string(char **p)
1662 Token *line = tokenize(*p);
1663 line = expand_smacro(line);
1664 *p = detoken(line, false);
1668 * Common code for defining an smacro
1670 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1671 int nparam, Token *expansion)
1673 SMacro *smac, **smhead;
1675 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1676 if (!smac) {
1677 error(ERR_WARNING,
1678 "single-line macro `%s' defined both with and"
1679 " without parameters", mname);
1681 /* Some instances of the old code considered this a failure,
1682 some others didn't. What is the right thing to do here? */
1683 free_tlist(expansion);
1684 return false; /* Failure */
1685 } else {
1687 * We're redefining, so we have to take over an
1688 * existing SMacro structure. This means freeing
1689 * what was already in it.
1691 nasm_free(smac->name);
1692 free_tlist(smac->expansion);
1694 } else {
1695 if (!ctx)
1696 smhead = (SMacro **) hash_findi_add(smacros, mname);
1697 else
1698 smhead = &ctx->localmac;
1700 smac = nasm_malloc(sizeof(SMacro));
1701 smac->next = *smhead;
1702 *smhead = smac;
1704 smac->name = nasm_strdup(mname);
1705 smac->casesense = casesense;
1706 smac->nparam = nparam;
1707 smac->expansion = expansion;
1708 smac->in_progress = false;
1709 return true; /* Success */
1713 * Undefine an smacro
1715 static void undef_smacro(Context *ctx, const char *mname)
1717 SMacro **smhead, *s, **sp;
1719 if (!ctx)
1720 smhead = (SMacro **) hash_findi(smacros, mname, NULL);
1721 else
1722 smhead = &ctx->localmac;
1724 if (smhead) {
1726 * We now have a macro name... go hunt for it.
1728 sp = smhead;
1729 while ((s = *sp) != NULL) {
1730 if (!mstrcmp(s->name, mname, s->casesense)) {
1731 *sp = s->next;
1732 nasm_free(s->name);
1733 free_tlist(s->expansion);
1734 nasm_free(s);
1735 } else {
1736 sp = &s->next;
1743 * Decode a size directive
1745 static int parse_size(const char *str) {
1746 static const char *size_names[] =
1747 { "byte", "dword", "oword", "qword", "tword", "word" };
1748 static const int sizes[] =
1749 { 0, 1, 4, 16, 8, 10, 2 };
1751 return sizes[bsii(str, size_names, elements(size_names))+1];
1755 * find and process preprocessor directive in passed line
1756 * Find out if a line contains a preprocessor directive, and deal
1757 * with it if so.
1759 * If a directive _is_ found, it is the responsibility of this routine
1760 * (and not the caller) to free_tlist() the line.
1762 * @param tline a pointer to the current tokeninzed line linked list
1763 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1766 static int do_directive(Token * tline)
1768 enum preproc_token i;
1769 int j;
1770 bool err;
1771 int nparam;
1772 bool nolist;
1773 bool casesense;
1774 int k, m;
1775 int offset;
1776 char *p, *mname;
1777 Include *inc;
1778 Context *ctx;
1779 Cond *cond;
1780 MMacro *mmac, **mmhead;
1781 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1782 Line *l;
1783 struct tokenval tokval;
1784 expr *evalresult;
1785 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1786 int64_t count;
1788 origline = tline;
1790 skip_white_(tline);
1791 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1792 (tline->text[1] == '%' || tline->text[1] == '$'
1793 || tline->text[1] == '!'))
1794 return NO_DIRECTIVE_FOUND;
1796 i = pp_token_hash(tline->text);
1799 * If we're in a non-emitting branch of a condition construct,
1800 * or walking to the end of an already terminated %rep block,
1801 * we should ignore all directives except for condition
1802 * directives.
1804 if (((istk->conds && !emitting(istk->conds->state)) ||
1805 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1806 return NO_DIRECTIVE_FOUND;
1810 * If we're defining a macro or reading a %rep block, we should
1811 * ignore all directives except for %macro/%imacro (which
1812 * generate an error), %endm/%endmacro, and (only if we're in a
1813 * %rep block) %endrep. If we're in a %rep block, another %rep
1814 * causes an error, so should be let through.
1816 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1817 i != PP_ENDMACRO && i != PP_ENDM &&
1818 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1819 return NO_DIRECTIVE_FOUND;
1822 switch (i) {
1823 case PP_INVALID:
1824 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1825 tline->text);
1826 return NO_DIRECTIVE_FOUND; /* didn't get it */
1828 case PP_STACKSIZE:
1829 /* Directive to tell NASM what the default stack size is. The
1830 * default is for a 16-bit stack, and this can be overriden with
1831 * %stacksize large.
1832 * the following form:
1834 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1836 tline = tline->next;
1837 if (tline && tline->type == TOK_WHITESPACE)
1838 tline = tline->next;
1839 if (!tline || tline->type != TOK_ID) {
1840 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1841 free_tlist(origline);
1842 return DIRECTIVE_FOUND;
1844 if (nasm_stricmp(tline->text, "flat") == 0) {
1845 /* All subsequent ARG directives are for a 32-bit stack */
1846 StackSize = 4;
1847 StackPointer = "ebp";
1848 ArgOffset = 8;
1849 LocalOffset = 0;
1850 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1851 /* All subsequent ARG directives are for a 64-bit stack */
1852 StackSize = 8;
1853 StackPointer = "rbp";
1854 ArgOffset = 8;
1855 LocalOffset = 0;
1856 } else if (nasm_stricmp(tline->text, "large") == 0) {
1857 /* All subsequent ARG directives are for a 16-bit stack,
1858 * far function call.
1860 StackSize = 2;
1861 StackPointer = "bp";
1862 ArgOffset = 4;
1863 LocalOffset = 0;
1864 } else if (nasm_stricmp(tline->text, "small") == 0) {
1865 /* All subsequent ARG directives are for a 16-bit stack,
1866 * far function call. We don't support near functions.
1868 StackSize = 2;
1869 StackPointer = "bp";
1870 ArgOffset = 6;
1871 LocalOffset = 0;
1872 } else {
1873 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1874 free_tlist(origline);
1875 return DIRECTIVE_FOUND;
1877 free_tlist(origline);
1878 return DIRECTIVE_FOUND;
1880 case PP_ARG:
1881 /* TASM like ARG directive to define arguments to functions, in
1882 * the following form:
1884 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1886 offset = ArgOffset;
1887 do {
1888 char *arg, directive[256];
1889 int size = StackSize;
1891 /* Find the argument name */
1892 tline = tline->next;
1893 if (tline && tline->type == TOK_WHITESPACE)
1894 tline = tline->next;
1895 if (!tline || tline->type != TOK_ID) {
1896 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1897 free_tlist(origline);
1898 return DIRECTIVE_FOUND;
1900 arg = tline->text;
1902 /* Find the argument size type */
1903 tline = tline->next;
1904 if (!tline || tline->type != TOK_OTHER
1905 || tline->text[0] != ':') {
1906 error(ERR_NONFATAL,
1907 "Syntax error processing `%%arg' directive");
1908 free_tlist(origline);
1909 return DIRECTIVE_FOUND;
1911 tline = tline->next;
1912 if (!tline || tline->type != TOK_ID) {
1913 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1914 free_tlist(origline);
1915 return DIRECTIVE_FOUND;
1918 /* Allow macro expansion of type parameter */
1919 tt = tokenize(tline->text);
1920 tt = expand_smacro(tt);
1921 size = parse_size(tt->text);
1922 if (!size) {
1923 error(ERR_NONFATAL,
1924 "Invalid size type for `%%arg' missing directive");
1925 free_tlist(tt);
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1929 free_tlist(tt);
1931 /* Round up to even stack slots */
1932 size = (size+StackSize-1) & ~(StackSize-1);
1934 /* Now define the macro for the argument */
1935 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1936 arg, StackPointer, offset);
1937 do_directive(tokenize(directive));
1938 offset += size;
1940 /* Move to the next argument in the list */
1941 tline = tline->next;
1942 if (tline && tline->type == TOK_WHITESPACE)
1943 tline = tline->next;
1944 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1945 ArgOffset = offset;
1946 free_tlist(origline);
1947 return DIRECTIVE_FOUND;
1949 case PP_LOCAL:
1950 /* TASM like LOCAL directive to define local variables for a
1951 * function, in the following form:
1953 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1955 * The '= LocalSize' at the end is ignored by NASM, but is
1956 * required by TASM to define the local parameter size (and used
1957 * by the TASM macro package).
1959 offset = LocalOffset;
1960 do {
1961 char *local, directive[256];
1962 int size = StackSize;
1964 /* Find the argument name */
1965 tline = tline->next;
1966 if (tline && tline->type == TOK_WHITESPACE)
1967 tline = tline->next;
1968 if (!tline || tline->type != TOK_ID) {
1969 error(ERR_NONFATAL,
1970 "`%%local' missing argument parameter");
1971 free_tlist(origline);
1972 return DIRECTIVE_FOUND;
1974 local = tline->text;
1976 /* Find the argument size type */
1977 tline = tline->next;
1978 if (!tline || tline->type != TOK_OTHER
1979 || tline->text[0] != ':') {
1980 error(ERR_NONFATAL,
1981 "Syntax error processing `%%local' directive");
1982 free_tlist(origline);
1983 return DIRECTIVE_FOUND;
1985 tline = tline->next;
1986 if (!tline || tline->type != TOK_ID) {
1987 error(ERR_NONFATAL,
1988 "`%%local' missing size type parameter");
1989 free_tlist(origline);
1990 return DIRECTIVE_FOUND;
1993 /* Allow macro expansion of type parameter */
1994 tt = tokenize(tline->text);
1995 tt = expand_smacro(tt);
1996 size = parse_size(tt->text);
1997 if (!size) {
1998 error(ERR_NONFATAL,
1999 "Invalid size type for `%%local' missing directive");
2000 free_tlist(tt);
2001 free_tlist(origline);
2002 return DIRECTIVE_FOUND;
2004 free_tlist(tt);
2006 /* Round up to even stack slots */
2007 size = (size+StackSize-1) & ~(StackSize-1);
2009 offset += size; /* Negative offset, increment before */
2011 /* Now define the macro for the argument */
2012 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2013 local, StackPointer, offset);
2014 do_directive(tokenize(directive));
2016 /* Now define the assign to setup the enter_c macro correctly */
2017 snprintf(directive, sizeof(directive),
2018 "%%assign %%$localsize %%$localsize+%d", size);
2019 do_directive(tokenize(directive));
2021 /* Move to the next argument in the list */
2022 tline = tline->next;
2023 if (tline && tline->type == TOK_WHITESPACE)
2024 tline = tline->next;
2025 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2026 LocalOffset = offset;
2027 free_tlist(origline);
2028 return DIRECTIVE_FOUND;
2030 case PP_CLEAR:
2031 if (tline->next)
2032 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2033 free_macros();
2034 init_macros();
2035 free_tlist(origline);
2036 return DIRECTIVE_FOUND;
2038 case PP_INCLUDE:
2039 tline = tline->next;
2040 skip_white_(tline);
2041 if (!tline || (tline->type != TOK_STRING &&
2042 tline->type != TOK_INTERNAL_STRING)) {
2043 error(ERR_NONFATAL, "`%%include' expects a file name");
2044 free_tlist(origline);
2045 return DIRECTIVE_FOUND; /* but we did _something_ */
2047 if (tline->next)
2048 error(ERR_WARNING,
2049 "trailing garbage after `%%include' ignored");
2050 if (tline->type != TOK_INTERNAL_STRING) {
2051 p = tline->text + 1; /* point past the quote to the name */
2052 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2053 } else
2054 p = tline->text; /* internal_string is easier */
2055 expand_macros_in_string(&p);
2056 inc = nasm_malloc(sizeof(Include));
2057 inc->next = istk;
2058 inc->conds = NULL;
2059 inc->fp = inc_fopen(p);
2060 if (!inc->fp && pass == 0) {
2061 /* -MG given but file not found */
2062 nasm_free(inc);
2063 } else {
2064 inc->fname = src_set_fname(p);
2065 inc->lineno = src_set_linnum(0);
2066 inc->lineinc = 1;
2067 inc->expansion = NULL;
2068 inc->mstk = NULL;
2069 istk = inc;
2070 list->uplevel(LIST_INCLUDE);
2072 free_tlist(origline);
2073 return DIRECTIVE_FOUND;
2075 case PP_PUSH:
2076 tline = tline->next;
2077 skip_white_(tline);
2078 tline = expand_id(tline);
2079 if (!tok_type_(tline, TOK_ID)) {
2080 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2081 free_tlist(origline);
2082 return DIRECTIVE_FOUND; /* but we did _something_ */
2084 if (tline->next)
2085 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2086 ctx = nasm_malloc(sizeof(Context));
2087 ctx->next = cstk;
2088 ctx->localmac = NULL;
2089 ctx->name = nasm_strdup(tline->text);
2090 ctx->number = unique++;
2091 cstk = ctx;
2092 free_tlist(origline);
2093 break;
2095 case PP_REPL:
2096 tline = tline->next;
2097 skip_white_(tline);
2098 tline = expand_id(tline);
2099 if (!tok_type_(tline, TOK_ID)) {
2100 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2101 free_tlist(origline);
2102 return DIRECTIVE_FOUND; /* but we did _something_ */
2104 if (tline->next)
2105 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2106 if (!cstk)
2107 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2108 else {
2109 nasm_free(cstk->name);
2110 cstk->name = nasm_strdup(tline->text);
2112 free_tlist(origline);
2113 break;
2115 case PP_POP:
2116 if (tline->next)
2117 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2118 if (!cstk)
2119 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2120 else
2121 ctx_pop();
2122 free_tlist(origline);
2123 break;
2125 case PP_ERROR:
2126 tline->next = expand_smacro(tline->next);
2127 tline = tline->next;
2128 skip_white_(tline);
2129 if (tok_type_(tline, TOK_STRING)) {
2130 p = tline->text + 1; /* point past the quote to the name */
2131 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2132 expand_macros_in_string(&p);
2133 error(ERR_NONFATAL, "%s", p);
2134 nasm_free(p);
2135 } else {
2136 p = detoken(tline, false);
2137 error(ERR_WARNING, "%s", p);
2138 nasm_free(p);
2140 free_tlist(origline);
2141 break;
2143 CASE_PP_IF:
2144 if (istk->conds && !emitting(istk->conds->state))
2145 j = COND_NEVER;
2146 else {
2147 j = if_condition(tline->next, i);
2148 tline->next = NULL; /* it got freed */
2149 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2151 cond = nasm_malloc(sizeof(Cond));
2152 cond->next = istk->conds;
2153 cond->state = j;
2154 istk->conds = cond;
2155 free_tlist(origline);
2156 return DIRECTIVE_FOUND;
2158 CASE_PP_ELIF:
2159 if (!istk->conds)
2160 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2161 if (emitting(istk->conds->state)
2162 || istk->conds->state == COND_NEVER)
2163 istk->conds->state = COND_NEVER;
2164 else {
2166 * IMPORTANT: In the case of %if, we will already have
2167 * called expand_mmac_params(); however, if we're
2168 * processing an %elif we must have been in a
2169 * non-emitting mode, which would have inhibited
2170 * the normal invocation of expand_mmac_params(). Therefore,
2171 * we have to do it explicitly here.
2173 j = if_condition(expand_mmac_params(tline->next), i);
2174 tline->next = NULL; /* it got freed */
2175 istk->conds->state =
2176 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2178 free_tlist(origline);
2179 return DIRECTIVE_FOUND;
2181 case PP_ELSE:
2182 if (tline->next)
2183 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2184 if (!istk->conds)
2185 error(ERR_FATAL, "`%%else': no matching `%%if'");
2186 if (emitting(istk->conds->state)
2187 || istk->conds->state == COND_NEVER)
2188 istk->conds->state = COND_ELSE_FALSE;
2189 else
2190 istk->conds->state = COND_ELSE_TRUE;
2191 free_tlist(origline);
2192 return DIRECTIVE_FOUND;
2194 case PP_ENDIF:
2195 if (tline->next)
2196 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2197 if (!istk->conds)
2198 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2199 cond = istk->conds;
2200 istk->conds = cond->next;
2201 nasm_free(cond);
2202 free_tlist(origline);
2203 return DIRECTIVE_FOUND;
2205 case PP_MACRO:
2206 case PP_IMACRO:
2207 if (defining)
2208 error(ERR_FATAL,
2209 "`%%%smacro': already defining a macro",
2210 (i == PP_IMACRO ? "i" : ""));
2211 tline = tline->next;
2212 skip_white_(tline);
2213 tline = expand_id(tline);
2214 if (!tok_type_(tline, TOK_ID)) {
2215 error(ERR_NONFATAL,
2216 "`%%%smacro' expects a macro name",
2217 (i == PP_IMACRO ? "i" : ""));
2218 return DIRECTIVE_FOUND;
2220 defining = nasm_malloc(sizeof(MMacro));
2221 defining->name = nasm_strdup(tline->text);
2222 defining->casesense = (i == PP_MACRO);
2223 defining->plus = false;
2224 defining->nolist = false;
2225 defining->in_progress = 0;
2226 defining->rep_nest = NULL;
2227 tline = expand_smacro(tline->next);
2228 skip_white_(tline);
2229 if (!tok_type_(tline, TOK_NUMBER)) {
2230 error(ERR_NONFATAL,
2231 "`%%%smacro' expects a parameter count",
2232 (i == PP_IMACRO ? "i" : ""));
2233 defining->nparam_min = defining->nparam_max = 0;
2234 } else {
2235 defining->nparam_min = defining->nparam_max =
2236 readnum(tline->text, &err);
2237 if (err)
2238 error(ERR_NONFATAL,
2239 "unable to parse parameter count `%s'", tline->text);
2241 if (tline && tok_is_(tline->next, "-")) {
2242 tline = tline->next->next;
2243 if (tok_is_(tline, "*"))
2244 defining->nparam_max = INT_MAX;
2245 else if (!tok_type_(tline, TOK_NUMBER))
2246 error(ERR_NONFATAL,
2247 "`%%%smacro' expects a parameter count after `-'",
2248 (i == PP_IMACRO ? "i" : ""));
2249 else {
2250 defining->nparam_max = readnum(tline->text, &err);
2251 if (err)
2252 error(ERR_NONFATAL,
2253 "unable to parse parameter count `%s'",
2254 tline->text);
2255 if (defining->nparam_min > defining->nparam_max)
2256 error(ERR_NONFATAL,
2257 "minimum parameter count exceeds maximum");
2260 if (tline && tok_is_(tline->next, "+")) {
2261 tline = tline->next;
2262 defining->plus = true;
2264 if (tline && tok_type_(tline->next, TOK_ID) &&
2265 !nasm_stricmp(tline->next->text, ".nolist")) {
2266 tline = tline->next;
2267 defining->nolist = true;
2269 mmac = (MMacro *) hash_findix(mmacros, defining->name);
2270 while (mmac) {
2271 if (!strcmp(mmac->name, defining->name) &&
2272 (mmac->nparam_min <= defining->nparam_max
2273 || defining->plus)
2274 && (defining->nparam_min <= mmac->nparam_max
2275 || mmac->plus)) {
2276 error(ERR_WARNING,
2277 "redefining multi-line macro `%s'", defining->name);
2278 break;
2280 mmac = mmac->next;
2283 * Handle default parameters.
2285 if (tline && tline->next) {
2286 defining->dlist = tline->next;
2287 tline->next = NULL;
2288 count_mmac_params(defining->dlist, &defining->ndefs,
2289 &defining->defaults);
2290 } else {
2291 defining->dlist = NULL;
2292 defining->defaults = NULL;
2294 defining->expansion = NULL;
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2298 case PP_ENDM:
2299 case PP_ENDMACRO:
2300 if (!defining) {
2301 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2302 return DIRECTIVE_FOUND;
2304 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2305 defining->next = *mmhead;
2306 *mmhead = defining;
2307 defining = NULL;
2308 free_tlist(origline);
2309 return DIRECTIVE_FOUND;
2311 case PP_ROTATE:
2312 if (tline->next && tline->next->type == TOK_WHITESPACE)
2313 tline = tline->next;
2314 if (tline->next == NULL) {
2315 free_tlist(origline);
2316 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2317 return DIRECTIVE_FOUND;
2319 t = expand_smacro(tline->next);
2320 tline->next = NULL;
2321 free_tlist(origline);
2322 tline = t;
2323 tptr = &t;
2324 tokval.t_type = TOKEN_INVALID;
2325 evalresult =
2326 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2327 free_tlist(tline);
2328 if (!evalresult)
2329 return DIRECTIVE_FOUND;
2330 if (tokval.t_type)
2331 error(ERR_WARNING,
2332 "trailing garbage after expression ignored");
2333 if (!is_simple(evalresult)) {
2334 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2335 return DIRECTIVE_FOUND;
2337 mmac = istk->mstk;
2338 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2339 mmac = mmac->next_active;
2340 if (!mmac) {
2341 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2342 } else if (mmac->nparam == 0) {
2343 error(ERR_NONFATAL,
2344 "`%%rotate' invoked within macro without parameters");
2345 } else {
2346 int rotate = mmac->rotate + reloc_value(evalresult);
2348 rotate %= (int)mmac->nparam;
2349 if (rotate < 0)
2350 rotate += mmac->nparam;
2352 mmac->rotate = rotate;
2354 return DIRECTIVE_FOUND;
2356 case PP_REP:
2357 nolist = false;
2358 do {
2359 tline = tline->next;
2360 } while (tok_type_(tline, TOK_WHITESPACE));
2362 if (tok_type_(tline, TOK_ID) &&
2363 nasm_stricmp(tline->text, ".nolist") == 0) {
2364 nolist = true;
2365 do {
2366 tline = tline->next;
2367 } while (tok_type_(tline, TOK_WHITESPACE));
2370 if (tline) {
2371 t = expand_smacro(tline);
2372 tptr = &t;
2373 tokval.t_type = TOKEN_INVALID;
2374 evalresult =
2375 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2376 if (!evalresult) {
2377 free_tlist(origline);
2378 return DIRECTIVE_FOUND;
2380 if (tokval.t_type)
2381 error(ERR_WARNING,
2382 "trailing garbage after expression ignored");
2383 if (!is_simple(evalresult)) {
2384 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2385 return DIRECTIVE_FOUND;
2387 count = reloc_value(evalresult) + 1;
2388 } else {
2389 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2390 count = 0;
2392 free_tlist(origline);
2394 tmp_defining = defining;
2395 defining = nasm_malloc(sizeof(MMacro));
2396 defining->name = NULL; /* flags this macro as a %rep block */
2397 defining->casesense = false;
2398 defining->plus = false;
2399 defining->nolist = nolist;
2400 defining->in_progress = count;
2401 defining->nparam_min = defining->nparam_max = 0;
2402 defining->defaults = NULL;
2403 defining->dlist = NULL;
2404 defining->expansion = NULL;
2405 defining->next_active = istk->mstk;
2406 defining->rep_nest = tmp_defining;
2407 return DIRECTIVE_FOUND;
2409 case PP_ENDREP:
2410 if (!defining || defining->name) {
2411 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2412 return DIRECTIVE_FOUND;
2416 * Now we have a "macro" defined - although it has no name
2417 * and we won't be entering it in the hash tables - we must
2418 * push a macro-end marker for it on to istk->expansion.
2419 * After that, it will take care of propagating itself (a
2420 * macro-end marker line for a macro which is really a %rep
2421 * block will cause the macro to be re-expanded, complete
2422 * with another macro-end marker to ensure the process
2423 * continues) until the whole expansion is forcibly removed
2424 * from istk->expansion by a %exitrep.
2426 l = nasm_malloc(sizeof(Line));
2427 l->next = istk->expansion;
2428 l->finishes = defining;
2429 l->first = NULL;
2430 istk->expansion = l;
2432 istk->mstk = defining;
2434 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2435 tmp_defining = defining;
2436 defining = defining->rep_nest;
2437 free_tlist(origline);
2438 return DIRECTIVE_FOUND;
2440 case PP_EXITREP:
2442 * We must search along istk->expansion until we hit a
2443 * macro-end marker for a macro with no name. Then we set
2444 * its `in_progress' flag to 0.
2446 for (l = istk->expansion; l; l = l->next)
2447 if (l->finishes && !l->finishes->name)
2448 break;
2450 if (l)
2451 l->finishes->in_progress = 0;
2452 else
2453 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
2457 case PP_XDEFINE:
2458 case PP_IXDEFINE:
2459 case PP_DEFINE:
2460 case PP_IDEFINE:
2461 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2463 tline = tline->next;
2464 skip_white_(tline);
2465 tline = expand_id(tline);
2466 if (!tline || (tline->type != TOK_ID &&
2467 (tline->type != TOK_PREPROC_ID ||
2468 tline->text[1] != '$'))) {
2469 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2470 pp_directives[i]);
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2475 ctx = get_ctx(tline->text, false);
2477 mname = tline->text;
2478 last = tline;
2479 param_start = tline = tline->next;
2480 nparam = 0;
2482 /* Expand the macro definition now for %xdefine and %ixdefine */
2483 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2484 tline = expand_smacro(tline);
2486 if (tok_is_(tline, "(")) {
2488 * This macro has parameters.
2491 tline = tline->next;
2492 while (1) {
2493 skip_white_(tline);
2494 if (!tline) {
2495 error(ERR_NONFATAL, "parameter identifier expected");
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
2499 if (tline->type != TOK_ID) {
2500 error(ERR_NONFATAL,
2501 "`%s': parameter identifier expected",
2502 tline->text);
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2506 tline->type = TOK_SMAC_PARAM + nparam++;
2507 tline = tline->next;
2508 skip_white_(tline);
2509 if (tok_is_(tline, ",")) {
2510 tline = tline->next;
2511 continue;
2513 if (!tok_is_(tline, ")")) {
2514 error(ERR_NONFATAL,
2515 "`)' expected to terminate macro template");
2516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
2519 break;
2521 last = tline;
2522 tline = tline->next;
2524 if (tok_type_(tline, TOK_WHITESPACE))
2525 last = tline, tline = tline->next;
2526 macro_start = NULL;
2527 last->next = NULL;
2528 t = tline;
2529 while (t) {
2530 if (t->type == TOK_ID) {
2531 for (tt = param_start; tt; tt = tt->next)
2532 if (tt->type >= TOK_SMAC_PARAM &&
2533 !strcmp(tt->text, t->text))
2534 t->type = tt->type;
2536 tt = t->next;
2537 t->next = macro_start;
2538 macro_start = t;
2539 t = tt;
2542 * Good. We now have a macro name, a parameter count, and a
2543 * token list (in reverse order) for an expansion. We ought
2544 * to be OK just to create an SMacro, store it, and let
2545 * free_tlist have the rest of the line (which we have
2546 * carefully re-terminated after chopping off the expansion
2547 * from the end).
2549 define_smacro(ctx, mname, casesense, nparam, macro_start);
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND;
2553 case PP_UNDEF:
2554 tline = tline->next;
2555 skip_white_(tline);
2556 tline = expand_id(tline);
2557 if (!tline || (tline->type != TOK_ID &&
2558 (tline->type != TOK_PREPROC_ID ||
2559 tline->text[1] != '$'))) {
2560 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2561 free_tlist(origline);
2562 return DIRECTIVE_FOUND;
2564 if (tline->next) {
2565 error(ERR_WARNING,
2566 "trailing garbage after macro name ignored");
2569 /* Find the context that symbol belongs to */
2570 ctx = get_ctx(tline->text, false);
2571 undef_smacro(ctx, tline->text);
2572 free_tlist(origline);
2573 return DIRECTIVE_FOUND;
2575 case PP_STRLEN:
2576 casesense = true;
2578 tline = tline->next;
2579 skip_white_(tline);
2580 tline = expand_id(tline);
2581 if (!tline || (tline->type != TOK_ID &&
2582 (tline->type != TOK_PREPROC_ID ||
2583 tline->text[1] != '$'))) {
2584 error(ERR_NONFATAL,
2585 "`%%strlen' expects a macro identifier as first parameter");
2586 free_tlist(origline);
2587 return DIRECTIVE_FOUND;
2589 ctx = get_ctx(tline->text, false);
2591 mname = tline->text;
2592 last = tline;
2593 tline = expand_smacro(tline->next);
2594 last->next = NULL;
2596 t = tline;
2597 while (tok_type_(t, TOK_WHITESPACE))
2598 t = t->next;
2599 /* t should now point to the string */
2600 if (t->type != TOK_STRING) {
2601 error(ERR_NONFATAL,
2602 "`%%strlen` requires string as second parameter");
2603 free_tlist(tline);
2604 free_tlist(origline);
2605 return DIRECTIVE_FOUND;
2608 macro_start = nasm_malloc(sizeof(*macro_start));
2609 macro_start->next = NULL;
2610 make_tok_num(macro_start, strlen(t->text) - 2);
2611 macro_start->mac = NULL;
2614 * We now have a macro name, an implicit parameter count of
2615 * zero, and a numeric token to use as an expansion. Create
2616 * and store an SMacro.
2618 define_smacro(ctx, mname, casesense, 0, macro_start);
2619 free_tlist(tline);
2620 free_tlist(origline);
2621 return DIRECTIVE_FOUND;
2623 case PP_SUBSTR:
2624 casesense = true;
2626 tline = tline->next;
2627 skip_white_(tline);
2628 tline = expand_id(tline);
2629 if (!tline || (tline->type != TOK_ID &&
2630 (tline->type != TOK_PREPROC_ID ||
2631 tline->text[1] != '$'))) {
2632 error(ERR_NONFATAL,
2633 "`%%substr' expects a macro identifier as first parameter");
2634 free_tlist(origline);
2635 return DIRECTIVE_FOUND;
2637 ctx = get_ctx(tline->text, false);
2639 mname = tline->text;
2640 last = tline;
2641 tline = expand_smacro(tline->next);
2642 last->next = NULL;
2644 t = tline->next;
2645 while (tok_type_(t, TOK_WHITESPACE))
2646 t = t->next;
2648 /* t should now point to the string */
2649 if (t->type != TOK_STRING) {
2650 error(ERR_NONFATAL,
2651 "`%%substr` requires string as second parameter");
2652 free_tlist(tline);
2653 free_tlist(origline);
2654 return DIRECTIVE_FOUND;
2657 tt = t->next;
2658 tptr = &tt;
2659 tokval.t_type = TOKEN_INVALID;
2660 evalresult =
2661 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2662 if (!evalresult) {
2663 free_tlist(tline);
2664 free_tlist(origline);
2665 return DIRECTIVE_FOUND;
2667 if (!is_simple(evalresult)) {
2668 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2669 free_tlist(tline);
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2674 macro_start = nasm_malloc(sizeof(*macro_start));
2675 macro_start->next = NULL;
2676 macro_start->text = nasm_strdup("'''");
2677 if (evalresult->value > 0
2678 && evalresult->value < (int) strlen(t->text) - 1) {
2679 macro_start->text[1] = t->text[evalresult->value];
2680 } else {
2681 macro_start->text[2] = '\0';
2683 macro_start->type = TOK_STRING;
2684 macro_start->mac = NULL;
2687 * We now have a macro name, an implicit parameter count of
2688 * zero, and a numeric token to use as an expansion. Create
2689 * and store an SMacro.
2691 define_smacro(ctx, mname, casesense, 0, macro_start);
2692 free_tlist(tline);
2693 free_tlist(origline);
2694 return DIRECTIVE_FOUND;
2696 case PP_ASSIGN:
2697 case PP_IASSIGN:
2698 casesense = (i == PP_ASSIGN);
2700 tline = tline->next;
2701 skip_white_(tline);
2702 tline = expand_id(tline);
2703 if (!tline || (tline->type != TOK_ID &&
2704 (tline->type != TOK_PREPROC_ID ||
2705 tline->text[1] != '$'))) {
2706 error(ERR_NONFATAL,
2707 "`%%%sassign' expects a macro identifier",
2708 (i == PP_IASSIGN ? "i" : ""));
2709 free_tlist(origline);
2710 return DIRECTIVE_FOUND;
2712 ctx = get_ctx(tline->text, false);
2714 mname = tline->text;
2715 last = tline;
2716 tline = expand_smacro(tline->next);
2717 last->next = NULL;
2719 t = tline;
2720 tptr = &t;
2721 tokval.t_type = TOKEN_INVALID;
2722 evalresult =
2723 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2724 free_tlist(tline);
2725 if (!evalresult) {
2726 free_tlist(origline);
2727 return DIRECTIVE_FOUND;
2730 if (tokval.t_type)
2731 error(ERR_WARNING,
2732 "trailing garbage after expression ignored");
2734 if (!is_simple(evalresult)) {
2735 error(ERR_NONFATAL,
2736 "non-constant value given to `%%%sassign'",
2737 (i == PP_IASSIGN ? "i" : ""));
2738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
2742 macro_start = nasm_malloc(sizeof(*macro_start));
2743 macro_start->next = NULL;
2744 make_tok_num(macro_start, reloc_value(evalresult));
2745 macro_start->mac = NULL;
2748 * We now have a macro name, an implicit parameter count of
2749 * zero, and a numeric token to use as an expansion. Create
2750 * and store an SMacro.
2752 define_smacro(ctx, mname, casesense, 0, macro_start);
2753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
2756 case PP_LINE:
2758 * Syntax is `%line nnn[+mmm] [filename]'
2760 tline = tline->next;
2761 skip_white_(tline);
2762 if (!tok_type_(tline, TOK_NUMBER)) {
2763 error(ERR_NONFATAL, "`%%line' expects line number");
2764 free_tlist(origline);
2765 return DIRECTIVE_FOUND;
2767 k = readnum(tline->text, &err);
2768 m = 1;
2769 tline = tline->next;
2770 if (tok_is_(tline, "+")) {
2771 tline = tline->next;
2772 if (!tok_type_(tline, TOK_NUMBER)) {
2773 error(ERR_NONFATAL, "`%%line' expects line increment");
2774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
2777 m = readnum(tline->text, &err);
2778 tline = tline->next;
2780 skip_white_(tline);
2781 src_set_linnum(k);
2782 istk->lineinc = m;
2783 if (tline) {
2784 nasm_free(src_set_fname(detoken(tline, false)));
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2789 default:
2790 error(ERR_FATAL,
2791 "preprocessor directive `%s' not yet implemented",
2792 pp_directives[i]);
2793 break;
2795 return DIRECTIVE_FOUND;
2799 * Ensure that a macro parameter contains a condition code and
2800 * nothing else. Return the condition code index if so, or -1
2801 * otherwise.
2803 static int find_cc(Token * t)
2805 Token *tt;
2806 int i, j, k, m;
2808 if (!t)
2809 return -1; /* Probably a %+ without a space */
2811 skip_white_(t);
2812 if (t->type != TOK_ID)
2813 return -1;
2814 tt = t->next;
2815 skip_white_(tt);
2816 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2817 return -1;
2819 i = -1;
2820 j = elements(conditions);
2821 while (j - i > 1) {
2822 k = (j + i) / 2;
2823 m = nasm_stricmp(t->text, conditions[k]);
2824 if (m == 0) {
2825 i = k;
2826 j = -2;
2827 break;
2828 } else if (m < 0) {
2829 j = k;
2830 } else
2831 i = k;
2833 if (j != -2)
2834 return -1;
2835 return i;
2839 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2840 * %-n) and MMacro-local identifiers (%%foo).
2842 static Token *expand_mmac_params(Token * tline)
2844 Token *t, *tt, **tail, *thead;
2846 tail = &thead;
2847 thead = NULL;
2849 while (tline) {
2850 if (tline->type == TOK_PREPROC_ID &&
2851 (((tline->text[1] == '+' || tline->text[1] == '-')
2852 && tline->text[2]) || tline->text[1] == '%'
2853 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2854 char *text = NULL;
2855 int type = 0, cc; /* type = 0 to placate optimisers */
2856 char tmpbuf[30];
2857 unsigned int n;
2858 int i;
2859 MMacro *mac;
2861 t = tline;
2862 tline = tline->next;
2864 mac = istk->mstk;
2865 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2866 mac = mac->next_active;
2867 if (!mac)
2868 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2869 else
2870 switch (t->text[1]) {
2872 * We have to make a substitution of one of the
2873 * forms %1, %-1, %+1, %%foo, %0.
2875 case '0':
2876 type = TOK_NUMBER;
2877 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2878 text = nasm_strdup(tmpbuf);
2879 break;
2880 case '%':
2881 type = TOK_ID;
2882 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2883 mac->unique);
2884 text = nasm_strcat(tmpbuf, t->text + 2);
2885 break;
2886 case '-':
2887 n = atoi(t->text + 2) - 1;
2888 if (n >= mac->nparam)
2889 tt = NULL;
2890 else {
2891 if (mac->nparam > 1)
2892 n = (n + mac->rotate) % mac->nparam;
2893 tt = mac->params[n];
2895 cc = find_cc(tt);
2896 if (cc == -1) {
2897 error(ERR_NONFATAL,
2898 "macro parameter %d is not a condition code",
2899 n + 1);
2900 text = NULL;
2901 } else {
2902 type = TOK_ID;
2903 if (inverse_ccs[cc] == -1) {
2904 error(ERR_NONFATAL,
2905 "condition code `%s' is not invertible",
2906 conditions[cc]);
2907 text = NULL;
2908 } else
2909 text =
2910 nasm_strdup(conditions[inverse_ccs[cc]]);
2912 break;
2913 case '+':
2914 n = atoi(t->text + 2) - 1;
2915 if (n >= mac->nparam)
2916 tt = NULL;
2917 else {
2918 if (mac->nparam > 1)
2919 n = (n + mac->rotate) % mac->nparam;
2920 tt = mac->params[n];
2922 cc = find_cc(tt);
2923 if (cc == -1) {
2924 error(ERR_NONFATAL,
2925 "macro parameter %d is not a condition code",
2926 n + 1);
2927 text = NULL;
2928 } else {
2929 type = TOK_ID;
2930 text = nasm_strdup(conditions[cc]);
2932 break;
2933 default:
2934 n = atoi(t->text + 1) - 1;
2935 if (n >= mac->nparam)
2936 tt = NULL;
2937 else {
2938 if (mac->nparam > 1)
2939 n = (n + mac->rotate) % mac->nparam;
2940 tt = mac->params[n];
2942 if (tt) {
2943 for (i = 0; i < mac->paramlen[n]; i++) {
2944 *tail = new_Token(NULL, tt->type, tt->text, 0);
2945 tail = &(*tail)->next;
2946 tt = tt->next;
2949 text = NULL; /* we've done it here */
2950 break;
2952 if (!text) {
2953 delete_Token(t);
2954 } else {
2955 *tail = t;
2956 tail = &t->next;
2957 t->type = type;
2958 nasm_free(t->text);
2959 t->text = text;
2960 t->mac = NULL;
2962 continue;
2963 } else {
2964 t = *tail = tline;
2965 tline = tline->next;
2966 t->mac = NULL;
2967 tail = &t->next;
2970 *tail = NULL;
2971 t = thead;
2972 for (; t && (tt = t->next) != NULL; t = t->next)
2973 switch (t->type) {
2974 case TOK_WHITESPACE:
2975 if (tt->type == TOK_WHITESPACE) {
2976 t->next = delete_Token(tt);
2978 break;
2979 case TOK_ID:
2980 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2981 char *tmp = nasm_strcat(t->text, tt->text);
2982 nasm_free(t->text);
2983 t->text = tmp;
2984 t->next = delete_Token(tt);
2986 break;
2987 case TOK_NUMBER:
2988 if (tt->type == TOK_NUMBER) {
2989 char *tmp = nasm_strcat(t->text, tt->text);
2990 nasm_free(t->text);
2991 t->text = tmp;
2992 t->next = delete_Token(tt);
2994 break;
2995 default:
2996 break;
2999 return thead;
3003 * Expand all single-line macro calls made in the given line.
3004 * Return the expanded version of the line. The original is deemed
3005 * to be destroyed in the process. (In reality we'll just move
3006 * Tokens from input to output a lot of the time, rather than
3007 * actually bothering to destroy and replicate.)
3009 #define DEADMAN_LIMIT (1 << 20)
3011 static Token *expand_smacro(Token * tline)
3013 Token *t, *tt, *mstart, **tail, *thead;
3014 SMacro *head = NULL, *m;
3015 Token **params;
3016 int *paramsize;
3017 unsigned int nparam, sparam;
3018 int brackets, rescan;
3019 Token *org_tline = tline;
3020 Context *ctx;
3021 char *mname;
3022 int deadman = DEADMAN_LIMIT;
3025 * Trick: we should avoid changing the start token pointer since it can
3026 * be contained in "next" field of other token. Because of this
3027 * we allocate a copy of first token and work with it; at the end of
3028 * routine we copy it back
3030 if (org_tline) {
3031 tline =
3032 new_Token(org_tline->next, org_tline->type, org_tline->text,
3034 tline->mac = org_tline->mac;
3035 nasm_free(org_tline->text);
3036 org_tline->text = NULL;
3039 again:
3040 tail = &thead;
3041 thead = NULL;
3043 while (tline) { /* main token loop */
3044 if (!--deadman) {
3045 error(ERR_NONFATAL, "interminable macro recursion");
3046 break;
3049 if ((mname = tline->text)) {
3050 /* if this token is a local macro, look in local context */
3051 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3052 ctx = get_ctx(mname, true);
3053 else
3054 ctx = NULL;
3055 if (!ctx) {
3056 head = (SMacro *) hash_findix(smacros, mname);
3057 } else {
3058 head = ctx->localmac;
3061 * We've hit an identifier. As in is_mmacro below, we first
3062 * check whether the identifier is a single-line macro at
3063 * all, then think about checking for parameters if
3064 * necessary.
3066 for (m = head; m; m = m->next)
3067 if (!mstrcmp(m->name, mname, m->casesense))
3068 break;
3069 if (m) {
3070 mstart = tline;
3071 params = NULL;
3072 paramsize = NULL;
3073 if (m->nparam == 0) {
3075 * Simple case: the macro is parameterless. Discard the
3076 * one token that the macro call took, and push the
3077 * expansion back on the to-do stack.
3079 if (!m->expansion) {
3080 if (!strcmp("__FILE__", m->name)) {
3081 int32_t num = 0;
3082 src_get(&num, &(tline->text));
3083 nasm_quote(&(tline->text));
3084 tline->type = TOK_STRING;
3085 continue;
3087 if (!strcmp("__LINE__", m->name)) {
3088 nasm_free(tline->text);
3089 make_tok_num(tline, src_get_linnum());
3090 continue;
3092 if (!strcmp("__BITS__", m->name)) {
3093 nasm_free(tline->text);
3094 make_tok_num(tline, globalbits);
3095 continue;
3097 tline = delete_Token(tline);
3098 continue;
3100 } else {
3102 * Complicated case: at least one macro with this name
3103 * exists and takes parameters. We must find the
3104 * parameters in the call, count them, find the SMacro
3105 * that corresponds to that form of the macro call, and
3106 * substitute for the parameters when we expand. What a
3107 * pain.
3109 /*tline = tline->next;
3110 skip_white_(tline); */
3111 do {
3112 t = tline->next;
3113 while (tok_type_(t, TOK_SMAC_END)) {
3114 t->mac->in_progress = false;
3115 t->text = NULL;
3116 t = tline->next = delete_Token(t);
3118 tline = t;
3119 } while (tok_type_(tline, TOK_WHITESPACE));
3120 if (!tok_is_(tline, "(")) {
3122 * This macro wasn't called with parameters: ignore
3123 * the call. (Behaviour borrowed from gnu cpp.)
3125 tline = mstart;
3126 m = NULL;
3127 } else {
3128 int paren = 0;
3129 int white = 0;
3130 brackets = 0;
3131 nparam = 0;
3132 sparam = PARAM_DELTA;
3133 params = nasm_malloc(sparam * sizeof(Token *));
3134 params[0] = tline->next;
3135 paramsize = nasm_malloc(sparam * sizeof(int));
3136 paramsize[0] = 0;
3137 while (true) { /* parameter loop */
3139 * For some unusual expansions
3140 * which concatenates function call
3142 t = tline->next;
3143 while (tok_type_(t, TOK_SMAC_END)) {
3144 t->mac->in_progress = false;
3145 t->text = NULL;
3146 t = tline->next = delete_Token(t);
3148 tline = t;
3150 if (!tline) {
3151 error(ERR_NONFATAL,
3152 "macro call expects terminating `)'");
3153 break;
3155 if (tline->type == TOK_WHITESPACE
3156 && brackets <= 0) {
3157 if (paramsize[nparam])
3158 white++;
3159 else
3160 params[nparam] = tline->next;
3161 continue; /* parameter loop */
3163 if (tline->type == TOK_OTHER
3164 && tline->text[1] == 0) {
3165 char ch = tline->text[0];
3166 if (ch == ',' && !paren && brackets <= 0) {
3167 if (++nparam >= sparam) {
3168 sparam += PARAM_DELTA;
3169 params = nasm_realloc(params,
3170 sparam *
3171 sizeof(Token
3172 *));
3173 paramsize =
3174 nasm_realloc(paramsize,
3175 sparam *
3176 sizeof(int));
3178 params[nparam] = tline->next;
3179 paramsize[nparam] = 0;
3180 white = 0;
3181 continue; /* parameter loop */
3183 if (ch == '{' &&
3184 (brackets > 0 || (brackets == 0 &&
3185 !paramsize[nparam])))
3187 if (!(brackets++)) {
3188 params[nparam] = tline->next;
3189 continue; /* parameter loop */
3192 if (ch == '}' && brackets > 0)
3193 if (--brackets == 0) {
3194 brackets = -1;
3195 continue; /* parameter loop */
3197 if (ch == '(' && !brackets)
3198 paren++;
3199 if (ch == ')' && brackets <= 0)
3200 if (--paren < 0)
3201 break;
3203 if (brackets < 0) {
3204 brackets = 0;
3205 error(ERR_NONFATAL, "braces do not "
3206 "enclose all of macro parameter");
3208 paramsize[nparam] += white + 1;
3209 white = 0;
3210 } /* parameter loop */
3211 nparam++;
3212 while (m && (m->nparam != nparam ||
3213 mstrcmp(m->name, mname,
3214 m->casesense)))
3215 m = m->next;
3216 if (!m)
3217 error(ERR_WARNING | ERR_WARN_MNP,
3218 "macro `%s' exists, "
3219 "but not taking %d parameters",
3220 mstart->text, nparam);
3223 if (m && m->in_progress)
3224 m = NULL;
3225 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3227 * Design question: should we handle !tline, which
3228 * indicates missing ')' here, or expand those
3229 * macros anyway, which requires the (t) test a few
3230 * lines down?
3232 nasm_free(params);
3233 nasm_free(paramsize);
3234 tline = mstart;
3235 } else {
3237 * Expand the macro: we are placed on the last token of the
3238 * call, so that we can easily split the call from the
3239 * following tokens. We also start by pushing an SMAC_END
3240 * token for the cycle removal.
3242 t = tline;
3243 if (t) {
3244 tline = t->next;
3245 t->next = NULL;
3247 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3248 tt->mac = m;
3249 m->in_progress = true;
3250 tline = tt;
3251 for (t = m->expansion; t; t = t->next) {
3252 if (t->type >= TOK_SMAC_PARAM) {
3253 Token *pcopy = tline, **ptail = &pcopy;
3254 Token *ttt, *pt;
3255 int i;
3257 ttt = params[t->type - TOK_SMAC_PARAM];
3258 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3259 --i >= 0;) {
3260 pt = *ptail =
3261 new_Token(tline, ttt->type, ttt->text,
3263 ptail = &pt->next;
3264 ttt = ttt->next;
3266 tline = pcopy;
3267 } else {
3268 tt = new_Token(tline, t->type, t->text, 0);
3269 tline = tt;
3274 * Having done that, get rid of the macro call, and clean
3275 * up the parameters.
3277 nasm_free(params);
3278 nasm_free(paramsize);
3279 free_tlist(mstart);
3280 continue; /* main token loop */
3285 if (tline->type == TOK_SMAC_END) {
3286 tline->mac->in_progress = false;
3287 tline = delete_Token(tline);
3288 } else {
3289 t = *tail = tline;
3290 tline = tline->next;
3291 t->mac = NULL;
3292 t->next = NULL;
3293 tail = &t->next;
3298 * Now scan the entire line and look for successive TOK_IDs that resulted
3299 * after expansion (they can't be produced by tokenize()). The successive
3300 * TOK_IDs should be concatenated.
3301 * Also we look for %+ tokens and concatenate the tokens before and after
3302 * them (without white spaces in between).
3304 t = thead;
3305 rescan = 0;
3306 while (t) {
3307 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3308 t = t->next;
3309 if (!t || !t->next)
3310 break;
3311 if (t->next->type == TOK_ID ||
3312 t->next->type == TOK_PREPROC_ID ||
3313 t->next->type == TOK_NUMBER) {
3314 char *p = nasm_strcat(t->text, t->next->text);
3315 nasm_free(t->text);
3316 t->next = delete_Token(t->next);
3317 t->text = p;
3318 rescan = 1;
3319 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3320 t->next->next->type == TOK_PREPROC_ID &&
3321 strcmp(t->next->next->text, "%+") == 0) {
3322 /* free the next whitespace, the %+ token and next whitespace */
3323 int i;
3324 for (i = 1; i <= 3; i++) {
3325 if (!t->next
3326 || (i != 2 && t->next->type != TOK_WHITESPACE))
3327 break;
3328 t->next = delete_Token(t->next);
3329 } /* endfor */
3330 } else
3331 t = t->next;
3333 /* If we concatenaded something, re-scan the line for macros */
3334 if (rescan) {
3335 tline = thead;
3336 goto again;
3339 if (org_tline) {
3340 if (thead) {
3341 *org_tline = *thead;
3342 /* since we just gave text to org_line, don't free it */
3343 thead->text = NULL;
3344 delete_Token(thead);
3345 } else {
3346 /* the expression expanded to empty line;
3347 we can't return NULL for some reasons
3348 we just set the line to a single WHITESPACE token. */
3349 memset(org_tline, 0, sizeof(*org_tline));
3350 org_tline->text = NULL;
3351 org_tline->type = TOK_WHITESPACE;
3353 thead = org_tline;
3356 return thead;
3360 * Similar to expand_smacro but used exclusively with macro identifiers
3361 * right before they are fetched in. The reason is that there can be
3362 * identifiers consisting of several subparts. We consider that if there
3363 * are more than one element forming the name, user wants a expansion,
3364 * otherwise it will be left as-is. Example:
3366 * %define %$abc cde
3368 * the identifier %$abc will be left as-is so that the handler for %define
3369 * will suck it and define the corresponding value. Other case:
3371 * %define _%$abc cde
3373 * In this case user wants name to be expanded *before* %define starts
3374 * working, so we'll expand %$abc into something (if it has a value;
3375 * otherwise it will be left as-is) then concatenate all successive
3376 * PP_IDs into one.
3378 static Token *expand_id(Token * tline)
3380 Token *cur, *oldnext = NULL;
3382 if (!tline || !tline->next)
3383 return tline;
3385 cur = tline;
3386 while (cur->next &&
3387 (cur->next->type == TOK_ID ||
3388 cur->next->type == TOK_PREPROC_ID
3389 || cur->next->type == TOK_NUMBER))
3390 cur = cur->next;
3392 /* If identifier consists of just one token, don't expand */
3393 if (cur == tline)
3394 return tline;
3396 if (cur) {
3397 oldnext = cur->next; /* Detach the tail past identifier */
3398 cur->next = NULL; /* so that expand_smacro stops here */
3401 tline = expand_smacro(tline);
3403 if (cur) {
3404 /* expand_smacro possibly changhed tline; re-scan for EOL */
3405 cur = tline;
3406 while (cur && cur->next)
3407 cur = cur->next;
3408 if (cur)
3409 cur->next = oldnext;
3412 return tline;
3416 * Determine whether the given line constitutes a multi-line macro
3417 * call, and return the MMacro structure called if so. Doesn't have
3418 * to check for an initial label - that's taken care of in
3419 * expand_mmacro - but must check numbers of parameters. Guaranteed
3420 * to be called with tline->type == TOK_ID, so the putative macro
3421 * name is easy to find.
3423 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3425 MMacro *head, *m;
3426 Token **params;
3427 int nparam;
3429 head = (MMacro *) hash_findix(mmacros, tline->text);
3432 * Efficiency: first we see if any macro exists with the given
3433 * name. If not, we can return NULL immediately. _Then_ we
3434 * count the parameters, and then we look further along the
3435 * list if necessary to find the proper MMacro.
3437 for (m = head; m; m = m->next)
3438 if (!mstrcmp(m->name, tline->text, m->casesense))
3439 break;
3440 if (!m)
3441 return NULL;
3444 * OK, we have a potential macro. Count and demarcate the
3445 * parameters.
3447 count_mmac_params(tline->next, &nparam, &params);
3450 * So we know how many parameters we've got. Find the MMacro
3451 * structure that handles this number.
3453 while (m) {
3454 if (m->nparam_min <= nparam
3455 && (m->plus || nparam <= m->nparam_max)) {
3457 * This one is right. Just check if cycle removal
3458 * prohibits us using it before we actually celebrate...
3460 if (m->in_progress) {
3461 #if 0
3462 error(ERR_NONFATAL,
3463 "self-reference in multi-line macro `%s'", m->name);
3464 #endif
3465 nasm_free(params);
3466 return NULL;
3469 * It's right, and we can use it. Add its default
3470 * parameters to the end of our list if necessary.
3472 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3473 params =
3474 nasm_realloc(params,
3475 ((m->nparam_min + m->ndefs +
3476 1) * sizeof(*params)));
3477 while (nparam < m->nparam_min + m->ndefs) {
3478 params[nparam] = m->defaults[nparam - m->nparam_min];
3479 nparam++;
3483 * If we've gone over the maximum parameter count (and
3484 * we're in Plus mode), ignore parameters beyond
3485 * nparam_max.
3487 if (m->plus && nparam > m->nparam_max)
3488 nparam = m->nparam_max;
3490 * Then terminate the parameter list, and leave.
3492 if (!params) { /* need this special case */
3493 params = nasm_malloc(sizeof(*params));
3494 nparam = 0;
3496 params[nparam] = NULL;
3497 *params_array = params;
3498 return m;
3501 * This one wasn't right: look for the next one with the
3502 * same name.
3504 for (m = m->next; m; m = m->next)
3505 if (!mstrcmp(m->name, tline->text, m->casesense))
3506 break;
3510 * After all that, we didn't find one with the right number of
3511 * parameters. Issue a warning, and fail to expand the macro.
3513 error(ERR_WARNING | ERR_WARN_MNP,
3514 "macro `%s' exists, but not taking %d parameters",
3515 tline->text, nparam);
3516 nasm_free(params);
3517 return NULL;
3521 * Expand the multi-line macro call made by the given line, if
3522 * there is one to be expanded. If there is, push the expansion on
3523 * istk->expansion and return 1. Otherwise return 0.
3525 static int expand_mmacro(Token * tline)
3527 Token *startline = tline;
3528 Token *label = NULL;
3529 int dont_prepend = 0;
3530 Token **params, *t, *tt;
3531 MMacro *m;
3532 Line *l, *ll;
3533 int i, nparam, *paramlen;
3535 t = tline;
3536 skip_white_(t);
3537 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3538 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3539 return 0;
3540 m = is_mmacro(t, &params);
3541 if (!m) {
3542 Token *last;
3544 * We have an id which isn't a macro call. We'll assume
3545 * it might be a label; we'll also check to see if a
3546 * colon follows it. Then, if there's another id after
3547 * that lot, we'll check it again for macro-hood.
3549 label = last = t;
3550 t = t->next;
3551 if (tok_type_(t, TOK_WHITESPACE))
3552 last = t, t = t->next;
3553 if (tok_is_(t, ":")) {
3554 dont_prepend = 1;
3555 last = t, t = t->next;
3556 if (tok_type_(t, TOK_WHITESPACE))
3557 last = t, t = t->next;
3559 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3560 return 0;
3561 last->next = NULL;
3562 tline = t;
3566 * Fix up the parameters: this involves stripping leading and
3567 * trailing whitespace, then stripping braces if they are
3568 * present.
3570 for (nparam = 0; params[nparam]; nparam++) ;
3571 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3573 for (i = 0; params[i]; i++) {
3574 int brace = false;
3575 int comma = (!m->plus || i < nparam - 1);
3577 t = params[i];
3578 skip_white_(t);
3579 if (tok_is_(t, "{"))
3580 t = t->next, brace = true, comma = false;
3581 params[i] = t;
3582 paramlen[i] = 0;
3583 while (t) {
3584 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3585 break; /* ... because we have hit a comma */
3586 if (comma && t->type == TOK_WHITESPACE
3587 && tok_is_(t->next, ","))
3588 break; /* ... or a space then a comma */
3589 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3590 break; /* ... or a brace */
3591 t = t->next;
3592 paramlen[i]++;
3597 * OK, we have a MMacro structure together with a set of
3598 * parameters. We must now go through the expansion and push
3599 * copies of each Line on to istk->expansion. Substitution of
3600 * parameter tokens and macro-local tokens doesn't get done
3601 * until the single-line macro substitution process; this is
3602 * because delaying them allows us to change the semantics
3603 * later through %rotate.
3605 * First, push an end marker on to istk->expansion, mark this
3606 * macro as in progress, and set up its invocation-specific
3607 * variables.
3609 ll = nasm_malloc(sizeof(Line));
3610 ll->next = istk->expansion;
3611 ll->finishes = m;
3612 ll->first = NULL;
3613 istk->expansion = ll;
3615 m->in_progress = true;
3616 m->params = params;
3617 m->iline = tline;
3618 m->nparam = nparam;
3619 m->rotate = 0;
3620 m->paramlen = paramlen;
3621 m->unique = unique++;
3622 m->lineno = 0;
3624 m->next_active = istk->mstk;
3625 istk->mstk = m;
3627 for (l = m->expansion; l; l = l->next) {
3628 Token **tail;
3630 ll = nasm_malloc(sizeof(Line));
3631 ll->finishes = NULL;
3632 ll->next = istk->expansion;
3633 istk->expansion = ll;
3634 tail = &ll->first;
3636 for (t = l->first; t; t = t->next) {
3637 Token *x = t;
3638 if (t->type == TOK_PREPROC_ID &&
3639 t->text[1] == '0' && t->text[2] == '0') {
3640 dont_prepend = -1;
3641 x = label;
3642 if (!x)
3643 continue;
3645 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3646 tail = &tt->next;
3648 *tail = NULL;
3652 * If we had a label, push it on as the first line of
3653 * the macro expansion.
3655 if (label) {
3656 if (dont_prepend < 0)
3657 free_tlist(startline);
3658 else {
3659 ll = nasm_malloc(sizeof(Line));
3660 ll->finishes = NULL;
3661 ll->next = istk->expansion;
3662 istk->expansion = ll;
3663 ll->first = startline;
3664 if (!dont_prepend) {
3665 while (label->next)
3666 label = label->next;
3667 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3672 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3674 return 1;
3678 * Since preprocessor always operate only on the line that didn't
3679 * arrived yet, we should always use ERR_OFFBY1. Also since user
3680 * won't want to see same error twice (preprocessing is done once
3681 * per pass) we will want to show errors only during pass one.
3683 static void error(int severity, const char *fmt, ...)
3685 va_list arg;
3686 char buff[1024];
3688 /* If we're in a dead branch of IF or something like it, ignore the error */
3689 if (istk && istk->conds && !emitting(istk->conds->state))
3690 return;
3692 va_start(arg, fmt);
3693 vsnprintf(buff, sizeof(buff), fmt, arg);
3694 va_end(arg);
3696 if (istk && istk->mstk && istk->mstk->name)
3697 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3698 istk->mstk->lineno, buff);
3699 else
3700 _error(severity | ERR_PASS1, "%s", buff);
3703 static void
3704 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3705 ListGen * listgen)
3707 _error = errfunc;
3708 cstk = NULL;
3709 istk = nasm_malloc(sizeof(Include));
3710 istk->next = NULL;
3711 istk->conds = NULL;
3712 istk->expansion = NULL;
3713 istk->mstk = NULL;
3714 istk->fp = fopen(file, "r");
3715 istk->fname = NULL;
3716 src_set_fname(nasm_strdup(file));
3717 src_set_linnum(0);
3718 istk->lineinc = 1;
3719 if (!istk->fp)
3720 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3721 file);
3722 defining = NULL;
3723 init_macros();
3724 unique = 0;
3725 if (tasm_compatible_mode) {
3726 stdmacpos = stdmac;
3727 } else {
3728 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3730 any_extrastdmac = (extrastdmac != NULL);
3731 list = listgen;
3732 evaluate = eval;
3733 pass = apass;
3736 static char *pp_getline(void)
3738 char *line;
3739 Token *tline;
3741 while (1) {
3743 * Fetch a tokenized line, either from the macro-expansion
3744 * buffer or from the input file.
3746 tline = NULL;
3747 while (istk->expansion && istk->expansion->finishes) {
3748 Line *l = istk->expansion;
3749 if (!l->finishes->name && l->finishes->in_progress > 1) {
3750 Line *ll;
3753 * This is a macro-end marker for a macro with no
3754 * name, which means it's not really a macro at all
3755 * but a %rep block, and the `in_progress' field is
3756 * more than 1, meaning that we still need to
3757 * repeat. (1 means the natural last repetition; 0
3758 * means termination by %exitrep.) We have
3759 * therefore expanded up to the %endrep, and must
3760 * push the whole block on to the expansion buffer
3761 * again. We don't bother to remove the macro-end
3762 * marker: we'd only have to generate another one
3763 * if we did.
3765 l->finishes->in_progress--;
3766 for (l = l->finishes->expansion; l; l = l->next) {
3767 Token *t, *tt, **tail;
3769 ll = nasm_malloc(sizeof(Line));
3770 ll->next = istk->expansion;
3771 ll->finishes = NULL;
3772 ll->first = NULL;
3773 tail = &ll->first;
3775 for (t = l->first; t; t = t->next) {
3776 if (t->text || t->type == TOK_WHITESPACE) {
3777 tt = *tail =
3778 new_Token(NULL, t->type, t->text, 0);
3779 tail = &tt->next;
3783 istk->expansion = ll;
3785 } else {
3787 * Check whether a `%rep' was started and not ended
3788 * within this macro expansion. This can happen and
3789 * should be detected. It's a fatal error because
3790 * I'm too confused to work out how to recover
3791 * sensibly from it.
3793 if (defining) {
3794 if (defining->name)
3795 error(ERR_PANIC,
3796 "defining with name in expansion");
3797 else if (istk->mstk->name)
3798 error(ERR_FATAL,
3799 "`%%rep' without `%%endrep' within"
3800 " expansion of macro `%s'",
3801 istk->mstk->name);
3805 * FIXME: investigate the relationship at this point between
3806 * istk->mstk and l->finishes
3809 MMacro *m = istk->mstk;
3810 istk->mstk = m->next_active;
3811 if (m->name) {
3813 * This was a real macro call, not a %rep, and
3814 * therefore the parameter information needs to
3815 * be freed.
3817 nasm_free(m->params);
3818 free_tlist(m->iline);
3819 nasm_free(m->paramlen);
3820 l->finishes->in_progress = false;
3821 } else
3822 free_mmacro(m);
3824 istk->expansion = l->next;
3825 nasm_free(l);
3826 list->downlevel(LIST_MACRO);
3829 while (1) { /* until we get a line we can use */
3831 if (istk->expansion) { /* from a macro expansion */
3832 char *p;
3833 Line *l = istk->expansion;
3834 if (istk->mstk)
3835 istk->mstk->lineno++;
3836 tline = l->first;
3837 istk->expansion = l->next;
3838 nasm_free(l);
3839 p = detoken(tline, false);
3840 list->line(LIST_MACRO, p);
3841 nasm_free(p);
3842 break;
3844 line = read_line();
3845 if (line) { /* from the current input file */
3846 line = prepreproc(line);
3847 tline = tokenize(line);
3848 nasm_free(line);
3849 break;
3852 * The current file has ended; work down the istk
3855 Include *i = istk;
3856 fclose(i->fp);
3857 if (i->conds)
3858 error(ERR_FATAL,
3859 "expected `%%endif' before end of file");
3860 /* only set line and file name if there's a next node */
3861 if (i->next) {
3862 src_set_linnum(i->lineno);
3863 nasm_free(src_set_fname(i->fname));
3865 istk = i->next;
3866 list->downlevel(LIST_INCLUDE);
3867 nasm_free(i);
3868 if (!istk)
3869 return NULL;
3874 * We must expand MMacro parameters and MMacro-local labels
3875 * _before_ we plunge into directive processing, to cope
3876 * with things like `%define something %1' such as STRUC
3877 * uses. Unless we're _defining_ a MMacro, in which case
3878 * those tokens should be left alone to go into the
3879 * definition; and unless we're in a non-emitting
3880 * condition, in which case we don't want to meddle with
3881 * anything.
3883 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3884 tline = expand_mmac_params(tline);
3887 * Check the line to see if it's a preprocessor directive.
3889 if (do_directive(tline) == DIRECTIVE_FOUND) {
3890 continue;
3891 } else if (defining) {
3893 * We're defining a multi-line macro. We emit nothing
3894 * at all, and just
3895 * shove the tokenized line on to the macro definition.
3897 Line *l = nasm_malloc(sizeof(Line));
3898 l->next = defining->expansion;
3899 l->first = tline;
3900 l->finishes = false;
3901 defining->expansion = l;
3902 continue;
3903 } else if (istk->conds && !emitting(istk->conds->state)) {
3905 * We're in a non-emitting branch of a condition block.
3906 * Emit nothing at all, not even a blank line: when we
3907 * emerge from the condition we'll give a line-number
3908 * directive so we keep our place correctly.
3910 free_tlist(tline);
3911 continue;
3912 } else if (istk->mstk && !istk->mstk->in_progress) {
3914 * We're in a %rep block which has been terminated, so
3915 * we're walking through to the %endrep without
3916 * emitting anything. Emit nothing at all, not even a
3917 * blank line: when we emerge from the %rep block we'll
3918 * give a line-number directive so we keep our place
3919 * correctly.
3921 free_tlist(tline);
3922 continue;
3923 } else {
3924 tline = expand_smacro(tline);
3925 if (!expand_mmacro(tline)) {
3927 * De-tokenize the line again, and emit it.
3929 line = detoken(tline, true);
3930 free_tlist(tline);
3931 break;
3932 } else {
3933 continue; /* expand_mmacro calls free_tlist */
3938 return line;
3941 static void pp_cleanup(int pass)
3943 if (defining) {
3944 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3945 defining->name);
3946 free_mmacro(defining);
3948 while (cstk)
3949 ctx_pop();
3950 free_macros();
3951 while (istk) {
3952 Include *i = istk;
3953 istk = istk->next;
3954 fclose(i->fp);
3955 nasm_free(i->fname);
3956 nasm_free(i);
3958 while (cstk)
3959 ctx_pop();
3960 if (pass == 0) {
3961 free_llist(predef);
3962 delete_Blocks();
3966 void pp_include_path(char *path)
3968 IncPath *i;
3970 i = nasm_malloc(sizeof(IncPath));
3971 i->path = path ? nasm_strdup(path) : NULL;
3972 i->next = NULL;
3974 if (ipath != NULL) {
3975 IncPath *j = ipath;
3976 while (j->next != NULL)
3977 j = j->next;
3978 j->next = i;
3979 } else {
3980 ipath = i;
3985 * added by alexfru:
3987 * This function is used to "export" the include paths, e.g.
3988 * the paths specified in the '-I' command switch.
3989 * The need for such exporting is due to the 'incbin' directive,
3990 * which includes raw binary files (unlike '%include', which
3991 * includes text source files). It would be real nice to be
3992 * able to specify paths to search for incbin'ned files also.
3993 * So, this is a simple workaround.
3995 * The function use is simple:
3997 * The 1st call (with NULL argument) returns a pointer to the 1st path
3998 * (char** type) or NULL if none include paths available.
4000 * All subsequent calls take as argument the value returned by this
4001 * function last. The return value is either the next path
4002 * (char** type) or NULL if the end of the paths list is reached.
4004 * It is maybe not the best way to do things, but I didn't want
4005 * to export too much, just one or two functions and no types or
4006 * variables exported.
4008 * Can't say I like the current situation with e.g. this path list either,
4009 * it seems to be never deallocated after creation...
4011 char **pp_get_include_path_ptr(char **pPrevPath)
4013 /* This macro returns offset of a member of a structure */
4014 #define GetMemberOffset(StructType,MemberName)\
4015 ((size_t)&((StructType*)0)->MemberName)
4016 IncPath *i;
4018 if (pPrevPath == NULL) {
4019 if (ipath != NULL)
4020 return &ipath->path;
4021 else
4022 return NULL;
4024 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4025 i = i->next;
4026 if (i != NULL)
4027 return &i->path;
4028 else
4029 return NULL;
4030 #undef GetMemberOffset
4033 void pp_pre_include(char *fname)
4035 Token *inc, *space, *name;
4036 Line *l;
4038 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4039 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4040 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4042 l = nasm_malloc(sizeof(Line));
4043 l->next = predef;
4044 l->first = inc;
4045 l->finishes = false;
4046 predef = l;
4049 void pp_pre_define(char *definition)
4051 Token *def, *space;
4052 Line *l;
4053 char *equals;
4055 equals = strchr(definition, '=');
4056 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4057 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4058 if (equals)
4059 *equals = ' ';
4060 space->next = tokenize(definition);
4061 if (equals)
4062 *equals = '=';
4064 l = nasm_malloc(sizeof(Line));
4065 l->next = predef;
4066 l->first = def;
4067 l->finishes = false;
4068 predef = l;
4071 void pp_pre_undefine(char *definition)
4073 Token *def, *space;
4074 Line *l;
4076 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4077 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4078 space->next = tokenize(definition);
4080 l = nasm_malloc(sizeof(Line));
4081 l->next = predef;
4082 l->first = def;
4083 l->finishes = false;
4084 predef = l;
4088 * Added by Keith Kanios:
4090 * This function is used to assist with "runtime" preprocessor
4091 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4093 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4094 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4097 void pp_runtime(char *definition)
4099 Token *def;
4101 def = tokenize(definition);
4102 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4103 free_tlist(def);
4107 void pp_extra_stdmac(const char **macros)
4109 extrastdmac = macros;
4112 static void make_tok_num(Token * tok, int64_t val)
4114 char numbuf[20];
4115 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4116 tok->text = nasm_strdup(numbuf);
4117 tok->type = TOK_NUMBER;
4120 Preproc nasmpp = {
4121 pp_reset,
4122 pp_getline,
4123 pp_cleanup