nasm.c: use string helpers
[nasm/avx512.git] / preproc.c
blobdab0b2af293fc37509922f56e848736322511332
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
48 * }
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
63 #include "compiler.h"
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <stddef.h>
69 #include <string.h>
70 #include <ctype.h>
71 #include <limits.h>
72 #include <inttypes.h>
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "preproc.h"
77 #include "hashtbl.h"
78 #include "quote.h"
79 #include "stdscan.h"
80 #include "eval.h"
81 #include "tokens.h"
82 #include "tables.h"
84 typedef struct SMacro SMacro;
85 typedef struct MMacro MMacro;
86 typedef struct MMacroInvocation MMacroInvocation;
87 typedef struct Context Context;
88 typedef struct Token Token;
89 typedef struct Blocks Blocks;
90 typedef struct Line Line;
91 typedef struct Include Include;
92 typedef struct Cond Cond;
93 typedef struct IncPath IncPath;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
107 struct SMacro {
108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
133 struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
159 /* Store the definition of a multi-line macro, as defined in a
160 * previous recursive macro expansion.
162 struct MMacroInvocation {
163 MMacroInvocation *prev; /* previous invocation */
164 Token **params; /* actual parameters */
165 Token *iline; /* invocation line */
166 unsigned int nparam, rotate;
167 int *paramlen;
168 uint64_t unique;
173 * The context stack is composed of a linked list of these.
175 struct Context {
176 Context *next;
177 char *name;
178 struct hash_table localmac;
179 uint32_t number;
183 * This is the internal form which we break input lines up into.
184 * Typically stored in linked lists.
186 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
187 * necessarily used as-is, but is intended to denote the number of
188 * the substituted parameter. So in the definition
190 * %define a(x,y) ( (x) & ~(y) )
192 * the token representing `x' will have its type changed to
193 * TOK_SMAC_PARAM, but the one representing `y' will be
194 * TOK_SMAC_PARAM+1.
196 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
197 * which doesn't need quotes around it. Used in the pre-include
198 * mechanism as an alternative to trying to find a sensible type of
199 * quote to use on the filename we were passed.
201 enum pp_token_type {
202 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
203 TOK_PREPROC_ID, TOK_STRING,
204 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
205 TOK_INTERNAL_STRING,
206 TOK_PREPROC_Q, TOK_PREPROC_QQ,
207 TOK_PASTE, /* %+ */
208 TOK_INDIRECT, /* %[...] */
209 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
210 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
213 struct Token {
214 Token *next;
215 char *text;
216 union {
217 SMacro *mac; /* associated macro for TOK_SMAC_END */
218 size_t len; /* scratch length field */
219 } a; /* Auxiliary data */
220 enum pp_token_type type;
224 * Multi-line macro definitions are stored as a linked list of
225 * these, which is essentially a container to allow several linked
226 * lists of Tokens.
228 * Note that in this module, linked lists are treated as stacks
229 * wherever possible. For this reason, Lines are _pushed_ on to the
230 * `expansion' field in MMacro structures, so that the linked list,
231 * if walked, would give the macro lines in reverse order; this
232 * means that we can walk the list when expanding a macro, and thus
233 * push the lines on to the `expansion' field in _istk_ in reverse
234 * order (so that when popped back off they are in the right
235 * order). It may seem cockeyed, and it relies on my design having
236 * an even number of steps in, but it works...
238 * Some of these structures, rather than being actual lines, are
239 * markers delimiting the end of the expansion of a given macro.
240 * This is for use in the cycle-tracking and %rep-handling code.
241 * Such structures have `finishes' non-NULL, and `first' NULL. All
242 * others have `finishes' NULL, but `first' may still be NULL if
243 * the line is blank.
245 struct Line {
246 Line *next;
247 MMacro *finishes;
248 Token *first;
252 * To handle an arbitrary level of file inclusion, we maintain a
253 * stack (ie linked list) of these things.
255 struct Include {
256 Include *next;
257 FILE *fp;
258 Cond *conds;
259 Line *expansion;
260 char *fname;
261 int lineno, lineinc;
262 MMacro *mstk; /* stack of active macros/reps */
266 * Include search path. This is simply a list of strings which get
267 * prepended, in turn, to the name of an include file, in an
268 * attempt to find the file if it's not in the current directory.
270 struct IncPath {
271 IncPath *next;
272 char *path;
276 * Conditional assembly: we maintain a separate stack of these for
277 * each level of file inclusion. (The only reason we keep the
278 * stacks separate is to ensure that a stray `%endif' in a file
279 * included from within the true branch of a `%if' won't terminate
280 * it and cause confusion: instead, rightly, it'll cause an error.)
282 struct Cond {
283 Cond *next;
284 int state;
286 enum {
288 * These states are for use just after %if or %elif: IF_TRUE
289 * means the condition has evaluated to truth so we are
290 * currently emitting, whereas IF_FALSE means we are not
291 * currently emitting but will start doing so if a %else comes
292 * up. In these states, all directives are admissible: %elif,
293 * %else and %endif. (And of course %if.)
295 COND_IF_TRUE, COND_IF_FALSE,
297 * These states come up after a %else: ELSE_TRUE means we're
298 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
299 * any %elif or %else will cause an error.
301 COND_ELSE_TRUE, COND_ELSE_FALSE,
303 * These states mean that we're not emitting now, and also that
304 * nothing until %endif will be emitted at all. COND_DONE is
305 * used when we've had our moment of emission
306 * and have now started seeing %elifs. COND_NEVER is used when
307 * the condition construct in question is contained within a
308 * non-emitting branch of a larger condition construct,
309 * or if there is an error.
311 COND_DONE, COND_NEVER
313 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316 * These defines are used as the possible return values for do_directive
318 #define NO_DIRECTIVE_FOUND 0
319 #define DIRECTIVE_FOUND 1
322 * This define sets the upper limit for smacro and recursive mmacro
323 * expansions
325 #define DEADMAN_LIMIT (1 << 20)
328 * Condition codes. Note that we use c_ prefix not C_ because C_ is
329 * used in nasm.h for the "real" condition codes. At _this_ level,
330 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
331 * ones, so we need a different enum...
333 static const char * const conditions[] = {
334 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
335 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
336 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
338 enum pp_conds {
339 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
340 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
341 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
342 c_none = -1
344 static const enum pp_conds inverse_ccs[] = {
345 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
346 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,
347 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
351 * Directive names.
353 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
354 static int is_condition(enum preproc_token arg)
356 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
359 /* For TASM compatibility we need to be able to recognise TASM compatible
360 * conditional compilation directives. Using the NASM pre-processor does
361 * not work, so we look for them specifically from the following list and
362 * then jam in the equivalent NASM directive into the input stream.
365 enum {
366 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
367 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
370 static const char * const tasm_directives[] = {
371 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
372 "ifndef", "include", "local"
375 static int StackSize = 4;
376 static char *StackPointer = "ebp";
377 static int ArgOffset = 8;
378 static int LocalOffset = 0;
380 static Context *cstk;
381 static Include *istk;
382 static IncPath *ipath = NULL;
384 static int pass; /* HACK: pass 0 = generate dependencies only */
385 static StrList **dephead, **deptail; /* Dependency list */
387 static uint64_t unique; /* unique identifier numbers */
389 static Line *predef = NULL;
390 static bool do_predef;
392 static ListGen *list;
395 * The current set of multi-line macros we have defined.
397 static struct hash_table mmacros;
400 * The current set of single-line macros we have defined.
402 static struct hash_table smacros;
405 * The multi-line macro we are currently defining, or the %rep
406 * block we are currently reading, if any.
408 static MMacro *defining;
410 static uint64_t nested_mac_count;
411 static uint64_t nested_rep_count;
414 * The number of macro parameters to allocate space for at a time.
416 #define PARAM_DELTA 16
419 * The standard macro set: defined in macros.c in the array nasm_stdmac.
420 * This gives our position in the macro set, when we're processing it.
422 static macros_t *stdmacpos;
425 * The extra standard macros that come from the object format, if
426 * any.
428 static macros_t *extrastdmac = NULL;
429 static bool any_extrastdmac;
432 * Tokens are allocated in blocks to improve speed
434 #define TOKEN_BLOCKSIZE 4096
435 static Token *freeTokens = NULL;
436 struct Blocks {
437 Blocks *next;
438 void *chunk;
441 static Blocks blocks = { NULL, NULL };
444 * Forward declarations.
446 static Token *expand_mmac_params(Token * tline);
447 static Token *expand_smacro(Token * tline);
448 static Token *expand_id(Token * tline);
449 static Context *get_ctx(const char *name, const char **namep,
450 bool all_contexts);
451 static void make_tok_num(Token * tok, int64_t val);
452 static void error(int severity, const char *fmt, ...);
453 static void error_precond(int severity, const char *fmt, ...);
454 static void *new_Block(size_t size);
455 static void delete_Blocks(void);
456 static Token *new_Token(Token * next, enum pp_token_type type,
457 const char *text, int txtlen);
458 static Token *delete_Token(Token * t);
461 * Macros for safe checking of token pointers, avoid *(NULL)
463 #define tok_type_(x,t) ((x) && (x)->type == (t))
464 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
465 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
466 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
468 /* Handle TASM specific directives, which do not contain a % in
469 * front of them. We do it here because I could not find any other
470 * place to do it for the moment, and it is a hack (ideally it would
471 * be nice to be able to use the NASM pre-processor to do it).
473 static char *check_tasm_directive(char *line)
475 int32_t i, j, k, m, len;
476 char *p, *q, *oldline, oldchar;
478 p = nasm_skip_spaces(line);
480 /* Binary search for the directive name */
481 i = -1;
482 j = elements(tasm_directives);
483 q = nasm_skip_word(p);
484 len = q - p;
485 if (len) {
486 oldchar = p[len];
487 p[len] = 0;
488 while (j - i > 1) {
489 k = (j + i) / 2;
490 m = nasm_stricmp(p, tasm_directives[k]);
491 if (m == 0) {
492 /* We have found a directive, so jam a % in front of it
493 * so that NASM will then recognise it as one if it's own.
495 p[len] = oldchar;
496 len = strlen(p);
497 oldline = line;
498 line = nasm_malloc(len + 2);
499 line[0] = '%';
500 if (k == TM_IFDIFI) {
502 * NASM does not recognise IFDIFI, so we convert
503 * it to %if 0. This is not used in NASM
504 * compatible code, but does need to parse for the
505 * TASM macro package.
507 strcpy(line + 1, "if 0");
508 } else {
509 memcpy(line + 1, p, len + 1);
511 nasm_free(oldline);
512 return line;
513 } else if (m < 0) {
514 j = k;
515 } else
516 i = k;
518 p[len] = oldchar;
520 return line;
524 * The pre-preprocessing stage... This function translates line
525 * number indications as they emerge from GNU cpp (`# lineno "file"
526 * flags') into NASM preprocessor line number indications (`%line
527 * lineno file').
529 static char *prepreproc(char *line)
531 int lineno, fnlen;
532 char *fname, *oldline;
534 if (line[0] == '#' && line[1] == ' ') {
535 oldline = line;
536 fname = oldline + 2;
537 lineno = atoi(fname);
538 fname += strspn(fname, "0123456789 ");
539 if (*fname == '"')
540 fname++;
541 fnlen = strcspn(fname, "\"");
542 line = nasm_malloc(20 + fnlen);
543 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
544 nasm_free(oldline);
546 if (tasm_compatible_mode)
547 return check_tasm_directive(line);
548 return line;
552 * Free a linked list of tokens.
554 static void free_tlist(Token * list)
556 while (list) {
557 list = delete_Token(list);
562 * Free a linked list of lines.
564 static void free_llist(Line * list)
566 Line *l;
567 while (list) {
568 l = list;
569 list = list->next;
570 free_tlist(l->first);
571 nasm_free(l);
576 * Free an MMacro
578 static void free_mmacro(MMacro * m)
580 nasm_free(m->name);
581 free_tlist(m->dlist);
582 nasm_free(m->defaults);
583 free_llist(m->expansion);
584 nasm_free(m);
588 * Free all currently defined macros, and free the hash tables
590 static void free_smacro_table(struct hash_table *smt)
592 SMacro *s;
593 const char *key;
594 struct hash_tbl_node *it = NULL;
596 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
597 nasm_free((void *)key);
598 while (s) {
599 SMacro *ns = s->next;
600 nasm_free(s->name);
601 free_tlist(s->expansion);
602 nasm_free(s);
603 s = ns;
606 hash_free(smt);
609 static void free_mmacro_table(struct hash_table *mmt)
611 MMacro *m;
612 const char *key;
613 struct hash_tbl_node *it = NULL;
615 it = NULL;
616 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
617 nasm_free((void *)key);
618 while (m) {
619 MMacro *nm = m->next;
620 free_mmacro(m);
621 m = nm;
624 hash_free(mmt);
627 static void free_macros(void)
629 free_smacro_table(&smacros);
630 free_mmacro_table(&mmacros);
634 * Initialize the hash tables
636 static void init_macros(void)
638 hash_init(&smacros, HASH_LARGE);
639 hash_init(&mmacros, HASH_LARGE);
643 * Pop the context stack.
645 static void ctx_pop(void)
647 Context *c = cstk;
649 cstk = cstk->next;
650 free_smacro_table(&c->localmac);
651 nasm_free(c->name);
652 nasm_free(c);
656 * Search for a key in the hash index; adding it if necessary
657 * (in which case we initialize the data pointer to NULL.)
659 static void **
660 hash_findi_add(struct hash_table *hash, const char *str)
662 struct hash_insert hi;
663 void **r;
664 char *strx;
666 r = hash_findi(hash, str, &hi);
667 if (r)
668 return r;
670 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
671 return hash_add(&hi, strx, NULL);
675 * Like hash_findi, but returns the data element rather than a pointer
676 * to it. Used only when not adding a new element, hence no third
677 * argument.
679 static void *
680 hash_findix(struct hash_table *hash, const char *str)
682 void **p;
684 p = hash_findi(hash, str, NULL);
685 return p ? *p : NULL;
688 #define BUF_DELTA 512
690 * Read a line from the top file in istk, handling multiple CR/LFs
691 * at the end of the line read, and handling spurious ^Zs. Will
692 * return lines from the standard macro set if this has not already
693 * been done.
695 static char *read_line(void)
697 char *buffer, *p, *q;
698 int bufsize, continued_count;
700 if (stdmacpos) {
701 unsigned char c;
702 const unsigned char *p = stdmacpos;
703 char *ret, *q;
704 size_t len = 0;
705 while ((c = *p++)) {
706 if (c >= 0x80)
707 len += pp_directives_len[c-0x80]+1;
708 else
709 len++;
711 ret = nasm_malloc(len+1);
712 q = ret;
713 while ((c = *stdmacpos++)) {
714 if (c >= 0x80) {
715 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
716 q += pp_directives_len[c-0x80];
717 *q++ = ' ';
718 } else {
719 *q++ = c;
722 stdmacpos = p;
723 *q = '\0';
725 if (!*stdmacpos) {
726 /* This was the last of the standard macro chain... */
727 stdmacpos = NULL;
728 if (any_extrastdmac) {
729 stdmacpos = extrastdmac;
730 any_extrastdmac = false;
731 } else if (do_predef) {
732 Line *pd, *l;
733 Token *head, **tail, *t;
736 * Nasty hack: here we push the contents of
737 * `predef' on to the top-level expansion stack,
738 * since this is the most convenient way to
739 * implement the pre-include and pre-define
740 * features.
742 for (pd = predef; pd; pd = pd->next) {
743 head = NULL;
744 tail = &head;
745 for (t = pd->first; t; t = t->next) {
746 *tail = new_Token(NULL, t->type, t->text, 0);
747 tail = &(*tail)->next;
749 l = nasm_malloc(sizeof(Line));
750 l->next = istk->expansion;
751 l->first = head;
752 l->finishes = NULL;
753 istk->expansion = l;
755 do_predef = false;
758 return ret;
761 bufsize = BUF_DELTA;
762 buffer = nasm_malloc(BUF_DELTA);
763 p = buffer;
764 continued_count = 0;
765 while (1) {
766 q = fgets(p, bufsize - (p - buffer), istk->fp);
767 if (!q)
768 break;
769 p += strlen(p);
770 if (p > buffer && p[-1] == '\n') {
771 /* Convert backslash-CRLF line continuation sequences into
772 nothing at all (for DOS and Windows) */
773 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
774 p -= 3;
775 *p = 0;
776 continued_count++;
778 /* Also convert backslash-LF line continuation sequences into
779 nothing at all (for Unix) */
780 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
781 p -= 2;
782 *p = 0;
783 continued_count++;
784 } else {
785 break;
788 if (p - buffer > bufsize - 10) {
789 int32_t offset = p - buffer;
790 bufsize += BUF_DELTA;
791 buffer = nasm_realloc(buffer, bufsize);
792 p = buffer + offset; /* prevent stale-pointer problems */
796 if (!q && p == buffer) {
797 nasm_free(buffer);
798 return NULL;
801 src_set_linnum(src_get_linnum() + istk->lineinc +
802 (continued_count * istk->lineinc));
805 * Play safe: remove CRs as well as LFs, if any of either are
806 * present at the end of the line.
808 while (--p >= buffer && (*p == '\n' || *p == '\r'))
809 *p = '\0';
812 * Handle spurious ^Z, which may be inserted into source files
813 * by some file transfer utilities.
815 buffer[strcspn(buffer, "\032")] = '\0';
817 list->line(LIST_READ, buffer);
819 return buffer;
823 * Tokenize a line of text. This is a very simple process since we
824 * don't need to parse the value out of e.g. numeric tokens: we
825 * simply split one string into many.
827 static Token *tokenize(char *line)
829 char c, *p = line;
830 enum pp_token_type type;
831 Token *list = NULL;
832 Token *t, **tail = &list;
834 while (*line) {
835 p = line;
836 if (*p == '%') {
837 p++;
838 if (*p == '+' && !nasm_isdigit(p[1])) {
839 p++;
840 type = TOK_PASTE;
841 } else if (nasm_isdigit(*p) ||
842 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
843 do {
844 p++;
846 while (nasm_isdigit(*p));
847 type = TOK_PREPROC_ID;
848 } else if (*p == '{') {
849 p++;
850 while (*p && *p != '}') {
851 p[-1] = *p;
852 p++;
854 p[-1] = '\0';
855 if (*p)
856 p++;
857 type = TOK_PREPROC_ID;
858 } else if (*p == '[') {
859 int lvl = 1;
860 line += 2; /* Skip the leading %[ */
861 p++;
862 while (lvl && (c = *p++)) {
863 switch (c) {
864 case ']':
865 lvl--;
866 break;
867 case '%':
868 if (*p == '[')
869 lvl++;
870 break;
871 case '\'':
872 case '\"':
873 case '`':
874 p = nasm_skip_string(p)+1;
875 break;
876 default:
877 break;
880 p--;
881 if (*p)
882 *p++ = '\0';
883 if (lvl)
884 error(ERR_NONFATAL, "unterminated %[ construct");
885 type = TOK_INDIRECT;
886 } else if (*p == '?') {
887 type = TOK_PREPROC_Q; /* %? */
888 p++;
889 if (*p == '?') {
890 type = TOK_PREPROC_QQ; /* %?? */
891 p++;
893 } else if (isidchar(*p) ||
894 ((*p == '!' || *p == '%' || *p == '$') &&
895 isidchar(p[1]))) {
896 do {
897 p++;
899 while (isidchar(*p));
900 type = TOK_PREPROC_ID;
901 } else {
902 type = TOK_OTHER;
903 if (*p == '%')
904 p++;
906 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
907 type = TOK_ID;
908 p++;
909 while (*p && isidchar(*p))
910 p++;
911 } else if (*p == '\'' || *p == '"' || *p == '`') {
913 * A string token.
915 type = TOK_STRING;
916 p = nasm_skip_string(p);
918 if (*p) {
919 p++;
920 } else {
921 error(ERR_WARNING|ERR_PASS1, "unterminated string");
922 /* Handling unterminated strings by UNV */
923 /* type = -1; */
925 } else if (p[0] == '$' && p[1] == '$') {
926 type = TOK_OTHER; /* TOKEN_BASE */
927 p += 2;
928 } else if (isnumstart(*p)) {
929 bool is_hex = false;
930 bool is_float = false;
931 bool has_e = false;
932 char c, *r;
935 * A numeric token.
938 if (*p == '$') {
939 p++;
940 is_hex = true;
943 for (;;) {
944 c = *p++;
946 if (!is_hex && (c == 'e' || c == 'E')) {
947 has_e = true;
948 if (*p == '+' || *p == '-') {
949 /* e can only be followed by +/- if it is either a
950 prefixed hex number or a floating-point number */
951 p++;
952 is_float = true;
954 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
955 is_hex = true;
956 } else if (c == 'P' || c == 'p') {
957 is_float = true;
958 if (*p == '+' || *p == '-')
959 p++;
960 } else if (isnumchar(c) || c == '_')
961 ; /* just advance */
962 else if (c == '.') {
963 /* we need to deal with consequences of the legacy
964 parser, like "1.nolist" being two tokens
965 (TOK_NUMBER, TOK_ID) here; at least give it
966 a shot for now. In the future, we probably need
967 a flex-based scanner with proper pattern matching
968 to do it as well as it can be done. Nothing in
969 the world is going to help the person who wants
970 0x123.p16 interpreted as two tokens, though. */
971 r = p;
972 while (*r == '_')
973 r++;
975 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
976 (!is_hex && (*r == 'e' || *r == 'E')) ||
977 (*r == 'p' || *r == 'P')) {
978 p = r;
979 is_float = true;
980 } else
981 break; /* Terminate the token */
982 } else
983 break;
985 p--; /* Point to first character beyond number */
987 if (p == line+1 && *line == '$') {
988 type = TOK_OTHER; /* TOKEN_HERE */
989 } else {
990 if (has_e && !is_hex) {
991 /* 1e13 is floating-point, but 1e13h is not */
992 is_float = true;
995 type = is_float ? TOK_FLOAT : TOK_NUMBER;
997 } else if (nasm_isspace(*p)) {
998 type = TOK_WHITESPACE;
999 p = nasm_skip_spaces(p);
1001 * Whitespace just before end-of-line is discarded by
1002 * pretending it's a comment; whitespace just before a
1003 * comment gets lumped into the comment.
1005 if (!*p || *p == ';') {
1006 type = TOK_COMMENT;
1007 while (*p)
1008 p++;
1010 } else if (*p == ';') {
1011 type = TOK_COMMENT;
1012 while (*p)
1013 p++;
1014 } else {
1016 * Anything else is an operator of some kind. We check
1017 * for all the double-character operators (>>, <<, //,
1018 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1019 * else is a single-character operator.
1021 type = TOK_OTHER;
1022 if ((p[0] == '>' && p[1] == '>') ||
1023 (p[0] == '<' && p[1] == '<') ||
1024 (p[0] == '/' && p[1] == '/') ||
1025 (p[0] == '<' && p[1] == '=') ||
1026 (p[0] == '>' && p[1] == '=') ||
1027 (p[0] == '=' && p[1] == '=') ||
1028 (p[0] == '!' && p[1] == '=') ||
1029 (p[0] == '<' && p[1] == '>') ||
1030 (p[0] == '&' && p[1] == '&') ||
1031 (p[0] == '|' && p[1] == '|') ||
1032 (p[0] == '^' && p[1] == '^')) {
1033 p++;
1035 p++;
1038 /* Handling unterminated string by UNV */
1039 /*if (type == -1)
1041 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1042 t->text[p-line] = *line;
1043 tail = &t->next;
1045 else */
1046 if (type != TOK_COMMENT) {
1047 *tail = t = new_Token(NULL, type, line, p - line);
1048 tail = &t->next;
1050 line = p;
1052 return list;
1056 * this function allocates a new managed block of memory and
1057 * returns a pointer to the block. The managed blocks are
1058 * deleted only all at once by the delete_Blocks function.
1060 static void *new_Block(size_t size)
1062 Blocks *b = &blocks;
1064 /* first, get to the end of the linked list */
1065 while (b->next)
1066 b = b->next;
1067 /* now allocate the requested chunk */
1068 b->chunk = nasm_malloc(size);
1070 /* now allocate a new block for the next request */
1071 b->next = nasm_malloc(sizeof(Blocks));
1072 /* and initialize the contents of the new block */
1073 b->next->next = NULL;
1074 b->next->chunk = NULL;
1075 return b->chunk;
1079 * this function deletes all managed blocks of memory
1081 static void delete_Blocks(void)
1083 Blocks *a, *b = &blocks;
1086 * keep in mind that the first block, pointed to by blocks
1087 * is a static and not dynamically allocated, so we don't
1088 * free it.
1090 while (b) {
1091 if (b->chunk)
1092 nasm_free(b->chunk);
1093 a = b;
1094 b = b->next;
1095 if (a != &blocks)
1096 nasm_free(a);
1101 * this function creates a new Token and passes a pointer to it
1102 * back to the caller. It sets the type and text elements, and
1103 * also the a.mac and next elements to NULL.
1105 static Token *new_Token(Token * next, enum pp_token_type type,
1106 const char *text, int txtlen)
1108 Token *t;
1109 int i;
1111 if (!freeTokens) {
1112 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1113 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1114 freeTokens[i].next = &freeTokens[i + 1];
1115 freeTokens[i].next = NULL;
1117 t = freeTokens;
1118 freeTokens = t->next;
1119 t->next = next;
1120 t->a.mac = NULL;
1121 t->type = type;
1122 if (type == TOK_WHITESPACE || !text) {
1123 t->text = NULL;
1124 } else {
1125 if (txtlen == 0)
1126 txtlen = strlen(text);
1127 t->text = nasm_malloc(txtlen+1);
1128 memcpy(t->text, text, txtlen);
1129 t->text[txtlen] = '\0';
1131 return t;
1134 static Token *delete_Token(Token * t)
1136 Token *next = t->next;
1137 nasm_free(t->text);
1138 t->next = freeTokens;
1139 freeTokens = t;
1140 return next;
1144 * Convert a line of tokens back into text.
1145 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1146 * will be transformed into ..@ctxnum.xxx
1148 static char *detoken(Token * tlist, bool expand_locals)
1150 Token *t;
1151 int len;
1152 char *line, *p;
1153 const char *q;
1155 len = 0;
1156 for (t = tlist; t; t = t->next) {
1157 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1158 char *p = getenv(t->text + 2);
1159 nasm_free(t->text);
1160 if (p)
1161 t->text = nasm_strdup(p);
1162 else
1163 t->text = NULL;
1165 /* Expand local macros here and not during preprocessing */
1166 if (expand_locals &&
1167 t->type == TOK_PREPROC_ID && t->text &&
1168 t->text[0] == '%' && t->text[1] == '$') {
1169 const char *q;
1170 char *p;
1171 Context *ctx = get_ctx(t->text, &q, false);
1172 if (ctx) {
1173 char buffer[40];
1174 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1175 p = nasm_strcat(buffer, q);
1176 nasm_free(t->text);
1177 t->text = p;
1180 if (t->type == TOK_WHITESPACE) {
1181 len++;
1182 } else if (t->text) {
1183 len += strlen(t->text);
1186 p = line = nasm_malloc(len + 1);
1187 for (t = tlist; t; t = t->next) {
1188 if (t->type == TOK_WHITESPACE) {
1189 *p++ = ' ';
1190 } else if (t->text) {
1191 q = t->text;
1192 while (*q)
1193 *p++ = *q++;
1196 *p = '\0';
1197 return line;
1201 * A scanner, suitable for use by the expression evaluator, which
1202 * operates on a line of Tokens. Expects a pointer to a pointer to
1203 * the first token in the line to be passed in as its private_data
1204 * field.
1206 * FIX: This really needs to be unified with stdscan.
1208 static int ppscan(void *private_data, struct tokenval *tokval)
1210 Token **tlineptr = private_data;
1211 Token *tline;
1212 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1214 do {
1215 tline = *tlineptr;
1216 *tlineptr = tline ? tline->next : NULL;
1218 while (tline && (tline->type == TOK_WHITESPACE ||
1219 tline->type == TOK_COMMENT));
1221 if (!tline)
1222 return tokval->t_type = TOKEN_EOS;
1224 tokval->t_charptr = tline->text;
1226 if (tline->text[0] == '$' && !tline->text[1])
1227 return tokval->t_type = TOKEN_HERE;
1228 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1229 return tokval->t_type = TOKEN_BASE;
1231 if (tline->type == TOK_ID) {
1232 p = tokval->t_charptr = tline->text;
1233 if (p[0] == '$') {
1234 tokval->t_charptr++;
1235 return tokval->t_type = TOKEN_ID;
1238 for (r = p, s = ourcopy; *r; r++) {
1239 if (r >= p+MAX_KEYWORD)
1240 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1241 *s++ = nasm_tolower(*r);
1243 *s = '\0';
1244 /* right, so we have an identifier sitting in temp storage. now,
1245 * is it actually a register or instruction name, or what? */
1246 return nasm_token_hash(ourcopy, tokval);
1249 if (tline->type == TOK_NUMBER) {
1250 bool rn_error;
1251 tokval->t_integer = readnum(tline->text, &rn_error);
1252 tokval->t_charptr = tline->text;
1253 if (rn_error)
1254 return tokval->t_type = TOKEN_ERRNUM;
1255 else
1256 return tokval->t_type = TOKEN_NUM;
1259 if (tline->type == TOK_FLOAT) {
1260 return tokval->t_type = TOKEN_FLOAT;
1263 if (tline->type == TOK_STRING) {
1264 char bq, *ep;
1266 bq = tline->text[0];
1267 tokval->t_charptr = tline->text;
1268 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1270 if (ep[0] != bq || ep[1] != '\0')
1271 return tokval->t_type = TOKEN_ERRSTR;
1272 else
1273 return tokval->t_type = TOKEN_STR;
1276 if (tline->type == TOK_OTHER) {
1277 if (!strcmp(tline->text, "<<"))
1278 return tokval->t_type = TOKEN_SHL;
1279 if (!strcmp(tline->text, ">>"))
1280 return tokval->t_type = TOKEN_SHR;
1281 if (!strcmp(tline->text, "//"))
1282 return tokval->t_type = TOKEN_SDIV;
1283 if (!strcmp(tline->text, "%%"))
1284 return tokval->t_type = TOKEN_SMOD;
1285 if (!strcmp(tline->text, "=="))
1286 return tokval->t_type = TOKEN_EQ;
1287 if (!strcmp(tline->text, "<>"))
1288 return tokval->t_type = TOKEN_NE;
1289 if (!strcmp(tline->text, "!="))
1290 return tokval->t_type = TOKEN_NE;
1291 if (!strcmp(tline->text, "<="))
1292 return tokval->t_type = TOKEN_LE;
1293 if (!strcmp(tline->text, ">="))
1294 return tokval->t_type = TOKEN_GE;
1295 if (!strcmp(tline->text, "&&"))
1296 return tokval->t_type = TOKEN_DBL_AND;
1297 if (!strcmp(tline->text, "^^"))
1298 return tokval->t_type = TOKEN_DBL_XOR;
1299 if (!strcmp(tline->text, "||"))
1300 return tokval->t_type = TOKEN_DBL_OR;
1304 * We have no other options: just return the first character of
1305 * the token text.
1307 return tokval->t_type = tline->text[0];
1311 * Compare a string to the name of an existing macro; this is a
1312 * simple wrapper which calls either strcmp or nasm_stricmp
1313 * depending on the value of the `casesense' parameter.
1315 static int mstrcmp(const char *p, const char *q, bool casesense)
1317 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1321 * Compare a string to the name of an existing macro; this is a
1322 * simple wrapper which calls either strcmp or nasm_stricmp
1323 * depending on the value of the `casesense' parameter.
1325 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1327 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1331 * Return the Context structure associated with a %$ token. Return
1332 * NULL, having _already_ reported an error condition, if the
1333 * context stack isn't deep enough for the supplied number of $
1334 * signs.
1335 * If all_contexts == true, contexts that enclose current are
1336 * also scanned for such smacro, until it is found; if not -
1337 * only the context that directly results from the number of $'s
1338 * in variable's name.
1340 * If "namep" is non-NULL, set it to the pointer to the macro name
1341 * tail, i.e. the part beyond %$...
1343 static Context *get_ctx(const char *name, const char **namep,
1344 bool all_contexts)
1346 Context *ctx;
1347 SMacro *m;
1348 int i;
1350 if (namep)
1351 *namep = name;
1353 if (!name || name[0] != '%' || name[1] != '$')
1354 return NULL;
1356 if (!cstk) {
1357 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1358 return NULL;
1361 name += 2;
1362 ctx = cstk;
1363 i = 0;
1364 while (ctx && *name == '$') {
1365 name++;
1366 i++;
1367 ctx = ctx->next;
1369 if (!ctx) {
1370 error(ERR_NONFATAL, "`%s': context stack is only"
1371 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1372 return NULL;
1375 if (namep)
1376 *namep = name;
1378 if (!all_contexts)
1379 return ctx;
1381 do {
1382 /* Search for this smacro in found context */
1383 m = hash_findix(&ctx->localmac, name);
1384 while (m) {
1385 if (!mstrcmp(m->name, name, m->casesense))
1386 return ctx;
1387 m = m->next;
1389 ctx = ctx->next;
1391 while (ctx);
1392 return NULL;
1396 * Check to see if a file is already in a string list
1398 static bool in_list(const StrList *list, const char *str)
1400 while (list) {
1401 if (!strcmp(list->str, str))
1402 return true;
1403 list = list->next;
1405 return false;
1409 * Open an include file. This routine must always return a valid
1410 * file pointer if it returns - it's responsible for throwing an
1411 * ERR_FATAL and bombing out completely if not. It should also try
1412 * the include path one by one until it finds the file or reaches
1413 * the end of the path.
1415 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1416 bool missing_ok)
1418 FILE *fp;
1419 char *prefix = "";
1420 IncPath *ip = ipath;
1421 int len = strlen(file);
1422 size_t prefix_len = 0;
1423 StrList *sl;
1425 while (1) {
1426 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1427 memcpy(sl->str, prefix, prefix_len);
1428 memcpy(sl->str+prefix_len, file, len+1);
1429 fp = fopen(sl->str, "r");
1430 if (fp && dhead && !in_list(*dhead, sl->str)) {
1431 sl->next = NULL;
1432 **dtail = sl;
1433 *dtail = &sl->next;
1434 } else {
1435 nasm_free(sl);
1437 if (fp)
1438 return fp;
1439 if (!ip) {
1440 if (!missing_ok)
1441 break;
1442 prefix = NULL;
1443 } else {
1444 prefix = ip->path;
1445 ip = ip->next;
1447 if (prefix) {
1448 prefix_len = strlen(prefix);
1449 } else {
1450 /* -MG given and file not found */
1451 if (dhead && !in_list(*dhead, file)) {
1452 sl = nasm_malloc(len+1+sizeof sl->next);
1453 sl->next = NULL;
1454 strcpy(sl->str, file);
1455 **dtail = sl;
1456 *dtail = &sl->next;
1458 return NULL;
1462 error(ERR_FATAL, "unable to open include file `%s'", file);
1463 return NULL; /* never reached - placate compilers */
1467 * Determine if we should warn on defining a single-line macro of
1468 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1469 * return true if _any_ single-line macro of that name is defined.
1470 * Otherwise, will return true if a single-line macro with either
1471 * `nparam' or no parameters is defined.
1473 * If a macro with precisely the right number of parameters is
1474 * defined, or nparam is -1, the address of the definition structure
1475 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1476 * is NULL, no action will be taken regarding its contents, and no
1477 * error will occur.
1479 * Note that this is also called with nparam zero to resolve
1480 * `ifdef'.
1482 * If you already know which context macro belongs to, you can pass
1483 * the context pointer as first parameter; if you won't but name begins
1484 * with %$ the context will be automatically computed. If all_contexts
1485 * is true, macro will be searched in outer contexts as well.
1487 static bool
1488 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1489 bool nocase)
1491 struct hash_table *smtbl;
1492 SMacro *m;
1494 if (ctx) {
1495 smtbl = &ctx->localmac;
1496 } else if (name[0] == '%' && name[1] == '$') {
1497 if (cstk)
1498 ctx = get_ctx(name, &name, false);
1499 if (!ctx)
1500 return false; /* got to return _something_ */
1501 smtbl = &ctx->localmac;
1502 } else {
1503 smtbl = &smacros;
1505 m = (SMacro *) hash_findix(smtbl, name);
1507 while (m) {
1508 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1509 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1510 if (defn) {
1511 if (nparam == (int) m->nparam || nparam == -1)
1512 *defn = m;
1513 else
1514 *defn = NULL;
1516 return true;
1518 m = m->next;
1521 return false;
1525 * Count and mark off the parameters in a multi-line macro call.
1526 * This is called both from within the multi-line macro expansion
1527 * code, and also to mark off the default parameters when provided
1528 * in a %macro definition line.
1530 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1532 int paramsize, brace;
1534 *nparam = paramsize = 0;
1535 *params = NULL;
1536 while (t) {
1537 /* +1: we need space for the final NULL */
1538 if (*nparam+1 >= paramsize) {
1539 paramsize += PARAM_DELTA;
1540 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1542 skip_white_(t);
1543 brace = false;
1544 if (tok_is_(t, "{"))
1545 brace = true;
1546 (*params)[(*nparam)++] = t;
1547 while (tok_isnt_(t, brace ? "}" : ","))
1548 t = t->next;
1549 if (t) { /* got a comma/brace */
1550 t = t->next;
1551 if (brace) {
1553 * Now we've found the closing brace, look further
1554 * for the comma.
1556 skip_white_(t);
1557 if (tok_isnt_(t, ",")) {
1558 error(ERR_NONFATAL,
1559 "braces do not enclose all of macro parameter");
1560 while (tok_isnt_(t, ","))
1561 t = t->next;
1563 if (t)
1564 t = t->next; /* eat the comma */
1571 * Determine whether one of the various `if' conditions is true or
1572 * not.
1574 * We must free the tline we get passed.
1576 static bool if_condition(Token * tline, enum preproc_token ct)
1578 enum pp_conditional i = PP_COND(ct);
1579 bool j;
1580 Token *t, *tt, **tptr, *origline;
1581 struct tokenval tokval;
1582 expr *evalresult;
1583 enum pp_token_type needtype;
1585 origline = tline;
1587 switch (i) {
1588 case PPC_IFCTX:
1589 j = false; /* have we matched yet? */
1590 while (true) {
1591 skip_white_(tline);
1592 if (!tline)
1593 break;
1594 if (tline->type != TOK_ID) {
1595 error(ERR_NONFATAL,
1596 "`%s' expects context identifiers", pp_directives[ct]);
1597 free_tlist(origline);
1598 return -1;
1600 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1601 j = true;
1602 tline = tline->next;
1604 break;
1606 case PPC_IFDEF:
1607 j = false; /* have we matched yet? */
1608 while (tline) {
1609 skip_white_(tline);
1610 if (!tline || (tline->type != TOK_ID &&
1611 (tline->type != TOK_PREPROC_ID ||
1612 tline->text[1] != '$'))) {
1613 error(ERR_NONFATAL,
1614 "`%s' expects macro identifiers", pp_directives[ct]);
1615 goto fail;
1617 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1618 j = true;
1619 tline = tline->next;
1621 break;
1623 case PPC_IFIDN:
1624 case PPC_IFIDNI:
1625 tline = expand_smacro(tline);
1626 t = tt = tline;
1627 while (tok_isnt_(tt, ","))
1628 tt = tt->next;
1629 if (!tt) {
1630 error(ERR_NONFATAL,
1631 "`%s' expects two comma-separated arguments",
1632 pp_directives[ct]);
1633 goto fail;
1635 tt = tt->next;
1636 j = true; /* assume equality unless proved not */
1637 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1638 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1639 error(ERR_NONFATAL, "`%s': more than one comma on line",
1640 pp_directives[ct]);
1641 goto fail;
1643 if (t->type == TOK_WHITESPACE) {
1644 t = t->next;
1645 continue;
1647 if (tt->type == TOK_WHITESPACE) {
1648 tt = tt->next;
1649 continue;
1651 if (tt->type != t->type) {
1652 j = false; /* found mismatching tokens */
1653 break;
1655 /* When comparing strings, need to unquote them first */
1656 if (t->type == TOK_STRING) {
1657 size_t l1 = nasm_unquote(t->text, NULL);
1658 size_t l2 = nasm_unquote(tt->text, NULL);
1660 if (l1 != l2) {
1661 j = false;
1662 break;
1664 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1665 j = false;
1666 break;
1668 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1669 j = false; /* found mismatching tokens */
1670 break;
1673 t = t->next;
1674 tt = tt->next;
1676 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1677 j = false; /* trailing gunk on one end or other */
1678 break;
1680 case PPC_IFMACRO:
1682 bool found = false;
1683 MMacro searching, *mmac;
1685 skip_white_(tline);
1686 tline = expand_id(tline);
1687 if (!tok_type_(tline, TOK_ID)) {
1688 error(ERR_NONFATAL,
1689 "`%s' expects a macro name", pp_directives[ct]);
1690 goto fail;
1692 searching.name = nasm_strdup(tline->text);
1693 searching.casesense = true;
1694 searching.plus = false;
1695 searching.nolist = false;
1696 searching.in_progress = 0;
1697 searching.max_depth = 0;
1698 searching.rep_nest = NULL;
1699 searching.nparam_min = 0;
1700 searching.nparam_max = INT_MAX;
1701 tline = expand_smacro(tline->next);
1702 skip_white_(tline);
1703 if (!tline) {
1704 } else if (!tok_type_(tline, TOK_NUMBER)) {
1705 error(ERR_NONFATAL,
1706 "`%s' expects a parameter count or nothing",
1707 pp_directives[ct]);
1708 } else {
1709 searching.nparam_min = searching.nparam_max =
1710 readnum(tline->text, &j);
1711 if (j)
1712 error(ERR_NONFATAL,
1713 "unable to parse parameter count `%s'",
1714 tline->text);
1716 if (tline && tok_is_(tline->next, "-")) {
1717 tline = tline->next->next;
1718 if (tok_is_(tline, "*"))
1719 searching.nparam_max = INT_MAX;
1720 else if (!tok_type_(tline, TOK_NUMBER))
1721 error(ERR_NONFATAL,
1722 "`%s' expects a parameter count after `-'",
1723 pp_directives[ct]);
1724 else {
1725 searching.nparam_max = readnum(tline->text, &j);
1726 if (j)
1727 error(ERR_NONFATAL,
1728 "unable to parse parameter count `%s'",
1729 tline->text);
1730 if (searching.nparam_min > searching.nparam_max)
1731 error(ERR_NONFATAL,
1732 "minimum parameter count exceeds maximum");
1735 if (tline && tok_is_(tline->next, "+")) {
1736 tline = tline->next;
1737 searching.plus = true;
1739 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1740 while (mmac) {
1741 if (!strcmp(mmac->name, searching.name) &&
1742 (mmac->nparam_min <= searching.nparam_max
1743 || searching.plus)
1744 && (searching.nparam_min <= mmac->nparam_max
1745 || mmac->plus)) {
1746 found = true;
1747 break;
1749 mmac = mmac->next;
1751 if (tline && tline->next)
1752 error(ERR_WARNING|ERR_PASS1,
1753 "trailing garbage after %%ifmacro ignored");
1754 nasm_free(searching.name);
1755 j = found;
1756 break;
1759 case PPC_IFID:
1760 needtype = TOK_ID;
1761 goto iftype;
1762 case PPC_IFNUM:
1763 needtype = TOK_NUMBER;
1764 goto iftype;
1765 case PPC_IFSTR:
1766 needtype = TOK_STRING;
1767 goto iftype;
1769 iftype:
1770 t = tline = expand_smacro(tline);
1772 while (tok_type_(t, TOK_WHITESPACE) ||
1773 (needtype == TOK_NUMBER &&
1774 tok_type_(t, TOK_OTHER) &&
1775 (t->text[0] == '-' || t->text[0] == '+') &&
1776 !t->text[1]))
1777 t = t->next;
1779 j = tok_type_(t, needtype);
1780 break;
1782 case PPC_IFTOKEN:
1783 t = tline = expand_smacro(tline);
1784 while (tok_type_(t, TOK_WHITESPACE))
1785 t = t->next;
1787 j = false;
1788 if (t) {
1789 t = t->next; /* Skip the actual token */
1790 while (tok_type_(t, TOK_WHITESPACE))
1791 t = t->next;
1792 j = !t; /* Should be nothing left */
1794 break;
1796 case PPC_IFEMPTY:
1797 t = tline = expand_smacro(tline);
1798 while (tok_type_(t, TOK_WHITESPACE))
1799 t = t->next;
1801 j = !t; /* Should be empty */
1802 break;
1804 case PPC_IF:
1805 t = tline = expand_smacro(tline);
1806 tptr = &t;
1807 tokval.t_type = TOKEN_INVALID;
1808 evalresult = evaluate(ppscan, tptr, &tokval,
1809 NULL, pass | CRITICAL, error, NULL);
1810 if (!evalresult)
1811 return -1;
1812 if (tokval.t_type)
1813 error(ERR_WARNING|ERR_PASS1,
1814 "trailing garbage after expression ignored");
1815 if (!is_simple(evalresult)) {
1816 error(ERR_NONFATAL,
1817 "non-constant value given to `%s'", pp_directives[ct]);
1818 goto fail;
1820 j = reloc_value(evalresult) != 0;
1821 break;
1823 default:
1824 error(ERR_FATAL,
1825 "preprocessor directive `%s' not yet implemented",
1826 pp_directives[ct]);
1827 goto fail;
1830 free_tlist(origline);
1831 return j ^ PP_NEGATIVE(ct);
1833 fail:
1834 free_tlist(origline);
1835 return -1;
1839 * Common code for defining an smacro
1841 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
1842 int nparam, Token *expansion)
1844 SMacro *smac, **smhead;
1845 struct hash_table *smtbl;
1847 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1848 if (!smac) {
1849 error(ERR_WARNING|ERR_PASS1,
1850 "single-line macro `%s' defined both with and"
1851 " without parameters", mname);
1853 /* Some instances of the old code considered this a failure,
1854 some others didn't. What is the right thing to do here? */
1855 free_tlist(expansion);
1856 return false; /* Failure */
1857 } else {
1859 * We're redefining, so we have to take over an
1860 * existing SMacro structure. This means freeing
1861 * what was already in it.
1863 nasm_free(smac->name);
1864 free_tlist(smac->expansion);
1866 } else {
1867 smtbl = ctx ? &ctx->localmac : &smacros;
1868 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1869 smac = nasm_malloc(sizeof(SMacro));
1870 smac->next = *smhead;
1871 *smhead = smac;
1873 smac->name = nasm_strdup(mname);
1874 smac->casesense = casesense;
1875 smac->nparam = nparam;
1876 smac->expansion = expansion;
1877 smac->in_progress = false;
1878 return true; /* Success */
1882 * Undefine an smacro
1884 static void undef_smacro(Context *ctx, const char *mname)
1886 SMacro **smhead, *s, **sp;
1887 struct hash_table *smtbl;
1889 smtbl = ctx ? &ctx->localmac : &smacros;
1890 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
1892 if (smhead) {
1894 * We now have a macro name... go hunt for it.
1896 sp = smhead;
1897 while ((s = *sp) != NULL) {
1898 if (!mstrcmp(s->name, mname, s->casesense)) {
1899 *sp = s->next;
1900 nasm_free(s->name);
1901 free_tlist(s->expansion);
1902 nasm_free(s);
1903 } else {
1904 sp = &s->next;
1911 * Parse a mmacro specification.
1913 static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1915 bool err;
1917 tline = tline->next;
1918 skip_white_(tline);
1919 tline = expand_id(tline);
1920 if (!tok_type_(tline, TOK_ID)) {
1921 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1922 return false;
1925 def->prev = NULL;
1926 def->name = nasm_strdup(tline->text);
1927 def->plus = false;
1928 def->nolist = false;
1929 def->in_progress = 0;
1930 def->rep_nest = NULL;
1931 def->nparam_min = 0;
1932 def->nparam_max = 0;
1934 tline = expand_smacro(tline->next);
1935 skip_white_(tline);
1936 if (!tok_type_(tline, TOK_NUMBER)) {
1937 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1938 } else {
1939 def->nparam_min = def->nparam_max =
1940 readnum(tline->text, &err);
1941 if (err)
1942 error(ERR_NONFATAL,
1943 "unable to parse parameter count `%s'", tline->text);
1945 if (tline && tok_is_(tline->next, "-")) {
1946 tline = tline->next->next;
1947 if (tok_is_(tline, "*")) {
1948 def->nparam_max = INT_MAX;
1949 } else if (!tok_type_(tline, TOK_NUMBER)) {
1950 error(ERR_NONFATAL,
1951 "`%s' expects a parameter count after `-'", directive);
1952 } else {
1953 def->nparam_max = readnum(tline->text, &err);
1954 if (err) {
1955 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1956 tline->text);
1958 if (def->nparam_min > def->nparam_max) {
1959 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1963 if (tline && tok_is_(tline->next, "+")) {
1964 tline = tline->next;
1965 def->plus = true;
1967 if (tline && tok_type_(tline->next, TOK_ID) &&
1968 !nasm_stricmp(tline->next->text, ".nolist")) {
1969 tline = tline->next;
1970 def->nolist = true;
1974 * Handle default parameters.
1976 if (tline && tline->next) {
1977 def->dlist = tline->next;
1978 tline->next = NULL;
1979 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1980 } else {
1981 def->dlist = NULL;
1982 def->defaults = NULL;
1984 def->expansion = NULL;
1986 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
1987 !def->plus)
1988 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1989 "too many default macro parameters");
1991 return true;
1996 * Decode a size directive
1998 static int parse_size(const char *str) {
1999 static const char *size_names[] =
2000 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2001 static const int sizes[] =
2002 { 0, 1, 4, 16, 8, 10, 2, 32 };
2004 return sizes[bsii(str, size_names, elements(size_names))+1];
2008 * nasm_unquote with error if the string contains NUL characters.
2009 * If the string contains NUL characters, issue an error and return
2010 * the C len, i.e. truncate at the NUL.
2012 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2014 size_t len = nasm_unquote(qstr, NULL);
2015 size_t clen = strlen(qstr);
2017 if (len != clen)
2018 error(ERR_NONFATAL, "NUL character in `%s' directive",
2019 pp_directives[directive]);
2021 return clen;
2025 * find and process preprocessor directive in passed line
2026 * Find out if a line contains a preprocessor directive, and deal
2027 * with it if so.
2029 * If a directive _is_ found, it is the responsibility of this routine
2030 * (and not the caller) to free_tlist() the line.
2032 * @param tline a pointer to the current tokeninzed line linked list
2033 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2036 static int do_directive(Token * tline)
2038 enum preproc_token i;
2039 int j;
2040 bool err;
2041 int nparam;
2042 bool nolist;
2043 bool casesense;
2044 int k, m;
2045 int offset;
2046 char *p, *pp;
2047 const char *mname;
2048 Include *inc;
2049 Context *ctx;
2050 Cond *cond;
2051 MMacro *mmac, **mmhead;
2052 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2053 Line *l;
2054 struct tokenval tokval;
2055 expr *evalresult;
2056 MMacro *tmp_defining; /* Used when manipulating rep_nest */
2057 int64_t count;
2058 size_t len;
2059 int severity;
2061 origline = tline;
2063 skip_white_(tline);
2064 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2065 (tline->text[1] == '%' || tline->text[1] == '$'
2066 || tline->text[1] == '!'))
2067 return NO_DIRECTIVE_FOUND;
2069 i = pp_token_hash(tline->text);
2072 * If we're in a non-emitting branch of a condition construct,
2073 * or walking to the end of an already terminated %rep block,
2074 * we should ignore all directives except for condition
2075 * directives.
2077 if (((istk->conds && !emitting(istk->conds->state)) ||
2078 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2079 return NO_DIRECTIVE_FOUND;
2083 * If we're defining a macro or reading a %rep block, we should
2084 * ignore all directives except for %macro/%imacro (which nest),
2085 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2086 * If we're in a %rep block, another %rep nests, so should be let through.
2088 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2089 i != PP_RMACRO && i != PP_IRMACRO &&
2090 i != PP_ENDMACRO && i != PP_ENDM &&
2091 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2092 return NO_DIRECTIVE_FOUND;
2095 if (defining) {
2096 if (i == PP_MACRO || i == PP_IMACRO ||
2097 i == PP_RMACRO || i == PP_IRMACRO) {
2098 nested_mac_count++;
2099 return NO_DIRECTIVE_FOUND;
2100 } else if (nested_mac_count > 0) {
2101 if (i == PP_ENDMACRO) {
2102 nested_mac_count--;
2103 return NO_DIRECTIVE_FOUND;
2106 if (!defining->name) {
2107 if (i == PP_REP) {
2108 nested_rep_count++;
2109 return NO_DIRECTIVE_FOUND;
2110 } else if (nested_rep_count > 0) {
2111 if (i == PP_ENDREP) {
2112 nested_rep_count--;
2113 return NO_DIRECTIVE_FOUND;
2119 switch (i) {
2120 case PP_INVALID:
2121 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2122 tline->text);
2123 return NO_DIRECTIVE_FOUND; /* didn't get it */
2125 case PP_STACKSIZE:
2126 /* Directive to tell NASM what the default stack size is. The
2127 * default is for a 16-bit stack, and this can be overriden with
2128 * %stacksize large.
2129 * the following form:
2131 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2133 tline = tline->next;
2134 if (tline && tline->type == TOK_WHITESPACE)
2135 tline = tline->next;
2136 if (!tline || tline->type != TOK_ID) {
2137 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2138 free_tlist(origline);
2139 return DIRECTIVE_FOUND;
2141 if (nasm_stricmp(tline->text, "flat") == 0) {
2142 /* All subsequent ARG directives are for a 32-bit stack */
2143 StackSize = 4;
2144 StackPointer = "ebp";
2145 ArgOffset = 8;
2146 LocalOffset = 0;
2147 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2148 /* All subsequent ARG directives are for a 64-bit stack */
2149 StackSize = 8;
2150 StackPointer = "rbp";
2151 ArgOffset = 8;
2152 LocalOffset = 0;
2153 } else if (nasm_stricmp(tline->text, "large") == 0) {
2154 /* All subsequent ARG directives are for a 16-bit stack,
2155 * far function call.
2157 StackSize = 2;
2158 StackPointer = "bp";
2159 ArgOffset = 4;
2160 LocalOffset = 0;
2161 } else if (nasm_stricmp(tline->text, "small") == 0) {
2162 /* All subsequent ARG directives are for a 16-bit stack,
2163 * far function call. We don't support near functions.
2165 StackSize = 2;
2166 StackPointer = "bp";
2167 ArgOffset = 6;
2168 LocalOffset = 0;
2169 } else {
2170 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2171 free_tlist(origline);
2172 return DIRECTIVE_FOUND;
2174 free_tlist(origline);
2175 return DIRECTIVE_FOUND;
2177 case PP_ARG:
2178 /* TASM like ARG directive to define arguments to functions, in
2179 * the following form:
2181 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2183 offset = ArgOffset;
2184 do {
2185 char *arg, directive[256];
2186 int size = StackSize;
2188 /* Find the argument name */
2189 tline = tline->next;
2190 if (tline && tline->type == TOK_WHITESPACE)
2191 tline = tline->next;
2192 if (!tline || tline->type != TOK_ID) {
2193 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2194 free_tlist(origline);
2195 return DIRECTIVE_FOUND;
2197 arg = tline->text;
2199 /* Find the argument size type */
2200 tline = tline->next;
2201 if (!tline || tline->type != TOK_OTHER
2202 || tline->text[0] != ':') {
2203 error(ERR_NONFATAL,
2204 "Syntax error processing `%%arg' directive");
2205 free_tlist(origline);
2206 return DIRECTIVE_FOUND;
2208 tline = tline->next;
2209 if (!tline || tline->type != TOK_ID) {
2210 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2211 free_tlist(origline);
2212 return DIRECTIVE_FOUND;
2215 /* Allow macro expansion of type parameter */
2216 tt = tokenize(tline->text);
2217 tt = expand_smacro(tt);
2218 size = parse_size(tt->text);
2219 if (!size) {
2220 error(ERR_NONFATAL,
2221 "Invalid size type for `%%arg' missing directive");
2222 free_tlist(tt);
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND;
2226 free_tlist(tt);
2228 /* Round up to even stack slots */
2229 size = (size+StackSize-1) & ~(StackSize-1);
2231 /* Now define the macro for the argument */
2232 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2233 arg, StackPointer, offset);
2234 do_directive(tokenize(directive));
2235 offset += size;
2237 /* Move to the next argument in the list */
2238 tline = tline->next;
2239 if (tline && tline->type == TOK_WHITESPACE)
2240 tline = tline->next;
2241 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2242 ArgOffset = offset;
2243 free_tlist(origline);
2244 return DIRECTIVE_FOUND;
2246 case PP_LOCAL:
2247 /* TASM like LOCAL directive to define local variables for a
2248 * function, in the following form:
2250 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2252 * The '= LocalSize' at the end is ignored by NASM, but is
2253 * required by TASM to define the local parameter size (and used
2254 * by the TASM macro package).
2256 offset = LocalOffset;
2257 do {
2258 char *local, directive[256];
2259 int size = StackSize;
2261 /* Find the argument name */
2262 tline = tline->next;
2263 if (tline && tline->type == TOK_WHITESPACE)
2264 tline = tline->next;
2265 if (!tline || tline->type != TOK_ID) {
2266 error(ERR_NONFATAL,
2267 "`%%local' missing argument parameter");
2268 free_tlist(origline);
2269 return DIRECTIVE_FOUND;
2271 local = tline->text;
2273 /* Find the argument size type */
2274 tline = tline->next;
2275 if (!tline || tline->type != TOK_OTHER
2276 || tline->text[0] != ':') {
2277 error(ERR_NONFATAL,
2278 "Syntax error processing `%%local' directive");
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND;
2282 tline = tline->next;
2283 if (!tline || tline->type != TOK_ID) {
2284 error(ERR_NONFATAL,
2285 "`%%local' missing size type parameter");
2286 free_tlist(origline);
2287 return DIRECTIVE_FOUND;
2290 /* Allow macro expansion of type parameter */
2291 tt = tokenize(tline->text);
2292 tt = expand_smacro(tt);
2293 size = parse_size(tt->text);
2294 if (!size) {
2295 error(ERR_NONFATAL,
2296 "Invalid size type for `%%local' missing directive");
2297 free_tlist(tt);
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND;
2301 free_tlist(tt);
2303 /* Round up to even stack slots */
2304 size = (size+StackSize-1) & ~(StackSize-1);
2306 offset += size; /* Negative offset, increment before */
2308 /* Now define the macro for the argument */
2309 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2310 local, StackPointer, offset);
2311 do_directive(tokenize(directive));
2313 /* Now define the assign to setup the enter_c macro correctly */
2314 snprintf(directive, sizeof(directive),
2315 "%%assign %%$localsize %%$localsize+%d", size);
2316 do_directive(tokenize(directive));
2318 /* Move to the next argument in the list */
2319 tline = tline->next;
2320 if (tline && tline->type == TOK_WHITESPACE)
2321 tline = tline->next;
2322 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2323 LocalOffset = offset;
2324 free_tlist(origline);
2325 return DIRECTIVE_FOUND;
2327 case PP_CLEAR:
2328 if (tline->next)
2329 error(ERR_WARNING|ERR_PASS1,
2330 "trailing garbage after `%%clear' ignored");
2331 free_macros();
2332 init_macros();
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2336 case PP_DEPEND:
2337 t = tline->next = expand_smacro(tline->next);
2338 skip_white_(t);
2339 if (!t || (t->type != TOK_STRING &&
2340 t->type != TOK_INTERNAL_STRING)) {
2341 error(ERR_NONFATAL, "`%%depend' expects a file name");
2342 free_tlist(origline);
2343 return DIRECTIVE_FOUND; /* but we did _something_ */
2345 if (t->next)
2346 error(ERR_WARNING|ERR_PASS1,
2347 "trailing garbage after `%%depend' ignored");
2348 p = t->text;
2349 if (t->type != TOK_INTERNAL_STRING)
2350 nasm_unquote_cstr(p, i);
2351 if (dephead && !in_list(*dephead, p)) {
2352 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2353 sl->next = NULL;
2354 strcpy(sl->str, p);
2355 *deptail = sl;
2356 deptail = &sl->next;
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2361 case PP_INCLUDE:
2362 t = tline->next = expand_smacro(tline->next);
2363 skip_white_(t);
2365 if (!t || (t->type != TOK_STRING &&
2366 t->type != TOK_INTERNAL_STRING)) {
2367 error(ERR_NONFATAL, "`%%include' expects a file name");
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND; /* but we did _something_ */
2371 if (t->next)
2372 error(ERR_WARNING|ERR_PASS1,
2373 "trailing garbage after `%%include' ignored");
2374 p = t->text;
2375 if (t->type != TOK_INTERNAL_STRING)
2376 nasm_unquote_cstr(p, i);
2377 inc = nasm_malloc(sizeof(Include));
2378 inc->next = istk;
2379 inc->conds = NULL;
2380 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2381 if (!inc->fp) {
2382 /* -MG given but file not found */
2383 nasm_free(inc);
2384 } else {
2385 inc->fname = src_set_fname(nasm_strdup(p));
2386 inc->lineno = src_set_linnum(0);
2387 inc->lineinc = 1;
2388 inc->expansion = NULL;
2389 inc->mstk = NULL;
2390 istk = inc;
2391 list->uplevel(LIST_INCLUDE);
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2396 case PP_USE:
2398 static macros_t *use_pkg;
2399 const char *pkg_macro;
2401 tline = tline->next;
2402 skip_white_(tline);
2403 tline = expand_id(tline);
2405 if (!tline || (tline->type != TOK_STRING &&
2406 tline->type != TOK_INTERNAL_STRING &&
2407 tline->type != TOK_ID)) {
2408 error(ERR_NONFATAL, "`%%use' expects a package name");
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND; /* but we did _something_ */
2412 if (tline->next)
2413 error(ERR_WARNING|ERR_PASS1,
2414 "trailing garbage after `%%use' ignored");
2415 if (tline->type == TOK_STRING)
2416 nasm_unquote_cstr(tline->text, i);
2417 use_pkg = nasm_stdmac_find_package(tline->text);
2418 if (!use_pkg)
2419 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2420 /* The first string will be <%define>__USE_*__ */
2421 pkg_macro = (char *)use_pkg + 1;
2422 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2423 /* Not already included, go ahead and include it */
2424 stdmacpos = use_pkg;
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2429 case PP_PUSH:
2430 case PP_REPL:
2431 case PP_POP:
2432 tline = tline->next;
2433 skip_white_(tline);
2434 tline = expand_id(tline);
2435 if (tline) {
2436 if (!tok_type_(tline, TOK_ID)) {
2437 error(ERR_NONFATAL, "`%s' expects a context identifier",
2438 pp_directives[i]);
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND; /* but we did _something_ */
2442 if (tline->next)
2443 error(ERR_WARNING|ERR_PASS1,
2444 "trailing garbage after `%s' ignored",
2445 pp_directives[i]);
2446 p = nasm_strdup(tline->text);
2447 } else {
2448 p = NULL; /* Anonymous */
2451 if (i == PP_PUSH) {
2452 ctx = nasm_malloc(sizeof(Context));
2453 ctx->next = cstk;
2454 hash_init(&ctx->localmac, HASH_SMALL);
2455 ctx->name = p;
2456 ctx->number = unique++;
2457 cstk = ctx;
2458 } else {
2459 /* %pop or %repl */
2460 if (!cstk) {
2461 error(ERR_NONFATAL, "`%s': context stack is empty",
2462 pp_directives[i]);
2463 } else if (i == PP_POP) {
2464 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2465 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2466 "expected %s",
2467 cstk->name ? cstk->name : "anonymous", p);
2468 else
2469 ctx_pop();
2470 } else {
2471 /* i == PP_REPL */
2472 nasm_free(cstk->name);
2473 cstk->name = p;
2474 p = NULL;
2476 nasm_free(p);
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 case PP_FATAL:
2481 severity = ERR_FATAL;
2482 goto issue_error;
2483 case PP_ERROR:
2484 severity = ERR_NONFATAL;
2485 goto issue_error;
2486 case PP_WARNING:
2487 severity = ERR_WARNING|ERR_WARN_USER;
2488 goto issue_error;
2490 issue_error:
2492 /* Only error out if this is the final pass */
2493 if (pass != 2 && i != PP_FATAL)
2494 return DIRECTIVE_FOUND;
2496 tline->next = expand_smacro(tline->next);
2497 tline = tline->next;
2498 skip_white_(tline);
2499 t = tline ? tline->next : NULL;
2500 skip_white_(t);
2501 if (tok_type_(tline, TOK_STRING) && !t) {
2502 /* The line contains only a quoted string */
2503 p = tline->text;
2504 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2505 error(severity, "%s", p);
2506 } else {
2507 /* Not a quoted string, or more than a quoted string */
2508 p = detoken(tline, false);
2509 error(severity, "%s", p);
2510 nasm_free(p);
2512 free_tlist(origline);
2513 return DIRECTIVE_FOUND;
2516 CASE_PP_IF:
2517 if (istk->conds && !emitting(istk->conds->state))
2518 j = COND_NEVER;
2519 else {
2520 j = if_condition(tline->next, i);
2521 tline->next = NULL; /* it got freed */
2522 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2524 cond = nasm_malloc(sizeof(Cond));
2525 cond->next = istk->conds;
2526 cond->state = j;
2527 istk->conds = cond;
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2531 CASE_PP_ELIF:
2532 if (!istk->conds)
2533 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2534 switch(istk->conds->state) {
2535 case COND_IF_TRUE:
2536 istk->conds->state = COND_DONE;
2537 break;
2539 case COND_DONE:
2540 case COND_NEVER:
2541 break;
2543 case COND_ELSE_TRUE:
2544 case COND_ELSE_FALSE:
2545 error_precond(ERR_WARNING|ERR_PASS1,
2546 "`%%elif' after `%%else' ignored");
2547 istk->conds->state = COND_NEVER;
2548 break;
2550 case COND_IF_FALSE:
2552 * IMPORTANT: In the case of %if, we will already have
2553 * called expand_mmac_params(); however, if we're
2554 * processing an %elif we must have been in a
2555 * non-emitting mode, which would have inhibited
2556 * the normal invocation of expand_mmac_params().
2557 * Therefore, we have to do it explicitly here.
2559 j = if_condition(expand_mmac_params(tline->next), i);
2560 tline->next = NULL; /* it got freed */
2561 istk->conds->state =
2562 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2563 break;
2565 free_tlist(origline);
2566 return DIRECTIVE_FOUND;
2568 case PP_ELSE:
2569 if (tline->next)
2570 error_precond(ERR_WARNING|ERR_PASS1,
2571 "trailing garbage after `%%else' ignored");
2572 if (!istk->conds)
2573 error(ERR_FATAL, "`%%else': no matching `%%if'");
2574 switch(istk->conds->state) {
2575 case COND_IF_TRUE:
2576 case COND_DONE:
2577 istk->conds->state = COND_ELSE_FALSE;
2578 break;
2580 case COND_NEVER:
2581 break;
2583 case COND_IF_FALSE:
2584 istk->conds->state = COND_ELSE_TRUE;
2585 break;
2587 case COND_ELSE_TRUE:
2588 case COND_ELSE_FALSE:
2589 error_precond(ERR_WARNING|ERR_PASS1,
2590 "`%%else' after `%%else' ignored.");
2591 istk->conds->state = COND_NEVER;
2592 break;
2594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
2597 case PP_ENDIF:
2598 if (tline->next)
2599 error_precond(ERR_WARNING|ERR_PASS1,
2600 "trailing garbage after `%%endif' ignored");
2601 if (!istk->conds)
2602 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2603 cond = istk->conds;
2604 istk->conds = cond->next;
2605 nasm_free(cond);
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND;
2609 case PP_RMACRO:
2610 case PP_IRMACRO:
2611 case PP_MACRO:
2612 case PP_IMACRO:
2613 if (defining) {
2614 error(ERR_FATAL, "`%s': already defining a macro",
2615 pp_directives[i]);
2616 return DIRECTIVE_FOUND;
2618 defining = nasm_malloc(sizeof(MMacro));
2619 defining->max_depth =
2620 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2621 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2622 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2623 nasm_free(defining);
2624 defining = NULL;
2625 return DIRECTIVE_FOUND;
2628 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2629 while (mmac) {
2630 if (!strcmp(mmac->name, defining->name) &&
2631 (mmac->nparam_min <= defining->nparam_max
2632 || defining->plus)
2633 && (defining->nparam_min <= mmac->nparam_max
2634 || mmac->plus)) {
2635 error(ERR_WARNING|ERR_PASS1,
2636 "redefining multi-line macro `%s'", defining->name);
2637 return DIRECTIVE_FOUND;
2639 mmac = mmac->next;
2641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
2644 case PP_ENDM:
2645 case PP_ENDMACRO:
2646 if (! (defining && defining->name)) {
2647 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2648 return DIRECTIVE_FOUND;
2650 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2651 defining->next = *mmhead;
2652 *mmhead = defining;
2653 defining = NULL;
2654 free_tlist(origline);
2655 return DIRECTIVE_FOUND;
2657 case PP_EXITMACRO:
2659 * We must search along istk->expansion until we hit a
2660 * macro-end marker for a macro with a name. Then we set
2661 * its `in_progress' flag to 0.
2663 for (l = istk->expansion; l; l = l->next)
2664 if (l->finishes && l->finishes->name)
2665 break;
2667 if (l) {
2668 l->finishes->in_progress = 0;
2669 } else {
2670 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2672 free_tlist(origline);
2673 return DIRECTIVE_FOUND;
2675 case PP_UNMACRO:
2676 case PP_UNIMACRO:
2678 MMacro **mmac_p;
2679 MMacro spec;
2681 spec.casesense = (i == PP_UNMACRO);
2682 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2683 return DIRECTIVE_FOUND;
2685 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2686 while (mmac_p && *mmac_p) {
2687 mmac = *mmac_p;
2688 if (mmac->casesense == spec.casesense &&
2689 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2690 mmac->nparam_min == spec.nparam_min &&
2691 mmac->nparam_max == spec.nparam_max &&
2692 mmac->plus == spec.plus) {
2693 *mmac_p = mmac->next;
2694 free_mmacro(mmac);
2695 } else {
2696 mmac_p = &mmac->next;
2699 free_tlist(origline);
2700 free_tlist(spec.dlist);
2701 return DIRECTIVE_FOUND;
2704 case PP_ROTATE:
2705 if (tline->next && tline->next->type == TOK_WHITESPACE)
2706 tline = tline->next;
2707 if (!tline->next) {
2708 free_tlist(origline);
2709 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2710 return DIRECTIVE_FOUND;
2712 t = expand_smacro(tline->next);
2713 tline->next = NULL;
2714 free_tlist(origline);
2715 tline = t;
2716 tptr = &t;
2717 tokval.t_type = TOKEN_INVALID;
2718 evalresult =
2719 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2720 free_tlist(tline);
2721 if (!evalresult)
2722 return DIRECTIVE_FOUND;
2723 if (tokval.t_type)
2724 error(ERR_WARNING|ERR_PASS1,
2725 "trailing garbage after expression ignored");
2726 if (!is_simple(evalresult)) {
2727 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2728 return DIRECTIVE_FOUND;
2730 mmac = istk->mstk;
2731 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2732 mmac = mmac->next_active;
2733 if (!mmac) {
2734 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2735 } else if (mmac->nparam == 0) {
2736 error(ERR_NONFATAL,
2737 "`%%rotate' invoked within macro without parameters");
2738 } else {
2739 int rotate = mmac->rotate + reloc_value(evalresult);
2741 rotate %= (int)mmac->nparam;
2742 if (rotate < 0)
2743 rotate += mmac->nparam;
2745 mmac->rotate = rotate;
2747 return DIRECTIVE_FOUND;
2749 case PP_REP:
2750 nolist = false;
2751 do {
2752 tline = tline->next;
2753 } while (tok_type_(tline, TOK_WHITESPACE));
2755 if (tok_type_(tline, TOK_ID) &&
2756 nasm_stricmp(tline->text, ".nolist") == 0) {
2757 nolist = true;
2758 do {
2759 tline = tline->next;
2760 } while (tok_type_(tline, TOK_WHITESPACE));
2763 if (tline) {
2764 t = expand_smacro(tline);
2765 tptr = &t;
2766 tokval.t_type = TOKEN_INVALID;
2767 evalresult =
2768 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2769 if (!evalresult) {
2770 free_tlist(origline);
2771 return DIRECTIVE_FOUND;
2773 if (tokval.t_type)
2774 error(ERR_WARNING|ERR_PASS1,
2775 "trailing garbage after expression ignored");
2776 if (!is_simple(evalresult)) {
2777 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2778 return DIRECTIVE_FOUND;
2780 count = reloc_value(evalresult) + 1;
2781 } else {
2782 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2783 count = 0;
2785 free_tlist(origline);
2787 tmp_defining = defining;
2788 defining = nasm_malloc(sizeof(MMacro));
2789 defining->prev = NULL;
2790 defining->name = NULL; /* flags this macro as a %rep block */
2791 defining->casesense = false;
2792 defining->plus = false;
2793 defining->nolist = nolist;
2794 defining->in_progress = count;
2795 defining->max_depth = 0;
2796 defining->nparam_min = defining->nparam_max = 0;
2797 defining->defaults = NULL;
2798 defining->dlist = NULL;
2799 defining->expansion = NULL;
2800 defining->next_active = istk->mstk;
2801 defining->rep_nest = tmp_defining;
2802 return DIRECTIVE_FOUND;
2804 case PP_ENDREP:
2805 if (!defining || defining->name) {
2806 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2807 return DIRECTIVE_FOUND;
2811 * Now we have a "macro" defined - although it has no name
2812 * and we won't be entering it in the hash tables - we must
2813 * push a macro-end marker for it on to istk->expansion.
2814 * After that, it will take care of propagating itself (a
2815 * macro-end marker line for a macro which is really a %rep
2816 * block will cause the macro to be re-expanded, complete
2817 * with another macro-end marker to ensure the process
2818 * continues) until the whole expansion is forcibly removed
2819 * from istk->expansion by a %exitrep.
2821 l = nasm_malloc(sizeof(Line));
2822 l->next = istk->expansion;
2823 l->finishes = defining;
2824 l->first = NULL;
2825 istk->expansion = l;
2827 istk->mstk = defining;
2829 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2830 tmp_defining = defining;
2831 defining = defining->rep_nest;
2832 free_tlist(origline);
2833 return DIRECTIVE_FOUND;
2835 case PP_EXITREP:
2837 * We must search along istk->expansion until we hit a
2838 * macro-end marker for a macro with no name. Then we set
2839 * its `in_progress' flag to 0.
2841 for (l = istk->expansion; l; l = l->next)
2842 if (l->finishes && !l->finishes->name)
2843 break;
2845 if (l)
2846 l->finishes->in_progress = 1;
2847 else
2848 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2849 free_tlist(origline);
2850 return DIRECTIVE_FOUND;
2852 case PP_XDEFINE:
2853 case PP_IXDEFINE:
2854 case PP_DEFINE:
2855 case PP_IDEFINE:
2856 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2858 tline = tline->next;
2859 skip_white_(tline);
2860 tline = expand_id(tline);
2861 if (!tline || (tline->type != TOK_ID &&
2862 (tline->type != TOK_PREPROC_ID ||
2863 tline->text[1] != '$'))) {
2864 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2865 pp_directives[i]);
2866 free_tlist(origline);
2867 return DIRECTIVE_FOUND;
2870 ctx = get_ctx(tline->text, &mname, false);
2871 last = tline;
2872 param_start = tline = tline->next;
2873 nparam = 0;
2875 /* Expand the macro definition now for %xdefine and %ixdefine */
2876 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2877 tline = expand_smacro(tline);
2879 if (tok_is_(tline, "(")) {
2881 * This macro has parameters.
2884 tline = tline->next;
2885 while (1) {
2886 skip_white_(tline);
2887 if (!tline) {
2888 error(ERR_NONFATAL, "parameter identifier expected");
2889 free_tlist(origline);
2890 return DIRECTIVE_FOUND;
2892 if (tline->type != TOK_ID) {
2893 error(ERR_NONFATAL,
2894 "`%s': parameter identifier expected",
2895 tline->text);
2896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
2899 tline->type = TOK_SMAC_PARAM + nparam++;
2900 tline = tline->next;
2901 skip_white_(tline);
2902 if (tok_is_(tline, ",")) {
2903 tline = tline->next;
2904 } else {
2905 if (!tok_is_(tline, ")")) {
2906 error(ERR_NONFATAL,
2907 "`)' expected to terminate macro template");
2908 free_tlist(origline);
2909 return DIRECTIVE_FOUND;
2911 break;
2914 last = tline;
2915 tline = tline->next;
2917 if (tok_type_(tline, TOK_WHITESPACE))
2918 last = tline, tline = tline->next;
2919 macro_start = NULL;
2920 last->next = NULL;
2921 t = tline;
2922 while (t) {
2923 if (t->type == TOK_ID) {
2924 for (tt = param_start; tt; tt = tt->next)
2925 if (tt->type >= TOK_SMAC_PARAM &&
2926 !strcmp(tt->text, t->text))
2927 t->type = tt->type;
2929 tt = t->next;
2930 t->next = macro_start;
2931 macro_start = t;
2932 t = tt;
2935 * Good. We now have a macro name, a parameter count, and a
2936 * token list (in reverse order) for an expansion. We ought
2937 * to be OK just to create an SMacro, store it, and let
2938 * free_tlist have the rest of the line (which we have
2939 * carefully re-terminated after chopping off the expansion
2940 * from the end).
2942 define_smacro(ctx, mname, casesense, nparam, macro_start);
2943 free_tlist(origline);
2944 return DIRECTIVE_FOUND;
2946 case PP_UNDEF:
2947 tline = tline->next;
2948 skip_white_(tline);
2949 tline = expand_id(tline);
2950 if (!tline || (tline->type != TOK_ID &&
2951 (tline->type != TOK_PREPROC_ID ||
2952 tline->text[1] != '$'))) {
2953 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
2957 if (tline->next) {
2958 error(ERR_WARNING|ERR_PASS1,
2959 "trailing garbage after macro name ignored");
2962 /* Find the context that symbol belongs to */
2963 ctx = get_ctx(tline->text, &mname, false);
2964 undef_smacro(ctx, mname);
2965 free_tlist(origline);
2966 return DIRECTIVE_FOUND;
2968 case PP_DEFSTR:
2969 case PP_IDEFSTR:
2970 casesense = (i == PP_DEFSTR);
2972 tline = tline->next;
2973 skip_white_(tline);
2974 tline = expand_id(tline);
2975 if (!tline || (tline->type != TOK_ID &&
2976 (tline->type != TOK_PREPROC_ID ||
2977 tline->text[1] != '$'))) {
2978 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2979 pp_directives[i]);
2980 free_tlist(origline);
2981 return DIRECTIVE_FOUND;
2984 ctx = get_ctx(tline->text, &mname, false);
2985 last = tline;
2986 tline = expand_smacro(tline->next);
2987 last->next = NULL;
2989 while (tok_type_(tline, TOK_WHITESPACE))
2990 tline = delete_Token(tline);
2992 p = detoken(tline, false);
2993 macro_start = nasm_malloc(sizeof(*macro_start));
2994 macro_start->next = NULL;
2995 macro_start->text = nasm_quote(p, strlen(p));
2996 macro_start->type = TOK_STRING;
2997 macro_start->a.mac = NULL;
2998 nasm_free(p);
3001 * We now have a macro name, an implicit parameter count of
3002 * zero, and a string token to use as an expansion. Create
3003 * and store an SMacro.
3005 define_smacro(ctx, mname, casesense, 0, macro_start);
3006 free_tlist(origline);
3007 return DIRECTIVE_FOUND;
3009 case PP_DEFTOK:
3010 case PP_IDEFTOK:
3011 casesense = (i == PP_DEFTOK);
3013 tline = tline->next;
3014 skip_white_(tline);
3015 tline = expand_id(tline);
3016 if (!tline || (tline->type != TOK_ID &&
3017 (tline->type != TOK_PREPROC_ID ||
3018 tline->text[1] != '$'))) {
3019 error(ERR_NONFATAL,
3020 "`%s' expects a macro identifier as first parameter",
3021 pp_directives[i]);
3022 free_tlist(origline);
3023 return DIRECTIVE_FOUND;
3025 ctx = get_ctx(tline->text, &mname, false);
3026 last = tline;
3027 tline = expand_smacro(tline->next);
3028 last->next = NULL;
3030 t = tline;
3031 while (tok_type_(t, TOK_WHITESPACE))
3032 t = t->next;
3033 /* t should now point to the string */
3034 if (t->type != TOK_STRING) {
3035 error(ERR_NONFATAL,
3036 "`%s` requires string as second parameter",
3037 pp_directives[i]);
3038 free_tlist(tline);
3039 free_tlist(origline);
3040 return DIRECTIVE_FOUND;
3043 nasm_unquote_cstr(t->text, i);
3044 macro_start = tokenize(t->text);
3047 * We now have a macro name, an implicit parameter count of
3048 * zero, and a numeric token to use as an expansion. Create
3049 * and store an SMacro.
3051 define_smacro(ctx, mname, casesense, 0, macro_start);
3052 free_tlist(tline);
3053 free_tlist(origline);
3054 return DIRECTIVE_FOUND;
3056 case PP_PATHSEARCH:
3058 FILE *fp;
3059 StrList *xsl = NULL;
3060 StrList **xst = &xsl;
3062 casesense = true;
3064 tline = tline->next;
3065 skip_white_(tline);
3066 tline = expand_id(tline);
3067 if (!tline || (tline->type != TOK_ID &&
3068 (tline->type != TOK_PREPROC_ID ||
3069 tline->text[1] != '$'))) {
3070 error(ERR_NONFATAL,
3071 "`%%pathsearch' expects a macro identifier as first parameter");
3072 free_tlist(origline);
3073 return DIRECTIVE_FOUND;
3075 ctx = get_ctx(tline->text, &mname, false);
3076 last = tline;
3077 tline = expand_smacro(tline->next);
3078 last->next = NULL;
3080 t = tline;
3081 while (tok_type_(t, TOK_WHITESPACE))
3082 t = t->next;
3084 if (!t || (t->type != TOK_STRING &&
3085 t->type != TOK_INTERNAL_STRING)) {
3086 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3087 free_tlist(tline);
3088 free_tlist(origline);
3089 return DIRECTIVE_FOUND; /* but we did _something_ */
3091 if (t->next)
3092 error(ERR_WARNING|ERR_PASS1,
3093 "trailing garbage after `%%pathsearch' ignored");
3094 p = t->text;
3095 if (t->type != TOK_INTERNAL_STRING)
3096 nasm_unquote(p, NULL);
3098 fp = inc_fopen(p, &xsl, &xst, true);
3099 if (fp) {
3100 p = xsl->str;
3101 fclose(fp); /* Don't actually care about the file */
3103 macro_start = nasm_malloc(sizeof(*macro_start));
3104 macro_start->next = NULL;
3105 macro_start->text = nasm_quote(p, strlen(p));
3106 macro_start->type = TOK_STRING;
3107 macro_start->a.mac = NULL;
3108 if (xsl)
3109 nasm_free(xsl);
3112 * We now have a macro name, an implicit parameter count of
3113 * zero, and a string token to use as an expansion. Create
3114 * and store an SMacro.
3116 define_smacro(ctx, mname, casesense, 0, macro_start);
3117 free_tlist(tline);
3118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
3122 case PP_STRLEN:
3123 casesense = true;
3125 tline = tline->next;
3126 skip_white_(tline);
3127 tline = expand_id(tline);
3128 if (!tline || (tline->type != TOK_ID &&
3129 (tline->type != TOK_PREPROC_ID ||
3130 tline->text[1] != '$'))) {
3131 error(ERR_NONFATAL,
3132 "`%%strlen' expects a macro identifier as first parameter");
3133 free_tlist(origline);
3134 return DIRECTIVE_FOUND;
3136 ctx = get_ctx(tline->text, &mname, false);
3137 last = tline;
3138 tline = expand_smacro(tline->next);
3139 last->next = NULL;
3141 t = tline;
3142 while (tok_type_(t, TOK_WHITESPACE))
3143 t = t->next;
3144 /* t should now point to the string */
3145 if (t->type != TOK_STRING) {
3146 error(ERR_NONFATAL,
3147 "`%%strlen` requires string as second parameter");
3148 free_tlist(tline);
3149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3153 macro_start = nasm_malloc(sizeof(*macro_start));
3154 macro_start->next = NULL;
3155 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3156 macro_start->a.mac = NULL;
3159 * We now have a macro name, an implicit parameter count of
3160 * zero, and a numeric token to use as an expansion. Create
3161 * and store an SMacro.
3163 define_smacro(ctx, mname, casesense, 0, macro_start);
3164 free_tlist(tline);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3168 case PP_STRCAT:
3169 casesense = true;
3171 tline = tline->next;
3172 skip_white_(tline);
3173 tline = expand_id(tline);
3174 if (!tline || (tline->type != TOK_ID &&
3175 (tline->type != TOK_PREPROC_ID ||
3176 tline->text[1] != '$'))) {
3177 error(ERR_NONFATAL,
3178 "`%%strcat' expects a macro identifier as first parameter");
3179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
3182 ctx = get_ctx(tline->text, &mname, false);
3183 last = tline;
3184 tline = expand_smacro(tline->next);
3185 last->next = NULL;
3187 len = 0;
3188 for (t = tline; t; t = t->next) {
3189 switch (t->type) {
3190 case TOK_WHITESPACE:
3191 break;
3192 case TOK_STRING:
3193 len += t->a.len = nasm_unquote(t->text, NULL);
3194 break;
3195 case TOK_OTHER:
3196 if (!strcmp(t->text, ",")) /* permit comma separators */
3197 break;
3198 /* else fall through */
3199 default:
3200 error(ERR_NONFATAL,
3201 "non-string passed to `%%strcat' (%d)", t->type);
3202 free_tlist(tline);
3203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
3208 p = pp = nasm_malloc(len);
3209 for (t = tline; t; t = t->next) {
3210 if (t->type == TOK_STRING) {
3211 memcpy(p, t->text, t->a.len);
3212 p += t->a.len;
3217 * We now have a macro name, an implicit parameter count of
3218 * zero, and a numeric token to use as an expansion. Create
3219 * and store an SMacro.
3221 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3222 macro_start->text = nasm_quote(pp, len);
3223 nasm_free(pp);
3224 define_smacro(ctx, mname, casesense, 0, macro_start);
3225 free_tlist(tline);
3226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
3229 case PP_SUBSTR:
3231 int64_t a1, a2;
3232 size_t len;
3234 casesense = true;
3236 tline = tline->next;
3237 skip_white_(tline);
3238 tline = expand_id(tline);
3239 if (!tline || (tline->type != TOK_ID &&
3240 (tline->type != TOK_PREPROC_ID ||
3241 tline->text[1] != '$'))) {
3242 error(ERR_NONFATAL,
3243 "`%%substr' expects a macro identifier as first parameter");
3244 free_tlist(origline);
3245 return DIRECTIVE_FOUND;
3247 ctx = get_ctx(tline->text, &mname, false);
3248 last = tline;
3249 tline = expand_smacro(tline->next);
3250 last->next = NULL;
3252 t = tline->next;
3253 while (tok_type_(t, TOK_WHITESPACE))
3254 t = t->next;
3256 /* t should now point to the string */
3257 if (t->type != TOK_STRING) {
3258 error(ERR_NONFATAL,
3259 "`%%substr` requires string as second parameter");
3260 free_tlist(tline);
3261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3265 tt = t->next;
3266 tptr = &tt;
3267 tokval.t_type = TOKEN_INVALID;
3268 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3269 pass, error, NULL);
3270 if (!evalresult) {
3271 free_tlist(tline);
3272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
3274 } else if (!is_simple(evalresult)) {
3275 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3276 free_tlist(tline);
3277 free_tlist(origline);
3278 return DIRECTIVE_FOUND;
3280 a1 = evalresult->value-1;
3282 while (tok_type_(tt, TOK_WHITESPACE))
3283 tt = tt->next;
3284 if (!tt) {
3285 a2 = 1; /* Backwards compatibility: one character */
3286 } else {
3287 tokval.t_type = TOKEN_INVALID;
3288 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3289 pass, error, NULL);
3290 if (!evalresult) {
3291 free_tlist(tline);
3292 free_tlist(origline);
3293 return DIRECTIVE_FOUND;
3294 } else if (!is_simple(evalresult)) {
3295 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3300 a2 = evalresult->value;
3303 len = nasm_unquote(t->text, NULL);
3304 if (a2 < 0)
3305 a2 = a2+1+len-a1;
3306 if (a1+a2 > (int64_t)len)
3307 a2 = len-a1;
3309 macro_start = nasm_malloc(sizeof(*macro_start));
3310 macro_start->next = NULL;
3311 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
3312 macro_start->type = TOK_STRING;
3313 macro_start->a.mac = NULL;
3316 * We now have a macro name, an implicit parameter count of
3317 * zero, and a numeric token to use as an expansion. Create
3318 * and store an SMacro.
3320 define_smacro(ctx, mname, casesense, 0, macro_start);
3321 free_tlist(tline);
3322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
3326 case PP_ASSIGN:
3327 case PP_IASSIGN:
3328 casesense = (i == PP_ASSIGN);
3330 tline = tline->next;
3331 skip_white_(tline);
3332 tline = expand_id(tline);
3333 if (!tline || (tline->type != TOK_ID &&
3334 (tline->type != TOK_PREPROC_ID ||
3335 tline->text[1] != '$'))) {
3336 error(ERR_NONFATAL,
3337 "`%%%sassign' expects a macro identifier",
3338 (i == PP_IASSIGN ? "i" : ""));
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
3342 ctx = get_ctx(tline->text, &mname, false);
3343 last = tline;
3344 tline = expand_smacro(tline->next);
3345 last->next = NULL;
3347 t = tline;
3348 tptr = &t;
3349 tokval.t_type = TOKEN_INVALID;
3350 evalresult =
3351 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3352 free_tlist(tline);
3353 if (!evalresult) {
3354 free_tlist(origline);
3355 return DIRECTIVE_FOUND;
3358 if (tokval.t_type)
3359 error(ERR_WARNING|ERR_PASS1,
3360 "trailing garbage after expression ignored");
3362 if (!is_simple(evalresult)) {
3363 error(ERR_NONFATAL,
3364 "non-constant value given to `%%%sassign'",
3365 (i == PP_IASSIGN ? "i" : ""));
3366 free_tlist(origline);
3367 return DIRECTIVE_FOUND;
3370 macro_start = nasm_malloc(sizeof(*macro_start));
3371 macro_start->next = NULL;
3372 make_tok_num(macro_start, reloc_value(evalresult));
3373 macro_start->a.mac = NULL;
3376 * We now have a macro name, an implicit parameter count of
3377 * zero, and a numeric token to use as an expansion. Create
3378 * and store an SMacro.
3380 define_smacro(ctx, mname, casesense, 0, macro_start);
3381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
3384 case PP_LINE:
3386 * Syntax is `%line nnn[+mmm] [filename]'
3388 tline = tline->next;
3389 skip_white_(tline);
3390 if (!tok_type_(tline, TOK_NUMBER)) {
3391 error(ERR_NONFATAL, "`%%line' expects line number");
3392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3395 k = readnum(tline->text, &err);
3396 m = 1;
3397 tline = tline->next;
3398 if (tok_is_(tline, "+")) {
3399 tline = tline->next;
3400 if (!tok_type_(tline, TOK_NUMBER)) {
3401 error(ERR_NONFATAL, "`%%line' expects line increment");
3402 free_tlist(origline);
3403 return DIRECTIVE_FOUND;
3405 m = readnum(tline->text, &err);
3406 tline = tline->next;
3408 skip_white_(tline);
3409 src_set_linnum(k);
3410 istk->lineinc = m;
3411 if (tline) {
3412 nasm_free(src_set_fname(detoken(tline, false)));
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3417 default:
3418 error(ERR_FATAL,
3419 "preprocessor directive `%s' not yet implemented",
3420 pp_directives[i]);
3421 return DIRECTIVE_FOUND;
3426 * Ensure that a macro parameter contains a condition code and
3427 * nothing else. Return the condition code index if so, or -1
3428 * otherwise.
3430 static int find_cc(Token * t)
3432 Token *tt;
3433 int i, j, k, m;
3435 if (!t)
3436 return -1; /* Probably a %+ without a space */
3438 skip_white_(t);
3439 if (t->type != TOK_ID)
3440 return -1;
3441 tt = t->next;
3442 skip_white_(tt);
3443 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3444 return -1;
3446 i = -1;
3447 j = elements(conditions);
3448 while (j - i > 1) {
3449 k = (j + i) / 2;
3450 m = nasm_stricmp(t->text, conditions[k]);
3451 if (m == 0) {
3452 i = k;
3453 j = -2;
3454 break;
3455 } else if (m < 0) {
3456 j = k;
3457 } else
3458 i = k;
3460 if (j != -2)
3461 return -1;
3462 return i;
3465 static bool paste_tokens(Token **head, bool handle_paste_tokens)
3467 Token **tail, *t, *tt;
3468 Token **paste_head;
3469 bool did_paste = false;
3470 char *tmp;
3472 /* Now handle token pasting... */
3473 paste_head = NULL;
3474 tail = head;
3475 while ((t = *tail) && (tt = t->next)) {
3476 switch (t->type) {
3477 case TOK_WHITESPACE:
3478 if (tt->type == TOK_WHITESPACE) {
3479 /* Zap adjacent whitespace tokens */
3480 t->next = delete_Token(tt);
3481 } else {
3482 /* Do not advance paste_head here */
3483 tail = &t->next;
3485 break;
3486 case TOK_ID:
3487 case TOK_PREPROC_ID:
3488 case TOK_NUMBER:
3489 case TOK_FLOAT:
3491 size_t len = 0;
3492 char *tmp, *p;
3494 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3495 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3496 tt->type == TOK_OTHER)) {
3497 len += strlen(tt->text);
3498 tt = tt->next;
3501 /* Now tt points to the first token after the potential
3502 paste area... */
3503 if (tt != t->next) {
3504 /* We have at least two tokens... */
3505 len += strlen(t->text);
3506 p = tmp = nasm_malloc(len+1);
3508 while (t != tt) {
3509 strcpy(p, t->text);
3510 p = strchr(p, '\0');
3511 t = delete_Token(t);
3514 t = *tail = tokenize(tmp);
3515 nasm_free(tmp);
3517 while (t->next) {
3518 tail = &t->next;
3519 t = t->next;
3521 t->next = tt; /* Attach the remaining token chain */
3523 did_paste = true;
3525 paste_head = tail;
3526 tail = &t->next;
3527 break;
3529 case TOK_PASTE: /* %+ */
3530 if (handle_paste_tokens) {
3531 /* Zap %+ and whitespace tokens to the right */
3532 while (t && (t->type == TOK_WHITESPACE ||
3533 t->type == TOK_PASTE))
3534 t = *tail = delete_Token(t);
3535 if (!paste_head || !t)
3536 break; /* Nothing to paste with */
3537 tail = paste_head;
3538 t = *tail;
3539 tt = t->next;
3540 while (tok_type_(tt, TOK_WHITESPACE))
3541 tt = t->next = delete_Token(tt);
3543 if (tt) {
3544 tmp = nasm_strcat(t->text, tt->text);
3545 delete_Token(t);
3546 tt = delete_Token(tt);
3547 t = *tail = tokenize(tmp);
3548 nasm_free(tmp);
3549 while (t->next) {
3550 tail = &t->next;
3551 t = t->next;
3553 t->next = tt; /* Attach the remaining token chain */
3554 did_paste = true;
3556 paste_head = tail;
3557 tail = &t->next;
3558 break;
3560 /* else fall through */
3561 default:
3562 tail = paste_head = &t->next;
3563 break;
3566 return did_paste;
3569 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3570 * %-n) and MMacro-local identifiers (%%foo) as well as
3571 * macro indirection (%[...]).
3573 static Token *expand_mmac_params(Token * tline)
3575 Token *t, *tt, **tail, *thead;
3576 bool changed = false;
3578 tail = &thead;
3579 thead = NULL;
3581 while (tline) {
3582 if (tline->type == TOK_PREPROC_ID &&
3583 (((tline->text[1] == '+' || tline->text[1] == '-')
3584 && tline->text[2]) || tline->text[1] == '%'
3585 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
3586 char *text = NULL;
3587 int type = 0, cc; /* type = 0 to placate optimisers */
3588 char tmpbuf[30];
3589 unsigned int n;
3590 int i;
3591 MMacro *mac;
3593 t = tline;
3594 tline = tline->next;
3596 mac = istk->mstk;
3597 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3598 mac = mac->next_active;
3599 if (!mac)
3600 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3601 else
3602 switch (t->text[1]) {
3604 * We have to make a substitution of one of the
3605 * forms %1, %-1, %+1, %%foo, %0.
3607 case '0':
3608 type = TOK_NUMBER;
3609 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3610 text = nasm_strdup(tmpbuf);
3611 break;
3612 case '%':
3613 type = TOK_ID;
3614 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3615 mac->unique);
3616 text = nasm_strcat(tmpbuf, t->text + 2);
3617 break;
3618 case '-':
3619 n = atoi(t->text + 2) - 1;
3620 if (n >= mac->nparam)
3621 tt = NULL;
3622 else {
3623 if (mac->nparam > 1)
3624 n = (n + mac->rotate) % mac->nparam;
3625 tt = mac->params[n];
3627 cc = find_cc(tt);
3628 if (cc == -1) {
3629 error(ERR_NONFATAL,
3630 "macro parameter %d is not a condition code",
3631 n + 1);
3632 text = NULL;
3633 } else {
3634 type = TOK_ID;
3635 if (inverse_ccs[cc] == -1) {
3636 error(ERR_NONFATAL,
3637 "condition code `%s' is not invertible",
3638 conditions[cc]);
3639 text = NULL;
3640 } else
3641 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3643 break;
3644 case '+':
3645 n = atoi(t->text + 2) - 1;
3646 if (n >= mac->nparam)
3647 tt = NULL;
3648 else {
3649 if (mac->nparam > 1)
3650 n = (n + mac->rotate) % mac->nparam;
3651 tt = mac->params[n];
3653 cc = find_cc(tt);
3654 if (cc == -1) {
3655 error(ERR_NONFATAL,
3656 "macro parameter %d is not a condition code",
3657 n + 1);
3658 text = NULL;
3659 } else {
3660 type = TOK_ID;
3661 text = nasm_strdup(conditions[cc]);
3663 break;
3664 default:
3665 n = atoi(t->text + 1) - 1;
3666 if (n >= mac->nparam)
3667 tt = NULL;
3668 else {
3669 if (mac->nparam > 1)
3670 n = (n + mac->rotate) % mac->nparam;
3671 tt = mac->params[n];
3673 if (tt) {
3674 for (i = 0; i < mac->paramlen[n]; i++) {
3675 *tail = new_Token(NULL, tt->type, tt->text, 0);
3676 tail = &(*tail)->next;
3677 tt = tt->next;
3680 text = NULL; /* we've done it here */
3681 break;
3683 if (!text) {
3684 delete_Token(t);
3685 } else {
3686 *tail = t;
3687 tail = &t->next;
3688 t->type = type;
3689 nasm_free(t->text);
3690 t->text = text;
3691 t->a.mac = NULL;
3693 changed = true;
3694 continue;
3695 } else if (tline->type == TOK_INDIRECT) {
3696 t = tline;
3697 tline = tline->next;
3698 tt = tokenize(t->text);
3699 tt = expand_mmac_params(tt);
3700 tt = expand_smacro(tt);
3701 *tail = tt;
3702 while (tt) {
3703 tt->a.mac = NULL; /* Necessary? */
3704 tail = &tt->next;
3705 tt = tt->next;
3707 delete_Token(t);
3708 changed = true;
3709 } else {
3710 t = *tail = tline;
3711 tline = tline->next;
3712 t->a.mac = NULL;
3713 tail = &t->next;
3716 *tail = NULL;
3718 if (changed)
3719 paste_tokens(&thead, false);
3721 return thead;
3725 * Expand all single-line macro calls made in the given line.
3726 * Return the expanded version of the line. The original is deemed
3727 * to be destroyed in the process. (In reality we'll just move
3728 * Tokens from input to output a lot of the time, rather than
3729 * actually bothering to destroy and replicate.)
3732 static Token *expand_smacro(Token * tline)
3734 Token *t, *tt, *mstart, **tail, *thead;
3735 struct hash_table *smtbl;
3736 SMacro *head = NULL, *m;
3737 Token **params;
3738 int *paramsize;
3739 unsigned int nparam, sparam;
3740 int brackets;
3741 Token *org_tline = tline;
3742 Context *ctx;
3743 const char *mname;
3744 int deadman = DEADMAN_LIMIT;
3745 bool expanded;
3748 * Trick: we should avoid changing the start token pointer since it can
3749 * be contained in "next" field of other token. Because of this
3750 * we allocate a copy of first token and work with it; at the end of
3751 * routine we copy it back
3753 if (org_tline) {
3754 tline =
3755 new_Token(org_tline->next, org_tline->type, org_tline->text,
3757 tline->a.mac = org_tline->a.mac;
3758 nasm_free(org_tline->text);
3759 org_tline->text = NULL;
3762 expanded = true; /* Always expand %+ at least once */
3764 again:
3765 tail = &thead;
3766 thead = NULL;
3768 while (tline) { /* main token loop */
3769 if (!--deadman) {
3770 error(ERR_NONFATAL, "interminable macro recursion");
3771 break;
3774 if ((mname = tline->text)) {
3775 /* if this token is a local macro, look in local context */
3776 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3777 ctx = get_ctx(mname, &mname, true);
3778 else
3779 ctx = NULL;
3780 smtbl = ctx ? &ctx->localmac : &smacros;
3781 head = (SMacro *) hash_findix(smtbl, mname);
3784 * We've hit an identifier. As in is_mmacro below, we first
3785 * check whether the identifier is a single-line macro at
3786 * all, then think about checking for parameters if
3787 * necessary.
3789 for (m = head; m; m = m->next)
3790 if (!mstrcmp(m->name, mname, m->casesense))
3791 break;
3792 if (m) {
3793 mstart = tline;
3794 params = NULL;
3795 paramsize = NULL;
3796 if (m->nparam == 0) {
3798 * Simple case: the macro is parameterless. Discard the
3799 * one token that the macro call took, and push the
3800 * expansion back on the to-do stack.
3802 if (!m->expansion) {
3803 if (!strcmp("__FILE__", m->name)) {
3804 int32_t num = 0;
3805 char *file = NULL;
3806 src_get(&num, &file);
3807 tline->text = nasm_quote(file, strlen(file));
3808 tline->type = TOK_STRING;
3809 nasm_free(file);
3810 continue;
3812 if (!strcmp("__LINE__", m->name)) {
3813 nasm_free(tline->text);
3814 make_tok_num(tline, src_get_linnum());
3815 continue;
3817 if (!strcmp("__BITS__", m->name)) {
3818 nasm_free(tline->text);
3819 make_tok_num(tline, globalbits);
3820 continue;
3822 tline = delete_Token(tline);
3823 continue;
3825 } else {
3827 * Complicated case: at least one macro with this name
3828 * exists and takes parameters. We must find the
3829 * parameters in the call, count them, find the SMacro
3830 * that corresponds to that form of the macro call, and
3831 * substitute for the parameters when we expand. What a
3832 * pain.
3834 /*tline = tline->next;
3835 skip_white_(tline); */
3836 do {
3837 t = tline->next;
3838 while (tok_type_(t, TOK_SMAC_END)) {
3839 t->a.mac->in_progress = false;
3840 t->text = NULL;
3841 t = tline->next = delete_Token(t);
3843 tline = t;
3844 } while (tok_type_(tline, TOK_WHITESPACE));
3845 if (!tok_is_(tline, "(")) {
3847 * This macro wasn't called with parameters: ignore
3848 * the call. (Behaviour borrowed from gnu cpp.)
3850 tline = mstart;
3851 m = NULL;
3852 } else {
3853 int paren = 0;
3854 int white = 0;
3855 brackets = 0;
3856 nparam = 0;
3857 sparam = PARAM_DELTA;
3858 params = nasm_malloc(sparam * sizeof(Token *));
3859 params[0] = tline->next;
3860 paramsize = nasm_malloc(sparam * sizeof(int));
3861 paramsize[0] = 0;
3862 while (true) { /* parameter loop */
3864 * For some unusual expansions
3865 * which concatenates function call
3867 t = tline->next;
3868 while (tok_type_(t, TOK_SMAC_END)) {
3869 t->a.mac->in_progress = false;
3870 t->text = NULL;
3871 t = tline->next = delete_Token(t);
3873 tline = t;
3875 if (!tline) {
3876 error(ERR_NONFATAL,
3877 "macro call expects terminating `)'");
3878 break;
3880 if (tline->type == TOK_WHITESPACE
3881 && brackets <= 0) {
3882 if (paramsize[nparam])
3883 white++;
3884 else
3885 params[nparam] = tline->next;
3886 continue; /* parameter loop */
3888 if (tline->type == TOK_OTHER
3889 && tline->text[1] == 0) {
3890 char ch = tline->text[0];
3891 if (ch == ',' && !paren && brackets <= 0) {
3892 if (++nparam >= sparam) {
3893 sparam += PARAM_DELTA;
3894 params = nasm_realloc(params,
3895 sparam *
3896 sizeof(Token
3897 *));
3898 paramsize =
3899 nasm_realloc(paramsize,
3900 sparam *
3901 sizeof(int));
3903 params[nparam] = tline->next;
3904 paramsize[nparam] = 0;
3905 white = 0;
3906 continue; /* parameter loop */
3908 if (ch == '{' &&
3909 (brackets > 0 || (brackets == 0 &&
3910 !paramsize[nparam])))
3912 if (!(brackets++)) {
3913 params[nparam] = tline->next;
3914 continue; /* parameter loop */
3917 if (ch == '}' && brackets > 0)
3918 if (--brackets == 0) {
3919 brackets = -1;
3920 continue; /* parameter loop */
3922 if (ch == '(' && !brackets)
3923 paren++;
3924 if (ch == ')' && brackets <= 0)
3925 if (--paren < 0)
3926 break;
3928 if (brackets < 0) {
3929 brackets = 0;
3930 error(ERR_NONFATAL, "braces do not "
3931 "enclose all of macro parameter");
3933 paramsize[nparam] += white + 1;
3934 white = 0;
3935 } /* parameter loop */
3936 nparam++;
3937 while (m && (m->nparam != nparam ||
3938 mstrcmp(m->name, mname,
3939 m->casesense)))
3940 m = m->next;
3941 if (!m)
3942 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
3943 "macro `%s' exists, "
3944 "but not taking %d parameters",
3945 mstart->text, nparam);
3948 if (m && m->in_progress)
3949 m = NULL;
3950 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3952 * Design question: should we handle !tline, which
3953 * indicates missing ')' here, or expand those
3954 * macros anyway, which requires the (t) test a few
3955 * lines down?
3957 nasm_free(params);
3958 nasm_free(paramsize);
3959 tline = mstart;
3960 } else {
3962 * Expand the macro: we are placed on the last token of the
3963 * call, so that we can easily split the call from the
3964 * following tokens. We also start by pushing an SMAC_END
3965 * token for the cycle removal.
3967 t = tline;
3968 if (t) {
3969 tline = t->next;
3970 t->next = NULL;
3972 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3973 tt->a.mac = m;
3974 m->in_progress = true;
3975 tline = tt;
3976 for (t = m->expansion; t; t = t->next) {
3977 if (t->type >= TOK_SMAC_PARAM) {
3978 Token *pcopy = tline, **ptail = &pcopy;
3979 Token *ttt, *pt;
3980 int i;
3982 ttt = params[t->type - TOK_SMAC_PARAM];
3983 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3984 --i >= 0;) {
3985 pt = *ptail =
3986 new_Token(tline, ttt->type, ttt->text,
3988 ptail = &pt->next;
3989 ttt = ttt->next;
3991 tline = pcopy;
3992 } else if (t->type == TOK_PREPROC_Q) {
3993 tt = new_Token(tline, TOK_ID, mname, 0);
3994 tline = tt;
3995 } else if (t->type == TOK_PREPROC_QQ) {
3996 tt = new_Token(tline, TOK_ID, m->name, 0);
3997 tline = tt;
3998 } else {
3999 tt = new_Token(tline, t->type, t->text, 0);
4000 tline = tt;
4005 * Having done that, get rid of the macro call, and clean
4006 * up the parameters.
4008 nasm_free(params);
4009 nasm_free(paramsize);
4010 free_tlist(mstart);
4011 expanded = true;
4012 continue; /* main token loop */
4017 if (tline->type == TOK_SMAC_END) {
4018 tline->a.mac->in_progress = false;
4019 tline = delete_Token(tline);
4020 } else {
4021 t = *tail = tline;
4022 tline = tline->next;
4023 t->a.mac = NULL;
4024 t->next = NULL;
4025 tail = &t->next;
4030 * Now scan the entire line and look for successive TOK_IDs that resulted
4031 * after expansion (they can't be produced by tokenize()). The successive
4032 * TOK_IDs should be concatenated.
4033 * Also we look for %+ tokens and concatenate the tokens before and after
4034 * them (without white spaces in between).
4036 if (expanded && paste_tokens(&thead, true)) {
4038 * If we concatenated something, *and* we had previously expanded
4039 * an actual macro, scan the lines again for macros...
4041 tline = thead;
4042 expanded = false;
4043 goto again;
4046 if (org_tline) {
4047 if (thead) {
4048 *org_tline = *thead;
4049 /* since we just gave text to org_line, don't free it */
4050 thead->text = NULL;
4051 delete_Token(thead);
4052 } else {
4053 /* the expression expanded to empty line;
4054 we can't return NULL for some reasons
4055 we just set the line to a single WHITESPACE token. */
4056 memset(org_tline, 0, sizeof(*org_tline));
4057 org_tline->text = NULL;
4058 org_tline->type = TOK_WHITESPACE;
4060 thead = org_tline;
4063 return thead;
4067 * Similar to expand_smacro but used exclusively with macro identifiers
4068 * right before they are fetched in. The reason is that there can be
4069 * identifiers consisting of several subparts. We consider that if there
4070 * are more than one element forming the name, user wants a expansion,
4071 * otherwise it will be left as-is. Example:
4073 * %define %$abc cde
4075 * the identifier %$abc will be left as-is so that the handler for %define
4076 * will suck it and define the corresponding value. Other case:
4078 * %define _%$abc cde
4080 * In this case user wants name to be expanded *before* %define starts
4081 * working, so we'll expand %$abc into something (if it has a value;
4082 * otherwise it will be left as-is) then concatenate all successive
4083 * PP_IDs into one.
4085 static Token *expand_id(Token * tline)
4087 Token *cur, *oldnext = NULL;
4089 if (!tline || !tline->next)
4090 return tline;
4092 cur = tline;
4093 while (cur->next &&
4094 (cur->next->type == TOK_ID ||
4095 cur->next->type == TOK_PREPROC_ID
4096 || cur->next->type == TOK_NUMBER))
4097 cur = cur->next;
4099 /* If identifier consists of just one token, don't expand */
4100 if (cur == tline)
4101 return tline;
4103 if (cur) {
4104 oldnext = cur->next; /* Detach the tail past identifier */
4105 cur->next = NULL; /* so that expand_smacro stops here */
4108 tline = expand_smacro(tline);
4110 if (cur) {
4111 /* expand_smacro possibly changhed tline; re-scan for EOL */
4112 cur = tline;
4113 while (cur && cur->next)
4114 cur = cur->next;
4115 if (cur)
4116 cur->next = oldnext;
4119 return tline;
4123 * Determine whether the given line constitutes a multi-line macro
4124 * call, and return the MMacro structure called if so. Doesn't have
4125 * to check for an initial label - that's taken care of in
4126 * expand_mmacro - but must check numbers of parameters. Guaranteed
4127 * to be called with tline->type == TOK_ID, so the putative macro
4128 * name is easy to find.
4130 static MMacro *is_mmacro(Token * tline, Token *** params_array)
4132 MMacro *head, *m;
4133 Token **params;
4134 int nparam;
4136 head = (MMacro *) hash_findix(&mmacros, tline->text);
4139 * Efficiency: first we see if any macro exists with the given
4140 * name. If not, we can return NULL immediately. _Then_ we
4141 * count the parameters, and then we look further along the
4142 * list if necessary to find the proper MMacro.
4144 for (m = head; m; m = m->next)
4145 if (!mstrcmp(m->name, tline->text, m->casesense))
4146 break;
4147 if (!m)
4148 return NULL;
4151 * OK, we have a potential macro. Count and demarcate the
4152 * parameters.
4154 count_mmac_params(tline->next, &nparam, &params);
4157 * So we know how many parameters we've got. Find the MMacro
4158 * structure that handles this number.
4160 while (m) {
4161 if (m->nparam_min <= nparam
4162 && (m->plus || nparam <= m->nparam_max)) {
4164 * This one is right. Just check if cycle removal
4165 * prohibits us using it before we actually celebrate...
4167 if (m->in_progress > m->max_depth) {
4168 if (m->max_depth > 0) {
4169 error(ERR_WARNING,
4170 "reached maximum recursion depth of %i",
4171 m->max_depth);
4173 nasm_free(params);
4174 return NULL;
4177 * It's right, and we can use it. Add its default
4178 * parameters to the end of our list if necessary.
4180 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4181 params =
4182 nasm_realloc(params,
4183 ((m->nparam_min + m->ndefs +
4184 1) * sizeof(*params)));
4185 while (nparam < m->nparam_min + m->ndefs) {
4186 params[nparam] = m->defaults[nparam - m->nparam_min];
4187 nparam++;
4191 * If we've gone over the maximum parameter count (and
4192 * we're in Plus mode), ignore parameters beyond
4193 * nparam_max.
4195 if (m->plus && nparam > m->nparam_max)
4196 nparam = m->nparam_max;
4198 * Then terminate the parameter list, and leave.
4200 if (!params) { /* need this special case */
4201 params = nasm_malloc(sizeof(*params));
4202 nparam = 0;
4204 params[nparam] = NULL;
4205 *params_array = params;
4206 return m;
4209 * This one wasn't right: look for the next one with the
4210 * same name.
4212 for (m = m->next; m; m = m->next)
4213 if (!mstrcmp(m->name, tline->text, m->casesense))
4214 break;
4218 * After all that, we didn't find one with the right number of
4219 * parameters. Issue a warning, and fail to expand the macro.
4221 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4222 "macro `%s' exists, but not taking %d parameters",
4223 tline->text, nparam);
4224 nasm_free(params);
4225 return NULL;
4230 * Save MMacro invocation specific fields in
4231 * preparation for a recursive macro expansion
4233 static void push_mmacro(MMacro *m)
4235 MMacroInvocation *i;
4237 i = nasm_malloc(sizeof(MMacroInvocation));
4238 i->prev = m->prev;
4239 i->params = m->params;
4240 i->iline = m->iline;
4241 i->nparam = m->nparam;
4242 i->rotate = m->rotate;
4243 i->paramlen = m->paramlen;
4244 i->unique = m->unique;
4245 m->prev = i;
4250 * Restore MMacro invocation specific fields that were
4251 * saved during a previous recursive macro expansion
4253 static void pop_mmacro(MMacro *m)
4255 MMacroInvocation *i;
4257 if (m->prev) {
4258 i = m->prev;
4259 m->prev = i->prev;
4260 m->params = i->params;
4261 m->iline = i->iline;
4262 m->nparam = i->nparam;
4263 m->rotate = i->rotate;
4264 m->paramlen = i->paramlen;
4265 m->unique = i->unique;
4266 nasm_free(i);
4272 * Expand the multi-line macro call made by the given line, if
4273 * there is one to be expanded. If there is, push the expansion on
4274 * istk->expansion and return 1. Otherwise return 0.
4276 static int expand_mmacro(Token * tline)
4278 Token *startline = tline;
4279 Token *label = NULL;
4280 int dont_prepend = 0;
4281 Token **params, *t, *mtok, *tt;
4282 MMacro *m;
4283 Line *l, *ll;
4284 int i, nparam, *paramlen;
4285 const char *mname;
4287 t = tline;
4288 skip_white_(t);
4289 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4290 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4291 return 0;
4292 mtok = t;
4293 m = is_mmacro(t, &params);
4294 if (m) {
4295 mname = t->text;
4296 } else {
4297 Token *last;
4299 * We have an id which isn't a macro call. We'll assume
4300 * it might be a label; we'll also check to see if a
4301 * colon follows it. Then, if there's another id after
4302 * that lot, we'll check it again for macro-hood.
4304 label = last = t;
4305 t = t->next;
4306 if (tok_type_(t, TOK_WHITESPACE))
4307 last = t, t = t->next;
4308 if (tok_is_(t, ":")) {
4309 dont_prepend = 1;
4310 last = t, t = t->next;
4311 if (tok_type_(t, TOK_WHITESPACE))
4312 last = t, t = t->next;
4314 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4315 return 0;
4316 last->next = NULL;
4317 mname = t->text;
4318 tline = t;
4322 * Fix up the parameters: this involves stripping leading and
4323 * trailing whitespace, then stripping braces if they are
4324 * present.
4326 for (nparam = 0; params[nparam]; nparam++) ;
4327 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4329 for (i = 0; params[i]; i++) {
4330 int brace = false;
4331 int comma = (!m->plus || i < nparam - 1);
4333 t = params[i];
4334 skip_white_(t);
4335 if (tok_is_(t, "{"))
4336 t = t->next, brace = true, comma = false;
4337 params[i] = t;
4338 paramlen[i] = 0;
4339 while (t) {
4340 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4341 break; /* ... because we have hit a comma */
4342 if (comma && t->type == TOK_WHITESPACE
4343 && tok_is_(t->next, ","))
4344 break; /* ... or a space then a comma */
4345 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4346 break; /* ... or a brace */
4347 t = t->next;
4348 paramlen[i]++;
4353 * OK, we have a MMacro structure together with a set of
4354 * parameters. We must now go through the expansion and push
4355 * copies of each Line on to istk->expansion. Substitution of
4356 * parameter tokens and macro-local tokens doesn't get done
4357 * until the single-line macro substitution process; this is
4358 * because delaying them allows us to change the semantics
4359 * later through %rotate.
4361 * First, push an end marker on to istk->expansion, mark this
4362 * macro as in progress, and set up its invocation-specific
4363 * variables.
4365 ll = nasm_malloc(sizeof(Line));
4366 ll->next = istk->expansion;
4367 ll->finishes = m;
4368 ll->first = NULL;
4369 istk->expansion = ll;
4372 * Save the previous MMacro expansion in the case of
4373 * macro recursion
4375 if (m->max_depth && m->in_progress)
4376 push_mmacro(m);
4378 m->in_progress ++;
4379 m->params = params;
4380 m->iline = tline;
4381 m->nparam = nparam;
4382 m->rotate = 0;
4383 m->paramlen = paramlen;
4384 m->unique = unique++;
4385 m->lineno = 0;
4387 m->next_active = istk->mstk;
4388 istk->mstk = m;
4390 for (l = m->expansion; l; l = l->next) {
4391 Token **tail;
4393 ll = nasm_malloc(sizeof(Line));
4394 ll->finishes = NULL;
4395 ll->next = istk->expansion;
4396 istk->expansion = ll;
4397 tail = &ll->first;
4399 for (t = l->first; t; t = t->next) {
4400 Token *x = t;
4401 switch (t->type) {
4402 case TOK_PREPROC_Q:
4403 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4404 break;
4405 case TOK_PREPROC_QQ:
4406 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4407 break;
4408 case TOK_PREPROC_ID:
4409 if (t->text[1] == '0' && t->text[2] == '0') {
4410 dont_prepend = -1;
4411 x = label;
4412 if (!x)
4413 continue;
4415 /* fall through */
4416 default:
4417 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4418 break;
4420 tail = &tt->next;
4422 *tail = NULL;
4426 * If we had a label, push it on as the first line of
4427 * the macro expansion.
4429 if (label) {
4430 if (dont_prepend < 0)
4431 free_tlist(startline);
4432 else {
4433 ll = nasm_malloc(sizeof(Line));
4434 ll->finishes = NULL;
4435 ll->next = istk->expansion;
4436 istk->expansion = ll;
4437 ll->first = startline;
4438 if (!dont_prepend) {
4439 while (label->next)
4440 label = label->next;
4441 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4446 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4448 return 1;
4451 /* The function that actually does the error reporting */
4452 static void verror(int severity, const char *fmt, va_list arg)
4454 char buff[1024];
4456 vsnprintf(buff, sizeof(buff), fmt, arg);
4458 if (istk && istk->mstk && istk->mstk->name)
4459 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
4460 istk->mstk->lineno, buff);
4461 else
4462 nasm_error(severity, "%s", buff);
4466 * Since preprocessor always operate only on the line that didn't
4467 * arrived yet, we should always use ERR_OFFBY1.
4469 static void error(int severity, const char *fmt, ...)
4471 va_list arg;
4473 /* If we're in a dead branch of IF or something like it, ignore the error */
4474 if (istk && istk->conds && !emitting(istk->conds->state))
4475 return;
4477 va_start(arg, fmt);
4478 verror(severity, fmt, arg);
4479 va_end(arg);
4483 * Because %else etc are evaluated in the state context
4484 * of the previous branch, errors might get lost with error():
4485 * %if 0 ... %else trailing garbage ... %endif
4486 * So %else etc should report errors with this function.
4488 static void error_precond(int severity, const char *fmt, ...)
4490 va_list arg;
4492 /* Only ignore the error if it's really in a dead branch */
4493 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4494 return;
4496 va_start(arg, fmt);
4497 verror(severity, fmt, arg);
4498 va_end(arg);
4501 static void
4502 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
4504 Token *t;
4506 cstk = NULL;
4507 istk = nasm_malloc(sizeof(Include));
4508 istk->next = NULL;
4509 istk->conds = NULL;
4510 istk->expansion = NULL;
4511 istk->mstk = NULL;
4512 istk->fp = fopen(file, "r");
4513 istk->fname = NULL;
4514 src_set_fname(nasm_strdup(file));
4515 src_set_linnum(0);
4516 istk->lineinc = 1;
4517 if (!istk->fp)
4518 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
4519 file);
4520 defining = NULL;
4521 nested_mac_count = 0;
4522 nested_rep_count = 0;
4523 init_macros();
4524 unique = 0;
4525 if (tasm_compatible_mode) {
4526 stdmacpos = nasm_stdmac;
4527 } else {
4528 stdmacpos = nasm_stdmac_after_tasm;
4530 any_extrastdmac = extrastdmac && *extrastdmac;
4531 do_predef = true;
4532 list = listgen;
4535 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4536 * The caller, however, will also pass in 3 for preprocess-only so
4537 * we can set __PASS__ accordingly.
4539 pass = apass > 2 ? 2 : apass;
4541 dephead = deptail = deplist;
4542 if (deplist) {
4543 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4544 sl->next = NULL;
4545 strcpy(sl->str, file);
4546 *deptail = sl;
4547 deptail = &sl->next;
4551 * Define the __PASS__ macro. This is defined here unlike
4552 * all the other builtins, because it is special -- it varies between
4553 * passes.
4555 t = nasm_malloc(sizeof(*t));
4556 t->next = NULL;
4557 make_tok_num(t, apass);
4558 t->a.mac = NULL;
4559 define_smacro(NULL, "__PASS__", true, 0, t);
4562 static char *pp_getline(void)
4564 char *line;
4565 Token *tline;
4567 while (1) {
4569 * Fetch a tokenized line, either from the macro-expansion
4570 * buffer or from the input file.
4572 tline = NULL;
4573 while (istk->expansion && istk->expansion->finishes) {
4574 Line *l = istk->expansion;
4575 if (!l->finishes->name && l->finishes->in_progress > 1) {
4576 Line *ll;
4579 * This is a macro-end marker for a macro with no
4580 * name, which means it's not really a macro at all
4581 * but a %rep block, and the `in_progress' field is
4582 * more than 1, meaning that we still need to
4583 * repeat. (1 means the natural last repetition; 0
4584 * means termination by %exitrep.) We have
4585 * therefore expanded up to the %endrep, and must
4586 * push the whole block on to the expansion buffer
4587 * again. We don't bother to remove the macro-end
4588 * marker: we'd only have to generate another one
4589 * if we did.
4591 l->finishes->in_progress--;
4592 for (l = l->finishes->expansion; l; l = l->next) {
4593 Token *t, *tt, **tail;
4595 ll = nasm_malloc(sizeof(Line));
4596 ll->next = istk->expansion;
4597 ll->finishes = NULL;
4598 ll->first = NULL;
4599 tail = &ll->first;
4601 for (t = l->first; t; t = t->next) {
4602 if (t->text || t->type == TOK_WHITESPACE) {
4603 tt = *tail =
4604 new_Token(NULL, t->type, t->text, 0);
4605 tail = &tt->next;
4609 istk->expansion = ll;
4611 } else {
4613 * Check whether a `%rep' was started and not ended
4614 * within this macro expansion. This can happen and
4615 * should be detected. It's a fatal error because
4616 * I'm too confused to work out how to recover
4617 * sensibly from it.
4619 if (defining) {
4620 if (defining->name)
4621 error(ERR_PANIC,
4622 "defining with name in expansion");
4623 else if (istk->mstk->name)
4624 error(ERR_FATAL,
4625 "`%%rep' without `%%endrep' within"
4626 " expansion of macro `%s'",
4627 istk->mstk->name);
4631 * FIXME: investigate the relationship at this point between
4632 * istk->mstk and l->finishes
4635 MMacro *m = istk->mstk;
4636 istk->mstk = m->next_active;
4637 if (m->name) {
4639 * This was a real macro call, not a %rep, and
4640 * therefore the parameter information needs to
4641 * be freed.
4643 if (m->prev) {
4644 pop_mmacro(m);
4645 l->finishes->in_progress --;
4646 } else {
4647 nasm_free(m->params);
4648 free_tlist(m->iline);
4649 nasm_free(m->paramlen);
4650 l->finishes->in_progress = 0;
4652 } else
4653 free_mmacro(m);
4655 istk->expansion = l->next;
4656 nasm_free(l);
4657 list->downlevel(LIST_MACRO);
4660 while (1) { /* until we get a line we can use */
4662 if (istk->expansion) { /* from a macro expansion */
4663 char *p;
4664 Line *l = istk->expansion;
4665 if (istk->mstk)
4666 istk->mstk->lineno++;
4667 tline = l->first;
4668 istk->expansion = l->next;
4669 nasm_free(l);
4670 p = detoken(tline, false);
4671 list->line(LIST_MACRO, p);
4672 nasm_free(p);
4673 break;
4675 line = read_line();
4676 if (line) { /* from the current input file */
4677 line = prepreproc(line);
4678 tline = tokenize(line);
4679 nasm_free(line);
4680 break;
4683 * The current file has ended; work down the istk
4686 Include *i = istk;
4687 fclose(i->fp);
4688 if (i->conds)
4689 error(ERR_FATAL,
4690 "expected `%%endif' before end of file");
4691 /* only set line and file name if there's a next node */
4692 if (i->next) {
4693 src_set_linnum(i->lineno);
4694 nasm_free(src_set_fname(i->fname));
4696 istk = i->next;
4697 list->downlevel(LIST_INCLUDE);
4698 nasm_free(i);
4699 if (!istk)
4700 return NULL;
4701 if (istk->expansion && istk->expansion->finishes)
4702 break;
4707 * We must expand MMacro parameters and MMacro-local labels
4708 * _before_ we plunge into directive processing, to cope
4709 * with things like `%define something %1' such as STRUC
4710 * uses. Unless we're _defining_ a MMacro, in which case
4711 * those tokens should be left alone to go into the
4712 * definition; and unless we're in a non-emitting
4713 * condition, in which case we don't want to meddle with
4714 * anything.
4716 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4717 && !(istk->mstk && !istk->mstk->in_progress)) {
4718 tline = expand_mmac_params(tline);
4722 * Check the line to see if it's a preprocessor directive.
4724 if (do_directive(tline) == DIRECTIVE_FOUND) {
4725 continue;
4726 } else if (defining) {
4728 * We're defining a multi-line macro. We emit nothing
4729 * at all, and just
4730 * shove the tokenized line on to the macro definition.
4732 Line *l = nasm_malloc(sizeof(Line));
4733 l->next = defining->expansion;
4734 l->first = tline;
4735 l->finishes = NULL;
4736 defining->expansion = l;
4737 continue;
4738 } else if (istk->conds && !emitting(istk->conds->state)) {
4740 * We're in a non-emitting branch of a condition block.
4741 * Emit nothing at all, not even a blank line: when we
4742 * emerge from the condition we'll give a line-number
4743 * directive so we keep our place correctly.
4745 free_tlist(tline);
4746 continue;
4747 } else if (istk->mstk && !istk->mstk->in_progress) {
4749 * We're in a %rep block which has been terminated, so
4750 * we're walking through to the %endrep without
4751 * emitting anything. Emit nothing at all, not even a
4752 * blank line: when we emerge from the %rep block we'll
4753 * give a line-number directive so we keep our place
4754 * correctly.
4756 free_tlist(tline);
4757 continue;
4758 } else {
4759 tline = expand_smacro(tline);
4760 if (!expand_mmacro(tline)) {
4762 * De-tokenize the line again, and emit it.
4764 line = detoken(tline, true);
4765 free_tlist(tline);
4766 break;
4767 } else {
4768 continue; /* expand_mmacro calls free_tlist */
4773 return line;
4776 static void pp_cleanup(int pass)
4778 if (defining) {
4779 if (defining->name) {
4780 error(ERR_NONFATAL,
4781 "end of file while still defining macro `%s'",
4782 defining->name);
4783 } else {
4784 error(ERR_NONFATAL, "end of file while still in %%rep");
4787 free_mmacro(defining);
4789 while (cstk)
4790 ctx_pop();
4791 free_macros();
4792 while (istk) {
4793 Include *i = istk;
4794 istk = istk->next;
4795 fclose(i->fp);
4796 nasm_free(i->fname);
4797 nasm_free(i);
4799 while (cstk)
4800 ctx_pop();
4801 nasm_free(src_set_fname(NULL));
4802 if (pass == 0) {
4803 IncPath *i;
4804 free_llist(predef);
4805 delete_Blocks();
4806 while ((i = ipath)) {
4807 ipath = i->next;
4808 if (i->path)
4809 nasm_free(i->path);
4810 nasm_free(i);
4815 void pp_include_path(char *path)
4817 IncPath *i;
4819 i = nasm_malloc(sizeof(IncPath));
4820 i->path = path ? nasm_strdup(path) : NULL;
4821 i->next = NULL;
4823 if (ipath) {
4824 IncPath *j = ipath;
4825 while (j->next)
4826 j = j->next;
4827 j->next = i;
4828 } else {
4829 ipath = i;
4833 void pp_pre_include(char *fname)
4835 Token *inc, *space, *name;
4836 Line *l;
4838 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4839 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4840 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4842 l = nasm_malloc(sizeof(Line));
4843 l->next = predef;
4844 l->first = inc;
4845 l->finishes = NULL;
4846 predef = l;
4849 void pp_pre_define(char *definition)
4851 Token *def, *space;
4852 Line *l;
4853 char *equals;
4855 equals = strchr(definition, '=');
4856 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4857 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4858 if (equals)
4859 *equals = ' ';
4860 space->next = tokenize(definition);
4861 if (equals)
4862 *equals = '=';
4864 l = nasm_malloc(sizeof(Line));
4865 l->next = predef;
4866 l->first = def;
4867 l->finishes = NULL;
4868 predef = l;
4871 void pp_pre_undefine(char *definition)
4873 Token *def, *space;
4874 Line *l;
4876 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4877 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4878 space->next = tokenize(definition);
4880 l = nasm_malloc(sizeof(Line));
4881 l->next = predef;
4882 l->first = def;
4883 l->finishes = NULL;
4884 predef = l;
4888 * Added by Keith Kanios:
4890 * This function is used to assist with "runtime" preprocessor
4891 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4893 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4894 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4897 void pp_runtime(char *definition)
4899 Token *def;
4901 def = tokenize(definition);
4902 if (do_directive(def) == NO_DIRECTIVE_FOUND)
4903 free_tlist(def);
4907 void pp_extra_stdmac(macros_t *macros)
4909 extrastdmac = macros;
4912 static void make_tok_num(Token * tok, int64_t val)
4914 char numbuf[20];
4915 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4916 tok->text = nasm_strdup(numbuf);
4917 tok->type = TOK_NUMBER;
4920 Preproc nasmpp = {
4921 pp_reset,
4922 pp_getline,
4923 pp_cleanup