1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 * This define sets the upper limit for smacro and recursive mmacro
346 #define DEADMAN_LIMIT (1 << 20)
349 #define REP_LIMIT ((INT64_C(1) << 62))
352 * Condition codes. Note that we use c_ prefix not C_ because C_ is
353 * used in nasm.h for the "real" condition codes. At _this_ level,
354 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
355 * ones, so we need a different enum...
357 static const char * const conditions
[] = {
358 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
359 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
360 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
363 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
364 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
365 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
368 static const enum pp_conds inverse_ccs
[] = {
369 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
370 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
,
371 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
377 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
378 static int is_condition(enum preproc_token arg
)
380 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
383 /* For TASM compatibility we need to be able to recognise TASM compatible
384 * conditional compilation directives. Using the NASM pre-processor does
385 * not work, so we look for them specifically from the following list and
386 * then jam in the equivalent NASM directive into the input stream.
390 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
391 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
394 static const char * const tasm_directives
[] = {
395 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
396 "ifndef", "include", "local"
399 static int StackSize
= 4;
400 static const char *StackPointer
= "ebp";
401 static int ArgOffset
= 8;
402 static int LocalOffset
= 0;
404 static Context
*cstk
;
405 static Include
*istk
;
406 static IncPath
*ipath
= NULL
;
408 static int pass
; /* HACK: pass 0 = generate dependencies only */
409 static StrList
**dephead
;
411 static uint64_t unique
; /* unique identifier numbers */
413 static Line
*predef
= NULL
;
414 static bool do_predef
;
417 * The current set of multi-line macros we have defined.
419 static struct hash_table mmacros
;
422 * The current set of single-line macros we have defined.
424 static struct hash_table smacros
;
427 * The multi-line macro we are currently defining, or the %rep
428 * block we are currently reading, if any.
430 static MMacro
*defining
;
432 static uint64_t nested_mac_count
;
433 static uint64_t nested_rep_count
;
436 * The number of macro parameters to allocate space for at a time.
438 #define PARAM_DELTA 16
441 * The standard macro set: defined in macros.c in a set of arrays.
442 * This gives our position in any macro set, while we are processing it.
443 * The stdmacset is an array of such macro sets.
445 static macros_t
*stdmacpos
;
446 static macros_t
**stdmacnext
;
447 static macros_t
*stdmacros
[8];
448 static macros_t
*extrastdmac
;
451 * Tokens are allocated in blocks to improve speed
453 #define TOKEN_BLOCKSIZE 4096
454 static Token
*freeTokens
= NULL
;
460 static Blocks blocks
= { NULL
, NULL
};
463 * Forward declarations.
465 static void pp_add_stdmac(macros_t
*macros
);
466 static Token
*expand_mmac_params(Token
* tline
);
467 static Token
*expand_smacro(Token
* tline
);
468 static Token
*expand_id(Token
* tline
);
469 static Context
*get_ctx(const char *name
, const char **namep
);
470 static void make_tok_num(Token
* tok
, int64_t val
);
471 static void pp_verror(int severity
, const char *fmt
, va_list ap
);
472 static vefunc real_verror
;
473 static void *new_Block(size_t size
);
474 static void delete_Blocks(void);
475 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
476 const char *text
, int txtlen
);
477 static Token
*delete_Token(Token
* t
);
480 * Macros for safe checking of token pointers, avoid *(NULL)
482 #define tok_type_(x,t) ((x) && (x)->type == (t))
483 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
484 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
485 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
488 * nasm_unquote with error if the string contains NUL characters.
489 * If the string contains NUL characters, issue an error and return
490 * the C len, i.e. truncate at the NUL.
492 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
494 size_t len
= nasm_unquote(qstr
, NULL
);
495 size_t clen
= strlen(qstr
);
498 nasm_error(ERR_NONFATAL
, "NUL character in `%s' directive",
499 pp_directives
[directive
]);
505 * In-place reverse a list of tokens.
507 static Token
*reverse_tokens(Token
*t
)
523 * Handle TASM specific directives, which do not contain a % in
524 * front of them. We do it here because I could not find any other
525 * place to do it for the moment, and it is a hack (ideally it would
526 * be nice to be able to use the NASM pre-processor to do it).
528 static char *check_tasm_directive(char *line
)
530 int32_t i
, j
, k
, m
, len
;
531 char *p
, *q
, *oldline
, oldchar
;
533 p
= nasm_skip_spaces(line
);
535 /* Binary search for the directive name */
537 j
= ARRAY_SIZE(tasm_directives
);
538 q
= nasm_skip_word(p
);
545 m
= nasm_stricmp(p
, tasm_directives
[k
]);
547 /* We have found a directive, so jam a % in front of it
548 * so that NASM will then recognise it as one if it's own.
553 line
= nasm_malloc(len
+ 2);
555 if (k
== TM_IFDIFI
) {
557 * NASM does not recognise IFDIFI, so we convert
558 * it to %if 0. This is not used in NASM
559 * compatible code, but does need to parse for the
560 * TASM macro package.
562 strcpy(line
+ 1, "if 0");
564 memcpy(line
+ 1, p
, len
+ 1);
579 * The pre-preprocessing stage... This function translates line
580 * number indications as they emerge from GNU cpp (`# lineno "file"
581 * flags') into NASM preprocessor line number indications (`%line
584 static char *prepreproc(char *line
)
587 char *fname
, *oldline
;
589 if (line
[0] == '#' && line
[1] == ' ') {
592 lineno
= atoi(fname
);
593 fname
+= strspn(fname
, "0123456789 ");
596 fnlen
= strcspn(fname
, "\"");
597 line
= nasm_malloc(20 + fnlen
);
598 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
601 if (tasm_compatible_mode
)
602 return check_tasm_directive(line
);
607 * Free a linked list of tokens.
609 static void free_tlist(Token
* list
)
612 list
= delete_Token(list
);
616 * Free a linked list of lines.
618 static void free_llist(Line
* list
)
621 list_for_each_safe(l
, tmp
, list
) {
622 free_tlist(l
->first
);
630 static void free_mmacro(MMacro
* m
)
633 free_tlist(m
->dlist
);
634 nasm_free(m
->defaults
);
635 free_llist(m
->expansion
);
640 * Free all currently defined macros, and free the hash tables
642 static void free_smacro_table(struct hash_table
*smt
)
646 struct hash_tbl_node
*it
= NULL
;
648 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
649 nasm_free((void *)key
);
650 list_for_each_safe(s
, tmp
, s
) {
652 free_tlist(s
->expansion
);
659 static void free_mmacro_table(struct hash_table
*mmt
)
663 struct hash_tbl_node
*it
= NULL
;
666 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
667 nasm_free((void *)key
);
668 list_for_each_safe(m
,tmp
, m
)
674 static void free_macros(void)
676 free_smacro_table(&smacros
);
677 free_mmacro_table(&mmacros
);
681 * Initialize the hash tables
683 static void init_macros(void)
685 hash_init(&smacros
, HASH_LARGE
);
686 hash_init(&mmacros
, HASH_LARGE
);
690 * Pop the context stack.
692 static void ctx_pop(void)
697 free_smacro_table(&c
->localmac
);
703 * Search for a key in the hash index; adding it if necessary
704 * (in which case we initialize the data pointer to NULL.)
707 hash_findi_add(struct hash_table
*hash
, const char *str
)
709 struct hash_insert hi
;
713 r
= hash_findi(hash
, str
, &hi
);
717 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
718 return hash_add(&hi
, strx
, NULL
);
722 * Like hash_findi, but returns the data element rather than a pointer
723 * to it. Used only when not adding a new element, hence no third
727 hash_findix(struct hash_table
*hash
, const char *str
)
731 p
= hash_findi(hash
, str
, NULL
);
732 return p
? *p
: NULL
;
736 * read line from standart macros set,
737 * if there no more left -- return NULL
739 static char *line_from_stdmac(void)
742 const unsigned char *p
= stdmacpos
;
751 len
+= pp_directives_len
[c
- 0x80] + 1;
756 line
= nasm_malloc(len
+ 1);
758 while ((c
= *stdmacpos
++)) {
760 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
761 q
+= pp_directives_len
[c
- 0x80];
771 /* This was the last of this particular macro set */
774 stdmacpos
= *stdmacnext
++;
775 } else if (do_predef
) {
777 Token
*head
, **tail
, *t
;
780 * Nasty hack: here we push the contents of
781 * `predef' on to the top-level expansion stack,
782 * since this is the most convenient way to
783 * implement the pre-include and pre-define
786 list_for_each(pd
, predef
) {
789 list_for_each(t
, pd
->first
) {
790 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
791 tail
= &(*tail
)->next
;
794 l
= nasm_malloc(sizeof(Line
));
795 l
->next
= istk
->expansion
;
808 static char *read_line(void)
810 unsigned int size
, c
, next
;
811 const unsigned int delta
= 512;
812 const unsigned int pad
= 8;
813 unsigned int nr_cont
= 0;
817 /* Standart macros set (predefined) goes first */
818 p
= line_from_stdmac();
823 p
= buffer
= nasm_malloc(size
);
827 if ((int)(c
) == EOF
) {
834 next
= fgetc(istk
->fp
);
836 ungetc(next
, istk
->fp
);
851 next
= fgetc(istk
->fp
);
852 ungetc(next
, istk
->fp
);
853 if (next
== '\r' || next
== '\n') {
861 if (c
== '\r' || c
== '\n') {
866 if (p
>= (buffer
+ size
- pad
)) {
867 buffer
= nasm_realloc(buffer
, size
+ delta
);
868 p
= buffer
+ size
- pad
;
872 *p
++ = (unsigned char)c
;
880 src_set_linnum(src_get_linnum() + istk
->lineinc
+
881 (nr_cont
* istk
->lineinc
));
884 * Handle spurious ^Z, which may be inserted into source files
885 * by some file transfer utilities.
887 buffer
[strcspn(buffer
, "\032")] = '\0';
889 lfmt
->line(LIST_READ
, buffer
);
895 * Tokenize a line of text. This is a very simple process since we
896 * don't need to parse the value out of e.g. numeric tokens: we
897 * simply split one string into many.
899 static Token
*tokenize(char *line
)
902 enum pp_token_type type
;
904 Token
*t
, **tail
= &list
;
910 if (*p
== '+' && !nasm_isdigit(p
[1])) {
913 } else if (nasm_isdigit(*p
) ||
914 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
918 while (nasm_isdigit(*p
));
919 type
= TOK_PREPROC_ID
;
920 } else if (*p
== '{') {
929 nasm_error(ERR_WARNING
| ERR_PASS1
,
930 "unterminated %%{ construct");
934 type
= TOK_PREPROC_ID
;
935 } else if (*p
== '[') {
937 line
+= 2; /* Skip the leading %[ */
939 while (lvl
&& (c
= *p
++)) {
951 p
= nasm_skip_string(p
- 1) + 1;
961 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
962 "unterminated %%[ construct");
964 } else if (*p
== '?') {
965 type
= TOK_PREPROC_Q
; /* %? */
968 type
= TOK_PREPROC_QQ
; /* %?? */
971 } else if (*p
== '!') {
972 type
= TOK_PREPROC_ID
;
978 while (isidchar(*p
));
979 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
980 p
= nasm_skip_string(p
);
984 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
985 "unterminated %%! string");
987 /* %! without string or identifier */
988 type
= TOK_OTHER
; /* Legacy behavior... */
990 } else if (isidchar(*p
) ||
991 ((*p
== '!' || *p
== '%' || *p
== '$') &&
996 while (isidchar(*p
));
997 type
= TOK_PREPROC_ID
;
1003 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
1006 while (*p
&& isidchar(*p
))
1008 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1013 p
= nasm_skip_string(p
);
1018 nasm_error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1019 /* Handling unterminated strings by UNV */
1022 } else if (p
[0] == '$' && p
[1] == '$') {
1023 type
= TOK_OTHER
; /* TOKEN_BASE */
1025 } else if (isnumstart(*p
)) {
1026 bool is_hex
= false;
1027 bool is_float
= false;
1043 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1045 if (*p
== '+' || *p
== '-') {
1047 * e can only be followed by +/- if it is either a
1048 * prefixed hex number or a floating-point number
1053 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1055 } else if (c
== 'P' || c
== 'p') {
1057 if (*p
== '+' || *p
== '-')
1059 } else if (isnumchar(c
))
1060 ; /* just advance */
1061 else if (c
== '.') {
1063 * we need to deal with consequences of the legacy
1064 * parser, like "1.nolist" being two tokens
1065 * (TOK_NUMBER, TOK_ID) here; at least give it
1066 * a shot for now. In the future, we probably need
1067 * a flex-based scanner with proper pattern matching
1068 * to do it as well as it can be done. Nothing in
1069 * the world is going to help the person who wants
1070 * 0x123.p16 interpreted as two tokens, though.
1076 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1077 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1078 (*r
== 'p' || *r
== 'P')) {
1082 break; /* Terminate the token */
1086 p
--; /* Point to first character beyond number */
1088 if (p
== line
+1 && *line
== '$') {
1089 type
= TOK_OTHER
; /* TOKEN_HERE */
1091 if (has_e
&& !is_hex
) {
1092 /* 1e13 is floating-point, but 1e13h is not */
1096 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1098 } else if (nasm_isspace(*p
)) {
1099 type
= TOK_WHITESPACE
;
1100 p
= nasm_skip_spaces(p
);
1102 * Whitespace just before end-of-line is discarded by
1103 * pretending it's a comment; whitespace just before a
1104 * comment gets lumped into the comment.
1106 if (!*p
|| *p
== ';') {
1111 } else if (*p
== ';') {
1117 * Anything else is an operator of some kind. We check
1118 * for all the double-character operators (>>, <<, //,
1119 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1120 * else is a single-character operator.
1123 if ((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] == '=') ||
1130 (p
[0] == '<' && p
[1] == '>') ||
1131 (p
[0] == '&' && p
[1] == '&') ||
1132 (p
[0] == '|' && p
[1] == '|') ||
1133 (p
[0] == '^' && p
[1] == '^')) {
1139 /* Handling unterminated string by UNV */
1142 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1143 t->text[p-line] = *line;
1147 if (type
!= TOK_COMMENT
) {
1148 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1157 * this function allocates a new managed block of memory and
1158 * returns a pointer to the block. The managed blocks are
1159 * deleted only all at once by the delete_Blocks function.
1161 static void *new_Block(size_t size
)
1163 Blocks
*b
= &blocks
;
1165 /* first, get to the end of the linked list */
1168 /* now allocate the requested chunk */
1169 b
->chunk
= nasm_malloc(size
);
1171 /* now allocate a new block for the next request */
1172 b
->next
= nasm_zalloc(sizeof(Blocks
));
1177 * this function deletes all managed blocks of memory
1179 static void delete_Blocks(void)
1181 Blocks
*a
, *b
= &blocks
;
1184 * keep in mind that the first block, pointed to by blocks
1185 * is a static and not dynamically allocated, so we don't
1190 nasm_free(b
->chunk
);
1196 memset(&blocks
, 0, sizeof(blocks
));
1200 * this function creates a new Token and passes a pointer to it
1201 * back to the caller. It sets the type and text elements, and
1202 * also the a.mac and next elements to NULL.
1204 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1205 const char *text
, int txtlen
)
1211 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1212 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1213 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1214 freeTokens
[i
].next
= NULL
;
1217 freeTokens
= t
->next
;
1221 if (type
== TOK_WHITESPACE
|| !text
) {
1225 txtlen
= strlen(text
);
1226 t
->text
= nasm_malloc(txtlen
+1);
1227 memcpy(t
->text
, text
, txtlen
);
1228 t
->text
[txtlen
] = '\0';
1233 static Token
*delete_Token(Token
* t
)
1235 Token
*next
= t
->next
;
1237 t
->next
= freeTokens
;
1243 * Convert a line of tokens back into text.
1244 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1245 * will be transformed into ..@ctxnum.xxx
1247 static char *detoken(Token
* tlist
, bool expand_locals
)
1254 list_for_each(t
, tlist
) {
1255 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1260 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1261 size_t len
= nasm_unquote(v
, NULL
);
1262 size_t clen
= strlen(v
);
1265 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1266 "NUL character in %%! string");
1272 char *p
= getenv(v
);
1274 nasm_error(ERR_NONFATAL
| ERR_PASS1
,
1275 "nonexistent environment variable `%s'", v
);
1277 * FIXME We better should investigate if accessing
1278 * ->text[1] without ->text[0] is safe enough.
1280 t
->text
= nasm_zalloc(2);
1282 t
->text
= nasm_strdup(p
);
1287 /* Expand local macros here and not during preprocessing */
1288 if (expand_locals
&&
1289 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1290 t
->text
[0] == '%' && t
->text
[1] == '$') {
1293 Context
*ctx
= get_ctx(t
->text
, &q
);
1296 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1297 p
= nasm_strcat(buffer
, q
);
1302 if (t
->type
== TOK_WHITESPACE
)
1305 len
+= strlen(t
->text
);
1308 p
= line
= nasm_malloc(len
+ 1);
1310 list_for_each(t
, tlist
) {
1311 if (t
->type
== TOK_WHITESPACE
) {
1313 } else if (t
->text
) {
1325 * A scanner, suitable for use by the expression evaluator, which
1326 * operates on a line of Tokens. Expects a pointer to a pointer to
1327 * the first token in the line to be passed in as its private_data
1330 * FIX: This really needs to be unified with stdscan.
1332 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1334 Token
**tlineptr
= private_data
;
1336 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1340 *tlineptr
= tline
? tline
->next
: NULL
;
1341 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1342 tline
->type
== TOK_COMMENT
));
1345 return tokval
->t_type
= TOKEN_EOS
;
1347 tokval
->t_charptr
= tline
->text
;
1349 if (tline
->text
[0] == '$' && !tline
->text
[1])
1350 return tokval
->t_type
= TOKEN_HERE
;
1351 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1352 return tokval
->t_type
= TOKEN_BASE
;
1354 if (tline
->type
== TOK_ID
) {
1355 p
= tokval
->t_charptr
= tline
->text
;
1357 tokval
->t_charptr
++;
1358 return tokval
->t_type
= TOKEN_ID
;
1361 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1362 if (r
>= p
+MAX_KEYWORD
)
1363 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1364 *s
++ = nasm_tolower(*r
);
1367 /* right, so we have an identifier sitting in temp storage. now,
1368 * is it actually a register or instruction name, or what? */
1369 return nasm_token_hash(ourcopy
, tokval
);
1372 if (tline
->type
== TOK_NUMBER
) {
1374 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1375 tokval
->t_charptr
= tline
->text
;
1377 return tokval
->t_type
= TOKEN_ERRNUM
;
1379 return tokval
->t_type
= TOKEN_NUM
;
1382 if (tline
->type
== TOK_FLOAT
) {
1383 return tokval
->t_type
= TOKEN_FLOAT
;
1386 if (tline
->type
== TOK_STRING
) {
1389 bq
= tline
->text
[0];
1390 tokval
->t_charptr
= tline
->text
;
1391 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1393 if (ep
[0] != bq
|| ep
[1] != '\0')
1394 return tokval
->t_type
= TOKEN_ERRSTR
;
1396 return tokval
->t_type
= TOKEN_STR
;
1399 if (tline
->type
== TOK_OTHER
) {
1400 if (!strcmp(tline
->text
, "<<"))
1401 return tokval
->t_type
= TOKEN_SHL
;
1402 if (!strcmp(tline
->text
, ">>"))
1403 return tokval
->t_type
= TOKEN_SHR
;
1404 if (!strcmp(tline
->text
, "//"))
1405 return tokval
->t_type
= TOKEN_SDIV
;
1406 if (!strcmp(tline
->text
, "%%"))
1407 return tokval
->t_type
= TOKEN_SMOD
;
1408 if (!strcmp(tline
->text
, "=="))
1409 return tokval
->t_type
= TOKEN_EQ
;
1410 if (!strcmp(tline
->text
, "<>"))
1411 return tokval
->t_type
= TOKEN_NE
;
1412 if (!strcmp(tline
->text
, "!="))
1413 return tokval
->t_type
= TOKEN_NE
;
1414 if (!strcmp(tline
->text
, "<="))
1415 return tokval
->t_type
= TOKEN_LE
;
1416 if (!strcmp(tline
->text
, ">="))
1417 return tokval
->t_type
= TOKEN_GE
;
1418 if (!strcmp(tline
->text
, "&&"))
1419 return tokval
->t_type
= TOKEN_DBL_AND
;
1420 if (!strcmp(tline
->text
, "^^"))
1421 return tokval
->t_type
= TOKEN_DBL_XOR
;
1422 if (!strcmp(tline
->text
, "||"))
1423 return tokval
->t_type
= TOKEN_DBL_OR
;
1427 * We have no other options: just return the first character of
1430 return tokval
->t_type
= tline
->text
[0];
1434 * Compare a string to the name of an existing macro; this is a
1435 * simple wrapper which calls either strcmp or nasm_stricmp
1436 * depending on the value of the `casesense' parameter.
1438 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1440 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1444 * Compare a string to the name of an existing macro; this is a
1445 * simple wrapper which calls either strcmp or nasm_stricmp
1446 * depending on the value of the `casesense' parameter.
1448 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1450 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1454 * Return the Context structure associated with a %$ token. Return
1455 * NULL, having _already_ reported an error condition, if the
1456 * context stack isn't deep enough for the supplied number of $
1459 * If "namep" is non-NULL, set it to the pointer to the macro name
1460 * tail, i.e. the part beyond %$...
1462 static Context
*get_ctx(const char *name
, const char **namep
)
1470 if (!name
|| name
[0] != '%' || name
[1] != '$')
1474 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1481 while (ctx
&& *name
== '$') {
1487 nasm_error(ERR_NONFATAL
, "`%s': context stack is only"
1488 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1499 * Open an include file. This routine must always return a valid
1500 * file pointer if it returns - it's responsible for throwing an
1501 * ERR_FATAL and bombing out completely if not. It should also try
1502 * the include path one by one until it finds the file or reaches
1503 * the end of the path.
1505 * Note: for INC_PROBE the function returns NULL at all times;
1506 * instead look for the
1509 INC_NEEDED
, /* File must exist */
1510 INC_OPTIONAL
, /* Missing is OK */
1511 INC_PROBE
/* Only an existence probe */
1514 /* This is conducts a full pathname search */
1515 static FILE *inc_fopen_search(const char *file
, StrList
**slpath
,
1516 enum incopen_mode omode
, enum file_flags fmode
)
1520 const IncPath
*ip
= ipath
;
1521 int len
= strlen(file
);
1522 size_t prefix_len
= 0;
1528 path_len
= prefix_len
+ len
+ 1;
1530 sl
= nasm_malloc(path_len
+ sizeof sl
->next
);
1531 memcpy(sl
->str
, prefix
, prefix_len
);
1532 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1535 if (omode
== INC_PROBE
) {
1537 found
= nasm_file_exists(sl
->str
);
1539 fp
= nasm_open_read(sl
->str
, fmode
);
1540 found
= (fp
!= NULL
);
1553 prefix_len
= strlen(prefix
);
1559 * Open a file, or test for the presence of one (depending on omode),
1560 * considering the include path.
1562 static FILE *inc_fopen(const char *file
,
1564 const char **found_path
,
1565 enum incopen_mode omode
,
1566 enum file_flags fmode
)
1569 struct hash_insert hi
;
1574 hp
= hash_find(&FileHash
, file
, &hi
);
1577 if (path
|| omode
!= INC_NEEDED
) {
1578 nasm_add_string_to_strlist(dhead
, path
? path
: file
);
1581 /* Need to do the actual path search */
1585 fp
= inc_fopen_search(file
, &sl
, omode
, fmode
);
1587 file_len
= strlen(file
);
1590 /* Store negative result for this file */
1591 sl
= nasm_malloc(file_len
+ 1 + sizeof sl
->next
);
1592 memcpy(sl
->str
, file
, file_len
+1);
1598 file
= strchr(path
, '\0') - file_len
;
1601 hash_add(&hi
, file
, path
); /* Positive or negative result */
1604 * Add file to dependency path. The in_list() is needed
1605 * in case the file was already added with %depend.
1607 if (path
|| omode
!= INC_NEEDED
)
1608 nasm_add_to_strlist(dhead
, sl
);
1612 if (omode
== INC_NEEDED
)
1613 nasm_fatal(0, "unable to open include file `%s'", file
);
1621 if (!fp
&& omode
!= INC_PROBE
)
1622 fp
= nasm_open_read(path
, fmode
);
1631 * Opens an include or input file. Public version, for use by modules
1632 * that get a file:lineno pair and need to look at the file again
1633 * (e.g. the CodeView debug backend). Returns NULL on failure.
1635 FILE *pp_input_fopen(const char *filename
, enum file_flags mode
)
1637 return inc_fopen(filename
, NULL
, NULL
, INC_OPTIONAL
, mode
);
1641 * Determine if we should warn on defining a single-line macro of
1642 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1643 * return true if _any_ single-line macro of that name is defined.
1644 * Otherwise, will return true if a single-line macro with either
1645 * `nparam' or no parameters is defined.
1647 * If a macro with precisely the right number of parameters is
1648 * defined, or nparam is -1, the address of the definition structure
1649 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1650 * is NULL, no action will be taken regarding its contents, and no
1653 * Note that this is also called with nparam zero to resolve
1656 * If you already know which context macro belongs to, you can pass
1657 * the context pointer as first parameter; if you won't but name begins
1658 * with %$ the context will be automatically computed. If all_contexts
1659 * is true, macro will be searched in outer contexts as well.
1662 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1665 struct hash_table
*smtbl
;
1669 smtbl
= &ctx
->localmac
;
1670 } else if (name
[0] == '%' && name
[1] == '$') {
1672 ctx
= get_ctx(name
, &name
);
1674 return false; /* got to return _something_ */
1675 smtbl
= &ctx
->localmac
;
1679 m
= (SMacro
*) hash_findix(smtbl
, name
);
1682 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1683 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1685 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1699 * Count and mark off the parameters in a multi-line macro call.
1700 * This is called both from within the multi-line macro expansion
1701 * code, and also to mark off the default parameters when provided
1702 * in a %macro definition line.
1704 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1706 int paramsize
, brace
;
1708 *nparam
= paramsize
= 0;
1711 /* +1: we need space for the final NULL */
1712 if (*nparam
+1 >= paramsize
) {
1713 paramsize
+= PARAM_DELTA
;
1714 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1718 if (tok_is_(t
, "{"))
1720 (*params
)[(*nparam
)++] = t
;
1722 while (brace
&& (t
= t
->next
) != NULL
) {
1723 if (tok_is_(t
, "{"))
1725 else if (tok_is_(t
, "}"))
1731 * Now we've found the closing brace, look further
1736 if (tok_isnt_(t
, ",")) {
1737 nasm_error(ERR_NONFATAL
,
1738 "braces do not enclose all of macro parameter");
1739 while (tok_isnt_(t
, ","))
1744 while (tok_isnt_(t
, ","))
1747 if (t
) { /* got a comma/brace */
1748 t
= t
->next
; /* eat the comma */
1754 * Determine whether one of the various `if' conditions is true or
1757 * We must free the tline we get passed.
1759 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1761 enum pp_conditional i
= PP_COND(ct
);
1763 Token
*t
, *tt
, **tptr
, *origline
;
1764 struct tokenval tokval
;
1766 enum pp_token_type needtype
;
1773 j
= false; /* have we matched yet? */
1778 if (tline
->type
!= TOK_ID
) {
1779 nasm_error(ERR_NONFATAL
,
1780 "`%s' expects context identifiers", pp_directives
[ct
]);
1781 free_tlist(origline
);
1784 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1786 tline
= tline
->next
;
1791 j
= false; /* have we matched yet? */
1794 if (!tline
|| (tline
->type
!= TOK_ID
&&
1795 (tline
->type
!= TOK_PREPROC_ID
||
1796 tline
->text
[1] != '$'))) {
1797 nasm_error(ERR_NONFATAL
,
1798 "`%s' expects macro identifiers", pp_directives
[ct
]);
1801 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1803 tline
= tline
->next
;
1808 tline
= expand_smacro(tline
);
1809 j
= false; /* have we matched yet? */
1812 if (!tline
|| (tline
->type
!= TOK_ID
&&
1813 tline
->type
!= TOK_STRING
&&
1814 (tline
->type
!= TOK_PREPROC_ID
||
1815 tline
->text
[1] != '!'))) {
1816 nasm_error(ERR_NONFATAL
,
1817 "`%s' expects environment variable names",
1822 if (tline
->type
== TOK_PREPROC_ID
)
1823 p
+= 2; /* Skip leading %! */
1824 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1825 nasm_unquote_cstr(p
, ct
);
1828 tline
= tline
->next
;
1834 tline
= expand_smacro(tline
);
1836 while (tok_isnt_(tt
, ","))
1839 nasm_error(ERR_NONFATAL
,
1840 "`%s' expects two comma-separated arguments",
1845 j
= true; /* assume equality unless proved not */
1846 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1847 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1848 nasm_error(ERR_NONFATAL
, "`%s': more than one comma on line",
1852 if (t
->type
== TOK_WHITESPACE
) {
1856 if (tt
->type
== TOK_WHITESPACE
) {
1860 if (tt
->type
!= t
->type
) {
1861 j
= false; /* found mismatching tokens */
1864 /* When comparing strings, need to unquote them first */
1865 if (t
->type
== TOK_STRING
) {
1866 size_t l1
= nasm_unquote(t
->text
, NULL
);
1867 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1873 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1877 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1878 j
= false; /* found mismatching tokens */
1885 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1886 j
= false; /* trailing gunk on one end or other */
1892 MMacro searching
, *mmac
;
1895 tline
= expand_id(tline
);
1896 if (!tok_type_(tline
, TOK_ID
)) {
1897 nasm_error(ERR_NONFATAL
,
1898 "`%s' expects a macro name", pp_directives
[ct
]);
1901 searching
.name
= nasm_strdup(tline
->text
);
1902 searching
.casesense
= true;
1903 searching
.plus
= false;
1904 searching
.nolist
= false;
1905 searching
.in_progress
= 0;
1906 searching
.max_depth
= 0;
1907 searching
.rep_nest
= NULL
;
1908 searching
.nparam_min
= 0;
1909 searching
.nparam_max
= INT_MAX
;
1910 tline
= expand_smacro(tline
->next
);
1913 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1914 nasm_error(ERR_NONFATAL
,
1915 "`%s' expects a parameter count or nothing",
1918 searching
.nparam_min
= searching
.nparam_max
=
1919 readnum(tline
->text
, &j
);
1921 nasm_error(ERR_NONFATAL
,
1922 "unable to parse parameter count `%s'",
1925 if (tline
&& tok_is_(tline
->next
, "-")) {
1926 tline
= tline
->next
->next
;
1927 if (tok_is_(tline
, "*"))
1928 searching
.nparam_max
= INT_MAX
;
1929 else if (!tok_type_(tline
, TOK_NUMBER
))
1930 nasm_error(ERR_NONFATAL
,
1931 "`%s' expects a parameter count after `-'",
1934 searching
.nparam_max
= readnum(tline
->text
, &j
);
1936 nasm_error(ERR_NONFATAL
,
1937 "unable to parse parameter count `%s'",
1939 if (searching
.nparam_min
> searching
.nparam_max
)
1940 nasm_error(ERR_NONFATAL
,
1941 "minimum parameter count exceeds maximum");
1944 if (tline
&& tok_is_(tline
->next
, "+")) {
1945 tline
= tline
->next
;
1946 searching
.plus
= true;
1948 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1950 if (!strcmp(mmac
->name
, searching
.name
) &&
1951 (mmac
->nparam_min
<= searching
.nparam_max
1953 && (searching
.nparam_min
<= mmac
->nparam_max
1960 if (tline
&& tline
->next
)
1961 nasm_error(ERR_WARNING
|ERR_PASS1
,
1962 "trailing garbage after %%ifmacro ignored");
1963 nasm_free(searching
.name
);
1972 needtype
= TOK_NUMBER
;
1975 needtype
= TOK_STRING
;
1979 t
= tline
= expand_smacro(tline
);
1981 while (tok_type_(t
, TOK_WHITESPACE
) ||
1982 (needtype
== TOK_NUMBER
&&
1983 tok_type_(t
, TOK_OTHER
) &&
1984 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1988 j
= tok_type_(t
, needtype
);
1992 t
= tline
= expand_smacro(tline
);
1993 while (tok_type_(t
, TOK_WHITESPACE
))
1998 t
= t
->next
; /* Skip the actual token */
1999 while (tok_type_(t
, TOK_WHITESPACE
))
2001 j
= !t
; /* Should be nothing left */
2006 t
= tline
= expand_smacro(tline
);
2007 while (tok_type_(t
, TOK_WHITESPACE
))
2010 j
= !t
; /* Should be empty */
2014 t
= tline
= expand_smacro(tline
);
2016 tokval
.t_type
= TOKEN_INVALID
;
2017 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2018 NULL
, pass
| CRITICAL
, NULL
);
2022 nasm_error(ERR_WARNING
|ERR_PASS1
,
2023 "trailing garbage after expression ignored");
2024 if (!is_simple(evalresult
)) {
2025 nasm_error(ERR_NONFATAL
,
2026 "non-constant value given to `%s'", pp_directives
[ct
]);
2029 j
= reloc_value(evalresult
) != 0;
2033 nasm_error(ERR_FATAL
,
2034 "preprocessor directive `%s' not yet implemented",
2039 free_tlist(origline
);
2040 return j
^ PP_NEGATIVE(ct
);
2043 free_tlist(origline
);
2048 * Common code for defining an smacro
2050 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2051 int nparam
, Token
*expansion
)
2053 SMacro
*smac
, **smhead
;
2054 struct hash_table
*smtbl
;
2056 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2058 nasm_error(ERR_WARNING
|ERR_PASS1
,
2059 "single-line macro `%s' defined both with and"
2060 " without parameters", mname
);
2062 * Some instances of the old code considered this a failure,
2063 * some others didn't. What is the right thing to do here?
2065 free_tlist(expansion
);
2066 return false; /* Failure */
2069 * We're redefining, so we have to take over an
2070 * existing SMacro structure. This means freeing
2071 * what was already in it.
2073 nasm_free(smac
->name
);
2074 free_tlist(smac
->expansion
);
2077 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2078 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2079 smac
= nasm_malloc(sizeof(SMacro
));
2080 smac
->next
= *smhead
;
2083 smac
->name
= nasm_strdup(mname
);
2084 smac
->casesense
= casesense
;
2085 smac
->nparam
= nparam
;
2086 smac
->expansion
= expansion
;
2087 smac
->in_progress
= false;
2088 return true; /* Success */
2092 * Undefine an smacro
2094 static void undef_smacro(Context
*ctx
, const char *mname
)
2096 SMacro
**smhead
, *s
, **sp
;
2097 struct hash_table
*smtbl
;
2099 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2100 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2104 * We now have a macro name... go hunt for it.
2107 while ((s
= *sp
) != NULL
) {
2108 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2111 free_tlist(s
->expansion
);
2121 * Parse a mmacro specification.
2123 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2127 tline
= tline
->next
;
2129 tline
= expand_id(tline
);
2130 if (!tok_type_(tline
, TOK_ID
)) {
2131 nasm_error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2136 def
->name
= nasm_strdup(tline
->text
);
2138 def
->nolist
= false;
2139 def
->in_progress
= 0;
2140 def
->rep_nest
= NULL
;
2141 def
->nparam_min
= 0;
2142 def
->nparam_max
= 0;
2144 tline
= expand_smacro(tline
->next
);
2146 if (!tok_type_(tline
, TOK_NUMBER
)) {
2147 nasm_error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2149 def
->nparam_min
= def
->nparam_max
=
2150 readnum(tline
->text
, &err
);
2152 nasm_error(ERR_NONFATAL
,
2153 "unable to parse parameter count `%s'", tline
->text
);
2155 if (tline
&& tok_is_(tline
->next
, "-")) {
2156 tline
= tline
->next
->next
;
2157 if (tok_is_(tline
, "*")) {
2158 def
->nparam_max
= INT_MAX
;
2159 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2160 nasm_error(ERR_NONFATAL
,
2161 "`%s' expects a parameter count after `-'", directive
);
2163 def
->nparam_max
= readnum(tline
->text
, &err
);
2165 nasm_error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2168 if (def
->nparam_min
> def
->nparam_max
) {
2169 nasm_error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2173 if (tline
&& tok_is_(tline
->next
, "+")) {
2174 tline
= tline
->next
;
2177 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2178 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2179 tline
= tline
->next
;
2184 * Handle default parameters.
2186 if (tline
&& tline
->next
) {
2187 def
->dlist
= tline
->next
;
2189 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2192 def
->defaults
= NULL
;
2194 def
->expansion
= NULL
;
2196 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2198 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2199 "too many default macro parameters");
2206 * Decode a size directive
2208 static int parse_size(const char *str
) {
2209 static const char *size_names
[] =
2210 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2211 static const int sizes
[] =
2212 { 0, 1, 4, 16, 8, 10, 2, 32 };
2214 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2218 * Process a preprocessor %pragma directive. Currently there are none.
2219 * Gets passed the token list starting with the "preproc" token from
2220 * "%pragma preproc".
2222 static void do_pragma_preproc(Token
*tline
)
2224 /* Skip to the real stuff */
2225 tline
= tline
->next
;
2230 (void)tline
; /* Nothing else to do at present */
2234 * find and process preprocessor directive in passed line
2235 * Find out if a line contains a preprocessor directive, and deal
2238 * If a directive _is_ found, it is the responsibility of this routine
2239 * (and not the caller) to free_tlist() the line.
2241 * @param tline a pointer to the current tokeninzed line linked list
2242 * @param output if this directive generated output
2243 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2246 static int do_directive(Token
*tline
, char **output
)
2248 enum preproc_token i
;
2257 const char *found_path
;
2262 MMacro
*mmac
, **mmhead
;
2263 Token
*t
= NULL
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2265 struct tokenval tokval
;
2267 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2272 *output
= NULL
; /* No output generated */
2276 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2277 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2278 || tline
->text
[1] == '!'))
2279 return NO_DIRECTIVE_FOUND
;
2281 i
= pp_token_hash(tline
->text
);
2284 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2285 * since they are known to be buggy at moment, we need to fix them
2286 * in future release (2.09-2.10)
2288 if (i
== PP_RMACRO
|| i
== PP_IRMACRO
|| i
== PP_EXITMACRO
) {
2289 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2291 return NO_DIRECTIVE_FOUND
;
2295 * If we're in a non-emitting branch of a condition construct,
2296 * or walking to the end of an already terminated %rep block,
2297 * we should ignore all directives except for condition
2300 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2301 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2302 return NO_DIRECTIVE_FOUND
;
2306 * If we're defining a macro or reading a %rep block, we should
2307 * ignore all directives except for %macro/%imacro (which nest),
2308 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2309 * If we're in a %rep block, another %rep nests, so should be let through.
2311 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2312 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2313 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2314 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2315 return NO_DIRECTIVE_FOUND
;
2319 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2320 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2322 return NO_DIRECTIVE_FOUND
;
2323 } else if (nested_mac_count
> 0) {
2324 if (i
== PP_ENDMACRO
) {
2326 return NO_DIRECTIVE_FOUND
;
2329 if (!defining
->name
) {
2332 return NO_DIRECTIVE_FOUND
;
2333 } else if (nested_rep_count
> 0) {
2334 if (i
== PP_ENDREP
) {
2336 return NO_DIRECTIVE_FOUND
;
2344 nasm_error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2346 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2350 * %pragma namespace options...
2352 * The namespace "preproc" is reserved for the preprocessor;
2353 * all other namespaces generate a [pragma] assembly directive.
2355 * Invalid %pragmas are ignored and may have different
2356 * meaning in future versions of NASM.
2358 tline
= tline
->next
;
2360 tline
= expand_smacro(tline
);
2361 if (tok_type_(tline
, TOK_ID
)) {
2362 if (!nasm_stricmp(tline
->text
, "preproc")) {
2363 /* Preprocessor pragma */
2364 do_pragma_preproc(tline
);
2366 /* Build the assembler directive */
2367 t
= new_Token(NULL
, TOK_OTHER
, "[", 1);
2368 t
->next
= new_Token(NULL
, TOK_ID
, "pragma", 6);
2369 t
->next
->next
= new_Token(tline
, TOK_WHITESPACE
, NULL
, 0);
2371 for (t
= tline
; t
->next
; t
= t
->next
)
2373 t
->next
= new_Token(NULL
, TOK_OTHER
, "]", 1);
2374 /* true here can be revisited in the future */
2375 *output
= detoken(tline
, true);
2378 free_tlist(origline
);
2379 return DIRECTIVE_FOUND
;
2382 /* Directive to tell NASM what the default stack size is. The
2383 * default is for a 16-bit stack, and this can be overriden with
2386 tline
= tline
->next
;
2387 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2388 tline
= tline
->next
;
2389 if (!tline
|| tline
->type
!= TOK_ID
) {
2390 nasm_error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2391 free_tlist(origline
);
2392 return DIRECTIVE_FOUND
;
2394 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2395 /* All subsequent ARG directives are for a 32-bit stack */
2397 StackPointer
= "ebp";
2400 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2401 /* All subsequent ARG directives are for a 64-bit stack */
2403 StackPointer
= "rbp";
2406 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2407 /* All subsequent ARG directives are for a 16-bit stack,
2408 * far function call.
2411 StackPointer
= "bp";
2414 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2415 /* All subsequent ARG directives are for a 16-bit stack,
2416 * far function call. We don't support near functions.
2419 StackPointer
= "bp";
2423 nasm_error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2424 free_tlist(origline
);
2425 return DIRECTIVE_FOUND
;
2427 free_tlist(origline
);
2428 return DIRECTIVE_FOUND
;
2431 /* TASM like ARG directive to define arguments to functions, in
2432 * the following form:
2434 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2438 char *arg
, directive
[256];
2439 int size
= StackSize
;
2441 /* Find the argument name */
2442 tline
= tline
->next
;
2443 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2444 tline
= tline
->next
;
2445 if (!tline
|| tline
->type
!= TOK_ID
) {
2446 nasm_error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2447 free_tlist(origline
);
2448 return DIRECTIVE_FOUND
;
2452 /* Find the argument size type */
2453 tline
= tline
->next
;
2454 if (!tline
|| tline
->type
!= TOK_OTHER
2455 || tline
->text
[0] != ':') {
2456 nasm_error(ERR_NONFATAL
,
2457 "Syntax error processing `%%arg' directive");
2458 free_tlist(origline
);
2459 return DIRECTIVE_FOUND
;
2461 tline
= tline
->next
;
2462 if (!tline
|| tline
->type
!= TOK_ID
) {
2463 nasm_error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2464 free_tlist(origline
);
2465 return DIRECTIVE_FOUND
;
2468 /* Allow macro expansion of type parameter */
2469 tt
= tokenize(tline
->text
);
2470 tt
= expand_smacro(tt
);
2471 size
= parse_size(tt
->text
);
2473 nasm_error(ERR_NONFATAL
,
2474 "Invalid size type for `%%arg' missing directive");
2476 free_tlist(origline
);
2477 return DIRECTIVE_FOUND
;
2481 /* Round up to even stack slots */
2482 size
= ALIGN(size
, StackSize
);
2484 /* Now define the macro for the argument */
2485 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2486 arg
, StackPointer
, offset
);
2487 do_directive(tokenize(directive
), output
);
2490 /* Move to the next argument in the list */
2491 tline
= tline
->next
;
2492 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2493 tline
= tline
->next
;
2494 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2496 free_tlist(origline
);
2497 return DIRECTIVE_FOUND
;
2500 /* TASM like LOCAL directive to define local variables for a
2501 * function, in the following form:
2503 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2505 * The '= LocalSize' at the end is ignored by NASM, but is
2506 * required by TASM to define the local parameter size (and used
2507 * by the TASM macro package).
2509 offset
= LocalOffset
;
2511 char *local
, directive
[256];
2512 int size
= StackSize
;
2514 /* Find the argument name */
2515 tline
= tline
->next
;
2516 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2517 tline
= tline
->next
;
2518 if (!tline
|| tline
->type
!= TOK_ID
) {
2519 nasm_error(ERR_NONFATAL
,
2520 "`%%local' missing argument parameter");
2521 free_tlist(origline
);
2522 return DIRECTIVE_FOUND
;
2524 local
= tline
->text
;
2526 /* Find the argument size type */
2527 tline
= tline
->next
;
2528 if (!tline
|| tline
->type
!= TOK_OTHER
2529 || tline
->text
[0] != ':') {
2530 nasm_error(ERR_NONFATAL
,
2531 "Syntax error processing `%%local' directive");
2532 free_tlist(origline
);
2533 return DIRECTIVE_FOUND
;
2535 tline
= tline
->next
;
2536 if (!tline
|| tline
->type
!= TOK_ID
) {
2537 nasm_error(ERR_NONFATAL
,
2538 "`%%local' missing size type parameter");
2539 free_tlist(origline
);
2540 return DIRECTIVE_FOUND
;
2543 /* Allow macro expansion of type parameter */
2544 tt
= tokenize(tline
->text
);
2545 tt
= expand_smacro(tt
);
2546 size
= parse_size(tt
->text
);
2548 nasm_error(ERR_NONFATAL
,
2549 "Invalid size type for `%%local' missing directive");
2551 free_tlist(origline
);
2552 return DIRECTIVE_FOUND
;
2556 /* Round up to even stack slots */
2557 size
= ALIGN(size
, StackSize
);
2559 offset
+= size
; /* Negative offset, increment before */
2561 /* Now define the macro for the argument */
2562 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2563 local
, StackPointer
, offset
);
2564 do_directive(tokenize(directive
), output
);
2566 /* Now define the assign to setup the enter_c macro correctly */
2567 snprintf(directive
, sizeof(directive
),
2568 "%%assign %%$localsize %%$localsize+%d", size
);
2569 do_directive(tokenize(directive
), output
);
2571 /* Move to the next argument in the list */
2572 tline
= tline
->next
;
2573 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2574 tline
= tline
->next
;
2575 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2576 LocalOffset
= offset
;
2577 free_tlist(origline
);
2578 return DIRECTIVE_FOUND
;
2582 nasm_error(ERR_WARNING
|ERR_PASS1
,
2583 "trailing garbage after `%%clear' ignored");
2586 free_tlist(origline
);
2587 return DIRECTIVE_FOUND
;
2590 t
= tline
->next
= expand_smacro(tline
->next
);
2592 if (!t
|| (t
->type
!= TOK_STRING
&&
2593 t
->type
!= TOK_INTERNAL_STRING
)) {
2594 nasm_error(ERR_NONFATAL
, "`%%depend' expects a file name");
2595 free_tlist(origline
);
2596 return DIRECTIVE_FOUND
; /* but we did _something_ */
2599 nasm_error(ERR_WARNING
|ERR_PASS1
,
2600 "trailing garbage after `%%depend' ignored");
2602 if (t
->type
!= TOK_INTERNAL_STRING
)
2603 nasm_unquote_cstr(p
, i
);
2604 nasm_add_string_to_strlist(dephead
, p
);
2605 free_tlist(origline
);
2606 return DIRECTIVE_FOUND
;
2609 t
= tline
->next
= expand_smacro(tline
->next
);
2612 if (!t
|| (t
->type
!= TOK_STRING
&&
2613 t
->type
!= TOK_INTERNAL_STRING
)) {
2614 nasm_error(ERR_NONFATAL
, "`%%include' expects a file name");
2615 free_tlist(origline
);
2616 return DIRECTIVE_FOUND
; /* but we did _something_ */
2619 nasm_error(ERR_WARNING
|ERR_PASS1
,
2620 "trailing garbage after `%%include' ignored");
2622 if (t
->type
!= TOK_INTERNAL_STRING
)
2623 nasm_unquote_cstr(p
, i
);
2624 inc
= nasm_malloc(sizeof(Include
));
2628 inc
->fp
= inc_fopen(p
, dephead
, &found_path
,
2629 pass
== 0 ? INC_OPTIONAL
: INC_NEEDED
, NF_TEXT
);
2631 /* -MG given but file not found */
2634 inc
->fname
= src_set_fname(found_path
? found_path
: p
);
2635 inc
->lineno
= src_set_linnum(0);
2637 inc
->expansion
= NULL
;
2640 lfmt
->uplevel(LIST_INCLUDE
);
2642 free_tlist(origline
);
2643 return DIRECTIVE_FOUND
;
2647 static macros_t
*use_pkg
;
2648 const char *pkg_macro
= NULL
;
2650 tline
= tline
->next
;
2652 tline
= expand_id(tline
);
2654 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2655 tline
->type
!= TOK_INTERNAL_STRING
&&
2656 tline
->type
!= TOK_ID
)) {
2657 nasm_error(ERR_NONFATAL
, "`%%use' expects a package name");
2658 free_tlist(origline
);
2659 return DIRECTIVE_FOUND
; /* but we did _something_ */
2662 nasm_error(ERR_WARNING
|ERR_PASS1
,
2663 "trailing garbage after `%%use' ignored");
2664 if (tline
->type
== TOK_STRING
)
2665 nasm_unquote_cstr(tline
->text
, i
);
2666 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2668 nasm_error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2670 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2671 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2672 /* Not already included, go ahead and include it */
2673 stdmacpos
= use_pkg
;
2675 free_tlist(origline
);
2676 return DIRECTIVE_FOUND
;
2681 tline
= tline
->next
;
2683 tline
= expand_id(tline
);
2685 if (!tok_type_(tline
, TOK_ID
)) {
2686 nasm_error(ERR_NONFATAL
, "`%s' expects a context identifier",
2688 free_tlist(origline
);
2689 return DIRECTIVE_FOUND
; /* but we did _something_ */
2692 nasm_error(ERR_WARNING
|ERR_PASS1
,
2693 "trailing garbage after `%s' ignored",
2695 p
= nasm_strdup(tline
->text
);
2697 p
= NULL
; /* Anonymous */
2701 ctx
= nasm_malloc(sizeof(Context
));
2703 hash_init(&ctx
->localmac
, HASH_SMALL
);
2705 ctx
->number
= unique
++;
2710 nasm_error(ERR_NONFATAL
, "`%s': context stack is empty",
2712 } else if (i
== PP_POP
) {
2713 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2714 nasm_error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2716 cstk
->name
? cstk
->name
: "anonymous", p
);
2721 nasm_free(cstk
->name
);
2727 free_tlist(origline
);
2728 return DIRECTIVE_FOUND
;
2730 severity
= ERR_FATAL
;
2733 severity
= ERR_NONFATAL
;
2736 severity
= ERR_WARNING
|ERR_WARN_USER
;
2741 /* Only error out if this is the final pass */
2742 if (pass
!= 2 && i
!= PP_FATAL
)
2743 return DIRECTIVE_FOUND
;
2745 tline
->next
= expand_smacro(tline
->next
);
2746 tline
= tline
->next
;
2748 t
= tline
? tline
->next
: NULL
;
2750 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2751 /* The line contains only a quoted string */
2753 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2754 nasm_error(severity
, "%s", p
);
2756 /* Not a quoted string, or more than a quoted string */
2757 p
= detoken(tline
, false);
2758 nasm_error(severity
, "%s", p
);
2761 free_tlist(origline
);
2762 return DIRECTIVE_FOUND
;
2766 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2769 j
= if_condition(tline
->next
, i
);
2770 tline
->next
= NULL
; /* it got freed */
2771 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2773 cond
= nasm_malloc(sizeof(Cond
));
2774 cond
->next
= istk
->conds
;
2778 istk
->mstk
->condcnt
++;
2779 free_tlist(origline
);
2780 return DIRECTIVE_FOUND
;
2784 nasm_error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2785 switch(istk
->conds
->state
) {
2787 istk
->conds
->state
= COND_DONE
;
2794 case COND_ELSE_TRUE
:
2795 case COND_ELSE_FALSE
:
2796 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2797 "`%%elif' after `%%else' ignored");
2798 istk
->conds
->state
= COND_NEVER
;
2803 * IMPORTANT: In the case of %if, we will already have
2804 * called expand_mmac_params(); however, if we're
2805 * processing an %elif we must have been in a
2806 * non-emitting mode, which would have inhibited
2807 * the normal invocation of expand_mmac_params().
2808 * Therefore, we have to do it explicitly here.
2810 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2811 tline
->next
= NULL
; /* it got freed */
2812 istk
->conds
->state
=
2813 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2816 free_tlist(origline
);
2817 return DIRECTIVE_FOUND
;
2821 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2822 "trailing garbage after `%%else' ignored");
2824 nasm_fatal(0, "`%%else: no matching `%%if'");
2825 switch(istk
->conds
->state
) {
2828 istk
->conds
->state
= COND_ELSE_FALSE
;
2835 istk
->conds
->state
= COND_ELSE_TRUE
;
2838 case COND_ELSE_TRUE
:
2839 case COND_ELSE_FALSE
:
2840 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2841 "`%%else' after `%%else' ignored.");
2842 istk
->conds
->state
= COND_NEVER
;
2845 free_tlist(origline
);
2846 return DIRECTIVE_FOUND
;
2850 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_PP_PRECOND
,
2851 "trailing garbage after `%%endif' ignored");
2853 nasm_error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2855 istk
->conds
= cond
->next
;
2858 istk
->mstk
->condcnt
--;
2859 free_tlist(origline
);
2860 return DIRECTIVE_FOUND
;
2867 nasm_error(ERR_FATAL
, "`%s': already defining a macro",
2869 return DIRECTIVE_FOUND
;
2871 defining
= nasm_zalloc(sizeof(MMacro
));
2872 defining
->max_depth
=
2873 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2874 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2875 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2876 nasm_free(defining
);
2878 return DIRECTIVE_FOUND
;
2881 src_get(&defining
->xline
, &defining
->fname
);
2883 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2885 if (!strcmp(mmac
->name
, defining
->name
) &&
2886 (mmac
->nparam_min
<= defining
->nparam_max
2888 && (defining
->nparam_min
<= mmac
->nparam_max
2890 nasm_error(ERR_WARNING
|ERR_PASS1
,
2891 "redefining multi-line macro `%s'", defining
->name
);
2892 return DIRECTIVE_FOUND
;
2896 free_tlist(origline
);
2897 return DIRECTIVE_FOUND
;
2901 if (! (defining
&& defining
->name
)) {
2902 nasm_error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2903 return DIRECTIVE_FOUND
;
2905 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2906 defining
->next
= *mmhead
;
2909 free_tlist(origline
);
2910 return DIRECTIVE_FOUND
;
2914 * We must search along istk->expansion until we hit a
2915 * macro-end marker for a macro with a name. Then we
2916 * bypass all lines between exitmacro and endmacro.
2918 list_for_each(l
, istk
->expansion
)
2919 if (l
->finishes
&& l
->finishes
->name
)
2924 * Remove all conditional entries relative to this
2925 * macro invocation. (safe to do in this context)
2927 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2929 istk
->conds
= cond
->next
;
2932 istk
->expansion
= l
;
2934 nasm_error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2936 free_tlist(origline
);
2937 return DIRECTIVE_FOUND
;
2945 spec
.casesense
= (i
== PP_UNMACRO
);
2946 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2947 return DIRECTIVE_FOUND
;
2949 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2950 while (mmac_p
&& *mmac_p
) {
2952 if (mmac
->casesense
== spec
.casesense
&&
2953 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2954 mmac
->nparam_min
== spec
.nparam_min
&&
2955 mmac
->nparam_max
== spec
.nparam_max
&&
2956 mmac
->plus
== spec
.plus
) {
2957 *mmac_p
= mmac
->next
;
2960 mmac_p
= &mmac
->next
;
2963 free_tlist(origline
);
2964 free_tlist(spec
.dlist
);
2965 return DIRECTIVE_FOUND
;
2969 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2970 tline
= tline
->next
;
2972 free_tlist(origline
);
2973 nasm_error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2974 return DIRECTIVE_FOUND
;
2976 t
= expand_smacro(tline
->next
);
2978 free_tlist(origline
);
2981 tokval
.t_type
= TOKEN_INVALID
;
2983 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
2986 return DIRECTIVE_FOUND
;
2988 nasm_error(ERR_WARNING
|ERR_PASS1
,
2989 "trailing garbage after expression ignored");
2990 if (!is_simple(evalresult
)) {
2991 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2992 return DIRECTIVE_FOUND
;
2995 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2996 mmac
= mmac
->next_active
;
2998 nasm_error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2999 } else if (mmac
->nparam
== 0) {
3000 nasm_error(ERR_NONFATAL
,
3001 "`%%rotate' invoked within macro without parameters");
3003 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
3005 rotate
%= (int)mmac
->nparam
;
3007 rotate
+= mmac
->nparam
;
3009 mmac
->rotate
= rotate
;
3011 return DIRECTIVE_FOUND
;
3016 tline
= tline
->next
;
3017 } while (tok_type_(tline
, TOK_WHITESPACE
));
3019 if (tok_type_(tline
, TOK_ID
) &&
3020 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3023 tline
= tline
->next
;
3024 } while (tok_type_(tline
, TOK_WHITESPACE
));
3028 t
= expand_smacro(tline
);
3030 tokval
.t_type
= TOKEN_INVALID
;
3032 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3034 free_tlist(origline
);
3035 return DIRECTIVE_FOUND
;
3038 nasm_error(ERR_WARNING
|ERR_PASS1
,
3039 "trailing garbage after expression ignored");
3040 if (!is_simple(evalresult
)) {
3041 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3042 return DIRECTIVE_FOUND
;
3044 count
= reloc_value(evalresult
);
3045 if (count
>= REP_LIMIT
) {
3046 nasm_error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
3051 nasm_error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3054 free_tlist(origline
);
3056 tmp_defining
= defining
;
3057 defining
= nasm_malloc(sizeof(MMacro
));
3058 defining
->prev
= NULL
;
3059 defining
->name
= NULL
; /* flags this macro as a %rep block */
3060 defining
->casesense
= false;
3061 defining
->plus
= false;
3062 defining
->nolist
= nolist
;
3063 defining
->in_progress
= count
;
3064 defining
->max_depth
= 0;
3065 defining
->nparam_min
= defining
->nparam_max
= 0;
3066 defining
->defaults
= NULL
;
3067 defining
->dlist
= NULL
;
3068 defining
->expansion
= NULL
;
3069 defining
->next_active
= istk
->mstk
;
3070 defining
->rep_nest
= tmp_defining
;
3071 return DIRECTIVE_FOUND
;
3074 if (!defining
|| defining
->name
) {
3075 nasm_error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3076 return DIRECTIVE_FOUND
;
3080 * Now we have a "macro" defined - although it has no name
3081 * and we won't be entering it in the hash tables - we must
3082 * push a macro-end marker for it on to istk->expansion.
3083 * After that, it will take care of propagating itself (a
3084 * macro-end marker line for a macro which is really a %rep
3085 * block will cause the macro to be re-expanded, complete
3086 * with another macro-end marker to ensure the process
3087 * continues) until the whole expansion is forcibly removed
3088 * from istk->expansion by a %exitrep.
3090 l
= nasm_malloc(sizeof(Line
));
3091 l
->next
= istk
->expansion
;
3092 l
->finishes
= defining
;
3094 istk
->expansion
= l
;
3096 istk
->mstk
= defining
;
3098 lfmt
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3099 tmp_defining
= defining
;
3100 defining
= defining
->rep_nest
;
3101 free_tlist(origline
);
3102 return DIRECTIVE_FOUND
;
3106 * We must search along istk->expansion until we hit a
3107 * macro-end marker for a macro with no name. Then we set
3108 * its `in_progress' flag to 0.
3110 list_for_each(l
, istk
->expansion
)
3111 if (l
->finishes
&& !l
->finishes
->name
)
3115 l
->finishes
->in_progress
= 1;
3117 nasm_error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3118 free_tlist(origline
);
3119 return DIRECTIVE_FOUND
;
3125 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3127 tline
= tline
->next
;
3129 tline
= expand_id(tline
);
3130 if (!tline
|| (tline
->type
!= TOK_ID
&&
3131 (tline
->type
!= TOK_PREPROC_ID
||
3132 tline
->text
[1] != '$'))) {
3133 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3135 free_tlist(origline
);
3136 return DIRECTIVE_FOUND
;
3139 ctx
= get_ctx(tline
->text
, &mname
);
3141 param_start
= tline
= tline
->next
;
3144 /* Expand the macro definition now for %xdefine and %ixdefine */
3145 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3146 tline
= expand_smacro(tline
);
3148 if (tok_is_(tline
, "(")) {
3150 * This macro has parameters.
3153 tline
= tline
->next
;
3157 nasm_error(ERR_NONFATAL
, "parameter identifier expected");
3158 free_tlist(origline
);
3159 return DIRECTIVE_FOUND
;
3161 if (tline
->type
!= TOK_ID
) {
3162 nasm_error(ERR_NONFATAL
,
3163 "`%s': parameter identifier expected",
3165 free_tlist(origline
);
3166 return DIRECTIVE_FOUND
;
3168 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3169 tline
= tline
->next
;
3171 if (tok_is_(tline
, ",")) {
3172 tline
= tline
->next
;
3174 if (!tok_is_(tline
, ")")) {
3175 nasm_error(ERR_NONFATAL
,
3176 "`)' expected to terminate macro template");
3177 free_tlist(origline
);
3178 return DIRECTIVE_FOUND
;
3184 tline
= tline
->next
;
3186 if (tok_type_(tline
, TOK_WHITESPACE
))
3187 last
= tline
, tline
= tline
->next
;
3192 if (t
->type
== TOK_ID
) {
3193 list_for_each(tt
, param_start
)
3194 if (tt
->type
>= TOK_SMAC_PARAM
&&
3195 !strcmp(tt
->text
, t
->text
))
3199 t
->next
= macro_start
;
3204 * Good. We now have a macro name, a parameter count, and a
3205 * token list (in reverse order) for an expansion. We ought
3206 * to be OK just to create an SMacro, store it, and let
3207 * free_tlist have the rest of the line (which we have
3208 * carefully re-terminated after chopping off the expansion
3211 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3212 free_tlist(origline
);
3213 return DIRECTIVE_FOUND
;
3216 tline
= tline
->next
;
3218 tline
= expand_id(tline
);
3219 if (!tline
|| (tline
->type
!= TOK_ID
&&
3220 (tline
->type
!= TOK_PREPROC_ID
||
3221 tline
->text
[1] != '$'))) {
3222 nasm_error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3223 free_tlist(origline
);
3224 return DIRECTIVE_FOUND
;
3227 nasm_error(ERR_WARNING
|ERR_PASS1
,
3228 "trailing garbage after macro name ignored");
3231 /* Find the context that symbol belongs to */
3232 ctx
= get_ctx(tline
->text
, &mname
);
3233 undef_smacro(ctx
, mname
);
3234 free_tlist(origline
);
3235 return DIRECTIVE_FOUND
;
3239 casesense
= (i
== PP_DEFSTR
);
3241 tline
= tline
->next
;
3243 tline
= expand_id(tline
);
3244 if (!tline
|| (tline
->type
!= TOK_ID
&&
3245 (tline
->type
!= TOK_PREPROC_ID
||
3246 tline
->text
[1] != '$'))) {
3247 nasm_error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3249 free_tlist(origline
);
3250 return DIRECTIVE_FOUND
;
3253 ctx
= get_ctx(tline
->text
, &mname
);
3255 tline
= expand_smacro(tline
->next
);
3258 while (tok_type_(tline
, TOK_WHITESPACE
))
3259 tline
= delete_Token(tline
);
3261 p
= detoken(tline
, false);
3262 macro_start
= nasm_malloc(sizeof(*macro_start
));
3263 macro_start
->next
= NULL
;
3264 macro_start
->text
= nasm_quote(p
, strlen(p
));
3265 macro_start
->type
= TOK_STRING
;
3266 macro_start
->a
.mac
= NULL
;
3270 * We now have a macro name, an implicit parameter count of
3271 * zero, and a string token to use as an expansion. Create
3272 * and store an SMacro.
3274 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3275 free_tlist(origline
);
3276 return DIRECTIVE_FOUND
;
3280 casesense
= (i
== PP_DEFTOK
);
3282 tline
= tline
->next
;
3284 tline
= expand_id(tline
);
3285 if (!tline
|| (tline
->type
!= TOK_ID
&&
3286 (tline
->type
!= TOK_PREPROC_ID
||
3287 tline
->text
[1] != '$'))) {
3288 nasm_error(ERR_NONFATAL
,
3289 "`%s' expects a macro identifier as first parameter",
3291 free_tlist(origline
);
3292 return DIRECTIVE_FOUND
;
3294 ctx
= get_ctx(tline
->text
, &mname
);
3296 tline
= expand_smacro(tline
->next
);
3300 while (tok_type_(t
, TOK_WHITESPACE
))
3302 /* t should now point to the string */
3303 if (!tok_type_(t
, TOK_STRING
)) {
3304 nasm_error(ERR_NONFATAL
,
3305 "`%s` requires string as second parameter",
3308 free_tlist(origline
);
3309 return DIRECTIVE_FOUND
;
3313 * Convert the string to a token stream. Note that smacros
3314 * are stored with the token stream reversed, so we have to
3315 * reverse the output of tokenize().
3317 nasm_unquote_cstr(t
->text
, i
);
3318 macro_start
= reverse_tokens(tokenize(t
->text
));
3321 * We now have a macro name, an implicit parameter count of
3322 * zero, and a numeric token to use as an expansion. Create
3323 * and store an SMacro.
3325 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3327 free_tlist(origline
);
3328 return DIRECTIVE_FOUND
;
3332 const char *found_path
;
3336 tline
= tline
->next
;
3338 tline
= expand_id(tline
);
3339 if (!tline
|| (tline
->type
!= TOK_ID
&&
3340 (tline
->type
!= TOK_PREPROC_ID
||
3341 tline
->text
[1] != '$'))) {
3342 nasm_error(ERR_NONFATAL
,
3343 "`%%pathsearch' expects a macro identifier as first parameter");
3344 free_tlist(origline
);
3345 return DIRECTIVE_FOUND
;
3347 ctx
= get_ctx(tline
->text
, &mname
);
3349 tline
= expand_smacro(tline
->next
);
3353 while (tok_type_(t
, TOK_WHITESPACE
))
3356 if (!t
|| (t
->type
!= TOK_STRING
&&
3357 t
->type
!= TOK_INTERNAL_STRING
)) {
3358 nasm_error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3360 free_tlist(origline
);
3361 return DIRECTIVE_FOUND
; /* but we did _something_ */
3364 nasm_error(ERR_WARNING
|ERR_PASS1
,
3365 "trailing garbage after `%%pathsearch' ignored");
3367 if (t
->type
!= TOK_INTERNAL_STRING
)
3368 nasm_unquote(p
, NULL
);
3370 inc_fopen(p
, NULL
, &found_path
, INC_PROBE
, NF_BINARY
);
3373 macro_start
= nasm_malloc(sizeof(*macro_start
));
3374 macro_start
->next
= NULL
;
3375 macro_start
->text
= nasm_quote(found_path
, strlen(found_path
));
3376 macro_start
->type
= TOK_STRING
;
3377 macro_start
->a
.mac
= NULL
;
3380 * We now have a macro name, an implicit parameter count of
3381 * zero, and a string token to use as an expansion. Create
3382 * and store an SMacro.
3384 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3386 free_tlist(origline
);
3387 return DIRECTIVE_FOUND
;
3393 tline
= tline
->next
;
3395 tline
= expand_id(tline
);
3396 if (!tline
|| (tline
->type
!= TOK_ID
&&
3397 (tline
->type
!= TOK_PREPROC_ID
||
3398 tline
->text
[1] != '$'))) {
3399 nasm_error(ERR_NONFATAL
,
3400 "`%%strlen' expects a macro identifier as first parameter");
3401 free_tlist(origline
);
3402 return DIRECTIVE_FOUND
;
3404 ctx
= get_ctx(tline
->text
, &mname
);
3406 tline
= expand_smacro(tline
->next
);
3410 while (tok_type_(t
, TOK_WHITESPACE
))
3412 /* t should now point to the string */
3413 if (!tok_type_(t
, TOK_STRING
)) {
3414 nasm_error(ERR_NONFATAL
,
3415 "`%%strlen` requires string as second parameter");
3417 free_tlist(origline
);
3418 return DIRECTIVE_FOUND
;
3421 macro_start
= nasm_malloc(sizeof(*macro_start
));
3422 macro_start
->next
= NULL
;
3423 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3424 macro_start
->a
.mac
= NULL
;
3427 * We now have a macro name, an implicit parameter count of
3428 * zero, and a numeric token to use as an expansion. Create
3429 * and store an SMacro.
3431 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3433 free_tlist(origline
);
3434 return DIRECTIVE_FOUND
;
3439 tline
= tline
->next
;
3441 tline
= expand_id(tline
);
3442 if (!tline
|| (tline
->type
!= TOK_ID
&&
3443 (tline
->type
!= TOK_PREPROC_ID
||
3444 tline
->text
[1] != '$'))) {
3445 nasm_error(ERR_NONFATAL
,
3446 "`%%strcat' expects a macro identifier as first parameter");
3447 free_tlist(origline
);
3448 return DIRECTIVE_FOUND
;
3450 ctx
= get_ctx(tline
->text
, &mname
);
3452 tline
= expand_smacro(tline
->next
);
3456 list_for_each(t
, tline
) {
3458 case TOK_WHITESPACE
:
3461 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3464 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3466 /* else fall through */
3468 nasm_error(ERR_NONFATAL
,
3469 "non-string passed to `%%strcat' (%d)", t
->type
);
3471 free_tlist(origline
);
3472 return DIRECTIVE_FOUND
;
3476 p
= pp
= nasm_malloc(len
);
3477 list_for_each(t
, tline
) {
3478 if (t
->type
== TOK_STRING
) {
3479 memcpy(p
, t
->text
, t
->a
.len
);
3485 * We now have a macro name, an implicit parameter count of
3486 * zero, and a numeric token to use as an expansion. Create
3487 * and store an SMacro.
3489 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3490 macro_start
->text
= nasm_quote(pp
, len
);
3492 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3494 free_tlist(origline
);
3495 return DIRECTIVE_FOUND
;
3499 int64_t start
, count
;
3504 tline
= tline
->next
;
3506 tline
= expand_id(tline
);
3507 if (!tline
|| (tline
->type
!= TOK_ID
&&
3508 (tline
->type
!= TOK_PREPROC_ID
||
3509 tline
->text
[1] != '$'))) {
3510 nasm_error(ERR_NONFATAL
,
3511 "`%%substr' expects a macro identifier as first parameter");
3512 free_tlist(origline
);
3513 return DIRECTIVE_FOUND
;
3515 ctx
= get_ctx(tline
->text
, &mname
);
3517 tline
= expand_smacro(tline
->next
);
3520 if (tline
) /* skip expanded id */
3522 while (tok_type_(t
, TOK_WHITESPACE
))
3525 /* t should now point to the string */
3526 if (!tok_type_(t
, TOK_STRING
)) {
3527 nasm_error(ERR_NONFATAL
,
3528 "`%%substr` requires string as second parameter");
3530 free_tlist(origline
);
3531 return DIRECTIVE_FOUND
;
3536 tokval
.t_type
= TOKEN_INVALID
;
3537 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3540 free_tlist(origline
);
3541 return DIRECTIVE_FOUND
;
3542 } else if (!is_simple(evalresult
)) {
3543 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3545 free_tlist(origline
);
3546 return DIRECTIVE_FOUND
;
3548 start
= evalresult
->value
- 1;
3550 while (tok_type_(tt
, TOK_WHITESPACE
))
3553 count
= 1; /* Backwards compatibility: one character */
3555 tokval
.t_type
= TOKEN_INVALID
;
3556 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3559 free_tlist(origline
);
3560 return DIRECTIVE_FOUND
;
3561 } else if (!is_simple(evalresult
)) {
3562 nasm_error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3564 free_tlist(origline
);
3565 return DIRECTIVE_FOUND
;
3567 count
= evalresult
->value
;
3570 len
= nasm_unquote(t
->text
, NULL
);
3572 /* make start and count being in range */
3576 count
= len
+ count
+ 1 - start
;
3577 if (start
+ count
> (int64_t)len
)
3578 count
= len
- start
;
3579 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3580 start
= -1, count
= 0; /* empty string */
3582 macro_start
= nasm_malloc(sizeof(*macro_start
));
3583 macro_start
->next
= NULL
;
3584 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3585 macro_start
->type
= TOK_STRING
;
3586 macro_start
->a
.mac
= NULL
;
3589 * We now have a macro name, an implicit parameter count of
3590 * zero, and a numeric token to use as an expansion. Create
3591 * and store an SMacro.
3593 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3595 free_tlist(origline
);
3596 return DIRECTIVE_FOUND
;
3601 casesense
= (i
== PP_ASSIGN
);
3603 tline
= tline
->next
;
3605 tline
= expand_id(tline
);
3606 if (!tline
|| (tline
->type
!= TOK_ID
&&
3607 (tline
->type
!= TOK_PREPROC_ID
||
3608 tline
->text
[1] != '$'))) {
3609 nasm_error(ERR_NONFATAL
,
3610 "`%%%sassign' expects a macro identifier",
3611 (i
== PP_IASSIGN
? "i" : ""));
3612 free_tlist(origline
);
3613 return DIRECTIVE_FOUND
;
3615 ctx
= get_ctx(tline
->text
, &mname
);
3617 tline
= expand_smacro(tline
->next
);
3622 tokval
.t_type
= TOKEN_INVALID
;
3623 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, NULL
);
3626 free_tlist(origline
);
3627 return DIRECTIVE_FOUND
;
3631 nasm_error(ERR_WARNING
|ERR_PASS1
,
3632 "trailing garbage after expression ignored");
3634 if (!is_simple(evalresult
)) {
3635 nasm_error(ERR_NONFATAL
,
3636 "non-constant value given to `%%%sassign'",
3637 (i
== PP_IASSIGN
? "i" : ""));
3638 free_tlist(origline
);
3639 return DIRECTIVE_FOUND
;
3642 macro_start
= nasm_malloc(sizeof(*macro_start
));
3643 macro_start
->next
= NULL
;
3644 make_tok_num(macro_start
, reloc_value(evalresult
));
3645 macro_start
->a
.mac
= NULL
;
3648 * We now have a macro name, an implicit parameter count of
3649 * zero, and a numeric token to use as an expansion. Create
3650 * and store an SMacro.
3652 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3653 free_tlist(origline
);
3654 return DIRECTIVE_FOUND
;
3658 * Syntax is `%line nnn[+mmm] [filename]'
3660 tline
= tline
->next
;
3662 if (!tok_type_(tline
, TOK_NUMBER
)) {
3663 nasm_error(ERR_NONFATAL
, "`%%line' expects line number");
3664 free_tlist(origline
);
3665 return DIRECTIVE_FOUND
;
3667 k
= readnum(tline
->text
, &err
);
3669 tline
= tline
->next
;
3670 if (tok_is_(tline
, "+")) {
3671 tline
= tline
->next
;
3672 if (!tok_type_(tline
, TOK_NUMBER
)) {
3673 nasm_error(ERR_NONFATAL
, "`%%line' expects line increment");
3674 free_tlist(origline
);
3675 return DIRECTIVE_FOUND
;
3677 m
= readnum(tline
->text
, &err
);
3678 tline
= tline
->next
;
3684 char *fname
= detoken(tline
, false);
3685 src_set_fname(fname
);
3688 free_tlist(origline
);
3689 return DIRECTIVE_FOUND
;
3692 nasm_error(ERR_FATAL
,
3693 "preprocessor directive `%s' not yet implemented",
3695 return DIRECTIVE_FOUND
;
3700 * Ensure that a macro parameter contains a condition code and
3701 * nothing else. Return the condition code index if so, or -1
3704 static int find_cc(Token
* t
)
3709 return -1; /* Probably a %+ without a space */
3712 if (t
->type
!= TOK_ID
)
3716 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3719 return bsii(t
->text
, (const char **)conditions
, ARRAY_SIZE(conditions
));
3723 * This routines walks over tokens strem and hadnles tokens
3724 * pasting, if @handle_explicit passed then explicit pasting
3725 * term is handled, otherwise -- implicit pastings only.
3727 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3728 size_t mnum
, bool handle_explicit
)
3730 Token
*tok
, *next
, **prev_next
, **prev_nonspace
;
3731 bool pasted
= false;
3736 * The last token before pasting. We need it
3737 * to be able to connect new handled tokens.
3738 * In other words if there were a tokens stream
3742 * and we've joined tokens B and C, the resulting
3750 if (!tok_type_(tok
, TOK_WHITESPACE
) && !tok_type_(tok
, TOK_PASTE
))
3751 prev_nonspace
= head
;
3753 prev_nonspace
= NULL
;
3755 while (tok
&& (next
= tok
->next
)) {
3757 switch (tok
->type
) {
3758 case TOK_WHITESPACE
:
3759 /* Zap redundant whitespaces */
3760 while (tok_type_(next
, TOK_WHITESPACE
))
3761 next
= delete_Token(next
);
3766 /* Explicit pasting */
3767 if (!handle_explicit
)
3769 next
= delete_Token(tok
);
3771 while (tok_type_(next
, TOK_WHITESPACE
))
3772 next
= delete_Token(next
);
3777 /* Left pasting token is start of line */
3779 nasm_error(ERR_FATAL
, "No lvalue found on pasting");
3782 * No ending token, this might happen in two
3785 * 1) There indeed no right token at all
3786 * 2) There is a bare "%define ID" statement,
3787 * and @ID does expand to whitespace.
3789 * So technically we need to do a grammar analysis
3790 * in another stage of parsing, but for now lets don't
3791 * change the behaviour people used to. Simply allow
3792 * whitespace after paste token.
3796 * Zap ending space tokens and that's all.
3798 tok
= (*prev_nonspace
)->next
;
3799 while (tok_type_(tok
, TOK_WHITESPACE
))
3800 tok
= delete_Token(tok
);
3801 tok
= *prev_nonspace
;
3806 tok
= *prev_nonspace
;
3807 while (tok_type_(tok
, TOK_WHITESPACE
))
3808 tok
= delete_Token(tok
);
3809 len
= strlen(tok
->text
);
3810 len
+= strlen(next
->text
);
3812 p
= buf
= nasm_malloc(len
+ 1);
3813 strcpy(p
, tok
->text
);
3814 p
= strchr(p
, '\0');
3815 strcpy(p
, next
->text
);
3819 tok
= tokenize(buf
);
3822 *prev_nonspace
= tok
;
3823 while (tok
&& tok
->next
)
3826 tok
->next
= delete_Token(next
);
3828 /* Restart from pasted tokens head */
3829 tok
= *prev_nonspace
;
3833 /* implicit pasting */
3834 for (i
= 0; i
< mnum
; i
++) {
3835 if (!(PP_CONCAT_MATCH(tok
, m
[i
].mask_head
)))
3839 while (next
&& PP_CONCAT_MATCH(next
, m
[i
].mask_tail
)) {
3840 len
+= strlen(next
->text
);
3848 len
+= strlen(tok
->text
);
3849 p
= buf
= nasm_malloc(len
+ 1);
3851 strcpy(p
, tok
->text
);
3852 p
= strchr(p
, '\0');
3853 tok
= delete_Token(tok
);
3855 while (tok
!= next
) {
3856 if (PP_CONCAT_MATCH(tok
, m
[i
].mask_tail
)) {
3857 strcpy(p
, tok
->text
);
3858 p
= strchr(p
, '\0');
3860 tok
= delete_Token(tok
);
3863 tok
= tokenize(buf
);
3872 * Connect pasted into original stream,
3873 * ie A -> new-tokens -> B
3875 while (tok
&& tok
->next
)
3882 /* Restart from pasted tokens head */
3883 tok
= prev_next
? *prev_next
: *head
;
3889 prev_next
= &tok
->next
;
3892 !tok_type_(tok
->next
, TOK_WHITESPACE
) &&
3893 !tok_type_(tok
->next
, TOK_PASTE
))
3894 prev_nonspace
= prev_next
;
3903 * expands to a list of tokens from %{x:y}
3905 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3907 Token
*t
= tline
, **tt
, *tm
, *head
;
3911 pos
= strchr(tline
->text
, ':');
3914 lst
= atoi(pos
+ 1);
3915 fst
= atoi(tline
->text
+ 1);
3918 * only macros params are accounted so
3919 * if someone passes %0 -- we reject such
3922 if (lst
== 0 || fst
== 0)
3925 /* the values should be sane */
3926 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3927 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3930 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3931 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3933 /* counted from zero */
3937 * It will be at least one token. Note we
3938 * need to scan params until separator, otherwise
3939 * only first token will be passed.
3941 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3942 head
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3943 tt
= &head
->next
, tm
= tm
->next
;
3944 while (tok_isnt_(tm
, ",")) {
3945 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3946 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3950 for (i
= fst
+ 1; i
<= lst
; i
++) {
3951 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3952 *tt
= t
, tt
= &t
->next
;
3953 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3954 tm
= mac
->params
[j
];
3955 while (tok_isnt_(tm
, ",")) {
3956 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3957 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3961 for (i
= fst
- 1; i
>= lst
; i
--) {
3962 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3963 *tt
= t
, tt
= &t
->next
;
3964 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3965 tm
= mac
->params
[j
];
3966 while (tok_isnt_(tm
, ",")) {
3967 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3968 *tt
= t
, tt
= &t
->next
, tm
= tm
->next
;
3977 nasm_error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3983 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3984 * %-n) and MMacro-local identifiers (%%foo) as well as
3985 * macro indirection (%[...]) and range (%{..:..}).
3987 static Token
*expand_mmac_params(Token
* tline
)
3989 Token
*t
, *tt
, **tail
, *thead
;
3990 bool changed
= false;
3997 if (tline
->type
== TOK_PREPROC_ID
&&
3998 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3999 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4000 tline
->text
[1] == '%')) {
4002 int type
= 0, cc
; /* type = 0 to placate optimisers */
4009 tline
= tline
->next
;
4012 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
4013 mac
= mac
->next_active
;
4015 nasm_error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4017 pos
= strchr(t
->text
, ':');
4019 switch (t
->text
[1]) {
4021 * We have to make a substitution of one of the
4022 * forms %1, %-1, %+1, %%foo, %0.
4026 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
4027 text
= nasm_strdup(tmpbuf
);
4031 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4033 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4036 n
= atoi(t
->text
+ 2) - 1;
4037 if (n
>= mac
->nparam
)
4040 if (mac
->nparam
> 1)
4041 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4042 tt
= mac
->params
[n
];
4046 nasm_error(ERR_NONFATAL
,
4047 "macro parameter %d is not a condition code",
4052 if (inverse_ccs
[cc
] == -1) {
4053 nasm_error(ERR_NONFATAL
,
4054 "condition code `%s' is not invertible",
4058 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4062 n
= atoi(t
->text
+ 2) - 1;
4063 if (n
>= mac
->nparam
)
4066 if (mac
->nparam
> 1)
4067 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4068 tt
= mac
->params
[n
];
4072 nasm_error(ERR_NONFATAL
,
4073 "macro parameter %d is not a condition code",
4078 text
= nasm_strdup(conditions
[cc
]);
4082 n
= atoi(t
->text
+ 1) - 1;
4083 if (n
>= mac
->nparam
)
4086 if (mac
->nparam
> 1)
4087 n
= (n
+ mac
->rotate
) % mac
->nparam
;
4088 tt
= mac
->params
[n
];
4091 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
4092 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4093 tail
= &(*tail
)->next
;
4097 text
= NULL
; /* we've done it here */
4102 * seems we have a parameters range here
4104 Token
*head
, **last
;
4105 head
= expand_mmac_params_range(mac
, t
, &last
);
4126 } else if (tline
->type
== TOK_INDIRECT
) {
4128 tline
= tline
->next
;
4129 tt
= tokenize(t
->text
);
4130 tt
= expand_mmac_params(tt
);
4131 tt
= expand_smacro(tt
);
4134 tt
->a
.mac
= NULL
; /* Necessary? */
4142 tline
= tline
->next
;
4150 const struct tokseq_match t
[] = {
4152 PP_CONCAT_MASK(TOK_ID
) |
4153 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4154 PP_CONCAT_MASK(TOK_ID
) |
4155 PP_CONCAT_MASK(TOK_NUMBER
) |
4156 PP_CONCAT_MASK(TOK_FLOAT
) |
4157 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4160 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4161 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4164 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4171 * Expand all single-line macro calls made in the given line.
4172 * Return the expanded version of the line. The original is deemed
4173 * to be destroyed in the process. (In reality we'll just move
4174 * Tokens from input to output a lot of the time, rather than
4175 * actually bothering to destroy and replicate.)
4178 static Token
*expand_smacro(Token
* tline
)
4180 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4181 SMacro
*head
= NULL
, *m
;
4184 unsigned int nparam
, sparam
;
4186 Token
*org_tline
= tline
;
4189 int deadman
= DEADMAN_LIMIT
;
4193 * Trick: we should avoid changing the start token pointer since it can
4194 * be contained in "next" field of other token. Because of this
4195 * we allocate a copy of first token and work with it; at the end of
4196 * routine we copy it back
4199 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4200 org_tline
->text
, 0);
4201 tline
->a
.mac
= org_tline
->a
.mac
;
4202 nasm_free(org_tline
->text
);
4203 org_tline
->text
= NULL
;
4206 expanded
= true; /* Always expand %+ at least once */
4212 while (tline
) { /* main token loop */
4214 nasm_error(ERR_NONFATAL
, "interminable macro recursion");
4218 if ((mname
= tline
->text
)) {
4219 /* if this token is a local macro, look in local context */
4220 if (tline
->type
== TOK_ID
) {
4221 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4222 } else if (tline
->type
== TOK_PREPROC_ID
) {
4223 ctx
= get_ctx(mname
, &mname
);
4224 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4229 * We've hit an identifier. As in is_mmacro below, we first
4230 * check whether the identifier is a single-line macro at
4231 * all, then think about checking for parameters if
4234 list_for_each(m
, head
)
4235 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4241 if (m
->nparam
== 0) {
4243 * Simple case: the macro is parameterless. Discard the
4244 * one token that the macro call took, and push the
4245 * expansion back on the to-do stack.
4247 if (!m
->expansion
) {
4248 if (!strcmp("__FILE__", m
->name
)) {
4249 const char *file
= src_get_fname();
4250 /* nasm_free(tline->text); here? */
4251 tline
->text
= nasm_quote(file
, strlen(file
));
4252 tline
->type
= TOK_STRING
;
4255 if (!strcmp("__LINE__", m
->name
)) {
4256 nasm_free(tline
->text
);
4257 make_tok_num(tline
, src_get_linnum());
4260 if (!strcmp("__BITS__", m
->name
)) {
4261 nasm_free(tline
->text
);
4262 make_tok_num(tline
, globalbits
);
4265 tline
= delete_Token(tline
);
4270 * Complicated case: at least one macro with this name
4271 * exists and takes parameters. We must find the
4272 * parameters in the call, count them, find the SMacro
4273 * that corresponds to that form of the macro call, and
4274 * substitute for the parameters when we expand. What a
4277 /*tline = tline->next;
4278 skip_white_(tline); */
4281 while (tok_type_(t
, TOK_SMAC_END
)) {
4282 t
->a
.mac
->in_progress
= false;
4284 t
= tline
->next
= delete_Token(t
);
4287 } while (tok_type_(tline
, TOK_WHITESPACE
));
4288 if (!tok_is_(tline
, "(")) {
4290 * This macro wasn't called with parameters: ignore
4291 * the call. (Behaviour borrowed from gnu cpp.)
4300 sparam
= PARAM_DELTA
;
4301 params
= nasm_malloc(sparam
* sizeof(Token
*));
4302 params
[0] = tline
->next
;
4303 paramsize
= nasm_malloc(sparam
* sizeof(int));
4305 while (true) { /* parameter loop */
4307 * For some unusual expansions
4308 * which concatenates function call
4311 while (tok_type_(t
, TOK_SMAC_END
)) {
4312 t
->a
.mac
->in_progress
= false;
4314 t
= tline
->next
= delete_Token(t
);
4319 nasm_error(ERR_NONFATAL
,
4320 "macro call expects terminating `)'");
4323 if (tline
->type
== TOK_WHITESPACE
4325 if (paramsize
[nparam
])
4328 params
[nparam
] = tline
->next
;
4329 continue; /* parameter loop */
4331 if (tline
->type
== TOK_OTHER
4332 && tline
->text
[1] == 0) {
4333 char ch
= tline
->text
[0];
4334 if (ch
== ',' && !paren
&& brackets
<= 0) {
4335 if (++nparam
>= sparam
) {
4336 sparam
+= PARAM_DELTA
;
4337 params
= nasm_realloc(params
,
4338 sparam
* sizeof(Token
*));
4339 paramsize
= nasm_realloc(paramsize
,
4340 sparam
* sizeof(int));
4342 params
[nparam
] = tline
->next
;
4343 paramsize
[nparam
] = 0;
4345 continue; /* parameter loop */
4348 (brackets
> 0 || (brackets
== 0 &&
4349 !paramsize
[nparam
])))
4351 if (!(brackets
++)) {
4352 params
[nparam
] = tline
->next
;
4353 continue; /* parameter loop */
4356 if (ch
== '}' && brackets
> 0)
4357 if (--brackets
== 0) {
4359 continue; /* parameter loop */
4361 if (ch
== '(' && !brackets
)
4363 if (ch
== ')' && brackets
<= 0)
4369 nasm_error(ERR_NONFATAL
, "braces do not "
4370 "enclose all of macro parameter");
4372 paramsize
[nparam
] += white
+ 1;
4374 } /* parameter loop */
4376 while (m
&& (m
->nparam
!= nparam
||
4377 mstrcmp(m
->name
, mname
,
4381 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4382 "macro `%s' exists, "
4383 "but not taking %d parameters",
4384 mstart
->text
, nparam
);
4387 if (m
&& m
->in_progress
)
4389 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4391 * Design question: should we handle !tline, which
4392 * indicates missing ')' here, or expand those
4393 * macros anyway, which requires the (t) test a few
4397 nasm_free(paramsize
);
4401 * Expand the macro: we are placed on the last token of the
4402 * call, so that we can easily split the call from the
4403 * following tokens. We also start by pushing an SMAC_END
4404 * token for the cycle removal.
4411 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4413 m
->in_progress
= true;
4415 list_for_each(t
, m
->expansion
) {
4416 if (t
->type
>= TOK_SMAC_PARAM
) {
4417 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4421 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4422 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4424 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4430 } else if (t
->type
== TOK_PREPROC_Q
) {
4431 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4433 } else if (t
->type
== TOK_PREPROC_QQ
) {
4434 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4437 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4443 * Having done that, get rid of the macro call, and clean
4444 * up the parameters.
4447 nasm_free(paramsize
);
4450 continue; /* main token loop */
4455 if (tline
->type
== TOK_SMAC_END
) {
4456 tline
->a
.mac
->in_progress
= false;
4457 tline
= delete_Token(tline
);
4460 tline
= tline
->next
;
4468 * Now scan the entire line and look for successive TOK_IDs that resulted
4469 * after expansion (they can't be produced by tokenize()). The successive
4470 * TOK_IDs should be concatenated.
4471 * Also we look for %+ tokens and concatenate the tokens before and after
4472 * them (without white spaces in between).
4475 const struct tokseq_match t
[] = {
4477 PP_CONCAT_MASK(TOK_ID
) |
4478 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4479 PP_CONCAT_MASK(TOK_ID
) |
4480 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4481 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4484 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4486 * If we concatenated something, *and* we had previously expanded
4487 * an actual macro, scan the lines again for macros...
4498 *org_tline
= *thead
;
4499 /* since we just gave text to org_line, don't free it */
4501 delete_Token(thead
);
4503 /* the expression expanded to empty line;
4504 we can't return NULL for some reasons
4505 we just set the line to a single WHITESPACE token. */
4506 memset(org_tline
, 0, sizeof(*org_tline
));
4507 org_tline
->text
= NULL
;
4508 org_tline
->type
= TOK_WHITESPACE
;
4517 * Similar to expand_smacro but used exclusively with macro identifiers
4518 * right before they are fetched in. The reason is that there can be
4519 * identifiers consisting of several subparts. We consider that if there
4520 * are more than one element forming the name, user wants a expansion,
4521 * otherwise it will be left as-is. Example:
4525 * the identifier %$abc will be left as-is so that the handler for %define
4526 * will suck it and define the corresponding value. Other case:
4528 * %define _%$abc cde
4530 * In this case user wants name to be expanded *before* %define starts
4531 * working, so we'll expand %$abc into something (if it has a value;
4532 * otherwise it will be left as-is) then concatenate all successive
4535 static Token
*expand_id(Token
* tline
)
4537 Token
*cur
, *oldnext
= NULL
;
4539 if (!tline
|| !tline
->next
)
4544 (cur
->next
->type
== TOK_ID
||
4545 cur
->next
->type
== TOK_PREPROC_ID
4546 || cur
->next
->type
== TOK_NUMBER
))
4549 /* If identifier consists of just one token, don't expand */
4554 oldnext
= cur
->next
; /* Detach the tail past identifier */
4555 cur
->next
= NULL
; /* so that expand_smacro stops here */
4558 tline
= expand_smacro(tline
);
4561 /* expand_smacro possibly changhed tline; re-scan for EOL */
4563 while (cur
&& cur
->next
)
4566 cur
->next
= oldnext
;
4573 * Determine whether the given line constitutes a multi-line macro
4574 * call, and return the MMacro structure called if so. Doesn't have
4575 * to check for an initial label - that's taken care of in
4576 * expand_mmacro - but must check numbers of parameters. Guaranteed
4577 * to be called with tline->type == TOK_ID, so the putative macro
4578 * name is easy to find.
4580 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4586 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4589 * Efficiency: first we see if any macro exists with the given
4590 * name. If not, we can return NULL immediately. _Then_ we
4591 * count the parameters, and then we look further along the
4592 * list if necessary to find the proper MMacro.
4594 list_for_each(m
, head
)
4595 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4601 * OK, we have a potential macro. Count and demarcate the
4604 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4607 * So we know how many parameters we've got. Find the MMacro
4608 * structure that handles this number.
4611 if (m
->nparam_min
<= nparam
4612 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4614 * This one is right. Just check if cycle removal
4615 * prohibits us using it before we actually celebrate...
4617 if (m
->in_progress
> m
->max_depth
) {
4618 if (m
->max_depth
> 0) {
4619 nasm_error(ERR_WARNING
,
4620 "reached maximum recursion depth of %i",
4627 * It's right, and we can use it. Add its default
4628 * parameters to the end of our list if necessary.
4630 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4632 nasm_realloc(params
,
4633 ((m
->nparam_min
+ m
->ndefs
+
4634 1) * sizeof(*params
)));
4635 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4636 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4641 * If we've gone over the maximum parameter count (and
4642 * we're in Plus mode), ignore parameters beyond
4645 if (m
->plus
&& nparam
> m
->nparam_max
)
4646 nparam
= m
->nparam_max
;
4648 * Then terminate the parameter list, and leave.
4650 if (!params
) { /* need this special case */
4651 params
= nasm_malloc(sizeof(*params
));
4654 params
[nparam
] = NULL
;
4655 *params_array
= params
;
4659 * This one wasn't right: look for the next one with the
4662 list_for_each(m
, m
->next
)
4663 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4668 * After all that, we didn't find one with the right number of
4669 * parameters. Issue a warning, and fail to expand the macro.
4671 nasm_error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4672 "macro `%s' exists, but not taking %d parameters",
4673 tline
->text
, nparam
);
4680 * Save MMacro invocation specific fields in
4681 * preparation for a recursive macro expansion
4683 static void push_mmacro(MMacro
*m
)
4685 MMacroInvocation
*i
;
4687 i
= nasm_malloc(sizeof(MMacroInvocation
));
4689 i
->params
= m
->params
;
4690 i
->iline
= m
->iline
;
4691 i
->nparam
= m
->nparam
;
4692 i
->rotate
= m
->rotate
;
4693 i
->paramlen
= m
->paramlen
;
4694 i
->unique
= m
->unique
;
4695 i
->condcnt
= m
->condcnt
;
4701 * Restore MMacro invocation specific fields that were
4702 * saved during a previous recursive macro expansion
4704 static void pop_mmacro(MMacro
*m
)
4706 MMacroInvocation
*i
;
4711 m
->params
= i
->params
;
4712 m
->iline
= i
->iline
;
4713 m
->nparam
= i
->nparam
;
4714 m
->rotate
= i
->rotate
;
4715 m
->paramlen
= i
->paramlen
;
4716 m
->unique
= i
->unique
;
4717 m
->condcnt
= i
->condcnt
;
4724 * Expand the multi-line macro call made by the given line, if
4725 * there is one to be expanded. If there is, push the expansion on
4726 * istk->expansion and return 1. Otherwise return 0.
4728 static int expand_mmacro(Token
* tline
)
4730 Token
*startline
= tline
;
4731 Token
*label
= NULL
;
4732 int dont_prepend
= 0;
4733 Token
**params
, *t
, *tt
;
4736 int i
, nparam
, *paramlen
;
4741 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4742 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4744 m
= is_mmacro(t
, ¶ms
);
4750 * We have an id which isn't a macro call. We'll assume
4751 * it might be a label; we'll also check to see if a
4752 * colon follows it. Then, if there's another id after
4753 * that lot, we'll check it again for macro-hood.
4757 if (tok_type_(t
, TOK_WHITESPACE
))
4758 last
= t
, t
= t
->next
;
4759 if (tok_is_(t
, ":")) {
4761 last
= t
, t
= t
->next
;
4762 if (tok_type_(t
, TOK_WHITESPACE
))
4763 last
= t
, t
= t
->next
;
4765 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4773 * Fix up the parameters: this involves stripping leading and
4774 * trailing whitespace, then stripping braces if they are
4777 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4778 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4780 for (i
= 0; params
[i
]; i
++) {
4782 int comma
= (!m
->plus
|| i
< nparam
- 1);
4786 if (tok_is_(t
, "{"))
4787 t
= t
->next
, brace
++, comma
= false;
4791 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4792 break; /* ... because we have hit a comma */
4793 if (comma
&& t
->type
== TOK_WHITESPACE
4794 && tok_is_(t
->next
, ","))
4795 break; /* ... or a space then a comma */
4796 if (brace
&& t
->type
== TOK_OTHER
) {
4797 if (t
->text
[0] == '{')
4798 brace
++; /* ... or a nested opening brace */
4799 else if (t
->text
[0] == '}')
4801 break; /* ... or a brace */
4807 nasm_error(ERR_NONFATAL
, "macro params should be enclosed in braces");
4811 * OK, we have a MMacro structure together with a set of
4812 * parameters. We must now go through the expansion and push
4813 * copies of each Line on to istk->expansion. Substitution of
4814 * parameter tokens and macro-local tokens doesn't get done
4815 * until the single-line macro substitution process; this is
4816 * because delaying them allows us to change the semantics
4817 * later through %rotate.
4819 * First, push an end marker on to istk->expansion, mark this
4820 * macro as in progress, and set up its invocation-specific
4823 ll
= nasm_malloc(sizeof(Line
));
4824 ll
->next
= istk
->expansion
;
4827 istk
->expansion
= ll
;
4830 * Save the previous MMacro expansion in the case of
4833 if (m
->max_depth
&& m
->in_progress
)
4841 m
->paramlen
= paramlen
;
4842 m
->unique
= unique
++;
4846 m
->next_active
= istk
->mstk
;
4849 list_for_each(l
, m
->expansion
) {
4852 ll
= nasm_malloc(sizeof(Line
));
4853 ll
->finishes
= NULL
;
4854 ll
->next
= istk
->expansion
;
4855 istk
->expansion
= ll
;
4858 list_for_each(t
, l
->first
) {
4862 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4864 case TOK_PREPROC_QQ
:
4865 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4867 case TOK_PREPROC_ID
:
4868 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4876 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4885 * If we had a label, push it on as the first line of
4886 * the macro expansion.
4889 if (dont_prepend
< 0)
4890 free_tlist(startline
);
4892 ll
= nasm_malloc(sizeof(Line
));
4893 ll
->finishes
= NULL
;
4894 ll
->next
= istk
->expansion
;
4895 istk
->expansion
= ll
;
4896 ll
->first
= startline
;
4897 if (!dont_prepend
) {
4899 label
= label
->next
;
4900 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4905 lfmt
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4911 * This function adds macro names to error messages, and suppresses
4912 * them if necessary.
4914 static void pp_verror(int severity
, const char *fmt
, va_list arg
)
4917 MMacro
*mmac
= NULL
;
4921 * If we're in a dead branch of IF or something like it, ignore the error.
4922 * However, because %else etc are evaluated in the state context
4923 * of the previous branch, errors might get lost:
4924 * %if 0 ... %else trailing garbage ... %endif
4925 * So %else etc should set the ERR_PP_PRECOND flag.
4927 if ((severity
& ERR_MASK
) < ERR_FATAL
&&
4928 istk
&& istk
->conds
&&
4929 ((severity
& ERR_PP_PRECOND
) ?
4930 istk
->conds
->state
== COND_NEVER
:
4931 !emitting(istk
->conds
->state
)))
4934 /* get %macro name */
4935 if (!(severity
& ERR_NOFILE
) && istk
&& istk
->mstk
) {
4937 /* but %rep blocks should be skipped */
4938 while (mmac
&& !mmac
->name
)
4939 mmac
= mmac
->next_active
, delta
++;
4943 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4945 nasm_set_verror(real_verror
);
4946 nasm_error(severity
, "(%s:%d) %s",
4947 mmac
->name
, mmac
->lineno
- delta
, buff
);
4948 nasm_set_verror(pp_verror
);
4950 real_verror(severity
, fmt
, arg
);
4955 pp_reset(char *file
, int apass
, StrList
**deplist
)
4960 istk
= nasm_malloc(sizeof(Include
));
4963 istk
->expansion
= NULL
;
4965 istk
->fp
= nasm_open_read(file
, NF_TEXT
);
4970 nasm_fatal(ERR_NOFILE
, "unable to open input file `%s'", file
);
4972 nested_mac_count
= 0;
4973 nested_rep_count
= 0;
4977 if (tasm_compatible_mode
)
4978 pp_add_stdmac(nasm_stdmac_tasm
);
4980 pp_add_stdmac(nasm_stdmac_nasm
);
4981 pp_add_stdmac(nasm_stdmac_version
);
4984 pp_add_stdmac(extrastdmac
);
4986 stdmacpos
= stdmacros
[0];
4987 stdmacnext
= &stdmacros
[1];
4992 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4993 * The caller, however, will also pass in 3 for preprocess-only so
4994 * we can set __PASS__ accordingly.
4996 pass
= apass
> 2 ? 2 : apass
;
4999 nasm_add_string_to_strlist(dephead
, file
);
5002 * Define the __PASS__ macro. This is defined here unlike
5003 * all the other builtins, because it is special -- it varies between
5006 t
= nasm_malloc(sizeof(*t
));
5008 make_tok_num(t
, apass
);
5010 define_smacro(NULL
, "__PASS__", true, 0, t
);
5013 static void pp_init(void)
5015 hash_init(&FileHash
, HASH_MEDIUM
);
5018 static char *pp_getline(void)
5023 real_verror
= nasm_set_verror(pp_verror
);
5027 * Fetch a tokenized line, either from the macro-expansion
5028 * buffer or from the input file.
5031 while (istk
->expansion
&& istk
->expansion
->finishes
) {
5032 Line
*l
= istk
->expansion
;
5033 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
5037 * This is a macro-end marker for a macro with no
5038 * name, which means it's not really a macro at all
5039 * but a %rep block, and the `in_progress' field is
5040 * more than 1, meaning that we still need to
5041 * repeat. (1 means the natural last repetition; 0
5042 * means termination by %exitrep.) We have
5043 * therefore expanded up to the %endrep, and must
5044 * push the whole block on to the expansion buffer
5045 * again. We don't bother to remove the macro-end
5046 * marker: we'd only have to generate another one
5049 l
->finishes
->in_progress
--;
5050 list_for_each(l
, l
->finishes
->expansion
) {
5051 Token
*t
, *tt
, **tail
;
5053 ll
= nasm_malloc(sizeof(Line
));
5054 ll
->next
= istk
->expansion
;
5055 ll
->finishes
= NULL
;
5059 list_for_each(t
, l
->first
) {
5060 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
5061 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
5066 istk
->expansion
= ll
;
5070 * Check whether a `%rep' was started and not ended
5071 * within this macro expansion. This can happen and
5072 * should be detected. It's a fatal error because
5073 * I'm too confused to work out how to recover
5078 nasm_panic(0, "defining with name in expansion");
5079 else if (istk
->mstk
->name
)
5080 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5081 " expansion of macro `%s'",
5086 * FIXME: investigate the relationship at this point between
5087 * istk->mstk and l->finishes
5090 MMacro
*m
= istk
->mstk
;
5091 istk
->mstk
= m
->next_active
;
5094 * This was a real macro call, not a %rep, and
5095 * therefore the parameter information needs to
5100 l
->finishes
->in_progress
--;
5102 nasm_free(m
->params
);
5103 free_tlist(m
->iline
);
5104 nasm_free(m
->paramlen
);
5105 l
->finishes
->in_progress
= 0;
5110 * FIXME It is incorrect to always free_mmacro here.
5111 * It leads to usage-after-free.
5113 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5120 istk
->expansion
= l
->next
;
5122 lfmt
->downlevel(LIST_MACRO
);
5125 while (1) { /* until we get a line we can use */
5127 if (istk
->expansion
) { /* from a macro expansion */
5129 Line
*l
= istk
->expansion
;
5131 istk
->mstk
->lineno
++;
5133 istk
->expansion
= l
->next
;
5135 p
= detoken(tline
, false);
5136 lfmt
->line(LIST_MACRO
, p
);
5141 if (line
) { /* from the current input file */
5142 line
= prepreproc(line
);
5143 tline
= tokenize(line
);
5148 * The current file has ended; work down the istk
5154 /* nasm_error can't be conditionally suppressed */
5156 "expected `%%endif' before end of file");
5158 /* only set line and file name if there's a next node */
5160 src_set(i
->lineno
, i
->fname
);
5162 lfmt
->downlevel(LIST_INCLUDE
);
5168 if (istk
->expansion
&& istk
->expansion
->finishes
)
5174 * We must expand MMacro parameters and MMacro-local labels
5175 * _before_ we plunge into directive processing, to cope
5176 * with things like `%define something %1' such as STRUC
5177 * uses. Unless we're _defining_ a MMacro, in which case
5178 * those tokens should be left alone to go into the
5179 * definition; and unless we're in a non-emitting
5180 * condition, in which case we don't want to meddle with
5183 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
5184 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
5185 tline
= expand_mmac_params(tline
);
5189 * Check the line to see if it's a preprocessor directive.
5191 if (do_directive(tline
, &line
) == DIRECTIVE_FOUND
) {
5193 break; /* Directive generated output */
5196 } else if (defining
) {
5198 * We're defining a multi-line macro. We emit nothing
5200 * shove the tokenized line on to the macro definition.
5202 Line
*l
= nasm_malloc(sizeof(Line
));
5203 l
->next
= defining
->expansion
;
5206 defining
->expansion
= l
;
5208 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
5210 * We're in a non-emitting branch of a condition block.
5211 * Emit nothing at all, not even a blank line: when we
5212 * emerge from the condition we'll give a line-number
5213 * directive so we keep our place correctly.
5217 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5219 * We're in a %rep block which has been terminated, so
5220 * we're walking through to the %endrep without
5221 * emitting anything. Emit nothing at all, not even a
5222 * blank line: when we emerge from the %rep block we'll
5223 * give a line-number directive so we keep our place
5229 tline
= expand_smacro(tline
);
5230 if (!expand_mmacro(tline
)) {
5232 * De-tokenize the line again, and emit it.
5234 line
= detoken(tline
, true);
5238 continue; /* expand_mmacro calls free_tlist */
5244 nasm_set_verror(real_verror
);
5248 static void pp_cleanup(int pass
)
5250 real_verror
= nasm_set_verror(pp_verror
);
5253 if (defining
->name
) {
5254 nasm_error(ERR_NONFATAL
,
5255 "end of file while still defining macro `%s'",
5258 nasm_error(ERR_NONFATAL
, "end of file while still in %%rep");
5261 free_mmacro(defining
);
5265 nasm_set_verror(real_verror
);
5278 src_set_fname(NULL
);
5285 while ((i
= ipath
)) {
5294 static void pp_include_path(char *path
)
5298 i
= nasm_malloc(sizeof(IncPath
));
5299 i
->path
= path
? nasm_strdup(path
) : NULL
;
5312 static void pp_pre_include(char *fname
)
5314 Token
*inc
, *space
, *name
;
5317 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5318 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5319 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5321 l
= nasm_malloc(sizeof(Line
));
5328 static void pp_pre_define(char *definition
)
5334 real_verror
= nasm_set_verror(pp_verror
);
5336 equals
= strchr(definition
, '=');
5337 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5338 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5341 space
->next
= tokenize(definition
);
5345 if (space
->next
->type
!= TOK_PREPROC_ID
&&
5346 space
->next
->type
!= TOK_ID
)
5347 nasm_error(ERR_WARNING
, "pre-defining non ID `%s\'\n", definition
);
5349 l
= nasm_malloc(sizeof(Line
));
5355 nasm_set_verror(real_verror
);
5358 static void pp_pre_undefine(char *definition
)
5363 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5364 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5365 space
->next
= tokenize(definition
);
5367 l
= nasm_malloc(sizeof(Line
));
5374 static void pp_add_stdmac(macros_t
*macros
)
5378 /* Find the end of the list and avoid duplicates */
5379 for (mp
= stdmacros
; *mp
; mp
++) {
5381 return; /* Nothing to do */
5384 nasm_assert(mp
< &stdmacros
[ARRAY_SIZE(stdmacros
)-1]);
5389 static void pp_extra_stdmac(macros_t
*macros
)
5391 extrastdmac
= macros
;
5394 static void make_tok_num(Token
* tok
, int64_t val
)
5397 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5398 tok
->text
= nasm_strdup(numbuf
);
5399 tok
->type
= TOK_NUMBER
;
5402 static void pp_list_one_macro(MMacro
*m
, int severity
)
5407 /* We need to print the next_active list in reverse order */
5408 pp_list_one_macro(m
->next_active
, severity
);
5410 if (m
->name
&& !m
->nolist
) {
5411 src_set(m
->xline
+ m
->lineno
, m
->fname
);
5412 nasm_error(severity
, "... from macro `%s' defined here", m
->name
);
5416 static void pp_error_list_macros(int severity
)
5419 const char *saved_fname
= NULL
;
5421 severity
|= ERR_PP_LISTMACRO
| ERR_NO_SEVERITY
;
5422 src_get(&saved_line
, &saved_fname
);
5425 pp_list_one_macro(istk
->mstk
, severity
);
5427 src_set(saved_line
, saved_fname
);
5430 const struct preproc_ops nasmpp
= {
5440 pp_error_list_macros
,