1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro
;
85 typedef struct MMacro MMacro
;
86 typedef struct MMacroInvocation MMacroInvocation
;
87 typedef struct Context Context
;
88 typedef struct Token Token
;
89 typedef struct Blocks Blocks
;
90 typedef struct Line Line
;
91 typedef struct Include Include
;
92 typedef struct Cond Cond
;
93 typedef struct IncPath IncPath
;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
135 MMacroInvocation
*prev
; /* previous invocation */
137 int nparam_min
, nparam_max
;
139 bool plus
; /* is the last parameter greedy? */
140 bool nolist
; /* is this macro listing-inhibited? */
141 int64_t in_progress
; /* is this macro currently being expanded? */
142 int32_t max_depth
; /* maximum number of recursive expansions allowed */
143 Token
*dlist
; /* All defaults as one list */
144 Token
**defaults
; /* Parameter default pointers */
145 int ndefs
; /* number of default parameters */
149 MMacro
*rep_nest
; /* used for nesting %rep */
150 Token
**params
; /* actual parameters */
151 Token
*iline
; /* invocation line */
152 unsigned int nparam
, rotate
;
155 int lineno
; /* Current line number on expansion */
156 uint64_t condcnt
; /* number of if blocks... */
160 /* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
163 struct MMacroInvocation
{
164 MMacroInvocation
*prev
; /* previous invocation */
165 Token
**params
; /* actual parameters */
166 Token
*iline
; /* invocation line */
167 unsigned int nparam
, rotate
;
175 * The context stack is composed of a linked list of these.
180 struct hash_table localmac
;
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
192 * %define a(x,y) ( (x) & ~(y) )
194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
204 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
205 TOK_PREPROC_ID
, TOK_STRING
,
206 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
208 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
210 TOK_INDIRECT
, /* %[...] */
211 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
219 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
220 size_t len
; /* scratch length field */
221 } a
; /* Auxiliary data */
222 enum pp_token_type type
;
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
264 MMacro
*mstk
; /* stack of active macros/reps */
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
297 COND_IF_TRUE
, COND_IF_FALSE
,
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
303 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
313 COND_DONE
, COND_NEVER
315 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318 * These defines are used as the possible return values for do_directive
320 #define NO_DIRECTIVE_FOUND 0
321 #define DIRECTIVE_FOUND 1
324 * This define sets the upper limit for smacro and recursive mmacro
327 #define DEADMAN_LIMIT (1 << 20)
330 #define REP_LIMIT ((INT64_C(1) << 62))
333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
338 static const char * const conditions
[] = {
339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
344 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
345 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
346 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
349 static const enum pp_conds inverse_ccs
[] = {
350 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
351 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
,
352 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
358 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
359 static int is_condition(enum preproc_token arg
)
361 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
364 /* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
371 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
372 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
375 static const char * const tasm_directives
[] = {
376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
380 static int StackSize
= 4;
381 static char *StackPointer
= "ebp";
382 static int ArgOffset
= 8;
383 static int LocalOffset
= 0;
385 static Context
*cstk
;
386 static Include
*istk
;
387 static IncPath
*ipath
= NULL
;
389 static int pass
; /* HACK: pass 0 = generate dependencies only */
390 static StrList
**dephead
, **deptail
; /* Dependency list */
392 static uint64_t unique
; /* unique identifier numbers */
394 static Line
*predef
= NULL
;
395 static bool do_predef
;
397 static ListGen
*list
;
400 * The current set of multi-line macros we have defined.
402 static struct hash_table mmacros
;
405 * The current set of single-line macros we have defined.
407 static struct hash_table smacros
;
410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
413 static MMacro
*defining
;
415 static uint64_t nested_mac_count
;
416 static uint64_t nested_rep_count
;
419 * The number of macro parameters to allocate space for at a time.
421 #define PARAM_DELTA 16
424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
427 static macros_t
*stdmacpos
;
430 * The extra standard macros that come from the object format, if
433 static macros_t
*extrastdmac
= NULL
;
434 static bool any_extrastdmac
;
437 * Tokens are allocated in blocks to improve speed
439 #define TOKEN_BLOCKSIZE 4096
440 static Token
*freeTokens
= NULL
;
446 static Blocks blocks
= { NULL
, NULL
};
449 * Forward declarations.
451 static Token
*expand_mmac_params(Token
* tline
);
452 static Token
*expand_smacro(Token
* tline
);
453 static Token
*expand_id(Token
* tline
);
454 static Context
*get_ctx(const char *name
, const char **namep
,
456 static void make_tok_num(Token
* tok
, int64_t val
);
457 static void error(int severity
, const char *fmt
, ...);
458 static void error_precond(int severity
, const char *fmt
, ...);
459 static void *new_Block(size_t size
);
460 static void delete_Blocks(void);
461 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
462 const char *text
, int txtlen
);
463 static Token
*delete_Token(Token
* t
);
466 * Macros for safe checking of token pointers, avoid *(NULL)
468 #define tok_type_(x,t) ((x) && (x)->type == (t))
469 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
478 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
480 size_t len
= nasm_unquote(qstr
, NULL
);
481 size_t clen
= strlen(qstr
);
484 error(ERR_NONFATAL
, "NUL character in `%s' directive",
485 pp_directives
[directive
]);
491 * In-place reverse a list of tokens.
493 static Token
*reverse_tokens(Token
*t
)
509 * Handle TASM specific directives, which do not contain a % in
510 * front of them. We do it here because I could not find any other
511 * place to do it for the moment, and it is a hack (ideally it would
512 * be nice to be able to use the NASM pre-processor to do it).
514 static char *check_tasm_directive(char *line
)
516 int32_t i
, j
, k
, m
, len
;
517 char *p
, *q
, *oldline
, oldchar
;
519 p
= nasm_skip_spaces(line
);
521 /* Binary search for the directive name */
523 j
= ARRAY_SIZE(tasm_directives
);
524 q
= nasm_skip_word(p
);
531 m
= nasm_stricmp(p
, tasm_directives
[k
]);
533 /* We have found a directive, so jam a % in front of it
534 * so that NASM will then recognise it as one if it's own.
539 line
= nasm_malloc(len
+ 2);
541 if (k
== TM_IFDIFI
) {
543 * NASM does not recognise IFDIFI, so we convert
544 * it to %if 0. This is not used in NASM
545 * compatible code, but does need to parse for the
546 * TASM macro package.
548 strcpy(line
+ 1, "if 0");
550 memcpy(line
+ 1, p
, len
+ 1);
565 * The pre-preprocessing stage... This function translates line
566 * number indications as they emerge from GNU cpp (`# lineno "file"
567 * flags') into NASM preprocessor line number indications (`%line
570 static char *prepreproc(char *line
)
573 char *fname
, *oldline
;
575 if (line
[0] == '#' && line
[1] == ' ') {
578 lineno
= atoi(fname
);
579 fname
+= strspn(fname
, "0123456789 ");
582 fnlen
= strcspn(fname
, "\"");
583 line
= nasm_malloc(20 + fnlen
);
584 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
587 if (tasm_compatible_mode
)
588 return check_tasm_directive(line
);
593 * Free a linked list of tokens.
595 static void free_tlist(Token
* list
)
598 list
= delete_Token(list
);
602 * Free a linked list of lines.
604 static void free_llist(Line
* list
)
607 list_for_each_safe(l
, tmp
, list
) {
608 free_tlist(l
->first
);
616 static void free_mmacro(MMacro
* m
)
619 free_tlist(m
->dlist
);
620 nasm_free(m
->defaults
);
621 free_llist(m
->expansion
);
626 * Free all currently defined macros, and free the hash tables
628 static void free_smacro_table(struct hash_table
*smt
)
632 struct hash_tbl_node
*it
= NULL
;
634 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
635 nasm_free((void *)key
);
636 list_for_each_safe(s
, tmp
, s
) {
638 free_tlist(s
->expansion
);
645 static void free_mmacro_table(struct hash_table
*mmt
)
649 struct hash_tbl_node
*it
= NULL
;
652 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
653 nasm_free((void *)key
);
654 list_for_each_safe(m
,tmp
, m
)
660 static void free_macros(void)
662 free_smacro_table(&smacros
);
663 free_mmacro_table(&mmacros
);
667 * Initialize the hash tables
669 static void init_macros(void)
671 hash_init(&smacros
, HASH_LARGE
);
672 hash_init(&mmacros
, HASH_LARGE
);
676 * Pop the context stack.
678 static void ctx_pop(void)
683 free_smacro_table(&c
->localmac
);
689 * Search for a key in the hash index; adding it if necessary
690 * (in which case we initialize the data pointer to NULL.)
693 hash_findi_add(struct hash_table
*hash
, const char *str
)
695 struct hash_insert hi
;
699 r
= hash_findi(hash
, str
, &hi
);
703 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
704 return hash_add(&hi
, strx
, NULL
);
708 * Like hash_findi, but returns the data element rather than a pointer
709 * to it. Used only when not adding a new element, hence no third
713 hash_findix(struct hash_table
*hash
, const char *str
)
717 p
= hash_findi(hash
, str
, NULL
);
718 return p
? *p
: NULL
;
722 * read line from standart macros set,
723 * if there no more left -- return NULL
725 static char *line_from_stdmac(void)
728 const unsigned char *p
= stdmacpos
;
737 len
+= pp_directives_len
[c
- 0x80] + 1;
742 line
= nasm_malloc(len
+ 1);
744 while ((c
= *stdmacpos
++)) {
746 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
747 q
+= pp_directives_len
[c
- 0x80];
757 /* This was the last of the standard macro chain... */
759 if (any_extrastdmac
) {
760 stdmacpos
= extrastdmac
;
761 any_extrastdmac
= false;
762 } else if (do_predef
) {
764 Token
*head
, **tail
, *t
;
767 * Nasty hack: here we push the contents of
768 * `predef' on to the top-level expansion stack,
769 * since this is the most convenient way to
770 * implement the pre-include and pre-define
773 list_for_each(pd
, predef
) {
776 list_for_each(t
, pd
->first
) {
777 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
778 tail
= &(*tail
)->next
;
781 l
= nasm_malloc(sizeof(Line
));
782 l
->next
= istk
->expansion
;
795 #define BUF_DELTA 512
797 * Read a line from the top file in istk, handling multiple CR/LFs
798 * at the end of the line read, and handling spurious ^Zs. Will
799 * return lines from the standard macro set if this has not already
802 static char *read_line(void)
804 char *buffer
, *p
, *q
;
805 int bufsize
, continued_count
;
808 * standart macros set (predefined) goes first
810 p
= line_from_stdmac();
815 * regular read from a file
818 buffer
= nasm_malloc(BUF_DELTA
);
822 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
826 if (p
> buffer
&& p
[-1] == '\n') {
828 * Convert backslash-CRLF line continuation sequences into
829 * nothing at all (for DOS and Windows)
831 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
837 * Also convert backslash-LF line continuation sequences into
838 * nothing at all (for Unix)
840 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
848 if (p
- buffer
> bufsize
- 10) {
849 int32_t offset
= p
- buffer
;
850 bufsize
+= BUF_DELTA
;
851 buffer
= nasm_realloc(buffer
, bufsize
);
852 p
= buffer
+ offset
; /* prevent stale-pointer problems */
856 if (!q
&& p
== buffer
) {
861 src_set_linnum(src_get_linnum() + istk
->lineinc
+
862 (continued_count
* istk
->lineinc
));
865 * Play safe: remove CRs as well as LFs, if any of either are
866 * present at the end of the line.
868 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
872 * Handle spurious ^Z, which may be inserted into source files
873 * by some file transfer utilities.
875 buffer
[strcspn(buffer
, "\032")] = '\0';
877 list
->line(LIST_READ
, buffer
);
883 * Tokenize a line of text. This is a very simple process since we
884 * don't need to parse the value out of e.g. numeric tokens: we
885 * simply split one string into many.
887 static Token
*tokenize(char *line
)
890 enum pp_token_type type
;
892 Token
*t
, **tail
= &list
;
898 if (*p
== '+' && !nasm_isdigit(p
[1])) {
901 } else if (nasm_isdigit(*p
) ||
902 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
906 while (nasm_isdigit(*p
));
907 type
= TOK_PREPROC_ID
;
908 } else if (*p
== '{') {
910 while (*p
&& *p
!= '}') {
917 type
= TOK_PREPROC_ID
;
918 } else if (*p
== '[') {
920 line
+= 2; /* Skip the leading %[ */
922 while (lvl
&& (c
= *p
++)) {
934 p
= nasm_skip_string(p
- 1) + 1;
944 error(ERR_NONFATAL
, "unterminated %[ construct");
946 } else if (*p
== '?') {
947 type
= TOK_PREPROC_Q
; /* %? */
950 type
= TOK_PREPROC_QQ
; /* %?? */
953 } else if (*p
== '!') {
954 type
= TOK_PREPROC_ID
;
959 } while (isidchar(*p
));
960 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
961 p
= nasm_skip_string(p
);
965 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
967 /* %! without string or identifier */
968 type
= TOK_OTHER
; /* Legacy behavior... */
970 } else if (isidchar(*p
) ||
971 ((*p
== '!' || *p
== '%' || *p
== '$') &&
976 while (isidchar(*p
));
977 type
= TOK_PREPROC_ID
;
983 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
986 while (*p
&& isidchar(*p
))
988 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
993 p
= nasm_skip_string(p
);
998 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
999 /* Handling unterminated strings by UNV */
1002 } else if (p
[0] == '$' && p
[1] == '$') {
1003 type
= TOK_OTHER
; /* TOKEN_BASE */
1005 } else if (isnumstart(*p
)) {
1006 bool is_hex
= false;
1007 bool is_float
= false;
1023 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1025 if (*p
== '+' || *p
== '-') {
1027 * e can only be followed by +/- if it is either a
1028 * prefixed hex number or a floating-point number
1033 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1035 } else if (c
== 'P' || c
== 'p') {
1037 if (*p
== '+' || *p
== '-')
1039 } else if (isnumchar(c
) || c
== '_')
1040 ; /* just advance */
1041 else if (c
== '.') {
1043 * we need to deal with consequences of the legacy
1044 * parser, like "1.nolist" being two tokens
1045 * (TOK_NUMBER, TOK_ID) here; at least give it
1046 * a shot for now. In the future, we probably need
1047 * a flex-based scanner with proper pattern matching
1048 * to do it as well as it can be done. Nothing in
1049 * the world is going to help the person who wants
1050 * 0x123.p16 interpreted as two tokens, though.
1056 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1057 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1058 (*r
== 'p' || *r
== 'P')) {
1062 break; /* Terminate the token */
1066 p
--; /* Point to first character beyond number */
1068 if (p
== line
+1 && *line
== '$') {
1069 type
= TOK_OTHER
; /* TOKEN_HERE */
1071 if (has_e
&& !is_hex
) {
1072 /* 1e13 is floating-point, but 1e13h is not */
1076 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1078 } else if (nasm_isspace(*p
)) {
1079 type
= TOK_WHITESPACE
;
1080 p
= nasm_skip_spaces(p
);
1082 * Whitespace just before end-of-line is discarded by
1083 * pretending it's a comment; whitespace just before a
1084 * comment gets lumped into the comment.
1086 if (!*p
|| *p
== ';') {
1091 } else if (*p
== ';') {
1097 * Anything else is an operator of some kind. We check
1098 * for all the double-character operators (>>, <<, //,
1099 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1100 * else is a single-character operator.
1103 if ((p
[0] == '>' && p
[1] == '>') ||
1104 (p
[0] == '<' && p
[1] == '<') ||
1105 (p
[0] == '/' && p
[1] == '/') ||
1106 (p
[0] == '<' && p
[1] == '=') ||
1107 (p
[0] == '>' && p
[1] == '=') ||
1108 (p
[0] == '=' && p
[1] == '=') ||
1109 (p
[0] == '!' && p
[1] == '=') ||
1110 (p
[0] == '<' && p
[1] == '>') ||
1111 (p
[0] == '&' && p
[1] == '&') ||
1112 (p
[0] == '|' && p
[1] == '|') ||
1113 (p
[0] == '^' && p
[1] == '^')) {
1119 /* Handling unterminated string by UNV */
1122 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1123 t->text[p-line] = *line;
1127 if (type
!= TOK_COMMENT
) {
1128 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1137 * this function allocates a new managed block of memory and
1138 * returns a pointer to the block. The managed blocks are
1139 * deleted only all at once by the delete_Blocks function.
1141 static void *new_Block(size_t size
)
1143 Blocks
*b
= &blocks
;
1145 /* first, get to the end of the linked list */
1148 /* now allocate the requested chunk */
1149 b
->chunk
= nasm_malloc(size
);
1151 /* now allocate a new block for the next request */
1152 b
->next
= nasm_malloc(sizeof(Blocks
));
1153 /* and initialize the contents of the new block */
1154 b
->next
->next
= NULL
;
1155 b
->next
->chunk
= NULL
;
1160 * this function deletes all managed blocks of memory
1162 static void delete_Blocks(void)
1164 Blocks
*a
, *b
= &blocks
;
1167 * keep in mind that the first block, pointed to by blocks
1168 * is a static and not dynamically allocated, so we don't
1173 nasm_free(b
->chunk
);
1182 * this function creates a new Token and passes a pointer to it
1183 * back to the caller. It sets the type and text elements, and
1184 * also the a.mac and next elements to NULL.
1186 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1187 const char *text
, int txtlen
)
1193 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1194 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1195 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1196 freeTokens
[i
].next
= NULL
;
1199 freeTokens
= t
->next
;
1203 if (type
== TOK_WHITESPACE
|| !text
) {
1207 txtlen
= strlen(text
);
1208 t
->text
= nasm_malloc(txtlen
+1);
1209 memcpy(t
->text
, text
, txtlen
);
1210 t
->text
[txtlen
] = '\0';
1215 static Token
*delete_Token(Token
* t
)
1217 Token
*next
= t
->next
;
1219 t
->next
= freeTokens
;
1225 * Convert a line of tokens back into text.
1226 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1227 * will be transformed into ..@ctxnum.xxx
1229 static char *detoken(Token
* tlist
, bool expand_locals
)
1236 list_for_each(t
, tlist
) {
1237 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1242 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1243 size_t len
= nasm_unquote(v
, NULL
);
1244 size_t clen
= strlen(v
);
1247 error(ERR_NONFATAL
| ERR_PASS1
,
1248 "NUL character in %! string");
1254 char *p
= getenv(v
);
1256 error(ERR_NONFATAL
| ERR_PASS1
,
1257 "nonexistent environment variable `%s'", v
);
1260 t
->text
= nasm_strdup(p
);
1265 /* Expand local macros here and not during preprocessing */
1266 if (expand_locals
&&
1267 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1268 t
->text
[0] == '%' && t
->text
[1] == '$') {
1271 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1274 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1275 p
= nasm_strcat(buffer
, q
);
1280 if (t
->type
== TOK_WHITESPACE
)
1283 len
+= strlen(t
->text
);
1286 p
= line
= nasm_malloc(len
+ 1);
1288 list_for_each(t
, tlist
) {
1289 if (t
->type
== TOK_WHITESPACE
) {
1291 } else if (t
->text
) {
1303 * A scanner, suitable for use by the expression evaluator, which
1304 * operates on a line of Tokens. Expects a pointer to a pointer to
1305 * the first token in the line to be passed in as its private_data
1308 * FIX: This really needs to be unified with stdscan.
1310 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1312 Token
**tlineptr
= private_data
;
1314 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1318 *tlineptr
= tline
? tline
->next
: NULL
;
1319 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1320 tline
->type
== TOK_COMMENT
));
1323 return tokval
->t_type
= TOKEN_EOS
;
1325 tokval
->t_charptr
= tline
->text
;
1327 if (tline
->text
[0] == '$' && !tline
->text
[1])
1328 return tokval
->t_type
= TOKEN_HERE
;
1329 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1330 return tokval
->t_type
= TOKEN_BASE
;
1332 if (tline
->type
== TOK_ID
) {
1333 p
= tokval
->t_charptr
= tline
->text
;
1335 tokval
->t_charptr
++;
1336 return tokval
->t_type
= TOKEN_ID
;
1339 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1340 if (r
>= p
+MAX_KEYWORD
)
1341 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1342 *s
++ = nasm_tolower(*r
);
1345 /* right, so we have an identifier sitting in temp storage. now,
1346 * is it actually a register or instruction name, or what? */
1347 return nasm_token_hash(ourcopy
, tokval
);
1350 if (tline
->type
== TOK_NUMBER
) {
1352 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1353 tokval
->t_charptr
= tline
->text
;
1355 return tokval
->t_type
= TOKEN_ERRNUM
;
1357 return tokval
->t_type
= TOKEN_NUM
;
1360 if (tline
->type
== TOK_FLOAT
) {
1361 return tokval
->t_type
= TOKEN_FLOAT
;
1364 if (tline
->type
== TOK_STRING
) {
1367 bq
= tline
->text
[0];
1368 tokval
->t_charptr
= tline
->text
;
1369 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1371 if (ep
[0] != bq
|| ep
[1] != '\0')
1372 return tokval
->t_type
= TOKEN_ERRSTR
;
1374 return tokval
->t_type
= TOKEN_STR
;
1377 if (tline
->type
== TOK_OTHER
) {
1378 if (!strcmp(tline
->text
, "<<"))
1379 return tokval
->t_type
= TOKEN_SHL
;
1380 if (!strcmp(tline
->text
, ">>"))
1381 return tokval
->t_type
= TOKEN_SHR
;
1382 if (!strcmp(tline
->text
, "//"))
1383 return tokval
->t_type
= TOKEN_SDIV
;
1384 if (!strcmp(tline
->text
, "%%"))
1385 return tokval
->t_type
= TOKEN_SMOD
;
1386 if (!strcmp(tline
->text
, "=="))
1387 return tokval
->t_type
= TOKEN_EQ
;
1388 if (!strcmp(tline
->text
, "<>"))
1389 return tokval
->t_type
= TOKEN_NE
;
1390 if (!strcmp(tline
->text
, "!="))
1391 return tokval
->t_type
= TOKEN_NE
;
1392 if (!strcmp(tline
->text
, "<="))
1393 return tokval
->t_type
= TOKEN_LE
;
1394 if (!strcmp(tline
->text
, ">="))
1395 return tokval
->t_type
= TOKEN_GE
;
1396 if (!strcmp(tline
->text
, "&&"))
1397 return tokval
->t_type
= TOKEN_DBL_AND
;
1398 if (!strcmp(tline
->text
, "^^"))
1399 return tokval
->t_type
= TOKEN_DBL_XOR
;
1400 if (!strcmp(tline
->text
, "||"))
1401 return tokval
->t_type
= TOKEN_DBL_OR
;
1405 * We have no other options: just return the first character of
1408 return tokval
->t_type
= tline
->text
[0];
1412 * Compare a string to the name of an existing macro; this is a
1413 * simple wrapper which calls either strcmp or nasm_stricmp
1414 * depending on the value of the `casesense' parameter.
1416 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1418 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1422 * Compare a string to the name of an existing macro; this is a
1423 * simple wrapper which calls either strcmp or nasm_stricmp
1424 * depending on the value of the `casesense' parameter.
1426 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1428 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1432 * Return the Context structure associated with a %$ token. Return
1433 * NULL, having _already_ reported an error condition, if the
1434 * context stack isn't deep enough for the supplied number of $
1436 * If all_contexts == true, contexts that enclose current are
1437 * also scanned for such smacro, until it is found; if not -
1438 * only the context that directly results from the number of $'s
1439 * in variable's name.
1441 * If "namep" is non-NULL, set it to the pointer to the macro name
1442 * tail, i.e. the part beyond %$...
1444 static Context
*get_ctx(const char *name
, const char **namep
,
1454 if (!name
|| name
[0] != '%' || name
[1] != '$')
1458 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1465 while (ctx
&& *name
== '$') {
1471 error(ERR_NONFATAL
, "`%s': context stack is only"
1472 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1483 * NOTE: In 2.10 we will not need lookup in extarnal
1484 * contexts, so this is a gentle way to inform users
1485 * about their source code need to be updated
1488 /* first round -- check the current context */
1489 m
= hash_findix(&ctx
->localmac
, name
);
1491 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1496 /* second round - external contexts */
1497 while ((ctx
= ctx
->next
)) {
1498 /* Search for this smacro in found context */
1499 m
= hash_findix(&ctx
->localmac
, name
);
1501 if (!mstrcmp(m
->name
, name
, m
->casesense
)) {
1502 /* NOTE: deprecated as of 2.10 */
1503 static int once
= 0;
1505 error(ERR_WARNING
, "context-local macro expansion"
1506 " fall-through (automatic searching of outer"
1507 " contexts) will be deprecated starting in"
1508 " NASM 2.10, please see the NASM Manual for"
1509 " more information");
1512 error(ERR_WARNING
, "`%s': context-local macro expansion fall-through", name
);
1523 * Check to see if a file is already in a string list
1525 static bool in_list(const StrList
*list
, const char *str
)
1528 if (!strcmp(list
->str
, str
))
1536 * Open an include file. This routine must always return a valid
1537 * file pointer if it returns - it's responsible for throwing an
1538 * ERR_FATAL and bombing out completely if not. It should also try
1539 * the include path one by one until it finds the file or reaches
1540 * the end of the path.
1542 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1547 IncPath
*ip
= ipath
;
1548 int len
= strlen(file
);
1549 size_t prefix_len
= 0;
1553 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1554 memcpy(sl
->str
, prefix
, prefix_len
);
1555 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1556 fp
= fopen(sl
->str
, "r");
1557 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1575 prefix_len
= strlen(prefix
);
1577 /* -MG given and file not found */
1578 if (dhead
&& !in_list(*dhead
, file
)) {
1579 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1581 strcpy(sl
->str
, file
);
1589 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1594 * Determine if we should warn on defining a single-line macro of
1595 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1596 * return true if _any_ single-line macro of that name is defined.
1597 * Otherwise, will return true if a single-line macro with either
1598 * `nparam' or no parameters is defined.
1600 * If a macro with precisely the right number of parameters is
1601 * defined, or nparam is -1, the address of the definition structure
1602 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1603 * is NULL, no action will be taken regarding its contents, and no
1606 * Note that this is also called with nparam zero to resolve
1609 * If you already know which context macro belongs to, you can pass
1610 * the context pointer as first parameter; if you won't but name begins
1611 * with %$ the context will be automatically computed. If all_contexts
1612 * is true, macro will be searched in outer contexts as well.
1615 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1618 struct hash_table
*smtbl
;
1622 smtbl
= &ctx
->localmac
;
1623 } else if (name
[0] == '%' && name
[1] == '$') {
1625 ctx
= get_ctx(name
, &name
, false);
1627 return false; /* got to return _something_ */
1628 smtbl
= &ctx
->localmac
;
1632 m
= (SMacro
*) hash_findix(smtbl
, name
);
1635 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1636 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1638 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1652 * Count and mark off the parameters in a multi-line macro call.
1653 * This is called both from within the multi-line macro expansion
1654 * code, and also to mark off the default parameters when provided
1655 * in a %macro definition line.
1657 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1659 int paramsize
, brace
;
1661 *nparam
= paramsize
= 0;
1664 /* +1: we need space for the final NULL */
1665 if (*nparam
+1 >= paramsize
) {
1666 paramsize
+= PARAM_DELTA
;
1667 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1671 if (tok_is_(t
, "{"))
1673 (*params
)[(*nparam
)++] = t
;
1674 while (tok_isnt_(t
, brace
? "}" : ","))
1676 if (t
) { /* got a comma/brace */
1680 * Now we've found the closing brace, look further
1684 if (tok_isnt_(t
, ",")) {
1686 "braces do not enclose all of macro parameter");
1687 while (tok_isnt_(t
, ","))
1691 t
= t
->next
; /* eat the comma */
1698 * Determine whether one of the various `if' conditions is true or
1701 * We must free the tline we get passed.
1703 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1705 enum pp_conditional i
= PP_COND(ct
);
1707 Token
*t
, *tt
, **tptr
, *origline
;
1708 struct tokenval tokval
;
1710 enum pp_token_type needtype
;
1717 j
= false; /* have we matched yet? */
1722 if (tline
->type
!= TOK_ID
) {
1724 "`%s' expects context identifiers", pp_directives
[ct
]);
1725 free_tlist(origline
);
1728 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1730 tline
= tline
->next
;
1735 j
= false; /* have we matched yet? */
1738 if (!tline
|| (tline
->type
!= TOK_ID
&&
1739 (tline
->type
!= TOK_PREPROC_ID
||
1740 tline
->text
[1] != '$'))) {
1742 "`%s' expects macro identifiers", pp_directives
[ct
]);
1745 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1747 tline
= tline
->next
;
1752 tline
= expand_smacro(tline
);
1753 j
= false; /* have we matched yet? */
1756 if (!tline
|| (tline
->type
!= TOK_ID
&&
1757 tline
->type
!= TOK_STRING
&&
1758 (tline
->type
!= TOK_PREPROC_ID
||
1759 tline
->text
[1] != '!'))) {
1761 "`%s' expects environment variable names",
1766 if (tline
->type
== TOK_PREPROC_ID
)
1767 p
+= 2; /* Skip leading %! */
1768 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1769 nasm_unquote_cstr(p
, ct
);
1772 tline
= tline
->next
;
1778 tline
= expand_smacro(tline
);
1780 while (tok_isnt_(tt
, ","))
1784 "`%s' expects two comma-separated arguments",
1789 j
= true; /* assume equality unless proved not */
1790 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1791 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1792 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1796 if (t
->type
== TOK_WHITESPACE
) {
1800 if (tt
->type
== TOK_WHITESPACE
) {
1804 if (tt
->type
!= t
->type
) {
1805 j
= false; /* found mismatching tokens */
1808 /* When comparing strings, need to unquote them first */
1809 if (t
->type
== TOK_STRING
) {
1810 size_t l1
= nasm_unquote(t
->text
, NULL
);
1811 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1817 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1821 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1822 j
= false; /* found mismatching tokens */
1829 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1830 j
= false; /* trailing gunk on one end or other */
1836 MMacro searching
, *mmac
;
1839 tline
= expand_id(tline
);
1840 if (!tok_type_(tline
, TOK_ID
)) {
1842 "`%s' expects a macro name", pp_directives
[ct
]);
1845 searching
.name
= nasm_strdup(tline
->text
);
1846 searching
.casesense
= true;
1847 searching
.plus
= false;
1848 searching
.nolist
= false;
1849 searching
.in_progress
= 0;
1850 searching
.max_depth
= 0;
1851 searching
.rep_nest
= NULL
;
1852 searching
.nparam_min
= 0;
1853 searching
.nparam_max
= INT_MAX
;
1854 tline
= expand_smacro(tline
->next
);
1857 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1859 "`%s' expects a parameter count or nothing",
1862 searching
.nparam_min
= searching
.nparam_max
=
1863 readnum(tline
->text
, &j
);
1866 "unable to parse parameter count `%s'",
1869 if (tline
&& tok_is_(tline
->next
, "-")) {
1870 tline
= tline
->next
->next
;
1871 if (tok_is_(tline
, "*"))
1872 searching
.nparam_max
= INT_MAX
;
1873 else if (!tok_type_(tline
, TOK_NUMBER
))
1875 "`%s' expects a parameter count after `-'",
1878 searching
.nparam_max
= readnum(tline
->text
, &j
);
1881 "unable to parse parameter count `%s'",
1883 if (searching
.nparam_min
> searching
.nparam_max
)
1885 "minimum parameter count exceeds maximum");
1888 if (tline
&& tok_is_(tline
->next
, "+")) {
1889 tline
= tline
->next
;
1890 searching
.plus
= true;
1892 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1894 if (!strcmp(mmac
->name
, searching
.name
) &&
1895 (mmac
->nparam_min
<= searching
.nparam_max
1897 && (searching
.nparam_min
<= mmac
->nparam_max
1904 if (tline
&& tline
->next
)
1905 error(ERR_WARNING
|ERR_PASS1
,
1906 "trailing garbage after %%ifmacro ignored");
1907 nasm_free(searching
.name
);
1916 needtype
= TOK_NUMBER
;
1919 needtype
= TOK_STRING
;
1923 t
= tline
= expand_smacro(tline
);
1925 while (tok_type_(t
, TOK_WHITESPACE
) ||
1926 (needtype
== TOK_NUMBER
&&
1927 tok_type_(t
, TOK_OTHER
) &&
1928 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1932 j
= tok_type_(t
, needtype
);
1936 t
= tline
= expand_smacro(tline
);
1937 while (tok_type_(t
, TOK_WHITESPACE
))
1942 t
= t
->next
; /* Skip the actual token */
1943 while (tok_type_(t
, TOK_WHITESPACE
))
1945 j
= !t
; /* Should be nothing left */
1950 t
= tline
= expand_smacro(tline
);
1951 while (tok_type_(t
, TOK_WHITESPACE
))
1954 j
= !t
; /* Should be empty */
1958 t
= tline
= expand_smacro(tline
);
1960 tokval
.t_type
= TOKEN_INVALID
;
1961 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1962 NULL
, pass
| CRITICAL
, error
, NULL
);
1966 error(ERR_WARNING
|ERR_PASS1
,
1967 "trailing garbage after expression ignored");
1968 if (!is_simple(evalresult
)) {
1970 "non-constant value given to `%s'", pp_directives
[ct
]);
1973 j
= reloc_value(evalresult
) != 0;
1978 "preprocessor directive `%s' not yet implemented",
1983 free_tlist(origline
);
1984 return j
^ PP_NEGATIVE(ct
);
1987 free_tlist(origline
);
1992 * Common code for defining an smacro
1994 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1995 int nparam
, Token
*expansion
)
1997 SMacro
*smac
, **smhead
;
1998 struct hash_table
*smtbl
;
2000 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2002 error(ERR_WARNING
|ERR_PASS1
,
2003 "single-line macro `%s' defined both with and"
2004 " without parameters", mname
);
2006 * Some instances of the old code considered this a failure,
2007 * some others didn't. What is the right thing to do here?
2009 free_tlist(expansion
);
2010 return false; /* Failure */
2013 * We're redefining, so we have to take over an
2014 * existing SMacro structure. This means freeing
2015 * what was already in it.
2017 nasm_free(smac
->name
);
2018 free_tlist(smac
->expansion
);
2021 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2022 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2023 smac
= nasm_malloc(sizeof(SMacro
));
2024 smac
->next
= *smhead
;
2027 smac
->name
= nasm_strdup(mname
);
2028 smac
->casesense
= casesense
;
2029 smac
->nparam
= nparam
;
2030 smac
->expansion
= expansion
;
2031 smac
->in_progress
= false;
2032 return true; /* Success */
2036 * Undefine an smacro
2038 static void undef_smacro(Context
*ctx
, const char *mname
)
2040 SMacro
**smhead
, *s
, **sp
;
2041 struct hash_table
*smtbl
;
2043 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2044 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2048 * We now have a macro name... go hunt for it.
2051 while ((s
= *sp
) != NULL
) {
2052 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2055 free_tlist(s
->expansion
);
2065 * Parse a mmacro specification.
2067 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2071 tline
= tline
->next
;
2073 tline
= expand_id(tline
);
2074 if (!tok_type_(tline
, TOK_ID
)) {
2075 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2080 def
->name
= nasm_strdup(tline
->text
);
2082 def
->nolist
= false;
2083 def
->in_progress
= 0;
2084 def
->rep_nest
= NULL
;
2085 def
->nparam_min
= 0;
2086 def
->nparam_max
= 0;
2088 tline
= expand_smacro(tline
->next
);
2090 if (!tok_type_(tline
, TOK_NUMBER
)) {
2091 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2093 def
->nparam_min
= def
->nparam_max
=
2094 readnum(tline
->text
, &err
);
2097 "unable to parse parameter count `%s'", tline
->text
);
2099 if (tline
&& tok_is_(tline
->next
, "-")) {
2100 tline
= tline
->next
->next
;
2101 if (tok_is_(tline
, "*")) {
2102 def
->nparam_max
= INT_MAX
;
2103 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2105 "`%s' expects a parameter count after `-'", directive
);
2107 def
->nparam_max
= readnum(tline
->text
, &err
);
2109 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2112 if (def
->nparam_min
> def
->nparam_max
) {
2113 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2117 if (tline
&& tok_is_(tline
->next
, "+")) {
2118 tline
= tline
->next
;
2121 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2122 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2123 tline
= tline
->next
;
2128 * Handle default parameters.
2130 if (tline
&& tline
->next
) {
2131 def
->dlist
= tline
->next
;
2133 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2136 def
->defaults
= NULL
;
2138 def
->expansion
= NULL
;
2140 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2142 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2143 "too many default macro parameters");
2150 * Decode a size directive
2152 static int parse_size(const char *str
) {
2153 static const char *size_names
[] =
2154 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2155 static const int sizes
[] =
2156 { 0, 1, 4, 16, 8, 10, 2, 32 };
2158 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2162 * find and process preprocessor directive in passed line
2163 * Find out if a line contains a preprocessor directive, and deal
2166 * If a directive _is_ found, it is the responsibility of this routine
2167 * (and not the caller) to free_tlist() the line.
2169 * @param tline a pointer to the current tokeninzed line linked list
2170 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2173 static int do_directive(Token
* tline
)
2175 enum preproc_token i
;
2188 MMacro
*mmac
, **mmhead
;
2189 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2191 struct tokenval tokval
;
2193 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2201 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2202 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2203 || tline
->text
[1] == '!'))
2204 return NO_DIRECTIVE_FOUND
;
2206 i
= pp_token_hash(tline
->text
);
2209 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2210 * since they are known to be buggy at moment, we need to fix them
2211 * in future release (2.09-2.10)
2213 if (i
== PP_RMACRO
|| i
== PP_RMACRO
|| i
== PP_EXITMACRO
) {
2214 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2216 return NO_DIRECTIVE_FOUND
;
2220 * If we're in a non-emitting branch of a condition construct,
2221 * or walking to the end of an already terminated %rep block,
2222 * we should ignore all directives except for condition
2225 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2226 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2227 return NO_DIRECTIVE_FOUND
;
2231 * If we're defining a macro or reading a %rep block, we should
2232 * ignore all directives except for %macro/%imacro (which nest),
2233 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2234 * If we're in a %rep block, another %rep nests, so should be let through.
2236 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2237 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2238 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2239 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2240 return NO_DIRECTIVE_FOUND
;
2244 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2245 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2247 return NO_DIRECTIVE_FOUND
;
2248 } else if (nested_mac_count
> 0) {
2249 if (i
== PP_ENDMACRO
) {
2251 return NO_DIRECTIVE_FOUND
;
2254 if (!defining
->name
) {
2257 return NO_DIRECTIVE_FOUND
;
2258 } else if (nested_rep_count
> 0) {
2259 if (i
== PP_ENDREP
) {
2261 return NO_DIRECTIVE_FOUND
;
2269 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2271 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2274 /* Directive to tell NASM what the default stack size is. The
2275 * default is for a 16-bit stack, and this can be overriden with
2278 tline
= tline
->next
;
2279 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2280 tline
= tline
->next
;
2281 if (!tline
|| tline
->type
!= TOK_ID
) {
2282 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2283 free_tlist(origline
);
2284 return DIRECTIVE_FOUND
;
2286 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2287 /* All subsequent ARG directives are for a 32-bit stack */
2289 StackPointer
= "ebp";
2292 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2293 /* All subsequent ARG directives are for a 64-bit stack */
2295 StackPointer
= "rbp";
2298 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2299 /* All subsequent ARG directives are for a 16-bit stack,
2300 * far function call.
2303 StackPointer
= "bp";
2306 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2307 /* All subsequent ARG directives are for a 16-bit stack,
2308 * far function call. We don't support near functions.
2311 StackPointer
= "bp";
2315 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2316 free_tlist(origline
);
2317 return DIRECTIVE_FOUND
;
2319 free_tlist(origline
);
2320 return DIRECTIVE_FOUND
;
2323 /* TASM like ARG directive to define arguments to functions, in
2324 * the following form:
2326 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2330 char *arg
, directive
[256];
2331 int size
= StackSize
;
2333 /* Find the argument name */
2334 tline
= tline
->next
;
2335 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2336 tline
= tline
->next
;
2337 if (!tline
|| tline
->type
!= TOK_ID
) {
2338 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2339 free_tlist(origline
);
2340 return DIRECTIVE_FOUND
;
2344 /* Find the argument size type */
2345 tline
= tline
->next
;
2346 if (!tline
|| tline
->type
!= TOK_OTHER
2347 || tline
->text
[0] != ':') {
2349 "Syntax error processing `%%arg' directive");
2350 free_tlist(origline
);
2351 return DIRECTIVE_FOUND
;
2353 tline
= tline
->next
;
2354 if (!tline
|| tline
->type
!= TOK_ID
) {
2355 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2356 free_tlist(origline
);
2357 return DIRECTIVE_FOUND
;
2360 /* Allow macro expansion of type parameter */
2361 tt
= tokenize(tline
->text
);
2362 tt
= expand_smacro(tt
);
2363 size
= parse_size(tt
->text
);
2366 "Invalid size type for `%%arg' missing directive");
2368 free_tlist(origline
);
2369 return DIRECTIVE_FOUND
;
2373 /* Round up to even stack slots */
2374 size
= ALIGN(size
, StackSize
);
2376 /* Now define the macro for the argument */
2377 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2378 arg
, StackPointer
, offset
);
2379 do_directive(tokenize(directive
));
2382 /* Move to the next argument in the list */
2383 tline
= tline
->next
;
2384 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2385 tline
= tline
->next
;
2386 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2388 free_tlist(origline
);
2389 return DIRECTIVE_FOUND
;
2392 /* TASM like LOCAL directive to define local variables for a
2393 * function, in the following form:
2395 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2397 * The '= LocalSize' at the end is ignored by NASM, but is
2398 * required by TASM to define the local parameter size (and used
2399 * by the TASM macro package).
2401 offset
= LocalOffset
;
2403 char *local
, directive
[256];
2404 int size
= StackSize
;
2406 /* Find the argument name */
2407 tline
= tline
->next
;
2408 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2409 tline
= tline
->next
;
2410 if (!tline
|| tline
->type
!= TOK_ID
) {
2412 "`%%local' missing argument parameter");
2413 free_tlist(origline
);
2414 return DIRECTIVE_FOUND
;
2416 local
= tline
->text
;
2418 /* Find the argument size type */
2419 tline
= tline
->next
;
2420 if (!tline
|| tline
->type
!= TOK_OTHER
2421 || tline
->text
[0] != ':') {
2423 "Syntax error processing `%%local' directive");
2424 free_tlist(origline
);
2425 return DIRECTIVE_FOUND
;
2427 tline
= tline
->next
;
2428 if (!tline
|| tline
->type
!= TOK_ID
) {
2430 "`%%local' missing size type parameter");
2431 free_tlist(origline
);
2432 return DIRECTIVE_FOUND
;
2435 /* Allow macro expansion of type parameter */
2436 tt
= tokenize(tline
->text
);
2437 tt
= expand_smacro(tt
);
2438 size
= parse_size(tt
->text
);
2441 "Invalid size type for `%%local' missing directive");
2443 free_tlist(origline
);
2444 return DIRECTIVE_FOUND
;
2448 /* Round up to even stack slots */
2449 size
= ALIGN(size
, StackSize
);
2451 offset
+= size
; /* Negative offset, increment before */
2453 /* Now define the macro for the argument */
2454 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2455 local
, StackPointer
, offset
);
2456 do_directive(tokenize(directive
));
2458 /* Now define the assign to setup the enter_c macro correctly */
2459 snprintf(directive
, sizeof(directive
),
2460 "%%assign %%$localsize %%$localsize+%d", size
);
2461 do_directive(tokenize(directive
));
2463 /* Move to the next argument in the list */
2464 tline
= tline
->next
;
2465 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2466 tline
= tline
->next
;
2467 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2468 LocalOffset
= offset
;
2469 free_tlist(origline
);
2470 return DIRECTIVE_FOUND
;
2474 error(ERR_WARNING
|ERR_PASS1
,
2475 "trailing garbage after `%%clear' ignored");
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
;
2482 t
= tline
->next
= expand_smacro(tline
->next
);
2484 if (!t
|| (t
->type
!= TOK_STRING
&&
2485 t
->type
!= TOK_INTERNAL_STRING
)) {
2486 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2487 free_tlist(origline
);
2488 return DIRECTIVE_FOUND
; /* but we did _something_ */
2491 error(ERR_WARNING
|ERR_PASS1
,
2492 "trailing garbage after `%%depend' ignored");
2494 if (t
->type
!= TOK_INTERNAL_STRING
)
2495 nasm_unquote_cstr(p
, i
);
2496 if (dephead
&& !in_list(*dephead
, p
)) {
2497 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2501 deptail
= &sl
->next
;
2503 free_tlist(origline
);
2504 return DIRECTIVE_FOUND
;
2507 t
= tline
->next
= expand_smacro(tline
->next
);
2510 if (!t
|| (t
->type
!= TOK_STRING
&&
2511 t
->type
!= TOK_INTERNAL_STRING
)) {
2512 error(ERR_NONFATAL
, "`%%include' expects a file name");
2513 free_tlist(origline
);
2514 return DIRECTIVE_FOUND
; /* but we did _something_ */
2517 error(ERR_WARNING
|ERR_PASS1
,
2518 "trailing garbage after `%%include' ignored");
2520 if (t
->type
!= TOK_INTERNAL_STRING
)
2521 nasm_unquote_cstr(p
, i
);
2522 inc
= nasm_malloc(sizeof(Include
));
2525 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2527 /* -MG given but file not found */
2530 inc
->fname
= src_set_fname(nasm_strdup(p
));
2531 inc
->lineno
= src_set_linnum(0);
2533 inc
->expansion
= NULL
;
2536 list
->uplevel(LIST_INCLUDE
);
2538 free_tlist(origline
);
2539 return DIRECTIVE_FOUND
;
2543 static macros_t
*use_pkg
;
2544 const char *pkg_macro
= NULL
;
2546 tline
= tline
->next
;
2548 tline
= expand_id(tline
);
2550 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2551 tline
->type
!= TOK_INTERNAL_STRING
&&
2552 tline
->type
!= TOK_ID
)) {
2553 error(ERR_NONFATAL
, "`%%use' expects a package name");
2554 free_tlist(origline
);
2555 return DIRECTIVE_FOUND
; /* but we did _something_ */
2558 error(ERR_WARNING
|ERR_PASS1
,
2559 "trailing garbage after `%%use' ignored");
2560 if (tline
->type
== TOK_STRING
)
2561 nasm_unquote_cstr(tline
->text
, i
);
2562 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2564 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2566 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2567 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2568 /* Not already included, go ahead and include it */
2569 stdmacpos
= use_pkg
;
2571 free_tlist(origline
);
2572 return DIRECTIVE_FOUND
;
2577 tline
= tline
->next
;
2579 tline
= expand_id(tline
);
2581 if (!tok_type_(tline
, TOK_ID
)) {
2582 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2584 free_tlist(origline
);
2585 return DIRECTIVE_FOUND
; /* but we did _something_ */
2588 error(ERR_WARNING
|ERR_PASS1
,
2589 "trailing garbage after `%s' ignored",
2591 p
= nasm_strdup(tline
->text
);
2593 p
= NULL
; /* Anonymous */
2597 ctx
= nasm_malloc(sizeof(Context
));
2599 hash_init(&ctx
->localmac
, HASH_SMALL
);
2601 ctx
->number
= unique
++;
2606 error(ERR_NONFATAL
, "`%s': context stack is empty",
2608 } else if (i
== PP_POP
) {
2609 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2610 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2612 cstk
->name
? cstk
->name
: "anonymous", p
);
2617 nasm_free(cstk
->name
);
2623 free_tlist(origline
);
2624 return DIRECTIVE_FOUND
;
2626 severity
= ERR_FATAL
;
2629 severity
= ERR_NONFATAL
;
2632 severity
= ERR_WARNING
|ERR_WARN_USER
;
2637 /* Only error out if this is the final pass */
2638 if (pass
!= 2 && i
!= PP_FATAL
)
2639 return DIRECTIVE_FOUND
;
2641 tline
->next
= expand_smacro(tline
->next
);
2642 tline
= tline
->next
;
2644 t
= tline
? tline
->next
: NULL
;
2646 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2647 /* The line contains only a quoted string */
2649 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2650 error(severity
, "%s", p
);
2652 /* Not a quoted string, or more than a quoted string */
2653 p
= detoken(tline
, false);
2654 error(severity
, "%s", p
);
2657 free_tlist(origline
);
2658 return DIRECTIVE_FOUND
;
2662 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2665 j
= if_condition(tline
->next
, i
);
2666 tline
->next
= NULL
; /* it got freed */
2667 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2669 cond
= nasm_malloc(sizeof(Cond
));
2670 cond
->next
= istk
->conds
;
2674 istk
->mstk
->condcnt
++;
2675 free_tlist(origline
);
2676 return DIRECTIVE_FOUND
;
2680 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2681 switch(istk
->conds
->state
) {
2683 istk
->conds
->state
= COND_DONE
;
2690 case COND_ELSE_TRUE
:
2691 case COND_ELSE_FALSE
:
2692 error_precond(ERR_WARNING
|ERR_PASS1
,
2693 "`%%elif' after `%%else' ignored");
2694 istk
->conds
->state
= COND_NEVER
;
2699 * IMPORTANT: In the case of %if, we will already have
2700 * called expand_mmac_params(); however, if we're
2701 * processing an %elif we must have been in a
2702 * non-emitting mode, which would have inhibited
2703 * the normal invocation of expand_mmac_params().
2704 * Therefore, we have to do it explicitly here.
2706 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2707 tline
->next
= NULL
; /* it got freed */
2708 istk
->conds
->state
=
2709 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2712 free_tlist(origline
);
2713 return DIRECTIVE_FOUND
;
2717 error_precond(ERR_WARNING
|ERR_PASS1
,
2718 "trailing garbage after `%%else' ignored");
2720 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2721 switch(istk
->conds
->state
) {
2724 istk
->conds
->state
= COND_ELSE_FALSE
;
2731 istk
->conds
->state
= COND_ELSE_TRUE
;
2734 case COND_ELSE_TRUE
:
2735 case COND_ELSE_FALSE
:
2736 error_precond(ERR_WARNING
|ERR_PASS1
,
2737 "`%%else' after `%%else' ignored.");
2738 istk
->conds
->state
= COND_NEVER
;
2741 free_tlist(origline
);
2742 return DIRECTIVE_FOUND
;
2746 error_precond(ERR_WARNING
|ERR_PASS1
,
2747 "trailing garbage after `%%endif' ignored");
2749 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2751 istk
->conds
= cond
->next
;
2754 istk
->mstk
->condcnt
--;
2755 free_tlist(origline
);
2756 return DIRECTIVE_FOUND
;
2763 error(ERR_FATAL
, "`%s': already defining a macro",
2765 return DIRECTIVE_FOUND
;
2767 defining
= nasm_malloc(sizeof(MMacro
));
2768 defining
->max_depth
=
2769 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2770 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2771 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2772 nasm_free(defining
);
2774 return DIRECTIVE_FOUND
;
2777 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2779 if (!strcmp(mmac
->name
, defining
->name
) &&
2780 (mmac
->nparam_min
<= defining
->nparam_max
2782 && (defining
->nparam_min
<= mmac
->nparam_max
2784 error(ERR_WARNING
|ERR_PASS1
,
2785 "redefining multi-line macro `%s'", defining
->name
);
2786 return DIRECTIVE_FOUND
;
2790 free_tlist(origline
);
2791 return DIRECTIVE_FOUND
;
2795 if (! (defining
&& defining
->name
)) {
2796 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2797 return DIRECTIVE_FOUND
;
2799 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2800 defining
->next
= *mmhead
;
2803 free_tlist(origline
);
2804 return DIRECTIVE_FOUND
;
2808 * We must search along istk->expansion until we hit a
2809 * macro-end marker for a macro with a name. Then we
2810 * bypass all lines between exitmacro and endmacro.
2812 list_for_each(l
, istk
->expansion
)
2813 if (l
->finishes
&& l
->finishes
->name
)
2818 * Remove all conditional entries relative to this
2819 * macro invocation. (safe to do in this context)
2821 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2823 istk
->conds
= cond
->next
;
2826 istk
->expansion
= l
;
2828 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2830 free_tlist(origline
);
2831 return DIRECTIVE_FOUND
;
2839 spec
.casesense
= (i
== PP_UNMACRO
);
2840 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2841 return DIRECTIVE_FOUND
;
2843 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2844 while (mmac_p
&& *mmac_p
) {
2846 if (mmac
->casesense
== spec
.casesense
&&
2847 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2848 mmac
->nparam_min
== spec
.nparam_min
&&
2849 mmac
->nparam_max
== spec
.nparam_max
&&
2850 mmac
->plus
== spec
.plus
) {
2851 *mmac_p
= mmac
->next
;
2854 mmac_p
= &mmac
->next
;
2857 free_tlist(origline
);
2858 free_tlist(spec
.dlist
);
2859 return DIRECTIVE_FOUND
;
2863 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2864 tline
= tline
->next
;
2866 free_tlist(origline
);
2867 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2868 return DIRECTIVE_FOUND
;
2870 t
= expand_smacro(tline
->next
);
2872 free_tlist(origline
);
2875 tokval
.t_type
= TOKEN_INVALID
;
2877 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2880 return DIRECTIVE_FOUND
;
2882 error(ERR_WARNING
|ERR_PASS1
,
2883 "trailing garbage after expression ignored");
2884 if (!is_simple(evalresult
)) {
2885 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2886 return DIRECTIVE_FOUND
;
2889 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2890 mmac
= mmac
->next_active
;
2892 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2893 } else if (mmac
->nparam
== 0) {
2895 "`%%rotate' invoked within macro without parameters");
2897 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2899 rotate
%= (int)mmac
->nparam
;
2901 rotate
+= mmac
->nparam
;
2903 mmac
->rotate
= rotate
;
2905 return DIRECTIVE_FOUND
;
2910 tline
= tline
->next
;
2911 } while (tok_type_(tline
, TOK_WHITESPACE
));
2913 if (tok_type_(tline
, TOK_ID
) &&
2914 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2917 tline
= tline
->next
;
2918 } while (tok_type_(tline
, TOK_WHITESPACE
));
2922 t
= expand_smacro(tline
);
2924 tokval
.t_type
= TOKEN_INVALID
;
2926 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2928 free_tlist(origline
);
2929 return DIRECTIVE_FOUND
;
2932 error(ERR_WARNING
|ERR_PASS1
,
2933 "trailing garbage after expression ignored");
2934 if (!is_simple(evalresult
)) {
2935 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2936 return DIRECTIVE_FOUND
;
2938 count
= reloc_value(evalresult
);
2939 if (count
>= REP_LIMIT
) {
2940 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
2945 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2948 free_tlist(origline
);
2950 tmp_defining
= defining
;
2951 defining
= nasm_malloc(sizeof(MMacro
));
2952 defining
->prev
= NULL
;
2953 defining
->name
= NULL
; /* flags this macro as a %rep block */
2954 defining
->casesense
= false;
2955 defining
->plus
= false;
2956 defining
->nolist
= nolist
;
2957 defining
->in_progress
= count
;
2958 defining
->max_depth
= 0;
2959 defining
->nparam_min
= defining
->nparam_max
= 0;
2960 defining
->defaults
= NULL
;
2961 defining
->dlist
= NULL
;
2962 defining
->expansion
= NULL
;
2963 defining
->next_active
= istk
->mstk
;
2964 defining
->rep_nest
= tmp_defining
;
2965 return DIRECTIVE_FOUND
;
2968 if (!defining
|| defining
->name
) {
2969 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2970 return DIRECTIVE_FOUND
;
2974 * Now we have a "macro" defined - although it has no name
2975 * and we won't be entering it in the hash tables - we must
2976 * push a macro-end marker for it on to istk->expansion.
2977 * After that, it will take care of propagating itself (a
2978 * macro-end marker line for a macro which is really a %rep
2979 * block will cause the macro to be re-expanded, complete
2980 * with another macro-end marker to ensure the process
2981 * continues) until the whole expansion is forcibly removed
2982 * from istk->expansion by a %exitrep.
2984 l
= nasm_malloc(sizeof(Line
));
2985 l
->next
= istk
->expansion
;
2986 l
->finishes
= defining
;
2988 istk
->expansion
= l
;
2990 istk
->mstk
= defining
;
2992 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2993 tmp_defining
= defining
;
2994 defining
= defining
->rep_nest
;
2995 free_tlist(origline
);
2996 return DIRECTIVE_FOUND
;
3000 * We must search along istk->expansion until we hit a
3001 * macro-end marker for a macro with no name. Then we set
3002 * its `in_progress' flag to 0.
3004 list_for_each(l
, istk
->expansion
)
3005 if (l
->finishes
&& !l
->finishes
->name
)
3009 l
->finishes
->in_progress
= 1;
3011 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3012 free_tlist(origline
);
3013 return DIRECTIVE_FOUND
;
3019 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3021 tline
= tline
->next
;
3023 tline
= expand_id(tline
);
3024 if (!tline
|| (tline
->type
!= TOK_ID
&&
3025 (tline
->type
!= TOK_PREPROC_ID
||
3026 tline
->text
[1] != '$'))) {
3027 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3029 free_tlist(origline
);
3030 return DIRECTIVE_FOUND
;
3033 ctx
= get_ctx(tline
->text
, &mname
, false);
3035 param_start
= tline
= tline
->next
;
3038 /* Expand the macro definition now for %xdefine and %ixdefine */
3039 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3040 tline
= expand_smacro(tline
);
3042 if (tok_is_(tline
, "(")) {
3044 * This macro has parameters.
3047 tline
= tline
->next
;
3051 error(ERR_NONFATAL
, "parameter identifier expected");
3052 free_tlist(origline
);
3053 return DIRECTIVE_FOUND
;
3055 if (tline
->type
!= TOK_ID
) {
3057 "`%s': parameter identifier expected",
3059 free_tlist(origline
);
3060 return DIRECTIVE_FOUND
;
3062 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3063 tline
= tline
->next
;
3065 if (tok_is_(tline
, ",")) {
3066 tline
= tline
->next
;
3068 if (!tok_is_(tline
, ")")) {
3070 "`)' expected to terminate macro template");
3071 free_tlist(origline
);
3072 return DIRECTIVE_FOUND
;
3078 tline
= tline
->next
;
3080 if (tok_type_(tline
, TOK_WHITESPACE
))
3081 last
= tline
, tline
= tline
->next
;
3086 if (t
->type
== TOK_ID
) {
3087 list_for_each(tt
, param_start
)
3088 if (tt
->type
>= TOK_SMAC_PARAM
&&
3089 !strcmp(tt
->text
, t
->text
))
3093 t
->next
= macro_start
;
3098 * Good. We now have a macro name, a parameter count, and a
3099 * token list (in reverse order) for an expansion. We ought
3100 * to be OK just to create an SMacro, store it, and let
3101 * free_tlist have the rest of the line (which we have
3102 * carefully re-terminated after chopping off the expansion
3105 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3106 free_tlist(origline
);
3107 return DIRECTIVE_FOUND
;
3110 tline
= tline
->next
;
3112 tline
= expand_id(tline
);
3113 if (!tline
|| (tline
->type
!= TOK_ID
&&
3114 (tline
->type
!= TOK_PREPROC_ID
||
3115 tline
->text
[1] != '$'))) {
3116 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3117 free_tlist(origline
);
3118 return DIRECTIVE_FOUND
;
3121 error(ERR_WARNING
|ERR_PASS1
,
3122 "trailing garbage after macro name ignored");
3125 /* Find the context that symbol belongs to */
3126 ctx
= get_ctx(tline
->text
, &mname
, false);
3127 undef_smacro(ctx
, mname
);
3128 free_tlist(origline
);
3129 return DIRECTIVE_FOUND
;
3133 casesense
= (i
== PP_DEFSTR
);
3135 tline
= tline
->next
;
3137 tline
= expand_id(tline
);
3138 if (!tline
|| (tline
->type
!= TOK_ID
&&
3139 (tline
->type
!= TOK_PREPROC_ID
||
3140 tline
->text
[1] != '$'))) {
3141 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3143 free_tlist(origline
);
3144 return DIRECTIVE_FOUND
;
3147 ctx
= get_ctx(tline
->text
, &mname
, false);
3149 tline
= expand_smacro(tline
->next
);
3152 while (tok_type_(tline
, TOK_WHITESPACE
))
3153 tline
= delete_Token(tline
);
3155 p
= detoken(tline
, false);
3156 macro_start
= nasm_malloc(sizeof(*macro_start
));
3157 macro_start
->next
= NULL
;
3158 macro_start
->text
= nasm_quote(p
, strlen(p
));
3159 macro_start
->type
= TOK_STRING
;
3160 macro_start
->a
.mac
= NULL
;
3164 * We now have a macro name, an implicit parameter count of
3165 * zero, and a string token to use as an expansion. Create
3166 * and store an SMacro.
3168 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3169 free_tlist(origline
);
3170 return DIRECTIVE_FOUND
;
3174 casesense
= (i
== PP_DEFTOK
);
3176 tline
= tline
->next
;
3178 tline
= expand_id(tline
);
3179 if (!tline
|| (tline
->type
!= TOK_ID
&&
3180 (tline
->type
!= TOK_PREPROC_ID
||
3181 tline
->text
[1] != '$'))) {
3183 "`%s' expects a macro identifier as first parameter",
3185 free_tlist(origline
);
3186 return DIRECTIVE_FOUND
;
3188 ctx
= get_ctx(tline
->text
, &mname
, false);
3190 tline
= expand_smacro(tline
->next
);
3194 while (tok_type_(t
, TOK_WHITESPACE
))
3196 /* t should now point to the string */
3197 if (!tok_type_(t
, TOK_STRING
)) {
3199 "`%s` requires string as second parameter",
3202 free_tlist(origline
);
3203 return DIRECTIVE_FOUND
;
3207 * Convert the string to a token stream. Note that smacros
3208 * are stored with the token stream reversed, so we have to
3209 * reverse the output of tokenize().
3211 nasm_unquote_cstr(t
->text
, i
);
3212 macro_start
= reverse_tokens(tokenize(t
->text
));
3215 * We now have a macro name, an implicit parameter count of
3216 * zero, and a numeric token to use as an expansion. Create
3217 * and store an SMacro.
3219 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3221 free_tlist(origline
);
3222 return DIRECTIVE_FOUND
;
3227 StrList
*xsl
= NULL
;
3228 StrList
**xst
= &xsl
;
3232 tline
= tline
->next
;
3234 tline
= expand_id(tline
);
3235 if (!tline
|| (tline
->type
!= TOK_ID
&&
3236 (tline
->type
!= TOK_PREPROC_ID
||
3237 tline
->text
[1] != '$'))) {
3239 "`%%pathsearch' expects a macro identifier as first parameter");
3240 free_tlist(origline
);
3241 return DIRECTIVE_FOUND
;
3243 ctx
= get_ctx(tline
->text
, &mname
, false);
3245 tline
= expand_smacro(tline
->next
);
3249 while (tok_type_(t
, TOK_WHITESPACE
))
3252 if (!t
|| (t
->type
!= TOK_STRING
&&
3253 t
->type
!= TOK_INTERNAL_STRING
)) {
3254 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3256 free_tlist(origline
);
3257 return DIRECTIVE_FOUND
; /* but we did _something_ */
3260 error(ERR_WARNING
|ERR_PASS1
,
3261 "trailing garbage after `%%pathsearch' ignored");
3263 if (t
->type
!= TOK_INTERNAL_STRING
)
3264 nasm_unquote(p
, NULL
);
3266 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3269 fclose(fp
); /* Don't actually care about the file */
3271 macro_start
= nasm_malloc(sizeof(*macro_start
));
3272 macro_start
->next
= NULL
;
3273 macro_start
->text
= nasm_quote(p
, strlen(p
));
3274 macro_start
->type
= TOK_STRING
;
3275 macro_start
->a
.mac
= NULL
;
3280 * We now have a macro name, an implicit parameter count of
3281 * zero, and a string token to use as an expansion. Create
3282 * and store an SMacro.
3284 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3286 free_tlist(origline
);
3287 return DIRECTIVE_FOUND
;
3293 tline
= tline
->next
;
3295 tline
= expand_id(tline
);
3296 if (!tline
|| (tline
->type
!= TOK_ID
&&
3297 (tline
->type
!= TOK_PREPROC_ID
||
3298 tline
->text
[1] != '$'))) {
3300 "`%%strlen' expects a macro identifier as first parameter");
3301 free_tlist(origline
);
3302 return DIRECTIVE_FOUND
;
3304 ctx
= get_ctx(tline
->text
, &mname
, false);
3306 tline
= expand_smacro(tline
->next
);
3310 while (tok_type_(t
, TOK_WHITESPACE
))
3312 /* t should now point to the string */
3313 if (!tok_type_(t
, TOK_STRING
)) {
3315 "`%%strlen` requires string as second parameter");
3317 free_tlist(origline
);
3318 return DIRECTIVE_FOUND
;
3321 macro_start
= nasm_malloc(sizeof(*macro_start
));
3322 macro_start
->next
= NULL
;
3323 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3324 macro_start
->a
.mac
= NULL
;
3327 * We now have a macro name, an implicit parameter count of
3328 * zero, and a numeric token to use as an expansion. Create
3329 * and store an SMacro.
3331 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3333 free_tlist(origline
);
3334 return DIRECTIVE_FOUND
;
3339 tline
= tline
->next
;
3341 tline
= expand_id(tline
);
3342 if (!tline
|| (tline
->type
!= TOK_ID
&&
3343 (tline
->type
!= TOK_PREPROC_ID
||
3344 tline
->text
[1] != '$'))) {
3346 "`%%strcat' expects a macro identifier as first parameter");
3347 free_tlist(origline
);
3348 return DIRECTIVE_FOUND
;
3350 ctx
= get_ctx(tline
->text
, &mname
, false);
3352 tline
= expand_smacro(tline
->next
);
3356 list_for_each(t
, tline
) {
3358 case TOK_WHITESPACE
:
3361 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3364 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3366 /* else fall through */
3369 "non-string passed to `%%strcat' (%d)", t
->type
);
3371 free_tlist(origline
);
3372 return DIRECTIVE_FOUND
;
3376 p
= pp
= nasm_malloc(len
);
3377 list_for_each(t
, tline
) {
3378 if (t
->type
== TOK_STRING
) {
3379 memcpy(p
, t
->text
, t
->a
.len
);
3385 * We now have a macro name, an implicit parameter count of
3386 * zero, and a numeric token to use as an expansion. Create
3387 * and store an SMacro.
3389 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3390 macro_start
->text
= nasm_quote(pp
, len
);
3392 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3394 free_tlist(origline
);
3395 return DIRECTIVE_FOUND
;
3399 int64_t start
, count
;
3404 tline
= tline
->next
;
3406 tline
= expand_id(tline
);
3407 if (!tline
|| (tline
->type
!= TOK_ID
&&
3408 (tline
->type
!= TOK_PREPROC_ID
||
3409 tline
->text
[1] != '$'))) {
3411 "`%%substr' expects a macro identifier as first parameter");
3412 free_tlist(origline
);
3413 return DIRECTIVE_FOUND
;
3415 ctx
= get_ctx(tline
->text
, &mname
, false);
3417 tline
= expand_smacro(tline
->next
);
3420 if (tline
) /* skip expanded id */
3422 while (tok_type_(t
, TOK_WHITESPACE
))
3425 /* t should now point to the string */
3426 if (!tok_type_(t
, TOK_STRING
)) {
3428 "`%%substr` requires string as second parameter");
3430 free_tlist(origline
);
3431 return DIRECTIVE_FOUND
;
3436 tokval
.t_type
= TOKEN_INVALID
;
3437 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3441 free_tlist(origline
);
3442 return DIRECTIVE_FOUND
;
3443 } else if (!is_simple(evalresult
)) {
3444 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3446 free_tlist(origline
);
3447 return DIRECTIVE_FOUND
;
3449 start
= evalresult
->value
- 1;
3451 while (tok_type_(tt
, TOK_WHITESPACE
))
3454 count
= 1; /* Backwards compatibility: one character */
3456 tokval
.t_type
= TOKEN_INVALID
;
3457 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3461 free_tlist(origline
);
3462 return DIRECTIVE_FOUND
;
3463 } else if (!is_simple(evalresult
)) {
3464 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3466 free_tlist(origline
);
3467 return DIRECTIVE_FOUND
;
3469 count
= evalresult
->value
;
3472 len
= nasm_unquote(t
->text
, NULL
);
3474 /* make start and count being in range */
3478 count
= len
+ count
+ 1 - start
;
3479 if (start
+ count
> (int64_t)len
)
3480 count
= len
- start
;
3481 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3482 start
= -1, count
= 0; /* empty string */
3484 macro_start
= nasm_malloc(sizeof(*macro_start
));
3485 macro_start
->next
= NULL
;
3486 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3487 macro_start
->type
= TOK_STRING
;
3488 macro_start
->a
.mac
= NULL
;
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a numeric token to use as an expansion. Create
3493 * and store an SMacro.
3495 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3497 free_tlist(origline
);
3498 return DIRECTIVE_FOUND
;
3503 casesense
= (i
== PP_ASSIGN
);
3505 tline
= tline
->next
;
3507 tline
= expand_id(tline
);
3508 if (!tline
|| (tline
->type
!= TOK_ID
&&
3509 (tline
->type
!= TOK_PREPROC_ID
||
3510 tline
->text
[1] != '$'))) {
3512 "`%%%sassign' expects a macro identifier",
3513 (i
== PP_IASSIGN
? "i" : ""));
3514 free_tlist(origline
);
3515 return DIRECTIVE_FOUND
;
3517 ctx
= get_ctx(tline
->text
, &mname
, false);
3519 tline
= expand_smacro(tline
->next
);
3524 tokval
.t_type
= TOKEN_INVALID
;
3526 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3529 free_tlist(origline
);
3530 return DIRECTIVE_FOUND
;
3534 error(ERR_WARNING
|ERR_PASS1
,
3535 "trailing garbage after expression ignored");
3537 if (!is_simple(evalresult
)) {
3539 "non-constant value given to `%%%sassign'",
3540 (i
== PP_IASSIGN
? "i" : ""));
3541 free_tlist(origline
);
3542 return DIRECTIVE_FOUND
;
3545 macro_start
= nasm_malloc(sizeof(*macro_start
));
3546 macro_start
->next
= NULL
;
3547 make_tok_num(macro_start
, reloc_value(evalresult
));
3548 macro_start
->a
.mac
= NULL
;
3551 * We now have a macro name, an implicit parameter count of
3552 * zero, and a numeric token to use as an expansion. Create
3553 * and store an SMacro.
3555 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3556 free_tlist(origline
);
3557 return DIRECTIVE_FOUND
;
3561 * Syntax is `%line nnn[+mmm] [filename]'
3563 tline
= tline
->next
;
3565 if (!tok_type_(tline
, TOK_NUMBER
)) {
3566 error(ERR_NONFATAL
, "`%%line' expects line number");
3567 free_tlist(origline
);
3568 return DIRECTIVE_FOUND
;
3570 k
= readnum(tline
->text
, &err
);
3572 tline
= tline
->next
;
3573 if (tok_is_(tline
, "+")) {
3574 tline
= tline
->next
;
3575 if (!tok_type_(tline
, TOK_NUMBER
)) {
3576 error(ERR_NONFATAL
, "`%%line' expects line increment");
3577 free_tlist(origline
);
3578 return DIRECTIVE_FOUND
;
3580 m
= readnum(tline
->text
, &err
);
3581 tline
= tline
->next
;
3587 nasm_free(src_set_fname(detoken(tline
, false)));
3589 free_tlist(origline
);
3590 return DIRECTIVE_FOUND
;
3594 "preprocessor directive `%s' not yet implemented",
3596 return DIRECTIVE_FOUND
;
3601 * Ensure that a macro parameter contains a condition code and
3602 * nothing else. Return the condition code index if so, or -1
3605 static int find_cc(Token
* t
)
3611 return -1; /* Probably a %+ without a space */
3614 if (t
->type
!= TOK_ID
)
3618 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3622 j
= ARRAY_SIZE(conditions
);
3625 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3640 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3642 Token
**tail
, *t
, *tt
;
3644 bool did_paste
= false;
3647 /* Now handle token pasting... */
3650 while ((t
= *tail
) && (tt
= t
->next
)) {
3652 case TOK_WHITESPACE
:
3653 if (tt
->type
== TOK_WHITESPACE
) {
3654 /* Zap adjacent whitespace tokens */
3655 t
->next
= delete_Token(tt
);
3657 /* Do not advance paste_head here */
3662 case TOK_PREPROC_ID
:
3669 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3670 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3671 tt
->type
== TOK_OTHER
)) {
3672 len
+= strlen(tt
->text
);
3677 * Now tt points to the first token after
3678 * the potential paste area...
3680 if (tt
!= t
->next
) {
3681 /* We have at least two tokens... */
3682 len
+= strlen(t
->text
);
3683 p
= tmp
= nasm_malloc(len
+1);
3687 p
= strchr(p
, '\0');
3688 t
= delete_Token(t
);
3691 t
= *tail
= tokenize(tmp
);
3698 t
->next
= tt
; /* Attach the remaining token chain */
3706 case TOK_PASTE
: /* %+ */
3707 if (handle_paste_tokens
) {
3708 /* Zap %+ and whitespace tokens to the right */
3709 while (t
&& (t
->type
== TOK_WHITESPACE
||
3710 t
->type
== TOK_PASTE
))
3711 t
= *tail
= delete_Token(t
);
3712 if (!paste_head
|| !t
)
3713 break; /* Nothing to paste with */
3717 while (tok_type_(tt
, TOK_WHITESPACE
))
3718 tt
= t
->next
= delete_Token(tt
);
3721 tmp
= nasm_strcat(t
->text
, tt
->text
);
3723 tt
= delete_Token(tt
);
3724 t
= *tail
= tokenize(tmp
);
3730 t
->next
= tt
; /* Attach the remaining token chain */
3737 /* else fall through */
3740 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3749 * expands to a list of tokens from %{x:y}
3751 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3753 Token
*t
= tline
, **tt
, *tm
, *head
;
3757 pos
= strchr(tline
->text
, ':');
3760 lst
= atoi(pos
+ 1);
3761 fst
= atoi(tline
->text
+ 1);
3764 * only macros params are accounted so
3765 * if someone passes %0 -- we reject such
3768 if (lst
== 0 || fst
== 0)
3771 /* the values should be sane */
3772 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3773 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3776 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3777 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3779 /* counted from zero */
3783 * it will be at least one token
3785 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3786 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3787 head
= t
, tt
= &t
->next
;
3789 for (i
= fst
+ 1; i
<= lst
; i
++) {
3790 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3791 *tt
= t
, tt
= &t
->next
;
3792 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3793 tm
= mac
->params
[j
];
3794 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3795 *tt
= t
, tt
= &t
->next
;
3798 for (i
= fst
- 1; i
>= lst
; i
--) {
3799 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3800 *tt
= t
, tt
= &t
->next
;
3801 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3802 tm
= mac
->params
[j
];
3803 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3804 *tt
= t
, tt
= &t
->next
;
3812 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3818 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3819 * %-n) and MMacro-local identifiers (%%foo) as well as
3820 * macro indirection (%[...]) and range (%{..:..}).
3822 static Token
*expand_mmac_params(Token
* tline
)
3824 Token
*t
, *tt
, **tail
, *thead
;
3825 bool changed
= false;
3832 if (tline
->type
== TOK_PREPROC_ID
&&
3833 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3834 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3835 tline
->text
[1] == '%')) {
3837 int type
= 0, cc
; /* type = 0 to placate optimisers */
3844 tline
= tline
->next
;
3847 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3848 mac
= mac
->next_active
;
3850 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3852 pos
= strchr(t
->text
, ':');
3854 switch (t
->text
[1]) {
3856 * We have to make a substitution of one of the
3857 * forms %1, %-1, %+1, %%foo, %0.
3861 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3862 text
= nasm_strdup(tmpbuf
);
3866 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3868 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3871 n
= atoi(t
->text
+ 2) - 1;
3872 if (n
>= mac
->nparam
)
3875 if (mac
->nparam
> 1)
3876 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3877 tt
= mac
->params
[n
];
3882 "macro parameter %d is not a condition code",
3887 if (inverse_ccs
[cc
] == -1) {
3889 "condition code `%s' is not invertible",
3893 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3897 n
= atoi(t
->text
+ 2) - 1;
3898 if (n
>= mac
->nparam
)
3901 if (mac
->nparam
> 1)
3902 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3903 tt
= mac
->params
[n
];
3908 "macro parameter %d is not a condition code",
3913 text
= nasm_strdup(conditions
[cc
]);
3917 n
= atoi(t
->text
+ 1) - 1;
3918 if (n
>= mac
->nparam
)
3921 if (mac
->nparam
> 1)
3922 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3923 tt
= mac
->params
[n
];
3926 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3927 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3928 tail
= &(*tail
)->next
;
3932 text
= NULL
; /* we've done it here */
3937 * seems we have a parameters range here
3939 Token
*head
, **last
;
3940 head
= expand_mmac_params_range(mac
, t
, &last
);
3961 } else if (tline
->type
== TOK_INDIRECT
) {
3963 tline
= tline
->next
;
3964 tt
= tokenize(t
->text
);
3965 tt
= expand_mmac_params(tt
);
3966 tt
= expand_smacro(tt
);
3969 tt
->a
.mac
= NULL
; /* Necessary? */
3975 } else if (tline
->type
== TOK_PREPROC_ID
&&
3976 tline
->text
[0] == '%' &&
3977 tline
->text
[1] == '$' &&
3978 !tok_type_(tline
->next
, TOK_WHITESPACE
) &&
3979 (tok_type_(tline
->next
, TOK_ID
) ||
3980 tok_type_(tline
->next
, TOK_PREPROC_ID
) ||
3981 tok_type_(tline
->next
, TOK_NUMBER
) ||
3982 tok_type_(tline
->next
, TOK_OTHER
) ||
3983 tok_type_(tline
->next
, TOK_FLOAT
))) {
3985 * In a sake of backward compatibility we allow
3986 * to expand local single macro that early before
3987 * pasting token code have place
3989 * NOTE: that new code MUST use %+ macro to obtain
3993 tline
= tline
->next
;
3994 tt
= tokenize(t
->text
);
3995 tt
= expand_smacro(tt
);
4006 tline
= tline
->next
;
4014 paste_tokens(&thead
, false);
4020 * Expand all single-line macro calls made in the given line.
4021 * Return the expanded version of the line. The original is deemed
4022 * to be destroyed in the process. (In reality we'll just move
4023 * Tokens from input to output a lot of the time, rather than
4024 * actually bothering to destroy and replicate.)
4027 static Token
*expand_smacro(Token
* tline
)
4029 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4030 SMacro
*head
= NULL
, *m
;
4033 unsigned int nparam
, sparam
;
4035 Token
*org_tline
= tline
;
4038 int deadman
= DEADMAN_LIMIT
;
4042 * Trick: we should avoid changing the start token pointer since it can
4043 * be contained in "next" field of other token. Because of this
4044 * we allocate a copy of first token and work with it; at the end of
4045 * routine we copy it back
4048 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4049 org_tline
->text
, 0);
4050 tline
->a
.mac
= org_tline
->a
.mac
;
4051 nasm_free(org_tline
->text
);
4052 org_tline
->text
= NULL
;
4055 expanded
= true; /* Always expand %+ at least once */
4061 while (tline
) { /* main token loop */
4063 error(ERR_NONFATAL
, "interminable macro recursion");
4067 if ((mname
= tline
->text
)) {
4068 /* if this token is a local macro, look in local context */
4069 if (tline
->type
== TOK_ID
) {
4070 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4071 } else if (tline
->type
== TOK_PREPROC_ID
) {
4072 ctx
= get_ctx(mname
, &mname
, true);
4073 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4078 * We've hit an identifier. As in is_mmacro below, we first
4079 * check whether the identifier is a single-line macro at
4080 * all, then think about checking for parameters if
4083 list_for_each(m
, head
)
4084 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4090 if (m
->nparam
== 0) {
4092 * Simple case: the macro is parameterless. Discard the
4093 * one token that the macro call took, and push the
4094 * expansion back on the to-do stack.
4096 if (!m
->expansion
) {
4097 if (!strcmp("__FILE__", m
->name
)) {
4100 src_get(&num
, &file
);
4101 tline
->text
= nasm_quote(file
, strlen(file
));
4102 tline
->type
= TOK_STRING
;
4106 if (!strcmp("__LINE__", m
->name
)) {
4107 nasm_free(tline
->text
);
4108 make_tok_num(tline
, src_get_linnum());
4111 if (!strcmp("__BITS__", m
->name
)) {
4112 nasm_free(tline
->text
);
4113 make_tok_num(tline
, globalbits
);
4116 tline
= delete_Token(tline
);
4121 * Complicated case: at least one macro with this name
4122 * exists and takes parameters. We must find the
4123 * parameters in the call, count them, find the SMacro
4124 * that corresponds to that form of the macro call, and
4125 * substitute for the parameters when we expand. What a
4128 /*tline = tline->next;
4129 skip_white_(tline); */
4132 while (tok_type_(t
, TOK_SMAC_END
)) {
4133 t
->a
.mac
->in_progress
= false;
4135 t
= tline
->next
= delete_Token(t
);
4138 } while (tok_type_(tline
, TOK_WHITESPACE
));
4139 if (!tok_is_(tline
, "(")) {
4141 * This macro wasn't called with parameters: ignore
4142 * the call. (Behaviour borrowed from gnu cpp.)
4151 sparam
= PARAM_DELTA
;
4152 params
= nasm_malloc(sparam
* sizeof(Token
*));
4153 params
[0] = tline
->next
;
4154 paramsize
= nasm_malloc(sparam
* sizeof(int));
4156 while (true) { /* parameter loop */
4158 * For some unusual expansions
4159 * which concatenates function call
4162 while (tok_type_(t
, TOK_SMAC_END
)) {
4163 t
->a
.mac
->in_progress
= false;
4165 t
= tline
->next
= delete_Token(t
);
4171 "macro call expects terminating `)'");
4174 if (tline
->type
== TOK_WHITESPACE
4176 if (paramsize
[nparam
])
4179 params
[nparam
] = tline
->next
;
4180 continue; /* parameter loop */
4182 if (tline
->type
== TOK_OTHER
4183 && tline
->text
[1] == 0) {
4184 char ch
= tline
->text
[0];
4185 if (ch
== ',' && !paren
&& brackets
<= 0) {
4186 if (++nparam
>= sparam
) {
4187 sparam
+= PARAM_DELTA
;
4188 params
= nasm_realloc(params
,
4189 sparam
* sizeof(Token
*));
4190 paramsize
= nasm_realloc(paramsize
,
4191 sparam
* sizeof(int));
4193 params
[nparam
] = tline
->next
;
4194 paramsize
[nparam
] = 0;
4196 continue; /* parameter loop */
4199 (brackets
> 0 || (brackets
== 0 &&
4200 !paramsize
[nparam
])))
4202 if (!(brackets
++)) {
4203 params
[nparam
] = tline
->next
;
4204 continue; /* parameter loop */
4207 if (ch
== '}' && brackets
> 0)
4208 if (--brackets
== 0) {
4210 continue; /* parameter loop */
4212 if (ch
== '(' && !brackets
)
4214 if (ch
== ')' && brackets
<= 0)
4220 error(ERR_NONFATAL
, "braces do not "
4221 "enclose all of macro parameter");
4223 paramsize
[nparam
] += white
+ 1;
4225 } /* parameter loop */
4227 while (m
&& (m
->nparam
!= nparam
||
4228 mstrcmp(m
->name
, mname
,
4232 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4233 "macro `%s' exists, "
4234 "but not taking %d parameters",
4235 mstart
->text
, nparam
);
4238 if (m
&& m
->in_progress
)
4240 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4242 * Design question: should we handle !tline, which
4243 * indicates missing ')' here, or expand those
4244 * macros anyway, which requires the (t) test a few
4248 nasm_free(paramsize
);
4252 * Expand the macro: we are placed on the last token of the
4253 * call, so that we can easily split the call from the
4254 * following tokens. We also start by pushing an SMAC_END
4255 * token for the cycle removal.
4262 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4264 m
->in_progress
= true;
4266 list_for_each(t
, m
->expansion
) {
4267 if (t
->type
>= TOK_SMAC_PARAM
) {
4268 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4272 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4273 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4275 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4281 } else if (t
->type
== TOK_PREPROC_Q
) {
4282 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4284 } else if (t
->type
== TOK_PREPROC_QQ
) {
4285 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4288 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4294 * Having done that, get rid of the macro call, and clean
4295 * up the parameters.
4298 nasm_free(paramsize
);
4301 continue; /* main token loop */
4306 if (tline
->type
== TOK_SMAC_END
) {
4307 tline
->a
.mac
->in_progress
= false;
4308 tline
= delete_Token(tline
);
4311 tline
= tline
->next
;
4319 * Now scan the entire line and look for successive TOK_IDs that resulted
4320 * after expansion (they can't be produced by tokenize()). The successive
4321 * TOK_IDs should be concatenated.
4322 * Also we look for %+ tokens and concatenate the tokens before and after
4323 * them (without white spaces in between).
4325 if (expanded
&& paste_tokens(&thead
, true)) {
4327 * If we concatenated something, *and* we had previously expanded
4328 * an actual macro, scan the lines again for macros...
4338 *org_tline
= *thead
;
4339 /* since we just gave text to org_line, don't free it */
4341 delete_Token(thead
);
4343 /* the expression expanded to empty line;
4344 we can't return NULL for some reasons
4345 we just set the line to a single WHITESPACE token. */
4346 memset(org_tline
, 0, sizeof(*org_tline
));
4347 org_tline
->text
= NULL
;
4348 org_tline
->type
= TOK_WHITESPACE
;
4357 * Similar to expand_smacro but used exclusively with macro identifiers
4358 * right before they are fetched in. The reason is that there can be
4359 * identifiers consisting of several subparts. We consider that if there
4360 * are more than one element forming the name, user wants a expansion,
4361 * otherwise it will be left as-is. Example:
4365 * the identifier %$abc will be left as-is so that the handler for %define
4366 * will suck it and define the corresponding value. Other case:
4368 * %define _%$abc cde
4370 * In this case user wants name to be expanded *before* %define starts
4371 * working, so we'll expand %$abc into something (if it has a value;
4372 * otherwise it will be left as-is) then concatenate all successive
4375 static Token
*expand_id(Token
* tline
)
4377 Token
*cur
, *oldnext
= NULL
;
4379 if (!tline
|| !tline
->next
)
4384 (cur
->next
->type
== TOK_ID
||
4385 cur
->next
->type
== TOK_PREPROC_ID
4386 || cur
->next
->type
== TOK_NUMBER
))
4389 /* If identifier consists of just one token, don't expand */
4394 oldnext
= cur
->next
; /* Detach the tail past identifier */
4395 cur
->next
= NULL
; /* so that expand_smacro stops here */
4398 tline
= expand_smacro(tline
);
4401 /* expand_smacro possibly changhed tline; re-scan for EOL */
4403 while (cur
&& cur
->next
)
4406 cur
->next
= oldnext
;
4413 * Determine whether the given line constitutes a multi-line macro
4414 * call, and return the MMacro structure called if so. Doesn't have
4415 * to check for an initial label - that's taken care of in
4416 * expand_mmacro - but must check numbers of parameters. Guaranteed
4417 * to be called with tline->type == TOK_ID, so the putative macro
4418 * name is easy to find.
4420 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4426 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4429 * Efficiency: first we see if any macro exists with the given
4430 * name. If not, we can return NULL immediately. _Then_ we
4431 * count the parameters, and then we look further along the
4432 * list if necessary to find the proper MMacro.
4434 list_for_each(m
, head
)
4435 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4441 * OK, we have a potential macro. Count and demarcate the
4444 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4447 * So we know how many parameters we've got. Find the MMacro
4448 * structure that handles this number.
4451 if (m
->nparam_min
<= nparam
4452 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4454 * This one is right. Just check if cycle removal
4455 * prohibits us using it before we actually celebrate...
4457 if (m
->in_progress
> m
->max_depth
) {
4458 if (m
->max_depth
> 0) {
4460 "reached maximum recursion depth of %i",
4467 * It's right, and we can use it. Add its default
4468 * parameters to the end of our list if necessary.
4470 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4472 nasm_realloc(params
,
4473 ((m
->nparam_min
+ m
->ndefs
+
4474 1) * sizeof(*params
)));
4475 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4476 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4481 * If we've gone over the maximum parameter count (and
4482 * we're in Plus mode), ignore parameters beyond
4485 if (m
->plus
&& nparam
> m
->nparam_max
)
4486 nparam
= m
->nparam_max
;
4488 * Then terminate the parameter list, and leave.
4490 if (!params
) { /* need this special case */
4491 params
= nasm_malloc(sizeof(*params
));
4494 params
[nparam
] = NULL
;
4495 *params_array
= params
;
4499 * This one wasn't right: look for the next one with the
4502 list_for_each(m
, m
->next
)
4503 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4508 * After all that, we didn't find one with the right number of
4509 * parameters. Issue a warning, and fail to expand the macro.
4511 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4512 "macro `%s' exists, but not taking %d parameters",
4513 tline
->text
, nparam
);
4520 * Save MMacro invocation specific fields in
4521 * preparation for a recursive macro expansion
4523 static void push_mmacro(MMacro
*m
)
4525 MMacroInvocation
*i
;
4527 i
= nasm_malloc(sizeof(MMacroInvocation
));
4529 i
->params
= m
->params
;
4530 i
->iline
= m
->iline
;
4531 i
->nparam
= m
->nparam
;
4532 i
->rotate
= m
->rotate
;
4533 i
->paramlen
= m
->paramlen
;
4534 i
->unique
= m
->unique
;
4535 i
->condcnt
= m
->condcnt
;
4541 * Restore MMacro invocation specific fields that were
4542 * saved during a previous recursive macro expansion
4544 static void pop_mmacro(MMacro
*m
)
4546 MMacroInvocation
*i
;
4551 m
->params
= i
->params
;
4552 m
->iline
= i
->iline
;
4553 m
->nparam
= i
->nparam
;
4554 m
->rotate
= i
->rotate
;
4555 m
->paramlen
= i
->paramlen
;
4556 m
->unique
= i
->unique
;
4557 m
->condcnt
= i
->condcnt
;
4564 * Expand the multi-line macro call made by the given line, if
4565 * there is one to be expanded. If there is, push the expansion on
4566 * istk->expansion and return 1. Otherwise return 0.
4568 static int expand_mmacro(Token
* tline
)
4570 Token
*startline
= tline
;
4571 Token
*label
= NULL
;
4572 int dont_prepend
= 0;
4573 Token
**params
, *t
, *mtok
, *tt
;
4576 int i
, nparam
, *paramlen
;
4581 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4582 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4585 m
= is_mmacro(t
, ¶ms
);
4591 * We have an id which isn't a macro call. We'll assume
4592 * it might be a label; we'll also check to see if a
4593 * colon follows it. Then, if there's another id after
4594 * that lot, we'll check it again for macro-hood.
4598 if (tok_type_(t
, TOK_WHITESPACE
))
4599 last
= t
, t
= t
->next
;
4600 if (tok_is_(t
, ":")) {
4602 last
= t
, t
= t
->next
;
4603 if (tok_type_(t
, TOK_WHITESPACE
))
4604 last
= t
, t
= t
->next
;
4606 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4614 * Fix up the parameters: this involves stripping leading and
4615 * trailing whitespace, then stripping braces if they are
4618 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4619 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4621 for (i
= 0; params
[i
]; i
++) {
4623 int comma
= (!m
->plus
|| i
< nparam
- 1);
4627 if (tok_is_(t
, "{"))
4628 t
= t
->next
, brace
= true, comma
= false;
4632 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4633 break; /* ... because we have hit a comma */
4634 if (comma
&& t
->type
== TOK_WHITESPACE
4635 && tok_is_(t
->next
, ","))
4636 break; /* ... or a space then a comma */
4637 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4638 break; /* ... or a brace */
4645 * OK, we have a MMacro structure together with a set of
4646 * parameters. We must now go through the expansion and push
4647 * copies of each Line on to istk->expansion. Substitution of
4648 * parameter tokens and macro-local tokens doesn't get done
4649 * until the single-line macro substitution process; this is
4650 * because delaying them allows us to change the semantics
4651 * later through %rotate.
4653 * First, push an end marker on to istk->expansion, mark this
4654 * macro as in progress, and set up its invocation-specific
4657 ll
= nasm_malloc(sizeof(Line
));
4658 ll
->next
= istk
->expansion
;
4661 istk
->expansion
= ll
;
4664 * Save the previous MMacro expansion in the case of
4667 if (m
->max_depth
&& m
->in_progress
)
4675 m
->paramlen
= paramlen
;
4676 m
->unique
= unique
++;
4680 m
->next_active
= istk
->mstk
;
4683 list_for_each(l
, m
->expansion
) {
4686 ll
= nasm_malloc(sizeof(Line
));
4687 ll
->finishes
= NULL
;
4688 ll
->next
= istk
->expansion
;
4689 istk
->expansion
= ll
;
4692 list_for_each(t
, l
->first
) {
4696 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4698 case TOK_PREPROC_QQ
:
4699 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4701 case TOK_PREPROC_ID
:
4702 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4710 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4719 * If we had a label, push it on as the first line of
4720 * the macro expansion.
4723 if (dont_prepend
< 0)
4724 free_tlist(startline
);
4726 ll
= nasm_malloc(sizeof(Line
));
4727 ll
->finishes
= NULL
;
4728 ll
->next
= istk
->expansion
;
4729 istk
->expansion
= ll
;
4730 ll
->first
= startline
;
4731 if (!dont_prepend
) {
4733 label
= label
->next
;
4734 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4739 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4744 /* The function that actually does the error reporting */
4745 static void verror(int severity
, const char *fmt
, va_list arg
)
4748 MMacro
*mmac
= NULL
;
4751 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4753 /* get %macro name */
4754 if (istk
&& istk
->mstk
) {
4756 /* but %rep blocks should be skipped */
4757 while (mmac
&& !mmac
->name
)
4758 mmac
= mmac
->next_active
, delta
++;
4762 nasm_error(severity
, "(%s:%d) %s",
4763 mmac
->name
, mmac
->lineno
- delta
, buff
);
4765 nasm_error(severity
, "%s", buff
);
4769 * Since preprocessor always operate only on the line that didn't
4770 * arrived yet, we should always use ERR_OFFBY1.
4772 static void error(int severity
, const char *fmt
, ...)
4776 /* If we're in a dead branch of IF or something like it, ignore the error */
4777 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4781 verror(severity
, fmt
, arg
);
4786 * Because %else etc are evaluated in the state context
4787 * of the previous branch, errors might get lost with error():
4788 * %if 0 ... %else trailing garbage ... %endif
4789 * So %else etc should report errors with this function.
4791 static void error_precond(int severity
, const char *fmt
, ...)
4795 /* Only ignore the error if it's really in a dead branch */
4796 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4800 verror(severity
, fmt
, arg
);
4805 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4810 istk
= nasm_malloc(sizeof(Include
));
4813 istk
->expansion
= NULL
;
4815 istk
->fp
= fopen(file
, "r");
4817 src_set_fname(nasm_strdup(file
));
4821 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4824 nested_mac_count
= 0;
4825 nested_rep_count
= 0;
4828 if (tasm_compatible_mode
) {
4829 stdmacpos
= nasm_stdmac
;
4831 stdmacpos
= nasm_stdmac_after_tasm
;
4833 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4838 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4839 * The caller, however, will also pass in 3 for preprocess-only so
4840 * we can set __PASS__ accordingly.
4842 pass
= apass
> 2 ? 2 : apass
;
4844 dephead
= deptail
= deplist
;
4846 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4848 strcpy(sl
->str
, file
);
4850 deptail
= &sl
->next
;
4854 * Define the __PASS__ macro. This is defined here unlike
4855 * all the other builtins, because it is special -- it varies between
4858 t
= nasm_malloc(sizeof(*t
));
4860 make_tok_num(t
, apass
);
4862 define_smacro(NULL
, "__PASS__", true, 0, t
);
4865 static char *pp_getline(void)
4872 * Fetch a tokenized line, either from the macro-expansion
4873 * buffer or from the input file.
4876 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4877 Line
*l
= istk
->expansion
;
4878 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4882 * This is a macro-end marker for a macro with no
4883 * name, which means it's not really a macro at all
4884 * but a %rep block, and the `in_progress' field is
4885 * more than 1, meaning that we still need to
4886 * repeat. (1 means the natural last repetition; 0
4887 * means termination by %exitrep.) We have
4888 * therefore expanded up to the %endrep, and must
4889 * push the whole block on to the expansion buffer
4890 * again. We don't bother to remove the macro-end
4891 * marker: we'd only have to generate another one
4894 l
->finishes
->in_progress
--;
4895 list_for_each(l
, l
->finishes
->expansion
) {
4896 Token
*t
, *tt
, **tail
;
4898 ll
= nasm_malloc(sizeof(Line
));
4899 ll
->next
= istk
->expansion
;
4900 ll
->finishes
= NULL
;
4904 list_for_each(t
, l
->first
) {
4905 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4906 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4911 istk
->expansion
= ll
;
4915 * Check whether a `%rep' was started and not ended
4916 * within this macro expansion. This can happen and
4917 * should be detected. It's a fatal error because
4918 * I'm too confused to work out how to recover
4924 "defining with name in expansion");
4925 else if (istk
->mstk
->name
)
4927 "`%%rep' without `%%endrep' within"
4928 " expansion of macro `%s'",
4933 * FIXME: investigate the relationship at this point between
4934 * istk->mstk and l->finishes
4937 MMacro
*m
= istk
->mstk
;
4938 istk
->mstk
= m
->next_active
;
4941 * This was a real macro call, not a %rep, and
4942 * therefore the parameter information needs to
4947 l
->finishes
->in_progress
--;
4949 nasm_free(m
->params
);
4950 free_tlist(m
->iline
);
4951 nasm_free(m
->paramlen
);
4952 l
->finishes
->in_progress
= 0;
4957 istk
->expansion
= l
->next
;
4959 list
->downlevel(LIST_MACRO
);
4962 while (1) { /* until we get a line we can use */
4964 if (istk
->expansion
) { /* from a macro expansion */
4966 Line
*l
= istk
->expansion
;
4968 istk
->mstk
->lineno
++;
4970 istk
->expansion
= l
->next
;
4972 p
= detoken(tline
, false);
4973 list
->line(LIST_MACRO
, p
);
4978 if (line
) { /* from the current input file */
4979 line
= prepreproc(line
);
4980 tline
= tokenize(line
);
4985 * The current file has ended; work down the istk
4991 /* nasm_error can't be conditionally suppressed */
4992 nasm_error(ERR_FATAL
,
4993 "expected `%%endif' before end of file");
4995 /* only set line and file name if there's a next node */
4997 src_set_linnum(i
->lineno
);
4998 nasm_free(src_set_fname(i
->fname
));
5001 list
->downlevel(LIST_INCLUDE
);
5005 if (istk
->expansion
&& istk
->expansion
->finishes
)
5011 * We must expand MMacro parameters and MMacro-local labels
5012 * _before_ we plunge into directive processing, to cope
5013 * with things like `%define something %1' such as STRUC
5014 * uses. Unless we're _defining_ a MMacro, in which case
5015 * those tokens should be left alone to go into the
5016 * definition; and unless we're in a non-emitting
5017 * condition, in which case we don't want to meddle with
5020 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
5021 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
5022 tline
= expand_mmac_params(tline
);
5026 * Check the line to see if it's a preprocessor directive.
5028 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5030 } else if (defining
) {
5032 * We're defining a multi-line macro. We emit nothing
5034 * shove the tokenized line on to the macro definition.
5036 Line
*l
= nasm_malloc(sizeof(Line
));
5037 l
->next
= defining
->expansion
;
5040 defining
->expansion
= l
;
5042 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
5044 * We're in a non-emitting branch of a condition block.
5045 * Emit nothing at all, not even a blank line: when we
5046 * emerge from the condition we'll give a line-number
5047 * directive so we keep our place correctly.
5051 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5053 * We're in a %rep block which has been terminated, so
5054 * we're walking through to the %endrep without
5055 * emitting anything. Emit nothing at all, not even a
5056 * blank line: when we emerge from the %rep block we'll
5057 * give a line-number directive so we keep our place
5063 tline
= expand_smacro(tline
);
5064 if (!expand_mmacro(tline
)) {
5066 * De-tokenize the line again, and emit it.
5068 line
= detoken(tline
, true);
5072 continue; /* expand_mmacro calls free_tlist */
5080 static void pp_cleanup(int pass
)
5083 if (defining
->name
) {
5085 "end of file while still defining macro `%s'",
5088 error(ERR_NONFATAL
, "end of file while still in %%rep");
5091 free_mmacro(defining
);
5101 nasm_free(i
->fname
);
5106 nasm_free(src_set_fname(NULL
));
5111 while ((i
= ipath
)) {
5120 void pp_include_path(char *path
)
5124 i
= nasm_malloc(sizeof(IncPath
));
5125 i
->path
= path
? nasm_strdup(path
) : NULL
;
5138 void pp_pre_include(char *fname
)
5140 Token
*inc
, *space
, *name
;
5143 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5144 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5145 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5147 l
= nasm_malloc(sizeof(Line
));
5154 void pp_pre_define(char *definition
)
5160 equals
= strchr(definition
, '=');
5161 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5162 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5165 space
->next
= tokenize(definition
);
5169 l
= nasm_malloc(sizeof(Line
));
5176 void pp_pre_undefine(char *definition
)
5181 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5182 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5183 space
->next
= tokenize(definition
);
5185 l
= nasm_malloc(sizeof(Line
));
5193 * Added by Keith Kanios:
5195 * This function is used to assist with "runtime" preprocessor
5196 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5198 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5199 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5202 void pp_runtime(char *definition
)
5206 def
= tokenize(definition
);
5207 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5212 void pp_extra_stdmac(macros_t
*macros
)
5214 extrastdmac
= macros
;
5217 static void make_tok_num(Token
* tok
, int64_t val
)
5220 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5221 tok
->text
= nasm_strdup(numbuf
);
5222 tok
->type
= TOK_NUMBER
;