NASM 0.98.08
[nasm/avx512.git] / preproc.c
blob87c128f7aefb4e87778af28cd37d553fd999aa16
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenised lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <limits.h>
44 #include "nasm.h"
45 #include "nasmlib.h"
47 typedef struct SMacro SMacro;
48 typedef struct MMacro MMacro;
49 typedef struct Context Context;
50 typedef struct Token Token;
51 typedef struct Line Line;
52 typedef struct Include Include;
53 typedef struct Cond Cond;
54 typedef struct IncPath IncPath;
57 * Store the definition of a single-line macro.
59 struct SMacro {
60 SMacro *next;
61 char *name;
62 int casesense;
63 int nparam;
64 int in_progress;
65 Token *expansion;
69 * Store the definition of a multi-line macro. This is also used to
70 * store the interiors of `%rep...%endrep' blocks, which are
71 * effectively self-re-invoking multi-line macros which simply
72 * don't have a name or bother to appear in the hash tables. %rep
73 * blocks are signified by having a NULL `name' field.
75 * In a MMacro describing a `%rep' block, the `in_progress' field
76 * isn't merely boolean, but gives the number of repeats left to
77 * run.
79 * The `next' field is used for storing MMacros in hash tables; the
80 * `next_active' field is for stacking them on istk entries.
82 * When a MMacro is being expanded, `params', `iline', `nparam',
83 * `paramlen', `rotate' and `unique' are local to the invocation.
85 struct MMacro {
86 MMacro *next;
87 char *name;
88 int casesense;
89 int nparam_min, nparam_max;
90 int plus; /* is the last parameter greedy? */
91 int nolist; /* is this macro listing-inhibited? */
92 int in_progress;
93 Token *dlist; /* All defaults as one list */
94 Token **defaults; /* Parameter default pointers */
95 int ndefs; /* number of default parameters */
96 Line *expansion;
98 MMacro *next_active;
99 MMacro *rep_nest; /* used for nesting %rep */
100 Token **params; /* actual parameters */
101 Token *iline; /* invocation line */
102 int nparam, rotate, *paramlen;
103 unsigned long unique;
104 int lineno; /* Current line number on expansion */
108 * The context stack is composed of a linked list of these.
110 struct Context {
111 Context *next;
112 SMacro *localmac;
113 char *name;
114 unsigned long number;
118 * This is the internal form which we break input lines up into.
119 * Typically stored in linked lists.
121 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
122 * necessarily used as-is, but is intended to denote the number of
123 * the substituted parameter. So in the definition
125 * %define a(x,y) ( (x) & ~(y) )
127 * the token representing `x' will have its type changed to
128 * TOK_SMAC_PARAM, but the one representing `y' will be
129 * TOK_SMAC_PARAM+1.
131 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
132 * which doesn't need quotes around it. Used in the pre-include
133 * mechanism as an alternative to trying to find a sensible type of
134 * quote to use on the filename we were passed.
136 struct Token {
137 Token *next;
138 char *text;
139 SMacro *mac; /* associated macro for TOK_SMAC_END */
140 int type;
142 enum {
143 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
144 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
145 TOK_INTERNAL_STRING
149 * Multi-line macro definitions are stored as a linked list of
150 * these, which is essentially a container to allow several linked
151 * lists of Tokens.
153 * Note that in this module, linked lists are treated as stacks
154 * wherever possible. For this reason, Lines are _pushed_ on to the
155 * `expansion' field in MMacro structures, so that the linked list,
156 * if walked, would give the macro lines in reverse order; this
157 * means that we can walk the list when expanding a macro, and thus
158 * push the lines on to the `expansion' field in _istk_ in reverse
159 * order (so that when popped back off they are in the right
160 * order). It may seem cockeyed, and it relies on my design having
161 * an even number of steps in, but it works...
163 * Some of these structures, rather than being actual lines, are
164 * markers delimiting the end of the expansion of a given macro.
165 * This is for use in the cycle-tracking and %rep-handling code.
166 * Such structures have `finishes' non-NULL, and `first' NULL. All
167 * others have `finishes' NULL, but `first' may still be NULL if
168 * the line is blank.
170 struct Line {
171 Line *next;
172 MMacro *finishes;
173 Token *first;
177 * To handle an arbitrary level of file inclusion, we maintain a
178 * stack (ie linked list) of these things.
180 struct Include {
181 Include *next;
182 FILE *fp;
183 Cond *conds;
184 Line *expansion;
185 char *fname;
186 int lineno, lineinc;
187 MMacro *mstk; /* stack of active macros/reps */
191 * Include search path. This is simply a list of strings which get
192 * prepended, in turn, to the name of an include file, in an
193 * attempt to find the file if it's not in the current directory.
195 struct IncPath {
196 IncPath *next;
197 char *path;
201 * Conditional assembly: we maintain a separate stack of these for
202 * each level of file inclusion. (The only reason we keep the
203 * stacks separate is to ensure that a stray `%endif' in a file
204 * included from within the true branch of a `%if' won't terminate
205 * it and cause confusion: instead, rightly, it'll cause an error.)
207 struct Cond {
208 Cond *next;
209 int state;
211 enum {
213 * These states are for use just after %if or %elif: IF_TRUE
214 * means the condition has evaluated to truth so we are
215 * currently emitting, whereas IF_FALSE means we are not
216 * currently emitting but will start doing so if a %else comes
217 * up. In these states, all directives are admissible: %elif,
218 * %else and %endif. (And of course %if.)
220 COND_IF_TRUE, COND_IF_FALSE,
222 * These states come up after a %else: ELSE_TRUE means we're
223 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
224 * any %elif or %else will cause an error.
226 COND_ELSE_TRUE, COND_ELSE_FALSE,
228 * This state means that we're not emitting now, and also that
229 * nothing until %endif will be emitted at all. It's for use in
230 * two circumstances: (i) when we've had our moment of emission
231 * and have now started seeing %elifs, and (ii) when the
232 * condition construct in question is contained within a
233 * non-emitting branch of a larger condition construct.
235 COND_NEVER
237 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
240 * Condition codes. Note that we use c_ prefix not C_ because C_ is
241 * used in nasm.h for the "real" condition codes. At _this_ level,
242 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
243 * ones, so we need a different enum...
245 static char *conditions[] = {
246 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
247 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
248 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
250 enum {
251 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
252 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
253 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
255 static int inverse_ccs[] = {
256 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
257 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,
258 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
262 * Directive names.
264 static char *directives[] = {
265 #ifdef TASM_COMPAT
266 "%arg",
267 #endif
268 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
269 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
270 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
271 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
272 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
273 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
274 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
275 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
276 "%ixdefine", "%line",
277 #ifdef TASM_COMPAT
278 "%local",
279 #endif
280 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
281 #ifdef TASM_COMPAT
282 "%stacksize",
283 #endif
284 "%strlen", "%substr", "%undef", "%xdefine"
286 enum {
287 #ifdef TASM_COMPAT
288 PP_ARG,
289 #endif
290 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
291 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFNCTX, PP_ELIFNDEF,
292 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNNUM, PP_ELIFNSTR,
293 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
294 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
295 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFNCTX,
296 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNNUM,
297 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
298 PP_IXDEFINE, PP_LINE,
299 #ifdef TASM_COMPAT
300 PP_LOCAL,
301 #endif
302 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
303 #ifdef TASM_COMPAT
304 PP_STACKSIZE,
305 #endif
306 PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
309 #ifdef TASM_COMPAT
311 /* For TASM compatibility we need to be able to recognise TASM compatible
312 * conditional compilation directives. Using the NASM pre-processor does
313 * not work, so we look for them specifically from the following list and
314 * then jam in the equivalent NASM directive into the input stream.
317 #ifndef MAX
318 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
319 #endif
321 enum {
322 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
323 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
326 static char *tasm_directives[] = {
327 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
328 "ifndef", "include", "local"
331 static int StackSize = 4;
332 static char *StackPointer = "ebp";
333 static int ArgOffset = 8;
334 static int LocalOffset = 4;
336 #endif
338 static Context *cstk;
339 static Include *istk;
340 static IncPath *ipath = NULL;
342 static efunc __error; /* Pointer to client-provided error reporting function */
343 static evalfunc evaluate;
345 static int pass; /* HACK: pass 0 = generate dependencies only */
347 static unsigned long unique; /* unique identifier numbers */
349 static Line *predef = NULL;
351 static ListGen *list;
354 * The number of hash values we use for the macro lookup tables.
355 * FIXME: We should *really* be able to configure this at run time,
356 * or even have the hash table automatically expanding when necessary.
358 #define NHASH 31
361 * The current set of multi-line macros we have defined.
363 static MMacro *mmacros[NHASH];
366 * The current set of single-line macros we have defined.
368 static SMacro *smacros[NHASH];
371 * The multi-line macro we are currently defining, or the %rep
372 * block we are currently reading, if any.
374 static MMacro *defining;
377 * The number of macro parameters to allocate space for at a time.
379 #define PARAM_DELTA 16
382 * The standard macro set: defined as `static char *stdmac[]'. Also
383 * gives our position in the macro set, when we're processing it.
385 #include "macros.c"
386 static char **stdmacpos;
389 * The extra standard macros that come from the object format, if
390 * any.
392 static char **extrastdmac = NULL;
393 int any_extrastdmac;
396 * Forward declarations.
398 static Token *expand_mmac_params (Token *tline);
399 static Token *expand_smacro (Token *tline);
400 static Token *expand_id (Token *tline);
401 static Context *get_ctx (char *name, int all_contexts);
402 static void make_tok_num(Token *tok, long val);
403 static void error (int severity, char *fmt, ...);
406 * Macros for safe checking of token pointers, avoid *(NULL)
408 #define tok_type_(x,t) ((x) && (x)->type == (t))
409 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
410 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
411 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
413 #ifdef TASM_COMPAT
414 /* Handle TASM specific directives, which do not contain a % in
415 * front of them. We do it here because I could not find any other
416 * place to do it for the moment, and it is a hack (ideally it would
417 * be nice to be able to use the NASM pre-processor to do it).
419 static char *check_tasm_directive(char *line)
421 int i, j, k, m, len;
422 char *p = line, *oldline, oldchar;
424 /* Skip whitespace */
425 while (isspace(*p) && *p != 0)
426 p++;
428 /* Binary search for the directive name */
429 i = -1;
430 j = sizeof(tasm_directives) / sizeof(*tasm_directives);
431 len = 0;
432 while (!isspace(p [len]) && p [len] != 0)
433 len++;
434 if (len) {
435 oldchar = p [len];
436 p [len] = 0;
437 while (j - i > 1) {
438 k = (j + i) / 2;
439 m = nasm_stricmp(p, tasm_directives [k]);
440 if (m == 0) {
441 /* We have found a directive, so jam a % in front of it
442 * so that NASM will then recognise it as one if it's own.
444 p [len] = oldchar;
445 len = strlen(p);
446 oldline = line;
447 line = nasm_malloc(len + 2);
448 line [0] = '%';
449 if (k == TM_IFDIFI) {
450 /* NASM does not recognise IFDIFI, so we convert it to
451 * %ifdef BOGUS. This is not used in NASM comaptible
452 * code, but does need to parse for the TASM macro
453 * package.
455 strcpy(line + 1,"ifdef BOGUS");
456 } else {
457 memcpy(line + 1, p, len + 1);
459 nasm_free(oldline);
460 return line;
461 } else if (m < 0) {
462 j = k;
463 } else
464 i = k;
466 p [len] = oldchar;
468 return line;
470 #endif
473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
476 * lineno file').
478 static char *prepreproc(char *line)
480 int lineno, fnlen;
481 char *fname, *oldline;
483 if (line[0] == '#' && line[1] == ' ') {
484 oldline = line;
485 fname = oldline+2;
486 lineno = atoi(fname);
487 fname += strspn(fname, "0123456789 ");
488 if (*fname == '"')
489 fname++;
490 fnlen = strcspn(fname, "\"");
491 line = nasm_malloc(20+fnlen);
492 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
493 nasm_free (oldline);
495 #ifdef TASM_COMPAT
496 if (tasm_compatible_mode)
497 return check_tasm_directive(line);
498 #endif
499 return line;
503 * The hash function for macro lookups. Note that due to some
504 * macros having case-insensitive names, the hash function must be
505 * invariant under case changes. We implement this by applying a
506 * perfectly normal hash function to the uppercase of the string.
508 static int hash(char *s)
510 unsigned int h = 0;
511 int i = 0;
513 * Powers of three, mod 31.
515 static const int multipliers[] = {
516 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
517 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
521 while (*s) {
522 h += multipliers[i] * (unsigned char) (toupper(*s));
523 s++;
524 if (++i >= sizeof(multipliers)/sizeof(*multipliers))
525 i = 0;
527 h %= NHASH;
528 return h;
532 * Free a linked list of tokens.
534 static void free_tlist (Token *list)
536 Token *t;
537 while (list) {
538 t = list;
539 list = list->next;
540 nasm_free (t->text);
541 nasm_free (t);
546 * Free a linked list of lines.
548 static void free_llist (Line *list)
550 Line *l;
551 while (list) {
552 l = list;
553 list = list->next;
554 free_tlist (l->first);
555 nasm_free (l);
560 * Free an MMacro
562 static void free_mmacro (MMacro *m)
564 nasm_free (m->name);
565 free_tlist (m->dlist);
566 nasm_free (m->defaults);
567 free_llist (m->expansion);
568 nasm_free (m);
572 * Pop the context stack.
574 static void ctx_pop (void)
576 Context *c = cstk;
577 SMacro *smac, *s;
579 cstk = cstk->next;
580 smac = c->localmac;
581 while (smac) {
582 s = smac;
583 smac = smac->next;
584 nasm_free (s->name);
585 free_tlist (s->expansion);
586 nasm_free (s);
588 nasm_free (c->name);
589 nasm_free (c);
592 #define BUF_DELTA 512
594 * Read a line from the top file in istk, handling multiple CR/LFs
595 * at the end of the line read, and handling spurious ^Zs. Will
596 * return lines from the standard macro set if this has not already
597 * been done.
599 static char *read_line (void)
601 char *buffer, *p, *q;
602 int bufsize;
604 if (stdmacpos) {
605 if (*stdmacpos) {
606 char *ret = nasm_strdup(*stdmacpos++);
607 if (!*stdmacpos && any_extrastdmac)
609 stdmacpos = extrastdmac;
610 any_extrastdmac = FALSE;
611 return ret;
614 * Nasty hack: here we push the contents of `predef' on
615 * to the top-level expansion stack, since this is the
616 * most convenient way to implement the pre-include and
617 * pre-define features.
619 if (!*stdmacpos)
621 Line *pd, *l;
622 Token *head, **tail, *t, *tt;
624 for (pd = predef; pd; pd = pd->next) {
625 head = NULL;
626 tail = &head;
627 for (t = pd->first; t; t = t->next) {
628 tt = *tail = nasm_malloc(sizeof(Token));
629 tt->next = NULL;
630 tail = &tt->next;
631 tt->type = t->type;
632 tt->text = nasm_strdup(t->text);
633 tt->mac = t->mac; /* always NULL here, in fact */
635 l = nasm_malloc(sizeof(Line));
636 l->next = istk->expansion;
637 l->first = head;
638 l->finishes = FALSE;
639 istk->expansion = l;
642 return ret;
644 else {
645 stdmacpos = NULL;
649 bufsize = BUF_DELTA;
650 buffer = nasm_malloc(BUF_DELTA);
651 p = buffer;
652 while (1) {
653 q = fgets(p, bufsize-(p-buffer), istk->fp);
654 if (!q)
655 break;
656 p += strlen(p);
657 if (p > buffer && p[-1] == '\n') {
658 break;
660 if (p-buffer > bufsize-10) {
661 long offset = p-buffer;
662 bufsize += BUF_DELTA;
663 buffer = nasm_realloc(buffer, bufsize);
664 p = buffer+offset; /* prevent stale-pointer problems */
668 if (!q && p == buffer) {
669 nasm_free (buffer);
670 return NULL;
673 src_set_linnum(src_get_linnum() + istk->lineinc);
676 * Play safe: remove CRs as well as LFs, if any of either are
677 * present at the end of the line.
679 while (--p >= buffer && (*p == '\n' || *p == '\r'))
680 *p = '\0';
683 * Handle spurious ^Z, which may be inserted into source files
684 * by some file transfer utilities.
686 buffer[strcspn(buffer, "\032")] = '\0';
688 list->line (LIST_READ, buffer);
690 return buffer;
694 * Tokenise a line of text. This is a very simple process since we
695 * don't need to parse the value out of e.g. numeric tokens: we
696 * simply split one string into many.
698 static Token *tokenise (char *line)
700 char *p = line;
701 int type;
702 Token *list = NULL;
703 Token *t, **tail = &list;
705 while (*line) {
706 p = line;
707 if (*p == '%' &&
708 (isdigit(p[1]) ||
709 ((p[1] == '-' || p[1] == '+') && isdigit(p[2])) ||
710 ((p[1] == '+') && (isspace (p[2]) || !p[2]))))
712 p++;
713 do {
714 p++;
715 } while (isdigit(*p));
716 type = TOK_PREPROC_ID;
718 else if (*p == '%' && p[1] == '{') {
719 p += 2;
720 while (*p && *p != '}') {
721 p[-1] = *p;
722 p++;
724 p[-1] = '\0';
725 if (*p) p++;
726 type = TOK_PREPROC_ID;
728 else if (*p == '%' && (isidchar(p[1]) ||
729 ((p[1] == '!' || p[1] == '%' || p[1] == '$') &&
730 isidchar(p[2]))))
732 p++;
733 do {
734 p++;
735 } while (isidchar(*p));
736 type = TOK_PREPROC_ID;
738 else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
739 type = TOK_ID;
740 p++;
741 while (*p && isidchar(*p))
742 p++;
744 else if (*p == '\'' || *p == '"') {
746 * A string token.
748 char c = *p;
749 p++;
750 type = TOK_STRING;
751 while (*p && *p != c)
752 p++;
753 if (*p) {
754 p++;
756 else {
757 error(ERR_WARNING, "unterminated string");
760 else if (isnumstart(*p)) {
762 * A number token.
764 type = TOK_NUMBER;
765 p++;
766 while (*p && isnumchar(*p))
767 p++;
769 else if (isspace(*p)) {
770 type = TOK_WHITESPACE;
771 p++;
772 while (*p && isspace(*p))
773 p++;
775 * Whitespace just before end-of-line is discarded by
776 * pretending it's a comment; whitespace just before a
777 * comment gets lumped into the comment.
779 if (!*p || *p == ';') {
780 type = TOK_COMMENT;
781 while (*p) p++;
784 else if (*p == ';') {
785 type = TOK_COMMENT;
786 while (*p) p++;
788 else {
790 * Anything else is an operator of some kind. We check
791 * for all the double-character operators (>>, <<, //,
792 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
793 * else is a single-character operator.
795 type = TOK_OTHER;
796 if ((p[0] == '>' && p[1] == '>') ||
797 (p[0] == '<' && p[1] == '<') ||
798 (p[0] == '/' && p[1] == '/') ||
799 (p[0] == '%' && p[1] == '%') ||
800 (p[0] == '<' && p[1] == '=') ||
801 (p[0] == '>' && p[1] == '=') ||
802 (p[0] == '=' && p[1] == '=') ||
803 (p[0] == '!' && p[1] == '=') ||
804 (p[0] == '<' && p[1] == '>') ||
805 (p[0] == '&' && p[1] == '&') ||
806 (p[0] == '|' && p[1] == '|') ||
807 (p[0] == '^' && p[1] == '^'))
809 p++;
811 p++;
813 if (type != TOK_COMMENT) {
814 *tail = t = nasm_malloc (sizeof(Token));
815 tail = &t->next;
816 t->next = NULL;
817 t->type = type;
818 t->text = nasm_malloc(1+p-line);
819 strncpy(t->text, line, p-line);
820 t->text[p-line] = '\0';
822 line = p;
825 return list;
829 * Convert a line of tokens back into text.
830 * If expand_locals is not zero, identifiers of the form "%$*xxx"
831 * will be transformed into ..@ctxnum.xxx
833 static char *detoken (Token *tlist, int expand_locals)
835 Token *t;
836 int len;
837 char *line, *p;
839 len = 0;
840 for (t = tlist; t; t = t->next) {
841 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
842 char *p = getenv(t->text+2);
843 nasm_free (t->text);
844 if (p)
845 t->text = nasm_strdup(p);
846 else
847 t->text = NULL;
849 /* Expand local macros here and not during preprocessing */
850 if (expand_locals &&
851 t->type == TOK_PREPROC_ID && t->text &&
852 t->text[0] == '%' && t->text [1] == '$') {
853 Context *ctx = get_ctx (t->text, FALSE);
854 if (ctx) {
855 char buffer [40];
856 char *p, *q = t->text + 2;
858 q += strspn (q, "$");
859 sprintf (buffer, "..@%lu.", ctx->number);
860 p = nasm_malloc (strlen(buffer)+strlen(q)+1);
861 strcpy (p, buffer);
862 strcat (p, q);
863 nasm_free (t->text);
864 t->text = p;
867 if (t->text)
868 len += strlen(t->text);
870 p = line = nasm_malloc(len+1);
871 for (t = tlist; t; t = t->next) {
872 if (t->text) {
873 strcpy (p, t->text);
874 p += strlen(p);
877 *p = '\0';
878 return line;
882 * A scanner, suitable for use by the expression evaluator, which
883 * operates on a line of Tokens. Expects a pointer to a pointer to
884 * the first token in the line to be passed in as its private_data
885 * field.
887 static int ppscan(void *private_data, struct tokenval *tokval)
889 Token **tlineptr = private_data;
890 Token *tline;
892 do {
893 tline = *tlineptr;
894 *tlineptr = tline ? tline->next : NULL;
895 } while (tline && (tline->type == TOK_WHITESPACE ||
896 tline->type == TOK_COMMENT));
898 if (!tline)
899 return tokval->t_type = TOKEN_EOS;
901 if (tline->text[0] == '$' && !tline->text[1])
902 return tokval->t_type = TOKEN_HERE;
903 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[1])
904 return tokval->t_type = TOKEN_BASE;
906 if (tline->type == TOK_ID) {
907 tokval->t_charptr = tline->text;
908 if (tline->text[0] == '$') {
909 tokval->t_charptr++;
910 return tokval->t_type = TOKEN_ID;
914 * This is the only special case we actually need to worry
915 * about in this restricted context.
917 if (!nasm_stricmp(tline->text, "seg"))
918 return tokval->t_type = TOKEN_SEG;
920 return tokval->t_type = TOKEN_ID;
923 if (tline->type == TOK_NUMBER) {
924 int rn_error;
926 tokval->t_integer = readnum(tline->text, &rn_error);
927 if (rn_error)
928 return tokval->t_type = TOKEN_ERRNUM;
929 tokval->t_charptr = NULL;
930 return tokval->t_type = TOKEN_NUM;
933 if (tline->type == TOK_STRING) {
934 int rn_warn;
935 char q, *r;
936 int l;
938 r = tline->text;
939 q = *r++;
940 l = strlen(r);
942 if (l == 0 || r[l-1] != q)
943 return tokval->t_type = TOKEN_ERRNUM;
944 tokval->t_integer = readstrnum(r, l-1, &rn_warn);
945 if (rn_warn)
946 error(ERR_WARNING|ERR_PASS1,
947 "character constant too long");
948 tokval->t_charptr = NULL;
949 return tokval->t_type = TOKEN_NUM;
952 if (tline->type == TOK_OTHER) {
953 if (!strcmp(tline->text, "<<")) return tokval->t_type = TOKEN_SHL;
954 if (!strcmp(tline->text, ">>")) return tokval->t_type = TOKEN_SHR;
955 if (!strcmp(tline->text, "//")) return tokval->t_type = TOKEN_SDIV;
956 if (!strcmp(tline->text, "%%")) return tokval->t_type = TOKEN_SMOD;
957 if (!strcmp(tline->text, "==")) return tokval->t_type = TOKEN_EQ;
958 if (!strcmp(tline->text, "<>")) return tokval->t_type = TOKEN_NE;
959 if (!strcmp(tline->text, "!=")) return tokval->t_type = TOKEN_NE;
960 if (!strcmp(tline->text, "<=")) return tokval->t_type = TOKEN_LE;
961 if (!strcmp(tline->text, ">=")) return tokval->t_type = TOKEN_GE;
962 if (!strcmp(tline->text, "&&")) return tokval->t_type = TOKEN_DBL_AND;
963 if (!strcmp(tline->text, "^^")) return tokval->t_type = TOKEN_DBL_XOR;
964 if (!strcmp(tline->text, "||")) return tokval->t_type = TOKEN_DBL_OR;
968 * We have no other options: just return the first character of
969 * the token text.
971 return tokval->t_type = tline->text[0];
975 * Compare a string to the name of an existing macro; this is a
976 * simple wrapper which calls either strcmp or nasm_stricmp
977 * depending on the value of the `casesense' parameter.
979 static int mstrcmp(char *p, char *q, int casesense)
981 return casesense ? strcmp(p,q) : nasm_stricmp(p,q);
985 * Return the Context structure associated with a %$ token. Return
986 * NULL, having _already_ reported an error condition, if the
987 * context stack isn't deep enough for the supplied number of $
988 * signs.
989 * If all_contexts == TRUE, contexts that enclose current are
990 * also scanned for such smacro, until it is found; if not -
991 * only the context that directly results from the number of $'s
992 * in variable's name.
994 static Context *get_ctx (char *name, int all_contexts)
996 Context *ctx;
997 SMacro *m;
998 int i;
1000 if (!name || name[0] != '%' || name[1] != '$')
1001 return NULL;
1003 if (!cstk) {
1004 error (ERR_NONFATAL, "`%s': context stack is empty", name);
1005 return NULL;
1008 for (i = strspn (name+2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1009 ctx = ctx->next;
1010 i--;
1012 if (!ctx) {
1013 error (ERR_NONFATAL, "`%s': context stack is only"
1014 " %d level%s deep", name, i-1, (i==2 ? "" : "s"));
1015 return NULL;
1017 if (!all_contexts)
1018 return ctx;
1020 do {
1021 /* Search for this smacro in found context */
1022 m = ctx->localmac;
1023 while (m) {
1024 if (!mstrcmp(m->name, name, m->casesense))
1025 return ctx;
1026 m = m->next;
1028 ctx = ctx->next;
1029 } while (ctx);
1030 return NULL;
1033 #ifdef TASM_COMPAT
1034 /* Add a slash to the end of a path if it is missing. We use the
1035 * forward slash to make it compatible with Unix systems.
1037 static void backslash(char *s)
1039 int pos = strlen(s);
1040 if (s[pos-1] != '\\' && s[pos-1] != '/') {
1041 s[pos] = '/';
1042 s[pos+1] = '\0';
1045 #endif
1048 * Open an include file. This routine must always return a valid
1049 * file pointer if it returns - it's responsible for throwing an
1050 * ERR_FATAL and bombing out completely if not. It should also try
1051 * the include path one by one until it finds the file or reaches
1052 * the end of the path.
1054 static FILE *inc_fopen(char *file)
1056 FILE *fp;
1057 char *prefix = "", *combine;
1058 IncPath *ip = ipath;
1059 static int namelen = 0;
1060 #ifdef TASM_COMPAT
1061 int len = strlen(file);
1062 #endif
1064 while (1) {
1065 #ifdef TASM_COMPAT
1066 combine = nasm_malloc(strlen(prefix)+1+len+1);
1067 strcpy(combine, prefix);
1068 if (prefix[0] != 0)
1069 backslash(combine);
1070 strcat(combine, file);
1071 #else
1072 combine = nasm_strcat(prefix,file);
1073 #endif
1074 fp = fopen(combine, "r");
1075 if (pass == 0 && fp)
1077 namelen += strlen(combine) + 1;
1078 if (namelen > 62)
1080 printf(" \\\n ");
1081 namelen = 2;
1083 printf(" %s", combine);
1085 nasm_free (combine);
1086 if (fp)
1087 return fp;
1088 if (!ip)
1089 break;
1090 prefix = ip->path;
1091 ip = ip->next;
1094 error (ERR_FATAL,
1095 "unable to open include file `%s'", file);
1096 return NULL; /* never reached - placate compilers */
1100 * Determine if we should warn on defining a single-line macro of
1101 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1102 * return TRUE if _any_ single-line macro of that name is defined.
1103 * Otherwise, will return TRUE if a single-line macro with either
1104 * `nparam' or no parameters is defined.
1106 * If a macro with precisely the right number of parameters is
1107 * defined, or nparam is -1, the address of the definition structure
1108 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1109 * is NULL, no action will be taken regarding its contents, and no
1110 * error will occur.
1112 * Note that this is also called with nparam zero to resolve
1113 * `ifdef'.
1115 * If you already know which context macro belongs to, you can pass
1116 * the context pointer as first parameter; if you won't but name begins
1117 * with %$ the context will be automatically computed. If all_contexts
1118 * is true, macro will be searched in outer contexts as well.
1120 static int smacro_defined (Context *ctx, char *name, int nparam, SMacro **defn,
1121 int nocase)
1123 SMacro *m;
1125 if (ctx)
1126 m = ctx->localmac;
1127 else if (name[0] == '%' && name[1] == '$') {
1128 if (cstk)
1129 ctx = get_ctx (name, FALSE);
1130 if (!ctx)
1131 return FALSE; /* got to return _something_ */
1132 m = ctx->localmac;
1133 } else
1134 m = smacros[hash(name)];
1136 while (m) {
1137 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1138 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1139 if (defn) {
1140 if (nparam == m->nparam || nparam == -1)
1141 *defn = m;
1142 else
1143 *defn = NULL;
1145 return TRUE;
1147 m = m->next;
1150 return FALSE;
1154 * Count and mark off the parameters in a multi-line macro call.
1155 * This is called both from within the multi-line macro expansion
1156 * code, and also to mark off the default parameters when provided
1157 * in a %macro definition line.
1159 static void count_mmac_params (Token *t, int *nparam, Token ***params)
1161 int paramsize, brace;
1163 *nparam = paramsize = 0;
1164 *params = NULL;
1165 while (t) {
1166 if (*nparam >= paramsize) {
1167 paramsize += PARAM_DELTA;
1168 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1170 skip_white_(t);
1171 brace = FALSE;
1172 if (tok_is_(t, "{"))
1173 brace = TRUE;
1174 (*params)[(*nparam)++] = t;
1175 while (tok_isnt_(t, brace ? "}" : ","))
1176 t = t->next;
1177 if (t) { /* got a comma/brace */
1178 t = t->next;
1179 if (brace) {
1181 * Now we've found the closing brace, look further
1182 * for the comma.
1184 skip_white_(t);
1185 if (tok_isnt_(t, ",")) {
1186 error (ERR_NONFATAL,
1187 "braces do not enclose all of macro parameter");
1188 while (tok_isnt_(t, ","))
1189 t = t->next;
1191 if (t)
1192 t = t->next; /* eat the comma */
1199 * Determine whether one of the various `if' conditions is true or
1200 * not.
1202 * We must free the tline we get passed.
1204 static int if_condition (Token *tline, int i)
1206 int j, casesense;
1207 Token * t, * tt, ** tptr, * origline;
1208 struct tokenval tokval;
1209 expr * evalresult;
1211 origline = tline;
1213 switch (i) {
1214 case PP_IFCTX: case PP_ELIFCTX:
1215 case PP_IFNCTX: case PP_ELIFNCTX:
1216 j = FALSE; /* have we matched yet? */
1217 while (cstk && tline) {
1218 skip_white_(tline);
1219 if (!tline || tline->type != TOK_ID) {
1220 error(ERR_NONFATAL,
1221 "`%s' expects context identifiers", directives[i]);
1222 free_tlist (origline);
1223 return -1;
1225 if (!nasm_stricmp(tline->text, cstk->name))
1226 j = TRUE;
1227 tline = tline->next;
1229 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1230 j = !j;
1231 free_tlist (origline);
1232 return j;
1234 case PP_IFDEF: case PP_ELIFDEF:
1235 case PP_IFNDEF: case PP_ELIFNDEF:
1236 j = FALSE; /* have we matched yet? */
1237 while (tline) {
1238 skip_white_(tline);
1239 if (!tline || (tline->type != TOK_ID &&
1240 (tline->type != TOK_PREPROC_ID ||
1241 tline->text[1] != '$'))) {
1242 error(ERR_NONFATAL,
1243 "`%%if%sdef' expects macro identifiers",
1244 (i==PP_ELIFNDEF ? "n" : ""));
1245 free_tlist (origline);
1246 return -1;
1248 if (smacro_defined (NULL, tline->text, 0, NULL, 1))
1249 j = TRUE;
1250 tline = tline->next;
1252 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1253 j = !j;
1254 free_tlist (origline);
1255 return j;
1257 case PP_IFIDN: case PP_ELIFIDN: case PP_IFNIDN: case PP_ELIFNIDN:
1258 case PP_IFIDNI: case PP_ELIFIDNI: case PP_IFNIDNI: case PP_ELIFNIDNI:
1259 tline = expand_smacro(tline);
1260 t = tt = tline;
1261 while (tok_isnt_(tt, ","))
1262 tt = tt->next;
1263 if (!tt) {
1264 error(ERR_NONFATAL, "`%s' expects two comma-separated arguments",
1265 directives[i]);
1266 free_tlist (tline);
1267 return -1;
1269 tt = tt->next;
1270 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1271 i == PP_IFNIDN || i == PP_ELIFNIDN);
1272 j = TRUE; /* assume equality unless proved not */
1273 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1274 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1275 error(ERR_NONFATAL, "`%s': more than one comma on line",
1276 directives[i]);
1277 free_tlist (tline);
1278 return -1;
1280 if (t->type == TOK_WHITESPACE) {
1281 t = t->next;
1282 continue;
1283 } else if (tt->type == TOK_WHITESPACE) {
1284 tt = tt->next;
1285 continue;
1286 } else if (tt->type != t->type ||
1287 (casesense ? strcmp(tt->text, t->text) :
1288 nasm_stricmp(tt->text, t->text))) {
1289 j = FALSE; /* found mismatching tokens */
1290 break;
1291 } else {
1292 t = t->next;
1293 tt = tt->next;
1294 continue;
1297 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1298 j = FALSE; /* trailing gunk on one end or other */
1299 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1300 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1301 j = !j;
1302 free_tlist (tline);
1303 return j;
1305 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1306 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1307 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1308 tline = expand_smacro(tline);
1309 t = tline;
1310 while (tok_type_(t, TOK_WHITESPACE))
1311 t = t->next;
1312 j = FALSE; /* placate optimiser */
1313 if (t) switch (i) {
1314 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1315 j = (t->type == TOK_ID);
1316 break;
1317 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1318 j = (t->type == TOK_NUMBER);
1319 break;
1320 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1321 j = (t->type == TOK_STRING);
1322 break;
1324 if (i == PP_IFNID || i == PP_ELIFNID ||
1325 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1326 i == PP_IFNSTR || i == PP_ELIFNSTR)
1327 j = !j;
1328 free_tlist (tline);
1329 return j;
1331 case PP_IF: case PP_ELIF:
1332 t = tline = expand_smacro(tline);
1333 tptr = &t;
1334 tokval.t_type = TOKEN_INVALID;
1335 evalresult = evaluate (ppscan, tptr, &tokval,
1336 NULL, pass | CRITICAL, error, NULL);
1337 free_tlist (tline);
1338 if (!evalresult)
1339 return -1;
1340 if (tokval.t_type)
1341 error(ERR_WARNING,
1342 "trailing garbage after expression ignored");
1343 if (!is_simple(evalresult)) {
1344 error(ERR_NONFATAL,
1345 "non-constant value given to `%s'", directives[i]);
1346 return -1;
1348 return reloc_value(evalresult) != 0;
1350 default:
1351 error(ERR_FATAL,
1352 "preprocessor directive `%s' not yet implemented",
1353 directives[i]);
1354 free_tlist (origline);
1355 return -1; /* yeah, right */
1360 * Expand macros in a string. Used in %error and %include directives.
1361 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1362 * The returned variable should ALWAYS be freed after usage.
1364 void expand_macros_in_string (char **p)
1366 Token *line = tokenise (*p);
1367 line = expand_smacro (line);
1368 *p = detoken (line, FALSE);
1372 * Find out if a line contains a preprocessor directive, and deal
1373 * with it if so.
1375 * If a directive _is_ found, we are expected to free_tlist() the
1376 * line.
1378 * Return values go like this:
1380 * bit 0 is set if a directive was found (so the line gets freed)
1382 static int do_directive (Token *tline)
1384 int i, j, k, m, nparam, nolist;
1385 #ifdef TASM_COMPAT
1386 int offset;
1387 #endif
1388 char *p, *mname;
1389 Include *inc;
1390 Context *ctx;
1391 Cond *cond;
1392 SMacro *smac, **smhead;
1393 MMacro *mmac;
1394 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1395 Line *l;
1396 struct tokenval tokval;
1397 expr *evalresult;
1398 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1400 origline = tline;
1402 skip_white_(tline);
1403 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1404 (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!'))
1405 return 0;
1407 i = -1;
1408 j = sizeof(directives)/sizeof(*directives);
1409 while (j-i > 1) {
1410 k = (j+i) / 2;
1411 m = nasm_stricmp(tline->text, directives[k]);
1412 if (m == 0) {
1413 i = k;
1414 j = -2;
1415 break;
1416 } else if (m < 0) {
1417 j = k;
1418 } else
1419 i = k;
1423 * If we're in a non-emitting branch of a condition construct,
1424 * or walking to the end of an already terminated %rep block,
1425 * we should ignore all directives except for condition
1426 * directives.
1428 if (((istk->conds && !emitting(istk->conds->state)) ||
1429 (istk->mstk && !istk->mstk->in_progress)) &&
1430 i != PP_IF && i != PP_ELIF &&
1431 i != PP_IFCTX && i != PP_ELIFCTX &&
1432 i != PP_IFDEF && i != PP_ELIFDEF &&
1433 i != PP_IFID && i != PP_ELIFID &&
1434 i != PP_IFIDN && i != PP_ELIFIDN &&
1435 i != PP_IFIDNI && i != PP_ELIFIDNI &&
1436 i != PP_IFNCTX && i != PP_ELIFNCTX &&
1437 i != PP_IFNDEF && i != PP_ELIFNDEF &&
1438 i != PP_IFNID && i != PP_ELIFNID &&
1439 i != PP_IFNIDN && i != PP_ELIFNIDN &&
1440 i != PP_IFNIDNI && i != PP_ELIFNIDNI &&
1441 i != PP_IFNNUM && i != PP_ELIFNNUM &&
1442 i != PP_IFNSTR && i != PP_ELIFNSTR &&
1443 i != PP_IFNUM && i != PP_ELIFNUM &&
1444 i != PP_IFSTR && i != PP_ELIFSTR &&
1445 i != PP_ELSE && i != PP_ENDIF)
1447 return 0;
1451 * If we're defining a macro or reading a %rep block, we should
1452 * ignore all directives except for %macro/%imacro (which
1453 * generate an error), %endm/%endmacro, and (only if we're in a
1454 * %rep block) %endrep. If we're in a %rep block, another %rep
1455 * causes an error, so should be let through.
1457 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1458 i != PP_ENDMACRO && i != PP_ENDM &&
1459 (defining->name || (i != PP_ENDREP && i != PP_REP)))
1461 return 0;
1464 if (j != -2) {
1465 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1466 tline->text);
1467 return 0; /* didn't get it */
1470 switch (i) {
1471 #ifdef TASM_COMPAT
1472 case PP_STACKSIZE:
1473 /* Directive to tell NASM what the default stack size is. The
1474 * default is for a 16-bit stack, and this can be overriden with
1475 * %stacksize large.
1476 * the following form:
1478 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1480 tline = tline->next;
1481 if (tline && tline->type == TOK_WHITESPACE)
1482 tline = tline->next;
1483 if (!tline || tline->type != TOK_ID) {
1484 error (ERR_NONFATAL,"`%%stacksize' missing size parameter");
1485 free_tlist (origline);
1486 return 3;
1488 if (nasm_stricmp(tline->text,"flat") == 0) {
1489 /* All subsequent ARG directives are for a 32-bit stack */
1490 StackSize = 4;
1491 StackPointer = "ebp";
1492 ArgOffset = 8;
1493 LocalOffset = 4;
1494 } else if (nasm_stricmp(tline->text,"large") == 0) {
1495 /* All subsequent ARG directives are for a 16-bit stack,
1496 * far function call.
1498 StackSize = 2;
1499 StackPointer = "bp";
1500 ArgOffset = 4;
1501 LocalOffset = 2;
1502 } else if (nasm_stricmp(tline->text,"small") == 0) {
1503 /* All subsequent ARG directives are for a 16-bit stack,
1504 * far function call. We don't support near functions.
1506 StackSize = 2;
1507 StackPointer = "bp";
1508 ArgOffset = 6;
1509 LocalOffset = 2;
1510 } else {
1511 error (ERR_NONFATAL,"`%%stacksize' invalid size type");
1512 free_tlist (origline);
1513 return 3;
1515 free_tlist(origline);
1516 return 3;
1518 case PP_ARG:
1519 /* TASM like ARG directive to define arguments to functions, in
1520 * the following form:
1522 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1524 offset = ArgOffset;
1525 do {
1526 char *arg,directive[256];
1527 int size = StackSize;
1529 /* Find the argument name */
1530 tline = tline->next;
1531 if (tline && tline->type == TOK_WHITESPACE)
1532 tline = tline->next;
1533 if (!tline || tline->type != TOK_ID) {
1534 error (ERR_NONFATAL,"`%%arg' missing argument parameter");
1535 free_tlist (origline);
1536 return 3;
1538 arg = tline->text;
1540 /* Find the argument size type */
1541 tline = tline->next;
1542 if (!tline || tline->type != TOK_OTHER || tline->text[0] != ':') {
1543 error (ERR_NONFATAL,"Syntax error processing `%%arg' directive");
1544 free_tlist (origline);
1545 return 3;
1547 tline = tline->next;
1548 if (!tline || tline->type != TOK_ID) {
1549 error (ERR_NONFATAL,"`%%arg' missing size type parameter");
1550 free_tlist (origline);
1551 return 3;
1554 /* Allow macro expansion of type parameter */
1555 tt = tokenise(tline->text);
1556 tt = expand_smacro(tt);
1557 if (nasm_stricmp(tt->text,"byte") == 0) {
1558 size = MAX(StackSize,1);
1559 } else if (nasm_stricmp(tt->text,"word") == 0) {
1560 size = MAX(StackSize,2);
1561 } else if (nasm_stricmp(tt->text,"dword") == 0) {
1562 size = MAX(StackSize,4);
1563 } else if (nasm_stricmp(tt->text,"qword") == 0) {
1564 size = MAX(StackSize,8);
1565 } else if (nasm_stricmp(tt->text,"tword") == 0) {
1566 size = MAX(StackSize,10);
1567 } else {
1568 error (ERR_NONFATAL,"Invalid size type for `%%arg' missing directive");
1569 free_tlist (tt);
1570 free_tlist (origline);
1571 return 3;
1573 free_tlist (tt);
1575 /* Now define the macro for the argument */
1576 sprintf(directive,"%%define %s (%s+%d)", arg, StackPointer, offset);
1577 do_directive(tokenise(directive));
1578 offset += size;
1580 /* Move to the next argument in the list */
1581 tline = tline->next;
1582 if (tline && tline->type == TOK_WHITESPACE)
1583 tline = tline->next;
1584 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1585 free_tlist (origline);
1586 return 3;
1588 case PP_LOCAL:
1589 /* TASM like LOCAL directive to define local variables for a
1590 * function, in the following form:
1592 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1594 * The '= LocalSize' at the end is ignored by NASM, but is
1595 * required by TASM to define the local parameter size (and used
1596 * by the TASM macro package).
1598 offset = LocalOffset;
1599 do {
1600 char *local,directive[256];
1601 int size = StackSize;
1603 /* Find the argument name */
1604 tline = tline->next;
1605 if (tline && tline->type == TOK_WHITESPACE)
1606 tline = tline->next;
1607 if (!tline || tline->type != TOK_ID) {
1608 error (ERR_NONFATAL,"`%%local' missing argument parameter");
1609 free_tlist (origline);
1610 return 3;
1612 local = tline->text;
1614 /* Find the argument size type */
1615 tline = tline->next;
1616 if (!tline || tline->type != TOK_OTHER || tline->text[0] != ':') {
1617 error (ERR_NONFATAL,"Syntax error processing `%%local' directive");
1618 free_tlist (origline);
1619 return 3;
1621 tline = tline->next;
1622 if (!tline || tline->type != TOK_ID) {
1623 error (ERR_NONFATAL,"`%%local' missing size type parameter");
1624 free_tlist (origline);
1625 return 3;
1628 /* Allow macro expansion of type parameter */
1629 tt = tokenise(tline->text);
1630 tt = expand_smacro(tt);
1631 if (nasm_stricmp(tt->text,"byte") == 0) {
1632 size = MAX(StackSize,1);
1633 } else if (nasm_stricmp(tt->text,"word") == 0) {
1634 size = MAX(StackSize,2);
1635 } else if (nasm_stricmp(tt->text,"dword") == 0) {
1636 size = MAX(StackSize,4);
1637 } else if (nasm_stricmp(tt->text,"qword") == 0) {
1638 size = MAX(StackSize,8);
1639 } else if (nasm_stricmp(tt->text,"tword") == 0) {
1640 size = MAX(StackSize,10);
1641 } else {
1642 error (ERR_NONFATAL,"Invalid size type for `%%local' missing directive");
1643 free_tlist (tt);
1644 free_tlist (origline);
1645 return 3;
1647 free_tlist (tt);
1649 /* Now define the macro for the argument */
1650 sprintf(directive,"%%define %s (%s-%d)", local, StackPointer, offset);
1651 do_directive(tokenise(directive));
1652 offset += size;
1654 /* Now define the assign to setup the enter_c macro correctly */
1655 sprintf(directive,"%%assign %%$localsize %%$localsize+%d", size);
1656 do_directive(tokenise(directive));
1658 /* Move to the next argument in the list */
1659 tline = tline->next;
1660 if (tline && tline->type == TOK_WHITESPACE)
1661 tline = tline->next;
1662 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1663 free_tlist (origline);
1664 return 3;
1665 #endif
1667 case PP_CLEAR:
1668 if (tline->next)
1669 error(ERR_WARNING,
1670 "trailing garbage after `%%clear' ignored");
1671 for (j=0; j<NHASH; j++) {
1672 while (mmacros[j]) {
1673 MMacro *m = mmacros[j];
1674 mmacros[j] = m->next;
1675 free_mmacro(m);
1677 while (smacros[j]) {
1678 SMacro *s = smacros[j];
1679 smacros[j] = smacros[j]->next;
1680 nasm_free (s->name);
1681 free_tlist (s->expansion);
1682 nasm_free (s);
1685 free_tlist (origline);
1686 return 3;
1688 case PP_INCLUDE:
1689 tline = tline->next;
1690 skip_white_(tline);
1691 if (!tline || (tline->type != TOK_STRING &&
1692 tline->type != TOK_INTERNAL_STRING))
1694 error(ERR_NONFATAL, "`%%include' expects a file name");
1695 free_tlist (origline);
1696 return 3; /* but we did _something_ */
1698 if (tline->next)
1699 error(ERR_WARNING,
1700 "trailing garbage after `%%include' ignored");
1701 if (tline->type != TOK_INTERNAL_STRING) {
1702 p = tline->text+1; /* point past the quote to the name */
1703 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
1704 } else
1705 p = tline->text; /* internal_string is easier */
1706 expand_macros_in_string (&p);
1707 inc = nasm_malloc(sizeof(Include));
1708 inc->next = istk;
1709 inc->conds = NULL;
1710 inc->fp = inc_fopen(p);
1711 inc->fname = src_set_fname (p);
1712 inc->lineno = src_set_linnum(0);
1713 inc->lineinc = 1;
1714 inc->expansion = NULL;
1715 inc->mstk = NULL;
1716 istk = inc;
1717 list->uplevel (LIST_INCLUDE);
1718 free_tlist (origline);
1719 return 5;
1721 case PP_PUSH:
1722 tline = tline->next;
1723 skip_white_(tline);
1724 tline = expand_id (tline);
1725 if (!tok_type_(tline, TOK_ID)) {
1726 error(ERR_NONFATAL,
1727 "`%%push' expects a context identifier");
1728 free_tlist (origline);
1729 return 3; /* but we did _something_ */
1731 if (tline->next)
1732 error(ERR_WARNING,
1733 "trailing garbage after `%%push' ignored");
1734 ctx = nasm_malloc(sizeof(Context));
1735 ctx->next = cstk;
1736 ctx->localmac = NULL;
1737 ctx->name = nasm_strdup(tline->text);
1738 ctx->number = unique++;
1739 cstk = ctx;
1740 free_tlist (origline);
1741 break;
1743 case PP_REPL:
1744 tline = tline->next;
1745 skip_white_(tline);
1746 tline = expand_id (tline);
1747 if (!tok_type_(tline, TOK_ID)) {
1748 error(ERR_NONFATAL,
1749 "`%%repl' expects a context identifier");
1750 free_tlist (origline);
1751 return 3; /* but we did _something_ */
1753 if (tline->next)
1754 error(ERR_WARNING,
1755 "trailing garbage after `%%repl' ignored");
1756 if (!cstk)
1757 error(ERR_NONFATAL,
1758 "`%%repl': context stack is empty");
1759 else {
1760 nasm_free (cstk->name);
1761 cstk->name = nasm_strdup(tline->text);
1763 free_tlist (origline);
1764 break;
1766 case PP_POP:
1767 if (tline->next)
1768 error(ERR_WARNING,
1769 "trailing garbage after `%%pop' ignored");
1770 if (!cstk)
1771 error(ERR_NONFATAL,
1772 "`%%pop': context stack is already empty");
1773 else
1774 ctx_pop();
1775 free_tlist (origline);
1776 break;
1778 case PP_ERROR:
1779 tline->next = expand_smacro (tline->next);
1780 tline = tline->next;
1781 skip_white_(tline);
1782 if (tok_type_(tline, TOK_STRING)) {
1783 p = tline->text+1; /* point past the quote to the name */
1784 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
1785 expand_macros_in_string (&p);
1786 error (ERR_NONFATAL, "%s", p);
1787 nasm_free (p);
1788 } else {
1789 p = detoken(tline, FALSE);
1790 error (ERR_WARNING, "%s", p);
1791 nasm_free(p);
1793 free_tlist (origline);
1794 break;
1796 case PP_IF:
1797 case PP_IFCTX:
1798 case PP_IFDEF:
1799 case PP_IFID:
1800 case PP_IFIDN:
1801 case PP_IFIDNI:
1802 case PP_IFNCTX:
1803 case PP_IFNDEF:
1804 case PP_IFNID:
1805 case PP_IFNIDN:
1806 case PP_IFNIDNI:
1807 case PP_IFNNUM:
1808 case PP_IFNSTR:
1809 case PP_IFNUM:
1810 case PP_IFSTR:
1811 if (istk->conds && !emitting(istk->conds->state))
1812 j = COND_NEVER;
1813 else {
1814 j = if_condition(tline->next, i);
1815 tline->next = NULL; /* it got freed */
1816 free_tlist (origline);
1817 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1819 cond = nasm_malloc(sizeof(Cond));
1820 cond->next = istk->conds;
1821 cond->state = j;
1822 istk->conds = cond;
1823 return (j == COND_IF_TRUE ? 3 : 1);
1825 case PP_ELIF:
1826 case PP_ELIFCTX:
1827 case PP_ELIFDEF:
1828 case PP_ELIFID:
1829 case PP_ELIFIDN:
1830 case PP_ELIFIDNI:
1831 case PP_ELIFNCTX:
1832 case PP_ELIFNDEF:
1833 case PP_ELIFNID:
1834 case PP_ELIFNIDN:
1835 case PP_ELIFNIDNI:
1836 case PP_ELIFNNUM:
1837 case PP_ELIFNSTR:
1838 case PP_ELIFNUM:
1839 case PP_ELIFSTR:
1840 if (!istk->conds)
1841 error(ERR_FATAL, "`%s': no matching `%%if'",
1842 directives[i]);
1843 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1844 istk->conds->state = COND_NEVER;
1845 else {
1846 j = if_condition(expand_mmac_params(tline->next), i);
1847 tline->next = NULL; /* it got freed */
1848 free_tlist (origline);
1849 istk->conds->state = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1851 return (istk->conds->state == COND_IF_TRUE ? 5 : 1);
1853 case PP_ELSE:
1854 if (tline->next)
1855 error(ERR_WARNING,
1856 "trailing garbage after `%%else' ignored");
1857 if (!istk->conds)
1858 error(ERR_FATAL,
1859 "`%%else': no matching `%%if'");
1860 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1861 istk->conds->state = COND_ELSE_FALSE;
1862 else
1863 istk->conds->state = COND_ELSE_TRUE;
1864 free_tlist (origline);
1865 return 5;
1867 case PP_ENDIF:
1868 if (tline->next)
1869 error(ERR_WARNING,
1870 "trailing garbage after `%%endif' ignored");
1871 if (!istk->conds)
1872 error(ERR_FATAL,
1873 "`%%endif': no matching `%%if'");
1874 cond = istk->conds;
1875 istk->conds = cond->next;
1876 nasm_free (cond);
1877 free_tlist (origline);
1878 return 5;
1880 case PP_MACRO:
1881 case PP_IMACRO:
1882 if (defining)
1883 error (ERR_FATAL,
1884 "`%%%smacro': already defining a macro",
1885 (i == PP_IMACRO ? "i" : ""));
1886 tline = tline->next;
1887 skip_white_(tline);
1888 tline = expand_id (tline);
1889 if (!tok_type_(tline, TOK_ID)) {
1890 error (ERR_NONFATAL,
1891 "`%%%smacro' expects a macro name",
1892 (i == PP_IMACRO ? "i" : ""));
1893 return 3;
1895 defining = nasm_malloc(sizeof(MMacro));
1896 defining->name = nasm_strdup(tline->text);
1897 defining->casesense = (i == PP_MACRO);
1898 defining->plus = FALSE;
1899 defining->nolist = FALSE;
1900 defining->in_progress = FALSE;
1901 defining->rep_nest = NULL;
1902 tline = expand_smacro (tline->next);
1903 skip_white_(tline);
1904 if (!tok_type_(tline, TOK_NUMBER)) {
1905 error (ERR_NONFATAL,
1906 "`%%%smacro' expects a parameter count",
1907 (i == PP_IMACRO ? "i" : ""));
1908 defining->nparam_min = defining->nparam_max = 0;
1909 } else {
1910 defining->nparam_min = defining->nparam_max =
1911 readnum(tline->text, &j);
1912 if (j)
1913 error (ERR_NONFATAL,
1914 "unable to parse parameter count `%s'", tline->text);
1916 if (tline && tok_is_(tline->next, "-")) {
1917 tline = tline->next->next;
1918 if (tok_is_(tline, "*"))
1919 defining->nparam_max = INT_MAX;
1920 else if (!tok_type_(tline, TOK_NUMBER))
1921 error (ERR_NONFATAL,
1922 "`%%%smacro' expects a parameter count after `-'",
1923 (i == PP_IMACRO ? "i" : ""));
1924 else {
1925 defining->nparam_max = readnum(tline->text, &j);
1926 if (j)
1927 error (ERR_NONFATAL,
1928 "unable to parse parameter count `%s'",
1929 tline->text);
1930 if (defining->nparam_min > defining->nparam_max)
1931 error (ERR_NONFATAL,
1932 "minimum parameter count exceeds maximum");
1935 if (tline && tok_is_(tline->next, "+")) {
1936 tline = tline->next;
1937 defining->plus = TRUE;
1939 if (tline && tok_type_(tline->next, TOK_ID) &&
1940 !nasm_stricmp(tline->next->text, ".nolist"))
1942 tline = tline->next;
1943 defining->nolist = TRUE;
1945 mmac = mmacros[hash(defining->name)];
1946 while (mmac) {
1947 if (!strcmp(mmac->name, defining->name) &&
1948 (mmac->nparam_min<=defining->nparam_max || defining->plus) &&
1949 (defining->nparam_min<=mmac->nparam_max || mmac->plus))
1951 error (ERR_WARNING,
1952 "redefining multi-line macro `%s'", defining->name);
1953 break;
1955 mmac = mmac->next;
1958 * Handle default parameters.
1960 if (tline && tline->next) {
1961 defining->dlist = tline->next;
1962 tline->next = NULL;
1963 count_mmac_params (defining->dlist, &defining->ndefs,
1964 &defining->defaults);
1965 } else {
1966 defining->dlist = NULL;
1967 defining->defaults = NULL;
1969 defining->expansion = NULL;
1970 free_tlist (origline);
1971 return 1;
1973 case PP_ENDM:
1974 case PP_ENDMACRO:
1975 if (!defining) {
1976 error (ERR_NONFATAL, "`%s': not defining a macro",
1977 tline->text);
1978 return 3;
1980 k = hash(defining->name);
1981 defining->next = mmacros[k];
1982 mmacros[k] = defining;
1983 defining = NULL;
1984 free_tlist (origline);
1985 return 5;
1987 case PP_ROTATE:
1988 if (tline->next && tline->next->type == TOK_WHITESPACE)
1989 tline = tline->next;
1990 t = expand_smacro(tline->next);
1991 tline->next = NULL;
1992 free_tlist (origline);
1993 tline = t;
1994 tptr = &t;
1995 tokval.t_type = TOKEN_INVALID;
1996 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1997 free_tlist (tline);
1998 if (!evalresult)
1999 return 3;
2000 if (tokval.t_type)
2001 error(ERR_WARNING,
2002 "trailing garbage after expression ignored");
2003 if (!is_simple(evalresult)) {
2004 error(ERR_NONFATAL,
2005 "non-constant value given to `%%rotate'");
2006 return 3;
2008 mmac = istk->mstk;
2009 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2010 mmac = mmac->next_active;
2011 if (!mmac)
2012 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2013 mmac->rotate = mmac->rotate + reloc_value(evalresult);
2014 if (mmac->rotate < 0)
2015 mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam;
2016 mmac->rotate %= mmac->nparam;
2017 return 1;
2019 case PP_REP:
2020 nolist = FALSE;
2021 tline = tline->next;
2022 if (tline->next && tline->next->type == TOK_WHITESPACE)
2023 tline = tline->next;
2024 if (tline->next && tline->next->type == TOK_ID &&
2025 !nasm_stricmp(tline->next->text, ".nolist")) {
2026 tline = tline->next;
2027 nolist = TRUE;
2029 t = expand_smacro(tline->next);
2030 tline->next = NULL;
2031 free_tlist (origline);
2032 tline = t;
2033 tptr = &t;
2034 tokval.t_type = TOKEN_INVALID;
2035 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2036 free_tlist (tline);
2037 if (!evalresult)
2038 return 3;
2039 if (tokval.t_type)
2040 error(ERR_WARNING,
2041 "trailing garbage after expression ignored");
2042 if (!is_simple(evalresult)) {
2043 error(ERR_NONFATAL,
2044 "non-constant value given to `%%rep'");
2045 return 3;
2047 tmp_defining = defining;
2048 defining = nasm_malloc(sizeof(MMacro));
2049 defining->name = NULL; /* flags this macro as a %rep block */
2050 defining->casesense = 0;
2051 defining->plus = FALSE;
2052 defining->nolist = nolist;
2053 defining->in_progress = reloc_value(evalresult) + 1;
2054 defining->nparam_min = defining->nparam_max = 0;
2055 defining->defaults = NULL;
2056 defining->dlist = NULL;
2057 defining->expansion = NULL;
2058 defining->next_active = istk->mstk;
2059 defining->rep_nest = tmp_defining;
2060 return 1;
2062 case PP_ENDREP:
2063 if (!defining || defining->name) {
2064 error (ERR_NONFATAL,
2065 "`%%endrep': no matching `%%rep'");
2066 return 3;
2070 * Now we have a "macro" defined - although it has no name
2071 * and we won't be entering it in the hash tables - we must
2072 * push a macro-end marker for it on to istk->expansion.
2073 * After that, it will take care of propagating itself (a
2074 * macro-end marker line for a macro which is really a %rep
2075 * block will cause the macro to be re-expanded, complete
2076 * with another macro-end marker to ensure the process
2077 * continues) until the whole expansion is forcibly removed
2078 * from istk->expansion by a %exitrep.
2080 l = nasm_malloc(sizeof(Line));
2081 l->next = istk->expansion;
2082 l->finishes = defining;
2083 l->first = NULL;
2084 istk->expansion = l;
2086 istk->mstk = defining;
2088 list->uplevel (defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2089 tmp_defining = defining;
2090 defining = defining->rep_nest;
2091 free_tlist (origline);
2092 return 1;
2094 case PP_EXITREP:
2096 * We must search along istk->expansion until we hit a
2097 * macro-end marker for a macro with no name. Then we set
2098 * its `in_progress' flag to 0.
2100 for (l = istk->expansion; l; l = l->next)
2101 if (l->finishes && !l->finishes->name)
2102 break;
2104 if (l)
2105 l->finishes->in_progress = 0;
2106 else
2107 error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2108 free_tlist (origline);
2109 return 1;
2111 case PP_XDEFINE:
2112 case PP_IXDEFINE:
2113 case PP_DEFINE:
2114 case PP_IDEFINE:
2115 tline = tline->next;
2116 skip_white_(tline);
2117 tline = expand_id (tline);
2118 if (!tline || (tline->type != TOK_ID &&
2119 (tline->type != TOK_PREPROC_ID ||
2120 tline->text[1] != '$'))) {
2121 error (ERR_NONFATAL,
2122 "`%%%s%sdefine' expects a macro identifier",
2123 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2124 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2125 free_tlist (origline);
2126 return 3;
2129 ctx = get_ctx (tline->text, FALSE);
2130 if (!ctx)
2131 smhead = &smacros[hash(tline->text)];
2132 else
2133 smhead = &ctx->localmac;
2134 mname = tline->text;
2135 last = tline;
2136 param_start = tline = tline->next;
2137 nparam = 0;
2139 /* Expand the macro definition now for %xdefine and %ixdefine */
2140 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2141 tline = expand_smacro (tline);
2143 if (tok_is_(tline, "(")) {
2145 * This macro has parameters.
2148 tline = tline->next;
2149 while (1) {
2150 skip_white_(tline);
2151 if (!tline) {
2152 error (ERR_NONFATAL,
2153 "parameter identifier expected");
2154 free_tlist (origline);
2155 return 3;
2157 if (tline->type != TOK_ID) {
2158 error (ERR_NONFATAL,
2159 "`%s': parameter identifier expected",
2160 tline->text);
2161 free_tlist (origline);
2162 return 3;
2164 tline->type = TOK_SMAC_PARAM + nparam++;
2165 tline = tline->next;
2166 skip_white_(tline);
2167 if (tok_is_(tline, ",")) {
2168 tline = tline->next;
2169 continue;
2171 if (!tok_is_(tline, ")")) {
2172 error (ERR_NONFATAL,
2173 "`)' expected to terminate macro template");
2174 free_tlist (origline);
2175 return 3;
2177 break;
2179 last = tline;
2180 tline = tline->next;
2182 if (tok_type_(tline, TOK_WHITESPACE))
2183 last = tline, tline = tline->next;
2184 macro_start = NULL;
2185 last->next = NULL;
2186 t = tline;
2187 while (t) {
2188 if (t->type == TOK_ID) {
2189 for (tt = param_start; tt; tt = tt->next)
2190 if (tt->type >= TOK_SMAC_PARAM &&
2191 !strcmp(tt->text, t->text))
2192 t->type = tt->type;
2194 tt = t->next;
2195 t->next = macro_start;
2196 macro_start = t;
2197 t = tt;
2200 * Good. We now have a macro name, a parameter count, and a
2201 * token list (in reverse order) for an expansion. We ought
2202 * to be OK just to create an SMacro, store it, and let
2203 * free_tlist have the rest of the line (which we have
2204 * carefully re-terminated after chopping off the expansion
2205 * from the end).
2207 if (smacro_defined (ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2208 if (!smac) {
2209 error (ERR_WARNING,
2210 "single-line macro `%s' defined both with and"
2211 " without parameters", mname);
2212 free_tlist (origline);
2213 free_tlist (macro_start);
2214 return 3;
2215 } else {
2217 * We're redefining, so we have to take over an
2218 * existing SMacro structure. This means freeing
2219 * what was already in it.
2221 nasm_free (smac->name);
2222 free_tlist (smac->expansion);
2224 } else {
2225 smac = nasm_malloc(sizeof(SMacro));
2226 smac->next = *smhead;
2227 *smhead = smac;
2229 smac->name = nasm_strdup(mname);
2230 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2231 smac->nparam = nparam;
2232 smac->expansion = macro_start;
2233 smac->in_progress = FALSE;
2234 free_tlist (origline);
2235 return 3;
2237 case PP_UNDEF:
2238 tline = tline->next;
2239 skip_white_(tline);
2240 tline = expand_id (tline);
2241 if (!tline || (tline->type != TOK_ID &&
2242 (tline->type != TOK_PREPROC_ID ||
2243 tline->text[1] != '$'))) {
2244 error (ERR_NONFATAL,
2245 "`%%undef' expects a macro identifier");
2246 free_tlist (origline);
2247 return 3;
2249 if (tline->next) {
2250 error (ERR_WARNING,
2251 "trailing garbage after macro name ignored");
2254 /* Find the context that symbol belongs to */
2255 ctx = get_ctx (tline->text, FALSE);
2256 if (!ctx)
2257 smhead = &smacros[hash(tline->text)];
2258 else
2259 smhead = &ctx->localmac;
2261 mname = tline->text;
2262 last = tline;
2263 last->next = NULL;
2266 * We now have a macro name... go hunt for it.
2268 while (smacro_defined (ctx, mname, -1, &smac, 1)) {
2269 /* Defined, so we need to find its predecessor and nuke it */
2270 SMacro **s;
2271 for ( s = smhead ; *s && *s != smac ; s = &(*s)->next );
2272 if ( *s ) {
2273 *s = smac->next;
2274 nasm_free(smac->name);
2275 free_tlist(smac->expansion);
2276 nasm_free(smac);
2279 free_tlist (origline);
2280 return 3;
2282 case PP_STRLEN:
2283 tline = tline->next;
2284 skip_white_(tline);
2285 tline = expand_id (tline);
2286 if (!tline || (tline->type != TOK_ID &&
2287 (tline->type != TOK_PREPROC_ID ||
2288 tline->text[1] != '$'))) {
2289 error (ERR_NONFATAL,
2290 "`%%strlen' expects a macro identifier as first parameter");
2291 free_tlist (origline);
2292 return 3;
2294 ctx = get_ctx (tline->text, FALSE);
2295 if (!ctx)
2296 smhead = &smacros[hash(tline->text)];
2297 else
2298 smhead = &ctx->localmac;
2299 mname = tline->text;
2300 last = tline;
2301 tline = expand_smacro (tline->next);
2302 last->next = NULL;
2304 t = tline;
2305 while (tok_type_(t, TOK_WHITESPACE))
2306 t = t->next;
2307 /* t should now point to the string */
2308 if (t->type != TOK_STRING) {
2309 error(ERR_NONFATAL,
2310 "`%%strlen` requires string as second parameter");
2311 free_tlist(tline);
2312 free_tlist(origline);
2313 return 3;
2316 macro_start = nasm_malloc(sizeof(*macro_start));
2317 macro_start->next = NULL;
2318 make_tok_num(macro_start, strlen(t->text)-2);
2319 macro_start->mac = NULL;
2322 * We now have a macro name, an implicit parameter count of
2323 * zero, and a numeric token to use as an expansion. Create
2324 * and store an SMacro.
2326 if (smacro_defined (ctx, mname, 0, &smac, i == PP_STRLEN)) {
2327 if (!smac)
2328 error (ERR_WARNING,
2329 "single-line macro `%s' defined both with and"
2330 " without parameters", mname);
2331 else {
2333 * We're redefining, so we have to take over an
2334 * existing SMacro structure. This means freeing
2335 * what was already in it.
2337 nasm_free (smac->name);
2338 free_tlist (smac->expansion);
2341 else {
2342 smac = nasm_malloc(sizeof(SMacro));
2343 smac->next = *smhead;
2344 *smhead = smac;
2346 smac->name = nasm_strdup(mname);
2347 smac->casesense = (i == PP_STRLEN);
2348 smac->nparam = 0;
2349 smac->expansion = macro_start;
2350 smac->in_progress = FALSE;
2351 free_tlist (tline);
2352 free_tlist (origline);
2353 return 3;
2355 case PP_SUBSTR:
2356 tline = tline->next;
2357 skip_white_(tline);
2358 tline = expand_id (tline);
2359 if (!tline || (tline->type != TOK_ID &&
2360 (tline->type != TOK_PREPROC_ID ||
2361 tline->text[1] != '$'))) {
2362 error (ERR_NONFATAL,
2363 "`%%substr' expects a macro identifier as first parameter");
2364 free_tlist (origline);
2365 return 3;
2367 ctx = get_ctx (tline->text, FALSE);
2368 if (!ctx)
2369 smhead = &smacros[hash(tline->text)];
2370 else
2371 smhead = &ctx->localmac;
2372 mname = tline->text;
2373 last = tline;
2374 tline = expand_smacro (tline->next);
2375 last->next = NULL;
2377 t = tline->next;
2378 while (tok_type_(t, TOK_WHITESPACE))
2379 t = t->next;
2381 /* t should now point to the string */
2382 if (t->type != TOK_STRING) {
2383 error(ERR_NONFATAL,
2384 "`%%substr` requires string as second parameter");
2385 free_tlist(tline);
2386 free_tlist(origline);
2387 return 3;
2390 tt = t->next;
2391 tptr = &tt;
2392 tokval.t_type = TOKEN_INVALID;
2393 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2394 if (!evalresult) {
2395 free_tlist(tline);
2396 free_tlist(origline);
2397 return 3;
2399 if (!is_simple(evalresult)) {
2400 error(ERR_NONFATAL,
2401 "non-constant value given to `%%substr`");
2402 free_tlist(tline);
2403 free_tlist(origline);
2404 return 3;
2407 macro_start = nasm_malloc(sizeof(*macro_start));
2408 macro_start->next = NULL;
2409 macro_start->text = nasm_strdup("'''");
2410 if (evalresult->value > 0 && evalresult->value < strlen(t->text)-1) {
2411 macro_start->text[1] = t->text[evalresult->value];
2413 else {
2414 macro_start->text[2] = '\0';
2416 macro_start->type = TOK_STRING;
2417 macro_start->mac = NULL;
2420 * We now have a macro name, an implicit parameter count of
2421 * zero, and a numeric token to use as an expansion. Create
2422 * and store an SMacro.
2424 if (smacro_defined (ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2425 if (!smac)
2426 error (ERR_WARNING,
2427 "single-line macro `%s' defined both with and"
2428 " without parameters", mname);
2429 else {
2431 * We're redefining, so we have to take over an
2432 * existing SMacro structure. This means freeing
2433 * what was already in it.
2435 nasm_free (smac->name);
2436 free_tlist (smac->expansion);
2439 else {
2440 smac = nasm_malloc(sizeof(SMacro));
2441 smac->next = *smhead;
2442 *smhead = smac;
2444 smac->name = nasm_strdup(mname);
2445 smac->casesense = (i == PP_SUBSTR);
2446 smac->nparam = 0;
2447 smac->expansion = macro_start;
2448 smac->in_progress = FALSE;
2449 free_tlist (tline);
2450 free_tlist (origline);
2451 return 3;
2454 case PP_ASSIGN:
2455 case PP_IASSIGN:
2456 tline = tline->next;
2457 skip_white_(tline);
2458 tline = expand_id (tline);
2459 if (!tline || (tline->type != TOK_ID &&
2460 (tline->type != TOK_PREPROC_ID ||
2461 tline->text[1] != '$'))) {
2462 error (ERR_NONFATAL,
2463 "`%%%sassign' expects a macro identifier",
2464 (i == PP_IASSIGN ? "i" : ""));
2465 free_tlist (origline);
2466 return 3;
2468 ctx = get_ctx (tline->text, FALSE);
2469 if (!ctx)
2470 smhead = &smacros[hash(tline->text)];
2471 else
2472 smhead = &ctx->localmac;
2473 mname = tline->text;
2474 last = tline;
2475 tline = expand_smacro (tline->next);
2476 last->next = NULL;
2478 t = tline;
2479 tptr = &t;
2480 tokval.t_type = TOKEN_INVALID;
2481 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2482 free_tlist (tline);
2483 if (!evalresult) {
2484 free_tlist (origline);
2485 return 3;
2488 if (tokval.t_type)
2489 error(ERR_WARNING,
2490 "trailing garbage after expression ignored");
2492 if (!is_simple(evalresult)) {
2493 error(ERR_NONFATAL,
2494 "non-constant value given to `%%%sassign'",
2495 (i == PP_IASSIGN ? "i" : ""));
2496 free_tlist (origline);
2497 return 3;
2500 macro_start = nasm_malloc(sizeof(*macro_start));
2501 macro_start->next = NULL;
2502 make_tok_num(macro_start, reloc_value(evalresult));
2503 macro_start->mac = NULL;
2506 * We now have a macro name, an implicit parameter count of
2507 * zero, and a numeric token to use as an expansion. Create
2508 * and store an SMacro.
2510 if (smacro_defined (ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2511 if (!smac)
2512 error (ERR_WARNING,
2513 "single-line macro `%s' defined both with and"
2514 " without parameters", mname);
2515 else {
2517 * We're redefining, so we have to take over an
2518 * existing SMacro structure. This means freeing
2519 * what was already in it.
2521 nasm_free (smac->name);
2522 free_tlist (smac->expansion);
2525 else {
2526 smac = nasm_malloc(sizeof(SMacro));
2527 smac->next = *smhead;
2528 *smhead = smac;
2530 smac->name = nasm_strdup(mname);
2531 smac->casesense = (i == PP_ASSIGN);
2532 smac->nparam = 0;
2533 smac->expansion = macro_start;
2534 smac->in_progress = FALSE;
2535 free_tlist (origline);
2536 return 3;
2538 case PP_LINE:
2540 * Syntax is `%line nnn[+mmm] [filename]'
2542 tline = tline->next;
2543 skip_white_(tline);
2544 if (!tok_type_(tline, TOK_NUMBER)) {
2545 error (ERR_NONFATAL, "`%%line' expects line number");
2546 free_tlist (origline);
2547 return 3;
2549 k = readnum(tline->text, &j);
2550 m = 1;
2551 tline = tline->next;
2552 if (tok_is_(tline, "+")) {
2553 tline = tline->next;
2554 if (!tok_type_(tline, TOK_NUMBER)) {
2555 error (ERR_NONFATAL,
2556 "`%%line' expects line increment");
2557 free_tlist (origline);
2558 return 3;
2560 m = readnum(tline->text, &j);
2561 tline = tline->next;
2563 skip_white_(tline);
2564 src_set_linnum(k);
2565 istk->lineinc = m;
2566 if (tline) {
2567 nasm_free (src_set_fname (detoken (tline, FALSE)));
2569 free_tlist (origline);
2570 return 5;
2572 default:
2573 error(ERR_FATAL,
2574 "preprocessor directive `%s' not yet implemented",
2575 directives[i]);
2576 break;
2578 return 3;
2582 * Ensure that a macro parameter contains a condition code and
2583 * nothing else. Return the condition code index if so, or -1
2584 * otherwise.
2586 static int find_cc (Token *t)
2588 Token *tt;
2589 int i, j, k, m;
2591 skip_white_(t);
2592 if (t->type != TOK_ID)
2593 return -1;
2594 tt = t->next;
2595 skip_white_(tt);
2596 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2597 return -1;
2599 i = -1;
2600 j = sizeof(conditions)/sizeof(*conditions);
2601 while (j-i > 1) {
2602 k = (j+i) / 2;
2603 m = nasm_stricmp(t->text, conditions[k]);
2604 if (m == 0) {
2605 i = k;
2606 j = -2;
2607 break;
2608 } else if (m < 0) {
2609 j = k;
2610 } else
2611 i = k;
2613 if (j != -2)
2614 return -1;
2615 return i;
2619 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2620 * %-n) and MMacro-local identifiers (%%foo).
2622 static Token *expand_mmac_params (Token *tline)
2624 Token *t, *tt, *ttt, **tail, *thead;
2626 tail = &thead;
2627 thead = NULL;
2629 while (tline) {
2630 if (tline->type == TOK_PREPROC_ID &&
2631 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text [2]) ||
2632 tline->text[1] == '%' ||
2633 (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2634 char *text = NULL;
2635 int type = 0, cc; /* type = 0 to placate optimisers */
2636 char tmpbuf[30];
2637 int n, i;
2638 MMacro *mac;
2640 t = tline;
2641 tline = tline->next;
2643 mac = istk->mstk;
2644 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2645 mac = mac->next_active;
2646 if (!mac)
2647 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2648 else switch (t->text[1]) {
2650 * We have to make a substitution of one of the
2651 * forms %1, %-1, %+1, %%foo, %0.
2653 case '0':
2654 type = TOK_NUMBER;
2655 sprintf(tmpbuf, "%d", mac->nparam);
2656 text = nasm_strdup(tmpbuf);
2657 break;
2658 case '%':
2659 type = TOK_ID;
2660 sprintf(tmpbuf, "..@%lu.", mac->unique);
2661 text = nasm_strcat(tmpbuf, t->text+2);
2662 break;
2663 case '-':
2664 n = atoi(t->text+2)-1;
2665 if (n >= mac->nparam)
2666 tt = NULL;
2667 else {
2668 if (mac->nparam > 1)
2669 n = (n + mac->rotate) % mac->nparam;
2670 tt = mac->params[n];
2672 cc = find_cc (tt);
2673 if (cc == -1) {
2674 error (ERR_NONFATAL,
2675 "macro parameter %d is not a condition code",
2676 n+1);
2677 text = NULL;
2678 } else {
2679 type = TOK_ID;
2680 if (inverse_ccs[cc] == -1) {
2681 error (ERR_NONFATAL,
2682 "condition code `%s' is not invertible",
2683 conditions[cc]);
2684 text = NULL;
2685 } else
2686 text = nasm_strdup(conditions[inverse_ccs[cc]]);
2688 break;
2689 case '+':
2690 n = atoi(t->text+2)-1;
2691 if (n >= mac->nparam)
2692 tt = NULL;
2693 else {
2694 if (mac->nparam > 1)
2695 n = (n + mac->rotate) % mac->nparam;
2696 tt = mac->params[n];
2698 cc = find_cc (tt);
2699 if (cc == -1) {
2700 error (ERR_NONFATAL,
2701 "macro parameter %d is not a condition code",
2702 n+1);
2703 text = NULL;
2704 } else {
2705 type = TOK_ID;
2706 text = nasm_strdup(conditions[cc]);
2708 break;
2709 default:
2710 n = atoi(t->text+1)-1;
2711 if (n >= mac->nparam)
2712 tt = NULL;
2713 else {
2714 if (mac->nparam > 1)
2715 n = (n + mac->rotate) % mac->nparam;
2716 tt = mac->params[n];
2718 if (tt) {
2719 for (i=0; i<mac->paramlen[n]; i++) {
2720 ttt = *tail = nasm_malloc(sizeof(Token));
2721 tail = &ttt->next;
2722 ttt->type = tt->type;
2723 ttt->text = nasm_strdup(tt->text);
2724 ttt->mac = NULL;
2725 tt = tt->next;
2728 text = NULL; /* we've done it here */
2729 break;
2731 nasm_free (t->text);
2732 if (!text) {
2733 nasm_free (t);
2734 } else {
2735 *tail = t;
2736 tail = &t->next;
2737 t->type = type;
2738 t->text = text;
2739 t->mac = NULL;
2741 continue;
2742 } else {
2743 t = *tail = tline;
2744 tline = tline->next;
2745 t->mac = NULL;
2746 tail = &t->next;
2749 *tail = NULL;
2750 t = thead;
2751 for (; t && (tt=t->next)!=NULL ; t = t->next)
2752 switch (t->type) {
2753 case TOK_WHITESPACE:
2754 if (tt->type == TOK_WHITESPACE) {
2755 t->next = tt->next;
2756 nasm_free(tt->text);
2757 nasm_free(tt);
2759 break;
2760 case TOK_ID:
2761 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2762 char *tmp = nasm_strcat(t->text, tt->text);
2763 nasm_free(t->text);
2764 t->text = tmp;
2765 t->next = tt->next;
2766 nasm_free(tt->text);
2767 nasm_free(tt);
2769 break;
2770 case TOK_NUMBER:
2771 if (tt->type == TOK_NUMBER) {
2772 char *tmp = nasm_strcat(t->text, tt->text);
2773 nasm_free(t->text);
2774 t->text = tmp;
2775 t->next = tt->next;
2776 nasm_free(tt->text);
2777 nasm_free(tt);
2779 break;
2782 return thead;
2786 * Expand all single-line macro calls made in the given line.
2787 * Return the expanded version of the line. The original is deemed
2788 * to be destroyed in the process. (In reality we'll just move
2789 * Tokens from input to output a lot of the time, rather than
2790 * actually bothering to destroy and replicate.)
2792 static Token *expand_smacro (Token *tline)
2794 Token *t, *tt, *mstart, **tail, *thead;
2795 SMacro *head = NULL, *m;
2796 Token **params;
2797 int *paramsize;
2798 int nparam, sparam, brackets, rescan;
2799 Token *org_tline = tline;
2800 Context *ctx;
2801 char *mname;
2804 * Trick: we should avoid changing the start token pointer since it can
2805 * be contained in "next" field of other token. Because of this
2806 * we allocate a copy of first token and work with it; at the end of
2807 * routine we copy it back
2809 if (org_tline)
2811 tline = nasm_malloc (sizeof (Token));
2812 *tline = *org_tline;
2815 again:
2816 tail = &thead;
2817 thead = NULL;
2819 while (tline) { /* main token loop */
2820 if ((mname = tline->text)) {
2821 /* if this token is a local macro, look in local context */
2822 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2823 ctx = get_ctx (mname, TRUE);
2824 else
2825 ctx = NULL;
2826 if (!ctx)
2827 head = smacros[hash(mname)];
2828 else
2829 head = ctx->localmac;
2831 * We've hit an identifier. As in is_mmacro below, we first
2832 * check whether the identifier is a single-line macro at
2833 * all, then think about checking for parameters if
2834 * necessary.
2836 for (m = head; m; m = m->next)
2837 if (!mstrcmp(m->name, mname, m->casesense))
2838 break;
2839 if (m) {
2840 mstart = tline;
2841 params = NULL;
2842 paramsize = NULL;
2843 if (m->nparam == 0) {
2845 * Simple case: the macro is parameterless. Discard the
2846 * one token that the macro call took, and push the
2847 * expansion back on the to-do stack.
2849 if (!m->expansion)
2851 if (!strcmp("__FILE__", m->name)) {
2852 long num=0;
2853 src_get(&num, &(tline->text));
2854 nasm_quote(&(tline->text));
2855 tline->type = TOK_STRING;
2856 continue;
2858 if (!strcmp("__LINE__", m->name)) {
2859 nasm_free(tline->text);
2860 make_tok_num(tline, src_get_linnum());
2861 continue;
2863 t = tline;
2864 tline = tline->next;
2865 nasm_free (t->text);
2866 nasm_free (t);
2867 continue;
2869 } else {
2871 * Complicated case: at least one macro with this name
2872 * exists and takes parameters. We must find the
2873 * parameters in the call, count them, find the SMacro
2874 * that corresponds to that form of the macro call, and
2875 * substitute for the parameters when we expand. What a
2876 * pain.
2878 tline = tline->next;
2879 skip_white_(tline);
2880 if (!tok_is_(tline, "(")) {
2882 * This macro wasn't called with parameters: ignore
2883 * the call. (Behaviour borrowed from gnu cpp.)
2885 tline = mstart;
2886 m = NULL;
2888 else {
2889 int paren = 0;
2890 int white = 0;
2891 brackets = 0;
2892 nparam = 0;
2893 tline = tline->next;
2894 sparam = PARAM_DELTA;
2895 params = nasm_malloc (sparam*sizeof(Token *));
2896 params[0] = tline;
2897 paramsize = nasm_malloc (sparam*sizeof(int));
2898 paramsize[0] = 0;
2899 for (;;tline = tline->next) { /* parameter loop */
2900 if (!tline) {
2901 error(ERR_NONFATAL,
2902 "macro call expects terminating `)'");
2903 break;
2905 if (tline->type == TOK_WHITESPACE && brackets<=0) {
2906 if (paramsize[nparam])
2907 white++;
2908 else
2909 params[nparam] = tline->next;
2910 continue; /* parameter loop */
2912 if (tline->type == TOK_OTHER && tline->text[1]==0) {
2913 char ch = tline->text[0];
2914 if (ch == ',' && !paren && brackets<=0) {
2915 if (++nparam >= sparam) {
2916 sparam += PARAM_DELTA;
2917 params = nasm_realloc (params,
2918 sparam*sizeof(Token *));
2919 paramsize = nasm_realloc (paramsize,
2920 sparam*sizeof(int));
2922 params[nparam] = tline->next;
2923 paramsize[nparam] = 0;
2924 white = 0;
2925 continue; /* parameter loop */
2927 if (ch == '{' &&
2928 (brackets>0 || (brackets==0 &&
2929 !paramsize[nparam])))
2931 if (!(brackets++))
2933 params[nparam] = tline->next;
2934 continue; /* parameter loop */
2937 if (ch == '}' && brackets>0)
2938 if (--brackets == 0) {
2939 brackets = -1;
2940 continue; /* parameter loop */
2942 if (ch == '(' && !brackets)
2943 paren++;
2944 if (ch == ')' && brackets<=0)
2945 if (--paren < 0)
2946 break;
2948 if (brackets<0) {
2949 brackets = 0;
2950 error (ERR_NONFATAL, "braces do not "
2951 "enclose all of macro parameter");
2953 paramsize[nparam] += white+1;
2954 white = 0;
2955 } /* parameter loop */
2956 nparam++;
2957 while (m && (m->nparam != nparam ||
2958 mstrcmp(m->name, mname, m->casesense)))
2959 m = m->next;
2960 if (!m)
2961 error (ERR_WARNING|ERR_WARN_MNP,
2962 "macro `%s' exists, "
2963 "but not taking %d parameters",
2964 mstart->text, nparam);
2967 if (m && m->in_progress)
2968 m = NULL;
2969 if (!m) /* in progess or didn't find '(' or wrong nparam */
2972 * Design question: should we handle !tline, which
2973 * indicates missing ')' here, or expand those
2974 * macros anyway, which requires the (t) test a few
2975 * lines down?
2977 nasm_free (params);
2978 nasm_free (paramsize);
2979 tline = mstart;
2980 } else {
2982 * Expand the macro: we are placed on the last token of the
2983 * call, so that we can easily split the call from the
2984 * following tokens. We also start by pushing an SMAC_END
2985 * token for the cycle removal.
2987 t = tline;
2988 if (t) {
2989 tline = t->next;
2990 t->next = NULL;
2992 tt = nasm_malloc(sizeof(Token));
2993 tt->type = TOK_SMAC_END;
2994 tt->text = NULL;
2995 tt->mac = m;
2996 m->in_progress = TRUE;
2997 tt->next = tline;
2998 tline = tt;
2999 for (t = m->expansion; t; t = t->next) {
3000 if (t->type >= TOK_SMAC_PARAM) {
3001 Token *pcopy = tline, **ptail = &pcopy;
3002 Token *ttt, *pt;
3003 int i;
3005 ttt = params[t->type - TOK_SMAC_PARAM];
3006 for (i=paramsize[t->type-TOK_SMAC_PARAM]; --i>=0;) {
3007 pt = *ptail = nasm_malloc(sizeof(Token));
3008 pt->next = tline;
3009 ptail = &pt->next;
3010 pt->text = nasm_strdup(ttt->text);
3011 pt->type = ttt->type;
3012 pt->mac = NULL;
3013 ttt = ttt->next;
3015 tline = pcopy;
3016 } else {
3017 tt = nasm_malloc(sizeof(Token));
3018 tt->type = t->type;
3019 tt->text = nasm_strdup(t->text);
3020 tt->mac = NULL;
3021 tt->next = tline;
3022 tline = tt;
3027 * Having done that, get rid of the macro call, and clean
3028 * up the parameters.
3030 nasm_free (params);
3031 nasm_free (paramsize);
3032 free_tlist (mstart);
3033 continue; /* main token loop */
3038 if (tline->type == TOK_SMAC_END) {
3039 tline->mac->in_progress = FALSE;
3040 t = tline;
3041 tline = tline->next;
3042 nasm_free (t);
3043 } else {
3044 t = *tail = tline;
3045 tline = tline->next;
3046 t->mac = NULL;
3047 t->next = NULL;
3048 tail = &t->next;
3053 * Now scan the entire line and look for successive TOK_IDs that resulted
3054 * after expansion (they can't be produced by tokenise()). The successive
3055 * TOK_IDs should be concatenated.
3056 * Also we look for %+ tokens and concatenate the tokens before and after
3057 * them (without white spaces in between).
3059 t = thead;
3060 rescan = 0;
3061 while (t) {
3062 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3063 t = t->next;
3064 if (!t || !t->next)
3065 break;
3066 if (t->next->type == TOK_ID ||
3067 t->next->type == TOK_PREPROC_ID ||
3068 t->next->type == TOK_NUMBER) {
3069 Token *next = t->next->next;
3070 char *p = nasm_malloc (strlen (t->text) + strlen (t->next->text) + 1);
3071 strcpy (p, t->text);
3072 strcat (p, t->next->text);
3073 nasm_free (t->text);
3074 nasm_free (t->next->text);
3075 nasm_free (t->next);
3076 t->next = next;
3077 t->text = p;
3078 rescan = 1;
3079 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3080 t->next->next->type == TOK_PREPROC_ID &&
3081 strcmp (t->next->next->text, "%+") == 0) {
3082 /* free the next whitespace, the %+ token and next whitespace */
3083 int i;
3084 for (i = 1; i <= 3; i++)
3086 Token *next;
3087 if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
3088 break;
3089 next = t->next->next;
3090 nasm_free (t->next->text);
3091 nasm_free (t->next);
3092 t->next = next;
3093 } /* endfor */
3094 } else
3095 t = t->next;
3097 /* If we concatenaded something, re-scan the line for macros */
3098 if (rescan) {
3099 tline = thead;
3100 goto again;
3103 if (org_tline)
3105 if (thead) {
3106 *org_tline = *thead;
3107 nasm_free (thead);
3108 } else
3110 /* the expression expanded to empty line;
3111 we can't return NULL for some reasons
3112 we just set the line to a single WHITESPACE token. */
3113 memset (org_tline, 0, sizeof (*org_tline));
3114 org_tline->text = nasm_strdup (" ");
3115 org_tline->type = TOK_WHITESPACE;
3117 thead = org_tline;
3120 return thead;
3124 * Similar to expand_smacro but used exclusively with macro identifiers
3125 * right before they are fetched in. The reason is that there can be
3126 * identifiers consisting of several subparts. We consider that if there
3127 * are more than one element forming the name, user wants a expansion,
3128 * otherwise it will be left as-is. Example:
3130 * %define %$abc cde
3132 * the identifier %$abc will be left as-is so that the handler for %define
3133 * will suck it and define the corresponding value. Other case:
3135 * %define _%$abc cde
3137 * In this case user wants name to be expanded *before* %define starts
3138 * working, so we'll expand %$abc into something (if it has a value;
3139 * otherwise it will be left as-is) then concatenate all successive
3140 * PP_IDs into one.
3142 static Token *expand_id (Token *tline)
3144 Token *cur, *oldnext = NULL;
3146 if (!tline ||
3147 !tline->next)
3148 return tline;
3150 cur = tline;
3151 while (cur->next &&
3152 (cur->next->type == TOK_ID ||
3153 cur->next->type == TOK_PREPROC_ID ||
3154 cur->next->type == TOK_NUMBER))
3155 cur = cur->next;
3157 /* If identifier consists of just one token, don't expand */
3158 if (cur == tline)
3159 return tline;
3161 if (cur) {
3162 oldnext = cur->next; /* Detach the tail past identifier */
3163 cur->next = NULL; /* so that expand_smacro stops here */
3166 tline = expand_smacro (tline);
3168 if (cur) {
3169 /* expand_smacro possibly changhed tline; re-scan for EOL */
3170 cur = tline;
3171 while (cur && cur->next)
3172 cur = cur->next;
3173 if (cur)
3174 cur->next = oldnext;
3177 return tline;
3181 * Determine whether the given line constitutes a multi-line macro
3182 * call, and return the MMacro structure called if so. Doesn't have
3183 * to check for an initial label - that's taken care of in
3184 * expand_mmacro - but must check numbers of parameters. Guaranteed
3185 * to be called with tline->type == TOK_ID, so the putative macro
3186 * name is easy to find.
3188 static MMacro *is_mmacro (Token *tline, Token ***params_array)
3190 MMacro *head, *m;
3191 Token **params;
3192 int nparam;
3194 head = mmacros[hash(tline->text)];
3197 * Efficiency: first we see if any macro exists with the given
3198 * name. If not, we can return NULL immediately. _Then_ we
3199 * count the parameters, and then we look further along the
3200 * list if necessary to find the proper MMacro.
3202 for (m = head; m; m = m->next)
3203 if (!mstrcmp(m->name, tline->text, m->casesense))
3204 break;
3205 if (!m)
3206 return NULL;
3209 * OK, we have a potential macro. Count and demarcate the
3210 * parameters.
3212 count_mmac_params (tline->next, &nparam, &params);
3215 * So we know how many parameters we've got. Find the MMacro
3216 * structure that handles this number.
3218 while (m) {
3219 if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) {
3221 * This one is right. Just check if cycle removal
3222 * prohibits us using it before we actually celebrate...
3224 if (m->in_progress) {
3225 #if 0
3226 error (ERR_NONFATAL,
3227 "self-reference in multi-line macro `%s'",
3228 m->name);
3229 #endif
3230 nasm_free (params);
3231 return NULL;
3234 * It's right, and we can use it. Add its default
3235 * parameters to the end of our list if necessary.
3237 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3238 params = nasm_realloc (params, ((m->nparam_min+m->ndefs+1) *
3239 sizeof(*params)));
3240 while (nparam < m->nparam_min + m->ndefs) {
3241 params[nparam] = m->defaults[nparam - m->nparam_min];
3242 nparam++;
3246 * If we've gone over the maximum parameter count (and
3247 * we're in Plus mode), ignore parameters beyond
3248 * nparam_max.
3250 if (m->plus && nparam > m->nparam_max)
3251 nparam = m->nparam_max;
3253 * Then terminate the parameter list, and leave.
3255 if (!params) { /* need this special case */
3256 params = nasm_malloc(sizeof(*params));
3257 nparam = 0;
3259 params[nparam] = NULL;
3260 *params_array = params;
3261 return m;
3264 * This one wasn't right: look for the next one with the
3265 * same name.
3267 for (m = m->next; m; m = m->next)
3268 if (!mstrcmp(m->name, tline->text, m->casesense))
3269 break;
3273 * After all that, we didn't find one with the right number of
3274 * parameters. Issue a warning, and fail to expand the macro.
3276 error (ERR_WARNING|ERR_WARN_MNP,
3277 "macro `%s' exists, but not taking %d parameters",
3278 tline->text, nparam);
3279 nasm_free (params);
3280 return NULL;
3284 * Expand the multi-line macro call made by the given line, if
3285 * there is one to be expanded. If there is, push the expansion on
3286 * istk->expansion and return 1. Otherwise return 0.
3288 static int expand_mmacro (Token *tline)
3290 Token *startline = tline;
3291 Token *label = NULL;
3292 int dont_prepend = 0;
3293 Token **params, *t, *tt;
3294 MMacro *m;
3295 Line *l, *ll;
3296 int i, nparam, *paramlen;
3298 t = tline;
3299 skip_white_(t);
3300 if (!tok_type_(t, TOK_ID))
3301 return 0;
3302 m = is_mmacro (t, &params);
3303 if (!m) {
3304 Token *last;
3306 * We have an id which isn't a macro call. We'll assume
3307 * it might be a label; we'll also check to see if a
3308 * colon follows it. Then, if there's another id after
3309 * that lot, we'll check it again for macro-hood.
3311 label = last = t;
3312 t = t->next;
3313 if (tok_type_(t, TOK_WHITESPACE))
3314 last = t, t = t->next;
3315 if (tok_is_(t, ":")) {
3316 dont_prepend = 1;
3317 last = t, t = t->next;
3318 if (tok_type_(t, TOK_WHITESPACE))
3319 last = t, t = t->next;
3321 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3322 return 0;
3323 last->next = NULL;
3324 tline = t;
3328 * Fix up the parameters: this involves stripping leading and
3329 * trailing whitespace, then stripping braces if they are
3330 * present.
3332 for (nparam = 0; params[nparam]; nparam++)
3334 paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL;
3336 for (i = 0; params[i]; i++) {
3337 int brace = FALSE;
3338 int comma = (!m->plus || i < nparam-1);
3340 t = params[i];
3341 skip_white_(t);
3342 if (tok_is_(t, "{"))
3343 t = t->next, brace = TRUE, comma = FALSE;
3344 params[i] = t;
3345 paramlen[i] = 0;
3346 while (t) {
3347 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3348 break; /* ... because we have hit a comma */
3349 if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
3350 break; /* ... or a space then a comma */
3351 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3352 break; /* ... or a brace */
3353 t = t->next;
3354 paramlen[i]++;
3359 * OK, we have a MMacro structure together with a set of
3360 * parameters. We must now go through the expansion and push
3361 * copies of each Line on to istk->expansion. Substitution of
3362 * parameter tokens and macro-local tokens doesn't get done
3363 * until the single-line macro substitution process; this is
3364 * because delaying them allows us to change the semantics
3365 * later through %rotate.
3367 * First, push an end marker on to istk->expansion, mark this
3368 * macro as in progress, and set up its invocation-specific
3369 * variables.
3371 ll = nasm_malloc(sizeof(Line));
3372 ll->next = istk->expansion;
3373 ll->finishes = m;
3374 ll->first = NULL;
3375 istk->expansion = ll;
3377 m->in_progress = TRUE;
3378 m->params = params;
3379 m->iline = tline;
3380 m->nparam = nparam;
3381 m->rotate = 0;
3382 m->paramlen = paramlen;
3383 m->unique = unique++;
3384 m->lineno = 0;
3386 m->next_active = istk->mstk;
3387 istk->mstk = m;
3389 for (l = m->expansion; l; l = l->next) {
3390 Token **tail;
3392 ll = nasm_malloc(sizeof(Line));
3393 ll->finishes = NULL;
3394 ll->next = istk->expansion;
3395 istk->expansion = ll;
3396 tail = &ll->first;
3398 for (t = l->first; t; t = t->next) {
3399 Token *x = t;
3400 if (t->type == TOK_PREPROC_ID &&
3401 t->text[1]=='0' && t->text[2]=='0')
3403 dont_prepend = -1;
3404 x = label;
3405 if (!x)
3406 continue;
3408 tt = *tail = nasm_malloc(sizeof(Token));
3409 tail = &tt->next;
3410 tt->type = x->type;
3411 tt->text = nasm_strdup(x->text);
3412 tt->mac = NULL;
3414 *tail = NULL;
3418 * If we had a label, push it on as the first line of
3419 * the macro expansion.
3421 if (label) {
3422 if (dont_prepend<0)
3423 free_tlist(startline);
3424 else {
3425 ll = nasm_malloc(sizeof(Line));
3426 ll->finishes = NULL;
3427 ll->next = istk->expansion;
3428 istk->expansion = ll;
3429 ll->first = startline;
3430 if (!dont_prepend) {
3431 while (label->next)
3432 label = label->next;
3433 label->next = tt = nasm_malloc(sizeof(Token));
3434 tt->next = NULL;
3435 tt->mac = NULL;
3436 tt->type = TOK_OTHER;
3437 tt->text = nasm_strdup(":");
3442 list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3444 return 1;
3448 * Since preprocessor always operate only on the line that didn't
3449 * arrived yet, we should always use ERR_OFFBY1. Also since user
3450 * won't want to see same error twice (preprocessing is done once
3451 * per pass) we will want to show errors only during pass one.
3453 static void error (int severity, char *fmt, ...)
3455 va_list arg;
3456 char buff [1024];
3458 /* If we're in a dead branch of IF or something like it, ignore the error */
3459 if (istk->conds && !emitting(istk->conds->state))
3460 return;
3462 va_start (arg, fmt);
3463 vsprintf (buff, fmt, arg);
3464 va_end (arg);
3466 if (istk->mstk && istk->mstk->name)
3467 __error (severity|ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3468 istk->mstk->lineno, buff);
3469 else
3470 __error (severity|ERR_PASS1, "%s", buff);
3473 static void pp_reset (char *file, int apass, efunc errfunc, evalfunc eval,
3474 ListGen *listgen)
3476 int h;
3478 __error = errfunc;
3479 cstk = NULL;
3480 istk = nasm_malloc(sizeof(Include));
3481 istk->next = NULL;
3482 istk->conds = NULL;
3483 istk->expansion = NULL;
3484 istk->mstk = NULL;
3485 istk->fp = fopen(file, "r");
3486 istk->fname = NULL;
3487 src_set_fname(nasm_strdup(file));
3488 src_set_linnum(0);
3489 istk->lineinc = 1;
3490 if (!istk->fp)
3491 error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file);
3492 defining = NULL;
3493 for (h=0; h<NHASH; h++) {
3494 mmacros[h] = NULL;
3495 smacros[h] = NULL;
3497 unique = 0;
3498 stdmacpos = stdmac;
3499 any_extrastdmac = (extrastdmac != NULL);
3500 list = listgen;
3501 evaluate = eval;
3502 pass = apass;
3505 static char *pp_getline (void)
3507 char *line;
3508 Token *tline;
3509 int ret;
3511 while (1) {
3513 * Fetch a tokenised line, either from the macro-expansion
3514 * buffer or from the input file.
3516 tline = NULL;
3517 while (istk->expansion && istk->expansion->finishes) {
3518 Line *l = istk->expansion;
3519 if (!l->finishes->name && l->finishes->in_progress > 1) {
3520 Line *ll;
3523 * This is a macro-end marker for a macro with no
3524 * name, which means it's not really a macro at all
3525 * but a %rep block, and the `in_progress' field is
3526 * more than 1, meaning that we still need to
3527 * repeat. (1 means the natural last repetition; 0
3528 * means termination by %exitrep.) We have
3529 * therefore expanded up to the %endrep, and must
3530 * push the whole block on to the expansion buffer
3531 * again. We don't bother to remove the macro-end
3532 * marker: we'd only have to generate another one
3533 * if we did.
3535 l->finishes->in_progress--;
3536 for (l = l->finishes->expansion; l; l = l->next) {
3537 Token *t, *tt, **tail;
3539 ll = nasm_malloc(sizeof(Line));
3540 ll->next = istk->expansion;
3541 ll->finishes = NULL;
3542 ll->first = NULL;
3543 tail = &ll->first;
3545 for (t = l->first; t; t = t->next) {
3546 if (t->text) {
3547 tt = *tail = nasm_malloc(sizeof(Token));
3548 tt->next = NULL;
3549 tail = &tt->next;
3550 tt->type = t->type;
3551 tt->text = nasm_strdup(t->text);
3552 tt->mac = NULL;
3556 istk->expansion = ll;
3558 } else {
3560 * Check whether a `%rep' was started and not ended
3561 * within this macro expansion. This can happen and
3562 * should be detected. It's a fatal error because
3563 * I'm too confused to work out how to recover
3564 * sensibly from it.
3566 if (defining) {
3567 if (defining->name)
3568 error (ERR_PANIC,
3569 "defining with name in expansion");
3570 else if (istk->mstk->name)
3571 error (ERR_FATAL, "`%%rep' without `%%endrep' within"
3572 " expansion of macro `%s'", istk->mstk->name);
3576 * FIXME: investigate the relationship at this point between
3577 * istk->mstk and l->finishes
3580 MMacro *m = istk->mstk;
3581 istk->mstk = m->next_active;
3582 if (m->name) {
3584 * This was a real macro call, not a %rep, and
3585 * therefore the parameter information needs to
3586 * be freed.
3588 nasm_free(m->params);
3589 free_tlist(m->iline);
3590 nasm_free(m->paramlen);
3591 l->finishes->in_progress = FALSE;
3593 else
3594 free_mmacro(m);
3596 istk->expansion = l->next;
3597 nasm_free (l);
3598 list->downlevel (LIST_MACRO);
3601 while (1) { /* until we get a line we can use */
3603 if (istk->expansion) { /* from a macro expansion */
3604 char *p;
3605 Line *l = istk->expansion;
3606 if (istk->mstk)
3607 istk->mstk->lineno++;
3608 tline = l->first;
3609 istk->expansion = l->next;
3610 nasm_free (l);
3611 p = detoken (tline, FALSE);
3612 list->line (LIST_MACRO, p);
3613 nasm_free(p);
3614 break;
3616 line = read_line();
3617 if (line) { /* from the current input file */
3618 line = prepreproc(line);
3619 tline = tokenise(line);
3620 nasm_free (line);
3621 break;
3624 * The current file has ended; work down the istk
3627 Include *i = istk;
3628 fclose(i->fp);
3629 if (i->conds)
3630 error(ERR_FATAL, "expected `%%endif' before end of file");
3631 istk = i->next;
3632 list->downlevel (LIST_INCLUDE);
3633 src_set_linnum(i->lineno);
3634 nasm_free ( src_set_fname(i->fname) );
3635 nasm_free (i);
3636 if (!istk)
3637 return NULL;
3642 * We must expand MMacro parameters and MMacro-local labels
3643 * _before_ we plunge into directive processing, to cope
3644 * with things like `%define something %1' such as STRUC
3645 * uses. Unless we're _defining_ a MMacro, in which case
3646 * those tokens should be left alone to go into the
3647 * definition; and unless we're in a non-emitting
3648 * condition, in which case we don't want to meddle with
3649 * anything.
3651 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3652 tline = expand_mmac_params(tline);
3655 * Check the line to see if it's a preprocessor directive.
3657 ret = do_directive(tline);
3658 if (ret & 1) {
3659 continue;
3660 } else if (defining) {
3662 * We're defining a multi-line macro. We emit nothing
3663 * at all, and just
3664 * shove the tokenised line on to the macro definition.
3666 Line *l = nasm_malloc(sizeof(Line));
3667 l->next = defining->expansion;
3668 l->first = tline;
3669 l->finishes = FALSE;
3670 defining->expansion = l;
3671 continue;
3672 } else if (istk->conds && !emitting(istk->conds->state)) {
3674 * We're in a non-emitting branch of a condition block.
3675 * Emit nothing at all, not even a blank line: when we
3676 * emerge from the condition we'll give a line-number
3677 * directive so we keep our place correctly.
3679 free_tlist(tline);
3680 continue;
3681 } else if (istk->mstk && !istk->mstk->in_progress) {
3683 * We're in a %rep block which has been terminated, so
3684 * we're walking through to the %endrep without
3685 * emitting anything. Emit nothing at all, not even a
3686 * blank line: when we emerge from the %rep block we'll
3687 * give a line-number directive so we keep our place
3688 * correctly.
3690 free_tlist(tline);
3691 continue;
3692 } else {
3693 tline = expand_smacro(tline);
3694 ret = expand_mmacro(tline);
3695 if (!ret) {
3697 * De-tokenise the line again, and emit it.
3699 line = detoken(tline, TRUE);
3700 free_tlist (tline);
3701 break;
3702 } else {
3703 continue; /* expand_mmacro calls free_tlist */
3708 return line;
3711 static void pp_cleanup (void)
3713 int h;
3715 if (defining) {
3716 error (ERR_NONFATAL, "end of file while still defining macro `%s'",
3717 defining->name);
3718 free_mmacro (defining);
3720 while (cstk)
3721 ctx_pop();
3722 for (h=0; h<NHASH; h++) {
3723 while (mmacros[h]) {
3724 MMacro *m = mmacros[h];
3725 mmacros[h] = mmacros[h]->next;
3726 free_mmacro(m);
3728 while (smacros[h]) {
3729 SMacro *s = smacros[h];
3730 smacros[h] = smacros[h]->next;
3731 nasm_free (s->name);
3732 free_tlist (s->expansion);
3733 nasm_free (s);
3736 while (istk) {
3737 Include *i = istk;
3738 istk = istk->next;
3739 fclose(i->fp);
3740 nasm_free (i->fname);
3741 nasm_free (i);
3743 while (cstk)
3744 ctx_pop();
3747 void pp_include_path (char *path)
3749 IncPath *i;
3751 i = nasm_malloc(sizeof(IncPath));
3752 i->path = nasm_strdup(path);
3753 i->next = ipath;
3754 ipath = i;
3757 void pp_pre_include (char *fname)
3759 Token *inc, *space, *name;
3760 Line *l;
3762 inc = nasm_malloc(sizeof(Token));
3763 inc->next = space = nasm_malloc(sizeof(Token));
3764 space->next = name = nasm_malloc(sizeof(Token));
3765 name->next = NULL;
3767 inc->type = TOK_PREPROC_ID;
3768 inc->text = nasm_strdup("%include");
3769 space->type = TOK_WHITESPACE;
3770 space->text = nasm_strdup(" ");
3771 name->type = TOK_INTERNAL_STRING;
3772 name->text = nasm_strdup(fname);
3774 inc->mac = space->mac = name->mac = NULL;
3776 l = nasm_malloc(sizeof(Line));
3777 l->next = predef;
3778 l->first = inc;
3779 l->finishes = FALSE;
3780 predef = l;
3783 void pp_pre_define (char *definition)
3785 Token *def, *space;
3786 Line *l;
3787 char *equals;
3789 equals = strchr(definition, '=');
3791 def = nasm_malloc(sizeof(Token));
3792 def->next = space = nasm_malloc(sizeof(Token));
3793 if (equals)
3794 *equals = ' ';
3795 space->next = tokenise(definition);
3796 if (equals)
3797 *equals = '=';
3799 def->type = TOK_PREPROC_ID;
3800 def->text = nasm_strdup("%define");
3801 space->type = TOK_WHITESPACE;
3802 space->text = nasm_strdup(" ");
3804 def->mac = space->mac = NULL;
3806 l = nasm_malloc(sizeof(Line));
3807 l->next = predef;
3808 l->first = def;
3809 l->finishes = FALSE;
3810 predef = l;
3813 void pp_pre_undefine (char *definition)
3815 Token *def, *space;
3816 Line *l;
3818 def = nasm_malloc(sizeof(Token));
3819 def->next = space = nasm_malloc(sizeof(Token));
3820 space->next = tokenise(definition);
3822 def->type = TOK_PREPROC_ID;
3823 def->text = nasm_strdup("%undef");
3824 space->type = TOK_WHITESPACE;
3825 space->text = nasm_strdup(" ");
3827 def->mac = space->mac = NULL;
3829 l = nasm_malloc(sizeof(Line));
3830 l->next = predef;
3831 l->first = def;
3832 l->finishes = FALSE;
3833 predef = l;
3836 void pp_extra_stdmac (char **macros)
3838 extrastdmac = macros;
3841 static void make_tok_num(Token *tok, long val)
3843 char numbuf[20];
3844 sprintf(numbuf, "%ld", val);
3845 tok->text = nasm_strdup(numbuf);
3846 tok->type = TOK_NUMBER;
3849 Preproc nasmpp = {
3850 pp_reset,
3851 pp_getline,
3852 pp_cleanup