1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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
85 typedef struct SMacro SMacro
;
86 typedef struct MMacro MMacro
;
87 typedef struct MMacroInvocation MMacroInvocation
;
88 typedef struct Context Context
;
89 typedef struct Token Token
;
90 typedef struct Blocks Blocks
;
91 typedef struct Line Line
;
92 typedef struct Include Include
;
93 typedef struct Cond Cond
;
94 typedef struct IncPath IncPath
;
97 * Note on the storage of both SMacro and MMacros: the hash table
98 * indexes them case-insensitively, and we then have to go through a
99 * linked list of potential case aliases (and, for MMacros, parameter
100 * ranges); this is to preserve the matching semantics of the earlier
101 * code. If the number of case aliases for a specific macro is a
102 * performance issue, you may want to reconsider your coding style.
106 * Store the definition of a single-line macro.
118 * Store the definition of a multi-line macro. This is also used to
119 * store the interiors of `%rep...%endrep' blocks, which are
120 * effectively self-re-invoking multi-line macros which simply
121 * don't have a name or bother to appear in the hash tables. %rep
122 * blocks are signified by having a NULL `name' field.
124 * In a MMacro describing a `%rep' block, the `in_progress' field
125 * isn't merely boolean, but gives the number of repeats left to
128 * The `next' field is used for storing MMacros in hash tables; the
129 * `next_active' field is for stacking them on istk entries.
131 * When a MMacro is being expanded, `params', `iline', `nparam',
132 * `paramlen', `rotate' and `unique' are local to the invocation.
136 MMacroInvocation
*prev
; /* previous invocation */
138 int nparam_min
, nparam_max
;
140 bool plus
; /* is the last parameter greedy? */
141 bool nolist
; /* is this macro listing-inhibited? */
142 int64_t in_progress
; /* is this macro currently being expanded? */
143 int32_t max_depth
; /* maximum number of recursive expansions allowed */
144 Token
*dlist
; /* All defaults as one list */
145 Token
**defaults
; /* Parameter default pointers */
146 int ndefs
; /* number of default parameters */
150 MMacro
*rep_nest
; /* used for nesting %rep */
151 Token
**params
; /* actual parameters */
152 Token
*iline
; /* invocation line */
153 unsigned int nparam
, rotate
;
156 int lineno
; /* Current line number on expansion */
157 uint64_t condcnt
; /* number of if blocks... */
159 const char *fname
; /* File where defined */
160 int32_t xline
; /* First line in macro */
164 /* Store the definition of a multi-line macro, as defined in a
165 * previous recursive macro expansion.
167 struct MMacroInvocation
{
168 MMacroInvocation
*prev
; /* previous invocation */
169 Token
**params
; /* actual parameters */
170 Token
*iline
; /* invocation line */
171 unsigned int nparam
, rotate
;
179 * The context stack is composed of a linked list of these.
184 struct hash_table localmac
;
189 * This is the internal form which we break input lines up into.
190 * Typically stored in linked lists.
192 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
193 * necessarily used as-is, but is intended to denote the number of
194 * the substituted parameter. So in the definition
196 * %define a(x,y) ( (x) & ~(y) )
198 * the token representing `x' will have its type changed to
199 * TOK_SMAC_PARAM, but the one representing `y' will be
202 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
203 * which doesn't need quotes around it. Used in the pre-include
204 * mechanism as an alternative to trying to find a sensible type of
205 * quote to use on the filename we were passed.
208 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
209 TOK_PREPROC_ID
, TOK_STRING
,
210 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
212 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
214 TOK_INDIRECT
, /* %[...] */
215 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
216 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
219 #define PP_CONCAT_MASK(x) (1 << (x))
220 #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
222 struct tokseq_match
{
231 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
232 size_t len
; /* scratch length field */
233 } a
; /* Auxiliary data */
234 enum pp_token_type type
;
238 * Multi-line macro definitions are stored as a linked list of
239 * these, which is essentially a container to allow several linked
242 * Note that in this module, linked lists are treated as stacks
243 * wherever possible. For this reason, Lines are _pushed_ on to the
244 * `expansion' field in MMacro structures, so that the linked list,
245 * if walked, would give the macro lines in reverse order; this
246 * means that we can walk the list when expanding a macro, and thus
247 * push the lines on to the `expansion' field in _istk_ in reverse
248 * order (so that when popped back off they are in the right
249 * order). It may seem cockeyed, and it relies on my design having
250 * an even number of steps in, but it works...
252 * Some of these structures, rather than being actual lines, are
253 * markers delimiting the end of the expansion of a given macro.
254 * This is for use in the cycle-tracking and %rep-handling code.
255 * Such structures have `finishes' non-NULL, and `first' NULL. All
256 * others have `finishes' NULL, but `first' may still be NULL if
266 * To handle an arbitrary level of file inclusion, we maintain a
267 * stack (ie linked list) of these things.
276 MMacro
*mstk
; /* stack of active macros/reps */
280 * Include search path. This is simply a list of strings which get
281 * prepended, in turn, to the name of an include file, in an
282 * attempt to find the file if it's not in the current directory.
290 * Conditional assembly: we maintain a separate stack of these for
291 * each level of file inclusion. (The only reason we keep the
292 * stacks separate is to ensure that a stray `%endif' in a file
293 * included from within the true branch of a `%if' won't terminate
294 * it and cause confusion: instead, rightly, it'll cause an error.)
302 * These states are for use just after %if or %elif: IF_TRUE
303 * means the condition has evaluated to truth so we are
304 * currently emitting, whereas IF_FALSE means we are not
305 * currently emitting but will start doing so if a %else comes
306 * up. In these states, all directives are admissible: %elif,
307 * %else and %endif. (And of course %if.)
309 COND_IF_TRUE
, COND_IF_FALSE
,
311 * These states come up after a %else: ELSE_TRUE means we're
312 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
313 * any %elif or %else will cause an error.
315 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
317 * These states mean that we're not emitting now, and also that
318 * nothing until %endif will be emitted at all. COND_DONE is
319 * used when we've had our moment of emission
320 * and have now started seeing %elifs. COND_NEVER is used when
321 * the condition construct in question is contained within a
322 * non-emitting branch of a larger condition construct,
323 * or if there is an error.
325 COND_DONE
, COND_NEVER
327 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
330 * These defines are used as the possible return values for do_directive
332 #define NO_DIRECTIVE_FOUND 0
333 #define DIRECTIVE_FOUND 1
336 * This define sets the upper limit for smacro and recursive mmacro
339 #define DEADMAN_LIMIT (1 << 20)
342 #define REP_LIMIT ((INT64_C(1) << 62))
345 * Condition codes. Note that we use c_ prefix not C_ because C_ is
346 * used in nasm.h for the "real" condition codes. At _this_ level,
347 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
348 * ones, so we need a different enum...
350 static const char * const conditions
[] = {
351 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
352 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
353 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
356 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
357 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
358 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
361 static const enum pp_conds inverse_ccs
[] = {
362 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
363 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
,
364 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
370 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
371 static int is_condition(enum preproc_token arg
)
373 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
376 /* For TASM compatibility we need to be able to recognise TASM compatible
377 * conditional compilation directives. Using the NASM pre-processor does
378 * not work, so we look for them specifically from the following list and
379 * then jam in the equivalent NASM directive into the input stream.
383 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
384 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
387 static const char * const tasm_directives
[] = {
388 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
389 "ifndef", "include", "local"
392 static int StackSize
= 4;
393 static char *StackPointer
= "ebp";
394 static int ArgOffset
= 8;
395 static int LocalOffset
= 0;
397 static Context
*cstk
;
398 static Include
*istk
;
399 static IncPath
*ipath
= NULL
;
401 static int pass
; /* HACK: pass 0 = generate dependencies only */
402 static StrList
**dephead
, **deptail
; /* Dependency list */
404 static uint64_t unique
; /* unique identifier numbers */
406 static Line
*predef
= NULL
;
407 static bool do_predef
;
410 * The current set of multi-line macros we have defined.
412 static struct hash_table mmacros
;
415 * The current set of single-line macros we have defined.
417 static struct hash_table smacros
;
420 * The multi-line macro we are currently defining, or the %rep
421 * block we are currently reading, if any.
423 static MMacro
*defining
;
425 static uint64_t nested_mac_count
;
426 static uint64_t nested_rep_count
;
429 * The number of macro parameters to allocate space for at a time.
431 #define PARAM_DELTA 16
434 * The standard macro set: defined in macros.c in the array nasm_stdmac.
435 * This gives our position in the macro set, when we're processing it.
437 static macros_t
*stdmacpos
;
440 * The extra standard macros that come from the object format, if
443 static macros_t
*extrastdmac
= NULL
;
444 static bool any_extrastdmac
;
447 * Tokens are allocated in blocks to improve speed
449 #define TOKEN_BLOCKSIZE 4096
450 static Token
*freeTokens
= NULL
;
456 static Blocks blocks
= { NULL
, NULL
};
459 * Forward declarations.
461 static Token
*expand_mmac_params(Token
* tline
);
462 static Token
*expand_smacro(Token
* tline
);
463 static Token
*expand_id(Token
* tline
);
464 static Context
*get_ctx(const char *name
, const char **namep
);
465 static void make_tok_num(Token
* tok
, int64_t val
);
466 static void pp_verror(int severity
, const char *fmt
, va_list ap
);
467 static vefunc real_verror
;
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
*delete_Token(Token
* t
);
475 * Macros for safe checking of token pointers, avoid *(NULL)
477 #define tok_type_(x,t) ((x) && (x)->type == (t))
478 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
479 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
480 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
483 * nasm_unquote with error if the string contains NUL characters.
484 * If the string contains NUL characters, issue an error and return
485 * the C len, i.e. truncate at the NUL.
487 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
489 size_t len
= nasm_unquote(qstr
, NULL
);
490 size_t clen
= strlen(qstr
);
493 nasm_error(ERR_NONFATAL
, "NUL character in `%s' directive",
494 pp_directives
[directive
]);
500 * In-place reverse a list of tokens.
502 static Token
*reverse_tokens(Token
*t
)
518 * Handle TASM specific directives, which do not contain a % in
519 * front of them. We do it here because I could not find any other
520 * place to do it for the moment, and it is a hack (ideally it would
521 * be nice to be able to use the NASM pre-processor to do it).
523 static char *check_tasm_directive(char *line
)
525 int32_t i
, j
, k
, m
, len
;
526 char *p
, *q
, *oldline
, oldchar
;
528 p
= nasm_skip_spaces(line
);
530 /* Binary search for the directive name */
532 j
= ARRAY_SIZE(tasm_directives
);
533 q
= nasm_skip_word(p
);
540 m
= nasm_stricmp(p
, tasm_directives
[k
]);
542 /* We have found a directive, so jam a % in front of it
543 * so that NASM will then recognise it as one if it's own.
548 line
= nasm_malloc(len
+ 2);
550 if (k
== TM_IFDIFI
) {
552 * NASM does not recognise IFDIFI, so we convert
553 * it to %if 0. This is not used in NASM
554 * compatible code, but does need to parse for the
555 * TASM macro package.
557 strcpy(line
+ 1, "if 0");
559 memcpy(line
+ 1, p
, len
+ 1);
574 * The pre-preprocessing stage... This function translates line
575 * number indications as they emerge from GNU cpp (`# lineno "file"
576 * flags') into NASM preprocessor line number indications (`%line
579 static char *prepreproc(char *line
)
582 char *fname
, *oldline
;
584 if (line
[0] == '#' && line
[1] == ' ') {
587 lineno
= atoi(fname
);
588 fname
+= strspn(fname
, "0123456789 ");
591 fnlen
= strcspn(fname
, "\"");
592 line
= nasm_malloc(20 + fnlen
);
593 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
596 if (tasm_compatible_mode
)
597 return check_tasm_directive(line
);
602 * Free a linked list of tokens.
604 static void free_tlist(Token
* list
)
607 list
= delete_Token(list
);
611 * Free a linked list of lines.
613 static void free_llist(Line
* list
)
616 list_for_each_safe(l
, tmp
, list
) {
617 free_tlist(l
->first
);
625 static void free_mmacro(MMacro
* m
)
628 free_tlist(m
->dlist
);
629 nasm_free(m
->defaults
);
630 free_llist(m
->expansion
);
635 * Free all currently defined macros, and free the hash tables
637 static void free_smacro_table(struct hash_table
*smt
)
641 struct hash_tbl_node
*it
= NULL
;
643 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
644 nasm_free((void *)key
);
645 list_for_each_safe(s
, tmp
, s
) {
647 free_tlist(s
->expansion
);
654 static void free_mmacro_table(struct hash_table
*mmt
)
658 struct hash_tbl_node
*it
= NULL
;
661 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
662 nasm_free((void *)key
);
663 list_for_each_safe(m
,tmp
, m
)
669 static void free_macros(void)
671 free_smacro_table(&smacros
);
672 free_mmacro_table(&mmacros
);
676 * Initialize the hash tables
678 static void init_macros(void)
680 hash_init(&smacros
, HASH_LARGE
);
681 hash_init(&mmacros
, HASH_LARGE
);
685 * Pop the context stack.
687 static void ctx_pop(void)
692 free_smacro_table(&c
->localmac
);
698 * Search for a key in the hash index; adding it if necessary
699 * (in which case we initialize the data pointer to NULL.)
702 hash_findi_add(struct hash_table
*hash
, const char *str
)
704 struct hash_insert hi
;
708 r
= hash_findi(hash
, str
, &hi
);
712 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
713 return hash_add(&hi
, strx
, NULL
);
717 * Like hash_findi, but returns the data element rather than a pointer
718 * to it. Used only when not adding a new element, hence no third
722 hash_findix(struct hash_table
*hash
, const char *str
)
726 p
= hash_findi(hash
, str
, NULL
);
727 return p
? *p
: NULL
;
731 * read line from standart macros set,
732 * if there no more left -- return NULL
734 static char *line_from_stdmac(void)
737 const unsigned char *p
= stdmacpos
;
746 len
+= pp_directives_len
[c
- 0x80] + 1;
751 line
= nasm_malloc(len
+ 1);
753 while ((c
= *stdmacpos
++)) {
755 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
756 q
+= pp_directives_len
[c
- 0x80];
766 /* This was the last of the standard macro chain... */
768 if (any_extrastdmac
) {
769 stdmacpos
= extrastdmac
;
770 any_extrastdmac
= false;
771 } else if (do_predef
) {
773 Token
*head
, **tail
, *t
;
776 * Nasty hack: here we push the contents of
777 * `predef' on to the top-level expansion stack,
778 * since this is the most convenient way to
779 * implement the pre-include and pre-define
782 list_for_each(pd
, predef
) {
785 list_for_each(t
, pd
->first
) {
786 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
787 tail
= &(*tail
)->next
;
790 l
= nasm_malloc(sizeof(Line
));
791 l
->next
= istk
->expansion
;
804 static char *read_line(void)
806 unsigned int size
, c
, next
;
807 const unsigned int delta
= 512;
808 const unsigned int pad
= 8;
809 unsigned int nr_cont
= 0;
813 /* Standart macros set (predefined) goes first */
814 p
= line_from_stdmac();
819 p
= buffer
= nasm_malloc(size
);
823 if ((int)(c
) == EOF
) {
830 next
= fgetc(istk
->fp
);
832 ungetc(next
, istk
->fp
);
847 next
= fgetc(istk
->fp
);
848 ungetc(next
, istk
->fp
);
849 if (next
== '\r' || next
== '\n') {
857 if (c
== '\r' || c
== '\n') {
862 if (p
>= (buffer
+ size
- pad
)) {
863 buffer
= nasm_realloc(buffer
, size
+ delta
);
864 p
= buffer
+ size
- pad
;
868 *p
++ = (unsigned char)c
;
876 src_set_linnum(src_get_linnum() + istk
->lineinc
+
877 (nr_cont
* istk
->lineinc
));
880 * Handle spurious ^Z, which may be inserted into source files
881 * by some file transfer utilities.
883 buffer
[strcspn(buffer
, "\032")] = '\0';
885 lfmt
->line(LIST_READ
, buffer
);
891 * Tokenize a line of text. This is a very simple process since we
892 * don't need to parse the value out of e.g. numeric tokens: we
893 * simply split one string into many.
895 static Token
*tokenize(char *line
)
898 enum pp_token_type type
;
900 Token
*t
, **tail
= &list
;
906 if (*p
== '+' && !nasm_isdigit(p
[1])) {
909 } else if (nasm_isdigit(*p
) ||
910 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
914 while (nasm_isdigit(*p
));
915 type
= TOK_PREPROC_ID
;
916 } else if (*p
== '{') {
925 nasm_error(ERR_WARNING
| ERR_PASS1
,
926 "unterminated %%{ construct");
930 type
= TOK_PREPROC_ID
;
931 } else if (*p
== '[') {
933 line
+= 2; /* Skip the leading %[ */
935 while (lvl
&& (c
= *p
++)) {
947 p
= nasm_skip_string(p
- 1) + 1;
957 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
958 "unterminated %%[ construct");
960 } else if (*p
== '?') {
961 type
= TOK_PREPROC_Q
; /* %? */
964 type
= TOK_PREPROC_QQ
; /* %?? */
967 } else if (*p
== '!') {
968 type
= TOK_PREPROC_ID
;
974 while (isidchar(*p
));
975 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
976 p
= nasm_skip_string(p
);
980 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
981 "unterminated %%! string");
983 /* %! without string or identifier */
984 type
= TOK_OTHER
; /* Legacy behavior... */
986 } else if (isidchar(*p
) ||
987 ((*p
== '!' || *p
== '%' || *p
== '$') &&
992 while (isidchar(*p
));
993 type
= TOK_PREPROC_ID
;
999 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
1002 while (*p
&& isidchar(*p
))
1004 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1009 p
= nasm_skip_string(p
);
1014 nasm_error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1015 /* Handling unterminated strings by UNV */
1018 } else if (p
[0] == '$' && p
[1] == '$') {
1019 type
= TOK_OTHER
; /* TOKEN_BASE */
1021 } else if (isnumstart(*p
)) {
1022 bool is_hex
= false;
1023 bool is_float
= false;
1039 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1041 if (*p
== '+' || *p
== '-') {
1043 * e can only be followed by +/- if it is either a
1044 * prefixed hex number or a floating-point number
1049 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1051 } else if (c
== 'P' || c
== 'p') {
1053 if (*p
== '+' || *p
== '-')
1055 } else if (isnumchar(c
) || c
== '_')
1056 ; /* just advance */
1057 else if (c
== '.') {
1059 * we need to deal with consequences of the legacy
1060 * parser, like "1.nolist" being two tokens
1061 * (TOK_NUMBER, TOK_ID) here; at least give it
1062 * a shot for now. In the future, we probably need
1063 * a flex-based scanner with proper pattern matching
1064 * to do it as well as it can be done. Nothing in
1065 * the world is going to help the person who wants
1066 * 0x123.p16 interpreted as two tokens, though.
1072 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1073 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1074 (*r
== 'p' || *r
== 'P')) {
1078 break; /* Terminate the token */
1082 p
--; /* Point to first character beyond number */
1084 if (p
== line
+1 && *line
== '$') {
1085 type
= TOK_OTHER
; /* TOKEN_HERE */
1087 if (has_e
&& !is_hex
) {
1088 /* 1e13 is floating-point, but 1e13h is not */
1092 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1094 } else if (nasm_isspace(*p
)) {
1095 type
= TOK_WHITESPACE
;
1096 p
= nasm_skip_spaces(p
);
1098 * Whitespace just before end-of-line is discarded by
1099 * pretending it's a comment; whitespace just before a
1100 * comment gets lumped into the comment.
1102 if (!*p
|| *p
== ';') {
1107 } else if (*p
== ';') {
1113 * Anything else is an operator of some kind. We check
1114 * for all the double-character operators (>>, <<, //,
1115 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1116 * else is a single-character operator.
1119 if ((p
[0] == '>' && p
[1] == '>') ||
1120 (p
[0] == '<' && p
[1] == '<') ||
1121 (p
[0] == '/' && p
[1] == '/') ||
1122 (p
[0] == '<' && p
[1] == '=') ||
1123 (p
[0] == '>' && p
[1] == '=') ||
1124 (p
[0] == '=' && p
[1] == '=') ||
1125 (p
[0] == '!' && p
[1] == '=') ||
1126 (p
[0] == '<' && p
[1] == '>') ||
1127 (p
[0] == '&' && p
[1] == '&') ||
1128 (p
[0] == '|' && p
[1] == '|') ||
1129 (p
[0] == '^' && p
[1] == '^')) {
1135 /* Handling unterminated string by UNV */
1138 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1139 t->text[p-line] = *line;
1143 if (type
!= TOK_COMMENT
) {
1144 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1153 * this function allocates a new managed block of memory and
1154 * returns a pointer to the block. The managed blocks are
1155 * deleted only all at once by the delete_Blocks function.
1157 static void *new_Block(size_t size
)
1159 Blocks
*b
= &blocks
;
1161 /* first, get to the end of the linked list */
1164 /* now allocate the requested chunk */
1165 b
->chunk
= nasm_malloc(size
);
1167 /* now allocate a new block for the next request */
1168 b
->next
= nasm_zalloc(sizeof(Blocks
));
1173 * this function deletes all managed blocks of memory
1175 static void delete_Blocks(void)
1177 Blocks
*a
, *b
= &blocks
;
1180 * keep in mind that the first block, pointed to by blocks
1181 * is a static and not dynamically allocated, so we don't
1186 nasm_free(b
->chunk
);
1192 memset(&blocks
, 0, sizeof(blocks
));
1196 * this function creates a new Token and passes a pointer to it
1197 * back to the caller. It sets the type and text elements, and
1198 * also the a.mac and next elements to NULL.
1200 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1201 const char *text
, int txtlen
)
1207 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1208 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1209 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1210 freeTokens
[i
].next
= NULL
;
1213 freeTokens
= t
->next
;
1217 if (type
== TOK_WHITESPACE
|| !text
) {
1221 txtlen
= strlen(text
);
1222 t
->text
= nasm_malloc(txtlen
+1);
1223 memcpy(t
->text
, text
, txtlen
);
1224 t
->text
[txtlen
] = '\0';
1229 static Token
*delete_Token(Token
* t
)
1231 Token
*next
= t
->next
;
1233 t
->next
= freeTokens
;
1239 * Convert a line of tokens back into text.
1240 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1241 * will be transformed into ..@ctxnum.xxx
1243 static char *detoken(Token
* tlist
, bool expand_locals
)
1250 list_for_each(t
, tlist
) {
1251 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1256 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1257 size_t len
= nasm_unquote(v
, NULL
);
1258 size_t clen
= strlen(v
);
1261 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1262 "NUL character in %%! string");
1268 char *p
= getenv(v
);
1270 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1271 "nonexistent environment variable `%s'", v
);
1274 t
->text
= nasm_strdup(p
);
1279 /* Expand local macros here and not during preprocessing */
1280 if (expand_locals
&&
1281 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1282 t
->text
[0] == '%' && t
->text
[1] == '$') {
1285 Context
*ctx
= get_ctx(t
->text
, &q
);
1288 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1289 p
= nasm_strcat(buffer
, q
);
1294 if (t
->type
== TOK_WHITESPACE
)
1297 len
+= strlen(t
->text
);
1300 p
= line
= nasm_malloc(len
+ 1);
1302 list_for_each(t
, tlist
) {
1303 if (t
->type
== TOK_WHITESPACE
) {
1305 } else if (t
->text
) {
1317 * A scanner, suitable for use by the expression evaluator, which
1318 * operates on a line of Tokens. Expects a pointer to a pointer to
1319 * the first token in the line to be passed in as its private_data
1322 * FIX: This really needs to be unified with stdscan.
1324 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1326 Token
**tlineptr
= private_data
;
1328 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1332 *tlineptr
= tline
? tline
->next
: NULL
;
1333 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1334 tline
->type
== TOK_COMMENT
));
1337 return tokval
->t_type
= TOKEN_EOS
;
1339 tokval
->t_charptr
= tline
->text
;
1341 if (tline
->text
[0] == '$' && !tline
->text
[1])
1342 return tokval
->t_type
= TOKEN_HERE
;
1343 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1344 return tokval
->t_type
= TOKEN_BASE
;
1346 if (tline
->type
== TOK_ID
) {
1347 p
= tokval
->t_charptr
= tline
->text
;
1349 tokval
->t_charptr
++;
1350 return tokval
->t_type
= TOKEN_ID
;
1353 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1354 if (r
>= p
+MAX_KEYWORD
)
1355 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1356 *s
++ = nasm_tolower(*r
);
1359 /* right, so we have an identifier sitting in temp storage. now,
1360 * is it actually a register or instruction name, or what? */
1361 return nasm_token_hash(ourcopy
, tokval
);
1364 if (tline
->type
== TOK_NUMBER
) {
1366 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1367 tokval
->t_charptr
= tline
->text
;
1369 return tokval
->t_type
= TOKEN_ERRNUM
;
1371 return tokval
->t_type
= TOKEN_NUM
;
1374 if (tline
->type
== TOK_FLOAT
) {
1375 return tokval
->t_type
= TOKEN_FLOAT
;
1378 if (tline
->type
== TOK_STRING
) {
1381 bq
= tline
->text
[0];
1382 tokval
->t_charptr
= tline
->text
;
1383 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1385 if (ep
[0] != bq
|| ep
[1] != '\0')
1386 return tokval
->t_type
= TOKEN_ERRSTR
;
1388 return tokval
->t_type
= TOKEN_STR
;
1391 if (tline
->type
== TOK_OTHER
) {
1392 if (!strcmp(tline
->text
, "<<"))
1393 return tokval
->t_type
= TOKEN_SHL
;
1394 if (!strcmp(tline
->text
, ">>"))
1395 return tokval
->t_type
= TOKEN_SHR
;
1396 if (!strcmp(tline
->text
, "//"))
1397 return tokval
->t_type
= TOKEN_SDIV
;
1398 if (!strcmp(tline
->text
, "%%"))
1399 return tokval
->t_type
= TOKEN_SMOD
;
1400 if (!strcmp(tline
->text
, "=="))
1401 return tokval
->t_type
= TOKEN_EQ
;
1402 if (!strcmp(tline
->text
, "<>"))
1403 return tokval
->t_type
= TOKEN_NE
;
1404 if (!strcmp(tline
->text
, "!="))
1405 return tokval
->t_type
= TOKEN_NE
;
1406 if (!strcmp(tline
->text
, "<="))
1407 return tokval
->t_type
= TOKEN_LE
;
1408 if (!strcmp(tline
->text
, ">="))
1409 return tokval
->t_type
= TOKEN_GE
;
1410 if (!strcmp(tline
->text
, "&&"))
1411 return tokval
->t_type
= TOKEN_DBL_AND
;
1412 if (!strcmp(tline
->text
, "^^"))
1413 return tokval
->t_type
= TOKEN_DBL_XOR
;
1414 if (!strcmp(tline
->text
, "||"))
1415 return tokval
->t_type
= TOKEN_DBL_OR
;
1419 * We have no other options: just return the first character of
1422 return tokval
->t_type
= tline
->text
[0];
1426 * Compare a string to the name of an existing macro; this is a
1427 * simple wrapper which calls either strcmp or nasm_stricmp
1428 * depending on the value of the `casesense' parameter.
1430 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1432 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1436 * Compare a string to the name of an existing macro; this is a
1437 * simple wrapper which calls either strcmp or nasm_stricmp
1438 * depending on the value of the `casesense' parameter.
1440 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1442 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1446 * Return the Context structure associated with a %$ token. Return
1447 * NULL, having _already_ reported an error condition, if the
1448 * context stack isn't deep enough for the supplied number of $
1451 * If "namep" is non-NULL, set it to the pointer to the macro name
1452 * tail, i.e. the part beyond %$...
1454 static Context
*get_ctx(const char *name
, const char **namep
)
1462 if (!name
|| name
[0] != '%' || name
[1] != '$')
1466 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1473 while (ctx
&& *name
== '$') {
1479 nasm_error(ERR_NONFATAL
, "`%s': context stack is only"
1480 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1491 * Check to see if a file is already in a string list
1493 static bool in_list(const StrList
*list
, const char *str
)
1496 if (!strcmp(list
->str
, str
))
1504 * Open an include file. This routine must always return a valid
1505 * file pointer if it returns - it's responsible for throwing an
1506 * ERR_FATAL and bombing out completely if not. It should also try
1507 * the include path one by one until it finds the file or reaches
1508 * the end of the path.
1510 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1511 char **found_path
, bool missing_ok
, const char *mode
)
1515 IncPath
*ip
= ipath
;
1516 int len
= strlen(file
);
1517 size_t prefix_len
= 0;
1522 path_len
= prefix_len
+ len
+ 1;
1524 sl
= nasm_malloc(path_len
+ sizeof sl
->next
);
1525 memcpy(sl
->str
, prefix
, prefix_len
);
1526 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1528 if (found_path
!= NULL
) {
1529 *found_path
= nasm_malloc(path_len
);
1530 memcpy(*found_path
, sl
->str
, path_len
);
1533 fp
= fopen(sl
->str
, mode
);
1534 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1544 if (found_path
!= NULL
&& *found_path
!= NULL
) {
1545 nasm_free(*found_path
);
1558 prefix_len
= strlen(prefix
);
1560 /* -MG given and file not found */
1561 if (dhead
&& !in_list(*dhead
, file
)) {
1562 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1564 strcpy(sl
->str
, file
);
1572 nasm_error(ERR_FATAL
, "unable to open include file `%s'", file
);
1577 * Opens an include or input file. Public version, for use by modules
1578 * that get a file:lineno pair and need to look at the file again
1579 * (e.g. the CodeView debug backend). Returns NULL on failure.
1581 FILE *pp_input_fopen(const char *filename
, const char *mode
)
1584 StrList
*xsl
= NULL
;
1585 StrList
**xst
= &xsl
;
1587 fp
= inc_fopen(filename
, &xsl
, &xst
, NULL
, true, mode
);
1594 * Determine if we should warn on defining a single-line macro of
1595 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1596 * return true if _any_ single-line macro of that name is defined.
1597 * Otherwise, will return true if a single-line macro with either
1598 * `nparam' or no parameters is defined.
1600 * If a macro with precisely the right number of parameters is
1601 * defined, or nparam is -1, the address of the definition structure
1602 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1603 * is NULL, no action will be taken regarding its contents, and no
1606 * Note that this is also called with nparam zero to resolve
1609 * If you already know which context macro belongs to, you can pass
1610 * the context pointer as first parameter; if you won't but name begins
1611 * with %$ the context will be automatically computed. If all_contexts
1612 * is true, macro will be searched in outer contexts as well.
1615 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1618 struct hash_table
*smtbl
;
1622 smtbl
= &ctx
->localmac
;
1623 } else if (name
[0] == '%' && name
[1] == '$') {
1625 ctx
= get_ctx(name
, &name
);
1627 return false; /* got to return _something_ */
1628 smtbl
= &ctx
->localmac
;
1632 m
= (SMacro
*) hash_findix(smtbl
, name
);
1635 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1636 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1638 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1652 * Count and mark off the parameters in a multi-line macro call.
1653 * This is called both from within the multi-line macro expansion
1654 * code, and also to mark off the default parameters when provided
1655 * in a %macro definition line.
1657 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1659 int paramsize
, brace
;
1661 *nparam
= paramsize
= 0;
1664 /* +1: we need space for the final NULL */
1665 if (*nparam
+1 >= paramsize
) {
1666 paramsize
+= PARAM_DELTA
;
1667 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1671 if (tok_is_(t
, "{"))
1673 (*params
)[(*nparam
)++] = t
;
1675 while (brace
&& (t
= t
->next
) != NULL
) {
1676 if (tok_is_(t
, "{"))
1678 else if (tok_is_(t
, "}"))
1684 * Now we've found the closing brace, look further
1689 if (tok_isnt_(t
, ",")) {
1690 nasm_error(ERR_NONFATAL
,
1691 "braces do not enclose all of macro parameter");
1692 while (tok_isnt_(t
, ","))
1697 while (tok_isnt_(t
, ","))
1700 if (t
) { /* got a comma/brace */
1701 t
= t
->next
; /* eat the comma */
1707 * Determine whether one of the various `if' conditions is true or
1710 * We must free the tline we get passed.
1712 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1714 enum pp_conditional i
= PP_COND(ct
);
1716 Token
*t
, *tt
, **tptr
, *origline
;
1717 struct tokenval tokval
;
1719 enum pp_token_type needtype
;
1726 j
= false; /* have we matched yet? */
1731 if (tline
->type
!= TOK_ID
) {
1732 nasm_error(ERR_NONFATAL
,
1733 "`%s' expects context identifiers", pp_directives
[ct
]);
1734 free_tlist(origline
);
1737 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1739 tline
= tline
->next
;
1744 j
= false; /* have we matched yet? */
1747 if (!tline
|| (tline
->type
!= TOK_ID
&&
1748 (tline
->type
!= TOK_PREPROC_ID
||
1749 tline
->text
[1] != '$'))) {
1750 nasm_error(ERR_NONFATAL
,
1751 "`%s' expects macro identifiers", pp_directives
[ct
]);
1754 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1756 tline
= tline
->next
;
1761 tline
= expand_smacro(tline
);
1762 j
= false; /* have we matched yet? */
1765 if (!tline
|| (tline
->type
!= TOK_ID
&&
1766 tline
->type
!= TOK_STRING
&&
1767 (tline
->type
!= TOK_PREPROC_ID
||
1768 tline
->text
[1] != '!'))) {
1769 nasm_error(ERR_NONFATAL
,
1770 "`%s' expects environment variable names",
1775 if (tline
->type
== TOK_PREPROC_ID
)
1776 p
+= 2; /* Skip leading %! */
1777 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1778 nasm_unquote_cstr(p
, ct
);
1781 tline
= tline
->next
;
1787 tline
= expand_smacro(tline
);
1789 while (tok_isnt_(tt
, ","))
1792 nasm_error(ERR_NONFATAL
,
1793 "`%s' expects two comma-separated arguments",
1798 j
= true; /* assume equality unless proved not */
1799 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1800 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1801 nasm_error(ERR_NONFATAL
, "`%s': more than one comma on line",
1805 if (t
->type
== TOK_WHITESPACE
) {
1809 if (tt
->type
== TOK_WHITESPACE
) {
1813 if (tt
->type
!= t
->type
) {
1814 j
= false; /* found mismatching tokens */
1817 /* When comparing strings, need to unquote them first */
1818 if (t
->type
== TOK_STRING
) {
1819 size_t l1
= nasm_unquote(t
->text
, NULL
);
1820 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1826 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1830 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1831 j
= false; /* found mismatching tokens */
1838 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1839 j
= false; /* trailing gunk on one end or other */
1845 MMacro searching
, *mmac
;
1848 tline
= expand_id(tline
);
1849 if (!tok_type_(tline
, TOK_ID
)) {
1850 nasm_error(ERR_NONFATAL
,
1851 "`%s' expects a macro name", pp_directives
[ct
]);
1854 searching
.name
= nasm_strdup(tline
->text
);
1855 searching
.casesense
= true;
1856 searching
.plus
= false;
1857 searching
.nolist
= false;
1858 searching
.in_progress
= 0;
1859 searching
.max_depth
= 0;
1860 searching
.rep_nest
= NULL
;
1861 searching
.nparam_min
= 0;
1862 searching
.nparam_max
= INT_MAX
;
1863 tline
= expand_smacro(tline
->next
);
1866 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1867 nasm_error(ERR_NONFATAL
,
1868 "`%s' expects a parameter count or nothing",
1871 searching
.nparam_min
= searching
.nparam_max
=
1872 readnum(tline
->text
, &j
);
1874 nasm_error(ERR_NONFATAL
,
1875 "unable to parse parameter count `%s'",
1878 if (tline
&& tok_is_(tline
->next
, "-")) {
1879 tline
= tline
->next
->next
;
1880 if (tok_is_(tline
, "*"))
1881 searching
.nparam_max
= INT_MAX
;
1882 else if (!tok_type_(tline
, TOK_NUMBER
))
1883 nasm_error(ERR_NONFATAL
,
1884 "`%s' expects a parameter count after `-'",
1887 searching
.nparam_max
= readnum(tline
->text
, &j
);
1889 nasm_error(ERR_NONFATAL
,
1890 "unable to parse parameter count `%s'",
1892 if (searching
.nparam_min
> searching
.nparam_max
)
1893 nasm_error(ERR_NONFATAL
,
1894 "minimum parameter count exceeds maximum");
1897 if (tline
&& tok_is_(tline
->next
, "+")) {
1898 tline
= tline
->next
;
1899 searching
.plus
= true;
1901 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1903 if (!strcmp(mmac
->name
, searching
.name
) &&
1904 (mmac
->nparam_min
<= searching
.nparam_max
1906 && (searching
.nparam_min
<= mmac
->nparam_max
1913 if (tline
&& tline
->next
)
1914 nasm_error(ERR_WARNING
|ERR_PASS1
,
1915 "trailing garbage after %%ifmacro ignored");
1916 nasm_free(searching
.name
);
1925 needtype
= TOK_NUMBER
;
1928 needtype
= TOK_STRING
;
1932 t
= tline
= expand_smacro(tline
);
1934 while (tok_type_(t
, TOK_WHITESPACE
) ||
1935 (needtype
== TOK_NUMBER
&&
1936 tok_type_(t
, TOK_OTHER
) &&
1937 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1941 j
= tok_type_(t
, needtype
);
1945 t
= tline
= expand_smacro(tline
);
1946 while (tok_type_(t
, TOK_WHITESPACE
))
1951 t
= t
->next
; /* Skip the actual token */
1952 while (tok_type_(t
, TOK_WHITESPACE
))
1954 j
= !t
; /* Should be nothing left */
1959 t
= tline
= expand_smacro(tline
);
1960 while (tok_type_(t
, TOK_WHITESPACE
))
1963 j
= !t
; /* Should be empty */
1967 t
= tline
= expand_smacro(tline
);
1969 tokval
.t_type
= TOKEN_INVALID
;
1970 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1971 NULL
, pass
| CRITICAL
, NULL
);
1975 nasm_error(ERR_WARNING
|ERR_PASS1
,
1976 "trailing garbage after expression ignored");
1977 if (!is_simple(evalresult
)) {
1978 nasm_error(ERR_NONFATAL
,
1979 "non-constant value given to `%s'", pp_directives
[ct
]);
1982 j
= reloc_value(evalresult
) != 0;
1986 nasm_error(ERR_FATAL
,
1987 "preprocessor directive `%s' not yet implemented",
1992 free_tlist(origline
);
1993 return j
^ PP_NEGATIVE(ct
);
1996 free_tlist(origline
);
2001 * Common code for defining an smacro
2003 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2004 int nparam
, Token
*expansion
)
2006 SMacro
*smac
, **smhead
;
2007 struct hash_table
*smtbl
;
2009 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2011 nasm_error(ERR_WARNING
|ERR_PASS1
,
2012 "single-line macro `%s' defined both with and"
2013 " without parameters", mname
);
2015 * Some instances of the old code considered this a failure,
2016 * some others didn't. What is the right thing to do here?
2018 free_tlist(expansion
);
2019 return false; /* Failure */
2022 * We're redefining, so we have to take over an
2023 * existing SMacro structure. This means freeing
2024 * what was already in it.
2026 nasm_free(smac
->name
);
2027 free_tlist(smac
->expansion
);
2030 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2031 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2032 smac
= nasm_malloc(sizeof(SMacro
));
2033 smac
->next
= *smhead
;
2036 smac
->name
= nasm_strdup(mname
);
2037 smac
->casesense
= casesense
;
2038 smac
->nparam
= nparam
;
2039 smac
->expansion
= expansion
;
2040 smac
->in_progress
= false;
2041 return true; /* Success */
2045 * Undefine an smacro
2047 static void undef_smacro(Context
*ctx
, const char *mname
)
2049 SMacro
**smhead
, *s
, **sp
;
2050 struct hash_table
*smtbl
;
2052 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2053 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2057 * We now have a macro name... go hunt for it.
2060 while ((s
= *sp
) != NULL
) {
2061 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2064 free_tlist(s
->expansion
);
2074 * Parse a mmacro specification.
2076 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2080 tline
= tline
->next
;
2082 tline
= expand_id(tline
);
2083 if (!tok_type_(tline
, TOK_ID
)) {
2084 nasm_error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2089 def
->name
= nasm_strdup(tline
->text
);
2091 def
->nolist
= false;
2092 def
->in_progress
= 0;
2093 def
->rep_nest
= NULL
;
2094 def
->nparam_min
= 0;
2095 def
->nparam_max
= 0;
2097 tline
= expand_smacro(tline
->next
);
2099 if (!tok_type_(tline
, TOK_NUMBER
)) {
2100 nasm_error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2102 def
->nparam_min
= def
->nparam_max
=
2103 readnum(tline
->text
, &err
);
2105 nasm_error(ERR_NONFATAL
,
2106 "unable to parse parameter count `%s'", tline
->text
);
2108 if (tline
&& tok_is_(tline
->next
, "-")) {
2109 tline
= tline
->next
->next
;
2110 if (tok_is_(tline
, "*")) {
2111 def
->nparam_max
= INT_MAX
;
2112 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2113 nasm_error(ERR_NONFATAL
,
2114 "`%s' expects a parameter count after `-'", directive
);
2116 def
->nparam_max
= readnum(tline
->text
, &err
);
2118 nasm_error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2121 if (def
->nparam_min
> def
->nparam_max
) {
2122 nasm_error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2126 if (tline
&& tok_is_(tline
->next
, "+")) {
2127 tline
= tline
->next
;
2130 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2131 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2132 tline
= tline
->next
;
2137 * Handle default parameters.
2139 if (tline
&& tline
->next
) {
2140 def
->dlist
= tline
->next
;
2142 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2145 def
->defaults
= NULL
;
2147 def
->expansion
= NULL
;
2149 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2151 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2152 "too many default macro parameters");
2159 * Decode a size directive
2161 static int parse_size(const char *str
) {
2162 static const char *size_names
[] =
2163 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2164 static const int sizes
[] =
2165 { 0, 1, 4, 16, 8, 10, 2, 32 };
2167 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2171 * find and process preprocessor directive in passed line
2172 * Find out if a line contains a preprocessor directive, and deal
2175 * If a directive _is_ found, it is the responsibility of this routine
2176 * (and not the caller) to free_tlist() the line.
2178 * @param tline a pointer to the current tokeninzed line linked list
2179 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2182 static int do_directive(Token
* tline
)
2184 enum preproc_token i
;
2192 char *p
, *pp
, *found_path
;
2197 MMacro
*mmac
, **mmhead
;
2198 Token
*t
= NULL
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2200 struct tokenval tokval
;
2202 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2210 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2211 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2212 || tline
->text
[1] == '!'))
2213 return NO_DIRECTIVE_FOUND
;
2215 i
= pp_token_hash(tline
->text
);
2218 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2219 * since they are known to be buggy at moment, we need to fix them
2220 * in future release (2.09-2.10)
2222 if (i
== PP_RMACRO
|| i
== PP_IRMACRO
|| i
== PP_EXITMACRO
) {
2223 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2225 return NO_DIRECTIVE_FOUND
;
2229 * If we're in a non-emitting branch of a condition construct,
2230 * or walking to the end of an already terminated %rep block,
2231 * we should ignore all directives except for condition
2234 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2235 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2236 return NO_DIRECTIVE_FOUND
;
2240 * If we're defining a macro or reading a %rep block, we should
2241 * ignore all directives except for %macro/%imacro (which nest),
2242 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2243 * If we're in a %rep block, another %rep nests, so should be let through.
2245 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2246 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2247 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2248 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2249 return NO_DIRECTIVE_FOUND
;
2253 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2254 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2256 return NO_DIRECTIVE_FOUND
;
2257 } else if (nested_mac_count
> 0) {
2258 if (i
== PP_ENDMACRO
) {
2260 return NO_DIRECTIVE_FOUND
;
2263 if (!defining
->name
) {
2266 return NO_DIRECTIVE_FOUND
;
2267 } else if (nested_rep_count
> 0) {
2268 if (i
== PP_ENDREP
) {
2270 return NO_DIRECTIVE_FOUND
;
2278 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2280 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2283 /* Directive to tell NASM what the default stack size is. The
2284 * default is for a 16-bit stack, and this can be overriden with
2287 tline
= tline
->next
;
2288 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2289 tline
= tline
->next
;
2290 if (!tline
|| tline
->type
!= TOK_ID
) {
2291 nasm_error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2292 free_tlist(origline
);
2293 return DIRECTIVE_FOUND
;
2295 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2296 /* All subsequent ARG directives are for a 32-bit stack */
2298 StackPointer
= "ebp";
2301 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2302 /* All subsequent ARG directives are for a 64-bit stack */
2304 StackPointer
= "rbp";
2307 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2308 /* All subsequent ARG directives are for a 16-bit stack,
2309 * far function call.
2312 StackPointer
= "bp";
2315 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2316 /* All subsequent ARG directives are for a 16-bit stack,
2317 * far function call. We don't support near functions.
2320 StackPointer
= "bp";
2324 nasm_error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2325 free_tlist(origline
);
2326 return DIRECTIVE_FOUND
;
2328 free_tlist(origline
);
2329 return DIRECTIVE_FOUND
;
2332 /* TASM like ARG directive to define arguments to functions, in
2333 * the following form:
2335 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2339 char *arg
, directive
[256];
2340 int size
= StackSize
;
2342 /* Find the argument name */
2343 tline
= tline
->next
;
2344 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2345 tline
= tline
->next
;
2346 if (!tline
|| tline
->type
!= TOK_ID
) {
2347 nasm_error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2348 free_tlist(origline
);
2349 return DIRECTIVE_FOUND
;
2353 /* Find the argument size type */
2354 tline
= tline
->next
;
2355 if (!tline
|| tline
->type
!= TOK_OTHER
2356 || tline
->text
[0] != ':') {
2357 nasm_error(ERR_NONFATAL
,
2358 "Syntax error processing `%%arg' directive");
2359 free_tlist(origline
);
2360 return DIRECTIVE_FOUND
;
2362 tline
= tline
->next
;
2363 if (!tline
|| tline
->type
!= TOK_ID
) {
2364 nasm_error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2365 free_tlist(origline
);
2366 return DIRECTIVE_FOUND
;
2369 /* Allow macro expansion of type parameter */
2370 tt
= tokenize(tline
->text
);
2371 tt
= expand_smacro(tt
);
2372 size
= parse_size(tt
->text
);
2374 nasm_error(ERR_NONFATAL
,
2375 "Invalid size type for `%%arg' missing directive");
2377 free_tlist(origline
);
2378 return DIRECTIVE_FOUND
;
2382 /* Round up to even stack slots */
2383 size
= ALIGN(size
, StackSize
);
2385 /* Now define the macro for the argument */
2386 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2387 arg
, StackPointer
, offset
);
2388 do_directive(tokenize(directive
));
2391 /* Move to the next argument in the list */
2392 tline
= tline
->next
;
2393 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2394 tline
= tline
->next
;
2395 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2397 free_tlist(origline
);
2398 return DIRECTIVE_FOUND
;
2401 /* TASM like LOCAL directive to define local variables for a
2402 * function, in the following form:
2404 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2406 * The '= LocalSize' at the end is ignored by NASM, but is
2407 * required by TASM to define the local parameter size (and used
2408 * by the TASM macro package).
2410 offset
= LocalOffset
;
2412 char *local
, directive
[256];
2413 int size
= StackSize
;
2415 /* Find the argument name */
2416 tline
= tline
->next
;
2417 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2418 tline
= tline
->next
;
2419 if (!tline
|| tline
->type
!= TOK_ID
) {
2420 nasm_error(ERR_NONFATAL
,
2421 "`%%local' missing argument parameter");
2422 free_tlist(origline
);
2423 return DIRECTIVE_FOUND
;
2425 local
= tline
->text
;
2427 /* Find the argument size type */
2428 tline
= tline
->next
;
2429 if (!tline
|| tline
->type
!= TOK_OTHER
2430 || tline
->text
[0] != ':') {
2431 nasm_error(ERR_NONFATAL
,
2432 "Syntax error processing `%%local' directive");
2433 free_tlist(origline
);
2434 return DIRECTIVE_FOUND
;
2436 tline
= tline
->next
;
2437 if (!tline
|| tline
->type
!= TOK_ID
) {
2438 nasm_error(ERR_NONFATAL
,
2439 "`%%local' missing size type parameter");
2440 free_tlist(origline
);
2441 return DIRECTIVE_FOUND
;
2444 /* Allow macro expansion of type parameter */
2445 tt
= tokenize(tline
->text
);
2446 tt
= expand_smacro(tt
);
2447 size
= parse_size(tt
->text
);
2449 nasm_error(ERR_NONFATAL
,
2450 "Invalid size type for `%%local' missing directive");
2452 free_tlist(origline
);
2453 return DIRECTIVE_FOUND
;
2457 /* Round up to even stack slots */
2458 size
= ALIGN(size
, StackSize
);
2460 offset
+= size
; /* Negative offset, increment before */
2462 /* Now define the macro for the argument */
2463 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2464 local
, StackPointer
, offset
);
2465 do_directive(tokenize(directive
));
2467 /* Now define the assign to setup the enter_c macro correctly */
2468 snprintf(directive
, sizeof(directive
),
2469 "%%assign %%$localsize %%$localsize+%d", size
);
2470 do_directive(tokenize(directive
));
2472 /* Move to the next argument in the list */
2473 tline
= tline
->next
;
2474 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2475 tline
= tline
->next
;
2476 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2477 LocalOffset
= offset
;
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
;
2483 nasm_error(ERR_WARNING
|ERR_PASS1
,
2484 "trailing garbage after `%%clear' ignored");
2487 free_tlist(origline
);
2488 return DIRECTIVE_FOUND
;
2491 t
= tline
->next
= expand_smacro(tline
->next
);
2493 if (!t
|| (t
->type
!= TOK_STRING
&&
2494 t
->type
!= TOK_INTERNAL_STRING
)) {
2495 nasm_error(ERR_NONFATAL
, "`%%depend' expects a file name");
2496 free_tlist(origline
);
2497 return DIRECTIVE_FOUND
; /* but we did _something_ */
2500 nasm_error(ERR_WARNING
|ERR_PASS1
,
2501 "trailing garbage after `%%depend' ignored");
2503 if (t
->type
!= TOK_INTERNAL_STRING
)
2504 nasm_unquote_cstr(p
, i
);
2505 if (dephead
&& !in_list(*dephead
, p
)) {
2506 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2510 deptail
= &sl
->next
;
2512 free_tlist(origline
);
2513 return DIRECTIVE_FOUND
;
2516 t
= tline
->next
= expand_smacro(tline
->next
);
2519 if (!t
|| (t
->type
!= TOK_STRING
&&
2520 t
->type
!= TOK_INTERNAL_STRING
)) {
2521 nasm_error(ERR_NONFATAL
, "`%%include' expects a file name");
2522 free_tlist(origline
);
2523 return DIRECTIVE_FOUND
; /* but we did _something_ */
2526 nasm_error(ERR_WARNING
|ERR_PASS1
,
2527 "trailing garbage after `%%include' ignored");
2529 if (t
->type
!= TOK_INTERNAL_STRING
)
2530 nasm_unquote_cstr(p
, i
);
2531 inc
= nasm_malloc(sizeof(Include
));
2535 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, &found_path
, pass
== 0, "r");
2537 /* -MG given but file not found */
2540 inc
->fname
= src_set_fname(found_path
? found_path
: p
);
2541 inc
->lineno
= src_set_linnum(0);
2543 inc
->expansion
= NULL
;
2546 lfmt
->uplevel(LIST_INCLUDE
);
2548 free_tlist(origline
);
2549 return DIRECTIVE_FOUND
;
2553 static macros_t
*use_pkg
;
2554 const char *pkg_macro
= NULL
;
2556 tline
= tline
->next
;
2558 tline
= expand_id(tline
);
2560 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2561 tline
->type
!= TOK_INTERNAL_STRING
&&
2562 tline
->type
!= TOK_ID
)) {
2563 nasm_error(ERR_NONFATAL
, "`%%use' expects a package name");
2564 free_tlist(origline
);
2565 return DIRECTIVE_FOUND
; /* but we did _something_ */
2568 nasm_error(ERR_WARNING
|ERR_PASS1
,
2569 "trailing garbage after `%%use' ignored");
2570 if (tline
->type
== TOK_STRING
)
2571 nasm_unquote_cstr(tline
->text
, i
);
2572 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2574 nasm_error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2576 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2577 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2578 /* Not already included, go ahead and include it */
2579 stdmacpos
= use_pkg
;
2581 free_tlist(origline
);
2582 return DIRECTIVE_FOUND
;
2587 tline
= tline
->next
;
2589 tline
= expand_id(tline
);
2591 if (!tok_type_(tline
, TOK_ID
)) {
2592 nasm_error(ERR_NONFATAL
, "`%s' expects a context identifier",
2594 free_tlist(origline
);
2595 return DIRECTIVE_FOUND
; /* but we did _something_ */
2598 nasm_error(ERR_WARNING
|ERR_PASS1
,
2599 "trailing garbage after `%s' ignored",
2601 p
= nasm_strdup(tline
->text
);
2603 p
= NULL
; /* Anonymous */
2607 ctx
= nasm_malloc(sizeof(Context
));
2609 hash_init(&ctx
->localmac
, HASH_SMALL
);
2611 ctx
->number
= unique
++;
2616 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty",
2618 } else if (i
== PP_POP
) {
2619 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2620 nasm_error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2622 cstk
->name
? cstk
->name
: "anonymous", p
);
2627 nasm_free(cstk
->name
);
2633 free_tlist(origline
);
2634 return DIRECTIVE_FOUND
;
2636 severity
= ERR_FATAL
;
2639 severity
= ERR_NONFATAL
;
2642 severity
= ERR_WARNING
|ERR_WARN_USER
;
2647 /* Only error out if this is the final pass */
2648 if (pass
!= 2 && i
!= PP_FATAL
)
2649 return DIRECTIVE_FOUND
;
2651 tline
->next
= expand_smacro(tline
->next
);
2652 tline
= tline
->next
;
2654 t
= tline
? tline
->next
: NULL
;
2656 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2657 /* The line contains only a quoted string */
2659 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2660 nasm_error(severity
, "%s", p
);
2662 /* Not a quoted string, or more than a quoted string */
2663 p
= detoken(tline
, false);
2664 nasm_error(severity
, "%s", p
);
2667 free_tlist(origline
);
2668 return DIRECTIVE_FOUND
;
2672 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2675 j
= if_condition(tline
->next
, i
);
2676 tline
->next
= NULL
; /* it got freed */
2677 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2679 cond
= nasm_malloc(sizeof(Cond
));
2680 cond
->next
= istk
->conds
;
2684 istk
->mstk
->condcnt
++;
2685 free_tlist(origline
);
2686 return DIRECTIVE_FOUND
;
2690 nasm_error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2691 switch(istk
->conds
->state
) {
2693 istk
->conds
->state
= COND_DONE
;
2700 case COND_ELSE_TRUE
:
2701 case COND_ELSE_FALSE
:
2702 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2703 "`%%elif' after `%%else' ignored");
2704 istk
->conds
->state
= COND_NEVER
;
2709 * IMPORTANT: In the case of %if, we will already have
2710 * called expand_mmac_params(); however, if we're
2711 * processing an %elif we must have been in a
2712 * non-emitting mode, which would have inhibited
2713 * the normal invocation of expand_mmac_params().
2714 * Therefore, we have to do it explicitly here.
2716 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2717 tline
->next
= NULL
; /* it got freed */
2718 istk
->conds
->state
=
2719 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2722 free_tlist(origline
);
2723 return DIRECTIVE_FOUND
;
2727 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2728 "trailing garbage after `%%else' ignored");
2730 nasm_fatal(0, "`%%else: no matching `%%if'");
2731 switch(istk
->conds
->state
) {
2734 istk
->conds
->state
= COND_ELSE_FALSE
;
2741 istk
->conds
->state
= COND_ELSE_TRUE
;
2744 case COND_ELSE_TRUE
:
2745 case COND_ELSE_FALSE
:
2746 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2747 "`%%else' after `%%else' ignored.");
2748 istk
->conds
->state
= COND_NEVER
;
2751 free_tlist(origline
);
2752 return DIRECTIVE_FOUND
;
2756 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2757 "trailing garbage after `%%endif' ignored");
2759 nasm_error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2761 istk
->conds
= cond
->next
;
2764 istk
->mstk
->condcnt
--;
2765 free_tlist(origline
);
2766 return DIRECTIVE_FOUND
;
2773 nasm_error(ERR_FATAL
, "`%s': already defining a macro",
2775 return DIRECTIVE_FOUND
;
2777 defining
= nasm_zalloc(sizeof(MMacro
));
2778 defining
->max_depth
=
2779 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2780 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2781 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2782 nasm_free(defining
);
2784 return DIRECTIVE_FOUND
;
2787 src_get(&defining
->xline
, &defining
->fname
);
2789 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2791 if (!strcmp(mmac
->name
, defining
->name
) &&
2792 (mmac
->nparam_min
<= defining
->nparam_max
2794 && (defining
->nparam_min
<= mmac
->nparam_max
2796 nasm_error(ERR_WARNING
|ERR_PASS1
,
2797 "redefining multi-line macro `%s'", defining
->name
);
2798 return DIRECTIVE_FOUND
;
2802 free_tlist(origline
);
2803 return DIRECTIVE_FOUND
;
2807 if (! (defining
&& defining
->name
)) {
2808 nasm_error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2809 return DIRECTIVE_FOUND
;
2811 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2812 defining
->next
= *mmhead
;
2815 free_tlist(origline
);
2816 return DIRECTIVE_FOUND
;
2820 * We must search along istk->expansion until we hit a
2821 * macro-end marker for a macro with a name. Then we
2822 * bypass all lines between exitmacro and endmacro.
2824 list_for_each(l
, istk
->expansion
)
2825 if (l
->finishes
&& l
->finishes
->name
)
2830 * Remove all conditional entries relative to this
2831 * macro invocation. (safe to do in this context)
2833 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2835 istk
->conds
= cond
->next
;
2838 istk
->expansion
= l
;
2840 nasm_error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2842 free_tlist(origline
);
2843 return DIRECTIVE_FOUND
;
2851 spec
.casesense
= (i
== PP_UNMACRO
);
2852 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2853 return DIRECTIVE_FOUND
;
2855 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2856 while (mmac_p
&& *mmac_p
) {
2858 if (mmac
->casesense
== spec
.casesense
&&
2859 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2860 mmac
->nparam_min
== spec
.nparam_min
&&
2861 mmac
->nparam_max
== spec
.nparam_max
&&
2862 mmac
->plus
== spec
.plus
) {
2863 *mmac_p
= mmac
->next
;
2866 mmac_p
= &mmac
->next
;
2869 free_tlist(origline
);
2870 free_tlist(spec
.dlist
);
2871 return DIRECTIVE_FOUND
;
2875 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2876 tline
= tline
->next
;
2878 free_tlist(origline
);
2879 nasm_error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2880 return DIRECTIVE_FOUND
;
2882 t
= expand_smacro(tline
->next
);
2884 free_tlist(origline
);
2887 tokval
.t_type
= TOKEN_INVALID
;
2889 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
2892 return DIRECTIVE_FOUND
;
2894 nasm_error(ERR_WARNING
|ERR_PASS1
,
2895 "trailing garbage after expression ignored");
2896 if (!is_simple(evalresult
)) {
2897 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2898 return DIRECTIVE_FOUND
;
2901 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2902 mmac
= mmac
->next_active
;
2904 nasm_error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2905 } else if (mmac
->nparam
== 0) {
2906 nasm_error(ERR_NONFATAL
,
2907 "`%%rotate' invoked within macro without parameters");
2909 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2911 rotate
%= (int)mmac
->nparam
;
2913 rotate
+= mmac
->nparam
;
2915 mmac
->rotate
= rotate
;
2917 return DIRECTIVE_FOUND
;
2922 tline
= tline
->next
;
2923 } while (tok_type_(tline
, TOK_WHITESPACE
));
2925 if (tok_type_(tline
, TOK_ID
) &&
2926 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2929 tline
= tline
->next
;
2930 } while (tok_type_(tline
, TOK_WHITESPACE
));
2934 t
= expand_smacro(tline
);
2936 tokval
.t_type
= TOKEN_INVALID
;
2938 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
2940 free_tlist(origline
);
2941 return DIRECTIVE_FOUND
;
2944 nasm_error(ERR_WARNING
|ERR_PASS1
,
2945 "trailing garbage after expression ignored");
2946 if (!is_simple(evalresult
)) {
2947 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2948 return DIRECTIVE_FOUND
;
2950 count
= reloc_value(evalresult
);
2951 if (count
>= REP_LIMIT
) {
2952 nasm_error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
2957 nasm_error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2960 free_tlist(origline
);
2962 tmp_defining
= defining
;
2963 defining
= nasm_malloc(sizeof(MMacro
));
2964 defining
->prev
= NULL
;
2965 defining
->name
= NULL
; /* flags this macro as a %rep block */
2966 defining
->casesense
= false;
2967 defining
->plus
= false;
2968 defining
->nolist
= nolist
;
2969 defining
->in_progress
= count
;
2970 defining
->max_depth
= 0;
2971 defining
->nparam_min
= defining
->nparam_max
= 0;
2972 defining
->defaults
= NULL
;
2973 defining
->dlist
= NULL
;
2974 defining
->expansion
= NULL
;
2975 defining
->next_active
= istk
->mstk
;
2976 defining
->rep_nest
= tmp_defining
;
2977 return DIRECTIVE_FOUND
;
2980 if (!defining
|| defining
->name
) {
2981 nasm_error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2982 return DIRECTIVE_FOUND
;
2986 * Now we have a "macro" defined - although it has no name
2987 * and we won't be entering it in the hash tables - we must
2988 * push a macro-end marker for it on to istk->expansion.
2989 * After that, it will take care of propagating itself (a
2990 * macro-end marker line for a macro which is really a %rep
2991 * block will cause the macro to be re-expanded, complete
2992 * with another macro-end marker to ensure the process
2993 * continues) until the whole expansion is forcibly removed
2994 * from istk->expansion by a %exitrep.
2996 l
= nasm_malloc(sizeof(Line
));
2997 l
->next
= istk
->expansion
;
2998 l
->finishes
= defining
;
3000 istk
->expansion
= l
;
3002 istk
->mstk
= defining
;
3004 lfmt
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3005 tmp_defining
= defining
;
3006 defining
= defining
->rep_nest
;
3007 free_tlist(origline
);
3008 return DIRECTIVE_FOUND
;
3012 * We must search along istk->expansion until we hit a
3013 * macro-end marker for a macro with no name. Then we set
3014 * its `in_progress' flag to 0.
3016 list_for_each(l
, istk
->expansion
)
3017 if (l
->finishes
&& !l
->finishes
->name
)
3021 l
->finishes
->in_progress
= 1;
3023 nasm_error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3024 free_tlist(origline
);
3025 return DIRECTIVE_FOUND
;
3031 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3033 tline
= tline
->next
;
3035 tline
= expand_id(tline
);
3036 if (!tline
|| (tline
->type
!= TOK_ID
&&
3037 (tline
->type
!= TOK_PREPROC_ID
||
3038 tline
->text
[1] != '$'))) {
3039 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3041 free_tlist(origline
);
3042 return DIRECTIVE_FOUND
;
3045 ctx
= get_ctx(tline
->text
, &mname
);
3047 param_start
= tline
= tline
->next
;
3050 /* Expand the macro definition now for %xdefine and %ixdefine */
3051 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3052 tline
= expand_smacro(tline
);
3054 if (tok_is_(tline
, "(")) {
3056 * This macro has parameters.
3059 tline
= tline
->next
;
3063 nasm_error(ERR_NONFATAL
, "parameter identifier expected");
3064 free_tlist(origline
);
3065 return DIRECTIVE_FOUND
;
3067 if (tline
->type
!= TOK_ID
) {
3068 nasm_error(ERR_NONFATAL
,
3069 "`%s': parameter identifier expected",
3071 free_tlist(origline
);
3072 return DIRECTIVE_FOUND
;
3074 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3075 tline
= tline
->next
;
3077 if (tok_is_(tline
, ",")) {
3078 tline
= tline
->next
;
3080 if (!tok_is_(tline
, ")")) {
3081 nasm_error(ERR_NONFATAL
,
3082 "`)' expected to terminate macro template");
3083 free_tlist(origline
);
3084 return DIRECTIVE_FOUND
;
3090 tline
= tline
->next
;
3092 if (tok_type_(tline
, TOK_WHITESPACE
))
3093 last
= tline
, tline
= tline
->next
;
3098 if (t
->type
== TOK_ID
) {
3099 list_for_each(tt
, param_start
)
3100 if (tt
->type
>= TOK_SMAC_PARAM
&&
3101 !strcmp(tt
->text
, t
->text
))
3105 t
->next
= macro_start
;
3110 * Good. We now have a macro name, a parameter count, and a
3111 * token list (in reverse order) for an expansion. We ought
3112 * to be OK just to create an SMacro, store it, and let
3113 * free_tlist have the rest of the line (which we have
3114 * carefully re-terminated after chopping off the expansion
3117 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3118 free_tlist(origline
);
3119 return DIRECTIVE_FOUND
;
3122 tline
= tline
->next
;
3124 tline
= expand_id(tline
);
3125 if (!tline
|| (tline
->type
!= TOK_ID
&&
3126 (tline
->type
!= TOK_PREPROC_ID
||
3127 tline
->text
[1] != '$'))) {
3128 nasm_error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3129 free_tlist(origline
);
3130 return DIRECTIVE_FOUND
;
3133 nasm_error(ERR_WARNING
|ERR_PASS1
,
3134 "trailing garbage after macro name ignored");
3137 /* Find the context that symbol belongs to */
3138 ctx
= get_ctx(tline
->text
, &mname
);
3139 undef_smacro(ctx
, mname
);
3140 free_tlist(origline
);
3141 return DIRECTIVE_FOUND
;
3145 casesense
= (i
== PP_DEFSTR
);
3147 tline
= tline
->next
;
3149 tline
= expand_id(tline
);
3150 if (!tline
|| (tline
->type
!= TOK_ID
&&
3151 (tline
->type
!= TOK_PREPROC_ID
||
3152 tline
->text
[1] != '$'))) {
3153 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3155 free_tlist(origline
);
3156 return DIRECTIVE_FOUND
;
3159 ctx
= get_ctx(tline
->text
, &mname
);
3161 tline
= expand_smacro(tline
->next
);
3164 while (tok_type_(tline
, TOK_WHITESPACE
))
3165 tline
= delete_Token(tline
);
3167 p
= detoken(tline
, false);
3168 macro_start
= nasm_malloc(sizeof(*macro_start
));
3169 macro_start
->next
= NULL
;
3170 macro_start
->text
= nasm_quote(p
, strlen(p
));
3171 macro_start
->type
= TOK_STRING
;
3172 macro_start
->a
.mac
= NULL
;
3176 * We now have a macro name, an implicit parameter count of
3177 * zero, and a string token to use as an expansion. Create
3178 * and store an SMacro.
3180 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3181 free_tlist(origline
);
3182 return DIRECTIVE_FOUND
;
3186 casesense
= (i
== PP_DEFTOK
);
3188 tline
= tline
->next
;
3190 tline
= expand_id(tline
);
3191 if (!tline
|| (tline
->type
!= TOK_ID
&&
3192 (tline
->type
!= TOK_PREPROC_ID
||
3193 tline
->text
[1] != '$'))) {
3194 nasm_error(ERR_NONFATAL
,
3195 "`%s' expects a macro identifier as first parameter",
3197 free_tlist(origline
);
3198 return DIRECTIVE_FOUND
;
3200 ctx
= get_ctx(tline
->text
, &mname
);
3202 tline
= expand_smacro(tline
->next
);
3206 while (tok_type_(t
, TOK_WHITESPACE
))
3208 /* t should now point to the string */
3209 if (!tok_type_(t
, TOK_STRING
)) {
3210 nasm_error(ERR_NONFATAL
,
3211 "`%s` requires string as second parameter",
3214 free_tlist(origline
);
3215 return DIRECTIVE_FOUND
;
3219 * Convert the string to a token stream. Note that smacros
3220 * are stored with the token stream reversed, so we have to
3221 * reverse the output of tokenize().
3223 nasm_unquote_cstr(t
->text
, i
);
3224 macro_start
= reverse_tokens(tokenize(t
->text
));
3227 * We now have a macro name, an implicit parameter count of
3228 * zero, and a numeric token to use as an expansion. Create
3229 * and store an SMacro.
3231 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3233 free_tlist(origline
);
3234 return DIRECTIVE_FOUND
;
3239 StrList
*xsl
= NULL
;
3240 StrList
**xst
= &xsl
;
3244 tline
= tline
->next
;
3246 tline
= expand_id(tline
);
3247 if (!tline
|| (tline
->type
!= TOK_ID
&&
3248 (tline
->type
!= TOK_PREPROC_ID
||
3249 tline
->text
[1] != '$'))) {
3250 nasm_error(ERR_NONFATAL
,
3251 "`%%pathsearch' expects a macro identifier as first parameter");
3252 free_tlist(origline
);
3253 return DIRECTIVE_FOUND
;
3255 ctx
= get_ctx(tline
->text
, &mname
);
3257 tline
= expand_smacro(tline
->next
);
3261 while (tok_type_(t
, TOK_WHITESPACE
))
3264 if (!t
|| (t
->type
!= TOK_STRING
&&
3265 t
->type
!= TOK_INTERNAL_STRING
)) {
3266 nasm_error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3268 free_tlist(origline
);
3269 return DIRECTIVE_FOUND
; /* but we did _something_ */
3272 nasm_error(ERR_WARNING
|ERR_PASS1
,
3273 "trailing garbage after `%%pathsearch' ignored");
3275 if (t
->type
!= TOK_INTERNAL_STRING
)
3276 nasm_unquote(p
, NULL
);
3278 fp
= inc_fopen(p
, &xsl
, &xst
, NULL
, true, "r");
3281 fclose(fp
); /* Don't actually care about the file */
3283 macro_start
= nasm_malloc(sizeof(*macro_start
));
3284 macro_start
->next
= NULL
;
3285 macro_start
->text
= nasm_quote(p
, strlen(p
));
3286 macro_start
->type
= TOK_STRING
;
3287 macro_start
->a
.mac
= NULL
;
3292 * We now have a macro name, an implicit parameter count of
3293 * zero, and a string token to use as an expansion. Create
3294 * and store an SMacro.
3296 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3298 free_tlist(origline
);
3299 return DIRECTIVE_FOUND
;
3305 tline
= tline
->next
;
3307 tline
= expand_id(tline
);
3308 if (!tline
|| (tline
->type
!= TOK_ID
&&
3309 (tline
->type
!= TOK_PREPROC_ID
||
3310 tline
->text
[1] != '$'))) {
3311 nasm_error(ERR_NONFATAL
,
3312 "`%%strlen' expects a macro identifier as first parameter");
3313 free_tlist(origline
);
3314 return DIRECTIVE_FOUND
;
3316 ctx
= get_ctx(tline
->text
, &mname
);
3318 tline
= expand_smacro(tline
->next
);
3322 while (tok_type_(t
, TOK_WHITESPACE
))
3324 /* t should now point to the string */
3325 if (!tok_type_(t
, TOK_STRING
)) {
3326 nasm_error(ERR_NONFATAL
,
3327 "`%%strlen` requires string as second parameter");
3329 free_tlist(origline
);
3330 return DIRECTIVE_FOUND
;
3333 macro_start
= nasm_malloc(sizeof(*macro_start
));
3334 macro_start
->next
= NULL
;
3335 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3336 macro_start
->a
.mac
= NULL
;
3339 * We now have a macro name, an implicit parameter count of
3340 * zero, and a numeric token to use as an expansion. Create
3341 * and store an SMacro.
3343 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3345 free_tlist(origline
);
3346 return DIRECTIVE_FOUND
;
3351 tline
= tline
->next
;
3353 tline
= expand_id(tline
);
3354 if (!tline
|| (tline
->type
!= TOK_ID
&&
3355 (tline
->type
!= TOK_PREPROC_ID
||
3356 tline
->text
[1] != '$'))) {
3357 nasm_error(ERR_NONFATAL
,
3358 "`%%strcat' expects a macro identifier as first parameter");
3359 free_tlist(origline
);
3360 return DIRECTIVE_FOUND
;
3362 ctx
= get_ctx(tline
->text
, &mname
);
3364 tline
= expand_smacro(tline
->next
);
3368 list_for_each(t
, tline
) {
3370 case TOK_WHITESPACE
:
3373 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3376 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3378 /* else fall through */
3380 nasm_error(ERR_NONFATAL
,
3381 "non-string passed to `%%strcat' (%d)", t
->type
);
3383 free_tlist(origline
);
3384 return DIRECTIVE_FOUND
;
3388 p
= pp
= nasm_malloc(len
);
3389 list_for_each(t
, tline
) {
3390 if (t
->type
== TOK_STRING
) {
3391 memcpy(p
, t
->text
, t
->a
.len
);
3397 * We now have a macro name, an implicit parameter count of
3398 * zero, and a numeric token to use as an expansion. Create
3399 * and store an SMacro.
3401 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3402 macro_start
->text
= nasm_quote(pp
, len
);
3404 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3406 free_tlist(origline
);
3407 return DIRECTIVE_FOUND
;
3411 int64_t start
, count
;
3416 tline
= tline
->next
;
3418 tline
= expand_id(tline
);
3419 if (!tline
|| (tline
->type
!= TOK_ID
&&
3420 (tline
->type
!= TOK_PREPROC_ID
||
3421 tline
->text
[1] != '$'))) {
3422 nasm_error(ERR_NONFATAL
,
3423 "`%%substr' expects a macro identifier as first parameter");
3424 free_tlist(origline
);
3425 return DIRECTIVE_FOUND
;
3427 ctx
= get_ctx(tline
->text
, &mname
);
3429 tline
= expand_smacro(tline
->next
);
3432 if (tline
) /* skip expanded id */
3434 while (tok_type_(t
, TOK_WHITESPACE
))
3437 /* t should now point to the string */
3438 if (!tok_type_(t
, TOK_STRING
)) {
3439 nasm_error(ERR_NONFATAL
,
3440 "`%%substr` requires string as second parameter");
3442 free_tlist(origline
);
3443 return DIRECTIVE_FOUND
;
3448 tokval
.t_type
= TOKEN_INVALID
;
3449 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3452 free_tlist(origline
);
3453 return DIRECTIVE_FOUND
;
3454 } else if (!is_simple(evalresult
)) {
3455 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3457 free_tlist(origline
);
3458 return DIRECTIVE_FOUND
;
3460 start
= evalresult
->value
- 1;
3462 while (tok_type_(tt
, TOK_WHITESPACE
))
3465 count
= 1; /* Backwards compatibility: one character */
3467 tokval
.t_type
= TOKEN_INVALID
;
3468 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3471 free_tlist(origline
);
3472 return DIRECTIVE_FOUND
;
3473 } else if (!is_simple(evalresult
)) {
3474 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3476 free_tlist(origline
);
3477 return DIRECTIVE_FOUND
;
3479 count
= evalresult
->value
;
3482 len
= nasm_unquote(t
->text
, NULL
);
3484 /* make start and count being in range */
3488 count
= len
+ count
+ 1 - start
;
3489 if (start
+ count
> (int64_t)len
)
3490 count
= len
- start
;
3491 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3492 start
= -1, count
= 0; /* empty string */
3494 macro_start
= nasm_malloc(sizeof(*macro_start
));
3495 macro_start
->next
= NULL
;
3496 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3497 macro_start
->type
= TOK_STRING
;
3498 macro_start
->a
.mac
= NULL
;
3501 * We now have a macro name, an implicit parameter count of
3502 * zero, and a numeric token to use as an expansion. Create
3503 * and store an SMacro.
3505 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3507 free_tlist(origline
);
3508 return DIRECTIVE_FOUND
;
3513 casesense
= (i
== PP_ASSIGN
);
3515 tline
= tline
->next
;
3517 tline
= expand_id(tline
);
3518 if (!tline
|| (tline
->type
!= TOK_ID
&&
3519 (tline
->type
!= TOK_PREPROC_ID
||
3520 tline
->text
[1] != '$'))) {
3521 nasm_error(ERR_NONFATAL
,
3522 "`%%%sassign' expects a macro identifier",
3523 (i
== PP_IASSIGN
? "i" : ""));
3524 free_tlist(origline
);
3525 return DIRECTIVE_FOUND
;
3527 ctx
= get_ctx(tline
->text
, &mname
);
3529 tline
= expand_smacro(tline
->next
);
3534 tokval
.t_type
= TOKEN_INVALID
;
3535 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3538 free_tlist(origline
);
3539 return DIRECTIVE_FOUND
;
3543 nasm_error(ERR_WARNING
|ERR_PASS1
,
3544 "trailing garbage after expression ignored");
3546 if (!is_simple(evalresult
)) {
3547 nasm_error(ERR_NONFATAL
,
3548 "non-constant value given to `%%%sassign'",
3549 (i
== PP_IASSIGN
? "i" : ""));
3550 free_tlist(origline
);
3551 return DIRECTIVE_FOUND
;
3554 macro_start
= nasm_malloc(sizeof(*macro_start
));
3555 macro_start
->next
= NULL
;
3556 make_tok_num(macro_start
, reloc_value(evalresult
));
3557 macro_start
->a
.mac
= NULL
;
3560 * We now have a macro name, an implicit parameter count of
3561 * zero, and a numeric token to use as an expansion. Create
3562 * and store an SMacro.
3564 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3565 free_tlist(origline
);
3566 return DIRECTIVE_FOUND
;
3570 * Syntax is `%line nnn[+mmm] [filename]'
3572 tline
= tline
->next
;
3574 if (!tok_type_(tline
, TOK_NUMBER
)) {
3575 nasm_error(ERR_NONFATAL
, "`%%line' expects line number");
3576 free_tlist(origline
);
3577 return DIRECTIVE_FOUND
;
3579 k
= readnum(tline
->text
, &err
);
3581 tline
= tline
->next
;
3582 if (tok_is_(tline
, "+")) {
3583 tline
= tline
->next
;
3584 if (!tok_type_(tline
, TOK_NUMBER
)) {
3585 nasm_error(ERR_NONFATAL
, "`%%line' expects line increment");
3586 free_tlist(origline
);
3587 return DIRECTIVE_FOUND
;
3589 m
= readnum(tline
->text
, &err
);
3590 tline
= tline
->next
;
3596 char *fname
= detoken(tline
, false);
3597 src_set_fname(fname
);
3600 free_tlist(origline
);
3601 return DIRECTIVE_FOUND
;
3604 nasm_error(ERR_FATAL
,
3605 "preprocessor directive `%s' not yet implemented",
3607 return DIRECTIVE_FOUND
;
3612 * Ensure that a macro parameter contains a condition code and
3613 * nothing else. Return the condition code index if so, or -1
3616 static int find_cc(Token
* t
)
3621 return -1; /* Probably a %+ without a space */
3624 if (t
->type
!= TOK_ID
)
3628 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3631 return bsii(t
->text
, (const char **)conditions
, ARRAY_SIZE(conditions
));
3635 * This routines walks over tokens strem and hadnles tokens
3636 * pasting, if @handle_explicit passed then explicit pasting
3637 * term is handled, otherwise -- implicit pastings only.
3639 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3640 size_t mnum
, bool handle_explicit
)
3642 Token
*tok
, *next
, **prev_next
, **prev_nonspace
;
3643 bool pasted
= false;
3648 * The last token before pasting. We need it
3649 * to be able to connect new handled tokens.
3650 * In other words if there were a tokens stream
3654 * and we've joined tokens B and C, the resulting
3662 if (!tok_type_(tok
, TOK_WHITESPACE
) && !tok_type_(tok
, TOK_PASTE
))
3663 prev_nonspace
= head
;
3665 prev_nonspace
= NULL
;
3667 while (tok
&& (next
= tok
->next
)) {
3669 switch (tok
->type
) {
3670 case TOK_WHITESPACE
:
3671 /* Zap redundant whitespaces */
3672 while (tok_type_(next
, TOK_WHITESPACE
))
3673 next
= delete_Token(next
);
3678 /* Explicit pasting */
3679 if (!handle_explicit
)
3681 next
= delete_Token(tok
);
3683 while (tok_type_(next
, TOK_WHITESPACE
))
3684 next
= delete_Token(next
);
3689 /* Left pasting token is start of line */
3691 nasm_error(ERR_FATAL
, "No lvalue found on pasting");
3694 * No ending token, this might happen in two
3697 * 1) There indeed no right token at all
3698 * 2) There is a bare "%define ID" statement,
3699 * and @ID does expand to whitespace.
3701 * So technically we need to do a grammar analysis
3702 * in another stage of parsing, but for now lets don't
3703 * change the behaviour people used to. Simply allow
3704 * whitespace after paste token.
3708 * Zap ending space tokens and that's all.
3710 tok
= (*prev_nonspace
)->next
;
3711 while (tok_type_(tok
, TOK_WHITESPACE
))
3712 tok
= delete_Token(tok
);
3713 tok
= *prev_nonspace
;
3718 tok
= *prev_nonspace
;
3719 while (tok_type_(tok
, TOK_WHITESPACE
))
3720 tok
= delete_Token(tok
);
3721 len
= strlen(tok
->text
);
3722 len
+= strlen(next
->text
);
3724 p
= buf
= nasm_malloc(len
+ 1);
3725 strcpy(p
, tok
->text
);
3726 p
= strchr(p
, '\0');
3727 strcpy(p
, next
->text
);
3731 tok
= tokenize(buf
);
3734 *prev_nonspace
= tok
;
3735 while (tok
&& tok
->next
)
3738 tok
->next
= delete_Token(next
);
3740 /* Restart from pasted tokens head */
3741 tok
= *prev_nonspace
;
3745 /* implicit pasting */
3746 for (i
= 0; i
< mnum
; i
++) {
3747 if (!(PP_CONCAT_MATCH(tok
, m
[i
].mask_head
)))
3751 while (next
&& PP_CONCAT_MATCH(next
, m
[i
].mask_tail
)) {
3752 len
+= strlen(next
->text
);
3760 len
+= strlen(tok
->text
);
3761 p
= buf
= nasm_malloc(len
+ 1);
3763 while (tok
!= next
) {
3764 strcpy(p
, tok
->text
);
3765 p
= strchr(p
, '\0');
3766 tok
= delete_Token(tok
);
3769 tok
= tokenize(buf
);
3778 * Connect pasted into original stream,
3779 * ie A -> new-tokens -> B
3781 while (tok
&& tok
->next
)
3788 /* Restart from pasted tokens head */
3789 tok
= prev_next
? *prev_next
: *head
;
3795 prev_next
= &tok
->next
;
3798 !tok_type_(tok
->next
, TOK_WHITESPACE
) &&
3799 !tok_type_(tok
->next
, TOK_PASTE
))
3800 prev_nonspace
= prev_next
;
3809 * expands to a list of tokens from %{x:y}
3811 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3813 Token
*t
= tline
, **tt
, *tm
, *head
;
3817 pos
= strchr(tline
->text
, ':');
3820 lst
= atoi(pos
+ 1);
3821 fst
= atoi(tline
->text
+ 1);
3824 * only macros params are accounted so
3825 * if someone passes %0 -- we reject such
3828 if (lst
== 0 || fst
== 0)
3831 /* the values should be sane */
3832 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3833 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3836 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3837 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3839 /* counted from zero */
3843 * It will be at least one token. Note we
3844 * need to scan params until separator, otherwise
3845 * only first token will be passed.
3847 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3848 head
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3849 tt
= &head
->next
, tm
= tm
->next
;
3850 while (tok_isnt_(tm
, ",")) {
3851 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3852 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3856 for (i
= fst
+ 1; i
<= lst
; i
++) {
3857 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3858 *tt
= t
, tt
= &t
->next
;
3859 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3860 tm
= mac
->params
[j
];
3861 while (tok_isnt_(tm
, ",")) {
3862 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3863 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3867 for (i
= fst
- 1; i
>= lst
; i
--) {
3868 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3869 *tt
= t
, tt
= &t
->next
;
3870 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3871 tm
= mac
->params
[j
];
3872 while (tok_isnt_(tm
, ",")) {
3873 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3874 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3883 nasm_error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3889 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3890 * %-n) and MMacro-local identifiers (%%foo) as well as
3891 * macro indirection (%[...]) and range (%{..:..}).
3893 static Token
*expand_mmac_params(Token
* tline
)
3895 Token
*t
, *tt
, **tail
, *thead
;
3896 bool changed
= false;
3903 if (tline
->type
== TOK_PREPROC_ID
&&
3904 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3905 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3906 tline
->text
[1] == '%')) {
3908 int type
= 0, cc
; /* type = 0 to placate optimisers */
3915 tline
= tline
->next
;
3918 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3919 mac
= mac
->next_active
;
3921 nasm_error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3923 pos
= strchr(t
->text
, ':');
3925 switch (t
->text
[1]) {
3927 * We have to make a substitution of one of the
3928 * forms %1, %-1, %+1, %%foo, %0.
3932 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3933 text
= nasm_strdup(tmpbuf
);
3937 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3939 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3942 n
= atoi(t
->text
+ 2) - 1;
3943 if (n
>= mac
->nparam
)
3946 if (mac
->nparam
> 1)
3947 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3948 tt
= mac
->params
[n
];
3952 nasm_error(ERR_NONFATAL
,
3953 "macro parameter %d is not a condition code",
3958 if (inverse_ccs
[cc
] == -1) {
3959 nasm_error(ERR_NONFATAL
,
3960 "condition code `%s' is not invertible",
3964 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3968 n
= atoi(t
->text
+ 2) - 1;
3969 if (n
>= mac
->nparam
)
3972 if (mac
->nparam
> 1)
3973 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3974 tt
= mac
->params
[n
];
3978 nasm_error(ERR_NONFATAL
,
3979 "macro parameter %d is not a condition code",
3984 text
= nasm_strdup(conditions
[cc
]);
3988 n
= atoi(t
->text
+ 1) - 1;
3989 if (n
>= mac
->nparam
)
3992 if (mac
->nparam
> 1)
3993 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3994 tt
= mac
->params
[n
];
3997 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3998 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3999 tail
= &(*tail
)->next
;
4003 text
= NULL
; /* we've done it here */
4008 * seems we have a parameters range here
4010 Token
*head
, **last
;
4011 head
= expand_mmac_params_range(mac
, t
, &last
);
4032 } else if (tline
->type
== TOK_INDIRECT
) {
4034 tline
= tline
->next
;
4035 tt
= tokenize(t
->text
);
4036 tt
= expand_mmac_params(tt
);
4037 tt
= expand_smacro(tt
);
4040 tt
->a
.mac
= NULL
; /* Necessary? */
4048 tline
= tline
->next
;
4056 const struct tokseq_match t
[] = {
4058 PP_CONCAT_MASK(TOK_ID
) |
4059 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4060 PP_CONCAT_MASK(TOK_ID
) |
4061 PP_CONCAT_MASK(TOK_NUMBER
) |
4062 PP_CONCAT_MASK(TOK_FLOAT
) |
4063 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4066 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4067 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4070 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4077 * Expand all single-line macro calls made in the given line.
4078 * Return the expanded version of the line. The original is deemed
4079 * to be destroyed in the process. (In reality we'll just move
4080 * Tokens from input to output a lot of the time, rather than
4081 * actually bothering to destroy and replicate.)
4084 static Token
*expand_smacro(Token
* tline
)
4086 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4087 SMacro
*head
= NULL
, *m
;
4090 unsigned int nparam
, sparam
;
4092 Token
*org_tline
= tline
;
4095 int deadman
= DEADMAN_LIMIT
;
4099 * Trick: we should avoid changing the start token pointer since it can
4100 * be contained in "next" field of other token. Because of this
4101 * we allocate a copy of first token and work with it; at the end of
4102 * routine we copy it back
4105 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4106 org_tline
->text
, 0);
4107 tline
->a
.mac
= org_tline
->a
.mac
;
4108 nasm_free(org_tline
->text
);
4109 org_tline
->text
= NULL
;
4112 expanded
= true; /* Always expand %+ at least once */
4118 while (tline
) { /* main token loop */
4120 nasm_error(ERR_NONFATAL
, "interminable macro recursion");
4124 if ((mname
= tline
->text
)) {
4125 /* if this token is a local macro, look in local context */
4126 if (tline
->type
== TOK_ID
) {
4127 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4128 } else if (tline
->type
== TOK_PREPROC_ID
) {
4129 ctx
= get_ctx(mname
, &mname
);
4130 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4135 * We've hit an identifier. As in is_mmacro below, we first
4136 * check whether the identifier is a single-line macro at
4137 * all, then think about checking for parameters if
4140 list_for_each(m
, head
)
4141 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4147 if (m
->nparam
== 0) {
4149 * Simple case: the macro is parameterless. Discard the
4150 * one token that the macro call took, and push the
4151 * expansion back on the to-do stack.
4153 if (!m
->expansion
) {
4154 if (!strcmp("__FILE__", m
->name
)) {
4155 const char *file
= src_get_fname();
4156 /* nasm_free(tline->text); here? */
4157 tline
->text
= nasm_quote(file
, strlen(file
));
4158 tline
->type
= TOK_STRING
;
4161 if (!strcmp("__LINE__", m
->name
)) {
4162 nasm_free(tline
->text
);
4163 make_tok_num(tline
, src_get_linnum());
4166 if (!strcmp("__BITS__", m
->name
)) {
4167 nasm_free(tline
->text
);
4168 make_tok_num(tline
, globalbits
);
4171 tline
= delete_Token(tline
);
4176 * Complicated case: at least one macro with this name
4177 * exists and takes parameters. We must find the
4178 * parameters in the call, count them, find the SMacro
4179 * that corresponds to that form of the macro call, and
4180 * substitute for the parameters when we expand. What a
4183 /*tline = tline->next;
4184 skip_white_(tline); */
4187 while (tok_type_(t
, TOK_SMAC_END
)) {
4188 t
->a
.mac
->in_progress
= false;
4190 t
= tline
->next
= delete_Token(t
);
4193 } while (tok_type_(tline
, TOK_WHITESPACE
));
4194 if (!tok_is_(tline
, "(")) {
4196 * This macro wasn't called with parameters: ignore
4197 * the call. (Behaviour borrowed from gnu cpp.)
4206 sparam
= PARAM_DELTA
;
4207 params
= nasm_malloc(sparam
* sizeof(Token
*));
4208 params
[0] = tline
->next
;
4209 paramsize
= nasm_malloc(sparam
* sizeof(int));
4211 while (true) { /* parameter loop */
4213 * For some unusual expansions
4214 * which concatenates function call
4217 while (tok_type_(t
, TOK_SMAC_END
)) {
4218 t
->a
.mac
->in_progress
= false;
4220 t
= tline
->next
= delete_Token(t
);
4225 nasm_error(ERR_NONFATAL
,
4226 "macro call expects terminating `)'");
4229 if (tline
->type
== TOK_WHITESPACE
4231 if (paramsize
[nparam
])
4234 params
[nparam
] = tline
->next
;
4235 continue; /* parameter loop */
4237 if (tline
->type
== TOK_OTHER
4238 && tline
->text
[1] == 0) {
4239 char ch
= tline
->text
[0];
4240 if (ch
== ',' && !paren
&& brackets
<= 0) {
4241 if (++nparam
>= sparam
) {
4242 sparam
+= PARAM_DELTA
;
4243 params
= nasm_realloc(params
,
4244 sparam
* sizeof(Token
*));
4245 paramsize
= nasm_realloc(paramsize
,
4246 sparam
* sizeof(int));
4248 params
[nparam
] = tline
->next
;
4249 paramsize
[nparam
] = 0;
4251 continue; /* parameter loop */
4254 (brackets
> 0 || (brackets
== 0 &&
4255 !paramsize
[nparam
])))
4257 if (!(brackets
++)) {
4258 params
[nparam
] = tline
->next
;
4259 continue; /* parameter loop */
4262 if (ch
== '}' && brackets
> 0)
4263 if (--brackets
== 0) {
4265 continue; /* parameter loop */
4267 if (ch
== '(' && !brackets
)
4269 if (ch
== ')' && brackets
<= 0)
4275 nasm_error(ERR_NONFATAL
, "braces do not "
4276 "enclose all of macro parameter");
4278 paramsize
[nparam
] += white
+ 1;
4280 } /* parameter loop */
4282 while (m
&& (m
->nparam
!= nparam
||
4283 mstrcmp(m
->name
, mname
,
4287 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4288 "macro `%s' exists, "
4289 "but not taking %d parameters",
4290 mstart
->text
, nparam
);
4293 if (m
&& m
->in_progress
)
4295 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4297 * Design question: should we handle !tline, which
4298 * indicates missing ')' here, or expand those
4299 * macros anyway, which requires the (t) test a few
4303 nasm_free(paramsize
);
4307 * Expand the macro: we are placed on the last token of the
4308 * call, so that we can easily split the call from the
4309 * following tokens. We also start by pushing an SMAC_END
4310 * token for the cycle removal.
4317 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4319 m
->in_progress
= true;
4321 list_for_each(t
, m
->expansion
) {
4322 if (t
->type
>= TOK_SMAC_PARAM
) {
4323 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4327 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4328 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4330 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4336 } else if (t
->type
== TOK_PREPROC_Q
) {
4337 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4339 } else if (t
->type
== TOK_PREPROC_QQ
) {
4340 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4343 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4349 * Having done that, get rid of the macro call, and clean
4350 * up the parameters.
4353 nasm_free(paramsize
);
4356 continue; /* main token loop */
4361 if (tline
->type
== TOK_SMAC_END
) {
4362 tline
->a
.mac
->in_progress
= false;
4363 tline
= delete_Token(tline
);
4366 tline
= tline
->next
;
4374 * Now scan the entire line and look for successive TOK_IDs that resulted
4375 * after expansion (they can't be produced by tokenize()). The successive
4376 * TOK_IDs should be concatenated.
4377 * Also we look for %+ tokens and concatenate the tokens before and after
4378 * them (without white spaces in between).
4381 const struct tokseq_match t
[] = {
4383 PP_CONCAT_MASK(TOK_ID
) |
4384 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4385 PP_CONCAT_MASK(TOK_ID
) |
4386 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4387 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4390 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4392 * If we concatenated something, *and* we had previously expanded
4393 * an actual macro, scan the lines again for macros...
4404 *org_tline
= *thead
;
4405 /* since we just gave text to org_line, don't free it */
4407 delete_Token(thead
);
4409 /* the expression expanded to empty line;
4410 we can't return NULL for some reasons
4411 we just set the line to a single WHITESPACE token. */
4412 memset(org_tline
, 0, sizeof(*org_tline
));
4413 org_tline
->text
= NULL
;
4414 org_tline
->type
= TOK_WHITESPACE
;
4423 * Similar to expand_smacro but used exclusively with macro identifiers
4424 * right before they are fetched in. The reason is that there can be
4425 * identifiers consisting of several subparts. We consider that if there
4426 * are more than one element forming the name, user wants a expansion,
4427 * otherwise it will be left as-is. Example:
4431 * the identifier %$abc will be left as-is so that the handler for %define
4432 * will suck it and define the corresponding value. Other case:
4434 * %define _%$abc cde
4436 * In this case user wants name to be expanded *before* %define starts
4437 * working, so we'll expand %$abc into something (if it has a value;
4438 * otherwise it will be left as-is) then concatenate all successive
4441 static Token
*expand_id(Token
* tline
)
4443 Token
*cur
, *oldnext
= NULL
;
4445 if (!tline
|| !tline
->next
)
4450 (cur
->next
->type
== TOK_ID
||
4451 cur
->next
->type
== TOK_PREPROC_ID
4452 || cur
->next
->type
== TOK_NUMBER
))
4455 /* If identifier consists of just one token, don't expand */
4460 oldnext
= cur
->next
; /* Detach the tail past identifier */
4461 cur
->next
= NULL
; /* so that expand_smacro stops here */
4464 tline
= expand_smacro(tline
);
4467 /* expand_smacro possibly changhed tline; re-scan for EOL */
4469 while (cur
&& cur
->next
)
4472 cur
->next
= oldnext
;
4479 * Determine whether the given line constitutes a multi-line macro
4480 * call, and return the MMacro structure called if so. Doesn't have
4481 * to check for an initial label - that's taken care of in
4482 * expand_mmacro - but must check numbers of parameters. Guaranteed
4483 * to be called with tline->type == TOK_ID, so the putative macro
4484 * name is easy to find.
4486 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4492 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4495 * Efficiency: first we see if any macro exists with the given
4496 * name. If not, we can return NULL immediately. _Then_ we
4497 * count the parameters, and then we look further along the
4498 * list if necessary to find the proper MMacro.
4500 list_for_each(m
, head
)
4501 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4507 * OK, we have a potential macro. Count and demarcate the
4510 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4513 * So we know how many parameters we've got. Find the MMacro
4514 * structure that handles this number.
4517 if (m
->nparam_min
<= nparam
4518 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4520 * This one is right. Just check if cycle removal
4521 * prohibits us using it before we actually celebrate...
4523 if (m
->in_progress
> m
->max_depth
) {
4524 if (m
->max_depth
> 0) {
4525 nasm_error(ERR_WARNING
,
4526 "reached maximum recursion depth of %i",
4533 * It's right, and we can use it. Add its default
4534 * parameters to the end of our list if necessary.
4536 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4538 nasm_realloc(params
,
4539 ((m
->nparam_min
+ m
->ndefs
+
4540 1) * sizeof(*params
)));
4541 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4542 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4547 * If we've gone over the maximum parameter count (and
4548 * we're in Plus mode), ignore parameters beyond
4551 if (m
->plus
&& nparam
> m
->nparam_max
)
4552 nparam
= m
->nparam_max
;
4554 * Then terminate the parameter list, and leave.
4556 if (!params
) { /* need this special case */
4557 params
= nasm_malloc(sizeof(*params
));
4560 params
[nparam
] = NULL
;
4561 *params_array
= params
;
4565 * This one wasn't right: look for the next one with the
4568 list_for_each(m
, m
->next
)
4569 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4574 * After all that, we didn't find one with the right number of
4575 * parameters. Issue a warning, and fail to expand the macro.
4577 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4578 "macro `%s' exists, but not taking %d parameters",
4579 tline
->text
, nparam
);
4586 * Save MMacro invocation specific fields in
4587 * preparation for a recursive macro expansion
4589 static void push_mmacro(MMacro
*m
)
4591 MMacroInvocation
*i
;
4593 i
= nasm_malloc(sizeof(MMacroInvocation
));
4595 i
->params
= m
->params
;
4596 i
->iline
= m
->iline
;
4597 i
->nparam
= m
->nparam
;
4598 i
->rotate
= m
->rotate
;
4599 i
->paramlen
= m
->paramlen
;
4600 i
->unique
= m
->unique
;
4601 i
->condcnt
= m
->condcnt
;
4607 * Restore MMacro invocation specific fields that were
4608 * saved during a previous recursive macro expansion
4610 static void pop_mmacro(MMacro
*m
)
4612 MMacroInvocation
*i
;
4617 m
->params
= i
->params
;
4618 m
->iline
= i
->iline
;
4619 m
->nparam
= i
->nparam
;
4620 m
->rotate
= i
->rotate
;
4621 m
->paramlen
= i
->paramlen
;
4622 m
->unique
= i
->unique
;
4623 m
->condcnt
= i
->condcnt
;
4630 * Expand the multi-line macro call made by the given line, if
4631 * there is one to be expanded. If there is, push the expansion on
4632 * istk->expansion and return 1. Otherwise return 0.
4634 static int expand_mmacro(Token
* tline
)
4636 Token
*startline
= tline
;
4637 Token
*label
= NULL
;
4638 int dont_prepend
= 0;
4639 Token
**params
, *t
, *tt
;
4642 int i
, nparam
, *paramlen
;
4647 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4648 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4650 m
= is_mmacro(t
, ¶ms
);
4656 * We have an id which isn't a macro call. We'll assume
4657 * it might be a label; we'll also check to see if a
4658 * colon follows it. Then, if there's another id after
4659 * that lot, we'll check it again for macro-hood.
4663 if (tok_type_(t
, TOK_WHITESPACE
))
4664 last
= t
, t
= t
->next
;
4665 if (tok_is_(t
, ":")) {
4667 last
= t
, t
= t
->next
;
4668 if (tok_type_(t
, TOK_WHITESPACE
))
4669 last
= t
, t
= t
->next
;
4671 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4679 * Fix up the parameters: this involves stripping leading and
4680 * trailing whitespace, then stripping braces if they are
4683 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4684 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4686 for (i
= 0; params
[i
]; i
++) {
4688 int comma
= (!m
->plus
|| i
< nparam
- 1);
4692 if (tok_is_(t
, "{"))
4693 t
= t
->next
, brace
++, comma
= false;
4697 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4698 break; /* ... because we have hit a comma */
4699 if (comma
&& t
->type
== TOK_WHITESPACE
4700 && tok_is_(t
->next
, ","))
4701 break; /* ... or a space then a comma */
4702 if (brace
&& t
->type
== TOK_OTHER
) {
4703 if (t
->text
[0] == '{')
4704 brace
++; /* ... or a nested opening brace */
4705 else if (t
->text
[0] == '}')
4707 break; /* ... or a brace */
4713 nasm_error(ERR_NONFATAL
, "macro params should be enclosed in braces");
4717 * OK, we have a MMacro structure together with a set of
4718 * parameters. We must now go through the expansion and push
4719 * copies of each Line on to istk->expansion. Substitution of
4720 * parameter tokens and macro-local tokens doesn't get done
4721 * until the single-line macro substitution process; this is
4722 * because delaying them allows us to change the semantics
4723 * later through %rotate.
4725 * First, push an end marker on to istk->expansion, mark this
4726 * macro as in progress, and set up its invocation-specific
4729 ll
= nasm_malloc(sizeof(Line
));
4730 ll
->next
= istk
->expansion
;
4733 istk
->expansion
= ll
;
4736 * Save the previous MMacro expansion in the case of
4739 if (m
->max_depth
&& m
->in_progress
)
4747 m
->paramlen
= paramlen
;
4748 m
->unique
= unique
++;
4752 m
->next_active
= istk
->mstk
;
4755 list_for_each(l
, m
->expansion
) {
4758 ll
= nasm_malloc(sizeof(Line
));
4759 ll
->finishes
= NULL
;
4760 ll
->next
= istk
->expansion
;
4761 istk
->expansion
= ll
;
4764 list_for_each(t
, l
->first
) {
4768 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4770 case TOK_PREPROC_QQ
:
4771 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4773 case TOK_PREPROC_ID
:
4774 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4782 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4791 * If we had a label, push it on as the first line of
4792 * the macro expansion.
4795 if (dont_prepend
< 0)
4796 free_tlist(startline
);
4798 ll
= nasm_malloc(sizeof(Line
));
4799 ll
->finishes
= NULL
;
4800 ll
->next
= istk
->expansion
;
4801 istk
->expansion
= ll
;
4802 ll
->first
= startline
;
4803 if (!dont_prepend
) {
4805 label
= label
->next
;
4806 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4811 lfmt
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4817 * This function adds macro names to error messages, and suppresses
4818 * them if necessary.
4820 static void pp_verror(int severity
, const char *fmt
, va_list arg
)
4823 MMacro
*mmac
= NULL
;
4827 * If we're in a dead branch of IF or something like it, ignore the error.
4828 * However, because %else etc are evaluated in the state context
4829 * of the previous branch, errors might get lost:
4830 * %if 0 ... %else trailing garbage ... %endif
4831 * So %else etc should set the ERR_PP_PRECOND flag.
4833 if ((severity
& ERR_MASK
) < ERR_FATAL
&&
4834 istk
&& istk
->conds
&&
4835 ((severity
& ERR_PP_PRECOND
) ?
4836 istk
->conds
->state
== COND_NEVER
:
4837 !emitting(istk
->conds
->state
)))
4840 /* get %macro name */
4841 if (!(severity
& ERR_NOFILE
) && istk
&& istk
->mstk
) {
4843 /* but %rep blocks should be skipped */
4844 while (mmac
&& !mmac
->name
)
4845 mmac
= mmac
->next_active
, delta
++;
4849 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4851 nasm_set_verror(real_verror
);
4852 nasm_error(severity
, "(%s:%d) %s",
4853 mmac
->name
, mmac
->lineno
- delta
, buff
);
4854 nasm_set_verror(pp_verror
);
4856 real_verror(severity
, fmt
, arg
);
4861 pp_reset(char *file
, int apass
, StrList
**deplist
)
4866 istk
= nasm_malloc(sizeof(Include
));
4869 istk
->expansion
= NULL
;
4871 istk
->fp
= fopen(file
, "r");
4876 nasm_fatal(ERR_NOFILE
, "unable to open input file `%s'", file
);
4878 nested_mac_count
= 0;
4879 nested_rep_count
= 0;
4882 if (tasm_compatible_mode
) {
4883 stdmacpos
= nasm_stdmac
;
4885 stdmacpos
= nasm_stdmac_after_tasm
;
4887 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4891 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4892 * The caller, however, will also pass in 3 for preprocess-only so
4893 * we can set __PASS__ accordingly.
4895 pass
= apass
> 2 ? 2 : apass
;
4897 dephead
= deptail
= deplist
;
4899 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4901 strcpy(sl
->str
, file
);
4903 deptail
= &sl
->next
;
4907 * Define the __PASS__ macro. This is defined here unlike
4908 * all the other builtins, because it is special -- it varies between
4911 t
= nasm_malloc(sizeof(*t
));
4913 make_tok_num(t
, apass
);
4915 define_smacro(NULL
, "__PASS__", true, 0, t
);
4918 static char *pp_getline(void)
4923 real_verror
= nasm_set_verror(pp_verror
);
4927 * Fetch a tokenized line, either from the macro-expansion
4928 * buffer or from the input file.
4931 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4932 Line
*l
= istk
->expansion
;
4933 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4937 * This is a macro-end marker for a macro with no
4938 * name, which means it's not really a macro at all
4939 * but a %rep block, and the `in_progress' field is
4940 * more than 1, meaning that we still need to
4941 * repeat. (1 means the natural last repetition; 0
4942 * means termination by %exitrep.) We have
4943 * therefore expanded up to the %endrep, and must
4944 * push the whole block on to the expansion buffer
4945 * again. We don't bother to remove the macro-end
4946 * marker: we'd only have to generate another one
4949 l
->finishes
->in_progress
--;
4950 list_for_each(l
, l
->finishes
->expansion
) {
4951 Token
*t
, *tt
, **tail
;
4953 ll
= nasm_malloc(sizeof(Line
));
4954 ll
->next
= istk
->expansion
;
4955 ll
->finishes
= NULL
;
4959 list_for_each(t
, l
->first
) {
4960 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4961 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4966 istk
->expansion
= ll
;
4970 * Check whether a `%rep' was started and not ended
4971 * within this macro expansion. This can happen and
4972 * should be detected. It's a fatal error because
4973 * I'm too confused to work out how to recover
4978 nasm_panic(0, "defining with name in expansion");
4979 else if (istk
->mstk
->name
)
4980 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4981 " expansion of macro `%s'",
4986 * FIXME: investigate the relationship at this point between
4987 * istk->mstk and l->finishes
4990 MMacro
*m
= istk
->mstk
;
4991 istk
->mstk
= m
->next_active
;
4994 * This was a real macro call, not a %rep, and
4995 * therefore the parameter information needs to
5000 l
->finishes
->in_progress
--;
5002 nasm_free(m
->params
);
5003 free_tlist(m
->iline
);
5004 nasm_free(m
->paramlen
);
5005 l
->finishes
->in_progress
= 0;
5010 istk
->expansion
= l
->next
;
5012 lfmt
->downlevel(LIST_MACRO
);
5015 while (1) { /* until we get a line we can use */
5017 if (istk
->expansion
) { /* from a macro expansion */
5019 Line
*l
= istk
->expansion
;
5021 istk
->mstk
->lineno
++;
5023 istk
->expansion
= l
->next
;
5025 p
= detoken(tline
, false);
5026 lfmt
->line(LIST_MACRO
, p
);
5031 if (line
) { /* from the current input file */
5032 line
= prepreproc(line
);
5033 tline
= tokenize(line
);
5038 * The current file has ended; work down the istk
5044 /* nasm_error can't be conditionally suppressed */
5046 "expected `%%endif' before end of file");
5048 /* only set line and file name if there's a next node */
5050 src_set(i
->lineno
, i
->fname
);
5052 lfmt
->downlevel(LIST_INCLUDE
);
5058 if (istk
->expansion
&& istk
->expansion
->finishes
)
5064 * We must expand MMacro parameters and MMacro-local labels
5065 * _before_ we plunge into directive processing, to cope
5066 * with things like `%define something %1' such as STRUC
5067 * uses. Unless we're _defining_ a MMacro, in which case
5068 * those tokens should be left alone to go into the
5069 * definition; and unless we're in a non-emitting
5070 * condition, in which case we don't want to meddle with
5073 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
5074 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
5075 tline
= expand_mmac_params(tline
);
5079 * Check the line to see if it's a preprocessor directive.
5081 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5083 } else if (defining
) {
5085 * We're defining a multi-line macro. We emit nothing
5087 * shove the tokenized line on to the macro definition.
5089 Line
*l
= nasm_malloc(sizeof(Line
));
5090 l
->next
= defining
->expansion
;
5093 defining
->expansion
= l
;
5095 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
5097 * We're in a non-emitting branch of a condition block.
5098 * Emit nothing at all, not even a blank line: when we
5099 * emerge from the condition we'll give a line-number
5100 * directive so we keep our place correctly.
5104 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5106 * We're in a %rep block which has been terminated, so
5107 * we're walking through to the %endrep without
5108 * emitting anything. Emit nothing at all, not even a
5109 * blank line: when we emerge from the %rep block we'll
5110 * give a line-number directive so we keep our place
5116 tline
= expand_smacro(tline
);
5117 if (!expand_mmacro(tline
)) {
5119 * De-tokenize the line again, and emit it.
5121 line
= detoken(tline
, true);
5125 continue; /* expand_mmacro calls free_tlist */
5131 nasm_set_verror(real_verror
);
5135 static void pp_cleanup(int pass
)
5137 real_verror
= nasm_set_verror(pp_verror
);
5140 if (defining
->name
) {
5141 nasm_error(ERR_NONFATAL
,
5142 "end of file while still defining macro `%s'",
5145 nasm_error(ERR_NONFATAL
, "end of file while still in %%rep");
5148 free_mmacro(defining
);
5152 nasm_set_verror(real_verror
);
5165 src_set_fname(NULL
);
5172 while ((i
= ipath
)) {
5181 static void pp_include_path(char *path
)
5185 i
= nasm_malloc(sizeof(IncPath
));
5186 i
->path
= path
? nasm_strdup(path
) : NULL
;
5199 static void pp_pre_include(char *fname
)
5201 Token
*inc
, *space
, *name
;
5204 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5205 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5206 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5208 l
= nasm_malloc(sizeof(Line
));
5215 static void pp_pre_define(char *definition
)
5221 real_verror
= nasm_set_verror(pp_verror
);
5223 equals
= strchr(definition
, '=');
5224 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5225 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5228 space
->next
= tokenize(definition
);
5232 if (space
->next
->type
!= TOK_PREPROC_ID
&&
5233 space
->next
->type
!= TOK_ID
)
5234 nasm_error(ERR_WARNING
, "pre-defining non ID `%s\'\n", definition
);
5236 l
= nasm_malloc(sizeof(Line
));
5242 nasm_set_verror(real_verror
);
5245 static void pp_pre_undefine(char *definition
)
5250 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5251 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5252 space
->next
= tokenize(definition
);
5254 l
= nasm_malloc(sizeof(Line
));
5261 static void pp_extra_stdmac(macros_t
*macros
)
5263 extrastdmac
= macros
;
5266 static void make_tok_num(Token
* tok
, int64_t val
)
5269 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5270 tok
->text
= nasm_strdup(numbuf
);
5271 tok
->type
= TOK_NUMBER
;
5274 static void pp_list_one_macro(MMacro
*m
, int severity
)
5279 /* We need to print the next_active list in reverse order */
5280 pp_list_one_macro(m
->next_active
, severity
);
5282 if (m
->name
&& !m
->nolist
) {
5283 src_set(m
->xline
+ m
->lineno
, m
->fname
);
5284 nasm_error(severity
, "... from macro `%s' defined here", m
->name
);
5288 static void pp_error_list_macros(int severity
)
5291 const char *saved_fname
= NULL
;
5293 severity
|= ERR_PP_LISTMACRO
| ERR_NO_SEVERITY
;
5294 src_get(&saved_line
, &saved_fname
);
5297 pp_list_one_macro(istk
->mstk
, severity
);
5299 src_set(saved_line
, saved_fname
);
5302 const struct preproc_ops nasmpp
= {
5311 pp_error_list_macros
,