Makefile/dependency updates
[nasm/avx512.git] / preproc.c
blobac289ca31e74aabdfc5fa8b489290fdf9056ba9f
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 "quote.h"
52 #include "stdscan.h"
53 #include "tokens.h"
54 #include "tables.h"
56 typedef struct SMacro SMacro;
57 typedef struct MMacro MMacro;
58 typedef struct Context Context;
59 typedef struct Token Token;
60 typedef struct Blocks Blocks;
61 typedef struct Line Line;
62 typedef struct Include Include;
63 typedef struct Cond Cond;
64 typedef struct IncPath IncPath;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
78 struct SMacro {
79 SMacro *next;
80 char *name;
81 bool casesense;
82 bool in_progress;
83 unsigned int nparam;
84 Token *expansion;
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
104 struct MMacro {
105 MMacro *next;
106 char *name;
107 int nparam_min, nparam_max;
108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
111 int64_t in_progress;
112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
115 Line *expansion;
117 MMacro *next_active;
118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
121 unsigned int nparam, rotate;
122 int *paramlen;
123 uint64_t unique;
124 int lineno; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
130 struct Context {
131 Context *next;
132 char *name;
133 struct hash_table localmac;
134 uint32_t number;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
156 enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
166 struct Token {
167 Token *next;
168 char *text;
169 SMacro *mac; /* associated macro for TOK_SMAC_END */
170 enum pp_token_type type;
174 * Multi-line macro definitions are stored as a linked list of
175 * these, which is essentially a container to allow several linked
176 * lists of Tokens.
178 * Note that in this module, linked lists are treated as stacks
179 * wherever possible. For this reason, Lines are _pushed_ on to the
180 * `expansion' field in MMacro structures, so that the linked list,
181 * if walked, would give the macro lines in reverse order; this
182 * means that we can walk the list when expanding a macro, and thus
183 * push the lines on to the `expansion' field in _istk_ in reverse
184 * order (so that when popped back off they are in the right
185 * order). It may seem cockeyed, and it relies on my design having
186 * an even number of steps in, but it works...
188 * Some of these structures, rather than being actual lines, are
189 * markers delimiting the end of the expansion of a given macro.
190 * This is for use in the cycle-tracking and %rep-handling code.
191 * Such structures have `finishes' non-NULL, and `first' NULL. All
192 * others have `finishes' NULL, but `first' may still be NULL if
193 * the line is blank.
195 struct Line {
196 Line *next;
197 MMacro *finishes;
198 Token *first;
202 * To handle an arbitrary level of file inclusion, we maintain a
203 * stack (ie linked list) of these things.
205 struct Include {
206 Include *next;
207 FILE *fp;
208 Cond *conds;
209 Line *expansion;
210 char *fname;
211 int lineno, lineinc;
212 MMacro *mstk; /* stack of active macros/reps */
216 * Include search path. This is simply a list of strings which get
217 * prepended, in turn, to the name of an include file, in an
218 * attempt to find the file if it's not in the current directory.
220 struct IncPath {
221 IncPath *next;
222 char *path;
226 * Conditional assembly: we maintain a separate stack of these for
227 * each level of file inclusion. (The only reason we keep the
228 * stacks separate is to ensure that a stray `%endif' in a file
229 * included from within the true branch of a `%if' won't terminate
230 * it and cause confusion: instead, rightly, it'll cause an error.)
232 struct Cond {
233 Cond *next;
234 int state;
236 enum {
238 * These states are for use just after %if or %elif: IF_TRUE
239 * means the condition has evaluated to truth so we are
240 * currently emitting, whereas IF_FALSE means we are not
241 * currently emitting but will start doing so if a %else comes
242 * up. In these states, all directives are admissible: %elif,
243 * %else and %endif. (And of course %if.)
245 COND_IF_TRUE, COND_IF_FALSE,
247 * These states come up after a %else: ELSE_TRUE means we're
248 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
249 * any %elif or %else will cause an error.
251 COND_ELSE_TRUE, COND_ELSE_FALSE,
253 * This state means that we're not emitting now, and also that
254 * nothing until %endif will be emitted at all. It's for use in
255 * two circumstances: (i) when we've had our moment of emission
256 * and have now started seeing %elifs, and (ii) when the
257 * condition construct in question is contained within a
258 * non-emitting branch of a larger condition construct.
260 COND_NEVER
262 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
265 * These defines are used as the possible return values for do_directive
267 #define NO_DIRECTIVE_FOUND 0
268 #define DIRECTIVE_FOUND 1
271 * Condition codes. Note that we use c_ prefix not C_ because C_ is
272 * used in nasm.h for the "real" condition codes. At _this_ level,
273 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
274 * ones, so we need a different enum...
276 static const char * const conditions[] = {
277 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
278 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
279 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
281 enum pp_conds {
282 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
283 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
284 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
285 c_none = -1
287 static const enum pp_conds inverse_ccs[] = {
288 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
289 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,
290 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
294 * Directive names.
296 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
297 static int is_condition(enum preproc_token arg)
299 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
302 /* For TASM compatibility we need to be able to recognise TASM compatible
303 * conditional compilation directives. Using the NASM pre-processor does
304 * not work, so we look for them specifically from the following list and
305 * then jam in the equivalent NASM directive into the input stream.
308 enum {
309 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
310 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
313 static const char * const tasm_directives[] = {
314 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
315 "ifndef", "include", "local"
318 static int StackSize = 4;
319 static char *StackPointer = "ebp";
320 static int ArgOffset = 8;
321 static int LocalOffset = 0;
323 static Context *cstk;
324 static Include *istk;
325 static IncPath *ipath = NULL;
327 static efunc _error; /* Pointer to client-provided error reporting function */
328 static evalfunc evaluate;
330 static int pass; /* HACK: pass 0 = generate dependencies only */
331 static StrList **dephead, **deptail; /* Dependency list */
333 static uint64_t unique; /* unique identifier numbers */
335 static Line *predef = NULL;
337 static ListGen *list;
340 * The current set of multi-line macros we have defined.
342 static struct hash_table mmacros;
345 * The current set of single-line macros we have defined.
347 static struct hash_table smacros;
350 * The multi-line macro we are currently defining, or the %rep
351 * block we are currently reading, if any.
353 static MMacro *defining;
356 * The number of macro parameters to allocate space for at a time.
358 #define PARAM_DELTA 16
361 * The standard macro set: defined in macros.c in the array nasm_stdmac.
362 * This gives our position in the macro set, when we're processing it.
364 static const char * const *stdmacpos;
367 * The extra standard macros that come from the object format, if
368 * any.
370 static const char * const *extrastdmac = NULL;
371 bool any_extrastdmac;
374 * Tokens are allocated in blocks to improve speed
376 #define TOKEN_BLOCKSIZE 4096
377 static Token *freeTokens = NULL;
378 struct Blocks {
379 Blocks *next;
380 void *chunk;
383 static Blocks blocks = { NULL, NULL };
386 * Forward declarations.
388 static Token *expand_mmac_params(Token * tline);
389 static Token *expand_smacro(Token * tline);
390 static Token *expand_id(Token * tline);
391 static Context *get_ctx(char *name, bool all_contexts);
392 static void make_tok_num(Token * tok, int64_t val);
393 static void error(int severity, const char *fmt, ...);
394 static void *new_Block(size_t size);
395 static void delete_Blocks(void);
396 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
397 static Token *delete_Token(Token * t);
400 * Macros for safe checking of token pointers, avoid *(NULL)
402 #define tok_type_(x,t) ((x) && (x)->type == (t))
403 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
404 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
405 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
407 /* Handle TASM specific directives, which do not contain a % in
408 * front of them. We do it here because I could not find any other
409 * place to do it for the moment, and it is a hack (ideally it would
410 * be nice to be able to use the NASM pre-processor to do it).
412 static char *check_tasm_directive(char *line)
414 int32_t i, j, k, m, len;
415 char *p = line, *oldline, oldchar;
417 /* Skip whitespace */
418 while (isspace(*p) && *p != 0)
419 p++;
421 /* Binary search for the directive name */
422 i = -1;
423 j = elements(tasm_directives);
424 len = 0;
425 while (!isspace(p[len]) && p[len] != 0)
426 len++;
427 if (len) {
428 oldchar = p[len];
429 p[len] = 0;
430 while (j - i > 1) {
431 k = (j + i) / 2;
432 m = nasm_stricmp(p, tasm_directives[k]);
433 if (m == 0) {
434 /* We have found a directive, so jam a % in front of it
435 * so that NASM will then recognise it as one if it's own.
437 p[len] = oldchar;
438 len = strlen(p);
439 oldline = line;
440 line = nasm_malloc(len + 2);
441 line[0] = '%';
442 if (k == TM_IFDIFI) {
443 /* NASM does not recognise IFDIFI, so we convert it to
444 * %ifdef BOGUS. This is not used in NASM comaptible
445 * code, but does need to parse for the TASM macro
446 * package.
448 strcpy(line + 1, "ifdef BOGUS");
449 } else {
450 memcpy(line + 1, p, len + 1);
452 nasm_free(oldline);
453 return line;
454 } else if (m < 0) {
455 j = k;
456 } else
457 i = k;
459 p[len] = oldchar;
461 return line;
465 * The pre-preprocessing stage... This function translates line
466 * number indications as they emerge from GNU cpp (`# lineno "file"
467 * flags') into NASM preprocessor line number indications (`%line
468 * lineno file').
470 static char *prepreproc(char *line)
472 int lineno, fnlen;
473 char *fname, *oldline;
475 if (line[0] == '#' && line[1] == ' ') {
476 oldline = line;
477 fname = oldline + 2;
478 lineno = atoi(fname);
479 fname += strspn(fname, "0123456789 ");
480 if (*fname == '"')
481 fname++;
482 fnlen = strcspn(fname, "\"");
483 line = nasm_malloc(20 + fnlen);
484 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
485 nasm_free(oldline);
487 if (tasm_compatible_mode)
488 return check_tasm_directive(line);
489 return line;
493 * Free a linked list of tokens.
495 static void free_tlist(Token * list)
497 while (list) {
498 list = delete_Token(list);
503 * Free a linked list of lines.
505 static void free_llist(Line * list)
507 Line *l;
508 while (list) {
509 l = list;
510 list = list->next;
511 free_tlist(l->first);
512 nasm_free(l);
517 * Free an MMacro
519 static void free_mmacro(MMacro * m)
521 nasm_free(m->name);
522 free_tlist(m->dlist);
523 nasm_free(m->defaults);
524 free_llist(m->expansion);
525 nasm_free(m);
529 * Free all currently defined macros, and free the hash tables
531 static void free_smacro_table(struct hash_table *smt)
533 SMacro *s;
534 const char *key;
535 struct hash_tbl_node *it = NULL;
537 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
538 nasm_free((void *)key);
539 while (s) {
540 SMacro *ns = s->next;
541 nasm_free(s->name);
542 free_tlist(s->expansion);
543 nasm_free(s);
544 s = ns;
547 hash_free(smt);
550 static void free_mmacro_table(struct hash_table *mmt)
552 MMacro *m;
553 const char *key;
554 struct hash_tbl_node *it = NULL;
556 it = NULL;
557 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
558 nasm_free((void *)key);
559 while (m) {
560 MMacro *nm = m->next;
561 free_mmacro(m);
562 m = nm;
565 hash_free(mmt);
568 static void free_macros(void)
570 free_smacro_table(&smacros);
571 free_mmacro_table(&mmacros);
575 * Initialize the hash tables
577 static void init_macros(void)
579 hash_init(&smacros, HASH_LARGE);
580 hash_init(&mmacros, HASH_LARGE);
584 * Pop the context stack.
586 static void ctx_pop(void)
588 Context *c = cstk;
590 cstk = cstk->next;
591 free_smacro_table(&c->localmac);
592 nasm_free(c->name);
593 nasm_free(c);
597 * Search for a key in the hash index; adding it if necessary
598 * (in which case we initialize the data pointer to NULL.)
600 static void **
601 hash_findi_add(struct hash_table *hash, const char *str)
603 struct hash_insert hi;
604 void **r;
605 char *strx;
607 r = hash_findi(hash, str, &hi);
608 if (r)
609 return r;
611 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
612 return hash_add(&hi, strx, NULL);
616 * Like hash_findi, but returns the data element rather than a pointer
617 * to it. Used only when not adding a new element, hence no third
618 * argument.
620 static void *
621 hash_findix(struct hash_table *hash, const char *str)
623 void **p;
625 p = hash_findi(hash, str, NULL);
626 return p ? *p : NULL;
629 #define BUF_DELTA 512
631 * Read a line from the top file in istk, handling multiple CR/LFs
632 * at the end of the line read, and handling spurious ^Zs. Will
633 * return lines from the standard macro set if this has not already
634 * been done.
636 static char *read_line(void)
638 char *buffer, *p, *q;
639 int bufsize, continued_count;
641 if (stdmacpos) {
642 if (*stdmacpos) {
643 char *ret = nasm_strdup(*stdmacpos++);
644 if (!*stdmacpos && any_extrastdmac) {
645 stdmacpos = extrastdmac;
646 any_extrastdmac = false;
647 return ret;
650 * Nasty hack: here we push the contents of `predef' on
651 * to the top-level expansion stack, since this is the
652 * most convenient way to implement the pre-include and
653 * pre-define features.
655 if (!*stdmacpos) {
656 Line *pd, *l;
657 Token *head, **tail, *t;
659 for (pd = predef; pd; pd = pd->next) {
660 head = NULL;
661 tail = &head;
662 for (t = pd->first; t; t = t->next) {
663 *tail = new_Token(NULL, t->type, t->text, 0);
664 tail = &(*tail)->next;
666 l = nasm_malloc(sizeof(Line));
667 l->next = istk->expansion;
668 l->first = head;
669 l->finishes = false;
670 istk->expansion = l;
673 return ret;
674 } else {
675 stdmacpos = NULL;
679 bufsize = BUF_DELTA;
680 buffer = nasm_malloc(BUF_DELTA);
681 p = buffer;
682 continued_count = 0;
683 while (1) {
684 q = fgets(p, bufsize - (p - buffer), istk->fp);
685 if (!q)
686 break;
687 p += strlen(p);
688 if (p > buffer && p[-1] == '\n') {
689 /* Convert backslash-CRLF line continuation sequences into
690 nothing at all (for DOS and Windows) */
691 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
692 p -= 3;
693 *p = 0;
694 continued_count++;
696 /* Also convert backslash-LF line continuation sequences into
697 nothing at all (for Unix) */
698 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
699 p -= 2;
700 *p = 0;
701 continued_count++;
702 } else {
703 break;
706 if (p - buffer > bufsize - 10) {
707 int32_t offset = p - buffer;
708 bufsize += BUF_DELTA;
709 buffer = nasm_realloc(buffer, bufsize);
710 p = buffer + offset; /* prevent stale-pointer problems */
714 if (!q && p == buffer) {
715 nasm_free(buffer);
716 return NULL;
719 src_set_linnum(src_get_linnum() + istk->lineinc +
720 (continued_count * istk->lineinc));
723 * Play safe: remove CRs as well as LFs, if any of either are
724 * present at the end of the line.
726 while (--p >= buffer && (*p == '\n' || *p == '\r'))
727 *p = '\0';
730 * Handle spurious ^Z, which may be inserted into source files
731 * by some file transfer utilities.
733 buffer[strcspn(buffer, "\032")] = '\0';
735 list->line(LIST_READ, buffer);
737 return buffer;
741 * Tokenize a line of text. This is a very simple process since we
742 * don't need to parse the value out of e.g. numeric tokens: we
743 * simply split one string into many.
745 static Token *tokenize(char *line)
747 char *p = line;
748 enum pp_token_type type;
749 Token *list = NULL;
750 Token *t, **tail = &list;
752 while (*line) {
753 p = line;
754 if (*p == '%') {
755 p++;
756 if (isdigit(*p) ||
757 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
758 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
759 do {
760 p++;
762 while (isdigit(*p));
763 type = TOK_PREPROC_ID;
764 } else if (*p == '{') {
765 p++;
766 while (*p && *p != '}') {
767 p[-1] = *p;
768 p++;
770 p[-1] = '\0';
771 if (*p)
772 p++;
773 type = TOK_PREPROC_ID;
774 } else if (*p == '?') {
775 type = TOK_PREPROC_Q; /* %? */
776 p++;
777 if (*p == '?') {
778 type = TOK_PREPROC_QQ; /* %?? */
779 p++;
781 } else if (isidchar(*p) ||
782 ((*p == '!' || *p == '%' || *p == '$') &&
783 isidchar(p[1]))) {
784 do {
785 p++;
787 while (isidchar(*p));
788 type = TOK_PREPROC_ID;
789 } else {
790 type = TOK_OTHER;
791 if (*p == '%')
792 p++;
794 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
795 type = TOK_ID;
796 p++;
797 while (*p && isidchar(*p))
798 p++;
799 } else if (*p == '\'' || *p == '"' || *p == '`') {
801 * A string token.
803 type = TOK_STRING;
804 p = nasm_skip_string(p);
806 if (*p) {
807 p++;
808 } else {
809 error(ERR_WARNING, "unterminated string");
810 /* Handling unterminated strings by UNV */
811 /* type = -1; */
813 } else if (isnumstart(*p)) {
814 bool is_hex = false;
815 bool is_float = false;
816 bool has_e = false;
817 char c, *r;
820 * A numeric token.
823 if (*p == '$') {
824 p++;
825 is_hex = true;
828 for (;;) {
829 c = *p++;
831 if (!is_hex && (c == 'e' || c == 'E')) {
832 has_e = true;
833 if (*p == '+' || *p == '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
836 p++;
837 is_float = true;
839 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
840 is_hex = true;
841 } else if (c == 'P' || c == 'p') {
842 is_float = true;
843 if (*p == '+' || *p == '-')
844 p++;
845 } else if (isnumchar(c) || c == '_')
846 ; /* just advance */
847 else if (c == '.') {
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
856 r = p;
857 while (*r == '_')
858 r++;
860 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
861 (!is_hex && (*r == 'e' || *r == 'E')) ||
862 (*r == 'p' || *r == 'P')) {
863 p = r;
864 is_float = true;
865 } else
866 break; /* Terminate the token */
867 } else
868 break;
870 p--; /* Point to first character beyond number */
872 if (has_e && !is_hex) {
873 /* 1e13 is floating-point, but 1e13h is not */
874 is_float = true;
877 type = is_float ? TOK_FLOAT : TOK_NUMBER;
878 } else if (isspace(*p)) {
879 type = TOK_WHITESPACE;
880 p++;
881 while (*p && isspace(*p))
882 p++;
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
888 if (!*p || *p == ';') {
889 type = TOK_COMMENT;
890 while (*p)
891 p++;
893 } else if (*p == ';') {
894 type = TOK_COMMENT;
895 while (*p)
896 p++;
897 } else {
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
902 * else is a single-character operator.
904 type = TOK_OTHER;
905 if ((p[0] == '>' && p[1] == '>') ||
906 (p[0] == '<' && p[1] == '<') ||
907 (p[0] == '/' && p[1] == '/') ||
908 (p[0] == '<' && p[1] == '=') ||
909 (p[0] == '>' && p[1] == '=') ||
910 (p[0] == '=' && p[1] == '=') ||
911 (p[0] == '!' && p[1] == '=') ||
912 (p[0] == '<' && p[1] == '>') ||
913 (p[0] == '&' && p[1] == '&') ||
914 (p[0] == '|' && p[1] == '|') ||
915 (p[0] == '^' && p[1] == '^')) {
916 p++;
918 p++;
921 /* Handling unterminated string by UNV */
922 /*if (type == -1)
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
926 tail = &t->next;
928 else */
929 if (type != TOK_COMMENT) {
930 *tail = t = new_Token(NULL, type, line, p - line);
931 tail = &t->next;
933 line = p;
935 return list;
939 * this function allocates a new managed block of memory and
940 * returns a pointer to the block. The managed blocks are
941 * deleted only all at once by the delete_Blocks function.
943 static void *new_Block(size_t size)
945 Blocks *b = &blocks;
947 /* first, get to the end of the linked list */
948 while (b->next)
949 b = b->next;
950 /* now allocate the requested chunk */
951 b->chunk = nasm_malloc(size);
953 /* now allocate a new block for the next request */
954 b->next = nasm_malloc(sizeof(Blocks));
955 /* and initialize the contents of the new block */
956 b->next->next = NULL;
957 b->next->chunk = NULL;
958 return b->chunk;
962 * this function deletes all managed blocks of memory
964 static void delete_Blocks(void)
966 Blocks *a, *b = &blocks;
969 * keep in mind that the first block, pointed to by blocks
970 * is a static and not dynamically allocated, so we don't
971 * free it.
973 while (b) {
974 if (b->chunk)
975 nasm_free(b->chunk);
976 a = b;
977 b = b->next;
978 if (a != &blocks)
979 nasm_free(a);
984 * this function creates a new Token and passes a pointer to it
985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
988 static Token *new_Token(Token * next, enum pp_token_type type,
989 char *text, int txtlen)
991 Token *t;
992 int i;
994 if (freeTokens == NULL) {
995 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
996 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
997 freeTokens[i].next = &freeTokens[i + 1];
998 freeTokens[i].next = NULL;
1000 t = freeTokens;
1001 freeTokens = t->next;
1002 t->next = next;
1003 t->mac = NULL;
1004 t->type = type;
1005 if (type == TOK_WHITESPACE || text == NULL) {
1006 t->text = NULL;
1007 } else {
1008 if (txtlen == 0)
1009 txtlen = strlen(text);
1010 t->text = nasm_malloc(txtlen+1);
1011 memcpy(t->text, text, txtlen);
1012 t->text[txtlen] = '\0';
1014 return t;
1017 static Token *delete_Token(Token * t)
1019 Token *next = t->next;
1020 nasm_free(t->text);
1021 t->next = freeTokens;
1022 freeTokens = t;
1023 return next;
1027 * Convert a line of tokens back into text.
1028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
1031 static char *detoken(Token * tlist, int expand_locals)
1033 Token *t;
1034 int len;
1035 char *line, *p;
1036 const char *q;
1038 len = 0;
1039 for (t = tlist; t; t = t->next) {
1040 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1041 char *p = getenv(t->text + 2);
1042 nasm_free(t->text);
1043 if (p)
1044 t->text = nasm_strdup(p);
1045 else
1046 t->text = NULL;
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals &&
1050 t->type == TOK_PREPROC_ID && t->text &&
1051 t->text[0] == '%' && t->text[1] == '$') {
1052 Context *ctx = get_ctx(t->text, false);
1053 if (ctx) {
1054 char buffer[40];
1055 char *p, *q = t->text + 2;
1057 q += strspn(q, "$");
1058 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1059 p = nasm_strcat(buffer, q);
1060 nasm_free(t->text);
1061 t->text = p;
1064 if (t->type == TOK_WHITESPACE) {
1065 len++;
1066 } else if (t->text) {
1067 len += strlen(t->text);
1070 p = line = nasm_malloc(len + 1);
1071 for (t = tlist; t; t = t->next) {
1072 if (t->type == TOK_WHITESPACE) {
1073 *p++ = ' ';
1074 } else if (t->text) {
1075 q = t->text;
1076 while (*q)
1077 *p++ = *q++;
1080 *p = '\0';
1081 return line;
1085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1088 * field.
1090 * FIX: This really needs to be unified with stdscan.
1092 static int ppscan(void *private_data, struct tokenval *tokval)
1094 Token **tlineptr = private_data;
1095 Token *tline;
1096 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1098 do {
1099 tline = *tlineptr;
1100 *tlineptr = tline ? tline->next : NULL;
1102 while (tline && (tline->type == TOK_WHITESPACE ||
1103 tline->type == TOK_COMMENT));
1105 if (!tline)
1106 return tokval->t_type = TOKEN_EOS;
1108 tokval->t_charptr = tline->text;
1110 if (tline->text[0] == '$' && !tline->text[1])
1111 return tokval->t_type = TOKEN_HERE;
1112 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1113 return tokval->t_type = TOKEN_BASE;
1115 if (tline->type == TOK_ID) {
1116 p = tokval->t_charptr = tline->text;
1117 if (p[0] == '$') {
1118 tokval->t_charptr++;
1119 return tokval->t_type = TOKEN_ID;
1122 for (r = p, s = ourcopy; *r; r++) {
1123 if (r >= p+MAX_KEYWORD)
1124 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1125 *s++ = tolower(*r);
1127 *s = '\0';
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy, tokval);
1133 if (tline->type == TOK_NUMBER) {
1134 bool rn_error;
1135 tokval->t_integer = readnum(tline->text, &rn_error);
1136 if (rn_error)
1137 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1138 tokval->t_charptr = tline->text;
1139 return tokval->t_type = TOKEN_NUM;
1142 if (tline->type == TOK_FLOAT) {
1143 return tokval->t_type = TOKEN_FLOAT;
1146 if (tline->type == TOK_STRING) {
1147 bool rn_warn;
1148 size_t l;
1150 l = nasm_unquote(tline->text);
1151 /* TOKEN_ERRNUM if improperly quoted... */
1153 tokval->t_integer = readstrnum(tline->text, l, &rn_warn);
1154 if (rn_warn)
1155 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1156 tokval->t_charptr = NULL;
1157 return tokval->t_type = TOKEN_NUM;
1160 if (tline->type == TOK_OTHER) {
1161 if (!strcmp(tline->text, "<<"))
1162 return tokval->t_type = TOKEN_SHL;
1163 if (!strcmp(tline->text, ">>"))
1164 return tokval->t_type = TOKEN_SHR;
1165 if (!strcmp(tline->text, "//"))
1166 return tokval->t_type = TOKEN_SDIV;
1167 if (!strcmp(tline->text, "%%"))
1168 return tokval->t_type = TOKEN_SMOD;
1169 if (!strcmp(tline->text, "=="))
1170 return tokval->t_type = TOKEN_EQ;
1171 if (!strcmp(tline->text, "<>"))
1172 return tokval->t_type = TOKEN_NE;
1173 if (!strcmp(tline->text, "!="))
1174 return tokval->t_type = TOKEN_NE;
1175 if (!strcmp(tline->text, "<="))
1176 return tokval->t_type = TOKEN_LE;
1177 if (!strcmp(tline->text, ">="))
1178 return tokval->t_type = TOKEN_GE;
1179 if (!strcmp(tline->text, "&&"))
1180 return tokval->t_type = TOKEN_DBL_AND;
1181 if (!strcmp(tline->text, "^^"))
1182 return tokval->t_type = TOKEN_DBL_XOR;
1183 if (!strcmp(tline->text, "||"))
1184 return tokval->t_type = TOKEN_DBL_OR;
1188 * We have no other options: just return the first character of
1189 * the token text.
1191 return tokval->t_type = tline->text[0];
1195 * Compare a string to the name of an existing macro; this is a
1196 * simple wrapper which calls either strcmp or nasm_stricmp
1197 * depending on the value of the `casesense' parameter.
1199 static int mstrcmp(const char *p, const char *q, bool casesense)
1201 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1205 * Compare a string to the name of an existing macro; this is a
1206 * simple wrapper which calls either strcmp or nasm_stricmp
1207 * depending on the value of the `casesense' parameter.
1209 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1211 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1215 * Return the Context structure associated with a %$ token. Return
1216 * NULL, having _already_ reported an error condition, if the
1217 * context stack isn't deep enough for the supplied number of $
1218 * signs.
1219 * If all_contexts == true, contexts that enclose current are
1220 * also scanned for such smacro, until it is found; if not -
1221 * only the context that directly results from the number of $'s
1222 * in variable's name.
1224 static Context *get_ctx(char *name, bool all_contexts)
1226 Context *ctx;
1227 SMacro *m;
1228 int i;
1230 if (!name || name[0] != '%' || name[1] != '$')
1231 return NULL;
1233 if (!cstk) {
1234 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1235 return NULL;
1238 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1239 ctx = ctx->next;
1240 /* i--; Lino - 02/25/02 */
1242 if (!ctx) {
1243 error(ERR_NONFATAL, "`%s': context stack is only"
1244 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1245 return NULL;
1247 if (!all_contexts)
1248 return ctx;
1250 do {
1251 /* Search for this smacro in found context */
1252 m = hash_findix(&ctx->localmac, name);
1253 while (m) {
1254 if (!mstrcmp(m->name, name, m->casesense))
1255 return ctx;
1256 m = m->next;
1258 ctx = ctx->next;
1260 while (ctx);
1261 return NULL;
1265 * Check to see if a file is already in a string list
1267 static bool in_list(const StrList *list, const char *str)
1269 while (list) {
1270 if (!strcmp(list->str, str))
1271 return true;
1272 list = list->next;
1274 return false;
1278 * Open an include file. This routine must always return a valid
1279 * file pointer if it returns - it's responsible for throwing an
1280 * ERR_FATAL and bombing out completely if not. It should also try
1281 * the include path one by one until it finds the file or reaches
1282 * the end of the path.
1284 static FILE *inc_fopen(const char *file, StrList **dhead, StrList **dtail,
1285 bool missing_ok)
1287 FILE *fp;
1288 char *prefix = "";
1289 IncPath *ip = ipath;
1290 int len = strlen(file);
1291 size_t prefix_len = 0;
1292 StrList *sl;
1294 while (1) {
1295 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1296 memcpy(sl->str, prefix, prefix_len);
1297 memcpy(sl->str+prefix_len, file, len+1);
1298 fp = fopen(sl->str, "r");
1299 if (fp && dhead && !in_list(*dhead, sl->str)) {
1300 sl->next = NULL;
1301 *dtail = sl;
1302 dtail = &sl->next;
1303 } else {
1304 nasm_free(sl);
1306 if (fp)
1307 return fp;
1308 if (!ip) {
1309 if (!missing_ok)
1310 break;
1311 prefix = NULL;
1312 } else {
1313 prefix = ip->path;
1314 ip = ip->next;
1316 if (prefix) {
1317 prefix_len = strlen(prefix);
1318 } else {
1319 /* -MG given and file not found */
1320 if (dhead && !in_list(*dhead, file)) {
1321 sl = nasm_malloc(len+1+sizeof sl->next);
1322 sl->next = NULL;
1323 strcpy(sl->str, file);
1324 *dtail = sl;
1325 dtail = &sl->next;
1327 return NULL;
1331 error(ERR_FATAL, "unable to open include file `%s'", file);
1332 return NULL; /* never reached - placate compilers */
1336 * Determine if we should warn on defining a single-line macro of
1337 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1338 * return true if _any_ single-line macro of that name is defined.
1339 * Otherwise, will return true if a single-line macro with either
1340 * `nparam' or no parameters is defined.
1342 * If a macro with precisely the right number of parameters is
1343 * defined, or nparam is -1, the address of the definition structure
1344 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1345 * is NULL, no action will be taken regarding its contents, and no
1346 * error will occur.
1348 * Note that this is also called with nparam zero to resolve
1349 * `ifdef'.
1351 * If you already know which context macro belongs to, you can pass
1352 * the context pointer as first parameter; if you won't but name begins
1353 * with %$ the context will be automatically computed. If all_contexts
1354 * is true, macro will be searched in outer contexts as well.
1356 static bool
1357 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1358 bool nocase)
1360 struct hash_table *smtbl;
1361 SMacro *m;
1363 if (ctx) {
1364 smtbl = &ctx->localmac;
1365 } else if (name[0] == '%' && name[1] == '$') {
1366 if (cstk)
1367 ctx = get_ctx(name, false);
1368 if (!ctx)
1369 return false; /* got to return _something_ */
1370 smtbl = &ctx->localmac;
1371 } else {
1372 smtbl = &smacros;
1374 m = (SMacro *) hash_findix(smtbl, name);
1376 while (m) {
1377 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1378 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1379 if (defn) {
1380 if (nparam == (int) m->nparam || nparam == -1)
1381 *defn = m;
1382 else
1383 *defn = NULL;
1385 return true;
1387 m = m->next;
1390 return false;
1394 * Count and mark off the parameters in a multi-line macro call.
1395 * This is called both from within the multi-line macro expansion
1396 * code, and also to mark off the default parameters when provided
1397 * in a %macro definition line.
1399 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1401 int paramsize, brace;
1403 *nparam = paramsize = 0;
1404 *params = NULL;
1405 while (t) {
1406 if (*nparam >= paramsize) {
1407 paramsize += PARAM_DELTA;
1408 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1410 skip_white_(t);
1411 brace = false;
1412 if (tok_is_(t, "{"))
1413 brace = true;
1414 (*params)[(*nparam)++] = t;
1415 while (tok_isnt_(t, brace ? "}" : ","))
1416 t = t->next;
1417 if (t) { /* got a comma/brace */
1418 t = t->next;
1419 if (brace) {
1421 * Now we've found the closing brace, look further
1422 * for the comma.
1424 skip_white_(t);
1425 if (tok_isnt_(t, ",")) {
1426 error(ERR_NONFATAL,
1427 "braces do not enclose all of macro parameter");
1428 while (tok_isnt_(t, ","))
1429 t = t->next;
1431 if (t)
1432 t = t->next; /* eat the comma */
1439 * Determine whether one of the various `if' conditions is true or
1440 * not.
1442 * We must free the tline we get passed.
1444 static bool if_condition(Token * tline, enum preproc_token ct)
1446 enum pp_conditional i = PP_COND(ct);
1447 bool j;
1448 Token *t, *tt, **tptr, *origline;
1449 struct tokenval tokval;
1450 expr *evalresult;
1451 enum pp_token_type needtype;
1453 origline = tline;
1455 switch (i) {
1456 case PPC_IFCTX:
1457 j = false; /* have we matched yet? */
1458 while (cstk && tline) {
1459 skip_white_(tline);
1460 if (!tline || tline->type != TOK_ID) {
1461 error(ERR_NONFATAL,
1462 "`%s' expects context identifiers", pp_directives[ct]);
1463 free_tlist(origline);
1464 return -1;
1466 if (!nasm_stricmp(tline->text, cstk->name))
1467 j = true;
1468 tline = tline->next;
1470 break;
1472 case PPC_IFDEF:
1473 j = false; /* have we matched yet? */
1474 while (tline) {
1475 skip_white_(tline);
1476 if (!tline || (tline->type != TOK_ID &&
1477 (tline->type != TOK_PREPROC_ID ||
1478 tline->text[1] != '$'))) {
1479 error(ERR_NONFATAL,
1480 "`%s' expects macro identifiers", pp_directives[ct]);
1481 goto fail;
1483 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1484 j = true;
1485 tline = tline->next;
1487 break;
1489 case PPC_IFIDN:
1490 case PPC_IFIDNI:
1491 tline = expand_smacro(tline);
1492 t = tt = tline;
1493 while (tok_isnt_(tt, ","))
1494 tt = tt->next;
1495 if (!tt) {
1496 error(ERR_NONFATAL,
1497 "`%s' expects two comma-separated arguments",
1498 pp_directives[ct]);
1499 goto fail;
1501 tt = tt->next;
1502 j = true; /* assume equality unless proved not */
1503 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1504 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1505 error(ERR_NONFATAL, "`%s': more than one comma on line",
1506 pp_directives[ct]);
1507 goto fail;
1509 if (t->type == TOK_WHITESPACE) {
1510 t = t->next;
1511 continue;
1513 if (tt->type == TOK_WHITESPACE) {
1514 tt = tt->next;
1515 continue;
1517 if (tt->type != t->type) {
1518 j = false; /* found mismatching tokens */
1519 break;
1521 /* When comparing strings, need to unquote them first */
1522 if (t->type == TOK_STRING) {
1523 size_t l1 = nasm_unquote(t->text);
1524 size_t l2 = nasm_unquote(tt->text);
1526 if (l1 != l2) {
1527 j = false;
1528 break;
1530 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1531 j = false;
1532 break;
1534 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1535 j = false; /* found mismatching tokens */
1536 break;
1539 t = t->next;
1540 tt = tt->next;
1542 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1543 j = false; /* trailing gunk on one end or other */
1544 break;
1546 case PPC_IFMACRO:
1548 bool found = false;
1549 MMacro searching, *mmac;
1551 tline = tline->next;
1552 skip_white_(tline);
1553 tline = expand_id(tline);
1554 if (!tok_type_(tline, TOK_ID)) {
1555 error(ERR_NONFATAL,
1556 "`%s' expects a macro name", pp_directives[ct]);
1557 goto fail;
1559 searching.name = nasm_strdup(tline->text);
1560 searching.casesense = true;
1561 searching.plus = false;
1562 searching.nolist = false;
1563 searching.in_progress = 0;
1564 searching.rep_nest = NULL;
1565 searching.nparam_min = 0;
1566 searching.nparam_max = INT_MAX;
1567 tline = expand_smacro(tline->next);
1568 skip_white_(tline);
1569 if (!tline) {
1570 } else if (!tok_type_(tline, TOK_NUMBER)) {
1571 error(ERR_NONFATAL,
1572 "`%s' expects a parameter count or nothing",
1573 pp_directives[ct]);
1574 } else {
1575 searching.nparam_min = searching.nparam_max =
1576 readnum(tline->text, &j);
1577 if (j)
1578 error(ERR_NONFATAL,
1579 "unable to parse parameter count `%s'",
1580 tline->text);
1582 if (tline && tok_is_(tline->next, "-")) {
1583 tline = tline->next->next;
1584 if (tok_is_(tline, "*"))
1585 searching.nparam_max = INT_MAX;
1586 else if (!tok_type_(tline, TOK_NUMBER))
1587 error(ERR_NONFATAL,
1588 "`%s' expects a parameter count after `-'",
1589 pp_directives[ct]);
1590 else {
1591 searching.nparam_max = readnum(tline->text, &j);
1592 if (j)
1593 error(ERR_NONFATAL,
1594 "unable to parse parameter count `%s'",
1595 tline->text);
1596 if (searching.nparam_min > searching.nparam_max)
1597 error(ERR_NONFATAL,
1598 "minimum parameter count exceeds maximum");
1601 if (tline && tok_is_(tline->next, "+")) {
1602 tline = tline->next;
1603 searching.plus = true;
1605 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1606 while (mmac) {
1607 if (!strcmp(mmac->name, searching.name) &&
1608 (mmac->nparam_min <= searching.nparam_max
1609 || searching.plus)
1610 && (searching.nparam_min <= mmac->nparam_max
1611 || mmac->plus)) {
1612 found = true;
1613 break;
1615 mmac = mmac->next;
1617 nasm_free(searching.name);
1618 j = found;
1619 break;
1622 case PPC_IFID:
1623 needtype = TOK_ID;
1624 goto iftype;
1625 case PPC_IFNUM:
1626 needtype = TOK_NUMBER;
1627 goto iftype;
1628 case PPC_IFSTR:
1629 needtype = TOK_STRING;
1630 goto iftype;
1632 iftype:
1633 t = tline = expand_smacro(tline);
1635 while (tok_type_(t, TOK_WHITESPACE) ||
1636 (needtype == TOK_NUMBER &&
1637 tok_type_(t, TOK_OTHER) &&
1638 (t->text[0] == '-' || t->text[0] == '+') &&
1639 !t->text[1]))
1640 t = t->next;
1642 j = tok_type_(t, needtype);
1643 break;
1645 case PPC_IFTOKEN:
1646 t = tline = expand_smacro(tline);
1647 while (tok_type_(t, TOK_WHITESPACE))
1648 t = t->next;
1650 j = false;
1651 if (t) {
1652 t = t->next; /* Skip the actual token */
1653 while (tok_type_(t, TOK_WHITESPACE))
1654 t = t->next;
1655 j = !t; /* Should be nothing left */
1657 break;
1659 case PPC_IFEMPTY:
1660 t = tline = expand_smacro(tline);
1661 while (tok_type_(t, TOK_WHITESPACE))
1662 t = t->next;
1664 j = !t; /* Should be empty */
1665 break;
1667 case PPC_IF:
1668 t = tline = expand_smacro(tline);
1669 tptr = &t;
1670 tokval.t_type = TOKEN_INVALID;
1671 evalresult = evaluate(ppscan, tptr, &tokval,
1672 NULL, pass | CRITICAL, error, NULL);
1673 if (!evalresult)
1674 return -1;
1675 if (tokval.t_type)
1676 error(ERR_WARNING,
1677 "trailing garbage after expression ignored");
1678 if (!is_simple(evalresult)) {
1679 error(ERR_NONFATAL,
1680 "non-constant value given to `%s'", pp_directives[ct]);
1681 goto fail;
1683 j = reloc_value(evalresult) != 0;
1684 return j;
1686 default:
1687 error(ERR_FATAL,
1688 "preprocessor directive `%s' not yet implemented",
1689 pp_directives[ct]);
1690 goto fail;
1693 free_tlist(origline);
1694 return j ^ PP_NEGATIVE(ct);
1696 fail:
1697 free_tlist(origline);
1698 return -1;
1702 * Expand macros in a string. Used in %error directives (and it should
1703 * almost certainly be removed from there, too.)
1705 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1706 * The returned variable should ALWAYS be freed after usage.
1708 void expand_macros_in_string(char **p)
1710 Token *line = tokenize(*p);
1711 line = expand_smacro(line);
1712 *p = detoken(line, false);
1716 * Common code for defining an smacro
1718 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1719 int nparam, Token *expansion)
1721 SMacro *smac, **smhead;
1722 struct hash_table *smtbl;
1724 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1725 if (!smac) {
1726 error(ERR_WARNING,
1727 "single-line macro `%s' defined both with and"
1728 " without parameters", mname);
1730 /* Some instances of the old code considered this a failure,
1731 some others didn't. What is the right thing to do here? */
1732 free_tlist(expansion);
1733 return false; /* Failure */
1734 } else {
1736 * We're redefining, so we have to take over an
1737 * existing SMacro structure. This means freeing
1738 * what was already in it.
1740 nasm_free(smac->name);
1741 free_tlist(smac->expansion);
1743 } else {
1744 smtbl = ctx ? &ctx->localmac : &smacros;
1745 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1746 smac = nasm_malloc(sizeof(SMacro));
1747 smac->next = *smhead;
1748 *smhead = smac;
1750 smac->name = nasm_strdup(mname);
1751 smac->casesense = casesense;
1752 smac->nparam = nparam;
1753 smac->expansion = expansion;
1754 smac->in_progress = false;
1755 return true; /* Success */
1759 * Undefine an smacro
1761 static void undef_smacro(Context *ctx, const char *mname)
1763 SMacro **smhead, *s, **sp;
1764 struct hash_table *smtbl;
1766 smtbl = ctx ? &ctx->localmac : &smacros;
1767 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1769 if (smhead) {
1771 * We now have a macro name... go hunt for it.
1773 sp = smhead;
1774 while ((s = *sp) != NULL) {
1775 if (!mstrcmp(s->name, mname, s->casesense)) {
1776 *sp = s->next;
1777 nasm_free(s->name);
1778 free_tlist(s->expansion);
1779 nasm_free(s);
1780 } else {
1781 sp = &s->next;
1788 * Decode a size directive
1790 static int parse_size(const char *str) {
1791 static const char *size_names[] =
1792 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1793 static const int sizes[] =
1794 { 0, 1, 4, 16, 8, 10, 2, 32 };
1796 return sizes[bsii(str, size_names, elements(size_names))+1];
1800 * find and process preprocessor directive in passed line
1801 * Find out if a line contains a preprocessor directive, and deal
1802 * with it if so.
1804 * If a directive _is_ found, it is the responsibility of this routine
1805 * (and not the caller) to free_tlist() the line.
1807 * @param tline a pointer to the current tokeninzed line linked list
1808 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1811 static int do_directive(Token * tline)
1813 enum preproc_token i;
1814 int j;
1815 bool err;
1816 int nparam;
1817 bool nolist;
1818 bool casesense;
1819 int k, m;
1820 int offset;
1821 char *p, *mname;
1822 Include *inc;
1823 Context *ctx;
1824 Cond *cond;
1825 MMacro *mmac, **mmhead;
1826 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1827 Line *l;
1828 struct tokenval tokval;
1829 expr *evalresult;
1830 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1831 int64_t count;
1833 origline = tline;
1835 skip_white_(tline);
1836 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1837 (tline->text[1] == '%' || tline->text[1] == '$'
1838 || tline->text[1] == '!'))
1839 return NO_DIRECTIVE_FOUND;
1841 i = pp_token_hash(tline->text);
1844 * If we're in a non-emitting branch of a condition construct,
1845 * or walking to the end of an already terminated %rep block,
1846 * we should ignore all directives except for condition
1847 * directives.
1849 if (((istk->conds && !emitting(istk->conds->state)) ||
1850 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1851 return NO_DIRECTIVE_FOUND;
1855 * If we're defining a macro or reading a %rep block, we should
1856 * ignore all directives except for %macro/%imacro (which
1857 * generate an error), %endm/%endmacro, and (only if we're in a
1858 * %rep block) %endrep. If we're in a %rep block, another %rep
1859 * causes an error, so should be let through.
1861 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1862 i != PP_ENDMACRO && i != PP_ENDM &&
1863 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1864 return NO_DIRECTIVE_FOUND;
1867 switch (i) {
1868 case PP_INVALID:
1869 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1870 tline->text);
1871 return NO_DIRECTIVE_FOUND; /* didn't get it */
1873 case PP_STACKSIZE:
1874 /* Directive to tell NASM what the default stack size is. The
1875 * default is for a 16-bit stack, and this can be overriden with
1876 * %stacksize large.
1877 * the following form:
1879 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1881 tline = tline->next;
1882 if (tline && tline->type == TOK_WHITESPACE)
1883 tline = tline->next;
1884 if (!tline || tline->type != TOK_ID) {
1885 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1886 free_tlist(origline);
1887 return DIRECTIVE_FOUND;
1889 if (nasm_stricmp(tline->text, "flat") == 0) {
1890 /* All subsequent ARG directives are for a 32-bit stack */
1891 StackSize = 4;
1892 StackPointer = "ebp";
1893 ArgOffset = 8;
1894 LocalOffset = 0;
1895 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1896 /* All subsequent ARG directives are for a 64-bit stack */
1897 StackSize = 8;
1898 StackPointer = "rbp";
1899 ArgOffset = 8;
1900 LocalOffset = 0;
1901 } else if (nasm_stricmp(tline->text, "large") == 0) {
1902 /* All subsequent ARG directives are for a 16-bit stack,
1903 * far function call.
1905 StackSize = 2;
1906 StackPointer = "bp";
1907 ArgOffset = 4;
1908 LocalOffset = 0;
1909 } else if (nasm_stricmp(tline->text, "small") == 0) {
1910 /* All subsequent ARG directives are for a 16-bit stack,
1911 * far function call. We don't support near functions.
1913 StackSize = 2;
1914 StackPointer = "bp";
1915 ArgOffset = 6;
1916 LocalOffset = 0;
1917 } else {
1918 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1919 free_tlist(origline);
1920 return DIRECTIVE_FOUND;
1922 free_tlist(origline);
1923 return DIRECTIVE_FOUND;
1925 case PP_ARG:
1926 /* TASM like ARG directive to define arguments to functions, in
1927 * the following form:
1929 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1931 offset = ArgOffset;
1932 do {
1933 char *arg, directive[256];
1934 int size = StackSize;
1936 /* Find the argument name */
1937 tline = tline->next;
1938 if (tline && tline->type == TOK_WHITESPACE)
1939 tline = tline->next;
1940 if (!tline || tline->type != TOK_ID) {
1941 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1942 free_tlist(origline);
1943 return DIRECTIVE_FOUND;
1945 arg = tline->text;
1947 /* Find the argument size type */
1948 tline = tline->next;
1949 if (!tline || tline->type != TOK_OTHER
1950 || tline->text[0] != ':') {
1951 error(ERR_NONFATAL,
1952 "Syntax error processing `%%arg' directive");
1953 free_tlist(origline);
1954 return DIRECTIVE_FOUND;
1956 tline = tline->next;
1957 if (!tline || tline->type != TOK_ID) {
1958 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1959 free_tlist(origline);
1960 return DIRECTIVE_FOUND;
1963 /* Allow macro expansion of type parameter */
1964 tt = tokenize(tline->text);
1965 tt = expand_smacro(tt);
1966 size = parse_size(tt->text);
1967 if (!size) {
1968 error(ERR_NONFATAL,
1969 "Invalid size type for `%%arg' missing directive");
1970 free_tlist(tt);
1971 free_tlist(origline);
1972 return DIRECTIVE_FOUND;
1974 free_tlist(tt);
1976 /* Round up to even stack slots */
1977 size = (size+StackSize-1) & ~(StackSize-1);
1979 /* Now define the macro for the argument */
1980 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1981 arg, StackPointer, offset);
1982 do_directive(tokenize(directive));
1983 offset += size;
1985 /* Move to the next argument in the list */
1986 tline = tline->next;
1987 if (tline && tline->type == TOK_WHITESPACE)
1988 tline = tline->next;
1989 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1990 ArgOffset = offset;
1991 free_tlist(origline);
1992 return DIRECTIVE_FOUND;
1994 case PP_LOCAL:
1995 /* TASM like LOCAL directive to define local variables for a
1996 * function, in the following form:
1998 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2000 * The '= LocalSize' at the end is ignored by NASM, but is
2001 * required by TASM to define the local parameter size (and used
2002 * by the TASM macro package).
2004 offset = LocalOffset;
2005 do {
2006 char *local, directive[256];
2007 int size = StackSize;
2009 /* Find the argument name */
2010 tline = tline->next;
2011 if (tline && tline->type == TOK_WHITESPACE)
2012 tline = tline->next;
2013 if (!tline || tline->type != TOK_ID) {
2014 error(ERR_NONFATAL,
2015 "`%%local' missing argument parameter");
2016 free_tlist(origline);
2017 return DIRECTIVE_FOUND;
2019 local = tline->text;
2021 /* Find the argument size type */
2022 tline = tline->next;
2023 if (!tline || tline->type != TOK_OTHER
2024 || tline->text[0] != ':') {
2025 error(ERR_NONFATAL,
2026 "Syntax error processing `%%local' directive");
2027 free_tlist(origline);
2028 return DIRECTIVE_FOUND;
2030 tline = tline->next;
2031 if (!tline || tline->type != TOK_ID) {
2032 error(ERR_NONFATAL,
2033 "`%%local' missing size type parameter");
2034 free_tlist(origline);
2035 return DIRECTIVE_FOUND;
2038 /* Allow macro expansion of type parameter */
2039 tt = tokenize(tline->text);
2040 tt = expand_smacro(tt);
2041 size = parse_size(tt->text);
2042 if (!size) {
2043 error(ERR_NONFATAL,
2044 "Invalid size type for `%%local' missing directive");
2045 free_tlist(tt);
2046 free_tlist(origline);
2047 return DIRECTIVE_FOUND;
2049 free_tlist(tt);
2051 /* Round up to even stack slots */
2052 size = (size+StackSize-1) & ~(StackSize-1);
2054 offset += size; /* Negative offset, increment before */
2056 /* Now define the macro for the argument */
2057 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2058 local, StackPointer, offset);
2059 do_directive(tokenize(directive));
2061 /* Now define the assign to setup the enter_c macro correctly */
2062 snprintf(directive, sizeof(directive),
2063 "%%assign %%$localsize %%$localsize+%d", size);
2064 do_directive(tokenize(directive));
2066 /* Move to the next argument in the list */
2067 tline = tline->next;
2068 if (tline && tline->type == TOK_WHITESPACE)
2069 tline = tline->next;
2070 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2071 LocalOffset = offset;
2072 free_tlist(origline);
2073 return DIRECTIVE_FOUND;
2075 case PP_CLEAR:
2076 if (tline->next)
2077 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2078 free_macros();
2079 init_macros();
2080 free_tlist(origline);
2081 return DIRECTIVE_FOUND;
2083 case PP_DEPEND:
2084 tline = expand_smacro(tline->next);
2085 skip_white_(tline);
2086 if (!tline || (tline->type != TOK_STRING &&
2087 tline->type != TOK_INTERNAL_STRING)) {
2088 error(ERR_NONFATAL, "`%%depend' expects a file name");
2089 free_tlist(origline);
2090 return DIRECTIVE_FOUND; /* but we did _something_ */
2092 if (tline->next)
2093 error(ERR_WARNING,
2094 "trailing garbage after `%%depend' ignored");
2095 p = tline->text;
2096 if (tline->type != TOK_INTERNAL_STRING)
2097 nasm_unquote(p);
2098 if (dephead && !in_list(*dephead, p)) {
2099 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2100 sl->next = NULL;
2101 strcpy(sl->str, p);
2102 *deptail = sl;
2103 deptail = &sl->next;
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND;
2108 case PP_INCLUDE:
2109 tline = expand_smacro(tline->next);
2110 skip_white_(tline);
2112 if (!tline || (tline->type != TOK_STRING &&
2113 tline->type != TOK_INTERNAL_STRING)) {
2114 error(ERR_NONFATAL, "`%%include' expects a file name");
2115 free_tlist(origline);
2116 return DIRECTIVE_FOUND; /* but we did _something_ */
2118 if (tline->next)
2119 error(ERR_WARNING,
2120 "trailing garbage after `%%include' ignored");
2121 p = tline->text;
2122 if (tline->type != TOK_INTERNAL_STRING)
2123 nasm_unquote(p);
2124 inc = nasm_malloc(sizeof(Include));
2125 inc->next = istk;
2126 inc->conds = NULL;
2127 inc->fp = inc_fopen(p, dephead, deptail, pass == 0);
2128 if (!inc->fp) {
2129 /* -MG given but file not found */
2130 nasm_free(inc);
2131 } else {
2132 inc->fname = src_set_fname(p);
2133 inc->lineno = src_set_linnum(0);
2134 inc->lineinc = 1;
2135 inc->expansion = NULL;
2136 inc->mstk = NULL;
2137 istk = inc;
2138 list->uplevel(LIST_INCLUDE);
2140 free_tlist(origline);
2141 return DIRECTIVE_FOUND;
2143 case PP_PUSH:
2144 tline = tline->next;
2145 skip_white_(tline);
2146 tline = expand_id(tline);
2147 if (!tok_type_(tline, TOK_ID)) {
2148 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2149 free_tlist(origline);
2150 return DIRECTIVE_FOUND; /* but we did _something_ */
2152 if (tline->next)
2153 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2154 ctx = nasm_malloc(sizeof(Context));
2155 ctx->next = cstk;
2156 hash_init(&ctx->localmac, HASH_SMALL);
2157 ctx->name = nasm_strdup(tline->text);
2158 ctx->number = unique++;
2159 cstk = ctx;
2160 free_tlist(origline);
2161 break;
2163 case PP_REPL:
2164 tline = tline->next;
2165 skip_white_(tline);
2166 tline = expand_id(tline);
2167 if (!tok_type_(tline, TOK_ID)) {
2168 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2169 free_tlist(origline);
2170 return DIRECTIVE_FOUND; /* but we did _something_ */
2172 if (tline->next)
2173 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2174 if (!cstk)
2175 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2176 else {
2177 nasm_free(cstk->name);
2178 cstk->name = nasm_strdup(tline->text);
2180 free_tlist(origline);
2181 break;
2183 case PP_POP:
2184 if (tline->next)
2185 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2186 if (!cstk)
2187 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2188 else
2189 ctx_pop();
2190 free_tlist(origline);
2191 break;
2193 case PP_ERROR:
2194 tline->next = expand_smacro(tline->next);
2195 tline = tline->next;
2196 skip_white_(tline);
2197 if (tok_type_(tline, TOK_STRING)) {
2198 p = tline->text;
2199 nasm_unquote(p);
2200 expand_macros_in_string(&p); /* WHY? */
2201 error(ERR_NONFATAL, "%s", p);
2202 nasm_free(p);
2203 } else {
2204 p = detoken(tline, false);
2205 error(ERR_WARNING, "%s", p); /* WARNING!??!! */
2206 nasm_free(p);
2208 free_tlist(origline);
2209 break;
2211 CASE_PP_IF:
2212 if (istk->conds && !emitting(istk->conds->state))
2213 j = COND_NEVER;
2214 else {
2215 j = if_condition(tline->next, i);
2216 tline->next = NULL; /* it got freed */
2217 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2219 cond = nasm_malloc(sizeof(Cond));
2220 cond->next = istk->conds;
2221 cond->state = j;
2222 istk->conds = cond;
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND;
2226 CASE_PP_ELIF:
2227 if (!istk->conds)
2228 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2229 if (emitting(istk->conds->state)
2230 || istk->conds->state == COND_NEVER)
2231 istk->conds->state = COND_NEVER;
2232 else {
2234 * IMPORTANT: In the case of %if, we will already have
2235 * called expand_mmac_params(); however, if we're
2236 * processing an %elif we must have been in a
2237 * non-emitting mode, which would have inhibited
2238 * the normal invocation of expand_mmac_params(). Therefore,
2239 * we have to do it explicitly here.
2241 j = if_condition(expand_mmac_params(tline->next), i);
2242 tline->next = NULL; /* it got freed */
2243 istk->conds->state =
2244 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2246 free_tlist(origline);
2247 return DIRECTIVE_FOUND;
2249 case PP_ELSE:
2250 if (tline->next)
2251 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2252 if (!istk->conds)
2253 error(ERR_FATAL, "`%%else': no matching `%%if'");
2254 if (emitting(istk->conds->state)
2255 || istk->conds->state == COND_NEVER)
2256 istk->conds->state = COND_ELSE_FALSE;
2257 else
2258 istk->conds->state = COND_ELSE_TRUE;
2259 free_tlist(origline);
2260 return DIRECTIVE_FOUND;
2262 case PP_ENDIF:
2263 if (tline->next)
2264 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2265 if (!istk->conds)
2266 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2267 cond = istk->conds;
2268 istk->conds = cond->next;
2269 nasm_free(cond);
2270 free_tlist(origline);
2271 return DIRECTIVE_FOUND;
2273 case PP_MACRO:
2274 case PP_IMACRO:
2275 if (defining)
2276 error(ERR_FATAL,
2277 "`%%%smacro': already defining a macro",
2278 (i == PP_IMACRO ? "i" : ""));
2279 tline = tline->next;
2280 skip_white_(tline);
2281 tline = expand_id(tline);
2282 if (!tok_type_(tline, TOK_ID)) {
2283 error(ERR_NONFATAL,
2284 "`%%%smacro' expects a macro name",
2285 (i == PP_IMACRO ? "i" : ""));
2286 return DIRECTIVE_FOUND;
2288 defining = nasm_malloc(sizeof(MMacro));
2289 defining->name = nasm_strdup(tline->text);
2290 defining->casesense = (i == PP_MACRO);
2291 defining->plus = false;
2292 defining->nolist = false;
2293 defining->in_progress = 0;
2294 defining->rep_nest = NULL;
2295 tline = expand_smacro(tline->next);
2296 skip_white_(tline);
2297 if (!tok_type_(tline, TOK_NUMBER)) {
2298 error(ERR_NONFATAL,
2299 "`%%%smacro' expects a parameter count",
2300 (i == PP_IMACRO ? "i" : ""));
2301 defining->nparam_min = defining->nparam_max = 0;
2302 } else {
2303 defining->nparam_min = defining->nparam_max =
2304 readnum(tline->text, &err);
2305 if (err)
2306 error(ERR_NONFATAL,
2307 "unable to parse parameter count `%s'", tline->text);
2309 if (tline && tok_is_(tline->next, "-")) {
2310 tline = tline->next->next;
2311 if (tok_is_(tline, "*"))
2312 defining->nparam_max = INT_MAX;
2313 else if (!tok_type_(tline, TOK_NUMBER))
2314 error(ERR_NONFATAL,
2315 "`%%%smacro' expects a parameter count after `-'",
2316 (i == PP_IMACRO ? "i" : ""));
2317 else {
2318 defining->nparam_max = readnum(tline->text, &err);
2319 if (err)
2320 error(ERR_NONFATAL,
2321 "unable to parse parameter count `%s'",
2322 tline->text);
2323 if (defining->nparam_min > defining->nparam_max)
2324 error(ERR_NONFATAL,
2325 "minimum parameter count exceeds maximum");
2328 if (tline && tok_is_(tline->next, "+")) {
2329 tline = tline->next;
2330 defining->plus = true;
2332 if (tline && tok_type_(tline->next, TOK_ID) &&
2333 !nasm_stricmp(tline->next->text, ".nolist")) {
2334 tline = tline->next;
2335 defining->nolist = true;
2337 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2338 while (mmac) {
2339 if (!strcmp(mmac->name, defining->name) &&
2340 (mmac->nparam_min <= defining->nparam_max
2341 || defining->plus)
2342 && (defining->nparam_min <= mmac->nparam_max
2343 || mmac->plus)) {
2344 error(ERR_WARNING,
2345 "redefining multi-line macro `%s'", defining->name);
2346 break;
2348 mmac = mmac->next;
2351 * Handle default parameters.
2353 if (tline && tline->next) {
2354 defining->dlist = tline->next;
2355 tline->next = NULL;
2356 count_mmac_params(defining->dlist, &defining->ndefs,
2357 &defining->defaults);
2358 } else {
2359 defining->dlist = NULL;
2360 defining->defaults = NULL;
2362 defining->expansion = NULL;
2363 free_tlist(origline);
2364 return DIRECTIVE_FOUND;
2366 case PP_ENDM:
2367 case PP_ENDMACRO:
2368 if (!defining) {
2369 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2370 return DIRECTIVE_FOUND;
2372 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2373 defining->next = *mmhead;
2374 *mmhead = defining;
2375 defining = NULL;
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2379 case PP_ROTATE:
2380 if (tline->next && tline->next->type == TOK_WHITESPACE)
2381 tline = tline->next;
2382 if (tline->next == NULL) {
2383 free_tlist(origline);
2384 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2385 return DIRECTIVE_FOUND;
2387 t = expand_smacro(tline->next);
2388 tline->next = NULL;
2389 free_tlist(origline);
2390 tline = t;
2391 tptr = &t;
2392 tokval.t_type = TOKEN_INVALID;
2393 evalresult =
2394 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2395 free_tlist(tline);
2396 if (!evalresult)
2397 return DIRECTIVE_FOUND;
2398 if (tokval.t_type)
2399 error(ERR_WARNING,
2400 "trailing garbage after expression ignored");
2401 if (!is_simple(evalresult)) {
2402 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2403 return DIRECTIVE_FOUND;
2405 mmac = istk->mstk;
2406 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2407 mmac = mmac->next_active;
2408 if (!mmac) {
2409 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2410 } else if (mmac->nparam == 0) {
2411 error(ERR_NONFATAL,
2412 "`%%rotate' invoked within macro without parameters");
2413 } else {
2414 int rotate = mmac->rotate + reloc_value(evalresult);
2416 rotate %= (int)mmac->nparam;
2417 if (rotate < 0)
2418 rotate += mmac->nparam;
2420 mmac->rotate = rotate;
2422 return DIRECTIVE_FOUND;
2424 case PP_REP:
2425 nolist = false;
2426 do {
2427 tline = tline->next;
2428 } while (tok_type_(tline, TOK_WHITESPACE));
2430 if (tok_type_(tline, TOK_ID) &&
2431 nasm_stricmp(tline->text, ".nolist") == 0) {
2432 nolist = true;
2433 do {
2434 tline = tline->next;
2435 } while (tok_type_(tline, TOK_WHITESPACE));
2438 if (tline) {
2439 t = expand_smacro(tline);
2440 tptr = &t;
2441 tokval.t_type = TOKEN_INVALID;
2442 evalresult =
2443 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2444 if (!evalresult) {
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2448 if (tokval.t_type)
2449 error(ERR_WARNING,
2450 "trailing garbage after expression ignored");
2451 if (!is_simple(evalresult)) {
2452 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2453 return DIRECTIVE_FOUND;
2455 count = reloc_value(evalresult) + 1;
2456 } else {
2457 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2458 count = 0;
2460 free_tlist(origline);
2462 tmp_defining = defining;
2463 defining = nasm_malloc(sizeof(MMacro));
2464 defining->name = NULL; /* flags this macro as a %rep block */
2465 defining->casesense = false;
2466 defining->plus = false;
2467 defining->nolist = nolist;
2468 defining->in_progress = count;
2469 defining->nparam_min = defining->nparam_max = 0;
2470 defining->defaults = NULL;
2471 defining->dlist = NULL;
2472 defining->expansion = NULL;
2473 defining->next_active = istk->mstk;
2474 defining->rep_nest = tmp_defining;
2475 return DIRECTIVE_FOUND;
2477 case PP_ENDREP:
2478 if (!defining || defining->name) {
2479 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2480 return DIRECTIVE_FOUND;
2484 * Now we have a "macro" defined - although it has no name
2485 * and we won't be entering it in the hash tables - we must
2486 * push a macro-end marker for it on to istk->expansion.
2487 * After that, it will take care of propagating itself (a
2488 * macro-end marker line for a macro which is really a %rep
2489 * block will cause the macro to be re-expanded, complete
2490 * with another macro-end marker to ensure the process
2491 * continues) until the whole expansion is forcibly removed
2492 * from istk->expansion by a %exitrep.
2494 l = nasm_malloc(sizeof(Line));
2495 l->next = istk->expansion;
2496 l->finishes = defining;
2497 l->first = NULL;
2498 istk->expansion = l;
2500 istk->mstk = defining;
2502 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2503 tmp_defining = defining;
2504 defining = defining->rep_nest;
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2508 case PP_EXITREP:
2510 * We must search along istk->expansion until we hit a
2511 * macro-end marker for a macro with no name. Then we set
2512 * its `in_progress' flag to 0.
2514 for (l = istk->expansion; l; l = l->next)
2515 if (l->finishes && !l->finishes->name)
2516 break;
2518 if (l)
2519 l->finishes->in_progress = 0;
2520 else
2521 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2525 case PP_XDEFINE:
2526 case PP_IXDEFINE:
2527 case PP_DEFINE:
2528 case PP_IDEFINE:
2529 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2531 tline = tline->next;
2532 skip_white_(tline);
2533 tline = expand_id(tline);
2534 if (!tline || (tline->type != TOK_ID &&
2535 (tline->type != TOK_PREPROC_ID ||
2536 tline->text[1] != '$'))) {
2537 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2538 pp_directives[i]);
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
2543 ctx = get_ctx(tline->text, false);
2545 mname = tline->text;
2546 last = tline;
2547 param_start = tline = tline->next;
2548 nparam = 0;
2550 /* Expand the macro definition now for %xdefine and %ixdefine */
2551 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2552 tline = expand_smacro(tline);
2554 if (tok_is_(tline, "(")) {
2556 * This macro has parameters.
2559 tline = tline->next;
2560 while (1) {
2561 skip_white_(tline);
2562 if (!tline) {
2563 error(ERR_NONFATAL, "parameter identifier expected");
2564 free_tlist(origline);
2565 return DIRECTIVE_FOUND;
2567 if (tline->type != TOK_ID) {
2568 error(ERR_NONFATAL,
2569 "`%s': parameter identifier expected",
2570 tline->text);
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2574 tline->type = TOK_SMAC_PARAM + nparam++;
2575 tline = tline->next;
2576 skip_white_(tline);
2577 if (tok_is_(tline, ",")) {
2578 tline = tline->next;
2579 continue;
2581 if (!tok_is_(tline, ")")) {
2582 error(ERR_NONFATAL,
2583 "`)' expected to terminate macro template");
2584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
2587 break;
2589 last = tline;
2590 tline = tline->next;
2592 if (tok_type_(tline, TOK_WHITESPACE))
2593 last = tline, tline = tline->next;
2594 macro_start = NULL;
2595 last->next = NULL;
2596 t = tline;
2597 while (t) {
2598 if (t->type == TOK_ID) {
2599 for (tt = param_start; tt; tt = tt->next)
2600 if (tt->type >= TOK_SMAC_PARAM &&
2601 !strcmp(tt->text, t->text))
2602 t->type = tt->type;
2604 tt = t->next;
2605 t->next = macro_start;
2606 macro_start = t;
2607 t = tt;
2610 * Good. We now have a macro name, a parameter count, and a
2611 * token list (in reverse order) for an expansion. We ought
2612 * to be OK just to create an SMacro, store it, and let
2613 * free_tlist have the rest of the line (which we have
2614 * carefully re-terminated after chopping off the expansion
2615 * from the end).
2617 define_smacro(ctx, mname, casesense, nparam, macro_start);
2618 free_tlist(origline);
2619 return DIRECTIVE_FOUND;
2621 case PP_UNDEF:
2622 tline = tline->next;
2623 skip_white_(tline);
2624 tline = expand_id(tline);
2625 if (!tline || (tline->type != TOK_ID &&
2626 (tline->type != TOK_PREPROC_ID ||
2627 tline->text[1] != '$'))) {
2628 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2629 free_tlist(origline);
2630 return DIRECTIVE_FOUND;
2632 if (tline->next) {
2633 error(ERR_WARNING,
2634 "trailing garbage after macro name ignored");
2637 /* Find the context that symbol belongs to */
2638 ctx = get_ctx(tline->text, false);
2639 undef_smacro(ctx, tline->text);
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2643 case PP_PATHSEARCH:
2645 FILE *fp;
2646 StrList *xsl = NULL;
2648 casesense = true;
2650 tline = tline->next;
2651 skip_white_(tline);
2652 tline = expand_id(tline);
2653 if (!tline || (tline->type != TOK_ID &&
2654 (tline->type != TOK_PREPROC_ID ||
2655 tline->text[1] != '$'))) {
2656 error(ERR_NONFATAL,
2657 "`%%pathsearch' expects a macro identifier as first parameter");
2658 free_tlist(origline);
2659 return DIRECTIVE_FOUND;
2661 ctx = get_ctx(tline->text, false);
2663 mname = tline->text;
2664 last = tline;
2665 tline = expand_smacro(tline->next);
2666 last->next = NULL;
2668 t = tline;
2669 while (tok_type_(t, TOK_WHITESPACE))
2670 t = t->next;
2672 if (!t || (t->type != TOK_STRING &&
2673 t->type != TOK_INTERNAL_STRING)) {
2674 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2675 free_tlist(tline);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND; /* but we did _something_ */
2679 if (t->next)
2680 error(ERR_WARNING,
2681 "trailing garbage after `%%pathsearch' ignored");
2682 p = t->text;
2683 if (t->type != TOK_INTERNAL_STRING)
2684 nasm_unquote(p);
2686 fp = inc_fopen(p, &xsl, &xsl, true);
2687 if (fp) {
2688 p = xsl->str;
2689 fclose(fp); /* Don't actually care about the file */
2691 macro_start = nasm_malloc(sizeof(*macro_start));
2692 macro_start->next = NULL;
2693 macro_start->text = nasm_quote(p, strlen(p));
2694 macro_start->type = TOK_STRING;
2695 macro_start->mac = NULL;
2696 if (xsl)
2697 nasm_free(xsl);
2700 * We now have a macro name, an implicit parameter count of
2701 * zero, and a string 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;
2710 case PP_STRLEN:
2711 casesense = true;
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 "`%%strlen' expects a macro identifier as first parameter");
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
2724 ctx = get_ctx(tline->text, false);
2726 mname = tline->text;
2727 last = tline;
2728 tline = expand_smacro(tline->next);
2729 last->next = NULL;
2731 t = tline;
2732 while (tok_type_(t, TOK_WHITESPACE))
2733 t = t->next;
2734 /* t should now point to the string */
2735 if (t->type != TOK_STRING) {
2736 error(ERR_NONFATAL,
2737 "`%%strlen` requires string as second parameter");
2738 free_tlist(tline);
2739 free_tlist(origline);
2740 return DIRECTIVE_FOUND;
2743 macro_start = nasm_malloc(sizeof(*macro_start));
2744 macro_start->next = NULL;
2745 make_tok_num(macro_start, nasm_unquote(t->text));
2746 macro_start->mac = NULL;
2749 * We now have a macro name, an implicit parameter count of
2750 * zero, and a numeric token to use as an expansion. Create
2751 * and store an SMacro.
2753 define_smacro(ctx, mname, casesense, 0, macro_start);
2754 free_tlist(tline);
2755 free_tlist(origline);
2756 return DIRECTIVE_FOUND;
2758 case PP_SUBSTR:
2760 int64_t a1, a2;
2761 size_t len;
2763 casesense = true;
2765 tline = tline->next;
2766 skip_white_(tline);
2767 tline = expand_id(tline);
2768 if (!tline || (tline->type != TOK_ID &&
2769 (tline->type != TOK_PREPROC_ID ||
2770 tline->text[1] != '$'))) {
2771 error(ERR_NONFATAL,
2772 "`%%substr' expects a macro identifier as first parameter");
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
2776 ctx = get_ctx(tline->text, false);
2778 mname = tline->text;
2779 last = tline;
2780 tline = expand_smacro(tline->next);
2781 last->next = NULL;
2783 t = tline->next;
2784 while (tok_type_(t, TOK_WHITESPACE))
2785 t = t->next;
2787 /* t should now point to the string */
2788 if (t->type != TOK_STRING) {
2789 error(ERR_NONFATAL,
2790 "`%%substr` requires string as second parameter");
2791 free_tlist(tline);
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
2796 tt = t->next;
2797 tptr = &tt;
2798 tokval.t_type = TOKEN_INVALID;
2799 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2800 pass, error, NULL);
2801 if (!evalresult) {
2802 free_tlist(tline);
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
2805 } else if (!is_simple(evalresult)) {
2806 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2807 free_tlist(tline);
2808 free_tlist(origline);
2809 return DIRECTIVE_FOUND;
2811 a1 = evalresult->value-1;
2813 while (tok_type_(tt, TOK_WHITESPACE))
2814 tt = tt->next;
2815 if (!tt) {
2816 a2 = 1; /* Backwards compatibility: one character */
2817 } else {
2818 tokval.t_type = TOKEN_INVALID;
2819 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2820 pass, error, NULL);
2821 if (!evalresult) {
2822 free_tlist(tline);
2823 free_tlist(origline);
2824 return DIRECTIVE_FOUND;
2825 } else if (!is_simple(evalresult)) {
2826 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2827 free_tlist(tline);
2828 free_tlist(origline);
2829 return DIRECTIVE_FOUND;
2831 a2 = evalresult->value;
2834 len = nasm_unquote(t->text);
2835 if (a2 < 0)
2836 a2 = a2+1+len-a1;
2837 if (a1+a2 > (int64_t)len)
2838 a2 = len-a1;
2840 macro_start = nasm_malloc(sizeof(*macro_start));
2841 macro_start->next = NULL;
2842 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
2843 macro_start->type = TOK_STRING;
2844 macro_start->mac = NULL;
2847 * We now have a macro name, an implicit parameter count of
2848 * zero, and a numeric token to use as an expansion. Create
2849 * and store an SMacro.
2851 define_smacro(ctx, mname, casesense, 0, macro_start);
2852 free_tlist(tline);
2853 free_tlist(origline);
2854 return DIRECTIVE_FOUND;
2857 case PP_ASSIGN:
2858 case PP_IASSIGN:
2859 casesense = (i == PP_ASSIGN);
2861 tline = tline->next;
2862 skip_white_(tline);
2863 tline = expand_id(tline);
2864 if (!tline || (tline->type != TOK_ID &&
2865 (tline->type != TOK_PREPROC_ID ||
2866 tline->text[1] != '$'))) {
2867 error(ERR_NONFATAL,
2868 "`%%%sassign' expects a macro identifier",
2869 (i == PP_IASSIGN ? "i" : ""));
2870 free_tlist(origline);
2871 return DIRECTIVE_FOUND;
2873 ctx = get_ctx(tline->text, false);
2875 mname = tline->text;
2876 last = tline;
2877 tline = expand_smacro(tline->next);
2878 last->next = NULL;
2880 t = tline;
2881 tptr = &t;
2882 tokval.t_type = TOKEN_INVALID;
2883 evalresult =
2884 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2885 free_tlist(tline);
2886 if (!evalresult) {
2887 free_tlist(origline);
2888 return DIRECTIVE_FOUND;
2891 if (tokval.t_type)
2892 error(ERR_WARNING,
2893 "trailing garbage after expression ignored");
2895 if (!is_simple(evalresult)) {
2896 error(ERR_NONFATAL,
2897 "non-constant value given to `%%%sassign'",
2898 (i == PP_IASSIGN ? "i" : ""));
2899 free_tlist(origline);
2900 return DIRECTIVE_FOUND;
2903 macro_start = nasm_malloc(sizeof(*macro_start));
2904 macro_start->next = NULL;
2905 make_tok_num(macro_start, reloc_value(evalresult));
2906 macro_start->mac = NULL;
2909 * We now have a macro name, an implicit parameter count of
2910 * zero, and a numeric token to use as an expansion. Create
2911 * and store an SMacro.
2913 define_smacro(ctx, mname, casesense, 0, macro_start);
2914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2917 case PP_LINE:
2919 * Syntax is `%line nnn[+mmm] [filename]'
2921 tline = tline->next;
2922 skip_white_(tline);
2923 if (!tok_type_(tline, TOK_NUMBER)) {
2924 error(ERR_NONFATAL, "`%%line' expects line number");
2925 free_tlist(origline);
2926 return DIRECTIVE_FOUND;
2928 k = readnum(tline->text, &err);
2929 m = 1;
2930 tline = tline->next;
2931 if (tok_is_(tline, "+")) {
2932 tline = tline->next;
2933 if (!tok_type_(tline, TOK_NUMBER)) {
2934 error(ERR_NONFATAL, "`%%line' expects line increment");
2935 free_tlist(origline);
2936 return DIRECTIVE_FOUND;
2938 m = readnum(tline->text, &err);
2939 tline = tline->next;
2941 skip_white_(tline);
2942 src_set_linnum(k);
2943 istk->lineinc = m;
2944 if (tline) {
2945 nasm_free(src_set_fname(detoken(tline, false)));
2947 free_tlist(origline);
2948 return DIRECTIVE_FOUND;
2950 default:
2951 error(ERR_FATAL,
2952 "preprocessor directive `%s' not yet implemented",
2953 pp_directives[i]);
2954 break;
2956 return DIRECTIVE_FOUND;
2960 * Ensure that a macro parameter contains a condition code and
2961 * nothing else. Return the condition code index if so, or -1
2962 * otherwise.
2964 static int find_cc(Token * t)
2966 Token *tt;
2967 int i, j, k, m;
2969 if (!t)
2970 return -1; /* Probably a %+ without a space */
2972 skip_white_(t);
2973 if (t->type != TOK_ID)
2974 return -1;
2975 tt = t->next;
2976 skip_white_(tt);
2977 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2978 return -1;
2980 i = -1;
2981 j = elements(conditions);
2982 while (j - i > 1) {
2983 k = (j + i) / 2;
2984 m = nasm_stricmp(t->text, conditions[k]);
2985 if (m == 0) {
2986 i = k;
2987 j = -2;
2988 break;
2989 } else if (m < 0) {
2990 j = k;
2991 } else
2992 i = k;
2994 if (j != -2)
2995 return -1;
2996 return i;
3000 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3001 * %-n) and MMacro-local identifiers (%%foo).
3003 static Token *expand_mmac_params(Token * tline)
3005 Token *t, *tt, **tail, *thead;
3007 tail = &thead;
3008 thead = NULL;
3010 while (tline) {
3011 if (tline->type == TOK_PREPROC_ID &&
3012 (((tline->text[1] == '+' || tline->text[1] == '-')
3013 && tline->text[2]) || tline->text[1] == '%'
3014 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3015 char *text = NULL;
3016 int type = 0, cc; /* type = 0 to placate optimisers */
3017 char tmpbuf[30];
3018 unsigned int n;
3019 int i;
3020 MMacro *mac;
3022 t = tline;
3023 tline = tline->next;
3025 mac = istk->mstk;
3026 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3027 mac = mac->next_active;
3028 if (!mac)
3029 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3030 else
3031 switch (t->text[1]) {
3033 * We have to make a substitution of one of the
3034 * forms %1, %-1, %+1, %%foo, %0.
3036 case '0':
3037 type = TOK_NUMBER;
3038 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3039 text = nasm_strdup(tmpbuf);
3040 break;
3041 case '%':
3042 type = TOK_ID;
3043 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3044 mac->unique);
3045 text = nasm_strcat(tmpbuf, t->text + 2);
3046 break;
3047 case '-':
3048 n = atoi(t->text + 2) - 1;
3049 if (n >= mac->nparam)
3050 tt = NULL;
3051 else {
3052 if (mac->nparam > 1)
3053 n = (n + mac->rotate) % mac->nparam;
3054 tt = mac->params[n];
3056 cc = find_cc(tt);
3057 if (cc == -1) {
3058 error(ERR_NONFATAL,
3059 "macro parameter %d is not a condition code",
3060 n + 1);
3061 text = NULL;
3062 } else {
3063 type = TOK_ID;
3064 if (inverse_ccs[cc] == -1) {
3065 error(ERR_NONFATAL,
3066 "condition code `%s' is not invertible",
3067 conditions[cc]);
3068 text = NULL;
3069 } else
3070 text =
3071 nasm_strdup(conditions[inverse_ccs[cc]]);
3073 break;
3074 case '+':
3075 n = atoi(t->text + 2) - 1;
3076 if (n >= mac->nparam)
3077 tt = NULL;
3078 else {
3079 if (mac->nparam > 1)
3080 n = (n + mac->rotate) % mac->nparam;
3081 tt = mac->params[n];
3083 cc = find_cc(tt);
3084 if (cc == -1) {
3085 error(ERR_NONFATAL,
3086 "macro parameter %d is not a condition code",
3087 n + 1);
3088 text = NULL;
3089 } else {
3090 type = TOK_ID;
3091 text = nasm_strdup(conditions[cc]);
3093 break;
3094 default:
3095 n = atoi(t->text + 1) - 1;
3096 if (n >= mac->nparam)
3097 tt = NULL;
3098 else {
3099 if (mac->nparam > 1)
3100 n = (n + mac->rotate) % mac->nparam;
3101 tt = mac->params[n];
3103 if (tt) {
3104 for (i = 0; i < mac->paramlen[n]; i++) {
3105 *tail = new_Token(NULL, tt->type, tt->text, 0);
3106 tail = &(*tail)->next;
3107 tt = tt->next;
3110 text = NULL; /* we've done it here */
3111 break;
3113 if (!text) {
3114 delete_Token(t);
3115 } else {
3116 *tail = t;
3117 tail = &t->next;
3118 t->type = type;
3119 nasm_free(t->text);
3120 t->text = text;
3121 t->mac = NULL;
3123 continue;
3124 } else {
3125 t = *tail = tline;
3126 tline = tline->next;
3127 t->mac = NULL;
3128 tail = &t->next;
3131 *tail = NULL;
3132 t = thead;
3133 for (; t && (tt = t->next) != NULL; t = t->next)
3134 switch (t->type) {
3135 case TOK_WHITESPACE:
3136 if (tt->type == TOK_WHITESPACE) {
3137 t->next = delete_Token(tt);
3139 break;
3140 case TOK_ID:
3141 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3142 char *tmp = nasm_strcat(t->text, tt->text);
3143 nasm_free(t->text);
3144 t->text = tmp;
3145 t->next = delete_Token(tt);
3147 break;
3148 case TOK_NUMBER:
3149 if (tt->type == TOK_NUMBER) {
3150 char *tmp = nasm_strcat(t->text, tt->text);
3151 nasm_free(t->text);
3152 t->text = tmp;
3153 t->next = delete_Token(tt);
3155 break;
3156 default:
3157 break;
3160 return thead;
3164 * Expand all single-line macro calls made in the given line.
3165 * Return the expanded version of the line. The original is deemed
3166 * to be destroyed in the process. (In reality we'll just move
3167 * Tokens from input to output a lot of the time, rather than
3168 * actually bothering to destroy and replicate.)
3170 #define DEADMAN_LIMIT (1 << 20)
3172 static Token *expand_smacro(Token * tline)
3174 Token *t, *tt, *mstart, **tail, *thead;
3175 struct hash_table *smtbl;
3176 SMacro *head = NULL, *m;
3177 Token **params;
3178 int *paramsize;
3179 unsigned int nparam, sparam;
3180 int brackets, rescan;
3181 Token *org_tline = tline;
3182 Context *ctx;
3183 char *mname;
3184 int deadman = DEADMAN_LIMIT;
3187 * Trick: we should avoid changing the start token pointer since it can
3188 * be contained in "next" field of other token. Because of this
3189 * we allocate a copy of first token and work with it; at the end of
3190 * routine we copy it back
3192 if (org_tline) {
3193 tline =
3194 new_Token(org_tline->next, org_tline->type, org_tline->text,
3196 tline->mac = org_tline->mac;
3197 nasm_free(org_tline->text);
3198 org_tline->text = NULL;
3201 again:
3202 tail = &thead;
3203 thead = NULL;
3205 while (tline) { /* main token loop */
3206 if (!--deadman) {
3207 error(ERR_NONFATAL, "interminable macro recursion");
3208 break;
3211 if ((mname = tline->text)) {
3212 /* if this token is a local macro, look in local context */
3213 ctx = NULL;
3214 smtbl = &smacros;
3215 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3216 ctx = get_ctx(mname, true);
3217 if (ctx)
3218 smtbl = &ctx->localmac;
3220 head = (SMacro *) hash_findix(smtbl, mname);
3223 * We've hit an identifier. As in is_mmacro below, we first
3224 * check whether the identifier is a single-line macro at
3225 * all, then think about checking for parameters if
3226 * necessary.
3228 for (m = head; m; m = m->next)
3229 if (!mstrcmp(m->name, mname, m->casesense))
3230 break;
3231 if (m) {
3232 mstart = tline;
3233 params = NULL;
3234 paramsize = NULL;
3235 if (m->nparam == 0) {
3237 * Simple case: the macro is parameterless. Discard the
3238 * one token that the macro call took, and push the
3239 * expansion back on the to-do stack.
3241 if (!m->expansion) {
3242 if (!strcmp("__FILE__", m->name)) {
3243 int32_t num = 0;
3244 char *file;
3245 src_get(&num, &file);
3246 tline->text = nasm_quote(file, strlen(file));
3247 tline->type = TOK_STRING;
3248 nasm_free(file);
3249 continue;
3251 if (!strcmp("__LINE__", m->name)) {
3252 nasm_free(tline->text);
3253 make_tok_num(tline, src_get_linnum());
3254 continue;
3256 if (!strcmp("__BITS__", m->name)) {
3257 nasm_free(tline->text);
3258 make_tok_num(tline, globalbits);
3259 continue;
3261 tline = delete_Token(tline);
3262 continue;
3264 } else {
3266 * Complicated case: at least one macro with this name
3267 * exists and takes parameters. We must find the
3268 * parameters in the call, count them, find the SMacro
3269 * that corresponds to that form of the macro call, and
3270 * substitute for the parameters when we expand. What a
3271 * pain.
3273 /*tline = tline->next;
3274 skip_white_(tline); */
3275 do {
3276 t = tline->next;
3277 while (tok_type_(t, TOK_SMAC_END)) {
3278 t->mac->in_progress = false;
3279 t->text = NULL;
3280 t = tline->next = delete_Token(t);
3282 tline = t;
3283 } while (tok_type_(tline, TOK_WHITESPACE));
3284 if (!tok_is_(tline, "(")) {
3286 * This macro wasn't called with parameters: ignore
3287 * the call. (Behaviour borrowed from gnu cpp.)
3289 tline = mstart;
3290 m = NULL;
3291 } else {
3292 int paren = 0;
3293 int white = 0;
3294 brackets = 0;
3295 nparam = 0;
3296 sparam = PARAM_DELTA;
3297 params = nasm_malloc(sparam * sizeof(Token *));
3298 params[0] = tline->next;
3299 paramsize = nasm_malloc(sparam * sizeof(int));
3300 paramsize[0] = 0;
3301 while (true) { /* parameter loop */
3303 * For some unusual expansions
3304 * which concatenates function call
3306 t = tline->next;
3307 while (tok_type_(t, TOK_SMAC_END)) {
3308 t->mac->in_progress = false;
3309 t->text = NULL;
3310 t = tline->next = delete_Token(t);
3312 tline = t;
3314 if (!tline) {
3315 error(ERR_NONFATAL,
3316 "macro call expects terminating `)'");
3317 break;
3319 if (tline->type == TOK_WHITESPACE
3320 && brackets <= 0) {
3321 if (paramsize[nparam])
3322 white++;
3323 else
3324 params[nparam] = tline->next;
3325 continue; /* parameter loop */
3327 if (tline->type == TOK_OTHER
3328 && tline->text[1] == 0) {
3329 char ch = tline->text[0];
3330 if (ch == ',' && !paren && brackets <= 0) {
3331 if (++nparam >= sparam) {
3332 sparam += PARAM_DELTA;
3333 params = nasm_realloc(params,
3334 sparam *
3335 sizeof(Token
3336 *));
3337 paramsize =
3338 nasm_realloc(paramsize,
3339 sparam *
3340 sizeof(int));
3342 params[nparam] = tline->next;
3343 paramsize[nparam] = 0;
3344 white = 0;
3345 continue; /* parameter loop */
3347 if (ch == '{' &&
3348 (brackets > 0 || (brackets == 0 &&
3349 !paramsize[nparam])))
3351 if (!(brackets++)) {
3352 params[nparam] = tline->next;
3353 continue; /* parameter loop */
3356 if (ch == '}' && brackets > 0)
3357 if (--brackets == 0) {
3358 brackets = -1;
3359 continue; /* parameter loop */
3361 if (ch == '(' && !brackets)
3362 paren++;
3363 if (ch == ')' && brackets <= 0)
3364 if (--paren < 0)
3365 break;
3367 if (brackets < 0) {
3368 brackets = 0;
3369 error(ERR_NONFATAL, "braces do not "
3370 "enclose all of macro parameter");
3372 paramsize[nparam] += white + 1;
3373 white = 0;
3374 } /* parameter loop */
3375 nparam++;
3376 while (m && (m->nparam != nparam ||
3377 mstrcmp(m->name, mname,
3378 m->casesense)))
3379 m = m->next;
3380 if (!m)
3381 error(ERR_WARNING | ERR_WARN_MNP,
3382 "macro `%s' exists, "
3383 "but not taking %d parameters",
3384 mstart->text, nparam);
3387 if (m && m->in_progress)
3388 m = NULL;
3389 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3391 * Design question: should we handle !tline, which
3392 * indicates missing ')' here, or expand those
3393 * macros anyway, which requires the (t) test a few
3394 * lines down?
3396 nasm_free(params);
3397 nasm_free(paramsize);
3398 tline = mstart;
3399 } else {
3401 * Expand the macro: we are placed on the last token of the
3402 * call, so that we can easily split the call from the
3403 * following tokens. We also start by pushing an SMAC_END
3404 * token for the cycle removal.
3406 t = tline;
3407 if (t) {
3408 tline = t->next;
3409 t->next = NULL;
3411 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3412 tt->mac = m;
3413 m->in_progress = true;
3414 tline = tt;
3415 for (t = m->expansion; t; t = t->next) {
3416 if (t->type >= TOK_SMAC_PARAM) {
3417 Token *pcopy = tline, **ptail = &pcopy;
3418 Token *ttt, *pt;
3419 int i;
3421 ttt = params[t->type - TOK_SMAC_PARAM];
3422 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3423 --i >= 0;) {
3424 pt = *ptail =
3425 new_Token(tline, ttt->type, ttt->text,
3427 ptail = &pt->next;
3428 ttt = ttt->next;
3430 tline = pcopy;
3431 } else if (t->type == TOK_PREPROC_Q) {
3432 tt = new_Token(tline, TOK_ID, mname, 0);
3433 tline = tt;
3434 } else if (t->type == TOK_PREPROC_QQ) {
3435 tt = new_Token(tline, TOK_ID, m->name, 0);
3436 tline = tt;
3437 } else {
3438 tt = new_Token(tline, t->type, t->text, 0);
3439 tline = tt;
3444 * Having done that, get rid of the macro call, and clean
3445 * up the parameters.
3447 nasm_free(params);
3448 nasm_free(paramsize);
3449 free_tlist(mstart);
3450 continue; /* main token loop */
3455 if (tline->type == TOK_SMAC_END) {
3456 tline->mac->in_progress = false;
3457 tline = delete_Token(tline);
3458 } else {
3459 t = *tail = tline;
3460 tline = tline->next;
3461 t->mac = NULL;
3462 t->next = NULL;
3463 tail = &t->next;
3468 * Now scan the entire line and look for successive TOK_IDs that resulted
3469 * after expansion (they can't be produced by tokenize()). The successive
3470 * TOK_IDs should be concatenated.
3471 * Also we look for %+ tokens and concatenate the tokens before and after
3472 * them (without white spaces in between).
3474 t = thead;
3475 rescan = 0;
3476 while (t) {
3477 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3478 t = t->next;
3479 if (!t || !t->next)
3480 break;
3481 if (t->next->type == TOK_ID ||
3482 t->next->type == TOK_PREPROC_ID ||
3483 t->next->type == TOK_NUMBER) {
3484 char *p = nasm_strcat(t->text, t->next->text);
3485 nasm_free(t->text);
3486 t->next = delete_Token(t->next);
3487 t->text = p;
3488 rescan = 1;
3489 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3490 t->next->next->type == TOK_PREPROC_ID &&
3491 strcmp(t->next->next->text, "%+") == 0) {
3492 /* free the next whitespace, the %+ token and next whitespace */
3493 int i;
3494 for (i = 1; i <= 3; i++) {
3495 if (!t->next
3496 || (i != 2 && t->next->type != TOK_WHITESPACE))
3497 break;
3498 t->next = delete_Token(t->next);
3499 } /* endfor */
3500 } else
3501 t = t->next;
3503 /* If we concatenaded something, re-scan the line for macros */
3504 if (rescan) {
3505 tline = thead;
3506 goto again;
3509 if (org_tline) {
3510 if (thead) {
3511 *org_tline = *thead;
3512 /* since we just gave text to org_line, don't free it */
3513 thead->text = NULL;
3514 delete_Token(thead);
3515 } else {
3516 /* the expression expanded to empty line;
3517 we can't return NULL for some reasons
3518 we just set the line to a single WHITESPACE token. */
3519 memset(org_tline, 0, sizeof(*org_tline));
3520 org_tline->text = NULL;
3521 org_tline->type = TOK_WHITESPACE;
3523 thead = org_tline;
3526 return thead;
3530 * Similar to expand_smacro but used exclusively with macro identifiers
3531 * right before they are fetched in. The reason is that there can be
3532 * identifiers consisting of several subparts. We consider that if there
3533 * are more than one element forming the name, user wants a expansion,
3534 * otherwise it will be left as-is. Example:
3536 * %define %$abc cde
3538 * the identifier %$abc will be left as-is so that the handler for %define
3539 * will suck it and define the corresponding value. Other case:
3541 * %define _%$abc cde
3543 * In this case user wants name to be expanded *before* %define starts
3544 * working, so we'll expand %$abc into something (if it has a value;
3545 * otherwise it will be left as-is) then concatenate all successive
3546 * PP_IDs into one.
3548 static Token *expand_id(Token * tline)
3550 Token *cur, *oldnext = NULL;
3552 if (!tline || !tline->next)
3553 return tline;
3555 cur = tline;
3556 while (cur->next &&
3557 (cur->next->type == TOK_ID ||
3558 cur->next->type == TOK_PREPROC_ID
3559 || cur->next->type == TOK_NUMBER))
3560 cur = cur->next;
3562 /* If identifier consists of just one token, don't expand */
3563 if (cur == tline)
3564 return tline;
3566 if (cur) {
3567 oldnext = cur->next; /* Detach the tail past identifier */
3568 cur->next = NULL; /* so that expand_smacro stops here */
3571 tline = expand_smacro(tline);
3573 if (cur) {
3574 /* expand_smacro possibly changhed tline; re-scan for EOL */
3575 cur = tline;
3576 while (cur && cur->next)
3577 cur = cur->next;
3578 if (cur)
3579 cur->next = oldnext;
3582 return tline;
3586 * Determine whether the given line constitutes a multi-line macro
3587 * call, and return the MMacro structure called if so. Doesn't have
3588 * to check for an initial label - that's taken care of in
3589 * expand_mmacro - but must check numbers of parameters. Guaranteed
3590 * to be called with tline->type == TOK_ID, so the putative macro
3591 * name is easy to find.
3593 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3595 MMacro *head, *m;
3596 Token **params;
3597 int nparam;
3599 head = (MMacro *) hash_findix(&mmacros, tline->text);
3602 * Efficiency: first we see if any macro exists with the given
3603 * name. If not, we can return NULL immediately. _Then_ we
3604 * count the parameters, and then we look further along the
3605 * list if necessary to find the proper MMacro.
3607 for (m = head; m; m = m->next)
3608 if (!mstrcmp(m->name, tline->text, m->casesense))
3609 break;
3610 if (!m)
3611 return NULL;
3614 * OK, we have a potential macro. Count and demarcate the
3615 * parameters.
3617 count_mmac_params(tline->next, &nparam, &params);
3620 * So we know how many parameters we've got. Find the MMacro
3621 * structure that handles this number.
3623 while (m) {
3624 if (m->nparam_min <= nparam
3625 && (m->plus || nparam <= m->nparam_max)) {
3627 * This one is right. Just check if cycle removal
3628 * prohibits us using it before we actually celebrate...
3630 if (m->in_progress) {
3631 #if 0
3632 error(ERR_NONFATAL,
3633 "self-reference in multi-line macro `%s'", m->name);
3634 #endif
3635 nasm_free(params);
3636 return NULL;
3639 * It's right, and we can use it. Add its default
3640 * parameters to the end of our list if necessary.
3642 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3643 params =
3644 nasm_realloc(params,
3645 ((m->nparam_min + m->ndefs +
3646 1) * sizeof(*params)));
3647 while (nparam < m->nparam_min + m->ndefs) {
3648 params[nparam] = m->defaults[nparam - m->nparam_min];
3649 nparam++;
3653 * If we've gone over the maximum parameter count (and
3654 * we're in Plus mode), ignore parameters beyond
3655 * nparam_max.
3657 if (m->plus && nparam > m->nparam_max)
3658 nparam = m->nparam_max;
3660 * Then terminate the parameter list, and leave.
3662 if (!params) { /* need this special case */
3663 params = nasm_malloc(sizeof(*params));
3664 nparam = 0;
3666 params[nparam] = NULL;
3667 *params_array = params;
3668 return m;
3671 * This one wasn't right: look for the next one with the
3672 * same name.
3674 for (m = m->next; m; m = m->next)
3675 if (!mstrcmp(m->name, tline->text, m->casesense))
3676 break;
3680 * After all that, we didn't find one with the right number of
3681 * parameters. Issue a warning, and fail to expand the macro.
3683 error(ERR_WARNING | ERR_WARN_MNP,
3684 "macro `%s' exists, but not taking %d parameters",
3685 tline->text, nparam);
3686 nasm_free(params);
3687 return NULL;
3691 * Expand the multi-line macro call made by the given line, if
3692 * there is one to be expanded. If there is, push the expansion on
3693 * istk->expansion and return 1. Otherwise return 0.
3695 static int expand_mmacro(Token * tline)
3697 Token *startline = tline;
3698 Token *label = NULL;
3699 int dont_prepend = 0;
3700 Token **params, *t, *mtok, *tt;
3701 MMacro *m;
3702 Line *l, *ll;
3703 int i, nparam, *paramlen;
3705 t = tline;
3706 skip_white_(t);
3707 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3708 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3709 return 0;
3710 mtok = t;
3711 m = is_mmacro(t, &params);
3712 if (!m) {
3713 Token *last;
3715 * We have an id which isn't a macro call. We'll assume
3716 * it might be a label; we'll also check to see if a
3717 * colon follows it. Then, if there's another id after
3718 * that lot, we'll check it again for macro-hood.
3720 label = last = t;
3721 t = t->next;
3722 if (tok_type_(t, TOK_WHITESPACE))
3723 last = t, t = t->next;
3724 if (tok_is_(t, ":")) {
3725 dont_prepend = 1;
3726 last = t, t = t->next;
3727 if (tok_type_(t, TOK_WHITESPACE))
3728 last = t, t = t->next;
3730 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3731 return 0;
3732 last->next = NULL;
3733 tline = t;
3737 * Fix up the parameters: this involves stripping leading and
3738 * trailing whitespace, then stripping braces if they are
3739 * present.
3741 for (nparam = 0; params[nparam]; nparam++) ;
3742 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3744 for (i = 0; params[i]; i++) {
3745 int brace = false;
3746 int comma = (!m->plus || i < nparam - 1);
3748 t = params[i];
3749 skip_white_(t);
3750 if (tok_is_(t, "{"))
3751 t = t->next, brace = true, comma = false;
3752 params[i] = t;
3753 paramlen[i] = 0;
3754 while (t) {
3755 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3756 break; /* ... because we have hit a comma */
3757 if (comma && t->type == TOK_WHITESPACE
3758 && tok_is_(t->next, ","))
3759 break; /* ... or a space then a comma */
3760 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3761 break; /* ... or a brace */
3762 t = t->next;
3763 paramlen[i]++;
3768 * OK, we have a MMacro structure together with a set of
3769 * parameters. We must now go through the expansion and push
3770 * copies of each Line on to istk->expansion. Substitution of
3771 * parameter tokens and macro-local tokens doesn't get done
3772 * until the single-line macro substitution process; this is
3773 * because delaying them allows us to change the semantics
3774 * later through %rotate.
3776 * First, push an end marker on to istk->expansion, mark this
3777 * macro as in progress, and set up its invocation-specific
3778 * variables.
3780 ll = nasm_malloc(sizeof(Line));
3781 ll->next = istk->expansion;
3782 ll->finishes = m;
3783 ll->first = NULL;
3784 istk->expansion = ll;
3786 m->in_progress = true;
3787 m->params = params;
3788 m->iline = tline;
3789 m->nparam = nparam;
3790 m->rotate = 0;
3791 m->paramlen = paramlen;
3792 m->unique = unique++;
3793 m->lineno = 0;
3795 m->next_active = istk->mstk;
3796 istk->mstk = m;
3798 for (l = m->expansion; l; l = l->next) {
3799 Token **tail;
3801 ll = nasm_malloc(sizeof(Line));
3802 ll->finishes = NULL;
3803 ll->next = istk->expansion;
3804 istk->expansion = ll;
3805 tail = &ll->first;
3807 for (t = l->first; t; t = t->next) {
3808 Token *x = t;
3809 switch (t->type) {
3810 case TOK_PREPROC_Q:
3811 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3812 break;
3813 case TOK_PREPROC_QQ:
3814 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3815 break;
3816 case TOK_PREPROC_ID:
3817 if (t->text[1] == '0' && t->text[2] == '0') {
3818 dont_prepend = -1;
3819 x = label;
3820 if (!x)
3821 continue;
3823 /* fall through */
3824 default:
3825 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3826 break;
3828 tail = &tt->next;
3830 *tail = NULL;
3834 * If we had a label, push it on as the first line of
3835 * the macro expansion.
3837 if (label) {
3838 if (dont_prepend < 0)
3839 free_tlist(startline);
3840 else {
3841 ll = nasm_malloc(sizeof(Line));
3842 ll->finishes = NULL;
3843 ll->next = istk->expansion;
3844 istk->expansion = ll;
3845 ll->first = startline;
3846 if (!dont_prepend) {
3847 while (label->next)
3848 label = label->next;
3849 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3854 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3856 return 1;
3860 * Since preprocessor always operate only on the line that didn't
3861 * arrived yet, we should always use ERR_OFFBY1. Also since user
3862 * won't want to see same error twice (preprocessing is done once
3863 * per pass) we will want to show errors only during pass one.
3865 static void error(int severity, const char *fmt, ...)
3867 va_list arg;
3868 char buff[1024];
3870 /* If we're in a dead branch of IF or something like it, ignore the error */
3871 if (istk && istk->conds && !emitting(istk->conds->state))
3872 return;
3874 va_start(arg, fmt);
3875 vsnprintf(buff, sizeof(buff), fmt, arg);
3876 va_end(arg);
3878 if (istk && istk->mstk && istk->mstk->name)
3879 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3880 istk->mstk->lineno, buff);
3881 else
3882 _error(severity | ERR_PASS1, "%s", buff);
3885 static void
3886 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3887 ListGen * listgen, StrList **deplist)
3889 _error = errfunc;
3890 cstk = NULL;
3891 istk = nasm_malloc(sizeof(Include));
3892 istk->next = NULL;
3893 istk->conds = NULL;
3894 istk->expansion = NULL;
3895 istk->mstk = NULL;
3896 istk->fp = fopen(file, "r");
3897 istk->fname = NULL;
3898 src_set_fname(nasm_strdup(file));
3899 src_set_linnum(0);
3900 istk->lineinc = 1;
3901 if (!istk->fp)
3902 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3903 file);
3904 defining = NULL;
3905 init_macros();
3906 unique = 0;
3907 if (tasm_compatible_mode) {
3908 stdmacpos = nasm_stdmac;
3909 } else {
3910 stdmacpos = nasm_stdmac_after_tasm;
3912 any_extrastdmac = (extrastdmac != NULL);
3913 list = listgen;
3914 evaluate = eval;
3915 pass = apass;
3916 dephead = deptail = deplist;
3917 if (deplist) {
3918 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3919 sl->next = NULL;
3920 strcpy(sl->str, file);
3921 *deptail = sl;
3922 deptail = &sl->next;
3926 static char *pp_getline(void)
3928 char *line;
3929 Token *tline;
3931 while (1) {
3933 * Fetch a tokenized line, either from the macro-expansion
3934 * buffer or from the input file.
3936 tline = NULL;
3937 while (istk->expansion && istk->expansion->finishes) {
3938 Line *l = istk->expansion;
3939 if (!l->finishes->name && l->finishes->in_progress > 1) {
3940 Line *ll;
3943 * This is a macro-end marker for a macro with no
3944 * name, which means it's not really a macro at all
3945 * but a %rep block, and the `in_progress' field is
3946 * more than 1, meaning that we still need to
3947 * repeat. (1 means the natural last repetition; 0
3948 * means termination by %exitrep.) We have
3949 * therefore expanded up to the %endrep, and must
3950 * push the whole block on to the expansion buffer
3951 * again. We don't bother to remove the macro-end
3952 * marker: we'd only have to generate another one
3953 * if we did.
3955 l->finishes->in_progress--;
3956 for (l = l->finishes->expansion; l; l = l->next) {
3957 Token *t, *tt, **tail;
3959 ll = nasm_malloc(sizeof(Line));
3960 ll->next = istk->expansion;
3961 ll->finishes = NULL;
3962 ll->first = NULL;
3963 tail = &ll->first;
3965 for (t = l->first; t; t = t->next) {
3966 if (t->text || t->type == TOK_WHITESPACE) {
3967 tt = *tail =
3968 new_Token(NULL, t->type, t->text, 0);
3969 tail = &tt->next;
3973 istk->expansion = ll;
3975 } else {
3977 * Check whether a `%rep' was started and not ended
3978 * within this macro expansion. This can happen and
3979 * should be detected. It's a fatal error because
3980 * I'm too confused to work out how to recover
3981 * sensibly from it.
3983 if (defining) {
3984 if (defining->name)
3985 error(ERR_PANIC,
3986 "defining with name in expansion");
3987 else if (istk->mstk->name)
3988 error(ERR_FATAL,
3989 "`%%rep' without `%%endrep' within"
3990 " expansion of macro `%s'",
3991 istk->mstk->name);
3995 * FIXME: investigate the relationship at this point between
3996 * istk->mstk and l->finishes
3999 MMacro *m = istk->mstk;
4000 istk->mstk = m->next_active;
4001 if (m->name) {
4003 * This was a real macro call, not a %rep, and
4004 * therefore the parameter information needs to
4005 * be freed.
4007 nasm_free(m->params);
4008 free_tlist(m->iline);
4009 nasm_free(m->paramlen);
4010 l->finishes->in_progress = false;
4011 } else
4012 free_mmacro(m);
4014 istk->expansion = l->next;
4015 nasm_free(l);
4016 list->downlevel(LIST_MACRO);
4019 while (1) { /* until we get a line we can use */
4021 if (istk->expansion) { /* from a macro expansion */
4022 char *p;
4023 Line *l = istk->expansion;
4024 if (istk->mstk)
4025 istk->mstk->lineno++;
4026 tline = l->first;
4027 istk->expansion = l->next;
4028 nasm_free(l);
4029 p = detoken(tline, false);
4030 list->line(LIST_MACRO, p);
4031 nasm_free(p);
4032 break;
4034 line = read_line();
4035 if (line) { /* from the current input file */
4036 line = prepreproc(line);
4037 tline = tokenize(line);
4038 nasm_free(line);
4039 break;
4042 * The current file has ended; work down the istk
4045 Include *i = istk;
4046 fclose(i->fp);
4047 if (i->conds)
4048 error(ERR_FATAL,
4049 "expected `%%endif' before end of file");
4050 /* only set line and file name if there's a next node */
4051 if (i->next) {
4052 src_set_linnum(i->lineno);
4053 nasm_free(src_set_fname(i->fname));
4055 istk = i->next;
4056 list->downlevel(LIST_INCLUDE);
4057 nasm_free(i);
4058 if (!istk)
4059 return NULL;
4064 * We must expand MMacro parameters and MMacro-local labels
4065 * _before_ we plunge into directive processing, to cope
4066 * with things like `%define something %1' such as STRUC
4067 * uses. Unless we're _defining_ a MMacro, in which case
4068 * those tokens should be left alone to go into the
4069 * definition; and unless we're in a non-emitting
4070 * condition, in which case we don't want to meddle with
4071 * anything.
4073 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4074 tline = expand_mmac_params(tline);
4077 * Check the line to see if it's a preprocessor directive.
4079 if (do_directive(tline) == DIRECTIVE_FOUND) {
4080 continue;
4081 } else if (defining) {
4083 * We're defining a multi-line macro. We emit nothing
4084 * at all, and just
4085 * shove the tokenized line on to the macro definition.
4087 Line *l = nasm_malloc(sizeof(Line));
4088 l->next = defining->expansion;
4089 l->first = tline;
4090 l->finishes = false;
4091 defining->expansion = l;
4092 continue;
4093 } else if (istk->conds && !emitting(istk->conds->state)) {
4095 * We're in a non-emitting branch of a condition block.
4096 * Emit nothing at all, not even a blank line: when we
4097 * emerge from the condition we'll give a line-number
4098 * directive so we keep our place correctly.
4100 free_tlist(tline);
4101 continue;
4102 } else if (istk->mstk && !istk->mstk->in_progress) {
4104 * We're in a %rep block which has been terminated, so
4105 * we're walking through to the %endrep without
4106 * emitting anything. Emit nothing at all, not even a
4107 * blank line: when we emerge from the %rep block we'll
4108 * give a line-number directive so we keep our place
4109 * correctly.
4111 free_tlist(tline);
4112 continue;
4113 } else {
4114 tline = expand_smacro(tline);
4115 if (!expand_mmacro(tline)) {
4117 * De-tokenize the line again, and emit it.
4119 line = detoken(tline, true);
4120 free_tlist(tline);
4121 break;
4122 } else {
4123 continue; /* expand_mmacro calls free_tlist */
4128 return line;
4131 static void pp_cleanup(int pass)
4133 if (defining) {
4134 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4135 defining->name);
4136 free_mmacro(defining);
4138 while (cstk)
4139 ctx_pop();
4140 free_macros();
4141 while (istk) {
4142 Include *i = istk;
4143 istk = istk->next;
4144 fclose(i->fp);
4145 nasm_free(i->fname);
4146 nasm_free(i);
4148 while (cstk)
4149 ctx_pop();
4150 if (pass == 0) {
4151 free_llist(predef);
4152 delete_Blocks();
4156 void pp_include_path(char *path)
4158 IncPath *i;
4160 i = nasm_malloc(sizeof(IncPath));
4161 i->path = path ? nasm_strdup(path) : NULL;
4162 i->next = NULL;
4164 if (ipath != NULL) {
4165 IncPath *j = ipath;
4166 while (j->next != NULL)
4167 j = j->next;
4168 j->next = i;
4169 } else {
4170 ipath = i;
4174 void pp_pre_include(char *fname)
4176 Token *inc, *space, *name;
4177 Line *l;
4179 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4180 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4181 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4183 l = nasm_malloc(sizeof(Line));
4184 l->next = predef;
4185 l->first = inc;
4186 l->finishes = false;
4187 predef = l;
4190 void pp_pre_define(char *definition)
4192 Token *def, *space;
4193 Line *l;
4194 char *equals;
4196 equals = strchr(definition, '=');
4197 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4198 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4199 if (equals)
4200 *equals = ' ';
4201 space->next = tokenize(definition);
4202 if (equals)
4203 *equals = '=';
4205 l = nasm_malloc(sizeof(Line));
4206 l->next = predef;
4207 l->first = def;
4208 l->finishes = false;
4209 predef = l;
4212 void pp_pre_undefine(char *definition)
4214 Token *def, *space;
4215 Line *l;
4217 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4218 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4219 space->next = tokenize(definition);
4221 l = nasm_malloc(sizeof(Line));
4222 l->next = predef;
4223 l->first = def;
4224 l->finishes = false;
4225 predef = l;
4229 * Added by Keith Kanios:
4231 * This function is used to assist with "runtime" preprocessor
4232 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4234 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4235 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4238 void pp_runtime(char *definition)
4240 Token *def;
4242 def = tokenize(definition);
4243 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4244 free_tlist(def);
4248 void pp_extra_stdmac(const char **macros)
4250 extrastdmac = macros;
4253 static void make_tok_num(Token * tok, int64_t val)
4255 char numbuf[20];
4256 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4257 tok->text = nasm_strdup(numbuf);
4258 tok->type = TOK_NUMBER;
4261 Preproc nasmpp = {
4262 pp_reset,
4263 pp_getline,
4264 pp_cleanup