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