Support for Win32 safeseh (BR 195417)
[nasm.git] / preproc.c
blobc244e8c55834d6a38332285bb080b4c39b954d35
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include "compiler.h"
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51 #include "stdscan.h"
52 #include "tokens.h"
53 #include "tables.h"
55 typedef struct SMacro SMacro;
56 typedef struct MMacro MMacro;
57 typedef struct Context Context;
58 typedef struct Token Token;
59 typedef struct Blocks Blocks;
60 typedef struct Line Line;
61 typedef struct Include Include;
62 typedef struct Cond Cond;
63 typedef struct IncPath IncPath;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
77 struct SMacro {
78 SMacro *next;
79 char *name;
80 bool casesense;
81 bool in_progress;
82 unsigned int nparam;
83 Token *expansion;
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
103 struct MMacro {
104 MMacro *next;
105 char *name;
106 int nparam_min, nparam_max;
107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
110 int64_t in_progress;
111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
114 Line *expansion;
116 MMacro *next_active;
117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
120 unsigned int nparam, rotate;
121 int *paramlen;
122 uint64_t unique;
123 int lineno; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
129 struct Context {
130 Context *next;
131 struct hash_table *localmac;
132 char *name;
133 uint32_t number;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
155 enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
159 TOK_INTERNAL_STRING
162 struct Token {
163 Token *next;
164 char *text;
165 SMacro *mac; /* associated macro for TOK_SMAC_END */
166 enum pp_token_type type;
170 * Multi-line macro definitions are stored as a linked list of
171 * these, which is essentially a container to allow several linked
172 * lists of Tokens.
174 * Note that in this module, linked lists are treated as stacks
175 * wherever possible. For this reason, Lines are _pushed_ on to the
176 * `expansion' field in MMacro structures, so that the linked list,
177 * if walked, would give the macro lines in reverse order; this
178 * means that we can walk the list when expanding a macro, and thus
179 * push the lines on to the `expansion' field in _istk_ in reverse
180 * order (so that when popped back off they are in the right
181 * order). It may seem cockeyed, and it relies on my design having
182 * an even number of steps in, but it works...
184 * Some of these structures, rather than being actual lines, are
185 * markers delimiting the end of the expansion of a given macro.
186 * This is for use in the cycle-tracking and %rep-handling code.
187 * Such structures have `finishes' non-NULL, and `first' NULL. All
188 * others have `finishes' NULL, but `first' may still be NULL if
189 * the line is blank.
191 struct Line {
192 Line *next;
193 MMacro *finishes;
194 Token *first;
198 * To handle an arbitrary level of file inclusion, we maintain a
199 * stack (ie linked list) of these things.
201 struct Include {
202 Include *next;
203 FILE *fp;
204 Cond *conds;
205 Line *expansion;
206 char *fname;
207 int lineno, lineinc;
208 MMacro *mstk; /* stack of active macros/reps */
212 * Include search path. This is simply a list of strings which get
213 * prepended, in turn, to the name of an include file, in an
214 * attempt to find the file if it's not in the current directory.
216 struct IncPath {
217 IncPath *next;
218 char *path;
222 * Conditional assembly: we maintain a separate stack of these for
223 * each level of file inclusion. (The only reason we keep the
224 * stacks separate is to ensure that a stray `%endif' in a file
225 * included from within the true branch of a `%if' won't terminate
226 * it and cause confusion: instead, rightly, it'll cause an error.)
228 struct Cond {
229 Cond *next;
230 int state;
232 enum {
234 * These states are for use just after %if or %elif: IF_TRUE
235 * means the condition has evaluated to truth so we are
236 * currently emitting, whereas IF_FALSE means we are not
237 * currently emitting but will start doing so if a %else comes
238 * up. In these states, all directives are admissible: %elif,
239 * %else and %endif. (And of course %if.)
241 COND_IF_TRUE, COND_IF_FALSE,
243 * These states come up after a %else: ELSE_TRUE means we're
244 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
245 * any %elif or %else will cause an error.
247 COND_ELSE_TRUE, COND_ELSE_FALSE,
249 * This state means that we're not emitting now, and also that
250 * nothing until %endif will be emitted at all. It's for use in
251 * two circumstances: (i) when we've had our moment of emission
252 * and have now started seeing %elifs, and (ii) when the
253 * condition construct in question is contained within a
254 * non-emitting branch of a larger condition construct.
256 COND_NEVER
258 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
261 * These defines are used as the possible return values for do_directive
263 #define NO_DIRECTIVE_FOUND 0
264 #define DIRECTIVE_FOUND 1
267 * Condition codes. Note that we use c_ prefix not C_ because C_ is
268 * used in nasm.h for the "real" condition codes. At _this_ level,
269 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
270 * ones, so we need a different enum...
272 static const char * const conditions[] = {
273 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
274 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
275 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
277 enum pp_conds {
278 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
279 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
280 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
281 c_none = -1
283 static const enum pp_conds inverse_ccs[] = {
284 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
285 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,
286 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
290 * Directive names.
292 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
293 static int is_condition(enum preproc_token arg)
295 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
298 /* For TASM compatibility we need to be able to recognise TASM compatible
299 * conditional compilation directives. Using the NASM pre-processor does
300 * not work, so we look for them specifically from the following list and
301 * then jam in the equivalent NASM directive into the input stream.
304 enum {
305 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
306 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
309 static const char * const tasm_directives[] = {
310 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
311 "ifndef", "include", "local"
314 static int StackSize = 4;
315 static char *StackPointer = "ebp";
316 static int ArgOffset = 8;
317 static int LocalOffset = 0;
319 static Context *cstk;
320 static Include *istk;
321 static IncPath *ipath = NULL;
323 static efunc _error; /* Pointer to client-provided error reporting function */
324 static evalfunc evaluate;
326 static int pass; /* HACK: pass 0 = generate dependencies only */
328 static uint64_t unique; /* unique identifier numbers */
330 static Line *predef = NULL;
332 static ListGen *list;
335 * The current set of multi-line macros we have defined.
337 static struct hash_table *mmacros;
340 * The current set of single-line macros we have defined.
342 static struct hash_table *smacros;
345 * The multi-line macro we are currently defining, or the %rep
346 * block we are currently reading, if any.
348 static MMacro *defining;
351 * The number of macro parameters to allocate space for at a time.
353 #define PARAM_DELTA 16
356 * The standard macro set: defined in macros.c in the array nasm_stdmac.
357 * This gives our position in the macro set, when we're processing it.
359 static const char * const *stdmacpos;
362 * The extra standard macros that come from the object format, if
363 * any.
365 static const char * const *extrastdmac = NULL;
366 bool any_extrastdmac;
369 * Tokens are allocated in blocks to improve speed
371 #define TOKEN_BLOCKSIZE 4096
372 static Token *freeTokens = NULL;
373 struct Blocks {
374 Blocks *next;
375 void *chunk;
378 static Blocks blocks = { NULL, NULL };
381 * Forward declarations.
383 static Token *expand_mmac_params(Token * tline);
384 static Token *expand_smacro(Token * tline);
385 static Token *expand_id(Token * tline);
386 static Context *get_ctx(char *name, bool all_contexts);
387 static void make_tok_num(Token * tok, int64_t val);
388 static void error(int severity, const char *fmt, ...);
389 static void *new_Block(size_t size);
390 static void delete_Blocks(void);
391 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
392 static Token *delete_Token(Token * t);
395 * Macros for safe checking of token pointers, avoid *(NULL)
397 #define tok_type_(x,t) ((x) && (x)->type == (t))
398 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
402 /* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
407 static char *check_tasm_directive(char *line)
409 int32_t i, j, k, m, len;
410 char *p = line, *oldline, oldchar;
412 /* Skip whitespace */
413 while (isspace(*p) && *p != 0)
414 p++;
416 /* Binary search for the directive name */
417 i = -1;
418 j = elements(tasm_directives);
419 len = 0;
420 while (!isspace(p[len]) && p[len] != 0)
421 len++;
422 if (len) {
423 oldchar = p[len];
424 p[len] = 0;
425 while (j - i > 1) {
426 k = (j + i) / 2;
427 m = nasm_stricmp(p, tasm_directives[k]);
428 if (m == 0) {
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
432 p[len] = oldchar;
433 len = strlen(p);
434 oldline = line;
435 line = nasm_malloc(len + 2);
436 line[0] = '%';
437 if (k == TM_IFDIFI) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
441 * package.
443 strcpy(line + 1, "ifdef BOGUS");
444 } else {
445 memcpy(line + 1, p, len + 1);
447 nasm_free(oldline);
448 return line;
449 } else if (m < 0) {
450 j = k;
451 } else
452 i = k;
454 p[len] = oldchar;
456 return line;
460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
463 * lineno file').
465 static char *prepreproc(char *line)
467 int lineno, fnlen;
468 char *fname, *oldline;
470 if (line[0] == '#' && line[1] == ' ') {
471 oldline = line;
472 fname = oldline + 2;
473 lineno = atoi(fname);
474 fname += strspn(fname, "0123456789 ");
475 if (*fname == '"')
476 fname++;
477 fnlen = strcspn(fname, "\"");
478 line = nasm_malloc(20 + fnlen);
479 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
480 nasm_free(oldline);
482 if (tasm_compatible_mode)
483 return check_tasm_directive(line);
484 return line;
488 * Free a linked list of tokens.
490 static void free_tlist(Token * list)
492 while (list) {
493 list = delete_Token(list);
498 * Free a linked list of lines.
500 static void free_llist(Line * list)
502 Line *l;
503 while (list) {
504 l = list;
505 list = list->next;
506 free_tlist(l->first);
507 nasm_free(l);
512 * Free an MMacro
514 static void free_mmacro(MMacro * m)
516 nasm_free(m->name);
517 free_tlist(m->dlist);
518 nasm_free(m->defaults);
519 free_llist(m->expansion);
520 nasm_free(m);
524 * Free all currently defined macros, and free the hash tables
526 static void free_smacro_table(struct hash_table *smt)
528 SMacro *s;
529 const char *key;
530 struct hash_tbl_node *it = NULL;
532 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
533 nasm_free((void *)key);
534 while (s) {
535 SMacro *ns = s->next;
536 nasm_free(s->name);
537 free_tlist(s->expansion);
538 nasm_free(s);
539 s = ns;
542 hash_free(smt);
545 static void free_mmacro_table(struct hash_table *mmt)
547 MMacro *m;
548 const char *key;
549 struct hash_tbl_node *it = NULL;
551 it = NULL;
552 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
553 nasm_free((void *)key);
554 while (m) {
555 MMacro *nm = m->next;
556 free_mmacro(m);
557 m = nm;
560 hash_free(mmt);
563 static void free_macros(void)
565 free_smacro_table(smacros);
566 free_mmacro_table(mmacros);
570 * Initialize the hash tables
572 static void init_macros(void)
574 smacros = hash_init(HASH_LARGE);
575 mmacros = hash_init(HASH_LARGE);
579 * Pop the context stack.
581 static void ctx_pop(void)
583 Context *c = cstk;
585 cstk = cstk->next;
586 free_smacro_table(c->localmac);
587 nasm_free(c->name);
588 nasm_free(c);
592 * Search for a key in the hash index; adding it if necessary
593 * (in which case we initialize the data pointer to NULL.)
595 static void **
596 hash_findi_add(struct hash_table *hash, const char *str)
598 struct hash_insert hi;
599 void **r;
600 char *strx;
602 r = hash_findi(hash, str, &hi);
603 if (r)
604 return r;
606 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
607 return hash_add(&hi, strx, NULL);
611 * Like hash_findi, but returns the data element rather than a pointer
612 * to it. Used only when not adding a new element, hence no third
613 * argument.
615 static void *
616 hash_findix(struct hash_table *hash, const char *str)
618 void **p;
620 p = hash_findi(hash, str, NULL);
621 return p ? *p : NULL;
624 #define BUF_DELTA 512
626 * Read a line from the top file in istk, handling multiple CR/LFs
627 * at the end of the line read, and handling spurious ^Zs. Will
628 * return lines from the standard macro set if this has not already
629 * been done.
631 static char *read_line(void)
633 char *buffer, *p, *q;
634 int bufsize, continued_count;
636 if (stdmacpos) {
637 if (*stdmacpos) {
638 char *ret = nasm_strdup(*stdmacpos++);
639 if (!*stdmacpos && any_extrastdmac) {
640 stdmacpos = extrastdmac;
641 any_extrastdmac = false;
642 return ret;
645 * Nasty hack: here we push the contents of `predef' on
646 * to the top-level expansion stack, since this is the
647 * most convenient way to implement the pre-include and
648 * pre-define features.
650 if (!*stdmacpos) {
651 Line *pd, *l;
652 Token *head, **tail, *t;
654 for (pd = predef; pd; pd = pd->next) {
655 head = NULL;
656 tail = &head;
657 for (t = pd->first; t; t = t->next) {
658 *tail = new_Token(NULL, t->type, t->text, 0);
659 tail = &(*tail)->next;
661 l = nasm_malloc(sizeof(Line));
662 l->next = istk->expansion;
663 l->first = head;
664 l->finishes = false;
665 istk->expansion = l;
668 return ret;
669 } else {
670 stdmacpos = NULL;
674 bufsize = BUF_DELTA;
675 buffer = nasm_malloc(BUF_DELTA);
676 p = buffer;
677 continued_count = 0;
678 while (1) {
679 q = fgets(p, bufsize - (p - buffer), istk->fp);
680 if (!q)
681 break;
682 p += strlen(p);
683 if (p > buffer && p[-1] == '\n') {
684 /* Convert backslash-CRLF line continuation sequences into
685 nothing at all (for DOS and Windows) */
686 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
687 p -= 3;
688 *p = 0;
689 continued_count++;
691 /* Also convert backslash-LF line continuation sequences into
692 nothing at all (for Unix) */
693 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
694 p -= 2;
695 *p = 0;
696 continued_count++;
697 } else {
698 break;
701 if (p - buffer > bufsize - 10) {
702 int32_t offset = p - buffer;
703 bufsize += BUF_DELTA;
704 buffer = nasm_realloc(buffer, bufsize);
705 p = buffer + offset; /* prevent stale-pointer problems */
709 if (!q && p == buffer) {
710 nasm_free(buffer);
711 return NULL;
714 src_set_linnum(src_get_linnum() + istk->lineinc +
715 (continued_count * istk->lineinc));
718 * Play safe: remove CRs as well as LFs, if any of either are
719 * present at the end of the line.
721 while (--p >= buffer && (*p == '\n' || *p == '\r'))
722 *p = '\0';
725 * Handle spurious ^Z, which may be inserted into source files
726 * by some file transfer utilities.
728 buffer[strcspn(buffer, "\032")] = '\0';
730 list->line(LIST_READ, buffer);
732 return buffer;
736 * Tokenize a line of text. This is a very simple process since we
737 * don't need to parse the value out of e.g. numeric tokens: we
738 * simply split one string into many.
740 static Token *tokenize(char *line)
742 char *p = line;
743 enum pp_token_type type;
744 Token *list = NULL;
745 Token *t, **tail = &list;
747 while (*line) {
748 p = line;
749 if (*p == '%') {
750 p++;
751 if (isdigit(*p) ||
752 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
753 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
754 do {
755 p++;
757 while (isdigit(*p));
758 type = TOK_PREPROC_ID;
759 } else if (*p == '{') {
760 p++;
761 while (*p && *p != '}') {
762 p[-1] = *p;
763 p++;
765 p[-1] = '\0';
766 if (*p)
767 p++;
768 type = TOK_PREPROC_ID;
769 } else if (isidchar(*p) ||
770 ((*p == '!' || *p == '%' || *p == '$') &&
771 isidchar(p[1]))) {
772 do {
773 p++;
775 while (isidchar(*p));
776 type = TOK_PREPROC_ID;
777 } else {
778 type = TOK_OTHER;
779 if (*p == '%')
780 p++;
782 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
783 type = TOK_ID;
784 p++;
785 while (*p && isidchar(*p))
786 p++;
787 } else if (*p == '\'' || *p == '"') {
789 * A string token.
791 char c = *p;
792 p++;
793 type = TOK_STRING;
794 while (*p && *p != c)
795 p++;
797 if (*p) {
798 p++;
799 } else {
800 error(ERR_WARNING, "unterminated string");
801 /* Handling unterminated strings by UNV */
802 /* type = -1; */
804 } else if (isnumstart(*p)) {
805 bool is_hex = false;
806 bool is_float = false;
807 bool has_e = false;
808 char c, *r;
811 * A numeric token.
814 if (*p == '$') {
815 p++;
816 is_hex = true;
819 for (;;) {
820 c = *p++;
822 if (!is_hex && (c == 'e' || c == 'E')) {
823 has_e = true;
824 if (*p == '+' || *p == '-') {
825 /* e can only be followed by +/- if it is either a
826 prefixed hex number or a floating-point number */
827 p++;
828 is_float = true;
830 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
831 is_hex = true;
832 } else if (c == 'P' || c == 'p') {
833 is_float = true;
834 if (*p == '+' || *p == '-')
835 p++;
836 } else if (isnumchar(c) || c == '_')
837 ; /* just advance */
838 else if (c == '.') {
839 /* we need to deal with consequences of the legacy
840 parser, like "1.nolist" being two tokens
841 (TOK_NUMBER, TOK_ID) here; at least give it
842 a shot for now. In the future, we probably need
843 a flex-based scanner with proper pattern matching
844 to do it as well as it can be done. Nothing in
845 the world is going to help the person who wants
846 0x123.p16 interpreted as two tokens, though. */
847 r = p;
848 while (*r == '_')
849 r++;
851 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
852 (!is_hex && (*r == 'e' || *r == 'E')) ||
853 (*r == 'p' || *r == 'P')) {
854 p = r;
855 is_float = true;
856 } else
857 break; /* Terminate the token */
858 } else
859 break;
861 p--; /* Point to first character beyond number */
863 if (has_e && !is_hex) {
864 /* 1e13 is floating-point, but 1e13h is not */
865 is_float = true;
868 type = is_float ? TOK_FLOAT : TOK_NUMBER;
869 } else if (isspace(*p)) {
870 type = TOK_WHITESPACE;
871 p++;
872 while (*p && isspace(*p))
873 p++;
875 * Whitespace just before end-of-line is discarded by
876 * pretending it's a comment; whitespace just before a
877 * comment gets lumped into the comment.
879 if (!*p || *p == ';') {
880 type = TOK_COMMENT;
881 while (*p)
882 p++;
884 } else if (*p == ';') {
885 type = TOK_COMMENT;
886 while (*p)
887 p++;
888 } else {
890 * Anything else is an operator of some kind. We check
891 * for all the double-character operators (>>, <<, //,
892 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
893 * else is a single-character operator.
895 type = TOK_OTHER;
896 if ((p[0] == '>' && p[1] == '>') ||
897 (p[0] == '<' && p[1] == '<') ||
898 (p[0] == '/' && p[1] == '/') ||
899 (p[0] == '<' && p[1] == '=') ||
900 (p[0] == '>' && p[1] == '=') ||
901 (p[0] == '=' && p[1] == '=') ||
902 (p[0] == '!' && p[1] == '=') ||
903 (p[0] == '<' && p[1] == '>') ||
904 (p[0] == '&' && p[1] == '&') ||
905 (p[0] == '|' && p[1] == '|') ||
906 (p[0] == '^' && p[1] == '^')) {
907 p++;
909 p++;
912 /* Handling unterminated string by UNV */
913 /*if (type == -1)
915 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
916 t->text[p-line] = *line;
917 tail = &t->next;
919 else */
920 if (type != TOK_COMMENT) {
921 *tail = t = new_Token(NULL, type, line, p - line);
922 tail = &t->next;
924 line = p;
926 return list;
930 * this function allocates a new managed block of memory and
931 * returns a pointer to the block. The managed blocks are
932 * deleted only all at once by the delete_Blocks function.
934 static void *new_Block(size_t size)
936 Blocks *b = &blocks;
938 /* first, get to the end of the linked list */
939 while (b->next)
940 b = b->next;
941 /* now allocate the requested chunk */
942 b->chunk = nasm_malloc(size);
944 /* now allocate a new block for the next request */
945 b->next = nasm_malloc(sizeof(Blocks));
946 /* and initialize the contents of the new block */
947 b->next->next = NULL;
948 b->next->chunk = NULL;
949 return b->chunk;
953 * this function deletes all managed blocks of memory
955 static void delete_Blocks(void)
957 Blocks *a, *b = &blocks;
960 * keep in mind that the first block, pointed to by blocks
961 * is a static and not dynamically allocated, so we don't
962 * free it.
964 while (b) {
965 if (b->chunk)
966 nasm_free(b->chunk);
967 a = b;
968 b = b->next;
969 if (a != &blocks)
970 nasm_free(a);
975 * this function creates a new Token and passes a pointer to it
976 * back to the caller. It sets the type and text elements, and
977 * also the mac and next elements to NULL.
979 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
981 Token *t;
982 int i;
984 if (freeTokens == NULL) {
985 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
986 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
987 freeTokens[i].next = &freeTokens[i + 1];
988 freeTokens[i].next = NULL;
990 t = freeTokens;
991 freeTokens = t->next;
992 t->next = next;
993 t->mac = NULL;
994 t->type = type;
995 if (type == TOK_WHITESPACE || text == NULL) {
996 t->text = NULL;
997 } else {
998 if (txtlen == 0)
999 txtlen = strlen(text);
1000 t->text = nasm_malloc(1 + txtlen);
1001 strncpy(t->text, text, txtlen);
1002 t->text[txtlen] = '\0';
1004 return t;
1007 static Token *delete_Token(Token * t)
1009 Token *next = t->next;
1010 nasm_free(t->text);
1011 t->next = freeTokens;
1012 freeTokens = t;
1013 return next;
1017 * Convert a line of tokens back into text.
1018 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1019 * will be transformed into ..@ctxnum.xxx
1021 static char *detoken(Token * tlist, int expand_locals)
1023 Token *t;
1024 int len;
1025 char *line, *p;
1026 const char *q;
1028 len = 0;
1029 for (t = tlist; t; t = t->next) {
1030 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1031 char *p = getenv(t->text + 2);
1032 nasm_free(t->text);
1033 if (p)
1034 t->text = nasm_strdup(p);
1035 else
1036 t->text = NULL;
1038 /* Expand local macros here and not during preprocessing */
1039 if (expand_locals &&
1040 t->type == TOK_PREPROC_ID && t->text &&
1041 t->text[0] == '%' && t->text[1] == '$') {
1042 Context *ctx = get_ctx(t->text, false);
1043 if (ctx) {
1044 char buffer[40];
1045 char *p, *q = t->text + 2;
1047 q += strspn(q, "$");
1048 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1049 p = nasm_strcat(buffer, q);
1050 nasm_free(t->text);
1051 t->text = p;
1054 if (t->type == TOK_WHITESPACE) {
1055 len++;
1056 } else if (t->text) {
1057 len += strlen(t->text);
1060 p = line = nasm_malloc(len + 1);
1061 for (t = tlist; t; t = t->next) {
1062 if (t->type == TOK_WHITESPACE) {
1063 *p++ = ' ';
1064 } else if (t->text) {
1065 q = t->text;
1066 while (*q)
1067 *p++ = *q++;
1070 *p = '\0';
1071 return line;
1075 * A scanner, suitable for use by the expression evaluator, which
1076 * operates on a line of Tokens. Expects a pointer to a pointer to
1077 * the first token in the line to be passed in as its private_data
1078 * field.
1080 * FIX: This really needs to be unified with stdscan.
1082 static int ppscan(void *private_data, struct tokenval *tokval)
1084 Token **tlineptr = private_data;
1085 Token *tline;
1086 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1088 do {
1089 tline = *tlineptr;
1090 *tlineptr = tline ? tline->next : NULL;
1092 while (tline && (tline->type == TOK_WHITESPACE ||
1093 tline->type == TOK_COMMENT));
1095 if (!tline)
1096 return tokval->t_type = TOKEN_EOS;
1098 tokval->t_charptr = tline->text;
1100 if (tline->text[0] == '$' && !tline->text[1])
1101 return tokval->t_type = TOKEN_HERE;
1102 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1103 return tokval->t_type = TOKEN_BASE;
1105 if (tline->type == TOK_ID) {
1106 p = tokval->t_charptr = tline->text;
1107 if (p[0] == '$') {
1108 tokval->t_charptr++;
1109 return tokval->t_type = TOKEN_ID;
1112 for (r = p, s = ourcopy; *r; r++) {
1113 if (r >= p+MAX_KEYWORD)
1114 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1115 *s++ = tolower(*r);
1117 *s = '\0';
1118 /* right, so we have an identifier sitting in temp storage. now,
1119 * is it actually a register or instruction name, or what? */
1120 return nasm_token_hash(ourcopy, tokval);
1123 if (tline->type == TOK_NUMBER) {
1124 bool rn_error;
1125 tokval->t_integer = readnum(tline->text, &rn_error);
1126 if (rn_error)
1127 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1128 tokval->t_charptr = tline->text;
1129 return tokval->t_type = TOKEN_NUM;
1132 if (tline->type == TOK_FLOAT) {
1133 return tokval->t_type = TOKEN_FLOAT;
1136 if (tline->type == TOK_STRING) {
1137 bool rn_warn;
1138 char q, *r;
1139 int l;
1141 r = tline->text;
1142 q = *r++;
1143 l = strlen(r);
1145 if (l == 0 || r[l - 1] != q)
1146 return tokval->t_type = TOKEN_ERRNUM;
1147 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1148 if (rn_warn)
1149 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1150 tokval->t_charptr = NULL;
1151 return tokval->t_type = TOKEN_NUM;
1154 if (tline->type == TOK_OTHER) {
1155 if (!strcmp(tline->text, "<<"))
1156 return tokval->t_type = TOKEN_SHL;
1157 if (!strcmp(tline->text, ">>"))
1158 return tokval->t_type = TOKEN_SHR;
1159 if (!strcmp(tline->text, "//"))
1160 return tokval->t_type = TOKEN_SDIV;
1161 if (!strcmp(tline->text, "%%"))
1162 return tokval->t_type = TOKEN_SMOD;
1163 if (!strcmp(tline->text, "=="))
1164 return tokval->t_type = TOKEN_EQ;
1165 if (!strcmp(tline->text, "<>"))
1166 return tokval->t_type = TOKEN_NE;
1167 if (!strcmp(tline->text, "!="))
1168 return tokval->t_type = TOKEN_NE;
1169 if (!strcmp(tline->text, "<="))
1170 return tokval->t_type = TOKEN_LE;
1171 if (!strcmp(tline->text, ">="))
1172 return tokval->t_type = TOKEN_GE;
1173 if (!strcmp(tline->text, "&&"))
1174 return tokval->t_type = TOKEN_DBL_AND;
1175 if (!strcmp(tline->text, "^^"))
1176 return tokval->t_type = TOKEN_DBL_XOR;
1177 if (!strcmp(tline->text, "||"))
1178 return tokval->t_type = TOKEN_DBL_OR;
1182 * We have no other options: just return the first character of
1183 * the token text.
1185 return tokval->t_type = tline->text[0];
1189 * Compare a string to the name of an existing macro; this is a
1190 * simple wrapper which calls either strcmp or nasm_stricmp
1191 * depending on the value of the `casesense' parameter.
1193 static int mstrcmp(const char *p, const char *q, bool casesense)
1195 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1199 * Return the Context structure associated with a %$ token. Return
1200 * NULL, having _already_ reported an error condition, if the
1201 * context stack isn't deep enough for the supplied number of $
1202 * signs.
1203 * If all_contexts == true, contexts that enclose current are
1204 * also scanned for such smacro, until it is found; if not -
1205 * only the context that directly results from the number of $'s
1206 * in variable's name.
1208 static Context *get_ctx(char *name, bool all_contexts)
1210 Context *ctx;
1211 SMacro *m;
1212 int i;
1214 if (!name || name[0] != '%' || name[1] != '$')
1215 return NULL;
1217 if (!cstk) {
1218 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1219 return NULL;
1222 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1223 ctx = ctx->next;
1224 /* i--; Lino - 02/25/02 */
1226 if (!ctx) {
1227 error(ERR_NONFATAL, "`%s': context stack is only"
1228 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1229 return NULL;
1231 if (!all_contexts)
1232 return ctx;
1234 do {
1235 /* Search for this smacro in found context */
1236 m = hash_findix(ctx->localmac, name);
1237 while (m) {
1238 if (!mstrcmp(m->name, name, m->casesense))
1239 return ctx;
1240 m = m->next;
1242 ctx = ctx->next;
1244 while (ctx);
1245 return NULL;
1249 * Open an include file. This routine must always return a valid
1250 * file pointer if it returns - it's responsible for throwing an
1251 * ERR_FATAL and bombing out completely if not. It should also try
1252 * the include path one by one until it finds the file or reaches
1253 * the end of the path.
1255 static FILE *inc_fopen(char *file)
1257 FILE *fp;
1258 char *prefix = "", *combine;
1259 IncPath *ip = ipath;
1260 static int namelen = 0;
1261 int len = strlen(file);
1263 while (1) {
1264 combine = nasm_malloc(strlen(prefix) + len + 1);
1265 strcpy(combine, prefix);
1266 strcat(combine, file);
1267 fp = fopen(combine, "r");
1268 if (pass == 0 && fp) {
1269 namelen += strlen(combine) + 1;
1270 if (namelen > 62) {
1271 printf(" \\\n ");
1272 namelen = 2;
1274 printf(" %s", combine);
1276 nasm_free(combine);
1277 if (fp)
1278 return fp;
1279 if (!ip)
1280 break;
1281 prefix = ip->path;
1282 ip = ip->next;
1284 if (!prefix) {
1285 /* -MG given and file not found */
1286 if (pass == 0) {
1287 namelen += strlen(file) + 1;
1288 if (namelen > 62) {
1289 printf(" \\\n ");
1290 namelen = 2;
1292 printf(" %s", file);
1294 return NULL;
1298 error(ERR_FATAL, "unable to open include file `%s'", file);
1299 return NULL; /* never reached - placate compilers */
1303 * Determine if we should warn on defining a single-line macro of
1304 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1305 * return true if _any_ single-line macro of that name is defined.
1306 * Otherwise, will return true if a single-line macro with either
1307 * `nparam' or no parameters is defined.
1309 * If a macro with precisely the right number of parameters is
1310 * defined, or nparam is -1, the address of the definition structure
1311 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1312 * is NULL, no action will be taken regarding its contents, and no
1313 * error will occur.
1315 * Note that this is also called with nparam zero to resolve
1316 * `ifdef'.
1318 * If you already know which context macro belongs to, you can pass
1319 * the context pointer as first parameter; if you won't but name begins
1320 * with %$ the context will be automatically computed. If all_contexts
1321 * is true, macro will be searched in outer contexts as well.
1323 static bool
1324 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1325 bool nocase)
1327 SMacro *m;
1329 if (ctx) {
1330 m = (SMacro *) hash_findix(ctx->localmac, name);
1331 } else if (name[0] == '%' && name[1] == '$') {
1332 if (cstk)
1333 ctx = get_ctx(name, false);
1334 if (!ctx)
1335 return false; /* got to return _something_ */
1336 m = (SMacro *) hash_findix(ctx->localmac, name);
1337 } else {
1338 m = (SMacro *) hash_findix(smacros, name);
1341 while (m) {
1342 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1343 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1344 if (defn) {
1345 if (nparam == (int) m->nparam || nparam == -1)
1346 *defn = m;
1347 else
1348 *defn = NULL;
1350 return true;
1352 m = m->next;
1355 return false;
1359 * Count and mark off the parameters in a multi-line macro call.
1360 * This is called both from within the multi-line macro expansion
1361 * code, and also to mark off the default parameters when provided
1362 * in a %macro definition line.
1364 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1366 int paramsize, brace;
1368 *nparam = paramsize = 0;
1369 *params = NULL;
1370 while (t) {
1371 if (*nparam >= paramsize) {
1372 paramsize += PARAM_DELTA;
1373 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1375 skip_white_(t);
1376 brace = false;
1377 if (tok_is_(t, "{"))
1378 brace = true;
1379 (*params)[(*nparam)++] = t;
1380 while (tok_isnt_(t, brace ? "}" : ","))
1381 t = t->next;
1382 if (t) { /* got a comma/brace */
1383 t = t->next;
1384 if (brace) {
1386 * Now we've found the closing brace, look further
1387 * for the comma.
1389 skip_white_(t);
1390 if (tok_isnt_(t, ",")) {
1391 error(ERR_NONFATAL,
1392 "braces do not enclose all of macro parameter");
1393 while (tok_isnt_(t, ","))
1394 t = t->next;
1396 if (t)
1397 t = t->next; /* eat the comma */
1404 * Determine whether one of the various `if' conditions is true or
1405 * not.
1407 * We must free the tline we get passed.
1409 static bool if_condition(Token * tline, enum preproc_token ct)
1411 enum pp_conditional i = PP_COND(ct);
1412 bool j;
1413 Token *t, *tt, **tptr, *origline;
1414 struct tokenval tokval;
1415 expr *evalresult;
1416 enum pp_token_type needtype;
1418 origline = tline;
1420 switch (i) {
1421 case PPC_IFCTX:
1422 j = false; /* have we matched yet? */
1423 while (cstk && tline) {
1424 skip_white_(tline);
1425 if (!tline || tline->type != TOK_ID) {
1426 error(ERR_NONFATAL,
1427 "`%s' expects context identifiers", pp_directives[ct]);
1428 free_tlist(origline);
1429 return -1;
1431 if (!nasm_stricmp(tline->text, cstk->name))
1432 j = true;
1433 tline = tline->next;
1435 break;
1437 case PPC_IFDEF:
1438 j = false; /* have we matched yet? */
1439 while (tline) {
1440 skip_white_(tline);
1441 if (!tline || (tline->type != TOK_ID &&
1442 (tline->type != TOK_PREPROC_ID ||
1443 tline->text[1] != '$'))) {
1444 error(ERR_NONFATAL,
1445 "`%s' expects macro identifiers", pp_directives[ct]);
1446 goto fail;
1448 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1449 j = true;
1450 tline = tline->next;
1452 break;
1454 case PPC_IFIDN:
1455 case PPC_IFIDNI:
1456 tline = expand_smacro(tline);
1457 t = tt = tline;
1458 while (tok_isnt_(tt, ","))
1459 tt = tt->next;
1460 if (!tt) {
1461 error(ERR_NONFATAL,
1462 "`%s' expects two comma-separated arguments",
1463 pp_directives[ct]);
1464 goto fail;
1466 tt = tt->next;
1467 j = true; /* assume equality unless proved not */
1468 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1469 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1470 error(ERR_NONFATAL, "`%s': more than one comma on line",
1471 pp_directives[ct]);
1472 goto fail;
1474 if (t->type == TOK_WHITESPACE) {
1475 t = t->next;
1476 continue;
1478 if (tt->type == TOK_WHITESPACE) {
1479 tt = tt->next;
1480 continue;
1482 if (tt->type != t->type) {
1483 j = false; /* found mismatching tokens */
1484 break;
1486 /* Unify surrounding quotes for strings */
1487 if (t->type == TOK_STRING) {
1488 tt->text[0] = t->text[0];
1489 tt->text[strlen(tt->text) - 1] = t->text[0];
1491 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1492 j = false; /* found mismatching tokens */
1493 break;
1496 t = t->next;
1497 tt = tt->next;
1499 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1500 j = false; /* trailing gunk on one end or other */
1501 break;
1503 case PPC_IFMACRO:
1505 bool found = false;
1506 MMacro searching, *mmac;
1508 tline = tline->next;
1509 skip_white_(tline);
1510 tline = expand_id(tline);
1511 if (!tok_type_(tline, TOK_ID)) {
1512 error(ERR_NONFATAL,
1513 "`%s' expects a macro name", pp_directives[ct]);
1514 goto fail;
1516 searching.name = nasm_strdup(tline->text);
1517 searching.casesense = true;
1518 searching.plus = false;
1519 searching.nolist = false;
1520 searching.in_progress = 0;
1521 searching.rep_nest = NULL;
1522 searching.nparam_min = 0;
1523 searching.nparam_max = INT_MAX;
1524 tline = expand_smacro(tline->next);
1525 skip_white_(tline);
1526 if (!tline) {
1527 } else if (!tok_type_(tline, TOK_NUMBER)) {
1528 error(ERR_NONFATAL,
1529 "`%s' expects a parameter count or nothing",
1530 pp_directives[ct]);
1531 } else {
1532 searching.nparam_min = searching.nparam_max =
1533 readnum(tline->text, &j);
1534 if (j)
1535 error(ERR_NONFATAL,
1536 "unable to parse parameter count `%s'",
1537 tline->text);
1539 if (tline && tok_is_(tline->next, "-")) {
1540 tline = tline->next->next;
1541 if (tok_is_(tline, "*"))
1542 searching.nparam_max = INT_MAX;
1543 else if (!tok_type_(tline, TOK_NUMBER))
1544 error(ERR_NONFATAL,
1545 "`%s' expects a parameter count after `-'",
1546 pp_directives[ct]);
1547 else {
1548 searching.nparam_max = readnum(tline->text, &j);
1549 if (j)
1550 error(ERR_NONFATAL,
1551 "unable to parse parameter count `%s'",
1552 tline->text);
1553 if (searching.nparam_min > searching.nparam_max)
1554 error(ERR_NONFATAL,
1555 "minimum parameter count exceeds maximum");
1558 if (tline && tok_is_(tline->next, "+")) {
1559 tline = tline->next;
1560 searching.plus = true;
1562 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1563 while (mmac) {
1564 if (!strcmp(mmac->name, searching.name) &&
1565 (mmac->nparam_min <= searching.nparam_max
1566 || searching.plus)
1567 && (searching.nparam_min <= mmac->nparam_max
1568 || mmac->plus)) {
1569 found = true;
1570 break;
1572 mmac = mmac->next;
1574 nasm_free(searching.name);
1575 j = found;
1576 break;
1579 case PPC_IFID:
1580 needtype = TOK_ID;
1581 goto iftype;
1582 case PPC_IFNUM:
1583 needtype = TOK_NUMBER;
1584 goto iftype;
1585 case PPC_IFSTR:
1586 needtype = TOK_STRING;
1587 goto iftype;
1589 iftype:
1590 t = tline = expand_smacro(tline);
1592 while (tok_type_(t, TOK_WHITESPACE) ||
1593 (needtype == TOK_NUMBER &&
1594 tok_type_(t, TOK_OTHER) &&
1595 (t->text[0] == '-' || t->text[0] == '+') &&
1596 !t->text[1]))
1597 t = t->next;
1599 j = tok_type_(t, needtype);
1600 break;
1602 case PPC_IFTOKEN:
1603 t = tline = expand_smacro(tline);
1604 while (tok_type_(t, TOK_WHITESPACE))
1605 t = t->next;
1607 j = false;
1608 if (t) {
1609 t = t->next; /* Skip the actual token */
1610 while (tok_type_(t, TOK_WHITESPACE))
1611 t = t->next;
1612 j = !t; /* Should be nothing left */
1614 break;
1616 case PPC_IFEMPTY:
1617 t = tline = expand_smacro(tline);
1618 while (tok_type_(t, TOK_WHITESPACE))
1619 t = t->next;
1621 j = !t; /* Should be empty */
1622 break;
1624 case PPC_IF:
1625 t = tline = expand_smacro(tline);
1626 tptr = &t;
1627 tokval.t_type = TOKEN_INVALID;
1628 evalresult = evaluate(ppscan, tptr, &tokval,
1629 NULL, pass | CRITICAL, error, NULL);
1630 if (!evalresult)
1631 return -1;
1632 if (tokval.t_type)
1633 error(ERR_WARNING,
1634 "trailing garbage after expression ignored");
1635 if (!is_simple(evalresult)) {
1636 error(ERR_NONFATAL,
1637 "non-constant value given to `%s'", pp_directives[ct]);
1638 goto fail;
1640 j = reloc_value(evalresult) != 0;
1641 return j;
1643 default:
1644 error(ERR_FATAL,
1645 "preprocessor directive `%s' not yet implemented",
1646 pp_directives[ct]);
1647 goto fail;
1650 free_tlist(origline);
1651 return j ^ PP_NEGATIVE(ct);
1653 fail:
1654 free_tlist(origline);
1655 return -1;
1659 * Expand macros in a string. Used in %error and %include directives.
1660 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1661 * The returned variable should ALWAYS be freed after usage.
1663 void expand_macros_in_string(char **p)
1665 Token *line = tokenize(*p);
1666 line = expand_smacro(line);
1667 *p = detoken(line, false);
1671 * Common code for defining an smacro
1673 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1674 int nparam, Token *expansion)
1676 SMacro *smac, **smhead;
1678 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1679 if (!smac) {
1680 error(ERR_WARNING,
1681 "single-line macro `%s' defined both with and"
1682 " without parameters", mname);
1684 /* Some instances of the old code considered this a failure,
1685 some others didn't. What is the right thing to do here? */
1686 free_tlist(expansion);
1687 return false; /* Failure */
1688 } else {
1690 * We're redefining, so we have to take over an
1691 * existing SMacro structure. This means freeing
1692 * what was already in it.
1694 nasm_free(smac->name);
1695 free_tlist(smac->expansion);
1697 } else {
1698 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1699 mname);
1700 smac = nasm_malloc(sizeof(SMacro));
1701 smac->next = *smhead;
1702 *smhead = smac;
1704 smac->name = nasm_strdup(mname);
1705 smac->casesense = casesense;
1706 smac->nparam = nparam;
1707 smac->expansion = expansion;
1708 smac->in_progress = false;
1709 return true; /* Success */
1713 * Undefine an smacro
1715 static void undef_smacro(Context *ctx, const char *mname)
1717 SMacro **smhead, *s, **sp;
1719 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
1721 if (smhead) {
1723 * We now have a macro name... go hunt for it.
1725 sp = smhead;
1726 while ((s = *sp) != NULL) {
1727 if (!mstrcmp(s->name, mname, s->casesense)) {
1728 *sp = s->next;
1729 nasm_free(s->name);
1730 free_tlist(s->expansion);
1731 nasm_free(s);
1732 } else {
1733 sp = &s->next;
1740 * Decode a size directive
1742 static int parse_size(const char *str) {
1743 static const char *size_names[] =
1744 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1745 static const int sizes[] =
1746 { 0, 1, 4, 16, 8, 10, 2, 32 };
1748 return sizes[bsii(str, size_names, elements(size_names))+1];
1752 * find and process preprocessor directive in passed line
1753 * Find out if a line contains a preprocessor directive, and deal
1754 * with it if so.
1756 * If a directive _is_ found, it is the responsibility of this routine
1757 * (and not the caller) to free_tlist() the line.
1759 * @param tline a pointer to the current tokeninzed line linked list
1760 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1763 static int do_directive(Token * tline)
1765 enum preproc_token i;
1766 int j;
1767 bool err;
1768 int nparam;
1769 bool nolist;
1770 bool casesense;
1771 int k, m;
1772 int offset;
1773 char *p, *mname;
1774 Include *inc;
1775 Context *ctx;
1776 Cond *cond;
1777 MMacro *mmac, **mmhead;
1778 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1779 Line *l;
1780 struct tokenval tokval;
1781 expr *evalresult;
1782 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1783 int64_t count;
1785 origline = tline;
1787 skip_white_(tline);
1788 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1789 (tline->text[1] == '%' || tline->text[1] == '$'
1790 || tline->text[1] == '!'))
1791 return NO_DIRECTIVE_FOUND;
1793 i = pp_token_hash(tline->text);
1796 * If we're in a non-emitting branch of a condition construct,
1797 * or walking to the end of an already terminated %rep block,
1798 * we should ignore all directives except for condition
1799 * directives.
1801 if (((istk->conds && !emitting(istk->conds->state)) ||
1802 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1803 return NO_DIRECTIVE_FOUND;
1807 * If we're defining a macro or reading a %rep block, we should
1808 * ignore all directives except for %macro/%imacro (which
1809 * generate an error), %endm/%endmacro, and (only if we're in a
1810 * %rep block) %endrep. If we're in a %rep block, another %rep
1811 * causes an error, so should be let through.
1813 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1814 i != PP_ENDMACRO && i != PP_ENDM &&
1815 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1816 return NO_DIRECTIVE_FOUND;
1819 switch (i) {
1820 case PP_INVALID:
1821 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1822 tline->text);
1823 return NO_DIRECTIVE_FOUND; /* didn't get it */
1825 case PP_STACKSIZE:
1826 /* Directive to tell NASM what the default stack size is. The
1827 * default is for a 16-bit stack, and this can be overriden with
1828 * %stacksize large.
1829 * the following form:
1831 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1833 tline = tline->next;
1834 if (tline && tline->type == TOK_WHITESPACE)
1835 tline = tline->next;
1836 if (!tline || tline->type != TOK_ID) {
1837 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1838 free_tlist(origline);
1839 return DIRECTIVE_FOUND;
1841 if (nasm_stricmp(tline->text, "flat") == 0) {
1842 /* All subsequent ARG directives are for a 32-bit stack */
1843 StackSize = 4;
1844 StackPointer = "ebp";
1845 ArgOffset = 8;
1846 LocalOffset = 0;
1847 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1848 /* All subsequent ARG directives are for a 64-bit stack */
1849 StackSize = 8;
1850 StackPointer = "rbp";
1851 ArgOffset = 8;
1852 LocalOffset = 0;
1853 } else if (nasm_stricmp(tline->text, "large") == 0) {
1854 /* All subsequent ARG directives are for a 16-bit stack,
1855 * far function call.
1857 StackSize = 2;
1858 StackPointer = "bp";
1859 ArgOffset = 4;
1860 LocalOffset = 0;
1861 } else if (nasm_stricmp(tline->text, "small") == 0) {
1862 /* All subsequent ARG directives are for a 16-bit stack,
1863 * far function call. We don't support near functions.
1865 StackSize = 2;
1866 StackPointer = "bp";
1867 ArgOffset = 6;
1868 LocalOffset = 0;
1869 } else {
1870 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1871 free_tlist(origline);
1872 return DIRECTIVE_FOUND;
1874 free_tlist(origline);
1875 return DIRECTIVE_FOUND;
1877 case PP_ARG:
1878 /* TASM like ARG directive to define arguments to functions, in
1879 * the following form:
1881 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1883 offset = ArgOffset;
1884 do {
1885 char *arg, directive[256];
1886 int size = StackSize;
1888 /* Find the argument name */
1889 tline = tline->next;
1890 if (tline && tline->type == TOK_WHITESPACE)
1891 tline = tline->next;
1892 if (!tline || tline->type != TOK_ID) {
1893 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1894 free_tlist(origline);
1895 return DIRECTIVE_FOUND;
1897 arg = tline->text;
1899 /* Find the argument size type */
1900 tline = tline->next;
1901 if (!tline || tline->type != TOK_OTHER
1902 || tline->text[0] != ':') {
1903 error(ERR_NONFATAL,
1904 "Syntax error processing `%%arg' directive");
1905 free_tlist(origline);
1906 return DIRECTIVE_FOUND;
1908 tline = tline->next;
1909 if (!tline || tline->type != TOK_ID) {
1910 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1911 free_tlist(origline);
1912 return DIRECTIVE_FOUND;
1915 /* Allow macro expansion of type parameter */
1916 tt = tokenize(tline->text);
1917 tt = expand_smacro(tt);
1918 size = parse_size(tt->text);
1919 if (!size) {
1920 error(ERR_NONFATAL,
1921 "Invalid size type for `%%arg' missing directive");
1922 free_tlist(tt);
1923 free_tlist(origline);
1924 return DIRECTIVE_FOUND;
1926 free_tlist(tt);
1928 /* Round up to even stack slots */
1929 size = (size+StackSize-1) & ~(StackSize-1);
1931 /* Now define the macro for the argument */
1932 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1933 arg, StackPointer, offset);
1934 do_directive(tokenize(directive));
1935 offset += size;
1937 /* Move to the next argument in the list */
1938 tline = tline->next;
1939 if (tline && tline->type == TOK_WHITESPACE)
1940 tline = tline->next;
1941 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1942 ArgOffset = offset;
1943 free_tlist(origline);
1944 return DIRECTIVE_FOUND;
1946 case PP_LOCAL:
1947 /* TASM like LOCAL directive to define local variables for a
1948 * function, in the following form:
1950 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1952 * The '= LocalSize' at the end is ignored by NASM, but is
1953 * required by TASM to define the local parameter size (and used
1954 * by the TASM macro package).
1956 offset = LocalOffset;
1957 do {
1958 char *local, directive[256];
1959 int size = StackSize;
1961 /* Find the argument name */
1962 tline = tline->next;
1963 if (tline && tline->type == TOK_WHITESPACE)
1964 tline = tline->next;
1965 if (!tline || tline->type != TOK_ID) {
1966 error(ERR_NONFATAL,
1967 "`%%local' missing argument parameter");
1968 free_tlist(origline);
1969 return DIRECTIVE_FOUND;
1971 local = tline->text;
1973 /* Find the argument size type */
1974 tline = tline->next;
1975 if (!tline || tline->type != TOK_OTHER
1976 || tline->text[0] != ':') {
1977 error(ERR_NONFATAL,
1978 "Syntax error processing `%%local' directive");
1979 free_tlist(origline);
1980 return DIRECTIVE_FOUND;
1982 tline = tline->next;
1983 if (!tline || tline->type != TOK_ID) {
1984 error(ERR_NONFATAL,
1985 "`%%local' missing size type parameter");
1986 free_tlist(origline);
1987 return DIRECTIVE_FOUND;
1990 /* Allow macro expansion of type parameter */
1991 tt = tokenize(tline->text);
1992 tt = expand_smacro(tt);
1993 size = parse_size(tt->text);
1994 if (!size) {
1995 error(ERR_NONFATAL,
1996 "Invalid size type for `%%local' missing directive");
1997 free_tlist(tt);
1998 free_tlist(origline);
1999 return DIRECTIVE_FOUND;
2001 free_tlist(tt);
2003 /* Round up to even stack slots */
2004 size = (size+StackSize-1) & ~(StackSize-1);
2006 offset += size; /* Negative offset, increment before */
2008 /* Now define the macro for the argument */
2009 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2010 local, StackPointer, offset);
2011 do_directive(tokenize(directive));
2013 /* Now define the assign to setup the enter_c macro correctly */
2014 snprintf(directive, sizeof(directive),
2015 "%%assign %%$localsize %%$localsize+%d", size);
2016 do_directive(tokenize(directive));
2018 /* Move to the next argument in the list */
2019 tline = tline->next;
2020 if (tline && tline->type == TOK_WHITESPACE)
2021 tline = tline->next;
2022 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2023 LocalOffset = offset;
2024 free_tlist(origline);
2025 return DIRECTIVE_FOUND;
2027 case PP_CLEAR:
2028 if (tline->next)
2029 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2030 free_macros();
2031 init_macros();
2032 free_tlist(origline);
2033 return DIRECTIVE_FOUND;
2035 case PP_INCLUDE:
2036 tline = tline->next;
2037 skip_white_(tline);
2038 if (!tline || (tline->type != TOK_STRING &&
2039 tline->type != TOK_INTERNAL_STRING)) {
2040 error(ERR_NONFATAL, "`%%include' expects a file name");
2041 free_tlist(origline);
2042 return DIRECTIVE_FOUND; /* but we did _something_ */
2044 if (tline->next)
2045 error(ERR_WARNING,
2046 "trailing garbage after `%%include' ignored");
2047 if (tline->type != TOK_INTERNAL_STRING) {
2048 p = tline->text + 1; /* point past the quote to the name */
2049 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2050 } else
2051 p = tline->text; /* internal_string is easier */
2052 expand_macros_in_string(&p);
2053 inc = nasm_malloc(sizeof(Include));
2054 inc->next = istk;
2055 inc->conds = NULL;
2056 inc->fp = inc_fopen(p);
2057 if (!inc->fp && pass == 0) {
2058 /* -MG given but file not found */
2059 nasm_free(inc);
2060 } else {
2061 inc->fname = src_set_fname(p);
2062 inc->lineno = src_set_linnum(0);
2063 inc->lineinc = 1;
2064 inc->expansion = NULL;
2065 inc->mstk = NULL;
2066 istk = inc;
2067 list->uplevel(LIST_INCLUDE);
2069 free_tlist(origline);
2070 return DIRECTIVE_FOUND;
2072 case PP_PUSH:
2073 tline = tline->next;
2074 skip_white_(tline);
2075 tline = expand_id(tline);
2076 if (!tok_type_(tline, TOK_ID)) {
2077 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2078 free_tlist(origline);
2079 return DIRECTIVE_FOUND; /* but we did _something_ */
2081 if (tline->next)
2082 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2083 ctx = nasm_malloc(sizeof(Context));
2084 ctx->next = cstk;
2085 ctx->localmac = hash_init(HASH_SMALL);
2086 ctx->name = nasm_strdup(tline->text);
2087 ctx->number = unique++;
2088 cstk = ctx;
2089 free_tlist(origline);
2090 break;
2092 case PP_REPL:
2093 tline = tline->next;
2094 skip_white_(tline);
2095 tline = expand_id(tline);
2096 if (!tok_type_(tline, TOK_ID)) {
2097 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2098 free_tlist(origline);
2099 return DIRECTIVE_FOUND; /* but we did _something_ */
2101 if (tline->next)
2102 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2103 if (!cstk)
2104 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2105 else {
2106 nasm_free(cstk->name);
2107 cstk->name = nasm_strdup(tline->text);
2109 free_tlist(origline);
2110 break;
2112 case PP_POP:
2113 if (tline->next)
2114 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2115 if (!cstk)
2116 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2117 else
2118 ctx_pop();
2119 free_tlist(origline);
2120 break;
2122 case PP_ERROR:
2123 tline->next = expand_smacro(tline->next);
2124 tline = tline->next;
2125 skip_white_(tline);
2126 if (tok_type_(tline, TOK_STRING)) {
2127 p = tline->text + 1; /* point past the quote to the name */
2128 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2129 expand_macros_in_string(&p);
2130 error(ERR_NONFATAL, "%s", p);
2131 nasm_free(p);
2132 } else {
2133 p = detoken(tline, false);
2134 error(ERR_WARNING, "%s", p);
2135 nasm_free(p);
2137 free_tlist(origline);
2138 break;
2140 CASE_PP_IF:
2141 if (istk->conds && !emitting(istk->conds->state))
2142 j = COND_NEVER;
2143 else {
2144 j = if_condition(tline->next, i);
2145 tline->next = NULL; /* it got freed */
2146 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2148 cond = nasm_malloc(sizeof(Cond));
2149 cond->next = istk->conds;
2150 cond->state = j;
2151 istk->conds = cond;
2152 free_tlist(origline);
2153 return DIRECTIVE_FOUND;
2155 CASE_PP_ELIF:
2156 if (!istk->conds)
2157 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2158 if (emitting(istk->conds->state)
2159 || istk->conds->state == COND_NEVER)
2160 istk->conds->state = COND_NEVER;
2161 else {
2163 * IMPORTANT: In the case of %if, we will already have
2164 * called expand_mmac_params(); however, if we're
2165 * processing an %elif we must have been in a
2166 * non-emitting mode, which would have inhibited
2167 * the normal invocation of expand_mmac_params(). Therefore,
2168 * we have to do it explicitly here.
2170 j = if_condition(expand_mmac_params(tline->next), i);
2171 tline->next = NULL; /* it got freed */
2172 istk->conds->state =
2173 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2175 free_tlist(origline);
2176 return DIRECTIVE_FOUND;
2178 case PP_ELSE:
2179 if (tline->next)
2180 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2181 if (!istk->conds)
2182 error(ERR_FATAL, "`%%else': no matching `%%if'");
2183 if (emitting(istk->conds->state)
2184 || istk->conds->state == COND_NEVER)
2185 istk->conds->state = COND_ELSE_FALSE;
2186 else
2187 istk->conds->state = COND_ELSE_TRUE;
2188 free_tlist(origline);
2189 return DIRECTIVE_FOUND;
2191 case PP_ENDIF:
2192 if (tline->next)
2193 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2194 if (!istk->conds)
2195 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2196 cond = istk->conds;
2197 istk->conds = cond->next;
2198 nasm_free(cond);
2199 free_tlist(origline);
2200 return DIRECTIVE_FOUND;
2202 case PP_MACRO:
2203 case PP_IMACRO:
2204 if (defining)
2205 error(ERR_FATAL,
2206 "`%%%smacro': already defining a macro",
2207 (i == PP_IMACRO ? "i" : ""));
2208 tline = tline->next;
2209 skip_white_(tline);
2210 tline = expand_id(tline);
2211 if (!tok_type_(tline, TOK_ID)) {
2212 error(ERR_NONFATAL,
2213 "`%%%smacro' expects a macro name",
2214 (i == PP_IMACRO ? "i" : ""));
2215 return DIRECTIVE_FOUND;
2217 defining = nasm_malloc(sizeof(MMacro));
2218 defining->name = nasm_strdup(tline->text);
2219 defining->casesense = (i == PP_MACRO);
2220 defining->plus = false;
2221 defining->nolist = false;
2222 defining->in_progress = 0;
2223 defining->rep_nest = NULL;
2224 tline = expand_smacro(tline->next);
2225 skip_white_(tline);
2226 if (!tok_type_(tline, TOK_NUMBER)) {
2227 error(ERR_NONFATAL,
2228 "`%%%smacro' expects a parameter count",
2229 (i == PP_IMACRO ? "i" : ""));
2230 defining->nparam_min = defining->nparam_max = 0;
2231 } else {
2232 defining->nparam_min = defining->nparam_max =
2233 readnum(tline->text, &err);
2234 if (err)
2235 error(ERR_NONFATAL,
2236 "unable to parse parameter count `%s'", tline->text);
2238 if (tline && tok_is_(tline->next, "-")) {
2239 tline = tline->next->next;
2240 if (tok_is_(tline, "*"))
2241 defining->nparam_max = INT_MAX;
2242 else if (!tok_type_(tline, TOK_NUMBER))
2243 error(ERR_NONFATAL,
2244 "`%%%smacro' expects a parameter count after `-'",
2245 (i == PP_IMACRO ? "i" : ""));
2246 else {
2247 defining->nparam_max = readnum(tline->text, &err);
2248 if (err)
2249 error(ERR_NONFATAL,
2250 "unable to parse parameter count `%s'",
2251 tline->text);
2252 if (defining->nparam_min > defining->nparam_max)
2253 error(ERR_NONFATAL,
2254 "minimum parameter count exceeds maximum");
2257 if (tline && tok_is_(tline->next, "+")) {
2258 tline = tline->next;
2259 defining->plus = true;
2261 if (tline && tok_type_(tline->next, TOK_ID) &&
2262 !nasm_stricmp(tline->next->text, ".nolist")) {
2263 tline = tline->next;
2264 defining->nolist = true;
2266 mmac = (MMacro *) hash_findix(mmacros, defining->name);
2267 while (mmac) {
2268 if (!strcmp(mmac->name, defining->name) &&
2269 (mmac->nparam_min <= defining->nparam_max
2270 || defining->plus)
2271 && (defining->nparam_min <= mmac->nparam_max
2272 || mmac->plus)) {
2273 error(ERR_WARNING,
2274 "redefining multi-line macro `%s'", defining->name);
2275 break;
2277 mmac = mmac->next;
2280 * Handle default parameters.
2282 if (tline && tline->next) {
2283 defining->dlist = tline->next;
2284 tline->next = NULL;
2285 count_mmac_params(defining->dlist, &defining->ndefs,
2286 &defining->defaults);
2287 } else {
2288 defining->dlist = NULL;
2289 defining->defaults = NULL;
2291 defining->expansion = NULL;
2292 free_tlist(origline);
2293 return DIRECTIVE_FOUND;
2295 case PP_ENDM:
2296 case PP_ENDMACRO:
2297 if (!defining) {
2298 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2299 return DIRECTIVE_FOUND;
2301 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2302 defining->next = *mmhead;
2303 *mmhead = defining;
2304 defining = NULL;
2305 free_tlist(origline);
2306 return DIRECTIVE_FOUND;
2308 case PP_ROTATE:
2309 if (tline->next && tline->next->type == TOK_WHITESPACE)
2310 tline = tline->next;
2311 if (tline->next == NULL) {
2312 free_tlist(origline);
2313 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2314 return DIRECTIVE_FOUND;
2316 t = expand_smacro(tline->next);
2317 tline->next = NULL;
2318 free_tlist(origline);
2319 tline = t;
2320 tptr = &t;
2321 tokval.t_type = TOKEN_INVALID;
2322 evalresult =
2323 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2324 free_tlist(tline);
2325 if (!evalresult)
2326 return DIRECTIVE_FOUND;
2327 if (tokval.t_type)
2328 error(ERR_WARNING,
2329 "trailing garbage after expression ignored");
2330 if (!is_simple(evalresult)) {
2331 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2332 return DIRECTIVE_FOUND;
2334 mmac = istk->mstk;
2335 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2336 mmac = mmac->next_active;
2337 if (!mmac) {
2338 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2339 } else if (mmac->nparam == 0) {
2340 error(ERR_NONFATAL,
2341 "`%%rotate' invoked within macro without parameters");
2342 } else {
2343 int rotate = mmac->rotate + reloc_value(evalresult);
2345 rotate %= (int)mmac->nparam;
2346 if (rotate < 0)
2347 rotate += mmac->nparam;
2349 mmac->rotate = rotate;
2351 return DIRECTIVE_FOUND;
2353 case PP_REP:
2354 nolist = false;
2355 do {
2356 tline = tline->next;
2357 } while (tok_type_(tline, TOK_WHITESPACE));
2359 if (tok_type_(tline, TOK_ID) &&
2360 nasm_stricmp(tline->text, ".nolist") == 0) {
2361 nolist = true;
2362 do {
2363 tline = tline->next;
2364 } while (tok_type_(tline, TOK_WHITESPACE));
2367 if (tline) {
2368 t = expand_smacro(tline);
2369 tptr = &t;
2370 tokval.t_type = TOKEN_INVALID;
2371 evalresult =
2372 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2373 if (!evalresult) {
2374 free_tlist(origline);
2375 return DIRECTIVE_FOUND;
2377 if (tokval.t_type)
2378 error(ERR_WARNING,
2379 "trailing garbage after expression ignored");
2380 if (!is_simple(evalresult)) {
2381 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2382 return DIRECTIVE_FOUND;
2384 count = reloc_value(evalresult) + 1;
2385 } else {
2386 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2387 count = 0;
2389 free_tlist(origline);
2391 tmp_defining = defining;
2392 defining = nasm_malloc(sizeof(MMacro));
2393 defining->name = NULL; /* flags this macro as a %rep block */
2394 defining->casesense = false;
2395 defining->plus = false;
2396 defining->nolist = nolist;
2397 defining->in_progress = count;
2398 defining->nparam_min = defining->nparam_max = 0;
2399 defining->defaults = NULL;
2400 defining->dlist = NULL;
2401 defining->expansion = NULL;
2402 defining->next_active = istk->mstk;
2403 defining->rep_nest = tmp_defining;
2404 return DIRECTIVE_FOUND;
2406 case PP_ENDREP:
2407 if (!defining || defining->name) {
2408 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2409 return DIRECTIVE_FOUND;
2413 * Now we have a "macro" defined - although it has no name
2414 * and we won't be entering it in the hash tables - we must
2415 * push a macro-end marker for it on to istk->expansion.
2416 * After that, it will take care of propagating itself (a
2417 * macro-end marker line for a macro which is really a %rep
2418 * block will cause the macro to be re-expanded, complete
2419 * with another macro-end marker to ensure the process
2420 * continues) until the whole expansion is forcibly removed
2421 * from istk->expansion by a %exitrep.
2423 l = nasm_malloc(sizeof(Line));
2424 l->next = istk->expansion;
2425 l->finishes = defining;
2426 l->first = NULL;
2427 istk->expansion = l;
2429 istk->mstk = defining;
2431 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2432 tmp_defining = defining;
2433 defining = defining->rep_nest;
2434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
2437 case PP_EXITREP:
2439 * We must search along istk->expansion until we hit a
2440 * macro-end marker for a macro with no name. Then we set
2441 * its `in_progress' flag to 0.
2443 for (l = istk->expansion; l; l = l->next)
2444 if (l->finishes && !l->finishes->name)
2445 break;
2447 if (l)
2448 l->finishes->in_progress = 0;
2449 else
2450 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
2454 case PP_XDEFINE:
2455 case PP_IXDEFINE:
2456 case PP_DEFINE:
2457 case PP_IDEFINE:
2458 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2460 tline = tline->next;
2461 skip_white_(tline);
2462 tline = expand_id(tline);
2463 if (!tline || (tline->type != TOK_ID &&
2464 (tline->type != TOK_PREPROC_ID ||
2465 tline->text[1] != '$'))) {
2466 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2467 pp_directives[i]);
2468 free_tlist(origline);
2469 return DIRECTIVE_FOUND;
2472 ctx = get_ctx(tline->text, false);
2474 mname = tline->text;
2475 last = tline;
2476 param_start = tline = tline->next;
2477 nparam = 0;
2479 /* Expand the macro definition now for %xdefine and %ixdefine */
2480 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2481 tline = expand_smacro(tline);
2483 if (tok_is_(tline, "(")) {
2485 * This macro has parameters.
2488 tline = tline->next;
2489 while (1) {
2490 skip_white_(tline);
2491 if (!tline) {
2492 error(ERR_NONFATAL, "parameter identifier expected");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2496 if (tline->type != TOK_ID) {
2497 error(ERR_NONFATAL,
2498 "`%s': parameter identifier expected",
2499 tline->text);
2500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2503 tline->type = TOK_SMAC_PARAM + nparam++;
2504 tline = tline->next;
2505 skip_white_(tline);
2506 if (tok_is_(tline, ",")) {
2507 tline = tline->next;
2508 continue;
2510 if (!tok_is_(tline, ")")) {
2511 error(ERR_NONFATAL,
2512 "`)' expected to terminate macro template");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
2516 break;
2518 last = tline;
2519 tline = tline->next;
2521 if (tok_type_(tline, TOK_WHITESPACE))
2522 last = tline, tline = tline->next;
2523 macro_start = NULL;
2524 last->next = NULL;
2525 t = tline;
2526 while (t) {
2527 if (t->type == TOK_ID) {
2528 for (tt = param_start; tt; tt = tt->next)
2529 if (tt->type >= TOK_SMAC_PARAM &&
2530 !strcmp(tt->text, t->text))
2531 t->type = tt->type;
2533 tt = t->next;
2534 t->next = macro_start;
2535 macro_start = t;
2536 t = tt;
2539 * Good. We now have a macro name, a parameter count, and a
2540 * token list (in reverse order) for an expansion. We ought
2541 * to be OK just to create an SMacro, store it, and let
2542 * free_tlist have the rest of the line (which we have
2543 * carefully re-terminated after chopping off the expansion
2544 * from the end).
2546 define_smacro(ctx, mname, casesense, nparam, macro_start);
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
2550 case PP_UNDEF:
2551 tline = tline->next;
2552 skip_white_(tline);
2553 tline = expand_id(tline);
2554 if (!tline || (tline->type != TOK_ID &&
2555 (tline->type != TOK_PREPROC_ID ||
2556 tline->text[1] != '$'))) {
2557 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2561 if (tline->next) {
2562 error(ERR_WARNING,
2563 "trailing garbage after macro name ignored");
2566 /* Find the context that symbol belongs to */
2567 ctx = get_ctx(tline->text, false);
2568 undef_smacro(ctx, tline->text);
2569 free_tlist(origline);
2570 return DIRECTIVE_FOUND;
2572 case PP_STRLEN:
2573 casesense = true;
2575 tline = tline->next;
2576 skip_white_(tline);
2577 tline = expand_id(tline);
2578 if (!tline || (tline->type != TOK_ID &&
2579 (tline->type != TOK_PREPROC_ID ||
2580 tline->text[1] != '$'))) {
2581 error(ERR_NONFATAL,
2582 "`%%strlen' expects a macro identifier as first parameter");
2583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
2586 ctx = get_ctx(tline->text, false);
2588 mname = tline->text;
2589 last = tline;
2590 tline = expand_smacro(tline->next);
2591 last->next = NULL;
2593 t = tline;
2594 while (tok_type_(t, TOK_WHITESPACE))
2595 t = t->next;
2596 /* t should now point to the string */
2597 if (t->type != TOK_STRING) {
2598 error(ERR_NONFATAL,
2599 "`%%strlen` requires string as second parameter");
2600 free_tlist(tline);
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
2605 macro_start = nasm_malloc(sizeof(*macro_start));
2606 macro_start->next = NULL;
2607 make_tok_num(macro_start, strlen(t->text) - 2);
2608 macro_start->mac = NULL;
2611 * We now have a macro name, an implicit parameter count of
2612 * zero, and a numeric token to use as an expansion. Create
2613 * and store an SMacro.
2615 define_smacro(ctx, mname, casesense, 0, macro_start);
2616 free_tlist(tline);
2617 free_tlist(origline);
2618 return DIRECTIVE_FOUND;
2620 case PP_SUBSTR:
2621 casesense = true;
2623 tline = tline->next;
2624 skip_white_(tline);
2625 tline = expand_id(tline);
2626 if (!tline || (tline->type != TOK_ID &&
2627 (tline->type != TOK_PREPROC_ID ||
2628 tline->text[1] != '$'))) {
2629 error(ERR_NONFATAL,
2630 "`%%substr' expects a macro identifier as first parameter");
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
2634 ctx = get_ctx(tline->text, false);
2636 mname = tline->text;
2637 last = tline;
2638 tline = expand_smacro(tline->next);
2639 last->next = NULL;
2641 t = tline->next;
2642 while (tok_type_(t, TOK_WHITESPACE))
2643 t = t->next;
2645 /* t should now point to the string */
2646 if (t->type != TOK_STRING) {
2647 error(ERR_NONFATAL,
2648 "`%%substr` requires string as second parameter");
2649 free_tlist(tline);
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2654 tt = t->next;
2655 tptr = &tt;
2656 tokval.t_type = TOKEN_INVALID;
2657 evalresult =
2658 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2659 if (!evalresult) {
2660 free_tlist(tline);
2661 free_tlist(origline);
2662 return DIRECTIVE_FOUND;
2664 if (!is_simple(evalresult)) {
2665 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2666 free_tlist(tline);
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2671 macro_start = nasm_malloc(sizeof(*macro_start));
2672 macro_start->next = NULL;
2673 macro_start->text = nasm_strdup("'''");
2674 if (evalresult->value > 0
2675 && evalresult->value < (int) strlen(t->text) - 1) {
2676 macro_start->text[1] = t->text[evalresult->value];
2677 } else {
2678 macro_start->text[2] = '\0';
2680 macro_start->type = TOK_STRING;
2681 macro_start->mac = NULL;
2684 * We now have a macro name, an implicit parameter count of
2685 * zero, and a numeric token to use as an expansion. Create
2686 * and store an SMacro.
2688 define_smacro(ctx, mname, casesense, 0, macro_start);
2689 free_tlist(tline);
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND;
2693 case PP_ASSIGN:
2694 case PP_IASSIGN:
2695 casesense = (i == PP_ASSIGN);
2697 tline = tline->next;
2698 skip_white_(tline);
2699 tline = expand_id(tline);
2700 if (!tline || (tline->type != TOK_ID &&
2701 (tline->type != TOK_PREPROC_ID ||
2702 tline->text[1] != '$'))) {
2703 error(ERR_NONFATAL,
2704 "`%%%sassign' expects a macro identifier",
2705 (i == PP_IASSIGN ? "i" : ""));
2706 free_tlist(origline);
2707 return DIRECTIVE_FOUND;
2709 ctx = get_ctx(tline->text, false);
2711 mname = tline->text;
2712 last = tline;
2713 tline = expand_smacro(tline->next);
2714 last->next = NULL;
2716 t = tline;
2717 tptr = &t;
2718 tokval.t_type = TOKEN_INVALID;
2719 evalresult =
2720 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2721 free_tlist(tline);
2722 if (!evalresult) {
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
2727 if (tokval.t_type)
2728 error(ERR_WARNING,
2729 "trailing garbage after expression ignored");
2731 if (!is_simple(evalresult)) {
2732 error(ERR_NONFATAL,
2733 "non-constant value given to `%%%sassign'",
2734 (i == PP_IASSIGN ? "i" : ""));
2735 free_tlist(origline);
2736 return DIRECTIVE_FOUND;
2739 macro_start = nasm_malloc(sizeof(*macro_start));
2740 macro_start->next = NULL;
2741 make_tok_num(macro_start, reloc_value(evalresult));
2742 macro_start->mac = NULL;
2745 * We now have a macro name, an implicit parameter count of
2746 * zero, and a numeric token to use as an expansion. Create
2747 * and store an SMacro.
2749 define_smacro(ctx, mname, casesense, 0, macro_start);
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
2753 case PP_LINE:
2755 * Syntax is `%line nnn[+mmm] [filename]'
2757 tline = tline->next;
2758 skip_white_(tline);
2759 if (!tok_type_(tline, TOK_NUMBER)) {
2760 error(ERR_NONFATAL, "`%%line' expects line number");
2761 free_tlist(origline);
2762 return DIRECTIVE_FOUND;
2764 k = readnum(tline->text, &err);
2765 m = 1;
2766 tline = tline->next;
2767 if (tok_is_(tline, "+")) {
2768 tline = tline->next;
2769 if (!tok_type_(tline, TOK_NUMBER)) {
2770 error(ERR_NONFATAL, "`%%line' expects line increment");
2771 free_tlist(origline);
2772 return DIRECTIVE_FOUND;
2774 m = readnum(tline->text, &err);
2775 tline = tline->next;
2777 skip_white_(tline);
2778 src_set_linnum(k);
2779 istk->lineinc = m;
2780 if (tline) {
2781 nasm_free(src_set_fname(detoken(tline, false)));
2783 free_tlist(origline);
2784 return DIRECTIVE_FOUND;
2786 default:
2787 error(ERR_FATAL,
2788 "preprocessor directive `%s' not yet implemented",
2789 pp_directives[i]);
2790 break;
2792 return DIRECTIVE_FOUND;
2796 * Ensure that a macro parameter contains a condition code and
2797 * nothing else. Return the condition code index if so, or -1
2798 * otherwise.
2800 static int find_cc(Token * t)
2802 Token *tt;
2803 int i, j, k, m;
2805 if (!t)
2806 return -1; /* Probably a %+ without a space */
2808 skip_white_(t);
2809 if (t->type != TOK_ID)
2810 return -1;
2811 tt = t->next;
2812 skip_white_(tt);
2813 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2814 return -1;
2816 i = -1;
2817 j = elements(conditions);
2818 while (j - i > 1) {
2819 k = (j + i) / 2;
2820 m = nasm_stricmp(t->text, conditions[k]);
2821 if (m == 0) {
2822 i = k;
2823 j = -2;
2824 break;
2825 } else if (m < 0) {
2826 j = k;
2827 } else
2828 i = k;
2830 if (j != -2)
2831 return -1;
2832 return i;
2836 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2837 * %-n) and MMacro-local identifiers (%%foo).
2839 static Token *expand_mmac_params(Token * tline)
2841 Token *t, *tt, **tail, *thead;
2843 tail = &thead;
2844 thead = NULL;
2846 while (tline) {
2847 if (tline->type == TOK_PREPROC_ID &&
2848 (((tline->text[1] == '+' || tline->text[1] == '-')
2849 && tline->text[2]) || tline->text[1] == '%'
2850 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2851 char *text = NULL;
2852 int type = 0, cc; /* type = 0 to placate optimisers */
2853 char tmpbuf[30];
2854 unsigned int n;
2855 int i;
2856 MMacro *mac;
2858 t = tline;
2859 tline = tline->next;
2861 mac = istk->mstk;
2862 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2863 mac = mac->next_active;
2864 if (!mac)
2865 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2866 else
2867 switch (t->text[1]) {
2869 * We have to make a substitution of one of the
2870 * forms %1, %-1, %+1, %%foo, %0.
2872 case '0':
2873 type = TOK_NUMBER;
2874 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2875 text = nasm_strdup(tmpbuf);
2876 break;
2877 case '%':
2878 type = TOK_ID;
2879 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2880 mac->unique);
2881 text = nasm_strcat(tmpbuf, t->text + 2);
2882 break;
2883 case '-':
2884 n = atoi(t->text + 2) - 1;
2885 if (n >= mac->nparam)
2886 tt = NULL;
2887 else {
2888 if (mac->nparam > 1)
2889 n = (n + mac->rotate) % mac->nparam;
2890 tt = mac->params[n];
2892 cc = find_cc(tt);
2893 if (cc == -1) {
2894 error(ERR_NONFATAL,
2895 "macro parameter %d is not a condition code",
2896 n + 1);
2897 text = NULL;
2898 } else {
2899 type = TOK_ID;
2900 if (inverse_ccs[cc] == -1) {
2901 error(ERR_NONFATAL,
2902 "condition code `%s' is not invertible",
2903 conditions[cc]);
2904 text = NULL;
2905 } else
2906 text =
2907 nasm_strdup(conditions[inverse_ccs[cc]]);
2909 break;
2910 case '+':
2911 n = atoi(t->text + 2) - 1;
2912 if (n >= mac->nparam)
2913 tt = NULL;
2914 else {
2915 if (mac->nparam > 1)
2916 n = (n + mac->rotate) % mac->nparam;
2917 tt = mac->params[n];
2919 cc = find_cc(tt);
2920 if (cc == -1) {
2921 error(ERR_NONFATAL,
2922 "macro parameter %d is not a condition code",
2923 n + 1);
2924 text = NULL;
2925 } else {
2926 type = TOK_ID;
2927 text = nasm_strdup(conditions[cc]);
2929 break;
2930 default:
2931 n = atoi(t->text + 1) - 1;
2932 if (n >= mac->nparam)
2933 tt = NULL;
2934 else {
2935 if (mac->nparam > 1)
2936 n = (n + mac->rotate) % mac->nparam;
2937 tt = mac->params[n];
2939 if (tt) {
2940 for (i = 0; i < mac->paramlen[n]; i++) {
2941 *tail = new_Token(NULL, tt->type, tt->text, 0);
2942 tail = &(*tail)->next;
2943 tt = tt->next;
2946 text = NULL; /* we've done it here */
2947 break;
2949 if (!text) {
2950 delete_Token(t);
2951 } else {
2952 *tail = t;
2953 tail = &t->next;
2954 t->type = type;
2955 nasm_free(t->text);
2956 t->text = text;
2957 t->mac = NULL;
2959 continue;
2960 } else {
2961 t = *tail = tline;
2962 tline = tline->next;
2963 t->mac = NULL;
2964 tail = &t->next;
2967 *tail = NULL;
2968 t = thead;
2969 for (; t && (tt = t->next) != NULL; t = t->next)
2970 switch (t->type) {
2971 case TOK_WHITESPACE:
2972 if (tt->type == TOK_WHITESPACE) {
2973 t->next = delete_Token(tt);
2975 break;
2976 case TOK_ID:
2977 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2978 char *tmp = nasm_strcat(t->text, tt->text);
2979 nasm_free(t->text);
2980 t->text = tmp;
2981 t->next = delete_Token(tt);
2983 break;
2984 case TOK_NUMBER:
2985 if (tt->type == TOK_NUMBER) {
2986 char *tmp = nasm_strcat(t->text, tt->text);
2987 nasm_free(t->text);
2988 t->text = tmp;
2989 t->next = delete_Token(tt);
2991 break;
2992 default:
2993 break;
2996 return thead;
3000 * Expand all single-line macro calls made in the given line.
3001 * Return the expanded version of the line. The original is deemed
3002 * to be destroyed in the process. (In reality we'll just move
3003 * Tokens from input to output a lot of the time, rather than
3004 * actually bothering to destroy and replicate.)
3006 #define DEADMAN_LIMIT (1 << 20)
3008 static Token *expand_smacro(Token * tline)
3010 Token *t, *tt, *mstart, **tail, *thead;
3011 SMacro *head = NULL, *m;
3012 Token **params;
3013 int *paramsize;
3014 unsigned int nparam, sparam;
3015 int brackets, rescan;
3016 Token *org_tline = tline;
3017 Context *ctx;
3018 char *mname;
3019 int deadman = DEADMAN_LIMIT;
3022 * Trick: we should avoid changing the start token pointer since it can
3023 * be contained in "next" field of other token. Because of this
3024 * we allocate a copy of first token and work with it; at the end of
3025 * routine we copy it back
3027 if (org_tline) {
3028 tline =
3029 new_Token(org_tline->next, org_tline->type, org_tline->text,
3031 tline->mac = org_tline->mac;
3032 nasm_free(org_tline->text);
3033 org_tline->text = NULL;
3036 again:
3037 tail = &thead;
3038 thead = NULL;
3040 while (tline) { /* main token loop */
3041 if (!--deadman) {
3042 error(ERR_NONFATAL, "interminable macro recursion");
3043 break;
3046 if ((mname = tline->text)) {
3047 /* if this token is a local macro, look in local context */
3048 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3049 ctx = get_ctx(mname, true);
3050 else
3051 ctx = NULL;
3053 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3054 mname);
3057 * We've hit an identifier. As in is_mmacro below, we first
3058 * check whether the identifier is a single-line macro at
3059 * all, then think about checking for parameters if
3060 * necessary.
3062 for (m = head; m; m = m->next)
3063 if (!mstrcmp(m->name, mname, m->casesense))
3064 break;
3065 if (m) {
3066 mstart = tline;
3067 params = NULL;
3068 paramsize = NULL;
3069 if (m->nparam == 0) {
3071 * Simple case: the macro is parameterless. Discard the
3072 * one token that the macro call took, and push the
3073 * expansion back on the to-do stack.
3075 if (!m->expansion) {
3076 if (!strcmp("__FILE__", m->name)) {
3077 int32_t num = 0;
3078 src_get(&num, &(tline->text));
3079 nasm_quote(&(tline->text));
3080 tline->type = TOK_STRING;
3081 continue;
3083 if (!strcmp("__LINE__", m->name)) {
3084 nasm_free(tline->text);
3085 make_tok_num(tline, src_get_linnum());
3086 continue;
3088 if (!strcmp("__BITS__", m->name)) {
3089 nasm_free(tline->text);
3090 make_tok_num(tline, globalbits);
3091 continue;
3093 tline = delete_Token(tline);
3094 continue;
3096 } else {
3098 * Complicated case: at least one macro with this name
3099 * exists and takes parameters. We must find the
3100 * parameters in the call, count them, find the SMacro
3101 * that corresponds to that form of the macro call, and
3102 * substitute for the parameters when we expand. What a
3103 * pain.
3105 /*tline = tline->next;
3106 skip_white_(tline); */
3107 do {
3108 t = tline->next;
3109 while (tok_type_(t, TOK_SMAC_END)) {
3110 t->mac->in_progress = false;
3111 t->text = NULL;
3112 t = tline->next = delete_Token(t);
3114 tline = t;
3115 } while (tok_type_(tline, TOK_WHITESPACE));
3116 if (!tok_is_(tline, "(")) {
3118 * This macro wasn't called with parameters: ignore
3119 * the call. (Behaviour borrowed from gnu cpp.)
3121 tline = mstart;
3122 m = NULL;
3123 } else {
3124 int paren = 0;
3125 int white = 0;
3126 brackets = 0;
3127 nparam = 0;
3128 sparam = PARAM_DELTA;
3129 params = nasm_malloc(sparam * sizeof(Token *));
3130 params[0] = tline->next;
3131 paramsize = nasm_malloc(sparam * sizeof(int));
3132 paramsize[0] = 0;
3133 while (true) { /* parameter loop */
3135 * For some unusual expansions
3136 * which concatenates function call
3138 t = tline->next;
3139 while (tok_type_(t, TOK_SMAC_END)) {
3140 t->mac->in_progress = false;
3141 t->text = NULL;
3142 t = tline->next = delete_Token(t);
3144 tline = t;
3146 if (!tline) {
3147 error(ERR_NONFATAL,
3148 "macro call expects terminating `)'");
3149 break;
3151 if (tline->type == TOK_WHITESPACE
3152 && brackets <= 0) {
3153 if (paramsize[nparam])
3154 white++;
3155 else
3156 params[nparam] = tline->next;
3157 continue; /* parameter loop */
3159 if (tline->type == TOK_OTHER
3160 && tline->text[1] == 0) {
3161 char ch = tline->text[0];
3162 if (ch == ',' && !paren && brackets <= 0) {
3163 if (++nparam >= sparam) {
3164 sparam += PARAM_DELTA;
3165 params = nasm_realloc(params,
3166 sparam *
3167 sizeof(Token
3168 *));
3169 paramsize =
3170 nasm_realloc(paramsize,
3171 sparam *
3172 sizeof(int));
3174 params[nparam] = tline->next;
3175 paramsize[nparam] = 0;
3176 white = 0;
3177 continue; /* parameter loop */
3179 if (ch == '{' &&
3180 (brackets > 0 || (brackets == 0 &&
3181 !paramsize[nparam])))
3183 if (!(brackets++)) {
3184 params[nparam] = tline->next;
3185 continue; /* parameter loop */
3188 if (ch == '}' && brackets > 0)
3189 if (--brackets == 0) {
3190 brackets = -1;
3191 continue; /* parameter loop */
3193 if (ch == '(' && !brackets)
3194 paren++;
3195 if (ch == ')' && brackets <= 0)
3196 if (--paren < 0)
3197 break;
3199 if (brackets < 0) {
3200 brackets = 0;
3201 error(ERR_NONFATAL, "braces do not "
3202 "enclose all of macro parameter");
3204 paramsize[nparam] += white + 1;
3205 white = 0;
3206 } /* parameter loop */
3207 nparam++;
3208 while (m && (m->nparam != nparam ||
3209 mstrcmp(m->name, mname,
3210 m->casesense)))
3211 m = m->next;
3212 if (!m)
3213 error(ERR_WARNING | ERR_WARN_MNP,
3214 "macro `%s' exists, "
3215 "but not taking %d parameters",
3216 mstart->text, nparam);
3219 if (m && m->in_progress)
3220 m = NULL;
3221 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3223 * Design question: should we handle !tline, which
3224 * indicates missing ')' here, or expand those
3225 * macros anyway, which requires the (t) test a few
3226 * lines down?
3228 nasm_free(params);
3229 nasm_free(paramsize);
3230 tline = mstart;
3231 } else {
3233 * Expand the macro: we are placed on the last token of the
3234 * call, so that we can easily split the call from the
3235 * following tokens. We also start by pushing an SMAC_END
3236 * token for the cycle removal.
3238 t = tline;
3239 if (t) {
3240 tline = t->next;
3241 t->next = NULL;
3243 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3244 tt->mac = m;
3245 m->in_progress = true;
3246 tline = tt;
3247 for (t = m->expansion; t; t = t->next) {
3248 if (t->type >= TOK_SMAC_PARAM) {
3249 Token *pcopy = tline, **ptail = &pcopy;
3250 Token *ttt, *pt;
3251 int i;
3253 ttt = params[t->type - TOK_SMAC_PARAM];
3254 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3255 --i >= 0;) {
3256 pt = *ptail =
3257 new_Token(tline, ttt->type, ttt->text,
3259 ptail = &pt->next;
3260 ttt = ttt->next;
3262 tline = pcopy;
3263 } else {
3264 tt = new_Token(tline, t->type, t->text, 0);
3265 tline = tt;
3270 * Having done that, get rid of the macro call, and clean
3271 * up the parameters.
3273 nasm_free(params);
3274 nasm_free(paramsize);
3275 free_tlist(mstart);
3276 continue; /* main token loop */
3281 if (tline->type == TOK_SMAC_END) {
3282 tline->mac->in_progress = false;
3283 tline = delete_Token(tline);
3284 } else {
3285 t = *tail = tline;
3286 tline = tline->next;
3287 t->mac = NULL;
3288 t->next = NULL;
3289 tail = &t->next;
3294 * Now scan the entire line and look for successive TOK_IDs that resulted
3295 * after expansion (they can't be produced by tokenize()). The successive
3296 * TOK_IDs should be concatenated.
3297 * Also we look for %+ tokens and concatenate the tokens before and after
3298 * them (without white spaces in between).
3300 t = thead;
3301 rescan = 0;
3302 while (t) {
3303 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3304 t = t->next;
3305 if (!t || !t->next)
3306 break;
3307 if (t->next->type == TOK_ID ||
3308 t->next->type == TOK_PREPROC_ID ||
3309 t->next->type == TOK_NUMBER) {
3310 char *p = nasm_strcat(t->text, t->next->text);
3311 nasm_free(t->text);
3312 t->next = delete_Token(t->next);
3313 t->text = p;
3314 rescan = 1;
3315 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3316 t->next->next->type == TOK_PREPROC_ID &&
3317 strcmp(t->next->next->text, "%+") == 0) {
3318 /* free the next whitespace, the %+ token and next whitespace */
3319 int i;
3320 for (i = 1; i <= 3; i++) {
3321 if (!t->next
3322 || (i != 2 && t->next->type != TOK_WHITESPACE))
3323 break;
3324 t->next = delete_Token(t->next);
3325 } /* endfor */
3326 } else
3327 t = t->next;
3329 /* If we concatenaded something, re-scan the line for macros */
3330 if (rescan) {
3331 tline = thead;
3332 goto again;
3335 if (org_tline) {
3336 if (thead) {
3337 *org_tline = *thead;
3338 /* since we just gave text to org_line, don't free it */
3339 thead->text = NULL;
3340 delete_Token(thead);
3341 } else {
3342 /* the expression expanded to empty line;
3343 we can't return NULL for some reasons
3344 we just set the line to a single WHITESPACE token. */
3345 memset(org_tline, 0, sizeof(*org_tline));
3346 org_tline->text = NULL;
3347 org_tline->type = TOK_WHITESPACE;
3349 thead = org_tline;
3352 return thead;
3356 * Similar to expand_smacro but used exclusively with macro identifiers
3357 * right before they are fetched in. The reason is that there can be
3358 * identifiers consisting of several subparts. We consider that if there
3359 * are more than one element forming the name, user wants a expansion,
3360 * otherwise it will be left as-is. Example:
3362 * %define %$abc cde
3364 * the identifier %$abc will be left as-is so that the handler for %define
3365 * will suck it and define the corresponding value. Other case:
3367 * %define _%$abc cde
3369 * In this case user wants name to be expanded *before* %define starts
3370 * working, so we'll expand %$abc into something (if it has a value;
3371 * otherwise it will be left as-is) then concatenate all successive
3372 * PP_IDs into one.
3374 static Token *expand_id(Token * tline)
3376 Token *cur, *oldnext = NULL;
3378 if (!tline || !tline->next)
3379 return tline;
3381 cur = tline;
3382 while (cur->next &&
3383 (cur->next->type == TOK_ID ||
3384 cur->next->type == TOK_PREPROC_ID
3385 || cur->next->type == TOK_NUMBER))
3386 cur = cur->next;
3388 /* If identifier consists of just one token, don't expand */
3389 if (cur == tline)
3390 return tline;
3392 if (cur) {
3393 oldnext = cur->next; /* Detach the tail past identifier */
3394 cur->next = NULL; /* so that expand_smacro stops here */
3397 tline = expand_smacro(tline);
3399 if (cur) {
3400 /* expand_smacro possibly changhed tline; re-scan for EOL */
3401 cur = tline;
3402 while (cur && cur->next)
3403 cur = cur->next;
3404 if (cur)
3405 cur->next = oldnext;
3408 return tline;
3412 * Determine whether the given line constitutes a multi-line macro
3413 * call, and return the MMacro structure called if so. Doesn't have
3414 * to check for an initial label - that's taken care of in
3415 * expand_mmacro - but must check numbers of parameters. Guaranteed
3416 * to be called with tline->type == TOK_ID, so the putative macro
3417 * name is easy to find.
3419 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3421 MMacro *head, *m;
3422 Token **params;
3423 int nparam;
3425 head = (MMacro *) hash_findix(mmacros, tline->text);
3428 * Efficiency: first we see if any macro exists with the given
3429 * name. If not, we can return NULL immediately. _Then_ we
3430 * count the parameters, and then we look further along the
3431 * list if necessary to find the proper MMacro.
3433 for (m = head; m; m = m->next)
3434 if (!mstrcmp(m->name, tline->text, m->casesense))
3435 break;
3436 if (!m)
3437 return NULL;
3440 * OK, we have a potential macro. Count and demarcate the
3441 * parameters.
3443 count_mmac_params(tline->next, &nparam, &params);
3446 * So we know how many parameters we've got. Find the MMacro
3447 * structure that handles this number.
3449 while (m) {
3450 if (m->nparam_min <= nparam
3451 && (m->plus || nparam <= m->nparam_max)) {
3453 * This one is right. Just check if cycle removal
3454 * prohibits us using it before we actually celebrate...
3456 if (m->in_progress) {
3457 #if 0
3458 error(ERR_NONFATAL,
3459 "self-reference in multi-line macro `%s'", m->name);
3460 #endif
3461 nasm_free(params);
3462 return NULL;
3465 * It's right, and we can use it. Add its default
3466 * parameters to the end of our list if necessary.
3468 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3469 params =
3470 nasm_realloc(params,
3471 ((m->nparam_min + m->ndefs +
3472 1) * sizeof(*params)));
3473 while (nparam < m->nparam_min + m->ndefs) {
3474 params[nparam] = m->defaults[nparam - m->nparam_min];
3475 nparam++;
3479 * If we've gone over the maximum parameter count (and
3480 * we're in Plus mode), ignore parameters beyond
3481 * nparam_max.
3483 if (m->plus && nparam > m->nparam_max)
3484 nparam = m->nparam_max;
3486 * Then terminate the parameter list, and leave.
3488 if (!params) { /* need this special case */
3489 params = nasm_malloc(sizeof(*params));
3490 nparam = 0;
3492 params[nparam] = NULL;
3493 *params_array = params;
3494 return m;
3497 * This one wasn't right: look for the next one with the
3498 * same name.
3500 for (m = m->next; m; m = m->next)
3501 if (!mstrcmp(m->name, tline->text, m->casesense))
3502 break;
3506 * After all that, we didn't find one with the right number of
3507 * parameters. Issue a warning, and fail to expand the macro.
3509 error(ERR_WARNING | ERR_WARN_MNP,
3510 "macro `%s' exists, but not taking %d parameters",
3511 tline->text, nparam);
3512 nasm_free(params);
3513 return NULL;
3517 * Expand the multi-line macro call made by the given line, if
3518 * there is one to be expanded. If there is, push the expansion on
3519 * istk->expansion and return 1. Otherwise return 0.
3521 static int expand_mmacro(Token * tline)
3523 Token *startline = tline;
3524 Token *label = NULL;
3525 int dont_prepend = 0;
3526 Token **params, *t, *tt;
3527 MMacro *m;
3528 Line *l, *ll;
3529 int i, nparam, *paramlen;
3531 t = tline;
3532 skip_white_(t);
3533 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3534 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3535 return 0;
3536 m = is_mmacro(t, &params);
3537 if (!m) {
3538 Token *last;
3540 * We have an id which isn't a macro call. We'll assume
3541 * it might be a label; we'll also check to see if a
3542 * colon follows it. Then, if there's another id after
3543 * that lot, we'll check it again for macro-hood.
3545 label = last = t;
3546 t = t->next;
3547 if (tok_type_(t, TOK_WHITESPACE))
3548 last = t, t = t->next;
3549 if (tok_is_(t, ":")) {
3550 dont_prepend = 1;
3551 last = t, t = t->next;
3552 if (tok_type_(t, TOK_WHITESPACE))
3553 last = t, t = t->next;
3555 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3556 return 0;
3557 last->next = NULL;
3558 tline = t;
3562 * Fix up the parameters: this involves stripping leading and
3563 * trailing whitespace, then stripping braces if they are
3564 * present.
3566 for (nparam = 0; params[nparam]; nparam++) ;
3567 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3569 for (i = 0; params[i]; i++) {
3570 int brace = false;
3571 int comma = (!m->plus || i < nparam - 1);
3573 t = params[i];
3574 skip_white_(t);
3575 if (tok_is_(t, "{"))
3576 t = t->next, brace = true, comma = false;
3577 params[i] = t;
3578 paramlen[i] = 0;
3579 while (t) {
3580 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3581 break; /* ... because we have hit a comma */
3582 if (comma && t->type == TOK_WHITESPACE
3583 && tok_is_(t->next, ","))
3584 break; /* ... or a space then a comma */
3585 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3586 break; /* ... or a brace */
3587 t = t->next;
3588 paramlen[i]++;
3593 * OK, we have a MMacro structure together with a set of
3594 * parameters. We must now go through the expansion and push
3595 * copies of each Line on to istk->expansion. Substitution of
3596 * parameter tokens and macro-local tokens doesn't get done
3597 * until the single-line macro substitution process; this is
3598 * because delaying them allows us to change the semantics
3599 * later through %rotate.
3601 * First, push an end marker on to istk->expansion, mark this
3602 * macro as in progress, and set up its invocation-specific
3603 * variables.
3605 ll = nasm_malloc(sizeof(Line));
3606 ll->next = istk->expansion;
3607 ll->finishes = m;
3608 ll->first = NULL;
3609 istk->expansion = ll;
3611 m->in_progress = true;
3612 m->params = params;
3613 m->iline = tline;
3614 m->nparam = nparam;
3615 m->rotate = 0;
3616 m->paramlen = paramlen;
3617 m->unique = unique++;
3618 m->lineno = 0;
3620 m->next_active = istk->mstk;
3621 istk->mstk = m;
3623 for (l = m->expansion; l; l = l->next) {
3624 Token **tail;
3626 ll = nasm_malloc(sizeof(Line));
3627 ll->finishes = NULL;
3628 ll->next = istk->expansion;
3629 istk->expansion = ll;
3630 tail = &ll->first;
3632 for (t = l->first; t; t = t->next) {
3633 Token *x = t;
3634 if (t->type == TOK_PREPROC_ID &&
3635 t->text[1] == '0' && t->text[2] == '0') {
3636 dont_prepend = -1;
3637 x = label;
3638 if (!x)
3639 continue;
3641 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3642 tail = &tt->next;
3644 *tail = NULL;
3648 * If we had a label, push it on as the first line of
3649 * the macro expansion.
3651 if (label) {
3652 if (dont_prepend < 0)
3653 free_tlist(startline);
3654 else {
3655 ll = nasm_malloc(sizeof(Line));
3656 ll->finishes = NULL;
3657 ll->next = istk->expansion;
3658 istk->expansion = ll;
3659 ll->first = startline;
3660 if (!dont_prepend) {
3661 while (label->next)
3662 label = label->next;
3663 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3668 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3670 return 1;
3674 * Since preprocessor always operate only on the line that didn't
3675 * arrived yet, we should always use ERR_OFFBY1. Also since user
3676 * won't want to see same error twice (preprocessing is done once
3677 * per pass) we will want to show errors only during pass one.
3679 static void error(int severity, const char *fmt, ...)
3681 va_list arg;
3682 char buff[1024];
3684 /* If we're in a dead branch of IF or something like it, ignore the error */
3685 if (istk && istk->conds && !emitting(istk->conds->state))
3686 return;
3688 va_start(arg, fmt);
3689 vsnprintf(buff, sizeof(buff), fmt, arg);
3690 va_end(arg);
3692 if (istk && istk->mstk && istk->mstk->name)
3693 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3694 istk->mstk->lineno, buff);
3695 else
3696 _error(severity | ERR_PASS1, "%s", buff);
3699 static void
3700 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3701 ListGen * listgen)
3703 _error = errfunc;
3704 cstk = NULL;
3705 istk = nasm_malloc(sizeof(Include));
3706 istk->next = NULL;
3707 istk->conds = NULL;
3708 istk->expansion = NULL;
3709 istk->mstk = NULL;
3710 istk->fp = fopen(file, "r");
3711 istk->fname = NULL;
3712 src_set_fname(nasm_strdup(file));
3713 src_set_linnum(0);
3714 istk->lineinc = 1;
3715 if (!istk->fp)
3716 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3717 file);
3718 defining = NULL;
3719 init_macros();
3720 unique = 0;
3721 if (tasm_compatible_mode) {
3722 stdmacpos = nasm_stdmac;
3723 } else {
3724 stdmacpos = nasm_stdmac_after_tasm;
3726 any_extrastdmac = (extrastdmac != NULL);
3727 list = listgen;
3728 evaluate = eval;
3729 pass = apass;
3732 static char *pp_getline(void)
3734 char *line;
3735 Token *tline;
3737 while (1) {
3739 * Fetch a tokenized line, either from the macro-expansion
3740 * buffer or from the input file.
3742 tline = NULL;
3743 while (istk->expansion && istk->expansion->finishes) {
3744 Line *l = istk->expansion;
3745 if (!l->finishes->name && l->finishes->in_progress > 1) {
3746 Line *ll;
3749 * This is a macro-end marker for a macro with no
3750 * name, which means it's not really a macro at all
3751 * but a %rep block, and the `in_progress' field is
3752 * more than 1, meaning that we still need to
3753 * repeat. (1 means the natural last repetition; 0
3754 * means termination by %exitrep.) We have
3755 * therefore expanded up to the %endrep, and must
3756 * push the whole block on to the expansion buffer
3757 * again. We don't bother to remove the macro-end
3758 * marker: we'd only have to generate another one
3759 * if we did.
3761 l->finishes->in_progress--;
3762 for (l = l->finishes->expansion; l; l = l->next) {
3763 Token *t, *tt, **tail;
3765 ll = nasm_malloc(sizeof(Line));
3766 ll->next = istk->expansion;
3767 ll->finishes = NULL;
3768 ll->first = NULL;
3769 tail = &ll->first;
3771 for (t = l->first; t; t = t->next) {
3772 if (t->text || t->type == TOK_WHITESPACE) {
3773 tt = *tail =
3774 new_Token(NULL, t->type, t->text, 0);
3775 tail = &tt->next;
3779 istk->expansion = ll;
3781 } else {
3783 * Check whether a `%rep' was started and not ended
3784 * within this macro expansion. This can happen and
3785 * should be detected. It's a fatal error because
3786 * I'm too confused to work out how to recover
3787 * sensibly from it.
3789 if (defining) {
3790 if (defining->name)
3791 error(ERR_PANIC,
3792 "defining with name in expansion");
3793 else if (istk->mstk->name)
3794 error(ERR_FATAL,
3795 "`%%rep' without `%%endrep' within"
3796 " expansion of macro `%s'",
3797 istk->mstk->name);
3801 * FIXME: investigate the relationship at this point between
3802 * istk->mstk and l->finishes
3805 MMacro *m = istk->mstk;
3806 istk->mstk = m->next_active;
3807 if (m->name) {
3809 * This was a real macro call, not a %rep, and
3810 * therefore the parameter information needs to
3811 * be freed.
3813 nasm_free(m->params);
3814 free_tlist(m->iline);
3815 nasm_free(m->paramlen);
3816 l->finishes->in_progress = false;
3817 } else
3818 free_mmacro(m);
3820 istk->expansion = l->next;
3821 nasm_free(l);
3822 list->downlevel(LIST_MACRO);
3825 while (1) { /* until we get a line we can use */
3827 if (istk->expansion) { /* from a macro expansion */
3828 char *p;
3829 Line *l = istk->expansion;
3830 if (istk->mstk)
3831 istk->mstk->lineno++;
3832 tline = l->first;
3833 istk->expansion = l->next;
3834 nasm_free(l);
3835 p = detoken(tline, false);
3836 list->line(LIST_MACRO, p);
3837 nasm_free(p);
3838 break;
3840 line = read_line();
3841 if (line) { /* from the current input file */
3842 line = prepreproc(line);
3843 tline = tokenize(line);
3844 nasm_free(line);
3845 break;
3848 * The current file has ended; work down the istk
3851 Include *i = istk;
3852 fclose(i->fp);
3853 if (i->conds)
3854 error(ERR_FATAL,
3855 "expected `%%endif' before end of file");
3856 /* only set line and file name if there's a next node */
3857 if (i->next) {
3858 src_set_linnum(i->lineno);
3859 nasm_free(src_set_fname(i->fname));
3861 istk = i->next;
3862 list->downlevel(LIST_INCLUDE);
3863 nasm_free(i);
3864 if (!istk)
3865 return NULL;
3870 * We must expand MMacro parameters and MMacro-local labels
3871 * _before_ we plunge into directive processing, to cope
3872 * with things like `%define something %1' such as STRUC
3873 * uses. Unless we're _defining_ a MMacro, in which case
3874 * those tokens should be left alone to go into the
3875 * definition; and unless we're in a non-emitting
3876 * condition, in which case we don't want to meddle with
3877 * anything.
3879 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3880 tline = expand_mmac_params(tline);
3883 * Check the line to see if it's a preprocessor directive.
3885 if (do_directive(tline) == DIRECTIVE_FOUND) {
3886 continue;
3887 } else if (defining) {
3889 * We're defining a multi-line macro. We emit nothing
3890 * at all, and just
3891 * shove the tokenized line on to the macro definition.
3893 Line *l = nasm_malloc(sizeof(Line));
3894 l->next = defining->expansion;
3895 l->first = tline;
3896 l->finishes = false;
3897 defining->expansion = l;
3898 continue;
3899 } else if (istk->conds && !emitting(istk->conds->state)) {
3901 * We're in a non-emitting branch of a condition block.
3902 * Emit nothing at all, not even a blank line: when we
3903 * emerge from the condition we'll give a line-number
3904 * directive so we keep our place correctly.
3906 free_tlist(tline);
3907 continue;
3908 } else if (istk->mstk && !istk->mstk->in_progress) {
3910 * We're in a %rep block which has been terminated, so
3911 * we're walking through to the %endrep without
3912 * emitting anything. Emit nothing at all, not even a
3913 * blank line: when we emerge from the %rep block we'll
3914 * give a line-number directive so we keep our place
3915 * correctly.
3917 free_tlist(tline);
3918 continue;
3919 } else {
3920 tline = expand_smacro(tline);
3921 if (!expand_mmacro(tline)) {
3923 * De-tokenize the line again, and emit it.
3925 line = detoken(tline, true);
3926 free_tlist(tline);
3927 break;
3928 } else {
3929 continue; /* expand_mmacro calls free_tlist */
3934 return line;
3937 static void pp_cleanup(int pass)
3939 if (defining) {
3940 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3941 defining->name);
3942 free_mmacro(defining);
3944 while (cstk)
3945 ctx_pop();
3946 free_macros();
3947 while (istk) {
3948 Include *i = istk;
3949 istk = istk->next;
3950 fclose(i->fp);
3951 nasm_free(i->fname);
3952 nasm_free(i);
3954 while (cstk)
3955 ctx_pop();
3956 if (pass == 0) {
3957 free_llist(predef);
3958 delete_Blocks();
3962 void pp_include_path(char *path)
3964 IncPath *i;
3966 i = nasm_malloc(sizeof(IncPath));
3967 i->path = path ? nasm_strdup(path) : NULL;
3968 i->next = NULL;
3970 if (ipath != NULL) {
3971 IncPath *j = ipath;
3972 while (j->next != NULL)
3973 j = j->next;
3974 j->next = i;
3975 } else {
3976 ipath = i;
3981 * added by alexfru:
3983 * This function is used to "export" the include paths, e.g.
3984 * the paths specified in the '-I' command switch.
3985 * The need for such exporting is due to the 'incbin' directive,
3986 * which includes raw binary files (unlike '%include', which
3987 * includes text source files). It would be real nice to be
3988 * able to specify paths to search for incbin'ned files also.
3989 * So, this is a simple workaround.
3991 * The function use is simple:
3993 * The 1st call (with NULL argument) returns a pointer to the 1st path
3994 * (char** type) or NULL if none include paths available.
3996 * All subsequent calls take as argument the value returned by this
3997 * function last. The return value is either the next path
3998 * (char** type) or NULL if the end of the paths list is reached.
4000 * It is maybe not the best way to do things, but I didn't want
4001 * to export too much, just one or two functions and no types or
4002 * variables exported.
4004 * Can't say I like the current situation with e.g. this path list either,
4005 * it seems to be never deallocated after creation...
4007 char **pp_get_include_path_ptr(char **pPrevPath)
4009 /* This macro returns offset of a member of a structure */
4010 #define GetMemberOffset(StructType,MemberName)\
4011 ((size_t)&((StructType*)0)->MemberName)
4012 IncPath *i;
4014 if (pPrevPath == NULL) {
4015 if (ipath != NULL)
4016 return &ipath->path;
4017 else
4018 return NULL;
4020 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4021 i = i->next;
4022 if (i != NULL)
4023 return &i->path;
4024 else
4025 return NULL;
4026 #undef GetMemberOffset
4029 void pp_pre_include(char *fname)
4031 Token *inc, *space, *name;
4032 Line *l;
4034 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4035 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4036 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4038 l = nasm_malloc(sizeof(Line));
4039 l->next = predef;
4040 l->first = inc;
4041 l->finishes = false;
4042 predef = l;
4045 void pp_pre_define(char *definition)
4047 Token *def, *space;
4048 Line *l;
4049 char *equals;
4051 equals = strchr(definition, '=');
4052 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4053 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4054 if (equals)
4055 *equals = ' ';
4056 space->next = tokenize(definition);
4057 if (equals)
4058 *equals = '=';
4060 l = nasm_malloc(sizeof(Line));
4061 l->next = predef;
4062 l->first = def;
4063 l->finishes = false;
4064 predef = l;
4067 void pp_pre_undefine(char *definition)
4069 Token *def, *space;
4070 Line *l;
4072 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4073 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4074 space->next = tokenize(definition);
4076 l = nasm_malloc(sizeof(Line));
4077 l->next = predef;
4078 l->first = def;
4079 l->finishes = false;
4080 predef = l;
4084 * Added by Keith Kanios:
4086 * This function is used to assist with "runtime" preprocessor
4087 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4089 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4090 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4093 void pp_runtime(char *definition)
4095 Token *def;
4097 def = tokenize(definition);
4098 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4099 free_tlist(def);
4103 void pp_extra_stdmac(const char **macros)
4105 extrastdmac = macros;
4108 static void make_tok_num(Token * tok, int64_t val)
4110 char numbuf[20];
4111 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4112 tok->text = nasm_strdup(numbuf);
4113 tok->type = TOK_NUMBER;
4116 Preproc nasmpp = {
4117 pp_reset,
4118 pp_getline,
4119 pp_cleanup