preproc.c: allow 64-bit repeat counts
[nasm/autotest.git] / preproc.c
blob888cd074e68b7cc621995ac8b124dfb4c1dcb030
1 /* -*- mode: c; c-file-style: "bsd" -*- */
2 /* preproc.c macro preprocessor for the Netwide Assembler
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
9 * initial version 18/iii/97 by Simon Tatham
12 /* Typical flow of text through preproc
14 * pp_getline gets tokenized lines, either
16 * from a macro expansion
18 * or
19 * {
20 * read_line gets raw text from stdmacpos, or predef, or current input file
21 * tokenize converts to tokens
22 * }
24 * expand_mmac_params is used to expand %1 etc., unless a macro is being
25 * defined or a false conditional is being processed
26 * (%0, %1, %+1, %-1, %%foo
28 * do_directive checks for directives
30 * expand_smacro is used to expand single line macros
32 * expand_mmacro is used to expand multi-line macros
34 * detoken is used to convert the line back to text
37 #include "compiler.h"
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <limits.h>
46 #include <inttypes.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "preproc.h"
51 #include "hashtbl.h"
53 typedef struct SMacro SMacro;
54 typedef struct MMacro MMacro;
55 typedef struct Context Context;
56 typedef struct Token Token;
57 typedef struct Blocks Blocks;
58 typedef struct Line Line;
59 typedef struct Include Include;
60 typedef struct Cond Cond;
61 typedef struct IncPath IncPath;
64 * Note on the storage of both SMacro and MMacros: the hash table
65 * indexes them case-insensitively, and we then have to go through a
66 * linked list of potential case aliases (and, for MMacros, parameter
67 * ranges); this is to preserve the matching semantics of the earlier
68 * code. If the number of case aliases for a specific macro is a
69 * performance issue, you may want to reconsider your coding style.
73 * Store the definition of a single-line macro.
75 struct SMacro {
76 SMacro *next;
77 char *name;
78 bool casesense;
79 bool in_progress;
80 unsigned int nparam;
81 Token *expansion;
85 * Store the definition of a multi-line macro. This is also used to
86 * store the interiors of `%rep...%endrep' blocks, which are
87 * effectively self-re-invoking multi-line macros which simply
88 * don't have a name or bother to appear in the hash tables. %rep
89 * blocks are signified by having a NULL `name' field.
91 * In a MMacro describing a `%rep' block, the `in_progress' field
92 * isn't merely boolean, but gives the number of repeats left to
93 * run.
95 * The `next' field is used for storing MMacros in hash tables; the
96 * `next_active' field is for stacking them on istk entries.
98 * When a MMacro is being expanded, `params', `iline', `nparam',
99 * `paramlen', `rotate' and `unique' are local to the invocation.
101 struct MMacro {
102 MMacro *next;
103 char *name;
104 int nparam_min, nparam_max;
105 bool casesense;
106 bool plus; /* is the last parameter greedy? */
107 bool nolist; /* is this macro listing-inhibited? */
108 int64_t in_progress;
109 Token *dlist; /* All defaults as one list */
110 Token **defaults; /* Parameter default pointers */
111 int ndefs; /* number of default parameters */
112 Line *expansion;
114 MMacro *next_active;
115 MMacro *rep_nest; /* used for nesting %rep */
116 Token **params; /* actual parameters */
117 Token *iline; /* invocation line */
118 unsigned int nparam, rotate;
119 int *paramlen;
120 uint64_t unique;
121 int lineno; /* Current line number on expansion */
125 * The context stack is composed of a linked list of these.
127 struct Context {
128 Context *next;
129 SMacro *localmac;
130 char *name;
131 uint32_t number;
135 * This is the internal form which we break input lines up into.
136 * Typically stored in linked lists.
138 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
139 * necessarily used as-is, but is intended to denote the number of
140 * the substituted parameter. So in the definition
142 * %define a(x,y) ( (x) & ~(y) )
144 * the token representing `x' will have its type changed to
145 * TOK_SMAC_PARAM, but the one representing `y' will be
146 * TOK_SMAC_PARAM+1.
148 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
149 * which doesn't need quotes around it. Used in the pre-include
150 * mechanism as an alternative to trying to find a sensible type of
151 * quote to use on the filename we were passed.
153 enum pp_token_type {
154 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
155 TOK_PREPROC_ID, TOK_STRING,
156 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
157 TOK_INTERNAL_STRING
160 struct Token {
161 Token *next;
162 char *text;
163 SMacro *mac; /* associated macro for TOK_SMAC_END */
164 enum pp_token_type type;
168 * Multi-line macro definitions are stored as a linked list of
169 * these, which is essentially a container to allow several linked
170 * lists of Tokens.
172 * Note that in this module, linked lists are treated as stacks
173 * wherever possible. For this reason, Lines are _pushed_ on to the
174 * `expansion' field in MMacro structures, so that the linked list,
175 * if walked, would give the macro lines in reverse order; this
176 * means that we can walk the list when expanding a macro, and thus
177 * push the lines on to the `expansion' field in _istk_ in reverse
178 * order (so that when popped back off they are in the right
179 * order). It may seem cockeyed, and it relies on my design having
180 * an even number of steps in, but it works...
182 * Some of these structures, rather than being actual lines, are
183 * markers delimiting the end of the expansion of a given macro.
184 * This is for use in the cycle-tracking and %rep-handling code.
185 * Such structures have `finishes' non-NULL, and `first' NULL. All
186 * others have `finishes' NULL, but `first' may still be NULL if
187 * the line is blank.
189 struct Line {
190 Line *next;
191 MMacro *finishes;
192 Token *first;
196 * To handle an arbitrary level of file inclusion, we maintain a
197 * stack (ie linked list) of these things.
199 struct Include {
200 Include *next;
201 FILE *fp;
202 Cond *conds;
203 Line *expansion;
204 char *fname;
205 int lineno, lineinc;
206 MMacro *mstk; /* stack of active macros/reps */
210 * Include search path. This is simply a list of strings which get
211 * prepended, in turn, to the name of an include file, in an
212 * attempt to find the file if it's not in the current directory.
214 struct IncPath {
215 IncPath *next;
216 char *path;
220 * Conditional assembly: we maintain a separate stack of these for
221 * each level of file inclusion. (The only reason we keep the
222 * stacks separate is to ensure that a stray `%endif' in a file
223 * included from within the true branch of a `%if' won't terminate
224 * it and cause confusion: instead, rightly, it'll cause an error.)
226 struct Cond {
227 Cond *next;
228 int state;
230 enum {
232 * These states are for use just after %if or %elif: IF_TRUE
233 * means the condition has evaluated to truth so we are
234 * currently emitting, whereas IF_FALSE means we are not
235 * currently emitting but will start doing so if a %else comes
236 * up. In these states, all directives are admissible: %elif,
237 * %else and %endif. (And of course %if.)
239 COND_IF_TRUE, COND_IF_FALSE,
241 * These states come up after a %else: ELSE_TRUE means we're
242 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
243 * any %elif or %else will cause an error.
245 COND_ELSE_TRUE, COND_ELSE_FALSE,
247 * This state means that we're not emitting now, and also that
248 * nothing until %endif will be emitted at all. It's for use in
249 * two circumstances: (i) when we've had our moment of emission
250 * and have now started seeing %elifs, and (ii) when the
251 * condition construct in question is contained within a
252 * non-emitting branch of a larger condition construct.
254 COND_NEVER
256 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
259 * These defines are used as the possible return values for do_directive
261 #define NO_DIRECTIVE_FOUND 0
262 #define DIRECTIVE_FOUND 1
265 * Condition codes. Note that we use c_ prefix not C_ because C_ is
266 * used in nasm.h for the "real" condition codes. At _this_ level,
267 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
268 * ones, so we need a different enum...
270 static const char * const conditions[] = {
271 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
272 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
273 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
275 enum pp_conds {
276 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
277 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
278 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
279 c_none = -1
281 static const enum pp_conds inverse_ccs[] = {
282 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
283 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,
284 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
288 * Directive names.
290 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
291 static int is_condition(enum preproc_token arg)
293 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
296 /* For TASM compatibility we need to be able to recognise TASM compatible
297 * conditional compilation directives. Using the NASM pre-processor does
298 * not work, so we look for them specifically from the following list and
299 * then jam in the equivalent NASM directive into the input stream.
302 #ifndef MAX
303 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
304 #endif
306 enum {
307 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
308 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
311 static const char * const tasm_directives[] = {
312 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
313 "ifndef", "include", "local"
316 static int StackSize = 4;
317 static char *StackPointer = "ebp";
318 static int ArgOffset = 8;
319 static int LocalOffset = 4;
321 static Context *cstk;
322 static Include *istk;
323 static IncPath *ipath = NULL;
325 static efunc _error; /* Pointer to client-provided error reporting function */
326 static evalfunc evaluate;
328 static int pass; /* HACK: pass 0 = generate dependencies only */
330 static uint64_t unique; /* unique identifier numbers */
332 static Line *predef = NULL;
334 static ListGen *list;
337 * The current set of multi-line macros we have defined.
339 static struct hash_table *mmacros;
342 * The current set of single-line macros we have defined.
344 static struct hash_table *smacros;
347 * The multi-line macro we are currently defining, or the %rep
348 * block we are currently reading, if any.
350 static MMacro *defining;
353 * The number of macro parameters to allocate space for at a time.
355 #define PARAM_DELTA 16
358 * The standard macro set: defined as `static char *stdmac[]'. Also
359 * gives our position in the macro set, when we're processing it.
361 #include "macros.c"
362 static const char **stdmacpos;
365 * The extra standard macros that come from the object format, if
366 * any.
368 static const char **extrastdmac = NULL;
369 bool any_extrastdmac;
372 * Tokens are allocated in blocks to improve speed
374 #define TOKEN_BLOCKSIZE 4096
375 static Token *freeTokens = NULL;
376 struct Blocks {
377 Blocks *next;
378 void *chunk;
381 static Blocks blocks = { NULL, NULL };
384 * Forward declarations.
386 static Token *expand_mmac_params(Token * tline);
387 static Token *expand_smacro(Token * tline);
388 static Token *expand_id(Token * tline);
389 static Context *get_ctx(char *name, bool all_contexts);
390 static void make_tok_num(Token * tok, int32_t val);
391 static void error(int severity, const char *fmt, ...);
392 static void *new_Block(size_t size);
393 static void delete_Blocks(void);
394 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
395 static Token *delete_Token(Token * t);
398 * Macros for safe checking of token pointers, avoid *(NULL)
400 #define tok_type_(x,t) ((x) && (x)->type == (t))
401 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
402 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
403 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
405 /* Handle TASM specific directives, which do not contain a % in
406 * front of them. We do it here because I could not find any other
407 * place to do it for the moment, and it is a hack (ideally it would
408 * be nice to be able to use the NASM pre-processor to do it).
410 static char *check_tasm_directive(char *line)
412 int32_t i, j, k, m, len;
413 char *p = line, *oldline, oldchar;
415 /* Skip whitespace */
416 while (isspace(*p) && *p != 0)
417 p++;
419 /* Binary search for the directive name */
420 i = -1;
421 j = elements(tasm_directives);
422 len = 0;
423 while (!isspace(p[len]) && p[len] != 0)
424 len++;
425 if (len) {
426 oldchar = p[len];
427 p[len] = 0;
428 while (j - i > 1) {
429 k = (j + i) / 2;
430 m = nasm_stricmp(p, tasm_directives[k]);
431 if (m == 0) {
432 /* We have found a directive, so jam a % in front of it
433 * so that NASM will then recognise it as one if it's own.
435 p[len] = oldchar;
436 len = strlen(p);
437 oldline = line;
438 line = nasm_malloc(len + 2);
439 line[0] = '%';
440 if (k == TM_IFDIFI) {
441 /* NASM does not recognise IFDIFI, so we convert it to
442 * %ifdef BOGUS. This is not used in NASM comaptible
443 * code, but does need to parse for the TASM macro
444 * package.
446 strcpy(line + 1, "ifdef BOGUS");
447 } else {
448 memcpy(line + 1, p, len + 1);
450 nasm_free(oldline);
451 return line;
452 } else if (m < 0) {
453 j = k;
454 } else
455 i = k;
457 p[len] = oldchar;
459 return line;
463 * The pre-preprocessing stage... This function translates line
464 * number indications as they emerge from GNU cpp (`# lineno "file"
465 * flags') into NASM preprocessor line number indications (`%line
466 * lineno file').
468 static char *prepreproc(char *line)
470 int lineno, fnlen;
471 char *fname, *oldline;
473 if (line[0] == '#' && line[1] == ' ') {
474 oldline = line;
475 fname = oldline + 2;
476 lineno = atoi(fname);
477 fname += strspn(fname, "0123456789 ");
478 if (*fname == '"')
479 fname++;
480 fnlen = strcspn(fname, "\"");
481 line = nasm_malloc(20 + fnlen);
482 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
483 nasm_free(oldline);
485 if (tasm_compatible_mode)
486 return check_tasm_directive(line);
487 return line;
491 * Free a linked list of tokens.
493 static void free_tlist(Token * list)
495 while (list) {
496 list = delete_Token(list);
501 * Free a linked list of lines.
503 static void free_llist(Line * list)
505 Line *l;
506 while (list) {
507 l = list;
508 list = list->next;
509 free_tlist(l->first);
510 nasm_free(l);
515 * Free an MMacro
517 static void free_mmacro(MMacro * m)
519 nasm_free(m->name);
520 free_tlist(m->dlist);
521 nasm_free(m->defaults);
522 free_llist(m->expansion);
523 nasm_free(m);
527 * Free all currently defined macros, and free the hash tables
529 static void free_macros(void)
531 struct hash_tbl_node *it;
532 const char *key;
533 SMacro *s;
534 MMacro *m;
536 it = NULL;
537 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
538 nasm_free((void *)key);
539 while (s) {
540 SMacro *ns = s->next;
541 nasm_free(s->name);
542 free_tlist(s->expansion);
543 nasm_free(s);
544 s = ns;
547 hash_free(smacros);
549 it = NULL;
550 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
551 nasm_free((void *)key);
552 while (m) {
553 MMacro *nm = m->next;
554 free_mmacro(m);
555 m = nm;
558 hash_free(mmacros);
562 * Initialize the hash tables
564 static void init_macros(void)
566 smacros = hash_init();
567 mmacros = hash_init();
571 * Pop the context stack.
573 static void ctx_pop(void)
575 Context *c = cstk;
576 SMacro *smac, *s;
578 cstk = cstk->next;
579 smac = c->localmac;
580 while (smac) {
581 s = smac;
582 smac = smac->next;
583 nasm_free(s->name);
584 free_tlist(s->expansion);
585 nasm_free(s);
587 nasm_free(c->name);
588 nasm_free(c);
591 #define BUF_DELTA 512
593 * Read a line from the top file in istk, handling multiple CR/LFs
594 * at the end of the line read, and handling spurious ^Zs. Will
595 * return lines from the standard macro set if this has not already
596 * been done.
598 static char *read_line(void)
600 char *buffer, *p, *q;
601 int bufsize, continued_count;
603 if (stdmacpos) {
604 if (*stdmacpos) {
605 char *ret = nasm_strdup(*stdmacpos++);
606 if (!*stdmacpos && any_extrastdmac) {
607 stdmacpos = extrastdmac;
608 any_extrastdmac = false;
609 return ret;
612 * Nasty hack: here we push the contents of `predef' on
613 * to the top-level expansion stack, since this is the
614 * most convenient way to implement the pre-include and
615 * pre-define features.
617 if (!*stdmacpos) {
618 Line *pd, *l;
619 Token *head, **tail, *t;
621 for (pd = predef; pd; pd = pd->next) {
622 head = NULL;
623 tail = &head;
624 for (t = pd->first; t; t = t->next) {
625 *tail = new_Token(NULL, t->type, t->text, 0);
626 tail = &(*tail)->next;
628 l = nasm_malloc(sizeof(Line));
629 l->next = istk->expansion;
630 l->first = head;
631 l->finishes = false;
632 istk->expansion = l;
635 return ret;
636 } else {
637 stdmacpos = NULL;
641 bufsize = BUF_DELTA;
642 buffer = nasm_malloc(BUF_DELTA);
643 p = buffer;
644 continued_count = 0;
645 while (1) {
646 q = fgets(p, bufsize - (p - buffer), istk->fp);
647 if (!q)
648 break;
649 p += strlen(p);
650 if (p > buffer && p[-1] == '\n') {
651 /* Convert backslash-CRLF line continuation sequences into
652 nothing at all (for DOS and Windows) */
653 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
654 p -= 3;
655 *p = 0;
656 continued_count++;
658 /* Also convert backslash-LF line continuation sequences into
659 nothing at all (for Unix) */
660 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
661 p -= 2;
662 *p = 0;
663 continued_count++;
664 } else {
665 break;
668 if (p - buffer > bufsize - 10) {
669 int32_t offset = p - buffer;
670 bufsize += BUF_DELTA;
671 buffer = nasm_realloc(buffer, bufsize);
672 p = buffer + offset; /* prevent stale-pointer problems */
676 if (!q && p == buffer) {
677 nasm_free(buffer);
678 return NULL;
681 src_set_linnum(src_get_linnum() + istk->lineinc +
682 (continued_count * istk->lineinc));
685 * Play safe: remove CRs as well as LFs, if any of either are
686 * present at the end of the line.
688 while (--p >= buffer && (*p == '\n' || *p == '\r'))
689 *p = '\0';
692 * Handle spurious ^Z, which may be inserted into source files
693 * by some file transfer utilities.
695 buffer[strcspn(buffer, "\032")] = '\0';
697 list->line(LIST_READ, buffer);
699 return buffer;
703 * Tokenize a line of text. This is a very simple process since we
704 * don't need to parse the value out of e.g. numeric tokens: we
705 * simply split one string into many.
707 static Token *tokenize(char *line)
709 char *p = line;
710 enum pp_token_type type;
711 Token *list = NULL;
712 Token *t, **tail = &list;
714 while (*line) {
715 p = line;
716 if (*p == '%') {
717 p++;
718 if (isdigit(*p) ||
719 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
720 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
721 do {
722 p++;
724 while (isdigit(*p));
725 type = TOK_PREPROC_ID;
726 } else if (*p == '{') {
727 p++;
728 while (*p && *p != '}') {
729 p[-1] = *p;
730 p++;
732 p[-1] = '\0';
733 if (*p)
734 p++;
735 type = TOK_PREPROC_ID;
736 } else if (isidchar(*p) ||
737 ((*p == '!' || *p == '%' || *p == '$') &&
738 isidchar(p[1]))) {
739 do {
740 p++;
742 while (isidchar(*p));
743 type = TOK_PREPROC_ID;
744 } else {
745 type = TOK_OTHER;
746 if (*p == '%')
747 p++;
749 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
750 type = TOK_ID;
751 p++;
752 while (*p && isidchar(*p))
753 p++;
754 } else if (*p == '\'' || *p == '"') {
756 * A string token.
758 char c = *p;
759 p++;
760 type = TOK_STRING;
761 while (*p && *p != c)
762 p++;
764 if (*p) {
765 p++;
766 } else {
767 error(ERR_WARNING, "unterminated string");
768 /* Handling unterminated strings by UNV */
769 /* type = -1; */
771 } else if (isnumstart(*p)) {
773 * A number token.
775 type = TOK_NUMBER;
776 p++;
777 while (*p && isnumchar(*p))
778 p++;
779 } else if (isspace(*p)) {
780 type = TOK_WHITESPACE;
781 p++;
782 while (*p && isspace(*p))
783 p++;
785 * Whitespace just before end-of-line is discarded by
786 * pretending it's a comment; whitespace just before a
787 * comment gets lumped into the comment.
789 if (!*p || *p == ';') {
790 type = TOK_COMMENT;
791 while (*p)
792 p++;
794 } else if (*p == ';') {
795 type = TOK_COMMENT;
796 while (*p)
797 p++;
798 } else {
800 * Anything else is an operator of some kind. We check
801 * for all the double-character operators (>>, <<, //,
802 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
803 * else is a single-character operator.
805 type = TOK_OTHER;
806 if ((p[0] == '>' && p[1] == '>') ||
807 (p[0] == '<' && p[1] == '<') ||
808 (p[0] == '/' && p[1] == '/') ||
809 (p[0] == '<' && p[1] == '=') ||
810 (p[0] == '>' && p[1] == '=') ||
811 (p[0] == '=' && p[1] == '=') ||
812 (p[0] == '!' && p[1] == '=') ||
813 (p[0] == '<' && p[1] == '>') ||
814 (p[0] == '&' && p[1] == '&') ||
815 (p[0] == '|' && p[1] == '|') ||
816 (p[0] == '^' && p[1] == '^')) {
817 p++;
819 p++;
822 /* Handling unterminated string by UNV */
823 /*if (type == -1)
825 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
826 t->text[p-line] = *line;
827 tail = &t->next;
829 else */
830 if (type != TOK_COMMENT) {
831 *tail = t = new_Token(NULL, type, line, p - line);
832 tail = &t->next;
834 line = p;
836 return list;
840 * this function allocates a new managed block of memory and
841 * returns a pointer to the block. The managed blocks are
842 * deleted only all at once by the delete_Blocks function.
844 static void *new_Block(size_t size)
846 Blocks *b = &blocks;
848 /* first, get to the end of the linked list */
849 while (b->next)
850 b = b->next;
851 /* now allocate the requested chunk */
852 b->chunk = nasm_malloc(size);
854 /* now allocate a new block for the next request */
855 b->next = nasm_malloc(sizeof(Blocks));
856 /* and initialize the contents of the new block */
857 b->next->next = NULL;
858 b->next->chunk = NULL;
859 return b->chunk;
863 * this function deletes all managed blocks of memory
865 static void delete_Blocks(void)
867 Blocks *a, *b = &blocks;
870 * keep in mind that the first block, pointed to by blocks
871 * is a static and not dynamically allocated, so we don't
872 * free it.
874 while (b) {
875 if (b->chunk)
876 nasm_free(b->chunk);
877 a = b;
878 b = b->next;
879 if (a != &blocks)
880 nasm_free(a);
885 * this function creates a new Token and passes a pointer to it
886 * back to the caller. It sets the type and text elements, and
887 * also the mac and next elements to NULL.
889 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
891 Token *t;
892 int i;
894 if (freeTokens == NULL) {
895 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
896 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
897 freeTokens[i].next = &freeTokens[i + 1];
898 freeTokens[i].next = NULL;
900 t = freeTokens;
901 freeTokens = t->next;
902 t->next = next;
903 t->mac = NULL;
904 t->type = type;
905 if (type == TOK_WHITESPACE || text == NULL) {
906 t->text = NULL;
907 } else {
908 if (txtlen == 0)
909 txtlen = strlen(text);
910 t->text = nasm_malloc(1 + txtlen);
911 strncpy(t->text, text, txtlen);
912 t->text[txtlen] = '\0';
914 return t;
917 static Token *delete_Token(Token * t)
919 Token *next = t->next;
920 nasm_free(t->text);
921 t->next = freeTokens;
922 freeTokens = t;
923 return next;
927 * Convert a line of tokens back into text.
928 * If expand_locals is not zero, identifiers of the form "%$*xxx"
929 * will be transformed into ..@ctxnum.xxx
931 static char *detoken(Token * tlist, int expand_locals)
933 Token *t;
934 int len;
935 char *line, *p;
937 len = 0;
938 for (t = tlist; t; t = t->next) {
939 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
940 char *p = getenv(t->text + 2);
941 nasm_free(t->text);
942 if (p)
943 t->text = nasm_strdup(p);
944 else
945 t->text = NULL;
947 /* Expand local macros here and not during preprocessing */
948 if (expand_locals &&
949 t->type == TOK_PREPROC_ID && t->text &&
950 t->text[0] == '%' && t->text[1] == '$') {
951 Context *ctx = get_ctx(t->text, false);
952 if (ctx) {
953 char buffer[40];
954 char *p, *q = t->text + 2;
956 q += strspn(q, "$");
957 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
958 p = nasm_strcat(buffer, q);
959 nasm_free(t->text);
960 t->text = p;
963 if (t->type == TOK_WHITESPACE) {
964 len++;
965 } else if (t->text) {
966 len += strlen(t->text);
969 p = line = nasm_malloc(len + 1);
970 for (t = tlist; t; t = t->next) {
971 if (t->type == TOK_WHITESPACE) {
972 *p = ' ';
973 p++;
974 *p = '\0';
975 } else if (t->text) {
976 strcpy(p, t->text);
977 p += strlen(p);
980 *p = '\0';
981 return line;
985 * A scanner, suitable for use by the expression evaluator, which
986 * operates on a line of Tokens. Expects a pointer to a pointer to
987 * the first token in the line to be passed in as its private_data
988 * field.
990 static int ppscan(void *private_data, struct tokenval *tokval)
992 Token **tlineptr = private_data;
993 Token *tline;
995 do {
996 tline = *tlineptr;
997 *tlineptr = tline ? tline->next : NULL;
999 while (tline && (tline->type == TOK_WHITESPACE ||
1000 tline->type == TOK_COMMENT));
1002 if (!tline)
1003 return tokval->t_type = TOKEN_EOS;
1005 if (tline->text[0] == '$' && !tline->text[1])
1006 return tokval->t_type = TOKEN_HERE;
1007 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1008 return tokval->t_type = TOKEN_BASE;
1010 if (tline->type == TOK_ID) {
1011 tokval->t_charptr = tline->text;
1012 if (tline->text[0] == '$') {
1013 tokval->t_charptr++;
1014 return tokval->t_type = TOKEN_ID;
1018 * This is the only special case we actually need to worry
1019 * about in this restricted context.
1021 if (!nasm_stricmp(tline->text, "seg"))
1022 return tokval->t_type = TOKEN_SEG;
1024 return tokval->t_type = TOKEN_ID;
1027 if (tline->type == TOK_NUMBER) {
1028 bool rn_error;
1030 tokval->t_integer = readnum(tline->text, &rn_error);
1031 if (rn_error)
1032 return tokval->t_type = TOKEN_ERRNUM;
1033 tokval->t_charptr = NULL;
1034 return tokval->t_type = TOKEN_NUM;
1037 if (tline->type == TOK_STRING) {
1038 bool rn_warn;
1039 char q, *r;
1040 int l;
1042 r = tline->text;
1043 q = *r++;
1044 l = strlen(r);
1046 if (l == 0 || r[l - 1] != q)
1047 return tokval->t_type = TOKEN_ERRNUM;
1048 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1049 if (rn_warn)
1050 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1051 tokval->t_charptr = NULL;
1052 return tokval->t_type = TOKEN_NUM;
1055 if (tline->type == TOK_OTHER) {
1056 if (!strcmp(tline->text, "<<"))
1057 return tokval->t_type = TOKEN_SHL;
1058 if (!strcmp(tline->text, ">>"))
1059 return tokval->t_type = TOKEN_SHR;
1060 if (!strcmp(tline->text, "//"))
1061 return tokval->t_type = TOKEN_SDIV;
1062 if (!strcmp(tline->text, "%%"))
1063 return tokval->t_type = TOKEN_SMOD;
1064 if (!strcmp(tline->text, "=="))
1065 return tokval->t_type = TOKEN_EQ;
1066 if (!strcmp(tline->text, "<>"))
1067 return tokval->t_type = TOKEN_NE;
1068 if (!strcmp(tline->text, "!="))
1069 return tokval->t_type = TOKEN_NE;
1070 if (!strcmp(tline->text, "<="))
1071 return tokval->t_type = TOKEN_LE;
1072 if (!strcmp(tline->text, ">="))
1073 return tokval->t_type = TOKEN_GE;
1074 if (!strcmp(tline->text, "&&"))
1075 return tokval->t_type = TOKEN_DBL_AND;
1076 if (!strcmp(tline->text, "^^"))
1077 return tokval->t_type = TOKEN_DBL_XOR;
1078 if (!strcmp(tline->text, "||"))
1079 return tokval->t_type = TOKEN_DBL_OR;
1083 * We have no other options: just return the first character of
1084 * the token text.
1086 return tokval->t_type = tline->text[0];
1090 * Compare a string to the name of an existing macro; this is a
1091 * simple wrapper which calls either strcmp or nasm_stricmp
1092 * depending on the value of the `casesense' parameter.
1094 static int mstrcmp(char *p, char *q, bool casesense)
1096 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1100 * Return the Context structure associated with a %$ token. Return
1101 * NULL, having _already_ reported an error condition, if the
1102 * context stack isn't deep enough for the supplied number of $
1103 * signs.
1104 * If all_contexts == true, contexts that enclose current are
1105 * also scanned for such smacro, until it is found; if not -
1106 * only the context that directly results from the number of $'s
1107 * in variable's name.
1109 static Context *get_ctx(char *name, bool all_contexts)
1111 Context *ctx;
1112 SMacro *m;
1113 int i;
1115 if (!name || name[0] != '%' || name[1] != '$')
1116 return NULL;
1118 if (!cstk) {
1119 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1120 return NULL;
1123 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1124 ctx = ctx->next;
1125 /* i--; Lino - 02/25/02 */
1127 if (!ctx) {
1128 error(ERR_NONFATAL, "`%s': context stack is only"
1129 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1130 return NULL;
1132 if (!all_contexts)
1133 return ctx;
1135 do {
1136 /* Search for this smacro in found context */
1137 m = ctx->localmac;
1138 while (m) {
1139 if (!mstrcmp(m->name, name, m->casesense))
1140 return ctx;
1141 m = m->next;
1143 ctx = ctx->next;
1145 while (ctx);
1146 return NULL;
1150 * Open an include file. This routine must always return a valid
1151 * file pointer if it returns - it's responsible for throwing an
1152 * ERR_FATAL and bombing out completely if not. It should also try
1153 * the include path one by one until it finds the file or reaches
1154 * the end of the path.
1156 static FILE *inc_fopen(char *file)
1158 FILE *fp;
1159 char *prefix = "", *combine;
1160 IncPath *ip = ipath;
1161 static int namelen = 0;
1162 int len = strlen(file);
1164 while (1) {
1165 combine = nasm_malloc(strlen(prefix) + len + 1);
1166 strcpy(combine, prefix);
1167 strcat(combine, file);
1168 fp = fopen(combine, "r");
1169 if (pass == 0 && fp) {
1170 namelen += strlen(combine) + 1;
1171 if (namelen > 62) {
1172 printf(" \\\n ");
1173 namelen = 2;
1175 printf(" %s", combine);
1177 nasm_free(combine);
1178 if (fp)
1179 return fp;
1180 if (!ip)
1181 break;
1182 prefix = ip->path;
1183 ip = ip->next;
1185 if (!prefix) {
1186 /* -MG given and file not found */
1187 if (pass == 0) {
1188 namelen += strlen(file) + 1;
1189 if (namelen > 62) {
1190 printf(" \\\n ");
1191 namelen = 2;
1193 printf(" %s", file);
1195 return NULL;
1199 error(ERR_FATAL, "unable to open include file `%s'", file);
1200 return NULL; /* never reached - placate compilers */
1204 * Search for a key in the hash index; adding it if necessary
1205 * (in which case we initialize the data pointer to NULL.)
1207 static void **
1208 hash_findi_add(struct hash_table *hash, const char *str)
1210 struct hash_insert hi;
1211 void **r;
1212 char *strx;
1214 r = hash_findi(hash, str, &hi);
1215 if (r)
1216 return r;
1218 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1219 return hash_add(&hi, strx, NULL);
1223 * Like hash_findi, but returns the data element rather than a pointer
1224 * to it. Used only when not adding a new element, hence no third
1225 * argument.
1227 static void *
1228 hash_findix(struct hash_table *hash, const char *str)
1230 void **p;
1232 p = hash_findi(hash, str, NULL);
1233 return p ? *p : NULL;
1237 * Determine if we should warn on defining a single-line macro of
1238 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1239 * return true if _any_ single-line macro of that name is defined.
1240 * Otherwise, will return true if a single-line macro with either
1241 * `nparam' or no parameters is defined.
1243 * If a macro with precisely the right number of parameters is
1244 * defined, or nparam is -1, the address of the definition structure
1245 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1246 * is NULL, no action will be taken regarding its contents, and no
1247 * error will occur.
1249 * Note that this is also called with nparam zero to resolve
1250 * `ifdef'.
1252 * If you already know which context macro belongs to, you can pass
1253 * the context pointer as first parameter; if you won't but name begins
1254 * with %$ the context will be automatically computed. If all_contexts
1255 * is true, macro will be searched in outer contexts as well.
1257 static int
1258 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1259 int nocase)
1261 SMacro *m;
1263 if (ctx) {
1264 m = ctx->localmac;
1265 } else if (name[0] == '%' && name[1] == '$') {
1266 if (cstk)
1267 ctx = get_ctx(name, false);
1268 if (!ctx)
1269 return false; /* got to return _something_ */
1270 m = ctx->localmac;
1271 } else {
1272 m = (SMacro *) hash_findix(smacros, name);
1275 while (m) {
1276 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1277 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1278 if (defn) {
1279 if (nparam == m->nparam || nparam == -1)
1280 *defn = m;
1281 else
1282 *defn = NULL;
1284 return true;
1286 m = m->next;
1289 return false;
1293 * Count and mark off the parameters in a multi-line macro call.
1294 * This is called both from within the multi-line macro expansion
1295 * code, and also to mark off the default parameters when provided
1296 * in a %macro definition line.
1298 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1300 int paramsize, brace;
1302 *nparam = paramsize = 0;
1303 *params = NULL;
1304 while (t) {
1305 if (*nparam >= paramsize) {
1306 paramsize += PARAM_DELTA;
1307 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1309 skip_white_(t);
1310 brace = false;
1311 if (tok_is_(t, "{"))
1312 brace = true;
1313 (*params)[(*nparam)++] = t;
1314 while (tok_isnt_(t, brace ? "}" : ","))
1315 t = t->next;
1316 if (t) { /* got a comma/brace */
1317 t = t->next;
1318 if (brace) {
1320 * Now we've found the closing brace, look further
1321 * for the comma.
1323 skip_white_(t);
1324 if (tok_isnt_(t, ",")) {
1325 error(ERR_NONFATAL,
1326 "braces do not enclose all of macro parameter");
1327 while (tok_isnt_(t, ","))
1328 t = t->next;
1330 if (t)
1331 t = t->next; /* eat the comma */
1338 * Determine whether one of the various `if' conditions is true or
1339 * not.
1341 * We must free the tline we get passed.
1343 static bool if_condition(Token * tline, enum preproc_token ct)
1345 enum pp_conditional i = PP_COND(ct);
1346 bool j;
1347 Token *t, *tt, **tptr, *origline;
1348 struct tokenval tokval;
1349 expr *evalresult;
1350 enum pp_token_type needtype;
1352 origline = tline;
1354 switch (i) {
1355 case PPC_IFCTX:
1356 j = false; /* have we matched yet? */
1357 while (cstk && tline) {
1358 skip_white_(tline);
1359 if (!tline || tline->type != TOK_ID) {
1360 error(ERR_NONFATAL,
1361 "`%s' expects context identifiers", pp_directives[ct]);
1362 free_tlist(origline);
1363 return -1;
1365 if (!nasm_stricmp(tline->text, cstk->name))
1366 j = true;
1367 tline = tline->next;
1369 break;
1371 case PPC_IFDEF:
1372 j = false; /* have we matched yet? */
1373 while (tline) {
1374 skip_white_(tline);
1375 if (!tline || (tline->type != TOK_ID &&
1376 (tline->type != TOK_PREPROC_ID ||
1377 tline->text[1] != '$'))) {
1378 error(ERR_NONFATAL,
1379 "`%s' expects macro identifiers", pp_directives[ct]);
1380 goto fail;
1382 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1383 j = true;
1384 tline = tline->next;
1386 break;
1388 case PPC_IFIDN:
1389 case PPC_IFIDNI:
1390 tline = expand_smacro(tline);
1391 t = tt = tline;
1392 while (tok_isnt_(tt, ","))
1393 tt = tt->next;
1394 if (!tt) {
1395 error(ERR_NONFATAL,
1396 "`%s' expects two comma-separated arguments",
1397 pp_directives[ct]);
1398 goto fail;
1400 tt = tt->next;
1401 j = true; /* assume equality unless proved not */
1402 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1403 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1404 error(ERR_NONFATAL, "`%s': more than one comma on line",
1405 pp_directives[ct]);
1406 goto fail;
1408 if (t->type == TOK_WHITESPACE) {
1409 t = t->next;
1410 continue;
1412 if (tt->type == TOK_WHITESPACE) {
1413 tt = tt->next;
1414 continue;
1416 if (tt->type != t->type) {
1417 j = false; /* found mismatching tokens */
1418 break;
1420 /* Unify surrounding quotes for strings */
1421 if (t->type == TOK_STRING) {
1422 tt->text[0] = t->text[0];
1423 tt->text[strlen(tt->text) - 1] = t->text[0];
1425 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1426 j = false; /* found mismatching tokens */
1427 break;
1430 t = t->next;
1431 tt = tt->next;
1433 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1434 j = false; /* trailing gunk on one end or other */
1435 break;
1437 case PPC_IFMACRO:
1439 bool found = false;
1440 MMacro searching, *mmac;
1442 tline = tline->next;
1443 skip_white_(tline);
1444 tline = expand_id(tline);
1445 if (!tok_type_(tline, TOK_ID)) {
1446 error(ERR_NONFATAL,
1447 "`%s' expects a macro name", pp_directives[ct]);
1448 goto fail;
1450 searching.name = nasm_strdup(tline->text);
1451 searching.casesense = (i == PP_MACRO);
1452 searching.plus = false;
1453 searching.nolist = false;
1454 searching.in_progress = 0;
1455 searching.rep_nest = NULL;
1456 searching.nparam_min = 0;
1457 searching.nparam_max = INT_MAX;
1458 tline = expand_smacro(tline->next);
1459 skip_white_(tline);
1460 if (!tline) {
1461 } else if (!tok_type_(tline, TOK_NUMBER)) {
1462 error(ERR_NONFATAL,
1463 "`%s' expects a parameter count or nothing",
1464 pp_directives[ct]);
1465 } else {
1466 searching.nparam_min = searching.nparam_max =
1467 readnum(tline->text, &j);
1468 if (j)
1469 error(ERR_NONFATAL,
1470 "unable to parse parameter count `%s'",
1471 tline->text);
1473 if (tline && tok_is_(tline->next, "-")) {
1474 tline = tline->next->next;
1475 if (tok_is_(tline, "*"))
1476 searching.nparam_max = INT_MAX;
1477 else if (!tok_type_(tline, TOK_NUMBER))
1478 error(ERR_NONFATAL,
1479 "`%s' expects a parameter count after `-'",
1480 pp_directives[ct]);
1481 else {
1482 searching.nparam_max = readnum(tline->text, &j);
1483 if (j)
1484 error(ERR_NONFATAL,
1485 "unable to parse parameter count `%s'",
1486 tline->text);
1487 if (searching.nparam_min > searching.nparam_max)
1488 error(ERR_NONFATAL,
1489 "minimum parameter count exceeds maximum");
1492 if (tline && tok_is_(tline->next, "+")) {
1493 tline = tline->next;
1494 searching.plus = true;
1496 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1497 while (mmac) {
1498 if (!strcmp(mmac->name, searching.name) &&
1499 (mmac->nparam_min <= searching.nparam_max
1500 || searching.plus)
1501 && (searching.nparam_min <= mmac->nparam_max
1502 || mmac->plus)) {
1503 found = true;
1504 break;
1506 mmac = mmac->next;
1508 nasm_free(searching.name);
1509 j = found;
1510 break;
1513 case PPC_IFID:
1514 needtype = TOK_ID;
1515 goto iftype;
1516 case PPC_IFNUM:
1517 needtype = TOK_NUMBER;
1518 goto iftype;
1519 case PPC_IFSTR:
1520 needtype = TOK_STRING;
1521 goto iftype;
1523 iftype:
1524 tline = expand_smacro(tline);
1525 t = tline;
1526 while (tok_type_(t, TOK_WHITESPACE))
1527 t = t->next;
1528 j = false; /* placate optimiser */
1529 if (t)
1530 j = t->type == needtype;
1531 break;
1533 case PPC_IF:
1534 t = tline = expand_smacro(tline);
1535 tptr = &t;
1536 tokval.t_type = TOKEN_INVALID;
1537 evalresult = evaluate(ppscan, tptr, &tokval,
1538 NULL, pass | CRITICAL, error, NULL);
1539 if (!evalresult)
1540 return -1;
1541 if (tokval.t_type)
1542 error(ERR_WARNING,
1543 "trailing garbage after expression ignored");
1544 if (!is_simple(evalresult)) {
1545 error(ERR_NONFATAL,
1546 "non-constant value given to `%s'", pp_directives[ct]);
1547 goto fail;
1549 j = reloc_value(evalresult) != 0;
1550 return j;
1552 default:
1553 error(ERR_FATAL,
1554 "preprocessor directive `%s' not yet implemented",
1555 pp_directives[ct]);
1556 goto fail;
1559 free_tlist(origline);
1560 return j ^ PP_NEGATIVE(ct);
1562 fail:
1563 free_tlist(origline);
1564 return -1;
1568 * Expand macros in a string. Used in %error and %include directives.
1569 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1570 * The returned variable should ALWAYS be freed after usage.
1572 void expand_macros_in_string(char **p)
1574 Token *line = tokenize(*p);
1575 line = expand_smacro(line);
1576 *p = detoken(line, false);
1580 * find and process preprocessor directive in passed line
1581 * Find out if a line contains a preprocessor directive, and deal
1582 * with it if so.
1584 * If a directive _is_ found, it is the responsibility of this routine
1585 * (and not the caller) to free_tlist() the line.
1587 * @param tline a pointer to the current tokeninzed line linked list
1588 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1591 static int do_directive(Token * tline)
1593 enum preproc_token i;
1594 int j;
1595 bool err;
1596 int nparam;
1597 bool nolist;
1598 int k, m;
1599 int offset;
1600 char *p, *mname;
1601 Include *inc;
1602 Context *ctx;
1603 Cond *cond;
1604 SMacro *smac, **smhead;
1605 MMacro *mmac, **mmhead;
1606 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1607 Line *l;
1608 struct tokenval tokval;
1609 expr *evalresult;
1610 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1611 int64_t count;
1613 origline = tline;
1615 skip_white_(tline);
1616 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1617 (tline->text[1] == '%' || tline->text[1] == '$'
1618 || tline->text[1] == '!'))
1619 return NO_DIRECTIVE_FOUND;
1621 i = pp_token_hash(tline->text);
1624 * If we're in a non-emitting branch of a condition construct,
1625 * or walking to the end of an already terminated %rep block,
1626 * we should ignore all directives except for condition
1627 * directives.
1629 if (((istk->conds && !emitting(istk->conds->state)) ||
1630 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1631 return NO_DIRECTIVE_FOUND;
1635 * If we're defining a macro or reading a %rep block, we should
1636 * ignore all directives except for %macro/%imacro (which
1637 * generate an error), %endm/%endmacro, and (only if we're in a
1638 * %rep block) %endrep. If we're in a %rep block, another %rep
1639 * causes an error, so should be let through.
1641 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1642 i != PP_ENDMACRO && i != PP_ENDM &&
1643 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1644 return NO_DIRECTIVE_FOUND;
1647 switch (i) {
1648 case PP_INVALID:
1649 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1650 tline->text);
1651 return NO_DIRECTIVE_FOUND; /* didn't get it */
1653 case PP_STACKSIZE:
1654 /* Directive to tell NASM what the default stack size is. The
1655 * default is for a 16-bit stack, and this can be overriden with
1656 * %stacksize large.
1657 * the following form:
1659 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1661 tline = tline->next;
1662 if (tline && tline->type == TOK_WHITESPACE)
1663 tline = tline->next;
1664 if (!tline || tline->type != TOK_ID) {
1665 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1666 free_tlist(origline);
1667 return DIRECTIVE_FOUND;
1669 if (nasm_stricmp(tline->text, "flat") == 0) {
1670 /* All subsequent ARG directives are for a 32-bit stack */
1671 StackSize = 4;
1672 StackPointer = "ebp";
1673 ArgOffset = 8;
1674 LocalOffset = 4;
1675 } else if (nasm_stricmp(tline->text, "large") == 0) {
1676 /* All subsequent ARG directives are for a 16-bit stack,
1677 * far function call.
1679 StackSize = 2;
1680 StackPointer = "bp";
1681 ArgOffset = 4;
1682 LocalOffset = 2;
1683 } else if (nasm_stricmp(tline->text, "small") == 0) {
1684 /* All subsequent ARG directives are for a 16-bit stack,
1685 * far function call. We don't support near functions.
1687 StackSize = 2;
1688 StackPointer = "bp";
1689 ArgOffset = 6;
1690 LocalOffset = 2;
1691 } else {
1692 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1693 free_tlist(origline);
1694 return DIRECTIVE_FOUND;
1696 free_tlist(origline);
1697 return DIRECTIVE_FOUND;
1699 case PP_ARG:
1700 /* TASM like ARG directive to define arguments to functions, in
1701 * the following form:
1703 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1705 offset = ArgOffset;
1706 do {
1707 char *arg, directive[256];
1708 int size = StackSize;
1710 /* Find the argument name */
1711 tline = tline->next;
1712 if (tline && tline->type == TOK_WHITESPACE)
1713 tline = tline->next;
1714 if (!tline || tline->type != TOK_ID) {
1715 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1716 free_tlist(origline);
1717 return DIRECTIVE_FOUND;
1719 arg = tline->text;
1721 /* Find the argument size type */
1722 tline = tline->next;
1723 if (!tline || tline->type != TOK_OTHER
1724 || tline->text[0] != ':') {
1725 error(ERR_NONFATAL,
1726 "Syntax error processing `%%arg' directive");
1727 free_tlist(origline);
1728 return DIRECTIVE_FOUND;
1730 tline = tline->next;
1731 if (!tline || tline->type != TOK_ID) {
1732 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1733 free_tlist(origline);
1734 return DIRECTIVE_FOUND;
1737 /* Allow macro expansion of type parameter */
1738 tt = tokenize(tline->text);
1739 tt = expand_smacro(tt);
1740 if (nasm_stricmp(tt->text, "byte") == 0) {
1741 size = MAX(StackSize, 1);
1742 } else if (nasm_stricmp(tt->text, "word") == 0) {
1743 size = MAX(StackSize, 2);
1744 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1745 size = MAX(StackSize, 4);
1746 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1747 size = MAX(StackSize, 8);
1748 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1749 size = MAX(StackSize, 10);
1750 } else {
1751 error(ERR_NONFATAL,
1752 "Invalid size type for `%%arg' missing directive");
1753 free_tlist(tt);
1754 free_tlist(origline);
1755 return DIRECTIVE_FOUND;
1757 free_tlist(tt);
1759 /* Now define the macro for the argument */
1760 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1761 arg, StackPointer, offset);
1762 do_directive(tokenize(directive));
1763 offset += size;
1765 /* Move to the next argument in the list */
1766 tline = tline->next;
1767 if (tline && tline->type == TOK_WHITESPACE)
1768 tline = tline->next;
1770 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1771 free_tlist(origline);
1772 return DIRECTIVE_FOUND;
1774 case PP_LOCAL:
1775 /* TASM like LOCAL directive to define local variables for a
1776 * function, in the following form:
1778 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1780 * The '= LocalSize' at the end is ignored by NASM, but is
1781 * required by TASM to define the local parameter size (and used
1782 * by the TASM macro package).
1784 offset = LocalOffset;
1785 do {
1786 char *local, directive[256];
1787 int size = StackSize;
1789 /* Find the argument name */
1790 tline = tline->next;
1791 if (tline && tline->type == TOK_WHITESPACE)
1792 tline = tline->next;
1793 if (!tline || tline->type != TOK_ID) {
1794 error(ERR_NONFATAL,
1795 "`%%local' missing argument parameter");
1796 free_tlist(origline);
1797 return DIRECTIVE_FOUND;
1799 local = tline->text;
1801 /* Find the argument size type */
1802 tline = tline->next;
1803 if (!tline || tline->type != TOK_OTHER
1804 || tline->text[0] != ':') {
1805 error(ERR_NONFATAL,
1806 "Syntax error processing `%%local' directive");
1807 free_tlist(origline);
1808 return DIRECTIVE_FOUND;
1810 tline = tline->next;
1811 if (!tline || tline->type != TOK_ID) {
1812 error(ERR_NONFATAL,
1813 "`%%local' missing size type parameter");
1814 free_tlist(origline);
1815 return DIRECTIVE_FOUND;
1818 /* Allow macro expansion of type parameter */
1819 tt = tokenize(tline->text);
1820 tt = expand_smacro(tt);
1821 if (nasm_stricmp(tt->text, "byte") == 0) {
1822 size = MAX(StackSize, 1);
1823 } else if (nasm_stricmp(tt->text, "word") == 0) {
1824 size = MAX(StackSize, 2);
1825 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1826 size = MAX(StackSize, 4);
1827 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1828 size = MAX(StackSize, 8);
1829 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1830 size = MAX(StackSize, 10);
1831 } else {
1832 error(ERR_NONFATAL,
1833 "Invalid size type for `%%local' missing directive");
1834 free_tlist(tt);
1835 free_tlist(origline);
1836 return DIRECTIVE_FOUND;
1838 free_tlist(tt);
1840 /* Now define the macro for the argument */
1841 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1842 local, StackPointer, offset);
1843 do_directive(tokenize(directive));
1844 offset += size;
1846 /* Now define the assign to setup the enter_c macro correctly */
1847 snprintf(directive, sizeof(directive),
1848 "%%assign %%$localsize %%$localsize+%d", size);
1849 do_directive(tokenize(directive));
1851 /* Move to the next argument in the list */
1852 tline = tline->next;
1853 if (tline && tline->type == TOK_WHITESPACE)
1854 tline = tline->next;
1856 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1857 free_tlist(origline);
1858 return DIRECTIVE_FOUND;
1860 case PP_CLEAR:
1861 if (tline->next)
1862 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1863 free_macros();
1864 init_macros();
1865 free_tlist(origline);
1866 return DIRECTIVE_FOUND;
1868 case PP_INCLUDE:
1869 tline = tline->next;
1870 skip_white_(tline);
1871 if (!tline || (tline->type != TOK_STRING &&
1872 tline->type != TOK_INTERNAL_STRING)) {
1873 error(ERR_NONFATAL, "`%%include' expects a file name");
1874 free_tlist(origline);
1875 return DIRECTIVE_FOUND; /* but we did _something_ */
1877 if (tline->next)
1878 error(ERR_WARNING,
1879 "trailing garbage after `%%include' ignored");
1880 if (tline->type != TOK_INTERNAL_STRING) {
1881 p = tline->text + 1; /* point past the quote to the name */
1882 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1883 } else
1884 p = tline->text; /* internal_string is easier */
1885 expand_macros_in_string(&p);
1886 inc = nasm_malloc(sizeof(Include));
1887 inc->next = istk;
1888 inc->conds = NULL;
1889 inc->fp = inc_fopen(p);
1890 if (!inc->fp && pass == 0) {
1891 /* -MG given but file not found */
1892 nasm_free(inc);
1893 } else {
1894 inc->fname = src_set_fname(p);
1895 inc->lineno = src_set_linnum(0);
1896 inc->lineinc = 1;
1897 inc->expansion = NULL;
1898 inc->mstk = NULL;
1899 istk = inc;
1900 list->uplevel(LIST_INCLUDE);
1902 free_tlist(origline);
1903 return DIRECTIVE_FOUND;
1905 case PP_PUSH:
1906 tline = tline->next;
1907 skip_white_(tline);
1908 tline = expand_id(tline);
1909 if (!tok_type_(tline, TOK_ID)) {
1910 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1911 free_tlist(origline);
1912 return DIRECTIVE_FOUND; /* but we did _something_ */
1914 if (tline->next)
1915 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1916 ctx = nasm_malloc(sizeof(Context));
1917 ctx->next = cstk;
1918 ctx->localmac = NULL;
1919 ctx->name = nasm_strdup(tline->text);
1920 ctx->number = unique++;
1921 cstk = ctx;
1922 free_tlist(origline);
1923 break;
1925 case PP_REPL:
1926 tline = tline->next;
1927 skip_white_(tline);
1928 tline = expand_id(tline);
1929 if (!tok_type_(tline, TOK_ID)) {
1930 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1931 free_tlist(origline);
1932 return DIRECTIVE_FOUND; /* but we did _something_ */
1934 if (tline->next)
1935 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1936 if (!cstk)
1937 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1938 else {
1939 nasm_free(cstk->name);
1940 cstk->name = nasm_strdup(tline->text);
1942 free_tlist(origline);
1943 break;
1945 case PP_POP:
1946 if (tline->next)
1947 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1948 if (!cstk)
1949 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1950 else
1951 ctx_pop();
1952 free_tlist(origline);
1953 break;
1955 case PP_ERROR:
1956 tline->next = expand_smacro(tline->next);
1957 tline = tline->next;
1958 skip_white_(tline);
1959 if (tok_type_(tline, TOK_STRING)) {
1960 p = tline->text + 1; /* point past the quote to the name */
1961 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1962 expand_macros_in_string(&p);
1963 error(ERR_NONFATAL, "%s", p);
1964 nasm_free(p);
1965 } else {
1966 p = detoken(tline, false);
1967 error(ERR_WARNING, "%s", p);
1968 nasm_free(p);
1970 free_tlist(origline);
1971 break;
1973 CASE_PP_IF:
1974 if (istk->conds && !emitting(istk->conds->state))
1975 j = COND_NEVER;
1976 else {
1977 j = if_condition(tline->next, i);
1978 tline->next = NULL; /* it got freed */
1979 free_tlist(origline);
1980 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1982 cond = nasm_malloc(sizeof(Cond));
1983 cond->next = istk->conds;
1984 cond->state = j;
1985 istk->conds = cond;
1986 return DIRECTIVE_FOUND;
1988 CASE_PP_ELIF:
1989 if (!istk->conds)
1990 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
1991 if (emitting(istk->conds->state)
1992 || istk->conds->state == COND_NEVER)
1993 istk->conds->state = COND_NEVER;
1994 else {
1996 * IMPORTANT: In the case of %if, we will already have
1997 * called expand_mmac_params(); however, if we're
1998 * processing an %elif we must have been in a
1999 * non-emitting mode, which would have inhibited
2000 * the normal invocation of expand_mmac_params(). Therefore,
2001 * we have to do it explicitly here.
2003 j = if_condition(expand_mmac_params(tline->next), i);
2004 tline->next = NULL; /* it got freed */
2005 free_tlist(origline);
2006 istk->conds->state =
2007 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2009 return DIRECTIVE_FOUND;
2011 case PP_ELSE:
2012 if (tline->next)
2013 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2014 if (!istk->conds)
2015 error(ERR_FATAL, "`%%else': no matching `%%if'");
2016 if (emitting(istk->conds->state)
2017 || istk->conds->state == COND_NEVER)
2018 istk->conds->state = COND_ELSE_FALSE;
2019 else
2020 istk->conds->state = COND_ELSE_TRUE;
2021 free_tlist(origline);
2022 return DIRECTIVE_FOUND;
2024 case PP_ENDIF:
2025 if (tline->next)
2026 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2027 if (!istk->conds)
2028 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2029 cond = istk->conds;
2030 istk->conds = cond->next;
2031 nasm_free(cond);
2032 free_tlist(origline);
2033 return DIRECTIVE_FOUND;
2035 case PP_MACRO:
2036 case PP_IMACRO:
2037 if (defining)
2038 error(ERR_FATAL,
2039 "`%%%smacro': already defining a macro",
2040 (i == PP_IMACRO ? "i" : ""));
2041 tline = tline->next;
2042 skip_white_(tline);
2043 tline = expand_id(tline);
2044 if (!tok_type_(tline, TOK_ID)) {
2045 error(ERR_NONFATAL,
2046 "`%%%smacro' expects a macro name",
2047 (i == PP_IMACRO ? "i" : ""));
2048 return DIRECTIVE_FOUND;
2050 defining = nasm_malloc(sizeof(MMacro));
2051 defining->name = nasm_strdup(tline->text);
2052 defining->casesense = (i == PP_MACRO);
2053 defining->plus = false;
2054 defining->nolist = false;
2055 defining->in_progress = 0;
2056 defining->rep_nest = NULL;
2057 tline = expand_smacro(tline->next);
2058 skip_white_(tline);
2059 if (!tok_type_(tline, TOK_NUMBER)) {
2060 error(ERR_NONFATAL,
2061 "`%%%smacro' expects a parameter count",
2062 (i == PP_IMACRO ? "i" : ""));
2063 defining->nparam_min = defining->nparam_max = 0;
2064 } else {
2065 defining->nparam_min = defining->nparam_max =
2066 readnum(tline->text, &err);
2067 if (err)
2068 error(ERR_NONFATAL,
2069 "unable to parse parameter count `%s'", tline->text);
2071 if (tline && tok_is_(tline->next, "-")) {
2072 tline = tline->next->next;
2073 if (tok_is_(tline, "*"))
2074 defining->nparam_max = INT_MAX;
2075 else if (!tok_type_(tline, TOK_NUMBER))
2076 error(ERR_NONFATAL,
2077 "`%%%smacro' expects a parameter count after `-'",
2078 (i == PP_IMACRO ? "i" : ""));
2079 else {
2080 defining->nparam_max = readnum(tline->text, &err);
2081 if (err)
2082 error(ERR_NONFATAL,
2083 "unable to parse parameter count `%s'",
2084 tline->text);
2085 if (defining->nparam_min > defining->nparam_max)
2086 error(ERR_NONFATAL,
2087 "minimum parameter count exceeds maximum");
2090 if (tline && tok_is_(tline->next, "+")) {
2091 tline = tline->next;
2092 defining->plus = true;
2094 if (tline && tok_type_(tline->next, TOK_ID) &&
2095 !nasm_stricmp(tline->next->text, ".nolist")) {
2096 tline = tline->next;
2097 defining->nolist = true;
2099 mmac = (MMacro *) hash_findix(mmacros, defining->name);
2100 while (mmac) {
2101 if (!strcmp(mmac->name, defining->name) &&
2102 (mmac->nparam_min <= defining->nparam_max
2103 || defining->plus)
2104 && (defining->nparam_min <= mmac->nparam_max
2105 || mmac->plus)) {
2106 error(ERR_WARNING,
2107 "redefining multi-line macro `%s'", defining->name);
2108 break;
2110 mmac = mmac->next;
2113 * Handle default parameters.
2115 if (tline && tline->next) {
2116 defining->dlist = tline->next;
2117 tline->next = NULL;
2118 count_mmac_params(defining->dlist, &defining->ndefs,
2119 &defining->defaults);
2120 } else {
2121 defining->dlist = NULL;
2122 defining->defaults = NULL;
2124 defining->expansion = NULL;
2125 free_tlist(origline);
2126 return DIRECTIVE_FOUND;
2128 case PP_ENDM:
2129 case PP_ENDMACRO:
2130 if (!defining) {
2131 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2132 return DIRECTIVE_FOUND;
2134 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2135 defining->next = *mmhead;
2136 *mmhead = defining;
2137 defining = NULL;
2138 free_tlist(origline);
2139 return DIRECTIVE_FOUND;
2141 case PP_ROTATE:
2142 if (tline->next && tline->next->type == TOK_WHITESPACE)
2143 tline = tline->next;
2144 if (tline->next == NULL) {
2145 free_tlist(origline);
2146 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2147 return DIRECTIVE_FOUND;
2149 t = expand_smacro(tline->next);
2150 tline->next = NULL;
2151 free_tlist(origline);
2152 tline = t;
2153 tptr = &t;
2154 tokval.t_type = TOKEN_INVALID;
2155 evalresult =
2156 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2157 free_tlist(tline);
2158 if (!evalresult)
2159 return DIRECTIVE_FOUND;
2160 if (tokval.t_type)
2161 error(ERR_WARNING,
2162 "trailing garbage after expression ignored");
2163 if (!is_simple(evalresult)) {
2164 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2165 return DIRECTIVE_FOUND;
2167 mmac = istk->mstk;
2168 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2169 mmac = mmac->next_active;
2170 if (!mmac) {
2171 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2172 } else if (mmac->nparam == 0) {
2173 error(ERR_NONFATAL,
2174 "`%%rotate' invoked within macro without parameters");
2175 } else {
2176 int rotate = mmac->rotate + reloc_value(evalresult);
2178 rotate %= (int)mmac->nparam;
2179 if (rotate < 0)
2180 rotate += mmac->nparam;
2182 mmac->rotate = rotate;
2184 return DIRECTIVE_FOUND;
2186 case PP_REP:
2187 nolist = false;
2188 do {
2189 tline = tline->next;
2190 } while (tok_type_(tline, TOK_WHITESPACE));
2192 if (tok_type_(tline, TOK_ID) &&
2193 nasm_stricmp(tline->text, ".nolist") == 0) {
2194 nolist = true;
2195 do {
2196 tline = tline->next;
2197 } while (tok_type_(tline, TOK_WHITESPACE));
2200 if (tline) {
2201 t = expand_smacro(tline);
2202 tptr = &t;
2203 tokval.t_type = TOKEN_INVALID;
2204 evalresult =
2205 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2206 if (!evalresult) {
2207 free_tlist(origline);
2208 return DIRECTIVE_FOUND;
2210 if (tokval.t_type)
2211 error(ERR_WARNING,
2212 "trailing garbage after expression ignored");
2213 if (!is_simple(evalresult)) {
2214 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2215 return DIRECTIVE_FOUND;
2217 count = reloc_value(evalresult) + 1;
2218 } else {
2219 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2220 count = 0;
2222 free_tlist(origline);
2224 tmp_defining = defining;
2225 defining = nasm_malloc(sizeof(MMacro));
2226 defining->name = NULL; /* flags this macro as a %rep block */
2227 defining->casesense = false;
2228 defining->plus = false;
2229 defining->nolist = nolist;
2230 defining->in_progress = count;
2231 defining->nparam_min = defining->nparam_max = 0;
2232 defining->defaults = NULL;
2233 defining->dlist = NULL;
2234 defining->expansion = NULL;
2235 defining->next_active = istk->mstk;
2236 defining->rep_nest = tmp_defining;
2237 return DIRECTIVE_FOUND;
2239 case PP_ENDREP:
2240 if (!defining || defining->name) {
2241 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2242 return DIRECTIVE_FOUND;
2246 * Now we have a "macro" defined - although it has no name
2247 * and we won't be entering it in the hash tables - we must
2248 * push a macro-end marker for it on to istk->expansion.
2249 * After that, it will take care of propagating itself (a
2250 * macro-end marker line for a macro which is really a %rep
2251 * block will cause the macro to be re-expanded, complete
2252 * with another macro-end marker to ensure the process
2253 * continues) until the whole expansion is forcibly removed
2254 * from istk->expansion by a %exitrep.
2256 l = nasm_malloc(sizeof(Line));
2257 l->next = istk->expansion;
2258 l->finishes = defining;
2259 l->first = NULL;
2260 istk->expansion = l;
2262 istk->mstk = defining;
2264 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2265 tmp_defining = defining;
2266 defining = defining->rep_nest;
2267 free_tlist(origline);
2268 return DIRECTIVE_FOUND;
2270 case PP_EXITREP:
2272 * We must search along istk->expansion until we hit a
2273 * macro-end marker for a macro with no name. Then we set
2274 * its `in_progress' flag to 0.
2276 for (l = istk->expansion; l; l = l->next)
2277 if (l->finishes && !l->finishes->name)
2278 break;
2280 if (l)
2281 l->finishes->in_progress = 0;
2282 else
2283 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2284 free_tlist(origline);
2285 return DIRECTIVE_FOUND;
2287 case PP_XDEFINE:
2288 case PP_IXDEFINE:
2289 case PP_DEFINE:
2290 case PP_IDEFINE:
2291 tline = tline->next;
2292 skip_white_(tline);
2293 tline = expand_id(tline);
2294 if (!tline || (tline->type != TOK_ID &&
2295 (tline->type != TOK_PREPROC_ID ||
2296 tline->text[1] != '$'))) {
2297 error(ERR_NONFATAL,
2298 "`%%%s%sdefine' expects a macro identifier",
2299 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2300 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
2305 ctx = get_ctx(tline->text, false);
2307 mname = tline->text;
2308 last = tline;
2309 param_start = tline = tline->next;
2310 nparam = 0;
2312 /* Expand the macro definition now for %xdefine and %ixdefine */
2313 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2314 tline = expand_smacro(tline);
2316 if (tok_is_(tline, "(")) {
2318 * This macro has parameters.
2321 tline = tline->next;
2322 while (1) {
2323 skip_white_(tline);
2324 if (!tline) {
2325 error(ERR_NONFATAL, "parameter identifier expected");
2326 free_tlist(origline);
2327 return DIRECTIVE_FOUND;
2329 if (tline->type != TOK_ID) {
2330 error(ERR_NONFATAL,
2331 "`%s': parameter identifier expected",
2332 tline->text);
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2336 tline->type = TOK_SMAC_PARAM + nparam++;
2337 tline = tline->next;
2338 skip_white_(tline);
2339 if (tok_is_(tline, ",")) {
2340 tline = tline->next;
2341 continue;
2343 if (!tok_is_(tline, ")")) {
2344 error(ERR_NONFATAL,
2345 "`)' expected to terminate macro template");
2346 free_tlist(origline);
2347 return DIRECTIVE_FOUND;
2349 break;
2351 last = tline;
2352 tline = tline->next;
2354 if (tok_type_(tline, TOK_WHITESPACE))
2355 last = tline, tline = tline->next;
2356 macro_start = NULL;
2357 last->next = NULL;
2358 t = tline;
2359 while (t) {
2360 if (t->type == TOK_ID) {
2361 for (tt = param_start; tt; tt = tt->next)
2362 if (tt->type >= TOK_SMAC_PARAM &&
2363 !strcmp(tt->text, t->text))
2364 t->type = tt->type;
2366 tt = t->next;
2367 t->next = macro_start;
2368 macro_start = t;
2369 t = tt;
2372 * Good. We now have a macro name, a parameter count, and a
2373 * token list (in reverse order) for an expansion. We ought
2374 * to be OK just to create an SMacro, store it, and let
2375 * free_tlist have the rest of the line (which we have
2376 * carefully re-terminated after chopping off the expansion
2377 * from the end).
2379 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2380 if (!smac) {
2381 error(ERR_WARNING,
2382 "single-line macro `%s' defined both with and"
2383 " without parameters", mname);
2384 free_tlist(origline);
2385 free_tlist(macro_start);
2386 return DIRECTIVE_FOUND;
2387 } else {
2389 * We're redefining, so we have to take over an
2390 * existing SMacro structure. This means freeing
2391 * what was already in it.
2393 nasm_free(smac->name);
2394 free_tlist(smac->expansion);
2396 } else {
2397 if (!ctx)
2398 smhead = (SMacro **) hash_findi_add(smacros, mname);
2399 else
2400 smhead = &ctx->localmac;
2402 smac = nasm_malloc(sizeof(SMacro));
2403 smac->next = *smhead;
2404 *smhead = smac;
2406 smac->name = nasm_strdup(mname);
2407 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2408 smac->nparam = nparam;
2409 smac->expansion = macro_start;
2410 smac->in_progress = false;
2411 free_tlist(origline);
2412 return DIRECTIVE_FOUND;
2414 case PP_UNDEF:
2415 tline = tline->next;
2416 skip_white_(tline);
2417 tline = expand_id(tline);
2418 if (!tline || (tline->type != TOK_ID &&
2419 (tline->type != TOK_PREPROC_ID ||
2420 tline->text[1] != '$'))) {
2421 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2422 free_tlist(origline);
2423 return DIRECTIVE_FOUND;
2425 if (tline->next) {
2426 error(ERR_WARNING,
2427 "trailing garbage after macro name ignored");
2430 /* Find the context that symbol belongs to */
2431 ctx = get_ctx(tline->text, false);
2432 if (!ctx)
2433 smhead = (SMacro **) hash_findi(smacros, tline->text, NULL);
2434 else
2435 smhead = &ctx->localmac;
2437 if (smhead) {
2438 SMacro *s, **sp;
2440 mname = tline->text;
2441 last = tline;
2442 last->next = NULL;
2445 * We now have a macro name... go hunt for it.
2447 sp = smhead;
2448 while ((s = *sp) != NULL) {
2449 if (!mstrcmp(s->name, tline->text, s->casesense)) {
2450 *sp = s->next;
2451 nasm_free(s->name);
2452 free_tlist(s->expansion);
2453 nasm_free(s);
2454 } else {
2455 sp = &s->next;
2458 free_tlist(origline);
2460 return DIRECTIVE_FOUND;
2462 case PP_STRLEN:
2463 tline = tline->next;
2464 skip_white_(tline);
2465 tline = expand_id(tline);
2466 if (!tline || (tline->type != TOK_ID &&
2467 (tline->type != TOK_PREPROC_ID ||
2468 tline->text[1] != '$'))) {
2469 error(ERR_NONFATAL,
2470 "`%%strlen' expects a macro identifier as first parameter");
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2474 ctx = get_ctx(tline->text, false);
2476 mname = tline->text;
2477 last = tline;
2478 tline = expand_smacro(tline->next);
2479 last->next = NULL;
2481 t = tline;
2482 while (tok_type_(t, TOK_WHITESPACE))
2483 t = t->next;
2484 /* t should now point to the string */
2485 if (t->type != TOK_STRING) {
2486 error(ERR_NONFATAL,
2487 "`%%strlen` requires string as second parameter");
2488 free_tlist(tline);
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
2493 macro_start = nasm_malloc(sizeof(*macro_start));
2494 macro_start->next = NULL;
2495 make_tok_num(macro_start, strlen(t->text) - 2);
2496 macro_start->mac = NULL;
2499 * We now have a macro name, an implicit parameter count of
2500 * zero, and a numeric token to use as an expansion. Create
2501 * and store an SMacro.
2503 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2504 if (!smac)
2505 error(ERR_WARNING,
2506 "single-line macro `%s' defined both with and"
2507 " without parameters", mname);
2508 else {
2510 * We're redefining, so we have to take over an
2511 * existing SMacro structure. This means freeing
2512 * what was already in it.
2514 nasm_free(smac->name);
2515 free_tlist(smac->expansion);
2517 } else {
2518 if (!ctx)
2519 smhead = (SMacro **) hash_findi_add(smacros, mname);
2520 else
2521 smhead = &ctx->localmac;
2523 smac = nasm_malloc(sizeof(SMacro));
2524 smac->next = *smhead;
2525 *smhead = smac;
2527 smac->name = nasm_strdup(mname);
2528 smac->casesense = (i == PP_STRLEN);
2529 smac->nparam = 0;
2530 smac->expansion = macro_start;
2531 smac->in_progress = false;
2532 free_tlist(tline);
2533 free_tlist(origline);
2534 return DIRECTIVE_FOUND;
2536 case PP_SUBSTR:
2537 tline = tline->next;
2538 skip_white_(tline);
2539 tline = expand_id(tline);
2540 if (!tline || (tline->type != TOK_ID &&
2541 (tline->type != TOK_PREPROC_ID ||
2542 tline->text[1] != '$'))) {
2543 error(ERR_NONFATAL,
2544 "`%%substr' expects a macro identifier as first parameter");
2545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
2548 ctx = get_ctx(tline->text, false);
2550 mname = tline->text;
2551 last = tline;
2552 tline = expand_smacro(tline->next);
2553 last->next = NULL;
2555 t = tline->next;
2556 while (tok_type_(t, TOK_WHITESPACE))
2557 t = t->next;
2559 /* t should now point to the string */
2560 if (t->type != TOK_STRING) {
2561 error(ERR_NONFATAL,
2562 "`%%substr` requires string as second parameter");
2563 free_tlist(tline);
2564 free_tlist(origline);
2565 return DIRECTIVE_FOUND;
2568 tt = t->next;
2569 tptr = &tt;
2570 tokval.t_type = TOKEN_INVALID;
2571 evalresult =
2572 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2573 if (!evalresult) {
2574 free_tlist(tline);
2575 free_tlist(origline);
2576 return DIRECTIVE_FOUND;
2578 if (!is_simple(evalresult)) {
2579 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2580 free_tlist(tline);
2581 free_tlist(origline);
2582 return DIRECTIVE_FOUND;
2585 macro_start = nasm_malloc(sizeof(*macro_start));
2586 macro_start->next = NULL;
2587 macro_start->text = nasm_strdup("'''");
2588 if (evalresult->value > 0
2589 && evalresult->value < strlen(t->text) - 1) {
2590 macro_start->text[1] = t->text[evalresult->value];
2591 } else {
2592 macro_start->text[2] = '\0';
2594 macro_start->type = TOK_STRING;
2595 macro_start->mac = NULL;
2598 * We now have a macro name, an implicit parameter count of
2599 * zero, and a numeric token to use as an expansion. Create
2600 * and store an SMacro.
2602 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2603 if (!smac)
2604 error(ERR_WARNING,
2605 "single-line macro `%s' defined both with and"
2606 " without parameters", mname);
2607 else {
2609 * We're redefining, so we have to take over an
2610 * existing SMacro structure. This means freeing
2611 * what was already in it.
2613 nasm_free(smac->name);
2614 free_tlist(smac->expansion);
2616 } else {
2617 if (!ctx)
2618 smhead = (SMacro **) hash_findi_add(smacros, tline->text);
2619 else
2620 smhead = &ctx->localmac;
2622 smac = nasm_malloc(sizeof(SMacro));
2623 smac->next = *smhead;
2624 *smhead = smac;
2626 smac->name = nasm_strdup(mname);
2627 smac->casesense = (i == PP_SUBSTR);
2628 smac->nparam = 0;
2629 smac->expansion = macro_start;
2630 smac->in_progress = false;
2631 free_tlist(tline);
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND;
2635 case PP_ASSIGN:
2636 case PP_IASSIGN:
2637 tline = tline->next;
2638 skip_white_(tline);
2639 tline = expand_id(tline);
2640 if (!tline || (tline->type != TOK_ID &&
2641 (tline->type != TOK_PREPROC_ID ||
2642 tline->text[1] != '$'))) {
2643 error(ERR_NONFATAL,
2644 "`%%%sassign' expects a macro identifier",
2645 (i == PP_IASSIGN ? "i" : ""));
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
2649 ctx = get_ctx(tline->text, false);
2651 mname = tline->text;
2652 last = tline;
2653 tline = expand_smacro(tline->next);
2654 last->next = NULL;
2656 t = tline;
2657 tptr = &t;
2658 tokval.t_type = TOKEN_INVALID;
2659 evalresult =
2660 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2661 free_tlist(tline);
2662 if (!evalresult) {
2663 free_tlist(origline);
2664 return DIRECTIVE_FOUND;
2667 if (tokval.t_type)
2668 error(ERR_WARNING,
2669 "trailing garbage after expression ignored");
2671 if (!is_simple(evalresult)) {
2672 error(ERR_NONFATAL,
2673 "non-constant value given to `%%%sassign'",
2674 (i == PP_IASSIGN ? "i" : ""));
2675 free_tlist(origline);
2676 return DIRECTIVE_FOUND;
2679 macro_start = nasm_malloc(sizeof(*macro_start));
2680 macro_start->next = NULL;
2681 make_tok_num(macro_start, reloc_value(evalresult));
2682 macro_start->mac = NULL;
2685 * We now have a macro name, an implicit parameter count of
2686 * zero, and a numeric token to use as an expansion. Create
2687 * and store an SMacro.
2689 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2690 if (!smac)
2691 error(ERR_WARNING,
2692 "single-line macro `%s' defined both with and"
2693 " without parameters", mname);
2694 else {
2696 * We're redefining, so we have to take over an
2697 * existing SMacro structure. This means freeing
2698 * what was already in it.
2700 nasm_free(smac->name);
2701 free_tlist(smac->expansion);
2703 } else {
2704 if (!ctx)
2705 smhead = (SMacro **) hash_findi_add(smacros, mname);
2706 else
2707 smhead = &ctx->localmac;
2709 smac = nasm_malloc(sizeof(SMacro));
2710 smac->next = *smhead;
2711 *smhead = smac;
2713 smac->name = nasm_strdup(mname);
2714 smac->casesense = (i == PP_ASSIGN);
2715 smac->nparam = 0;
2716 smac->expansion = macro_start;
2717 smac->in_progress = false;
2718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
2721 case PP_LINE:
2723 * Syntax is `%line nnn[+mmm] [filename]'
2725 tline = tline->next;
2726 skip_white_(tline);
2727 if (!tok_type_(tline, TOK_NUMBER)) {
2728 error(ERR_NONFATAL, "`%%line' expects line number");
2729 free_tlist(origline);
2730 return DIRECTIVE_FOUND;
2732 k = readnum(tline->text, &err);
2733 m = 1;
2734 tline = tline->next;
2735 if (tok_is_(tline, "+")) {
2736 tline = tline->next;
2737 if (!tok_type_(tline, TOK_NUMBER)) {
2738 error(ERR_NONFATAL, "`%%line' expects line increment");
2739 free_tlist(origline);
2740 return DIRECTIVE_FOUND;
2742 m = readnum(tline->text, &err);
2743 tline = tline->next;
2745 skip_white_(tline);
2746 src_set_linnum(k);
2747 istk->lineinc = m;
2748 if (tline) {
2749 nasm_free(src_set_fname(detoken(tline, false)));
2751 free_tlist(origline);
2752 return DIRECTIVE_FOUND;
2754 default:
2755 error(ERR_FATAL,
2756 "preprocessor directive `%s' not yet implemented",
2757 pp_directives[i]);
2758 break;
2760 return DIRECTIVE_FOUND;
2764 * Ensure that a macro parameter contains a condition code and
2765 * nothing else. Return the condition code index if so, or -1
2766 * otherwise.
2768 static int find_cc(Token * t)
2770 Token *tt;
2771 int i, j, k, m;
2773 if (!t)
2774 return -1; /* Probably a %+ without a space */
2776 skip_white_(t);
2777 if (t->type != TOK_ID)
2778 return -1;
2779 tt = t->next;
2780 skip_white_(tt);
2781 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2782 return -1;
2784 i = -1;
2785 j = elements(conditions);
2786 while (j - i > 1) {
2787 k = (j + i) / 2;
2788 m = nasm_stricmp(t->text, conditions[k]);
2789 if (m == 0) {
2790 i = k;
2791 j = -2;
2792 break;
2793 } else if (m < 0) {
2794 j = k;
2795 } else
2796 i = k;
2798 if (j != -2)
2799 return -1;
2800 return i;
2804 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2805 * %-n) and MMacro-local identifiers (%%foo).
2807 static Token *expand_mmac_params(Token * tline)
2809 Token *t, *tt, **tail, *thead;
2811 tail = &thead;
2812 thead = NULL;
2814 while (tline) {
2815 if (tline->type == TOK_PREPROC_ID &&
2816 (((tline->text[1] == '+' || tline->text[1] == '-')
2817 && tline->text[2]) || tline->text[1] == '%'
2818 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2819 char *text = NULL;
2820 int type = 0, cc; /* type = 0 to placate optimisers */
2821 char tmpbuf[30];
2822 unsigned int n;
2823 int i;
2824 MMacro *mac;
2826 t = tline;
2827 tline = tline->next;
2829 mac = istk->mstk;
2830 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2831 mac = mac->next_active;
2832 if (!mac)
2833 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2834 else
2835 switch (t->text[1]) {
2837 * We have to make a substitution of one of the
2838 * forms %1, %-1, %+1, %%foo, %0.
2840 case '0':
2841 type = TOK_NUMBER;
2842 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2843 text = nasm_strdup(tmpbuf);
2844 break;
2845 case '%':
2846 type = TOK_ID;
2847 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2848 mac->unique);
2849 text = nasm_strcat(tmpbuf, t->text + 2);
2850 break;
2851 case '-':
2852 n = atoi(t->text + 2) - 1;
2853 if (n >= mac->nparam)
2854 tt = NULL;
2855 else {
2856 if (mac->nparam > 1)
2857 n = (n + mac->rotate) % mac->nparam;
2858 tt = mac->params[n];
2860 cc = find_cc(tt);
2861 if (cc == -1) {
2862 error(ERR_NONFATAL,
2863 "macro parameter %d is not a condition code",
2864 n + 1);
2865 text = NULL;
2866 } else {
2867 type = TOK_ID;
2868 if (inverse_ccs[cc] == -1) {
2869 error(ERR_NONFATAL,
2870 "condition code `%s' is not invertible",
2871 conditions[cc]);
2872 text = NULL;
2873 } else
2874 text =
2875 nasm_strdup(conditions[inverse_ccs[cc]]);
2877 break;
2878 case '+':
2879 n = atoi(t->text + 2) - 1;
2880 if (n >= mac->nparam)
2881 tt = NULL;
2882 else {
2883 if (mac->nparam > 1)
2884 n = (n + mac->rotate) % mac->nparam;
2885 tt = mac->params[n];
2887 cc = find_cc(tt);
2888 if (cc == -1) {
2889 error(ERR_NONFATAL,
2890 "macro parameter %d is not a condition code",
2891 n + 1);
2892 text = NULL;
2893 } else {
2894 type = TOK_ID;
2895 text = nasm_strdup(conditions[cc]);
2897 break;
2898 default:
2899 n = atoi(t->text + 1) - 1;
2900 if (n >= mac->nparam)
2901 tt = NULL;
2902 else {
2903 if (mac->nparam > 1)
2904 n = (n + mac->rotate) % mac->nparam;
2905 tt = mac->params[n];
2907 if (tt) {
2908 for (i = 0; i < mac->paramlen[n]; i++) {
2909 *tail = new_Token(NULL, tt->type, tt->text, 0);
2910 tail = &(*tail)->next;
2911 tt = tt->next;
2914 text = NULL; /* we've done it here */
2915 break;
2917 if (!text) {
2918 delete_Token(t);
2919 } else {
2920 *tail = t;
2921 tail = &t->next;
2922 t->type = type;
2923 nasm_free(t->text);
2924 t->text = text;
2925 t->mac = NULL;
2927 continue;
2928 } else {
2929 t = *tail = tline;
2930 tline = tline->next;
2931 t->mac = NULL;
2932 tail = &t->next;
2935 *tail = NULL;
2936 t = thead;
2937 for (; t && (tt = t->next) != NULL; t = t->next)
2938 switch (t->type) {
2939 case TOK_WHITESPACE:
2940 if (tt->type == TOK_WHITESPACE) {
2941 t->next = delete_Token(tt);
2943 break;
2944 case TOK_ID:
2945 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2946 char *tmp = nasm_strcat(t->text, tt->text);
2947 nasm_free(t->text);
2948 t->text = tmp;
2949 t->next = delete_Token(tt);
2951 break;
2952 case TOK_NUMBER:
2953 if (tt->type == TOK_NUMBER) {
2954 char *tmp = nasm_strcat(t->text, tt->text);
2955 nasm_free(t->text);
2956 t->text = tmp;
2957 t->next = delete_Token(tt);
2959 break;
2960 default:
2961 break;
2964 return thead;
2968 * Expand all single-line macro calls made in the given line.
2969 * Return the expanded version of the line. The original is deemed
2970 * to be destroyed in the process. (In reality we'll just move
2971 * Tokens from input to output a lot of the time, rather than
2972 * actually bothering to destroy and replicate.)
2974 static Token *expand_smacro(Token * tline)
2976 Token *t, *tt, *mstart, **tail, *thead;
2977 SMacro *head = NULL, *m;
2978 Token **params;
2979 int *paramsize;
2980 unsigned int nparam, sparam;
2981 int brackets, rescan;
2982 Token *org_tline = tline;
2983 Context *ctx;
2984 char *mname;
2987 * Trick: we should avoid changing the start token pointer since it can
2988 * be contained in "next" field of other token. Because of this
2989 * we allocate a copy of first token and work with it; at the end of
2990 * routine we copy it back
2992 if (org_tline) {
2993 tline =
2994 new_Token(org_tline->next, org_tline->type, org_tline->text,
2996 tline->mac = org_tline->mac;
2997 nasm_free(org_tline->text);
2998 org_tline->text = NULL;
3001 again:
3002 tail = &thead;
3003 thead = NULL;
3005 while (tline) { /* main token loop */
3006 if ((mname = tline->text)) {
3007 /* if this token is a local macro, look in local context */
3008 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3009 ctx = get_ctx(mname, true);
3010 else
3011 ctx = NULL;
3012 if (!ctx) {
3013 head = (SMacro *) hash_findix(smacros, mname);
3014 } else {
3015 head = ctx->localmac;
3018 * We've hit an identifier. As in is_mmacro below, we first
3019 * check whether the identifier is a single-line macro at
3020 * all, then think about checking for parameters if
3021 * necessary.
3023 for (m = head; m; m = m->next)
3024 if (!mstrcmp(m->name, mname, m->casesense))
3025 break;
3026 if (m) {
3027 mstart = tline;
3028 params = NULL;
3029 paramsize = NULL;
3030 if (m->nparam == 0) {
3032 * Simple case: the macro is parameterless. Discard the
3033 * one token that the macro call took, and push the
3034 * expansion back on the to-do stack.
3036 if (!m->expansion) {
3037 if (!strcmp("__FILE__", m->name)) {
3038 int32_t num = 0;
3039 src_get(&num, &(tline->text));
3040 nasm_quote(&(tline->text));
3041 tline->type = TOK_STRING;
3042 continue;
3044 if (!strcmp("__LINE__", m->name)) {
3045 nasm_free(tline->text);
3046 make_tok_num(tline, src_get_linnum());
3047 continue;
3049 if (!strcmp("__BITS__", m->name)) {
3050 nasm_free(tline->text);
3051 make_tok_num(tline, globalbits);
3052 continue;
3054 tline = delete_Token(tline);
3055 continue;
3057 } else {
3059 * Complicated case: at least one macro with this name
3060 * exists and takes parameters. We must find the
3061 * parameters in the call, count them, find the SMacro
3062 * that corresponds to that form of the macro call, and
3063 * substitute for the parameters when we expand. What a
3064 * pain.
3066 /*tline = tline->next;
3067 skip_white_(tline); */
3068 do {
3069 t = tline->next;
3070 while (tok_type_(t, TOK_SMAC_END)) {
3071 t->mac->in_progress = false;
3072 t->text = NULL;
3073 t = tline->next = delete_Token(t);
3075 tline = t;
3076 } while (tok_type_(tline, TOK_WHITESPACE));
3077 if (!tok_is_(tline, "(")) {
3079 * This macro wasn't called with parameters: ignore
3080 * the call. (Behaviour borrowed from gnu cpp.)
3082 tline = mstart;
3083 m = NULL;
3084 } else {
3085 int paren = 0;
3086 int white = 0;
3087 brackets = 0;
3088 nparam = 0;
3089 sparam = PARAM_DELTA;
3090 params = nasm_malloc(sparam * sizeof(Token *));
3091 params[0] = tline->next;
3092 paramsize = nasm_malloc(sparam * sizeof(int));
3093 paramsize[0] = 0;
3094 while (true) { /* parameter loop */
3096 * For some unusual expansions
3097 * which concatenates function call
3099 t = tline->next;
3100 while (tok_type_(t, TOK_SMAC_END)) {
3101 t->mac->in_progress = false;
3102 t->text = NULL;
3103 t = tline->next = delete_Token(t);
3105 tline = t;
3107 if (!tline) {
3108 error(ERR_NONFATAL,
3109 "macro call expects terminating `)'");
3110 break;
3112 if (tline->type == TOK_WHITESPACE
3113 && brackets <= 0) {
3114 if (paramsize[nparam])
3115 white++;
3116 else
3117 params[nparam] = tline->next;
3118 continue; /* parameter loop */
3120 if (tline->type == TOK_OTHER
3121 && tline->text[1] == 0) {
3122 char ch = tline->text[0];
3123 if (ch == ',' && !paren && brackets <= 0) {
3124 if (++nparam >= sparam) {
3125 sparam += PARAM_DELTA;
3126 params = nasm_realloc(params,
3127 sparam *
3128 sizeof(Token
3129 *));
3130 paramsize =
3131 nasm_realloc(paramsize,
3132 sparam *
3133 sizeof(int));
3135 params[nparam] = tline->next;
3136 paramsize[nparam] = 0;
3137 white = 0;
3138 continue; /* parameter loop */
3140 if (ch == '{' &&
3141 (brackets > 0 || (brackets == 0 &&
3142 !paramsize[nparam])))
3144 if (!(brackets++)) {
3145 params[nparam] = tline->next;
3146 continue; /* parameter loop */
3149 if (ch == '}' && brackets > 0)
3150 if (--brackets == 0) {
3151 brackets = -1;
3152 continue; /* parameter loop */
3154 if (ch == '(' && !brackets)
3155 paren++;
3156 if (ch == ')' && brackets <= 0)
3157 if (--paren < 0)
3158 break;
3160 if (brackets < 0) {
3161 brackets = 0;
3162 error(ERR_NONFATAL, "braces do not "
3163 "enclose all of macro parameter");
3165 paramsize[nparam] += white + 1;
3166 white = 0;
3167 } /* parameter loop */
3168 nparam++;
3169 while (m && (m->nparam != nparam ||
3170 mstrcmp(m->name, mname,
3171 m->casesense)))
3172 m = m->next;
3173 if (!m)
3174 error(ERR_WARNING | ERR_WARN_MNP,
3175 "macro `%s' exists, "
3176 "but not taking %d parameters",
3177 mstart->text, nparam);
3180 if (m && m->in_progress)
3181 m = NULL;
3182 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3184 * Design question: should we handle !tline, which
3185 * indicates missing ')' here, or expand those
3186 * macros anyway, which requires the (t) test a few
3187 * lines down?
3189 nasm_free(params);
3190 nasm_free(paramsize);
3191 tline = mstart;
3192 } else {
3194 * Expand the macro: we are placed on the last token of the
3195 * call, so that we can easily split the call from the
3196 * following tokens. We also start by pushing an SMAC_END
3197 * token for the cycle removal.
3199 t = tline;
3200 if (t) {
3201 tline = t->next;
3202 t->next = NULL;
3204 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3205 tt->mac = m;
3206 m->in_progress = true;
3207 tline = tt;
3208 for (t = m->expansion; t; t = t->next) {
3209 if (t->type >= TOK_SMAC_PARAM) {
3210 Token *pcopy = tline, **ptail = &pcopy;
3211 Token *ttt, *pt;
3212 int i;
3214 ttt = params[t->type - TOK_SMAC_PARAM];
3215 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3216 --i >= 0;) {
3217 pt = *ptail =
3218 new_Token(tline, ttt->type, ttt->text,
3220 ptail = &pt->next;
3221 ttt = ttt->next;
3223 tline = pcopy;
3224 } else {
3225 tt = new_Token(tline, t->type, t->text, 0);
3226 tline = tt;
3231 * Having done that, get rid of the macro call, and clean
3232 * up the parameters.
3234 nasm_free(params);
3235 nasm_free(paramsize);
3236 free_tlist(mstart);
3237 continue; /* main token loop */
3242 if (tline->type == TOK_SMAC_END) {
3243 tline->mac->in_progress = false;
3244 tline = delete_Token(tline);
3245 } else {
3246 t = *tail = tline;
3247 tline = tline->next;
3248 t->mac = NULL;
3249 t->next = NULL;
3250 tail = &t->next;
3255 * Now scan the entire line and look for successive TOK_IDs that resulted
3256 * after expansion (they can't be produced by tokenize()). The successive
3257 * TOK_IDs should be concatenated.
3258 * Also we look for %+ tokens and concatenate the tokens before and after
3259 * them (without white spaces in between).
3261 t = thead;
3262 rescan = 0;
3263 while (t) {
3264 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3265 t = t->next;
3266 if (!t || !t->next)
3267 break;
3268 if (t->next->type == TOK_ID ||
3269 t->next->type == TOK_PREPROC_ID ||
3270 t->next->type == TOK_NUMBER) {
3271 char *p = nasm_strcat(t->text, t->next->text);
3272 nasm_free(t->text);
3273 t->next = delete_Token(t->next);
3274 t->text = p;
3275 rescan = 1;
3276 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3277 t->next->next->type == TOK_PREPROC_ID &&
3278 strcmp(t->next->next->text, "%+") == 0) {
3279 /* free the next whitespace, the %+ token and next whitespace */
3280 int i;
3281 for (i = 1; i <= 3; i++) {
3282 if (!t->next
3283 || (i != 2 && t->next->type != TOK_WHITESPACE))
3284 break;
3285 t->next = delete_Token(t->next);
3286 } /* endfor */
3287 } else
3288 t = t->next;
3290 /* If we concatenaded something, re-scan the line for macros */
3291 if (rescan) {
3292 tline = thead;
3293 goto again;
3296 if (org_tline) {
3297 if (thead) {
3298 *org_tline = *thead;
3299 /* since we just gave text to org_line, don't free it */
3300 thead->text = NULL;
3301 delete_Token(thead);
3302 } else {
3303 /* the expression expanded to empty line;
3304 we can't return NULL for some reasons
3305 we just set the line to a single WHITESPACE token. */
3306 memset(org_tline, 0, sizeof(*org_tline));
3307 org_tline->text = NULL;
3308 org_tline->type = TOK_WHITESPACE;
3310 thead = org_tline;
3313 return thead;
3317 * Similar to expand_smacro but used exclusively with macro identifiers
3318 * right before they are fetched in. The reason is that there can be
3319 * identifiers consisting of several subparts. We consider that if there
3320 * are more than one element forming the name, user wants a expansion,
3321 * otherwise it will be left as-is. Example:
3323 * %define %$abc cde
3325 * the identifier %$abc will be left as-is so that the handler for %define
3326 * will suck it and define the corresponding value. Other case:
3328 * %define _%$abc cde
3330 * In this case user wants name to be expanded *before* %define starts
3331 * working, so we'll expand %$abc into something (if it has a value;
3332 * otherwise it will be left as-is) then concatenate all successive
3333 * PP_IDs into one.
3335 static Token *expand_id(Token * tline)
3337 Token *cur, *oldnext = NULL;
3339 if (!tline || !tline->next)
3340 return tline;
3342 cur = tline;
3343 while (cur->next &&
3344 (cur->next->type == TOK_ID ||
3345 cur->next->type == TOK_PREPROC_ID
3346 || cur->next->type == TOK_NUMBER))
3347 cur = cur->next;
3349 /* If identifier consists of just one token, don't expand */
3350 if (cur == tline)
3351 return tline;
3353 if (cur) {
3354 oldnext = cur->next; /* Detach the tail past identifier */
3355 cur->next = NULL; /* so that expand_smacro stops here */
3358 tline = expand_smacro(tline);
3360 if (cur) {
3361 /* expand_smacro possibly changhed tline; re-scan for EOL */
3362 cur = tline;
3363 while (cur && cur->next)
3364 cur = cur->next;
3365 if (cur)
3366 cur->next = oldnext;
3369 return tline;
3373 * Determine whether the given line constitutes a multi-line macro
3374 * call, and return the MMacro structure called if so. Doesn't have
3375 * to check for an initial label - that's taken care of in
3376 * expand_mmacro - but must check numbers of parameters. Guaranteed
3377 * to be called with tline->type == TOK_ID, so the putative macro
3378 * name is easy to find.
3380 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3382 MMacro *head, *m;
3383 Token **params;
3384 int nparam;
3386 head = (MMacro *) hash_findix(mmacros, tline->text);
3389 * Efficiency: first we see if any macro exists with the given
3390 * name. If not, we can return NULL immediately. _Then_ we
3391 * count the parameters, and then we look further along the
3392 * list if necessary to find the proper MMacro.
3394 for (m = head; m; m = m->next)
3395 if (!mstrcmp(m->name, tline->text, m->casesense))
3396 break;
3397 if (!m)
3398 return NULL;
3401 * OK, we have a potential macro. Count and demarcate the
3402 * parameters.
3404 count_mmac_params(tline->next, &nparam, &params);
3407 * So we know how many parameters we've got. Find the MMacro
3408 * structure that handles this number.
3410 while (m) {
3411 if (m->nparam_min <= nparam
3412 && (m->plus || nparam <= m->nparam_max)) {
3414 * This one is right. Just check if cycle removal
3415 * prohibits us using it before we actually celebrate...
3417 if (m->in_progress) {
3418 #if 0
3419 error(ERR_NONFATAL,
3420 "self-reference in multi-line macro `%s'", m->name);
3421 #endif
3422 nasm_free(params);
3423 return NULL;
3426 * It's right, and we can use it. Add its default
3427 * parameters to the end of our list if necessary.
3429 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3430 params =
3431 nasm_realloc(params,
3432 ((m->nparam_min + m->ndefs +
3433 1) * sizeof(*params)));
3434 while (nparam < m->nparam_min + m->ndefs) {
3435 params[nparam] = m->defaults[nparam - m->nparam_min];
3436 nparam++;
3440 * If we've gone over the maximum parameter count (and
3441 * we're in Plus mode), ignore parameters beyond
3442 * nparam_max.
3444 if (m->plus && nparam > m->nparam_max)
3445 nparam = m->nparam_max;
3447 * Then terminate the parameter list, and leave.
3449 if (!params) { /* need this special case */
3450 params = nasm_malloc(sizeof(*params));
3451 nparam = 0;
3453 params[nparam] = NULL;
3454 *params_array = params;
3455 return m;
3458 * This one wasn't right: look for the next one with the
3459 * same name.
3461 for (m = m->next; m; m = m->next)
3462 if (!mstrcmp(m->name, tline->text, m->casesense))
3463 break;
3467 * After all that, we didn't find one with the right number of
3468 * parameters. Issue a warning, and fail to expand the macro.
3470 error(ERR_WARNING | ERR_WARN_MNP,
3471 "macro `%s' exists, but not taking %d parameters",
3472 tline->text, nparam);
3473 nasm_free(params);
3474 return NULL;
3478 * Expand the multi-line macro call made by the given line, if
3479 * there is one to be expanded. If there is, push the expansion on
3480 * istk->expansion and return 1. Otherwise return 0.
3482 static int expand_mmacro(Token * tline)
3484 Token *startline = tline;
3485 Token *label = NULL;
3486 int dont_prepend = 0;
3487 Token **params, *t, *tt;
3488 MMacro *m;
3489 Line *l, *ll;
3490 int i, nparam, *paramlen;
3492 t = tline;
3493 skip_white_(t);
3494 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3495 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3496 return 0;
3497 m = is_mmacro(t, &params);
3498 if (!m) {
3499 Token *last;
3501 * We have an id which isn't a macro call. We'll assume
3502 * it might be a label; we'll also check to see if a
3503 * colon follows it. Then, if there's another id after
3504 * that lot, we'll check it again for macro-hood.
3506 label = last = t;
3507 t = t->next;
3508 if (tok_type_(t, TOK_WHITESPACE))
3509 last = t, t = t->next;
3510 if (tok_is_(t, ":")) {
3511 dont_prepend = 1;
3512 last = t, t = t->next;
3513 if (tok_type_(t, TOK_WHITESPACE))
3514 last = t, t = t->next;
3516 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3517 return 0;
3518 last->next = NULL;
3519 tline = t;
3523 * Fix up the parameters: this involves stripping leading and
3524 * trailing whitespace, then stripping braces if they are
3525 * present.
3527 for (nparam = 0; params[nparam]; nparam++) ;
3528 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3530 for (i = 0; params[i]; i++) {
3531 int brace = false;
3532 int comma = (!m->plus || i < nparam - 1);
3534 t = params[i];
3535 skip_white_(t);
3536 if (tok_is_(t, "{"))
3537 t = t->next, brace = true, comma = false;
3538 params[i] = t;
3539 paramlen[i] = 0;
3540 while (t) {
3541 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3542 break; /* ... because we have hit a comma */
3543 if (comma && t->type == TOK_WHITESPACE
3544 && tok_is_(t->next, ","))
3545 break; /* ... or a space then a comma */
3546 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3547 break; /* ... or a brace */
3548 t = t->next;
3549 paramlen[i]++;
3554 * OK, we have a MMacro structure together with a set of
3555 * parameters. We must now go through the expansion and push
3556 * copies of each Line on to istk->expansion. Substitution of
3557 * parameter tokens and macro-local tokens doesn't get done
3558 * until the single-line macro substitution process; this is
3559 * because delaying them allows us to change the semantics
3560 * later through %rotate.
3562 * First, push an end marker on to istk->expansion, mark this
3563 * macro as in progress, and set up its invocation-specific
3564 * variables.
3566 ll = nasm_malloc(sizeof(Line));
3567 ll->next = istk->expansion;
3568 ll->finishes = m;
3569 ll->first = NULL;
3570 istk->expansion = ll;
3572 m->in_progress = true;
3573 m->params = params;
3574 m->iline = tline;
3575 m->nparam = nparam;
3576 m->rotate = 0;
3577 m->paramlen = paramlen;
3578 m->unique = unique++;
3579 m->lineno = 0;
3581 m->next_active = istk->mstk;
3582 istk->mstk = m;
3584 for (l = m->expansion; l; l = l->next) {
3585 Token **tail;
3587 ll = nasm_malloc(sizeof(Line));
3588 ll->finishes = NULL;
3589 ll->next = istk->expansion;
3590 istk->expansion = ll;
3591 tail = &ll->first;
3593 for (t = l->first; t; t = t->next) {
3594 Token *x = t;
3595 if (t->type == TOK_PREPROC_ID &&
3596 t->text[1] == '0' && t->text[2] == '0') {
3597 dont_prepend = -1;
3598 x = label;
3599 if (!x)
3600 continue;
3602 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3603 tail = &tt->next;
3605 *tail = NULL;
3609 * If we had a label, push it on as the first line of
3610 * the macro expansion.
3612 if (label) {
3613 if (dont_prepend < 0)
3614 free_tlist(startline);
3615 else {
3616 ll = nasm_malloc(sizeof(Line));
3617 ll->finishes = NULL;
3618 ll->next = istk->expansion;
3619 istk->expansion = ll;
3620 ll->first = startline;
3621 if (!dont_prepend) {
3622 while (label->next)
3623 label = label->next;
3624 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3629 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3631 return 1;
3635 * Since preprocessor always operate only on the line that didn't
3636 * arrived yet, we should always use ERR_OFFBY1. Also since user
3637 * won't want to see same error twice (preprocessing is done once
3638 * per pass) we will want to show errors only during pass one.
3640 static void error(int severity, const char *fmt, ...)
3642 va_list arg;
3643 char buff[1024];
3645 /* If we're in a dead branch of IF or something like it, ignore the error */
3646 if (istk && istk->conds && !emitting(istk->conds->state))
3647 return;
3649 va_start(arg, fmt);
3650 vsnprintf(buff, sizeof(buff), fmt, arg);
3651 va_end(arg);
3653 if (istk && istk->mstk && istk->mstk->name)
3654 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3655 istk->mstk->lineno, buff);
3656 else
3657 _error(severity | ERR_PASS1, "%s", buff);
3660 static void
3661 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3662 ListGen * listgen)
3664 _error = errfunc;
3665 cstk = NULL;
3666 istk = nasm_malloc(sizeof(Include));
3667 istk->next = NULL;
3668 istk->conds = NULL;
3669 istk->expansion = NULL;
3670 istk->mstk = NULL;
3671 istk->fp = fopen(file, "r");
3672 istk->fname = NULL;
3673 src_set_fname(nasm_strdup(file));
3674 src_set_linnum(0);
3675 istk->lineinc = 1;
3676 if (!istk->fp)
3677 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3678 file);
3679 defining = NULL;
3680 init_macros();
3681 unique = 0;
3682 if (tasm_compatible_mode) {
3683 stdmacpos = stdmac;
3684 } else {
3685 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3687 any_extrastdmac = (extrastdmac != NULL);
3688 list = listgen;
3689 evaluate = eval;
3690 pass = apass;
3693 static char *pp_getline(void)
3695 char *line;
3696 Token *tline;
3698 while (1) {
3700 * Fetch a tokenized line, either from the macro-expansion
3701 * buffer or from the input file.
3703 tline = NULL;
3704 while (istk->expansion && istk->expansion->finishes) {
3705 Line *l = istk->expansion;
3706 if (!l->finishes->name && l->finishes->in_progress > 1) {
3707 Line *ll;
3710 * This is a macro-end marker for a macro with no
3711 * name, which means it's not really a macro at all
3712 * but a %rep block, and the `in_progress' field is
3713 * more than 1, meaning that we still need to
3714 * repeat. (1 means the natural last repetition; 0
3715 * means termination by %exitrep.) We have
3716 * therefore expanded up to the %endrep, and must
3717 * push the whole block on to the expansion buffer
3718 * again. We don't bother to remove the macro-end
3719 * marker: we'd only have to generate another one
3720 * if we did.
3722 l->finishes->in_progress--;
3723 for (l = l->finishes->expansion; l; l = l->next) {
3724 Token *t, *tt, **tail;
3726 ll = nasm_malloc(sizeof(Line));
3727 ll->next = istk->expansion;
3728 ll->finishes = NULL;
3729 ll->first = NULL;
3730 tail = &ll->first;
3732 for (t = l->first; t; t = t->next) {
3733 if (t->text || t->type == TOK_WHITESPACE) {
3734 tt = *tail =
3735 new_Token(NULL, t->type, t->text, 0);
3736 tail = &tt->next;
3740 istk->expansion = ll;
3742 } else {
3744 * Check whether a `%rep' was started and not ended
3745 * within this macro expansion. This can happen and
3746 * should be detected. It's a fatal error because
3747 * I'm too confused to work out how to recover
3748 * sensibly from it.
3750 if (defining) {
3751 if (defining->name)
3752 error(ERR_PANIC,
3753 "defining with name in expansion");
3754 else if (istk->mstk->name)
3755 error(ERR_FATAL,
3756 "`%%rep' without `%%endrep' within"
3757 " expansion of macro `%s'",
3758 istk->mstk->name);
3762 * FIXME: investigate the relationship at this point between
3763 * istk->mstk and l->finishes
3766 MMacro *m = istk->mstk;
3767 istk->mstk = m->next_active;
3768 if (m->name) {
3770 * This was a real macro call, not a %rep, and
3771 * therefore the parameter information needs to
3772 * be freed.
3774 nasm_free(m->params);
3775 free_tlist(m->iline);
3776 nasm_free(m->paramlen);
3777 l->finishes->in_progress = false;
3778 } else
3779 free_mmacro(m);
3781 istk->expansion = l->next;
3782 nasm_free(l);
3783 list->downlevel(LIST_MACRO);
3786 while (1) { /* until we get a line we can use */
3788 if (istk->expansion) { /* from a macro expansion */
3789 char *p;
3790 Line *l = istk->expansion;
3791 if (istk->mstk)
3792 istk->mstk->lineno++;
3793 tline = l->first;
3794 istk->expansion = l->next;
3795 nasm_free(l);
3796 p = detoken(tline, false);
3797 list->line(LIST_MACRO, p);
3798 nasm_free(p);
3799 break;
3801 line = read_line();
3802 if (line) { /* from the current input file */
3803 line = prepreproc(line);
3804 tline = tokenize(line);
3805 nasm_free(line);
3806 break;
3809 * The current file has ended; work down the istk
3812 Include *i = istk;
3813 fclose(i->fp);
3814 if (i->conds)
3815 error(ERR_FATAL,
3816 "expected `%%endif' before end of file");
3817 /* only set line and file name if there's a next node */
3818 if (i->next) {
3819 src_set_linnum(i->lineno);
3820 nasm_free(src_set_fname(i->fname));
3822 istk = i->next;
3823 list->downlevel(LIST_INCLUDE);
3824 nasm_free(i);
3825 if (!istk)
3826 return NULL;
3831 * We must expand MMacro parameters and MMacro-local labels
3832 * _before_ we plunge into directive processing, to cope
3833 * with things like `%define something %1' such as STRUC
3834 * uses. Unless we're _defining_ a MMacro, in which case
3835 * those tokens should be left alone to go into the
3836 * definition; and unless we're in a non-emitting
3837 * condition, in which case we don't want to meddle with
3838 * anything.
3840 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3841 tline = expand_mmac_params(tline);
3844 * Check the line to see if it's a preprocessor directive.
3846 if (do_directive(tline) == DIRECTIVE_FOUND) {
3847 continue;
3848 } else if (defining) {
3850 * We're defining a multi-line macro. We emit nothing
3851 * at all, and just
3852 * shove the tokenized line on to the macro definition.
3854 Line *l = nasm_malloc(sizeof(Line));
3855 l->next = defining->expansion;
3856 l->first = tline;
3857 l->finishes = false;
3858 defining->expansion = l;
3859 continue;
3860 } else if (istk->conds && !emitting(istk->conds->state)) {
3862 * We're in a non-emitting branch of a condition block.
3863 * Emit nothing at all, not even a blank line: when we
3864 * emerge from the condition we'll give a line-number
3865 * directive so we keep our place correctly.
3867 free_tlist(tline);
3868 continue;
3869 } else if (istk->mstk && !istk->mstk->in_progress) {
3871 * We're in a %rep block which has been terminated, so
3872 * we're walking through to the %endrep without
3873 * emitting anything. Emit nothing at all, not even a
3874 * blank line: when we emerge from the %rep block we'll
3875 * give a line-number directive so we keep our place
3876 * correctly.
3878 free_tlist(tline);
3879 continue;
3880 } else {
3881 tline = expand_smacro(tline);
3882 if (!expand_mmacro(tline)) {
3884 * De-tokenize the line again, and emit it.
3886 line = detoken(tline, true);
3887 free_tlist(tline);
3888 break;
3889 } else {
3890 continue; /* expand_mmacro calls free_tlist */
3895 return line;
3898 static void pp_cleanup(int pass)
3900 if (defining) {
3901 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3902 defining->name);
3903 free_mmacro(defining);
3905 while (cstk)
3906 ctx_pop();
3907 free_macros();
3908 while (istk) {
3909 Include *i = istk;
3910 istk = istk->next;
3911 fclose(i->fp);
3912 nasm_free(i->fname);
3913 nasm_free(i);
3915 while (cstk)
3916 ctx_pop();
3917 if (pass == 0) {
3918 free_llist(predef);
3919 delete_Blocks();
3923 void pp_include_path(char *path)
3925 IncPath *i;
3927 i = nasm_malloc(sizeof(IncPath));
3928 i->path = path ? nasm_strdup(path) : NULL;
3929 i->next = NULL;
3931 if (ipath != NULL) {
3932 IncPath *j = ipath;
3933 while (j->next != NULL)
3934 j = j->next;
3935 j->next = i;
3936 } else {
3937 ipath = i;
3942 * added by alexfru:
3944 * This function is used to "export" the include paths, e.g.
3945 * the paths specified in the '-I' command switch.
3946 * The need for such exporting is due to the 'incbin' directive,
3947 * which includes raw binary files (unlike '%include', which
3948 * includes text source files). It would be real nice to be
3949 * able to specify paths to search for incbin'ned files also.
3950 * So, this is a simple workaround.
3952 * The function use is simple:
3954 * The 1st call (with NULL argument) returns a pointer to the 1st path
3955 * (char** type) or NULL if none include paths available.
3957 * All subsequent calls take as argument the value returned by this
3958 * function last. The return value is either the next path
3959 * (char** type) or NULL if the end of the paths list is reached.
3961 * It is maybe not the best way to do things, but I didn't want
3962 * to export too much, just one or two functions and no types or
3963 * variables exported.
3965 * Can't say I like the current situation with e.g. this path list either,
3966 * it seems to be never deallocated after creation...
3968 char **pp_get_include_path_ptr(char **pPrevPath)
3970 /* This macro returns offset of a member of a structure */
3971 #define GetMemberOffset(StructType,MemberName)\
3972 ((size_t)&((StructType*)0)->MemberName)
3973 IncPath *i;
3975 if (pPrevPath == NULL) {
3976 if (ipath != NULL)
3977 return &ipath->path;
3978 else
3979 return NULL;
3981 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
3982 i = i->next;
3983 if (i != NULL)
3984 return &i->path;
3985 else
3986 return NULL;
3987 #undef GetMemberOffset
3990 void pp_pre_include(char *fname)
3992 Token *inc, *space, *name;
3993 Line *l;
3995 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3996 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3997 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
3999 l = nasm_malloc(sizeof(Line));
4000 l->next = predef;
4001 l->first = inc;
4002 l->finishes = false;
4003 predef = l;
4006 void pp_pre_define(char *definition)
4008 Token *def, *space;
4009 Line *l;
4010 char *equals;
4012 equals = strchr(definition, '=');
4013 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4014 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4015 if (equals)
4016 *equals = ' ';
4017 space->next = tokenize(definition);
4018 if (equals)
4019 *equals = '=';
4021 l = nasm_malloc(sizeof(Line));
4022 l->next = predef;
4023 l->first = def;
4024 l->finishes = false;
4025 predef = l;
4028 void pp_pre_undefine(char *definition)
4030 Token *def, *space;
4031 Line *l;
4033 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4034 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4035 space->next = tokenize(definition);
4037 l = nasm_malloc(sizeof(Line));
4038 l->next = predef;
4039 l->first = def;
4040 l->finishes = false;
4041 predef = l;
4045 * Added by Keith Kanios:
4047 * This function is used to assist with "runtime" preprocessor
4048 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4050 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4051 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4054 void pp_runtime(char *definition)
4056 Token *def;
4058 def = tokenize(definition);
4059 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4060 free_tlist(def);
4064 void pp_extra_stdmac(const char **macros)
4066 extrastdmac = macros;
4069 static void make_tok_num(Token * tok, int32_t val)
4071 char numbuf[20];
4072 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
4073 tok->text = nasm_strdup(numbuf);
4074 tok->type = TOK_NUMBER;
4077 Preproc nasmpp = {
4078 pp_reset,
4079 pp_getline,
4080 pp_cleanup