NASM 2.03rc1
[nasm.git] / preproc.c
blobab5fff7b37019e900117dd1be959cffb8906c0ce
1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
9 */
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
21 * }
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
36 #include "compiler.h"
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "preproc.h"
50 #include "hashtbl.h"
51 #include "stdscan.h"
52 #include "tokens.h"
53 #include "tables.h"
55 typedef struct SMacro SMacro;
56 typedef struct MMacro MMacro;
57 typedef struct Context Context;
58 typedef struct Token Token;
59 typedef struct Blocks Blocks;
60 typedef struct Line Line;
61 typedef struct Include Include;
62 typedef struct Cond Cond;
63 typedef struct IncPath IncPath;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
77 struct SMacro {
78 SMacro *next;
79 char *name;
80 bool casesense;
81 bool in_progress;
82 unsigned int nparam;
83 Token *expansion;
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
103 struct MMacro {
104 MMacro *next;
105 char *name;
106 int nparam_min, nparam_max;
107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
110 int64_t in_progress;
111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
114 Line *expansion;
116 MMacro *next_active;
117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
120 unsigned int nparam, rotate;
121 int *paramlen;
122 uint64_t unique;
123 int lineno; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
129 struct Context {
130 Context *next;
131 struct hash_table *localmac;
132 char *name;
133 uint32_t number;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
155 enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
161 TOK_SMAC_PARAM /* MUST BE LAST IN THE LIST!!! */
164 struct Token {
165 Token *next;
166 char *text;
167 SMacro *mac; /* associated macro for TOK_SMAC_END */
168 enum pp_token_type type;
172 * Multi-line macro definitions are stored as a linked list of
173 * these, which is essentially a container to allow several linked
174 * lists of Tokens.
176 * Note that in this module, linked lists are treated as stacks
177 * wherever possible. For this reason, Lines are _pushed_ on to the
178 * `expansion' field in MMacro structures, so that the linked list,
179 * if walked, would give the macro lines in reverse order; this
180 * means that we can walk the list when expanding a macro, and thus
181 * push the lines on to the `expansion' field in _istk_ in reverse
182 * order (so that when popped back off they are in the right
183 * order). It may seem cockeyed, and it relies on my design having
184 * an even number of steps in, but it works...
186 * Some of these structures, rather than being actual lines, are
187 * markers delimiting the end of the expansion of a given macro.
188 * This is for use in the cycle-tracking and %rep-handling code.
189 * Such structures have `finishes' non-NULL, and `first' NULL. All
190 * others have `finishes' NULL, but `first' may still be NULL if
191 * the line is blank.
193 struct Line {
194 Line *next;
195 MMacro *finishes;
196 Token *first;
200 * To handle an arbitrary level of file inclusion, we maintain a
201 * stack (ie linked list) of these things.
203 struct Include {
204 Include *next;
205 FILE *fp;
206 Cond *conds;
207 Line *expansion;
208 char *fname;
209 int lineno, lineinc;
210 MMacro *mstk; /* stack of active macros/reps */
214 * Include search path. This is simply a list of strings which get
215 * prepended, in turn, to the name of an include file, in an
216 * attempt to find the file if it's not in the current directory.
218 struct IncPath {
219 IncPath *next;
220 char *path;
224 * Conditional assembly: we maintain a separate stack of these for
225 * each level of file inclusion. (The only reason we keep the
226 * stacks separate is to ensure that a stray `%endif' in a file
227 * included from within the true branch of a `%if' won't terminate
228 * it and cause confusion: instead, rightly, it'll cause an error.)
230 struct Cond {
231 Cond *next;
232 int state;
234 enum {
236 * These states are for use just after %if or %elif: IF_TRUE
237 * means the condition has evaluated to truth so we are
238 * currently emitting, whereas IF_FALSE means we are not
239 * currently emitting but will start doing so if a %else comes
240 * up. In these states, all directives are admissible: %elif,
241 * %else and %endif. (And of course %if.)
243 COND_IF_TRUE, COND_IF_FALSE,
245 * These states come up after a %else: ELSE_TRUE means we're
246 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
247 * any %elif or %else will cause an error.
249 COND_ELSE_TRUE, COND_ELSE_FALSE,
251 * This state means that we're not emitting now, and also that
252 * nothing until %endif will be emitted at all. It's for use in
253 * two circumstances: (i) when we've had our moment of emission
254 * and have now started seeing %elifs, and (ii) when the
255 * condition construct in question is contained within a
256 * non-emitting branch of a larger condition construct.
258 COND_NEVER
260 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
263 * These defines are used as the possible return values for do_directive
265 #define NO_DIRECTIVE_FOUND 0
266 #define DIRECTIVE_FOUND 1
269 * Condition codes. Note that we use c_ prefix not C_ because C_ is
270 * used in nasm.h for the "real" condition codes. At _this_ level,
271 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
272 * ones, so we need a different enum...
274 static const char * const conditions[] = {
275 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
276 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
277 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
279 enum pp_conds {
280 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
281 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
282 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
283 c_none = -1
285 static const enum pp_conds inverse_ccs[] = {
286 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
287 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,
288 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
292 * Directive names.
294 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
295 static int is_condition(enum preproc_token arg)
297 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
300 /* For TASM compatibility we need to be able to recognise TASM compatible
301 * conditional compilation directives. Using the NASM pre-processor does
302 * not work, so we look for them specifically from the following list and
303 * then jam in the equivalent NASM directive into the input stream.
306 enum {
307 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
308 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
311 static const char * const tasm_directives[] = {
312 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
313 "ifndef", "include", "local"
316 static int StackSize = 4;
317 static char *StackPointer = "ebp";
318 static int ArgOffset = 8;
319 static int LocalOffset = 0;
321 static Context *cstk;
322 static Include *istk;
323 static IncPath *ipath = NULL;
325 static efunc _error; /* Pointer to client-provided error reporting function */
326 static evalfunc evaluate;
328 static int pass; /* HACK: pass 0 = generate dependencies only */
330 static uint64_t unique; /* unique identifier numbers */
332 static Line *predef = NULL;
334 static ListGen *list;
337 * The current set of multi-line macros we have defined.
339 static struct hash_table *mmacros;
342 * The current set of single-line macros we have defined.
344 static struct hash_table *smacros;
347 * The multi-line macro we are currently defining, or the %rep
348 * block we are currently reading, if any.
350 static MMacro *defining;
353 * The number of macro parameters to allocate space for at a time.
355 #define PARAM_DELTA 16
358 * The standard macro set: defined in macros.c in the array nasm_stdmac.
359 * This gives our position in the macro set, when we're processing it.
361 static const char * const *stdmacpos;
364 * The extra standard macros that come from the object format, if
365 * any.
367 static const char * const *extrastdmac = NULL;
368 bool any_extrastdmac;
371 * Tokens are allocated in blocks to improve speed
373 #define TOKEN_BLOCKSIZE 4096
374 static Token *freeTokens = NULL;
375 struct Blocks {
376 Blocks *next;
377 void *chunk;
380 static Blocks blocks = { NULL, NULL };
383 * Forward declarations.
385 static Token *expand_mmac_params(Token * tline);
386 static Token *expand_smacro(Token * tline);
387 static Token *expand_id(Token * tline);
388 static Context *get_ctx(char *name, bool all_contexts);
389 static void make_tok_num(Token * tok, int64_t val);
390 static void error(int severity, const char *fmt, ...);
391 static void *new_Block(size_t size);
392 static void delete_Blocks(void);
393 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
394 static Token *delete_Token(Token * t);
397 * Macros for safe checking of token pointers, avoid *(NULL)
399 #define tok_type_(x,t) ((x) && (x)->type == (t))
400 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
404 /* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
409 static char *check_tasm_directive(char *line)
411 int32_t i, j, k, m, len;
412 char *p = line, *oldline, oldchar;
414 /* Skip whitespace */
415 while (isspace(*p) && *p != 0)
416 p++;
418 /* Binary search for the directive name */
419 i = -1;
420 j = elements(tasm_directives);
421 len = 0;
422 while (!isspace(p[len]) && p[len] != 0)
423 len++;
424 if (len) {
425 oldchar = p[len];
426 p[len] = 0;
427 while (j - i > 1) {
428 k = (j + i) / 2;
429 m = nasm_stricmp(p, tasm_directives[k]);
430 if (m == 0) {
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
434 p[len] = oldchar;
435 len = strlen(p);
436 oldline = line;
437 line = nasm_malloc(len + 2);
438 line[0] = '%';
439 if (k == TM_IFDIFI) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
443 * package.
445 strcpy(line + 1, "ifdef BOGUS");
446 } else {
447 memcpy(line + 1, p, len + 1);
449 nasm_free(oldline);
450 return line;
451 } else if (m < 0) {
452 j = k;
453 } else
454 i = k;
456 p[len] = oldchar;
458 return line;
462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
465 * lineno file').
467 static char *prepreproc(char *line)
469 int lineno, fnlen;
470 char *fname, *oldline;
472 if (line[0] == '#' && line[1] == ' ') {
473 oldline = line;
474 fname = oldline + 2;
475 lineno = atoi(fname);
476 fname += strspn(fname, "0123456789 ");
477 if (*fname == '"')
478 fname++;
479 fnlen = strcspn(fname, "\"");
480 line = nasm_malloc(20 + fnlen);
481 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
482 nasm_free(oldline);
484 if (tasm_compatible_mode)
485 return check_tasm_directive(line);
486 return line;
490 * Free a linked list of tokens.
492 static void free_tlist(Token * list)
494 while (list) {
495 list = delete_Token(list);
500 * Free a linked list of lines.
502 static void free_llist(Line * list)
504 Line *l;
505 while (list) {
506 l = list;
507 list = list->next;
508 free_tlist(l->first);
509 nasm_free(l);
514 * Free an MMacro
516 static void free_mmacro(MMacro * m)
518 nasm_free(m->name);
519 free_tlist(m->dlist);
520 nasm_free(m->defaults);
521 free_llist(m->expansion);
522 nasm_free(m);
526 * Free all currently defined macros, and free the hash tables
528 static void free_smacro_table(struct hash_table *smt)
530 SMacro *s;
531 const char *key;
532 struct hash_tbl_node *it = NULL;
534 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
535 nasm_free((void *)key);
536 while (s) {
537 SMacro *ns = s->next;
538 nasm_free(s->name);
539 free_tlist(s->expansion);
540 nasm_free(s);
541 s = ns;
544 hash_free(smt);
547 static void free_mmacro_table(struct hash_table *mmt)
549 MMacro *m;
550 const char *key;
551 struct hash_tbl_node *it = NULL;
553 it = NULL;
554 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
555 nasm_free((void *)key);
556 while (m) {
557 MMacro *nm = m->next;
558 free_mmacro(m);
559 m = nm;
562 hash_free(mmt);
565 static void free_macros(void)
567 free_smacro_table(smacros);
568 free_mmacro_table(mmacros);
572 * Initialize the hash tables
574 static void init_macros(void)
576 smacros = hash_init(HASH_LARGE);
577 mmacros = hash_init(HASH_LARGE);
581 * Pop the context stack.
583 static void ctx_pop(void)
585 Context *c = cstk;
587 cstk = cstk->next;
588 free_smacro_table(c->localmac);
589 nasm_free(c->name);
590 nasm_free(c);
594 * Search for a key in the hash index; adding it if necessary
595 * (in which case we initialize the data pointer to NULL.)
597 static void **
598 hash_findi_add(struct hash_table *hash, const char *str)
600 struct hash_insert hi;
601 void **r;
602 char *strx;
604 r = hash_findi(hash, str, &hi);
605 if (r)
606 return r;
608 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
609 return hash_add(&hi, strx, NULL);
613 * Like hash_findi, but returns the data element rather than a pointer
614 * to it. Used only when not adding a new element, hence no third
615 * argument.
617 static void *
618 hash_findix(struct hash_table *hash, const char *str)
620 void **p;
622 p = hash_findi(hash, str, NULL);
623 return p ? *p : NULL;
626 #define BUF_DELTA 512
628 * Read a line from the top file in istk, handling multiple CR/LFs
629 * at the end of the line read, and handling spurious ^Zs. Will
630 * return lines from the standard macro set if this has not already
631 * been done.
633 static char *read_line(void)
635 char *buffer, *p, *q;
636 int bufsize, continued_count;
638 if (stdmacpos) {
639 if (*stdmacpos) {
640 char *ret = nasm_strdup(*stdmacpos++);
641 if (!*stdmacpos && any_extrastdmac) {
642 stdmacpos = extrastdmac;
643 any_extrastdmac = false;
644 return ret;
647 * Nasty hack: here we push the contents of `predef' on
648 * to the top-level expansion stack, since this is the
649 * most convenient way to implement the pre-include and
650 * pre-define features.
652 if (!*stdmacpos) {
653 Line *pd, *l;
654 Token *head, **tail, *t;
656 for (pd = predef; pd; pd = pd->next) {
657 head = NULL;
658 tail = &head;
659 for (t = pd->first; t; t = t->next) {
660 *tail = new_Token(NULL, t->type, t->text, 0);
661 tail = &(*tail)->next;
663 l = nasm_malloc(sizeof(Line));
664 l->next = istk->expansion;
665 l->first = head;
666 l->finishes = false;
667 istk->expansion = l;
670 return ret;
671 } else {
672 stdmacpos = NULL;
676 bufsize = BUF_DELTA;
677 buffer = nasm_malloc(BUF_DELTA);
678 p = buffer;
679 continued_count = 0;
680 while (1) {
681 q = fgets(p, bufsize - (p - buffer), istk->fp);
682 if (!q)
683 break;
684 p += strlen(p);
685 if (p > buffer && p[-1] == '\n') {
686 /* Convert backslash-CRLF line continuation sequences into
687 nothing at all (for DOS and Windows) */
688 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
689 p -= 3;
690 *p = 0;
691 continued_count++;
693 /* Also convert backslash-LF line continuation sequences into
694 nothing at all (for Unix) */
695 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
696 p -= 2;
697 *p = 0;
698 continued_count++;
699 } else {
700 break;
703 if (p - buffer > bufsize - 10) {
704 int32_t offset = p - buffer;
705 bufsize += BUF_DELTA;
706 buffer = nasm_realloc(buffer, bufsize);
707 p = buffer + offset; /* prevent stale-pointer problems */
711 if (!q && p == buffer) {
712 nasm_free(buffer);
713 return NULL;
716 src_set_linnum(src_get_linnum() + istk->lineinc +
717 (continued_count * istk->lineinc));
720 * Play safe: remove CRs as well as LFs, if any of either are
721 * present at the end of the line.
723 while (--p >= buffer && (*p == '\n' || *p == '\r'))
724 *p = '\0';
727 * Handle spurious ^Z, which may be inserted into source files
728 * by some file transfer utilities.
730 buffer[strcspn(buffer, "\032")] = '\0';
732 list->line(LIST_READ, buffer);
734 return buffer;
738 * Tokenize a line of text. This is a very simple process since we
739 * don't need to parse the value out of e.g. numeric tokens: we
740 * simply split one string into many.
742 static Token *tokenize(char *line)
744 char *p = line;
745 enum pp_token_type type;
746 Token *list = NULL;
747 Token *t, **tail = &list;
749 while (*line) {
750 p = line;
751 if (*p == '%') {
752 p++;
753 if (isdigit(*p) ||
754 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
755 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
756 do {
757 p++;
759 while (isdigit(*p));
760 type = TOK_PREPROC_ID;
761 } else if (*p == '{') {
762 p++;
763 while (*p && *p != '}') {
764 p[-1] = *p;
765 p++;
767 p[-1] = '\0';
768 if (*p)
769 p++;
770 type = TOK_PREPROC_ID;
771 } else if (*p == '?') {
772 type = TOK_PREPROC_Q; /* %? */
773 p++;
774 if (*p == '?') {
775 type = TOK_PREPROC_QQ; /* %?? */
776 p++;
778 } else if (isidchar(*p) ||
779 ((*p == '!' || *p == '%' || *p == '$') &&
780 isidchar(p[1]))) {
781 do {
782 p++;
784 while (isidchar(*p));
785 type = TOK_PREPROC_ID;
786 } else {
787 type = TOK_OTHER;
788 if (*p == '%')
789 p++;
791 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
792 type = TOK_ID;
793 p++;
794 while (*p && isidchar(*p))
795 p++;
796 } else if (*p == '\'' || *p == '"') {
798 * A string token.
800 char c = *p;
801 p++;
802 type = TOK_STRING;
803 while (*p && *p != c)
804 p++;
806 if (*p) {
807 p++;
808 } else {
809 error(ERR_WARNING, "unterminated string");
810 /* Handling unterminated strings by UNV */
811 /* type = -1; */
813 } else if (isnumstart(*p)) {
814 bool is_hex = false;
815 bool is_float = false;
816 bool has_e = false;
817 char c, *r;
820 * A numeric token.
823 if (*p == '$') {
824 p++;
825 is_hex = true;
828 for (;;) {
829 c = *p++;
831 if (!is_hex && (c == 'e' || c == 'E')) {
832 has_e = true;
833 if (*p == '+' || *p == '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
836 p++;
837 is_float = true;
839 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
840 is_hex = true;
841 } else if (c == 'P' || c == 'p') {
842 is_float = true;
843 if (*p == '+' || *p == '-')
844 p++;
845 } else if (isnumchar(c) || c == '_')
846 ; /* just advance */
847 else if (c == '.') {
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
856 r = p;
857 while (*r == '_')
858 r++;
860 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
861 (!is_hex && (*r == 'e' || *r == 'E')) ||
862 (*r == 'p' || *r == 'P')) {
863 p = r;
864 is_float = true;
865 } else
866 break; /* Terminate the token */
867 } else
868 break;
870 p--; /* Point to first character beyond number */
872 if (has_e && !is_hex) {
873 /* 1e13 is floating-point, but 1e13h is not */
874 is_float = true;
877 type = is_float ? TOK_FLOAT : TOK_NUMBER;
878 } else if (isspace(*p)) {
879 type = TOK_WHITESPACE;
880 p++;
881 while (*p && isspace(*p))
882 p++;
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
888 if (!*p || *p == ';') {
889 type = TOK_COMMENT;
890 while (*p)
891 p++;
893 } else if (*p == ';') {
894 type = TOK_COMMENT;
895 while (*p)
896 p++;
897 } else {
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
902 * else is a single-character operator.
904 type = TOK_OTHER;
905 if ((p[0] == '>' && p[1] == '>') ||
906 (p[0] == '<' && p[1] == '<') ||
907 (p[0] == '/' && p[1] == '/') ||
908 (p[0] == '<' && p[1] == '=') ||
909 (p[0] == '>' && p[1] == '=') ||
910 (p[0] == '=' && p[1] == '=') ||
911 (p[0] == '!' && p[1] == '=') ||
912 (p[0] == '<' && p[1] == '>') ||
913 (p[0] == '&' && p[1] == '&') ||
914 (p[0] == '|' && p[1] == '|') ||
915 (p[0] == '^' && p[1] == '^')) {
916 p++;
918 p++;
921 /* Handling unterminated string by UNV */
922 /*if (type == -1)
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
926 tail = &t->next;
928 else */
929 if (type != TOK_COMMENT) {
930 *tail = t = new_Token(NULL, type, line, p - line);
931 tail = &t->next;
933 line = p;
935 return list;
939 * this function allocates a new managed block of memory and
940 * returns a pointer to the block. The managed blocks are
941 * deleted only all at once by the delete_Blocks function.
943 static void *new_Block(size_t size)
945 Blocks *b = &blocks;
947 /* first, get to the end of the linked list */
948 while (b->next)
949 b = b->next;
950 /* now allocate the requested chunk */
951 b->chunk = nasm_malloc(size);
953 /* now allocate a new block for the next request */
954 b->next = nasm_malloc(sizeof(Blocks));
955 /* and initialize the contents of the new block */
956 b->next->next = NULL;
957 b->next->chunk = NULL;
958 return b->chunk;
962 * this function deletes all managed blocks of memory
964 static void delete_Blocks(void)
966 Blocks *a, *b = &blocks;
969 * keep in mind that the first block, pointed to by blocks
970 * is a static and not dynamically allocated, so we don't
971 * free it.
973 while (b) {
974 if (b->chunk)
975 nasm_free(b->chunk);
976 a = b;
977 b = b->next;
978 if (a != &blocks)
979 nasm_free(a);
984 * this function creates a new Token and passes a pointer to it
985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
988 static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
990 Token *t;
991 int i;
993 if (freeTokens == NULL) {
994 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
995 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
996 freeTokens[i].next = &freeTokens[i + 1];
997 freeTokens[i].next = NULL;
999 t = freeTokens;
1000 freeTokens = t->next;
1001 t->next = next;
1002 t->mac = NULL;
1003 t->type = type;
1004 if (type == TOK_WHITESPACE || text == NULL) {
1005 t->text = NULL;
1006 } else {
1007 if (txtlen == 0)
1008 txtlen = strlen(text);
1009 t->text = nasm_malloc(1 + txtlen);
1010 strncpy(t->text, text, txtlen);
1011 t->text[txtlen] = '\0';
1013 return t;
1016 static Token *delete_Token(Token * t)
1018 Token *next = t->next;
1019 nasm_free(t->text);
1020 t->next = freeTokens;
1021 freeTokens = t;
1022 return next;
1026 * Convert a line of tokens back into text.
1027 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1028 * will be transformed into ..@ctxnum.xxx
1030 static char *detoken(Token * tlist, int expand_locals)
1032 Token *t;
1033 int len;
1034 char *line, *p;
1035 const char *q;
1037 len = 0;
1038 for (t = tlist; t; t = t->next) {
1039 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1040 char *p = getenv(t->text + 2);
1041 nasm_free(t->text);
1042 if (p)
1043 t->text = nasm_strdup(p);
1044 else
1045 t->text = NULL;
1047 /* Expand local macros here and not during preprocessing */
1048 if (expand_locals &&
1049 t->type == TOK_PREPROC_ID && t->text &&
1050 t->text[0] == '%' && t->text[1] == '$') {
1051 Context *ctx = get_ctx(t->text, false);
1052 if (ctx) {
1053 char buffer[40];
1054 char *p, *q = t->text + 2;
1056 q += strspn(q, "$");
1057 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1058 p = nasm_strcat(buffer, q);
1059 nasm_free(t->text);
1060 t->text = p;
1063 if (t->type == TOK_WHITESPACE) {
1064 len++;
1065 } else if (t->text) {
1066 len += strlen(t->text);
1069 p = line = nasm_malloc(len + 1);
1070 for (t = tlist; t; t = t->next) {
1071 if (t->type == TOK_WHITESPACE) {
1072 *p++ = ' ';
1073 } else if (t->text) {
1074 q = t->text;
1075 while (*q)
1076 *p++ = *q++;
1079 *p = '\0';
1080 return line;
1084 * A scanner, suitable for use by the expression evaluator, which
1085 * operates on a line of Tokens. Expects a pointer to a pointer to
1086 * the first token in the line to be passed in as its private_data
1087 * field.
1089 * FIX: This really needs to be unified with stdscan.
1091 static int ppscan(void *private_data, struct tokenval *tokval)
1093 Token **tlineptr = private_data;
1094 Token *tline;
1095 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1097 do {
1098 tline = *tlineptr;
1099 *tlineptr = tline ? tline->next : NULL;
1101 while (tline && (tline->type == TOK_WHITESPACE ||
1102 tline->type == TOK_COMMENT));
1104 if (!tline)
1105 return tokval->t_type = TOKEN_EOS;
1107 tokval->t_charptr = tline->text;
1109 if (tline->text[0] == '$' && !tline->text[1])
1110 return tokval->t_type = TOKEN_HERE;
1111 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1112 return tokval->t_type = TOKEN_BASE;
1114 if (tline->type == TOK_ID) {
1115 p = tokval->t_charptr = tline->text;
1116 if (p[0] == '$') {
1117 tokval->t_charptr++;
1118 return tokval->t_type = TOKEN_ID;
1121 for (r = p, s = ourcopy; *r; r++) {
1122 if (r >= p+MAX_KEYWORD)
1123 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1124 *s++ = tolower(*r);
1126 *s = '\0';
1127 /* right, so we have an identifier sitting in temp storage. now,
1128 * is it actually a register or instruction name, or what? */
1129 return nasm_token_hash(ourcopy, tokval);
1132 if (tline->type == TOK_NUMBER) {
1133 bool rn_error;
1134 tokval->t_integer = readnum(tline->text, &rn_error);
1135 if (rn_error)
1136 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1137 tokval->t_charptr = tline->text;
1138 return tokval->t_type = TOKEN_NUM;
1141 if (tline->type == TOK_FLOAT) {
1142 return tokval->t_type = TOKEN_FLOAT;
1145 if (tline->type == TOK_STRING) {
1146 bool rn_warn;
1147 char q, *r;
1148 int l;
1150 r = tline->text;
1151 q = *r++;
1152 l = strlen(r);
1154 if (l == 0 || r[l - 1] != q)
1155 return tokval->t_type = TOKEN_ERRNUM;
1156 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1157 if (rn_warn)
1158 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1159 tokval->t_charptr = NULL;
1160 return tokval->t_type = TOKEN_NUM;
1163 if (tline->type == TOK_OTHER) {
1164 if (!strcmp(tline->text, "<<"))
1165 return tokval->t_type = TOKEN_SHL;
1166 if (!strcmp(tline->text, ">>"))
1167 return tokval->t_type = TOKEN_SHR;
1168 if (!strcmp(tline->text, "//"))
1169 return tokval->t_type = TOKEN_SDIV;
1170 if (!strcmp(tline->text, "%%"))
1171 return tokval->t_type = TOKEN_SMOD;
1172 if (!strcmp(tline->text, "=="))
1173 return tokval->t_type = TOKEN_EQ;
1174 if (!strcmp(tline->text, "<>"))
1175 return tokval->t_type = TOKEN_NE;
1176 if (!strcmp(tline->text, "!="))
1177 return tokval->t_type = TOKEN_NE;
1178 if (!strcmp(tline->text, "<="))
1179 return tokval->t_type = TOKEN_LE;
1180 if (!strcmp(tline->text, ">="))
1181 return tokval->t_type = TOKEN_GE;
1182 if (!strcmp(tline->text, "&&"))
1183 return tokval->t_type = TOKEN_DBL_AND;
1184 if (!strcmp(tline->text, "^^"))
1185 return tokval->t_type = TOKEN_DBL_XOR;
1186 if (!strcmp(tline->text, "||"))
1187 return tokval->t_type = TOKEN_DBL_OR;
1191 * We have no other options: just return the first character of
1192 * the token text.
1194 return tokval->t_type = tline->text[0];
1198 * Compare a string to the name of an existing macro; this is a
1199 * simple wrapper which calls either strcmp or nasm_stricmp
1200 * depending on the value of the `casesense' parameter.
1202 static int mstrcmp(const char *p, const char *q, bool casesense)
1204 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1208 * Return the Context structure associated with a %$ token. Return
1209 * NULL, having _already_ reported an error condition, if the
1210 * context stack isn't deep enough for the supplied number of $
1211 * signs.
1212 * If all_contexts == true, contexts that enclose current are
1213 * also scanned for such smacro, until it is found; if not -
1214 * only the context that directly results from the number of $'s
1215 * in variable's name.
1217 static Context *get_ctx(char *name, bool all_contexts)
1219 Context *ctx;
1220 SMacro *m;
1221 int i;
1223 if (!name || name[0] != '%' || name[1] != '$')
1224 return NULL;
1226 if (!cstk) {
1227 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1228 return NULL;
1231 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1232 ctx = ctx->next;
1233 /* i--; Lino - 02/25/02 */
1235 if (!ctx) {
1236 error(ERR_NONFATAL, "`%s': context stack is only"
1237 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1238 return NULL;
1240 if (!all_contexts)
1241 return ctx;
1243 do {
1244 /* Search for this smacro in found context */
1245 m = hash_findix(ctx->localmac, name);
1246 while (m) {
1247 if (!mstrcmp(m->name, name, m->casesense))
1248 return ctx;
1249 m = m->next;
1251 ctx = ctx->next;
1253 while (ctx);
1254 return NULL;
1258 * Open an include file. This routine must always return a valid
1259 * file pointer if it returns - it's responsible for throwing an
1260 * ERR_FATAL and bombing out completely if not. It should also try
1261 * the include path one by one until it finds the file or reaches
1262 * the end of the path.
1264 static FILE *inc_fopen(char *file)
1266 FILE *fp;
1267 char *prefix = "", *combine;
1268 IncPath *ip = ipath;
1269 static int namelen = 0;
1270 int len = strlen(file);
1272 while (1) {
1273 combine = nasm_malloc(strlen(prefix) + len + 1);
1274 strcpy(combine, prefix);
1275 strcat(combine, file);
1276 fp = fopen(combine, "r");
1277 if (pass == 0 && fp) {
1278 namelen += strlen(combine) + 1;
1279 if (namelen > 62) {
1280 printf(" \\\n ");
1281 namelen = 2;
1283 printf(" %s", combine);
1285 nasm_free(combine);
1286 if (fp)
1287 return fp;
1288 if (!ip)
1289 break;
1290 prefix = ip->path;
1291 ip = ip->next;
1293 if (!prefix) {
1294 /* -MG given and file not found */
1295 if (pass == 0) {
1296 namelen += strlen(file) + 1;
1297 if (namelen > 62) {
1298 printf(" \\\n ");
1299 namelen = 2;
1301 printf(" %s", file);
1303 return NULL;
1307 error(ERR_FATAL, "unable to open include file `%s'", file);
1308 return NULL; /* never reached - placate compilers */
1312 * Determine if we should warn on defining a single-line macro of
1313 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1314 * return true if _any_ single-line macro of that name is defined.
1315 * Otherwise, will return true if a single-line macro with either
1316 * `nparam' or no parameters is defined.
1318 * If a macro with precisely the right number of parameters is
1319 * defined, or nparam is -1, the address of the definition structure
1320 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1321 * is NULL, no action will be taken regarding its contents, and no
1322 * error will occur.
1324 * Note that this is also called with nparam zero to resolve
1325 * `ifdef'.
1327 * If you already know which context macro belongs to, you can pass
1328 * the context pointer as first parameter; if you won't but name begins
1329 * with %$ the context will be automatically computed. If all_contexts
1330 * is true, macro will be searched in outer contexts as well.
1332 static bool
1333 smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1334 bool nocase)
1336 SMacro *m;
1338 if (ctx) {
1339 m = (SMacro *) hash_findix(ctx->localmac, name);
1340 } else if (name[0] == '%' && name[1] == '$') {
1341 if (cstk)
1342 ctx = get_ctx(name, false);
1343 if (!ctx)
1344 return false; /* got to return _something_ */
1345 m = (SMacro *) hash_findix(ctx->localmac, name);
1346 } else {
1347 m = (SMacro *) hash_findix(smacros, name);
1350 while (m) {
1351 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1352 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1353 if (defn) {
1354 if (nparam == (int) m->nparam || nparam == -1)
1355 *defn = m;
1356 else
1357 *defn = NULL;
1359 return true;
1361 m = m->next;
1364 return false;
1368 * Count and mark off the parameters in a multi-line macro call.
1369 * This is called both from within the multi-line macro expansion
1370 * code, and also to mark off the default parameters when provided
1371 * in a %macro definition line.
1373 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1375 int paramsize, brace;
1377 *nparam = paramsize = 0;
1378 *params = NULL;
1379 while (t) {
1380 if (*nparam >= paramsize) {
1381 paramsize += PARAM_DELTA;
1382 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1384 skip_white_(t);
1385 brace = false;
1386 if (tok_is_(t, "{"))
1387 brace = true;
1388 (*params)[(*nparam)++] = t;
1389 while (tok_isnt_(t, brace ? "}" : ","))
1390 t = t->next;
1391 if (t) { /* got a comma/brace */
1392 t = t->next;
1393 if (brace) {
1395 * Now we've found the closing brace, look further
1396 * for the comma.
1398 skip_white_(t);
1399 if (tok_isnt_(t, ",")) {
1400 error(ERR_NONFATAL,
1401 "braces do not enclose all of macro parameter");
1402 while (tok_isnt_(t, ","))
1403 t = t->next;
1405 if (t)
1406 t = t->next; /* eat the comma */
1413 * Determine whether one of the various `if' conditions is true or
1414 * not.
1416 * We must free the tline we get passed.
1418 static bool if_condition(Token * tline, enum preproc_token ct)
1420 enum pp_conditional i = PP_COND(ct);
1421 bool j;
1422 Token *t, *tt, **tptr, *origline;
1423 struct tokenval tokval;
1424 expr *evalresult;
1425 enum pp_token_type needtype;
1427 origline = tline;
1429 switch (i) {
1430 case PPC_IFCTX:
1431 j = false; /* have we matched yet? */
1432 while (cstk && tline) {
1433 skip_white_(tline);
1434 if (!tline || tline->type != TOK_ID) {
1435 error(ERR_NONFATAL,
1436 "`%s' expects context identifiers", pp_directives[ct]);
1437 free_tlist(origline);
1438 return -1;
1440 if (!nasm_stricmp(tline->text, cstk->name))
1441 j = true;
1442 tline = tline->next;
1444 break;
1446 case PPC_IFDEF:
1447 j = false; /* have we matched yet? */
1448 while (tline) {
1449 skip_white_(tline);
1450 if (!tline || (tline->type != TOK_ID &&
1451 (tline->type != TOK_PREPROC_ID ||
1452 tline->text[1] != '$'))) {
1453 error(ERR_NONFATAL,
1454 "`%s' expects macro identifiers", pp_directives[ct]);
1455 goto fail;
1457 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1458 j = true;
1459 tline = tline->next;
1461 break;
1463 case PPC_IFIDN:
1464 case PPC_IFIDNI:
1465 tline = expand_smacro(tline);
1466 t = tt = tline;
1467 while (tok_isnt_(tt, ","))
1468 tt = tt->next;
1469 if (!tt) {
1470 error(ERR_NONFATAL,
1471 "`%s' expects two comma-separated arguments",
1472 pp_directives[ct]);
1473 goto fail;
1475 tt = tt->next;
1476 j = true; /* assume equality unless proved not */
1477 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1478 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1479 error(ERR_NONFATAL, "`%s': more than one comma on line",
1480 pp_directives[ct]);
1481 goto fail;
1483 if (t->type == TOK_WHITESPACE) {
1484 t = t->next;
1485 continue;
1487 if (tt->type == TOK_WHITESPACE) {
1488 tt = tt->next;
1489 continue;
1491 if (tt->type != t->type) {
1492 j = false; /* found mismatching tokens */
1493 break;
1495 /* Unify surrounding quotes for strings */
1496 if (t->type == TOK_STRING) {
1497 tt->text[0] = t->text[0];
1498 tt->text[strlen(tt->text) - 1] = t->text[0];
1500 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1501 j = false; /* found mismatching tokens */
1502 break;
1505 t = t->next;
1506 tt = tt->next;
1508 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1509 j = false; /* trailing gunk on one end or other */
1510 break;
1512 case PPC_IFMACRO:
1514 bool found = false;
1515 MMacro searching, *mmac;
1517 tline = tline->next;
1518 skip_white_(tline);
1519 tline = expand_id(tline);
1520 if (!tok_type_(tline, TOK_ID)) {
1521 error(ERR_NONFATAL,
1522 "`%s' expects a macro name", pp_directives[ct]);
1523 goto fail;
1525 searching.name = nasm_strdup(tline->text);
1526 searching.casesense = true;
1527 searching.plus = false;
1528 searching.nolist = false;
1529 searching.in_progress = 0;
1530 searching.rep_nest = NULL;
1531 searching.nparam_min = 0;
1532 searching.nparam_max = INT_MAX;
1533 tline = expand_smacro(tline->next);
1534 skip_white_(tline);
1535 if (!tline) {
1536 } else if (!tok_type_(tline, TOK_NUMBER)) {
1537 error(ERR_NONFATAL,
1538 "`%s' expects a parameter count or nothing",
1539 pp_directives[ct]);
1540 } else {
1541 searching.nparam_min = searching.nparam_max =
1542 readnum(tline->text, &j);
1543 if (j)
1544 error(ERR_NONFATAL,
1545 "unable to parse parameter count `%s'",
1546 tline->text);
1548 if (tline && tok_is_(tline->next, "-")) {
1549 tline = tline->next->next;
1550 if (tok_is_(tline, "*"))
1551 searching.nparam_max = INT_MAX;
1552 else if (!tok_type_(tline, TOK_NUMBER))
1553 error(ERR_NONFATAL,
1554 "`%s' expects a parameter count after `-'",
1555 pp_directives[ct]);
1556 else {
1557 searching.nparam_max = readnum(tline->text, &j);
1558 if (j)
1559 error(ERR_NONFATAL,
1560 "unable to parse parameter count `%s'",
1561 tline->text);
1562 if (searching.nparam_min > searching.nparam_max)
1563 error(ERR_NONFATAL,
1564 "minimum parameter count exceeds maximum");
1567 if (tline && tok_is_(tline->next, "+")) {
1568 tline = tline->next;
1569 searching.plus = true;
1571 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1572 while (mmac) {
1573 if (!strcmp(mmac->name, searching.name) &&
1574 (mmac->nparam_min <= searching.nparam_max
1575 || searching.plus)
1576 && (searching.nparam_min <= mmac->nparam_max
1577 || mmac->plus)) {
1578 found = true;
1579 break;
1581 mmac = mmac->next;
1583 nasm_free(searching.name);
1584 j = found;
1585 break;
1588 case PPC_IFID:
1589 needtype = TOK_ID;
1590 goto iftype;
1591 case PPC_IFNUM:
1592 needtype = TOK_NUMBER;
1593 goto iftype;
1594 case PPC_IFSTR:
1595 needtype = TOK_STRING;
1596 goto iftype;
1598 iftype:
1599 t = tline = expand_smacro(tline);
1601 while (tok_type_(t, TOK_WHITESPACE) ||
1602 (needtype == TOK_NUMBER &&
1603 tok_type_(t, TOK_OTHER) &&
1604 (t->text[0] == '-' || t->text[0] == '+') &&
1605 !t->text[1]))
1606 t = t->next;
1608 j = tok_type_(t, needtype);
1609 break;
1611 case PPC_IFTOKEN:
1612 t = tline = expand_smacro(tline);
1613 while (tok_type_(t, TOK_WHITESPACE))
1614 t = t->next;
1616 j = false;
1617 if (t) {
1618 t = t->next; /* Skip the actual token */
1619 while (tok_type_(t, TOK_WHITESPACE))
1620 t = t->next;
1621 j = !t; /* Should be nothing left */
1623 break;
1625 case PPC_IFEMPTY:
1626 t = tline = expand_smacro(tline);
1627 while (tok_type_(t, TOK_WHITESPACE))
1628 t = t->next;
1630 j = !t; /* Should be empty */
1631 break;
1633 case PPC_IF:
1634 t = tline = expand_smacro(tline);
1635 tptr = &t;
1636 tokval.t_type = TOKEN_INVALID;
1637 evalresult = evaluate(ppscan, tptr, &tokval,
1638 NULL, pass | CRITICAL, error, NULL);
1639 if (!evalresult)
1640 return -1;
1641 if (tokval.t_type)
1642 error(ERR_WARNING,
1643 "trailing garbage after expression ignored");
1644 if (!is_simple(evalresult)) {
1645 error(ERR_NONFATAL,
1646 "non-constant value given to `%s'", pp_directives[ct]);
1647 goto fail;
1649 j = reloc_value(evalresult) != 0;
1650 return j;
1652 default:
1653 error(ERR_FATAL,
1654 "preprocessor directive `%s' not yet implemented",
1655 pp_directives[ct]);
1656 goto fail;
1659 free_tlist(origline);
1660 return j ^ PP_NEGATIVE(ct);
1662 fail:
1663 free_tlist(origline);
1664 return -1;
1668 * Expand macros in a string. Used in %error and %include directives.
1669 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1670 * The returned variable should ALWAYS be freed after usage.
1672 void expand_macros_in_string(char **p)
1674 Token *line = tokenize(*p);
1675 line = expand_smacro(line);
1676 *p = detoken(line, false);
1680 * Common code for defining an smacro
1682 static bool define_smacro(Context *ctx, char *mname, bool casesense,
1683 int nparam, Token *expansion)
1685 SMacro *smac, **smhead;
1687 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1688 if (!smac) {
1689 error(ERR_WARNING,
1690 "single-line macro `%s' defined both with and"
1691 " without parameters", mname);
1693 /* Some instances of the old code considered this a failure,
1694 some others didn't. What is the right thing to do here? */
1695 free_tlist(expansion);
1696 return false; /* Failure */
1697 } else {
1699 * We're redefining, so we have to take over an
1700 * existing SMacro structure. This means freeing
1701 * what was already in it.
1703 nasm_free(smac->name);
1704 free_tlist(smac->expansion);
1706 } else {
1707 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1708 mname);
1709 smac = nasm_malloc(sizeof(SMacro));
1710 smac->next = *smhead;
1711 *smhead = smac;
1713 smac->name = nasm_strdup(mname);
1714 smac->casesense = casesense;
1715 smac->nparam = nparam;
1716 smac->expansion = expansion;
1717 smac->in_progress = false;
1718 return true; /* Success */
1722 * Undefine an smacro
1724 static void undef_smacro(Context *ctx, const char *mname)
1726 SMacro **smhead, *s, **sp;
1728 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
1730 if (smhead) {
1732 * We now have a macro name... go hunt for it.
1734 sp = smhead;
1735 while ((s = *sp) != NULL) {
1736 if (!mstrcmp(s->name, mname, s->casesense)) {
1737 *sp = s->next;
1738 nasm_free(s->name);
1739 free_tlist(s->expansion);
1740 nasm_free(s);
1741 } else {
1742 sp = &s->next;
1749 * Decode a size directive
1751 static int parse_size(const char *str) {
1752 static const char *size_names[] =
1753 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1754 static const int sizes[] =
1755 { 0, 1, 4, 16, 8, 10, 2, 32 };
1757 return sizes[bsii(str, size_names, elements(size_names))+1];
1761 * find and process preprocessor directive in passed line
1762 * Find out if a line contains a preprocessor directive, and deal
1763 * with it if so.
1765 * If a directive _is_ found, it is the responsibility of this routine
1766 * (and not the caller) to free_tlist() the line.
1768 * @param tline a pointer to the current tokeninzed line linked list
1769 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1772 static int do_directive(Token * tline)
1774 enum preproc_token i;
1775 int j;
1776 bool err;
1777 int nparam;
1778 bool nolist;
1779 bool casesense;
1780 int k, m;
1781 int offset;
1782 char *p, *mname;
1783 Include *inc;
1784 Context *ctx;
1785 Cond *cond;
1786 MMacro *mmac, **mmhead;
1787 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1788 Line *l;
1789 struct tokenval tokval;
1790 expr *evalresult;
1791 MMacro *tmp_defining; /* Used when manipulating rep_nest */
1792 int64_t count;
1794 origline = tline;
1796 skip_white_(tline);
1797 if (!tok_type_(tline, TOK_PREPROC_ID) ||
1798 (tline->text[1] == '%' || tline->text[1] == '$'
1799 || tline->text[1] == '!'))
1800 return NO_DIRECTIVE_FOUND;
1802 i = pp_token_hash(tline->text);
1805 * If we're in a non-emitting branch of a condition construct,
1806 * or walking to the end of an already terminated %rep block,
1807 * we should ignore all directives except for condition
1808 * directives.
1810 if (((istk->conds && !emitting(istk->conds->state)) ||
1811 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1812 return NO_DIRECTIVE_FOUND;
1816 * If we're defining a macro or reading a %rep block, we should
1817 * ignore all directives except for %macro/%imacro (which
1818 * generate an error), %endm/%endmacro, and (only if we're in a
1819 * %rep block) %endrep. If we're in a %rep block, another %rep
1820 * causes an error, so should be let through.
1822 if (defining && i != PP_MACRO && i != PP_IMACRO &&
1823 i != PP_ENDMACRO && i != PP_ENDM &&
1824 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1825 return NO_DIRECTIVE_FOUND;
1828 switch (i) {
1829 case PP_INVALID:
1830 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1831 tline->text);
1832 return NO_DIRECTIVE_FOUND; /* didn't get it */
1834 case PP_STACKSIZE:
1835 /* Directive to tell NASM what the default stack size is. The
1836 * default is for a 16-bit stack, and this can be overriden with
1837 * %stacksize large.
1838 * the following form:
1840 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1842 tline = tline->next;
1843 if (tline && tline->type == TOK_WHITESPACE)
1844 tline = tline->next;
1845 if (!tline || tline->type != TOK_ID) {
1846 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1847 free_tlist(origline);
1848 return DIRECTIVE_FOUND;
1850 if (nasm_stricmp(tline->text, "flat") == 0) {
1851 /* All subsequent ARG directives are for a 32-bit stack */
1852 StackSize = 4;
1853 StackPointer = "ebp";
1854 ArgOffset = 8;
1855 LocalOffset = 0;
1856 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1857 /* All subsequent ARG directives are for a 64-bit stack */
1858 StackSize = 8;
1859 StackPointer = "rbp";
1860 ArgOffset = 8;
1861 LocalOffset = 0;
1862 } else if (nasm_stricmp(tline->text, "large") == 0) {
1863 /* All subsequent ARG directives are for a 16-bit stack,
1864 * far function call.
1866 StackSize = 2;
1867 StackPointer = "bp";
1868 ArgOffset = 4;
1869 LocalOffset = 0;
1870 } else if (nasm_stricmp(tline->text, "small") == 0) {
1871 /* All subsequent ARG directives are for a 16-bit stack,
1872 * far function call. We don't support near functions.
1874 StackSize = 2;
1875 StackPointer = "bp";
1876 ArgOffset = 6;
1877 LocalOffset = 0;
1878 } else {
1879 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1880 free_tlist(origline);
1881 return DIRECTIVE_FOUND;
1883 free_tlist(origline);
1884 return DIRECTIVE_FOUND;
1886 case PP_ARG:
1887 /* TASM like ARG directive to define arguments to functions, in
1888 * the following form:
1890 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1892 offset = ArgOffset;
1893 do {
1894 char *arg, directive[256];
1895 int size = StackSize;
1897 /* Find the argument name */
1898 tline = tline->next;
1899 if (tline && tline->type == TOK_WHITESPACE)
1900 tline = tline->next;
1901 if (!tline || tline->type != TOK_ID) {
1902 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1903 free_tlist(origline);
1904 return DIRECTIVE_FOUND;
1906 arg = tline->text;
1908 /* Find the argument size type */
1909 tline = tline->next;
1910 if (!tline || tline->type != TOK_OTHER
1911 || tline->text[0] != ':') {
1912 error(ERR_NONFATAL,
1913 "Syntax error processing `%%arg' directive");
1914 free_tlist(origline);
1915 return DIRECTIVE_FOUND;
1917 tline = tline->next;
1918 if (!tline || tline->type != TOK_ID) {
1919 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1920 free_tlist(origline);
1921 return DIRECTIVE_FOUND;
1924 /* Allow macro expansion of type parameter */
1925 tt = tokenize(tline->text);
1926 tt = expand_smacro(tt);
1927 size = parse_size(tt->text);
1928 if (!size) {
1929 error(ERR_NONFATAL,
1930 "Invalid size type for `%%arg' missing directive");
1931 free_tlist(tt);
1932 free_tlist(origline);
1933 return DIRECTIVE_FOUND;
1935 free_tlist(tt);
1937 /* Round up to even stack slots */
1938 size = (size+StackSize-1) & ~(StackSize-1);
1940 /* Now define the macro for the argument */
1941 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1942 arg, StackPointer, offset);
1943 do_directive(tokenize(directive));
1944 offset += size;
1946 /* Move to the next argument in the list */
1947 tline = tline->next;
1948 if (tline && tline->type == TOK_WHITESPACE)
1949 tline = tline->next;
1950 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1951 ArgOffset = offset;
1952 free_tlist(origline);
1953 return DIRECTIVE_FOUND;
1955 case PP_LOCAL:
1956 /* TASM like LOCAL directive to define local variables for a
1957 * function, in the following form:
1959 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1961 * The '= LocalSize' at the end is ignored by NASM, but is
1962 * required by TASM to define the local parameter size (and used
1963 * by the TASM macro package).
1965 offset = LocalOffset;
1966 do {
1967 char *local, directive[256];
1968 int size = StackSize;
1970 /* Find the argument name */
1971 tline = tline->next;
1972 if (tline && tline->type == TOK_WHITESPACE)
1973 tline = tline->next;
1974 if (!tline || tline->type != TOK_ID) {
1975 error(ERR_NONFATAL,
1976 "`%%local' missing argument parameter");
1977 free_tlist(origline);
1978 return DIRECTIVE_FOUND;
1980 local = tline->text;
1982 /* Find the argument size type */
1983 tline = tline->next;
1984 if (!tline || tline->type != TOK_OTHER
1985 || tline->text[0] != ':') {
1986 error(ERR_NONFATAL,
1987 "Syntax error processing `%%local' directive");
1988 free_tlist(origline);
1989 return DIRECTIVE_FOUND;
1991 tline = tline->next;
1992 if (!tline || tline->type != TOK_ID) {
1993 error(ERR_NONFATAL,
1994 "`%%local' missing size type parameter");
1995 free_tlist(origline);
1996 return DIRECTIVE_FOUND;
1999 /* Allow macro expansion of type parameter */
2000 tt = tokenize(tline->text);
2001 tt = expand_smacro(tt);
2002 size = parse_size(tt->text);
2003 if (!size) {
2004 error(ERR_NONFATAL,
2005 "Invalid size type for `%%local' missing directive");
2006 free_tlist(tt);
2007 free_tlist(origline);
2008 return DIRECTIVE_FOUND;
2010 free_tlist(tt);
2012 /* Round up to even stack slots */
2013 size = (size+StackSize-1) & ~(StackSize-1);
2015 offset += size; /* Negative offset, increment before */
2017 /* Now define the macro for the argument */
2018 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2019 local, StackPointer, offset);
2020 do_directive(tokenize(directive));
2022 /* Now define the assign to setup the enter_c macro correctly */
2023 snprintf(directive, sizeof(directive),
2024 "%%assign %%$localsize %%$localsize+%d", size);
2025 do_directive(tokenize(directive));
2027 /* Move to the next argument in the list */
2028 tline = tline->next;
2029 if (tline && tline->type == TOK_WHITESPACE)
2030 tline = tline->next;
2031 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2032 LocalOffset = offset;
2033 free_tlist(origline);
2034 return DIRECTIVE_FOUND;
2036 case PP_CLEAR:
2037 if (tline->next)
2038 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
2039 free_macros();
2040 init_macros();
2041 free_tlist(origline);
2042 return DIRECTIVE_FOUND;
2044 case PP_INCLUDE:
2045 tline = tline->next;
2046 skip_white_(tline);
2047 if (!tline || (tline->type != TOK_STRING &&
2048 tline->type != TOK_INTERNAL_STRING)) {
2049 error(ERR_NONFATAL, "`%%include' expects a file name");
2050 free_tlist(origline);
2051 return DIRECTIVE_FOUND; /* but we did _something_ */
2053 if (tline->next)
2054 error(ERR_WARNING,
2055 "trailing garbage after `%%include' ignored");
2056 if (tline->type != TOK_INTERNAL_STRING) {
2057 p = tline->text + 1; /* point past the quote to the name */
2058 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2059 } else
2060 p = tline->text; /* internal_string is easier */
2061 expand_macros_in_string(&p);
2062 inc = nasm_malloc(sizeof(Include));
2063 inc->next = istk;
2064 inc->conds = NULL;
2065 inc->fp = inc_fopen(p);
2066 if (!inc->fp && pass == 0) {
2067 /* -MG given but file not found */
2068 nasm_free(inc);
2069 } else {
2070 inc->fname = src_set_fname(p);
2071 inc->lineno = src_set_linnum(0);
2072 inc->lineinc = 1;
2073 inc->expansion = NULL;
2074 inc->mstk = NULL;
2075 istk = inc;
2076 list->uplevel(LIST_INCLUDE);
2078 free_tlist(origline);
2079 return DIRECTIVE_FOUND;
2081 case PP_PUSH:
2082 tline = tline->next;
2083 skip_white_(tline);
2084 tline = expand_id(tline);
2085 if (!tok_type_(tline, TOK_ID)) {
2086 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2087 free_tlist(origline);
2088 return DIRECTIVE_FOUND; /* but we did _something_ */
2090 if (tline->next)
2091 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2092 ctx = nasm_malloc(sizeof(Context));
2093 ctx->next = cstk;
2094 ctx->localmac = hash_init(HASH_SMALL);
2095 ctx->name = nasm_strdup(tline->text);
2096 ctx->number = unique++;
2097 cstk = ctx;
2098 free_tlist(origline);
2099 break;
2101 case PP_REPL:
2102 tline = tline->next;
2103 skip_white_(tline);
2104 tline = expand_id(tline);
2105 if (!tok_type_(tline, TOK_ID)) {
2106 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2107 free_tlist(origline);
2108 return DIRECTIVE_FOUND; /* but we did _something_ */
2110 if (tline->next)
2111 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2112 if (!cstk)
2113 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2114 else {
2115 nasm_free(cstk->name);
2116 cstk->name = nasm_strdup(tline->text);
2118 free_tlist(origline);
2119 break;
2121 case PP_POP:
2122 if (tline->next)
2123 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2124 if (!cstk)
2125 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2126 else
2127 ctx_pop();
2128 free_tlist(origline);
2129 break;
2131 case PP_ERROR:
2132 tline->next = expand_smacro(tline->next);
2133 tline = tline->next;
2134 skip_white_(tline);
2135 if (tok_type_(tline, TOK_STRING)) {
2136 p = tline->text + 1; /* point past the quote to the name */
2137 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2138 expand_macros_in_string(&p);
2139 error(ERR_NONFATAL, "%s", p);
2140 nasm_free(p);
2141 } else {
2142 p = detoken(tline, false);
2143 error(ERR_WARNING, "%s", p);
2144 nasm_free(p);
2146 free_tlist(origline);
2147 break;
2149 CASE_PP_IF:
2150 if (istk->conds && !emitting(istk->conds->state))
2151 j = COND_NEVER;
2152 else {
2153 j = if_condition(tline->next, i);
2154 tline->next = NULL; /* it got freed */
2155 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2157 cond = nasm_malloc(sizeof(Cond));
2158 cond->next = istk->conds;
2159 cond->state = j;
2160 istk->conds = cond;
2161 free_tlist(origline);
2162 return DIRECTIVE_FOUND;
2164 CASE_PP_ELIF:
2165 if (!istk->conds)
2166 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2167 if (emitting(istk->conds->state)
2168 || istk->conds->state == COND_NEVER)
2169 istk->conds->state = COND_NEVER;
2170 else {
2172 * IMPORTANT: In the case of %if, we will already have
2173 * called expand_mmac_params(); however, if we're
2174 * processing an %elif we must have been in a
2175 * non-emitting mode, which would have inhibited
2176 * the normal invocation of expand_mmac_params(). Therefore,
2177 * we have to do it explicitly here.
2179 j = if_condition(expand_mmac_params(tline->next), i);
2180 tline->next = NULL; /* it got freed */
2181 istk->conds->state =
2182 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2184 free_tlist(origline);
2185 return DIRECTIVE_FOUND;
2187 case PP_ELSE:
2188 if (tline->next)
2189 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2190 if (!istk->conds)
2191 error(ERR_FATAL, "`%%else': no matching `%%if'");
2192 if (emitting(istk->conds->state)
2193 || istk->conds->state == COND_NEVER)
2194 istk->conds->state = COND_ELSE_FALSE;
2195 else
2196 istk->conds->state = COND_ELSE_TRUE;
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
2200 case PP_ENDIF:
2201 if (tline->next)
2202 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2203 if (!istk->conds)
2204 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2205 cond = istk->conds;
2206 istk->conds = cond->next;
2207 nasm_free(cond);
2208 free_tlist(origline);
2209 return DIRECTIVE_FOUND;
2211 case PP_MACRO:
2212 case PP_IMACRO:
2213 if (defining)
2214 error(ERR_FATAL,
2215 "`%%%smacro': already defining a macro",
2216 (i == PP_IMACRO ? "i" : ""));
2217 tline = tline->next;
2218 skip_white_(tline);
2219 tline = expand_id(tline);
2220 if (!tok_type_(tline, TOK_ID)) {
2221 error(ERR_NONFATAL,
2222 "`%%%smacro' expects a macro name",
2223 (i == PP_IMACRO ? "i" : ""));
2224 return DIRECTIVE_FOUND;
2226 defining = nasm_malloc(sizeof(MMacro));
2227 defining->name = nasm_strdup(tline->text);
2228 defining->casesense = (i == PP_MACRO);
2229 defining->plus = false;
2230 defining->nolist = false;
2231 defining->in_progress = 0;
2232 defining->rep_nest = NULL;
2233 tline = expand_smacro(tline->next);
2234 skip_white_(tline);
2235 if (!tok_type_(tline, TOK_NUMBER)) {
2236 error(ERR_NONFATAL,
2237 "`%%%smacro' expects a parameter count",
2238 (i == PP_IMACRO ? "i" : ""));
2239 defining->nparam_min = defining->nparam_max = 0;
2240 } else {
2241 defining->nparam_min = defining->nparam_max =
2242 readnum(tline->text, &err);
2243 if (err)
2244 error(ERR_NONFATAL,
2245 "unable to parse parameter count `%s'", tline->text);
2247 if (tline && tok_is_(tline->next, "-")) {
2248 tline = tline->next->next;
2249 if (tok_is_(tline, "*"))
2250 defining->nparam_max = INT_MAX;
2251 else if (!tok_type_(tline, TOK_NUMBER))
2252 error(ERR_NONFATAL,
2253 "`%%%smacro' expects a parameter count after `-'",
2254 (i == PP_IMACRO ? "i" : ""));
2255 else {
2256 defining->nparam_max = readnum(tline->text, &err);
2257 if (err)
2258 error(ERR_NONFATAL,
2259 "unable to parse parameter count `%s'",
2260 tline->text);
2261 if (defining->nparam_min > defining->nparam_max)
2262 error(ERR_NONFATAL,
2263 "minimum parameter count exceeds maximum");
2266 if (tline && tok_is_(tline->next, "+")) {
2267 tline = tline->next;
2268 defining->plus = true;
2270 if (tline && tok_type_(tline->next, TOK_ID) &&
2271 !nasm_stricmp(tline->next->text, ".nolist")) {
2272 tline = tline->next;
2273 defining->nolist = true;
2275 mmac = (MMacro *) hash_findix(mmacros, defining->name);
2276 while (mmac) {
2277 if (!strcmp(mmac->name, defining->name) &&
2278 (mmac->nparam_min <= defining->nparam_max
2279 || defining->plus)
2280 && (defining->nparam_min <= mmac->nparam_max
2281 || mmac->plus)) {
2282 error(ERR_WARNING,
2283 "redefining multi-line macro `%s'", defining->name);
2284 break;
2286 mmac = mmac->next;
2289 * Handle default parameters.
2291 if (tline && tline->next) {
2292 defining->dlist = tline->next;
2293 tline->next = NULL;
2294 count_mmac_params(defining->dlist, &defining->ndefs,
2295 &defining->defaults);
2296 } else {
2297 defining->dlist = NULL;
2298 defining->defaults = NULL;
2300 defining->expansion = NULL;
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
2304 case PP_ENDM:
2305 case PP_ENDMACRO:
2306 if (!defining) {
2307 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2308 return DIRECTIVE_FOUND;
2310 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2311 defining->next = *mmhead;
2312 *mmhead = defining;
2313 defining = NULL;
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
2317 case PP_ROTATE:
2318 if (tline->next && tline->next->type == TOK_WHITESPACE)
2319 tline = tline->next;
2320 if (tline->next == NULL) {
2321 free_tlist(origline);
2322 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2323 return DIRECTIVE_FOUND;
2325 t = expand_smacro(tline->next);
2326 tline->next = NULL;
2327 free_tlist(origline);
2328 tline = t;
2329 tptr = &t;
2330 tokval.t_type = TOKEN_INVALID;
2331 evalresult =
2332 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2333 free_tlist(tline);
2334 if (!evalresult)
2335 return DIRECTIVE_FOUND;
2336 if (tokval.t_type)
2337 error(ERR_WARNING,
2338 "trailing garbage after expression ignored");
2339 if (!is_simple(evalresult)) {
2340 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2341 return DIRECTIVE_FOUND;
2343 mmac = istk->mstk;
2344 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2345 mmac = mmac->next_active;
2346 if (!mmac) {
2347 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2348 } else if (mmac->nparam == 0) {
2349 error(ERR_NONFATAL,
2350 "`%%rotate' invoked within macro without parameters");
2351 } else {
2352 int rotate = mmac->rotate + reloc_value(evalresult);
2354 rotate %= (int)mmac->nparam;
2355 if (rotate < 0)
2356 rotate += mmac->nparam;
2358 mmac->rotate = rotate;
2360 return DIRECTIVE_FOUND;
2362 case PP_REP:
2363 nolist = false;
2364 do {
2365 tline = tline->next;
2366 } while (tok_type_(tline, TOK_WHITESPACE));
2368 if (tok_type_(tline, TOK_ID) &&
2369 nasm_stricmp(tline->text, ".nolist") == 0) {
2370 nolist = true;
2371 do {
2372 tline = tline->next;
2373 } while (tok_type_(tline, TOK_WHITESPACE));
2376 if (tline) {
2377 t = expand_smacro(tline);
2378 tptr = &t;
2379 tokval.t_type = TOKEN_INVALID;
2380 evalresult =
2381 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2382 if (!evalresult) {
2383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
2386 if (tokval.t_type)
2387 error(ERR_WARNING,
2388 "trailing garbage after expression ignored");
2389 if (!is_simple(evalresult)) {
2390 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2391 return DIRECTIVE_FOUND;
2393 count = reloc_value(evalresult) + 1;
2394 } else {
2395 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2396 count = 0;
2398 free_tlist(origline);
2400 tmp_defining = defining;
2401 defining = nasm_malloc(sizeof(MMacro));
2402 defining->name = NULL; /* flags this macro as a %rep block */
2403 defining->casesense = false;
2404 defining->plus = false;
2405 defining->nolist = nolist;
2406 defining->in_progress = count;
2407 defining->nparam_min = defining->nparam_max = 0;
2408 defining->defaults = NULL;
2409 defining->dlist = NULL;
2410 defining->expansion = NULL;
2411 defining->next_active = istk->mstk;
2412 defining->rep_nest = tmp_defining;
2413 return DIRECTIVE_FOUND;
2415 case PP_ENDREP:
2416 if (!defining || defining->name) {
2417 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2418 return DIRECTIVE_FOUND;
2422 * Now we have a "macro" defined - although it has no name
2423 * and we won't be entering it in the hash tables - we must
2424 * push a macro-end marker for it on to istk->expansion.
2425 * After that, it will take care of propagating itself (a
2426 * macro-end marker line for a macro which is really a %rep
2427 * block will cause the macro to be re-expanded, complete
2428 * with another macro-end marker to ensure the process
2429 * continues) until the whole expansion is forcibly removed
2430 * from istk->expansion by a %exitrep.
2432 l = nasm_malloc(sizeof(Line));
2433 l->next = istk->expansion;
2434 l->finishes = defining;
2435 l->first = NULL;
2436 istk->expansion = l;
2438 istk->mstk = defining;
2440 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2441 tmp_defining = defining;
2442 defining = defining->rep_nest;
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2446 case PP_EXITREP:
2448 * We must search along istk->expansion until we hit a
2449 * macro-end marker for a macro with no name. Then we set
2450 * its `in_progress' flag to 0.
2452 for (l = istk->expansion; l; l = l->next)
2453 if (l->finishes && !l->finishes->name)
2454 break;
2456 if (l)
2457 l->finishes->in_progress = 0;
2458 else
2459 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2463 case PP_XDEFINE:
2464 case PP_IXDEFINE:
2465 case PP_DEFINE:
2466 case PP_IDEFINE:
2467 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
2469 tline = tline->next;
2470 skip_white_(tline);
2471 tline = expand_id(tline);
2472 if (!tline || (tline->type != TOK_ID &&
2473 (tline->type != TOK_PREPROC_ID ||
2474 tline->text[1] != '$'))) {
2475 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2476 pp_directives[i]);
2477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
2481 ctx = get_ctx(tline->text, false);
2483 mname = tline->text;
2484 last = tline;
2485 param_start = tline = tline->next;
2486 nparam = 0;
2488 /* Expand the macro definition now for %xdefine and %ixdefine */
2489 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2490 tline = expand_smacro(tline);
2492 if (tok_is_(tline, "(")) {
2494 * This macro has parameters.
2497 tline = tline->next;
2498 while (1) {
2499 skip_white_(tline);
2500 if (!tline) {
2501 error(ERR_NONFATAL, "parameter identifier expected");
2502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
2505 if (tline->type != TOK_ID) {
2506 error(ERR_NONFATAL,
2507 "`%s': parameter identifier expected",
2508 tline->text);
2509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2512 tline->type = TOK_SMAC_PARAM + nparam++;
2513 tline = tline->next;
2514 skip_white_(tline);
2515 if (tok_is_(tline, ",")) {
2516 tline = tline->next;
2517 continue;
2519 if (!tok_is_(tline, ")")) {
2520 error(ERR_NONFATAL,
2521 "`)' expected to terminate macro template");
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2525 break;
2527 last = tline;
2528 tline = tline->next;
2530 if (tok_type_(tline, TOK_WHITESPACE))
2531 last = tline, tline = tline->next;
2532 macro_start = NULL;
2533 last->next = NULL;
2534 t = tline;
2535 while (t) {
2536 if (t->type == TOK_ID) {
2537 for (tt = param_start; tt; tt = tt->next)
2538 if (tt->type >= TOK_SMAC_PARAM &&
2539 !strcmp(tt->text, t->text))
2540 t->type = tt->type;
2542 tt = t->next;
2543 t->next = macro_start;
2544 macro_start = t;
2545 t = tt;
2548 * Good. We now have a macro name, a parameter count, and a
2549 * token list (in reverse order) for an expansion. We ought
2550 * to be OK just to create an SMacro, store it, and let
2551 * free_tlist have the rest of the line (which we have
2552 * carefully re-terminated after chopping off the expansion
2553 * from the end).
2555 define_smacro(ctx, mname, casesense, nparam, macro_start);
2556 free_tlist(origline);
2557 return DIRECTIVE_FOUND;
2559 case PP_UNDEF:
2560 tline = tline->next;
2561 skip_white_(tline);
2562 tline = expand_id(tline);
2563 if (!tline || (tline->type != TOK_ID &&
2564 (tline->type != TOK_PREPROC_ID ||
2565 tline->text[1] != '$'))) {
2566 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND;
2570 if (tline->next) {
2571 error(ERR_WARNING,
2572 "trailing garbage after macro name ignored");
2575 /* Find the context that symbol belongs to */
2576 ctx = get_ctx(tline->text, false);
2577 undef_smacro(ctx, tline->text);
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND;
2581 case PP_STRLEN:
2582 casesense = true;
2584 tline = tline->next;
2585 skip_white_(tline);
2586 tline = expand_id(tline);
2587 if (!tline || (tline->type != TOK_ID &&
2588 (tline->type != TOK_PREPROC_ID ||
2589 tline->text[1] != '$'))) {
2590 error(ERR_NONFATAL,
2591 "`%%strlen' expects a macro identifier as first parameter");
2592 free_tlist(origline);
2593 return DIRECTIVE_FOUND;
2595 ctx = get_ctx(tline->text, false);
2597 mname = tline->text;
2598 last = tline;
2599 tline = expand_smacro(tline->next);
2600 last->next = NULL;
2602 t = tline;
2603 while (tok_type_(t, TOK_WHITESPACE))
2604 t = t->next;
2605 /* t should now point to the string */
2606 if (t->type != TOK_STRING) {
2607 error(ERR_NONFATAL,
2608 "`%%strlen` requires string as second parameter");
2609 free_tlist(tline);
2610 free_tlist(origline);
2611 return DIRECTIVE_FOUND;
2614 macro_start = nasm_malloc(sizeof(*macro_start));
2615 macro_start->next = NULL;
2616 make_tok_num(macro_start, strlen(t->text) - 2);
2617 macro_start->mac = NULL;
2620 * We now have a macro name, an implicit parameter count of
2621 * zero, and a numeric token to use as an expansion. Create
2622 * and store an SMacro.
2624 define_smacro(ctx, mname, casesense, 0, macro_start);
2625 free_tlist(tline);
2626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
2629 case PP_SUBSTR:
2630 casesense = true;
2632 tline = tline->next;
2633 skip_white_(tline);
2634 tline = expand_id(tline);
2635 if (!tline || (tline->type != TOK_ID &&
2636 (tline->type != TOK_PREPROC_ID ||
2637 tline->text[1] != '$'))) {
2638 error(ERR_NONFATAL,
2639 "`%%substr' expects a macro identifier as first parameter");
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2643 ctx = get_ctx(tline->text, false);
2645 mname = tline->text;
2646 last = tline;
2647 tline = expand_smacro(tline->next);
2648 last->next = NULL;
2650 t = tline->next;
2651 while (tok_type_(t, TOK_WHITESPACE))
2652 t = t->next;
2654 /* t should now point to the string */
2655 if (t->type != TOK_STRING) {
2656 error(ERR_NONFATAL,
2657 "`%%substr` requires string as second parameter");
2658 free_tlist(tline);
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2663 tt = t->next;
2664 tptr = &tt;
2665 tokval.t_type = TOKEN_INVALID;
2666 evalresult =
2667 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2668 if (!evalresult) {
2669 free_tlist(tline);
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2673 if (!is_simple(evalresult)) {
2674 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2675 free_tlist(tline);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND;
2680 macro_start = nasm_malloc(sizeof(*macro_start));
2681 macro_start->next = NULL;
2682 macro_start->text = nasm_strdup("'''");
2683 if (evalresult->value > 0
2684 && evalresult->value < (int) strlen(t->text) - 1) {
2685 macro_start->text[1] = t->text[evalresult->value];
2686 } else {
2687 macro_start->text[2] = '\0';
2689 macro_start->type = TOK_STRING;
2690 macro_start->mac = NULL;
2693 * We now have a macro name, an implicit parameter count of
2694 * zero, and a numeric token to use as an expansion. Create
2695 * and store an SMacro.
2697 define_smacro(ctx, mname, casesense, 0, macro_start);
2698 free_tlist(tline);
2699 free_tlist(origline);
2700 return DIRECTIVE_FOUND;
2702 case PP_ASSIGN:
2703 case PP_IASSIGN:
2704 casesense = (i == PP_ASSIGN);
2706 tline = tline->next;
2707 skip_white_(tline);
2708 tline = expand_id(tline);
2709 if (!tline || (tline->type != TOK_ID &&
2710 (tline->type != TOK_PREPROC_ID ||
2711 tline->text[1] != '$'))) {
2712 error(ERR_NONFATAL,
2713 "`%%%sassign' expects a macro identifier",
2714 (i == PP_IASSIGN ? "i" : ""));
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND;
2718 ctx = get_ctx(tline->text, false);
2720 mname = tline->text;
2721 last = tline;
2722 tline = expand_smacro(tline->next);
2723 last->next = NULL;
2725 t = tline;
2726 tptr = &t;
2727 tokval.t_type = TOKEN_INVALID;
2728 evalresult =
2729 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2730 free_tlist(tline);
2731 if (!evalresult) {
2732 free_tlist(origline);
2733 return DIRECTIVE_FOUND;
2736 if (tokval.t_type)
2737 error(ERR_WARNING,
2738 "trailing garbage after expression ignored");
2740 if (!is_simple(evalresult)) {
2741 error(ERR_NONFATAL,
2742 "non-constant value given to `%%%sassign'",
2743 (i == PP_IASSIGN ? "i" : ""));
2744 free_tlist(origline);
2745 return DIRECTIVE_FOUND;
2748 macro_start = nasm_malloc(sizeof(*macro_start));
2749 macro_start->next = NULL;
2750 make_tok_num(macro_start, reloc_value(evalresult));
2751 macro_start->mac = NULL;
2754 * We now have a macro name, an implicit parameter count of
2755 * zero, and a numeric token to use as an expansion. Create
2756 * and store an SMacro.
2758 define_smacro(ctx, mname, casesense, 0, macro_start);
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
2762 case PP_LINE:
2764 * Syntax is `%line nnn[+mmm] [filename]'
2766 tline = tline->next;
2767 skip_white_(tline);
2768 if (!tok_type_(tline, TOK_NUMBER)) {
2769 error(ERR_NONFATAL, "`%%line' expects line number");
2770 free_tlist(origline);
2771 return DIRECTIVE_FOUND;
2773 k = readnum(tline->text, &err);
2774 m = 1;
2775 tline = tline->next;
2776 if (tok_is_(tline, "+")) {
2777 tline = tline->next;
2778 if (!tok_type_(tline, TOK_NUMBER)) {
2779 error(ERR_NONFATAL, "`%%line' expects line increment");
2780 free_tlist(origline);
2781 return DIRECTIVE_FOUND;
2783 m = readnum(tline->text, &err);
2784 tline = tline->next;
2786 skip_white_(tline);
2787 src_set_linnum(k);
2788 istk->lineinc = m;
2789 if (tline) {
2790 nasm_free(src_set_fname(detoken(tline, false)));
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
2795 default:
2796 error(ERR_FATAL,
2797 "preprocessor directive `%s' not yet implemented",
2798 pp_directives[i]);
2799 break;
2801 return DIRECTIVE_FOUND;
2805 * Ensure that a macro parameter contains a condition code and
2806 * nothing else. Return the condition code index if so, or -1
2807 * otherwise.
2809 static int find_cc(Token * t)
2811 Token *tt;
2812 int i, j, k, m;
2814 if (!t)
2815 return -1; /* Probably a %+ without a space */
2817 skip_white_(t);
2818 if (t->type != TOK_ID)
2819 return -1;
2820 tt = t->next;
2821 skip_white_(tt);
2822 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2823 return -1;
2825 i = -1;
2826 j = elements(conditions);
2827 while (j - i > 1) {
2828 k = (j + i) / 2;
2829 m = nasm_stricmp(t->text, conditions[k]);
2830 if (m == 0) {
2831 i = k;
2832 j = -2;
2833 break;
2834 } else if (m < 0) {
2835 j = k;
2836 } else
2837 i = k;
2839 if (j != -2)
2840 return -1;
2841 return i;
2845 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2846 * %-n) and MMacro-local identifiers (%%foo).
2848 static Token *expand_mmac_params(Token * tline)
2850 Token *t, *tt, **tail, *thead;
2852 tail = &thead;
2853 thead = NULL;
2855 while (tline) {
2856 if (tline->type == TOK_PREPROC_ID &&
2857 (((tline->text[1] == '+' || tline->text[1] == '-')
2858 && tline->text[2]) || tline->text[1] == '%'
2859 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2860 char *text = NULL;
2861 int type = 0, cc; /* type = 0 to placate optimisers */
2862 char tmpbuf[30];
2863 unsigned int n;
2864 int i;
2865 MMacro *mac;
2867 t = tline;
2868 tline = tline->next;
2870 mac = istk->mstk;
2871 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2872 mac = mac->next_active;
2873 if (!mac)
2874 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2875 else
2876 switch (t->text[1]) {
2878 * We have to make a substitution of one of the
2879 * forms %1, %-1, %+1, %%foo, %0.
2881 case '0':
2882 type = TOK_NUMBER;
2883 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2884 text = nasm_strdup(tmpbuf);
2885 break;
2886 case '%':
2887 type = TOK_ID;
2888 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
2889 mac->unique);
2890 text = nasm_strcat(tmpbuf, t->text + 2);
2891 break;
2892 case '-':
2893 n = atoi(t->text + 2) - 1;
2894 if (n >= mac->nparam)
2895 tt = NULL;
2896 else {
2897 if (mac->nparam > 1)
2898 n = (n + mac->rotate) % mac->nparam;
2899 tt = mac->params[n];
2901 cc = find_cc(tt);
2902 if (cc == -1) {
2903 error(ERR_NONFATAL,
2904 "macro parameter %d is not a condition code",
2905 n + 1);
2906 text = NULL;
2907 } else {
2908 type = TOK_ID;
2909 if (inverse_ccs[cc] == -1) {
2910 error(ERR_NONFATAL,
2911 "condition code `%s' is not invertible",
2912 conditions[cc]);
2913 text = NULL;
2914 } else
2915 text =
2916 nasm_strdup(conditions[inverse_ccs[cc]]);
2918 break;
2919 case '+':
2920 n = atoi(t->text + 2) - 1;
2921 if (n >= mac->nparam)
2922 tt = NULL;
2923 else {
2924 if (mac->nparam > 1)
2925 n = (n + mac->rotate) % mac->nparam;
2926 tt = mac->params[n];
2928 cc = find_cc(tt);
2929 if (cc == -1) {
2930 error(ERR_NONFATAL,
2931 "macro parameter %d is not a condition code",
2932 n + 1);
2933 text = NULL;
2934 } else {
2935 type = TOK_ID;
2936 text = nasm_strdup(conditions[cc]);
2938 break;
2939 default:
2940 n = atoi(t->text + 1) - 1;
2941 if (n >= mac->nparam)
2942 tt = NULL;
2943 else {
2944 if (mac->nparam > 1)
2945 n = (n + mac->rotate) % mac->nparam;
2946 tt = mac->params[n];
2948 if (tt) {
2949 for (i = 0; i < mac->paramlen[n]; i++) {
2950 *tail = new_Token(NULL, tt->type, tt->text, 0);
2951 tail = &(*tail)->next;
2952 tt = tt->next;
2955 text = NULL; /* we've done it here */
2956 break;
2958 if (!text) {
2959 delete_Token(t);
2960 } else {
2961 *tail = t;
2962 tail = &t->next;
2963 t->type = type;
2964 nasm_free(t->text);
2965 t->text = text;
2966 t->mac = NULL;
2968 continue;
2969 } else {
2970 t = *tail = tline;
2971 tline = tline->next;
2972 t->mac = NULL;
2973 tail = &t->next;
2976 *tail = NULL;
2977 t = thead;
2978 for (; t && (tt = t->next) != NULL; t = t->next)
2979 switch (t->type) {
2980 case TOK_WHITESPACE:
2981 if (tt->type == TOK_WHITESPACE) {
2982 t->next = delete_Token(tt);
2984 break;
2985 case TOK_ID:
2986 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2987 char *tmp = nasm_strcat(t->text, tt->text);
2988 nasm_free(t->text);
2989 t->text = tmp;
2990 t->next = delete_Token(tt);
2992 break;
2993 case TOK_NUMBER:
2994 if (tt->type == TOK_NUMBER) {
2995 char *tmp = nasm_strcat(t->text, tt->text);
2996 nasm_free(t->text);
2997 t->text = tmp;
2998 t->next = delete_Token(tt);
3000 break;
3001 default:
3002 break;
3005 return thead;
3009 * Expand all single-line macro calls made in the given line.
3010 * Return the expanded version of the line. The original is deemed
3011 * to be destroyed in the process. (In reality we'll just move
3012 * Tokens from input to output a lot of the time, rather than
3013 * actually bothering to destroy and replicate.)
3015 #define DEADMAN_LIMIT (1 << 20)
3017 static Token *expand_smacro(Token * tline)
3019 Token *t, *tt, *mstart, **tail, *thead;
3020 SMacro *head = NULL, *m;
3021 Token **params;
3022 int *paramsize;
3023 unsigned int nparam, sparam;
3024 int brackets, rescan;
3025 Token *org_tline = tline;
3026 Context *ctx;
3027 char *mname;
3028 int deadman = DEADMAN_LIMIT;
3031 * Trick: we should avoid changing the start token pointer since it can
3032 * be contained in "next" field of other token. Because of this
3033 * we allocate a copy of first token and work with it; at the end of
3034 * routine we copy it back
3036 if (org_tline) {
3037 tline =
3038 new_Token(org_tline->next, org_tline->type, org_tline->text,
3040 tline->mac = org_tline->mac;
3041 nasm_free(org_tline->text);
3042 org_tline->text = NULL;
3045 again:
3046 tail = &thead;
3047 thead = NULL;
3049 while (tline) { /* main token loop */
3050 if (!--deadman) {
3051 error(ERR_NONFATAL, "interminable macro recursion");
3052 break;
3055 if ((mname = tline->text)) {
3056 /* if this token is a local macro, look in local context */
3057 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3058 ctx = get_ctx(mname, true);
3059 else
3060 ctx = NULL;
3062 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3063 mname);
3066 * We've hit an identifier. As in is_mmacro below, we first
3067 * check whether the identifier is a single-line macro at
3068 * all, then think about checking for parameters if
3069 * necessary.
3071 for (m = head; m; m = m->next)
3072 if (!mstrcmp(m->name, mname, m->casesense))
3073 break;
3074 if (m) {
3075 mstart = tline;
3076 params = NULL;
3077 paramsize = NULL;
3078 if (m->nparam == 0) {
3080 * Simple case: the macro is parameterless. Discard the
3081 * one token that the macro call took, and push the
3082 * expansion back on the to-do stack.
3084 if (!m->expansion) {
3085 if (!strcmp("__FILE__", m->name)) {
3086 int32_t num = 0;
3087 src_get(&num, &(tline->text));
3088 nasm_quote(&(tline->text));
3089 tline->type = TOK_STRING;
3090 continue;
3092 if (!strcmp("__LINE__", m->name)) {
3093 nasm_free(tline->text);
3094 make_tok_num(tline, src_get_linnum());
3095 continue;
3097 if (!strcmp("__BITS__", m->name)) {
3098 nasm_free(tline->text);
3099 make_tok_num(tline, globalbits);
3100 continue;
3102 tline = delete_Token(tline);
3103 continue;
3105 } else {
3107 * Complicated case: at least one macro with this name
3108 * exists and takes parameters. We must find the
3109 * parameters in the call, count them, find the SMacro
3110 * that corresponds to that form of the macro call, and
3111 * substitute for the parameters when we expand. What a
3112 * pain.
3114 /*tline = tline->next;
3115 skip_white_(tline); */
3116 do {
3117 t = tline->next;
3118 while (tok_type_(t, TOK_SMAC_END)) {
3119 t->mac->in_progress = false;
3120 t->text = NULL;
3121 t = tline->next = delete_Token(t);
3123 tline = t;
3124 } while (tok_type_(tline, TOK_WHITESPACE));
3125 if (!tok_is_(tline, "(")) {
3127 * This macro wasn't called with parameters: ignore
3128 * the call. (Behaviour borrowed from gnu cpp.)
3130 tline = mstart;
3131 m = NULL;
3132 } else {
3133 int paren = 0;
3134 int white = 0;
3135 brackets = 0;
3136 nparam = 0;
3137 sparam = PARAM_DELTA;
3138 params = nasm_malloc(sparam * sizeof(Token *));
3139 params[0] = tline->next;
3140 paramsize = nasm_malloc(sparam * sizeof(int));
3141 paramsize[0] = 0;
3142 while (true) { /* parameter loop */
3144 * For some unusual expansions
3145 * which concatenates function call
3147 t = tline->next;
3148 while (tok_type_(t, TOK_SMAC_END)) {
3149 t->mac->in_progress = false;
3150 t->text = NULL;
3151 t = tline->next = delete_Token(t);
3153 tline = t;
3155 if (!tline) {
3156 error(ERR_NONFATAL,
3157 "macro call expects terminating `)'");
3158 break;
3160 if (tline->type == TOK_WHITESPACE
3161 && brackets <= 0) {
3162 if (paramsize[nparam])
3163 white++;
3164 else
3165 params[nparam] = tline->next;
3166 continue; /* parameter loop */
3168 if (tline->type == TOK_OTHER
3169 && tline->text[1] == 0) {
3170 char ch = tline->text[0];
3171 if (ch == ',' && !paren && brackets <= 0) {
3172 if (++nparam >= sparam) {
3173 sparam += PARAM_DELTA;
3174 params = nasm_realloc(params,
3175 sparam *
3176 sizeof(Token
3177 *));
3178 paramsize =
3179 nasm_realloc(paramsize,
3180 sparam *
3181 sizeof(int));
3183 params[nparam] = tline->next;
3184 paramsize[nparam] = 0;
3185 white = 0;
3186 continue; /* parameter loop */
3188 if (ch == '{' &&
3189 (brackets > 0 || (brackets == 0 &&
3190 !paramsize[nparam])))
3192 if (!(brackets++)) {
3193 params[nparam] = tline->next;
3194 continue; /* parameter loop */
3197 if (ch == '}' && brackets > 0)
3198 if (--brackets == 0) {
3199 brackets = -1;
3200 continue; /* parameter loop */
3202 if (ch == '(' && !brackets)
3203 paren++;
3204 if (ch == ')' && brackets <= 0)
3205 if (--paren < 0)
3206 break;
3208 if (brackets < 0) {
3209 brackets = 0;
3210 error(ERR_NONFATAL, "braces do not "
3211 "enclose all of macro parameter");
3213 paramsize[nparam] += white + 1;
3214 white = 0;
3215 } /* parameter loop */
3216 nparam++;
3217 while (m && (m->nparam != nparam ||
3218 mstrcmp(m->name, mname,
3219 m->casesense)))
3220 m = m->next;
3221 if (!m)
3222 error(ERR_WARNING | ERR_WARN_MNP,
3223 "macro `%s' exists, "
3224 "but not taking %d parameters",
3225 mstart->text, nparam);
3228 if (m && m->in_progress)
3229 m = NULL;
3230 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3232 * Design question: should we handle !tline, which
3233 * indicates missing ')' here, or expand those
3234 * macros anyway, which requires the (t) test a few
3235 * lines down?
3237 nasm_free(params);
3238 nasm_free(paramsize);
3239 tline = mstart;
3240 } else {
3242 * Expand the macro: we are placed on the last token of the
3243 * call, so that we can easily split the call from the
3244 * following tokens. We also start by pushing an SMAC_END
3245 * token for the cycle removal.
3247 t = tline;
3248 if (t) {
3249 tline = t->next;
3250 t->next = NULL;
3252 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3253 tt->mac = m;
3254 m->in_progress = true;
3255 tline = tt;
3256 for (t = m->expansion; t; t = t->next) {
3257 if (t->type >= TOK_SMAC_PARAM) {
3258 Token *pcopy = tline, **ptail = &pcopy;
3259 Token *ttt, *pt;
3260 int i;
3262 ttt = params[t->type - TOK_SMAC_PARAM];
3263 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3264 --i >= 0;) {
3265 pt = *ptail =
3266 new_Token(tline, ttt->type, ttt->text,
3268 ptail = &pt->next;
3269 ttt = ttt->next;
3271 tline = pcopy;
3272 } else if (t->type == TOK_PREPROC_Q) {
3273 tt = new_Token(tline, TOK_ID, mname, 0);
3274 tline = tt;
3275 } else if (t->type == TOK_PREPROC_QQ) {
3276 tt = new_Token(tline, TOK_ID, m->name, 0);
3277 tline = tt;
3278 } else {
3279 tt = new_Token(tline, t->type, t->text, 0);
3280 tline = tt;
3285 * Having done that, get rid of the macro call, and clean
3286 * up the parameters.
3288 nasm_free(params);
3289 nasm_free(paramsize);
3290 free_tlist(mstart);
3291 continue; /* main token loop */
3296 if (tline->type == TOK_SMAC_END) {
3297 tline->mac->in_progress = false;
3298 tline = delete_Token(tline);
3299 } else {
3300 t = *tail = tline;
3301 tline = tline->next;
3302 t->mac = NULL;
3303 t->next = NULL;
3304 tail = &t->next;
3309 * Now scan the entire line and look for successive TOK_IDs that resulted
3310 * after expansion (they can't be produced by tokenize()). The successive
3311 * TOK_IDs should be concatenated.
3312 * Also we look for %+ tokens and concatenate the tokens before and after
3313 * them (without white spaces in between).
3315 t = thead;
3316 rescan = 0;
3317 while (t) {
3318 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3319 t = t->next;
3320 if (!t || !t->next)
3321 break;
3322 if (t->next->type == TOK_ID ||
3323 t->next->type == TOK_PREPROC_ID ||
3324 t->next->type == TOK_NUMBER) {
3325 char *p = nasm_strcat(t->text, t->next->text);
3326 nasm_free(t->text);
3327 t->next = delete_Token(t->next);
3328 t->text = p;
3329 rescan = 1;
3330 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3331 t->next->next->type == TOK_PREPROC_ID &&
3332 strcmp(t->next->next->text, "%+") == 0) {
3333 /* free the next whitespace, the %+ token and next whitespace */
3334 int i;
3335 for (i = 1; i <= 3; i++) {
3336 if (!t->next
3337 || (i != 2 && t->next->type != TOK_WHITESPACE))
3338 break;
3339 t->next = delete_Token(t->next);
3340 } /* endfor */
3341 } else
3342 t = t->next;
3344 /* If we concatenaded something, re-scan the line for macros */
3345 if (rescan) {
3346 tline = thead;
3347 goto again;
3350 if (org_tline) {
3351 if (thead) {
3352 *org_tline = *thead;
3353 /* since we just gave text to org_line, don't free it */
3354 thead->text = NULL;
3355 delete_Token(thead);
3356 } else {
3357 /* the expression expanded to empty line;
3358 we can't return NULL for some reasons
3359 we just set the line to a single WHITESPACE token. */
3360 memset(org_tline, 0, sizeof(*org_tline));
3361 org_tline->text = NULL;
3362 org_tline->type = TOK_WHITESPACE;
3364 thead = org_tline;
3367 return thead;
3371 * Similar to expand_smacro but used exclusively with macro identifiers
3372 * right before they are fetched in. The reason is that there can be
3373 * identifiers consisting of several subparts. We consider that if there
3374 * are more than one element forming the name, user wants a expansion,
3375 * otherwise it will be left as-is. Example:
3377 * %define %$abc cde
3379 * the identifier %$abc will be left as-is so that the handler for %define
3380 * will suck it and define the corresponding value. Other case:
3382 * %define _%$abc cde
3384 * In this case user wants name to be expanded *before* %define starts
3385 * working, so we'll expand %$abc into something (if it has a value;
3386 * otherwise it will be left as-is) then concatenate all successive
3387 * PP_IDs into one.
3389 static Token *expand_id(Token * tline)
3391 Token *cur, *oldnext = NULL;
3393 if (!tline || !tline->next)
3394 return tline;
3396 cur = tline;
3397 while (cur->next &&
3398 (cur->next->type == TOK_ID ||
3399 cur->next->type == TOK_PREPROC_ID
3400 || cur->next->type == TOK_NUMBER))
3401 cur = cur->next;
3403 /* If identifier consists of just one token, don't expand */
3404 if (cur == tline)
3405 return tline;
3407 if (cur) {
3408 oldnext = cur->next; /* Detach the tail past identifier */
3409 cur->next = NULL; /* so that expand_smacro stops here */
3412 tline = expand_smacro(tline);
3414 if (cur) {
3415 /* expand_smacro possibly changhed tline; re-scan for EOL */
3416 cur = tline;
3417 while (cur && cur->next)
3418 cur = cur->next;
3419 if (cur)
3420 cur->next = oldnext;
3423 return tline;
3427 * Determine whether the given line constitutes a multi-line macro
3428 * call, and return the MMacro structure called if so. Doesn't have
3429 * to check for an initial label - that's taken care of in
3430 * expand_mmacro - but must check numbers of parameters. Guaranteed
3431 * to be called with tline->type == TOK_ID, so the putative macro
3432 * name is easy to find.
3434 static MMacro *is_mmacro(Token * tline, Token *** params_array)
3436 MMacro *head, *m;
3437 Token **params;
3438 int nparam;
3440 head = (MMacro *) hash_findix(mmacros, tline->text);
3443 * Efficiency: first we see if any macro exists with the given
3444 * name. If not, we can return NULL immediately. _Then_ we
3445 * count the parameters, and then we look further along the
3446 * list if necessary to find the proper MMacro.
3448 for (m = head; m; m = m->next)
3449 if (!mstrcmp(m->name, tline->text, m->casesense))
3450 break;
3451 if (!m)
3452 return NULL;
3455 * OK, we have a potential macro. Count and demarcate the
3456 * parameters.
3458 count_mmac_params(tline->next, &nparam, &params);
3461 * So we know how many parameters we've got. Find the MMacro
3462 * structure that handles this number.
3464 while (m) {
3465 if (m->nparam_min <= nparam
3466 && (m->plus || nparam <= m->nparam_max)) {
3468 * This one is right. Just check if cycle removal
3469 * prohibits us using it before we actually celebrate...
3471 if (m->in_progress) {
3472 #if 0
3473 error(ERR_NONFATAL,
3474 "self-reference in multi-line macro `%s'", m->name);
3475 #endif
3476 nasm_free(params);
3477 return NULL;
3480 * It's right, and we can use it. Add its default
3481 * parameters to the end of our list if necessary.
3483 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3484 params =
3485 nasm_realloc(params,
3486 ((m->nparam_min + m->ndefs +
3487 1) * sizeof(*params)));
3488 while (nparam < m->nparam_min + m->ndefs) {
3489 params[nparam] = m->defaults[nparam - m->nparam_min];
3490 nparam++;
3494 * If we've gone over the maximum parameter count (and
3495 * we're in Plus mode), ignore parameters beyond
3496 * nparam_max.
3498 if (m->plus && nparam > m->nparam_max)
3499 nparam = m->nparam_max;
3501 * Then terminate the parameter list, and leave.
3503 if (!params) { /* need this special case */
3504 params = nasm_malloc(sizeof(*params));
3505 nparam = 0;
3507 params[nparam] = NULL;
3508 *params_array = params;
3509 return m;
3512 * This one wasn't right: look for the next one with the
3513 * same name.
3515 for (m = m->next; m; m = m->next)
3516 if (!mstrcmp(m->name, tline->text, m->casesense))
3517 break;
3521 * After all that, we didn't find one with the right number of
3522 * parameters. Issue a warning, and fail to expand the macro.
3524 error(ERR_WARNING | ERR_WARN_MNP,
3525 "macro `%s' exists, but not taking %d parameters",
3526 tline->text, nparam);
3527 nasm_free(params);
3528 return NULL;
3532 * Expand the multi-line macro call made by the given line, if
3533 * there is one to be expanded. If there is, push the expansion on
3534 * istk->expansion and return 1. Otherwise return 0.
3536 static int expand_mmacro(Token * tline)
3538 Token *startline = tline;
3539 Token *label = NULL;
3540 int dont_prepend = 0;
3541 Token **params, *t, *mtok, *tt;
3542 MMacro *m;
3543 Line *l, *ll;
3544 int i, nparam, *paramlen;
3546 t = tline;
3547 skip_white_(t);
3548 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3549 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
3550 return 0;
3551 mtok = t;
3552 m = is_mmacro(t, &params);
3553 if (!m) {
3554 Token *last;
3556 * We have an id which isn't a macro call. We'll assume
3557 * it might be a label; we'll also check to see if a
3558 * colon follows it. Then, if there's another id after
3559 * that lot, we'll check it again for macro-hood.
3561 label = last = t;
3562 t = t->next;
3563 if (tok_type_(t, TOK_WHITESPACE))
3564 last = t, t = t->next;
3565 if (tok_is_(t, ":")) {
3566 dont_prepend = 1;
3567 last = t, t = t->next;
3568 if (tok_type_(t, TOK_WHITESPACE))
3569 last = t, t = t->next;
3571 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3572 return 0;
3573 last->next = NULL;
3574 tline = t;
3578 * Fix up the parameters: this involves stripping leading and
3579 * trailing whitespace, then stripping braces if they are
3580 * present.
3582 for (nparam = 0; params[nparam]; nparam++) ;
3583 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
3585 for (i = 0; params[i]; i++) {
3586 int brace = false;
3587 int comma = (!m->plus || i < nparam - 1);
3589 t = params[i];
3590 skip_white_(t);
3591 if (tok_is_(t, "{"))
3592 t = t->next, brace = true, comma = false;
3593 params[i] = t;
3594 paramlen[i] = 0;
3595 while (t) {
3596 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3597 break; /* ... because we have hit a comma */
3598 if (comma && t->type == TOK_WHITESPACE
3599 && tok_is_(t->next, ","))
3600 break; /* ... or a space then a comma */
3601 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3602 break; /* ... or a brace */
3603 t = t->next;
3604 paramlen[i]++;
3609 * OK, we have a MMacro structure together with a set of
3610 * parameters. We must now go through the expansion and push
3611 * copies of each Line on to istk->expansion. Substitution of
3612 * parameter tokens and macro-local tokens doesn't get done
3613 * until the single-line macro substitution process; this is
3614 * because delaying them allows us to change the semantics
3615 * later through %rotate.
3617 * First, push an end marker on to istk->expansion, mark this
3618 * macro as in progress, and set up its invocation-specific
3619 * variables.
3621 ll = nasm_malloc(sizeof(Line));
3622 ll->next = istk->expansion;
3623 ll->finishes = m;
3624 ll->first = NULL;
3625 istk->expansion = ll;
3627 m->in_progress = true;
3628 m->params = params;
3629 m->iline = tline;
3630 m->nparam = nparam;
3631 m->rotate = 0;
3632 m->paramlen = paramlen;
3633 m->unique = unique++;
3634 m->lineno = 0;
3636 m->next_active = istk->mstk;
3637 istk->mstk = m;
3639 for (l = m->expansion; l; l = l->next) {
3640 Token **tail;
3642 ll = nasm_malloc(sizeof(Line));
3643 ll->finishes = NULL;
3644 ll->next = istk->expansion;
3645 istk->expansion = ll;
3646 tail = &ll->first;
3648 for (t = l->first; t; t = t->next) {
3649 Token *x = t;
3650 switch (t->type) {
3651 case TOK_PREPROC_Q:
3652 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3653 break;
3654 case TOK_PREPROC_QQ:
3655 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3656 break;
3657 case TOK_PREPROC_ID:
3658 if (t->text[1] == '0' && t->text[2] == '0') {
3659 dont_prepend = -1;
3660 x = label;
3661 if (!x)
3662 continue;
3664 /* fall through */
3665 default:
3666 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3667 break;
3669 tail = &tt->next;
3671 *tail = NULL;
3675 * If we had a label, push it on as the first line of
3676 * the macro expansion.
3678 if (label) {
3679 if (dont_prepend < 0)
3680 free_tlist(startline);
3681 else {
3682 ll = nasm_malloc(sizeof(Line));
3683 ll->finishes = NULL;
3684 ll->next = istk->expansion;
3685 istk->expansion = ll;
3686 ll->first = startline;
3687 if (!dont_prepend) {
3688 while (label->next)
3689 label = label->next;
3690 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3695 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3697 return 1;
3701 * Since preprocessor always operate only on the line that didn't
3702 * arrived yet, we should always use ERR_OFFBY1. Also since user
3703 * won't want to see same error twice (preprocessing is done once
3704 * per pass) we will want to show errors only during pass one.
3706 static void error(int severity, const char *fmt, ...)
3708 va_list arg;
3709 char buff[1024];
3711 /* If we're in a dead branch of IF or something like it, ignore the error */
3712 if (istk && istk->conds && !emitting(istk->conds->state))
3713 return;
3715 va_start(arg, fmt);
3716 vsnprintf(buff, sizeof(buff), fmt, arg);
3717 va_end(arg);
3719 if (istk && istk->mstk && istk->mstk->name)
3720 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3721 istk->mstk->lineno, buff);
3722 else
3723 _error(severity | ERR_PASS1, "%s", buff);
3726 static void
3727 pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
3728 ListGen * listgen)
3730 _error = errfunc;
3731 cstk = NULL;
3732 istk = nasm_malloc(sizeof(Include));
3733 istk->next = NULL;
3734 istk->conds = NULL;
3735 istk->expansion = NULL;
3736 istk->mstk = NULL;
3737 istk->fp = fopen(file, "r");
3738 istk->fname = NULL;
3739 src_set_fname(nasm_strdup(file));
3740 src_set_linnum(0);
3741 istk->lineinc = 1;
3742 if (!istk->fp)
3743 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3744 file);
3745 defining = NULL;
3746 init_macros();
3747 unique = 0;
3748 if (tasm_compatible_mode) {
3749 stdmacpos = nasm_stdmac;
3750 } else {
3751 stdmacpos = nasm_stdmac_after_tasm;
3753 any_extrastdmac = (extrastdmac != NULL);
3754 list = listgen;
3755 evaluate = eval;
3756 pass = apass;
3759 static char *pp_getline(void)
3761 char *line;
3762 Token *tline;
3764 while (1) {
3766 * Fetch a tokenized line, either from the macro-expansion
3767 * buffer or from the input file.
3769 tline = NULL;
3770 while (istk->expansion && istk->expansion->finishes) {
3771 Line *l = istk->expansion;
3772 if (!l->finishes->name && l->finishes->in_progress > 1) {
3773 Line *ll;
3776 * This is a macro-end marker for a macro with no
3777 * name, which means it's not really a macro at all
3778 * but a %rep block, and the `in_progress' field is
3779 * more than 1, meaning that we still need to
3780 * repeat. (1 means the natural last repetition; 0
3781 * means termination by %exitrep.) We have
3782 * therefore expanded up to the %endrep, and must
3783 * push the whole block on to the expansion buffer
3784 * again. We don't bother to remove the macro-end
3785 * marker: we'd only have to generate another one
3786 * if we did.
3788 l->finishes->in_progress--;
3789 for (l = l->finishes->expansion; l; l = l->next) {
3790 Token *t, *tt, **tail;
3792 ll = nasm_malloc(sizeof(Line));
3793 ll->next = istk->expansion;
3794 ll->finishes = NULL;
3795 ll->first = NULL;
3796 tail = &ll->first;
3798 for (t = l->first; t; t = t->next) {
3799 if (t->text || t->type == TOK_WHITESPACE) {
3800 tt = *tail =
3801 new_Token(NULL, t->type, t->text, 0);
3802 tail = &tt->next;
3806 istk->expansion = ll;
3808 } else {
3810 * Check whether a `%rep' was started and not ended
3811 * within this macro expansion. This can happen and
3812 * should be detected. It's a fatal error because
3813 * I'm too confused to work out how to recover
3814 * sensibly from it.
3816 if (defining) {
3817 if (defining->name)
3818 error(ERR_PANIC,
3819 "defining with name in expansion");
3820 else if (istk->mstk->name)
3821 error(ERR_FATAL,
3822 "`%%rep' without `%%endrep' within"
3823 " expansion of macro `%s'",
3824 istk->mstk->name);
3828 * FIXME: investigate the relationship at this point between
3829 * istk->mstk and l->finishes
3832 MMacro *m = istk->mstk;
3833 istk->mstk = m->next_active;
3834 if (m->name) {
3836 * This was a real macro call, not a %rep, and
3837 * therefore the parameter information needs to
3838 * be freed.
3840 nasm_free(m->params);
3841 free_tlist(m->iline);
3842 nasm_free(m->paramlen);
3843 l->finishes->in_progress = false;
3844 } else
3845 free_mmacro(m);
3847 istk->expansion = l->next;
3848 nasm_free(l);
3849 list->downlevel(LIST_MACRO);
3852 while (1) { /* until we get a line we can use */
3854 if (istk->expansion) { /* from a macro expansion */
3855 char *p;
3856 Line *l = istk->expansion;
3857 if (istk->mstk)
3858 istk->mstk->lineno++;
3859 tline = l->first;
3860 istk->expansion = l->next;
3861 nasm_free(l);
3862 p = detoken(tline, false);
3863 list->line(LIST_MACRO, p);
3864 nasm_free(p);
3865 break;
3867 line = read_line();
3868 if (line) { /* from the current input file */
3869 line = prepreproc(line);
3870 tline = tokenize(line);
3871 nasm_free(line);
3872 break;
3875 * The current file has ended; work down the istk
3878 Include *i = istk;
3879 fclose(i->fp);
3880 if (i->conds)
3881 error(ERR_FATAL,
3882 "expected `%%endif' before end of file");
3883 /* only set line and file name if there's a next node */
3884 if (i->next) {
3885 src_set_linnum(i->lineno);
3886 nasm_free(src_set_fname(i->fname));
3888 istk = i->next;
3889 list->downlevel(LIST_INCLUDE);
3890 nasm_free(i);
3891 if (!istk)
3892 return NULL;
3897 * We must expand MMacro parameters and MMacro-local labels
3898 * _before_ we plunge into directive processing, to cope
3899 * with things like `%define something %1' such as STRUC
3900 * uses. Unless we're _defining_ a MMacro, in which case
3901 * those tokens should be left alone to go into the
3902 * definition; and unless we're in a non-emitting
3903 * condition, in which case we don't want to meddle with
3904 * anything.
3906 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3907 tline = expand_mmac_params(tline);
3910 * Check the line to see if it's a preprocessor directive.
3912 if (do_directive(tline) == DIRECTIVE_FOUND) {
3913 continue;
3914 } else if (defining) {
3916 * We're defining a multi-line macro. We emit nothing
3917 * at all, and just
3918 * shove the tokenized line on to the macro definition.
3920 Line *l = nasm_malloc(sizeof(Line));
3921 l->next = defining->expansion;
3922 l->first = tline;
3923 l->finishes = false;
3924 defining->expansion = l;
3925 continue;
3926 } else if (istk->conds && !emitting(istk->conds->state)) {
3928 * We're in a non-emitting branch of a condition block.
3929 * Emit nothing at all, not even a blank line: when we
3930 * emerge from the condition we'll give a line-number
3931 * directive so we keep our place correctly.
3933 free_tlist(tline);
3934 continue;
3935 } else if (istk->mstk && !istk->mstk->in_progress) {
3937 * We're in a %rep block which has been terminated, so
3938 * we're walking through to the %endrep without
3939 * emitting anything. Emit nothing at all, not even a
3940 * blank line: when we emerge from the %rep block we'll
3941 * give a line-number directive so we keep our place
3942 * correctly.
3944 free_tlist(tline);
3945 continue;
3946 } else {
3947 tline = expand_smacro(tline);
3948 if (!expand_mmacro(tline)) {
3950 * De-tokenize the line again, and emit it.
3952 line = detoken(tline, true);
3953 free_tlist(tline);
3954 break;
3955 } else {
3956 continue; /* expand_mmacro calls free_tlist */
3961 return line;
3964 static void pp_cleanup(int pass)
3966 if (defining) {
3967 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3968 defining->name);
3969 free_mmacro(defining);
3971 while (cstk)
3972 ctx_pop();
3973 free_macros();
3974 while (istk) {
3975 Include *i = istk;
3976 istk = istk->next;
3977 fclose(i->fp);
3978 nasm_free(i->fname);
3979 nasm_free(i);
3981 while (cstk)
3982 ctx_pop();
3983 if (pass == 0) {
3984 free_llist(predef);
3985 delete_Blocks();
3989 void pp_include_path(char *path)
3991 IncPath *i;
3993 i = nasm_malloc(sizeof(IncPath));
3994 i->path = path ? nasm_strdup(path) : NULL;
3995 i->next = NULL;
3997 if (ipath != NULL) {
3998 IncPath *j = ipath;
3999 while (j->next != NULL)
4000 j = j->next;
4001 j->next = i;
4002 } else {
4003 ipath = i;
4008 * added by alexfru:
4010 * This function is used to "export" the include paths, e.g.
4011 * the paths specified in the '-I' command switch.
4012 * The need for such exporting is due to the 'incbin' directive,
4013 * which includes raw binary files (unlike '%include', which
4014 * includes text source files). It would be real nice to be
4015 * able to specify paths to search for incbin'ned files also.
4016 * So, this is a simple workaround.
4018 * The function use is simple:
4020 * The 1st call (with NULL argument) returns a pointer to the 1st path
4021 * (char** type) or NULL if none include paths available.
4023 * All subsequent calls take as argument the value returned by this
4024 * function last. The return value is either the next path
4025 * (char** type) or NULL if the end of the paths list is reached.
4027 * It is maybe not the best way to do things, but I didn't want
4028 * to export too much, just one or two functions and no types or
4029 * variables exported.
4031 * Can't say I like the current situation with e.g. this path list either,
4032 * it seems to be never deallocated after creation...
4034 char **pp_get_include_path_ptr(char **pPrevPath)
4036 /* This macro returns offset of a member of a structure */
4037 #define GetMemberOffset(StructType,MemberName)\
4038 ((size_t)&((StructType*)0)->MemberName)
4039 IncPath *i;
4041 if (pPrevPath == NULL) {
4042 if (ipath != NULL)
4043 return &ipath->path;
4044 else
4045 return NULL;
4047 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
4048 i = i->next;
4049 if (i != NULL)
4050 return &i->path;
4051 else
4052 return NULL;
4053 #undef GetMemberOffset
4056 void pp_pre_include(char *fname)
4058 Token *inc, *space, *name;
4059 Line *l;
4061 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4062 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4063 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
4065 l = nasm_malloc(sizeof(Line));
4066 l->next = predef;
4067 l->first = inc;
4068 l->finishes = false;
4069 predef = l;
4072 void pp_pre_define(char *definition)
4074 Token *def, *space;
4075 Line *l;
4076 char *equals;
4078 equals = strchr(definition, '=');
4079 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4080 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
4081 if (equals)
4082 *equals = ' ';
4083 space->next = tokenize(definition);
4084 if (equals)
4085 *equals = '=';
4087 l = nasm_malloc(sizeof(Line));
4088 l->next = predef;
4089 l->first = def;
4090 l->finishes = false;
4091 predef = l;
4094 void pp_pre_undefine(char *definition)
4096 Token *def, *space;
4097 Line *l;
4099 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4100 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
4101 space->next = tokenize(definition);
4103 l = nasm_malloc(sizeof(Line));
4104 l->next = predef;
4105 l->first = def;
4106 l->finishes = false;
4107 predef = l;
4111 * Added by Keith Kanios:
4113 * This function is used to assist with "runtime" preprocessor
4114 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4116 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4117 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4120 void pp_runtime(char *definition)
4122 Token *def;
4124 def = tokenize(definition);
4125 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4126 free_tlist(def);
4130 void pp_extra_stdmac(const char **macros)
4132 extrastdmac = macros;
4135 static void make_tok_num(Token * tok, int64_t val)
4137 char numbuf[20];
4138 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
4139 tok->text = nasm_strdup(numbuf);
4140 tok->type = TOK_NUMBER;
4143 Preproc nasmpp = {
4144 pp_reset,
4145 pp_getline,
4146 pp_cleanup