1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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 * File real name hash, so we don't have to re-search the include
291 * path for every pass (and potentially more than that if a file
292 * is used more than once.)
294 struct hash_table FileHash
;
297 * Conditional assembly: we maintain a separate stack of these for
298 * each level of file inclusion. (The only reason we keep the
299 * stacks separate is to ensure that a stray `%endif' in a file
300 * included from within the true branch of a `%if' won't terminate
301 * it and cause confusion: instead, rightly, it'll cause an error.)
309 * These states are for use just after %if or %elif: IF_TRUE
310 * means the condition has evaluated to truth so we are
311 * currently emitting, whereas IF_FALSE means we are not
312 * currently emitting but will start doing so if a %else comes
313 * up. In these states, all directives are admissible: %elif,
314 * %else and %endif. (And of course %if.)
316 COND_IF_TRUE
, COND_IF_FALSE
,
318 * These states come up after a %else: ELSE_TRUE means we're
319 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
320 * any %elif or %else will cause an error.
322 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
324 * These states mean that we're not emitting now, and also that
325 * nothing until %endif will be emitted at all. COND_DONE is
326 * used when we've had our moment of emission
327 * and have now started seeing %elifs. COND_NEVER is used when
328 * the condition construct in question is contained within a
329 * non-emitting branch of a larger condition construct,
330 * or if there is an error.
332 COND_DONE
, COND_NEVER
334 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
337 * These defines are used as the possible return values for do_directive
339 #define NO_DIRECTIVE_FOUND 0
340 #define DIRECTIVE_FOUND 1
343 #define REP_LIMIT ((INT64_C(1) << 62))
346 * Condition codes. Note that we use c_ prefix not C_ because C_ is
347 * used in nasm.h for the "real" condition codes. At _this_ level,
348 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
349 * ones, so we need a different enum...
351 static const char * const conditions
[] = {
352 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
353 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
354 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
357 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
358 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
359 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
362 static const enum pp_conds inverse_ccs
[] = {
363 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
364 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
,
365 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
371 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
372 static int is_condition(enum preproc_token arg
)
374 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
377 /* For TASM compatibility we need to be able to recognise TASM compatible
378 * conditional compilation directives. Using the NASM pre-processor does
379 * not work, so we look for them specifically from the following list and
380 * then jam in the equivalent NASM directive into the input stream.
384 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
385 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
388 static const char * const tasm_directives
[] = {
389 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
390 "ifndef", "include", "local"
393 static int StackSize
= 4;
394 static const char *StackPointer
= "ebp";
395 static int ArgOffset
= 8;
396 static int LocalOffset
= 0;
398 static Context
*cstk
;
399 static Include
*istk
;
400 static IncPath
*ipath
= NULL
;
402 static int pass
; /* HACK: pass 0 = generate dependencies only */
403 static StrList
**dephead
;
405 static uint64_t unique
; /* unique identifier numbers */
407 static Line
*predef
= NULL
;
408 static bool do_predef
;
411 * The current set of multi-line macros we have defined.
413 static struct hash_table mmacros
;
416 * The current set of single-line macros we have defined.
418 static struct hash_table smacros
;
421 * The multi-line macro we are currently defining, or the %rep
422 * block we are currently reading, if any.
424 static MMacro
*defining
;
426 static uint64_t nested_mac_count
;
427 static uint64_t nested_rep_count
;
430 * The number of macro parameters to allocate space for at a time.
432 #define PARAM_DELTA 16
435 * The standard macro set: defined in macros.c in a set of arrays.
436 * This gives our position in any macro set, while we are processing it.
437 * The stdmacset is an array of such macro sets.
439 static macros_t
*stdmacpos
;
440 static macros_t
**stdmacnext
;
441 static macros_t
*stdmacros
[8];
442 static macros_t
*extrastdmac
;
445 * Tokens are allocated in blocks to improve speed
447 #define TOKEN_BLOCKSIZE 4096
448 static Token
*freeTokens
= NULL
;
454 static Blocks blocks
= { NULL
, NULL
};
457 * Forward declarations.
459 static void pp_add_stdmac(macros_t
*macros
);
460 static Token
*expand_mmac_params(Token
* tline
);
461 static Token
*expand_smacro(Token
* tline
);
462 static Token
*expand_id(Token
* tline
);
463 static Context
*get_ctx(const char *name
, const char **namep
);
464 static void make_tok_num(Token
* tok
, int64_t val
);
465 static void pp_verror(int severity
, const char *fmt
, va_list ap
);
466 static vefunc real_verror
;
467 static void *new_Block(size_t size
);
468 static void delete_Blocks(void);
469 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
470 const char *text
, int txtlen
);
471 static Token
*delete_Token(Token
* t
);
474 * Macros for safe checking of token pointers, avoid *(NULL)
476 #define tok_type_(x,t) ((x) && (x)->type == (t))
477 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
478 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
479 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
482 * nasm_unquote with error if the string contains NUL characters.
483 * If the string contains NUL characters, issue an error and return
484 * the C len, i.e. truncate at the NUL.
486 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
488 size_t len
= nasm_unquote(qstr
, NULL
);
489 size_t clen
= strlen(qstr
);
492 nasm_error(ERR_NONFATAL
, "NUL character in `%s' directive",
493 pp_directives
[directive
]);
499 * In-place reverse a list of tokens.
501 static Token
*reverse_tokens(Token
*t
)
517 * Handle TASM specific directives, which do not contain a % in
518 * front of them. We do it here because I could not find any other
519 * place to do it for the moment, and it is a hack (ideally it would
520 * be nice to be able to use the NASM pre-processor to do it).
522 static char *check_tasm_directive(char *line
)
524 int32_t i
, j
, k
, m
, len
;
525 char *p
, *q
, *oldline
, oldchar
;
527 p
= nasm_skip_spaces(line
);
529 /* Binary search for the directive name */
531 j
= ARRAY_SIZE(tasm_directives
);
532 q
= nasm_skip_word(p
);
539 m
= nasm_stricmp(p
, tasm_directives
[k
]);
541 /* We have found a directive, so jam a % in front of it
542 * so that NASM will then recognise it as one if it's own.
547 line
= nasm_malloc(len
+ 2);
549 if (k
== TM_IFDIFI
) {
551 * NASM does not recognise IFDIFI, so we convert
552 * it to %if 0. This is not used in NASM
553 * compatible code, but does need to parse for the
554 * TASM macro package.
556 strcpy(line
+ 1, "if 0");
558 memcpy(line
+ 1, p
, len
+ 1);
573 * The pre-preprocessing stage... This function translates line
574 * number indications as they emerge from GNU cpp (`# lineno "file"
575 * flags') into NASM preprocessor line number indications (`%line
578 static char *prepreproc(char *line
)
581 char *fname
, *oldline
;
583 if (line
[0] == '#' && line
[1] == ' ') {
586 lineno
= atoi(fname
);
587 fname
+= strspn(fname
, "0123456789 ");
590 fnlen
= strcspn(fname
, "\"");
591 line
= nasm_malloc(20 + fnlen
);
592 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
595 if (tasm_compatible_mode
)
596 return check_tasm_directive(line
);
601 * Free a linked list of tokens.
603 static void free_tlist(Token
* list
)
606 list
= delete_Token(list
);
610 * Free a linked list of lines.
612 static void free_llist(Line
* list
)
615 list_for_each_safe(l
, tmp
, list
) {
616 free_tlist(l
->first
);
624 static void free_mmacro(MMacro
* m
)
627 free_tlist(m
->dlist
);
628 nasm_free(m
->defaults
);
629 free_llist(m
->expansion
);
634 * Free all currently defined macros, and free the hash tables
636 static void free_smacro_table(struct hash_table
*smt
)
640 struct hash_tbl_node
*it
= NULL
;
642 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
643 nasm_free((void *)key
);
644 list_for_each_safe(s
, tmp
, s
) {
646 free_tlist(s
->expansion
);
653 static void free_mmacro_table(struct hash_table
*mmt
)
657 struct hash_tbl_node
*it
= NULL
;
660 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
661 nasm_free((void *)key
);
662 list_for_each_safe(m
,tmp
, m
)
668 static void free_macros(void)
670 free_smacro_table(&smacros
);
671 free_mmacro_table(&mmacros
);
675 * Initialize the hash tables
677 static void init_macros(void)
679 hash_init(&smacros
, HASH_LARGE
);
680 hash_init(&mmacros
, HASH_LARGE
);
684 * Pop the context stack.
686 static void ctx_pop(void)
691 free_smacro_table(&c
->localmac
);
697 * Search for a key in the hash index; adding it if necessary
698 * (in which case we initialize the data pointer to NULL.)
701 hash_findi_add(struct hash_table
*hash
, const char *str
)
703 struct hash_insert hi
;
707 r
= hash_findi(hash
, str
, &hi
);
711 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
712 return hash_add(&hi
, strx
, NULL
);
716 * Like hash_findi, but returns the data element rather than a pointer
717 * to it. Used only when not adding a new element, hence no third
721 hash_findix(struct hash_table
*hash
, const char *str
)
725 p
= hash_findi(hash
, str
, NULL
);
726 return p
? *p
: NULL
;
730 * read line from standart macros set,
731 * if there no more left -- return NULL
733 static char *line_from_stdmac(void)
736 const unsigned char *p
= stdmacpos
;
745 len
+= pp_directives_len
[c
- 0x80] + 1;
750 line
= nasm_malloc(len
+ 1);
752 while ((c
= *stdmacpos
++)) {
754 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
755 q
+= pp_directives_len
[c
- 0x80];
765 /* This was the last of this particular macro set */
768 stdmacpos
= *stdmacnext
++;
769 } else if (do_predef
) {
771 Token
*head
, **tail
, *t
;
774 * Nasty hack: here we push the contents of
775 * `predef' on to the top-level expansion stack,
776 * since this is the most convenient way to
777 * implement the pre-include and pre-define
780 list_for_each(pd
, predef
) {
783 list_for_each(t
, pd
->first
) {
784 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
785 tail
= &(*tail
)->next
;
788 l
= nasm_malloc(sizeof(Line
));
789 l
->next
= istk
->expansion
;
802 static char *read_line(void)
804 unsigned int size
, c
, next
;
805 const unsigned int delta
= 512;
806 const unsigned int pad
= 8;
807 unsigned int nr_cont
= 0;
811 /* Standart macros set (predefined) goes first */
812 p
= line_from_stdmac();
817 p
= buffer
= nasm_malloc(size
);
821 if ((int)(c
) == EOF
) {
828 next
= fgetc(istk
->fp
);
830 ungetc(next
, istk
->fp
);
845 next
= fgetc(istk
->fp
);
846 ungetc(next
, istk
->fp
);
847 if (next
== '\r' || next
== '\n') {
855 if (c
== '\r' || c
== '\n') {
860 if (p
>= (buffer
+ size
- pad
)) {
861 buffer
= nasm_realloc(buffer
, size
+ delta
);
862 p
= buffer
+ size
- pad
;
866 *p
++ = (unsigned char)c
;
874 src_set_linnum(src_get_linnum() + istk
->lineinc
+
875 (nr_cont
* istk
->lineinc
));
878 * Handle spurious ^Z, which may be inserted into source files
879 * by some file transfer utilities.
881 buffer
[strcspn(buffer
, "\032")] = '\0';
883 lfmt
->line(LIST_READ
, buffer
);
889 * Tokenize a line of text. This is a very simple process since we
890 * don't need to parse the value out of e.g. numeric tokens: we
891 * simply split one string into many.
893 static Token
*tokenize(char *line
)
896 enum pp_token_type type
;
898 Token
*t
, **tail
= &list
;
904 if (*p
== '+' && !nasm_isdigit(p
[1])) {
907 } else if (nasm_isdigit(*p
) ||
908 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
912 while (nasm_isdigit(*p
));
913 type
= TOK_PREPROC_ID
;
914 } else if (*p
== '{') {
923 nasm_error(ERR_WARNING
| ERR_PASS1
,
924 "unterminated %%{ construct");
928 type
= TOK_PREPROC_ID
;
929 } else if (*p
== '[') {
931 line
+= 2; /* Skip the leading %[ */
933 while (lvl
&& (c
= *p
++)) {
945 p
= nasm_skip_string(p
- 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
))
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
&&
1252 t
->text
[0] && t
->text
[1] == '!') {
1257 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1258 size_t len
= nasm_unquote(v
, NULL
);
1259 size_t clen
= strlen(v
);
1262 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1263 "NUL character in %%! string");
1269 char *p
= getenv(v
);
1271 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1272 "nonexistent environment variable `%s'", v
);
1274 * FIXME We better should investigate if accessing
1275 * ->text[1] without ->text[0] is safe enough.
1277 t
->text
= nasm_zalloc(2);
1279 t
->text
= nasm_strdup(p
);
1284 /* Expand local macros here and not during preprocessing */
1285 if (expand_locals
&&
1286 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1287 t
->text
[0] == '%' && t
->text
[1] == '$') {
1290 Context
*ctx
= get_ctx(t
->text
, &q
);
1293 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1294 p
= nasm_strcat(buffer
, q
);
1299 if (t
->type
== TOK_WHITESPACE
)
1302 len
+= strlen(t
->text
);
1305 p
= line
= nasm_malloc(len
+ 1);
1307 list_for_each(t
, tlist
) {
1308 if (t
->type
== TOK_WHITESPACE
) {
1310 } else if (t
->text
) {
1322 * A scanner, suitable for use by the expression evaluator, which
1323 * operates on a line of Tokens. Expects a pointer to a pointer to
1324 * the first token in the line to be passed in as its private_data
1327 * FIX: This really needs to be unified with stdscan.
1329 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1331 Token
**tlineptr
= private_data
;
1333 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1337 *tlineptr
= tline
? tline
->next
: NULL
;
1338 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1339 tline
->type
== TOK_COMMENT
));
1342 return tokval
->t_type
= TOKEN_EOS
;
1344 tokval
->t_charptr
= tline
->text
;
1346 if (tline
->text
[0] == '$' && !tline
->text
[1])
1347 return tokval
->t_type
= TOKEN_HERE
;
1348 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1349 return tokval
->t_type
= TOKEN_BASE
;
1351 if (tline
->type
== TOK_ID
) {
1352 p
= tokval
->t_charptr
= tline
->text
;
1354 tokval
->t_charptr
++;
1355 return tokval
->t_type
= TOKEN_ID
;
1358 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1359 if (r
>= p
+MAX_KEYWORD
)
1360 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1361 *s
++ = nasm_tolower(*r
);
1364 /* right, so we have an identifier sitting in temp storage. now,
1365 * is it actually a register or instruction name, or what? */
1366 return nasm_token_hash(ourcopy
, tokval
);
1369 if (tline
->type
== TOK_NUMBER
) {
1371 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1372 tokval
->t_charptr
= tline
->text
;
1374 return tokval
->t_type
= TOKEN_ERRNUM
;
1376 return tokval
->t_type
= TOKEN_NUM
;
1379 if (tline
->type
== TOK_FLOAT
) {
1380 return tokval
->t_type
= TOKEN_FLOAT
;
1383 if (tline
->type
== TOK_STRING
) {
1386 bq
= tline
->text
[0];
1387 tokval
->t_charptr
= tline
->text
;
1388 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1390 if (ep
[0] != bq
|| ep
[1] != '\0')
1391 return tokval
->t_type
= TOKEN_ERRSTR
;
1393 return tokval
->t_type
= TOKEN_STR
;
1396 if (tline
->type
== TOK_OTHER
) {
1397 if (!strcmp(tline
->text
, "<<"))
1398 return tokval
->t_type
= TOKEN_SHL
;
1399 if (!strcmp(tline
->text
, ">>"))
1400 return tokval
->t_type
= TOKEN_SHR
;
1401 if (!strcmp(tline
->text
, "//"))
1402 return tokval
->t_type
= TOKEN_SDIV
;
1403 if (!strcmp(tline
->text
, "%%"))
1404 return tokval
->t_type
= TOKEN_SMOD
;
1405 if (!strcmp(tline
->text
, "=="))
1406 return tokval
->t_type
= TOKEN_EQ
;
1407 if (!strcmp(tline
->text
, "<>"))
1408 return tokval
->t_type
= TOKEN_NE
;
1409 if (!strcmp(tline
->text
, "!="))
1410 return tokval
->t_type
= TOKEN_NE
;
1411 if (!strcmp(tline
->text
, "<="))
1412 return tokval
->t_type
= TOKEN_LE
;
1413 if (!strcmp(tline
->text
, ">="))
1414 return tokval
->t_type
= TOKEN_GE
;
1415 if (!strcmp(tline
->text
, "&&"))
1416 return tokval
->t_type
= TOKEN_DBL_AND
;
1417 if (!strcmp(tline
->text
, "^^"))
1418 return tokval
->t_type
= TOKEN_DBL_XOR
;
1419 if (!strcmp(tline
->text
, "||"))
1420 return tokval
->t_type
= TOKEN_DBL_OR
;
1424 * We have no other options: just return the first character of
1427 return tokval
->t_type
= tline
->text
[0];
1431 * Compare a string to the name of an existing macro; this is a
1432 * simple wrapper which calls either strcmp or nasm_stricmp
1433 * depending on the value of the `casesense' parameter.
1435 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1437 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1441 * Compare a string to the name of an existing macro; this is a
1442 * simple wrapper which calls either strcmp or nasm_stricmp
1443 * depending on the value of the `casesense' parameter.
1445 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1447 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1451 * Return the Context structure associated with a %$ token. Return
1452 * NULL, having _already_ reported an error condition, if the
1453 * context stack isn't deep enough for the supplied number of $
1456 * If "namep" is non-NULL, set it to the pointer to the macro name
1457 * tail, i.e. the part beyond %$...
1459 static Context
*get_ctx(const char *name
, const char **namep
)
1467 if (!name
|| name
[0] != '%' || name
[1] != '$')
1471 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1478 while (ctx
&& *name
== '$') {
1484 nasm_error(ERR_NONFATAL
, "`%s': context stack is only"
1485 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1496 * Open an include file. This routine must always return a valid
1497 * file pointer if it returns - it's responsible for throwing an
1498 * ERR_FATAL and bombing out completely if not. It should also try
1499 * the include path one by one until it finds the file or reaches
1500 * the end of the path.
1502 * Note: for INC_PROBE the function returns NULL at all times;
1503 * instead look for the
1506 INC_NEEDED
, /* File must exist */
1507 INC_OPTIONAL
, /* Missing is OK */
1508 INC_PROBE
/* Only an existence probe */
1511 /* This is conducts a full pathname search */
1512 static FILE *inc_fopen_search(const char *file
, StrList
**slpath
,
1513 enum incopen_mode omode
, enum file_flags fmode
)
1517 const IncPath
*ip
= ipath
;
1524 sp
= nasm_catfile(prefix
, file
);
1525 len
= strlen(sp
) + 1;
1526 sl
= nasm_malloc(len
+ sizeof sl
->next
);
1527 memcpy(sl
->str
, sp
, len
);
1531 if (omode
== INC_PROBE
) {
1533 found
= nasm_file_exists(sl
->str
);
1535 fp
= nasm_open_read(sl
->str
, fmode
);
1536 found
= (fp
!= NULL
);
1554 * Open a file, or test for the presence of one (depending on omode),
1555 * considering the include path.
1557 static FILE *inc_fopen(const char *file
,
1559 const char **found_path
,
1560 enum incopen_mode omode
,
1561 enum file_flags fmode
)
1564 struct hash_insert hi
;
1569 hp
= hash_find(&FileHash
, file
, &hi
);
1572 if (path
|| omode
!= INC_NEEDED
) {
1573 nasm_add_string_to_strlist(dhead
, path
? path
: file
);
1576 /* Need to do the actual path search */
1580 fp
= inc_fopen_search(file
, &sl
, omode
, fmode
);
1582 file_len
= strlen(file
);
1585 /* Store negative result for this file */
1586 sl
= nasm_malloc(file_len
+ 1 + sizeof sl
->next
);
1587 memcpy(sl
->str
, file
, file_len
+1);
1593 file
= strchr(path
, '\0') - file_len
;
1596 hash_add(&hi
, file
, path
); /* Positive or negative result */
1599 * Add file to dependency path. The in_list() is needed
1600 * in case the file was already added with %depend.
1602 if (path
|| omode
!= INC_NEEDED
)
1603 nasm_add_to_strlist(dhead
, sl
);
1607 if (omode
== INC_NEEDED
)
1608 nasm_fatal(0, "unable to open include file `%s'", file
);
1616 if (!fp
&& omode
!= INC_PROBE
)
1617 fp
= nasm_open_read(path
, fmode
);
1626 * Opens an include or input file. Public version, for use by modules
1627 * that get a file:lineno pair and need to look at the file again
1628 * (e.g. the CodeView debug backend). Returns NULL on failure.
1630 FILE *pp_input_fopen(const char *filename
, enum file_flags mode
)
1632 return inc_fopen(filename
, NULL
, NULL
, INC_OPTIONAL
, mode
);
1636 * Determine if we should warn on defining a single-line macro of
1637 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1638 * return true if _any_ single-line macro of that name is defined.
1639 * Otherwise, will return true if a single-line macro with either
1640 * `nparam' or no parameters is defined.
1642 * If a macro with precisely the right number of parameters is
1643 * defined, or nparam is -1, the address of the definition structure
1644 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1645 * is NULL, no action will be taken regarding its contents, and no
1648 * Note that this is also called with nparam zero to resolve
1651 * If you already know which context macro belongs to, you can pass
1652 * the context pointer as first parameter; if you won't but name begins
1653 * with %$ the context will be automatically computed. If all_contexts
1654 * is true, macro will be searched in outer contexts as well.
1657 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1660 struct hash_table
*smtbl
;
1664 smtbl
= &ctx
->localmac
;
1665 } else if (name
[0] == '%' && name
[1] == '$') {
1667 ctx
= get_ctx(name
, &name
);
1669 return false; /* got to return _something_ */
1670 smtbl
= &ctx
->localmac
;
1674 m
= (SMacro
*) hash_findix(smtbl
, name
);
1677 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1678 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1680 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1694 * Count and mark off the parameters in a multi-line macro call.
1695 * This is called both from within the multi-line macro expansion
1696 * code, and also to mark off the default parameters when provided
1697 * in a %macro definition line.
1699 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1701 int paramsize
, brace
;
1703 *nparam
= paramsize
= 0;
1706 /* +1: we need space for the final NULL */
1707 if (*nparam
+1 >= paramsize
) {
1708 paramsize
+= PARAM_DELTA
;
1709 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1713 if (tok_is_(t
, "{"))
1715 (*params
)[(*nparam
)++] = t
;
1717 while (brace
&& (t
= t
->next
) != NULL
) {
1718 if (tok_is_(t
, "{"))
1720 else if (tok_is_(t
, "}"))
1726 * Now we've found the closing brace, look further
1731 if (tok_isnt_(t
, ",")) {
1732 nasm_error(ERR_NONFATAL
,
1733 "braces do not enclose all of macro parameter");
1734 while (tok_isnt_(t
, ","))
1739 while (tok_isnt_(t
, ","))
1742 if (t
) { /* got a comma/brace */
1743 t
= t
->next
; /* eat the comma */
1749 * Determine whether one of the various `if' conditions is true or
1752 * We must free the tline we get passed.
1754 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1756 enum pp_conditional i
= PP_COND(ct
);
1758 Token
*t
, *tt
, **tptr
, *origline
;
1759 struct tokenval tokval
;
1761 enum pp_token_type needtype
;
1768 j
= false; /* have we matched yet? */
1773 if (tline
->type
!= TOK_ID
) {
1774 nasm_error(ERR_NONFATAL
,
1775 "`%s' expects context identifiers", pp_directives
[ct
]);
1776 free_tlist(origline
);
1779 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1781 tline
= tline
->next
;
1786 j
= false; /* have we matched yet? */
1789 if (!tline
|| (tline
->type
!= TOK_ID
&&
1790 (tline
->type
!= TOK_PREPROC_ID
||
1791 tline
->text
[1] != '$'))) {
1792 nasm_error(ERR_NONFATAL
,
1793 "`%s' expects macro identifiers", pp_directives
[ct
]);
1796 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1798 tline
= tline
->next
;
1803 tline
= expand_smacro(tline
);
1804 j
= false; /* have we matched yet? */
1807 if (!tline
|| (tline
->type
!= TOK_ID
&&
1808 tline
->type
!= TOK_STRING
&&
1809 (tline
->type
!= TOK_PREPROC_ID
||
1810 tline
->text
[1] != '!'))) {
1811 nasm_error(ERR_NONFATAL
,
1812 "`%s' expects environment variable names",
1817 if (tline
->type
== TOK_PREPROC_ID
)
1818 p
+= 2; /* Skip leading %! */
1819 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1820 nasm_unquote_cstr(p
, ct
);
1823 tline
= tline
->next
;
1829 tline
= expand_smacro(tline
);
1831 while (tok_isnt_(tt
, ","))
1834 nasm_error(ERR_NONFATAL
,
1835 "`%s' expects two comma-separated arguments",
1840 j
= true; /* assume equality unless proved not */
1841 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1842 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1843 nasm_error(ERR_NONFATAL
, "`%s': more than one comma on line",
1847 if (t
->type
== TOK_WHITESPACE
) {
1851 if (tt
->type
== TOK_WHITESPACE
) {
1855 if (tt
->type
!= t
->type
) {
1856 j
= false; /* found mismatching tokens */
1859 /* When comparing strings, need to unquote them first */
1860 if (t
->type
== TOK_STRING
) {
1861 size_t l1
= nasm_unquote(t
->text
, NULL
);
1862 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1868 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1872 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1873 j
= false; /* found mismatching tokens */
1880 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1881 j
= false; /* trailing gunk on one end or other */
1887 MMacro searching
, *mmac
;
1890 tline
= expand_id(tline
);
1891 if (!tok_type_(tline
, TOK_ID
)) {
1892 nasm_error(ERR_NONFATAL
,
1893 "`%s' expects a macro name", pp_directives
[ct
]);
1896 searching
.name
= nasm_strdup(tline
->text
);
1897 searching
.casesense
= true;
1898 searching
.plus
= false;
1899 searching
.nolist
= false;
1900 searching
.in_progress
= 0;
1901 searching
.max_depth
= 0;
1902 searching
.rep_nest
= NULL
;
1903 searching
.nparam_min
= 0;
1904 searching
.nparam_max
= INT_MAX
;
1905 tline
= expand_smacro(tline
->next
);
1908 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1909 nasm_error(ERR_NONFATAL
,
1910 "`%s' expects a parameter count or nothing",
1913 searching
.nparam_min
= searching
.nparam_max
=
1914 readnum(tline
->text
, &j
);
1916 nasm_error(ERR_NONFATAL
,
1917 "unable to parse parameter count `%s'",
1920 if (tline
&& tok_is_(tline
->next
, "-")) {
1921 tline
= tline
->next
->next
;
1922 if (tok_is_(tline
, "*"))
1923 searching
.nparam_max
= INT_MAX
;
1924 else if (!tok_type_(tline
, TOK_NUMBER
))
1925 nasm_error(ERR_NONFATAL
,
1926 "`%s' expects a parameter count after `-'",
1929 searching
.nparam_max
= readnum(tline
->text
, &j
);
1931 nasm_error(ERR_NONFATAL
,
1932 "unable to parse parameter count `%s'",
1934 if (searching
.nparam_min
> searching
.nparam_max
) {
1935 nasm_error(ERR_NONFATAL
,
1936 "minimum parameter count exceeds maximum");
1937 searching
.nparam_max
= searching
.nparam_min
;
1941 if (tline
&& tok_is_(tline
->next
, "+")) {
1942 tline
= tline
->next
;
1943 searching
.plus
= true;
1945 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1947 if (!strcmp(mmac
->name
, searching
.name
) &&
1948 (mmac
->nparam_min
<= searching
.nparam_max
1950 && (searching
.nparam_min
<= mmac
->nparam_max
1957 if (tline
&& tline
->next
)
1958 nasm_error(ERR_WARNING
|ERR_PASS1
,
1959 "trailing garbage after %%ifmacro ignored");
1960 nasm_free(searching
.name
);
1969 needtype
= TOK_NUMBER
;
1972 needtype
= TOK_STRING
;
1976 t
= tline
= expand_smacro(tline
);
1978 while (tok_type_(t
, TOK_WHITESPACE
) ||
1979 (needtype
== TOK_NUMBER
&&
1980 tok_type_(t
, TOK_OTHER
) &&
1981 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1985 j
= tok_type_(t
, needtype
);
1989 t
= tline
= expand_smacro(tline
);
1990 while (tok_type_(t
, TOK_WHITESPACE
))
1995 t
= t
->next
; /* Skip the actual token */
1996 while (tok_type_(t
, TOK_WHITESPACE
))
1998 j
= !t
; /* Should be nothing left */
2003 t
= tline
= expand_smacro(tline
);
2004 while (tok_type_(t
, TOK_WHITESPACE
))
2007 j
= !t
; /* Should be empty */
2011 t
= tline
= expand_smacro(tline
);
2013 tokval
.t_type
= TOKEN_INVALID
;
2014 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2015 NULL
, pass
| CRITICAL
, NULL
);
2019 nasm_error(ERR_WARNING
|ERR_PASS1
,
2020 "trailing garbage after expression ignored");
2021 if (!is_simple(evalresult
)) {
2022 nasm_error(ERR_NONFATAL
,
2023 "non-constant value given to `%s'", pp_directives
[ct
]);
2026 j
= reloc_value(evalresult
) != 0;
2030 nasm_error(ERR_FATAL
,
2031 "preprocessor directive `%s' not yet implemented",
2036 free_tlist(origline
);
2037 return j
^ PP_NEGATIVE(ct
);
2040 free_tlist(origline
);
2045 * Common code for defining an smacro
2047 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2048 int nparam
, Token
*expansion
)
2050 SMacro
*smac
, **smhead
;
2051 struct hash_table
*smtbl
;
2053 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2055 nasm_error(ERR_WARNING
|ERR_PASS1
,
2056 "single-line macro `%s' defined both with and"
2057 " without parameters", mname
);
2059 * Some instances of the old code considered this a failure,
2060 * some others didn't. What is the right thing to do here?
2062 free_tlist(expansion
);
2063 return false; /* Failure */
2066 * We're redefining, so we have to take over an
2067 * existing SMacro structure. This means freeing
2068 * what was already in it.
2070 nasm_free(smac
->name
);
2071 free_tlist(smac
->expansion
);
2074 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2075 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2076 smac
= nasm_malloc(sizeof(SMacro
));
2077 smac
->next
= *smhead
;
2080 smac
->name
= nasm_strdup(mname
);
2081 smac
->casesense
= casesense
;
2082 smac
->nparam
= nparam
;
2083 smac
->expansion
= expansion
;
2084 smac
->in_progress
= false;
2085 return true; /* Success */
2089 * Undefine an smacro
2091 static void undef_smacro(Context
*ctx
, const char *mname
)
2093 SMacro
**smhead
, *s
, **sp
;
2094 struct hash_table
*smtbl
;
2096 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2097 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2101 * We now have a macro name... go hunt for it.
2104 while ((s
= *sp
) != NULL
) {
2105 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2108 free_tlist(s
->expansion
);
2118 * Parse a mmacro specification.
2120 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2124 tline
= tline
->next
;
2126 tline
= expand_id(tline
);
2127 if (!tok_type_(tline
, TOK_ID
)) {
2128 nasm_error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2133 def
->name
= nasm_strdup(tline
->text
);
2135 def
->nolist
= false;
2136 def
->in_progress
= 0;
2137 def
->rep_nest
= NULL
;
2138 def
->nparam_min
= 0;
2139 def
->nparam_max
= 0;
2141 tline
= expand_smacro(tline
->next
);
2143 if (!tok_type_(tline
, TOK_NUMBER
)) {
2144 nasm_error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2146 def
->nparam_min
= def
->nparam_max
=
2147 readnum(tline
->text
, &err
);
2149 nasm_error(ERR_NONFATAL
,
2150 "unable to parse parameter count `%s'", tline
->text
);
2152 if (tline
&& tok_is_(tline
->next
, "-")) {
2153 tline
= tline
->next
->next
;
2154 if (tok_is_(tline
, "*")) {
2155 def
->nparam_max
= INT_MAX
;
2156 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2157 nasm_error(ERR_NONFATAL
,
2158 "`%s' expects a parameter count after `-'", directive
);
2160 def
->nparam_max
= readnum(tline
->text
, &err
);
2162 nasm_error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2165 if (def
->nparam_min
> def
->nparam_max
) {
2166 nasm_error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2167 def
->nparam_max
= def
->nparam_min
;
2171 if (tline
&& tok_is_(tline
->next
, "+")) {
2172 tline
= tline
->next
;
2175 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2176 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2177 tline
= tline
->next
;
2182 * Handle default parameters.
2184 if (tline
&& tline
->next
) {
2185 def
->dlist
= tline
->next
;
2187 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2190 def
->defaults
= NULL
;
2192 def
->expansion
= NULL
;
2194 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2196 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2197 "too many default macro parameters");
2204 * Decode a size directive
2206 static int parse_size(const char *str
) {
2207 static const char *size_names
[] =
2208 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2209 static const int sizes
[] =
2210 { 0, 1, 4, 16, 8, 10, 2, 32 };
2211 return str
? sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1] : 0;
2215 * Process a preprocessor %pragma directive. Currently there are none.
2216 * Gets passed the token list starting with the "preproc" token from
2217 * "%pragma preproc".
2219 static void do_pragma_preproc(Token
*tline
)
2221 /* Skip to the real stuff */
2222 tline
= tline
->next
;
2227 (void)tline
; /* Nothing else to do at present */
2231 * find and process preprocessor directive in passed line
2232 * Find out if a line contains a preprocessor directive, and deal
2235 * If a directive _is_ found, it is the responsibility of this routine
2236 * (and not the caller) to free_tlist() the line.
2238 * @param tline a pointer to the current tokeninzed line linked list
2239 * @param output if this directive generated output
2240 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2243 static int do_directive(Token
*tline
, char **output
)
2245 enum preproc_token i
;
2254 const char *found_path
;
2259 MMacro
*mmac
, **mmhead
;
2260 Token
*t
= NULL
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2262 struct tokenval tokval
;
2264 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2269 *output
= NULL
; /* No output generated */
2273 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2274 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2275 || tline
->text
[1] == '!'))
2276 return NO_DIRECTIVE_FOUND
;
2278 i
= pp_token_hash(tline
->text
);
2281 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2282 * since they are known to be buggy at moment, we need to fix them
2283 * in future release (2.09-2.10)
2285 if (i
== PP_RMACRO
|| i
== PP_IRMACRO
|| i
== PP_EXITMACRO
) {
2286 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2288 return NO_DIRECTIVE_FOUND
;
2292 * If we're in a non-emitting branch of a condition construct,
2293 * or walking to the end of an already terminated %rep block,
2294 * we should ignore all directives except for condition
2297 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2298 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2299 return NO_DIRECTIVE_FOUND
;
2303 * If we're defining a macro or reading a %rep block, we should
2304 * ignore all directives except for %macro/%imacro (which nest),
2305 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2306 * If we're in a %rep block, another %rep nests, so should be let through.
2308 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2309 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2310 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2311 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2312 return NO_DIRECTIVE_FOUND
;
2316 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2317 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2319 return NO_DIRECTIVE_FOUND
;
2320 } else if (nested_mac_count
> 0) {
2321 if (i
== PP_ENDMACRO
) {
2323 return NO_DIRECTIVE_FOUND
;
2326 if (!defining
->name
) {
2329 return NO_DIRECTIVE_FOUND
;
2330 } else if (nested_rep_count
> 0) {
2331 if (i
== PP_ENDREP
) {
2333 return NO_DIRECTIVE_FOUND
;
2341 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2343 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2347 * %pragma namespace options...
2349 * The namespace "preproc" is reserved for the preprocessor;
2350 * all other namespaces generate a [pragma] assembly directive.
2352 * Invalid %pragmas are ignored and may have different
2353 * meaning in future versions of NASM.
2355 tline
= tline
->next
;
2357 tline
= expand_smacro(tline
);
2358 if (tok_type_(tline
, TOK_ID
)) {
2359 if (!nasm_stricmp(tline
->text
, "preproc")) {
2360 /* Preprocessor pragma */
2361 do_pragma_preproc(tline
);
2363 /* Build the assembler directive */
2364 t
= new_Token(NULL
, TOK_OTHER
, "[", 1);
2365 t
->next
= new_Token(NULL
, TOK_ID
, "pragma", 6);
2366 t
->next
->next
= new_Token(tline
, TOK_WHITESPACE
, NULL
, 0);
2368 for (t
= tline
; t
->next
; t
= t
->next
)
2370 t
->next
= new_Token(NULL
, TOK_OTHER
, "]", 1);
2371 /* true here can be revisited in the future */
2372 *output
= detoken(tline
, true);
2375 free_tlist(origline
);
2376 return DIRECTIVE_FOUND
;
2379 /* Directive to tell NASM what the default stack size is. The
2380 * default is for a 16-bit stack, and this can be overriden with
2383 tline
= tline
->next
;
2384 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2385 tline
= tline
->next
;
2386 if (!tline
|| tline
->type
!= TOK_ID
) {
2387 nasm_error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2388 free_tlist(origline
);
2389 return DIRECTIVE_FOUND
;
2391 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2392 /* All subsequent ARG directives are for a 32-bit stack */
2394 StackPointer
= "ebp";
2397 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2398 /* All subsequent ARG directives are for a 64-bit stack */
2400 StackPointer
= "rbp";
2403 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2404 /* All subsequent ARG directives are for a 16-bit stack,
2405 * far function call.
2408 StackPointer
= "bp";
2411 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2412 /* All subsequent ARG directives are for a 16-bit stack,
2413 * far function call. We don't support near functions.
2416 StackPointer
= "bp";
2420 nasm_error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2421 free_tlist(origline
);
2422 return DIRECTIVE_FOUND
;
2424 free_tlist(origline
);
2425 return DIRECTIVE_FOUND
;
2428 /* TASM like ARG directive to define arguments to functions, in
2429 * the following form:
2431 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2435 char *arg
, directive
[256];
2436 int size
= StackSize
;
2438 /* Find the argument name */
2439 tline
= tline
->next
;
2440 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2441 tline
= tline
->next
;
2442 if (!tline
|| tline
->type
!= TOK_ID
) {
2443 nasm_error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2444 free_tlist(origline
);
2445 return DIRECTIVE_FOUND
;
2449 /* Find the argument size type */
2450 tline
= tline
->next
;
2451 if (!tline
|| tline
->type
!= TOK_OTHER
2452 || tline
->text
[0] != ':') {
2453 nasm_error(ERR_NONFATAL
,
2454 "Syntax error processing `%%arg' directive");
2455 free_tlist(origline
);
2456 return DIRECTIVE_FOUND
;
2458 tline
= tline
->next
;
2459 if (!tline
|| tline
->type
!= TOK_ID
) {
2460 nasm_error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2461 free_tlist(origline
);
2462 return DIRECTIVE_FOUND
;
2465 /* Allow macro expansion of type parameter */
2466 tt
= tokenize(tline
->text
);
2467 tt
= expand_smacro(tt
);
2468 size
= parse_size(tt
->text
);
2470 nasm_error(ERR_NONFATAL
,
2471 "Invalid size type for `%%arg' missing directive");
2473 free_tlist(origline
);
2474 return DIRECTIVE_FOUND
;
2478 /* Round up to even stack slots */
2479 size
= ALIGN(size
, StackSize
);
2481 /* Now define the macro for the argument */
2482 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2483 arg
, StackPointer
, offset
);
2484 do_directive(tokenize(directive
), output
);
2487 /* Move to the next argument in the list */
2488 tline
= tline
->next
;
2489 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2490 tline
= tline
->next
;
2491 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2493 free_tlist(origline
);
2494 return DIRECTIVE_FOUND
;
2497 /* TASM like LOCAL directive to define local variables for a
2498 * function, in the following form:
2500 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2502 * The '= LocalSize' at the end is ignored by NASM, but is
2503 * required by TASM to define the local parameter size (and used
2504 * by the TASM macro package).
2506 offset
= LocalOffset
;
2508 char *local
, directive
[256];
2509 int size
= StackSize
;
2511 /* Find the argument name */
2512 tline
= tline
->next
;
2513 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2514 tline
= tline
->next
;
2515 if (!tline
|| tline
->type
!= TOK_ID
) {
2516 nasm_error(ERR_NONFATAL
,
2517 "`%%local' missing argument parameter");
2518 free_tlist(origline
);
2519 return DIRECTIVE_FOUND
;
2521 local
= tline
->text
;
2523 /* Find the argument size type */
2524 tline
= tline
->next
;
2525 if (!tline
|| tline
->type
!= TOK_OTHER
2526 || tline
->text
[0] != ':') {
2527 nasm_error(ERR_NONFATAL
,
2528 "Syntax error processing `%%local' directive");
2529 free_tlist(origline
);
2530 return DIRECTIVE_FOUND
;
2532 tline
= tline
->next
;
2533 if (!tline
|| tline
->type
!= TOK_ID
) {
2534 nasm_error(ERR_NONFATAL
,
2535 "`%%local' missing size type parameter");
2536 free_tlist(origline
);
2537 return DIRECTIVE_FOUND
;
2540 /* Allow macro expansion of type parameter */
2541 tt
= tokenize(tline
->text
);
2542 tt
= expand_smacro(tt
);
2543 size
= parse_size(tt
->text
);
2545 nasm_error(ERR_NONFATAL
,
2546 "Invalid size type for `%%local' missing directive");
2548 free_tlist(origline
);
2549 return DIRECTIVE_FOUND
;
2553 /* Round up to even stack slots */
2554 size
= ALIGN(size
, StackSize
);
2556 offset
+= size
; /* Negative offset, increment before */
2558 /* Now define the macro for the argument */
2559 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2560 local
, StackPointer
, offset
);
2561 do_directive(tokenize(directive
), output
);
2563 /* Now define the assign to setup the enter_c macro correctly */
2564 snprintf(directive
, sizeof(directive
),
2565 "%%assign %%$localsize %%$localsize+%d", size
);
2566 do_directive(tokenize(directive
), output
);
2568 /* Move to the next argument in the list */
2569 tline
= tline
->next
;
2570 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2571 tline
= tline
->next
;
2572 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2573 LocalOffset
= offset
;
2574 free_tlist(origline
);
2575 return DIRECTIVE_FOUND
;
2579 nasm_error(ERR_WARNING
|ERR_PASS1
,
2580 "trailing garbage after `%%clear' ignored");
2583 free_tlist(origline
);
2584 return DIRECTIVE_FOUND
;
2587 t
= tline
->next
= expand_smacro(tline
->next
);
2589 if (!t
|| (t
->type
!= TOK_STRING
&&
2590 t
->type
!= TOK_INTERNAL_STRING
)) {
2591 nasm_error(ERR_NONFATAL
, "`%%depend' expects a file name");
2592 free_tlist(origline
);
2593 return DIRECTIVE_FOUND
; /* but we did _something_ */
2596 nasm_error(ERR_WARNING
|ERR_PASS1
,
2597 "trailing garbage after `%%depend' ignored");
2599 if (t
->type
!= TOK_INTERNAL_STRING
)
2600 nasm_unquote_cstr(p
, i
);
2601 nasm_add_string_to_strlist(dephead
, p
);
2602 free_tlist(origline
);
2603 return DIRECTIVE_FOUND
;
2606 t
= tline
->next
= expand_smacro(tline
->next
);
2609 if (!t
|| (t
->type
!= TOK_STRING
&&
2610 t
->type
!= TOK_INTERNAL_STRING
)) {
2611 nasm_error(ERR_NONFATAL
, "`%%include' expects a file name");
2612 free_tlist(origline
);
2613 return DIRECTIVE_FOUND
; /* but we did _something_ */
2616 nasm_error(ERR_WARNING
|ERR_PASS1
,
2617 "trailing garbage after `%%include' ignored");
2619 if (t
->type
!= TOK_INTERNAL_STRING
)
2620 nasm_unquote_cstr(p
, i
);
2621 inc
= nasm_malloc(sizeof(Include
));
2625 inc
->fp
= inc_fopen(p
, dephead
, &found_path
,
2626 pass
== 0 ? INC_OPTIONAL
: INC_NEEDED
, NF_TEXT
);
2628 /* -MG given but file not found */
2631 inc
->fname
= src_set_fname(found_path
? found_path
: p
);
2632 inc
->lineno
= src_set_linnum(0);
2634 inc
->expansion
= NULL
;
2637 lfmt
->uplevel(LIST_INCLUDE
);
2639 free_tlist(origline
);
2640 return DIRECTIVE_FOUND
;
2644 static macros_t
*use_pkg
;
2645 const char *pkg_macro
= NULL
;
2647 tline
= tline
->next
;
2649 tline
= expand_id(tline
);
2651 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2652 tline
->type
!= TOK_INTERNAL_STRING
&&
2653 tline
->type
!= TOK_ID
)) {
2654 nasm_error(ERR_NONFATAL
, "`%%use' expects a package name");
2655 free_tlist(origline
);
2656 return DIRECTIVE_FOUND
; /* but we did _something_ */
2659 nasm_error(ERR_WARNING
|ERR_PASS1
,
2660 "trailing garbage after `%%use' ignored");
2661 if (tline
->type
== TOK_STRING
)
2662 nasm_unquote_cstr(tline
->text
, i
);
2663 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2665 nasm_error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2667 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2668 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2669 /* Not already included, go ahead and include it */
2670 stdmacpos
= use_pkg
;
2672 free_tlist(origline
);
2673 return DIRECTIVE_FOUND
;
2678 tline
= tline
->next
;
2680 tline
= expand_id(tline
);
2682 if (!tok_type_(tline
, TOK_ID
)) {
2683 nasm_error(ERR_NONFATAL
, "`%s' expects a context identifier",
2685 free_tlist(origline
);
2686 return DIRECTIVE_FOUND
; /* but we did _something_ */
2689 nasm_error(ERR_WARNING
|ERR_PASS1
,
2690 "trailing garbage after `%s' ignored",
2692 p
= nasm_strdup(tline
->text
);
2694 p
= NULL
; /* Anonymous */
2698 ctx
= nasm_malloc(sizeof(Context
));
2700 hash_init(&ctx
->localmac
, HASH_SMALL
);
2702 ctx
->number
= unique
++;
2707 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty",
2709 } else if (i
== PP_POP
) {
2710 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2711 nasm_error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2713 cstk
->name
? cstk
->name
: "anonymous", p
);
2718 nasm_free(cstk
->name
);
2724 free_tlist(origline
);
2725 return DIRECTIVE_FOUND
;
2727 severity
= ERR_FATAL
;
2730 severity
= ERR_NONFATAL
;
2733 severity
= ERR_WARNING
|ERR_WARN_USER
;
2738 /* Only error out if this is the final pass */
2739 if (pass
!= 2 && i
!= PP_FATAL
)
2740 return DIRECTIVE_FOUND
;
2742 tline
->next
= expand_smacro(tline
->next
);
2743 tline
= tline
->next
;
2745 t
= tline
? tline
->next
: NULL
;
2747 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2748 /* The line contains only a quoted string */
2750 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2751 nasm_error(severity
, "%s", p
);
2753 /* Not a quoted string, or more than a quoted string */
2754 p
= detoken(tline
, false);
2755 nasm_error(severity
, "%s", p
);
2758 free_tlist(origline
);
2759 return DIRECTIVE_FOUND
;
2763 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2766 j
= if_condition(tline
->next
, i
);
2767 tline
->next
= NULL
; /* it got freed */
2768 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2770 cond
= nasm_malloc(sizeof(Cond
));
2771 cond
->next
= istk
->conds
;
2775 istk
->mstk
->condcnt
++;
2776 free_tlist(origline
);
2777 return DIRECTIVE_FOUND
;
2781 nasm_error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2782 switch(istk
->conds
->state
) {
2784 istk
->conds
->state
= COND_DONE
;
2791 case COND_ELSE_TRUE
:
2792 case COND_ELSE_FALSE
:
2793 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2794 "`%%elif' after `%%else' ignored");
2795 istk
->conds
->state
= COND_NEVER
;
2800 * IMPORTANT: In the case of %if, we will already have
2801 * called expand_mmac_params(); however, if we're
2802 * processing an %elif we must have been in a
2803 * non-emitting mode, which would have inhibited
2804 * the normal invocation of expand_mmac_params().
2805 * Therefore, we have to do it explicitly here.
2807 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2808 tline
->next
= NULL
; /* it got freed */
2809 istk
->conds
->state
=
2810 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2813 free_tlist(origline
);
2814 return DIRECTIVE_FOUND
;
2818 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2819 "trailing garbage after `%%else' ignored");
2821 nasm_fatal(0, "`%%else: no matching `%%if'");
2822 switch(istk
->conds
->state
) {
2825 istk
->conds
->state
= COND_ELSE_FALSE
;
2832 istk
->conds
->state
= COND_ELSE_TRUE
;
2835 case COND_ELSE_TRUE
:
2836 case COND_ELSE_FALSE
:
2837 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2838 "`%%else' after `%%else' ignored.");
2839 istk
->conds
->state
= COND_NEVER
;
2842 free_tlist(origline
);
2843 return DIRECTIVE_FOUND
;
2847 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2848 "trailing garbage after `%%endif' ignored");
2850 nasm_error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2852 istk
->conds
= cond
->next
;
2855 istk
->mstk
->condcnt
--;
2856 free_tlist(origline
);
2857 return DIRECTIVE_FOUND
;
2864 nasm_error(ERR_FATAL
, "`%s': already defining a macro",
2866 return DIRECTIVE_FOUND
;
2868 defining
= nasm_zalloc(sizeof(MMacro
));
2869 defining
->max_depth
= ((i
== PP_RMACRO
) || (i
== PP_IRMACRO
))
2870 ? nasm_limit
[LIMIT_MACROS
] : 0;
2871 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2872 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2873 nasm_free(defining
);
2875 return DIRECTIVE_FOUND
;
2878 src_get(&defining
->xline
, &defining
->fname
);
2880 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2882 if (!strcmp(mmac
->name
, defining
->name
) &&
2883 (mmac
->nparam_min
<= defining
->nparam_max
2885 && (defining
->nparam_min
<= mmac
->nparam_max
2887 nasm_error(ERR_WARNING
|ERR_PASS1
,
2888 "redefining multi-line macro `%s'", defining
->name
);
2889 return DIRECTIVE_FOUND
;
2893 free_tlist(origline
);
2894 return DIRECTIVE_FOUND
;
2898 if (! (defining
&& defining
->name
)) {
2899 nasm_error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2900 return DIRECTIVE_FOUND
;
2902 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2903 defining
->next
= *mmhead
;
2906 free_tlist(origline
);
2907 return DIRECTIVE_FOUND
;
2911 * We must search along istk->expansion until we hit a
2912 * macro-end marker for a macro with a name. Then we
2913 * bypass all lines between exitmacro and endmacro.
2915 list_for_each(l
, istk
->expansion
)
2916 if (l
->finishes
&& l
->finishes
->name
)
2921 * Remove all conditional entries relative to this
2922 * macro invocation. (safe to do in this context)
2924 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2926 istk
->conds
= cond
->next
;
2929 istk
->expansion
= l
;
2931 nasm_error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2933 free_tlist(origline
);
2934 return DIRECTIVE_FOUND
;
2942 spec
.casesense
= (i
== PP_UNMACRO
);
2943 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2944 return DIRECTIVE_FOUND
;
2946 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2947 while (mmac_p
&& *mmac_p
) {
2949 if (mmac
->casesense
== spec
.casesense
&&
2950 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2951 mmac
->nparam_min
== spec
.nparam_min
&&
2952 mmac
->nparam_max
== spec
.nparam_max
&&
2953 mmac
->plus
== spec
.plus
) {
2954 *mmac_p
= mmac
->next
;
2957 mmac_p
= &mmac
->next
;
2960 free_tlist(origline
);
2961 free_tlist(spec
.dlist
);
2962 return DIRECTIVE_FOUND
;
2966 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2967 tline
= tline
->next
;
2969 free_tlist(origline
);
2970 nasm_error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2971 return DIRECTIVE_FOUND
;
2973 t
= expand_smacro(tline
->next
);
2975 free_tlist(origline
);
2978 tokval
.t_type
= TOKEN_INVALID
;
2980 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
2983 return DIRECTIVE_FOUND
;
2985 nasm_error(ERR_WARNING
|ERR_PASS1
,
2986 "trailing garbage after expression ignored");
2987 if (!is_simple(evalresult
)) {
2988 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2989 return DIRECTIVE_FOUND
;
2992 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2993 mmac
= mmac
->next_active
;
2995 nasm_error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2996 } else if (mmac
->nparam
== 0) {
2997 nasm_error(ERR_NONFATAL
,
2998 "`%%rotate' invoked within macro without parameters");
3000 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
3002 rotate
%= (int)mmac
->nparam
;
3004 rotate
+= mmac
->nparam
;
3006 mmac
->rotate
= rotate
;
3008 return DIRECTIVE_FOUND
;
3013 tline
= tline
->next
;
3014 } while (tok_type_(tline
, TOK_WHITESPACE
));
3016 if (tok_type_(tline
, TOK_ID
) &&
3017 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3020 tline
= tline
->next
;
3021 } while (tok_type_(tline
, TOK_WHITESPACE
));
3025 t
= expand_smacro(tline
);
3027 tokval
.t_type
= TOKEN_INVALID
;
3029 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3031 free_tlist(origline
);
3032 return DIRECTIVE_FOUND
;
3035 nasm_error(ERR_WARNING
|ERR_PASS1
,
3036 "trailing garbage after expression ignored");
3037 if (!is_simple(evalresult
)) {
3038 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3039 return DIRECTIVE_FOUND
;
3041 count
= reloc_value(evalresult
);
3042 if (count
> nasm_limit
[LIMIT_REP
]) {
3043 nasm_error(ERR_NONFATAL
,
3044 "`%%rep' count %"PRId64
" exceeds limit (currently %"PRId64
")",
3045 count
, nasm_limit
[LIMIT_REP
]);
3047 } else if (count
< 0) {
3048 nasm_error(ERR_WARNING
|ERR_PASS2
|ERR_WARN_NEG_REP
,
3049 "negative `%%rep' count: %"PRId64
, count
);
3055 nasm_error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3058 free_tlist(origline
);
3060 tmp_defining
= defining
;
3061 defining
= nasm_malloc(sizeof(MMacro
));
3062 defining
->prev
= NULL
;
3063 defining
->name
= NULL
; /* flags this macro as a %rep block */
3064 defining
->casesense
= false;
3065 defining
->plus
= false;
3066 defining
->nolist
= nolist
;
3067 defining
->in_progress
= count
;
3068 defining
->max_depth
= 0;
3069 defining
->nparam_min
= defining
->nparam_max
= 0;
3070 defining
->defaults
= NULL
;
3071 defining
->dlist
= NULL
;
3072 defining
->expansion
= NULL
;
3073 defining
->next_active
= istk
->mstk
;
3074 defining
->rep_nest
= tmp_defining
;
3075 return DIRECTIVE_FOUND
;
3078 if (!defining
|| defining
->name
) {
3079 nasm_error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3080 return DIRECTIVE_FOUND
;
3084 * Now we have a "macro" defined - although it has no name
3085 * and we won't be entering it in the hash tables - we must
3086 * push a macro-end marker for it on to istk->expansion.
3087 * After that, it will take care of propagating itself (a
3088 * macro-end marker line for a macro which is really a %rep
3089 * block will cause the macro to be re-expanded, complete
3090 * with another macro-end marker to ensure the process
3091 * continues) until the whole expansion is forcibly removed
3092 * from istk->expansion by a %exitrep.
3094 l
= nasm_malloc(sizeof(Line
));
3095 l
->next
= istk
->expansion
;
3096 l
->finishes
= defining
;
3098 istk
->expansion
= l
;
3100 istk
->mstk
= defining
;
3102 lfmt
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3103 tmp_defining
= defining
;
3104 defining
= defining
->rep_nest
;
3105 free_tlist(origline
);
3106 return DIRECTIVE_FOUND
;
3110 * We must search along istk->expansion until we hit a
3111 * macro-end marker for a macro with no name. Then we set
3112 * its `in_progress' flag to 0.
3114 list_for_each(l
, istk
->expansion
)
3115 if (l
->finishes
&& !l
->finishes
->name
)
3119 l
->finishes
->in_progress
= 1;
3121 nasm_error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3122 free_tlist(origline
);
3123 return DIRECTIVE_FOUND
;
3129 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3131 tline
= tline
->next
;
3133 tline
= expand_id(tline
);
3134 if (!tline
|| (tline
->type
!= TOK_ID
&&
3135 (tline
->type
!= TOK_PREPROC_ID
||
3136 tline
->text
[1] != '$'))) {
3137 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3139 free_tlist(origline
);
3140 return DIRECTIVE_FOUND
;
3143 ctx
= get_ctx(tline
->text
, &mname
);
3145 param_start
= tline
= tline
->next
;
3148 /* Expand the macro definition now for %xdefine and %ixdefine */
3149 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3150 tline
= expand_smacro(tline
);
3152 if (tok_is_(tline
, "(")) {
3154 * This macro has parameters.
3157 tline
= tline
->next
;
3161 nasm_error(ERR_NONFATAL
, "parameter identifier expected");
3162 free_tlist(origline
);
3163 return DIRECTIVE_FOUND
;
3165 if (tline
->type
!= TOK_ID
) {
3166 nasm_error(ERR_NONFATAL
,
3167 "`%s': parameter identifier expected",
3169 free_tlist(origline
);
3170 return DIRECTIVE_FOUND
;
3172 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3173 tline
= tline
->next
;
3175 if (tok_is_(tline
, ",")) {
3176 tline
= tline
->next
;
3178 if (!tok_is_(tline
, ")")) {
3179 nasm_error(ERR_NONFATAL
,
3180 "`)' expected to terminate macro template");
3181 free_tlist(origline
);
3182 return DIRECTIVE_FOUND
;
3188 tline
= tline
->next
;
3190 if (tok_type_(tline
, TOK_WHITESPACE
))
3191 last
= tline
, tline
= tline
->next
;
3196 if (t
->type
== TOK_ID
) {
3197 list_for_each(tt
, param_start
)
3198 if (tt
->type
>= TOK_SMAC_PARAM
&&
3199 !strcmp(tt
->text
, t
->text
))
3203 t
->next
= macro_start
;
3208 * Good. We now have a macro name, a parameter count, and a
3209 * token list (in reverse order) for an expansion. We ought
3210 * to be OK just to create an SMacro, store it, and let
3211 * free_tlist have the rest of the line (which we have
3212 * carefully re-terminated after chopping off the expansion
3215 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3216 free_tlist(origline
);
3217 return DIRECTIVE_FOUND
;
3220 tline
= tline
->next
;
3222 tline
= expand_id(tline
);
3223 if (!tline
|| (tline
->type
!= TOK_ID
&&
3224 (tline
->type
!= TOK_PREPROC_ID
||
3225 tline
->text
[1] != '$'))) {
3226 nasm_error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3227 free_tlist(origline
);
3228 return DIRECTIVE_FOUND
;
3231 nasm_error(ERR_WARNING
|ERR_PASS1
,
3232 "trailing garbage after macro name ignored");
3235 /* Find the context that symbol belongs to */
3236 ctx
= get_ctx(tline
->text
, &mname
);
3237 undef_smacro(ctx
, mname
);
3238 free_tlist(origline
);
3239 return DIRECTIVE_FOUND
;
3243 casesense
= (i
== PP_DEFSTR
);
3245 tline
= tline
->next
;
3247 tline
= expand_id(tline
);
3248 if (!tline
|| (tline
->type
!= TOK_ID
&&
3249 (tline
->type
!= TOK_PREPROC_ID
||
3250 tline
->text
[1] != '$'))) {
3251 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3253 free_tlist(origline
);
3254 return DIRECTIVE_FOUND
;
3257 ctx
= get_ctx(tline
->text
, &mname
);
3259 tline
= expand_smacro(tline
->next
);
3262 while (tok_type_(tline
, TOK_WHITESPACE
))
3263 tline
= delete_Token(tline
);
3265 p
= detoken(tline
, false);
3266 macro_start
= nasm_malloc(sizeof(*macro_start
));
3267 macro_start
->next
= NULL
;
3268 macro_start
->text
= nasm_quote(p
, strlen(p
));
3269 macro_start
->type
= TOK_STRING
;
3270 macro_start
->a
.mac
= NULL
;
3274 * We now have a macro name, an implicit parameter count of
3275 * zero, and a string token to use as an expansion. Create
3276 * and store an SMacro.
3278 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3279 free_tlist(origline
);
3280 return DIRECTIVE_FOUND
;
3284 casesense
= (i
== PP_DEFTOK
);
3286 tline
= tline
->next
;
3288 tline
= expand_id(tline
);
3289 if (!tline
|| (tline
->type
!= TOK_ID
&&
3290 (tline
->type
!= TOK_PREPROC_ID
||
3291 tline
->text
[1] != '$'))) {
3292 nasm_error(ERR_NONFATAL
,
3293 "`%s' expects a macro identifier as first parameter",
3295 free_tlist(origline
);
3296 return DIRECTIVE_FOUND
;
3298 ctx
= get_ctx(tline
->text
, &mname
);
3300 tline
= expand_smacro(tline
->next
);
3304 while (tok_type_(t
, TOK_WHITESPACE
))
3306 /* t should now point to the string */
3307 if (!tok_type_(t
, TOK_STRING
)) {
3308 nasm_error(ERR_NONFATAL
,
3309 "`%s` requires string as second parameter",
3312 free_tlist(origline
);
3313 return DIRECTIVE_FOUND
;
3317 * Convert the string to a token stream. Note that smacros
3318 * are stored with the token stream reversed, so we have to
3319 * reverse the output of tokenize().
3321 nasm_unquote_cstr(t
->text
, i
);
3322 macro_start
= reverse_tokens(tokenize(t
->text
));
3325 * We now have a macro name, an implicit parameter count of
3326 * zero, and a numeric token to use as an expansion. Create
3327 * and store an SMacro.
3329 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3331 free_tlist(origline
);
3332 return DIRECTIVE_FOUND
;
3336 const char *found_path
;
3340 tline
= tline
->next
;
3342 tline
= expand_id(tline
);
3343 if (!tline
|| (tline
->type
!= TOK_ID
&&
3344 (tline
->type
!= TOK_PREPROC_ID
||
3345 tline
->text
[1] != '$'))) {
3346 nasm_error(ERR_NONFATAL
,
3347 "`%%pathsearch' expects a macro identifier as first parameter");
3348 free_tlist(origline
);
3349 return DIRECTIVE_FOUND
;
3351 ctx
= get_ctx(tline
->text
, &mname
);
3353 tline
= expand_smacro(tline
->next
);
3357 while (tok_type_(t
, TOK_WHITESPACE
))
3360 if (!t
|| (t
->type
!= TOK_STRING
&&
3361 t
->type
!= TOK_INTERNAL_STRING
)) {
3362 nasm_error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3364 free_tlist(origline
);
3365 return DIRECTIVE_FOUND
; /* but we did _something_ */
3368 nasm_error(ERR_WARNING
|ERR_PASS1
,
3369 "trailing garbage after `%%pathsearch' ignored");
3371 if (t
->type
!= TOK_INTERNAL_STRING
)
3372 nasm_unquote(p
, NULL
);
3374 inc_fopen(p
, NULL
, &found_path
, INC_PROBE
, NF_BINARY
);
3377 macro_start
= nasm_malloc(sizeof(*macro_start
));
3378 macro_start
->next
= NULL
;
3379 macro_start
->text
= nasm_quote(found_path
, strlen(found_path
));
3380 macro_start
->type
= TOK_STRING
;
3381 macro_start
->a
.mac
= NULL
;
3384 * We now have a macro name, an implicit parameter count of
3385 * zero, and a string token to use as an expansion. Create
3386 * and store an SMacro.
3388 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3390 free_tlist(origline
);
3391 return DIRECTIVE_FOUND
;
3397 tline
= tline
->next
;
3399 tline
= expand_id(tline
);
3400 if (!tline
|| (tline
->type
!= TOK_ID
&&
3401 (tline
->type
!= TOK_PREPROC_ID
||
3402 tline
->text
[1] != '$'))) {
3403 nasm_error(ERR_NONFATAL
,
3404 "`%%strlen' expects a macro identifier as first parameter");
3405 free_tlist(origline
);
3406 return DIRECTIVE_FOUND
;
3408 ctx
= get_ctx(tline
->text
, &mname
);
3410 tline
= expand_smacro(tline
->next
);
3414 while (tok_type_(t
, TOK_WHITESPACE
))
3416 /* t should now point to the string */
3417 if (!tok_type_(t
, TOK_STRING
)) {
3418 nasm_error(ERR_NONFATAL
,
3419 "`%%strlen` requires string as second parameter");
3421 free_tlist(origline
);
3422 return DIRECTIVE_FOUND
;
3425 macro_start
= nasm_malloc(sizeof(*macro_start
));
3426 macro_start
->next
= NULL
;
3427 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3428 macro_start
->a
.mac
= NULL
;
3431 * We now have a macro name, an implicit parameter count of
3432 * zero, and a numeric token to use as an expansion. Create
3433 * and store an SMacro.
3435 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3437 free_tlist(origline
);
3438 return DIRECTIVE_FOUND
;
3443 tline
= tline
->next
;
3445 tline
= expand_id(tline
);
3446 if (!tline
|| (tline
->type
!= TOK_ID
&&
3447 (tline
->type
!= TOK_PREPROC_ID
||
3448 tline
->text
[1] != '$'))) {
3449 nasm_error(ERR_NONFATAL
,
3450 "`%%strcat' expects a macro identifier as first parameter");
3451 free_tlist(origline
);
3452 return DIRECTIVE_FOUND
;
3454 ctx
= get_ctx(tline
->text
, &mname
);
3456 tline
= expand_smacro(tline
->next
);
3460 list_for_each(t
, tline
) {
3462 case TOK_WHITESPACE
:
3465 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3468 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3470 /* else fall through */
3472 nasm_error(ERR_NONFATAL
,
3473 "non-string passed to `%%strcat' (%d)", t
->type
);
3475 free_tlist(origline
);
3476 return DIRECTIVE_FOUND
;
3480 p
= pp
= nasm_malloc(len
);
3481 list_for_each(t
, tline
) {
3482 if (t
->type
== TOK_STRING
) {
3483 memcpy(p
, t
->text
, t
->a
.len
);
3489 * We now have a macro name, an implicit parameter count of
3490 * zero, and a numeric token to use as an expansion. Create
3491 * and store an SMacro.
3493 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3494 macro_start
->text
= nasm_quote(pp
, len
);
3496 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3498 free_tlist(origline
);
3499 return DIRECTIVE_FOUND
;
3503 int64_t start
, count
;
3508 tline
= tline
->next
;
3510 tline
= expand_id(tline
);
3511 if (!tline
|| (tline
->type
!= TOK_ID
&&
3512 (tline
->type
!= TOK_PREPROC_ID
||
3513 tline
->text
[1] != '$'))) {
3514 nasm_error(ERR_NONFATAL
,
3515 "`%%substr' expects a macro identifier as first parameter");
3516 free_tlist(origline
);
3517 return DIRECTIVE_FOUND
;
3519 ctx
= get_ctx(tline
->text
, &mname
);
3521 tline
= expand_smacro(tline
->next
);
3524 if (tline
) /* skip expanded id */
3526 while (tok_type_(t
, TOK_WHITESPACE
))
3529 /* t should now point to the string */
3530 if (!tok_type_(t
, TOK_STRING
)) {
3531 nasm_error(ERR_NONFATAL
,
3532 "`%%substr` requires string as second parameter");
3534 free_tlist(origline
);
3535 return DIRECTIVE_FOUND
;
3540 tokval
.t_type
= TOKEN_INVALID
;
3541 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3544 free_tlist(origline
);
3545 return DIRECTIVE_FOUND
;
3546 } else if (!is_simple(evalresult
)) {
3547 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3549 free_tlist(origline
);
3550 return DIRECTIVE_FOUND
;
3552 start
= evalresult
->value
- 1;
3554 while (tok_type_(tt
, TOK_WHITESPACE
))
3557 count
= 1; /* Backwards compatibility: one character */
3559 tokval
.t_type
= TOKEN_INVALID
;
3560 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3563 free_tlist(origline
);
3564 return DIRECTIVE_FOUND
;
3565 } else if (!is_simple(evalresult
)) {
3566 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3568 free_tlist(origline
);
3569 return DIRECTIVE_FOUND
;
3571 count
= evalresult
->value
;
3574 len
= nasm_unquote(t
->text
, NULL
);
3576 /* make start and count being in range */
3580 count
= len
+ count
+ 1 - start
;
3581 if (start
+ count
> (int64_t)len
)
3582 count
= len
- start
;
3583 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3584 start
= -1, count
= 0; /* empty string */
3586 macro_start
= nasm_malloc(sizeof(*macro_start
));
3587 macro_start
->next
= NULL
;
3588 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3589 macro_start
->type
= TOK_STRING
;
3590 macro_start
->a
.mac
= NULL
;
3593 * We now have a macro name, an implicit parameter count of
3594 * zero, and a numeric token to use as an expansion. Create
3595 * and store an SMacro.
3597 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3599 free_tlist(origline
);
3600 return DIRECTIVE_FOUND
;
3605 casesense
= (i
== PP_ASSIGN
);
3607 tline
= tline
->next
;
3609 tline
= expand_id(tline
);
3610 if (!tline
|| (tline
->type
!= TOK_ID
&&
3611 (tline
->type
!= TOK_PREPROC_ID
||
3612 tline
->text
[1] != '$'))) {
3613 nasm_error(ERR_NONFATAL
,
3614 "`%%%sassign' expects a macro identifier",
3615 (i
== PP_IASSIGN
? "i" : ""));
3616 free_tlist(origline
);
3617 return DIRECTIVE_FOUND
;
3619 ctx
= get_ctx(tline
->text
, &mname
);
3621 tline
= expand_smacro(tline
->next
);
3626 tokval
.t_type
= TOKEN_INVALID
;
3627 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3630 free_tlist(origline
);
3631 return DIRECTIVE_FOUND
;
3635 nasm_error(ERR_WARNING
|ERR_PASS1
,
3636 "trailing garbage after expression ignored");
3638 if (!is_simple(evalresult
)) {
3639 nasm_error(ERR_NONFATAL
,
3640 "non-constant value given to `%%%sassign'",
3641 (i
== PP_IASSIGN
? "i" : ""));
3642 free_tlist(origline
);
3643 return DIRECTIVE_FOUND
;
3646 macro_start
= nasm_malloc(sizeof(*macro_start
));
3647 macro_start
->next
= NULL
;
3648 make_tok_num(macro_start
, reloc_value(evalresult
));
3649 macro_start
->a
.mac
= NULL
;
3652 * We now have a macro name, an implicit parameter count of
3653 * zero, and a numeric token to use as an expansion. Create
3654 * and store an SMacro.
3656 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3657 free_tlist(origline
);
3658 return DIRECTIVE_FOUND
;
3662 * Syntax is `%line nnn[+mmm] [filename]'
3664 tline
= tline
->next
;
3666 if (!tok_type_(tline
, TOK_NUMBER
)) {
3667 nasm_error(ERR_NONFATAL
, "`%%line' expects line number");
3668 free_tlist(origline
);
3669 return DIRECTIVE_FOUND
;
3671 k
= readnum(tline
->text
, &err
);
3673 tline
= tline
->next
;
3674 if (tok_is_(tline
, "+")) {
3675 tline
= tline
->next
;
3676 if (!tok_type_(tline
, TOK_NUMBER
)) {
3677 nasm_error(ERR_NONFATAL
, "`%%line' expects line increment");
3678 free_tlist(origline
);
3679 return DIRECTIVE_FOUND
;
3681 m
= readnum(tline
->text
, &err
);
3682 tline
= tline
->next
;
3688 char *fname
= detoken(tline
, false);
3689 src_set_fname(fname
);
3692 free_tlist(origline
);
3693 return DIRECTIVE_FOUND
;
3696 nasm_error(ERR_FATAL
,
3697 "preprocessor directive `%s' not yet implemented",
3699 return DIRECTIVE_FOUND
;
3704 * Ensure that a macro parameter contains a condition code and
3705 * nothing else. Return the condition code index if so, or -1
3708 static int find_cc(Token
* t
)
3713 return -1; /* Probably a %+ without a space */
3718 if (t
->type
!= TOK_ID
)
3722 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3725 return bsii(t
->text
, (const char **)conditions
, ARRAY_SIZE(conditions
));
3729 * This routines walks over tokens strem and hadnles tokens
3730 * pasting, if @handle_explicit passed then explicit pasting
3731 * term is handled, otherwise -- implicit pastings only.
3733 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3734 size_t mnum
, bool handle_explicit
)
3736 Token
*tok
, *next
, **prev_next
, **prev_nonspace
;
3737 bool pasted
= false;
3742 * The last token before pasting. We need it
3743 * to be able to connect new handled tokens.
3744 * In other words if there were a tokens stream
3748 * and we've joined tokens B and C, the resulting
3756 if (!tok_type_(tok
, TOK_WHITESPACE
) && !tok_type_(tok
, TOK_PASTE
))
3757 prev_nonspace
= head
;
3759 prev_nonspace
= NULL
;
3761 while (tok
&& (next
= tok
->next
)) {
3763 switch (tok
->type
) {
3764 case TOK_WHITESPACE
:
3765 /* Zap redundant whitespaces */
3766 while (tok_type_(next
, TOK_WHITESPACE
))
3767 next
= delete_Token(next
);
3772 /* Explicit pasting */
3773 if (!handle_explicit
)
3775 next
= delete_Token(tok
);
3777 while (tok_type_(next
, TOK_WHITESPACE
))
3778 next
= delete_Token(next
);
3783 /* Left pasting token is start of line */
3785 nasm_error(ERR_FATAL
, "No lvalue found on pasting");
3788 * No ending token, this might happen in two
3791 * 1) There indeed no right token at all
3792 * 2) There is a bare "%define ID" statement,
3793 * and @ID does expand to whitespace.
3795 * So technically we need to do a grammar analysis
3796 * in another stage of parsing, but for now lets don't
3797 * change the behaviour people used to. Simply allow
3798 * whitespace after paste token.
3802 * Zap ending space tokens and that's all.
3804 tok
= (*prev_nonspace
)->next
;
3805 while (tok_type_(tok
, TOK_WHITESPACE
))
3806 tok
= delete_Token(tok
);
3807 tok
= *prev_nonspace
;
3812 tok
= *prev_nonspace
;
3813 while (tok_type_(tok
, TOK_WHITESPACE
))
3814 tok
= delete_Token(tok
);
3815 len
= strlen(tok
->text
);
3816 len
+= strlen(next
->text
);
3818 p
= buf
= nasm_malloc(len
+ 1);
3819 strcpy(p
, tok
->text
);
3820 p
= strchr(p
, '\0');
3821 strcpy(p
, next
->text
);
3825 tok
= tokenize(buf
);
3828 *prev_nonspace
= tok
;
3829 while (tok
&& tok
->next
)
3832 tok
->next
= delete_Token(next
);
3834 /* Restart from pasted tokens head */
3835 tok
= *prev_nonspace
;
3839 /* implicit pasting */
3840 for (i
= 0; i
< mnum
; i
++) {
3841 if (!(PP_CONCAT_MATCH(tok
, m
[i
].mask_head
)))
3845 while (next
&& PP_CONCAT_MATCH(next
, m
[i
].mask_tail
)) {
3846 len
+= strlen(next
->text
);
3850 /* No match or no text to process */
3851 if (tok
== next
|| len
== 0)
3854 len
+= strlen(tok
->text
);
3855 p
= buf
= nasm_malloc(len
+ 1);
3857 strcpy(p
, tok
->text
);
3858 p
= strchr(p
, '\0');
3859 tok
= delete_Token(tok
);
3861 while (tok
!= next
) {
3862 if (PP_CONCAT_MATCH(tok
, m
[i
].mask_tail
)) {
3863 strcpy(p
, tok
->text
);
3864 p
= strchr(p
, '\0');
3866 tok
= delete_Token(tok
);
3869 tok
= tokenize(buf
);
3878 * Connect pasted into original stream,
3879 * ie A -> new-tokens -> B
3881 while (tok
&& tok
->next
)
3888 /* Restart from pasted tokens head */
3889 tok
= prev_next
? *prev_next
: *head
;
3895 prev_next
= &tok
->next
;
3898 !tok_type_(tok
->next
, TOK_WHITESPACE
) &&
3899 !tok_type_(tok
->next
, TOK_PASTE
))
3900 prev_nonspace
= prev_next
;
3909 * expands to a list of tokens from %{x:y}
3911 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3913 Token
*t
= tline
, **tt
, *tm
, *head
;
3917 pos
= strchr(tline
->text
, ':');
3920 lst
= atoi(pos
+ 1);
3921 fst
= atoi(tline
->text
+ 1);
3924 * only macros params are accounted so
3925 * if someone passes %0 -- we reject such
3928 if (lst
== 0 || fst
== 0)
3931 /* the values should be sane */
3932 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3933 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3936 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3937 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3939 /* counted from zero */
3943 * It will be at least one token. Note we
3944 * need to scan params until separator, otherwise
3945 * only first token will be passed.
3947 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3950 head
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3951 tt
= &head
->next
, tm
= tm
->next
;
3952 while (tok_isnt_(tm
, ",")) {
3953 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3954 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3958 for (i
= fst
+ 1; i
<= lst
; i
++) {
3959 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3960 *tt
= t
, tt
= &t
->next
;
3961 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3962 tm
= mac
->params
[j
];
3963 while (tok_isnt_(tm
, ",")) {
3964 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3965 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3969 for (i
= fst
- 1; i
>= lst
; i
--) {
3970 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3971 *tt
= t
, tt
= &t
->next
;
3972 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3973 tm
= mac
->params
[j
];
3974 while (tok_isnt_(tm
, ",")) {
3975 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3976 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3985 nasm_error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3991 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3992 * %-n) and MMacro-local identifiers (%%foo) as well as
3993 * macro indirection (%[...]) and range (%{..:..}).
3995 static Token
*expand_mmac_params(Token
* tline
)
3997 Token
*t
, *tt
, **tail
, *thead
;
3998 bool changed
= false;
4005 if (tline
->type
== TOK_PREPROC_ID
&& tline
->text
&& tline
->text
[0] &&
4006 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4007 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4008 tline
->text
[1] == '%')) {
4010 int type
= 0, cc
; /* type = 0 to placate optimisers */
4017 tline
= tline
->next
;
4020 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
4021 mac
= mac
->next_active
;
4023 nasm_error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4025 pos
= strchr(t
->text
, ':');
4027 switch (t
->text
[1]) {
4029 * We have to make a substitution of one of the
4030 * forms %1, %-1, %+1, %%foo, %0.
4034 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
4035 text
= nasm_strdup(tmpbuf
);
4039 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4041 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4044 n
= atoi(t
->text
+ 2) - 1;
4045 if (n
>= mac
->nparam
)
4048 if (mac
->nparam
> 1)
4049 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4050 tt
= mac
->params
[n
];
4054 nasm_error(ERR_NONFATAL
,
4055 "macro parameter %d is not a condition code",
4060 if (inverse_ccs
[cc
] == -1) {
4061 nasm_error(ERR_NONFATAL
,
4062 "condition code `%s' is not invertible",
4066 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4070 n
= atoi(t
->text
+ 2) - 1;
4071 if (n
>= mac
->nparam
)
4074 if (mac
->nparam
> 1)
4075 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4076 tt
= mac
->params
[n
];
4080 nasm_error(ERR_NONFATAL
,
4081 "macro parameter %d is not a condition code",
4086 text
= nasm_strdup(conditions
[cc
]);
4090 n
= atoi(t
->text
+ 1) - 1;
4091 if (n
>= mac
->nparam
)
4094 if (mac
->nparam
> 1)
4095 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4096 tt
= mac
->params
[n
];
4099 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
4100 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4101 tail
= &(*tail
)->next
;
4105 text
= NULL
; /* we've done it here */
4110 * seems we have a parameters range here
4112 Token
*head
, **last
;
4113 head
= expand_mmac_params_range(mac
, t
, &last
);
4134 } else if (tline
->type
== TOK_INDIRECT
) {
4136 tline
= tline
->next
;
4137 tt
= tokenize(t
->text
);
4138 tt
= expand_mmac_params(tt
);
4139 tt
= expand_smacro(tt
);
4142 tt
->a
.mac
= NULL
; /* Necessary? */
4150 tline
= tline
->next
;
4158 const struct tokseq_match t
[] = {
4160 PP_CONCAT_MASK(TOK_ID
) |
4161 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4162 PP_CONCAT_MASK(TOK_ID
) |
4163 PP_CONCAT_MASK(TOK_NUMBER
) |
4164 PP_CONCAT_MASK(TOK_FLOAT
) |
4165 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4168 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4169 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4172 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4179 * Expand all single-line macro calls made in the given line.
4180 * Return the expanded version of the line. The original is deemed
4181 * to be destroyed in the process. (In reality we'll just move
4182 * Tokens from input to output a lot of the time, rather than
4183 * actually bothering to destroy and replicate.)
4186 static Token
*expand_smacro(Token
* tline
)
4188 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4189 SMacro
*head
= NULL
, *m
;
4192 unsigned int nparam
, sparam
;
4194 Token
*org_tline
= tline
;
4197 int64_t deadman
= nasm_limit
[LIMIT_MACROS
];
4201 * Trick: we should avoid changing the start token pointer since it can
4202 * be contained in "next" field of other token. Because of this
4203 * we allocate a copy of first token and work with it; at the end of
4204 * routine we copy it back
4207 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4208 org_tline
->text
, 0);
4209 tline
->a
.mac
= org_tline
->a
.mac
;
4210 nasm_free(org_tline
->text
);
4211 org_tline
->text
= NULL
;
4214 expanded
= true; /* Always expand %+ at least once */
4220 while (tline
) { /* main token loop */
4222 nasm_error(ERR_NONFATAL
, "interminable macro recursion");
4226 if ((mname
= tline
->text
)) {
4227 /* if this token is a local macro, look in local context */
4228 if (tline
->type
== TOK_ID
) {
4229 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4230 } else if (tline
->type
== TOK_PREPROC_ID
) {
4231 ctx
= get_ctx(mname
, &mname
);
4232 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4237 * We've hit an identifier. As in is_mmacro below, we first
4238 * check whether the identifier is a single-line macro at
4239 * all, then think about checking for parameters if
4242 list_for_each(m
, head
)
4243 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4249 if (m
->nparam
== 0) {
4251 * Simple case: the macro is parameterless. Discard the
4252 * one token that the macro call took, and push the
4253 * expansion back on the to-do stack.
4255 if (!m
->expansion
) {
4256 if (!strcmp("__FILE__", m
->name
)) {
4257 const char *file
= src_get_fname();
4258 /* nasm_free(tline->text); here? */
4259 tline
->text
= nasm_quote(file
, strlen(file
));
4260 tline
->type
= TOK_STRING
;
4263 if (!strcmp("__LINE__", m
->name
)) {
4264 nasm_free(tline
->text
);
4265 make_tok_num(tline
, src_get_linnum());
4268 if (!strcmp("__BITS__", m
->name
)) {
4269 nasm_free(tline
->text
);
4270 make_tok_num(tline
, globalbits
);
4273 tline
= delete_Token(tline
);
4278 * Complicated case: at least one macro with this name
4279 * exists and takes parameters. We must find the
4280 * parameters in the call, count them, find the SMacro
4281 * that corresponds to that form of the macro call, and
4282 * substitute for the parameters when we expand. What a
4285 /*tline = tline->next;
4286 skip_white_(tline); */
4289 while (tok_type_(t
, TOK_SMAC_END
)) {
4290 t
->a
.mac
->in_progress
= false;
4292 t
= tline
->next
= delete_Token(t
);
4295 } while (tok_type_(tline
, TOK_WHITESPACE
));
4296 if (!tok_is_(tline
, "(")) {
4298 * This macro wasn't called with parameters: ignore
4299 * the call. (Behaviour borrowed from gnu cpp.)
4308 sparam
= PARAM_DELTA
;
4309 params
= nasm_malloc(sparam
* sizeof(Token
*));
4310 params
[0] = tline
->next
;
4311 paramsize
= nasm_malloc(sparam
* sizeof(int));
4313 while (true) { /* parameter loop */
4315 * For some unusual expansions
4316 * which concatenates function call
4319 while (tok_type_(t
, TOK_SMAC_END
)) {
4320 t
->a
.mac
->in_progress
= false;
4322 t
= tline
->next
= delete_Token(t
);
4327 nasm_error(ERR_NONFATAL
,
4328 "macro call expects terminating `)'");
4331 if (tline
->type
== TOK_WHITESPACE
4333 if (paramsize
[nparam
])
4336 params
[nparam
] = tline
->next
;
4337 continue; /* parameter loop */
4339 if (tline
->type
== TOK_OTHER
4340 && tline
->text
[1] == 0) {
4341 char ch
= tline
->text
[0];
4342 if (ch
== ',' && !paren
&& brackets
<= 0) {
4343 if (++nparam
>= sparam
) {
4344 sparam
+= PARAM_DELTA
;
4345 params
= nasm_realloc(params
,
4346 sparam
* sizeof(Token
*));
4347 paramsize
= nasm_realloc(paramsize
,
4348 sparam
* sizeof(int));
4350 params
[nparam
] = tline
->next
;
4351 paramsize
[nparam
] = 0;
4353 continue; /* parameter loop */
4356 (brackets
> 0 || (brackets
== 0 &&
4357 !paramsize
[nparam
])))
4359 if (!(brackets
++)) {
4360 params
[nparam
] = tline
->next
;
4361 continue; /* parameter loop */
4364 if (ch
== '}' && brackets
> 0)
4365 if (--brackets
== 0) {
4367 continue; /* parameter loop */
4369 if (ch
== '(' && !brackets
)
4371 if (ch
== ')' && brackets
<= 0)
4377 nasm_error(ERR_NONFATAL
, "braces do not "
4378 "enclose all of macro parameter");
4380 paramsize
[nparam
] += white
+ 1;
4382 } /* parameter loop */
4384 while (m
&& (m
->nparam
!= nparam
||
4385 mstrcmp(m
->name
, mname
,
4389 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4390 "macro `%s' exists, "
4391 "but not taking %d parameters",
4392 mstart
->text
, nparam
);
4395 if (m
&& m
->in_progress
)
4397 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4399 * Design question: should we handle !tline, which
4400 * indicates missing ')' here, or expand those
4401 * macros anyway, which requires the (t) test a few
4405 nasm_free(paramsize
);
4409 * Expand the macro: we are placed on the last token of the
4410 * call, so that we can easily split the call from the
4411 * following tokens. We also start by pushing an SMAC_END
4412 * token for the cycle removal.
4419 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4421 m
->in_progress
= true;
4423 list_for_each(t
, m
->expansion
) {
4424 if (t
->type
>= TOK_SMAC_PARAM
) {
4425 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4429 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4430 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4432 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4436 if (!ttt
&& i
> 0) {
4438 * FIXME: Need to handle more gracefully,
4439 * exiting early on agruments analysis.
4441 nasm_error(ERR_FATAL
,
4442 "macro `%s' expects %d args",
4444 (int)paramsize
[t
->type
- TOK_SMAC_PARAM
]);
4448 } else if (t
->type
== TOK_PREPROC_Q
) {
4449 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4451 } else if (t
->type
== TOK_PREPROC_QQ
) {
4452 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4455 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4461 * Having done that, get rid of the macro call, and clean
4462 * up the parameters.
4465 nasm_free(paramsize
);
4468 continue; /* main token loop */
4473 if (tline
->type
== TOK_SMAC_END
) {
4474 /* On error path it might already be dropped */
4476 tline
->a
.mac
->in_progress
= false;
4477 tline
= delete_Token(tline
);
4480 tline
= tline
->next
;
4488 * Now scan the entire line and look for successive TOK_IDs that resulted
4489 * after expansion (they can't be produced by tokenize()). The successive
4490 * TOK_IDs should be concatenated.
4491 * Also we look for %+ tokens and concatenate the tokens before and after
4492 * them (without white spaces in between).
4495 const struct tokseq_match t
[] = {
4497 PP_CONCAT_MASK(TOK_ID
) |
4498 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4499 PP_CONCAT_MASK(TOK_ID
) |
4500 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4501 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4504 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4506 * If we concatenated something, *and* we had previously expanded
4507 * an actual macro, scan the lines again for macros...
4518 *org_tline
= *thead
;
4519 /* since we just gave text to org_line, don't free it */
4521 delete_Token(thead
);
4523 /* the expression expanded to empty line;
4524 we can't return NULL for some reasons
4525 we just set the line to a single WHITESPACE token. */
4526 memset(org_tline
, 0, sizeof(*org_tline
));
4527 org_tline
->text
= NULL
;
4528 org_tline
->type
= TOK_WHITESPACE
;
4537 * Similar to expand_smacro but used exclusively with macro identifiers
4538 * right before they are fetched in. The reason is that there can be
4539 * identifiers consisting of several subparts. We consider that if there
4540 * are more than one element forming the name, user wants a expansion,
4541 * otherwise it will be left as-is. Example:
4545 * the identifier %$abc will be left as-is so that the handler for %define
4546 * will suck it and define the corresponding value. Other case:
4548 * %define _%$abc cde
4550 * In this case user wants name to be expanded *before* %define starts
4551 * working, so we'll expand %$abc into something (if it has a value;
4552 * otherwise it will be left as-is) then concatenate all successive
4555 static Token
*expand_id(Token
* tline
)
4557 Token
*cur
, *oldnext
= NULL
;
4559 if (!tline
|| !tline
->next
)
4564 (cur
->next
->type
== TOK_ID
||
4565 cur
->next
->type
== TOK_PREPROC_ID
4566 || cur
->next
->type
== TOK_NUMBER
))
4569 /* If identifier consists of just one token, don't expand */
4574 oldnext
= cur
->next
; /* Detach the tail past identifier */
4575 cur
->next
= NULL
; /* so that expand_smacro stops here */
4578 tline
= expand_smacro(tline
);
4581 /* expand_smacro possibly changhed tline; re-scan for EOL */
4583 while (cur
&& cur
->next
)
4586 cur
->next
= oldnext
;
4593 * Determine whether the given line constitutes a multi-line macro
4594 * call, and return the MMacro structure called if so. Doesn't have
4595 * to check for an initial label - that's taken care of in
4596 * expand_mmacro - but must check numbers of parameters. Guaranteed
4597 * to be called with tline->type == TOK_ID, so the putative macro
4598 * name is easy to find.
4600 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4606 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4609 * Efficiency: first we see if any macro exists with the given
4610 * name. If not, we can return NULL immediately. _Then_ we
4611 * count the parameters, and then we look further along the
4612 * list if necessary to find the proper MMacro.
4614 list_for_each(m
, head
)
4615 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4621 * OK, we have a potential macro. Count and demarcate the
4624 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4627 * So we know how many parameters we've got. Find the MMacro
4628 * structure that handles this number.
4631 if (m
->nparam_min
<= nparam
4632 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4634 * This one is right. Just check if cycle removal
4635 * prohibits us using it before we actually celebrate...
4637 if (m
->in_progress
> m
->max_depth
) {
4638 if (m
->max_depth
> 0) {
4639 nasm_error(ERR_WARNING
,
4640 "reached maximum recursion depth of %i",
4647 * It's right, and we can use it. Add its default
4648 * parameters to the end of our list if necessary.
4650 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4652 nasm_realloc(params
,
4653 ((m
->nparam_min
+ m
->ndefs
+
4654 1) * sizeof(*params
)));
4655 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4656 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4661 * If we've gone over the maximum parameter count (and
4662 * we're in Plus mode), ignore parameters beyond
4665 if (m
->plus
&& nparam
> m
->nparam_max
)
4666 nparam
= m
->nparam_max
;
4668 * Then terminate the parameter list, and leave.
4670 if (!params
) { /* need this special case */
4671 params
= nasm_malloc(sizeof(*params
));
4674 params
[nparam
] = NULL
;
4675 *params_array
= params
;
4679 * This one wasn't right: look for the next one with the
4682 list_for_each(m
, m
->next
)
4683 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4688 * After all that, we didn't find one with the right number of
4689 * parameters. Issue a warning, and fail to expand the macro.
4691 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4692 "macro `%s' exists, but not taking %d parameters",
4693 tline
->text
, nparam
);
4700 * Save MMacro invocation specific fields in
4701 * preparation for a recursive macro expansion
4703 static void push_mmacro(MMacro
*m
)
4705 MMacroInvocation
*i
;
4707 i
= nasm_malloc(sizeof(MMacroInvocation
));
4709 i
->params
= m
->params
;
4710 i
->iline
= m
->iline
;
4711 i
->nparam
= m
->nparam
;
4712 i
->rotate
= m
->rotate
;
4713 i
->paramlen
= m
->paramlen
;
4714 i
->unique
= m
->unique
;
4715 i
->condcnt
= m
->condcnt
;
4721 * Restore MMacro invocation specific fields that were
4722 * saved during a previous recursive macro expansion
4724 static void pop_mmacro(MMacro
*m
)
4726 MMacroInvocation
*i
;
4731 m
->params
= i
->params
;
4732 m
->iline
= i
->iline
;
4733 m
->nparam
= i
->nparam
;
4734 m
->rotate
= i
->rotate
;
4735 m
->paramlen
= i
->paramlen
;
4736 m
->unique
= i
->unique
;
4737 m
->condcnt
= i
->condcnt
;
4744 * Expand the multi-line macro call made by the given line, if
4745 * there is one to be expanded. If there is, push the expansion on
4746 * istk->expansion and return 1. Otherwise return 0.
4748 static int expand_mmacro(Token
* tline
)
4750 Token
*startline
= tline
;
4751 Token
*label
= NULL
;
4752 int dont_prepend
= 0;
4753 Token
**params
, *t
, *tt
;
4756 int i
, nparam
, *paramlen
;
4761 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4762 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4764 m
= is_mmacro(t
, ¶ms
);
4770 * We have an id which isn't a macro call. We'll assume
4771 * it might be a label; we'll also check to see if a
4772 * colon follows it. Then, if there's another id after
4773 * that lot, we'll check it again for macro-hood.
4777 if (tok_type_(t
, TOK_WHITESPACE
))
4778 last
= t
, t
= t
->next
;
4779 if (tok_is_(t
, ":")) {
4781 last
= t
, t
= t
->next
;
4782 if (tok_type_(t
, TOK_WHITESPACE
))
4783 last
= t
, t
= t
->next
;
4785 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4793 * Fix up the parameters: this involves stripping leading and
4794 * trailing whitespace, then stripping braces if they are
4797 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4798 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4800 for (i
= 0; params
[i
]; i
++) {
4802 int comma
= (!m
->plus
|| i
< nparam
- 1);
4806 if (tok_is_(t
, "{"))
4807 t
= t
->next
, brace
++, comma
= false;
4811 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4812 break; /* ... because we have hit a comma */
4813 if (comma
&& t
->type
== TOK_WHITESPACE
4814 && tok_is_(t
->next
, ","))
4815 break; /* ... or a space then a comma */
4816 if (brace
&& t
->type
== TOK_OTHER
) {
4817 if (t
->text
[0] == '{')
4818 brace
++; /* ... or a nested opening brace */
4819 else if (t
->text
[0] == '}')
4821 break; /* ... or a brace */
4827 nasm_error(ERR_NONFATAL
, "macro params should be enclosed in braces");
4831 * OK, we have a MMacro structure together with a set of
4832 * parameters. We must now go through the expansion and push
4833 * copies of each Line on to istk->expansion. Substitution of
4834 * parameter tokens and macro-local tokens doesn't get done
4835 * until the single-line macro substitution process; this is
4836 * because delaying them allows us to change the semantics
4837 * later through %rotate.
4839 * First, push an end marker on to istk->expansion, mark this
4840 * macro as in progress, and set up its invocation-specific
4843 ll
= nasm_malloc(sizeof(Line
));
4844 ll
->next
= istk
->expansion
;
4847 istk
->expansion
= ll
;
4850 * Save the previous MMacro expansion in the case of
4853 if (m
->max_depth
&& m
->in_progress
)
4861 m
->paramlen
= paramlen
;
4862 m
->unique
= unique
++;
4866 m
->next_active
= istk
->mstk
;
4869 list_for_each(l
, m
->expansion
) {
4872 ll
= nasm_malloc(sizeof(Line
));
4873 ll
->finishes
= NULL
;
4874 ll
->next
= istk
->expansion
;
4875 istk
->expansion
= ll
;
4878 list_for_each(t
, l
->first
) {
4882 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4884 case TOK_PREPROC_QQ
:
4885 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4887 case TOK_PREPROC_ID
:
4888 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4896 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4905 * If we had a label, push it on as the first line of
4906 * the macro expansion.
4909 if (dont_prepend
< 0)
4910 free_tlist(startline
);
4912 ll
= nasm_malloc(sizeof(Line
));
4913 ll
->finishes
= NULL
;
4914 ll
->next
= istk
->expansion
;
4915 istk
->expansion
= ll
;
4916 ll
->first
= startline
;
4917 if (!dont_prepend
) {
4919 label
= label
->next
;
4920 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4925 lfmt
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4931 * This function adds macro names to error messages, and suppresses
4932 * them if necessary.
4934 static void pp_verror(int severity
, const char *fmt
, va_list arg
)
4937 MMacro
*mmac
= NULL
;
4941 * If we're in a dead branch of IF or something like it, ignore the error.
4942 * However, because %else etc are evaluated in the state context
4943 * of the previous branch, errors might get lost:
4944 * %if 0 ... %else trailing garbage ... %endif
4945 * So %else etc should set the ERR_PP_PRECOND flag.
4947 if ((severity
& ERR_MASK
) < ERR_FATAL
&&
4948 istk
&& istk
->conds
&&
4949 ((severity
& ERR_PP_PRECOND
) ?
4950 istk
->conds
->state
== COND_NEVER
:
4951 !emitting(istk
->conds
->state
)))
4954 /* get %macro name */
4955 if (!(severity
& ERR_NOFILE
) && istk
&& istk
->mstk
) {
4957 /* but %rep blocks should be skipped */
4958 while (mmac
&& !mmac
->name
)
4959 mmac
= mmac
->next_active
, delta
++;
4963 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4965 nasm_set_verror(real_verror
);
4966 nasm_error(severity
, "(%s:%d) %s",
4967 mmac
->name
, mmac
->lineno
- delta
, buff
);
4968 nasm_set_verror(pp_verror
);
4970 real_verror(severity
, fmt
, arg
);
4975 pp_reset(const char *file
, int apass
, StrList
**deplist
)
4980 istk
= nasm_malloc(sizeof(Include
));
4983 istk
->expansion
= NULL
;
4985 istk
->fp
= nasm_open_read(file
, NF_TEXT
);
4990 nasm_fatal(ERR_NOFILE
, "unable to open input file `%s'", file
);
4992 nested_mac_count
= 0;
4993 nested_rep_count
= 0;
4997 if (tasm_compatible_mode
)
4998 pp_add_stdmac(nasm_stdmac_tasm
);
5000 pp_add_stdmac(nasm_stdmac_nasm
);
5001 pp_add_stdmac(nasm_stdmac_version
);
5004 pp_add_stdmac(extrastdmac
);
5006 stdmacpos
= stdmacros
[0];
5007 stdmacnext
= &stdmacros
[1];
5012 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5013 * The caller, however, will also pass in 3 for preprocess-only so
5014 * we can set __PASS__ accordingly.
5016 pass
= apass
> 2 ? 2 : apass
;
5019 nasm_add_string_to_strlist(dephead
, file
);
5022 * Define the __PASS__ macro. This is defined here unlike
5023 * all the other builtins, because it is special -- it varies between
5026 t
= nasm_malloc(sizeof(*t
));
5028 make_tok_num(t
, apass
);
5030 define_smacro(NULL
, "__PASS__", true, 0, t
);
5033 static void pp_init(void)
5035 hash_init(&FileHash
, HASH_MEDIUM
);
5038 static char *pp_getline(void)
5043 real_verror
= nasm_set_verror(pp_verror
);
5047 * Fetch a tokenized line, either from the macro-expansion
5048 * buffer or from the input file.
5051 while (istk
->expansion
&& istk
->expansion
->finishes
) {
5052 Line
*l
= istk
->expansion
;
5053 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
5057 * This is a macro-end marker for a macro with no
5058 * name, which means it's not really a macro at all
5059 * but a %rep block, and the `in_progress' field is
5060 * more than 1, meaning that we still need to
5061 * repeat. (1 means the natural last repetition; 0
5062 * means termination by %exitrep.) We have
5063 * therefore expanded up to the %endrep, and must
5064 * push the whole block on to the expansion buffer
5065 * again. We don't bother to remove the macro-end
5066 * marker: we'd only have to generate another one
5069 l
->finishes
->in_progress
--;
5070 list_for_each(l
, l
->finishes
->expansion
) {
5071 Token
*t
, *tt
, **tail
;
5073 ll
= nasm_malloc(sizeof(Line
));
5074 ll
->next
= istk
->expansion
;
5075 ll
->finishes
= NULL
;
5079 list_for_each(t
, l
->first
) {
5080 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
5081 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
5086 istk
->expansion
= ll
;
5090 * Check whether a `%rep' was started and not ended
5091 * within this macro expansion. This can happen and
5092 * should be detected. It's a fatal error because
5093 * I'm too confused to work out how to recover
5098 nasm_panic(0, "defining with name in expansion");
5099 else if (istk
->mstk
->name
)
5100 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5101 " expansion of macro `%s'",
5106 * FIXME: investigate the relationship at this point between
5107 * istk->mstk and l->finishes
5110 MMacro
*m
= istk
->mstk
;
5111 istk
->mstk
= m
->next_active
;
5114 * This was a real macro call, not a %rep, and
5115 * therefore the parameter information needs to
5120 l
->finishes
->in_progress
--;
5122 nasm_free(m
->params
);
5123 free_tlist(m
->iline
);
5124 nasm_free(m
->paramlen
);
5125 l
->finishes
->in_progress
= 0;
5130 * FIXME It is incorrect to always free_mmacro here.
5131 * It leads to usage-after-free.
5133 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5140 istk
->expansion
= l
->next
;
5142 lfmt
->downlevel(LIST_MACRO
);
5145 while (1) { /* until we get a line we can use */
5147 if (istk
->expansion
) { /* from a macro expansion */
5149 Line
*l
= istk
->expansion
;
5151 istk
->mstk
->lineno
++;
5153 istk
->expansion
= l
->next
;
5155 p
= detoken(tline
, false);
5156 lfmt
->line(LIST_MACRO
, p
);
5161 if (line
) { /* from the current input file */
5162 line
= prepreproc(line
);
5163 tline
= tokenize(line
);
5168 * The current file has ended; work down the istk
5174 /* nasm_error can't be conditionally suppressed */
5176 "expected `%%endif' before end of file");
5178 /* only set line and file name if there's a next node */
5180 src_set(i
->lineno
, i
->fname
);
5182 lfmt
->downlevel(LIST_INCLUDE
);
5188 if (istk
->expansion
&& istk
->expansion
->finishes
)
5194 * We must expand MMacro parameters and MMacro-local labels
5195 * _before_ we plunge into directive processing, to cope
5196 * with things like `%define something %1' such as STRUC
5197 * uses. Unless we're _defining_ a MMacro, in which case
5198 * those tokens should be left alone to go into the
5199 * definition; and unless we're in a non-emitting
5200 * condition, in which case we don't want to meddle with
5203 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
5204 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
5205 tline
= expand_mmac_params(tline
);
5209 * Check the line to see if it's a preprocessor directive.
5211 if (do_directive(tline
, &line
) == DIRECTIVE_FOUND
) {
5213 break; /* Directive generated output */
5216 } else if (defining
) {
5218 * We're defining a multi-line macro. We emit nothing
5220 * shove the tokenized line on to the macro definition.
5222 Line
*l
= nasm_malloc(sizeof(Line
));
5223 l
->next
= defining
->expansion
;
5226 defining
->expansion
= l
;
5228 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
5230 * We're in a non-emitting branch of a condition block.
5231 * Emit nothing at all, not even a blank line: when we
5232 * emerge from the condition we'll give a line-number
5233 * directive so we keep our place correctly.
5237 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5239 * We're in a %rep block which has been terminated, so
5240 * we're walking through to the %endrep without
5241 * emitting anything. Emit nothing at all, not even a
5242 * blank line: when we emerge from the %rep block we'll
5243 * give a line-number directive so we keep our place
5249 tline
= expand_smacro(tline
);
5250 if (!expand_mmacro(tline
)) {
5252 * De-tokenize the line again, and emit it.
5254 line
= detoken(tline
, true);
5258 continue; /* expand_mmacro calls free_tlist */
5264 nasm_set_verror(real_verror
);
5268 static void pp_cleanup(int pass
)
5270 real_verror
= nasm_set_verror(pp_verror
);
5273 if (defining
->name
) {
5274 nasm_error(ERR_NONFATAL
,
5275 "end of file while still defining macro `%s'",
5278 nasm_error(ERR_NONFATAL
, "end of file while still in %%rep");
5281 free_mmacro(defining
);
5285 nasm_set_verror(real_verror
);
5298 src_set_fname(NULL
);
5305 while ((i
= ipath
)) {
5314 static void pp_include_path(char *path
)
5318 i
= nasm_malloc(sizeof(IncPath
));
5319 i
->path
= path
? nasm_strdup(path
) : NULL
;
5332 static void pp_pre_include(char *fname
)
5334 Token
*inc
, *space
, *name
;
5337 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5338 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5339 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5341 l
= nasm_malloc(sizeof(Line
));
5348 static void pp_pre_define(char *definition
)
5354 real_verror
= nasm_set_verror(pp_verror
);
5356 equals
= strchr(definition
, '=');
5357 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5358 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5361 space
->next
= tokenize(definition
);
5365 if (space
->next
->type
!= TOK_PREPROC_ID
&&
5366 space
->next
->type
!= TOK_ID
)
5367 nasm_error(ERR_WARNING
, "pre-defining non ID `%s\'\n", definition
);
5369 l
= nasm_malloc(sizeof(Line
));
5375 nasm_set_verror(real_verror
);
5378 static void pp_pre_undefine(char *definition
)
5383 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5384 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5385 space
->next
= tokenize(definition
);
5387 l
= nasm_malloc(sizeof(Line
));
5394 /* Insert an early preprocessor command that doesn't need special handling */
5395 static void pp_pre_command(const char *what
, char *string
)
5401 def
= tokenize(string
);
5403 cmd
= nasm_strcat(what
[0] == '%' ? "" : "%", what
);
5404 space
= new_Token(def
, TOK_WHITESPACE
, NULL
, 0);
5405 def
= new_Token(space
, TOK_PREPROC_ID
, cmd
, 0);
5408 l
= nasm_malloc(sizeof(Line
));
5415 static void pp_add_stdmac(macros_t
*macros
)
5419 /* Find the end of the list and avoid duplicates */
5420 for (mp
= stdmacros
; *mp
; mp
++) {
5422 return; /* Nothing to do */
5425 nasm_assert(mp
< &stdmacros
[ARRAY_SIZE(stdmacros
)-1]);
5430 static void pp_extra_stdmac(macros_t
*macros
)
5432 extrastdmac
= macros
;
5435 static void make_tok_num(Token
* tok
, int64_t val
)
5438 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5439 tok
->text
= nasm_strdup(numbuf
);
5440 tok
->type
= TOK_NUMBER
;
5443 static void pp_list_one_macro(MMacro
*m
, int severity
)
5448 /* We need to print the next_active list in reverse order */
5449 pp_list_one_macro(m
->next_active
, severity
);
5451 if (m
->name
&& !m
->nolist
) {
5452 src_set(m
->xline
+ m
->lineno
, m
->fname
);
5453 nasm_error(severity
, "... from macro `%s' defined here", m
->name
);
5457 static void pp_error_list_macros(int severity
)
5460 const char *saved_fname
= NULL
;
5462 severity
|= ERR_PP_LISTMACRO
| ERR_NO_SEVERITY
;
5463 src_get(&saved_line
, &saved_fname
);
5466 pp_list_one_macro(istk
->mstk
, severity
);
5468 src_set(saved_line
, saved_fname
);
5471 const struct preproc_ops nasmpp
= {
5482 pp_error_list_macros
,