1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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))
217 struct tokseq_match
{
226 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
227 size_t len
; /* scratch length field */
228 } a
; /* Auxiliary data */
229 enum pp_token_type type
;
233 * Multi-line macro definitions are stored as a linked list of
234 * these, which is essentially a container to allow several linked
237 * Note that in this module, linked lists are treated as stacks
238 * wherever possible. For this reason, Lines are _pushed_ on to the
239 * `expansion' field in MMacro structures, so that the linked list,
240 * if walked, would give the macro lines in reverse order; this
241 * means that we can walk the list when expanding a macro, and thus
242 * push the lines on to the `expansion' field in _istk_ in reverse
243 * order (so that when popped back off they are in the right
244 * order). It may seem cockeyed, and it relies on my design having
245 * an even number of steps in, but it works...
247 * Some of these structures, rather than being actual lines, are
248 * markers delimiting the end of the expansion of a given macro.
249 * This is for use in the cycle-tracking and %rep-handling code.
250 * Such structures have `finishes' non-NULL, and `first' NULL. All
251 * others have `finishes' NULL, but `first' may still be NULL if
261 * To handle an arbitrary level of file inclusion, we maintain a
262 * stack (ie linked list) of these things.
271 MMacro
*mstk
; /* stack of active macros/reps */
275 * Include search path. This is simply a list of strings which get
276 * prepended, in turn, to the name of an include file, in an
277 * attempt to find the file if it's not in the current directory.
285 * Conditional assembly: we maintain a separate stack of these for
286 * each level of file inclusion. (The only reason we keep the
287 * stacks separate is to ensure that a stray `%endif' in a file
288 * included from within the true branch of a `%if' won't terminate
289 * it and cause confusion: instead, rightly, it'll cause an error.)
297 * These states are for use just after %if or %elif: IF_TRUE
298 * means the condition has evaluated to truth so we are
299 * currently emitting, whereas IF_FALSE means we are not
300 * currently emitting but will start doing so if a %else comes
301 * up. In these states, all directives are admissible: %elif,
302 * %else and %endif. (And of course %if.)
304 COND_IF_TRUE
, COND_IF_FALSE
,
306 * These states come up after a %else: ELSE_TRUE means we're
307 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
308 * any %elif or %else will cause an error.
310 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
312 * These states mean that we're not emitting now, and also that
313 * nothing until %endif will be emitted at all. COND_DONE is
314 * used when we've had our moment of emission
315 * and have now started seeing %elifs. COND_NEVER is used when
316 * the condition construct in question is contained within a
317 * non-emitting branch of a larger condition construct,
318 * or if there is an error.
320 COND_DONE
, COND_NEVER
322 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325 * These defines are used as the possible return values for do_directive
327 #define NO_DIRECTIVE_FOUND 0
328 #define DIRECTIVE_FOUND 1
331 * This define sets the upper limit for smacro and recursive mmacro
334 #define DEADMAN_LIMIT (1 << 20)
337 #define REP_LIMIT ((INT64_C(1) << 62))
340 * Condition codes. Note that we use c_ prefix not C_ because C_ is
341 * used in nasm.h for the "real" condition codes. At _this_ level,
342 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
343 * ones, so we need a different enum...
345 static const char * const conditions
[] = {
346 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
347 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
348 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
351 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
352 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
353 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
356 static const enum pp_conds inverse_ccs
[] = {
357 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
358 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
,
359 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
365 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
366 static int is_condition(enum preproc_token arg
)
368 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
371 /* For TASM compatibility we need to be able to recognise TASM compatible
372 * conditional compilation directives. Using the NASM pre-processor does
373 * not work, so we look for them specifically from the following list and
374 * then jam in the equivalent NASM directive into the input stream.
378 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
379 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
382 static const char * const tasm_directives
[] = {
383 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
384 "ifndef", "include", "local"
387 static int StackSize
= 4;
388 static char *StackPointer
= "ebp";
389 static int ArgOffset
= 8;
390 static int LocalOffset
= 0;
392 static Context
*cstk
;
393 static Include
*istk
;
394 static IncPath
*ipath
= NULL
;
396 static int pass
; /* HACK: pass 0 = generate dependencies only */
397 static StrList
**dephead
, **deptail
; /* Dependency list */
399 static uint64_t unique
; /* unique identifier numbers */
401 static Line
*predef
= NULL
;
402 static bool do_predef
;
404 static ListGen
*list
;
407 * The current set of multi-line macros we have defined.
409 static struct hash_table mmacros
;
412 * The current set of single-line macros we have defined.
414 static struct hash_table smacros
;
417 * The multi-line macro we are currently defining, or the %rep
418 * block we are currently reading, if any.
420 static MMacro
*defining
;
422 static uint64_t nested_mac_count
;
423 static uint64_t nested_rep_count
;
426 * The number of macro parameters to allocate space for at a time.
428 #define PARAM_DELTA 16
431 * The standard macro set: defined in macros.c in the array nasm_stdmac.
432 * This gives our position in the macro set, when we're processing it.
434 static macros_t
*stdmacpos
;
437 * The extra standard macros that come from the object format, if
440 static macros_t
*extrastdmac
= NULL
;
441 static bool any_extrastdmac
;
444 * Tokens are allocated in blocks to improve speed
446 #define TOKEN_BLOCKSIZE 4096
447 static Token
*freeTokens
= NULL
;
453 static Blocks blocks
= { NULL
, NULL
};
456 * Forward declarations.
458 static Token
*expand_mmac_params(Token
* tline
);
459 static Token
*expand_smacro(Token
* tline
);
460 static Token
*expand_id(Token
* tline
);
461 static Context
*get_ctx(const char *name
, const char **namep
);
462 static void make_tok_num(Token
* tok
, int64_t val
);
463 static void error(int severity
, const char *fmt
, ...);
464 static void error_precond(int severity
, const char *fmt
, ...);
465 static void *new_Block(size_t size
);
466 static void delete_Blocks(void);
467 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
468 const char *text
, int txtlen
);
469 static Token
*delete_Token(Token
* t
);
472 * Macros for safe checking of token pointers, avoid *(NULL)
474 #define tok_type_(x,t) ((x) && (x)->type == (t))
475 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
476 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
477 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
480 * nasm_unquote with error if the string contains NUL characters.
481 * If the string contains NUL characters, issue an error and return
482 * the C len, i.e. truncate at the NUL.
484 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
486 size_t len
= nasm_unquote(qstr
, NULL
);
487 size_t clen
= strlen(qstr
);
490 error(ERR_NONFATAL
, "NUL character in `%s' directive",
491 pp_directives
[directive
]);
497 * In-place reverse a list of tokens.
499 static Token
*reverse_tokens(Token
*t
)
515 * Handle TASM specific directives, which do not contain a % in
516 * front of them. We do it here because I could not find any other
517 * place to do it for the moment, and it is a hack (ideally it would
518 * be nice to be able to use the NASM pre-processor to do it).
520 static char *check_tasm_directive(char *line
)
522 int32_t i
, j
, k
, m
, len
;
523 char *p
, *q
, *oldline
, oldchar
;
525 p
= nasm_skip_spaces(line
);
527 /* Binary search for the directive name */
529 j
= ARRAY_SIZE(tasm_directives
);
530 q
= nasm_skip_word(p
);
537 m
= nasm_stricmp(p
, tasm_directives
[k
]);
539 /* We have found a directive, so jam a % in front of it
540 * so that NASM will then recognise it as one if it's own.
545 line
= nasm_malloc(len
+ 2);
547 if (k
== TM_IFDIFI
) {
549 * NASM does not recognise IFDIFI, so we convert
550 * it to %if 0. This is not used in NASM
551 * compatible code, but does need to parse for the
552 * TASM macro package.
554 strcpy(line
+ 1, "if 0");
556 memcpy(line
+ 1, p
, len
+ 1);
571 * The pre-preprocessing stage... This function translates line
572 * number indications as they emerge from GNU cpp (`# lineno "file"
573 * flags') into NASM preprocessor line number indications (`%line
576 static char *prepreproc(char *line
)
579 char *fname
, *oldline
;
581 if (line
[0] == '#' && line
[1] == ' ') {
584 lineno
= atoi(fname
);
585 fname
+= strspn(fname
, "0123456789 ");
588 fnlen
= strcspn(fname
, "\"");
589 line
= nasm_malloc(20 + fnlen
);
590 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
593 if (tasm_compatible_mode
)
594 return check_tasm_directive(line
);
599 * Free a linked list of tokens.
601 static void free_tlist(Token
* list
)
604 list
= delete_Token(list
);
608 * Free a linked list of lines.
610 static void free_llist(Line
* list
)
613 list_for_each_safe(l
, tmp
, list
) {
614 free_tlist(l
->first
);
622 static void free_mmacro(MMacro
* m
)
625 free_tlist(m
->dlist
);
626 nasm_free(m
->defaults
);
627 free_llist(m
->expansion
);
632 * Free all currently defined macros, and free the hash tables
634 static void free_smacro_table(struct hash_table
*smt
)
638 struct hash_tbl_node
*it
= NULL
;
640 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
641 nasm_free((void *)key
);
642 list_for_each_safe(s
, tmp
, s
) {
644 free_tlist(s
->expansion
);
651 static void free_mmacro_table(struct hash_table
*mmt
)
655 struct hash_tbl_node
*it
= NULL
;
658 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
659 nasm_free((void *)key
);
660 list_for_each_safe(m
,tmp
, m
)
666 static void free_macros(void)
668 free_smacro_table(&smacros
);
669 free_mmacro_table(&mmacros
);
673 * Initialize the hash tables
675 static void init_macros(void)
677 hash_init(&smacros
, HASH_LARGE
);
678 hash_init(&mmacros
, HASH_LARGE
);
682 * Pop the context stack.
684 static void ctx_pop(void)
689 free_smacro_table(&c
->localmac
);
695 * Search for a key in the hash index; adding it if necessary
696 * (in which case we initialize the data pointer to NULL.)
699 hash_findi_add(struct hash_table
*hash
, const char *str
)
701 struct hash_insert hi
;
705 r
= hash_findi(hash
, str
, &hi
);
709 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
710 return hash_add(&hi
, strx
, NULL
);
714 * Like hash_findi, but returns the data element rather than a pointer
715 * to it. Used only when not adding a new element, hence no third
719 hash_findix(struct hash_table
*hash
, const char *str
)
723 p
= hash_findi(hash
, str
, NULL
);
724 return p
? *p
: NULL
;
728 * read line from standart macros set,
729 * if there no more left -- return NULL
731 static char *line_from_stdmac(void)
734 const unsigned char *p
= stdmacpos
;
743 len
+= pp_directives_len
[c
- 0x80] + 1;
748 line
= nasm_malloc(len
+ 1);
750 while ((c
= *stdmacpos
++)) {
752 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
753 q
+= pp_directives_len
[c
- 0x80];
763 /* This was the last of the standard macro chain... */
765 if (any_extrastdmac
) {
766 stdmacpos
= extrastdmac
;
767 any_extrastdmac
= false;
768 } else if (do_predef
) {
770 Token
*head
, **tail
, *t
;
773 * Nasty hack: here we push the contents of
774 * `predef' on to the top-level expansion stack,
775 * since this is the most convenient way to
776 * implement the pre-include and pre-define
779 list_for_each(pd
, predef
) {
782 list_for_each(t
, pd
->first
) {
783 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
784 tail
= &(*tail
)->next
;
787 l
= nasm_malloc(sizeof(Line
));
788 l
->next
= istk
->expansion
;
801 #define BUF_DELTA 512
803 * Read a line from the top file in istk, handling multiple CR/LFs
804 * at the end of the line read, and handling spurious ^Zs. Will
805 * return lines from the standard macro set if this has not already
808 static char *read_line(void)
810 char *buffer
, *p
, *q
;
811 int bufsize
, continued_count
;
814 * standart macros set (predefined) goes first
816 p
= line_from_stdmac();
821 * regular read from a file
824 buffer
= nasm_malloc(BUF_DELTA
);
828 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
832 if (p
> buffer
&& p
[-1] == '\n') {
834 * Convert backslash-CRLF line continuation sequences into
835 * nothing at all (for DOS and Windows)
837 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
843 * Also convert backslash-LF line continuation sequences into
844 * nothing at all (for Unix)
846 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
854 if (p
- buffer
> bufsize
- 10) {
855 int32_t offset
= p
- buffer
;
856 bufsize
+= BUF_DELTA
;
857 buffer
= nasm_realloc(buffer
, bufsize
);
858 p
= buffer
+ offset
; /* prevent stale-pointer problems */
862 if (!q
&& p
== buffer
) {
867 src_set_linnum(src_get_linnum() + istk
->lineinc
+
868 (continued_count
* istk
->lineinc
));
871 * Play safe: remove CRs as well as LFs, if any of either are
872 * present at the end of the line.
874 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
878 * Handle spurious ^Z, which may be inserted into source files
879 * by some file transfer utilities.
881 buffer
[strcspn(buffer
, "\032")] = '\0';
883 list
->line(LIST_READ
, buffer
);
889 * Tokenize a line of text. This is a very simple process since we
890 * don't need to parse the value out of e.g. numeric tokens: we
891 * simply split one string into many.
893 static Token
*tokenize(char *line
)
896 enum pp_token_type type
;
898 Token
*t
, **tail
= &list
;
904 if (*p
== '+' && !nasm_isdigit(p
[1])) {
907 } else if (nasm_isdigit(*p
) ||
908 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
912 while (nasm_isdigit(*p
));
913 type
= TOK_PREPROC_ID
;
914 } else if (*p
== '{') {
923 error(ERR_WARNING
| ERR_PASS1
, "unterminated %{ construct");
927 type
= TOK_PREPROC_ID
;
928 } else if (*p
== '[') {
930 line
+= 2; /* Skip the leading %[ */
932 while (lvl
&& (c
= *p
++)) {
944 p
= nasm_skip_string(p
- 1) + 1;
954 error(ERR_NONFATAL
, "unterminated %[ construct");
956 } else if (*p
== '?') {
957 type
= TOK_PREPROC_Q
; /* %? */
960 type
= TOK_PREPROC_QQ
; /* %?? */
963 } else if (*p
== '!') {
964 type
= TOK_PREPROC_ID
;
970 while (isidchar(*p
));
971 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
972 p
= nasm_skip_string(p
);
976 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
978 /* %! without string or identifier */
979 type
= TOK_OTHER
; /* Legacy behavior... */
981 } else if (isidchar(*p
) ||
982 ((*p
== '!' || *p
== '%' || *p
== '$') &&
987 while (isidchar(*p
));
988 type
= TOK_PREPROC_ID
;
994 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
997 while (*p
&& isidchar(*p
))
999 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1004 p
= nasm_skip_string(p
);
1009 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1010 /* Handling unterminated strings by UNV */
1013 } else if (p
[0] == '$' && p
[1] == '$') {
1014 type
= TOK_OTHER
; /* TOKEN_BASE */
1016 } else if (isnumstart(*p
)) {
1017 bool is_hex
= false;
1018 bool is_float
= false;
1034 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1036 if (*p
== '+' || *p
== '-') {
1038 * e can only be followed by +/- if it is either a
1039 * prefixed hex number or a floating-point number
1044 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1046 } else if (c
== 'P' || c
== 'p') {
1048 if (*p
== '+' || *p
== '-')
1050 } else if (isnumchar(c
) || c
== '_')
1051 ; /* just advance */
1052 else if (c
== '.') {
1054 * we need to deal with consequences of the legacy
1055 * parser, like "1.nolist" being two tokens
1056 * (TOK_NUMBER, TOK_ID) here; at least give it
1057 * a shot for now. In the future, we probably need
1058 * a flex-based scanner with proper pattern matching
1059 * to do it as well as it can be done. Nothing in
1060 * the world is going to help the person who wants
1061 * 0x123.p16 interpreted as two tokens, though.
1067 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1068 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1069 (*r
== 'p' || *r
== 'P')) {
1073 break; /* Terminate the token */
1077 p
--; /* Point to first character beyond number */
1079 if (p
== line
+1 && *line
== '$') {
1080 type
= TOK_OTHER
; /* TOKEN_HERE */
1082 if (has_e
&& !is_hex
) {
1083 /* 1e13 is floating-point, but 1e13h is not */
1087 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1089 } else if (nasm_isspace(*p
)) {
1090 type
= TOK_WHITESPACE
;
1091 p
= nasm_skip_spaces(p
);
1093 * Whitespace just before end-of-line is discarded by
1094 * pretending it's a comment; whitespace just before a
1095 * comment gets lumped into the comment.
1097 if (!*p
|| *p
== ';') {
1102 } else if (*p
== ';') {
1108 * Anything else is an operator of some kind. We check
1109 * for all the double-character operators (>>, <<, //,
1110 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1111 * else is a single-character operator.
1114 if ((p
[0] == '>' && p
[1] == '>') ||
1115 (p
[0] == '<' && p
[1] == '<') ||
1116 (p
[0] == '/' && p
[1] == '/') ||
1117 (p
[0] == '<' && p
[1] == '=') ||
1118 (p
[0] == '>' && p
[1] == '=') ||
1119 (p
[0] == '=' && p
[1] == '=') ||
1120 (p
[0] == '!' && p
[1] == '=') ||
1121 (p
[0] == '<' && p
[1] == '>') ||
1122 (p
[0] == '&' && p
[1] == '&') ||
1123 (p
[0] == '|' && p
[1] == '|') ||
1124 (p
[0] == '^' && p
[1] == '^')) {
1130 /* Handling unterminated string by UNV */
1133 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1134 t->text[p-line] = *line;
1138 if (type
!= TOK_COMMENT
) {
1139 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1148 * this function allocates a new managed block of memory and
1149 * returns a pointer to the block. The managed blocks are
1150 * deleted only all at once by the delete_Blocks function.
1152 static void *new_Block(size_t size
)
1154 Blocks
*b
= &blocks
;
1156 /* first, get to the end of the linked list */
1159 /* now allocate the requested chunk */
1160 b
->chunk
= nasm_malloc(size
);
1162 /* now allocate a new block for the next request */
1163 b
->next
= nasm_malloc(sizeof(Blocks
));
1164 /* and initialize the contents of the new block */
1165 b
->next
->next
= NULL
;
1166 b
->next
->chunk
= NULL
;
1171 * this function deletes all managed blocks of memory
1173 static void delete_Blocks(void)
1175 Blocks
*a
, *b
= &blocks
;
1178 * keep in mind that the first block, pointed to by blocks
1179 * is a static and not dynamically allocated, so we don't
1184 nasm_free(b
->chunk
);
1193 * this function creates a new Token and passes a pointer to it
1194 * back to the caller. It sets the type and text elements, and
1195 * also the a.mac and next elements to NULL.
1197 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1198 const char *text
, int txtlen
)
1204 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1205 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1206 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1207 freeTokens
[i
].next
= NULL
;
1210 freeTokens
= t
->next
;
1214 if (type
== TOK_WHITESPACE
|| !text
) {
1218 txtlen
= strlen(text
);
1219 t
->text
= nasm_malloc(txtlen
+1);
1220 memcpy(t
->text
, text
, txtlen
);
1221 t
->text
[txtlen
] = '\0';
1226 static Token
*delete_Token(Token
* t
)
1228 Token
*next
= t
->next
;
1230 t
->next
= freeTokens
;
1236 * Convert a line of tokens back into text.
1237 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1238 * will be transformed into ..@ctxnum.xxx
1240 static char *detoken(Token
* tlist
, bool expand_locals
)
1247 list_for_each(t
, tlist
) {
1248 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1253 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1254 size_t len
= nasm_unquote(v
, NULL
);
1255 size_t clen
= strlen(v
);
1258 error(ERR_NONFATAL
| ERR_PASS1
,
1259 "NUL character in %! string");
1265 char *p
= getenv(v
);
1267 error(ERR_NONFATAL
| ERR_PASS1
,
1268 "nonexistent environment variable `%s'", v
);
1271 t
->text
= nasm_strdup(p
);
1276 /* Expand local macros here and not during preprocessing */
1277 if (expand_locals
&&
1278 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1279 t
->text
[0] == '%' && t
->text
[1] == '$') {
1282 Context
*ctx
= get_ctx(t
->text
, &q
);
1285 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1286 p
= nasm_strcat(buffer
, q
);
1291 if (t
->type
== TOK_WHITESPACE
)
1294 len
+= strlen(t
->text
);
1297 p
= line
= nasm_malloc(len
+ 1);
1299 list_for_each(t
, tlist
) {
1300 if (t
->type
== TOK_WHITESPACE
) {
1302 } else if (t
->text
) {
1314 * A scanner, suitable for use by the expression evaluator, which
1315 * operates on a line of Tokens. Expects a pointer to a pointer to
1316 * the first token in the line to be passed in as its private_data
1319 * FIX: This really needs to be unified with stdscan.
1321 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1323 Token
**tlineptr
= private_data
;
1325 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1329 *tlineptr
= tline
? tline
->next
: NULL
;
1330 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1331 tline
->type
== TOK_COMMENT
));
1334 return tokval
->t_type
= TOKEN_EOS
;
1336 tokval
->t_charptr
= tline
->text
;
1338 if (tline
->text
[0] == '$' && !tline
->text
[1])
1339 return tokval
->t_type
= TOKEN_HERE
;
1340 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1341 return tokval
->t_type
= TOKEN_BASE
;
1343 if (tline
->type
== TOK_ID
) {
1344 p
= tokval
->t_charptr
= tline
->text
;
1346 tokval
->t_charptr
++;
1347 return tokval
->t_type
= TOKEN_ID
;
1350 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1351 if (r
>= p
+MAX_KEYWORD
)
1352 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1353 *s
++ = nasm_tolower(*r
);
1356 /* right, so we have an identifier sitting in temp storage. now,
1357 * is it actually a register or instruction name, or what? */
1358 return nasm_token_hash(ourcopy
, tokval
);
1361 if (tline
->type
== TOK_NUMBER
) {
1363 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1364 tokval
->t_charptr
= tline
->text
;
1366 return tokval
->t_type
= TOKEN_ERRNUM
;
1368 return tokval
->t_type
= TOKEN_NUM
;
1371 if (tline
->type
== TOK_FLOAT
) {
1372 return tokval
->t_type
= TOKEN_FLOAT
;
1375 if (tline
->type
== TOK_STRING
) {
1378 bq
= tline
->text
[0];
1379 tokval
->t_charptr
= tline
->text
;
1380 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1382 if (ep
[0] != bq
|| ep
[1] != '\0')
1383 return tokval
->t_type
= TOKEN_ERRSTR
;
1385 return tokval
->t_type
= TOKEN_STR
;
1388 if (tline
->type
== TOK_OTHER
) {
1389 if (!strcmp(tline
->text
, "<<"))
1390 return tokval
->t_type
= TOKEN_SHL
;
1391 if (!strcmp(tline
->text
, ">>"))
1392 return tokval
->t_type
= TOKEN_SHR
;
1393 if (!strcmp(tline
->text
, "//"))
1394 return tokval
->t_type
= TOKEN_SDIV
;
1395 if (!strcmp(tline
->text
, "%%"))
1396 return tokval
->t_type
= TOKEN_SMOD
;
1397 if (!strcmp(tline
->text
, "=="))
1398 return tokval
->t_type
= TOKEN_EQ
;
1399 if (!strcmp(tline
->text
, "<>"))
1400 return tokval
->t_type
= TOKEN_NE
;
1401 if (!strcmp(tline
->text
, "!="))
1402 return tokval
->t_type
= TOKEN_NE
;
1403 if (!strcmp(tline
->text
, "<="))
1404 return tokval
->t_type
= TOKEN_LE
;
1405 if (!strcmp(tline
->text
, ">="))
1406 return tokval
->t_type
= TOKEN_GE
;
1407 if (!strcmp(tline
->text
, "&&"))
1408 return tokval
->t_type
= TOKEN_DBL_AND
;
1409 if (!strcmp(tline
->text
, "^^"))
1410 return tokval
->t_type
= TOKEN_DBL_XOR
;
1411 if (!strcmp(tline
->text
, "||"))
1412 return tokval
->t_type
= TOKEN_DBL_OR
;
1416 * We have no other options: just return the first character of
1419 return tokval
->t_type
= tline
->text
[0];
1423 * Compare a string to the name of an existing macro; this is a
1424 * simple wrapper which calls either strcmp or nasm_stricmp
1425 * depending on the value of the `casesense' parameter.
1427 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1429 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1433 * Compare a string to the name of an existing macro; this is a
1434 * simple wrapper which calls either strcmp or nasm_stricmp
1435 * depending on the value of the `casesense' parameter.
1437 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1439 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1443 * Return the Context structure associated with a %$ token. Return
1444 * NULL, having _already_ reported an error condition, if the
1445 * context stack isn't deep enough for the supplied number of $
1448 * If "namep" is non-NULL, set it to the pointer to the macro name
1449 * tail, i.e. the part beyond %$...
1451 static Context
*get_ctx(const char *name
, const char **namep
)
1459 if (!name
|| name
[0] != '%' || name
[1] != '$')
1463 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1470 while (ctx
&& *name
== '$') {
1476 error(ERR_NONFATAL
, "`%s': context stack is only"
1477 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1488 * Check to see if a file is already in a string list
1490 static bool in_list(const StrList
*list
, const char *str
)
1493 if (!strcmp(list
->str
, str
))
1501 * Open an include file. This routine must always return a valid
1502 * file pointer if it returns - it's responsible for throwing an
1503 * ERR_FATAL and bombing out completely if not. It should also try
1504 * the include path one by one until it finds the file or reaches
1505 * the end of the path.
1507 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1512 IncPath
*ip
= ipath
;
1513 int len
= strlen(file
);
1514 size_t prefix_len
= 0;
1518 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1519 memcpy(sl
->str
, prefix
, prefix_len
);
1520 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1521 fp
= fopen(sl
->str
, "r");
1522 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1540 prefix_len
= strlen(prefix
);
1542 /* -MG given and file not found */
1543 if (dhead
&& !in_list(*dhead
, file
)) {
1544 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1546 strcpy(sl
->str
, file
);
1554 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1559 * Determine if we should warn on defining a single-line macro of
1560 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1561 * return true if _any_ single-line macro of that name is defined.
1562 * Otherwise, will return true if a single-line macro with either
1563 * `nparam' or no parameters is defined.
1565 * If a macro with precisely the right number of parameters is
1566 * defined, or nparam is -1, the address of the definition structure
1567 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1568 * is NULL, no action will be taken regarding its contents, and no
1571 * Note that this is also called with nparam zero to resolve
1574 * If you already know which context macro belongs to, you can pass
1575 * the context pointer as first parameter; if you won't but name begins
1576 * with %$ the context will be automatically computed. If all_contexts
1577 * is true, macro will be searched in outer contexts as well.
1580 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1583 struct hash_table
*smtbl
;
1587 smtbl
= &ctx
->localmac
;
1588 } else if (name
[0] == '%' && name
[1] == '$') {
1590 ctx
= get_ctx(name
, &name
);
1592 return false; /* got to return _something_ */
1593 smtbl
= &ctx
->localmac
;
1597 m
= (SMacro
*) hash_findix(smtbl
, name
);
1600 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1601 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1603 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1617 * Count and mark off the parameters in a multi-line macro call.
1618 * This is called both from within the multi-line macro expansion
1619 * code, and also to mark off the default parameters when provided
1620 * in a %macro definition line.
1622 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1624 int paramsize
, brace
;
1626 *nparam
= paramsize
= 0;
1629 /* +1: we need space for the final NULL */
1630 if (*nparam
+1 >= paramsize
) {
1631 paramsize
+= PARAM_DELTA
;
1632 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1636 if (tok_is_(t
, "{"))
1638 (*params
)[(*nparam
)++] = t
;
1639 while (tok_isnt_(t
, brace
? "}" : ","))
1641 if (t
) { /* got a comma/brace */
1645 * Now we've found the closing brace, look further
1649 if (tok_isnt_(t
, ",")) {
1651 "braces do not enclose all of macro parameter");
1652 while (tok_isnt_(t
, ","))
1656 t
= t
->next
; /* eat the comma */
1663 * Determine whether one of the various `if' conditions is true or
1666 * We must free the tline we get passed.
1668 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1670 enum pp_conditional i
= PP_COND(ct
);
1672 Token
*t
, *tt
, **tptr
, *origline
;
1673 struct tokenval tokval
;
1675 enum pp_token_type needtype
;
1682 j
= false; /* have we matched yet? */
1687 if (tline
->type
!= TOK_ID
) {
1689 "`%s' expects context identifiers", pp_directives
[ct
]);
1690 free_tlist(origline
);
1693 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1695 tline
= tline
->next
;
1700 j
= false; /* have we matched yet? */
1703 if (!tline
|| (tline
->type
!= TOK_ID
&&
1704 (tline
->type
!= TOK_PREPROC_ID
||
1705 tline
->text
[1] != '$'))) {
1707 "`%s' expects macro identifiers", pp_directives
[ct
]);
1710 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1712 tline
= tline
->next
;
1717 tline
= expand_smacro(tline
);
1718 j
= false; /* have we matched yet? */
1721 if (!tline
|| (tline
->type
!= TOK_ID
&&
1722 tline
->type
!= TOK_STRING
&&
1723 (tline
->type
!= TOK_PREPROC_ID
||
1724 tline
->text
[1] != '!'))) {
1726 "`%s' expects environment variable names",
1731 if (tline
->type
== TOK_PREPROC_ID
)
1732 p
+= 2; /* Skip leading %! */
1733 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1734 nasm_unquote_cstr(p
, ct
);
1737 tline
= tline
->next
;
1743 tline
= expand_smacro(tline
);
1745 while (tok_isnt_(tt
, ","))
1749 "`%s' expects two comma-separated arguments",
1754 j
= true; /* assume equality unless proved not */
1755 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1756 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1757 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1761 if (t
->type
== TOK_WHITESPACE
) {
1765 if (tt
->type
== TOK_WHITESPACE
) {
1769 if (tt
->type
!= t
->type
) {
1770 j
= false; /* found mismatching tokens */
1773 /* When comparing strings, need to unquote them first */
1774 if (t
->type
== TOK_STRING
) {
1775 size_t l1
= nasm_unquote(t
->text
, NULL
);
1776 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1782 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1786 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1787 j
= false; /* found mismatching tokens */
1794 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1795 j
= false; /* trailing gunk on one end or other */
1801 MMacro searching
, *mmac
;
1804 tline
= expand_id(tline
);
1805 if (!tok_type_(tline
, TOK_ID
)) {
1807 "`%s' expects a macro name", pp_directives
[ct
]);
1810 searching
.name
= nasm_strdup(tline
->text
);
1811 searching
.casesense
= true;
1812 searching
.plus
= false;
1813 searching
.nolist
= false;
1814 searching
.in_progress
= 0;
1815 searching
.max_depth
= 0;
1816 searching
.rep_nest
= NULL
;
1817 searching
.nparam_min
= 0;
1818 searching
.nparam_max
= INT_MAX
;
1819 tline
= expand_smacro(tline
->next
);
1822 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1824 "`%s' expects a parameter count or nothing",
1827 searching
.nparam_min
= searching
.nparam_max
=
1828 readnum(tline
->text
, &j
);
1831 "unable to parse parameter count `%s'",
1834 if (tline
&& tok_is_(tline
->next
, "-")) {
1835 tline
= tline
->next
->next
;
1836 if (tok_is_(tline
, "*"))
1837 searching
.nparam_max
= INT_MAX
;
1838 else if (!tok_type_(tline
, TOK_NUMBER
))
1840 "`%s' expects a parameter count after `-'",
1843 searching
.nparam_max
= readnum(tline
->text
, &j
);
1846 "unable to parse parameter count `%s'",
1848 if (searching
.nparam_min
> searching
.nparam_max
)
1850 "minimum parameter count exceeds maximum");
1853 if (tline
&& tok_is_(tline
->next
, "+")) {
1854 tline
= tline
->next
;
1855 searching
.plus
= true;
1857 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1859 if (!strcmp(mmac
->name
, searching
.name
) &&
1860 (mmac
->nparam_min
<= searching
.nparam_max
1862 && (searching
.nparam_min
<= mmac
->nparam_max
1869 if (tline
&& tline
->next
)
1870 error(ERR_WARNING
|ERR_PASS1
,
1871 "trailing garbage after %%ifmacro ignored");
1872 nasm_free(searching
.name
);
1881 needtype
= TOK_NUMBER
;
1884 needtype
= TOK_STRING
;
1888 t
= tline
= expand_smacro(tline
);
1890 while (tok_type_(t
, TOK_WHITESPACE
) ||
1891 (needtype
== TOK_NUMBER
&&
1892 tok_type_(t
, TOK_OTHER
) &&
1893 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1897 j
= tok_type_(t
, needtype
);
1901 t
= tline
= expand_smacro(tline
);
1902 while (tok_type_(t
, TOK_WHITESPACE
))
1907 t
= t
->next
; /* Skip the actual token */
1908 while (tok_type_(t
, TOK_WHITESPACE
))
1910 j
= !t
; /* Should be nothing left */
1915 t
= tline
= expand_smacro(tline
);
1916 while (tok_type_(t
, TOK_WHITESPACE
))
1919 j
= !t
; /* Should be empty */
1923 t
= tline
= expand_smacro(tline
);
1925 tokval
.t_type
= TOKEN_INVALID
;
1926 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1927 NULL
, pass
| CRITICAL
, error
, NULL
);
1931 error(ERR_WARNING
|ERR_PASS1
,
1932 "trailing garbage after expression ignored");
1933 if (!is_simple(evalresult
)) {
1935 "non-constant value given to `%s'", pp_directives
[ct
]);
1938 j
= reloc_value(evalresult
) != 0;
1943 "preprocessor directive `%s' not yet implemented",
1948 free_tlist(origline
);
1949 return j
^ PP_NEGATIVE(ct
);
1952 free_tlist(origline
);
1957 * Common code for defining an smacro
1959 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1960 int nparam
, Token
*expansion
)
1962 SMacro
*smac
, **smhead
;
1963 struct hash_table
*smtbl
;
1965 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1967 error(ERR_WARNING
|ERR_PASS1
,
1968 "single-line macro `%s' defined both with and"
1969 " without parameters", mname
);
1971 * Some instances of the old code considered this a failure,
1972 * some others didn't. What is the right thing to do here?
1974 free_tlist(expansion
);
1975 return false; /* Failure */
1978 * We're redefining, so we have to take over an
1979 * existing SMacro structure. This means freeing
1980 * what was already in it.
1982 nasm_free(smac
->name
);
1983 free_tlist(smac
->expansion
);
1986 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1987 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1988 smac
= nasm_malloc(sizeof(SMacro
));
1989 smac
->next
= *smhead
;
1992 smac
->name
= nasm_strdup(mname
);
1993 smac
->casesense
= casesense
;
1994 smac
->nparam
= nparam
;
1995 smac
->expansion
= expansion
;
1996 smac
->in_progress
= false;
1997 return true; /* Success */
2001 * Undefine an smacro
2003 static void undef_smacro(Context
*ctx
, const char *mname
)
2005 SMacro
**smhead
, *s
, **sp
;
2006 struct hash_table
*smtbl
;
2008 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2009 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2013 * We now have a macro name... go hunt for it.
2016 while ((s
= *sp
) != NULL
) {
2017 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2020 free_tlist(s
->expansion
);
2030 * Parse a mmacro specification.
2032 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
2036 tline
= tline
->next
;
2038 tline
= expand_id(tline
);
2039 if (!tok_type_(tline
, TOK_ID
)) {
2040 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2045 def
->name
= nasm_strdup(tline
->text
);
2047 def
->nolist
= false;
2048 def
->in_progress
= 0;
2049 def
->rep_nest
= NULL
;
2050 def
->nparam_min
= 0;
2051 def
->nparam_max
= 0;
2053 tline
= expand_smacro(tline
->next
);
2055 if (!tok_type_(tline
, TOK_NUMBER
)) {
2056 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2058 def
->nparam_min
= def
->nparam_max
=
2059 readnum(tline
->text
, &err
);
2062 "unable to parse parameter count `%s'", tline
->text
);
2064 if (tline
&& tok_is_(tline
->next
, "-")) {
2065 tline
= tline
->next
->next
;
2066 if (tok_is_(tline
, "*")) {
2067 def
->nparam_max
= INT_MAX
;
2068 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2070 "`%s' expects a parameter count after `-'", directive
);
2072 def
->nparam_max
= readnum(tline
->text
, &err
);
2074 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2077 if (def
->nparam_min
> def
->nparam_max
) {
2078 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2082 if (tline
&& tok_is_(tline
->next
, "+")) {
2083 tline
= tline
->next
;
2086 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2087 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2088 tline
= tline
->next
;
2093 * Handle default parameters.
2095 if (tline
&& tline
->next
) {
2096 def
->dlist
= tline
->next
;
2098 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2101 def
->defaults
= NULL
;
2103 def
->expansion
= NULL
;
2105 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2107 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2108 "too many default macro parameters");
2115 * Decode a size directive
2117 static int parse_size(const char *str
) {
2118 static const char *size_names
[] =
2119 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2120 static const int sizes
[] =
2121 { 0, 1, 4, 16, 8, 10, 2, 32 };
2123 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2127 * find and process preprocessor directive in passed line
2128 * Find out if a line contains a preprocessor directive, and deal
2131 * If a directive _is_ found, it is the responsibility of this routine
2132 * (and not the caller) to free_tlist() the line.
2134 * @param tline a pointer to the current tokeninzed line linked list
2135 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2138 static int do_directive(Token
* tline
)
2140 enum preproc_token i
;
2153 MMacro
*mmac
, **mmhead
;
2154 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2156 struct tokenval tokval
;
2158 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2166 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2167 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2168 || tline
->text
[1] == '!'))
2169 return NO_DIRECTIVE_FOUND
;
2171 i
= pp_token_hash(tline
->text
);
2174 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2175 * since they are known to be buggy at moment, we need to fix them
2176 * in future release (2.09-2.10)
2178 if (i
== PP_RMACRO
|| i
== PP_RMACRO
|| i
== PP_EXITMACRO
) {
2179 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2181 return NO_DIRECTIVE_FOUND
;
2185 * If we're in a non-emitting branch of a condition construct,
2186 * or walking to the end of an already terminated %rep block,
2187 * we should ignore all directives except for condition
2190 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2191 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2192 return NO_DIRECTIVE_FOUND
;
2196 * If we're defining a macro or reading a %rep block, we should
2197 * ignore all directives except for %macro/%imacro (which nest),
2198 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2199 * If we're in a %rep block, another %rep nests, so should be let through.
2201 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2202 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2203 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2204 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2205 return NO_DIRECTIVE_FOUND
;
2209 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2210 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2212 return NO_DIRECTIVE_FOUND
;
2213 } else if (nested_mac_count
> 0) {
2214 if (i
== PP_ENDMACRO
) {
2216 return NO_DIRECTIVE_FOUND
;
2219 if (!defining
->name
) {
2222 return NO_DIRECTIVE_FOUND
;
2223 } else if (nested_rep_count
> 0) {
2224 if (i
== PP_ENDREP
) {
2226 return NO_DIRECTIVE_FOUND
;
2234 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2236 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2239 /* Directive to tell NASM what the default stack size is. The
2240 * default is for a 16-bit stack, and this can be overriden with
2243 tline
= tline
->next
;
2244 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2245 tline
= tline
->next
;
2246 if (!tline
|| tline
->type
!= TOK_ID
) {
2247 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2248 free_tlist(origline
);
2249 return DIRECTIVE_FOUND
;
2251 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2252 /* All subsequent ARG directives are for a 32-bit stack */
2254 StackPointer
= "ebp";
2257 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2258 /* All subsequent ARG directives are for a 64-bit stack */
2260 StackPointer
= "rbp";
2263 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2264 /* All subsequent ARG directives are for a 16-bit stack,
2265 * far function call.
2268 StackPointer
= "bp";
2271 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2272 /* All subsequent ARG directives are for a 16-bit stack,
2273 * far function call. We don't support near functions.
2276 StackPointer
= "bp";
2280 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2281 free_tlist(origline
);
2282 return DIRECTIVE_FOUND
;
2284 free_tlist(origline
);
2285 return DIRECTIVE_FOUND
;
2288 /* TASM like ARG directive to define arguments to functions, in
2289 * the following form:
2291 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2295 char *arg
, directive
[256];
2296 int size
= StackSize
;
2298 /* Find the argument name */
2299 tline
= tline
->next
;
2300 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2301 tline
= tline
->next
;
2302 if (!tline
|| tline
->type
!= TOK_ID
) {
2303 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2304 free_tlist(origline
);
2305 return DIRECTIVE_FOUND
;
2309 /* Find the argument size type */
2310 tline
= tline
->next
;
2311 if (!tline
|| tline
->type
!= TOK_OTHER
2312 || tline
->text
[0] != ':') {
2314 "Syntax error processing `%%arg' directive");
2315 free_tlist(origline
);
2316 return DIRECTIVE_FOUND
;
2318 tline
= tline
->next
;
2319 if (!tline
|| tline
->type
!= TOK_ID
) {
2320 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2321 free_tlist(origline
);
2322 return DIRECTIVE_FOUND
;
2325 /* Allow macro expansion of type parameter */
2326 tt
= tokenize(tline
->text
);
2327 tt
= expand_smacro(tt
);
2328 size
= parse_size(tt
->text
);
2331 "Invalid size type for `%%arg' missing directive");
2333 free_tlist(origline
);
2334 return DIRECTIVE_FOUND
;
2338 /* Round up to even stack slots */
2339 size
= ALIGN(size
, StackSize
);
2341 /* Now define the macro for the argument */
2342 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2343 arg
, StackPointer
, offset
);
2344 do_directive(tokenize(directive
));
2347 /* Move to the next argument in the list */
2348 tline
= tline
->next
;
2349 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2350 tline
= tline
->next
;
2351 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2353 free_tlist(origline
);
2354 return DIRECTIVE_FOUND
;
2357 /* TASM like LOCAL directive to define local variables for a
2358 * function, in the following form:
2360 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2362 * The '= LocalSize' at the end is ignored by NASM, but is
2363 * required by TASM to define the local parameter size (and used
2364 * by the TASM macro package).
2366 offset
= LocalOffset
;
2368 char *local
, directive
[256];
2369 int size
= StackSize
;
2371 /* Find the argument name */
2372 tline
= tline
->next
;
2373 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2374 tline
= tline
->next
;
2375 if (!tline
|| tline
->type
!= TOK_ID
) {
2377 "`%%local' missing argument parameter");
2378 free_tlist(origline
);
2379 return DIRECTIVE_FOUND
;
2381 local
= tline
->text
;
2383 /* Find the argument size type */
2384 tline
= tline
->next
;
2385 if (!tline
|| tline
->type
!= TOK_OTHER
2386 || tline
->text
[0] != ':') {
2388 "Syntax error processing `%%local' directive");
2389 free_tlist(origline
);
2390 return DIRECTIVE_FOUND
;
2392 tline
= tline
->next
;
2393 if (!tline
|| tline
->type
!= TOK_ID
) {
2395 "`%%local' missing size type parameter");
2396 free_tlist(origline
);
2397 return DIRECTIVE_FOUND
;
2400 /* Allow macro expansion of type parameter */
2401 tt
= tokenize(tline
->text
);
2402 tt
= expand_smacro(tt
);
2403 size
= parse_size(tt
->text
);
2406 "Invalid size type for `%%local' missing directive");
2408 free_tlist(origline
);
2409 return DIRECTIVE_FOUND
;
2413 /* Round up to even stack slots */
2414 size
= ALIGN(size
, StackSize
);
2416 offset
+= size
; /* Negative offset, increment before */
2418 /* Now define the macro for the argument */
2419 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2420 local
, StackPointer
, offset
);
2421 do_directive(tokenize(directive
));
2423 /* Now define the assign to setup the enter_c macro correctly */
2424 snprintf(directive
, sizeof(directive
),
2425 "%%assign %%$localsize %%$localsize+%d", size
);
2426 do_directive(tokenize(directive
));
2428 /* Move to the next argument in the list */
2429 tline
= tline
->next
;
2430 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2431 tline
= tline
->next
;
2432 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2433 LocalOffset
= offset
;
2434 free_tlist(origline
);
2435 return DIRECTIVE_FOUND
;
2439 error(ERR_WARNING
|ERR_PASS1
,
2440 "trailing garbage after `%%clear' ignored");
2443 free_tlist(origline
);
2444 return DIRECTIVE_FOUND
;
2447 t
= tline
->next
= expand_smacro(tline
->next
);
2449 if (!t
|| (t
->type
!= TOK_STRING
&&
2450 t
->type
!= TOK_INTERNAL_STRING
)) {
2451 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2452 free_tlist(origline
);
2453 return DIRECTIVE_FOUND
; /* but we did _something_ */
2456 error(ERR_WARNING
|ERR_PASS1
,
2457 "trailing garbage after `%%depend' ignored");
2459 if (t
->type
!= TOK_INTERNAL_STRING
)
2460 nasm_unquote_cstr(p
, i
);
2461 if (dephead
&& !in_list(*dephead
, p
)) {
2462 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2466 deptail
= &sl
->next
;
2468 free_tlist(origline
);
2469 return DIRECTIVE_FOUND
;
2472 t
= tline
->next
= expand_smacro(tline
->next
);
2475 if (!t
|| (t
->type
!= TOK_STRING
&&
2476 t
->type
!= TOK_INTERNAL_STRING
)) {
2477 error(ERR_NONFATAL
, "`%%include' expects a file name");
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
; /* but we did _something_ */
2482 error(ERR_WARNING
|ERR_PASS1
,
2483 "trailing garbage after `%%include' ignored");
2485 if (t
->type
!= TOK_INTERNAL_STRING
)
2486 nasm_unquote_cstr(p
, i
);
2487 inc
= nasm_malloc(sizeof(Include
));
2490 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2492 /* -MG given but file not found */
2495 inc
->fname
= src_set_fname(nasm_strdup(p
));
2496 inc
->lineno
= src_set_linnum(0);
2498 inc
->expansion
= NULL
;
2501 list
->uplevel(LIST_INCLUDE
);
2503 free_tlist(origline
);
2504 return DIRECTIVE_FOUND
;
2508 static macros_t
*use_pkg
;
2509 const char *pkg_macro
= NULL
;
2511 tline
= tline
->next
;
2513 tline
= expand_id(tline
);
2515 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2516 tline
->type
!= TOK_INTERNAL_STRING
&&
2517 tline
->type
!= TOK_ID
)) {
2518 error(ERR_NONFATAL
, "`%%use' expects a package name");
2519 free_tlist(origline
);
2520 return DIRECTIVE_FOUND
; /* but we did _something_ */
2523 error(ERR_WARNING
|ERR_PASS1
,
2524 "trailing garbage after `%%use' ignored");
2525 if (tline
->type
== TOK_STRING
)
2526 nasm_unquote_cstr(tline
->text
, i
);
2527 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2529 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2531 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2532 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2533 /* Not already included, go ahead and include it */
2534 stdmacpos
= use_pkg
;
2536 free_tlist(origline
);
2537 return DIRECTIVE_FOUND
;
2542 tline
= tline
->next
;
2544 tline
= expand_id(tline
);
2546 if (!tok_type_(tline
, TOK_ID
)) {
2547 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2549 free_tlist(origline
);
2550 return DIRECTIVE_FOUND
; /* but we did _something_ */
2553 error(ERR_WARNING
|ERR_PASS1
,
2554 "trailing garbage after `%s' ignored",
2556 p
= nasm_strdup(tline
->text
);
2558 p
= NULL
; /* Anonymous */
2562 ctx
= nasm_malloc(sizeof(Context
));
2564 hash_init(&ctx
->localmac
, HASH_SMALL
);
2566 ctx
->number
= unique
++;
2571 error(ERR_NONFATAL
, "`%s': context stack is empty",
2573 } else if (i
== PP_POP
) {
2574 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2575 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2577 cstk
->name
? cstk
->name
: "anonymous", p
);
2582 nasm_free(cstk
->name
);
2588 free_tlist(origline
);
2589 return DIRECTIVE_FOUND
;
2591 severity
= ERR_FATAL
;
2594 severity
= ERR_NONFATAL
;
2597 severity
= ERR_WARNING
|ERR_WARN_USER
;
2602 /* Only error out if this is the final pass */
2603 if (pass
!= 2 && i
!= PP_FATAL
)
2604 return DIRECTIVE_FOUND
;
2606 tline
->next
= expand_smacro(tline
->next
);
2607 tline
= tline
->next
;
2609 t
= tline
? tline
->next
: NULL
;
2611 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2612 /* The line contains only a quoted string */
2614 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2615 error(severity
, "%s", p
);
2617 /* Not a quoted string, or more than a quoted string */
2618 p
= detoken(tline
, false);
2619 error(severity
, "%s", p
);
2622 free_tlist(origline
);
2623 return DIRECTIVE_FOUND
;
2627 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2630 j
= if_condition(tline
->next
, i
);
2631 tline
->next
= NULL
; /* it got freed */
2632 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2634 cond
= nasm_malloc(sizeof(Cond
));
2635 cond
->next
= istk
->conds
;
2639 istk
->mstk
->condcnt
++;
2640 free_tlist(origline
);
2641 return DIRECTIVE_FOUND
;
2645 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2646 switch(istk
->conds
->state
) {
2648 istk
->conds
->state
= COND_DONE
;
2655 case COND_ELSE_TRUE
:
2656 case COND_ELSE_FALSE
:
2657 error_precond(ERR_WARNING
|ERR_PASS1
,
2658 "`%%elif' after `%%else' ignored");
2659 istk
->conds
->state
= COND_NEVER
;
2664 * IMPORTANT: In the case of %if, we will already have
2665 * called expand_mmac_params(); however, if we're
2666 * processing an %elif we must have been in a
2667 * non-emitting mode, which would have inhibited
2668 * the normal invocation of expand_mmac_params().
2669 * Therefore, we have to do it explicitly here.
2671 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2672 tline
->next
= NULL
; /* it got freed */
2673 istk
->conds
->state
=
2674 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2677 free_tlist(origline
);
2678 return DIRECTIVE_FOUND
;
2682 error_precond(ERR_WARNING
|ERR_PASS1
,
2683 "trailing garbage after `%%else' ignored");
2685 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2686 switch(istk
->conds
->state
) {
2689 istk
->conds
->state
= COND_ELSE_FALSE
;
2696 istk
->conds
->state
= COND_ELSE_TRUE
;
2699 case COND_ELSE_TRUE
:
2700 case COND_ELSE_FALSE
:
2701 error_precond(ERR_WARNING
|ERR_PASS1
,
2702 "`%%else' after `%%else' ignored.");
2703 istk
->conds
->state
= COND_NEVER
;
2706 free_tlist(origline
);
2707 return DIRECTIVE_FOUND
;
2711 error_precond(ERR_WARNING
|ERR_PASS1
,
2712 "trailing garbage after `%%endif' ignored");
2714 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2716 istk
->conds
= cond
->next
;
2719 istk
->mstk
->condcnt
--;
2720 free_tlist(origline
);
2721 return DIRECTIVE_FOUND
;
2728 error(ERR_FATAL
, "`%s': already defining a macro",
2730 return DIRECTIVE_FOUND
;
2732 defining
= nasm_malloc(sizeof(MMacro
));
2733 defining
->max_depth
=
2734 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2735 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2736 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2737 nasm_free(defining
);
2739 return DIRECTIVE_FOUND
;
2742 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2744 if (!strcmp(mmac
->name
, defining
->name
) &&
2745 (mmac
->nparam_min
<= defining
->nparam_max
2747 && (defining
->nparam_min
<= mmac
->nparam_max
2749 error(ERR_WARNING
|ERR_PASS1
,
2750 "redefining multi-line macro `%s'", defining
->name
);
2751 return DIRECTIVE_FOUND
;
2755 free_tlist(origline
);
2756 return DIRECTIVE_FOUND
;
2760 if (! (defining
&& defining
->name
)) {
2761 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2762 return DIRECTIVE_FOUND
;
2764 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2765 defining
->next
= *mmhead
;
2768 free_tlist(origline
);
2769 return DIRECTIVE_FOUND
;
2773 * We must search along istk->expansion until we hit a
2774 * macro-end marker for a macro with a name. Then we
2775 * bypass all lines between exitmacro and endmacro.
2777 list_for_each(l
, istk
->expansion
)
2778 if (l
->finishes
&& l
->finishes
->name
)
2783 * Remove all conditional entries relative to this
2784 * macro invocation. (safe to do in this context)
2786 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2788 istk
->conds
= cond
->next
;
2791 istk
->expansion
= l
;
2793 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2795 free_tlist(origline
);
2796 return DIRECTIVE_FOUND
;
2804 spec
.casesense
= (i
== PP_UNMACRO
);
2805 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2806 return DIRECTIVE_FOUND
;
2808 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2809 while (mmac_p
&& *mmac_p
) {
2811 if (mmac
->casesense
== spec
.casesense
&&
2812 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2813 mmac
->nparam_min
== spec
.nparam_min
&&
2814 mmac
->nparam_max
== spec
.nparam_max
&&
2815 mmac
->plus
== spec
.plus
) {
2816 *mmac_p
= mmac
->next
;
2819 mmac_p
= &mmac
->next
;
2822 free_tlist(origline
);
2823 free_tlist(spec
.dlist
);
2824 return DIRECTIVE_FOUND
;
2828 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2829 tline
= tline
->next
;
2831 free_tlist(origline
);
2832 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2833 return DIRECTIVE_FOUND
;
2835 t
= expand_smacro(tline
->next
);
2837 free_tlist(origline
);
2840 tokval
.t_type
= TOKEN_INVALID
;
2842 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2845 return DIRECTIVE_FOUND
;
2847 error(ERR_WARNING
|ERR_PASS1
,
2848 "trailing garbage after expression ignored");
2849 if (!is_simple(evalresult
)) {
2850 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2851 return DIRECTIVE_FOUND
;
2854 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2855 mmac
= mmac
->next_active
;
2857 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2858 } else if (mmac
->nparam
== 0) {
2860 "`%%rotate' invoked within macro without parameters");
2862 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2864 rotate
%= (int)mmac
->nparam
;
2866 rotate
+= mmac
->nparam
;
2868 mmac
->rotate
= rotate
;
2870 return DIRECTIVE_FOUND
;
2875 tline
= tline
->next
;
2876 } while (tok_type_(tline
, TOK_WHITESPACE
));
2878 if (tok_type_(tline
, TOK_ID
) &&
2879 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2882 tline
= tline
->next
;
2883 } while (tok_type_(tline
, TOK_WHITESPACE
));
2887 t
= expand_smacro(tline
);
2889 tokval
.t_type
= TOKEN_INVALID
;
2891 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2893 free_tlist(origline
);
2894 return DIRECTIVE_FOUND
;
2897 error(ERR_WARNING
|ERR_PASS1
,
2898 "trailing garbage after expression ignored");
2899 if (!is_simple(evalresult
)) {
2900 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2901 return DIRECTIVE_FOUND
;
2903 count
= reloc_value(evalresult
);
2904 if (count
>= REP_LIMIT
) {
2905 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
2910 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2913 free_tlist(origline
);
2915 tmp_defining
= defining
;
2916 defining
= nasm_malloc(sizeof(MMacro
));
2917 defining
->prev
= NULL
;
2918 defining
->name
= NULL
; /* flags this macro as a %rep block */
2919 defining
->casesense
= false;
2920 defining
->plus
= false;
2921 defining
->nolist
= nolist
;
2922 defining
->in_progress
= count
;
2923 defining
->max_depth
= 0;
2924 defining
->nparam_min
= defining
->nparam_max
= 0;
2925 defining
->defaults
= NULL
;
2926 defining
->dlist
= NULL
;
2927 defining
->expansion
= NULL
;
2928 defining
->next_active
= istk
->mstk
;
2929 defining
->rep_nest
= tmp_defining
;
2930 return DIRECTIVE_FOUND
;
2933 if (!defining
|| defining
->name
) {
2934 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2935 return DIRECTIVE_FOUND
;
2939 * Now we have a "macro" defined - although it has no name
2940 * and we won't be entering it in the hash tables - we must
2941 * push a macro-end marker for it on to istk->expansion.
2942 * After that, it will take care of propagating itself (a
2943 * macro-end marker line for a macro which is really a %rep
2944 * block will cause the macro to be re-expanded, complete
2945 * with another macro-end marker to ensure the process
2946 * continues) until the whole expansion is forcibly removed
2947 * from istk->expansion by a %exitrep.
2949 l
= nasm_malloc(sizeof(Line
));
2950 l
->next
= istk
->expansion
;
2951 l
->finishes
= defining
;
2953 istk
->expansion
= l
;
2955 istk
->mstk
= defining
;
2957 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2958 tmp_defining
= defining
;
2959 defining
= defining
->rep_nest
;
2960 free_tlist(origline
);
2961 return DIRECTIVE_FOUND
;
2965 * We must search along istk->expansion until we hit a
2966 * macro-end marker for a macro with no name. Then we set
2967 * its `in_progress' flag to 0.
2969 list_for_each(l
, istk
->expansion
)
2970 if (l
->finishes
&& !l
->finishes
->name
)
2974 l
->finishes
->in_progress
= 1;
2976 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2977 free_tlist(origline
);
2978 return DIRECTIVE_FOUND
;
2984 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2986 tline
= tline
->next
;
2988 tline
= expand_id(tline
);
2989 if (!tline
|| (tline
->type
!= TOK_ID
&&
2990 (tline
->type
!= TOK_PREPROC_ID
||
2991 tline
->text
[1] != '$'))) {
2992 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2994 free_tlist(origline
);
2995 return DIRECTIVE_FOUND
;
2998 ctx
= get_ctx(tline
->text
, &mname
);
3000 param_start
= tline
= tline
->next
;
3003 /* Expand the macro definition now for %xdefine and %ixdefine */
3004 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3005 tline
= expand_smacro(tline
);
3007 if (tok_is_(tline
, "(")) {
3009 * This macro has parameters.
3012 tline
= tline
->next
;
3016 error(ERR_NONFATAL
, "parameter identifier expected");
3017 free_tlist(origline
);
3018 return DIRECTIVE_FOUND
;
3020 if (tline
->type
!= TOK_ID
) {
3022 "`%s': parameter identifier expected",
3024 free_tlist(origline
);
3025 return DIRECTIVE_FOUND
;
3027 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3028 tline
= tline
->next
;
3030 if (tok_is_(tline
, ",")) {
3031 tline
= tline
->next
;
3033 if (!tok_is_(tline
, ")")) {
3035 "`)' expected to terminate macro template");
3036 free_tlist(origline
);
3037 return DIRECTIVE_FOUND
;
3043 tline
= tline
->next
;
3045 if (tok_type_(tline
, TOK_WHITESPACE
))
3046 last
= tline
, tline
= tline
->next
;
3051 if (t
->type
== TOK_ID
) {
3052 list_for_each(tt
, param_start
)
3053 if (tt
->type
>= TOK_SMAC_PARAM
&&
3054 !strcmp(tt
->text
, t
->text
))
3058 t
->next
= macro_start
;
3063 * Good. We now have a macro name, a parameter count, and a
3064 * token list (in reverse order) for an expansion. We ought
3065 * to be OK just to create an SMacro, store it, and let
3066 * free_tlist have the rest of the line (which we have
3067 * carefully re-terminated after chopping off the expansion
3070 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3071 free_tlist(origline
);
3072 return DIRECTIVE_FOUND
;
3075 tline
= tline
->next
;
3077 tline
= expand_id(tline
);
3078 if (!tline
|| (tline
->type
!= TOK_ID
&&
3079 (tline
->type
!= TOK_PREPROC_ID
||
3080 tline
->text
[1] != '$'))) {
3081 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3082 free_tlist(origline
);
3083 return DIRECTIVE_FOUND
;
3086 error(ERR_WARNING
|ERR_PASS1
,
3087 "trailing garbage after macro name ignored");
3090 /* Find the context that symbol belongs to */
3091 ctx
= get_ctx(tline
->text
, &mname
);
3092 undef_smacro(ctx
, mname
);
3093 free_tlist(origline
);
3094 return DIRECTIVE_FOUND
;
3098 casesense
= (i
== PP_DEFSTR
);
3100 tline
= tline
->next
;
3102 tline
= expand_id(tline
);
3103 if (!tline
|| (tline
->type
!= TOK_ID
&&
3104 (tline
->type
!= TOK_PREPROC_ID
||
3105 tline
->text
[1] != '$'))) {
3106 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3108 free_tlist(origline
);
3109 return DIRECTIVE_FOUND
;
3112 ctx
= get_ctx(tline
->text
, &mname
);
3114 tline
= expand_smacro(tline
->next
);
3117 while (tok_type_(tline
, TOK_WHITESPACE
))
3118 tline
= delete_Token(tline
);
3120 p
= detoken(tline
, false);
3121 macro_start
= nasm_malloc(sizeof(*macro_start
));
3122 macro_start
->next
= NULL
;
3123 macro_start
->text
= nasm_quote(p
, strlen(p
));
3124 macro_start
->type
= TOK_STRING
;
3125 macro_start
->a
.mac
= NULL
;
3129 * We now have a macro name, an implicit parameter count of
3130 * zero, and a string token to use as an expansion. Create
3131 * and store an SMacro.
3133 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3134 free_tlist(origline
);
3135 return DIRECTIVE_FOUND
;
3139 casesense
= (i
== PP_DEFTOK
);
3141 tline
= tline
->next
;
3143 tline
= expand_id(tline
);
3144 if (!tline
|| (tline
->type
!= TOK_ID
&&
3145 (tline
->type
!= TOK_PREPROC_ID
||
3146 tline
->text
[1] != '$'))) {
3148 "`%s' expects a macro identifier as first parameter",
3150 free_tlist(origline
);
3151 return DIRECTIVE_FOUND
;
3153 ctx
= get_ctx(tline
->text
, &mname
);
3155 tline
= expand_smacro(tline
->next
);
3159 while (tok_type_(t
, TOK_WHITESPACE
))
3161 /* t should now point to the string */
3162 if (!tok_type_(t
, TOK_STRING
)) {
3164 "`%s` requires string as second parameter",
3167 free_tlist(origline
);
3168 return DIRECTIVE_FOUND
;
3172 * Convert the string to a token stream. Note that smacros
3173 * are stored with the token stream reversed, so we have to
3174 * reverse the output of tokenize().
3176 nasm_unquote_cstr(t
->text
, i
);
3177 macro_start
= reverse_tokens(tokenize(t
->text
));
3180 * We now have a macro name, an implicit parameter count of
3181 * zero, and a numeric token to use as an expansion. Create
3182 * and store an SMacro.
3184 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3186 free_tlist(origline
);
3187 return DIRECTIVE_FOUND
;
3192 StrList
*xsl
= NULL
;
3193 StrList
**xst
= &xsl
;
3197 tline
= tline
->next
;
3199 tline
= expand_id(tline
);
3200 if (!tline
|| (tline
->type
!= TOK_ID
&&
3201 (tline
->type
!= TOK_PREPROC_ID
||
3202 tline
->text
[1] != '$'))) {
3204 "`%%pathsearch' expects a macro identifier as first parameter");
3205 free_tlist(origline
);
3206 return DIRECTIVE_FOUND
;
3208 ctx
= get_ctx(tline
->text
, &mname
);
3210 tline
= expand_smacro(tline
->next
);
3214 while (tok_type_(t
, TOK_WHITESPACE
))
3217 if (!t
|| (t
->type
!= TOK_STRING
&&
3218 t
->type
!= TOK_INTERNAL_STRING
)) {
3219 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3221 free_tlist(origline
);
3222 return DIRECTIVE_FOUND
; /* but we did _something_ */
3225 error(ERR_WARNING
|ERR_PASS1
,
3226 "trailing garbage after `%%pathsearch' ignored");
3228 if (t
->type
!= TOK_INTERNAL_STRING
)
3229 nasm_unquote(p
, NULL
);
3231 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3234 fclose(fp
); /* Don't actually care about the file */
3236 macro_start
= nasm_malloc(sizeof(*macro_start
));
3237 macro_start
->next
= NULL
;
3238 macro_start
->text
= nasm_quote(p
, strlen(p
));
3239 macro_start
->type
= TOK_STRING
;
3240 macro_start
->a
.mac
= NULL
;
3245 * We now have a macro name, an implicit parameter count of
3246 * zero, and a string token to use as an expansion. Create
3247 * and store an SMacro.
3249 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3251 free_tlist(origline
);
3252 return DIRECTIVE_FOUND
;
3258 tline
= tline
->next
;
3260 tline
= expand_id(tline
);
3261 if (!tline
|| (tline
->type
!= TOK_ID
&&
3262 (tline
->type
!= TOK_PREPROC_ID
||
3263 tline
->text
[1] != '$'))) {
3265 "`%%strlen' expects a macro identifier as first parameter");
3266 free_tlist(origline
);
3267 return DIRECTIVE_FOUND
;
3269 ctx
= get_ctx(tline
->text
, &mname
);
3271 tline
= expand_smacro(tline
->next
);
3275 while (tok_type_(t
, TOK_WHITESPACE
))
3277 /* t should now point to the string */
3278 if (!tok_type_(t
, TOK_STRING
)) {
3280 "`%%strlen` requires string as second parameter");
3282 free_tlist(origline
);
3283 return DIRECTIVE_FOUND
;
3286 macro_start
= nasm_malloc(sizeof(*macro_start
));
3287 macro_start
->next
= NULL
;
3288 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3289 macro_start
->a
.mac
= NULL
;
3292 * We now have a macro name, an implicit parameter count of
3293 * zero, and a numeric token to use as an expansion. Create
3294 * and store an SMacro.
3296 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3298 free_tlist(origline
);
3299 return DIRECTIVE_FOUND
;
3304 tline
= tline
->next
;
3306 tline
= expand_id(tline
);
3307 if (!tline
|| (tline
->type
!= TOK_ID
&&
3308 (tline
->type
!= TOK_PREPROC_ID
||
3309 tline
->text
[1] != '$'))) {
3311 "`%%strcat' expects a macro identifier as first parameter");
3312 free_tlist(origline
);
3313 return DIRECTIVE_FOUND
;
3315 ctx
= get_ctx(tline
->text
, &mname
);
3317 tline
= expand_smacro(tline
->next
);
3321 list_for_each(t
, tline
) {
3323 case TOK_WHITESPACE
:
3326 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3329 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3331 /* else fall through */
3334 "non-string passed to `%%strcat' (%d)", t
->type
);
3336 free_tlist(origline
);
3337 return DIRECTIVE_FOUND
;
3341 p
= pp
= nasm_malloc(len
);
3342 list_for_each(t
, tline
) {
3343 if (t
->type
== TOK_STRING
) {
3344 memcpy(p
, t
->text
, t
->a
.len
);
3350 * We now have a macro name, an implicit parameter count of
3351 * zero, and a numeric token to use as an expansion. Create
3352 * and store an SMacro.
3354 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3355 macro_start
->text
= nasm_quote(pp
, len
);
3357 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3359 free_tlist(origline
);
3360 return DIRECTIVE_FOUND
;
3364 int64_t start
, count
;
3369 tline
= tline
->next
;
3371 tline
= expand_id(tline
);
3372 if (!tline
|| (tline
->type
!= TOK_ID
&&
3373 (tline
->type
!= TOK_PREPROC_ID
||
3374 tline
->text
[1] != '$'))) {
3376 "`%%substr' expects a macro identifier as first parameter");
3377 free_tlist(origline
);
3378 return DIRECTIVE_FOUND
;
3380 ctx
= get_ctx(tline
->text
, &mname
);
3382 tline
= expand_smacro(tline
->next
);
3385 if (tline
) /* skip expanded id */
3387 while (tok_type_(t
, TOK_WHITESPACE
))
3390 /* t should now point to the string */
3391 if (!tok_type_(t
, TOK_STRING
)) {
3393 "`%%substr` requires string as second parameter");
3395 free_tlist(origline
);
3396 return DIRECTIVE_FOUND
;
3401 tokval
.t_type
= TOKEN_INVALID
;
3402 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3406 free_tlist(origline
);
3407 return DIRECTIVE_FOUND
;
3408 } else if (!is_simple(evalresult
)) {
3409 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3411 free_tlist(origline
);
3412 return DIRECTIVE_FOUND
;
3414 start
= evalresult
->value
- 1;
3416 while (tok_type_(tt
, TOK_WHITESPACE
))
3419 count
= 1; /* Backwards compatibility: one character */
3421 tokval
.t_type
= TOKEN_INVALID
;
3422 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3426 free_tlist(origline
);
3427 return DIRECTIVE_FOUND
;
3428 } else if (!is_simple(evalresult
)) {
3429 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3431 free_tlist(origline
);
3432 return DIRECTIVE_FOUND
;
3434 count
= evalresult
->value
;
3437 len
= nasm_unquote(t
->text
, NULL
);
3439 /* make start and count being in range */
3443 count
= len
+ count
+ 1 - start
;
3444 if (start
+ count
> (int64_t)len
)
3445 count
= len
- start
;
3446 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3447 start
= -1, count
= 0; /* empty string */
3449 macro_start
= nasm_malloc(sizeof(*macro_start
));
3450 macro_start
->next
= NULL
;
3451 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3452 macro_start
->type
= TOK_STRING
;
3453 macro_start
->a
.mac
= NULL
;
3456 * We now have a macro name, an implicit parameter count of
3457 * zero, and a numeric token to use as an expansion. Create
3458 * and store an SMacro.
3460 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3462 free_tlist(origline
);
3463 return DIRECTIVE_FOUND
;
3468 casesense
= (i
== PP_ASSIGN
);
3470 tline
= tline
->next
;
3472 tline
= expand_id(tline
);
3473 if (!tline
|| (tline
->type
!= TOK_ID
&&
3474 (tline
->type
!= TOK_PREPROC_ID
||
3475 tline
->text
[1] != '$'))) {
3477 "`%%%sassign' expects a macro identifier",
3478 (i
== PP_IASSIGN
? "i" : ""));
3479 free_tlist(origline
);
3480 return DIRECTIVE_FOUND
;
3482 ctx
= get_ctx(tline
->text
, &mname
);
3484 tline
= expand_smacro(tline
->next
);
3489 tokval
.t_type
= TOKEN_INVALID
;
3491 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3494 free_tlist(origline
);
3495 return DIRECTIVE_FOUND
;
3499 error(ERR_WARNING
|ERR_PASS1
,
3500 "trailing garbage after expression ignored");
3502 if (!is_simple(evalresult
)) {
3504 "non-constant value given to `%%%sassign'",
3505 (i
== PP_IASSIGN
? "i" : ""));
3506 free_tlist(origline
);
3507 return DIRECTIVE_FOUND
;
3510 macro_start
= nasm_malloc(sizeof(*macro_start
));
3511 macro_start
->next
= NULL
;
3512 make_tok_num(macro_start
, reloc_value(evalresult
));
3513 macro_start
->a
.mac
= NULL
;
3516 * We now have a macro name, an implicit parameter count of
3517 * zero, and a numeric token to use as an expansion. Create
3518 * and store an SMacro.
3520 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3521 free_tlist(origline
);
3522 return DIRECTIVE_FOUND
;
3526 * Syntax is `%line nnn[+mmm] [filename]'
3528 tline
= tline
->next
;
3530 if (!tok_type_(tline
, TOK_NUMBER
)) {
3531 error(ERR_NONFATAL
, "`%%line' expects line number");
3532 free_tlist(origline
);
3533 return DIRECTIVE_FOUND
;
3535 k
= readnum(tline
->text
, &err
);
3537 tline
= tline
->next
;
3538 if (tok_is_(tline
, "+")) {
3539 tline
= tline
->next
;
3540 if (!tok_type_(tline
, TOK_NUMBER
)) {
3541 error(ERR_NONFATAL
, "`%%line' expects line increment");
3542 free_tlist(origline
);
3543 return DIRECTIVE_FOUND
;
3545 m
= readnum(tline
->text
, &err
);
3546 tline
= tline
->next
;
3552 nasm_free(src_set_fname(detoken(tline
, false)));
3554 free_tlist(origline
);
3555 return DIRECTIVE_FOUND
;
3559 "preprocessor directive `%s' not yet implemented",
3561 return DIRECTIVE_FOUND
;
3566 * Ensure that a macro parameter contains a condition code and
3567 * nothing else. Return the condition code index if so, or -1
3570 static int find_cc(Token
* t
)
3575 return -1; /* Probably a %+ without a space */
3578 if (t
->type
!= TOK_ID
)
3582 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3585 return bsii(t
->text
, (const char **)conditions
, ARRAY_SIZE(conditions
));
3588 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3589 int mnum
, bool handle_paste_tokens
)
3591 Token
**tail
, *t
, *tt
;
3593 bool did_paste
= false;
3597 /* Now handle token pasting... */
3600 while ((t
= *tail
) && (tt
= t
->next
)) {
3602 case TOK_WHITESPACE
:
3603 if (tt
->type
== TOK_WHITESPACE
) {
3604 /* Zap adjacent whitespace tokens */
3605 t
->next
= delete_Token(tt
);
3607 /* Do not advance paste_head here */
3611 case TOK_PASTE
: /* %+ */
3612 if (handle_paste_tokens
) {
3613 /* Zap %+ and whitespace tokens to the right */
3614 while (t
&& (t
->type
== TOK_WHITESPACE
||
3615 t
->type
== TOK_PASTE
))
3616 t
= *tail
= delete_Token(t
);
3617 if (!t
) { /* Dangling %+ term */
3619 (*paste_head
)->next
= NULL
;
3627 while (tok_type_(tt
, TOK_WHITESPACE
))
3628 tt
= t
->next
= delete_Token(tt
);
3630 tmp
= nasm_strcat(t
->text
, tt
->text
);
3632 tt
= delete_Token(tt
);
3633 t
= *tail
= tokenize(tmp
);
3639 t
->next
= tt
; /* Attach the remaining token chain */
3646 /* else fall through */
3649 * Concatenation of tokens might look nontrivial
3650 * but in real it's pretty simple -- the caller
3651 * prepares the masks of token types to be concatenated
3652 * and we simply find matched sequences and slip
3655 for (i
= 0; i
< mnum
; i
++) {
3656 if (PP_CONCAT_MASK(t
->type
) & m
[i
].mask_head
) {
3660 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & m
[i
].mask_tail
)) {
3661 len
+= strlen(tt
->text
);
3666 * Now tt points to the first token after
3667 * the potential paste area...
3669 if (tt
!= t
->next
) {
3670 /* We have at least two tokens... */
3671 len
+= strlen(t
->text
);
3672 p
= tmp
= nasm_malloc(len
+1);
3675 p
= strchr(p
, '\0');
3676 t
= delete_Token(t
);
3678 t
= *tail
= tokenize(tmp
);
3684 t
->next
= tt
; /* Attach the remaining token chain */
3692 if (i
>= mnum
) { /* no match */
3694 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3704 * expands to a list of tokens from %{x:y}
3706 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3708 Token
*t
= tline
, **tt
, *tm
, *head
;
3712 pos
= strchr(tline
->text
, ':');
3715 lst
= atoi(pos
+ 1);
3716 fst
= atoi(tline
->text
+ 1);
3719 * only macros params are accounted so
3720 * if someone passes %0 -- we reject such
3723 if (lst
== 0 || fst
== 0)
3726 /* the values should be sane */
3727 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3728 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3731 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3732 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3734 /* counted from zero */
3738 * it will be at least one token
3740 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3741 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3742 head
= t
, tt
= &t
->next
;
3744 for (i
= fst
+ 1; i
<= lst
; i
++) {
3745 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3746 *tt
= t
, tt
= &t
->next
;
3747 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3748 tm
= mac
->params
[j
];
3749 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3750 *tt
= t
, tt
= &t
->next
;
3753 for (i
= fst
- 1; i
>= lst
; i
--) {
3754 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3755 *tt
= t
, tt
= &t
->next
;
3756 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3757 tm
= mac
->params
[j
];
3758 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3759 *tt
= t
, tt
= &t
->next
;
3767 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3773 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3774 * %-n) and MMacro-local identifiers (%%foo) as well as
3775 * macro indirection (%[...]) and range (%{..:..}).
3777 static Token
*expand_mmac_params(Token
* tline
)
3779 Token
*t
, *tt
, **tail
, *thead
;
3780 bool changed
= false;
3787 if (tline
->type
== TOK_PREPROC_ID
&&
3788 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3789 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3790 tline
->text
[1] == '%')) {
3792 int type
= 0, cc
; /* type = 0 to placate optimisers */
3799 tline
= tline
->next
;
3802 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3803 mac
= mac
->next_active
;
3805 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3807 pos
= strchr(t
->text
, ':');
3809 switch (t
->text
[1]) {
3811 * We have to make a substitution of one of the
3812 * forms %1, %-1, %+1, %%foo, %0.
3816 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3817 text
= nasm_strdup(tmpbuf
);
3821 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3823 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3826 n
= atoi(t
->text
+ 2) - 1;
3827 if (n
>= mac
->nparam
)
3830 if (mac
->nparam
> 1)
3831 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3832 tt
= mac
->params
[n
];
3837 "macro parameter %d is not a condition code",
3842 if (inverse_ccs
[cc
] == -1) {
3844 "condition code `%s' is not invertible",
3848 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3852 n
= atoi(t
->text
+ 2) - 1;
3853 if (n
>= mac
->nparam
)
3856 if (mac
->nparam
> 1)
3857 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3858 tt
= mac
->params
[n
];
3863 "macro parameter %d is not a condition code",
3868 text
= nasm_strdup(conditions
[cc
]);
3872 n
= atoi(t
->text
+ 1) - 1;
3873 if (n
>= mac
->nparam
)
3876 if (mac
->nparam
> 1)
3877 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3878 tt
= mac
->params
[n
];
3881 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3882 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3883 tail
= &(*tail
)->next
;
3887 text
= NULL
; /* we've done it here */
3892 * seems we have a parameters range here
3894 Token
*head
, **last
;
3895 head
= expand_mmac_params_range(mac
, t
, &last
);
3916 } else if (tline
->type
== TOK_INDIRECT
) {
3918 tline
= tline
->next
;
3919 tt
= tokenize(t
->text
);
3920 tt
= expand_mmac_params(tt
);
3921 tt
= expand_smacro(tt
);
3924 tt
->a
.mac
= NULL
; /* Necessary? */
3932 tline
= tline
->next
;
3940 const struct tokseq_match t
[] = {
3942 PP_CONCAT_MASK(TOK_ID
) |
3943 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
3944 PP_CONCAT_MASK(TOK_ID
) |
3945 PP_CONCAT_MASK(TOK_NUMBER
) |
3946 PP_CONCAT_MASK(TOK_FLOAT
) |
3947 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
3950 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
3951 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
3954 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
3961 * Expand all single-line macro calls made in the given line.
3962 * Return the expanded version of the line. The original is deemed
3963 * to be destroyed in the process. (In reality we'll just move
3964 * Tokens from input to output a lot of the time, rather than
3965 * actually bothering to destroy and replicate.)
3968 static Token
*expand_smacro(Token
* tline
)
3970 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3971 SMacro
*head
= NULL
, *m
;
3974 unsigned int nparam
, sparam
;
3976 Token
*org_tline
= tline
;
3979 int deadman
= DEADMAN_LIMIT
;
3983 * Trick: we should avoid changing the start token pointer since it can
3984 * be contained in "next" field of other token. Because of this
3985 * we allocate a copy of first token and work with it; at the end of
3986 * routine we copy it back
3989 tline
= new_Token(org_tline
->next
, org_tline
->type
,
3990 org_tline
->text
, 0);
3991 tline
->a
.mac
= org_tline
->a
.mac
;
3992 nasm_free(org_tline
->text
);
3993 org_tline
->text
= NULL
;
3996 expanded
= true; /* Always expand %+ at least once */
4002 while (tline
) { /* main token loop */
4004 error(ERR_NONFATAL
, "interminable macro recursion");
4008 if ((mname
= tline
->text
)) {
4009 /* if this token is a local macro, look in local context */
4010 if (tline
->type
== TOK_ID
) {
4011 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4012 } else if (tline
->type
== TOK_PREPROC_ID
) {
4013 ctx
= get_ctx(mname
, &mname
);
4014 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4019 * We've hit an identifier. As in is_mmacro below, we first
4020 * check whether the identifier is a single-line macro at
4021 * all, then think about checking for parameters if
4024 list_for_each(m
, head
)
4025 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4031 if (m
->nparam
== 0) {
4033 * Simple case: the macro is parameterless. Discard the
4034 * one token that the macro call took, and push the
4035 * expansion back on the to-do stack.
4037 if (!m
->expansion
) {
4038 if (!strcmp("__FILE__", m
->name
)) {
4041 src_get(&num
, &file
);
4042 tline
->text
= nasm_quote(file
, strlen(file
));
4043 tline
->type
= TOK_STRING
;
4047 if (!strcmp("__LINE__", m
->name
)) {
4048 nasm_free(tline
->text
);
4049 make_tok_num(tline
, src_get_linnum());
4052 if (!strcmp("__BITS__", m
->name
)) {
4053 nasm_free(tline
->text
);
4054 make_tok_num(tline
, globalbits
);
4057 tline
= delete_Token(tline
);
4062 * Complicated case: at least one macro with this name
4063 * exists and takes parameters. We must find the
4064 * parameters in the call, count them, find the SMacro
4065 * that corresponds to that form of the macro call, and
4066 * substitute for the parameters when we expand. What a
4069 /*tline = tline->next;
4070 skip_white_(tline); */
4073 while (tok_type_(t
, TOK_SMAC_END
)) {
4074 t
->a
.mac
->in_progress
= false;
4076 t
= tline
->next
= delete_Token(t
);
4079 } while (tok_type_(tline
, TOK_WHITESPACE
));
4080 if (!tok_is_(tline
, "(")) {
4082 * This macro wasn't called with parameters: ignore
4083 * the call. (Behaviour borrowed from gnu cpp.)
4092 sparam
= PARAM_DELTA
;
4093 params
= nasm_malloc(sparam
* sizeof(Token
*));
4094 params
[0] = tline
->next
;
4095 paramsize
= nasm_malloc(sparam
* sizeof(int));
4097 while (true) { /* parameter loop */
4099 * For some unusual expansions
4100 * which concatenates function call
4103 while (tok_type_(t
, TOK_SMAC_END
)) {
4104 t
->a
.mac
->in_progress
= false;
4106 t
= tline
->next
= delete_Token(t
);
4112 "macro call expects terminating `)'");
4115 if (tline
->type
== TOK_WHITESPACE
4117 if (paramsize
[nparam
])
4120 params
[nparam
] = tline
->next
;
4121 continue; /* parameter loop */
4123 if (tline
->type
== TOK_OTHER
4124 && tline
->text
[1] == 0) {
4125 char ch
= tline
->text
[0];
4126 if (ch
== ',' && !paren
&& brackets
<= 0) {
4127 if (++nparam
>= sparam
) {
4128 sparam
+= PARAM_DELTA
;
4129 params
= nasm_realloc(params
,
4130 sparam
* sizeof(Token
*));
4131 paramsize
= nasm_realloc(paramsize
,
4132 sparam
* sizeof(int));
4134 params
[nparam
] = tline
->next
;
4135 paramsize
[nparam
] = 0;
4137 continue; /* parameter loop */
4140 (brackets
> 0 || (brackets
== 0 &&
4141 !paramsize
[nparam
])))
4143 if (!(brackets
++)) {
4144 params
[nparam
] = tline
->next
;
4145 continue; /* parameter loop */
4148 if (ch
== '}' && brackets
> 0)
4149 if (--brackets
== 0) {
4151 continue; /* parameter loop */
4153 if (ch
== '(' && !brackets
)
4155 if (ch
== ')' && brackets
<= 0)
4161 error(ERR_NONFATAL
, "braces do not "
4162 "enclose all of macro parameter");
4164 paramsize
[nparam
] += white
+ 1;
4166 } /* parameter loop */
4168 while (m
&& (m
->nparam
!= nparam
||
4169 mstrcmp(m
->name
, mname
,
4173 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4174 "macro `%s' exists, "
4175 "but not taking %d parameters",
4176 mstart
->text
, nparam
);
4179 if (m
&& m
->in_progress
)
4181 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4183 * Design question: should we handle !tline, which
4184 * indicates missing ')' here, or expand those
4185 * macros anyway, which requires the (t) test a few
4189 nasm_free(paramsize
);
4193 * Expand the macro: we are placed on the last token of the
4194 * call, so that we can easily split the call from the
4195 * following tokens. We also start by pushing an SMAC_END
4196 * token for the cycle removal.
4203 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4205 m
->in_progress
= true;
4207 list_for_each(t
, m
->expansion
) {
4208 if (t
->type
>= TOK_SMAC_PARAM
) {
4209 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4213 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4214 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4216 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4222 } else if (t
->type
== TOK_PREPROC_Q
) {
4223 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4225 } else if (t
->type
== TOK_PREPROC_QQ
) {
4226 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4229 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4235 * Having done that, get rid of the macro call, and clean
4236 * up the parameters.
4239 nasm_free(paramsize
);
4242 continue; /* main token loop */
4247 if (tline
->type
== TOK_SMAC_END
) {
4248 tline
->a
.mac
->in_progress
= false;
4249 tline
= delete_Token(tline
);
4252 tline
= tline
->next
;
4260 * Now scan the entire line and look for successive TOK_IDs that resulted
4261 * after expansion (they can't be produced by tokenize()). The successive
4262 * TOK_IDs should be concatenated.
4263 * Also we look for %+ tokens and concatenate the tokens before and after
4264 * them (without white spaces in between).
4267 const struct tokseq_match t
[] = {
4269 PP_CONCAT_MASK(TOK_ID
) |
4270 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4271 PP_CONCAT_MASK(TOK_ID
) |
4272 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4273 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4276 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4278 * If we concatenated something, *and* we had previously expanded
4279 * an actual macro, scan the lines again for macros...
4290 *org_tline
= *thead
;
4291 /* since we just gave text to org_line, don't free it */
4293 delete_Token(thead
);
4295 /* the expression expanded to empty line;
4296 we can't return NULL for some reasons
4297 we just set the line to a single WHITESPACE token. */
4298 memset(org_tline
, 0, sizeof(*org_tline
));
4299 org_tline
->text
= NULL
;
4300 org_tline
->type
= TOK_WHITESPACE
;
4309 * Similar to expand_smacro but used exclusively with macro identifiers
4310 * right before they are fetched in. The reason is that there can be
4311 * identifiers consisting of several subparts. We consider that if there
4312 * are more than one element forming the name, user wants a expansion,
4313 * otherwise it will be left as-is. Example:
4317 * the identifier %$abc will be left as-is so that the handler for %define
4318 * will suck it and define the corresponding value. Other case:
4320 * %define _%$abc cde
4322 * In this case user wants name to be expanded *before* %define starts
4323 * working, so we'll expand %$abc into something (if it has a value;
4324 * otherwise it will be left as-is) then concatenate all successive
4327 static Token
*expand_id(Token
* tline
)
4329 Token
*cur
, *oldnext
= NULL
;
4331 if (!tline
|| !tline
->next
)
4336 (cur
->next
->type
== TOK_ID
||
4337 cur
->next
->type
== TOK_PREPROC_ID
4338 || cur
->next
->type
== TOK_NUMBER
))
4341 /* If identifier consists of just one token, don't expand */
4346 oldnext
= cur
->next
; /* Detach the tail past identifier */
4347 cur
->next
= NULL
; /* so that expand_smacro stops here */
4350 tline
= expand_smacro(tline
);
4353 /* expand_smacro possibly changhed tline; re-scan for EOL */
4355 while (cur
&& cur
->next
)
4358 cur
->next
= oldnext
;
4365 * Determine whether the given line constitutes a multi-line macro
4366 * call, and return the MMacro structure called if so. Doesn't have
4367 * to check for an initial label - that's taken care of in
4368 * expand_mmacro - but must check numbers of parameters. Guaranteed
4369 * to be called with tline->type == TOK_ID, so the putative macro
4370 * name is easy to find.
4372 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4378 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4381 * Efficiency: first we see if any macro exists with the given
4382 * name. If not, we can return NULL immediately. _Then_ we
4383 * count the parameters, and then we look further along the
4384 * list if necessary to find the proper MMacro.
4386 list_for_each(m
, head
)
4387 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4393 * OK, we have a potential macro. Count and demarcate the
4396 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4399 * So we know how many parameters we've got. Find the MMacro
4400 * structure that handles this number.
4403 if (m
->nparam_min
<= nparam
4404 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4406 * This one is right. Just check if cycle removal
4407 * prohibits us using it before we actually celebrate...
4409 if (m
->in_progress
> m
->max_depth
) {
4410 if (m
->max_depth
> 0) {
4412 "reached maximum recursion depth of %i",
4419 * It's right, and we can use it. Add its default
4420 * parameters to the end of our list if necessary.
4422 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4424 nasm_realloc(params
,
4425 ((m
->nparam_min
+ m
->ndefs
+
4426 1) * sizeof(*params
)));
4427 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4428 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4433 * If we've gone over the maximum parameter count (and
4434 * we're in Plus mode), ignore parameters beyond
4437 if (m
->plus
&& nparam
> m
->nparam_max
)
4438 nparam
= m
->nparam_max
;
4440 * Then terminate the parameter list, and leave.
4442 if (!params
) { /* need this special case */
4443 params
= nasm_malloc(sizeof(*params
));
4446 params
[nparam
] = NULL
;
4447 *params_array
= params
;
4451 * This one wasn't right: look for the next one with the
4454 list_for_each(m
, m
->next
)
4455 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4460 * After all that, we didn't find one with the right number of
4461 * parameters. Issue a warning, and fail to expand the macro.
4463 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4464 "macro `%s' exists, but not taking %d parameters",
4465 tline
->text
, nparam
);
4472 * Save MMacro invocation specific fields in
4473 * preparation for a recursive macro expansion
4475 static void push_mmacro(MMacro
*m
)
4477 MMacroInvocation
*i
;
4479 i
= nasm_malloc(sizeof(MMacroInvocation
));
4481 i
->params
= m
->params
;
4482 i
->iline
= m
->iline
;
4483 i
->nparam
= m
->nparam
;
4484 i
->rotate
= m
->rotate
;
4485 i
->paramlen
= m
->paramlen
;
4486 i
->unique
= m
->unique
;
4487 i
->condcnt
= m
->condcnt
;
4493 * Restore MMacro invocation specific fields that were
4494 * saved during a previous recursive macro expansion
4496 static void pop_mmacro(MMacro
*m
)
4498 MMacroInvocation
*i
;
4503 m
->params
= i
->params
;
4504 m
->iline
= i
->iline
;
4505 m
->nparam
= i
->nparam
;
4506 m
->rotate
= i
->rotate
;
4507 m
->paramlen
= i
->paramlen
;
4508 m
->unique
= i
->unique
;
4509 m
->condcnt
= i
->condcnt
;
4516 * Expand the multi-line macro call made by the given line, if
4517 * there is one to be expanded. If there is, push the expansion on
4518 * istk->expansion and return 1. Otherwise return 0.
4520 static int expand_mmacro(Token
* tline
)
4522 Token
*startline
= tline
;
4523 Token
*label
= NULL
;
4524 int dont_prepend
= 0;
4525 Token
**params
, *t
, *tt
;
4528 int i
, nparam
, *paramlen
;
4533 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4534 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4536 m
= is_mmacro(t
, ¶ms
);
4542 * We have an id which isn't a macro call. We'll assume
4543 * it might be a label; we'll also check to see if a
4544 * colon follows it. Then, if there's another id after
4545 * that lot, we'll check it again for macro-hood.
4549 if (tok_type_(t
, TOK_WHITESPACE
))
4550 last
= t
, t
= t
->next
;
4551 if (tok_is_(t
, ":")) {
4553 last
= t
, t
= t
->next
;
4554 if (tok_type_(t
, TOK_WHITESPACE
))
4555 last
= t
, t
= t
->next
;
4557 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4565 * Fix up the parameters: this involves stripping leading and
4566 * trailing whitespace, then stripping braces if they are
4569 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4570 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4572 for (i
= 0; params
[i
]; i
++) {
4574 int comma
= (!m
->plus
|| i
< nparam
- 1);
4578 if (tok_is_(t
, "{"))
4579 t
= t
->next
, brace
= true, comma
= false;
4583 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4584 break; /* ... because we have hit a comma */
4585 if (comma
&& t
->type
== TOK_WHITESPACE
4586 && tok_is_(t
->next
, ","))
4587 break; /* ... or a space then a comma */
4588 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4589 break; /* ... or a brace */
4596 * OK, we have a MMacro structure together with a set of
4597 * parameters. We must now go through the expansion and push
4598 * copies of each Line on to istk->expansion. Substitution of
4599 * parameter tokens and macro-local tokens doesn't get done
4600 * until the single-line macro substitution process; this is
4601 * because delaying them allows us to change the semantics
4602 * later through %rotate.
4604 * First, push an end marker on to istk->expansion, mark this
4605 * macro as in progress, and set up its invocation-specific
4608 ll
= nasm_malloc(sizeof(Line
));
4609 ll
->next
= istk
->expansion
;
4612 istk
->expansion
= ll
;
4615 * Save the previous MMacro expansion in the case of
4618 if (m
->max_depth
&& m
->in_progress
)
4626 m
->paramlen
= paramlen
;
4627 m
->unique
= unique
++;
4631 m
->next_active
= istk
->mstk
;
4634 list_for_each(l
, m
->expansion
) {
4637 ll
= nasm_malloc(sizeof(Line
));
4638 ll
->finishes
= NULL
;
4639 ll
->next
= istk
->expansion
;
4640 istk
->expansion
= ll
;
4643 list_for_each(t
, l
->first
) {
4647 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4649 case TOK_PREPROC_QQ
:
4650 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4652 case TOK_PREPROC_ID
:
4653 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4661 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4670 * If we had a label, push it on as the first line of
4671 * the macro expansion.
4674 if (dont_prepend
< 0)
4675 free_tlist(startline
);
4677 ll
= nasm_malloc(sizeof(Line
));
4678 ll
->finishes
= NULL
;
4679 ll
->next
= istk
->expansion
;
4680 istk
->expansion
= ll
;
4681 ll
->first
= startline
;
4682 if (!dont_prepend
) {
4684 label
= label
->next
;
4685 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4690 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4695 /* The function that actually does the error reporting */
4696 static void verror(int severity
, const char *fmt
, va_list arg
)
4699 MMacro
*mmac
= NULL
;
4702 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4704 /* get %macro name */
4705 if (istk
&& istk
->mstk
) {
4707 /* but %rep blocks should be skipped */
4708 while (mmac
&& !mmac
->name
)
4709 mmac
= mmac
->next_active
, delta
++;
4713 nasm_error(severity
, "(%s:%d) %s",
4714 mmac
->name
, mmac
->lineno
- delta
, buff
);
4716 nasm_error(severity
, "%s", buff
);
4720 * Since preprocessor always operate only on the line that didn't
4721 * arrived yet, we should always use ERR_OFFBY1.
4723 static void error(int severity
, const char *fmt
, ...)
4727 /* If we're in a dead branch of IF or something like it, ignore the error */
4728 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4732 verror(severity
, fmt
, arg
);
4737 * Because %else etc are evaluated in the state context
4738 * of the previous branch, errors might get lost with error():
4739 * %if 0 ... %else trailing garbage ... %endif
4740 * So %else etc should report errors with this function.
4742 static void error_precond(int severity
, const char *fmt
, ...)
4746 /* Only ignore the error if it's really in a dead branch */
4747 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4751 verror(severity
, fmt
, arg
);
4756 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4761 istk
= nasm_malloc(sizeof(Include
));
4764 istk
->expansion
= NULL
;
4766 istk
->fp
= fopen(file
, "r");
4768 src_set_fname(nasm_strdup(file
));
4772 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4775 nested_mac_count
= 0;
4776 nested_rep_count
= 0;
4779 if (tasm_compatible_mode
) {
4780 stdmacpos
= nasm_stdmac
;
4782 stdmacpos
= nasm_stdmac_after_tasm
;
4784 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4789 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4790 * The caller, however, will also pass in 3 for preprocess-only so
4791 * we can set __PASS__ accordingly.
4793 pass
= apass
> 2 ? 2 : apass
;
4795 dephead
= deptail
= deplist
;
4797 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4799 strcpy(sl
->str
, file
);
4801 deptail
= &sl
->next
;
4805 * Define the __PASS__ macro. This is defined here unlike
4806 * all the other builtins, because it is special -- it varies between
4809 t
= nasm_malloc(sizeof(*t
));
4811 make_tok_num(t
, apass
);
4813 define_smacro(NULL
, "__PASS__", true, 0, t
);
4816 static char *pp_getline(void)
4823 * Fetch a tokenized line, either from the macro-expansion
4824 * buffer or from the input file.
4827 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4828 Line
*l
= istk
->expansion
;
4829 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4833 * This is a macro-end marker for a macro with no
4834 * name, which means it's not really a macro at all
4835 * but a %rep block, and the `in_progress' field is
4836 * more than 1, meaning that we still need to
4837 * repeat. (1 means the natural last repetition; 0
4838 * means termination by %exitrep.) We have
4839 * therefore expanded up to the %endrep, and must
4840 * push the whole block on to the expansion buffer
4841 * again. We don't bother to remove the macro-end
4842 * marker: we'd only have to generate another one
4845 l
->finishes
->in_progress
--;
4846 list_for_each(l
, l
->finishes
->expansion
) {
4847 Token
*t
, *tt
, **tail
;
4849 ll
= nasm_malloc(sizeof(Line
));
4850 ll
->next
= istk
->expansion
;
4851 ll
->finishes
= NULL
;
4855 list_for_each(t
, l
->first
) {
4856 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4857 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4862 istk
->expansion
= ll
;
4866 * Check whether a `%rep' was started and not ended
4867 * within this macro expansion. This can happen and
4868 * should be detected. It's a fatal error because
4869 * I'm too confused to work out how to recover
4875 "defining with name in expansion");
4876 else if (istk
->mstk
->name
)
4878 "`%%rep' without `%%endrep' within"
4879 " expansion of macro `%s'",
4884 * FIXME: investigate the relationship at this point between
4885 * istk->mstk and l->finishes
4888 MMacro
*m
= istk
->mstk
;
4889 istk
->mstk
= m
->next_active
;
4892 * This was a real macro call, not a %rep, and
4893 * therefore the parameter information needs to
4898 l
->finishes
->in_progress
--;
4900 nasm_free(m
->params
);
4901 free_tlist(m
->iline
);
4902 nasm_free(m
->paramlen
);
4903 l
->finishes
->in_progress
= 0;
4908 istk
->expansion
= l
->next
;
4910 list
->downlevel(LIST_MACRO
);
4913 while (1) { /* until we get a line we can use */
4915 if (istk
->expansion
) { /* from a macro expansion */
4917 Line
*l
= istk
->expansion
;
4919 istk
->mstk
->lineno
++;
4921 istk
->expansion
= l
->next
;
4923 p
= detoken(tline
, false);
4924 list
->line(LIST_MACRO
, p
);
4929 if (line
) { /* from the current input file */
4930 line
= prepreproc(line
);
4931 tline
= tokenize(line
);
4936 * The current file has ended; work down the istk
4942 /* nasm_error can't be conditionally suppressed */
4943 nasm_error(ERR_FATAL
,
4944 "expected `%%endif' before end of file");
4946 /* only set line and file name if there's a next node */
4948 src_set_linnum(i
->lineno
);
4949 nasm_free(src_set_fname(nasm_strdup(i
->fname
)));
4952 list
->downlevel(LIST_INCLUDE
);
4956 if (istk
->expansion
&& istk
->expansion
->finishes
)
4962 * We must expand MMacro parameters and MMacro-local labels
4963 * _before_ we plunge into directive processing, to cope
4964 * with things like `%define something %1' such as STRUC
4965 * uses. Unless we're _defining_ a MMacro, in which case
4966 * those tokens should be left alone to go into the
4967 * definition; and unless we're in a non-emitting
4968 * condition, in which case we don't want to meddle with
4971 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4972 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4973 tline
= expand_mmac_params(tline
);
4977 * Check the line to see if it's a preprocessor directive.
4979 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4981 } else if (defining
) {
4983 * We're defining a multi-line macro. We emit nothing
4985 * shove the tokenized line on to the macro definition.
4987 Line
*l
= nasm_malloc(sizeof(Line
));
4988 l
->next
= defining
->expansion
;
4991 defining
->expansion
= l
;
4993 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4995 * We're in a non-emitting branch of a condition block.
4996 * Emit nothing at all, not even a blank line: when we
4997 * emerge from the condition we'll give a line-number
4998 * directive so we keep our place correctly.
5002 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
5004 * We're in a %rep block which has been terminated, so
5005 * we're walking through to the %endrep without
5006 * emitting anything. Emit nothing at all, not even a
5007 * blank line: when we emerge from the %rep block we'll
5008 * give a line-number directive so we keep our place
5014 tline
= expand_smacro(tline
);
5015 if (!expand_mmacro(tline
)) {
5017 * De-tokenize the line again, and emit it.
5019 line
= detoken(tline
, true);
5023 continue; /* expand_mmacro calls free_tlist */
5031 static void pp_cleanup(int pass
)
5034 if (defining
->name
) {
5036 "end of file while still defining macro `%s'",
5039 error(ERR_NONFATAL
, "end of file while still in %%rep");
5042 free_mmacro(defining
);
5052 nasm_free(i
->fname
);
5057 nasm_free(src_set_fname(NULL
));
5062 while ((i
= ipath
)) {
5071 static void pp_include_path(char *path
)
5075 i
= nasm_malloc(sizeof(IncPath
));
5076 i
->path
= path
? nasm_strdup(path
) : NULL
;
5089 static void pp_pre_include(char *fname
)
5091 Token
*inc
, *space
, *name
;
5094 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5095 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5096 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5098 l
= nasm_malloc(sizeof(Line
));
5105 static void pp_pre_define(char *definition
)
5111 equals
= strchr(definition
, '=');
5112 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5113 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5116 space
->next
= tokenize(definition
);
5120 l
= nasm_malloc(sizeof(Line
));
5127 static void pp_pre_undefine(char *definition
)
5132 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5133 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5134 space
->next
= tokenize(definition
);
5136 l
= nasm_malloc(sizeof(Line
));
5143 static void pp_extra_stdmac(macros_t
*macros
)
5145 extrastdmac
= macros
;
5148 static void make_tok_num(Token
* tok
, int64_t val
)
5151 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5152 tok
->text
= nasm_strdup(numbuf
);
5153 tok
->type
= TOK_NUMBER
;
5156 struct preproc_ops nasmpp
= {