1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro;
85 typedef struct ExpDef ExpDef;
86 typedef struct ExpInv ExpInv;
87 typedef struct Context Context;
88 typedef struct Token Token;
89 typedef struct Blocks Blocks;
90 typedef struct Line Line;
91 typedef struct Include Include;
92 typedef struct Cond Cond;
93 typedef struct IncPath IncPath;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * The context stack is composed of a linked list of these.
122 struct hash_table localmac;
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
134 * %define a(x,y) ( (x) & ~(y) )
136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
157 #define PP_CONCAT_MASK(x) (1 << (x))
159 struct tokseq_match {
168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
171 enum pp_token_type type;
175 * Expansion definitions are stored as a linked list of
176 * these, which is essentially a container to allow several linked
179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
193 EXP_NONE = 0, EXP_PREDEF,
196 EXP_COMMENT, EXP_FINAL,
197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
211 ExpDef *prev; /* previous definition */
212 ExpDef *next; /* next in hash table */
213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
223 int prepend; /* label prepend state */
227 int linecount; /* number of lines within expansion */
229 int64_t def_depth; /* current number of definition pairs deep */
230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
238 * Store the invocation of an expansion.
240 * The `prev' field is for the `istk->expansion` linked-list.
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
252 Line *current; /* pointer to current line in invocation */
254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
261 int lineno; /* current line number in expansion */
262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
306 COND_IF_TRUE, COND_IF_FALSE,
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
322 COND_DONE, COND_NEVER
324 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
327 * These defines are used as the possible return values for do_directive
329 #define NO_DIRECTIVE_FOUND 0
330 #define DIRECTIVE_FOUND 1
333 * This define sets the upper limit for smacro and expansions
335 #define DEADMAN_LIMIT (1 << 20)
338 #define REP_LIMIT ((INT64_C(1) << 62))
341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
346 static const char * const conditions[] = {
347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
357 static const enum pp_conds inverse_ccs[] = {
358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 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,
360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
363 /* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
374 static const char * const tasm_directives[] = {
375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
379 static int StackSize = 4;
380 static char *StackPointer = "ebp";
381 static int ArgOffset = 8;
382 static int LocalOffset = 0;
384 static Context *cstk;
385 static Include *istk;
386 static IncPath *ipath = NULL;
388 static int pass; /* HACK: pass 0 = generate dependencies only */
389 static StrList **dephead, **deptail; /* Dependency list */
391 static uint64_t unique; /* unique identifier numbers */
393 static Line *predef = NULL;
394 static bool do_predef;
396 static ListGen *list;
399 * The current set of expansion definitions we have defined.
401 static struct hash_table expdefs;
404 * The current set of single-line macros we have defined.
406 static struct hash_table smacros;
409 * Linked List of all active expansion definitions
411 struct ExpDef *expansions = NULL;
414 * The expansion we are currently defining
416 static ExpDef *defining = NULL;
418 static uint64_t nested_mac_count;
419 static uint64_t nested_rep_count;
422 * Linked-list of lines to preprocess, prior to cleanup
424 static Line *finals = NULL;
425 static bool in_final = false;
428 * The number of macro parameters to allocate space for at a time.
430 #define PARAM_DELTA 16
433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
436 static macros_t *stdmacpos;
439 * The extra standard macros that come from the object format, if
442 static macros_t *extrastdmac = NULL;
443 static bool any_extrastdmac;
446 * Tokens are allocated in blocks to improve speed
448 #define TOKEN_BLOCKSIZE 4096
449 static Token *freeTokens = NULL;
455 static Blocks blocks = { NULL, NULL };
458 * Forward declarations.
460 static Token *expand_mmac_params(Token * tline);
461 static Token *expand_smacro(Token * tline);
462 static Token *expand_id(Token * tline);
463 static Context *get_ctx(const char *name, const char **namep,
465 static void make_tok_num(Token * tok, int64_t val);
466 static void error(int severity, const char *fmt, ...);
467 static void error_precond(int severity, const char *fmt, ...);
468 static void *new_Block(size_t size);
469 static void delete_Blocks(void);
470 static Token *new_Token(Token * next, enum pp_token_type type,
471 const char *text, int txtlen);
472 static Token *copy_Token(Token * tline);
473 static Token *delete_Token(Token * t);
474 static Line *new_Line(void);
475 static ExpDef *new_ExpDef(int exp_type);
476 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
479 * Macros for safe checking of token pointers, avoid *(NULL)
481 #define tok_type_(x,t) ((x) && (x)->type == (t))
482 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
488 #define dump_token(t) raw_dump_token(t, __FILE__, __LINE__, __func__);
489 static void raw_dump_token(Token *token, const char *file, int line, const char *func)
491 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
494 list_for_each(t, token) {
496 printf("'%s' ", t->text);
505 * nasm_unquote with error if the string contains NUL characters.
506 * If the string contains NUL characters, issue an error and return
507 * the C len, i.e. truncate at the NUL.
509 static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
511 size_t len = nasm_unquote(qstr, NULL);
512 size_t clen = strlen(qstr);
515 error(ERR_NONFATAL, "NUL character in `%s' directive",
516 pp_directives[directive]);
522 * In-place reverse a list of tokens.
524 static Token *reverse_tokens(Token *t)
540 * Handle TASM specific directives, which do not contain a % in
541 * front of them. We do it here because I could not find any other
542 * place to do it for the moment, and it is a hack (ideally it would
543 * be nice to be able to use the NASM pre-processor to do it).
545 static char *check_tasm_directive(char *line)
547 int32_t i, j, k, m, len;
548 char *p, *q, *oldline, oldchar;
550 p = nasm_skip_spaces(line);
552 /* Binary search for the directive name */
554 j = ARRAY_SIZE(tasm_directives);
555 q = nasm_skip_word(p);
562 m = nasm_stricmp(p, tasm_directives[k]);
564 /* We have found a directive, so jam a % in front of it
565 * so that NASM will then recognise it as one if it's own.
570 line = nasm_malloc(len + 2);
572 if (k == TM_IFDIFI) {
574 * NASM does not recognise IFDIFI, so we convert
575 * it to %if 0. This is not used in NASM
576 * compatible code, but does need to parse for the
577 * TASM macro package.
579 strcpy(line + 1, "if 0");
581 memcpy(line + 1, p, len + 1);
596 * The pre-preprocessing stage... This function translates line
597 * number indications as they emerge from GNU cpp (`# lineno "file"
598 * flags') into NASM preprocessor line number indications (`%line
601 static char *prepreproc(char *line)
604 char *fname, *oldline;
606 if (line[0] == '#' && line[1] == ' ') {
609 lineno = atoi(fname);
610 fname += strspn(fname, "0123456789 ");
613 fnlen = strcspn(fname, "\"");
614 line = nasm_malloc(20 + fnlen);
615 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
618 if (tasm_compatible_mode)
619 return check_tasm_directive(line);
624 * Free a linked list of tokens.
626 static void free_tlist(Token * list)
629 list = delete_Token(list);
633 * Free a linked list of lines.
635 static void free_llist(Line * list)
638 list_for_each_safe(l, tmp, list) {
639 free_tlist(l->first);
647 static void free_expdef(ExpDef * ed)
650 free_tlist(ed->dlist);
651 nasm_free(ed->defaults);
652 free_llist(ed->line);
659 static void free_expinv(ExpInv * ei)
661 if (ei->name != NULL)
663 if (ei->label_text != NULL)
664 nasm_free(ei->label_text);
669 * Free all currently defined macros, and free the hash tables
671 static void free_smacro_table(struct hash_table *smt)
675 struct hash_tbl_node *it = NULL;
677 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
678 nasm_free((void *)key);
679 list_for_each_safe(s, tmp, s) {
681 free_tlist(s->expansion);
688 static void free_expdef_table(struct hash_table *edt)
692 struct hash_tbl_node *it = NULL;
695 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
696 nasm_free((void *)key);
697 list_for_each_safe(ed ,tmp, ed)
703 static void free_macros(void)
705 free_smacro_table(&smacros);
706 free_expdef_table(&expdefs);
710 * Initialize the hash tables
712 static void init_macros(void)
714 hash_init(&smacros, HASH_LARGE);
715 hash_init(&expdefs, HASH_LARGE);
719 * Pop the context stack.
721 static void ctx_pop(void)
726 free_smacro_table(&c->localmac);
732 * Search for a key in the hash index; adding it if necessary
733 * (in which case we initialize the data pointer to NULL.)
736 hash_findi_add(struct hash_table *hash, const char *str)
738 struct hash_insert hi;
742 r = hash_findi(hash, str, &hi);
746 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
747 return hash_add(&hi, strx, NULL);
751 * Like hash_findi, but returns the data element rather than a pointer
752 * to it. Used only when not adding a new element, hence no third
756 hash_findix(struct hash_table *hash, const char *str)
760 p = hash_findi(hash, str, NULL);
761 return p ? *p : NULL;
765 * read line from standard macros set,
766 * if there no more left -- return NULL
768 static char *line_from_stdmac(void)
771 const unsigned char *p = stdmacpos;
780 len += pp_directives_len[c - 0x80] + 1;
785 line = nasm_malloc(len + 1);
787 while ((c = *stdmacpos++)) {
789 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
790 q += pp_directives_len[c - 0x80];
800 /* This was the last of the standard macro chain... */
802 if (any_extrastdmac) {
803 stdmacpos = extrastdmac;
804 any_extrastdmac = false;
805 } else if (do_predef) {
808 Token *head, **tail, *t;
811 * Nasty hack: here we push the contents of
812 * `predef' on to the top-level expansion stack,
813 * since this is the most convenient way to
814 * implement the pre-include and pre-define
817 list_for_each(pd, predef) {
820 list_for_each(t, pd->first) {
821 *tail = new_Token(NULL, t->type, t->text, 0);
822 tail = &(*tail)->next;
827 ei = new_ExpInv(EXP_PREDEF, NULL);
830 ei->prev = istk->expansion;
831 istk->expansion = ei;
840 #define BUF_DELTA 512
842 * Read a line from the top file in istk, handling multiple CR/LFs
843 * at the end of the line read, and handling spurious ^Zs. Will
844 * return lines from the standard macro set if this has not already
847 static char *read_line(void)
849 char *buffer, *p, *q;
850 int bufsize, continued_count;
853 * standart macros set (predefined) goes first
855 p = line_from_stdmac();
860 * regular read from a file
863 buffer = nasm_malloc(BUF_DELTA);
867 q = fgets(p, bufsize - (p - buffer), istk->fp);
871 if (p > buffer && p[-1] == '\n') {
873 * Convert backslash-CRLF line continuation sequences into
874 * nothing at all (for DOS and Windows)
876 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
882 * Also convert backslash-LF line continuation sequences into
883 * nothing at all (for Unix)
885 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
893 if (p - buffer > bufsize - 10) {
894 int32_t offset = p - buffer;
895 bufsize += BUF_DELTA;
896 buffer = nasm_realloc(buffer, bufsize);
897 p = buffer + offset; /* prevent stale-pointer problems */
901 if (!q && p == buffer) {
906 src_set_linnum(src_get_linnum() + istk->lineinc +
907 (continued_count * istk->lineinc));
910 * Play safe: remove CRs as well as LFs, if any of either are
911 * present at the end of the line.
913 while (--p >= buffer && (*p == '\n' || *p == '\r'))
917 * Handle spurious ^Z, which may be inserted into source files
918 * by some file transfer utilities.
920 buffer[strcspn(buffer, "\032")] = '\0';
922 list->line(LIST_READ, buffer);
928 * Tokenize a line of text. This is a very simple process since we
929 * don't need to parse the value out of e.g. numeric tokens: we
930 * simply split one string into many.
932 static Token *tokenize(char *line)
935 enum pp_token_type type;
937 Token *t, **tail = &list;
940 if ((defining != NULL) && (defining->ignoring == true)) {
948 if (*p == '+' && !nasm_isdigit(p[1])) {
951 } else if (nasm_isdigit(*p) ||
952 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
956 while (nasm_isdigit(*p));
957 type = TOK_PREPROC_ID;
958 } else if (*p == '{') {
960 while (*p && *p != '}') {
967 type = TOK_PREPROC_ID;
968 } else if (*p == '[') {
970 line += 2; /* Skip the leading %[ */
972 while (lvl && (c = *p++)) {
984 p = nasm_skip_string(p - 1) + 1;
994 error(ERR_NONFATAL, "unterminated %[ construct");
996 } else if (*p == '?') {
997 type = TOK_PREPROC_Q; /* %? */
1000 type = TOK_PREPROC_QQ; /* %?? */
1003 } else if (*p == '!') {
1004 type = TOK_PREPROC_ID;
1009 } while (isidchar(*p));
1010 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1011 p = nasm_skip_string(p);
1015 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1017 /* %! without string or identifier */
1018 type = TOK_OTHER; /* Legacy behavior... */
1020 } else if (isidchar(*p) ||
1021 ((*p == '!' || *p == '%' || *p == '$') &&
1026 while (isidchar(*p));
1027 type = TOK_PREPROC_ID;
1033 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1036 while (*p && isidchar(*p))
1038 } else if (*p == '\'' || *p == '"' || *p == '`') {
1043 p = nasm_skip_string(p);
1047 } else if(verbose) {
1048 error(ERR_WARNING|ERR_PASS1, "unterminated string");
1049 /* Handling unterminated strings by UNV */
1052 } else if (p[0] == '$' && p[1] == '$') {
1053 type = TOK_OTHER; /* TOKEN_BASE */
1055 } else if (isnumstart(*p)) {
1056 bool is_hex = false;
1057 bool is_float = false;
1073 if (!is_hex && (c == 'e' || c == 'E')) {
1075 if (*p == '+' || *p == '-') {
1077 * e can only be followed by +/- if it is either a
1078 * prefixed hex number or a floating-point number
1083 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1085 } else if (c == 'P' || c == 'p') {
1087 if (*p == '+' || *p == '-')
1089 } else if (isnumchar(c) || c == '_')
1090 ; /* just advance */
1091 else if (c == '.') {
1093 * we need to deal with consequences of the legacy
1094 * parser, like "1.nolist" being two tokens
1095 * (TOK_NUMBER, TOK_ID) here; at least give it
1096 * a shot for now. In the future, we probably need
1097 * a flex-based scanner with proper pattern matching
1098 * to do it as well as it can be done. Nothing in
1099 * the world is going to help the person who wants
1100 * 0x123.p16 interpreted as two tokens, though.
1106 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1107 (!is_hex && (*r == 'e' || *r == 'E')) ||
1108 (*r == 'p' || *r == 'P')) {
1112 break; /* Terminate the token */
1116 p--; /* Point to first character beyond number */
1118 if (p == line+1 && *line == '$') {
1119 type = TOK_OTHER; /* TOKEN_HERE */
1121 if (has_e && !is_hex) {
1122 /* 1e13 is floating-point, but 1e13h is not */
1126 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1128 } else if (nasm_isspace(*p)) {
1129 type = TOK_WHITESPACE;
1130 p = nasm_skip_spaces(p);
1132 * Whitespace just before end-of-line is discarded by
1133 * pretending it's a comment; whitespace just before a
1134 * comment gets lumped into the comment.
1136 if (!*p || *p == ';') {
1141 } else if (*p == ';') {
1147 * Anything else is an operator of some kind. We check
1148 * for all the double-character operators (>>, <<, //,
1149 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1150 * else is a single-character operator.
1153 if ((p[0] == '>' && p[1] == '>') ||
1154 (p[0] == '<' && p[1] == '<') ||
1155 (p[0] == '/' && p[1] == '/') ||
1156 (p[0] == '<' && p[1] == '=') ||
1157 (p[0] == '>' && p[1] == '=') ||
1158 (p[0] == '=' && p[1] == '=') ||
1159 (p[0] == '!' && p[1] == '=') ||
1160 (p[0] == '<' && p[1] == '>') ||
1161 (p[0] == '&' && p[1] == '&') ||
1162 (p[0] == '|' && p[1] == '|') ||
1163 (p[0] == '^' && p[1] == '^')) {
1169 /* Handling unterminated string by UNV */
1172 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1173 t->text[p-line] = *line;
1177 if (type != TOK_COMMENT) {
1178 *tail = t = new_Token(NULL, type, line, p - line);
1187 * this function allocates a new managed block of memory and
1188 * returns a pointer to the block. The managed blocks are
1189 * deleted only all at once by the delete_Blocks function.
1191 static void *new_Block(size_t size)
1193 Blocks *b = &blocks;
1195 /* first, get to the end of the linked list */
1199 /* now allocate the requested chunk */
1200 b->chunk = nasm_malloc(size);
1202 /* now allocate a new block for the next request */
1203 b->next = nasm_zalloc(sizeof(Blocks));
1209 * this function deletes all managed blocks of memory
1211 static void delete_Blocks(void)
1213 Blocks *a, *b = &blocks;
1216 * keep in mind that the first block, pointed to by blocks
1217 * is a static and not dynamically allocated, so we don't
1222 nasm_free(b->chunk);
1231 * this function creates a new Token and passes a pointer to it
1232 * back to the caller. It sets the type and text elements, and
1233 * also the a.mac and next elements to NULL.
1235 static Token *new_Token(Token * next, enum pp_token_type type,
1236 const char *text, int txtlen)
1242 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1243 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1244 freeTokens[i].next = &freeTokens[i + 1];
1245 freeTokens[i].next = NULL;
1248 freeTokens = t->next;
1252 if (type == TOK_WHITESPACE || !text) {
1256 txtlen = strlen(text);
1257 t->text = nasm_malloc(txtlen+1);
1258 memcpy(t->text, text, txtlen);
1259 t->text[txtlen] = '\0';
1264 static Token *copy_Token(Token * tline)
1266 Token *t, *tt, *first = NULL, *prev = NULL;
1268 for (tt = tline; tt != NULL; tt = tt->next) {
1270 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1271 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1272 freeTokens[i].next = &freeTokens[i + 1];
1273 freeTokens[i].next = NULL;
1276 freeTokens = t->next;
1278 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1279 t->a.mac = tt->a.mac;
1280 t->a.len = tt->a.len;
1292 static Token *delete_Token(Token * t)
1294 Token *next = t->next;
1296 t->next = freeTokens;
1302 * Convert a line of tokens back into text.
1303 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1304 * will be transformed into ..@ctxnum.xxx
1306 static char *detoken(Token * tlist, bool expand_locals)
1313 list_for_each(t, tlist) {
1314 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
1319 if (*v == '\'' || *v == '\"' || *v == '`') {
1320 size_t len = nasm_unquote(v, NULL);
1321 size_t clen = strlen(v);
1324 error(ERR_NONFATAL | ERR_PASS1,
1325 "NUL character in %! string");
1331 char *p = getenv(v);
1333 error(ERR_NONFATAL | ERR_PASS1,
1334 "nonexistent environment variable `%s'", v);
1337 t->text = nasm_strdup(p);
1342 /* Expand local macros here and not during preprocessing */
1343 if (expand_locals &&
1344 t->type == TOK_PREPROC_ID && t->text &&
1345 t->text[0] == '%' && t->text[1] == '$') {
1348 Context *ctx = get_ctx(t->text, &q, false);
1351 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
1352 p = nasm_strcat(buffer, q);
1358 /* Expand %? and %?? directives */
1359 if ((istk->expansion != NULL) &&
1360 ((t->type == TOK_PREPROC_Q) ||
1361 (t->type == TOK_PREPROC_QQ))) {
1363 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1364 if (ei->type == EXP_MMACRO) {
1366 if (t->type == TOK_PREPROC_Q) {
1367 t->text = nasm_strdup(ei->name);
1369 t->text = nasm_strdup(ei->def->name);
1376 if (t->type == TOK_WHITESPACE)
1379 len += strlen(t->text);
1382 p = line = nasm_malloc(len + 1);
1384 list_for_each(t, tlist) {
1385 if (t->type == TOK_WHITESPACE) {
1387 } else if (t->text) {
1399 * Initialize a new Line
1401 static inline Line *new_Line(void)
1403 return (Line *)nasm_zalloc(sizeof(Line));
1408 * Initialize a new Expansion Definition
1410 static ExpDef *new_ExpDef(int exp_type)
1412 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1413 ed->type = exp_type;
1414 ed->casesense = true;
1415 ed->state = COND_NEVER;
1422 * Initialize a new Expansion Instance
1424 static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1426 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1427 ei->type = exp_type;
1429 ei->unique = ++unique;
1431 if ((istk->mmac_depth < 1) &&
1432 (istk->expansion == NULL) &&
1434 (ed->type != EXP_MMACRO) &&
1435 (ed->type != EXP_REP) &&
1436 (ed->type != EXP_WHILE)) {
1437 ei->linnum = src_get_linnum();
1438 src_set_linnum(ei->linnum - ed->linecount - 1);
1442 if ((istk->expansion == NULL) ||
1443 (ei->type == EXP_MMACRO)) {
1446 ei->relno = istk->expansion->lineno;
1448 ei->relno -= (ed->linecount + 1);
1455 * A scanner, suitable for use by the expression evaluator, which
1456 * operates on a line of Tokens. Expects a pointer to a pointer to
1457 * the first token in the line to be passed in as its private_data
1460 * FIX: This really needs to be unified with stdscan.
1462 static int ppscan(void *private_data, struct tokenval *tokval)
1464 Token **tlineptr = private_data;
1466 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
1470 *tlineptr = tline ? tline->next : NULL;
1471 } while (tline && (tline->type == TOK_WHITESPACE ||
1472 tline->type == TOK_COMMENT));
1475 return tokval->t_type = TOKEN_EOS;
1477 tokval->t_charptr = tline->text;
1479 if (tline->text[0] == '$' && !tline->text[1])
1480 return tokval->t_type = TOKEN_HERE;
1481 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
1482 return tokval->t_type = TOKEN_BASE;
1484 if (tline->type == TOK_ID) {
1485 p = tokval->t_charptr = tline->text;
1487 tokval->t_charptr++;
1488 return tokval->t_type = TOKEN_ID;
1491 for (r = p, s = ourcopy; *r; r++) {
1492 if (r >= p+MAX_KEYWORD)
1493 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1494 *s++ = nasm_tolower(*r);
1497 /* right, so we have an identifier sitting in temp storage. now,
1498 * is it actually a register or instruction name, or what? */
1499 return nasm_token_hash(ourcopy, tokval);
1502 if (tline->type == TOK_NUMBER) {
1504 tokval->t_integer = readnum(tline->text, &rn_error);
1505 tokval->t_charptr = tline->text;
1507 return tokval->t_type = TOKEN_ERRNUM;
1509 return tokval->t_type = TOKEN_NUM;
1512 if (tline->type == TOK_FLOAT) {
1513 return tokval->t_type = TOKEN_FLOAT;
1516 if (tline->type == TOK_STRING) {
1519 bq = tline->text[0];
1520 tokval->t_charptr = tline->text;
1521 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
1523 if (ep[0] != bq || ep[1] != '\0')
1524 return tokval->t_type = TOKEN_ERRSTR;
1526 return tokval->t_type = TOKEN_STR;
1529 if (tline->type == TOK_OTHER) {
1530 if (!strcmp(tline->text, "<<"))
1531 return tokval->t_type = TOKEN_SHL;
1532 if (!strcmp(tline->text, ">>"))
1533 return tokval->t_type = TOKEN_SHR;
1534 if (!strcmp(tline->text, "//"))
1535 return tokval->t_type = TOKEN_SDIV;
1536 if (!strcmp(tline->text, "%%"))
1537 return tokval->t_type = TOKEN_SMOD;
1538 if (!strcmp(tline->text, "=="))
1539 return tokval->t_type = TOKEN_EQ;
1540 if (!strcmp(tline->text, "<>"))
1541 return tokval->t_type = TOKEN_NE;
1542 if (!strcmp(tline->text, "!="))
1543 return tokval->t_type = TOKEN_NE;
1544 if (!strcmp(tline->text, "<="))
1545 return tokval->t_type = TOKEN_LE;
1546 if (!strcmp(tline->text, ">="))
1547 return tokval->t_type = TOKEN_GE;
1548 if (!strcmp(tline->text, "&&"))
1549 return tokval->t_type = TOKEN_DBL_AND;
1550 if (!strcmp(tline->text, "^^"))
1551 return tokval->t_type = TOKEN_DBL_XOR;
1552 if (!strcmp(tline->text, "||"))
1553 return tokval->t_type = TOKEN_DBL_OR;
1557 * We have no other options: just return the first character of
1560 return tokval->t_type = tline->text[0];
1564 * Compare a string to the name of an existing macro; this is a
1565 * simple wrapper which calls either strcmp or nasm_stricmp
1566 * depending on the value of the `casesense' parameter.
1568 static int mstrcmp(const char *p, const char *q, bool casesense)
1570 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
1574 * Compare a string to the name of an existing macro; this is a
1575 * simple wrapper which calls either strcmp or nasm_stricmp
1576 * depending on the value of the `casesense' parameter.
1578 static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1580 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1584 * Return the Context structure associated with a %$ token. Return
1585 * NULL, having _already_ reported an error condition, if the
1586 * context stack isn't deep enough for the supplied number of $
1588 * If all_contexts == true, contexts that enclose current are
1589 * also scanned for such smacro, until it is found; if not -
1590 * only the context that directly results from the number of $'s
1591 * in variable's name.
1593 * If "namep" is non-NULL, set it to the pointer to the macro name
1594 * tail, i.e. the part beyond %$...
1596 static Context *get_ctx(const char *name, const char **namep,
1606 if (!name || name[0] != '%' || name[1] != '$')
1610 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1617 while (ctx && *name == '$') {
1623 error(ERR_NONFATAL, "`%s': context stack is only"
1624 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
1635 /* Search for this smacro in found context */
1636 m = hash_findix(&ctx->localmac, name);
1638 if (!mstrcmp(m->name, name, m->casesense))
1649 * Check to see if a file is already in a string list
1651 static bool in_list(const StrList *list, const char *str)
1654 if (!strcmp(list->str, str))
1662 * Open an include file. This routine must always return a valid
1663 * file pointer if it returns - it's responsible for throwing an
1664 * ERR_FATAL and bombing out completely if not. It should also try
1665 * the include path one by one until it finds the file or reaches
1666 * the end of the path.
1668 static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
1673 IncPath *ip = ipath;
1674 int len = strlen(file);
1675 size_t prefix_len = 0;
1679 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1681 memcpy(sl->str, prefix, prefix_len);
1682 memcpy(sl->str+prefix_len, file, len+1);
1683 fp = fopen(sl->str, "r");
1684 if (fp && dhead && !in_list(*dhead, sl->str)) {
1701 prefix_len = strlen(prefix);
1703 /* -MG given and file not found */
1704 if (dhead && !in_list(*dhead, file)) {
1705 sl = nasm_malloc(len+1+sizeof sl->next);
1707 strcpy(sl->str, file);
1715 error(ERR_FATAL, "unable to open include file `%s'", file);
1720 * Determine if we should warn on defining a single-line macro of
1721 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1722 * return true if _any_ single-line macro of that name is defined.
1723 * Otherwise, will return true if a single-line macro with either
1724 * `nparam' or no parameters is defined.
1726 * If a macro with precisely the right number of parameters is
1727 * defined, or nparam is -1, the address of the definition structure
1728 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1729 * is NULL, no action will be taken regarding its contents, and no
1732 * Note that this is also called with nparam zero to resolve
1735 * If you already know which context macro belongs to, you can pass
1736 * the context pointer as first parameter; if you won't but name begins
1737 * with %$ the context will be automatically computed. If all_contexts
1738 * is true, macro will be searched in outer contexts as well.
1741 smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
1744 struct hash_table *smtbl;
1748 smtbl = &ctx->localmac;
1749 } else if (name[0] == '%' && name[1] == '$') {
1751 ctx = get_ctx(name, &name, false);
1753 return false; /* got to return _something_ */
1754 smtbl = &ctx->localmac;
1758 m = (SMacro *) hash_findix(smtbl, name);
1761 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1762 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
1764 if (nparam == (int) m->nparam || nparam == -1)
1778 * Count and mark off the parameters in a multi-line macro call.
1779 * This is called both from within the multi-line macro expansion
1780 * code, and also to mark off the default parameters when provided
1781 * in a %macro definition line.
1783 static void count_mmac_params(Token * t, int *nparam, Token *** params)
1785 int paramsize, brace;
1787 *nparam = paramsize = 0;
1790 /* +1: we need space for the final NULL */
1791 if (*nparam+1 >= paramsize) {
1792 paramsize += PARAM_DELTA;
1793 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1797 if (tok_is_(t, "{"))
1799 (*params)[(*nparam)++] = t;
1800 while (tok_isnt_(t, brace ? "}" : ","))
1802 if (t) { /* got a comma/brace */
1806 * Now we've found the closing brace, look further
1810 if (tok_isnt_(t, ",")) {
1812 "braces do not enclose all of macro parameter");
1813 while (tok_isnt_(t, ","))
1817 t = t->next; /* eat the comma */
1824 * Determine whether one of the various `if' conditions is true or
1827 * We must free the tline we get passed.
1829 static bool if_condition(Token * tline, enum preproc_token ct)
1831 enum pp_conditional i = PP_COND(ct);
1833 Token *t, *tt, **tptr, *origline;
1834 struct tokenval tokval;
1836 enum pp_token_type needtype;
1843 j = false; /* have we matched yet? */
1848 if (tline->type != TOK_ID) {
1850 "`%s' expects context identifiers", pp_directives[ct]);
1851 free_tlist(origline);
1854 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
1856 tline = tline->next;
1861 j = false; /* have we matched yet? */
1864 if (!tline || (tline->type != TOK_ID &&
1865 (tline->type != TOK_PREPROC_ID ||
1866 tline->text[1] != '$'))) {
1868 "`%s' expects macro identifiers", pp_directives[ct]);
1871 if (smacro_defined(NULL, tline->text, 0, NULL, true))
1873 tline = tline->next;
1878 tline = expand_smacro(tline);
1879 j = false; /* have we matched yet? */
1882 if (!tline || (tline->type != TOK_ID &&
1883 tline->type != TOK_STRING &&
1884 (tline->type != TOK_PREPROC_ID ||
1885 tline->text[1] != '!'))) {
1887 "`%s' expects environment variable names",
1892 if (tline->type == TOK_PREPROC_ID)
1893 p += 2; /* Skip leading %! */
1894 if (*p == '\'' || *p == '\"' || *p == '`')
1895 nasm_unquote_cstr(p, ct);
1898 tline = tline->next;
1904 tline = expand_smacro(tline);
1906 while (tok_isnt_(tt, ","))
1910 "`%s' expects two comma-separated arguments",
1915 j = true; /* assume equality unless proved not */
1916 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1917 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1918 error(ERR_NONFATAL, "`%s': more than one comma on line",
1922 if (t->type == TOK_WHITESPACE) {
1926 if (tt->type == TOK_WHITESPACE) {
1930 if (tt->type != t->type) {
1931 j = false; /* found mismatching tokens */
1934 /* When comparing strings, need to unquote them first */
1935 if (t->type == TOK_STRING) {
1936 size_t l1 = nasm_unquote(t->text, NULL);
1937 size_t l2 = nasm_unquote(tt->text, NULL);
1943 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1947 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
1948 j = false; /* found mismatching tokens */
1955 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1956 j = false; /* trailing gunk on one end or other */
1962 ExpDef searching, *ed;
1965 tline = expand_id(tline);
1966 if (!tok_type_(tline, TOK_ID)) {
1968 "`%s' expects a macro name", pp_directives[ct]);
1971 memset(&searching, 0, sizeof(searching));
1972 searching.name = nasm_strdup(tline->text);
1973 searching.casesense = true;
1974 searching.nparam_max = INT_MAX;
1975 tline = expand_smacro(tline->next);
1978 } else if (!tok_type_(tline, TOK_NUMBER)) {
1980 "`%s' expects a parameter count or nothing",
1983 searching.nparam_min = searching.nparam_max =
1984 readnum(tline->text, &j);
1987 "unable to parse parameter count `%s'",
1990 if (tline && tok_is_(tline->next, "-")) {
1991 tline = tline->next->next;
1992 if (tok_is_(tline, "*"))
1993 searching.nparam_max = INT_MAX;
1994 else if (!tok_type_(tline, TOK_NUMBER))
1996 "`%s' expects a parameter count after `-'",
1999 searching.nparam_max = readnum(tline->text, &j);
2002 "unable to parse parameter count `%s'",
2004 if (searching.nparam_min > searching.nparam_max)
2006 "minimum parameter count exceeds maximum");
2009 if (tline && tok_is_(tline->next, "+")) {
2010 tline = tline->next;
2011 searching.plus = true;
2013 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2014 while (ed != NULL) {
2015 if (!strcmp(ed->name, searching.name) &&
2016 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2017 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
2023 if (tline && tline->next)
2024 error(ERR_WARNING|ERR_PASS1,
2025 "trailing garbage after %%ifmacro ignored");
2026 nasm_free(searching.name);
2035 needtype = TOK_NUMBER;
2038 needtype = TOK_STRING;
2042 t = tline = expand_smacro(tline);
2044 while (tok_type_(t, TOK_WHITESPACE) ||
2045 (needtype == TOK_NUMBER &&
2046 tok_type_(t, TOK_OTHER) &&
2047 (t->text[0] == '-' || t->text[0] == '+') &&
2051 j = tok_type_(t, needtype);
2055 t = tline = expand_smacro(tline);
2056 while (tok_type_(t, TOK_WHITESPACE))
2061 t = t->next; /* Skip the actual token */
2062 while (tok_type_(t, TOK_WHITESPACE))
2064 j = !t; /* Should be nothing left */
2069 t = tline = expand_smacro(tline);
2070 while (tok_type_(t, TOK_WHITESPACE))
2073 j = !t; /* Should be empty */
2077 t = tline = expand_smacro(tline);
2079 tokval.t_type = TOKEN_INVALID;
2080 evalresult = evaluate(ppscan, tptr, &tokval,
2081 NULL, pass | CRITICAL, error, NULL);
2085 error(ERR_WARNING|ERR_PASS1,
2086 "trailing garbage after expression ignored");
2087 if (!is_simple(evalresult)) {
2089 "non-constant value given to `%s'", pp_directives[ct]);
2092 j = reloc_value(evalresult) != 0;
2097 "preprocessor directive `%s' not yet implemented",
2102 free_tlist(origline);
2103 return j ^ PP_NEGATIVE(ct);
2106 free_tlist(origline);
2111 * Common code for defining an smacro
2113 static bool define_smacro(Context *ctx, const char *mname, bool casesense,
2114 int nparam, Token *expansion)
2116 SMacro *smac, **smhead;
2117 struct hash_table *smtbl;
2119 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2121 error(ERR_WARNING|ERR_PASS1,
2122 "single-line macro `%s' defined both with and"
2123 " without parameters", mname);
2125 * Some instances of the old code considered this a failure,
2126 * some others didn't. What is the right thing to do here?
2128 free_tlist(expansion);
2129 return false; /* Failure */
2132 * We're redefining, so we have to take over an
2133 * existing SMacro structure. This means freeing
2134 * what was already in it.
2136 nasm_free(smac->name);
2137 free_tlist(smac->expansion);
2140 smtbl = ctx ? &ctx->localmac : &smacros;
2141 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2142 smac = nasm_zalloc(sizeof(SMacro));
2143 smac->next = *smhead;
2146 smac->name = nasm_strdup(mname);
2147 smac->casesense = casesense;
2148 smac->nparam = nparam;
2149 smac->expansion = expansion;
2150 smac->in_progress = false;
2151 return true; /* Success */
2155 * Undefine an smacro
2157 static void undef_smacro(Context *ctx, const char *mname)
2159 SMacro **smhead, *s, **sp;
2160 struct hash_table *smtbl;
2162 smtbl = ctx ? &ctx->localmac : &smacros;
2163 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
2167 * We now have a macro name... go hunt for it.
2170 while ((s = *sp) != NULL) {
2171 if (!mstrcmp(s->name, mname, s->casesense)) {
2174 free_tlist(s->expansion);
2184 * Parse a mmacro specification.
2186 static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
2190 tline = tline->next;
2192 tline = expand_id(tline);
2193 if (!tok_type_(tline, TOK_ID)) {
2194 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2198 def->name = nasm_strdup(tline->text);
2200 def->nolist = false;
2201 // def->in_progress = 0;
2202 // def->rep_nest = NULL;
2203 def->nparam_min = 0;
2204 def->nparam_max = 0;
2206 tline = expand_smacro(tline->next);
2208 if (!tok_type_(tline, TOK_NUMBER)) {
2209 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
2211 def->nparam_min = def->nparam_max =
2212 readnum(tline->text, &err);
2215 "unable to parse parameter count `%s'", tline->text);
2217 if (tline && tok_is_(tline->next, "-")) {
2218 tline = tline->next->next;
2219 if (tok_is_(tline, "*")) {
2220 def->nparam_max = INT_MAX;
2221 } else if (!tok_type_(tline, TOK_NUMBER)) {
2223 "`%s' expects a parameter count after `-'", directive);
2225 def->nparam_max = readnum(tline->text, &err);
2227 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2230 if (def->nparam_min > def->nparam_max) {
2231 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2235 if (tline && tok_is_(tline->next, "+")) {
2236 tline = tline->next;
2239 if (tline && tok_type_(tline->next, TOK_ID) &&
2240 !nasm_stricmp(tline->next->text, ".nolist")) {
2241 tline = tline->next;
2246 * Handle default parameters.
2248 if (tline && tline->next) {
2249 def->dlist = tline->next;
2251 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2254 def->defaults = NULL;
2258 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
2260 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2261 "too many default macro parameters");
2268 * Decode a size directive
2270 static int parse_size(const char *str) {
2271 static const char *size_names[] =
2272 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2273 static const int sizes[] =
2274 { 0, 1, 4, 16, 8, 10, 2, 32 };
2276 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
2280 * find and process preprocessor directive in passed line
2281 * Find out if a line contains a preprocessor directive, and deal
2284 * If a directive _is_ found, it is the responsibility of this routine
2285 * (and not the caller) to free_tlist() the line.
2287 * @param tline a pointer to the current tokeninzed line linked list
2288 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2291 static int do_directive(Token * tline)
2293 enum preproc_token i;
2306 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2307 struct tokenval tokval;
2309 ExpDef *ed, *eed, **edhead;
2318 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
2319 (tline->text[1] == '%' || tline->text[1] == '$'
2320 || tline->text[1] == '!'))
2321 return NO_DIRECTIVE_FOUND;
2323 i = pp_token_hash(tline->text);
2327 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2328 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2330 return NO_DIRECTIVE_FOUND; /* didn't get it */
2333 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2334 /* Directive to tell NASM what the default stack size is. The
2335 * default is for a 16-bit stack, and this can be overriden with
2338 tline = tline->next;
2339 if (tline && tline->type == TOK_WHITESPACE)
2340 tline = tline->next;
2341 if (!tline || tline->type != TOK_ID) {
2342 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2346 if (nasm_stricmp(tline->text, "flat") == 0) {
2347 /* All subsequent ARG directives are for a 32-bit stack */
2349 StackPointer = "ebp";
2352 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2353 /* All subsequent ARG directives are for a 64-bit stack */
2355 StackPointer = "rbp";
2358 } else if (nasm_stricmp(tline->text, "large") == 0) {
2359 /* All subsequent ARG directives are for a 16-bit stack,
2360 * far function call.
2363 StackPointer = "bp";
2366 } else if (nasm_stricmp(tline->text, "small") == 0) {
2367 /* All subsequent ARG directives are for a 16-bit stack,
2368 * far function call. We don't support near functions.
2371 StackPointer = "bp";
2375 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2379 free_tlist(origline);
2380 return DIRECTIVE_FOUND;
2383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2384 /* TASM like ARG directive to define arguments to functions, in
2385 * the following form:
2387 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2391 char *arg, directive[256];
2392 int size = StackSize;
2394 /* Find the argument name */
2395 tline = tline->next;
2396 if (tline && tline->type == TOK_WHITESPACE)
2397 tline = tline->next;
2398 if (!tline || tline->type != TOK_ID) {
2399 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2405 /* Find the argument size type */
2406 tline = tline->next;
2407 if (!tline || tline->type != TOK_OTHER
2408 || tline->text[0] != ':') {
2410 "Syntax error processing `%%arg' directive");
2411 free_tlist(origline);
2412 return DIRECTIVE_FOUND;
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_ID) {
2416 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2417 free_tlist(origline);
2418 return DIRECTIVE_FOUND;
2421 /* Allow macro expansion of type parameter */
2422 tt = tokenize(tline->text);
2423 tt = expand_smacro(tt);
2424 size = parse_size(tt->text);
2427 "Invalid size type for `%%arg' missing directive");
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2434 /* Round up to even stack slots */
2435 size = ALIGN(size, StackSize);
2437 /* Now define the macro for the argument */
2438 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2439 arg, StackPointer, offset);
2440 do_directive(tokenize(directive));
2443 /* Move to the next argument in the list */
2444 tline = tline->next;
2445 if (tline && tline->type == TOK_WHITESPACE)
2446 tline = tline->next;
2447 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2453 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2454 /* TASM like LOCAL directive to define local variables for a
2455 * function, in the following form:
2457 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2459 * The '= LocalSize' at the end is ignored by NASM, but is
2460 * required by TASM to define the local parameter size (and used
2461 * by the TASM macro package).
2463 offset = LocalOffset;
2465 char *local, directive[256];
2466 int size = StackSize;
2468 /* Find the argument name */
2469 tline = tline->next;
2470 if (tline && tline->type == TOK_WHITESPACE)
2471 tline = tline->next;
2472 if (!tline || tline->type != TOK_ID) {
2474 "`%%local' missing argument parameter");
2475 free_tlist(origline);
2476 return DIRECTIVE_FOUND;
2478 local = tline->text;
2480 /* Find the argument size type */
2481 tline = tline->next;
2482 if (!tline || tline->type != TOK_OTHER
2483 || tline->text[0] != ':') {
2485 "Syntax error processing `%%local' directive");
2486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
2489 tline = tline->next;
2490 if (!tline || tline->type != TOK_ID) {
2492 "`%%local' missing size type parameter");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2497 /* Allow macro expansion of type parameter */
2498 tt = tokenize(tline->text);
2499 tt = expand_smacro(tt);
2500 size = parse_size(tt->text);
2503 "Invalid size type for `%%local' missing directive");
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2510 /* Round up to even stack slots */
2511 size = ALIGN(size, StackSize);
2513 offset += size; /* Negative offset, increment before */
2515 /* Now define the macro for the argument */
2516 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2517 local, StackPointer, offset);
2518 do_directive(tokenize(directive));
2520 /* Now define the assign to setup the enter_c macro correctly */
2521 snprintf(directive, sizeof(directive),
2522 "%%assign %%$localsize %%$localsize+%d", size);
2523 do_directive(tokenize(directive));
2525 /* Move to the next argument in the list */
2526 tline = tline->next;
2527 if (tline && tline->type == TOK_WHITESPACE)
2528 tline = tline->next;
2529 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2530 LocalOffset = offset;
2531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
2535 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2537 error(ERR_WARNING|ERR_PASS1,
2538 "trailing garbage after `%%clear' ignored");
2541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2545 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2546 t = tline->next = expand_smacro(tline->next);
2548 if (!t || (t->type != TOK_STRING &&
2549 t->type != TOK_INTERNAL_STRING)) {
2550 error(ERR_NONFATAL, "`%%depend' expects a file name");
2551 free_tlist(origline);
2552 return DIRECTIVE_FOUND; /* but we did _something_ */
2555 error(ERR_WARNING|ERR_PASS1,
2556 "trailing garbage after `%%depend' ignored");
2558 if (t->type != TOK_INTERNAL_STRING)
2559 nasm_unquote_cstr(p, i);
2560 if (dephead && !in_list(*dephead, p)) {
2561 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2565 deptail = &sl->next;
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND;
2571 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2572 t = tline->next = expand_smacro(tline->next);
2575 if (!t || (t->type != TOK_STRING &&
2576 t->type != TOK_INTERNAL_STRING)) {
2577 error(ERR_NONFATAL, "`%%include' expects a file name");
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND; /* but we did _something_ */
2582 error(ERR_WARNING|ERR_PASS1,
2583 "trailing garbage after `%%include' ignored");
2585 if (t->type != TOK_INTERNAL_STRING)
2586 nasm_unquote_cstr(p, i);
2587 inc = nasm_zalloc(sizeof(Include));
2589 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
2591 /* -MG given but file not found */
2594 inc->fname = src_set_fname(nasm_strdup(p));
2595 inc->lineno = src_set_linnum(0);
2597 inc->expansion = NULL;
2599 list->uplevel(LIST_INCLUDE);
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
2605 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2607 static macros_t *use_pkg;
2608 const char *pkg_macro = NULL;
2610 tline = tline->next;
2612 tline = expand_id(tline);
2614 if (!tline || (tline->type != TOK_STRING &&
2615 tline->type != TOK_INTERNAL_STRING &&
2616 tline->type != TOK_ID)) {
2617 error(ERR_NONFATAL, "`%%use' expects a package name");
2618 free_tlist(origline);
2619 return DIRECTIVE_FOUND; /* but we did _something_ */
2622 error(ERR_WARNING|ERR_PASS1,
2623 "trailing garbage after `%%use' ignored");
2624 if (tline->type == TOK_STRING)
2625 nasm_unquote_cstr(tline->text, i);
2626 use_pkg = nasm_stdmac_find_package(tline->text);
2628 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2630 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2631 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2632 /* Not already included, go ahead and include it */
2633 stdmacpos = use_pkg;
2635 free_tlist(origline);
2636 return DIRECTIVE_FOUND;
2641 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2642 tline = tline->next;
2644 tline = expand_id(tline);
2646 if (!tok_type_(tline, TOK_ID)) {
2647 error(ERR_NONFATAL, "`%s' expects a context identifier",
2649 free_tlist(origline);
2650 return DIRECTIVE_FOUND; /* but we did _something_ */
2653 error(ERR_WARNING|ERR_PASS1,
2654 "trailing garbage after `%s' ignored",
2656 p = nasm_strdup(tline->text);
2658 p = NULL; /* Anonymous */
2662 ctx = nasm_zalloc(sizeof(Context));
2664 hash_init(&ctx->localmac, HASH_SMALL);
2666 ctx->number = unique++;
2671 error(ERR_NONFATAL, "`%s': context stack is empty",
2673 } else if (i == PP_POP) {
2674 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2675 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2677 cstk->name ? cstk->name : "anonymous", p);
2682 nasm_free(cstk->name);
2688 free_tlist(origline);
2689 return DIRECTIVE_FOUND;
2691 severity = ERR_FATAL;
2694 severity = ERR_NONFATAL;
2697 severity = ERR_WARNING|ERR_WARN_USER;
2701 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2703 /* Only error out if this is the final pass */
2704 if (pass != 2 && i != PP_FATAL)
2705 return DIRECTIVE_FOUND;
2707 tline->next = expand_smacro(tline->next);
2708 tline = tline->next;
2710 t = tline ? tline->next : NULL;
2712 if (tok_type_(tline, TOK_STRING) && !t) {
2713 /* The line contains only a quoted string */
2715 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2716 error(severity, "%s", p);
2718 /* Not a quoted string, or more than a quoted string */
2719 p = detoken(tline, false);
2720 error(severity, "%s", p);
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
2728 if (defining != NULL) {
2729 if (defining->type == EXP_IF) {
2730 defining->def_depth ++;
2732 return NO_DIRECTIVE_FOUND;
2734 if ((istk->expansion != NULL) &&
2735 (istk->expansion->emitting == false)) {
2738 j = if_condition(tline->next, i);
2739 tline->next = NULL; /* it got freed */
2740 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2742 ed = new_ExpDef(EXP_IF);
2748 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2749 ed->prev = defining;
2751 free_tlist(origline);
2752 return DIRECTIVE_FOUND;
2755 if (defining != NULL) {
2756 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2757 return NO_DIRECTIVE_FOUND;
2760 if ((defining == NULL) || (defining->type != EXP_IF)) {
2761 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2763 switch (defining->state) {
2765 defining->state = COND_DONE;
2766 defining->ignoring = true;
2771 defining->ignoring = true;
2774 case COND_ELSE_TRUE:
2775 case COND_ELSE_FALSE:
2776 error_precond(ERR_WARNING|ERR_PASS1,
2777 "`%%elif' after `%%else' ignored");
2778 defining->state = COND_NEVER;
2779 defining->ignoring = true;
2784 * IMPORTANT: In the case of %if, we will already have
2785 * called expand_mmac_params(); however, if we're
2786 * processing an %elif we must have been in a
2787 * non-emitting mode, which would have inhibited
2788 * the normal invocation of expand_mmac_params().
2789 * Therefore, we have to do it explicitly here.
2791 j = if_condition(expand_mmac_params(tline->next), i);
2792 tline->next = NULL; /* it got freed */
2794 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2795 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
2798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
2802 if (defining != NULL) {
2803 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2804 return NO_DIRECTIVE_FOUND;
2808 error_precond(ERR_WARNING|ERR_PASS1,
2809 "trailing garbage after `%%else' ignored");
2810 if ((defining == NULL) || (defining->type != EXP_IF)) {
2811 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2813 switch (defining->state) {
2816 defining->state = COND_ELSE_FALSE;
2817 defining->ignoring = true;
2821 defining->ignoring = true;
2825 defining->state = COND_ELSE_TRUE;
2826 defining->ignoring = false;
2829 case COND_ELSE_TRUE:
2830 case COND_ELSE_FALSE:
2831 error_precond(ERR_WARNING|ERR_PASS1,
2832 "`%%else' after `%%else' ignored.");
2833 defining->state = COND_NEVER;
2834 defining->ignoring = true;
2837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
2841 if (defining != NULL) {
2842 if (defining->type == EXP_IF) {
2843 if (defining->def_depth > 0) {
2844 defining->def_depth --;
2845 return NO_DIRECTIVE_FOUND;
2848 return NO_DIRECTIVE_FOUND;
2852 error_precond(ERR_WARNING|ERR_PASS1,
2853 "trailing garbage after `%%endif' ignored");
2854 if ((defining == NULL) || (defining->type != EXP_IF)) {
2855 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2856 return DIRECTIVE_FOUND;
2859 defining = ed->prev;
2860 ed->prev = expansions;
2862 ei = new_ExpInv(EXP_IF, ed);
2863 ei->current = ed->line;
2864 ei->emitting = true;
2865 ei->prev = istk->expansion;
2866 istk->expansion = ei;
2867 free_tlist(origline);
2868 return DIRECTIVE_FOUND;
2874 if (defining != NULL) {
2875 if (defining->type == EXP_MMACRO) {
2876 defining->def_depth ++;
2878 return NO_DIRECTIVE_FOUND;
2880 ed = new_ExpDef(EXP_MMACRO);
2882 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2883 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2884 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2887 return DIRECTIVE_FOUND;
2891 ed->max_depth = (ed->max_depth + 1);
2892 ed->ignoring = false;
2893 ed->prev = defining;
2896 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2898 if (!strcmp(eed->name, ed->name) &&
2899 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2900 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
2901 error(ERR_WARNING|ERR_PASS1,
2902 "redefining multi-line macro `%s'", ed->name);
2903 return DIRECTIVE_FOUND;
2907 free_tlist(origline);
2908 return DIRECTIVE_FOUND;
2912 if (defining != NULL) {
2913 if (defining->type == EXP_MMACRO) {
2914 if (defining->def_depth > 0) {
2915 defining->def_depth --;
2916 return NO_DIRECTIVE_FOUND;
2919 return NO_DIRECTIVE_FOUND;
2922 if (!(defining) || (defining->type != EXP_MMACRO)) {
2923 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2924 return DIRECTIVE_FOUND;
2926 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2927 defining->next = *edhead;
2930 defining = ed->prev;
2931 ed->prev = expansions;
2934 free_tlist(origline);
2935 return DIRECTIVE_FOUND;
2938 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2940 * We must search along istk->expansion until we hit a
2941 * macro invocation. Then we disable the emitting state(s)
2942 * between exitmacro and endmacro.
2944 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2945 if(ei->type == EXP_MMACRO) {
2952 * Set all invocations leading back to the macro
2953 * invocation to a non-emitting state.
2955 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2956 eei->emitting = false;
2958 eei->emitting = false;
2960 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2962 free_tlist(origline);
2963 return DIRECTIVE_FOUND;
2967 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2972 spec.casesense = (i == PP_UNMACRO);
2973 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2974 return DIRECTIVE_FOUND;
2976 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2977 while (ed_p && *ed_p) {
2979 if (ed->casesense == spec.casesense &&
2980 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2981 ed->nparam_min == spec.nparam_min &&
2982 ed->nparam_max == spec.nparam_max &&
2983 ed->plus == spec.plus) {
2984 if (ed->cur_depth > 0) {
2985 error(ERR_NONFATAL, "`%s' ignored on active macro",
2996 free_tlist(origline);
2997 free_tlist(spec.dlist);
2998 return DIRECTIVE_FOUND;
3002 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3003 if (tline->next && tline->next->type == TOK_WHITESPACE)
3004 tline = tline->next;
3006 free_tlist(origline);
3007 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3008 return DIRECTIVE_FOUND;
3010 t = expand_smacro(tline->next);
3012 free_tlist(origline);
3015 tokval.t_type = TOKEN_INVALID;
3017 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3020 return DIRECTIVE_FOUND;
3022 error(ERR_WARNING|ERR_PASS1,
3023 "trailing garbage after expression ignored");
3024 if (!is_simple(evalresult)) {
3025 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3026 return DIRECTIVE_FOUND;
3028 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3029 if (ei->type == EXP_MMACRO) {
3034 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3035 } else if (ei->nparam == 0) {
3037 "`%%rotate' invoked within macro without parameters");
3039 int rotate = ei->rotate + reloc_value(evalresult);
3041 rotate %= (int)ei->nparam;
3043 rotate += ei->nparam;
3044 ei->rotate = rotate;
3046 return DIRECTIVE_FOUND;
3049 if (defining != NULL) {
3050 if (defining->type == EXP_REP) {
3051 defining->def_depth ++;
3053 return NO_DIRECTIVE_FOUND;
3057 tline = tline->next;
3058 } while (tok_type_(tline, TOK_WHITESPACE));
3060 if (tok_type_(tline, TOK_ID) &&
3061 nasm_stricmp(tline->text, ".nolist") == 0) {
3064 tline = tline->next;
3065 } while (tok_type_(tline, TOK_WHITESPACE));
3069 t = expand_smacro(tline);
3071 tokval.t_type = TOKEN_INVALID;
3073 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3075 free_tlist(origline);
3076 return DIRECTIVE_FOUND;
3079 error(ERR_WARNING|ERR_PASS1,
3080 "trailing garbage after expression ignored");
3081 if (!is_simple(evalresult)) {
3082 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3083 return DIRECTIVE_FOUND;
3085 count = reloc_value(evalresult);
3086 if (count >= REP_LIMIT) {
3087 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
3092 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
3095 free_tlist(origline);
3096 ed = new_ExpDef(EXP_REP);
3097 ed->nolist = nolist;
3100 ed->max_depth = (count - 1);
3101 ed->ignoring = false;
3102 ed->prev = defining;
3104 return DIRECTIVE_FOUND;
3107 if (defining != NULL) {
3108 if (defining->type == EXP_REP) {
3109 if (defining->def_depth > 0) {
3110 defining->def_depth --;
3111 return NO_DIRECTIVE_FOUND;
3114 return NO_DIRECTIVE_FOUND;
3117 if ((defining == NULL) || (defining->type != EXP_REP)) {
3118 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3119 return DIRECTIVE_FOUND;
3123 * Now we have a "macro" defined - although it has no name
3124 * and we won't be entering it in the hash tables - we must
3125 * push a macro-end marker for it on to istk->expansion.
3126 * After that, it will take care of propagating itself (a
3127 * macro-end marker line for a macro which is really a %rep
3128 * block will cause the macro to be re-expanded, complete
3129 * with another macro-end marker to ensure the process
3130 * continues) until the whole expansion is forcibly removed
3131 * from istk->expansion by a %exitrep.
3134 defining = ed->prev;
3135 ed->prev = expansions;
3137 ei = new_ExpInv(EXP_REP, ed);
3138 ei->current = ed->line;
3139 ei->emitting = ((ed->max_depth > 0) ? true : false);
3140 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3141 ei->prev = istk->expansion;
3142 istk->expansion = ei;
3143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3147 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3149 * We must search along istk->expansion until we hit a
3150 * rep invocation. Then we disable the emitting state(s)
3151 * between exitrep and endrep.
3153 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3154 if (ei->type == EXP_REP) {
3161 * Set all invocations leading back to the rep
3162 * invocation to a non-emitting state.
3164 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3165 eei->emitting = false;
3167 eei->emitting = false;
3168 eei->current = NULL;
3169 eei->def->cur_depth = eei->def->max_depth;
3171 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3173 free_tlist(origline);
3174 return DIRECTIVE_FOUND;
3180 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3181 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
3183 tline = tline->next;
3185 tline = expand_id(tline);
3186 if (!tline || (tline->type != TOK_ID &&
3187 (tline->type != TOK_PREPROC_ID ||
3188 tline->text[1] != '$'))) {
3189 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
3195 ctx = get_ctx(tline->text, &mname, false);
3197 param_start = tline = tline->next;
3200 /* Expand the macro definition now for %xdefine and %ixdefine */
3201 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3202 tline = expand_smacro(tline);
3204 if (tok_is_(tline, "(")) {
3206 * This macro has parameters.
3209 tline = tline->next;
3213 error(ERR_NONFATAL, "parameter identifier expected");
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3217 if (tline->type != TOK_ID) {
3219 "`%s': parameter identifier expected",
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3224 tline->type = TOK_SMAC_PARAM + nparam++;
3225 tline = tline->next;
3227 if (tok_is_(tline, ",")) {
3228 tline = tline->next;
3230 if (!tok_is_(tline, ")")) {
3232 "`)' expected to terminate macro template");
3233 free_tlist(origline);
3234 return DIRECTIVE_FOUND;
3240 tline = tline->next;
3242 if (tok_type_(tline, TOK_WHITESPACE))
3243 last = tline, tline = tline->next;
3248 if (t->type == TOK_ID) {
3249 list_for_each(tt, param_start)
3250 if (tt->type >= TOK_SMAC_PARAM &&
3251 !strcmp(tt->text, t->text))
3255 t->next = macro_start;
3260 * Good. We now have a macro name, a parameter count, and a
3261 * token list (in reverse order) for an expansion. We ought
3262 * to be OK just to create an SMacro, store it, and let
3263 * free_tlist have the rest of the line (which we have
3264 * carefully re-terminated after chopping off the expansion
3267 define_smacro(ctx, mname, casesense, nparam, macro_start);
3268 free_tlist(origline);
3269 return DIRECTIVE_FOUND;
3272 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3273 tline = tline->next;
3275 tline = expand_id(tline);
3276 if (!tline || (tline->type != TOK_ID &&
3277 (tline->type != TOK_PREPROC_ID ||
3278 tline->text[1] != '$'))) {
3279 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND;
3284 error(ERR_WARNING|ERR_PASS1,
3285 "trailing garbage after macro name ignored");
3288 /* Find the context that symbol belongs to */
3289 ctx = get_ctx(tline->text, &mname, false);
3290 undef_smacro(ctx, mname);
3291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3296 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3297 casesense = (i == PP_DEFSTR);
3299 tline = tline->next;
3301 tline = expand_id(tline);
3302 if (!tline || (tline->type != TOK_ID &&
3303 (tline->type != TOK_PREPROC_ID ||
3304 tline->text[1] != '$'))) {
3305 error(ERR_NONFATAL, "`%s' expects a macro identifier",
3307 free_tlist(origline);
3308 return DIRECTIVE_FOUND;
3311 ctx = get_ctx(tline->text, &mname, false);
3313 tline = expand_smacro(tline->next);
3316 while (tok_type_(tline, TOK_WHITESPACE))
3317 tline = delete_Token(tline);
3319 p = detoken(tline, false);
3320 macro_start = nasm_zalloc(sizeof(*macro_start));
3321 macro_start->text = nasm_quote(p, strlen(p));
3322 macro_start->type = TOK_STRING;
3326 * We now have a macro name, an implicit parameter count of
3327 * zero, and a string token to use as an expansion. Create
3328 * and store an SMacro.
3330 define_smacro(ctx, mname, casesense, 0, macro_start);
3331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
3336 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3337 casesense = (i == PP_DEFTOK);
3339 tline = tline->next;
3341 tline = expand_id(tline);
3342 if (!tline || (tline->type != TOK_ID &&
3343 (tline->type != TOK_PREPROC_ID ||
3344 tline->text[1] != '$'))) {
3346 "`%s' expects a macro identifier as first parameter",
3348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
3351 ctx = get_ctx(tline->text, &mname, false);
3353 tline = expand_smacro(tline->next);
3357 while (tok_type_(t, TOK_WHITESPACE))
3359 /* t should now point to the string */
3360 if (!tok_type_(t, TOK_STRING)) {
3362 "`%s` requires string as second parameter",
3365 free_tlist(origline);
3366 return DIRECTIVE_FOUND;
3370 * Convert the string to a token stream. Note that smacros
3371 * are stored with the token stream reversed, so we have to
3372 * reverse the output of tokenize().
3374 nasm_unquote_cstr(t->text, i);
3375 macro_start = reverse_tokens(tokenize(t->text));
3378 * We now have a macro name, an implicit parameter count of
3379 * zero, and a numeric token to use as an expansion. Create
3380 * and store an SMacro.
3382 define_smacro(ctx, mname, casesense, 0, macro_start);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3388 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3391 StrList *xsl = NULL;
3392 StrList **xst = &xsl;
3396 tline = tline->next;
3398 tline = expand_id(tline);
3399 if (!tline || (tline->type != TOK_ID &&
3400 (tline->type != TOK_PREPROC_ID ||
3401 tline->text[1] != '$'))) {
3403 "`%%pathsearch' expects a macro identifier as first parameter");
3404 free_tlist(origline);
3405 return DIRECTIVE_FOUND;
3407 ctx = get_ctx(tline->text, &mname, false);
3409 tline = expand_smacro(tline->next);
3413 while (tok_type_(t, TOK_WHITESPACE))
3416 if (!t || (t->type != TOK_STRING &&
3417 t->type != TOK_INTERNAL_STRING)) {
3418 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3420 free_tlist(origline);
3421 return DIRECTIVE_FOUND; /* but we did _something_ */
3424 error(ERR_WARNING|ERR_PASS1,
3425 "trailing garbage after `%%pathsearch' ignored");
3427 if (t->type != TOK_INTERNAL_STRING)
3428 nasm_unquote(p, NULL);
3430 fp = inc_fopen(p, &xsl, &xst, true);
3433 fclose(fp); /* Don't actually care about the file */
3435 macro_start = nasm_zalloc(sizeof(*macro_start));
3436 macro_start->text = nasm_quote(p, strlen(p));
3437 macro_start->type = TOK_STRING;
3442 * We now have a macro name, an implicit parameter count of
3443 * zero, and a string token to use as an expansion. Create
3444 * and store an SMacro.
3446 define_smacro(ctx, mname, casesense, 0, macro_start);
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3453 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3456 tline = tline->next;
3458 tline = expand_id(tline);
3459 if (!tline || (tline->type != TOK_ID &&
3460 (tline->type != TOK_PREPROC_ID ||
3461 tline->text[1] != '$'))) {
3463 "`%%strlen' expects a macro identifier as first parameter");
3464 free_tlist(origline);
3465 return DIRECTIVE_FOUND;
3467 ctx = get_ctx(tline->text, &mname, false);
3469 tline = expand_smacro(tline->next);
3473 while (tok_type_(t, TOK_WHITESPACE))
3475 /* t should now point to the string */
3476 if (!tok_type_(t, TOK_STRING)) {
3478 "`%%strlen` requires string as second parameter");
3480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
3484 macro_start = nasm_zalloc(sizeof(*macro_start));
3485 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
3488 * We now have a macro name, an implicit parameter count of
3489 * zero, and a numeric token to use as an expansion. Create
3490 * and store an SMacro.
3492 define_smacro(ctx, mname, casesense, 0, macro_start);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3498 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3501 tline = tline->next;
3503 tline = expand_id(tline);
3504 if (!tline || (tline->type != TOK_ID &&
3505 (tline->type != TOK_PREPROC_ID ||
3506 tline->text[1] != '$'))) {
3508 "`%%strcat' expects a macro identifier as first parameter");
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3512 ctx = get_ctx(tline->text, &mname, false);
3514 tline = expand_smacro(tline->next);
3518 list_for_each(t, tline) {
3520 case TOK_WHITESPACE:
3523 len += t->a.len = nasm_unquote(t->text, NULL);
3526 if (!strcmp(t->text, ",")) /* permit comma separators */
3528 /* else fall through */
3531 "non-string passed to `%%strcat' (%d)", t->type);
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3538 p = pp = nasm_malloc(len);
3539 list_for_each(t, tline) {
3540 if (t->type == TOK_STRING) {
3541 memcpy(p, t->text, t->a.len);
3547 * We now have a macro name, an implicit parameter count of
3548 * zero, and a numeric token to use as an expansion. Create
3549 * and store an SMacro.
3551 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3552 macro_start->text = nasm_quote(pp, len);
3554 define_smacro(ctx, mname, casesense, 0, macro_start);
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3560 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3562 int64_t start, count;
3567 tline = tline->next;
3569 tline = expand_id(tline);
3570 if (!tline || (tline->type != TOK_ID &&
3571 (tline->type != TOK_PREPROC_ID ||
3572 tline->text[1] != '$'))) {
3574 "`%%substr' expects a macro identifier as first parameter");
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3578 ctx = get_ctx(tline->text, &mname, false);
3580 tline = expand_smacro(tline->next);
3583 if (tline) /* skip expanded id */
3585 while (tok_type_(t, TOK_WHITESPACE))
3588 /* t should now point to the string */
3589 if (!tok_type_(t, TOK_STRING)) {
3591 "`%%substr` requires string as second parameter");
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3599 tokval.t_type = TOKEN_INVALID;
3600 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3604 free_tlist(origline);
3605 return DIRECTIVE_FOUND;
3606 } else if (!is_simple(evalresult)) {
3607 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3609 free_tlist(origline);
3610 return DIRECTIVE_FOUND;
3612 start = evalresult->value - 1;
3614 while (tok_type_(tt, TOK_WHITESPACE))
3617 count = 1; /* Backwards compatibility: one character */
3619 tokval.t_type = TOKEN_INVALID;
3620 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
3626 } else if (!is_simple(evalresult)) {
3627 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3629 free_tlist(origline);
3630 return DIRECTIVE_FOUND;
3632 count = evalresult->value;
3635 len = nasm_unquote(t->text, NULL);
3636 /* make start and count being in range */
3640 count = len + count + 1 - start;
3641 if (start + count > (int64_t)len)
3642 count = len - start;
3643 if (!len || count < 0 || start >=(int64_t)len)
3644 start = -1, count = 0; /* empty string */
3646 macro_start = nasm_zalloc(sizeof(*macro_start));
3647 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
3648 macro_start->type = TOK_STRING;
3651 * We now have a macro name, an implicit parameter count of
3652 * zero, and a numeric token to use as an expansion. Create
3653 * and store an SMacro.
3655 define_smacro(ctx, mname, casesense, 0, macro_start);
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
3663 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3664 casesense = (i == PP_ASSIGN);
3666 tline = tline->next;
3668 tline = expand_id(tline);
3669 if (!tline || (tline->type != TOK_ID &&
3670 (tline->type != TOK_PREPROC_ID ||
3671 tline->text[1] != '$'))) {
3673 "`%%%sassign' expects a macro identifier",
3674 (i == PP_IASSIGN ? "i" : ""));
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3678 ctx = get_ctx(tline->text, &mname, false);
3680 tline = expand_smacro(tline->next);
3685 tokval.t_type = TOKEN_INVALID;
3687 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3690 free_tlist(origline);
3691 return DIRECTIVE_FOUND;
3695 error(ERR_WARNING|ERR_PASS1,
3696 "trailing garbage after expression ignored");
3698 if (!is_simple(evalresult)) {
3700 "non-constant value given to `%%%sassign'",
3701 (i == PP_IASSIGN ? "i" : ""));
3702 free_tlist(origline);
3703 return DIRECTIVE_FOUND;
3706 macro_start = nasm_zalloc(sizeof(*macro_start));
3707 make_tok_num(macro_start, reloc_value(evalresult));
3710 * We now have a macro name, an implicit parameter count of
3711 * zero, and a numeric token to use as an expansion. Create
3712 * and store an SMacro.
3714 define_smacro(ctx, mname, casesense, 0, macro_start);
3715 free_tlist(origline);
3716 return DIRECTIVE_FOUND;
3719 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3721 * Syntax is `%line nnn[+mmm] [filename]'
3723 tline = tline->next;
3725 if (!tok_type_(tline, TOK_NUMBER)) {
3726 error(ERR_NONFATAL, "`%%line' expects line number");
3727 free_tlist(origline);
3728 return DIRECTIVE_FOUND;
3730 k = readnum(tline->text, &err);
3732 tline = tline->next;
3733 if (tok_is_(tline, "+")) {
3734 tline = tline->next;
3735 if (!tok_type_(tline, TOK_NUMBER)) {
3736 error(ERR_NONFATAL, "`%%line' expects line increment");
3737 free_tlist(origline);
3738 return DIRECTIVE_FOUND;
3740 m = readnum(tline->text, &err);
3741 tline = tline->next;
3747 nasm_free(src_set_fname(detoken(tline, false)));
3749 free_tlist(origline);
3750 return DIRECTIVE_FOUND;
3753 if (defining != NULL) {
3754 if (defining->type == EXP_WHILE) {
3755 defining->def_depth ++;
3757 return NO_DIRECTIVE_FOUND;
3760 if ((istk->expansion != NULL) &&
3761 (istk->expansion->emitting == false)) {
3765 l->first = copy_Token(tline->next);
3766 j = if_condition(tline->next, i);
3767 tline->next = NULL; /* it got freed */
3768 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3770 ed = new_ExpDef(EXP_WHILE);
3773 ed->max_depth = DEADMAN_LIMIT;
3774 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3775 if (ed->ignoring == false) {
3778 } else if (l != NULL) {
3779 delete_Token(l->first);
3783 ed->prev = defining;
3785 free_tlist(origline);
3786 return DIRECTIVE_FOUND;
3789 if (defining != NULL) {
3790 if (defining->type == EXP_WHILE) {
3791 if (defining->def_depth > 0) {
3792 defining->def_depth --;
3793 return NO_DIRECTIVE_FOUND;
3796 return NO_DIRECTIVE_FOUND;
3799 if (tline->next != NULL) {
3800 error_precond(ERR_WARNING|ERR_PASS1,
3801 "trailing garbage after `%%endwhile' ignored");
3803 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3804 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3805 return DIRECTIVE_FOUND;
3808 defining = ed->prev;
3809 if (ed->ignoring == false) {
3810 ed->prev = expansions;
3812 ei = new_ExpInv(EXP_WHILE, ed);
3813 ei->current = ed->line->next;
3814 ei->emitting = true;
3815 ei->prev = istk->expansion;
3816 istk->expansion = ei;
3820 free_tlist(origline);
3821 return DIRECTIVE_FOUND;
3824 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3826 * We must search along istk->expansion until we hit a
3827 * while invocation. Then we disable the emitting state(s)
3828 * between exitwhile and endwhile.
3830 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3831 if (ei->type == EXP_WHILE) {
3838 * Set all invocations leading back to the while
3839 * invocation to a non-emitting state.
3841 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3842 eei->emitting = false;
3844 eei->emitting = false;
3845 eei->current = NULL;
3846 eei->def->cur_depth = eei->def->max_depth;
3848 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3850 free_tlist(origline);
3851 return DIRECTIVE_FOUND;
3854 if (defining != NULL) {
3855 if (defining->type == EXP_COMMENT) {
3856 defining->def_depth ++;
3858 return NO_DIRECTIVE_FOUND;
3860 ed = new_ExpDef(EXP_COMMENT);
3861 ed->ignoring = true;
3862 ed->prev = defining;
3864 free_tlist(origline);
3865 return DIRECTIVE_FOUND;
3868 if (defining != NULL) {
3869 if (defining->type == EXP_COMMENT) {
3870 if (defining->def_depth > 0) {
3871 defining->def_depth --;
3872 return NO_DIRECTIVE_FOUND;
3875 return NO_DIRECTIVE_FOUND;
3878 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3879 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3880 return DIRECTIVE_FOUND;
3883 defining = ed->prev;
3885 free_tlist(origline);
3886 return DIRECTIVE_FOUND;
3889 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3890 if (in_final != false) {
3891 error(ERR_FATAL, "`%%final' cannot be used recursively");
3893 tline = tline->next;
3895 if (tline == NULL) {
3896 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3899 l->first = copy_Token(tline);
3903 free_tlist(origline);
3904 return DIRECTIVE_FOUND;
3908 "preprocessor directive `%s' not yet implemented",
3910 return DIRECTIVE_FOUND;
3915 * Ensure that a macro parameter contains a condition code and
3916 * nothing else. Return the condition code index if so, or -1
3919 static int find_cc(Token * t)
3925 return -1; /* Probably a %+ without a space */
3928 if (t->type != TOK_ID)
3932 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3936 j = ARRAY_SIZE(conditions);
3939 m = nasm_stricmp(t->text, conditions[k]);
3954 static bool paste_tokens(Token **head, const struct tokseq_match *m,
3955 int mnum, bool handle_paste_tokens)
3957 Token **tail, *t, *tt;
3959 bool did_paste = false;
3963 /* Now handle token pasting... */
3966 while ((t = *tail) && (tt = t->next)) {
3968 case TOK_WHITESPACE:
3969 if (tt->type == TOK_WHITESPACE) {
3970 /* Zap adjacent whitespace tokens */
3971 t->next = delete_Token(tt);
3973 /* Do not advance paste_head here */
3977 case TOK_PASTE: /* %+ */
3978 if (handle_paste_tokens) {
3979 /* Zap %+ and whitespace tokens to the right */
3980 while (t && (t->type == TOK_WHITESPACE ||
3981 t->type == TOK_PASTE))
3982 t = *tail = delete_Token(t);
3983 if (!paste_head || !t)
3984 break; /* Nothing to paste with */
3988 while (tok_type_(tt, TOK_WHITESPACE))
3989 tt = t->next = delete_Token(tt);
3991 tmp = nasm_strcat(t->text, tt->text);
3993 tt = delete_Token(tt);
3994 t = *tail = tokenize(tmp);
4000 t->next = tt; /* Attach the remaining token chain */
4007 /* else fall through */
4010 * Concatenation of tokens might look nontrivial
4011 * but in real it's pretty simple -- the caller
4012 * prepares the masks of token types to be concatenated
4013 * and we simply find matched sequences and slip
4016 for (i = 0; i < mnum; i++) {
4017 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4021 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
4022 len += strlen(tt->text);
4027 * Now tt points to the first token after
4028 * the potential paste area...
4030 if (tt != t->next) {
4031 /* We have at least two tokens... */
4032 len += strlen(t->text);
4033 p = tmp = nasm_malloc(len+1);
4036 p = strchr(p, '\0');
4037 t = delete_Token(t);
4039 t = *tail = tokenize(tmp);
4045 t->next = tt; /* Attach the remaining token chain */
4053 if (i >= mnum) { /* no match */
4055 if (!tok_type_(t->next, TOK_WHITESPACE))
4065 * expands to a list of tokens from %{x:y}
4067 static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
4069 Token *t = tline, **tt, *tm, *head;
4073 pos = strchr(tline->text, ':');
4076 lst = atoi(pos + 1);
4077 fst = atoi(tline->text + 1);
4080 * only macros params are accounted so
4081 * if someone passes %0 -- we reject such
4084 if (lst == 0 || fst == 0)
4087 /* the values should be sane */
4088 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4089 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
4092 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4093 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
4095 /* counted from zero */
4099 * it will be at least one token
4101 tm = ei->params[(fst + ei->rotate) % ei->nparam];
4102 t = new_Token(NULL, tm->type, tm->text, 0);
4103 head = t, tt = &t->next;
4105 for (i = fst + 1; i <= lst; i++) {
4106 t = new_Token(NULL, TOK_OTHER, ",", 0);
4107 *tt = t, tt = &t->next;
4108 j = (i + ei->rotate) % ei->nparam;
4110 t = new_Token(NULL, tm->type, tm->text, 0);
4111 *tt = t, tt = &t->next;
4114 for (i = fst - 1; i >= lst; i--) {
4115 t = new_Token(NULL, TOK_OTHER, ",", 0);
4116 *tt = t, tt = &t->next;
4117 j = (i + ei->rotate) % ei->nparam;
4119 t = new_Token(NULL, tm->type, tm->text, 0);
4120 *tt = t, tt = &t->next;
4128 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4134 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4135 * %-n) and MMacro-local identifiers (%%foo) as well as
4136 * macro indirection (%[...]) and range (%{..:..}).
4138 static Token *expand_mmac_params(Token * tline)
4140 Token *t, *tt, **tail, *thead;
4141 bool changed = false;
4148 if (tline->type == TOK_PREPROC_ID &&
4149 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4150 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4151 tline->text[1] == '%')) {
4153 int type = 0, cc; /* type = 0 to placate optimisers */
4160 tline = tline->next;
4162 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4163 if (ei->type == EXP_MMACRO) {
4168 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
4170 pos = strchr(t->text, ':');
4172 switch (t->text[1]) {
4174 * We have to make a substitution of one of the
4175 * forms %1, %-1, %+1, %%foo, %0.
4178 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4180 text = nasm_strdup(ei->label_text);
4183 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4184 text = nasm_strdup(tmpbuf);
4189 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
4191 text = nasm_strcat(tmpbuf, t->text + 2);
4194 n = atoi(t->text + 2) - 1;
4195 if (n >= ei->nparam)
4199 n = (n + ei->rotate) % ei->nparam;
4205 "macro parameter %d is not a condition code",
4210 if (inverse_ccs[cc] == -1) {
4212 "condition code `%s' is not invertible",
4216 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4220 n = atoi(t->text + 2) - 1;
4221 if (n >= ei->nparam)
4225 n = (n + ei->rotate) % ei->nparam;
4231 "macro parameter %d is not a condition code",
4236 text = nasm_strdup(conditions[cc]);
4240 n = atoi(t->text + 1) - 1;
4241 if (n >= ei->nparam)
4245 n = (n + ei->rotate) % ei->nparam;
4249 for (i = 0; i < ei->paramlen[n]; i++) {
4250 *tail = new_Token(NULL, tt->type, tt->text, 0);
4251 tail = &(*tail)->next;
4255 text = NULL; /* we've done it here */
4260 * seems we have a parameters range here
4262 Token *head, **last;
4263 head = expand_mmac_params_range(ei, t, &last);
4284 } else if (tline->type == TOK_INDIRECT) {
4286 tline = tline->next;
4287 tt = tokenize(t->text);
4288 tt = expand_mmac_params(tt);
4289 tt = expand_smacro(tt);
4292 tt->a.mac = NULL; /* Necessary? */
4300 tline = tline->next;
4308 const struct tokseq_match t[] = {
4310 PP_CONCAT_MASK(TOK_ID) |
4311 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4312 PP_CONCAT_MASK(TOK_ID) |
4313 PP_CONCAT_MASK(TOK_NUMBER) |
4314 PP_CONCAT_MASK(TOK_FLOAT) |
4315 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4318 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4319 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4322 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4329 * Expand all single-line macro calls made in the given line.
4330 * Return the expanded version of the line. The original is deemed
4331 * to be destroyed in the process. (In reality we'll just move
4332 * Tokens from input to output a lot of the time, rather than
4333 * actually bothering to destroy and replicate.)
4336 static Token *expand_smacro(Token * tline)
4338 Token *t, *tt, *mstart, **tail, *thead;
4339 SMacro *head = NULL, *m;
4342 unsigned int nparam, sparam;
4344 Token *org_tline = tline;
4347 int deadman = DEADMAN_LIMIT;
4351 * Trick: we should avoid changing the start token pointer since it can
4352 * be contained in "next" field of other token. Because of this
4353 * we allocate a copy of first token and work with it; at the end of
4354 * routine we copy it back
4357 tline = new_Token(org_tline->next, org_tline->type,
4358 org_tline->text, 0);
4359 tline->a.mac = org_tline->a.mac;
4360 nasm_free(org_tline->text);
4361 org_tline->text = NULL;
4364 expanded = true; /* Always expand %+ at least once */
4370 while (tline) { /* main token loop */
4372 error(ERR_NONFATAL, "interminable macro recursion");
4376 if ((mname = tline->text)) {
4377 /* if this token is a local macro, look in local context */
4378 if (tline->type == TOK_ID) {
4379 head = (SMacro *)hash_findix(&smacros, mname);
4380 } else if (tline->type == TOK_PREPROC_ID) {
4381 ctx = get_ctx(mname, &mname, false);
4382 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4387 * We've hit an identifier. As in is_mmacro below, we first
4388 * check whether the identifier is a single-line macro at
4389 * all, then think about checking for parameters if
4392 list_for_each(m, head)
4393 if (!mstrcmp(m->name, mname, m->casesense))
4399 if (m->nparam == 0) {
4401 * Simple case: the macro is parameterless. Discard the
4402 * one token that the macro call took, and push the
4403 * expansion back on the to-do stack.
4405 if (!m->expansion) {
4406 if (!strcmp("__FILE__", m->name)) {
4409 src_get(&num, &file);
4410 tline->text = nasm_quote(file, strlen(file));
4411 tline->type = TOK_STRING;
4415 if (!strcmp("__LINE__", m->name)) {
4416 nasm_free(tline->text);
4417 make_tok_num(tline, src_get_linnum());
4420 if (!strcmp("__BITS__", m->name)) {
4421 nasm_free(tline->text);
4422 make_tok_num(tline, globalbits);
4425 tline = delete_Token(tline);
4430 * Complicated case: at least one macro with this name
4431 * exists and takes parameters. We must find the
4432 * parameters in the call, count them, find the SMacro
4433 * that corresponds to that form of the macro call, and
4434 * substitute for the parameters when we expand. What a
4437 /*tline = tline->next;
4438 skip_white_(tline); */
4441 while (tok_type_(t, TOK_SMAC_END)) {
4442 t->a.mac->in_progress = false;
4444 t = tline->next = delete_Token(t);
4447 } while (tok_type_(tline, TOK_WHITESPACE));
4448 if (!tok_is_(tline, "(")) {
4450 * This macro wasn't called with parameters: ignore
4451 * the call. (Behaviour borrowed from gnu cpp.)
4460 sparam = PARAM_DELTA;
4461 params = nasm_malloc(sparam * sizeof(Token *));
4462 params[0] = tline->next;
4463 paramsize = nasm_malloc(sparam * sizeof(int));
4465 while (true) { /* parameter loop */
4467 * For some unusual expansions
4468 * which concatenates function call
4471 while (tok_type_(t, TOK_SMAC_END)) {
4472 t->a.mac->in_progress = false;
4474 t = tline->next = delete_Token(t);
4480 "macro call expects terminating `)'");
4483 if (tline->type == TOK_WHITESPACE
4485 if (paramsize[nparam])
4488 params[nparam] = tline->next;
4489 continue; /* parameter loop */
4491 if (tline->type == TOK_OTHER
4492 && tline->text[1] == 0) {
4493 char ch = tline->text[0];
4494 if (ch == ',' && !paren && brackets <= 0) {
4495 if (++nparam >= sparam) {
4496 sparam += PARAM_DELTA;
4497 params = nasm_realloc(params,
4498 sparam * sizeof(Token *));
4499 paramsize = nasm_realloc(paramsize,
4500 sparam * sizeof(int));
4502 params[nparam] = tline->next;
4503 paramsize[nparam] = 0;
4505 continue; /* parameter loop */
4508 (brackets > 0 || (brackets == 0 &&
4509 !paramsize[nparam])))
4511 if (!(brackets++)) {
4512 params[nparam] = tline->next;
4513 continue; /* parameter loop */
4516 if (ch == '}' && brackets > 0)
4517 if (--brackets == 0) {
4519 continue; /* parameter loop */
4521 if (ch == '(' && !brackets)
4523 if (ch == ')' && brackets <= 0)
4529 error(ERR_NONFATAL, "braces do not "
4530 "enclose all of macro parameter");
4532 paramsize[nparam] += white + 1;
4534 } /* parameter loop */
4536 while (m && (m->nparam != nparam ||
4537 mstrcmp(m->name, mname,
4541 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4542 "macro `%s' exists, "
4543 "but not taking %d parameters",
4544 mstart->text, nparam);
4547 if (m && m->in_progress)
4549 if (!m) { /* in progess or didn't find '(' or wrong nparam */
4551 * Design question: should we handle !tline, which
4552 * indicates missing ')' here, or expand those
4553 * macros anyway, which requires the (t) test a few
4557 nasm_free(paramsize);
4561 * Expand the macro: we are placed on the last token of the
4562 * call, so that we can easily split the call from the
4563 * following tokens. We also start by pushing an SMAC_END
4564 * token for the cycle removal.
4571 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
4573 m->in_progress = true;
4575 list_for_each(t, m->expansion) {
4576 if (t->type >= TOK_SMAC_PARAM) {
4577 Token *pcopy = tline, **ptail = &pcopy;
4581 ttt = params[t->type - TOK_SMAC_PARAM];
4582 i = paramsize[t->type - TOK_SMAC_PARAM];
4584 pt = *ptail = new_Token(tline, ttt->type,
4590 } else if (t->type == TOK_PREPROC_Q) {
4591 tt = new_Token(tline, TOK_ID, mname, 0);
4593 } else if (t->type == TOK_PREPROC_QQ) {
4594 tt = new_Token(tline, TOK_ID, m->name, 0);
4597 tt = new_Token(tline, t->type, t->text, 0);
4603 * Having done that, get rid of the macro call, and clean
4604 * up the parameters.
4607 nasm_free(paramsize);
4610 continue; /* main token loop */
4615 if (tline->type == TOK_SMAC_END) {
4616 tline->a.mac->in_progress = false;
4617 tline = delete_Token(tline);
4620 tline = tline->next;
4628 * Now scan the entire line and look for successive TOK_IDs that resulted
4629 * after expansion (they can't be produced by tokenize()). The successive
4630 * TOK_IDs should be concatenated.
4631 * Also we look for %+ tokens and concatenate the tokens before and after
4632 * them (without white spaces in between).
4635 const struct tokseq_match t[] = {
4637 PP_CONCAT_MASK(TOK_ID) |
4638 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4639 PP_CONCAT_MASK(TOK_ID) |
4640 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4641 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4644 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
4646 * If we concatenated something, *and* we had previously expanded
4647 * an actual macro, scan the lines again for macros...
4658 *org_tline = *thead;
4659 /* since we just gave text to org_line, don't free it */
4661 delete_Token(thead);
4663 /* the expression expanded to empty line;
4664 we can't return NULL for some reasons
4665 we just set the line to a single WHITESPACE token. */
4666 memset(org_tline, 0, sizeof(*org_tline));
4667 org_tline->text = NULL;
4668 org_tline->type = TOK_WHITESPACE;
4677 * Similar to expand_smacro but used exclusively with macro identifiers
4678 * right before they are fetched in. The reason is that there can be
4679 * identifiers consisting of several subparts. We consider that if there
4680 * are more than one element forming the name, user wants a expansion,
4681 * otherwise it will be left as-is. Example:
4685 * the identifier %$abc will be left as-is so that the handler for %define
4686 * will suck it and define the corresponding value. Other case:
4688 * %define _%$abc cde
4690 * In this case user wants name to be expanded *before* %define starts
4691 * working, so we'll expand %$abc into something (if it has a value;
4692 * otherwise it will be left as-is) then concatenate all successive
4695 static Token *expand_id(Token * tline)
4697 Token *cur, *oldnext = NULL;
4699 if (!tline || !tline->next)
4704 (cur->next->type == TOK_ID ||
4705 cur->next->type == TOK_PREPROC_ID
4706 || cur->next->type == TOK_NUMBER))
4709 /* If identifier consists of just one token, don't expand */
4714 oldnext = cur->next; /* Detach the tail past identifier */
4715 cur->next = NULL; /* so that expand_smacro stops here */
4718 tline = expand_smacro(tline);
4721 /* expand_smacro possibly changhed tline; re-scan for EOL */
4723 while (cur && cur->next)
4726 cur->next = oldnext;
4733 * Determine whether the given line constitutes a multi-line macro
4734 * call, and return the ExpDef structure called if so. Doesn't have
4735 * to check for an initial label - that's taken care of in
4736 * expand_mmacro - but must check numbers of parameters. Guaranteed
4737 * to be called with tline->type == TOK_ID, so the putative macro
4738 * name is easy to find.
4740 static ExpDef *is_mmacro(Token * tline, Token *** params_array)
4746 head = (ExpDef *) hash_findix(&expdefs, tline->text);
4749 * Efficiency: first we see if any macro exists with the given
4750 * name. If not, we can return NULL immediately. _Then_ we
4751 * count the parameters, and then we look further along the
4752 * list if necessary to find the proper ExpDef.
4754 list_for_each(ed, head)
4755 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4761 * OK, we have a potential macro. Count and demarcate the
4764 count_mmac_params(tline->next, &nparam, ¶ms);
4767 * So we know how many parameters we've got. Find the ExpDef
4768 * structure that handles this number.
4771 if (ed->nparam_min <= nparam
4772 && (ed->plus || nparam <= ed->nparam_max)) {
4774 * It's right, and we can use it. Add its default
4775 * parameters to the end of our list if necessary.
4777 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
4779 nasm_realloc(params,
4780 ((ed->nparam_min + ed->ndefs +
4781 1) * sizeof(*params)));
4782 while (nparam < ed->nparam_min + ed->ndefs) {
4783 params[nparam] = ed->defaults[nparam - ed->nparam_min];
4788 * If we've gone over the maximum parameter count (and
4789 * we're in Plus mode), ignore parameters beyond
4792 if (ed->plus && nparam > ed->nparam_max)
4793 nparam = ed->nparam_max;
4795 * Then terminate the parameter list, and leave.
4797 if (!params) { /* need this special case */
4798 params = nasm_malloc(sizeof(*params));
4801 params[nparam] = NULL;
4802 *params_array = params;
4806 * This one wasn't right: look for the next one with the
4809 list_for_each(ed, ed->next)
4810 if (!mstrcmp(ed->name, tline->text, ed->casesense))
4815 * After all that, we didn't find one with the right number of
4816 * parameters. Issue a warning, and fail to expand the macro.
4818 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
4819 "macro `%s' exists, but not taking %d parameters",
4820 tline->text, nparam);
4826 * Expand the multi-line macro call made by the given line, if
4827 * there is one to be expanded. If there is, push the expansion on
4828 * istk->expansion and return true. Otherwise return false.
4830 static bool expand_mmacro(Token * tline)
4832 Token *label = NULL;
4833 int dont_prepend = 0;
4834 Token **params, *t, *mtok;
4838 int i, nparam, *paramlen;
4843 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4844 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
4847 ed = is_mmacro(t, ¶ms);
4853 * We have an id which isn't a macro call. We'll assume
4854 * it might be a label; we'll also check to see if a
4855 * colon follows it. Then, if there's another id after
4856 * that lot, we'll check it again for macro-hood.
4860 if (tok_type_(t, TOK_WHITESPACE))
4861 last = t, t = t->next;
4862 if (tok_is_(t, ":")) {
4864 last = t, t = t->next;
4865 if (tok_type_(t, TOK_WHITESPACE))
4866 last = t, t = t->next;
4868 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms)))
4876 * Fix up the parameters: this involves stripping leading and
4877 * trailing whitespace, then stripping braces if they are
4880 for (nparam = 0; params[nparam]; nparam++) ;
4881 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
4883 for (i = 0; params[i]; i++) {
4885 int comma = (!ed->plus || i < nparam - 1);
4889 if (tok_is_(t, "{"))
4890 t = t->next, brace = true, comma = false;
4894 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4895 break; /* ... because we have hit a comma */
4896 if (comma && t->type == TOK_WHITESPACE
4897 && tok_is_(t->next, ","))
4898 break; /* ... or a space then a comma */
4899 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4900 break; /* ... or a brace */
4906 if (ed->cur_depth >= ed->max_depth) {
4907 if (ed->max_depth > 1) {
4909 "reached maximum macro recursion depth of %i for %s",
4910 ed->max_depth,ed->name);
4918 * OK, we have found a ExpDef structure representing a
4919 * previously defined mmacro. Create an expansion invocation
4920 * and point it back to the expansion definition. Substitution of
4921 * parameter tokens and macro-local tokens doesn't get done
4922 * until the single-line macro substitution process; this is
4923 * because delaying them allows us to change the semantics
4924 * later through %rotate.
4926 ei = new_ExpInv(EXP_MMACRO, ed);
4927 ei->name = nasm_strdup(mname);
4928 //ei->label = label;
4929 //ei->label_text = detoken(label, false);
4930 ei->current = ed->line;
4931 ei->emitting = true;
4932 //ei->iline = tline;
4933 ei->params = params;
4934 ei->nparam = nparam;
4936 ei->paramlen = paramlen;
4939 ei->prev = istk->expansion;
4940 istk->expansion = ei;
4943 * Special case: detect %00 on first invocation; if found,
4944 * avoid emitting any labels that precede the mmacro call.
4945 * ed->prepend is set to -1 when %00 is detected, else 1.
4947 if (ed->prepend == 0) {
4948 for (l = ed->line; l != NULL; l = l->next) {
4949 for (t = l->first; t != NULL; t = t->next) {
4950 if ((t->type == TOK_PREPROC_ID) &&
4951 (strlen(t->text) == 3) &&
4952 (t->text[1] == '0') && (t->text[2] == '0')) {
4957 if (dont_prepend < 0) {
4961 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4965 * If we had a label, push it on as the first line of
4966 * the macro expansion.
4968 if (label != NULL) {
4969 if (ed->prepend < 0) {
4970 ei->label_text = detoken(label, false);
4972 if (dont_prepend == 0) {
4974 while (t->next != NULL) {
4977 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4980 l->first = copy_Token(label);
4981 l->next = ei->current;
4986 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
4992 /* The function that actually does the error reporting */
4993 static void verror(int severity, const char *fmt, va_list arg)
4997 vsnprintf(buff, sizeof(buff), fmt, arg);
4999 if (istk && istk->mmac_depth > 0) {
5000 ExpInv *ei = istk->expansion;
5001 int lineno = ei->lineno;
5003 if (ei->type == EXP_MMACRO)
5005 lineno += ei->relno;
5008 nasm_error(severity, "(%s:%d) %s", ei->def->name,
5011 nasm_error(severity, "%s", buff);
5015 * Since preprocessor always operate only on the line that didn't
5016 * arrived yet, we should always use ERR_OFFBY1.
5018 static void error(int severity, const char *fmt, ...)
5022 verror(severity, fmt, arg);
5027 * Because %else etc are evaluated in the state context
5028 * of the previous branch, errors might get lost with error():
5029 * %if 0 ... %else trailing garbage ... %endif
5030 * So %else etc should report errors with this function.
5032 static void error_precond(int severity, const char *fmt, ...)
5036 /* Only ignore the error if it's really in a dead branch */
5037 if ((istk != NULL) &&
5038 (istk->expansion != NULL) &&
5039 (istk->expansion->type == EXP_IF) &&
5040 (istk->expansion->def->state == COND_NEVER))
5044 verror(severity, fmt, arg);
5049 pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
5054 istk = nasm_zalloc(sizeof(Include));
5055 istk->fp = fopen(file, "r");
5056 src_set_fname(nasm_strdup(file));
5060 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
5065 nested_mac_count = 0;
5066 nested_rep_count = 0;
5069 if (tasm_compatible_mode) {
5070 stdmacpos = nasm_stdmac;
5072 stdmacpos = nasm_stdmac_after_tasm;
5074 any_extrastdmac = extrastdmac && *extrastdmac;
5079 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5080 * The caller, however, will also pass in 3 for preprocess-only so
5081 * we can set __PASS__ accordingly.
5083 pass = apass > 2 ? 2 : apass;
5085 dephead = deptail = deplist;
5087 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5089 strcpy(sl->str, file);
5091 deptail = &sl->next;
5095 * Define the __PASS__ macro. This is defined here unlike
5096 * all the other builtins, because it is special -- it varies between
5099 t = nasm_zalloc(sizeof(*t));
5100 make_tok_num(t, apass);
5101 define_smacro(NULL, "__PASS__", true, 0, t);
5104 static char *pp_getline(void)
5115 * Fetch a tokenized line, either from the expansion
5116 * buffer or from the input file.
5120 while (1) { /* until we get a line we can use */
5122 * Fetch a tokenized line from the expansion buffer
5124 if (istk->expansion != NULL) {
5125 ei = istk->expansion;
5126 if (ei->current != NULL) {
5127 if (ei->emitting == false) {
5132 ei->current = l->next;
5134 tline = copy_Token(l->first);
5135 if (((ei->type == EXP_REP) ||
5136 (ei->type == EXP_MMACRO) ||
5137 (ei->type == EXP_WHILE))
5138 && (ei->def->nolist == false)) {
5139 char *p = detoken(tline, false);
5140 list->line(LIST_MACRO, p);
5143 if (ei->linnum > -1) {
5144 src_set_linnum(src_get_linnum() + 1);
5147 } else if ((ei->type == EXP_REP) &&
5148 (ei->def->cur_depth < ei->def->max_depth)) {
5149 ei->def->cur_depth ++;
5150 ei->current = ei->def->line;
5153 } else if ((ei->type == EXP_WHILE) &&
5154 (ei->def->cur_depth < ei->def->max_depth)) {
5155 ei->current = ei->def->line;
5157 tline = copy_Token(ei->current->first);
5158 j = if_condition(tline, PP_WHILE);
5160 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5161 if (j == COND_IF_TRUE) {
5162 ei->current = ei->current->next;
5163 ei->def->cur_depth ++;
5165 ei->emitting = false;
5167 ei->def->cur_depth = ei->def->max_depth;
5171 istk->expansion = ei->prev;
5174 if ((ei->emitting == true) &&
5175 (ed->max_depth == DEADMAN_LIMIT) &&
5176 (ed->cur_depth == DEADMAN_LIMIT)
5178 error(ERR_FATAL, "runaway expansion detected, aborting");
5180 if (ed->cur_depth > 0) {
5182 } else if (ed->type != EXP_MMACRO) {
5183 expansions = ed->prev;
5186 if ((ei->type == EXP_REP) ||
5187 (ei->type == EXP_MMACRO) ||
5188 (ei->type == EXP_WHILE)) {
5189 list->downlevel(LIST_MACRO);
5190 if (ei->type == EXP_MMACRO) {
5195 if (ei->linnum > -1) {
5196 src_set_linnum(ei->linnum);
5204 * Read in line from input and tokenize
5207 if (line) { /* from the current input file */
5208 line = prepreproc(line);
5209 tline = tokenize(line);
5215 * The current file has ended; work down the istk
5220 if (i->expansion != NULL) {
5222 "end of file while still in an expansion");
5224 /* only set line and file name if there's a next node */
5226 src_set_linnum(i->lineno);
5227 nasm_free(src_set_fname(i->fname));
5229 if ((i->next == NULL) && (finals != NULL)) {
5231 ei = new_ExpInv(EXP_FINAL, NULL);
5232 ei->emitting = true;
5233 ei->current = finals;
5234 istk->expansion = ei;
5239 list->downlevel(LIST_INCLUDE);
5242 if (finals != NULL) {
5252 if (defining == NULL) {
5253 tline = expand_mmac_params(tline);
5257 * Check the line to see if it's a preprocessor directive.
5259 if (do_directive(tline) == DIRECTIVE_FOUND) {
5261 } else if (defining != NULL) {
5263 * We're defining an expansion. We emit nothing at all,
5264 * and just shove the tokenized line on to the definition.
5266 if (defining->ignoring == false) {
5267 Line *l = new_Line();
5269 if (defining->line == NULL) {
5273 defining->last->next = l;
5279 defining->linecount++;
5281 } else if ((istk->expansion != NULL) &&
5282 (istk->expansion->emitting != true)) {
5284 * We're in a non-emitting branch of an expansion.
5285 * Emit nothing at all, not even a blank line: when we
5286 * emerge from the expansion we'll give a line-number
5287 * directive so we keep our place correctly.
5292 tline = expand_smacro(tline);
5293 if (expand_mmacro(tline) != true) {
5295 * De-tokenize the line again, and emit it.
5297 line = detoken(tline, true);
5308 static void pp_cleanup(int pass)
5310 if (defining != NULL) {
5311 error(ERR_NONFATAL, "end of file while still defining an expansion");
5312 while (defining != NULL) {
5313 ExpDef *ed = defining;
5314 defining = ed->prev;
5319 while (cstk != NULL)
5322 while (istk != NULL) {
5326 nasm_free(i->fname);
5327 while (i->expansion != NULL) {
5328 ExpInv *ei = i->expansion;
5329 i->expansion = ei->prev;
5336 nasm_free(src_set_fname(NULL));
5341 while ((i = ipath)) {
5350 void pp_include_path(char *path)
5352 IncPath *i = nasm_zalloc(sizeof(IncPath));
5355 i->path = nasm_strdup(path);
5367 void pp_pre_include(char *fname)
5369 Token *inc, *space, *name;
5372 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5373 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5374 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
5382 void pp_pre_define(char *definition)
5388 equals = strchr(definition, '=');
5389 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5390 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
5393 space->next = tokenize(definition);
5403 void pp_pre_undefine(char *definition)
5408 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5409 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
5410 space->next = tokenize(definition);
5419 * This function is used to assist with "runtime" preprocessor
5420 * directives, e.g. pp_runtime("%define __BITS__ 64");
5422 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5423 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5426 void pp_runtime(char *definition)
5430 def = tokenize(definition);
5431 if (do_directive(def) == NO_DIRECTIVE_FOUND)
5436 void pp_extra_stdmac(macros_t *macros)
5438 extrastdmac = macros;
5441 static void make_tok_num(Token * tok, int64_t val)
5444 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5445 tok->text = nasm_strdup(numbuf);
5446 tok->type = TOK_NUMBER;