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 */
215 #define PP_CONCAT_MASK(x) (1 << (x))
221 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
222 size_t len
; /* scratch length field */
223 } a
; /* Auxiliary data */
224 enum pp_token_type type
;
228 * Multi-line macro definitions are stored as a linked list of
229 * these, which is essentially a container to allow several linked
232 * Note that in this module, linked lists are treated as stacks
233 * wherever possible. For this reason, Lines are _pushed_ on to the
234 * `expansion' field in MMacro structures, so that the linked list,
235 * if walked, would give the macro lines in reverse order; this
236 * means that we can walk the list when expanding a macro, and thus
237 * push the lines on to the `expansion' field in _istk_ in reverse
238 * order (so that when popped back off they are in the right
239 * order). It may seem cockeyed, and it relies on my design having
240 * an even number of steps in, but it works...
242 * Some of these structures, rather than being actual lines, are
243 * markers delimiting the end of the expansion of a given macro.
244 * This is for use in the cycle-tracking and %rep-handling code.
245 * Such structures have `finishes' non-NULL, and `first' NULL. All
246 * others have `finishes' NULL, but `first' may still be NULL if
256 * To handle an arbitrary level of file inclusion, we maintain a
257 * stack (ie linked list) of these things.
266 MMacro
*mstk
; /* stack of active macros/reps */
270 * Include search path. This is simply a list of strings which get
271 * prepended, in turn, to the name of an include file, in an
272 * attempt to find the file if it's not in the current directory.
280 * Conditional assembly: we maintain a separate stack of these for
281 * each level of file inclusion. (The only reason we keep the
282 * stacks separate is to ensure that a stray `%endif' in a file
283 * included from within the true branch of a `%if' won't terminate
284 * it and cause confusion: instead, rightly, it'll cause an error.)
292 * These states are for use just after %if or %elif: IF_TRUE
293 * means the condition has evaluated to truth so we are
294 * currently emitting, whereas IF_FALSE means we are not
295 * currently emitting but will start doing so if a %else comes
296 * up. In these states, all directives are admissible: %elif,
297 * %else and %endif. (And of course %if.)
299 COND_IF_TRUE
, COND_IF_FALSE
,
301 * These states come up after a %else: ELSE_TRUE means we're
302 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
303 * any %elif or %else will cause an error.
305 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
307 * These states mean that we're not emitting now, and also that
308 * nothing until %endif will be emitted at all. COND_DONE is
309 * used when we've had our moment of emission
310 * and have now started seeing %elifs. COND_NEVER is used when
311 * the condition construct in question is contained within a
312 * non-emitting branch of a larger condition construct,
313 * or if there is an error.
315 COND_DONE
, COND_NEVER
317 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
320 * These defines are used as the possible return values for do_directive
322 #define NO_DIRECTIVE_FOUND 0
323 #define DIRECTIVE_FOUND 1
326 * This define sets the upper limit for smacro and recursive mmacro
329 #define DEADMAN_LIMIT (1 << 20)
332 #define REP_LIMIT ((INT64_C(1) << 62))
335 * Condition codes. Note that we use c_ prefix not C_ because C_ is
336 * used in nasm.h for the "real" condition codes. At _this_ level,
337 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
338 * ones, so we need a different enum...
340 static const char * const conditions
[] = {
341 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
342 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
343 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
346 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
347 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
348 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
351 static const enum pp_conds inverse_ccs
[] = {
352 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
353 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
,
354 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
360 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
361 static int is_condition(enum preproc_token arg
)
363 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
366 /* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
373 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
374 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
377 static const char * const tasm_directives
[] = {
378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
382 static int StackSize
= 4;
383 static char *StackPointer
= "ebp";
384 static int ArgOffset
= 8;
385 static int LocalOffset
= 0;
387 static Context
*cstk
;
388 static Include
*istk
;
389 static IncPath
*ipath
= NULL
;
391 static int pass
; /* HACK: pass 0 = generate dependencies only */
392 static StrList
**dephead
, **deptail
; /* Dependency list */
394 static uint64_t unique
; /* unique identifier numbers */
396 static Line
*predef
= NULL
;
397 static bool do_predef
;
399 static ListGen
*list
;
402 * The current set of multi-line macros we have defined.
404 static struct hash_table mmacros
;
407 * The current set of single-line macros we have defined.
409 static struct hash_table smacros
;
412 * The multi-line macro we are currently defining, or the %rep
413 * block we are currently reading, if any.
415 static MMacro
*defining
;
417 static uint64_t nested_mac_count
;
418 static uint64_t nested_rep_count
;
421 * The number of macro parameters to allocate space for at a time.
423 #define PARAM_DELTA 16
426 * The standard macro set: defined in macros.c in the array nasm_stdmac.
427 * This gives our position in the macro set, when we're processing it.
429 static macros_t
*stdmacpos
;
432 * The extra standard macros that come from the object format, if
435 static macros_t
*extrastdmac
= NULL
;
436 static bool any_extrastdmac
;
439 * Tokens are allocated in blocks to improve speed
441 #define TOKEN_BLOCKSIZE 4096
442 static Token
*freeTokens
= NULL
;
448 static Blocks blocks
= { NULL
, NULL
};
451 * Forward declarations.
453 static Token
*expand_mmac_params(Token
* tline
);
454 static Token
*expand_smacro(Token
* tline
);
455 static Token
*expand_id(Token
* tline
);
456 static Context
*get_ctx(const char *name
, const char **namep
,
458 static void make_tok_num(Token
* tok
, int64_t val
);
459 static void error(int severity
, const char *fmt
, ...);
460 static void error_precond(int severity
, const char *fmt
, ...);
461 static void *new_Block(size_t size
);
462 static void delete_Blocks(void);
463 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
464 const char *text
, int txtlen
);
465 static Token
*delete_Token(Token
* t
);
468 * Macros for safe checking of token pointers, avoid *(NULL)
470 #define tok_type_(x,t) ((x) && (x)->type == (t))
471 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
472 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
473 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
476 * nasm_unquote with error if the string contains NUL characters.
477 * If the string contains NUL characters, issue an error and return
478 * the C len, i.e. truncate at the NUL.
480 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
482 size_t len
= nasm_unquote(qstr
, NULL
);
483 size_t clen
= strlen(qstr
);
486 error(ERR_NONFATAL
, "NUL character in `%s' directive",
487 pp_directives
[directive
]);
493 * In-place reverse a list of tokens.
495 static Token
*reverse_tokens(Token
*t
)
511 * Handle TASM specific directives, which do not contain a % in
512 * front of them. We do it here because I could not find any other
513 * place to do it for the moment, and it is a hack (ideally it would
514 * be nice to be able to use the NASM pre-processor to do it).
516 static char *check_tasm_directive(char *line
)
518 int32_t i
, j
, k
, m
, len
;
519 char *p
, *q
, *oldline
, oldchar
;
521 p
= nasm_skip_spaces(line
);
523 /* Binary search for the directive name */
525 j
= ARRAY_SIZE(tasm_directives
);
526 q
= nasm_skip_word(p
);
533 m
= nasm_stricmp(p
, tasm_directives
[k
]);
535 /* We have found a directive, so jam a % in front of it
536 * so that NASM will then recognise it as one if it's own.
541 line
= nasm_malloc(len
+ 2);
543 if (k
== TM_IFDIFI
) {
545 * NASM does not recognise IFDIFI, so we convert
546 * it to %if 0. This is not used in NASM
547 * compatible code, but does need to parse for the
548 * TASM macro package.
550 strcpy(line
+ 1, "if 0");
552 memcpy(line
+ 1, p
, len
+ 1);
567 * The pre-preprocessing stage... This function translates line
568 * number indications as they emerge from GNU cpp (`# lineno "file"
569 * flags') into NASM preprocessor line number indications (`%line
572 static char *prepreproc(char *line
)
575 char *fname
, *oldline
;
577 if (line
[0] == '#' && line
[1] == ' ') {
580 lineno
= atoi(fname
);
581 fname
+= strspn(fname
, "0123456789 ");
584 fnlen
= strcspn(fname
, "\"");
585 line
= nasm_malloc(20 + fnlen
);
586 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
589 if (tasm_compatible_mode
)
590 return check_tasm_directive(line
);
595 * Free a linked list of tokens.
597 static void free_tlist(Token
* list
)
600 list
= delete_Token(list
);
604 * Free a linked list of lines.
606 static void free_llist(Line
* list
)
609 list_for_each_safe(l
, tmp
, list
) {
610 free_tlist(l
->first
);
618 static void free_mmacro(MMacro
* m
)
621 free_tlist(m
->dlist
);
622 nasm_free(m
->defaults
);
623 free_llist(m
->expansion
);
628 * Free all currently defined macros, and free the hash tables
630 static void free_smacro_table(struct hash_table
*smt
)
634 struct hash_tbl_node
*it
= NULL
;
636 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
637 nasm_free((void *)key
);
638 list_for_each_safe(s
, tmp
, s
) {
640 free_tlist(s
->expansion
);
647 static void free_mmacro_table(struct hash_table
*mmt
)
651 struct hash_tbl_node
*it
= NULL
;
654 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
655 nasm_free((void *)key
);
656 list_for_each_safe(m
,tmp
, m
)
662 static void free_macros(void)
664 free_smacro_table(&smacros
);
665 free_mmacro_table(&mmacros
);
669 * Initialize the hash tables
671 static void init_macros(void)
673 hash_init(&smacros
, HASH_LARGE
);
674 hash_init(&mmacros
, HASH_LARGE
);
678 * Pop the context stack.
680 static void ctx_pop(void)
685 free_smacro_table(&c
->localmac
);
691 * Search for a key in the hash index; adding it if necessary
692 * (in which case we initialize the data pointer to NULL.)
695 hash_findi_add(struct hash_table
*hash
, const char *str
)
697 struct hash_insert hi
;
701 r
= hash_findi(hash
, str
, &hi
);
705 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
706 return hash_add(&hi
, strx
, NULL
);
710 * Like hash_findi, but returns the data element rather than a pointer
711 * to it. Used only when not adding a new element, hence no third
715 hash_findix(struct hash_table
*hash
, const char *str
)
719 p
= hash_findi(hash
, str
, NULL
);
720 return p
? *p
: NULL
;
724 * read line from standart macros set,
725 * if there no more left -- return NULL
727 static char *line_from_stdmac(void)
730 const unsigned char *p
= stdmacpos
;
739 len
+= pp_directives_len
[c
- 0x80] + 1;
744 line
= nasm_malloc(len
+ 1);
746 while ((c
= *stdmacpos
++)) {
748 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
749 q
+= pp_directives_len
[c
- 0x80];
759 /* This was the last of the standard macro chain... */
761 if (any_extrastdmac
) {
762 stdmacpos
= extrastdmac
;
763 any_extrastdmac
= false;
764 } else if (do_predef
) {
766 Token
*head
, **tail
, *t
;
769 * Nasty hack: here we push the contents of
770 * `predef' on to the top-level expansion stack,
771 * since this is the most convenient way to
772 * implement the pre-include and pre-define
775 list_for_each(pd
, predef
) {
778 list_for_each(t
, pd
->first
) {
779 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
780 tail
= &(*tail
)->next
;
783 l
= nasm_malloc(sizeof(Line
));
784 l
->next
= istk
->expansion
;
797 #define BUF_DELTA 512
799 * Read a line from the top file in istk, handling multiple CR/LFs
800 * at the end of the line read, and handling spurious ^Zs. Will
801 * return lines from the standard macro set if this has not already
804 static char *read_line(void)
806 char *buffer
, *p
, *q
;
807 int bufsize
, continued_count
;
810 * standart macros set (predefined) goes first
812 p
= line_from_stdmac();
817 * regular read from a file
820 buffer
= nasm_malloc(BUF_DELTA
);
824 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
828 if (p
> buffer
&& p
[-1] == '\n') {
830 * Convert backslash-CRLF line continuation sequences into
831 * nothing at all (for DOS and Windows)
833 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
839 * Also convert backslash-LF line continuation sequences into
840 * nothing at all (for Unix)
842 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
850 if (p
- buffer
> bufsize
- 10) {
851 int32_t offset
= p
- buffer
;
852 bufsize
+= BUF_DELTA
;
853 buffer
= nasm_realloc(buffer
, bufsize
);
854 p
= buffer
+ offset
; /* prevent stale-pointer problems */
858 if (!q
&& p
== buffer
) {
863 src_set_linnum(src_get_linnum() + istk
->lineinc
+
864 (continued_count
* istk
->lineinc
));
867 * Play safe: remove CRs as well as LFs, if any of either are
868 * present at the end of the line.
870 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
874 * Handle spurious ^Z, which may be inserted into source files
875 * by some file transfer utilities.
877 buffer
[strcspn(buffer
, "\032")] = '\0';
879 list
->line(LIST_READ
, buffer
);
885 * Tokenize a line of text. This is a very simple process since we
886 * don't need to parse the value out of e.g. numeric tokens: we
887 * simply split one string into many.
889 static Token
*tokenize(char *line
)
892 enum pp_token_type type
;
894 Token
*t
, **tail
= &list
;
900 if (*p
== '+' && !nasm_isdigit(p
[1])) {
903 } else if (nasm_isdigit(*p
) ||
904 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
908 while (nasm_isdigit(*p
));
909 type
= TOK_PREPROC_ID
;
910 } else if (*p
== '{') {
912 while (*p
&& *p
!= '}') {
919 type
= TOK_PREPROC_ID
;
920 } else if (*p
== '[') {
922 line
+= 2; /* Skip the leading %[ */
924 while (lvl
&& (c
= *p
++)) {
936 p
= nasm_skip_string(p
- 1) + 1;
946 error(ERR_NONFATAL
, "unterminated %[ construct");
948 } else if (*p
== '?') {
949 type
= TOK_PREPROC_Q
; /* %? */
952 type
= TOK_PREPROC_QQ
; /* %?? */
955 } else if (*p
== '!') {
956 type
= TOK_PREPROC_ID
;
961 } while (isidchar(*p
));
962 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
963 p
= nasm_skip_string(p
);
967 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
969 /* %! without string or identifier */
970 type
= TOK_OTHER
; /* Legacy behavior... */
972 } else if (isidchar(*p
) ||
973 ((*p
== '!' || *p
== '%' || *p
== '$') &&
978 while (isidchar(*p
));
979 type
= TOK_PREPROC_ID
;
985 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
988 while (*p
&& isidchar(*p
))
990 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
995 p
= nasm_skip_string(p
);
1000 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1001 /* Handling unterminated strings by UNV */
1004 } else if (p
[0] == '$' && p
[1] == '$') {
1005 type
= TOK_OTHER
; /* TOKEN_BASE */
1007 } else if (isnumstart(*p
)) {
1008 bool is_hex
= false;
1009 bool is_float
= false;
1025 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1027 if (*p
== '+' || *p
== '-') {
1029 * e can only be followed by +/- if it is either a
1030 * prefixed hex number or a floating-point number
1035 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1037 } else if (c
== 'P' || c
== 'p') {
1039 if (*p
== '+' || *p
== '-')
1041 } else if (isnumchar(c
) || c
== '_')
1042 ; /* just advance */
1043 else if (c
== '.') {
1045 * we need to deal with consequences of the legacy
1046 * parser, like "1.nolist" being two tokens
1047 * (TOK_NUMBER, TOK_ID) here; at least give it
1048 * a shot for now. In the future, we probably need
1049 * a flex-based scanner with proper pattern matching
1050 * to do it as well as it can be done. Nothing in
1051 * the world is going to help the person who wants
1052 * 0x123.p16 interpreted as two tokens, though.
1058 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1059 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1060 (*r
== 'p' || *r
== 'P')) {
1064 break; /* Terminate the token */
1068 p
--; /* Point to first character beyond number */
1070 if (p
== line
+1 && *line
== '$') {
1071 type
= TOK_OTHER
; /* TOKEN_HERE */
1073 if (has_e
&& !is_hex
) {
1074 /* 1e13 is floating-point, but 1e13h is not */
1078 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1080 } else if (nasm_isspace(*p
)) {
1081 type
= TOK_WHITESPACE
;
1082 p
= nasm_skip_spaces(p
);
1084 * Whitespace just before end-of-line is discarded by
1085 * pretending it's a comment; whitespace just before a
1086 * comment gets lumped into the comment.
1088 if (!*p
|| *p
== ';') {
1093 } else if (*p
== ';') {
1099 * Anything else is an operator of some kind. We check
1100 * for all the double-character operators (>>, <<, //,
1101 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1102 * else is a single-character operator.
1105 if ((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] == '&') ||
1114 (p
[0] == '|' && p
[1] == '|') ||
1115 (p
[0] == '^' && p
[1] == '^')) {
1121 /* Handling unterminated string by UNV */
1124 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1125 t->text[p-line] = *line;
1129 if (type
!= TOK_COMMENT
) {
1130 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1139 * this function allocates a new managed block of memory and
1140 * returns a pointer to the block. The managed blocks are
1141 * deleted only all at once by the delete_Blocks function.
1143 static void *new_Block(size_t size
)
1145 Blocks
*b
= &blocks
;
1147 /* first, get to the end of the linked list */
1150 /* now allocate the requested chunk */
1151 b
->chunk
= nasm_malloc(size
);
1153 /* now allocate a new block for the next request */
1154 b
->next
= nasm_malloc(sizeof(Blocks
));
1155 /* and initialize the contents of the new block */
1156 b
->next
->next
= NULL
;
1157 b
->next
->chunk
= NULL
;
1162 * this function deletes all managed blocks of memory
1164 static void delete_Blocks(void)
1166 Blocks
*a
, *b
= &blocks
;
1169 * keep in mind that the first block, pointed to by blocks
1170 * is a static and not dynamically allocated, so we don't
1175 nasm_free(b
->chunk
);
1184 * this function creates a new Token and passes a pointer to it
1185 * back to the caller. It sets the type and text elements, and
1186 * also the a.mac and next elements to NULL.
1188 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1189 const char *text
, int txtlen
)
1195 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1196 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1197 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1198 freeTokens
[i
].next
= NULL
;
1201 freeTokens
= t
->next
;
1205 if (type
== TOK_WHITESPACE
|| !text
) {
1209 txtlen
= strlen(text
);
1210 t
->text
= nasm_malloc(txtlen
+1);
1211 memcpy(t
->text
, text
, txtlen
);
1212 t
->text
[txtlen
] = '\0';
1217 static Token
*delete_Token(Token
* t
)
1219 Token
*next
= t
->next
;
1221 t
->next
= freeTokens
;
1227 * Convert a line of tokens back into text.
1228 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1229 * will be transformed into ..@ctxnum.xxx
1231 static char *detoken(Token
* tlist
, bool expand_locals
)
1238 list_for_each(t
, tlist
) {
1239 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1244 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1245 size_t len
= nasm_unquote(v
, NULL
);
1246 size_t clen
= strlen(v
);
1249 error(ERR_NONFATAL
| ERR_PASS1
,
1250 "NUL character in %! string");
1256 char *p
= getenv(v
);
1258 error(ERR_NONFATAL
| ERR_PASS1
,
1259 "nonexistent environment variable `%s'", v
);
1262 t
->text
= nasm_strdup(p
);
1267 /* Expand local macros here and not during preprocessing */
1268 if (expand_locals
&&
1269 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1270 t
->text
[0] == '%' && t
->text
[1] == '$') {
1273 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1276 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1277 p
= nasm_strcat(buffer
, q
);
1282 if (t
->type
== TOK_WHITESPACE
)
1285 len
+= strlen(t
->text
);
1288 p
= line
= nasm_malloc(len
+ 1);
1290 list_for_each(t
, tlist
) {
1291 if (t
->type
== TOK_WHITESPACE
) {
1293 } else if (t
->text
) {
1305 * A scanner, suitable for use by the expression evaluator, which
1306 * operates on a line of Tokens. Expects a pointer to a pointer to
1307 * the first token in the line to be passed in as its private_data
1310 * FIX: This really needs to be unified with stdscan.
1312 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1314 Token
**tlineptr
= private_data
;
1316 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1320 *tlineptr
= tline
? tline
->next
: NULL
;
1321 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1322 tline
->type
== TOK_COMMENT
));
1325 return tokval
->t_type
= TOKEN_EOS
;
1327 tokval
->t_charptr
= tline
->text
;
1329 if (tline
->text
[0] == '$' && !tline
->text
[1])
1330 return tokval
->t_type
= TOKEN_HERE
;
1331 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1332 return tokval
->t_type
= TOKEN_BASE
;
1334 if (tline
->type
== TOK_ID
) {
1335 p
= tokval
->t_charptr
= tline
->text
;
1337 tokval
->t_charptr
++;
1338 return tokval
->t_type
= TOKEN_ID
;
1341 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1342 if (r
>= p
+MAX_KEYWORD
)
1343 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1344 *s
++ = nasm_tolower(*r
);
1347 /* right, so we have an identifier sitting in temp storage. now,
1348 * is it actually a register or instruction name, or what? */
1349 return nasm_token_hash(ourcopy
, tokval
);
1352 if (tline
->type
== TOK_NUMBER
) {
1354 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1355 tokval
->t_charptr
= tline
->text
;
1357 return tokval
->t_type
= TOKEN_ERRNUM
;
1359 return tokval
->t_type
= TOKEN_NUM
;
1362 if (tline
->type
== TOK_FLOAT
) {
1363 return tokval
->t_type
= TOKEN_FLOAT
;
1366 if (tline
->type
== TOK_STRING
) {
1369 bq
= tline
->text
[0];
1370 tokval
->t_charptr
= tline
->text
;
1371 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1373 if (ep
[0] != bq
|| ep
[1] != '\0')
1374 return tokval
->t_type
= TOKEN_ERRSTR
;
1376 return tokval
->t_type
= TOKEN_STR
;
1379 if (tline
->type
== TOK_OTHER
) {
1380 if (!strcmp(tline
->text
, "<<"))
1381 return tokval
->t_type
= TOKEN_SHL
;
1382 if (!strcmp(tline
->text
, ">>"))
1383 return tokval
->t_type
= TOKEN_SHR
;
1384 if (!strcmp(tline
->text
, "//"))
1385 return tokval
->t_type
= TOKEN_SDIV
;
1386 if (!strcmp(tline
->text
, "%%"))
1387 return tokval
->t_type
= TOKEN_SMOD
;
1388 if (!strcmp(tline
->text
, "=="))
1389 return tokval
->t_type
= TOKEN_EQ
;
1390 if (!strcmp(tline
->text
, "<>"))
1391 return tokval
->t_type
= TOKEN_NE
;
1392 if (!strcmp(tline
->text
, "!="))
1393 return tokval
->t_type
= TOKEN_NE
;
1394 if (!strcmp(tline
->text
, "<="))
1395 return tokval
->t_type
= TOKEN_LE
;
1396 if (!strcmp(tline
->text
, ">="))
1397 return tokval
->t_type
= TOKEN_GE
;
1398 if (!strcmp(tline
->text
, "&&"))
1399 return tokval
->t_type
= TOKEN_DBL_AND
;
1400 if (!strcmp(tline
->text
, "^^"))
1401 return tokval
->t_type
= TOKEN_DBL_XOR
;
1402 if (!strcmp(tline
->text
, "||"))
1403 return tokval
->t_type
= TOKEN_DBL_OR
;
1407 * We have no other options: just return the first character of
1410 return tokval
->t_type
= tline
->text
[0];
1414 * Compare a string to the name of an existing macro; this is a
1415 * simple wrapper which calls either strcmp or nasm_stricmp
1416 * depending on the value of the `casesense' parameter.
1418 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1420 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1428 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1430 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1434 * Return the Context structure associated with a %$ token. Return
1435 * NULL, having _already_ reported an error condition, if the
1436 * context stack isn't deep enough for the supplied number of $
1438 * If all_contexts == true, contexts that enclose current are
1439 * also scanned for such smacro, until it is found; if not -
1440 * only the context that directly results from the number of $'s
1441 * in variable's name.
1443 * If "namep" is non-NULL, set it to the pointer to the macro name
1444 * tail, i.e. the part beyond %$...
1446 static Context
*get_ctx(const char *name
, const char **namep
,
1456 if (!name
|| name
[0] != '%' || name
[1] != '$')
1460 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1467 while (ctx
&& *name
== '$') {
1473 error(ERR_NONFATAL
, "`%s': context stack is only"
1474 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1485 * NOTE: In 2.10 we will not need lookup in extarnal
1486 * contexts, so this is a gentle way to inform users
1487 * about their source code need to be updated
1490 /* first round -- check the current context */
1491 m
= hash_findix(&ctx
->localmac
, name
);
1493 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1498 /* second round - external contexts */
1499 while ((ctx
= ctx
->next
)) {
1500 /* Search for this smacro in found context */
1501 m
= hash_findix(&ctx
->localmac
, name
);
1503 if (!mstrcmp(m
->name
, name
, m
->casesense
)) {
1504 /* NOTE: deprecated as of 2.10 */
1505 static int once
= 0;
1507 error(ERR_WARNING
, "context-local macro expansion"
1508 " fall-through (automatic searching of outer"
1509 " contexts) will be deprecated starting in"
1510 " NASM 2.10, please see the NASM Manual for"
1511 " more information");
1514 error(ERR_WARNING
, "`%s': context-local macro expansion fall-through", name
);
1525 * Check to see if a file is already in a string list
1527 static bool in_list(const StrList
*list
, const char *str
)
1530 if (!strcmp(list
->str
, str
))
1538 * Open an include file. This routine must always return a valid
1539 * file pointer if it returns - it's responsible for throwing an
1540 * ERR_FATAL and bombing out completely if not. It should also try
1541 * the include path one by one until it finds the file or reaches
1542 * the end of the path.
1544 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1549 IncPath
*ip
= ipath
;
1550 int len
= strlen(file
);
1551 size_t prefix_len
= 0;
1555 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1556 memcpy(sl
->str
, prefix
, prefix_len
);
1557 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1558 fp
= fopen(sl
->str
, "r");
1559 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1577 prefix_len
= strlen(prefix
);
1579 /* -MG given and file not found */
1580 if (dhead
&& !in_list(*dhead
, file
)) {
1581 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1583 strcpy(sl
->str
, file
);
1591 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1596 * Determine if we should warn on defining a single-line macro of
1597 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1598 * return true if _any_ single-line macro of that name is defined.
1599 * Otherwise, will return true if a single-line macro with either
1600 * `nparam' or no parameters is defined.
1602 * If a macro with precisely the right number of parameters is
1603 * defined, or nparam is -1, the address of the definition structure
1604 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1605 * is NULL, no action will be taken regarding its contents, and no
1608 * Note that this is also called with nparam zero to resolve
1611 * If you already know which context macro belongs to, you can pass
1612 * the context pointer as first parameter; if you won't but name begins
1613 * with %$ the context will be automatically computed. If all_contexts
1614 * is true, macro will be searched in outer contexts as well.
1617 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1620 struct hash_table
*smtbl
;
1624 smtbl
= &ctx
->localmac
;
1625 } else if (name
[0] == '%' && name
[1] == '$') {
1627 ctx
= get_ctx(name
, &name
, false);
1629 return false; /* got to return _something_ */
1630 smtbl
= &ctx
->localmac
;
1634 m
= (SMacro
*) hash_findix(smtbl
, name
);
1637 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1638 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1640 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1654 * Count and mark off the parameters in a multi-line macro call.
1655 * This is called both from within the multi-line macro expansion
1656 * code, and also to mark off the default parameters when provided
1657 * in a %macro definition line.
1659 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1661 int paramsize
, brace
;
1663 *nparam
= paramsize
= 0;
1666 /* +1: we need space for the final NULL */
1667 if (*nparam
+1 >= paramsize
) {
1668 paramsize
+= PARAM_DELTA
;
1669 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1673 if (tok_is_(t
, "{"))
1675 (*params
)[(*nparam
)++] = t
;
1676 while (tok_isnt_(t
, brace
? "}" : ","))
1678 if (t
) { /* got a comma/brace */
1682 * Now we've found the closing brace, look further
1686 if (tok_isnt_(t
, ",")) {
1688 "braces do not enclose all of macro parameter");
1689 while (tok_isnt_(t
, ","))
1693 t
= t
->next
; /* eat the comma */
1700 * Determine whether one of the various `if' conditions is true or
1703 * We must free the tline we get passed.
1705 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1707 enum pp_conditional i
= PP_COND(ct
);
1709 Token
*t
, *tt
, **tptr
, *origline
;
1710 struct tokenval tokval
;
1712 enum pp_token_type needtype
;
1719 j
= false; /* have we matched yet? */
1724 if (tline
->type
!= TOK_ID
) {
1726 "`%s' expects context identifiers", pp_directives
[ct
]);
1727 free_tlist(origline
);
1730 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1732 tline
= tline
->next
;
1737 j
= false; /* have we matched yet? */
1740 if (!tline
|| (tline
->type
!= TOK_ID
&&
1741 (tline
->type
!= TOK_PREPROC_ID
||
1742 tline
->text
[1] != '$'))) {
1744 "`%s' expects macro identifiers", pp_directives
[ct
]);
1747 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1749 tline
= tline
->next
;
1754 tline
= expand_smacro(tline
);
1755 j
= false; /* have we matched yet? */
1758 if (!tline
|| (tline
->type
!= TOK_ID
&&
1759 tline
->type
!= TOK_STRING
&&
1760 (tline
->type
!= TOK_PREPROC_ID
||
1761 tline
->text
[1] != '!'))) {
1763 "`%s' expects environment variable names",
1768 if (tline
->type
== TOK_PREPROC_ID
)
1769 p
+= 2; /* Skip leading %! */
1770 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1771 nasm_unquote_cstr(p
, ct
);
1774 tline
= tline
->next
;
1780 tline
= expand_smacro(tline
);
1782 while (tok_isnt_(tt
, ","))
1786 "`%s' expects two comma-separated arguments",
1791 j
= true; /* assume equality unless proved not */
1792 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1793 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1794 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1798 if (t
->type
== TOK_WHITESPACE
) {
1802 if (tt
->type
== TOK_WHITESPACE
) {
1806 if (tt
->type
!= t
->type
) {
1807 j
= false; /* found mismatching tokens */
1810 /* When comparing strings, need to unquote them first */
1811 if (t
->type
== TOK_STRING
) {
1812 size_t l1
= nasm_unquote(t
->text
, NULL
);
1813 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1819 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1823 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1824 j
= false; /* found mismatching tokens */
1831 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1832 j
= false; /* trailing gunk on one end or other */
1838 MMacro searching
, *mmac
;
1841 tline
= expand_id(tline
);
1842 if (!tok_type_(tline
, TOK_ID
)) {
1844 "`%s' expects a macro name", pp_directives
[ct
]);
1847 searching
.name
= nasm_strdup(tline
->text
);
1848 searching
.casesense
= true;
1849 searching
.plus
= false;
1850 searching
.nolist
= false;
1851 searching
.in_progress
= 0;
1852 searching
.max_depth
= 0;
1853 searching
.rep_nest
= NULL
;
1854 searching
.nparam_min
= 0;
1855 searching
.nparam_max
= INT_MAX
;
1856 tline
= expand_smacro(tline
->next
);
1859 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1861 "`%s' expects a parameter count or nothing",
1864 searching
.nparam_min
= searching
.nparam_max
=
1865 readnum(tline
->text
, &j
);
1868 "unable to parse parameter count `%s'",
1871 if (tline
&& tok_is_(tline
->next
, "-")) {
1872 tline
= tline
->next
->next
;
1873 if (tok_is_(tline
, "*"))
1874 searching
.nparam_max
= INT_MAX
;
1875 else if (!tok_type_(tline
, TOK_NUMBER
))
1877 "`%s' expects a parameter count after `-'",
1880 searching
.nparam_max
= readnum(tline
->text
, &j
);
1883 "unable to parse parameter count `%s'",
1885 if (searching
.nparam_min
> searching
.nparam_max
)
1887 "minimum parameter count exceeds maximum");
1890 if (tline
&& tok_is_(tline
->next
, "+")) {
1891 tline
= tline
->next
;
1892 searching
.plus
= true;
1894 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1896 if (!strcmp(mmac
->name
, searching
.name
) &&
1897 (mmac
->nparam_min
<= searching
.nparam_max
1899 && (searching
.nparam_min
<= mmac
->nparam_max
1906 if (tline
&& tline
->next
)
1907 error(ERR_WARNING
|ERR_PASS1
,
1908 "trailing garbage after %%ifmacro ignored");
1909 nasm_free(searching
.name
);
1918 needtype
= TOK_NUMBER
;
1921 needtype
= TOK_STRING
;
1925 t
= tline
= expand_smacro(tline
);
1927 while (tok_type_(t
, TOK_WHITESPACE
) ||
1928 (needtype
== TOK_NUMBER
&&
1929 tok_type_(t
, TOK_OTHER
) &&
1930 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1934 j
= tok_type_(t
, needtype
);
1938 t
= tline
= expand_smacro(tline
);
1939 while (tok_type_(t
, TOK_WHITESPACE
))
1944 t
= t
->next
; /* Skip the actual token */
1945 while (tok_type_(t
, TOK_WHITESPACE
))
1947 j
= !t
; /* Should be nothing left */
1952 t
= tline
= expand_smacro(tline
);
1953 while (tok_type_(t
, TOK_WHITESPACE
))
1956 j
= !t
; /* Should be empty */
1960 t
= tline
= expand_smacro(tline
);
1962 tokval
.t_type
= TOKEN_INVALID
;
1963 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1964 NULL
, pass
| CRITICAL
, error
, NULL
);
1968 error(ERR_WARNING
|ERR_PASS1
,
1969 "trailing garbage after expression ignored");
1970 if (!is_simple(evalresult
)) {
1972 "non-constant value given to `%s'", pp_directives
[ct
]);
1975 j
= reloc_value(evalresult
) != 0;
1980 "preprocessor directive `%s' not yet implemented",
1985 free_tlist(origline
);
1986 return j
^ PP_NEGATIVE(ct
);
1989 free_tlist(origline
);
1994 * Common code for defining an smacro
1996 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1997 int nparam
, Token
*expansion
)
1999 SMacro
*smac
, **smhead
;
2000 struct hash_table
*smtbl
;
2002 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2004 error(ERR_WARNING
|ERR_PASS1
,
2005 "single-line macro `%s' defined both with and"
2006 " without parameters", mname
);
2008 * Some instances of the old code considered this a failure,
2009 * some others didn't. What is the right thing to do here?
2011 free_tlist(expansion
);
2012 return false; /* Failure */
2015 * We're redefining, so we have to take over an
2016 * existing SMacro structure. This means freeing
2017 * what was already in it.
2019 nasm_free(smac
->name
);
2020 free_tlist(smac
->expansion
);
2023 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2024 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2025 smac
= nasm_malloc(sizeof(SMacro
));
2026 smac
->next
= *smhead
;
2029 smac
->name
= nasm_strdup(mname
);
2030 smac
->casesense
= casesense
;
2031 smac
->nparam
= nparam
;
2032 smac
->expansion
= expansion
;
2033 smac
->in_progress
= false;
2034 return true; /* Success */
2038 * Undefine an smacro
2040 static void undef_smacro(Context
*ctx
, const char *mname
)
2042 SMacro
**smhead
, *s
, **sp
;
2043 struct hash_table
*smtbl
;
2045 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2046 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2050 * We now have a macro name... go hunt for it.
2053 while ((s
= *sp
) != NULL
) {
2054 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2057 free_tlist(s
->expansion
);
2067 * Parse a mmacro specification.
2069 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2073 tline
= tline
->next
;
2075 tline
= expand_id(tline
);
2076 if (!tok_type_(tline
, TOK_ID
)) {
2077 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2082 def
->name
= nasm_strdup(tline
->text
);
2084 def
->nolist
= false;
2085 def
->in_progress
= 0;
2086 def
->rep_nest
= NULL
;
2087 def
->nparam_min
= 0;
2088 def
->nparam_max
= 0;
2090 tline
= expand_smacro(tline
->next
);
2092 if (!tok_type_(tline
, TOK_NUMBER
)) {
2093 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2095 def
->nparam_min
= def
->nparam_max
=
2096 readnum(tline
->text
, &err
);
2099 "unable to parse parameter count `%s'", tline
->text
);
2101 if (tline
&& tok_is_(tline
->next
, "-")) {
2102 tline
= tline
->next
->next
;
2103 if (tok_is_(tline
, "*")) {
2104 def
->nparam_max
= INT_MAX
;
2105 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2107 "`%s' expects a parameter count after `-'", directive
);
2109 def
->nparam_max
= readnum(tline
->text
, &err
);
2111 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2114 if (def
->nparam_min
> def
->nparam_max
) {
2115 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2119 if (tline
&& tok_is_(tline
->next
, "+")) {
2120 tline
= tline
->next
;
2123 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2124 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2125 tline
= tline
->next
;
2130 * Handle default parameters.
2132 if (tline
&& tline
->next
) {
2133 def
->dlist
= tline
->next
;
2135 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2138 def
->defaults
= NULL
;
2140 def
->expansion
= NULL
;
2142 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2144 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2145 "too many default macro parameters");
2152 * Decode a size directive
2154 static int parse_size(const char *str
) {
2155 static const char *size_names
[] =
2156 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2157 static const int sizes
[] =
2158 { 0, 1, 4, 16, 8, 10, 2, 32 };
2160 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2164 * find and process preprocessor directive in passed line
2165 * Find out if a line contains a preprocessor directive, and deal
2168 * If a directive _is_ found, it is the responsibility of this routine
2169 * (and not the caller) to free_tlist() the line.
2171 * @param tline a pointer to the current tokeninzed line linked list
2172 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2175 static int do_directive(Token
* tline
)
2177 enum preproc_token i
;
2190 MMacro
*mmac
, **mmhead
;
2191 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2193 struct tokenval tokval
;
2195 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2203 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2204 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2205 || tline
->text
[1] == '!'))
2206 return NO_DIRECTIVE_FOUND
;
2208 i
= pp_token_hash(tline
->text
);
2211 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2212 * since they are known to be buggy at moment, we need to fix them
2213 * in future release (2.09-2.10)
2215 if (i
== PP_RMACRO
|| i
== PP_RMACRO
|| i
== PP_EXITMACRO
) {
2216 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2218 return NO_DIRECTIVE_FOUND
;
2222 * If we're in a non-emitting branch of a condition construct,
2223 * or walking to the end of an already terminated %rep block,
2224 * we should ignore all directives except for condition
2227 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2228 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2229 return NO_DIRECTIVE_FOUND
;
2233 * If we're defining a macro or reading a %rep block, we should
2234 * ignore all directives except for %macro/%imacro (which nest),
2235 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2236 * If we're in a %rep block, another %rep nests, so should be let through.
2238 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2239 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2240 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2241 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2242 return NO_DIRECTIVE_FOUND
;
2246 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2247 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2249 return NO_DIRECTIVE_FOUND
;
2250 } else if (nested_mac_count
> 0) {
2251 if (i
== PP_ENDMACRO
) {
2253 return NO_DIRECTIVE_FOUND
;
2256 if (!defining
->name
) {
2259 return NO_DIRECTIVE_FOUND
;
2260 } else if (nested_rep_count
> 0) {
2261 if (i
== PP_ENDREP
) {
2263 return NO_DIRECTIVE_FOUND
;
2271 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2273 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2276 /* Directive to tell NASM what the default stack size is. The
2277 * default is for a 16-bit stack, and this can be overriden with
2280 tline
= tline
->next
;
2281 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2282 tline
= tline
->next
;
2283 if (!tline
|| tline
->type
!= TOK_ID
) {
2284 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2285 free_tlist(origline
);
2286 return DIRECTIVE_FOUND
;
2288 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2289 /* All subsequent ARG directives are for a 32-bit stack */
2291 StackPointer
= "ebp";
2294 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2295 /* All subsequent ARG directives are for a 64-bit stack */
2297 StackPointer
= "rbp";
2300 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2301 /* All subsequent ARG directives are for a 16-bit stack,
2302 * far function call.
2305 StackPointer
= "bp";
2308 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2309 /* All subsequent ARG directives are for a 16-bit stack,
2310 * far function call. We don't support near functions.
2313 StackPointer
= "bp";
2317 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2318 free_tlist(origline
);
2319 return DIRECTIVE_FOUND
;
2321 free_tlist(origline
);
2322 return DIRECTIVE_FOUND
;
2325 /* TASM like ARG directive to define arguments to functions, in
2326 * the following form:
2328 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2332 char *arg
, directive
[256];
2333 int size
= StackSize
;
2335 /* Find the argument name */
2336 tline
= tline
->next
;
2337 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2338 tline
= tline
->next
;
2339 if (!tline
|| tline
->type
!= TOK_ID
) {
2340 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2341 free_tlist(origline
);
2342 return DIRECTIVE_FOUND
;
2346 /* Find the argument size type */
2347 tline
= tline
->next
;
2348 if (!tline
|| tline
->type
!= TOK_OTHER
2349 || tline
->text
[0] != ':') {
2351 "Syntax error processing `%%arg' directive");
2352 free_tlist(origline
);
2353 return DIRECTIVE_FOUND
;
2355 tline
= tline
->next
;
2356 if (!tline
|| tline
->type
!= TOK_ID
) {
2357 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2358 free_tlist(origline
);
2359 return DIRECTIVE_FOUND
;
2362 /* Allow macro expansion of type parameter */
2363 tt
= tokenize(tline
->text
);
2364 tt
= expand_smacro(tt
);
2365 size
= parse_size(tt
->text
);
2368 "Invalid size type for `%%arg' missing directive");
2370 free_tlist(origline
);
2371 return DIRECTIVE_FOUND
;
2375 /* Round up to even stack slots */
2376 size
= ALIGN(size
, StackSize
);
2378 /* Now define the macro for the argument */
2379 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2380 arg
, StackPointer
, offset
);
2381 do_directive(tokenize(directive
));
2384 /* Move to the next argument in the list */
2385 tline
= tline
->next
;
2386 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2387 tline
= tline
->next
;
2388 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2390 free_tlist(origline
);
2391 return DIRECTIVE_FOUND
;
2394 /* TASM like LOCAL directive to define local variables for a
2395 * function, in the following form:
2397 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2399 * The '= LocalSize' at the end is ignored by NASM, but is
2400 * required by TASM to define the local parameter size (and used
2401 * by the TASM macro package).
2403 offset
= LocalOffset
;
2405 char *local
, directive
[256];
2406 int size
= StackSize
;
2408 /* Find the argument name */
2409 tline
= tline
->next
;
2410 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2411 tline
= tline
->next
;
2412 if (!tline
|| tline
->type
!= TOK_ID
) {
2414 "`%%local' missing argument parameter");
2415 free_tlist(origline
);
2416 return DIRECTIVE_FOUND
;
2418 local
= tline
->text
;
2420 /* Find the argument size type */
2421 tline
= tline
->next
;
2422 if (!tline
|| tline
->type
!= TOK_OTHER
2423 || tline
->text
[0] != ':') {
2425 "Syntax error processing `%%local' directive");
2426 free_tlist(origline
);
2427 return DIRECTIVE_FOUND
;
2429 tline
= tline
->next
;
2430 if (!tline
|| tline
->type
!= TOK_ID
) {
2432 "`%%local' missing size type parameter");
2433 free_tlist(origline
);
2434 return DIRECTIVE_FOUND
;
2437 /* Allow macro expansion of type parameter */
2438 tt
= tokenize(tline
->text
);
2439 tt
= expand_smacro(tt
);
2440 size
= parse_size(tt
->text
);
2443 "Invalid size type for `%%local' missing directive");
2445 free_tlist(origline
);
2446 return DIRECTIVE_FOUND
;
2450 /* Round up to even stack slots */
2451 size
= ALIGN(size
, StackSize
);
2453 offset
+= size
; /* Negative offset, increment before */
2455 /* Now define the macro for the argument */
2456 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2457 local
, StackPointer
, offset
);
2458 do_directive(tokenize(directive
));
2460 /* Now define the assign to setup the enter_c macro correctly */
2461 snprintf(directive
, sizeof(directive
),
2462 "%%assign %%$localsize %%$localsize+%d", size
);
2463 do_directive(tokenize(directive
));
2465 /* Move to the next argument in the list */
2466 tline
= tline
->next
;
2467 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2468 tline
= tline
->next
;
2469 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2470 LocalOffset
= offset
;
2471 free_tlist(origline
);
2472 return DIRECTIVE_FOUND
;
2476 error(ERR_WARNING
|ERR_PASS1
,
2477 "trailing garbage after `%%clear' ignored");
2480 free_tlist(origline
);
2481 return DIRECTIVE_FOUND
;
2484 t
= tline
->next
= expand_smacro(tline
->next
);
2486 if (!t
|| (t
->type
!= TOK_STRING
&&
2487 t
->type
!= TOK_INTERNAL_STRING
)) {
2488 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2489 free_tlist(origline
);
2490 return DIRECTIVE_FOUND
; /* but we did _something_ */
2493 error(ERR_WARNING
|ERR_PASS1
,
2494 "trailing garbage after `%%depend' ignored");
2496 if (t
->type
!= TOK_INTERNAL_STRING
)
2497 nasm_unquote_cstr(p
, i
);
2498 if (dephead
&& !in_list(*dephead
, p
)) {
2499 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2503 deptail
= &sl
->next
;
2505 free_tlist(origline
);
2506 return DIRECTIVE_FOUND
;
2509 t
= tline
->next
= expand_smacro(tline
->next
);
2512 if (!t
|| (t
->type
!= TOK_STRING
&&
2513 t
->type
!= TOK_INTERNAL_STRING
)) {
2514 error(ERR_NONFATAL
, "`%%include' expects a file name");
2515 free_tlist(origline
);
2516 return DIRECTIVE_FOUND
; /* but we did _something_ */
2519 error(ERR_WARNING
|ERR_PASS1
,
2520 "trailing garbage after `%%include' ignored");
2522 if (t
->type
!= TOK_INTERNAL_STRING
)
2523 nasm_unquote_cstr(p
, i
);
2524 inc
= nasm_malloc(sizeof(Include
));
2527 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2529 /* -MG given but file not found */
2532 inc
->fname
= src_set_fname(nasm_strdup(p
));
2533 inc
->lineno
= src_set_linnum(0);
2535 inc
->expansion
= NULL
;
2538 list
->uplevel(LIST_INCLUDE
);
2540 free_tlist(origline
);
2541 return DIRECTIVE_FOUND
;
2545 static macros_t
*use_pkg
;
2546 const char *pkg_macro
= NULL
;
2548 tline
= tline
->next
;
2550 tline
= expand_id(tline
);
2552 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2553 tline
->type
!= TOK_INTERNAL_STRING
&&
2554 tline
->type
!= TOK_ID
)) {
2555 error(ERR_NONFATAL
, "`%%use' expects a package name");
2556 free_tlist(origline
);
2557 return DIRECTIVE_FOUND
; /* but we did _something_ */
2560 error(ERR_WARNING
|ERR_PASS1
,
2561 "trailing garbage after `%%use' ignored");
2562 if (tline
->type
== TOK_STRING
)
2563 nasm_unquote_cstr(tline
->text
, i
);
2564 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2566 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2568 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2569 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2570 /* Not already included, go ahead and include it */
2571 stdmacpos
= use_pkg
;
2573 free_tlist(origline
);
2574 return DIRECTIVE_FOUND
;
2579 tline
= tline
->next
;
2581 tline
= expand_id(tline
);
2583 if (!tok_type_(tline
, TOK_ID
)) {
2584 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2586 free_tlist(origline
);
2587 return DIRECTIVE_FOUND
; /* but we did _something_ */
2590 error(ERR_WARNING
|ERR_PASS1
,
2591 "trailing garbage after `%s' ignored",
2593 p
= nasm_strdup(tline
->text
);
2595 p
= NULL
; /* Anonymous */
2599 ctx
= nasm_malloc(sizeof(Context
));
2601 hash_init(&ctx
->localmac
, HASH_SMALL
);
2603 ctx
->number
= unique
++;
2608 error(ERR_NONFATAL
, "`%s': context stack is empty",
2610 } else if (i
== PP_POP
) {
2611 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2612 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2614 cstk
->name
? cstk
->name
: "anonymous", p
);
2619 nasm_free(cstk
->name
);
2625 free_tlist(origline
);
2626 return DIRECTIVE_FOUND
;
2628 severity
= ERR_FATAL
;
2631 severity
= ERR_NONFATAL
;
2634 severity
= ERR_WARNING
|ERR_WARN_USER
;
2639 /* Only error out if this is the final pass */
2640 if (pass
!= 2 && i
!= PP_FATAL
)
2641 return DIRECTIVE_FOUND
;
2643 tline
->next
= expand_smacro(tline
->next
);
2644 tline
= tline
->next
;
2646 t
= tline
? tline
->next
: NULL
;
2648 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2649 /* The line contains only a quoted string */
2651 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2652 error(severity
, "%s", p
);
2654 /* Not a quoted string, or more than a quoted string */
2655 p
= detoken(tline
, false);
2656 error(severity
, "%s", p
);
2659 free_tlist(origline
);
2660 return DIRECTIVE_FOUND
;
2664 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2667 j
= if_condition(tline
->next
, i
);
2668 tline
->next
= NULL
; /* it got freed */
2669 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2671 cond
= nasm_malloc(sizeof(Cond
));
2672 cond
->next
= istk
->conds
;
2676 istk
->mstk
->condcnt
++;
2677 free_tlist(origline
);
2678 return DIRECTIVE_FOUND
;
2682 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2683 switch(istk
->conds
->state
) {
2685 istk
->conds
->state
= COND_DONE
;
2692 case COND_ELSE_TRUE
:
2693 case COND_ELSE_FALSE
:
2694 error_precond(ERR_WARNING
|ERR_PASS1
,
2695 "`%%elif' after `%%else' ignored");
2696 istk
->conds
->state
= COND_NEVER
;
2701 * IMPORTANT: In the case of %if, we will already have
2702 * called expand_mmac_params(); however, if we're
2703 * processing an %elif we must have been in a
2704 * non-emitting mode, which would have inhibited
2705 * the normal invocation of expand_mmac_params().
2706 * Therefore, we have to do it explicitly here.
2708 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2709 tline
->next
= NULL
; /* it got freed */
2710 istk
->conds
->state
=
2711 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2714 free_tlist(origline
);
2715 return DIRECTIVE_FOUND
;
2719 error_precond(ERR_WARNING
|ERR_PASS1
,
2720 "trailing garbage after `%%else' ignored");
2722 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2723 switch(istk
->conds
->state
) {
2726 istk
->conds
->state
= COND_ELSE_FALSE
;
2733 istk
->conds
->state
= COND_ELSE_TRUE
;
2736 case COND_ELSE_TRUE
:
2737 case COND_ELSE_FALSE
:
2738 error_precond(ERR_WARNING
|ERR_PASS1
,
2739 "`%%else' after `%%else' ignored.");
2740 istk
->conds
->state
= COND_NEVER
;
2743 free_tlist(origline
);
2744 return DIRECTIVE_FOUND
;
2748 error_precond(ERR_WARNING
|ERR_PASS1
,
2749 "trailing garbage after `%%endif' ignored");
2751 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2753 istk
->conds
= cond
->next
;
2756 istk
->mstk
->condcnt
--;
2757 free_tlist(origline
);
2758 return DIRECTIVE_FOUND
;
2765 error(ERR_FATAL
, "`%s': already defining a macro",
2767 return DIRECTIVE_FOUND
;
2769 defining
= nasm_malloc(sizeof(MMacro
));
2770 defining
->max_depth
=
2771 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2772 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2773 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2774 nasm_free(defining
);
2776 return DIRECTIVE_FOUND
;
2779 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2781 if (!strcmp(mmac
->name
, defining
->name
) &&
2782 (mmac
->nparam_min
<= defining
->nparam_max
2784 && (defining
->nparam_min
<= mmac
->nparam_max
2786 error(ERR_WARNING
|ERR_PASS1
,
2787 "redefining multi-line macro `%s'", defining
->name
);
2788 return DIRECTIVE_FOUND
;
2792 free_tlist(origline
);
2793 return DIRECTIVE_FOUND
;
2797 if (! (defining
&& defining
->name
)) {
2798 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2799 return DIRECTIVE_FOUND
;
2801 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2802 defining
->next
= *mmhead
;
2805 free_tlist(origline
);
2806 return DIRECTIVE_FOUND
;
2810 * We must search along istk->expansion until we hit a
2811 * macro-end marker for a macro with a name. Then we
2812 * bypass all lines between exitmacro and endmacro.
2814 list_for_each(l
, istk
->expansion
)
2815 if (l
->finishes
&& l
->finishes
->name
)
2820 * Remove all conditional entries relative to this
2821 * macro invocation. (safe to do in this context)
2823 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2825 istk
->conds
= cond
->next
;
2828 istk
->expansion
= l
;
2830 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2832 free_tlist(origline
);
2833 return DIRECTIVE_FOUND
;
2841 spec
.casesense
= (i
== PP_UNMACRO
);
2842 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2843 return DIRECTIVE_FOUND
;
2845 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2846 while (mmac_p
&& *mmac_p
) {
2848 if (mmac
->casesense
== spec
.casesense
&&
2849 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2850 mmac
->nparam_min
== spec
.nparam_min
&&
2851 mmac
->nparam_max
== spec
.nparam_max
&&
2852 mmac
->plus
== spec
.plus
) {
2853 *mmac_p
= mmac
->next
;
2856 mmac_p
= &mmac
->next
;
2859 free_tlist(origline
);
2860 free_tlist(spec
.dlist
);
2861 return DIRECTIVE_FOUND
;
2865 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2866 tline
= tline
->next
;
2868 free_tlist(origline
);
2869 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2870 return DIRECTIVE_FOUND
;
2872 t
= expand_smacro(tline
->next
);
2874 free_tlist(origline
);
2877 tokval
.t_type
= TOKEN_INVALID
;
2879 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2882 return DIRECTIVE_FOUND
;
2884 error(ERR_WARNING
|ERR_PASS1
,
2885 "trailing garbage after expression ignored");
2886 if (!is_simple(evalresult
)) {
2887 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2888 return DIRECTIVE_FOUND
;
2891 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2892 mmac
= mmac
->next_active
;
2894 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2895 } else if (mmac
->nparam
== 0) {
2897 "`%%rotate' invoked within macro without parameters");
2899 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2901 rotate
%= (int)mmac
->nparam
;
2903 rotate
+= mmac
->nparam
;
2905 mmac
->rotate
= rotate
;
2907 return DIRECTIVE_FOUND
;
2912 tline
= tline
->next
;
2913 } while (tok_type_(tline
, TOK_WHITESPACE
));
2915 if (tok_type_(tline
, TOK_ID
) &&
2916 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2919 tline
= tline
->next
;
2920 } while (tok_type_(tline
, TOK_WHITESPACE
));
2924 t
= expand_smacro(tline
);
2926 tokval
.t_type
= TOKEN_INVALID
;
2928 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2930 free_tlist(origline
);
2931 return DIRECTIVE_FOUND
;
2934 error(ERR_WARNING
|ERR_PASS1
,
2935 "trailing garbage after expression ignored");
2936 if (!is_simple(evalresult
)) {
2937 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2938 return DIRECTIVE_FOUND
;
2940 count
= reloc_value(evalresult
);
2941 if (count
>= REP_LIMIT
) {
2942 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
2947 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2950 free_tlist(origline
);
2952 tmp_defining
= defining
;
2953 defining
= nasm_malloc(sizeof(MMacro
));
2954 defining
->prev
= NULL
;
2955 defining
->name
= NULL
; /* flags this macro as a %rep block */
2956 defining
->casesense
= false;
2957 defining
->plus
= false;
2958 defining
->nolist
= nolist
;
2959 defining
->in_progress
= count
;
2960 defining
->max_depth
= 0;
2961 defining
->nparam_min
= defining
->nparam_max
= 0;
2962 defining
->defaults
= NULL
;
2963 defining
->dlist
= NULL
;
2964 defining
->expansion
= NULL
;
2965 defining
->next_active
= istk
->mstk
;
2966 defining
->rep_nest
= tmp_defining
;
2967 return DIRECTIVE_FOUND
;
2970 if (!defining
|| defining
->name
) {
2971 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2972 return DIRECTIVE_FOUND
;
2976 * Now we have a "macro" defined - although it has no name
2977 * and we won't be entering it in the hash tables - we must
2978 * push a macro-end marker for it on to istk->expansion.
2979 * After that, it will take care of propagating itself (a
2980 * macro-end marker line for a macro which is really a %rep
2981 * block will cause the macro to be re-expanded, complete
2982 * with another macro-end marker to ensure the process
2983 * continues) until the whole expansion is forcibly removed
2984 * from istk->expansion by a %exitrep.
2986 l
= nasm_malloc(sizeof(Line
));
2987 l
->next
= istk
->expansion
;
2988 l
->finishes
= defining
;
2990 istk
->expansion
= l
;
2992 istk
->mstk
= defining
;
2994 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2995 tmp_defining
= defining
;
2996 defining
= defining
->rep_nest
;
2997 free_tlist(origline
);
2998 return DIRECTIVE_FOUND
;
3002 * We must search along istk->expansion until we hit a
3003 * macro-end marker for a macro with no name. Then we set
3004 * its `in_progress' flag to 0.
3006 list_for_each(l
, istk
->expansion
)
3007 if (l
->finishes
&& !l
->finishes
->name
)
3011 l
->finishes
->in_progress
= 1;
3013 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3014 free_tlist(origline
);
3015 return DIRECTIVE_FOUND
;
3021 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3023 tline
= tline
->next
;
3025 tline
= expand_id(tline
);
3026 if (!tline
|| (tline
->type
!= TOK_ID
&&
3027 (tline
->type
!= TOK_PREPROC_ID
||
3028 tline
->text
[1] != '$'))) {
3029 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3031 free_tlist(origline
);
3032 return DIRECTIVE_FOUND
;
3035 ctx
= get_ctx(tline
->text
, &mname
, false);
3037 param_start
= tline
= tline
->next
;
3040 /* Expand the macro definition now for %xdefine and %ixdefine */
3041 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3042 tline
= expand_smacro(tline
);
3044 if (tok_is_(tline
, "(")) {
3046 * This macro has parameters.
3049 tline
= tline
->next
;
3053 error(ERR_NONFATAL
, "parameter identifier expected");
3054 free_tlist(origline
);
3055 return DIRECTIVE_FOUND
;
3057 if (tline
->type
!= TOK_ID
) {
3059 "`%s': parameter identifier expected",
3061 free_tlist(origline
);
3062 return DIRECTIVE_FOUND
;
3064 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3065 tline
= tline
->next
;
3067 if (tok_is_(tline
, ",")) {
3068 tline
= tline
->next
;
3070 if (!tok_is_(tline
, ")")) {
3072 "`)' expected to terminate macro template");
3073 free_tlist(origline
);
3074 return DIRECTIVE_FOUND
;
3080 tline
= tline
->next
;
3082 if (tok_type_(tline
, TOK_WHITESPACE
))
3083 last
= tline
, tline
= tline
->next
;
3088 if (t
->type
== TOK_ID
) {
3089 list_for_each(tt
, param_start
)
3090 if (tt
->type
>= TOK_SMAC_PARAM
&&
3091 !strcmp(tt
->text
, t
->text
))
3095 t
->next
= macro_start
;
3100 * Good. We now have a macro name, a parameter count, and a
3101 * token list (in reverse order) for an expansion. We ought
3102 * to be OK just to create an SMacro, store it, and let
3103 * free_tlist have the rest of the line (which we have
3104 * carefully re-terminated after chopping off the expansion
3107 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3108 free_tlist(origline
);
3109 return DIRECTIVE_FOUND
;
3112 tline
= tline
->next
;
3114 tline
= expand_id(tline
);
3115 if (!tline
|| (tline
->type
!= TOK_ID
&&
3116 (tline
->type
!= TOK_PREPROC_ID
||
3117 tline
->text
[1] != '$'))) {
3118 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3119 free_tlist(origline
);
3120 return DIRECTIVE_FOUND
;
3123 error(ERR_WARNING
|ERR_PASS1
,
3124 "trailing garbage after macro name ignored");
3127 /* Find the context that symbol belongs to */
3128 ctx
= get_ctx(tline
->text
, &mname
, false);
3129 undef_smacro(ctx
, mname
);
3130 free_tlist(origline
);
3131 return DIRECTIVE_FOUND
;
3135 casesense
= (i
== PP_DEFSTR
);
3137 tline
= tline
->next
;
3139 tline
= expand_id(tline
);
3140 if (!tline
|| (tline
->type
!= TOK_ID
&&
3141 (tline
->type
!= TOK_PREPROC_ID
||
3142 tline
->text
[1] != '$'))) {
3143 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3145 free_tlist(origline
);
3146 return DIRECTIVE_FOUND
;
3149 ctx
= get_ctx(tline
->text
, &mname
, false);
3151 tline
= expand_smacro(tline
->next
);
3154 while (tok_type_(tline
, TOK_WHITESPACE
))
3155 tline
= delete_Token(tline
);
3157 p
= detoken(tline
, false);
3158 macro_start
= nasm_malloc(sizeof(*macro_start
));
3159 macro_start
->next
= NULL
;
3160 macro_start
->text
= nasm_quote(p
, strlen(p
));
3161 macro_start
->type
= TOK_STRING
;
3162 macro_start
->a
.mac
= NULL
;
3166 * We now have a macro name, an implicit parameter count of
3167 * zero, and a string token to use as an expansion. Create
3168 * and store an SMacro.
3170 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3171 free_tlist(origline
);
3172 return DIRECTIVE_FOUND
;
3176 casesense
= (i
== PP_DEFTOK
);
3178 tline
= tline
->next
;
3180 tline
= expand_id(tline
);
3181 if (!tline
|| (tline
->type
!= TOK_ID
&&
3182 (tline
->type
!= TOK_PREPROC_ID
||
3183 tline
->text
[1] != '$'))) {
3185 "`%s' expects a macro identifier as first parameter",
3187 free_tlist(origline
);
3188 return DIRECTIVE_FOUND
;
3190 ctx
= get_ctx(tline
->text
, &mname
, false);
3192 tline
= expand_smacro(tline
->next
);
3196 while (tok_type_(t
, TOK_WHITESPACE
))
3198 /* t should now point to the string */
3199 if (!tok_type_(t
, TOK_STRING
)) {
3201 "`%s` requires string as second parameter",
3204 free_tlist(origline
);
3205 return DIRECTIVE_FOUND
;
3209 * Convert the string to a token stream. Note that smacros
3210 * are stored with the token stream reversed, so we have to
3211 * reverse the output of tokenize().
3213 nasm_unquote_cstr(t
->text
, i
);
3214 macro_start
= reverse_tokens(tokenize(t
->text
));
3217 * We now have a macro name, an implicit parameter count of
3218 * zero, and a numeric token to use as an expansion. Create
3219 * and store an SMacro.
3221 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3223 free_tlist(origline
);
3224 return DIRECTIVE_FOUND
;
3229 StrList
*xsl
= NULL
;
3230 StrList
**xst
= &xsl
;
3234 tline
= tline
->next
;
3236 tline
= expand_id(tline
);
3237 if (!tline
|| (tline
->type
!= TOK_ID
&&
3238 (tline
->type
!= TOK_PREPROC_ID
||
3239 tline
->text
[1] != '$'))) {
3241 "`%%pathsearch' expects a macro identifier as first parameter");
3242 free_tlist(origline
);
3243 return DIRECTIVE_FOUND
;
3245 ctx
= get_ctx(tline
->text
, &mname
, false);
3247 tline
= expand_smacro(tline
->next
);
3251 while (tok_type_(t
, TOK_WHITESPACE
))
3254 if (!t
|| (t
->type
!= TOK_STRING
&&
3255 t
->type
!= TOK_INTERNAL_STRING
)) {
3256 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3258 free_tlist(origline
);
3259 return DIRECTIVE_FOUND
; /* but we did _something_ */
3262 error(ERR_WARNING
|ERR_PASS1
,
3263 "trailing garbage after `%%pathsearch' ignored");
3265 if (t
->type
!= TOK_INTERNAL_STRING
)
3266 nasm_unquote(p
, NULL
);
3268 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3271 fclose(fp
); /* Don't actually care about the file */
3273 macro_start
= nasm_malloc(sizeof(*macro_start
));
3274 macro_start
->next
= NULL
;
3275 macro_start
->text
= nasm_quote(p
, strlen(p
));
3276 macro_start
->type
= TOK_STRING
;
3277 macro_start
->a
.mac
= NULL
;
3282 * We now have a macro name, an implicit parameter count of
3283 * zero, and a string token to use as an expansion. Create
3284 * and store an SMacro.
3286 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3288 free_tlist(origline
);
3289 return DIRECTIVE_FOUND
;
3295 tline
= tline
->next
;
3297 tline
= expand_id(tline
);
3298 if (!tline
|| (tline
->type
!= TOK_ID
&&
3299 (tline
->type
!= TOK_PREPROC_ID
||
3300 tline
->text
[1] != '$'))) {
3302 "`%%strlen' expects a macro identifier as first parameter");
3303 free_tlist(origline
);
3304 return DIRECTIVE_FOUND
;
3306 ctx
= get_ctx(tline
->text
, &mname
, false);
3308 tline
= expand_smacro(tline
->next
);
3312 while (tok_type_(t
, TOK_WHITESPACE
))
3314 /* t should now point to the string */
3315 if (!tok_type_(t
, TOK_STRING
)) {
3317 "`%%strlen` requires string as second parameter");
3319 free_tlist(origline
);
3320 return DIRECTIVE_FOUND
;
3323 macro_start
= nasm_malloc(sizeof(*macro_start
));
3324 macro_start
->next
= NULL
;
3325 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3326 macro_start
->a
.mac
= NULL
;
3329 * We now have a macro name, an implicit parameter count of
3330 * zero, and a numeric token to use as an expansion. Create
3331 * and store an SMacro.
3333 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3335 free_tlist(origline
);
3336 return DIRECTIVE_FOUND
;
3341 tline
= tline
->next
;
3343 tline
= expand_id(tline
);
3344 if (!tline
|| (tline
->type
!= TOK_ID
&&
3345 (tline
->type
!= TOK_PREPROC_ID
||
3346 tline
->text
[1] != '$'))) {
3348 "`%%strcat' expects a macro identifier as first parameter");
3349 free_tlist(origline
);
3350 return DIRECTIVE_FOUND
;
3352 ctx
= get_ctx(tline
->text
, &mname
, false);
3354 tline
= expand_smacro(tline
->next
);
3358 list_for_each(t
, tline
) {
3360 case TOK_WHITESPACE
:
3363 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3366 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3368 /* else fall through */
3371 "non-string passed to `%%strcat' (%d)", t
->type
);
3373 free_tlist(origline
);
3374 return DIRECTIVE_FOUND
;
3378 p
= pp
= nasm_malloc(len
);
3379 list_for_each(t
, tline
) {
3380 if (t
->type
== TOK_STRING
) {
3381 memcpy(p
, t
->text
, t
->a
.len
);
3387 * We now have a macro name, an implicit parameter count of
3388 * zero, and a numeric token to use as an expansion. Create
3389 * and store an SMacro.
3391 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3392 macro_start
->text
= nasm_quote(pp
, len
);
3394 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3396 free_tlist(origline
);
3397 return DIRECTIVE_FOUND
;
3401 int64_t start
, count
;
3406 tline
= tline
->next
;
3408 tline
= expand_id(tline
);
3409 if (!tline
|| (tline
->type
!= TOK_ID
&&
3410 (tline
->type
!= TOK_PREPROC_ID
||
3411 tline
->text
[1] != '$'))) {
3413 "`%%substr' expects a macro identifier as first parameter");
3414 free_tlist(origline
);
3415 return DIRECTIVE_FOUND
;
3417 ctx
= get_ctx(tline
->text
, &mname
, false);
3419 tline
= expand_smacro(tline
->next
);
3422 if (tline
) /* skip expanded id */
3424 while (tok_type_(t
, TOK_WHITESPACE
))
3427 /* t should now point to the string */
3428 if (!tok_type_(t
, TOK_STRING
)) {
3430 "`%%substr` requires string as second parameter");
3432 free_tlist(origline
);
3433 return DIRECTIVE_FOUND
;
3438 tokval
.t_type
= TOKEN_INVALID
;
3439 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3443 free_tlist(origline
);
3444 return DIRECTIVE_FOUND
;
3445 } else if (!is_simple(evalresult
)) {
3446 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3448 free_tlist(origline
);
3449 return DIRECTIVE_FOUND
;
3451 start
= evalresult
->value
- 1;
3453 while (tok_type_(tt
, TOK_WHITESPACE
))
3456 count
= 1; /* Backwards compatibility: one character */
3458 tokval
.t_type
= TOKEN_INVALID
;
3459 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3463 free_tlist(origline
);
3464 return DIRECTIVE_FOUND
;
3465 } else if (!is_simple(evalresult
)) {
3466 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3468 free_tlist(origline
);
3469 return DIRECTIVE_FOUND
;
3471 count
= evalresult
->value
;
3474 len
= nasm_unquote(t
->text
, NULL
);
3476 /* make start and count being in range */
3480 count
= len
+ count
+ 1 - start
;
3481 if (start
+ count
> (int64_t)len
)
3482 count
= len
- start
;
3483 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3484 start
= -1, count
= 0; /* empty string */
3486 macro_start
= nasm_malloc(sizeof(*macro_start
));
3487 macro_start
->next
= NULL
;
3488 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3489 macro_start
->type
= TOK_STRING
;
3490 macro_start
->a
.mac
= NULL
;
3493 * We now have a macro name, an implicit parameter count of
3494 * zero, and a numeric token to use as an expansion. Create
3495 * and store an SMacro.
3497 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3499 free_tlist(origline
);
3500 return DIRECTIVE_FOUND
;
3505 casesense
= (i
== PP_ASSIGN
);
3507 tline
= tline
->next
;
3509 tline
= expand_id(tline
);
3510 if (!tline
|| (tline
->type
!= TOK_ID
&&
3511 (tline
->type
!= TOK_PREPROC_ID
||
3512 tline
->text
[1] != '$'))) {
3514 "`%%%sassign' expects a macro identifier",
3515 (i
== PP_IASSIGN
? "i" : ""));
3516 free_tlist(origline
);
3517 return DIRECTIVE_FOUND
;
3519 ctx
= get_ctx(tline
->text
, &mname
, false);
3521 tline
= expand_smacro(tline
->next
);
3526 tokval
.t_type
= TOKEN_INVALID
;
3528 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3531 free_tlist(origline
);
3532 return DIRECTIVE_FOUND
;
3536 error(ERR_WARNING
|ERR_PASS1
,
3537 "trailing garbage after expression ignored");
3539 if (!is_simple(evalresult
)) {
3541 "non-constant value given to `%%%sassign'",
3542 (i
== PP_IASSIGN
? "i" : ""));
3543 free_tlist(origline
);
3544 return DIRECTIVE_FOUND
;
3547 macro_start
= nasm_malloc(sizeof(*macro_start
));
3548 macro_start
->next
= NULL
;
3549 make_tok_num(macro_start
, reloc_value(evalresult
));
3550 macro_start
->a
.mac
= NULL
;
3553 * We now have a macro name, an implicit parameter count of
3554 * zero, and a numeric token to use as an expansion. Create
3555 * and store an SMacro.
3557 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3558 free_tlist(origline
);
3559 return DIRECTIVE_FOUND
;
3563 * Syntax is `%line nnn[+mmm] [filename]'
3565 tline
= tline
->next
;
3567 if (!tok_type_(tline
, TOK_NUMBER
)) {
3568 error(ERR_NONFATAL
, "`%%line' expects line number");
3569 free_tlist(origline
);
3570 return DIRECTIVE_FOUND
;
3572 k
= readnum(tline
->text
, &err
);
3574 tline
= tline
->next
;
3575 if (tok_is_(tline
, "+")) {
3576 tline
= tline
->next
;
3577 if (!tok_type_(tline
, TOK_NUMBER
)) {
3578 error(ERR_NONFATAL
, "`%%line' expects line increment");
3579 free_tlist(origline
);
3580 return DIRECTIVE_FOUND
;
3582 m
= readnum(tline
->text
, &err
);
3583 tline
= tline
->next
;
3589 nasm_free(src_set_fname(detoken(tline
, false)));
3591 free_tlist(origline
);
3592 return DIRECTIVE_FOUND
;
3596 "preprocessor directive `%s' not yet implemented",
3598 return DIRECTIVE_FOUND
;
3603 * Ensure that a macro parameter contains a condition code and
3604 * nothing else. Return the condition code index if so, or -1
3607 static int find_cc(Token
* t
)
3613 return -1; /* Probably a %+ without a space */
3616 if (t
->type
!= TOK_ID
)
3620 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3624 j
= ARRAY_SIZE(conditions
);
3627 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3642 static bool paste_tokens(Token
**head
, int mask_head
, int mask_tail
,
3643 bool handle_paste_tokens
)
3645 Token
**tail
, *t
, *tt
;
3647 bool did_paste
= false;
3650 /* Now handle token pasting... */
3653 while ((t
= *tail
) && (tt
= t
->next
)) {
3655 case TOK_WHITESPACE
:
3656 if (tt
->type
== TOK_WHITESPACE
) {
3657 /* Zap adjacent whitespace tokens */
3658 t
->next
= delete_Token(tt
);
3660 /* Do not advance paste_head here */
3664 case TOK_PASTE
: /* %+ */
3665 if (handle_paste_tokens
) {
3666 /* Zap %+ and whitespace tokens to the right */
3667 while (t
&& (t
->type
== TOK_WHITESPACE
||
3668 t
->type
== TOK_PASTE
))
3669 t
= *tail
= delete_Token(t
);
3670 if (!paste_head
|| !t
)
3671 break; /* Nothing to paste with */
3675 while (tok_type_(tt
, TOK_WHITESPACE
))
3676 tt
= t
->next
= delete_Token(tt
);
3678 tmp
= nasm_strcat(t
->text
, tt
->text
);
3680 tt
= delete_Token(tt
);
3681 t
= *tail
= tokenize(tmp
);
3687 t
->next
= tt
; /* Attach the remaining token chain */
3694 /* else fall through */
3697 * Concatenation of tokens might look nontrivial
3698 * but in real it's pretty simple -- the caller
3699 * prepares the masks of tokens to be concatenated
3700 * and we simply find matched sequences and slip
3703 if (PP_CONCAT_MASK(t
->type
) & mask_head
) {
3707 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & mask_tail
)) {
3708 len
+= strlen(tt
->text
);
3713 * Now tt points to the first token after
3714 * the potential paste area...
3716 if (tt
!= t
->next
) {
3717 /* We have at least two tokens... */
3718 len
+= strlen(t
->text
);
3719 p
= tmp
= nasm_malloc(len
+1);
3722 p
= strchr(p
, '\0');
3723 t
= delete_Token(t
);
3725 t
= *tail
= tokenize(tmp
);
3731 t
->next
= tt
; /* Attach the remaining token chain */
3738 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3748 * expands to a list of tokens from %{x:y}
3750 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3752 Token
*t
= tline
, **tt
, *tm
, *head
;
3756 pos
= strchr(tline
->text
, ':');
3759 lst
= atoi(pos
+ 1);
3760 fst
= atoi(tline
->text
+ 1);
3763 * only macros params are accounted so
3764 * if someone passes %0 -- we reject such
3767 if (lst
== 0 || fst
== 0)
3770 /* the values should be sane */
3771 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3772 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3775 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3776 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3778 /* counted from zero */
3782 * it will be at least one token
3784 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3785 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3786 head
= t
, tt
= &t
->next
;
3788 for (i
= fst
+ 1; i
<= lst
; i
++) {
3789 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3790 *tt
= t
, tt
= &t
->next
;
3791 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3792 tm
= mac
->params
[j
];
3793 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3794 *tt
= t
, tt
= &t
->next
;
3797 for (i
= fst
- 1; i
>= lst
; i
--) {
3798 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3799 *tt
= t
, tt
= &t
->next
;
3800 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3801 tm
= mac
->params
[j
];
3802 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3803 *tt
= t
, tt
= &t
->next
;
3811 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3817 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3818 * %-n) and MMacro-local identifiers (%%foo) as well as
3819 * macro indirection (%[...]) and range (%{..:..}).
3821 static Token
*expand_mmac_params(Token
* tline
)
3823 Token
*t
, *tt
, **tail
, *thead
;
3824 bool changed
= false;
3831 if (tline
->type
== TOK_PREPROC_ID
&&
3832 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3833 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3834 tline
->text
[1] == '%')) {
3836 int type
= 0, cc
; /* type = 0 to placate optimisers */
3843 tline
= tline
->next
;
3846 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3847 mac
= mac
->next_active
;
3849 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3851 pos
= strchr(t
->text
, ':');
3853 switch (t
->text
[1]) {
3855 * We have to make a substitution of one of the
3856 * forms %1, %-1, %+1, %%foo, %0.
3860 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3861 text
= nasm_strdup(tmpbuf
);
3865 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3867 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3870 n
= atoi(t
->text
+ 2) - 1;
3871 if (n
>= mac
->nparam
)
3874 if (mac
->nparam
> 1)
3875 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3876 tt
= mac
->params
[n
];
3881 "macro parameter %d is not a condition code",
3886 if (inverse_ccs
[cc
] == -1) {
3888 "condition code `%s' is not invertible",
3892 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3896 n
= atoi(t
->text
+ 2) - 1;
3897 if (n
>= mac
->nparam
)
3900 if (mac
->nparam
> 1)
3901 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3902 tt
= mac
->params
[n
];
3907 "macro parameter %d is not a condition code",
3912 text
= nasm_strdup(conditions
[cc
]);
3916 n
= atoi(t
->text
+ 1) - 1;
3917 if (n
>= mac
->nparam
)
3920 if (mac
->nparam
> 1)
3921 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3922 tt
= mac
->params
[n
];
3925 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3926 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3927 tail
= &(*tail
)->next
;
3931 text
= NULL
; /* we've done it here */
3936 * seems we have a parameters range here
3938 Token
*head
, **last
;
3939 head
= expand_mmac_params_range(mac
, t
, &last
);
3960 } else if (tline
->type
== TOK_INDIRECT
) {
3962 tline
= tline
->next
;
3963 tt
= tokenize(t
->text
);
3964 tt
= expand_mmac_params(tt
);
3965 tt
= expand_smacro(tt
);
3968 tt
->a
.mac
= NULL
; /* Necessary? */
3976 tline
= tline
->next
;
3984 int mask_head
= PP_CONCAT_MASK(TOK_ID
) |
3985 PP_CONCAT_MASK(TOK_NUMBER
) |
3986 PP_CONCAT_MASK(TOK_FLOAT
);
3987 int mask_tail
= PP_CONCAT_MASK(TOK_ID
) |
3988 PP_CONCAT_MASK(TOK_NUMBER
) |
3989 PP_CONCAT_MASK(TOK_FLOAT
) |
3990 PP_CONCAT_MASK(TOK_OTHER
);
3991 paste_tokens(&thead
, mask_head
, mask_tail
, false);
3998 * Expand all single-line macro calls made in the given line.
3999 * Return the expanded version of the line. The original is deemed
4000 * to be destroyed in the process. (In reality we'll just move
4001 * Tokens from input to output a lot of the time, rather than
4002 * actually bothering to destroy and replicate.)
4005 static Token
*expand_smacro(Token
* tline
)
4007 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4008 SMacro
*head
= NULL
, *m
;
4011 unsigned int nparam
, sparam
;
4013 Token
*org_tline
= tline
;
4016 int deadman
= DEADMAN_LIMIT
;
4020 * Trick: we should avoid changing the start token pointer since it can
4021 * be contained in "next" field of other token. Because of this
4022 * we allocate a copy of first token and work with it; at the end of
4023 * routine we copy it back
4026 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4027 org_tline
->text
, 0);
4028 tline
->a
.mac
= org_tline
->a
.mac
;
4029 nasm_free(org_tline
->text
);
4030 org_tline
->text
= NULL
;
4033 expanded
= true; /* Always expand %+ at least once */
4039 while (tline
) { /* main token loop */
4041 error(ERR_NONFATAL
, "interminable macro recursion");
4045 if ((mname
= tline
->text
)) {
4046 /* if this token is a local macro, look in local context */
4047 if (tline
->type
== TOK_ID
) {
4048 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4049 } else if (tline
->type
== TOK_PREPROC_ID
) {
4050 ctx
= get_ctx(mname
, &mname
, true);
4051 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4056 * We've hit an identifier. As in is_mmacro below, we first
4057 * check whether the identifier is a single-line macro at
4058 * all, then think about checking for parameters if
4061 list_for_each(m
, head
)
4062 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4068 if (m
->nparam
== 0) {
4070 * Simple case: the macro is parameterless. Discard the
4071 * one token that the macro call took, and push the
4072 * expansion back on the to-do stack.
4074 if (!m
->expansion
) {
4075 if (!strcmp("__FILE__", m
->name
)) {
4078 src_get(&num
, &file
);
4079 tline
->text
= nasm_quote(file
, strlen(file
));
4080 tline
->type
= TOK_STRING
;
4084 if (!strcmp("__LINE__", m
->name
)) {
4085 nasm_free(tline
->text
);
4086 make_tok_num(tline
, src_get_linnum());
4089 if (!strcmp("__BITS__", m
->name
)) {
4090 nasm_free(tline
->text
);
4091 make_tok_num(tline
, globalbits
);
4094 tline
= delete_Token(tline
);
4099 * Complicated case: at least one macro with this name
4100 * exists and takes parameters. We must find the
4101 * parameters in the call, count them, find the SMacro
4102 * that corresponds to that form of the macro call, and
4103 * substitute for the parameters when we expand. What a
4106 /*tline = tline->next;
4107 skip_white_(tline); */
4110 while (tok_type_(t
, TOK_SMAC_END
)) {
4111 t
->a
.mac
->in_progress
= false;
4113 t
= tline
->next
= delete_Token(t
);
4116 } while (tok_type_(tline
, TOK_WHITESPACE
));
4117 if (!tok_is_(tline
, "(")) {
4119 * This macro wasn't called with parameters: ignore
4120 * the call. (Behaviour borrowed from gnu cpp.)
4129 sparam
= PARAM_DELTA
;
4130 params
= nasm_malloc(sparam
* sizeof(Token
*));
4131 params
[0] = tline
->next
;
4132 paramsize
= nasm_malloc(sparam
* sizeof(int));
4134 while (true) { /* parameter loop */
4136 * For some unusual expansions
4137 * which concatenates function call
4140 while (tok_type_(t
, TOK_SMAC_END
)) {
4141 t
->a
.mac
->in_progress
= false;
4143 t
= tline
->next
= delete_Token(t
);
4149 "macro call expects terminating `)'");
4152 if (tline
->type
== TOK_WHITESPACE
4154 if (paramsize
[nparam
])
4157 params
[nparam
] = tline
->next
;
4158 continue; /* parameter loop */
4160 if (tline
->type
== TOK_OTHER
4161 && tline
->text
[1] == 0) {
4162 char ch
= tline
->text
[0];
4163 if (ch
== ',' && !paren
&& brackets
<= 0) {
4164 if (++nparam
>= sparam
) {
4165 sparam
+= PARAM_DELTA
;
4166 params
= nasm_realloc(params
,
4167 sparam
* sizeof(Token
*));
4168 paramsize
= nasm_realloc(paramsize
,
4169 sparam
* sizeof(int));
4171 params
[nparam
] = tline
->next
;
4172 paramsize
[nparam
] = 0;
4174 continue; /* parameter loop */
4177 (brackets
> 0 || (brackets
== 0 &&
4178 !paramsize
[nparam
])))
4180 if (!(brackets
++)) {
4181 params
[nparam
] = tline
->next
;
4182 continue; /* parameter loop */
4185 if (ch
== '}' && brackets
> 0)
4186 if (--brackets
== 0) {
4188 continue; /* parameter loop */
4190 if (ch
== '(' && !brackets
)
4192 if (ch
== ')' && brackets
<= 0)
4198 error(ERR_NONFATAL
, "braces do not "
4199 "enclose all of macro parameter");
4201 paramsize
[nparam
] += white
+ 1;
4203 } /* parameter loop */
4205 while (m
&& (m
->nparam
!= nparam
||
4206 mstrcmp(m
->name
, mname
,
4210 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4211 "macro `%s' exists, "
4212 "but not taking %d parameters",
4213 mstart
->text
, nparam
);
4216 if (m
&& m
->in_progress
)
4218 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4220 * Design question: should we handle !tline, which
4221 * indicates missing ')' here, or expand those
4222 * macros anyway, which requires the (t) test a few
4226 nasm_free(paramsize
);
4230 * Expand the macro: we are placed on the last token of the
4231 * call, so that we can easily split the call from the
4232 * following tokens. We also start by pushing an SMAC_END
4233 * token for the cycle removal.
4240 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4242 m
->in_progress
= true;
4244 list_for_each(t
, m
->expansion
) {
4245 if (t
->type
>= TOK_SMAC_PARAM
) {
4246 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4250 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4251 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4253 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4259 } else if (t
->type
== TOK_PREPROC_Q
) {
4260 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4262 } else if (t
->type
== TOK_PREPROC_QQ
) {
4263 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4266 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4272 * Having done that, get rid of the macro call, and clean
4273 * up the parameters.
4276 nasm_free(paramsize
);
4279 continue; /* main token loop */
4284 if (tline
->type
== TOK_SMAC_END
) {
4285 tline
->a
.mac
->in_progress
= false;
4286 tline
= delete_Token(tline
);
4289 tline
= tline
->next
;
4297 * Now scan the entire line and look for successive TOK_IDs that resulted
4298 * after expansion (they can't be produced by tokenize()). The successive
4299 * TOK_IDs should be concatenated.
4300 * Also we look for %+ tokens and concatenate the tokens before and after
4301 * them (without white spaces in between).
4304 int mask_head
= PP_CONCAT_MASK(TOK_ID
) |
4305 PP_CONCAT_MASK(TOK_PREPROC_ID
);
4306 int mask_tail
= PP_CONCAT_MASK(TOK_ID
) |
4307 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4308 PP_CONCAT_MASK(TOK_NUMBER
);
4309 if (paste_tokens(&thead
, mask_head
, mask_tail
, true)) {
4311 * If we concatenated something, *and* we had previously expanded
4312 * an actual macro, scan the lines again for macros...
4323 *org_tline
= *thead
;
4324 /* since we just gave text to org_line, don't free it */
4326 delete_Token(thead
);
4328 /* the expression expanded to empty line;
4329 we can't return NULL for some reasons
4330 we just set the line to a single WHITESPACE token. */
4331 memset(org_tline
, 0, sizeof(*org_tline
));
4332 org_tline
->text
= NULL
;
4333 org_tline
->type
= TOK_WHITESPACE
;
4342 * Similar to expand_smacro but used exclusively with macro identifiers
4343 * right before they are fetched in. The reason is that there can be
4344 * identifiers consisting of several subparts. We consider that if there
4345 * are more than one element forming the name, user wants a expansion,
4346 * otherwise it will be left as-is. Example:
4350 * the identifier %$abc will be left as-is so that the handler for %define
4351 * will suck it and define the corresponding value. Other case:
4353 * %define _%$abc cde
4355 * In this case user wants name to be expanded *before* %define starts
4356 * working, so we'll expand %$abc into something (if it has a value;
4357 * otherwise it will be left as-is) then concatenate all successive
4360 static Token
*expand_id(Token
* tline
)
4362 Token
*cur
, *oldnext
= NULL
;
4364 if (!tline
|| !tline
->next
)
4369 (cur
->next
->type
== TOK_ID
||
4370 cur
->next
->type
== TOK_PREPROC_ID
4371 || cur
->next
->type
== TOK_NUMBER
))
4374 /* If identifier consists of just one token, don't expand */
4379 oldnext
= cur
->next
; /* Detach the tail past identifier */
4380 cur
->next
= NULL
; /* so that expand_smacro stops here */
4383 tline
= expand_smacro(tline
);
4386 /* expand_smacro possibly changhed tline; re-scan for EOL */
4388 while (cur
&& cur
->next
)
4391 cur
->next
= oldnext
;
4398 * Determine whether the given line constitutes a multi-line macro
4399 * call, and return the MMacro structure called if so. Doesn't have
4400 * to check for an initial label - that's taken care of in
4401 * expand_mmacro - but must check numbers of parameters. Guaranteed
4402 * to be called with tline->type == TOK_ID, so the putative macro
4403 * name is easy to find.
4405 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4411 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4414 * Efficiency: first we see if any macro exists with the given
4415 * name. If not, we can return NULL immediately. _Then_ we
4416 * count the parameters, and then we look further along the
4417 * list if necessary to find the proper MMacro.
4419 list_for_each(m
, head
)
4420 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4426 * OK, we have a potential macro. Count and demarcate the
4429 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4432 * So we know how many parameters we've got. Find the MMacro
4433 * structure that handles this number.
4436 if (m
->nparam_min
<= nparam
4437 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4439 * This one is right. Just check if cycle removal
4440 * prohibits us using it before we actually celebrate...
4442 if (m
->in_progress
> m
->max_depth
) {
4443 if (m
->max_depth
> 0) {
4445 "reached maximum recursion depth of %i",
4452 * It's right, and we can use it. Add its default
4453 * parameters to the end of our list if necessary.
4455 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4457 nasm_realloc(params
,
4458 ((m
->nparam_min
+ m
->ndefs
+
4459 1) * sizeof(*params
)));
4460 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4461 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4466 * If we've gone over the maximum parameter count (and
4467 * we're in Plus mode), ignore parameters beyond
4470 if (m
->plus
&& nparam
> m
->nparam_max
)
4471 nparam
= m
->nparam_max
;
4473 * Then terminate the parameter list, and leave.
4475 if (!params
) { /* need this special case */
4476 params
= nasm_malloc(sizeof(*params
));
4479 params
[nparam
] = NULL
;
4480 *params_array
= params
;
4484 * This one wasn't right: look for the next one with the
4487 list_for_each(m
, m
->next
)
4488 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4493 * After all that, we didn't find one with the right number of
4494 * parameters. Issue a warning, and fail to expand the macro.
4496 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4497 "macro `%s' exists, but not taking %d parameters",
4498 tline
->text
, nparam
);
4505 * Save MMacro invocation specific fields in
4506 * preparation for a recursive macro expansion
4508 static void push_mmacro(MMacro
*m
)
4510 MMacroInvocation
*i
;
4512 i
= nasm_malloc(sizeof(MMacroInvocation
));
4514 i
->params
= m
->params
;
4515 i
->iline
= m
->iline
;
4516 i
->nparam
= m
->nparam
;
4517 i
->rotate
= m
->rotate
;
4518 i
->paramlen
= m
->paramlen
;
4519 i
->unique
= m
->unique
;
4520 i
->condcnt
= m
->condcnt
;
4526 * Restore MMacro invocation specific fields that were
4527 * saved during a previous recursive macro expansion
4529 static void pop_mmacro(MMacro
*m
)
4531 MMacroInvocation
*i
;
4536 m
->params
= i
->params
;
4537 m
->iline
= i
->iline
;
4538 m
->nparam
= i
->nparam
;
4539 m
->rotate
= i
->rotate
;
4540 m
->paramlen
= i
->paramlen
;
4541 m
->unique
= i
->unique
;
4542 m
->condcnt
= i
->condcnt
;
4549 * Expand the multi-line macro call made by the given line, if
4550 * there is one to be expanded. If there is, push the expansion on
4551 * istk->expansion and return 1. Otherwise return 0.
4553 static int expand_mmacro(Token
* tline
)
4555 Token
*startline
= tline
;
4556 Token
*label
= NULL
;
4557 int dont_prepend
= 0;
4558 Token
**params
, *t
, *mtok
, *tt
;
4561 int i
, nparam
, *paramlen
;
4566 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4567 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4570 m
= is_mmacro(t
, ¶ms
);
4576 * We have an id which isn't a macro call. We'll assume
4577 * it might be a label; we'll also check to see if a
4578 * colon follows it. Then, if there's another id after
4579 * that lot, we'll check it again for macro-hood.
4583 if (tok_type_(t
, TOK_WHITESPACE
))
4584 last
= t
, t
= t
->next
;
4585 if (tok_is_(t
, ":")) {
4587 last
= t
, t
= t
->next
;
4588 if (tok_type_(t
, TOK_WHITESPACE
))
4589 last
= t
, t
= t
->next
;
4591 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4599 * Fix up the parameters: this involves stripping leading and
4600 * trailing whitespace, then stripping braces if they are
4603 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4604 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4606 for (i
= 0; params
[i
]; i
++) {
4608 int comma
= (!m
->plus
|| i
< nparam
- 1);
4612 if (tok_is_(t
, "{"))
4613 t
= t
->next
, brace
= true, comma
= false;
4617 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4618 break; /* ... because we have hit a comma */
4619 if (comma
&& t
->type
== TOK_WHITESPACE
4620 && tok_is_(t
->next
, ","))
4621 break; /* ... or a space then a comma */
4622 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4623 break; /* ... or a brace */
4630 * OK, we have a MMacro structure together with a set of
4631 * parameters. We must now go through the expansion and push
4632 * copies of each Line on to istk->expansion. Substitution of
4633 * parameter tokens and macro-local tokens doesn't get done
4634 * until the single-line macro substitution process; this is
4635 * because delaying them allows us to change the semantics
4636 * later through %rotate.
4638 * First, push an end marker on to istk->expansion, mark this
4639 * macro as in progress, and set up its invocation-specific
4642 ll
= nasm_malloc(sizeof(Line
));
4643 ll
->next
= istk
->expansion
;
4646 istk
->expansion
= ll
;
4649 * Save the previous MMacro expansion in the case of
4652 if (m
->max_depth
&& m
->in_progress
)
4660 m
->paramlen
= paramlen
;
4661 m
->unique
= unique
++;
4665 m
->next_active
= istk
->mstk
;
4668 list_for_each(l
, m
->expansion
) {
4671 ll
= nasm_malloc(sizeof(Line
));
4672 ll
->finishes
= NULL
;
4673 ll
->next
= istk
->expansion
;
4674 istk
->expansion
= ll
;
4677 list_for_each(t
, l
->first
) {
4681 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4683 case TOK_PREPROC_QQ
:
4684 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4686 case TOK_PREPROC_ID
:
4687 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4695 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4704 * If we had a label, push it on as the first line of
4705 * the macro expansion.
4708 if (dont_prepend
< 0)
4709 free_tlist(startline
);
4711 ll
= nasm_malloc(sizeof(Line
));
4712 ll
->finishes
= NULL
;
4713 ll
->next
= istk
->expansion
;
4714 istk
->expansion
= ll
;
4715 ll
->first
= startline
;
4716 if (!dont_prepend
) {
4718 label
= label
->next
;
4719 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4724 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4729 /* The function that actually does the error reporting */
4730 static void verror(int severity
, const char *fmt
, va_list arg
)
4733 MMacro
*mmac
= NULL
;
4736 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4738 /* get %macro name */
4739 if (istk
&& istk
->mstk
) {
4741 /* but %rep blocks should be skipped */
4742 while (mmac
&& !mmac
->name
)
4743 mmac
= mmac
->next_active
, delta
++;
4747 nasm_error(severity
, "(%s:%d) %s",
4748 mmac
->name
, mmac
->lineno
- delta
, buff
);
4750 nasm_error(severity
, "%s", buff
);
4754 * Since preprocessor always operate only on the line that didn't
4755 * arrived yet, we should always use ERR_OFFBY1.
4757 static void error(int severity
, const char *fmt
, ...)
4761 /* If we're in a dead branch of IF or something like it, ignore the error */
4762 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4766 verror(severity
, fmt
, arg
);
4771 * Because %else etc are evaluated in the state context
4772 * of the previous branch, errors might get lost with error():
4773 * %if 0 ... %else trailing garbage ... %endif
4774 * So %else etc should report errors with this function.
4776 static void error_precond(int severity
, const char *fmt
, ...)
4780 /* Only ignore the error if it's really in a dead branch */
4781 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4785 verror(severity
, fmt
, arg
);
4790 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4795 istk
= nasm_malloc(sizeof(Include
));
4798 istk
->expansion
= NULL
;
4800 istk
->fp
= fopen(file
, "r");
4802 src_set_fname(nasm_strdup(file
));
4806 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4809 nested_mac_count
= 0;
4810 nested_rep_count
= 0;
4813 if (tasm_compatible_mode
) {
4814 stdmacpos
= nasm_stdmac
;
4816 stdmacpos
= nasm_stdmac_after_tasm
;
4818 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4823 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4824 * The caller, however, will also pass in 3 for preprocess-only so
4825 * we can set __PASS__ accordingly.
4827 pass
= apass
> 2 ? 2 : apass
;
4829 dephead
= deptail
= deplist
;
4831 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4833 strcpy(sl
->str
, file
);
4835 deptail
= &sl
->next
;
4839 * Define the __PASS__ macro. This is defined here unlike
4840 * all the other builtins, because it is special -- it varies between
4843 t
= nasm_malloc(sizeof(*t
));
4845 make_tok_num(t
, apass
);
4847 define_smacro(NULL
, "__PASS__", true, 0, t
);
4850 static char *pp_getline(void)
4857 * Fetch a tokenized line, either from the macro-expansion
4858 * buffer or from the input file.
4861 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4862 Line
*l
= istk
->expansion
;
4863 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4867 * This is a macro-end marker for a macro with no
4868 * name, which means it's not really a macro at all
4869 * but a %rep block, and the `in_progress' field is
4870 * more than 1, meaning that we still need to
4871 * repeat. (1 means the natural last repetition; 0
4872 * means termination by %exitrep.) We have
4873 * therefore expanded up to the %endrep, and must
4874 * push the whole block on to the expansion buffer
4875 * again. We don't bother to remove the macro-end
4876 * marker: we'd only have to generate another one
4879 l
->finishes
->in_progress
--;
4880 list_for_each(l
, l
->finishes
->expansion
) {
4881 Token
*t
, *tt
, **tail
;
4883 ll
= nasm_malloc(sizeof(Line
));
4884 ll
->next
= istk
->expansion
;
4885 ll
->finishes
= NULL
;
4889 list_for_each(t
, l
->first
) {
4890 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4891 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4896 istk
->expansion
= ll
;
4900 * Check whether a `%rep' was started and not ended
4901 * within this macro expansion. This can happen and
4902 * should be detected. It's a fatal error because
4903 * I'm too confused to work out how to recover
4909 "defining with name in expansion");
4910 else if (istk
->mstk
->name
)
4912 "`%%rep' without `%%endrep' within"
4913 " expansion of macro `%s'",
4918 * FIXME: investigate the relationship at this point between
4919 * istk->mstk and l->finishes
4922 MMacro
*m
= istk
->mstk
;
4923 istk
->mstk
= m
->next_active
;
4926 * This was a real macro call, not a %rep, and
4927 * therefore the parameter information needs to
4932 l
->finishes
->in_progress
--;
4934 nasm_free(m
->params
);
4935 free_tlist(m
->iline
);
4936 nasm_free(m
->paramlen
);
4937 l
->finishes
->in_progress
= 0;
4942 istk
->expansion
= l
->next
;
4944 list
->downlevel(LIST_MACRO
);
4947 while (1) { /* until we get a line we can use */
4949 if (istk
->expansion
) { /* from a macro expansion */
4951 Line
*l
= istk
->expansion
;
4953 istk
->mstk
->lineno
++;
4955 istk
->expansion
= l
->next
;
4957 p
= detoken(tline
, false);
4958 list
->line(LIST_MACRO
, p
);
4963 if (line
) { /* from the current input file */
4964 line
= prepreproc(line
);
4965 tline
= tokenize(line
);
4970 * The current file has ended; work down the istk
4976 /* nasm_error can't be conditionally suppressed */
4977 nasm_error(ERR_FATAL
,
4978 "expected `%%endif' before end of file");
4980 /* only set line and file name if there's a next node */
4982 src_set_linnum(i
->lineno
);
4983 nasm_free(src_set_fname(i
->fname
));
4986 list
->downlevel(LIST_INCLUDE
);
4990 if (istk
->expansion
&& istk
->expansion
->finishes
)
4996 * We must expand MMacro parameters and MMacro-local labels
4997 * _before_ we plunge into directive processing, to cope
4998 * with things like `%define something %1' such as STRUC
4999 * uses. Unless we're _defining_ a MMacro, in which case
5000 * those tokens should be left alone to go into the
5001 * definition; and unless we're in a non-emitting
5002 * condition, in which case we don't want to meddle with
5005 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
5006 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
5007 tline
= expand_mmac_params(tline
);
5011 * Check the line to see if it's a preprocessor directive.
5013 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5015 } else if (defining
) {
5017 * We're defining a multi-line macro. We emit nothing
5019 * shove the tokenized line on to the macro definition.
5021 Line
*l
= nasm_malloc(sizeof(Line
));
5022 l
->next
= defining
->expansion
;
5025 defining
->expansion
= l
;
5027 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
5029 * We're in a non-emitting branch of a condition block.
5030 * Emit nothing at all, not even a blank line: when we
5031 * emerge from the condition we'll give a line-number
5032 * directive so we keep our place correctly.
5036 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5038 * We're in a %rep block which has been terminated, so
5039 * we're walking through to the %endrep without
5040 * emitting anything. Emit nothing at all, not even a
5041 * blank line: when we emerge from the %rep block we'll
5042 * give a line-number directive so we keep our place
5048 tline
= expand_smacro(tline
);
5049 if (!expand_mmacro(tline
)) {
5051 * De-tokenize the line again, and emit it.
5053 line
= detoken(tline
, true);
5057 continue; /* expand_mmacro calls free_tlist */
5065 static void pp_cleanup(int pass
)
5068 if (defining
->name
) {
5070 "end of file while still defining macro `%s'",
5073 error(ERR_NONFATAL
, "end of file while still in %%rep");
5076 free_mmacro(defining
);
5086 nasm_free(i
->fname
);
5091 nasm_free(src_set_fname(NULL
));
5096 while ((i
= ipath
)) {
5105 void pp_include_path(char *path
)
5109 i
= nasm_malloc(sizeof(IncPath
));
5110 i
->path
= path
? nasm_strdup(path
) : NULL
;
5123 void pp_pre_include(char *fname
)
5125 Token
*inc
, *space
, *name
;
5128 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5129 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5130 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5132 l
= nasm_malloc(sizeof(Line
));
5139 void pp_pre_define(char *definition
)
5145 equals
= strchr(definition
, '=');
5146 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5147 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5150 space
->next
= tokenize(definition
);
5154 l
= nasm_malloc(sizeof(Line
));
5161 void pp_pre_undefine(char *definition
)
5166 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5167 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5168 space
->next
= tokenize(definition
);
5170 l
= nasm_malloc(sizeof(Line
));
5178 * Added by Keith Kanios:
5180 * This function is used to assist with "runtime" preprocessor
5181 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5183 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5184 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5187 void pp_runtime(char *definition
)
5191 def
= tokenize(definition
);
5192 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5197 void pp_extra_stdmac(macros_t
*macros
)
5199 extrastdmac
= macros
;
5202 static void make_tok_num(Token
* tok
, int64_t val
)
5205 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5206 tok
->text
= nasm_strdup(numbuf
);
5207 tok
->type
= TOK_NUMBER
;