tokhash.pl: fix comment
[nasm/autotest.git] / preproc.c
blob8598beed1570aba308a2b0f35c8d353c4c7784be
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include "compiler.h"
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51 #include "stdscan.h"
52 #include "tokens.h"
53 #include "tables.h"
55 typedef struct SMacro SMacro;
56 typedef struct MMacro MMacro;
57 typedef struct Context Context;
58 typedef struct Token Token;
59 typedef struct Blocks Blocks;
60 typedef struct Line Line;
61 typedef struct Include Include;
62 typedef struct Cond Cond;
63 typedef struct IncPath IncPath;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
77 struct SMacro {
78 SMacro *next;
79 char *name;
80 bool casesense;
81 bool in_progress;
82 unsigned int nparam;
83 Token *expansion;
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
103 struct MMacro {
104 MMacro *next;
105 char *name;
106 int nparam_min, nparam_max;
107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
110 int64_t in_progress;
111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
114 Line *expansion;
116 MMacro *next_active;
117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
120 unsigned int nparam, rotate;
121 int *paramlen;
122 uint64_t unique;
123 int lineno; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
129 struct Context {
130 Context *next;
131 struct hash_table *localmac;
132 char *name;
133 uint32_t number;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
155 enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
161 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
165 struct Token {
166 Token *next;
167 char *text;
168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 enum pp_token_type type;
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
175 * lists of Tokens.
177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
192 * the line is blank.
194 struct Line {
195 Line *next;
196 MMacro *finishes;
197 Token *first;
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
204 struct Include {
205 Include *next;
206 FILE *fp;
207 Cond *conds;
208 Line *expansion;
209 char *fname;
210 int lineno, lineinc;
211 MMacro *mstk; /* stack of active macros/reps */
215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
219 struct IncPath {
220 IncPath *next;
221 char *path;
225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
231 struct Cond {
232 Cond *next;
233 int state;
235 enum {
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
244 COND_IF_TRUE, COND_IF_FALSE,
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
250 COND_ELSE_TRUE, COND_ELSE_FALSE,
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
259 COND_NEVER
261 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
264 * These defines are used as the possible return values for do_directive
266 #define NO_DIRECTIVE_FOUND 0
267 #define DIRECTIVE_FOUND 1
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
275 static const char * const conditions[] = {
276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
280 enum pp_conds {
281 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
282 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
283 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
284 c_none = -1
286 static const enum pp_conds inverse_ccs[] = {
287 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
288 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
289 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
293 * Directive names.
295 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
296 static int is_condition(enum preproc_token arg)
298 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
301 /* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
307 enum {
308 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
309 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
312 static const char * const tasm_directives[] = {
313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
317 static int StackSize = 4;
318 static char *StackPointer = "ebp";
319 static int ArgOffset = 8;
320 static int LocalOffset = 0;
322 static Context *cstk;
323 static Include *istk;
324 static IncPath *ipath = NULL;
326 static efunc _error; /* Pointer to client-provided error reporting function */
327 static evalfunc evaluate;
329 static int pass; /* HACK: pass 0 = generate dependencies only */
331 static uint64_t unique; /* unique identifier numbers */
333 static Line *predef = NULL;
335 static ListGen *list;
338 * The current set of multi-line macros we have defined.
340 static struct hash_table *mmacros;
343 * The current set of single-line macros we have defined.
345 static struct hash_table *smacros;
348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
351 static MMacro *defining;
354 * The number of macro parameters to allocate space for at a time.
356 #define PARAM_DELTA 16
359 * The standard macro set: defined in macros.c in the array nasm_stdmac.
360 * This gives our position in the macro set, when we're processing it.
362 static const char * const *stdmacpos;
365 * The extra standard macros that come from the object format, if
366 * any.
368 static const char * const *extrastdmac = NULL;
369 bool any_extrastdmac;
372 * Tokens are allocated in blocks to improve speed
374 #define TOKEN_BLOCKSIZE 4096
375 static Token *freeTokens = NULL;
376 struct Blocks {
377 Blocks *next;
378 void *chunk;
381 static Blocks blocks = { NULL, NULL };
384 * Forward declarations.
386 static Token *expand_mmac_params(Token * tline);
387 static Token *expand_smacro(Token * tline);
388 static Token *expand_id(Token * tline);
389 static Context *get_ctx(char *name, bool all_contexts);
390 static void make_tok_num(Token * tok, int64_t val);
391 static void error(int severity, const char *fmt, ...);
392 static void *new_Block(size_t size);
393 static void delete_Blocks(void);
394 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
395 static Token *delete_Token(Token * t);
398 * Macros for safe checking of token pointers, avoid *(NULL)
400 #define tok_type_(x,t) ((x) && (x)->type == (t))
401 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
402 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
403 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
405 /* Handle TASM specific directives, which do not contain a % in
406 * front of them. We do it here because I could not find any other
407 * place to do it for the moment, and it is a hack (ideally it would
408 * be nice to be able to use the NASM pre-processor to do it).
410 static char *check_tasm_directive(char *line)
412 int32_t i, j, k, m, len;
413 char *p = line, *oldline, oldchar;
415 /* Skip whitespace */
416 while (isspace(*p) && *p != 0)
417 p++;
419 /* Binary search for the directive name */
420 i = -1;
421 j = elements(tasm_directives);
422 len = 0;
423 while (!isspace(p[len]) && p[len] != 0)
424 len++;
425 if (len) {
426 oldchar = p[len];
427 p[len] = 0;
428 while (j - i > 1) {
429 k = (j + i) / 2;
430 m = nasm_stricmp(p, tasm_directives[k]);
431 if (m == 0) {
432 /* We have found a directive, so jam a % in front of it
433 * so that NASM will then recognise it as one if it's own.
435 p[len] = oldchar;
436 len = strlen(p);
437 oldline = line;
438 line = nasm_malloc(len + 2);
439 line[0] = '%';
440 if (k == TM_IFDIFI) {
441 /* NASM does not recognise IFDIFI, so we convert it to
442 * %ifdef BOGUS. This is not used in NASM comaptible
443 * code, but does need to parse for the TASM macro
444 * package.
446 strcpy(line + 1, "ifdef BOGUS");
447 } else {
448 memcpy(line + 1, p, len + 1);
450 nasm_free(oldline);
451 return line;
452 } else if (m < 0) {
453 j = k;
454 } else
455 i = k;
457 p[len] = oldchar;
459 return line;
463 * The pre-preprocessing stage... This function translates line
464 * number indications as they emerge from GNU cpp (`# lineno "file"
465 * flags') into NASM preprocessor line number indications (`%line
466 * lineno file').
468 static char *prepreproc(char *line)
470 int lineno, fnlen;
471 char *fname, *oldline;
473 if (line[0] == '#' && line[1] == ' ') {
474 oldline = line;
475 fname = oldline + 2;
476 lineno = atoi(fname);
477 fname += strspn(fname, "0123456789 ");
478 if (*fname == '"')
479 fname++;
480 fnlen = strcspn(fname, "\"");
481 line = nasm_malloc(20 + fnlen);
482 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
483 nasm_free(oldline);
485 if (tasm_compatible_mode)
486 return check_tasm_directive(line);
487 return line;
491 * Free a linked list of tokens.
493 static void free_tlist(Token * list)
495 while (list) {
496 list = delete_Token(list);
501 * Free a linked list of lines.
503 static void free_llist(Line * list)
505 Line *l;
506 while (list) {
507 l = list;
508 list = list->next;
509 free_tlist(l->first);
510 nasm_free(l);
515 * Free an MMacro
517 static void free_mmacro(MMacro * m)
519 nasm_free(m->name);
520 free_tlist(m->dlist);
521 nasm_free(m->defaults);
522 free_llist(m->expansion);
523 nasm_free(m);
527 * Free all currently defined macros, and free the hash tables
529 static void free_smacro_table(struct hash_table *smt)
531 SMacro *s;
532 const char *key;
533 struct hash_tbl_node *it = NULL;
535 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
536 nasm_free((void *)key);
537 while (s) {
538 SMacro *ns = s->next;
539 nasm_free(s->name);
540 free_tlist(s->expansion);
541 nasm_free(s);
542 s = ns;
545 hash_free(smt);
548 static void free_mmacro_table(struct hash_table *mmt)
550 MMacro *m;
551 const char *key;
552 struct hash_tbl_node *it = NULL;
554 it = NULL;
555 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
556 nasm_free((void *)key);
557 while (m) {
558 MMacro *nm = m->next;
559 free_mmacro(m);
560 m = nm;
563 hash_free(mmt);
566 static void free_macros(void)
568 free_smacro_table(smacros);
569 free_mmacro_table(mmacros);
573 * Initialize the hash tables
575 static void init_macros(void)
577 smacros = hash_init(HASH_LARGE);
578 mmacros = hash_init(HASH_LARGE);
582 * Pop the context stack.
584 static void ctx_pop(void)
586 Context *c = cstk;
588 cstk = cstk->next;
589 free_smacro_table(c->localmac);
590 nasm_free(c->name);
591 nasm_free(c);
595 * Search for a key in the hash index; adding it if necessary
596 * (in which case we initialize the data pointer to NULL.)
598 static void **
599 hash_findi_add(struct hash_table *hash, const char *str)
601 struct hash_insert hi;
602 void **r;
603 char *strx;
605 r = hash_findi(hash, str, &hi);
606 if (r)
607 return r;
609 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
610 return hash_add(&hi, strx, NULL);
614 * Like hash_findi, but returns the data element rather than a pointer
615 * to it. Used only when not adding a new element, hence no third
616 * argument.
618 static void *
619 hash_findix(struct hash_table *hash, const char *str)
621 void **p;
623 p = hash_findi(hash, str, NULL);
624 return p ? *p : NULL;
627 #define BUF_DELTA 512
629 * Read a line from the top file in istk, handling multiple CR/LFs
630 * at the end of the line read, and handling spurious ^Zs. Will
631 * return lines from the standard macro set if this has not already
632 * been done.
634 static char *read_line(void)
636 char *buffer, *p, *q;
637 int bufsize, continued_count;
639 if (stdmacpos) {
640 if (*stdmacpos) {
641 char *ret = nasm_strdup(*stdmacpos++);
642 if (!*stdmacpos && any_extrastdmac) {
643 stdmacpos = extrastdmac;
644 any_extrastdmac = false;
645 return ret;
648 * Nasty hack: here we push the contents of `predef' on
649 * to the top-level expansion stack, since this is the
650 * most convenient way to implement the pre-include and
651 * pre-define features.
653 if (!*stdmacpos) {
654 Line *pd, *l;
655 Token *head, **tail, *t;
657 for (pd = predef; pd; pd = pd->next) {
658 head = NULL;
659 tail = &head;
660 for (t = pd->first; t; t = t->next) {
661 *tail = new_Token(NULL, t->type, t->text, 0);
662 tail = &(*tail)->next;
664 l = nasm_malloc(sizeof(Line));
665 l->next = istk->expansion;
666 l->first = head;
667 l->finishes = false;
668 istk->expansion = l;
671 return ret;
672 } else {
673 stdmacpos = NULL;
677 bufsize = BUF_DELTA;
678 buffer = nasm_malloc(BUF_DELTA);
679 p = buffer;
680 continued_count = 0;
681 while (1) {
682 q = fgets(p, bufsize - (p - buffer), istk->fp);
683 if (!q)
684 break;
685 p += strlen(p);
686 if (p > buffer && p[-1] == '\n') {
687 /* Convert backslash-CRLF line continuation sequences into
688 nothing at all (for DOS and Windows) */
689 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
690 p -= 3;
691 *p = 0;
692 continued_count++;
694 /* Also convert backslash-LF line continuation sequences into
695 nothing at all (for Unix) */
696 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
697 p -= 2;
698 *p = 0;
699 continued_count++;
700 } else {
701 break;
704 if (p - buffer > bufsize - 10) {
705 int32_t offset = p - buffer;
706 bufsize += BUF_DELTA;
707 buffer = nasm_realloc(buffer, bufsize);
708 p = buffer + offset; /* prevent stale-pointer problems */
712 if (!q && p == buffer) {
713 nasm_free(buffer);
714 return NULL;
717 src_set_linnum(src_get_linnum() + istk->lineinc +
718 (continued_count * istk->lineinc));
721 * Play safe: remove CRs as well as LFs, if any of either are
722 * present at the end of the line.
724 while (--p >= buffer && (*p == '\n' || *p == '\r'))
725 *p = '\0';
728 * Handle spurious ^Z, which may be inserted into source files
729 * by some file transfer utilities.
731 buffer[strcspn(buffer, "\032")] = '\0';
733 list->line(LIST_READ, buffer);
735 return buffer;
739 * Tokenize a line of text. This is a very simple process since we
740 * don't need to parse the value out of e.g. numeric tokens: we
741 * simply split one string into many.
743 static Token *tokenize(char *line)
745 char *p = line;
746 enum pp_token_type type;
747 Token *list = NULL;
748 Token *t, **tail = &list;
750 while (*line) {
751 p = line;
752 if (*p == '%') {
753 p++;
754 if (isdigit(*p) ||
755 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
756 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
757 do {
758 p++;
760 while (isdigit(*p));
761 type = TOK_PREPROC_ID;
762 } else if (*p == '{') {
763 p++;
764 while (*p && *p != '}') {
765 p[-1] = *p;
766 p++;
768 p[-1] = '\0';
769 if (*p)
770 p++;
771 type = TOK_PREPROC_ID;
772 } else if (*p == '?') {
773 type = TOK_PREPROC_Q; /* %? */
774 p++;
775 if (*p == '?') {
776 type = TOK_PREPROC_QQ; /* %?? */
777 p++;
779 } else if (isidchar(*p) ||
780 ((*p == '!' || *p == '%' || *p == '$') &&
781 isidchar(p[1]))) {
782 do {
783 p++;
785 while (isidchar(*p));
786 type = TOK_PREPROC_ID;
787 } else {
788 type = TOK_OTHER;
789 if (*p == '%')
790 p++;
792 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
793 type = TOK_ID;
794 p++;
795 while (*p && isidchar(*p))
796 p++;
797 } else if (*p == '\'' || *p == '"') {
799 * A string token.
801 char c = *p;
802 p++;
803 type = TOK_STRING;
804 while (*p && *p != c)
805 p++;
807 if (*p) {
808 p++;
809 } else {
810 error(ERR_WARNING, "unterminated string");
811 /* Handling unterminated strings by UNV */
812 /* type = -1; */
814 } else if (isnumstart(*p)) {
815 bool is_hex = false;
816 bool is_float = false;
817 bool has_e = false;
818 char c, *r;
821 * A numeric token.
824 if (*p == '$') {
825 p++;
826 is_hex = true;
829 for (;;) {
830 c = *p++;
832 if (!is_hex && (c == 'e' || c == 'E')) {
833 has_e = true;
834 if (*p == '+' || *p == '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
837 p++;
838 is_float = true;
840 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
841 is_hex = true;
842 } else if (c == 'P' || c == 'p') {
843 is_float = true;
844 if (*p == '+' || *p == '-')
845 p++;
846 } else if (isnumchar(c) || c == '_')
847 ; /* just advance */
848 else if (c == '.') {
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
857 r = p;
858 while (*r == '_')
859 r++;
861 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
862 (!is_hex && (*r == 'e' || *r == 'E')) ||
863 (*r == 'p' || *r == 'P')) {
864 p = r;
865 is_float = true;
866 } else
867 break; /* Terminate the token */
868 } else
869 break;
871 p--; /* Point to first character beyond number */
873 if (has_e && !is_hex) {
874 /* 1e13 is floating-point, but 1e13h is not */
875 is_float = true;
878 type = is_float ? TOK_FLOAT : TOK_NUMBER;
879 } else if (isspace(*p)) {
880 type = TOK_WHITESPACE;
881 p++;
882 while (*p && isspace(*p))
883 p++;
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
889 if (!*p || *p == ';') {
890 type = TOK_COMMENT;
891 while (*p)
892 p++;
894 } else if (*p == ';') {
895 type = TOK_COMMENT;
896 while (*p)
897 p++;
898 } else {
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
903 * else is a single-character operator.
905 type = TOK_OTHER;
906 if ((p[0] == '>' && p[1] == '>') ||
907 (p[0] == '<' && p[1] == '<') ||
908 (p[0] == '/' && p[1] == '/') ||
909 (p[0] == '<' && p[1] == '=') ||
910 (p[0] == '>' && p[1] == '=') ||
911 (p[0] == '=' && p[1] == '=') ||
912 (p[0] == '!' && p[1] == '=') ||
913 (p[0] == '<' && p[1] == '>') ||
914 (p[0] == '&' && p[1] == '&') ||
915 (p[0] == '|' && p[1] == '|') ||
916 (p[0] == '^' && p[1] == '^')) {
917 p++;
919 p++;
922 /* Handling unterminated string by UNV */
923 /*if (type == -1)
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
927 tail = &t->next;
929 else */
930 if (type != TOK_COMMENT) {
931 *tail = t = new_Token(NULL, type, line, p - line);
932 tail = &t->next;
934 line = p;
936 return list;
940 * this function allocates a new managed block of memory and
941 * returns a pointer to the block. The managed blocks are
942 * deleted only all at once by the delete_Blocks function.
944 static void *new_Block(size_t size)
946 Blocks *b = &blocks;
948 /* first, get to the end of the linked list */
949 while (b->next)
950 b = b->next;
951 /* now allocate the requested chunk */
952 b->chunk = nasm_malloc(size);
954 /* now allocate a new block for the next request */
955 b->next = nasm_malloc(sizeof(Blocks));
956 /* and initialize the contents of the new block */
957 b->next->next = NULL;
958 b->next->chunk = NULL;
959 return b->chunk;
963 * this function deletes all managed blocks of memory
965 static void delete_Blocks(void)
967 Blocks *a, *b = &blocks;
970 * keep in mind that the first block, pointed to by blocks
971 * is a static and not dynamically allocated, so we don't
972 * free it.
974 while (b) {
975 if (b->chunk)
976 nasm_free(b->chunk);
977 a = b;
978 b = b->next;
979 if (a != &blocks)
980 nasm_free(a);
985 * this function creates a new Token and passes a pointer to it
986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
989 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
991 Token *t;
992 int i;
994 if (freeTokens == NULL) {
995 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
996 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
997 freeTokens[i].next = &freeTokens[i + 1];
998 freeTokens[i].next = NULL;
1000 t = freeTokens;
1001 freeTokens = t->next;
1002 t->next = next;
1003 t->mac = NULL;
1004 t->type = type;
1005 if (type == TOK_WHITESPACE || text == NULL) {
1006 t->text = NULL;
1007 } else {
1008 if (txtlen == 0)
1009 txtlen = strlen(text);
1010 t->text = nasm_malloc(1 + txtlen);
1011 strncpy(t->text, text, txtlen);
1012 t->text[txtlen] = '\0';
1014 return t;
1017 static Token *delete_Token(Token * t)
1019 Token *next = t->next;
1020 nasm_free(t->text);
1021 t->next = freeTokens;
1022 freeTokens = t;
1023 return next;
1027 * Convert a line of tokens back into text.
1028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
1031 static char *detoken(Token * tlist, int expand_locals)
1033 Token *t;
1034 int len;
1035 char *line, *p;
1036 const char *q;
1038 len = 0;
1039 for (t = tlist; t; t = t->next) {
1040 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1041 char *p = getenv(t->text + 2);
1042 nasm_free(t->text);
1043 if (p)
1044 t->text = nasm_strdup(p);
1045 else
1046 t->text = NULL;
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals &&
1050 t->type == TOK_PREPROC_ID && t->text &&
1051 t->text[0] == '%' && t->text[1] == '$') {
1052 Context *ctx = get_ctx(t->text, false);
1053 if (ctx) {
1054 char buffer[40];
1055 char *p, *q = t->text + 2;
1057 q += strspn(q, "$");
1058 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1059 p = nasm_strcat(buffer, q);
1060 nasm_free(t->text);
1061 t->text = p;
1064 if (t->type == TOK_WHITESPACE) {
1065 len++;
1066 } else if (t->text) {
1067 len += strlen(t->text);
1070 p = line = nasm_malloc(len + 1);
1071 for (t = tlist; t; t = t->next) {
1072 if (t->type == TOK_WHITESPACE) {
1073 *p++ = ' ';
1074 } else if (t->text) {
1075 q = t->text;
1076 while (*q)
1077 *p++ = *q++;
1080 *p = '\0';
1081 return line;
1085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1088 * field.
1090 * FIX: This really needs to be unified with stdscan.
1092 static int ppscan(void *private_data, struct tokenval *tokval)
1094 Token **tlineptr = private_data;
1095 Token *tline;
1096 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1098 do {
1099 tline = *tlineptr;
1100 *tlineptr = tline ? tline->next : NULL;
1102 while (tline && (tline->type == TOK_WHITESPACE ||
1103 tline->type == TOK_COMMENT));
1105 if (!tline)
1106 return tokval->t_type = TOKEN_EOS;
1108 tokval->t_charptr = tline->text;
1110 if (tline->text[0] == '$' && !tline->text[1])
1111 return tokval->t_type = TOKEN_HERE;
1112 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1113 return tokval->t_type = TOKEN_BASE;
1115 if (tline->type == TOK_ID) {
1116 p = tokval->t_charptr = tline->text;
1117 if (p[0] == '$') {
1118 tokval->t_charptr++;
1119 return tokval->t_type = TOKEN_ID;
1122 for (r = p, s = ourcopy; *r; r++) {
1123 if (r >= p+MAX_KEYWORD)
1124 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1125 *s++ = tolower(*r);
1127 *s = '\0';
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy, tokval);
1133 if (tline->type == TOK_NUMBER) {
1134 bool rn_error;
1135 tokval->t_integer = readnum(tline->text, &rn_error);
1136 if (rn_error)
1137 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1138 tokval->t_charptr = tline->text;
1139 return tokval->t_type = TOKEN_NUM;
1142 if (tline->type == TOK_FLOAT) {
1143 return tokval->t_type = TOKEN_FLOAT;
1146 if (tline->type == TOK_STRING) {
1147 bool rn_warn;
1148 char q, *r;
1149 int l;
1151 r = tline->text;
1152 q = *r++;
1153 l = strlen(r);
1155 if (l == 0 || r[l - 1] != q)
1156 return tokval->t_type = TOKEN_ERRNUM;
1157 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1158 if (rn_warn)
1159 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1160 tokval->t_charptr = NULL;
1161 return tokval->t_type = TOKEN_NUM;
1164 if (tline->type == TOK_OTHER) {
1165 if (!strcmp(tline->text, "<<"))
1166 return tokval->t_type = TOKEN_SHL;
1167 if (!strcmp(tline->text, ">>"))
1168 return tokval->t_type = TOKEN_SHR;
1169 if (!strcmp(tline->text, "//"))
1170 return tokval->t_type = TOKEN_SDIV;
1171 if (!strcmp(tline->text, "%%"))
1172 return tokval->t_type = TOKEN_SMOD;
1173 if (!strcmp(tline->text, "=="))
1174 return tokval->t_type = TOKEN_EQ;
1175 if (!strcmp(tline->text, "<>"))
1176 return tokval->t_type = TOKEN_NE;
1177 if (!strcmp(tline->text, "!="))
1178 return tokval->t_type = TOKEN_NE;
1179 if (!strcmp(tline->text, "<="))
1180 return tokval->t_type = TOKEN_LE;
1181 if (!strcmp(tline->text, ">="))
1182 return tokval->t_type = TOKEN_GE;
1183 if (!strcmp(tline->text, "&&"))
1184 return tokval->t_type = TOKEN_DBL_AND;
1185 if (!strcmp(tline->text, "^^"))
1186 return tokval->t_type = TOKEN_DBL_XOR;
1187 if (!strcmp(tline->text, "||"))
1188 return tokval->t_type = TOKEN_DBL_OR;
1192 * We have no other options: just return the first character of
1193 * the token text.
1195 return tokval->t_type = tline->text[0];
1199 * Compare a string to the name of an existing macro; this is a
1200 * simple wrapper which calls either strcmp or nasm_stricmp
1201 * depending on the value of the `casesense' parameter.
1203 static int mstrcmp(const char *p, const char *q, bool casesense)
1205 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1209 * Return the Context structure associated with a %$ token. Return
1210 * NULL, having _already_ reported an error condition, if the
1211 * context stack isn't deep enough for the supplied number of $
1212 * signs.
1213 * If all_contexts == true, contexts that enclose current are
1214 * also scanned for such smacro, until it is found; if not -
1215 * only the context that directly results from the number of $'s
1216 * in variable's name.
1218 static Context *get_ctx(char *name, bool all_contexts)
1220 Context *ctx;
1221 SMacro *m;
1222 int i;
1224 if (!name || name[0] != '%' || name[1] != '$')
1225 return NULL;
1227 if (!cstk) {
1228 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1229 return NULL;
1232 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1233 ctx = ctx->next;
1234 /* i--; Lino - 02/25/02 */
1236 if (!ctx) {
1237 error(ERR_NONFATAL, "`%s': context stack is only"
1238 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1239 return NULL;
1241 if (!all_contexts)
1242 return ctx;
1244 do {
1245 /* Search for this smacro in found context */
1246 m = hash_findix(ctx->localmac, name);
1247 while (m) {
1248 if (!mstrcmp(m->name, name, m->casesense))
1249 return ctx;
1250 m = m->next;
1252 ctx = ctx->next;
1254 while (ctx);
1255 return NULL;
1259 * Open an include file. This routine must always return a valid
1260 * file pointer if it returns - it's responsible for throwing an
1261 * ERR_FATAL and bombing out completely if not. It should also try
1262 * the include path one by one until it finds the file or reaches
1263 * the end of the path.
1265 static FILE *inc_fopen(char *file)
1267 FILE *fp;
1268 char *prefix = "", *combine;
1269 IncPath *ip = ipath;
1270 static int namelen = 0;
1271 int len = strlen(file);
1273 while (1) {
1274 combine = nasm_malloc(strlen(prefix) + len + 1);
1275 strcpy(combine, prefix);
1276 strcat(combine, file);
1277 fp = fopen(combine, "r");
1278 if (pass == 0 && fp) {
1279 namelen += strlen(combine) + 1;
1280 if (namelen > 62) {
1281 printf(" \\\n ");
1282 namelen = 2;
1284 printf(" %s", combine);
1286 nasm_free(combine);
1287 if (fp)
1288 return fp;
1289 if (!ip)
1290 break;
1291 prefix = ip->path;
1292 ip = ip->next;
1294 if (!prefix) {
1295 /* -MG given and file not found */
1296 if (pass == 0) {
1297 namelen += strlen(file) + 1;
1298 if (namelen > 62) {
1299 printf(" \\\n ");
1300 namelen = 2;
1302 printf(" %s", file);
1304 return NULL;
1308 error(ERR_FATAL, "unable to open include file `%s'", file);
1309 return NULL; /* never reached - placate compilers */
1313 * Determine if we should warn on defining a single-line macro of
1314 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1315 * return true if _any_ single-line macro of that name is defined.
1316 * Otherwise, will return true if a single-line macro with either
1317 * `nparam' or no parameters is defined.
1319 * If a macro with precisely the right number of parameters is
1320 * defined, or nparam is -1, the address of the definition structure
1321 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1322 * is NULL, no action will be taken regarding its contents, and no
1323 * error will occur.
1325 * Note that this is also called with nparam zero to resolve
1326 * `ifdef'.
1328 * If you already know which context macro belongs to, you can pass
1329 * the context pointer as first parameter; if you won't but name begins
1330 * with %$ the context will be automatically computed. If all_contexts
1331 * is true, macro will be searched in outer contexts as well.
1333 static bool
1334 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1335 bool nocase)
1337 SMacro *m;
1339 if (ctx) {
1340 m = (SMacro *) hash_findix(ctx->localmac, name);
1341 } else if (name[0] == '%' && name[1] == '$') {
1342 if (cstk)
1343 ctx = get_ctx(name, false);
1344 if (!ctx)
1345 return false; /* got to return _something_ */
1346 m = (SMacro *) hash_findix(ctx->localmac, name);
1347 } else {
1348 m = (SMacro *) hash_findix(smacros, name);
1351 while (m) {
1352 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1353 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1354 if (defn) {
1355 if (nparam == (int) m->nparam || nparam == -1)
1356 *defn = m;
1357 else
1358 *defn = NULL;
1360 return true;
1362 m = m->next;
1365 return false;
1369 * Count and mark off the parameters in a multi-line macro call.
1370 * This is called both from within the multi-line macro expansion
1371 * code, and also to mark off the default parameters when provided
1372 * in a %macro definition line.
1374 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1376 int paramsize, brace;
1378 *nparam = paramsize = 0;
1379 *params = NULL;
1380 while (t) {
1381 if (*nparam >= paramsize) {
1382 paramsize += PARAM_DELTA;
1383 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1385 skip_white_(t);
1386 brace = false;
1387 if (tok_is_(t, "{"))
1388 brace = true;
1389 (*params)[(*nparam)++] = t;
1390 while (tok_isnt_(t, brace ? "}" : ","))
1391 t = t->next;
1392 if (t) { /* got a comma/brace */
1393 t = t->next;
1394 if (brace) {
1396 * Now we've found the closing brace, look further
1397 * for the comma.
1399 skip_white_(t);
1400 if (tok_isnt_(t, ",")) {
1401 error(ERR_NONFATAL,
1402 "braces do not enclose all of macro parameter");
1403 while (tok_isnt_(t, ","))
1404 t = t->next;
1406 if (t)
1407 t = t->next; /* eat the comma */
1414 * Determine whether one of the various `if' conditions is true or
1415 * not.
1417 * We must free the tline we get passed.
1419 static bool if_condition(Token * tline, enum preproc_token ct)
1421 enum pp_conditional i = PP_COND(ct);
1422 bool j;
1423 Token *t, *tt, **tptr, *origline;
1424 struct tokenval tokval;
1425 expr *evalresult;
1426 enum pp_token_type needtype;
1428 origline = tline;
1430 switch (i) {
1431 case PPC_IFCTX:
1432 j = false; /* have we matched yet? */
1433 while (cstk && tline) {
1434 skip_white_(tline);
1435 if (!tline || tline->type != TOK_ID) {
1436 error(ERR_NONFATAL,
1437 "`%s' expects context identifiers", pp_directives[ct]);
1438 free_tlist(origline);
1439 return -1;
1441 if (!nasm_stricmp(tline->text, cstk->name))
1442 j = true;
1443 tline = tline->next;
1445 break;
1447 case PPC_IFDEF:
1448 j = false; /* have we matched yet? */
1449 while (tline) {
1450 skip_white_(tline);
1451 if (!tline || (tline->type != TOK_ID &&
1452 (tline->type != TOK_PREPROC_ID ||
1453 tline->text[1] != '$'))) {
1454 error(ERR_NONFATAL,
1455 "`%s' expects macro identifiers", pp_directives[ct]);
1456 goto fail;
1458 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1459 j = true;
1460 tline = tline->next;
1462 break;
1464 case PPC_IFIDN:
1465 case PPC_IFIDNI:
1466 tline = expand_smacro(tline);
1467 t = tt = tline;
1468 while (tok_isnt_(tt, ","))
1469 tt = tt->next;
1470 if (!tt) {
1471 error(ERR_NONFATAL,
1472 "`%s' expects two comma-separated arguments",
1473 pp_directives[ct]);
1474 goto fail;
1476 tt = tt->next;
1477 j = true; /* assume equality unless proved not */
1478 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1479 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1480 error(ERR_NONFATAL, "`%s': more than one comma on line",
1481 pp_directives[ct]);
1482 goto fail;
1484 if (t->type == TOK_WHITESPACE) {
1485 t = t->next;
1486 continue;
1488 if (tt->type == TOK_WHITESPACE) {
1489 tt = tt->next;
1490 continue;
1492 if (tt->type != t->type) {
1493 j = false; /* found mismatching tokens */
1494 break;
1496 /* Unify surrounding quotes for strings */
1497 if (t->type == TOK_STRING) {
1498 tt->text[0] = t->text[0];
1499 tt->text[strlen(tt->text) - 1] = t->text[0];
1501 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1502 j = false; /* found mismatching tokens */
1503 break;
1506 t = t->next;
1507 tt = tt->next;
1509 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1510 j = false; /* trailing gunk on one end or other */
1511 break;
1513 case PPC_IFMACRO:
1515 bool found = false;
1516 MMacro searching, *mmac;
1518 tline = tline->next;
1519 skip_white_(tline);
1520 tline = expand_id(tline);
1521 if (!tok_type_(tline, TOK_ID)) {
1522 error(ERR_NONFATAL,
1523 "`%s' expects a macro name", pp_directives[ct]);
1524 goto fail;
1526 searching.name = nasm_strdup(tline->text);
1527 searching.casesense = true;
1528 searching.plus = false;
1529 searching.nolist = false;
1530 searching.in_progress = 0;
1531 searching.rep_nest = NULL;
1532 searching.nparam_min = 0;
1533 searching.nparam_max = INT_MAX;
1534 tline = expand_smacro(tline->next);
1535 skip_white_(tline);
1536 if (!tline) {
1537 } else if (!tok_type_(tline, TOK_NUMBER)) {
1538 error(ERR_NONFATAL,
1539 "`%s' expects a parameter count or nothing",
1540 pp_directives[ct]);
1541 } else {
1542 searching.nparam_min = searching.nparam_max =
1543 readnum(tline->text, &j);
1544 if (j)
1545 error(ERR_NONFATAL,
1546 "unable to parse parameter count `%s'",
1547 tline->text);
1549 if (tline && tok_is_(tline->next, "-")) {
1550 tline = tline->next->next;
1551 if (tok_is_(tline, "*"))
1552 searching.nparam_max = INT_MAX;
1553 else if (!tok_type_(tline, TOK_NUMBER))
1554 error(ERR_NONFATAL,
1555 "`%s' expects a parameter count after `-'",
1556 pp_directives[ct]);
1557 else {
1558 searching.nparam_max = readnum(tline->text, &j);
1559 if (j)
1560 error(ERR_NONFATAL,
1561 "unable to parse parameter count `%s'",
1562 tline->text);
1563 if (searching.nparam_min > searching.nparam_max)
1564 error(ERR_NONFATAL,
1565 "minimum parameter count exceeds maximum");
1568 if (tline && tok_is_(tline->next, "+")) {
1569 tline = tline->next;
1570 searching.plus = true;
1572 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1573 while (mmac) {
1574 if (!strcmp(mmac->name, searching.name) &&
1575 (mmac->nparam_min <= searching.nparam_max
1576 || searching.plus)
1577 && (searching.nparam_min <= mmac->nparam_max
1578 || mmac->plus)) {
1579 found = true;
1580 break;
1582 mmac = mmac->next;
1584 nasm_free(searching.name);
1585 j = found;
1586 break;
1589 case PPC_IFID:
1590 needtype = TOK_ID;
1591 goto iftype;
1592 case PPC_IFNUM:
1593 needtype = TOK_NUMBER;
1594 goto iftype;
1595 case PPC_IFSTR:
1596 needtype = TOK_STRING;
1597 goto iftype;
1599 iftype:
1600 t = tline = expand_smacro(tline);
1602 while (tok_type_(t, TOK_WHITESPACE) ||
1603 (needtype == TOK_NUMBER &&
1604 tok_type_(t, TOK_OTHER) &&
1605 (t->text[0] == '-' || t->text[0] == '+') &&
1606 !t->text[1]))
1607 t = t->next;
1609 j = tok_type_(t, needtype);
1610 break;
1612 case PPC_IFTOKEN:
1613 t = tline = expand_smacro(tline);
1614 while (tok_type_(t, TOK_WHITESPACE))
1615 t = t->next;
1617 j = false;
1618 if (t) {
1619 t = t->next; /* Skip the actual token */
1620 while (tok_type_(t, TOK_WHITESPACE))
1621 t = t->next;
1622 j = !t; /* Should be nothing left */
1624 break;
1626 case PPC_IFEMPTY:
1627 t = tline = expand_smacro(tline);
1628 while (tok_type_(t, TOK_WHITESPACE))
1629 t = t->next;
1631 j = !t; /* Should be empty */
1632 break;
1634 case PPC_IF:
1635 t = tline = expand_smacro(tline);
1636 tptr = &t;
1637 tokval.t_type = TOKEN_INVALID;
1638 evalresult = evaluate(ppscan, tptr, &tokval,
1639 NULL, pass | CRITICAL, error, NULL);
1640 if (!evalresult)
1641 return -1;
1642 if (tokval.t_type)
1643 error(ERR_WARNING,
1644 "trailing garbage after expression ignored");
1645 if (!is_simple(evalresult)) {
1646 error(ERR_NONFATAL,
1647 "non-constant value given to `%s'", pp_directives[ct]);
1648 goto fail;
1650 j = reloc_value(evalresult) != 0;
1651 return j;
1653 default:
1654 error(ERR_FATAL,
1655 "preprocessor directive `%s' not yet implemented",
1656 pp_directives[ct]);
1657 goto fail;
1660 free_tlist(origline);
1661 return j ^ PP_NEGATIVE(ct);
1663 fail:
1664 free_tlist(origline);
1665 return -1;
1669 * Expand macros in a string. Used in %error and %include directives.
1670 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1671 * The returned variable should ALWAYS be freed after usage.
1673 void expand_macros_in_string(char **p)
1675 Token *line = tokenize(*p);
1676 line = expand_smacro(line);
1677 *p = detoken(line, false);
1681 * Common code for defining an smacro
1683 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1684 int nparam, Token *expansion)
1686 SMacro *smac, **smhead;
1688 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1689 if (!smac) {
1690 error(ERR_WARNING,
1691 "single-line macro `%s' defined both with and"
1692 " without parameters", mname);
1694 /* Some instances of the old code considered this a failure,
1695 some others didn't. What is the right thing to do here? */
1696 free_tlist(expansion);
1697 return false; /* Failure */
1698 } else {
1700 * We're redefining, so we have to take over an
1701 * existing SMacro structure. This means freeing
1702 * what was already in it.
1704 nasm_free(smac->name);
1705 free_tlist(smac->expansion);
1707 } else {
1708 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1709 mname);
1710 smac = nasm_malloc(sizeof(SMacro));
1711 smac->next = *smhead;
1712 *smhead = smac;
1714 smac->name = nasm_strdup(mname);
1715 smac->casesense = casesense;
1716 smac->nparam = nparam;
1717 smac->expansion = expansion;
1718 smac->in_progress = false;
1719 return true; /* Success */
1723 * Undefine an smacro
1725 static void undef_smacro(Context *ctx, const char *mname)
1727 SMacro **smhead, *s, **sp;
1729 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
1731 if (smhead) {
1733 * We now have a macro name... go hunt for it.
1735 sp = smhead;
1736 while ((s = *sp) != NULL) {
1737 if (!mstrcmp(s->name, mname, s->casesense)) {
1738 *sp = s->next;
1739 nasm_free(s->name);
1740 free_tlist(s->expansion);
1741 nasm_free(s);
1742 } else {
1743 sp = &s->next;
1750 * Decode a size directive
1752 static int parse_size(const char *str) {
1753 static const char *size_names[] =
1754 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1755 static const int sizes[] =
1756 { 0, 1, 4, 16, 8, 10, 2, 32 };
1758 return sizes[bsii(str, size_names, elements(size_names))+1];
1762 * find and process preprocessor directive in passed line
1763 * Find out if a line contains a preprocessor directive, and deal
1764 * with it if so.
1766 * If a directive _is_ found, it is the responsibility of this routine
1767 * (and not the caller) to free_tlist() the line.
1769 * @param tline a pointer to the current tokeninzed line linked list
1770 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1773 static int do_directive(Token * tline)
1775 enum preproc_token i;
1776 int j;
1777 bool err;
1778 int nparam;
1779 bool nolist;
1780 bool casesense;
1781 int k, m;
1782 int offset;
1783 char *p, *mname;
1784 Include *inc;
1785 Context *ctx;
1786 Cond *cond;
1787 MMacro *mmac, **mmhead;
1788 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1789 Line *l;
1790 struct tokenval tokval;
1791 expr *evalresult;
1792 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1793 int64_t count;
1795 origline = tline;
1797 skip_white_(tline);
1798 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1799 (tline->text[1] == '%' || tline->text[1] == '$'
1800 || tline->text[1] == '!'))
1801 return NO_DIRECTIVE_FOUND;
1803 i = pp_token_hash(tline->text);
1806 * If we're in a non-emitting branch of a condition construct,
1807 * or walking to the end of an already terminated %rep block,
1808 * we should ignore all directives except for condition
1809 * directives.
1811 if (((istk->conds && !emitting(istk->conds->state)) ||
1812 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1813 return NO_DIRECTIVE_FOUND;
1817 * If we're defining a macro or reading a %rep block, we should
1818 * ignore all directives except for %macro/%imacro (which
1819 * generate an error), %endm/%endmacro, and (only if we're in a
1820 * %rep block) %endrep. If we're in a %rep block, another %rep
1821 * causes an error, so should be let through.
1823 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1824 i != PP_ENDMACRO && i != PP_ENDM &&
1825 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1826 return NO_DIRECTIVE_FOUND;
1829 switch (i) {
1830 case PP_INVALID:
1831 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1832 tline->text);
1833 return NO_DIRECTIVE_FOUND; /* didn't get it */
1835 case PP_STACKSIZE:
1836 /* Directive to tell NASM what the default stack size is. The
1837 * default is for a 16-bit stack, and this can be overriden with
1838 * %stacksize large.
1839 * the following form:
1841 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1843 tline = tline->next;
1844 if (tline && tline->type == TOK_WHITESPACE)
1845 tline = tline->next;
1846 if (!tline || tline->type != TOK_ID) {
1847 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1848 free_tlist(origline);
1849 return DIRECTIVE_FOUND;
1851 if (nasm_stricmp(tline->text, "flat") == 0) {
1852 /* All subsequent ARG directives are for a 32-bit stack */
1853 StackSize = 4;
1854 StackPointer = "ebp";
1855 ArgOffset = 8;
1856 LocalOffset = 0;
1857 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1858 /* All subsequent ARG directives are for a 64-bit stack */
1859 StackSize = 8;
1860 StackPointer = "rbp";
1861 ArgOffset = 8;
1862 LocalOffset = 0;
1863 } else if (nasm_stricmp(tline->text, "large") == 0) {
1864 /* All subsequent ARG directives are for a 16-bit stack,
1865 * far function call.
1867 StackSize = 2;
1868 StackPointer = "bp";
1869 ArgOffset = 4;
1870 LocalOffset = 0;
1871 } else if (nasm_stricmp(tline->text, "small") == 0) {
1872 /* All subsequent ARG directives are for a 16-bit stack,
1873 * far function call. We don't support near functions.
1875 StackSize = 2;
1876 StackPointer = "bp";
1877 ArgOffset = 6;
1878 LocalOffset = 0;
1879 } else {
1880 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1881 free_tlist(origline);
1882 return DIRECTIVE_FOUND;
1884 free_tlist(origline);
1885 return DIRECTIVE_FOUND;
1887 case PP_ARG:
1888 /* TASM like ARG directive to define arguments to functions, in
1889 * the following form:
1891 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1893 offset = ArgOffset;
1894 do {
1895 char *arg, directive[256];
1896 int size = StackSize;
1898 /* Find the argument name */
1899 tline = tline->next;
1900 if (tline && tline->type == TOK_WHITESPACE)
1901 tline = tline->next;
1902 if (!tline || tline->type != TOK_ID) {
1903 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1904 free_tlist(origline);
1905 return DIRECTIVE_FOUND;
1907 arg = tline->text;
1909 /* Find the argument size type */
1910 tline = tline->next;
1911 if (!tline || tline->type != TOK_OTHER
1912 || tline->text[0] != ':') {
1913 error(ERR_NONFATAL,
1914 "Syntax error processing `%%arg' directive");
1915 free_tlist(origline);
1916 return DIRECTIVE_FOUND;
1918 tline = tline->next;
1919 if (!tline || tline->type != TOK_ID) {
1920 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1921 free_tlist(origline);
1922 return DIRECTIVE_FOUND;
1925 /* Allow macro expansion of type parameter */
1926 tt = tokenize(tline->text);
1927 tt = expand_smacro(tt);
1928 size = parse_size(tt->text);
1929 if (!size) {
1930 error(ERR_NONFATAL,
1931 "Invalid size type for `%%arg' missing directive");
1932 free_tlist(tt);
1933 free_tlist(origline);
1934 return DIRECTIVE_FOUND;
1936 free_tlist(tt);
1938 /* Round up to even stack slots */
1939 size = (size+StackSize-1) & ~(StackSize-1);
1941 /* Now define the macro for the argument */
1942 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1943 arg, StackPointer, offset);
1944 do_directive(tokenize(directive));
1945 offset += size;
1947 /* Move to the next argument in the list */
1948 tline = tline->next;
1949 if (tline && tline->type == TOK_WHITESPACE)
1950 tline = tline->next;
1951 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1952 ArgOffset = offset;
1953 free_tlist(origline);
1954 return DIRECTIVE_FOUND;
1956 case PP_LOCAL:
1957 /* TASM like LOCAL directive to define local variables for a
1958 * function, in the following form:
1960 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1962 * The '= LocalSize' at the end is ignored by NASM, but is
1963 * required by TASM to define the local parameter size (and used
1964 * by the TASM macro package).
1966 offset = LocalOffset;
1967 do {
1968 char *local, directive[256];
1969 int size = StackSize;
1971 /* Find the argument name */
1972 tline = tline->next;
1973 if (tline && tline->type == TOK_WHITESPACE)
1974 tline = tline->next;
1975 if (!tline || tline->type != TOK_ID) {
1976 error(ERR_NONFATAL,
1977 "`%%local' missing argument parameter");
1978 free_tlist(origline);
1979 return DIRECTIVE_FOUND;
1981 local = tline->text;
1983 /* Find the argument size type */
1984 tline = tline->next;
1985 if (!tline || tline->type != TOK_OTHER
1986 || tline->text[0] != ':') {
1987 error(ERR_NONFATAL,
1988 "Syntax error processing `%%local' directive");
1989 free_tlist(origline);
1990 return DIRECTIVE_FOUND;
1992 tline = tline->next;
1993 if (!tline || tline->type != TOK_ID) {
1994 error(ERR_NONFATAL,
1995 "`%%local' missing size type parameter");
1996 free_tlist(origline);
1997 return DIRECTIVE_FOUND;
2000 /* Allow macro expansion of type parameter */
2001 tt = tokenize(tline->text);
2002 tt = expand_smacro(tt);
2003 size = parse_size(tt->text);
2004 if (!size) {
2005 error(ERR_NONFATAL,
2006 "Invalid size type for `%%local' missing directive");
2007 free_tlist(tt);
2008 free_tlist(origline);
2009 return DIRECTIVE_FOUND;
2011 free_tlist(tt);
2013 /* Round up to even stack slots */
2014 size = (size+StackSize-1) & ~(StackSize-1);
2016 offset += size; /* Negative offset, increment before */
2018 /* Now define the macro for the argument */
2019 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2020 local, StackPointer, offset);
2021 do_directive(tokenize(directive));
2023 /* Now define the assign to setup the enter_c macro correctly */
2024 snprintf(directive, sizeof(directive),
2025 "%%assign %%$localsize %%$localsize+%d", size);
2026 do_directive(tokenize(directive));
2028 /* Move to the next argument in the list */
2029 tline = tline->next;
2030 if (tline && tline->type == TOK_WHITESPACE)
2031 tline = tline->next;
2032 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2033 LocalOffset = offset;
2034 free_tlist(origline);
2035 return DIRECTIVE_FOUND;
2037 case PP_CLEAR:
2038 if (tline->next)
2039 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2040 free_macros();
2041 init_macros();
2042 free_tlist(origline);
2043 return DIRECTIVE_FOUND;
2045 case PP_INCLUDE:
2046 tline = tline->next;
2047 skip_white_(tline);
2048 if (!tline || (tline->type != TOK_STRING &&
2049 tline->type != TOK_INTERNAL_STRING)) {
2050 error(ERR_NONFATAL, "`%%include' expects a file name");
2051 free_tlist(origline);
2052 return DIRECTIVE_FOUND; /* but we did _something_ */
2054 if (tline->next)
2055 error(ERR_WARNING,
2056 "trailing garbage after `%%include' ignored");
2057 if (tline->type != TOK_INTERNAL_STRING) {
2058 p = tline->text + 1; /* point past the quote to the name */
2059 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2060 } else
2061 p = tline->text; /* internal_string is easier */
2062 expand_macros_in_string(&p);
2063 inc = nasm_malloc(sizeof(Include));
2064 inc->next = istk;
2065 inc->conds = NULL;
2066 inc->fp = inc_fopen(p);
2067 if (!inc->fp && pass == 0) {
2068 /* -MG given but file not found */
2069 nasm_free(inc);
2070 } else {
2071 inc->fname = src_set_fname(p);
2072 inc->lineno = src_set_linnum(0);
2073 inc->lineinc = 1;
2074 inc->expansion = NULL;
2075 inc->mstk = NULL;
2076 istk = inc;
2077 list->uplevel(LIST_INCLUDE);
2079 free_tlist(origline);
2080 return DIRECTIVE_FOUND;
2082 case PP_PUSH:
2083 tline = tline->next;
2084 skip_white_(tline);
2085 tline = expand_id(tline);
2086 if (!tok_type_(tline, TOK_ID)) {
2087 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2088 free_tlist(origline);
2089 return DIRECTIVE_FOUND; /* but we did _something_ */
2091 if (tline->next)
2092 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2093 ctx = nasm_malloc(sizeof(Context));
2094 ctx->next = cstk;
2095 ctx->localmac = hash_init(HASH_SMALL);
2096 ctx->name = nasm_strdup(tline->text);
2097 ctx->number = unique++;
2098 cstk = ctx;
2099 free_tlist(origline);
2100 break;
2102 case PP_REPL:
2103 tline = tline->next;
2104 skip_white_(tline);
2105 tline = expand_id(tline);
2106 if (!tok_type_(tline, TOK_ID)) {
2107 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2108 free_tlist(origline);
2109 return DIRECTIVE_FOUND; /* but we did _something_ */
2111 if (tline->next)
2112 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2113 if (!cstk)
2114 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2115 else {
2116 nasm_free(cstk->name);
2117 cstk->name = nasm_strdup(tline->text);
2119 free_tlist(origline);
2120 break;
2122 case PP_POP:
2123 if (tline->next)
2124 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2125 if (!cstk)
2126 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2127 else
2128 ctx_pop();
2129 free_tlist(origline);
2130 break;
2132 case PP_ERROR:
2133 tline->next = expand_smacro(tline->next);
2134 tline = tline->next;
2135 skip_white_(tline);
2136 if (tok_type_(tline, TOK_STRING)) {
2137 p = tline->text + 1; /* point past the quote to the name */
2138 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2139 expand_macros_in_string(&p);
2140 error(ERR_NONFATAL, "%s", p);
2141 nasm_free(p);
2142 } else {
2143 p = detoken(tline, false);
2144 error(ERR_WARNING, "%s", p);
2145 nasm_free(p);
2147 free_tlist(origline);
2148 break;
2150 CASE_PP_IF:
2151 if (istk->conds && !emitting(istk->conds->state))
2152 j = COND_NEVER;
2153 else {
2154 j = if_condition(tline->next, i);
2155 tline->next = NULL; /* it got freed */
2156 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2158 cond = nasm_malloc(sizeof(Cond));
2159 cond->next = istk->conds;
2160 cond->state = j;
2161 istk->conds = cond;
2162 free_tlist(origline);
2163 return DIRECTIVE_FOUND;
2165 CASE_PP_ELIF:
2166 if (!istk->conds)
2167 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2168 if (emitting(istk->conds->state)
2169 || istk->conds->state == COND_NEVER)
2170 istk->conds->state = COND_NEVER;
2171 else {
2173 * IMPORTANT: In the case of %if, we will already have
2174 * called expand_mmac_params(); however, if we're
2175 * processing an %elif we must have been in a
2176 * non-emitting mode, which would have inhibited
2177 * the normal invocation of expand_mmac_params(). Therefore,
2178 * we have to do it explicitly here.
2180 j = if_condition(expand_mmac_params(tline->next), i);
2181 tline->next = NULL; /* it got freed */
2182 istk->conds->state =
2183 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2185 free_tlist(origline);
2186 return DIRECTIVE_FOUND;
2188 case PP_ELSE:
2189 if (tline->next)
2190 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2191 if (!istk->conds)
2192 error(ERR_FATAL, "`%%else': no matching `%%if'");
2193 if (emitting(istk->conds->state)
2194 || istk->conds->state == COND_NEVER)
2195 istk->conds->state = COND_ELSE_FALSE;
2196 else
2197 istk->conds->state = COND_ELSE_TRUE;
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND;
2201 case PP_ENDIF:
2202 if (tline->next)
2203 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2204 if (!istk->conds)
2205 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2206 cond = istk->conds;
2207 istk->conds = cond->next;
2208 nasm_free(cond);
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
2212 case PP_MACRO:
2213 case PP_IMACRO:
2214 if (defining)
2215 error(ERR_FATAL,
2216 "`%%%smacro': already defining a macro",
2217 (i == PP_IMACRO ? "i" : ""));
2218 tline = tline->next;
2219 skip_white_(tline);
2220 tline = expand_id(tline);
2221 if (!tok_type_(tline, TOK_ID)) {
2222 error(ERR_NONFATAL,
2223 "`%%%smacro' expects a macro name",
2224 (i == PP_IMACRO ? "i" : ""));
2225 return DIRECTIVE_FOUND;
2227 defining = nasm_malloc(sizeof(MMacro));
2228 defining->name = nasm_strdup(tline->text);
2229 defining->casesense = (i == PP_MACRO);
2230 defining->plus = false;
2231 defining->nolist = false;
2232 defining->in_progress = 0;
2233 defining->rep_nest = NULL;
2234 tline = expand_smacro(tline->next);
2235 skip_white_(tline);
2236 if (!tok_type_(tline, TOK_NUMBER)) {
2237 error(ERR_NONFATAL,
2238 "`%%%smacro' expects a parameter count",
2239 (i == PP_IMACRO ? "i" : ""));
2240 defining->nparam_min = defining->nparam_max = 0;
2241 } else {
2242 defining->nparam_min = defining->nparam_max =
2243 readnum(tline->text, &err);
2244 if (err)
2245 error(ERR_NONFATAL,
2246 "unable to parse parameter count `%s'", tline->text);
2248 if (tline && tok_is_(tline->next, "-")) {
2249 tline = tline->next->next;
2250 if (tok_is_(tline, "*"))
2251 defining->nparam_max = INT_MAX;
2252 else if (!tok_type_(tline, TOK_NUMBER))
2253 error(ERR_NONFATAL,
2254 "`%%%smacro' expects a parameter count after `-'",
2255 (i == PP_IMACRO ? "i" : ""));
2256 else {
2257 defining->nparam_max = readnum(tline->text, &err);
2258 if (err)
2259 error(ERR_NONFATAL,
2260 "unable to parse parameter count `%s'",
2261 tline->text);
2262 if (defining->nparam_min > defining->nparam_max)
2263 error(ERR_NONFATAL,
2264 "minimum parameter count exceeds maximum");
2267 if (tline && tok_is_(tline->next, "+")) {
2268 tline = tline->next;
2269 defining->plus = true;
2271 if (tline && tok_type_(tline->next, TOK_ID) &&
2272 !nasm_stricmp(tline->next->text, ".nolist")) {
2273 tline = tline->next;
2274 defining->nolist = true;
2276 mmac = (MMacro *) hash_findix(mmacros, defining->name);
2277 while (mmac) {
2278 if (!strcmp(mmac->name, defining->name) &&
2279 (mmac->nparam_min <= defining->nparam_max
2280 || defining->plus)
2281 && (defining->nparam_min <= mmac->nparam_max
2282 || mmac->plus)) {
2283 error(ERR_WARNING,
2284 "redefining multi-line macro `%s'", defining->name);
2285 break;
2287 mmac = mmac->next;
2290 * Handle default parameters.
2292 if (tline && tline->next) {
2293 defining->dlist = tline->next;
2294 tline->next = NULL;
2295 count_mmac_params(defining->dlist, &defining->ndefs,
2296 &defining->defaults);
2297 } else {
2298 defining->dlist = NULL;
2299 defining->defaults = NULL;
2301 defining->expansion = NULL;
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
2305 case PP_ENDM:
2306 case PP_ENDMACRO:
2307 if (!defining) {
2308 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2309 return DIRECTIVE_FOUND;
2311 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2312 defining->next = *mmhead;
2313 *mmhead = defining;
2314 defining = NULL;
2315 free_tlist(origline);
2316 return DIRECTIVE_FOUND;
2318 case PP_ROTATE:
2319 if (tline->next && tline->next->type == TOK_WHITESPACE)
2320 tline = tline->next;
2321 if (tline->next == NULL) {
2322 free_tlist(origline);
2323 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2324 return DIRECTIVE_FOUND;
2326 t = expand_smacro(tline->next);
2327 tline->next = NULL;
2328 free_tlist(origline);
2329 tline = t;
2330 tptr = &t;
2331 tokval.t_type = TOKEN_INVALID;
2332 evalresult =
2333 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2334 free_tlist(tline);
2335 if (!evalresult)
2336 return DIRECTIVE_FOUND;
2337 if (tokval.t_type)
2338 error(ERR_WARNING,
2339 "trailing garbage after expression ignored");
2340 if (!is_simple(evalresult)) {
2341 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2342 return DIRECTIVE_FOUND;
2344 mmac = istk->mstk;
2345 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2346 mmac = mmac->next_active;
2347 if (!mmac) {
2348 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2349 } else if (mmac->nparam == 0) {
2350 error(ERR_NONFATAL,
2351 "`%%rotate' invoked within macro without parameters");
2352 } else {
2353 int rotate = mmac->rotate + reloc_value(evalresult);
2355 rotate %= (int)mmac->nparam;
2356 if (rotate < 0)
2357 rotate += mmac->nparam;
2359 mmac->rotate = rotate;
2361 return DIRECTIVE_FOUND;
2363 case PP_REP:
2364 nolist = false;
2365 do {
2366 tline = tline->next;
2367 } while (tok_type_(tline, TOK_WHITESPACE));
2369 if (tok_type_(tline, TOK_ID) &&
2370 nasm_stricmp(tline->text, ".nolist") == 0) {
2371 nolist = true;
2372 do {
2373 tline = tline->next;
2374 } while (tok_type_(tline, TOK_WHITESPACE));
2377 if (tline) {
2378 t = expand_smacro(tline);
2379 tptr = &t;
2380 tokval.t_type = TOKEN_INVALID;
2381 evalresult =
2382 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2383 if (!evalresult) {
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2387 if (tokval.t_type)
2388 error(ERR_WARNING,
2389 "trailing garbage after expression ignored");
2390 if (!is_simple(evalresult)) {
2391 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2392 return DIRECTIVE_FOUND;
2394 count = reloc_value(evalresult) + 1;
2395 } else {
2396 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2397 count = 0;
2399 free_tlist(origline);
2401 tmp_defining = defining;
2402 defining = nasm_malloc(sizeof(MMacro));
2403 defining->name = NULL; /* flags this macro as a %rep block */
2404 defining->casesense = false;
2405 defining->plus = false;
2406 defining->nolist = nolist;
2407 defining->in_progress = count;
2408 defining->nparam_min = defining->nparam_max = 0;
2409 defining->defaults = NULL;
2410 defining->dlist = NULL;
2411 defining->expansion = NULL;
2412 defining->next_active = istk->mstk;
2413 defining->rep_nest = tmp_defining;
2414 return DIRECTIVE_FOUND;
2416 case PP_ENDREP:
2417 if (!defining || defining->name) {
2418 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2419 return DIRECTIVE_FOUND;
2423 * Now we have a "macro" defined - although it has no name
2424 * and we won't be entering it in the hash tables - we must
2425 * push a macro-end marker for it on to istk->expansion.
2426 * After that, it will take care of propagating itself (a
2427 * macro-end marker line for a macro which is really a %rep
2428 * block will cause the macro to be re-expanded, complete
2429 * with another macro-end marker to ensure the process
2430 * continues) until the whole expansion is forcibly removed
2431 * from istk->expansion by a %exitrep.
2433 l = nasm_malloc(sizeof(Line));
2434 l->next = istk->expansion;
2435 l->finishes = defining;
2436 l->first = NULL;
2437 istk->expansion = l;
2439 istk->mstk = defining;
2441 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2442 tmp_defining = defining;
2443 defining = defining->rep_nest;
2444 free_tlist(origline);
2445 return DIRECTIVE_FOUND;
2447 case PP_EXITREP:
2449 * We must search along istk->expansion until we hit a
2450 * macro-end marker for a macro with no name. Then we set
2451 * its `in_progress' flag to 0.
2453 for (l = istk->expansion; l; l = l->next)
2454 if (l->finishes && !l->finishes->name)
2455 break;
2457 if (l)
2458 l->finishes->in_progress = 0;
2459 else
2460 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2464 case PP_XDEFINE:
2465 case PP_IXDEFINE:
2466 case PP_DEFINE:
2467 case PP_IDEFINE:
2468 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2470 tline = tline->next;
2471 skip_white_(tline);
2472 tline = expand_id(tline);
2473 if (!tline || (tline->type != TOK_ID &&
2474 (tline->type != TOK_PREPROC_ID ||
2475 tline->text[1] != '$'))) {
2476 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2477 pp_directives[i]);
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2482 ctx = get_ctx(tline->text, false);
2484 mname = tline->text;
2485 last = tline;
2486 param_start = tline = tline->next;
2487 nparam = 0;
2489 /* Expand the macro definition now for %xdefine and %ixdefine */
2490 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2491 tline = expand_smacro(tline);
2493 if (tok_is_(tline, "(")) {
2495 * This macro has parameters.
2498 tline = tline->next;
2499 while (1) {
2500 skip_white_(tline);
2501 if (!tline) {
2502 error(ERR_NONFATAL, "parameter identifier expected");
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2506 if (tline->type != TOK_ID) {
2507 error(ERR_NONFATAL,
2508 "`%s': parameter identifier expected",
2509 tline->text);
2510 free_tlist(origline);
2511 return DIRECTIVE_FOUND;
2513 tline->type = TOK_SMAC_PARAM + nparam++;
2514 tline = tline->next;
2515 skip_white_(tline);
2516 if (tok_is_(tline, ",")) {
2517 tline = tline->next;
2518 continue;
2520 if (!tok_is_(tline, ")")) {
2521 error(ERR_NONFATAL,
2522 "`)' expected to terminate macro template");
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2526 break;
2528 last = tline;
2529 tline = tline->next;
2531 if (tok_type_(tline, TOK_WHITESPACE))
2532 last = tline, tline = tline->next;
2533 macro_start = NULL;
2534 last->next = NULL;
2535 t = tline;
2536 while (t) {
2537 if (t->type == TOK_ID) {
2538 for (tt = param_start; tt; tt = tt->next)
2539 if (tt->type >= TOK_SMAC_PARAM &&
2540 !strcmp(tt->text, t->text))
2541 t->type = tt->type;
2543 tt = t->next;
2544 t->next = macro_start;
2545 macro_start = t;
2546 t = tt;
2549 * Good. We now have a macro name, a parameter count, and a
2550 * token list (in reverse order) for an expansion. We ought
2551 * to be OK just to create an SMacro, store it, and let
2552 * free_tlist have the rest of the line (which we have
2553 * carefully re-terminated after chopping off the expansion
2554 * from the end).
2556 define_smacro(ctx, mname, casesense, nparam, macro_start);
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2560 case PP_UNDEF:
2561 tline = tline->next;
2562 skip_white_(tline);
2563 tline = expand_id(tline);
2564 if (!tline || (tline->type != TOK_ID &&
2565 (tline->type != TOK_PREPROC_ID ||
2566 tline->text[1] != '$'))) {
2567 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
2571 if (tline->next) {
2572 error(ERR_WARNING,
2573 "trailing garbage after macro name ignored");
2576 /* Find the context that symbol belongs to */
2577 ctx = get_ctx(tline->text, false);
2578 undef_smacro(ctx, tline->text);
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2582 case PP_STRLEN:
2583 casesense = true;
2585 tline = tline->next;
2586 skip_white_(tline);
2587 tline = expand_id(tline);
2588 if (!tline || (tline->type != TOK_ID &&
2589 (tline->type != TOK_PREPROC_ID ||
2590 tline->text[1] != '$'))) {
2591 error(ERR_NONFATAL,
2592 "`%%strlen' expects a macro identifier as first parameter");
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2596 ctx = get_ctx(tline->text, false);
2598 mname = tline->text;
2599 last = tline;
2600 tline = expand_smacro(tline->next);
2601 last->next = NULL;
2603 t = tline;
2604 while (tok_type_(t, TOK_WHITESPACE))
2605 t = t->next;
2606 /* t should now point to the string */
2607 if (t->type != TOK_STRING) {
2608 error(ERR_NONFATAL,
2609 "`%%strlen` requires string as second parameter");
2610 free_tlist(tline);
2611 free_tlist(origline);
2612 return DIRECTIVE_FOUND;
2615 macro_start = nasm_malloc(sizeof(*macro_start));
2616 macro_start->next = NULL;
2617 make_tok_num(macro_start, strlen(t->text) - 2);
2618 macro_start->mac = NULL;
2621 * We now have a macro name, an implicit parameter count of
2622 * zero, and a numeric token to use as an expansion. Create
2623 * and store an SMacro.
2625 define_smacro(ctx, mname, casesense, 0, macro_start);
2626 free_tlist(tline);
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND;
2630 case PP_SUBSTR:
2631 casesense = true;
2633 tline = tline->next;
2634 skip_white_(tline);
2635 tline = expand_id(tline);
2636 if (!tline || (tline->type != TOK_ID &&
2637 (tline->type != TOK_PREPROC_ID ||
2638 tline->text[1] != '$'))) {
2639 error(ERR_NONFATAL,
2640 "`%%substr' expects a macro identifier as first parameter");
2641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
2644 ctx = get_ctx(tline->text, false);
2646 mname = tline->text;
2647 last = tline;
2648 tline = expand_smacro(tline->next);
2649 last->next = NULL;
2651 t = tline->next;
2652 while (tok_type_(t, TOK_WHITESPACE))
2653 t = t->next;
2655 /* t should now point to the string */
2656 if (t->type != TOK_STRING) {
2657 error(ERR_NONFATAL,
2658 "`%%substr` requires string as second parameter");
2659 free_tlist(tline);
2660 free_tlist(origline);
2661 return DIRECTIVE_FOUND;
2664 tt = t->next;
2665 tptr = &tt;
2666 tokval.t_type = TOKEN_INVALID;
2667 evalresult =
2668 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2669 if (!evalresult) {
2670 free_tlist(tline);
2671 free_tlist(origline);
2672 return DIRECTIVE_FOUND;
2674 if (!is_simple(evalresult)) {
2675 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2676 free_tlist(tline);
2677 free_tlist(origline);
2678 return DIRECTIVE_FOUND;
2681 macro_start = nasm_malloc(sizeof(*macro_start));
2682 macro_start->next = NULL;
2683 macro_start->text = nasm_strdup("'''");
2684 if (evalresult->value > 0
2685 && evalresult->value < (int) strlen(t->text) - 1) {
2686 macro_start->text[1] = t->text[evalresult->value];
2687 } else {
2688 macro_start->text[2] = '\0';
2690 macro_start->type = TOK_STRING;
2691 macro_start->mac = NULL;
2694 * We now have a macro name, an implicit parameter count of
2695 * zero, and a numeric token to use as an expansion. Create
2696 * and store an SMacro.
2698 define_smacro(ctx, mname, casesense, 0, macro_start);
2699 free_tlist(tline);
2700 free_tlist(origline);
2701 return DIRECTIVE_FOUND;
2703 case PP_ASSIGN:
2704 case PP_IASSIGN:
2705 casesense = (i == PP_ASSIGN);
2707 tline = tline->next;
2708 skip_white_(tline);
2709 tline = expand_id(tline);
2710 if (!tline || (tline->type != TOK_ID &&
2711 (tline->type != TOK_PREPROC_ID ||
2712 tline->text[1] != '$'))) {
2713 error(ERR_NONFATAL,
2714 "`%%%sassign' expects a macro identifier",
2715 (i == PP_IASSIGN ? "i" : ""));
2716 free_tlist(origline);
2717 return DIRECTIVE_FOUND;
2719 ctx = get_ctx(tline->text, false);
2721 mname = tline->text;
2722 last = tline;
2723 tline = expand_smacro(tline->next);
2724 last->next = NULL;
2726 t = tline;
2727 tptr = &t;
2728 tokval.t_type = TOKEN_INVALID;
2729 evalresult =
2730 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2731 free_tlist(tline);
2732 if (!evalresult) {
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
2737 if (tokval.t_type)
2738 error(ERR_WARNING,
2739 "trailing garbage after expression ignored");
2741 if (!is_simple(evalresult)) {
2742 error(ERR_NONFATAL,
2743 "non-constant value given to `%%%sassign'",
2744 (i == PP_IASSIGN ? "i" : ""));
2745 free_tlist(origline);
2746 return DIRECTIVE_FOUND;
2749 macro_start = nasm_malloc(sizeof(*macro_start));
2750 macro_start->next = NULL;
2751 make_tok_num(macro_start, reloc_value(evalresult));
2752 macro_start->mac = NULL;
2755 * We now have a macro name, an implicit parameter count of
2756 * zero, and a numeric token to use as an expansion. Create
2757 * and store an SMacro.
2759 define_smacro(ctx, mname, casesense, 0, macro_start);
2760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
2763 case PP_LINE:
2765 * Syntax is `%line nnn[+mmm] [filename]'
2767 tline = tline->next;
2768 skip_white_(tline);
2769 if (!tok_type_(tline, TOK_NUMBER)) {
2770 error(ERR_NONFATAL, "`%%line' expects line number");
2771 free_tlist(origline);
2772 return DIRECTIVE_FOUND;
2774 k = readnum(tline->text, &err);
2775 m = 1;
2776 tline = tline->next;
2777 if (tok_is_(tline, "+")) {
2778 tline = tline->next;
2779 if (!tok_type_(tline, TOK_NUMBER)) {
2780 error(ERR_NONFATAL, "`%%line' expects line increment");
2781 free_tlist(origline);
2782 return DIRECTIVE_FOUND;
2784 m = readnum(tline->text, &err);
2785 tline = tline->next;
2787 skip_white_(tline);
2788 src_set_linnum(k);
2789 istk->lineinc = m;
2790 if (tline) {
2791 nasm_free(src_set_fname(detoken(tline, false)));
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
2796 default:
2797 error(ERR_FATAL,
2798 "preprocessor directive `%s' not yet implemented",
2799 pp_directives[i]);
2800 break;
2802 return DIRECTIVE_FOUND;
2806 * Ensure that a macro parameter contains a condition code and
2807 * nothing else. Return the condition code index if so, or -1
2808 * otherwise.
2810 static int find_cc(Token * t)
2812 Token *tt;
2813 int i, j, k, m;
2815 if (!t)
2816 return -1; /* Probably a %+ without a space */
2818 skip_white_(t);
2819 if (t->type != TOK_ID)
2820 return -1;
2821 tt = t->next;
2822 skip_white_(tt);
2823 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2824 return -1;
2826 i = -1;
2827 j = elements(conditions);
2828 while (j - i > 1) {
2829 k = (j + i) / 2;
2830 m = nasm_stricmp(t->text, conditions[k]);
2831 if (m == 0) {
2832 i = k;
2833 j = -2;
2834 break;
2835 } else if (m < 0) {
2836 j = k;
2837 } else
2838 i = k;
2840 if (j != -2)
2841 return -1;
2842 return i;
2846 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2847 * %-n) and MMacro-local identifiers (%%foo).
2849 static Token *expand_mmac_params(Token * tline)
2851 Token *t, *tt, **tail, *thead;
2853 tail = &thead;
2854 thead = NULL;
2856 while (tline) {
2857 if (tline->type == TOK_PREPROC_ID &&
2858 (((tline->text[1] == '+' || tline->text[1] == '-')
2859 && tline->text[2]) || tline->text[1] == '%'
2860 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2861 char *text = NULL;
2862 int type = 0, cc; /* type = 0 to placate optimisers */
2863 char tmpbuf[30];
2864 unsigned int n;
2865 int i;
2866 MMacro *mac;
2868 t = tline;
2869 tline = tline->next;
2871 mac = istk->mstk;
2872 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2873 mac = mac->next_active;
2874 if (!mac)
2875 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2876 else
2877 switch (t->text[1]) {
2879 * We have to make a substitution of one of the
2880 * forms %1, %-1, %+1, %%foo, %0.
2882 case '0':
2883 type = TOK_NUMBER;
2884 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2885 text = nasm_strdup(tmpbuf);
2886 break;
2887 case '%':
2888 type = TOK_ID;
2889 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2890 mac->unique);
2891 text = nasm_strcat(tmpbuf, t->text + 2);
2892 break;
2893 case '-':
2894 n = atoi(t->text + 2) - 1;
2895 if (n >= mac->nparam)
2896 tt = NULL;
2897 else {
2898 if (mac->nparam > 1)
2899 n = (n + mac->rotate) % mac->nparam;
2900 tt = mac->params[n];
2902 cc = find_cc(tt);
2903 if (cc == -1) {
2904 error(ERR_NONFATAL,
2905 "macro parameter %d is not a condition code",
2906 n + 1);
2907 text = NULL;
2908 } else {
2909 type = TOK_ID;
2910 if (inverse_ccs[cc] == -1) {
2911 error(ERR_NONFATAL,
2912 "condition code `%s' is not invertible",
2913 conditions[cc]);
2914 text = NULL;
2915 } else
2916 text =
2917 nasm_strdup(conditions[inverse_ccs[cc]]);
2919 break;
2920 case '+':
2921 n = atoi(t->text + 2) - 1;
2922 if (n >= mac->nparam)
2923 tt = NULL;
2924 else {
2925 if (mac->nparam > 1)
2926 n = (n + mac->rotate) % mac->nparam;
2927 tt = mac->params[n];
2929 cc = find_cc(tt);
2930 if (cc == -1) {
2931 error(ERR_NONFATAL,
2932 "macro parameter %d is not a condition code",
2933 n + 1);
2934 text = NULL;
2935 } else {
2936 type = TOK_ID;
2937 text = nasm_strdup(conditions[cc]);
2939 break;
2940 default:
2941 n = atoi(t->text + 1) - 1;
2942 if (n >= mac->nparam)
2943 tt = NULL;
2944 else {
2945 if (mac->nparam > 1)
2946 n = (n + mac->rotate) % mac->nparam;
2947 tt = mac->params[n];
2949 if (tt) {
2950 for (i = 0; i < mac->paramlen[n]; i++) {
2951 *tail = new_Token(NULL, tt->type, tt->text, 0);
2952 tail = &(*tail)->next;
2953 tt = tt->next;
2956 text = NULL; /* we've done it here */
2957 break;
2959 if (!text) {
2960 delete_Token(t);
2961 } else {
2962 *tail = t;
2963 tail = &t->next;
2964 t->type = type;
2965 nasm_free(t->text);
2966 t->text = text;
2967 t->mac = NULL;
2969 continue;
2970 } else {
2971 t = *tail = tline;
2972 tline = tline->next;
2973 t->mac = NULL;
2974 tail = &t->next;
2977 *tail = NULL;
2978 t = thead;
2979 for (; t && (tt = t->next) != NULL; t = t->next)
2980 switch (t->type) {
2981 case TOK_WHITESPACE:
2982 if (tt->type == TOK_WHITESPACE) {
2983 t->next = delete_Token(tt);
2985 break;
2986 case TOK_ID:
2987 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2988 char *tmp = nasm_strcat(t->text, tt->text);
2989 nasm_free(t->text);
2990 t->text = tmp;
2991 t->next = delete_Token(tt);
2993 break;
2994 case TOK_NUMBER:
2995 if (tt->type == TOK_NUMBER) {
2996 char *tmp = nasm_strcat(t->text, tt->text);
2997 nasm_free(t->text);
2998 t->text = tmp;
2999 t->next = delete_Token(tt);
3001 break;
3002 default:
3003 break;
3006 return thead;
3010 * Expand all single-line macro calls made in the given line.
3011 * Return the expanded version of the line. The original is deemed
3012 * to be destroyed in the process. (In reality we'll just move
3013 * Tokens from input to output a lot of the time, rather than
3014 * actually bothering to destroy and replicate.)
3016 #define DEADMAN_LIMIT (1 << 20)
3018 static Token *expand_smacro(Token * tline)
3020 Token *t, *tt, *mstart, **tail, *thead;
3021 SMacro *head = NULL, *m;
3022 Token **params;
3023 int *paramsize;
3024 unsigned int nparam, sparam;
3025 int brackets, rescan;
3026 Token *org_tline = tline;
3027 Context *ctx;
3028 char *mname;
3029 int deadman = DEADMAN_LIMIT;
3032 * Trick: we should avoid changing the start token pointer since it can
3033 * be contained in "next" field of other token. Because of this
3034 * we allocate a copy of first token and work with it; at the end of
3035 * routine we copy it back
3037 if (org_tline) {
3038 tline =
3039 new_Token(org_tline->next, org_tline->type, org_tline->text,
3041 tline->mac = org_tline->mac;
3042 nasm_free(org_tline->text);
3043 org_tline->text = NULL;
3046 again:
3047 tail = &thead;
3048 thead = NULL;
3050 while (tline) { /* main token loop */
3051 if (!--deadman) {
3052 error(ERR_NONFATAL, "interminable macro recursion");
3053 break;
3056 if ((mname = tline->text)) {
3057 /* if this token is a local macro, look in local context */
3058 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3059 ctx = get_ctx(mname, true);
3060 else
3061 ctx = NULL;
3063 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3064 mname);
3067 * We've hit an identifier. As in is_mmacro below, we first
3068 * check whether the identifier is a single-line macro at
3069 * all, then think about checking for parameters if
3070 * necessary.
3072 for (m = head; m; m = m->next)
3073 if (!mstrcmp(m->name, mname, m->casesense))
3074 break;
3075 if (m) {
3076 mstart = tline;
3077 params = NULL;
3078 paramsize = NULL;
3079 if (m->nparam == 0) {
3081 * Simple case: the macro is parameterless. Discard the
3082 * one token that the macro call took, and push the
3083 * expansion back on the to-do stack.
3085 if (!m->expansion) {
3086 if (!strcmp("__FILE__", m->name)) {
3087 int32_t num = 0;
3088 src_get(&num, &(tline->text));
3089 nasm_quote(&(tline->text));
3090 tline->type = TOK_STRING;
3091 continue;
3093 if (!strcmp("__LINE__", m->name)) {
3094 nasm_free(tline->text);
3095 make_tok_num(tline, src_get_linnum());
3096 continue;
3098 if (!strcmp("__BITS__", m->name)) {
3099 nasm_free(tline->text);
3100 make_tok_num(tline, globalbits);
3101 continue;
3103 tline = delete_Token(tline);
3104 continue;
3106 } else {
3108 * Complicated case: at least one macro with this name
3109 * exists and takes parameters. We must find the
3110 * parameters in the call, count them, find the SMacro
3111 * that corresponds to that form of the macro call, and
3112 * substitute for the parameters when we expand. What a
3113 * pain.
3115 /*tline = tline->next;
3116 skip_white_(tline); */
3117 do {
3118 t = tline->next;
3119 while (tok_type_(t, TOK_SMAC_END)) {
3120 t->mac->in_progress = false;
3121 t->text = NULL;
3122 t = tline->next = delete_Token(t);
3124 tline = t;
3125 } while (tok_type_(tline, TOK_WHITESPACE));
3126 if (!tok_is_(tline, "(")) {
3128 * This macro wasn't called with parameters: ignore
3129 * the call. (Behaviour borrowed from gnu cpp.)
3131 tline = mstart;
3132 m = NULL;
3133 } else {
3134 int paren = 0;
3135 int white = 0;
3136 brackets = 0;
3137 nparam = 0;
3138 sparam = PARAM_DELTA;
3139 params = nasm_malloc(sparam * sizeof(Token *));
3140 params[0] = tline->next;
3141 paramsize = nasm_malloc(sparam * sizeof(int));
3142 paramsize[0] = 0;
3143 while (true) { /* parameter loop */
3145 * For some unusual expansions
3146 * which concatenates function call
3148 t = tline->next;
3149 while (tok_type_(t, TOK_SMAC_END)) {
3150 t->mac->in_progress = false;
3151 t->text = NULL;
3152 t = tline->next = delete_Token(t);
3154 tline = t;
3156 if (!tline) {
3157 error(ERR_NONFATAL,
3158 "macro call expects terminating `)'");
3159 break;
3161 if (tline->type == TOK_WHITESPACE
3162 && brackets <= 0) {
3163 if (paramsize[nparam])
3164 white++;
3165 else
3166 params[nparam] = tline->next;
3167 continue; /* parameter loop */
3169 if (tline->type == TOK_OTHER
3170 && tline->text[1] == 0) {
3171 char ch = tline->text[0];
3172 if (ch == ',' && !paren && brackets <= 0) {
3173 if (++nparam >= sparam) {
3174 sparam += PARAM_DELTA;
3175 params = nasm_realloc(params,
3176 sparam *
3177 sizeof(Token
3178 *));
3179 paramsize =
3180 nasm_realloc(paramsize,
3181 sparam *
3182 sizeof(int));
3184 params[nparam] = tline->next;
3185 paramsize[nparam] = 0;
3186 white = 0;
3187 continue; /* parameter loop */
3189 if (ch == '{' &&
3190 (brackets > 0 || (brackets == 0 &&
3191 !paramsize[nparam])))
3193 if (!(brackets++)) {
3194 params[nparam] = tline->next;
3195 continue; /* parameter loop */
3198 if (ch == '}' && brackets > 0)
3199 if (--brackets == 0) {
3200 brackets = -1;
3201 continue; /* parameter loop */
3203 if (ch == '(' && !brackets)
3204 paren++;
3205 if (ch == ')' && brackets <= 0)
3206 if (--paren < 0)
3207 break;
3209 if (brackets < 0) {
3210 brackets = 0;
3211 error(ERR_NONFATAL, "braces do not "
3212 "enclose all of macro parameter");
3214 paramsize[nparam] += white + 1;
3215 white = 0;
3216 } /* parameter loop */
3217 nparam++;
3218 while (m && (m->nparam != nparam ||
3219 mstrcmp(m->name, mname,
3220 m->casesense)))
3221 m = m->next;
3222 if (!m)
3223 error(ERR_WARNING | ERR_WARN_MNP,
3224 "macro `%s' exists, "
3225 "but not taking %d parameters",
3226 mstart->text, nparam);
3229 if (m && m->in_progress)
3230 m = NULL;
3231 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3233 * Design question: should we handle !tline, which
3234 * indicates missing ')' here, or expand those
3235 * macros anyway, which requires the (t) test a few
3236 * lines down?
3238 nasm_free(params);
3239 nasm_free(paramsize);
3240 tline = mstart;
3241 } else {
3243 * Expand the macro: we are placed on the last token of the
3244 * call, so that we can easily split the call from the
3245 * following tokens. We also start by pushing an SMAC_END
3246 * token for the cycle removal.
3248 t = tline;
3249 if (t) {
3250 tline = t->next;
3251 t->next = NULL;
3253 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3254 tt->mac = m;
3255 m->in_progress = true;
3256 tline = tt;
3257 for (t = m->expansion; t; t = t->next) {
3258 if (t->type >= TOK_SMAC_PARAM) {
3259 Token *pcopy = tline, **ptail = &pcopy;
3260 Token *ttt, *pt;
3261 int i;
3263 ttt = params[t->type - TOK_SMAC_PARAM];
3264 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3265 --i >= 0;) {
3266 pt = *ptail =
3267 new_Token(tline, ttt->type, ttt->text,
3269 ptail = &pt->next;
3270 ttt = ttt->next;
3272 tline = pcopy;
3273 } else if (t->type == TOK_PREPROC_Q) {
3274 tt = new_Token(tline, TOK_ID, mname, 0);
3275 tline = tt;
3276 } else if (t->type == TOK_PREPROC_QQ) {
3277 tt = new_Token(tline, TOK_ID, m->name, 0);
3278 tline = tt;
3279 } else {
3280 tt = new_Token(tline, t->type, t->text, 0);
3281 tline = tt;
3286 * Having done that, get rid of the macro call, and clean
3287 * up the parameters.
3289 nasm_free(params);
3290 nasm_free(paramsize);
3291 free_tlist(mstart);
3292 continue; /* main token loop */
3297 if (tline->type == TOK_SMAC_END) {
3298 tline->mac->in_progress = false;
3299 tline = delete_Token(tline);
3300 } else {
3301 t = *tail = tline;
3302 tline = tline->next;
3303 t->mac = NULL;
3304 t->next = NULL;
3305 tail = &t->next;
3310 * Now scan the entire line and look for successive TOK_IDs that resulted
3311 * after expansion (they can't be produced by tokenize()). The successive
3312 * TOK_IDs should be concatenated.
3313 * Also we look for %+ tokens and concatenate the tokens before and after
3314 * them (without white spaces in between).
3316 t = thead;
3317 rescan = 0;
3318 while (t) {
3319 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3320 t = t->next;
3321 if (!t || !t->next)
3322 break;
3323 if (t->next->type == TOK_ID ||
3324 t->next->type == TOK_PREPROC_ID ||
3325 t->next->type == TOK_NUMBER) {
3326 char *p = nasm_strcat(t->text, t->next->text);
3327 nasm_free(t->text);
3328 t->next = delete_Token(t->next);
3329 t->text = p;
3330 rescan = 1;
3331 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3332 t->next->next->type == TOK_PREPROC_ID &&
3333 strcmp(t->next->next->text, "%+") == 0) {
3334 /* free the next whitespace, the %+ token and next whitespace */
3335 int i;
3336 for (i = 1; i <= 3; i++) {
3337 if (!t->next
3338 || (i != 2 && t->next->type != TOK_WHITESPACE))
3339 break;
3340 t->next = delete_Token(t->next);
3341 } /* endfor */
3342 } else
3343 t = t->next;
3345 /* If we concatenaded something, re-scan the line for macros */
3346 if (rescan) {
3347 tline = thead;
3348 goto again;
3351 if (org_tline) {
3352 if (thead) {
3353 *org_tline = *thead;
3354 /* since we just gave text to org_line, don't free it */
3355 thead->text = NULL;
3356 delete_Token(thead);
3357 } else {
3358 /* the expression expanded to empty line;
3359 we can't return NULL for some reasons
3360 we just set the line to a single WHITESPACE token. */
3361 memset(org_tline, 0, sizeof(*org_tline));
3362 org_tline->text = NULL;
3363 org_tline->type = TOK_WHITESPACE;
3365 thead = org_tline;
3368 return thead;
3372 * Similar to expand_smacro but used exclusively with macro identifiers
3373 * right before they are fetched in. The reason is that there can be
3374 * identifiers consisting of several subparts. We consider that if there
3375 * are more than one element forming the name, user wants a expansion,
3376 * otherwise it will be left as-is. Example:
3378 * %define %$abc cde
3380 * the identifier %$abc will be left as-is so that the handler for %define
3381 * will suck it and define the corresponding value. Other case:
3383 * %define _%$abc cde
3385 * In this case user wants name to be expanded *before* %define starts
3386 * working, so we'll expand %$abc into something (if it has a value;
3387 * otherwise it will be left as-is) then concatenate all successive
3388 * PP_IDs into one.
3390 static Token *expand_id(Token * tline)
3392 Token *cur, *oldnext = NULL;
3394 if (!tline || !tline->next)
3395 return tline;
3397 cur = tline;
3398 while (cur->next &&
3399 (cur->next->type == TOK_ID ||
3400 cur->next->type == TOK_PREPROC_ID
3401 || cur->next->type == TOK_NUMBER))
3402 cur = cur->next;
3404 /* If identifier consists of just one token, don't expand */
3405 if (cur == tline)
3406 return tline;
3408 if (cur) {
3409 oldnext = cur->next; /* Detach the tail past identifier */
3410 cur->next = NULL; /* so that expand_smacro stops here */
3413 tline = expand_smacro(tline);
3415 if (cur) {
3416 /* expand_smacro possibly changhed tline; re-scan for EOL */
3417 cur = tline;
3418 while (cur && cur->next)
3419 cur = cur->next;
3420 if (cur)
3421 cur->next = oldnext;
3424 return tline;
3428 * Determine whether the given line constitutes a multi-line macro
3429 * call, and return the MMacro structure called if so. Doesn't have
3430 * to check for an initial label - that's taken care of in
3431 * expand_mmacro - but must check numbers of parameters. Guaranteed
3432 * to be called with tline->type == TOK_ID, so the putative macro
3433 * name is easy to find.
3435 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3437 MMacro *head, *m;
3438 Token **params;
3439 int nparam;
3441 head = (MMacro *) hash_findix(mmacros, tline->text);
3444 * Efficiency: first we see if any macro exists with the given
3445 * name. If not, we can return NULL immediately. _Then_ we
3446 * count the parameters, and then we look further along the
3447 * list if necessary to find the proper MMacro.
3449 for (m = head; m; m = m->next)
3450 if (!mstrcmp(m->name, tline->text, m->casesense))
3451 break;
3452 if (!m)
3453 return NULL;
3456 * OK, we have a potential macro. Count and demarcate the
3457 * parameters.
3459 count_mmac_params(tline->next, &nparam, &params);
3462 * So we know how many parameters we've got. Find the MMacro
3463 * structure that handles this number.
3465 while (m) {
3466 if (m->nparam_min <= nparam
3467 && (m->plus || nparam <= m->nparam_max)) {
3469 * This one is right. Just check if cycle removal
3470 * prohibits us using it before we actually celebrate...
3472 if (m->in_progress) {
3473 #if 0
3474 error(ERR_NONFATAL,
3475 "self-reference in multi-line macro `%s'", m->name);
3476 #endif
3477 nasm_free(params);
3478 return NULL;
3481 * It's right, and we can use it. Add its default
3482 * parameters to the end of our list if necessary.
3484 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3485 params =
3486 nasm_realloc(params,
3487 ((m->nparam_min + m->ndefs +
3488 1) * sizeof(*params)));
3489 while (nparam < m->nparam_min + m->ndefs) {
3490 params[nparam] = m->defaults[nparam - m->nparam_min];
3491 nparam++;
3495 * If we've gone over the maximum parameter count (and
3496 * we're in Plus mode), ignore parameters beyond
3497 * nparam_max.
3499 if (m->plus && nparam > m->nparam_max)
3500 nparam = m->nparam_max;
3502 * Then terminate the parameter list, and leave.
3504 if (!params) { /* need this special case */
3505 params = nasm_malloc(sizeof(*params));
3506 nparam = 0;
3508 params[nparam] = NULL;
3509 *params_array = params;
3510 return m;
3513 * This one wasn't right: look for the next one with the
3514 * same name.
3516 for (m = m->next; m; m = m->next)
3517 if (!mstrcmp(m->name, tline->text, m->casesense))
3518 break;
3522 * After all that, we didn't find one with the right number of
3523 * parameters. Issue a warning, and fail to expand the macro.
3525 error(ERR_WARNING | ERR_WARN_MNP,
3526 "macro `%s' exists, but not taking %d parameters",
3527 tline->text, nparam);
3528 nasm_free(params);
3529 return NULL;
3533 * Expand the multi-line macro call made by the given line, if
3534 * there is one to be expanded. If there is, push the expansion on
3535 * istk->expansion and return 1. Otherwise return 0.
3537 static int expand_mmacro(Token * tline)
3539 Token *startline = tline;
3540 Token *label = NULL;
3541 int dont_prepend = 0;
3542 Token **params, *t, *mtok, *tt;
3543 MMacro *m;
3544 Line *l, *ll;
3545 int i, nparam, *paramlen;
3547 t = tline;
3548 skip_white_(t);
3549 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3550 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3551 return 0;
3552 mtok = t;
3553 m = is_mmacro(t, &params);
3554 if (!m) {
3555 Token *last;
3557 * We have an id which isn't a macro call. We'll assume
3558 * it might be a label; we'll also check to see if a
3559 * colon follows it. Then, if there's another id after
3560 * that lot, we'll check it again for macro-hood.
3562 label = last = t;
3563 t = t->next;
3564 if (tok_type_(t, TOK_WHITESPACE))
3565 last = t, t = t->next;
3566 if (tok_is_(t, ":")) {
3567 dont_prepend = 1;
3568 last = t, t = t->next;
3569 if (tok_type_(t, TOK_WHITESPACE))
3570 last = t, t = t->next;
3572 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3573 return 0;
3574 last->next = NULL;
3575 tline = t;
3579 * Fix up the parameters: this involves stripping leading and
3580 * trailing whitespace, then stripping braces if they are
3581 * present.
3583 for (nparam = 0; params[nparam]; nparam++) ;
3584 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3586 for (i = 0; params[i]; i++) {
3587 int brace = false;
3588 int comma = (!m->plus || i < nparam - 1);
3590 t = params[i];
3591 skip_white_(t);
3592 if (tok_is_(t, "{"))
3593 t = t->next, brace = true, comma = false;
3594 params[i] = t;
3595 paramlen[i] = 0;
3596 while (t) {
3597 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3598 break; /* ... because we have hit a comma */
3599 if (comma && t->type == TOK_WHITESPACE
3600 && tok_is_(t->next, ","))
3601 break; /* ... or a space then a comma */
3602 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3603 break; /* ... or a brace */
3604 t = t->next;
3605 paramlen[i]++;
3610 * OK, we have a MMacro structure together with a set of
3611 * parameters. We must now go through the expansion and push
3612 * copies of each Line on to istk->expansion. Substitution of
3613 * parameter tokens and macro-local tokens doesn't get done
3614 * until the single-line macro substitution process; this is
3615 * because delaying them allows us to change the semantics
3616 * later through %rotate.
3618 * First, push an end marker on to istk->expansion, mark this
3619 * macro as in progress, and set up its invocation-specific
3620 * variables.
3622 ll = nasm_malloc(sizeof(Line));
3623 ll->next = istk->expansion;
3624 ll->finishes = m;
3625 ll->first = NULL;
3626 istk->expansion = ll;
3628 m->in_progress = true;
3629 m->params = params;
3630 m->iline = tline;
3631 m->nparam = nparam;
3632 m->rotate = 0;
3633 m->paramlen = paramlen;
3634 m->unique = unique++;
3635 m->lineno = 0;
3637 m->next_active = istk->mstk;
3638 istk->mstk = m;
3640 for (l = m->expansion; l; l = l->next) {
3641 Token **tail;
3643 ll = nasm_malloc(sizeof(Line));
3644 ll->finishes = NULL;
3645 ll->next = istk->expansion;
3646 istk->expansion = ll;
3647 tail = &ll->first;
3649 for (t = l->first; t; t = t->next) {
3650 Token *x = t;
3651 switch (t->type) {
3652 case TOK_PREPROC_Q:
3653 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3654 break;
3655 case TOK_PREPROC_QQ:
3656 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3657 break;
3658 case TOK_PREPROC_ID:
3659 if (t->text[1] == '0' && t->text[2] == '0') {
3660 dont_prepend = -1;
3661 x = label;
3662 if (!x)
3663 continue;
3665 /* fall through */
3666 default:
3667 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3668 break;
3670 tail = &tt->next;
3672 *tail = NULL;
3676 * If we had a label, push it on as the first line of
3677 * the macro expansion.
3679 if (label) {
3680 if (dont_prepend < 0)
3681 free_tlist(startline);
3682 else {
3683 ll = nasm_malloc(sizeof(Line));
3684 ll->finishes = NULL;
3685 ll->next = istk->expansion;
3686 istk->expansion = ll;
3687 ll->first = startline;
3688 if (!dont_prepend) {
3689 while (label->next)
3690 label = label->next;
3691 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3696 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3698 return 1;
3702 * Since preprocessor always operate only on the line that didn't
3703 * arrived yet, we should always use ERR_OFFBY1. Also since user
3704 * won't want to see same error twice (preprocessing is done once
3705 * per pass) we will want to show errors only during pass one.
3707 static void error(int severity, const char *fmt, ...)
3709 va_list arg;
3710 char buff[1024];
3712 /* If we're in a dead branch of IF or something like it, ignore the error */
3713 if (istk && istk->conds && !emitting(istk->conds->state))
3714 return;
3716 va_start(arg, fmt);
3717 vsnprintf(buff, sizeof(buff), fmt, arg);
3718 va_end(arg);
3720 if (istk && istk->mstk && istk->mstk->name)
3721 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3722 istk->mstk->lineno, buff);
3723 else
3724 _error(severity | ERR_PASS1, "%s", buff);
3727 static void
3728 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3729 ListGen * listgen)
3731 _error = errfunc;
3732 cstk = NULL;
3733 istk = nasm_malloc(sizeof(Include));
3734 istk->next = NULL;
3735 istk->conds = NULL;
3736 istk->expansion = NULL;
3737 istk->mstk = NULL;
3738 istk->fp = fopen(file, "r");
3739 istk->fname = NULL;
3740 src_set_fname(nasm_strdup(file));
3741 src_set_linnum(0);
3742 istk->lineinc = 1;
3743 if (!istk->fp)
3744 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3745 file);
3746 defining = NULL;
3747 init_macros();
3748 unique = 0;
3749 if (tasm_compatible_mode) {
3750 stdmacpos = nasm_stdmac;
3751 } else {
3752 stdmacpos = nasm_stdmac_after_tasm;
3754 any_extrastdmac = (extrastdmac != NULL);
3755 list = listgen;
3756 evaluate = eval;
3757 pass = apass;
3760 static char *pp_getline(void)
3762 char *line;
3763 Token *tline;
3765 while (1) {
3767 * Fetch a tokenized line, either from the macro-expansion
3768 * buffer or from the input file.
3770 tline = NULL;
3771 while (istk->expansion && istk->expansion->finishes) {
3772 Line *l = istk->expansion;
3773 if (!l->finishes->name && l->finishes->in_progress > 1) {
3774 Line *ll;
3777 * This is a macro-end marker for a macro with no
3778 * name, which means it's not really a macro at all
3779 * but a %rep block, and the `in_progress' field is
3780 * more than 1, meaning that we still need to
3781 * repeat. (1 means the natural last repetition; 0
3782 * means termination by %exitrep.) We have
3783 * therefore expanded up to the %endrep, and must
3784 * push the whole block on to the expansion buffer
3785 * again. We don't bother to remove the macro-end
3786 * marker: we'd only have to generate another one
3787 * if we did.
3789 l->finishes->in_progress--;
3790 for (l = l->finishes->expansion; l; l = l->next) {
3791 Token *t, *tt, **tail;
3793 ll = nasm_malloc(sizeof(Line));
3794 ll->next = istk->expansion;
3795 ll->finishes = NULL;
3796 ll->first = NULL;
3797 tail = &ll->first;
3799 for (t = l->first; t; t = t->next) {
3800 if (t->text || t->type == TOK_WHITESPACE) {
3801 tt = *tail =
3802 new_Token(NULL, t->type, t->text, 0);
3803 tail = &tt->next;
3807 istk->expansion = ll;
3809 } else {
3811 * Check whether a `%rep' was started and not ended
3812 * within this macro expansion. This can happen and
3813 * should be detected. It's a fatal error because
3814 * I'm too confused to work out how to recover
3815 * sensibly from it.
3817 if (defining) {
3818 if (defining->name)
3819 error(ERR_PANIC,
3820 "defining with name in expansion");
3821 else if (istk->mstk->name)
3822 error(ERR_FATAL,
3823 "`%%rep' without `%%endrep' within"
3824 " expansion of macro `%s'",
3825 istk->mstk->name);
3829 * FIXME: investigate the relationship at this point between
3830 * istk->mstk and l->finishes
3833 MMacro *m = istk->mstk;
3834 istk->mstk = m->next_active;
3835 if (m->name) {
3837 * This was a real macro call, not a %rep, and
3838 * therefore the parameter information needs to
3839 * be freed.
3841 nasm_free(m->params);
3842 free_tlist(m->iline);
3843 nasm_free(m->paramlen);
3844 l->finishes->in_progress = false;
3845 } else
3846 free_mmacro(m);
3848 istk->expansion = l->next;
3849 nasm_free(l);
3850 list->downlevel(LIST_MACRO);
3853 while (1) { /* until we get a line we can use */
3855 if (istk->expansion) { /* from a macro expansion */
3856 char *p;
3857 Line *l = istk->expansion;
3858 if (istk->mstk)
3859 istk->mstk->lineno++;
3860 tline = l->first;
3861 istk->expansion = l->next;
3862 nasm_free(l);
3863 p = detoken(tline, false);
3864 list->line(LIST_MACRO, p);
3865 nasm_free(p);
3866 break;
3868 line = read_line();
3869 if (line) { /* from the current input file */
3870 line = prepreproc(line);
3871 tline = tokenize(line);
3872 nasm_free(line);
3873 break;
3876 * The current file has ended; work down the istk
3879 Include *i = istk;
3880 fclose(i->fp);
3881 if (i->conds)
3882 error(ERR_FATAL,
3883 "expected `%%endif' before end of file");
3884 /* only set line and file name if there's a next node */
3885 if (i->next) {
3886 src_set_linnum(i->lineno);
3887 nasm_free(src_set_fname(i->fname));
3889 istk = i->next;
3890 list->downlevel(LIST_INCLUDE);
3891 nasm_free(i);
3892 if (!istk)
3893 return NULL;
3898 * We must expand MMacro parameters and MMacro-local labels
3899 * _before_ we plunge into directive processing, to cope
3900 * with things like `%define something %1' such as STRUC
3901 * uses. Unless we're _defining_ a MMacro, in which case
3902 * those tokens should be left alone to go into the
3903 * definition; and unless we're in a non-emitting
3904 * condition, in which case we don't want to meddle with
3905 * anything.
3907 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3908 tline = expand_mmac_params(tline);
3911 * Check the line to see if it's a preprocessor directive.
3913 if (do_directive(tline) == DIRECTIVE_FOUND) {
3914 continue;
3915 } else if (defining) {
3917 * We're defining a multi-line macro. We emit nothing
3918 * at all, and just
3919 * shove the tokenized line on to the macro definition.
3921 Line *l = nasm_malloc(sizeof(Line));
3922 l->next = defining->expansion;
3923 l->first = tline;
3924 l->finishes = false;
3925 defining->expansion = l;
3926 continue;
3927 } else if (istk->conds && !emitting(istk->conds->state)) {
3929 * We're in a non-emitting branch of a condition block.
3930 * Emit nothing at all, not even a blank line: when we
3931 * emerge from the condition we'll give a line-number
3932 * directive so we keep our place correctly.
3934 free_tlist(tline);
3935 continue;
3936 } else if (istk->mstk && !istk->mstk->in_progress) {
3938 * We're in a %rep block which has been terminated, so
3939 * we're walking through to the %endrep without
3940 * emitting anything. Emit nothing at all, not even a
3941 * blank line: when we emerge from the %rep block we'll
3942 * give a line-number directive so we keep our place
3943 * correctly.
3945 free_tlist(tline);
3946 continue;
3947 } else {
3948 tline = expand_smacro(tline);
3949 if (!expand_mmacro(tline)) {
3951 * De-tokenize the line again, and emit it.
3953 line = detoken(tline, true);
3954 free_tlist(tline);
3955 break;
3956 } else {
3957 continue; /* expand_mmacro calls free_tlist */
3962 return line;
3965 static void pp_cleanup(int pass)
3967 if (defining) {
3968 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3969 defining->name);
3970 free_mmacro(defining);
3972 while (cstk)
3973 ctx_pop();
3974 free_macros();
3975 while (istk) {
3976 Include *i = istk;
3977 istk = istk->next;
3978 fclose(i->fp);
3979 nasm_free(i->fname);
3980 nasm_free(i);
3982 while (cstk)
3983 ctx_pop();
3984 if (pass == 0) {
3985 free_llist(predef);
3986 delete_Blocks();
3990 void pp_include_path(char *path)
3992 IncPath *i;
3994 i = nasm_malloc(sizeof(IncPath));
3995 i->path = path ? nasm_strdup(path) : NULL;
3996 i->next = NULL;
3998 if (ipath != NULL) {
3999 IncPath *j = ipath;
4000 while (j->next != NULL)
4001 j = j->next;
4002 j->next = i;
4003 } else {
4004 ipath = i;
4009 * added by alexfru:
4011 * This function is used to "export" the include paths, e.g.
4012 * the paths specified in the '-I' command switch.
4013 * The need for such exporting is due to the 'incbin' directive,
4014 * which includes raw binary files (unlike '%include', which
4015 * includes text source files). It would be real nice to be
4016 * able to specify paths to search for incbin'ned files also.
4017 * So, this is a simple workaround.
4019 * The function use is simple:
4021 * The 1st call (with NULL argument) returns a pointer to the 1st path
4022 * (char** type) or NULL if none include paths available.
4024 * All subsequent calls take as argument the value returned by this
4025 * function last. The return value is either the next path
4026 * (char** type) or NULL if the end of the paths list is reached.
4028 * It is maybe not the best way to do things, but I didn't want
4029 * to export too much, just one or two functions and no types or
4030 * variables exported.
4032 * Can't say I like the current situation with e.g. this path list either,
4033 * it seems to be never deallocated after creation...
4035 char **pp_get_include_path_ptr(char **pPrevPath)
4037 /* This macro returns offset of a member of a structure */
4038 #define GetMemberOffset(StructType,MemberName)\
4039 ((size_t)&((StructType*)0)->MemberName)
4040 IncPath *i;
4042 if (pPrevPath == NULL) {
4043 if (ipath != NULL)
4044 return &ipath->path;
4045 else
4046 return NULL;
4048 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4049 i = i->next;
4050 if (i != NULL)
4051 return &i->path;
4052 else
4053 return NULL;
4054 #undef GetMemberOffset
4057 void pp_pre_include(char *fname)
4059 Token *inc, *space, *name;
4060 Line *l;
4062 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4063 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4064 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4066 l = nasm_malloc(sizeof(Line));
4067 l->next = predef;
4068 l->first = inc;
4069 l->finishes = false;
4070 predef = l;
4073 void pp_pre_define(char *definition)
4075 Token *def, *space;
4076 Line *l;
4077 char *equals;
4079 equals = strchr(definition, '=');
4080 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4081 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4082 if (equals)
4083 *equals = ' ';
4084 space->next = tokenize(definition);
4085 if (equals)
4086 *equals = '=';
4088 l = nasm_malloc(sizeof(Line));
4089 l->next = predef;
4090 l->first = def;
4091 l->finishes = false;
4092 predef = l;
4095 void pp_pre_undefine(char *definition)
4097 Token *def, *space;
4098 Line *l;
4100 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4101 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4102 space->next = tokenize(definition);
4104 l = nasm_malloc(sizeof(Line));
4105 l->next = predef;
4106 l->first = def;
4107 l->finishes = false;
4108 predef = l;
4112 * Added by Keith Kanios:
4114 * This function is used to assist with "runtime" preprocessor
4115 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4117 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4118 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4121 void pp_runtime(char *definition)
4123 Token *def;
4125 def = tokenize(definition);
4126 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4127 free_tlist(def);
4131 void pp_extra_stdmac(const char **macros)
4133 extrastdmac = macros;
4136 static void make_tok_num(Token * tok, int64_t val)
4138 char numbuf[20];
4139 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4140 tok->text = nasm_strdup(numbuf);
4141 tok->type = TOK_NUMBER;
4144 Preproc nasmpp = {
4145 pp_reset,
4146 pp_getline,
4147 pp_cleanup