doc: A few floating-point examples
[nasm/autotest.git] / preproc.c
blobf324d1c5bd94bd585de139a14b5263c2606ee228
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, bool 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 char bq, *ep;
1148 bool errquote;
1149 bool rn_warn;
1150 size_t l;
1152 bq = tline->text[0];
1153 l = nasm_unquote(tline->text, &ep);
1154 if (ep[0] != bq || ep[1] != '\0')
1155 errquote = true;
1157 if (errquote)
1158 return tokval->t_type = TOKEN_ERRNUM;
1160 tokval->t_integer = readstrnum(tline->text, l, &rn_warn);
1161 if (rn_warn)
1162 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1163 tokval->t_charptr = NULL;
1164 return tokval->t_type = TOKEN_NUM;
1167 if (tline->type == TOK_OTHER) {
1168 if (!strcmp(tline->text, "<<"))
1169 return tokval->t_type = TOKEN_SHL;
1170 if (!strcmp(tline->text, ">>"))
1171 return tokval->t_type = TOKEN_SHR;
1172 if (!strcmp(tline->text, "//"))
1173 return tokval->t_type = TOKEN_SDIV;
1174 if (!strcmp(tline->text, "%%"))
1175 return tokval->t_type = TOKEN_SMOD;
1176 if (!strcmp(tline->text, "=="))
1177 return tokval->t_type = TOKEN_EQ;
1178 if (!strcmp(tline->text, "<>"))
1179 return tokval->t_type = TOKEN_NE;
1180 if (!strcmp(tline->text, "!="))
1181 return tokval->t_type = TOKEN_NE;
1182 if (!strcmp(tline->text, "<="))
1183 return tokval->t_type = TOKEN_LE;
1184 if (!strcmp(tline->text, ">="))
1185 return tokval->t_type = TOKEN_GE;
1186 if (!strcmp(tline->text, "&&"))
1187 return tokval->t_type = TOKEN_DBL_AND;
1188 if (!strcmp(tline->text, "^^"))
1189 return tokval->t_type = TOKEN_DBL_XOR;
1190 if (!strcmp(tline->text, "||"))
1191 return tokval->t_type = TOKEN_DBL_OR;
1195 * We have no other options: just return the first character of
1196 * the token text.
1198 return tokval->t_type = tline->text[0];
1202 * Compare a string to the name of an existing macro; this is a
1203 * simple wrapper which calls either strcmp or nasm_stricmp
1204 * depending on the value of the `casesense' parameter.
1206 static int mstrcmp(const char *p, const char *q, bool casesense)
1208 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1212 * Compare a string to the name of an existing macro; this is a
1213 * simple wrapper which calls either strcmp or nasm_stricmp
1214 * depending on the value of the `casesense' parameter.
1216 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1218 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1222 * Return the Context structure associated with a %$ token. Return
1223 * NULL, having _already_ reported an error condition, if the
1224 * context stack isn't deep enough for the supplied number of $
1225 * signs.
1226 * If all_contexts == true, contexts that enclose current are
1227 * also scanned for such smacro, until it is found; if not -
1228 * only the context that directly results from the number of $'s
1229 * in variable's name.
1231 static Context *get_ctx(char *name, bool all_contexts)
1233 Context *ctx;
1234 SMacro *m;
1235 int i;
1237 if (!name || name[0] != '%' || name[1] != '$')
1238 return NULL;
1240 if (!cstk) {
1241 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1242 return NULL;
1245 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1246 ctx = ctx->next;
1247 /* i--; Lino - 02/25/02 */
1249 if (!ctx) {
1250 error(ERR_NONFATAL, "`%s': context stack is only"
1251 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1252 return NULL;
1254 if (!all_contexts)
1255 return ctx;
1257 do {
1258 /* Search for this smacro in found context */
1259 m = hash_findix(&ctx->localmac, name);
1260 while (m) {
1261 if (!mstrcmp(m->name, name, m->casesense))
1262 return ctx;
1263 m = m->next;
1265 ctx = ctx->next;
1267 while (ctx);
1268 return NULL;
1272 * Check to see if a file is already in a string list
1274 static bool in_list(const StrList *list, const char *str)
1276 while (list) {
1277 if (!strcmp(list->str, str))
1278 return true;
1279 list = list->next;
1281 return false;
1285 * Open an include file. This routine must always return a valid
1286 * file pointer if it returns - it's responsible for throwing an
1287 * ERR_FATAL and bombing out completely if not. It should also try
1288 * the include path one by one until it finds the file or reaches
1289 * the end of the path.
1291 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1292 bool missing_ok)
1294 FILE *fp;
1295 char *prefix = "";
1296 IncPath *ip = ipath;
1297 int len = strlen(file);
1298 size_t prefix_len = 0;
1299 StrList *sl;
1301 while (1) {
1302 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1303 memcpy(sl->str, prefix, prefix_len);
1304 memcpy(sl->str+prefix_len, file, len+1);
1305 fp = fopen(sl->str, "r");
1306 if (fp && dhead && !in_list(*dhead, sl->str)) {
1307 sl->next = NULL;
1308 **dtail = sl;
1309 *dtail = &sl->next;
1310 } else {
1311 nasm_free(sl);
1313 if (fp)
1314 return fp;
1315 if (!ip) {
1316 if (!missing_ok)
1317 break;
1318 prefix = NULL;
1319 } else {
1320 prefix = ip->path;
1321 ip = ip->next;
1323 if (prefix) {
1324 prefix_len = strlen(prefix);
1325 } else {
1326 /* -MG given and file not found */
1327 if (dhead && !in_list(*dhead, file)) {
1328 sl = nasm_malloc(len+1+sizeof sl->next);
1329 sl->next = NULL;
1330 strcpy(sl->str, file);
1331 **dtail = sl;
1332 *dtail = &sl->next;
1334 return NULL;
1338 error(ERR_FATAL, "unable to open include file `%s'", file);
1339 return NULL; /* never reached - placate compilers */
1343 * Determine if we should warn on defining a single-line macro of
1344 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1345 * return true if _any_ single-line macro of that name is defined.
1346 * Otherwise, will return true if a single-line macro with either
1347 * `nparam' or no parameters is defined.
1349 * If a macro with precisely the right number of parameters is
1350 * defined, or nparam is -1, the address of the definition structure
1351 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1352 * is NULL, no action will be taken regarding its contents, and no
1353 * error will occur.
1355 * Note that this is also called with nparam zero to resolve
1356 * `ifdef'.
1358 * If you already know which context macro belongs to, you can pass
1359 * the context pointer as first parameter; if you won't but name begins
1360 * with %$ the context will be automatically computed. If all_contexts
1361 * is true, macro will be searched in outer contexts as well.
1363 static bool
1364 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1365 bool nocase)
1367 struct hash_table *smtbl;
1368 SMacro *m;
1370 if (ctx) {
1371 smtbl = &ctx->localmac;
1372 } else if (name[0] == '%' && name[1] == '$') {
1373 if (cstk)
1374 ctx = get_ctx(name, false);
1375 if (!ctx)
1376 return false; /* got to return _something_ */
1377 smtbl = &ctx->localmac;
1378 } else {
1379 smtbl = &smacros;
1381 m = (SMacro *) hash_findix(smtbl, name);
1383 while (m) {
1384 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1385 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1386 if (defn) {
1387 if (nparam == (int) m->nparam || nparam == -1)
1388 *defn = m;
1389 else
1390 *defn = NULL;
1392 return true;
1394 m = m->next;
1397 return false;
1401 * Count and mark off the parameters in a multi-line macro call.
1402 * This is called both from within the multi-line macro expansion
1403 * code, and also to mark off the default parameters when provided
1404 * in a %macro definition line.
1406 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1408 int paramsize, brace;
1410 *nparam = paramsize = 0;
1411 *params = NULL;
1412 while (t) {
1413 if (*nparam >= paramsize) {
1414 paramsize += PARAM_DELTA;
1415 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1417 skip_white_(t);
1418 brace = false;
1419 if (tok_is_(t, "{"))
1420 brace = true;
1421 (*params)[(*nparam)++] = t;
1422 while (tok_isnt_(t, brace ? "}" : ","))
1423 t = t->next;
1424 if (t) { /* got a comma/brace */
1425 t = t->next;
1426 if (brace) {
1428 * Now we've found the closing brace, look further
1429 * for the comma.
1431 skip_white_(t);
1432 if (tok_isnt_(t, ",")) {
1433 error(ERR_NONFATAL,
1434 "braces do not enclose all of macro parameter");
1435 while (tok_isnt_(t, ","))
1436 t = t->next;
1438 if (t)
1439 t = t->next; /* eat the comma */
1446 * Determine whether one of the various `if' conditions is true or
1447 * not.
1449 * We must free the tline we get passed.
1451 static bool if_condition(Token * tline, enum preproc_token ct)
1453 enum pp_conditional i = PP_COND(ct);
1454 bool j;
1455 Token *t, *tt, **tptr, *origline;
1456 struct tokenval tokval;
1457 expr *evalresult;
1458 enum pp_token_type needtype;
1460 origline = tline;
1462 switch (i) {
1463 case PPC_IFCTX:
1464 j = false; /* have we matched yet? */
1465 while (cstk && tline) {
1466 skip_white_(tline);
1467 if (!tline || tline->type != TOK_ID) {
1468 error(ERR_NONFATAL,
1469 "`%s' expects context identifiers", pp_directives[ct]);
1470 free_tlist(origline);
1471 return -1;
1473 if (!nasm_stricmp(tline->text, cstk->name))
1474 j = true;
1475 tline = tline->next;
1477 break;
1479 case PPC_IFDEF:
1480 j = false; /* have we matched yet? */
1481 while (tline) {
1482 skip_white_(tline);
1483 if (!tline || (tline->type != TOK_ID &&
1484 (tline->type != TOK_PREPROC_ID ||
1485 tline->text[1] != '$'))) {
1486 error(ERR_NONFATAL,
1487 "`%s' expects macro identifiers", pp_directives[ct]);
1488 goto fail;
1490 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1491 j = true;
1492 tline = tline->next;
1494 break;
1496 case PPC_IFIDN:
1497 case PPC_IFIDNI:
1498 tline = expand_smacro(tline);
1499 t = tt = tline;
1500 while (tok_isnt_(tt, ","))
1501 tt = tt->next;
1502 if (!tt) {
1503 error(ERR_NONFATAL,
1504 "`%s' expects two comma-separated arguments",
1505 pp_directives[ct]);
1506 goto fail;
1508 tt = tt->next;
1509 j = true; /* assume equality unless proved not */
1510 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1511 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1512 error(ERR_NONFATAL, "`%s': more than one comma on line",
1513 pp_directives[ct]);
1514 goto fail;
1516 if (t->type == TOK_WHITESPACE) {
1517 t = t->next;
1518 continue;
1520 if (tt->type == TOK_WHITESPACE) {
1521 tt = tt->next;
1522 continue;
1524 if (tt->type != t->type) {
1525 j = false; /* found mismatching tokens */
1526 break;
1528 /* When comparing strings, need to unquote them first */
1529 if (t->type == TOK_STRING) {
1530 size_t l1 = nasm_unquote(t->text, NULL);
1531 size_t l2 = nasm_unquote(tt->text, NULL);
1533 if (l1 != l2) {
1534 j = false;
1535 break;
1537 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1538 j = false;
1539 break;
1541 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1542 j = false; /* found mismatching tokens */
1543 break;
1546 t = t->next;
1547 tt = tt->next;
1549 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1550 j = false; /* trailing gunk on one end or other */
1551 break;
1553 case PPC_IFMACRO:
1555 bool found = false;
1556 MMacro searching, *mmac;
1558 tline = tline->next;
1559 skip_white_(tline);
1560 tline = expand_id(tline);
1561 if (!tok_type_(tline, TOK_ID)) {
1562 error(ERR_NONFATAL,
1563 "`%s' expects a macro name", pp_directives[ct]);
1564 goto fail;
1566 searching.name = nasm_strdup(tline->text);
1567 searching.casesense = true;
1568 searching.plus = false;
1569 searching.nolist = false;
1570 searching.in_progress = 0;
1571 searching.rep_nest = NULL;
1572 searching.nparam_min = 0;
1573 searching.nparam_max = INT_MAX;
1574 tline = expand_smacro(tline->next);
1575 skip_white_(tline);
1576 if (!tline) {
1577 } else if (!tok_type_(tline, TOK_NUMBER)) {
1578 error(ERR_NONFATAL,
1579 "`%s' expects a parameter count or nothing",
1580 pp_directives[ct]);
1581 } else {
1582 searching.nparam_min = searching.nparam_max =
1583 readnum(tline->text, &j);
1584 if (j)
1585 error(ERR_NONFATAL,
1586 "unable to parse parameter count `%s'",
1587 tline->text);
1589 if (tline && tok_is_(tline->next, "-")) {
1590 tline = tline->next->next;
1591 if (tok_is_(tline, "*"))
1592 searching.nparam_max = INT_MAX;
1593 else if (!tok_type_(tline, TOK_NUMBER))
1594 error(ERR_NONFATAL,
1595 "`%s' expects a parameter count after `-'",
1596 pp_directives[ct]);
1597 else {
1598 searching.nparam_max = readnum(tline->text, &j);
1599 if (j)
1600 error(ERR_NONFATAL,
1601 "unable to parse parameter count `%s'",
1602 tline->text);
1603 if (searching.nparam_min > searching.nparam_max)
1604 error(ERR_NONFATAL,
1605 "minimum parameter count exceeds maximum");
1608 if (tline && tok_is_(tline->next, "+")) {
1609 tline = tline->next;
1610 searching.plus = true;
1612 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1613 while (mmac) {
1614 if (!strcmp(mmac->name, searching.name) &&
1615 (mmac->nparam_min <= searching.nparam_max
1616 || searching.plus)
1617 && (searching.nparam_min <= mmac->nparam_max
1618 || mmac->plus)) {
1619 found = true;
1620 break;
1622 mmac = mmac->next;
1624 nasm_free(searching.name);
1625 j = found;
1626 break;
1629 case PPC_IFID:
1630 needtype = TOK_ID;
1631 goto iftype;
1632 case PPC_IFNUM:
1633 needtype = TOK_NUMBER;
1634 goto iftype;
1635 case PPC_IFSTR:
1636 needtype = TOK_STRING;
1637 goto iftype;
1639 iftype:
1640 t = tline = expand_smacro(tline);
1642 while (tok_type_(t, TOK_WHITESPACE) ||
1643 (needtype == TOK_NUMBER &&
1644 tok_type_(t, TOK_OTHER) &&
1645 (t->text[0] == '-' || t->text[0] == '+') &&
1646 !t->text[1]))
1647 t = t->next;
1649 j = tok_type_(t, needtype);
1650 break;
1652 case PPC_IFTOKEN:
1653 t = tline = expand_smacro(tline);
1654 while (tok_type_(t, TOK_WHITESPACE))
1655 t = t->next;
1657 j = false;
1658 if (t) {
1659 t = t->next; /* Skip the actual token */
1660 while (tok_type_(t, TOK_WHITESPACE))
1661 t = t->next;
1662 j = !t; /* Should be nothing left */
1664 break;
1666 case PPC_IFEMPTY:
1667 t = tline = expand_smacro(tline);
1668 while (tok_type_(t, TOK_WHITESPACE))
1669 t = t->next;
1671 j = !t; /* Should be empty */
1672 break;
1674 case PPC_IF:
1675 t = tline = expand_smacro(tline);
1676 tptr = &t;
1677 tokval.t_type = TOKEN_INVALID;
1678 evalresult = evaluate(ppscan, tptr, &tokval,
1679 NULL, pass | CRITICAL, error, NULL);
1680 if (!evalresult)
1681 return -1;
1682 if (tokval.t_type)
1683 error(ERR_WARNING,
1684 "trailing garbage after expression ignored");
1685 if (!is_simple(evalresult)) {
1686 error(ERR_NONFATAL,
1687 "non-constant value given to `%s'", pp_directives[ct]);
1688 goto fail;
1690 j = reloc_value(evalresult) != 0;
1691 return j;
1693 default:
1694 error(ERR_FATAL,
1695 "preprocessor directive `%s' not yet implemented",
1696 pp_directives[ct]);
1697 goto fail;
1700 free_tlist(origline);
1701 return j ^ PP_NEGATIVE(ct);
1703 fail:
1704 free_tlist(origline);
1705 return -1;
1709 * Expand macros in a string. Used in %error directives (and it should
1710 * almost certainly be removed from there, too.)
1712 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1713 * The returned variable should ALWAYS be freed after usage.
1715 void expand_macros_in_string(char **p)
1717 Token *line = tokenize(*p);
1718 line = expand_smacro(line);
1719 *p = detoken(line, false);
1723 * Common code for defining an smacro
1725 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1726 int nparam, Token *expansion)
1728 SMacro *smac, **smhead;
1729 struct hash_table *smtbl;
1731 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1732 if (!smac) {
1733 error(ERR_WARNING,
1734 "single-line macro `%s' defined both with and"
1735 " without parameters", mname);
1737 /* Some instances of the old code considered this a failure,
1738 some others didn't. What is the right thing to do here? */
1739 free_tlist(expansion);
1740 return false; /* Failure */
1741 } else {
1743 * We're redefining, so we have to take over an
1744 * existing SMacro structure. This means freeing
1745 * what was already in it.
1747 nasm_free(smac->name);
1748 free_tlist(smac->expansion);
1750 } else {
1751 smtbl = ctx ? &ctx->localmac : &smacros;
1752 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1753 smac = nasm_malloc(sizeof(SMacro));
1754 smac->next = *smhead;
1755 *smhead = smac;
1757 smac->name = nasm_strdup(mname);
1758 smac->casesense = casesense;
1759 smac->nparam = nparam;
1760 smac->expansion = expansion;
1761 smac->in_progress = false;
1762 return true; /* Success */
1766 * Undefine an smacro
1768 static void undef_smacro(Context *ctx, const char *mname)
1770 SMacro **smhead, *s, **sp;
1771 struct hash_table *smtbl;
1773 smtbl = ctx ? &ctx->localmac : &smacros;
1774 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1776 if (smhead) {
1778 * We now have a macro name... go hunt for it.
1780 sp = smhead;
1781 while ((s = *sp) != NULL) {
1782 if (!mstrcmp(s->name, mname, s->casesense)) {
1783 *sp = s->next;
1784 nasm_free(s->name);
1785 free_tlist(s->expansion);
1786 nasm_free(s);
1787 } else {
1788 sp = &s->next;
1795 * Decode a size directive
1797 static int parse_size(const char *str) {
1798 static const char *size_names[] =
1799 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1800 static const int sizes[] =
1801 { 0, 1, 4, 16, 8, 10, 2, 32 };
1803 return sizes[bsii(str, size_names, elements(size_names))+1];
1807 * find and process preprocessor directive in passed line
1808 * Find out if a line contains a preprocessor directive, and deal
1809 * with it if so.
1811 * If a directive _is_ found, it is the responsibility of this routine
1812 * (and not the caller) to free_tlist() the line.
1814 * @param tline a pointer to the current tokeninzed line linked list
1815 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1818 static int do_directive(Token * tline)
1820 enum preproc_token i;
1821 int j;
1822 bool err;
1823 int nparam;
1824 bool nolist;
1825 bool casesense;
1826 int k, m;
1827 int offset;
1828 char *p, *mname;
1829 Include *inc;
1830 Context *ctx;
1831 Cond *cond;
1832 MMacro *mmac, **mmhead;
1833 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1834 Line *l;
1835 struct tokenval tokval;
1836 expr *evalresult;
1837 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1838 int64_t count;
1840 origline = tline;
1842 skip_white_(tline);
1843 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
1844 (tline->text[1] == '%' || tline->text[1] == '$'
1845 || tline->text[1] == '!'))
1846 return NO_DIRECTIVE_FOUND;
1848 i = pp_token_hash(tline->text);
1851 * If we're in a non-emitting branch of a condition construct,
1852 * or walking to the end of an already terminated %rep block,
1853 * we should ignore all directives except for condition
1854 * directives.
1856 if (((istk->conds && !emitting(istk->conds->state)) ||
1857 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1858 return NO_DIRECTIVE_FOUND;
1862 * If we're defining a macro or reading a %rep block, we should
1863 * ignore all directives except for %macro/%imacro (which
1864 * generate an error), %endm/%endmacro, and (only if we're in a
1865 * %rep block) %endrep. If we're in a %rep block, another %rep
1866 * causes an error, so should be let through.
1868 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1869 i != PP_ENDMACRO && i != PP_ENDM &&
1870 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1871 return NO_DIRECTIVE_FOUND;
1874 switch (i) {
1875 case PP_INVALID:
1876 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1877 tline->text);
1878 return NO_DIRECTIVE_FOUND; /* didn't get it */
1880 case PP_STACKSIZE:
1881 /* Directive to tell NASM what the default stack size is. The
1882 * default is for a 16-bit stack, and this can be overriden with
1883 * %stacksize large.
1884 * the following form:
1886 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1888 tline = tline->next;
1889 if (tline && tline->type == TOK_WHITESPACE)
1890 tline = tline->next;
1891 if (!tline || tline->type != TOK_ID) {
1892 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1893 free_tlist(origline);
1894 return DIRECTIVE_FOUND;
1896 if (nasm_stricmp(tline->text, "flat") == 0) {
1897 /* All subsequent ARG directives are for a 32-bit stack */
1898 StackSize = 4;
1899 StackPointer = "ebp";
1900 ArgOffset = 8;
1901 LocalOffset = 0;
1902 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1903 /* All subsequent ARG directives are for a 64-bit stack */
1904 StackSize = 8;
1905 StackPointer = "rbp";
1906 ArgOffset = 8;
1907 LocalOffset = 0;
1908 } else if (nasm_stricmp(tline->text, "large") == 0) {
1909 /* All subsequent ARG directives are for a 16-bit stack,
1910 * far function call.
1912 StackSize = 2;
1913 StackPointer = "bp";
1914 ArgOffset = 4;
1915 LocalOffset = 0;
1916 } else if (nasm_stricmp(tline->text, "small") == 0) {
1917 /* All subsequent ARG directives are for a 16-bit stack,
1918 * far function call. We don't support near functions.
1920 StackSize = 2;
1921 StackPointer = "bp";
1922 ArgOffset = 6;
1923 LocalOffset = 0;
1924 } else {
1925 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1929 free_tlist(origline);
1930 return DIRECTIVE_FOUND;
1932 case PP_ARG:
1933 /* TASM like ARG directive to define arguments to functions, in
1934 * the following form:
1936 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1938 offset = ArgOffset;
1939 do {
1940 char *arg, directive[256];
1941 int size = StackSize;
1943 /* Find the argument name */
1944 tline = tline->next;
1945 if (tline && tline->type == TOK_WHITESPACE)
1946 tline = tline->next;
1947 if (!tline || tline->type != TOK_ID) {
1948 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1949 free_tlist(origline);
1950 return DIRECTIVE_FOUND;
1952 arg = tline->text;
1954 /* Find the argument size type */
1955 tline = tline->next;
1956 if (!tline || tline->type != TOK_OTHER
1957 || tline->text[0] != ':') {
1958 error(ERR_NONFATAL,
1959 "Syntax error processing `%%arg' directive");
1960 free_tlist(origline);
1961 return DIRECTIVE_FOUND;
1963 tline = tline->next;
1964 if (!tline || tline->type != TOK_ID) {
1965 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1966 free_tlist(origline);
1967 return DIRECTIVE_FOUND;
1970 /* Allow macro expansion of type parameter */
1971 tt = tokenize(tline->text);
1972 tt = expand_smacro(tt);
1973 size = parse_size(tt->text);
1974 if (!size) {
1975 error(ERR_NONFATAL,
1976 "Invalid size type for `%%arg' missing directive");
1977 free_tlist(tt);
1978 free_tlist(origline);
1979 return DIRECTIVE_FOUND;
1981 free_tlist(tt);
1983 /* Round up to even stack slots */
1984 size = (size+StackSize-1) & ~(StackSize-1);
1986 /* Now define the macro for the argument */
1987 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1988 arg, StackPointer, offset);
1989 do_directive(tokenize(directive));
1990 offset += size;
1992 /* Move to the next argument in the list */
1993 tline = tline->next;
1994 if (tline && tline->type == TOK_WHITESPACE)
1995 tline = tline->next;
1996 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1997 ArgOffset = offset;
1998 free_tlist(origline);
1999 return DIRECTIVE_FOUND;
2001 case PP_LOCAL:
2002 /* TASM like LOCAL directive to define local variables for a
2003 * function, in the following form:
2005 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2007 * The '= LocalSize' at the end is ignored by NASM, but is
2008 * required by TASM to define the local parameter size (and used
2009 * by the TASM macro package).
2011 offset = LocalOffset;
2012 do {
2013 char *local, directive[256];
2014 int size = StackSize;
2016 /* Find the argument name */
2017 tline = tline->next;
2018 if (tline && tline->type == TOK_WHITESPACE)
2019 tline = tline->next;
2020 if (!tline || tline->type != TOK_ID) {
2021 error(ERR_NONFATAL,
2022 "`%%local' missing argument parameter");
2023 free_tlist(origline);
2024 return DIRECTIVE_FOUND;
2026 local = tline->text;
2028 /* Find the argument size type */
2029 tline = tline->next;
2030 if (!tline || tline->type != TOK_OTHER
2031 || tline->text[0] != ':') {
2032 error(ERR_NONFATAL,
2033 "Syntax error processing `%%local' directive");
2034 free_tlist(origline);
2035 return DIRECTIVE_FOUND;
2037 tline = tline->next;
2038 if (!tline || tline->type != TOK_ID) {
2039 error(ERR_NONFATAL,
2040 "`%%local' missing size type parameter");
2041 free_tlist(origline);
2042 return DIRECTIVE_FOUND;
2045 /* Allow macro expansion of type parameter */
2046 tt = tokenize(tline->text);
2047 tt = expand_smacro(tt);
2048 size = parse_size(tt->text);
2049 if (!size) {
2050 error(ERR_NONFATAL,
2051 "Invalid size type for `%%local' missing directive");
2052 free_tlist(tt);
2053 free_tlist(origline);
2054 return DIRECTIVE_FOUND;
2056 free_tlist(tt);
2058 /* Round up to even stack slots */
2059 size = (size+StackSize-1) & ~(StackSize-1);
2061 offset += size; /* Negative offset, increment before */
2063 /* Now define the macro for the argument */
2064 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2065 local, StackPointer, offset);
2066 do_directive(tokenize(directive));
2068 /* Now define the assign to setup the enter_c macro correctly */
2069 snprintf(directive, sizeof(directive),
2070 "%%assign %%$localsize %%$localsize+%d", size);
2071 do_directive(tokenize(directive));
2073 /* Move to the next argument in the list */
2074 tline = tline->next;
2075 if (tline && tline->type == TOK_WHITESPACE)
2076 tline = tline->next;
2077 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2078 LocalOffset = offset;
2079 free_tlist(origline);
2080 return DIRECTIVE_FOUND;
2082 case PP_CLEAR:
2083 if (tline->next)
2084 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2085 free_macros();
2086 init_macros();
2087 free_tlist(origline);
2088 return DIRECTIVE_FOUND;
2090 case PP_DEPEND:
2091 t = tline->next = expand_smacro(tline->next);
2092 skip_white_(t);
2093 if (!t || (t->type != TOK_STRING &&
2094 t->type != TOK_INTERNAL_STRING)) {
2095 error(ERR_NONFATAL, "`%%depend' expects a file name");
2096 free_tlist(origline);
2097 return DIRECTIVE_FOUND; /* but we did _something_ */
2099 if (t->next)
2100 error(ERR_WARNING,
2101 "trailing garbage after `%%depend' ignored");
2102 p = t->text;
2103 if (t->type != TOK_INTERNAL_STRING)
2104 nasm_unquote(p, NULL);
2105 if (dephead && !in_list(*dephead, p)) {
2106 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2107 sl->next = NULL;
2108 strcpy(sl->str, p);
2109 *deptail = sl;
2110 deptail = &sl->next;
2112 free_tlist(origline);
2113 return DIRECTIVE_FOUND;
2115 case PP_INCLUDE:
2116 t = tline->next = expand_smacro(tline->next);
2117 skip_white_(t);
2119 if (!t || (t->type != TOK_STRING &&
2120 t->type != TOK_INTERNAL_STRING)) {
2121 error(ERR_NONFATAL, "`%%include' expects a file name");
2122 free_tlist(origline);
2123 return DIRECTIVE_FOUND; /* but we did _something_ */
2125 if (t->next)
2126 error(ERR_WARNING,
2127 "trailing garbage after `%%include' ignored");
2128 p = t->text;
2129 if (t->type != TOK_INTERNAL_STRING)
2130 nasm_unquote(p, NULL);
2131 inc = nasm_malloc(sizeof(Include));
2132 inc->next = istk;
2133 inc->conds = NULL;
2134 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2135 if (!inc->fp) {
2136 /* -MG given but file not found */
2137 nasm_free(inc);
2138 } else {
2139 inc->fname = src_set_fname(nasm_strdup(p));
2140 inc->lineno = src_set_linnum(0);
2141 inc->lineinc = 1;
2142 inc->expansion = NULL;
2143 inc->mstk = NULL;
2144 istk = inc;
2145 list->uplevel(LIST_INCLUDE);
2147 free_tlist(origline);
2148 return DIRECTIVE_FOUND;
2150 case PP_PUSH:
2151 tline = tline->next;
2152 skip_white_(tline);
2153 tline = expand_id(tline);
2154 if (!tok_type_(tline, TOK_ID)) {
2155 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2156 free_tlist(origline);
2157 return DIRECTIVE_FOUND; /* but we did _something_ */
2159 if (tline->next)
2160 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2161 ctx = nasm_malloc(sizeof(Context));
2162 ctx->next = cstk;
2163 hash_init(&ctx->localmac, HASH_SMALL);
2164 ctx->name = nasm_strdup(tline->text);
2165 ctx->number = unique++;
2166 cstk = ctx;
2167 free_tlist(origline);
2168 break;
2170 case PP_REPL:
2171 tline = tline->next;
2172 skip_white_(tline);
2173 tline = expand_id(tline);
2174 if (!tok_type_(tline, TOK_ID)) {
2175 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2176 free_tlist(origline);
2177 return DIRECTIVE_FOUND; /* but we did _something_ */
2179 if (tline->next)
2180 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2181 if (!cstk)
2182 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2183 else {
2184 nasm_free(cstk->name);
2185 cstk->name = nasm_strdup(tline->text);
2187 free_tlist(origline);
2188 break;
2190 case PP_POP:
2191 if (tline->next)
2192 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2193 if (!cstk)
2194 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2195 else
2196 ctx_pop();
2197 free_tlist(origline);
2198 break;
2200 case PP_ERROR:
2201 tline->next = expand_smacro(tline->next);
2202 tline = tline->next;
2203 skip_white_(tline);
2204 if (tok_type_(tline, TOK_STRING)) {
2205 p = tline->text;
2206 nasm_unquote(p, NULL);
2207 expand_macros_in_string(&p); /* WHY? */
2208 error(ERR_NONFATAL, "%s", p);
2209 nasm_free(p);
2210 } else {
2211 p = detoken(tline, false);
2212 error(ERR_WARNING, "%s", p); /* WARNING!??!! */
2213 nasm_free(p);
2215 free_tlist(origline);
2216 break;
2218 CASE_PP_IF:
2219 if (istk->conds && !emitting(istk->conds->state))
2220 j = COND_NEVER;
2221 else {
2222 j = if_condition(tline->next, i);
2223 tline->next = NULL; /* it got freed */
2224 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2226 cond = nasm_malloc(sizeof(Cond));
2227 cond->next = istk->conds;
2228 cond->state = j;
2229 istk->conds = cond;
2230 free_tlist(origline);
2231 return DIRECTIVE_FOUND;
2233 CASE_PP_ELIF:
2234 if (!istk->conds)
2235 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2236 if (emitting(istk->conds->state)
2237 || istk->conds->state == COND_NEVER)
2238 istk->conds->state = COND_NEVER;
2239 else {
2241 * IMPORTANT: In the case of %if, we will already have
2242 * called expand_mmac_params(); however, if we're
2243 * processing an %elif we must have been in a
2244 * non-emitting mode, which would have inhibited
2245 * the normal invocation of expand_mmac_params(). Therefore,
2246 * we have to do it explicitly here.
2248 j = if_condition(expand_mmac_params(tline->next), i);
2249 tline->next = NULL; /* it got freed */
2250 istk->conds->state =
2251 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2253 free_tlist(origline);
2254 return DIRECTIVE_FOUND;
2256 case PP_ELSE:
2257 if (tline->next)
2258 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2259 if (!istk->conds)
2260 error(ERR_FATAL, "`%%else': no matching `%%if'");
2261 if (emitting(istk->conds->state)
2262 || istk->conds->state == COND_NEVER)
2263 istk->conds->state = COND_ELSE_FALSE;
2264 else
2265 istk->conds->state = COND_ELSE_TRUE;
2266 free_tlist(origline);
2267 return DIRECTIVE_FOUND;
2269 case PP_ENDIF:
2270 if (tline->next)
2271 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2272 if (!istk->conds)
2273 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2274 cond = istk->conds;
2275 istk->conds = cond->next;
2276 nasm_free(cond);
2277 free_tlist(origline);
2278 return DIRECTIVE_FOUND;
2280 case PP_MACRO:
2281 case PP_IMACRO:
2282 if (defining)
2283 error(ERR_FATAL,
2284 "`%%%smacro': already defining a macro",
2285 (i == PP_IMACRO ? "i" : ""));
2286 tline = tline->next;
2287 skip_white_(tline);
2288 tline = expand_id(tline);
2289 if (!tok_type_(tline, TOK_ID)) {
2290 error(ERR_NONFATAL,
2291 "`%%%smacro' expects a macro name",
2292 (i == PP_IMACRO ? "i" : ""));
2293 return DIRECTIVE_FOUND;
2295 defining = nasm_malloc(sizeof(MMacro));
2296 defining->name = nasm_strdup(tline->text);
2297 defining->casesense = (i == PP_MACRO);
2298 defining->plus = false;
2299 defining->nolist = false;
2300 defining->in_progress = 0;
2301 defining->rep_nest = NULL;
2302 tline = expand_smacro(tline->next);
2303 skip_white_(tline);
2304 if (!tok_type_(tline, TOK_NUMBER)) {
2305 error(ERR_NONFATAL,
2306 "`%%%smacro' expects a parameter count",
2307 (i == PP_IMACRO ? "i" : ""));
2308 defining->nparam_min = defining->nparam_max = 0;
2309 } else {
2310 defining->nparam_min = defining->nparam_max =
2311 readnum(tline->text, &err);
2312 if (err)
2313 error(ERR_NONFATAL,
2314 "unable to parse parameter count `%s'", tline->text);
2316 if (tline && tok_is_(tline->next, "-")) {
2317 tline = tline->next->next;
2318 if (tok_is_(tline, "*"))
2319 defining->nparam_max = INT_MAX;
2320 else if (!tok_type_(tline, TOK_NUMBER))
2321 error(ERR_NONFATAL,
2322 "`%%%smacro' expects a parameter count after `-'",
2323 (i == PP_IMACRO ? "i" : ""));
2324 else {
2325 defining->nparam_max = readnum(tline->text, &err);
2326 if (err)
2327 error(ERR_NONFATAL,
2328 "unable to parse parameter count `%s'",
2329 tline->text);
2330 if (defining->nparam_min > defining->nparam_max)
2331 error(ERR_NONFATAL,
2332 "minimum parameter count exceeds maximum");
2335 if (tline && tok_is_(tline->next, "+")) {
2336 tline = tline->next;
2337 defining->plus = true;
2339 if (tline && tok_type_(tline->next, TOK_ID) &&
2340 !nasm_stricmp(tline->next->text, ".nolist")) {
2341 tline = tline->next;
2342 defining->nolist = true;
2344 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2345 while (mmac) {
2346 if (!strcmp(mmac->name, defining->name) &&
2347 (mmac->nparam_min <= defining->nparam_max
2348 || defining->plus)
2349 && (defining->nparam_min <= mmac->nparam_max
2350 || mmac->plus)) {
2351 error(ERR_WARNING,
2352 "redefining multi-line macro `%s'", defining->name);
2353 break;
2355 mmac = mmac->next;
2358 * Handle default parameters.
2360 if (tline && tline->next) {
2361 defining->dlist = tline->next;
2362 tline->next = NULL;
2363 count_mmac_params(defining->dlist, &defining->ndefs,
2364 &defining->defaults);
2365 } else {
2366 defining->dlist = NULL;
2367 defining->defaults = NULL;
2369 defining->expansion = NULL;
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2373 case PP_ENDM:
2374 case PP_ENDMACRO:
2375 if (!defining) {
2376 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2377 return DIRECTIVE_FOUND;
2379 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2380 defining->next = *mmhead;
2381 *mmhead = defining;
2382 defining = NULL;
2383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
2386 case PP_ROTATE:
2387 if (tline->next && tline->next->type == TOK_WHITESPACE)
2388 tline = tline->next;
2389 if (tline->next == NULL) {
2390 free_tlist(origline);
2391 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2392 return DIRECTIVE_FOUND;
2394 t = expand_smacro(tline->next);
2395 tline->next = NULL;
2396 free_tlist(origline);
2397 tline = t;
2398 tptr = &t;
2399 tokval.t_type = TOKEN_INVALID;
2400 evalresult =
2401 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2402 free_tlist(tline);
2403 if (!evalresult)
2404 return DIRECTIVE_FOUND;
2405 if (tokval.t_type)
2406 error(ERR_WARNING,
2407 "trailing garbage after expression ignored");
2408 if (!is_simple(evalresult)) {
2409 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2410 return DIRECTIVE_FOUND;
2412 mmac = istk->mstk;
2413 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2414 mmac = mmac->next_active;
2415 if (!mmac) {
2416 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2417 } else if (mmac->nparam == 0) {
2418 error(ERR_NONFATAL,
2419 "`%%rotate' invoked within macro without parameters");
2420 } else {
2421 int rotate = mmac->rotate + reloc_value(evalresult);
2423 rotate %= (int)mmac->nparam;
2424 if (rotate < 0)
2425 rotate += mmac->nparam;
2427 mmac->rotate = rotate;
2429 return DIRECTIVE_FOUND;
2431 case PP_REP:
2432 nolist = false;
2433 do {
2434 tline = tline->next;
2435 } while (tok_type_(tline, TOK_WHITESPACE));
2437 if (tok_type_(tline, TOK_ID) &&
2438 nasm_stricmp(tline->text, ".nolist") == 0) {
2439 nolist = true;
2440 do {
2441 tline = tline->next;
2442 } while (tok_type_(tline, TOK_WHITESPACE));
2445 if (tline) {
2446 t = expand_smacro(tline);
2447 tptr = &t;
2448 tokval.t_type = TOKEN_INVALID;
2449 evalresult =
2450 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2451 if (!evalresult) {
2452 free_tlist(origline);
2453 return DIRECTIVE_FOUND;
2455 if (tokval.t_type)
2456 error(ERR_WARNING,
2457 "trailing garbage after expression ignored");
2458 if (!is_simple(evalresult)) {
2459 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2460 return DIRECTIVE_FOUND;
2462 count = reloc_value(evalresult) + 1;
2463 } else {
2464 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2465 count = 0;
2467 free_tlist(origline);
2469 tmp_defining = defining;
2470 defining = nasm_malloc(sizeof(MMacro));
2471 defining->name = NULL; /* flags this macro as a %rep block */
2472 defining->casesense = false;
2473 defining->plus = false;
2474 defining->nolist = nolist;
2475 defining->in_progress = count;
2476 defining->nparam_min = defining->nparam_max = 0;
2477 defining->defaults = NULL;
2478 defining->dlist = NULL;
2479 defining->expansion = NULL;
2480 defining->next_active = istk->mstk;
2481 defining->rep_nest = tmp_defining;
2482 return DIRECTIVE_FOUND;
2484 case PP_ENDREP:
2485 if (!defining || defining->name) {
2486 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2487 return DIRECTIVE_FOUND;
2491 * Now we have a "macro" defined - although it has no name
2492 * and we won't be entering it in the hash tables - we must
2493 * push a macro-end marker for it on to istk->expansion.
2494 * After that, it will take care of propagating itself (a
2495 * macro-end marker line for a macro which is really a %rep
2496 * block will cause the macro to be re-expanded, complete
2497 * with another macro-end marker to ensure the process
2498 * continues) until the whole expansion is forcibly removed
2499 * from istk->expansion by a %exitrep.
2501 l = nasm_malloc(sizeof(Line));
2502 l->next = istk->expansion;
2503 l->finishes = defining;
2504 l->first = NULL;
2505 istk->expansion = l;
2507 istk->mstk = defining;
2509 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2510 tmp_defining = defining;
2511 defining = defining->rep_nest;
2512 free_tlist(origline);
2513 return DIRECTIVE_FOUND;
2515 case PP_EXITREP:
2517 * We must search along istk->expansion until we hit a
2518 * macro-end marker for a macro with no name. Then we set
2519 * its `in_progress' flag to 0.
2521 for (l = istk->expansion; l; l = l->next)
2522 if (l->finishes && !l->finishes->name)
2523 break;
2525 if (l)
2526 l->finishes->in_progress = 0;
2527 else
2528 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2529 free_tlist(origline);
2530 return DIRECTIVE_FOUND;
2532 case PP_XDEFINE:
2533 case PP_IXDEFINE:
2534 case PP_DEFINE:
2535 case PP_IDEFINE:
2536 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2538 tline = tline->next;
2539 skip_white_(tline);
2540 tline = expand_id(tline);
2541 if (!tline || (tline->type != TOK_ID &&
2542 (tline->type != TOK_PREPROC_ID ||
2543 tline->text[1] != '$'))) {
2544 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2545 pp_directives[i]);
2546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
2550 ctx = get_ctx(tline->text, false);
2552 mname = tline->text;
2553 last = tline;
2554 param_start = tline = tline->next;
2555 nparam = 0;
2557 /* Expand the macro definition now for %xdefine and %ixdefine */
2558 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2559 tline = expand_smacro(tline);
2561 if (tok_is_(tline, "(")) {
2563 * This macro has parameters.
2566 tline = tline->next;
2567 while (1) {
2568 skip_white_(tline);
2569 if (!tline) {
2570 error(ERR_NONFATAL, "parameter identifier expected");
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2574 if (tline->type != TOK_ID) {
2575 error(ERR_NONFATAL,
2576 "`%s': parameter identifier expected",
2577 tline->text);
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND;
2581 tline->type = TOK_SMAC_PARAM + nparam++;
2582 tline = tline->next;
2583 skip_white_(tline);
2584 if (tok_is_(tline, ",")) {
2585 tline = tline->next;
2586 continue;
2588 if (!tok_is_(tline, ")")) {
2589 error(ERR_NONFATAL,
2590 "`)' expected to terminate macro template");
2591 free_tlist(origline);
2592 return DIRECTIVE_FOUND;
2594 break;
2596 last = tline;
2597 tline = tline->next;
2599 if (tok_type_(tline, TOK_WHITESPACE))
2600 last = tline, tline = tline->next;
2601 macro_start = NULL;
2602 last->next = NULL;
2603 t = tline;
2604 while (t) {
2605 if (t->type == TOK_ID) {
2606 for (tt = param_start; tt; tt = tt->next)
2607 if (tt->type >= TOK_SMAC_PARAM &&
2608 !strcmp(tt->text, t->text))
2609 t->type = tt->type;
2611 tt = t->next;
2612 t->next = macro_start;
2613 macro_start = t;
2614 t = tt;
2617 * Good. We now have a macro name, a parameter count, and a
2618 * token list (in reverse order) for an expansion. We ought
2619 * to be OK just to create an SMacro, store it, and let
2620 * free_tlist have the rest of the line (which we have
2621 * carefully re-terminated after chopping off the expansion
2622 * from the end).
2624 define_smacro(ctx, mname, casesense, nparam, macro_start);
2625 free_tlist(origline);
2626 return DIRECTIVE_FOUND;
2628 case PP_UNDEF:
2629 tline = tline->next;
2630 skip_white_(tline);
2631 tline = expand_id(tline);
2632 if (!tline || (tline->type != TOK_ID &&
2633 (tline->type != TOK_PREPROC_ID ||
2634 tline->text[1] != '$'))) {
2635 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2636 free_tlist(origline);
2637 return DIRECTIVE_FOUND;
2639 if (tline->next) {
2640 error(ERR_WARNING,
2641 "trailing garbage after macro name ignored");
2644 /* Find the context that symbol belongs to */
2645 ctx = get_ctx(tline->text, false);
2646 undef_smacro(ctx, tline->text);
2647 free_tlist(origline);
2648 return DIRECTIVE_FOUND;
2650 case PP_DEFSTR:
2651 case PP_IDEFSTR:
2652 casesense = (i == PP_DEFSTR);
2654 tline = tline->next;
2655 skip_white_(tline);
2656 tline = expand_id(tline);
2657 if (!tline || (tline->type != TOK_ID &&
2658 (tline->type != TOK_PREPROC_ID ||
2659 tline->text[1] != '$'))) {
2660 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2661 pp_directives[i]);
2662 free_tlist(origline);
2663 return DIRECTIVE_FOUND;
2666 ctx = get_ctx(tline->text, false);
2668 mname = tline->text;
2669 last = tline;
2670 tline = expand_smacro(tline->next);
2671 last->next = NULL;
2673 while (tok_type_(tline, TOK_WHITESPACE))
2674 tline = delete_Token(tline);
2676 p = detoken(tline, false);
2677 macro_start = nasm_malloc(sizeof(*macro_start));
2678 macro_start->next = NULL;
2679 macro_start->text = nasm_quote(p, strlen(p));
2680 macro_start->type = TOK_STRING;
2681 macro_start->mac = NULL;
2682 nasm_free(p);
2685 * We now have a macro name, an implicit parameter count of
2686 * zero, and a string token to use as an expansion. Create
2687 * and store an SMacro.
2689 define_smacro(ctx, mname, casesense, 0, macro_start);
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND;
2693 case PP_PATHSEARCH:
2695 FILE *fp;
2696 StrList *xsl = NULL;
2697 StrList **xst = &xsl;
2699 casesense = true;
2701 tline = tline->next;
2702 skip_white_(tline);
2703 tline = expand_id(tline);
2704 if (!tline || (tline->type != TOK_ID &&
2705 (tline->type != TOK_PREPROC_ID ||
2706 tline->text[1] != '$'))) {
2707 error(ERR_NONFATAL,
2708 "`%%pathsearch' expects a macro identifier as first parameter");
2709 free_tlist(origline);
2710 return DIRECTIVE_FOUND;
2712 ctx = get_ctx(tline->text, false);
2714 mname = tline->text;
2715 last = tline;
2716 tline = expand_smacro(tline->next);
2717 last->next = NULL;
2719 t = tline;
2720 while (tok_type_(t, TOK_WHITESPACE))
2721 t = t->next;
2723 if (!t || (t->type != TOK_STRING &&
2724 t->type != TOK_INTERNAL_STRING)) {
2725 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2726 free_tlist(tline);
2727 free_tlist(origline);
2728 return DIRECTIVE_FOUND; /* but we did _something_ */
2730 if (t->next)
2731 error(ERR_WARNING,
2732 "trailing garbage after `%%pathsearch' ignored");
2733 p = t->text;
2734 if (t->type != TOK_INTERNAL_STRING)
2735 nasm_unquote(p, NULL);
2737 fp = inc_fopen(p, &xsl, &xst, true);
2738 if (fp) {
2739 p = xsl->str;
2740 fclose(fp); /* Don't actually care about the file */
2742 macro_start = nasm_malloc(sizeof(*macro_start));
2743 macro_start->next = NULL;
2744 macro_start->text = nasm_quote(p, strlen(p));
2745 macro_start->type = TOK_STRING;
2746 macro_start->mac = NULL;
2747 if (xsl)
2748 nasm_free(xsl);
2751 * We now have a macro name, an implicit parameter count of
2752 * zero, and a string token to use as an expansion. Create
2753 * and store an SMacro.
2755 define_smacro(ctx, mname, casesense, 0, macro_start);
2756 free_tlist(tline);
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
2761 case PP_STRLEN:
2762 casesense = true;
2764 tline = tline->next;
2765 skip_white_(tline);
2766 tline = expand_id(tline);
2767 if (!tline || (tline->type != TOK_ID &&
2768 (tline->type != TOK_PREPROC_ID ||
2769 tline->text[1] != '$'))) {
2770 error(ERR_NONFATAL,
2771 "`%%strlen' expects a macro identifier as first parameter");
2772 free_tlist(origline);
2773 return DIRECTIVE_FOUND;
2775 ctx = get_ctx(tline->text, false);
2777 mname = tline->text;
2778 last = tline;
2779 tline = expand_smacro(tline->next);
2780 last->next = NULL;
2782 t = tline;
2783 while (tok_type_(t, TOK_WHITESPACE))
2784 t = t->next;
2785 /* t should now point to the string */
2786 if (t->type != TOK_STRING) {
2787 error(ERR_NONFATAL,
2788 "`%%strlen` requires string as second parameter");
2789 free_tlist(tline);
2790 free_tlist(origline);
2791 return DIRECTIVE_FOUND;
2794 macro_start = nasm_malloc(sizeof(*macro_start));
2795 macro_start->next = NULL;
2796 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
2797 macro_start->mac = NULL;
2800 * We now have a macro name, an implicit parameter count of
2801 * zero, and a numeric token to use as an expansion. Create
2802 * and store an SMacro.
2804 define_smacro(ctx, mname, casesense, 0, macro_start);
2805 free_tlist(tline);
2806 free_tlist(origline);
2807 return DIRECTIVE_FOUND;
2809 case PP_SUBSTR:
2811 int64_t a1, a2;
2812 size_t len;
2814 casesense = true;
2816 tline = tline->next;
2817 skip_white_(tline);
2818 tline = expand_id(tline);
2819 if (!tline || (tline->type != TOK_ID &&
2820 (tline->type != TOK_PREPROC_ID ||
2821 tline->text[1] != '$'))) {
2822 error(ERR_NONFATAL,
2823 "`%%substr' expects a macro identifier as first parameter");
2824 free_tlist(origline);
2825 return DIRECTIVE_FOUND;
2827 ctx = get_ctx(tline->text, false);
2829 mname = tline->text;
2830 last = tline;
2831 tline = expand_smacro(tline->next);
2832 last->next = NULL;
2834 t = tline->next;
2835 while (tok_type_(t, TOK_WHITESPACE))
2836 t = t->next;
2838 /* t should now point to the string */
2839 if (t->type != TOK_STRING) {
2840 error(ERR_NONFATAL,
2841 "`%%substr` requires string as second parameter");
2842 free_tlist(tline);
2843 free_tlist(origline);
2844 return DIRECTIVE_FOUND;
2847 tt = t->next;
2848 tptr = &tt;
2849 tokval.t_type = TOKEN_INVALID;
2850 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2851 pass, error, NULL);
2852 if (!evalresult) {
2853 free_tlist(tline);
2854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
2856 } else if (!is_simple(evalresult)) {
2857 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2858 free_tlist(tline);
2859 free_tlist(origline);
2860 return DIRECTIVE_FOUND;
2862 a1 = evalresult->value-1;
2864 while (tok_type_(tt, TOK_WHITESPACE))
2865 tt = tt->next;
2866 if (!tt) {
2867 a2 = 1; /* Backwards compatibility: one character */
2868 } else {
2869 tokval.t_type = TOKEN_INVALID;
2870 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2871 pass, error, NULL);
2872 if (!evalresult) {
2873 free_tlist(tline);
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
2876 } else if (!is_simple(evalresult)) {
2877 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2878 free_tlist(tline);
2879 free_tlist(origline);
2880 return DIRECTIVE_FOUND;
2882 a2 = evalresult->value;
2885 len = nasm_unquote(t->text, NULL);
2886 if (a2 < 0)
2887 a2 = a2+1+len-a1;
2888 if (a1+a2 > (int64_t)len)
2889 a2 = len-a1;
2891 macro_start = nasm_malloc(sizeof(*macro_start));
2892 macro_start->next = NULL;
2893 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
2894 macro_start->type = TOK_STRING;
2895 macro_start->mac = NULL;
2898 * We now have a macro name, an implicit parameter count of
2899 * zero, and a numeric token to use as an expansion. Create
2900 * and store an SMacro.
2902 define_smacro(ctx, mname, casesense, 0, macro_start);
2903 free_tlist(tline);
2904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
2908 case PP_ASSIGN:
2909 case PP_IASSIGN:
2910 casesense = (i == PP_ASSIGN);
2912 tline = tline->next;
2913 skip_white_(tline);
2914 tline = expand_id(tline);
2915 if (!tline || (tline->type != TOK_ID &&
2916 (tline->type != TOK_PREPROC_ID ||
2917 tline->text[1] != '$'))) {
2918 error(ERR_NONFATAL,
2919 "`%%%sassign' expects a macro identifier",
2920 (i == PP_IASSIGN ? "i" : ""));
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
2924 ctx = get_ctx(tline->text, false);
2926 mname = tline->text;
2927 last = tline;
2928 tline = expand_smacro(tline->next);
2929 last->next = NULL;
2931 t = tline;
2932 tptr = &t;
2933 tokval.t_type = TOKEN_INVALID;
2934 evalresult =
2935 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2936 free_tlist(tline);
2937 if (!evalresult) {
2938 free_tlist(origline);
2939 return DIRECTIVE_FOUND;
2942 if (tokval.t_type)
2943 error(ERR_WARNING,
2944 "trailing garbage after expression ignored");
2946 if (!is_simple(evalresult)) {
2947 error(ERR_NONFATAL,
2948 "non-constant value given to `%%%sassign'",
2949 (i == PP_IASSIGN ? "i" : ""));
2950 free_tlist(origline);
2951 return DIRECTIVE_FOUND;
2954 macro_start = nasm_malloc(sizeof(*macro_start));
2955 macro_start->next = NULL;
2956 make_tok_num(macro_start, reloc_value(evalresult));
2957 macro_start->mac = NULL;
2960 * We now have a macro name, an implicit parameter count of
2961 * zero, and a numeric token to use as an expansion. Create
2962 * and store an SMacro.
2964 define_smacro(ctx, mname, casesense, 0, macro_start);
2965 free_tlist(origline);
2966 return DIRECTIVE_FOUND;
2968 case PP_LINE:
2970 * Syntax is `%line nnn[+mmm] [filename]'
2972 tline = tline->next;
2973 skip_white_(tline);
2974 if (!tok_type_(tline, TOK_NUMBER)) {
2975 error(ERR_NONFATAL, "`%%line' expects line number");
2976 free_tlist(origline);
2977 return DIRECTIVE_FOUND;
2979 k = readnum(tline->text, &err);
2980 m = 1;
2981 tline = tline->next;
2982 if (tok_is_(tline, "+")) {
2983 tline = tline->next;
2984 if (!tok_type_(tline, TOK_NUMBER)) {
2985 error(ERR_NONFATAL, "`%%line' expects line increment");
2986 free_tlist(origline);
2987 return DIRECTIVE_FOUND;
2989 m = readnum(tline->text, &err);
2990 tline = tline->next;
2992 skip_white_(tline);
2993 src_set_linnum(k);
2994 istk->lineinc = m;
2995 if (tline) {
2996 nasm_free(src_set_fname(detoken(tline, false)));
2998 free_tlist(origline);
2999 return DIRECTIVE_FOUND;
3001 default:
3002 error(ERR_FATAL,
3003 "preprocessor directive `%s' not yet implemented",
3004 pp_directives[i]);
3005 break;
3007 return DIRECTIVE_FOUND;
3011 * Ensure that a macro parameter contains a condition code and
3012 * nothing else. Return the condition code index if so, or -1
3013 * otherwise.
3015 static int find_cc(Token * t)
3017 Token *tt;
3018 int i, j, k, m;
3020 if (!t)
3021 return -1; /* Probably a %+ without a space */
3023 skip_white_(t);
3024 if (t->type != TOK_ID)
3025 return -1;
3026 tt = t->next;
3027 skip_white_(tt);
3028 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3029 return -1;
3031 i = -1;
3032 j = elements(conditions);
3033 while (j - i > 1) {
3034 k = (j + i) / 2;
3035 m = nasm_stricmp(t->text, conditions[k]);
3036 if (m == 0) {
3037 i = k;
3038 j = -2;
3039 break;
3040 } else if (m < 0) {
3041 j = k;
3042 } else
3043 i = k;
3045 if (j != -2)
3046 return -1;
3047 return i;
3051 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3052 * %-n) and MMacro-local identifiers (%%foo).
3054 static Token *expand_mmac_params(Token * tline)
3056 Token *t, *tt, **tail, *thead;
3058 tail = &thead;
3059 thead = NULL;
3061 while (tline) {
3062 if (tline->type == TOK_PREPROC_ID &&
3063 (((tline->text[1] == '+' || tline->text[1] == '-')
3064 && tline->text[2]) || tline->text[1] == '%'
3065 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3066 char *text = NULL;
3067 int type = 0, cc; /* type = 0 to placate optimisers */
3068 char tmpbuf[30];
3069 unsigned int n;
3070 int i;
3071 MMacro *mac;
3073 t = tline;
3074 tline = tline->next;
3076 mac = istk->mstk;
3077 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3078 mac = mac->next_active;
3079 if (!mac)
3080 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3081 else
3082 switch (t->text[1]) {
3084 * We have to make a substitution of one of the
3085 * forms %1, %-1, %+1, %%foo, %0.
3087 case '0':
3088 type = TOK_NUMBER;
3089 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3090 text = nasm_strdup(tmpbuf);
3091 break;
3092 case '%':
3093 type = TOK_ID;
3094 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3095 mac->unique);
3096 text = nasm_strcat(tmpbuf, t->text + 2);
3097 break;
3098 case '-':
3099 n = atoi(t->text + 2) - 1;
3100 if (n >= mac->nparam)
3101 tt = NULL;
3102 else {
3103 if (mac->nparam > 1)
3104 n = (n + mac->rotate) % mac->nparam;
3105 tt = mac->params[n];
3107 cc = find_cc(tt);
3108 if (cc == -1) {
3109 error(ERR_NONFATAL,
3110 "macro parameter %d is not a condition code",
3111 n + 1);
3112 text = NULL;
3113 } else {
3114 type = TOK_ID;
3115 if (inverse_ccs[cc] == -1) {
3116 error(ERR_NONFATAL,
3117 "condition code `%s' is not invertible",
3118 conditions[cc]);
3119 text = NULL;
3120 } else
3121 text =
3122 nasm_strdup(conditions[inverse_ccs[cc]]);
3124 break;
3125 case '+':
3126 n = atoi(t->text + 2) - 1;
3127 if (n >= mac->nparam)
3128 tt = NULL;
3129 else {
3130 if (mac->nparam > 1)
3131 n = (n + mac->rotate) % mac->nparam;
3132 tt = mac->params[n];
3134 cc = find_cc(tt);
3135 if (cc == -1) {
3136 error(ERR_NONFATAL,
3137 "macro parameter %d is not a condition code",
3138 n + 1);
3139 text = NULL;
3140 } else {
3141 type = TOK_ID;
3142 text = nasm_strdup(conditions[cc]);
3144 break;
3145 default:
3146 n = atoi(t->text + 1) - 1;
3147 if (n >= mac->nparam)
3148 tt = NULL;
3149 else {
3150 if (mac->nparam > 1)
3151 n = (n + mac->rotate) % mac->nparam;
3152 tt = mac->params[n];
3154 if (tt) {
3155 for (i = 0; i < mac->paramlen[n]; i++) {
3156 *tail = new_Token(NULL, tt->type, tt->text, 0);
3157 tail = &(*tail)->next;
3158 tt = tt->next;
3161 text = NULL; /* we've done it here */
3162 break;
3164 if (!text) {
3165 delete_Token(t);
3166 } else {
3167 *tail = t;
3168 tail = &t->next;
3169 t->type = type;
3170 nasm_free(t->text);
3171 t->text = text;
3172 t->mac = NULL;
3174 continue;
3175 } else {
3176 t = *tail = tline;
3177 tline = tline->next;
3178 t->mac = NULL;
3179 tail = &t->next;
3182 *tail = NULL;
3183 t = thead;
3184 for (; t && (tt = t->next) != NULL; t = t->next)
3185 switch (t->type) {
3186 case TOK_WHITESPACE:
3187 if (tt->type == TOK_WHITESPACE) {
3188 t->next = delete_Token(tt);
3190 break;
3191 case TOK_ID:
3192 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
3193 char *tmp = nasm_strcat(t->text, tt->text);
3194 nasm_free(t->text);
3195 t->text = tmp;
3196 t->next = delete_Token(tt);
3198 break;
3199 case TOK_NUMBER:
3200 if (tt->type == TOK_NUMBER) {
3201 char *tmp = nasm_strcat(t->text, tt->text);
3202 nasm_free(t->text);
3203 t->text = tmp;
3204 t->next = delete_Token(tt);
3206 break;
3207 default:
3208 break;
3211 return thead;
3215 * Expand all single-line macro calls made in the given line.
3216 * Return the expanded version of the line. The original is deemed
3217 * to be destroyed in the process. (In reality we'll just move
3218 * Tokens from input to output a lot of the time, rather than
3219 * actually bothering to destroy and replicate.)
3221 #define DEADMAN_LIMIT (1 << 20)
3223 static Token *expand_smacro(Token * tline)
3225 Token *t, *tt, *mstart, **tail, *thead;
3226 struct hash_table *smtbl;
3227 SMacro *head = NULL, *m;
3228 Token **params;
3229 int *paramsize;
3230 unsigned int nparam, sparam;
3231 int brackets, rescan;
3232 Token *org_tline = tline;
3233 Context *ctx;
3234 char *mname;
3235 int deadman = DEADMAN_LIMIT;
3238 * Trick: we should avoid changing the start token pointer since it can
3239 * be contained in "next" field of other token. Because of this
3240 * we allocate a copy of first token and work with it; at the end of
3241 * routine we copy it back
3243 if (org_tline) {
3244 tline =
3245 new_Token(org_tline->next, org_tline->type, org_tline->text,
3247 tline->mac = org_tline->mac;
3248 nasm_free(org_tline->text);
3249 org_tline->text = NULL;
3252 again:
3253 tail = &thead;
3254 thead = NULL;
3256 while (tline) { /* main token loop */
3257 if (!--deadman) {
3258 error(ERR_NONFATAL, "interminable macro recursion");
3259 break;
3262 if ((mname = tline->text)) {
3263 /* if this token is a local macro, look in local context */
3264 ctx = NULL;
3265 smtbl = &smacros;
3266 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
3267 ctx = get_ctx(mname, true);
3268 if (ctx)
3269 smtbl = &ctx->localmac;
3271 head = (SMacro *) hash_findix(smtbl, mname);
3274 * We've hit an identifier. As in is_mmacro below, we first
3275 * check whether the identifier is a single-line macro at
3276 * all, then think about checking for parameters if
3277 * necessary.
3279 for (m = head; m; m = m->next)
3280 if (!mstrcmp(m->name, mname, m->casesense))
3281 break;
3282 if (m) {
3283 mstart = tline;
3284 params = NULL;
3285 paramsize = NULL;
3286 if (m->nparam == 0) {
3288 * Simple case: the macro is parameterless. Discard the
3289 * one token that the macro call took, and push the
3290 * expansion back on the to-do stack.
3292 if (!m->expansion) {
3293 if (!strcmp("__FILE__", m->name)) {
3294 int32_t num = 0;
3295 char *file;
3296 src_get(&num, &file);
3297 tline->text = nasm_quote(file, strlen(file));
3298 tline->type = TOK_STRING;
3299 nasm_free(file);
3300 continue;
3302 if (!strcmp("__LINE__", m->name)) {
3303 nasm_free(tline->text);
3304 make_tok_num(tline, src_get_linnum());
3305 continue;
3307 if (!strcmp("__BITS__", m->name)) {
3308 nasm_free(tline->text);
3309 make_tok_num(tline, globalbits);
3310 continue;
3312 tline = delete_Token(tline);
3313 continue;
3315 } else {
3317 * Complicated case: at least one macro with this name
3318 * exists and takes parameters. We must find the
3319 * parameters in the call, count them, find the SMacro
3320 * that corresponds to that form of the macro call, and
3321 * substitute for the parameters when we expand. What a
3322 * pain.
3324 /*tline = tline->next;
3325 skip_white_(tline); */
3326 do {
3327 t = tline->next;
3328 while (tok_type_(t, TOK_SMAC_END)) {
3329 t->mac->in_progress = false;
3330 t->text = NULL;
3331 t = tline->next = delete_Token(t);
3333 tline = t;
3334 } while (tok_type_(tline, TOK_WHITESPACE));
3335 if (!tok_is_(tline, "(")) {
3337 * This macro wasn't called with parameters: ignore
3338 * the call. (Behaviour borrowed from gnu cpp.)
3340 tline = mstart;
3341 m = NULL;
3342 } else {
3343 int paren = 0;
3344 int white = 0;
3345 brackets = 0;
3346 nparam = 0;
3347 sparam = PARAM_DELTA;
3348 params = nasm_malloc(sparam * sizeof(Token *));
3349 params[0] = tline->next;
3350 paramsize = nasm_malloc(sparam * sizeof(int));
3351 paramsize[0] = 0;
3352 while (true) { /* parameter loop */
3354 * For some unusual expansions
3355 * which concatenates function call
3357 t = tline->next;
3358 while (tok_type_(t, TOK_SMAC_END)) {
3359 t->mac->in_progress = false;
3360 t->text = NULL;
3361 t = tline->next = delete_Token(t);
3363 tline = t;
3365 if (!tline) {
3366 error(ERR_NONFATAL,
3367 "macro call expects terminating `)'");
3368 break;
3370 if (tline->type == TOK_WHITESPACE
3371 && brackets <= 0) {
3372 if (paramsize[nparam])
3373 white++;
3374 else
3375 params[nparam] = tline->next;
3376 continue; /* parameter loop */
3378 if (tline->type == TOK_OTHER
3379 && tline->text[1] == 0) {
3380 char ch = tline->text[0];
3381 if (ch == ',' && !paren && brackets <= 0) {
3382 if (++nparam >= sparam) {
3383 sparam += PARAM_DELTA;
3384 params = nasm_realloc(params,
3385 sparam *
3386 sizeof(Token
3387 *));
3388 paramsize =
3389 nasm_realloc(paramsize,
3390 sparam *
3391 sizeof(int));
3393 params[nparam] = tline->next;
3394 paramsize[nparam] = 0;
3395 white = 0;
3396 continue; /* parameter loop */
3398 if (ch == '{' &&
3399 (brackets > 0 || (brackets == 0 &&
3400 !paramsize[nparam])))
3402 if (!(brackets++)) {
3403 params[nparam] = tline->next;
3404 continue; /* parameter loop */
3407 if (ch == '}' && brackets > 0)
3408 if (--brackets == 0) {
3409 brackets = -1;
3410 continue; /* parameter loop */
3412 if (ch == '(' && !brackets)
3413 paren++;
3414 if (ch == ')' && brackets <= 0)
3415 if (--paren < 0)
3416 break;
3418 if (brackets < 0) {
3419 brackets = 0;
3420 error(ERR_NONFATAL, "braces do not "
3421 "enclose all of macro parameter");
3423 paramsize[nparam] += white + 1;
3424 white = 0;
3425 } /* parameter loop */
3426 nparam++;
3427 while (m && (m->nparam != nparam ||
3428 mstrcmp(m->name, mname,
3429 m->casesense)))
3430 m = m->next;
3431 if (!m)
3432 error(ERR_WARNING | ERR_WARN_MNP,
3433 "macro `%s' exists, "
3434 "but not taking %d parameters",
3435 mstart->text, nparam);
3438 if (m && m->in_progress)
3439 m = NULL;
3440 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3442 * Design question: should we handle !tline, which
3443 * indicates missing ')' here, or expand those
3444 * macros anyway, which requires the (t) test a few
3445 * lines down?
3447 nasm_free(params);
3448 nasm_free(paramsize);
3449 tline = mstart;
3450 } else {
3452 * Expand the macro: we are placed on the last token of the
3453 * call, so that we can easily split the call from the
3454 * following tokens. We also start by pushing an SMAC_END
3455 * token for the cycle removal.
3457 t = tline;
3458 if (t) {
3459 tline = t->next;
3460 t->next = NULL;
3462 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3463 tt->mac = m;
3464 m->in_progress = true;
3465 tline = tt;
3466 for (t = m->expansion; t; t = t->next) {
3467 if (t->type >= TOK_SMAC_PARAM) {
3468 Token *pcopy = tline, **ptail = &pcopy;
3469 Token *ttt, *pt;
3470 int i;
3472 ttt = params[t->type - TOK_SMAC_PARAM];
3473 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3474 --i >= 0;) {
3475 pt = *ptail =
3476 new_Token(tline, ttt->type, ttt->text,
3478 ptail = &pt->next;
3479 ttt = ttt->next;
3481 tline = pcopy;
3482 } else if (t->type == TOK_PREPROC_Q) {
3483 tt = new_Token(tline, TOK_ID, mname, 0);
3484 tline = tt;
3485 } else if (t->type == TOK_PREPROC_QQ) {
3486 tt = new_Token(tline, TOK_ID, m->name, 0);
3487 tline = tt;
3488 } else {
3489 tt = new_Token(tline, t->type, t->text, 0);
3490 tline = tt;
3495 * Having done that, get rid of the macro call, and clean
3496 * up the parameters.
3498 nasm_free(params);
3499 nasm_free(paramsize);
3500 free_tlist(mstart);
3501 continue; /* main token loop */
3506 if (tline->type == TOK_SMAC_END) {
3507 tline->mac->in_progress = false;
3508 tline = delete_Token(tline);
3509 } else {
3510 t = *tail = tline;
3511 tline = tline->next;
3512 t->mac = NULL;
3513 t->next = NULL;
3514 tail = &t->next;
3519 * Now scan the entire line and look for successive TOK_IDs that resulted
3520 * after expansion (they can't be produced by tokenize()). The successive
3521 * TOK_IDs should be concatenated.
3522 * Also we look for %+ tokens and concatenate the tokens before and after
3523 * them (without white spaces in between).
3525 t = thead;
3526 rescan = 0;
3527 while (t) {
3528 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3529 t = t->next;
3530 if (!t || !t->next)
3531 break;
3532 if (t->next->type == TOK_ID ||
3533 t->next->type == TOK_PREPROC_ID ||
3534 t->next->type == TOK_NUMBER) {
3535 char *p = nasm_strcat(t->text, t->next->text);
3536 nasm_free(t->text);
3537 t->next = delete_Token(t->next);
3538 t->text = p;
3539 rescan = 1;
3540 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3541 t->next->next->type == TOK_PREPROC_ID &&
3542 strcmp(t->next->next->text, "%+") == 0) {
3543 /* free the next whitespace, the %+ token and next whitespace */
3544 int i;
3545 for (i = 1; i <= 3; i++) {
3546 if (!t->next
3547 || (i != 2 && t->next->type != TOK_WHITESPACE))
3548 break;
3549 t->next = delete_Token(t->next);
3550 } /* endfor */
3551 } else
3552 t = t->next;
3554 /* If we concatenaded something, re-scan the line for macros */
3555 if (rescan) {
3556 tline = thead;
3557 goto again;
3560 if (org_tline) {
3561 if (thead) {
3562 *org_tline = *thead;
3563 /* since we just gave text to org_line, don't free it */
3564 thead->text = NULL;
3565 delete_Token(thead);
3566 } else {
3567 /* the expression expanded to empty line;
3568 we can't return NULL for some reasons
3569 we just set the line to a single WHITESPACE token. */
3570 memset(org_tline, 0, sizeof(*org_tline));
3571 org_tline->text = NULL;
3572 org_tline->type = TOK_WHITESPACE;
3574 thead = org_tline;
3577 return thead;
3581 * Similar to expand_smacro but used exclusively with macro identifiers
3582 * right before they are fetched in. The reason is that there can be
3583 * identifiers consisting of several subparts. We consider that if there
3584 * are more than one element forming the name, user wants a expansion,
3585 * otherwise it will be left as-is. Example:
3587 * %define %$abc cde
3589 * the identifier %$abc will be left as-is so that the handler for %define
3590 * will suck it and define the corresponding value. Other case:
3592 * %define _%$abc cde
3594 * In this case user wants name to be expanded *before* %define starts
3595 * working, so we'll expand %$abc into something (if it has a value;
3596 * otherwise it will be left as-is) then concatenate all successive
3597 * PP_IDs into one.
3599 static Token *expand_id(Token * tline)
3601 Token *cur, *oldnext = NULL;
3603 if (!tline || !tline->next)
3604 return tline;
3606 cur = tline;
3607 while (cur->next &&
3608 (cur->next->type == TOK_ID ||
3609 cur->next->type == TOK_PREPROC_ID
3610 || cur->next->type == TOK_NUMBER))
3611 cur = cur->next;
3613 /* If identifier consists of just one token, don't expand */
3614 if (cur == tline)
3615 return tline;
3617 if (cur) {
3618 oldnext = cur->next; /* Detach the tail past identifier */
3619 cur->next = NULL; /* so that expand_smacro stops here */
3622 tline = expand_smacro(tline);
3624 if (cur) {
3625 /* expand_smacro possibly changhed tline; re-scan for EOL */
3626 cur = tline;
3627 while (cur && cur->next)
3628 cur = cur->next;
3629 if (cur)
3630 cur->next = oldnext;
3633 return tline;
3637 * Determine whether the given line constitutes a multi-line macro
3638 * call, and return the MMacro structure called if so. Doesn't have
3639 * to check for an initial label - that's taken care of in
3640 * expand_mmacro - but must check numbers of parameters. Guaranteed
3641 * to be called with tline->type == TOK_ID, so the putative macro
3642 * name is easy to find.
3644 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3646 MMacro *head, *m;
3647 Token **params;
3648 int nparam;
3650 head = (MMacro *) hash_findix(&mmacros, tline->text);
3653 * Efficiency: first we see if any macro exists with the given
3654 * name. If not, we can return NULL immediately. _Then_ we
3655 * count the parameters, and then we look further along the
3656 * list if necessary to find the proper MMacro.
3658 for (m = head; m; m = m->next)
3659 if (!mstrcmp(m->name, tline->text, m->casesense))
3660 break;
3661 if (!m)
3662 return NULL;
3665 * OK, we have a potential macro. Count and demarcate the
3666 * parameters.
3668 count_mmac_params(tline->next, &nparam, &params);
3671 * So we know how many parameters we've got. Find the MMacro
3672 * structure that handles this number.
3674 while (m) {
3675 if (m->nparam_min <= nparam
3676 && (m->plus || nparam <= m->nparam_max)) {
3678 * This one is right. Just check if cycle removal
3679 * prohibits us using it before we actually celebrate...
3681 if (m->in_progress) {
3682 #if 0
3683 error(ERR_NONFATAL,
3684 "self-reference in multi-line macro `%s'", m->name);
3685 #endif
3686 nasm_free(params);
3687 return NULL;
3690 * It's right, and we can use it. Add its default
3691 * parameters to the end of our list if necessary.
3693 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3694 params =
3695 nasm_realloc(params,
3696 ((m->nparam_min + m->ndefs +
3697 1) * sizeof(*params)));
3698 while (nparam < m->nparam_min + m->ndefs) {
3699 params[nparam] = m->defaults[nparam - m->nparam_min];
3700 nparam++;
3704 * If we've gone over the maximum parameter count (and
3705 * we're in Plus mode), ignore parameters beyond
3706 * nparam_max.
3708 if (m->plus && nparam > m->nparam_max)
3709 nparam = m->nparam_max;
3711 * Then terminate the parameter list, and leave.
3713 if (!params) { /* need this special case */
3714 params = nasm_malloc(sizeof(*params));
3715 nparam = 0;
3717 params[nparam] = NULL;
3718 *params_array = params;
3719 return m;
3722 * This one wasn't right: look for the next one with the
3723 * same name.
3725 for (m = m->next; m; m = m->next)
3726 if (!mstrcmp(m->name, tline->text, m->casesense))
3727 break;
3731 * After all that, we didn't find one with the right number of
3732 * parameters. Issue a warning, and fail to expand the macro.
3734 error(ERR_WARNING | ERR_WARN_MNP,
3735 "macro `%s' exists, but not taking %d parameters",
3736 tline->text, nparam);
3737 nasm_free(params);
3738 return NULL;
3742 * Expand the multi-line macro call made by the given line, if
3743 * there is one to be expanded. If there is, push the expansion on
3744 * istk->expansion and return 1. Otherwise return 0.
3746 static int expand_mmacro(Token * tline)
3748 Token *startline = tline;
3749 Token *label = NULL;
3750 int dont_prepend = 0;
3751 Token **params, *t, *mtok, *tt;
3752 MMacro *m;
3753 Line *l, *ll;
3754 int i, nparam, *paramlen;
3756 t = tline;
3757 skip_white_(t);
3758 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3759 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3760 return 0;
3761 mtok = t;
3762 m = is_mmacro(t, &params);
3763 if (!m) {
3764 Token *last;
3766 * We have an id which isn't a macro call. We'll assume
3767 * it might be a label; we'll also check to see if a
3768 * colon follows it. Then, if there's another id after
3769 * that lot, we'll check it again for macro-hood.
3771 label = last = t;
3772 t = t->next;
3773 if (tok_type_(t, TOK_WHITESPACE))
3774 last = t, t = t->next;
3775 if (tok_is_(t, ":")) {
3776 dont_prepend = 1;
3777 last = t, t = t->next;
3778 if (tok_type_(t, TOK_WHITESPACE))
3779 last = t, t = t->next;
3781 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3782 return 0;
3783 last->next = NULL;
3784 tline = t;
3788 * Fix up the parameters: this involves stripping leading and
3789 * trailing whitespace, then stripping braces if they are
3790 * present.
3792 for (nparam = 0; params[nparam]; nparam++) ;
3793 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3795 for (i = 0; params[i]; i++) {
3796 int brace = false;
3797 int comma = (!m->plus || i < nparam - 1);
3799 t = params[i];
3800 skip_white_(t);
3801 if (tok_is_(t, "{"))
3802 t = t->next, brace = true, comma = false;
3803 params[i] = t;
3804 paramlen[i] = 0;
3805 while (t) {
3806 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3807 break; /* ... because we have hit a comma */
3808 if (comma && t->type == TOK_WHITESPACE
3809 && tok_is_(t->next, ","))
3810 break; /* ... or a space then a comma */
3811 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3812 break; /* ... or a brace */
3813 t = t->next;
3814 paramlen[i]++;
3819 * OK, we have a MMacro structure together with a set of
3820 * parameters. We must now go through the expansion and push
3821 * copies of each Line on to istk->expansion. Substitution of
3822 * parameter tokens and macro-local tokens doesn't get done
3823 * until the single-line macro substitution process; this is
3824 * because delaying them allows us to change the semantics
3825 * later through %rotate.
3827 * First, push an end marker on to istk->expansion, mark this
3828 * macro as in progress, and set up its invocation-specific
3829 * variables.
3831 ll = nasm_malloc(sizeof(Line));
3832 ll->next = istk->expansion;
3833 ll->finishes = m;
3834 ll->first = NULL;
3835 istk->expansion = ll;
3837 m->in_progress = true;
3838 m->params = params;
3839 m->iline = tline;
3840 m->nparam = nparam;
3841 m->rotate = 0;
3842 m->paramlen = paramlen;
3843 m->unique = unique++;
3844 m->lineno = 0;
3846 m->next_active = istk->mstk;
3847 istk->mstk = m;
3849 for (l = m->expansion; l; l = l->next) {
3850 Token **tail;
3852 ll = nasm_malloc(sizeof(Line));
3853 ll->finishes = NULL;
3854 ll->next = istk->expansion;
3855 istk->expansion = ll;
3856 tail = &ll->first;
3858 for (t = l->first; t; t = t->next) {
3859 Token *x = t;
3860 switch (t->type) {
3861 case TOK_PREPROC_Q:
3862 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3863 break;
3864 case TOK_PREPROC_QQ:
3865 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3866 break;
3867 case TOK_PREPROC_ID:
3868 if (t->text[1] == '0' && t->text[2] == '0') {
3869 dont_prepend = -1;
3870 x = label;
3871 if (!x)
3872 continue;
3874 /* fall through */
3875 default:
3876 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3877 break;
3879 tail = &tt->next;
3881 *tail = NULL;
3885 * If we had a label, push it on as the first line of
3886 * the macro expansion.
3888 if (label) {
3889 if (dont_prepend < 0)
3890 free_tlist(startline);
3891 else {
3892 ll = nasm_malloc(sizeof(Line));
3893 ll->finishes = NULL;
3894 ll->next = istk->expansion;
3895 istk->expansion = ll;
3896 ll->first = startline;
3897 if (!dont_prepend) {
3898 while (label->next)
3899 label = label->next;
3900 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3905 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3907 return 1;
3911 * Since preprocessor always operate only on the line that didn't
3912 * arrived yet, we should always use ERR_OFFBY1. Also since user
3913 * won't want to see same error twice (preprocessing is done once
3914 * per pass) we will want to show errors only during pass one.
3916 static void error(int severity, const char *fmt, ...)
3918 va_list arg;
3919 char buff[1024];
3921 /* If we're in a dead branch of IF or something like it, ignore the error */
3922 if (istk && istk->conds && !emitting(istk->conds->state))
3923 return;
3925 va_start(arg, fmt);
3926 vsnprintf(buff, sizeof(buff), fmt, arg);
3927 va_end(arg);
3929 if (istk && istk->mstk && istk->mstk->name)
3930 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3931 istk->mstk->lineno, buff);
3932 else
3933 _error(severity | ERR_PASS1, "%s", buff);
3936 static void
3937 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3938 ListGen * listgen, StrList **deplist)
3940 _error = errfunc;
3941 cstk = NULL;
3942 istk = nasm_malloc(sizeof(Include));
3943 istk->next = NULL;
3944 istk->conds = NULL;
3945 istk->expansion = NULL;
3946 istk->mstk = NULL;
3947 istk->fp = fopen(file, "r");
3948 istk->fname = NULL;
3949 src_set_fname(nasm_strdup(file));
3950 src_set_linnum(0);
3951 istk->lineinc = 1;
3952 if (!istk->fp)
3953 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3954 file);
3955 defining = NULL;
3956 init_macros();
3957 unique = 0;
3958 if (tasm_compatible_mode) {
3959 stdmacpos = nasm_stdmac;
3960 } else {
3961 stdmacpos = nasm_stdmac_after_tasm;
3963 any_extrastdmac = (extrastdmac != NULL);
3964 list = listgen;
3965 evaluate = eval;
3966 pass = apass;
3967 dephead = deptail = deplist;
3968 if (deplist) {
3969 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3970 sl->next = NULL;
3971 strcpy(sl->str, file);
3972 *deptail = sl;
3973 deptail = &sl->next;
3977 static char *pp_getline(void)
3979 char *line;
3980 Token *tline;
3982 while (1) {
3984 * Fetch a tokenized line, either from the macro-expansion
3985 * buffer or from the input file.
3987 tline = NULL;
3988 while (istk->expansion && istk->expansion->finishes) {
3989 Line *l = istk->expansion;
3990 if (!l->finishes->name && l->finishes->in_progress > 1) {
3991 Line *ll;
3994 * This is a macro-end marker for a macro with no
3995 * name, which means it's not really a macro at all
3996 * but a %rep block, and the `in_progress' field is
3997 * more than 1, meaning that we still need to
3998 * repeat. (1 means the natural last repetition; 0
3999 * means termination by %exitrep.) We have
4000 * therefore expanded up to the %endrep, and must
4001 * push the whole block on to the expansion buffer
4002 * again. We don't bother to remove the macro-end
4003 * marker: we'd only have to generate another one
4004 * if we did.
4006 l->finishes->in_progress--;
4007 for (l = l->finishes->expansion; l; l = l->next) {
4008 Token *t, *tt, **tail;
4010 ll = nasm_malloc(sizeof(Line));
4011 ll->next = istk->expansion;
4012 ll->finishes = NULL;
4013 ll->first = NULL;
4014 tail = &ll->first;
4016 for (t = l->first; t; t = t->next) {
4017 if (t->text || t->type == TOK_WHITESPACE) {
4018 tt = *tail =
4019 new_Token(NULL, t->type, t->text, 0);
4020 tail = &tt->next;
4024 istk->expansion = ll;
4026 } else {
4028 * Check whether a `%rep' was started and not ended
4029 * within this macro expansion. This can happen and
4030 * should be detected. It's a fatal error because
4031 * I'm too confused to work out how to recover
4032 * sensibly from it.
4034 if (defining) {
4035 if (defining->name)
4036 error(ERR_PANIC,
4037 "defining with name in expansion");
4038 else if (istk->mstk->name)
4039 error(ERR_FATAL,
4040 "`%%rep' without `%%endrep' within"
4041 " expansion of macro `%s'",
4042 istk->mstk->name);
4046 * FIXME: investigate the relationship at this point between
4047 * istk->mstk and l->finishes
4050 MMacro *m = istk->mstk;
4051 istk->mstk = m->next_active;
4052 if (m->name) {
4054 * This was a real macro call, not a %rep, and
4055 * therefore the parameter information needs to
4056 * be freed.
4058 nasm_free(m->params);
4059 free_tlist(m->iline);
4060 nasm_free(m->paramlen);
4061 l->finishes->in_progress = false;
4062 } else
4063 free_mmacro(m);
4065 istk->expansion = l->next;
4066 nasm_free(l);
4067 list->downlevel(LIST_MACRO);
4070 while (1) { /* until we get a line we can use */
4072 if (istk->expansion) { /* from a macro expansion */
4073 char *p;
4074 Line *l = istk->expansion;
4075 if (istk->mstk)
4076 istk->mstk->lineno++;
4077 tline = l->first;
4078 istk->expansion = l->next;
4079 nasm_free(l);
4080 p = detoken(tline, false);
4081 list->line(LIST_MACRO, p);
4082 nasm_free(p);
4083 break;
4085 line = read_line();
4086 if (line) { /* from the current input file */
4087 line = prepreproc(line);
4088 tline = tokenize(line);
4089 nasm_free(line);
4090 break;
4093 * The current file has ended; work down the istk
4096 Include *i = istk;
4097 fclose(i->fp);
4098 if (i->conds)
4099 error(ERR_FATAL,
4100 "expected `%%endif' before end of file");
4101 /* only set line and file name if there's a next node */
4102 if (i->next) {
4103 src_set_linnum(i->lineno);
4104 nasm_free(src_set_fname(i->fname));
4106 istk = i->next;
4107 list->downlevel(LIST_INCLUDE);
4108 nasm_free(i);
4109 if (!istk)
4110 return NULL;
4115 * We must expand MMacro parameters and MMacro-local labels
4116 * _before_ we plunge into directive processing, to cope
4117 * with things like `%define something %1' such as STRUC
4118 * uses. Unless we're _defining_ a MMacro, in which case
4119 * those tokens should be left alone to go into the
4120 * definition; and unless we're in a non-emitting
4121 * condition, in which case we don't want to meddle with
4122 * anything.
4124 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4125 tline = expand_mmac_params(tline);
4128 * Check the line to see if it's a preprocessor directive.
4130 if (do_directive(tline) == DIRECTIVE_FOUND) {
4131 continue;
4132 } else if (defining) {
4134 * We're defining a multi-line macro. We emit nothing
4135 * at all, and just
4136 * shove the tokenized line on to the macro definition.
4138 Line *l = nasm_malloc(sizeof(Line));
4139 l->next = defining->expansion;
4140 l->first = tline;
4141 l->finishes = false;
4142 defining->expansion = l;
4143 continue;
4144 } else if (istk->conds && !emitting(istk->conds->state)) {
4146 * We're in a non-emitting branch of a condition block.
4147 * Emit nothing at all, not even a blank line: when we
4148 * emerge from the condition we'll give a line-number
4149 * directive so we keep our place correctly.
4151 free_tlist(tline);
4152 continue;
4153 } else if (istk->mstk && !istk->mstk->in_progress) {
4155 * We're in a %rep block which has been terminated, so
4156 * we're walking through to the %endrep without
4157 * emitting anything. Emit nothing at all, not even a
4158 * blank line: when we emerge from the %rep block we'll
4159 * give a line-number directive so we keep our place
4160 * correctly.
4162 free_tlist(tline);
4163 continue;
4164 } else {
4165 tline = expand_smacro(tline);
4166 if (!expand_mmacro(tline)) {
4168 * De-tokenize the line again, and emit it.
4170 line = detoken(tline, true);
4171 free_tlist(tline);
4172 break;
4173 } else {
4174 continue; /* expand_mmacro calls free_tlist */
4179 return line;
4182 static void pp_cleanup(int pass)
4184 if (defining) {
4185 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4186 defining->name);
4187 free_mmacro(defining);
4189 while (cstk)
4190 ctx_pop();
4191 free_macros();
4192 while (istk) {
4193 Include *i = istk;
4194 istk = istk->next;
4195 fclose(i->fp);
4196 nasm_free(i->fname);
4197 nasm_free(i);
4199 while (cstk)
4200 ctx_pop();
4201 if (pass == 0) {
4202 free_llist(predef);
4203 delete_Blocks();
4207 void pp_include_path(char *path)
4209 IncPath *i;
4211 i = nasm_malloc(sizeof(IncPath));
4212 i->path = path ? nasm_strdup(path) : NULL;
4213 i->next = NULL;
4215 if (ipath != NULL) {
4216 IncPath *j = ipath;
4217 while (j->next != NULL)
4218 j = j->next;
4219 j->next = i;
4220 } else {
4221 ipath = i;
4225 void pp_pre_include(char *fname)
4227 Token *inc, *space, *name;
4228 Line *l;
4230 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4231 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4232 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4234 l = nasm_malloc(sizeof(Line));
4235 l->next = predef;
4236 l->first = inc;
4237 l->finishes = false;
4238 predef = l;
4241 void pp_pre_define(char *definition)
4243 Token *def, *space;
4244 Line *l;
4245 char *equals;
4247 equals = strchr(definition, '=');
4248 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4249 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4250 if (equals)
4251 *equals = ' ';
4252 space->next = tokenize(definition);
4253 if (equals)
4254 *equals = '=';
4256 l = nasm_malloc(sizeof(Line));
4257 l->next = predef;
4258 l->first = def;
4259 l->finishes = false;
4260 predef = l;
4263 void pp_pre_undefine(char *definition)
4265 Token *def, *space;
4266 Line *l;
4268 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4269 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4270 space->next = tokenize(definition);
4272 l = nasm_malloc(sizeof(Line));
4273 l->next = predef;
4274 l->first = def;
4275 l->finishes = false;
4276 predef = l;
4280 * Added by Keith Kanios:
4282 * This function is used to assist with "runtime" preprocessor
4283 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4285 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4286 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4289 void pp_runtime(char *definition)
4291 Token *def;
4293 def = tokenize(definition);
4294 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4295 free_tlist(def);
4299 void pp_extra_stdmac(const char **macros)
4301 extrastdmac = macros;
4304 static void make_tok_num(Token * tok, int64_t val)
4306 char numbuf[20];
4307 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4308 tok->text = nasm_strdup(numbuf);
4309 tok->type = TOK_NUMBER;
4312 Preproc nasmpp = {
4313 pp_reset,
4314 pp_getline,
4315 pp_cleanup