1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro
;
85 typedef struct MMacro MMacro
;
86 typedef struct MMacroInvocation MMacroInvocation
;
87 typedef struct Context Context
;
88 typedef struct Token Token
;
89 typedef struct Blocks Blocks
;
90 typedef struct Line Line
;
91 typedef struct Include Include
;
92 typedef struct Cond Cond
;
93 typedef struct IncPath IncPath
;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
135 MMacroInvocation
*prev
; /* previous invocation */
137 int nparam_min
, nparam_max
;
139 bool plus
; /* is the last parameter greedy? */
140 bool nolist
; /* is this macro listing-inhibited? */
141 int64_t in_progress
; /* is this macro currently being expanded? */
142 int32_t max_depth
; /* maximum number of recursive expansions allowed */
143 Token
*dlist
; /* All defaults as one list */
144 Token
**defaults
; /* Parameter default pointers */
145 int ndefs
; /* number of default parameters */
149 MMacro
*rep_nest
; /* used for nesting %rep */
150 Token
**params
; /* actual parameters */
151 Token
*iline
; /* invocation line */
152 unsigned int nparam
, rotate
;
155 int lineno
; /* Current line number on expansion */
156 uint64_t condcnt
; /* number of if blocks... */
160 /* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
163 struct MMacroInvocation
{
164 MMacroInvocation
*prev
; /* previous invocation */
165 Token
**params
; /* actual parameters */
166 Token
*iline
; /* invocation line */
167 unsigned int nparam
, rotate
;
175 * The context stack is composed of a linked list of these.
180 struct hash_table localmac
;
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
192 * %define a(x,y) ( (x) & ~(y) )
194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
204 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
205 TOK_PREPROC_ID
, TOK_STRING
,
206 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
208 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
210 TOK_INDIRECT
, /* %[...] */
211 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
219 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
220 size_t len
; /* scratch length field */
221 } a
; /* Auxiliary data */
222 enum pp_token_type type
;
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
264 MMacro
*mstk
; /* stack of active macros/reps */
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
297 COND_IF_TRUE
, COND_IF_FALSE
,
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
303 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
313 COND_DONE
, COND_NEVER
315 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318 * These defines are used as the possible return values for do_directive
320 #define NO_DIRECTIVE_FOUND 0
321 #define DIRECTIVE_FOUND 1
324 * This define sets the upper limit for smacro and recursive mmacro
327 #define DEADMAN_LIMIT (1 << 20)
330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
335 static const char * const conditions
[] = {
336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
341 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
342 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
343 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
346 static const enum pp_conds inverse_ccs
[] = {
347 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
348 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
,
349 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
355 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
356 static int is_condition(enum preproc_token arg
)
358 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
361 /* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
368 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
369 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
372 static const char * const tasm_directives
[] = {
373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
377 static int StackSize
= 4;
378 static char *StackPointer
= "ebp";
379 static int ArgOffset
= 8;
380 static int LocalOffset
= 0;
382 static Context
*cstk
;
383 static Include
*istk
;
384 static IncPath
*ipath
= NULL
;
386 static int pass
; /* HACK: pass 0 = generate dependencies only */
387 static StrList
**dephead
, **deptail
; /* Dependency list */
389 static uint64_t unique
; /* unique identifier numbers */
391 static Line
*predef
= NULL
;
392 static bool do_predef
;
394 static ListGen
*list
;
397 * The current set of multi-line macros we have defined.
399 static struct hash_table mmacros
;
402 * The current set of single-line macros we have defined.
404 static struct hash_table smacros
;
407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
410 static MMacro
*defining
;
412 static uint64_t nested_mac_count
;
413 static uint64_t nested_rep_count
;
416 * The number of macro parameters to allocate space for at a time.
418 #define PARAM_DELTA 16
421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
424 static macros_t
*stdmacpos
;
427 * The extra standard macros that come from the object format, if
430 static macros_t
*extrastdmac
= NULL
;
431 static bool any_extrastdmac
;
434 * Tokens are allocated in blocks to improve speed
436 #define TOKEN_BLOCKSIZE 4096
437 static Token
*freeTokens
= NULL
;
443 static Blocks blocks
= { NULL
, NULL
};
446 * Forward declarations.
448 static Token
*expand_mmac_params(Token
* tline
);
449 static Token
*expand_smacro(Token
* tline
);
450 static Token
*expand_id(Token
* tline
);
451 static Context
*get_ctx(const char *name
, const char **namep
,
453 static void make_tok_num(Token
* tok
, int64_t val
);
454 static void error(int severity
, const char *fmt
, ...);
455 static void error_precond(int severity
, const char *fmt
, ...);
456 static void *new_Block(size_t size
);
457 static void delete_Blocks(void);
458 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
459 const char *text
, int txtlen
);
460 static Token
*delete_Token(Token
* t
);
463 * Macros for safe checking of token pointers, avoid *(NULL)
465 #define tok_type_(x,t) ((x) && (x)->type == (t))
466 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
471 * Handle TASM specific directives, which do not contain a % in
472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
476 static char *check_tasm_directive(char *line
)
478 int32_t i
, j
, k
, m
, len
;
479 char *p
, *q
, *oldline
, oldchar
;
481 p
= nasm_skip_spaces(line
);
483 /* Binary search for the directive name */
485 j
= ARRAY_SIZE(tasm_directives
);
486 q
= nasm_skip_word(p
);
493 m
= nasm_stricmp(p
, tasm_directives
[k
]);
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
501 line
= nasm_malloc(len
+ 2);
503 if (k
== TM_IFDIFI
) {
505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
510 strcpy(line
+ 1, "if 0");
512 memcpy(line
+ 1, p
, len
+ 1);
527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
532 static char *prepreproc(char *line
)
535 char *fname
, *oldline
;
537 if (line
[0] == '#' && line
[1] == ' ') {
540 lineno
= atoi(fname
);
541 fname
+= strspn(fname
, "0123456789 ");
544 fnlen
= strcspn(fname
, "\"");
545 line
= nasm_malloc(20 + fnlen
);
546 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
549 if (tasm_compatible_mode
)
550 return check_tasm_directive(line
);
555 * Free a linked list of tokens.
557 static void free_tlist(Token
* list
)
560 list
= delete_Token(list
);
564 * Free a linked list of lines.
566 static void free_llist(Line
* list
)
569 list_for_each_safe(l
, tmp
, list
) {
570 free_tlist(l
->first
);
578 static void free_mmacro(MMacro
* m
)
581 free_tlist(m
->dlist
);
582 nasm_free(m
->defaults
);
583 free_llist(m
->expansion
);
588 * Free all currently defined macros, and free the hash tables
590 static void free_smacro_table(struct hash_table
*smt
)
594 struct hash_tbl_node
*it
= NULL
;
596 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
597 nasm_free((void *)key
);
598 list_for_each_safe(s
, tmp
, s
) {
600 free_tlist(s
->expansion
);
607 static void free_mmacro_table(struct hash_table
*mmt
)
611 struct hash_tbl_node
*it
= NULL
;
614 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
615 nasm_free((void *)key
);
616 list_for_each_safe(m
,tmp
, m
)
622 static void free_macros(void)
624 free_smacro_table(&smacros
);
625 free_mmacro_table(&mmacros
);
629 * Initialize the hash tables
631 static void init_macros(void)
633 hash_init(&smacros
, HASH_LARGE
);
634 hash_init(&mmacros
, HASH_LARGE
);
638 * Pop the context stack.
640 static void ctx_pop(void)
645 free_smacro_table(&c
->localmac
);
651 * Search for a key in the hash index; adding it if necessary
652 * (in which case we initialize the data pointer to NULL.)
655 hash_findi_add(struct hash_table
*hash
, const char *str
)
657 struct hash_insert hi
;
661 r
= hash_findi(hash
, str
, &hi
);
665 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
666 return hash_add(&hi
, strx
, NULL
);
670 * Like hash_findi, but returns the data element rather than a pointer
671 * to it. Used only when not adding a new element, hence no third
675 hash_findix(struct hash_table
*hash
, const char *str
)
679 p
= hash_findi(hash
, str
, NULL
);
680 return p
? *p
: NULL
;
684 * read line from standart macros set,
685 * if there no more left -- return NULL
687 static char *line_from_stdmac(void)
690 const unsigned char *p
= stdmacpos
;
699 len
+= pp_directives_len
[c
- 0x80] + 1;
704 line
= nasm_malloc(len
+ 1);
706 while ((c
= *stdmacpos
++)) {
708 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
709 q
+= pp_directives_len
[c
- 0x80];
719 /* This was the last of the standard macro chain... */
721 if (any_extrastdmac
) {
722 stdmacpos
= extrastdmac
;
723 any_extrastdmac
= false;
724 } else if (do_predef
) {
726 Token
*head
, **tail
, *t
;
729 * Nasty hack: here we push the contents of
730 * `predef' on to the top-level expansion stack,
731 * since this is the most convenient way to
732 * implement the pre-include and pre-define
735 list_for_each(pd
, predef
) {
738 list_for_each(t
, pd
->first
) {
739 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
740 tail
= &(*tail
)->next
;
743 l
= nasm_malloc(sizeof(Line
));
744 l
->next
= istk
->expansion
;
757 #define BUF_DELTA 512
759 * Read a line from the top file in istk, handling multiple CR/LFs
760 * at the end of the line read, and handling spurious ^Zs. Will
761 * return lines from the standard macro set if this has not already
764 static char *read_line(void)
766 char *buffer
, *p
, *q
;
767 int bufsize
, continued_count
;
770 * standart macros set (predefined) goes first
772 p
= line_from_stdmac();
777 * regular read from a file
780 buffer
= nasm_malloc(BUF_DELTA
);
784 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
788 if (p
> buffer
&& p
[-1] == '\n') {
790 * Convert backslash-CRLF line continuation sequences into
791 * nothing at all (for DOS and Windows)
793 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
799 * Also convert backslash-LF line continuation sequences into
800 * nothing at all (for Unix)
802 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
810 if (p
- buffer
> bufsize
- 10) {
811 int32_t offset
= p
- buffer
;
812 bufsize
+= BUF_DELTA
;
813 buffer
= nasm_realloc(buffer
, bufsize
);
814 p
= buffer
+ offset
; /* prevent stale-pointer problems */
818 if (!q
&& p
== buffer
) {
823 src_set_linnum(src_get_linnum() + istk
->lineinc
+
824 (continued_count
* istk
->lineinc
));
827 * Play safe: remove CRs as well as LFs, if any of either are
828 * present at the end of the line.
830 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
834 * Handle spurious ^Z, which may be inserted into source files
835 * by some file transfer utilities.
837 buffer
[strcspn(buffer
, "\032")] = '\0';
839 list
->line(LIST_READ
, buffer
);
845 * Tokenize a line of text. This is a very simple process since we
846 * don't need to parse the value out of e.g. numeric tokens: we
847 * simply split one string into many.
849 static Token
*tokenize(char *line
)
852 enum pp_token_type type
;
854 Token
*t
, **tail
= &list
;
860 if (*p
== '+' && !nasm_isdigit(p
[1])) {
863 } else if (nasm_isdigit(*p
) ||
864 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
868 while (nasm_isdigit(*p
));
869 type
= TOK_PREPROC_ID
;
870 } else if (*p
== '{') {
872 while (*p
&& *p
!= '}') {
879 type
= TOK_PREPROC_ID
;
880 } else if (*p
== '[') {
882 line
+= 2; /* Skip the leading %[ */
884 while (lvl
&& (c
= *p
++)) {
896 p
= nasm_skip_string(p
- 1) + 1;
906 error(ERR_NONFATAL
, "unterminated %[ construct");
908 } else if (*p
== '?') {
909 type
= TOK_PREPROC_Q
; /* %? */
912 type
= TOK_PREPROC_QQ
; /* %?? */
915 } else if (isidchar(*p
) ||
916 ((*p
== '!' || *p
== '%' || *p
== '$') &&
921 while (isidchar(*p
));
922 type
= TOK_PREPROC_ID
;
928 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
931 while (*p
&& isidchar(*p
))
933 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
938 p
= nasm_skip_string(p
);
943 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
944 /* Handling unterminated strings by UNV */
947 } else if (p
[0] == '$' && p
[1] == '$') {
948 type
= TOK_OTHER
; /* TOKEN_BASE */
950 } else if (isnumstart(*p
)) {
952 bool is_float
= false;
968 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
970 if (*p
== '+' || *p
== '-') {
972 * e can only be followed by +/- if it is either a
973 * prefixed hex number or a floating-point number
978 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
980 } else if (c
== 'P' || c
== 'p') {
982 if (*p
== '+' || *p
== '-')
984 } else if (isnumchar(c
) || c
== '_')
988 * we need to deal with consequences of the legacy
989 * parser, like "1.nolist" being two tokens
990 * (TOK_NUMBER, TOK_ID) here; at least give it
991 * a shot for now. In the future, we probably need
992 * a flex-based scanner with proper pattern matching
993 * to do it as well as it can be done. Nothing in
994 * the world is going to help the person who wants
995 * 0x123.p16 interpreted as two tokens, though.
1001 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1002 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1003 (*r
== 'p' || *r
== 'P')) {
1007 break; /* Terminate the token */
1011 p
--; /* Point to first character beyond number */
1013 if (p
== line
+1 && *line
== '$') {
1014 type
= TOK_OTHER
; /* TOKEN_HERE */
1016 if (has_e
&& !is_hex
) {
1017 /* 1e13 is floating-point, but 1e13h is not */
1021 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1023 } else if (nasm_isspace(*p
)) {
1024 type
= TOK_WHITESPACE
;
1025 p
= nasm_skip_spaces(p
);
1027 * Whitespace just before end-of-line is discarded by
1028 * pretending it's a comment; whitespace just before a
1029 * comment gets lumped into the comment.
1031 if (!*p
|| *p
== ';') {
1036 } else if (*p
== ';') {
1042 * Anything else is an operator of some kind. We check
1043 * for all the double-character operators (>>, <<, //,
1044 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1045 * else is a single-character operator.
1048 if ((p
[0] == '>' && p
[1] == '>') ||
1049 (p
[0] == '<' && p
[1] == '<') ||
1050 (p
[0] == '/' && p
[1] == '/') ||
1051 (p
[0] == '<' && p
[1] == '=') ||
1052 (p
[0] == '>' && p
[1] == '=') ||
1053 (p
[0] == '=' && p
[1] == '=') ||
1054 (p
[0] == '!' && p
[1] == '=') ||
1055 (p
[0] == '<' && p
[1] == '>') ||
1056 (p
[0] == '&' && p
[1] == '&') ||
1057 (p
[0] == '|' && p
[1] == '|') ||
1058 (p
[0] == '^' && p
[1] == '^')) {
1064 /* Handling unterminated string by UNV */
1067 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1068 t->text[p-line] = *line;
1072 if (type
!= TOK_COMMENT
) {
1073 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1082 * this function allocates a new managed block of memory and
1083 * returns a pointer to the block. The managed blocks are
1084 * deleted only all at once by the delete_Blocks function.
1086 static void *new_Block(size_t size
)
1088 Blocks
*b
= &blocks
;
1090 /* first, get to the end of the linked list */
1093 /* now allocate the requested chunk */
1094 b
->chunk
= nasm_malloc(size
);
1096 /* now allocate a new block for the next request */
1097 b
->next
= nasm_malloc(sizeof(Blocks
));
1098 /* and initialize the contents of the new block */
1099 b
->next
->next
= NULL
;
1100 b
->next
->chunk
= NULL
;
1105 * this function deletes all managed blocks of memory
1107 static void delete_Blocks(void)
1109 Blocks
*a
, *b
= &blocks
;
1112 * keep in mind that the first block, pointed to by blocks
1113 * is a static and not dynamically allocated, so we don't
1118 nasm_free(b
->chunk
);
1127 * this function creates a new Token and passes a pointer to it
1128 * back to the caller. It sets the type and text elements, and
1129 * also the a.mac and next elements to NULL.
1131 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1132 const char *text
, int txtlen
)
1138 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1139 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1140 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1141 freeTokens
[i
].next
= NULL
;
1144 freeTokens
= t
->next
;
1148 if (type
== TOK_WHITESPACE
|| !text
) {
1152 txtlen
= strlen(text
);
1153 t
->text
= nasm_malloc(txtlen
+1);
1154 memcpy(t
->text
, text
, txtlen
);
1155 t
->text
[txtlen
] = '\0';
1160 static Token
*delete_Token(Token
* t
)
1162 Token
*next
= t
->next
;
1164 t
->next
= freeTokens
;
1170 * Convert a line of tokens back into text.
1171 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1172 * will be transformed into ..@ctxnum.xxx
1174 static char *detoken(Token
* tlist
, bool expand_locals
)
1181 list_for_each(t
, tlist
) {
1182 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1183 char *p
= getenv(t
->text
+ 2);
1186 t
->text
= nasm_strdup(p
);
1188 error(ERR_FATAL
, "`%s' is empty", q
+ 2);
1191 /* Expand local macros here and not during preprocessing */
1192 if (expand_locals
&&
1193 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1194 t
->text
[0] == '%' && t
->text
[1] == '$') {
1197 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1200 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1201 p
= nasm_strcat(buffer
, q
);
1206 if (t
->type
== TOK_WHITESPACE
)
1209 len
+= strlen(t
->text
);
1212 p
= line
= nasm_malloc(len
+ 1);
1214 list_for_each(t
, tlist
) {
1215 if (t
->type
== TOK_WHITESPACE
) {
1217 } else if (t
->text
) {
1229 * A scanner, suitable for use by the expression evaluator, which
1230 * operates on a line of Tokens. Expects a pointer to a pointer to
1231 * the first token in the line to be passed in as its private_data
1234 * FIX: This really needs to be unified with stdscan.
1236 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1238 Token
**tlineptr
= private_data
;
1240 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1244 *tlineptr
= tline
? tline
->next
: NULL
;
1245 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1246 tline
->type
== TOK_COMMENT
));
1249 return tokval
->t_type
= TOKEN_EOS
;
1251 tokval
->t_charptr
= tline
->text
;
1253 if (tline
->text
[0] == '$' && !tline
->text
[1])
1254 return tokval
->t_type
= TOKEN_HERE
;
1255 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1256 return tokval
->t_type
= TOKEN_BASE
;
1258 if (tline
->type
== TOK_ID
) {
1259 p
= tokval
->t_charptr
= tline
->text
;
1261 tokval
->t_charptr
++;
1262 return tokval
->t_type
= TOKEN_ID
;
1265 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1266 if (r
>= p
+MAX_KEYWORD
)
1267 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1268 *s
++ = nasm_tolower(*r
);
1271 /* right, so we have an identifier sitting in temp storage. now,
1272 * is it actually a register or instruction name, or what? */
1273 return nasm_token_hash(ourcopy
, tokval
);
1276 if (tline
->type
== TOK_NUMBER
) {
1278 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1279 tokval
->t_charptr
= tline
->text
;
1281 return tokval
->t_type
= TOKEN_ERRNUM
;
1283 return tokval
->t_type
= TOKEN_NUM
;
1286 if (tline
->type
== TOK_FLOAT
) {
1287 return tokval
->t_type
= TOKEN_FLOAT
;
1290 if (tline
->type
== TOK_STRING
) {
1293 bq
= tline
->text
[0];
1294 tokval
->t_charptr
= tline
->text
;
1295 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1297 if (ep
[0] != bq
|| ep
[1] != '\0')
1298 return tokval
->t_type
= TOKEN_ERRSTR
;
1300 return tokval
->t_type
= TOKEN_STR
;
1303 if (tline
->type
== TOK_OTHER
) {
1304 if (!strcmp(tline
->text
, "<<"))
1305 return tokval
->t_type
= TOKEN_SHL
;
1306 if (!strcmp(tline
->text
, ">>"))
1307 return tokval
->t_type
= TOKEN_SHR
;
1308 if (!strcmp(tline
->text
, "//"))
1309 return tokval
->t_type
= TOKEN_SDIV
;
1310 if (!strcmp(tline
->text
, "%%"))
1311 return tokval
->t_type
= TOKEN_SMOD
;
1312 if (!strcmp(tline
->text
, "=="))
1313 return tokval
->t_type
= TOKEN_EQ
;
1314 if (!strcmp(tline
->text
, "<>"))
1315 return tokval
->t_type
= TOKEN_NE
;
1316 if (!strcmp(tline
->text
, "!="))
1317 return tokval
->t_type
= TOKEN_NE
;
1318 if (!strcmp(tline
->text
, "<="))
1319 return tokval
->t_type
= TOKEN_LE
;
1320 if (!strcmp(tline
->text
, ">="))
1321 return tokval
->t_type
= TOKEN_GE
;
1322 if (!strcmp(tline
->text
, "&&"))
1323 return tokval
->t_type
= TOKEN_DBL_AND
;
1324 if (!strcmp(tline
->text
, "^^"))
1325 return tokval
->t_type
= TOKEN_DBL_XOR
;
1326 if (!strcmp(tline
->text
, "||"))
1327 return tokval
->t_type
= TOKEN_DBL_OR
;
1331 * We have no other options: just return the first character of
1334 return tokval
->t_type
= tline
->text
[0];
1338 * Compare a string to the name of an existing macro; this is a
1339 * simple wrapper which calls either strcmp or nasm_stricmp
1340 * depending on the value of the `casesense' parameter.
1342 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1344 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1348 * Compare a string to the name of an existing macro; this is a
1349 * simple wrapper which calls either strcmp or nasm_stricmp
1350 * depending on the value of the `casesense' parameter.
1352 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1354 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1358 * Return the Context structure associated with a %$ token. Return
1359 * NULL, having _already_ reported an error condition, if the
1360 * context stack isn't deep enough for the supplied number of $
1362 * If all_contexts == true, contexts that enclose current are
1363 * also scanned for such smacro, until it is found; if not -
1364 * only the context that directly results from the number of $'s
1365 * in variable's name.
1367 * If "namep" is non-NULL, set it to the pointer to the macro name
1368 * tail, i.e. the part beyond %$...
1370 static Context
*get_ctx(const char *name
, const char **namep
,
1380 if (!name
|| name
[0] != '%' || name
[1] != '$')
1384 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1391 while (ctx
&& *name
== '$') {
1397 error(ERR_NONFATAL
, "`%s': context stack is only"
1398 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1409 /* Search for this smacro in found context */
1410 m
= hash_findix(&ctx
->localmac
, name
);
1412 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1423 * Check to see if a file is already in a string list
1425 static bool in_list(const StrList
*list
, const char *str
)
1428 if (!strcmp(list
->str
, str
))
1436 * Open an include file. This routine must always return a valid
1437 * file pointer if it returns - it's responsible for throwing an
1438 * ERR_FATAL and bombing out completely if not. It should also try
1439 * the include path one by one until it finds the file or reaches
1440 * the end of the path.
1442 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1447 IncPath
*ip
= ipath
;
1448 int len
= strlen(file
);
1449 size_t prefix_len
= 0;
1453 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1454 memcpy(sl
->str
, prefix
, prefix_len
);
1455 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1456 fp
= fopen(sl
->str
, "r");
1457 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1475 prefix_len
= strlen(prefix
);
1477 /* -MG given and file not found */
1478 if (dhead
&& !in_list(*dhead
, file
)) {
1479 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1481 strcpy(sl
->str
, file
);
1489 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1494 * Determine if we should warn on defining a single-line macro of
1495 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1496 * return true if _any_ single-line macro of that name is defined.
1497 * Otherwise, will return true if a single-line macro with either
1498 * `nparam' or no parameters is defined.
1500 * If a macro with precisely the right number of parameters is
1501 * defined, or nparam is -1, the address of the definition structure
1502 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1503 * is NULL, no action will be taken regarding its contents, and no
1506 * Note that this is also called with nparam zero to resolve
1509 * If you already know which context macro belongs to, you can pass
1510 * the context pointer as first parameter; if you won't but name begins
1511 * with %$ the context will be automatically computed. If all_contexts
1512 * is true, macro will be searched in outer contexts as well.
1515 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1518 struct hash_table
*smtbl
;
1522 smtbl
= &ctx
->localmac
;
1523 } else if (name
[0] == '%' && name
[1] == '$') {
1525 ctx
= get_ctx(name
, &name
, false);
1527 return false; /* got to return _something_ */
1528 smtbl
= &ctx
->localmac
;
1532 m
= (SMacro
*) hash_findix(smtbl
, name
);
1535 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1536 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1538 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1552 * Count and mark off the parameters in a multi-line macro call.
1553 * This is called both from within the multi-line macro expansion
1554 * code, and also to mark off the default parameters when provided
1555 * in a %macro definition line.
1557 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1559 int paramsize
, brace
;
1561 *nparam
= paramsize
= 0;
1564 /* +1: we need space for the final NULL */
1565 if (*nparam
+1 >= paramsize
) {
1566 paramsize
+= PARAM_DELTA
;
1567 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1571 if (tok_is_(t
, "{"))
1573 (*params
)[(*nparam
)++] = t
;
1574 while (tok_isnt_(t
, brace
? "}" : ","))
1576 if (t
) { /* got a comma/brace */
1580 * Now we've found the closing brace, look further
1584 if (tok_isnt_(t
, ",")) {
1586 "braces do not enclose all of macro parameter");
1587 while (tok_isnt_(t
, ","))
1591 t
= t
->next
; /* eat the comma */
1598 * Determine whether one of the various `if' conditions is true or
1601 * We must free the tline we get passed.
1603 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1605 enum pp_conditional i
= PP_COND(ct
);
1607 Token
*t
, *tt
, **tptr
, *origline
;
1608 struct tokenval tokval
;
1610 enum pp_token_type needtype
;
1616 j
= false; /* have we matched yet? */
1621 if (tline
->type
!= TOK_ID
) {
1623 "`%s' expects context identifiers", pp_directives
[ct
]);
1624 free_tlist(origline
);
1627 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1629 tline
= tline
->next
;
1634 j
= false; /* have we matched yet? */
1637 if (!tline
|| (tline
->type
!= TOK_ID
&&
1638 (tline
->type
!= TOK_PREPROC_ID
||
1639 tline
->text
[1] != '$'))) {
1641 "`%s' expects macro identifiers", pp_directives
[ct
]);
1644 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1646 tline
= tline
->next
;
1652 tline
= expand_smacro(tline
);
1654 while (tok_isnt_(tt
, ","))
1658 "`%s' expects two comma-separated arguments",
1663 j
= true; /* assume equality unless proved not */
1664 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1665 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1666 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1670 if (t
->type
== TOK_WHITESPACE
) {
1674 if (tt
->type
== TOK_WHITESPACE
) {
1678 if (tt
->type
!= t
->type
) {
1679 j
= false; /* found mismatching tokens */
1682 /* When comparing strings, need to unquote them first */
1683 if (t
->type
== TOK_STRING
) {
1684 size_t l1
= nasm_unquote(t
->text
, NULL
);
1685 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1691 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1695 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1696 j
= false; /* found mismatching tokens */
1703 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1704 j
= false; /* trailing gunk on one end or other */
1710 MMacro searching
, *mmac
;
1713 tline
= expand_id(tline
);
1714 if (!tok_type_(tline
, TOK_ID
)) {
1716 "`%s' expects a macro name", pp_directives
[ct
]);
1719 searching
.name
= nasm_strdup(tline
->text
);
1720 searching
.casesense
= true;
1721 searching
.plus
= false;
1722 searching
.nolist
= false;
1723 searching
.in_progress
= 0;
1724 searching
.max_depth
= 0;
1725 searching
.rep_nest
= NULL
;
1726 searching
.nparam_min
= 0;
1727 searching
.nparam_max
= INT_MAX
;
1728 tline
= expand_smacro(tline
->next
);
1731 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1733 "`%s' expects a parameter count or nothing",
1736 searching
.nparam_min
= searching
.nparam_max
=
1737 readnum(tline
->text
, &j
);
1740 "unable to parse parameter count `%s'",
1743 if (tline
&& tok_is_(tline
->next
, "-")) {
1744 tline
= tline
->next
->next
;
1745 if (tok_is_(tline
, "*"))
1746 searching
.nparam_max
= INT_MAX
;
1747 else if (!tok_type_(tline
, TOK_NUMBER
))
1749 "`%s' expects a parameter count after `-'",
1752 searching
.nparam_max
= readnum(tline
->text
, &j
);
1755 "unable to parse parameter count `%s'",
1757 if (searching
.nparam_min
> searching
.nparam_max
)
1759 "minimum parameter count exceeds maximum");
1762 if (tline
&& tok_is_(tline
->next
, "+")) {
1763 tline
= tline
->next
;
1764 searching
.plus
= true;
1766 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1768 if (!strcmp(mmac
->name
, searching
.name
) &&
1769 (mmac
->nparam_min
<= searching
.nparam_max
1771 && (searching
.nparam_min
<= mmac
->nparam_max
1778 if (tline
&& tline
->next
)
1779 error(ERR_WARNING
|ERR_PASS1
,
1780 "trailing garbage after %%ifmacro ignored");
1781 nasm_free(searching
.name
);
1790 needtype
= TOK_NUMBER
;
1793 needtype
= TOK_STRING
;
1797 t
= tline
= expand_smacro(tline
);
1799 while (tok_type_(t
, TOK_WHITESPACE
) ||
1800 (needtype
== TOK_NUMBER
&&
1801 tok_type_(t
, TOK_OTHER
) &&
1802 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1806 j
= tok_type_(t
, needtype
);
1810 t
= tline
= expand_smacro(tline
);
1811 while (tok_type_(t
, TOK_WHITESPACE
))
1816 t
= t
->next
; /* Skip the actual token */
1817 while (tok_type_(t
, TOK_WHITESPACE
))
1819 j
= !t
; /* Should be nothing left */
1824 t
= tline
= expand_smacro(tline
);
1825 while (tok_type_(t
, TOK_WHITESPACE
))
1828 j
= !t
; /* Should be empty */
1832 t
= tline
= expand_smacro(tline
);
1834 tokval
.t_type
= TOKEN_INVALID
;
1835 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1836 NULL
, pass
| CRITICAL
, error
, NULL
);
1840 error(ERR_WARNING
|ERR_PASS1
,
1841 "trailing garbage after expression ignored");
1842 if (!is_simple(evalresult
)) {
1844 "non-constant value given to `%s'", pp_directives
[ct
]);
1847 j
= reloc_value(evalresult
) != 0;
1852 "preprocessor directive `%s' not yet implemented",
1857 free_tlist(origline
);
1858 return j
^ PP_NEGATIVE(ct
);
1861 free_tlist(origline
);
1866 * Common code for defining an smacro
1868 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1869 int nparam
, Token
*expansion
)
1871 SMacro
*smac
, **smhead
;
1872 struct hash_table
*smtbl
;
1874 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1876 error(ERR_WARNING
|ERR_PASS1
,
1877 "single-line macro `%s' defined both with and"
1878 " without parameters", mname
);
1880 * Some instances of the old code considered this a failure,
1881 * some others didn't. What is the right thing to do here?
1883 free_tlist(expansion
);
1884 return false; /* Failure */
1887 * We're redefining, so we have to take over an
1888 * existing SMacro structure. This means freeing
1889 * what was already in it.
1891 nasm_free(smac
->name
);
1892 free_tlist(smac
->expansion
);
1895 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1896 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1897 smac
= nasm_malloc(sizeof(SMacro
));
1898 smac
->next
= *smhead
;
1901 smac
->name
= nasm_strdup(mname
);
1902 smac
->casesense
= casesense
;
1903 smac
->nparam
= nparam
;
1904 smac
->expansion
= expansion
;
1905 smac
->in_progress
= false;
1906 return true; /* Success */
1910 * Undefine an smacro
1912 static void undef_smacro(Context
*ctx
, const char *mname
)
1914 SMacro
**smhead
, *s
, **sp
;
1915 struct hash_table
*smtbl
;
1917 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1918 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1922 * We now have a macro name... go hunt for it.
1925 while ((s
= *sp
) != NULL
) {
1926 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1929 free_tlist(s
->expansion
);
1939 * Parse a mmacro specification.
1941 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1945 tline
= tline
->next
;
1947 tline
= expand_id(tline
);
1948 if (!tok_type_(tline
, TOK_ID
)) {
1949 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1954 def
->name
= nasm_strdup(tline
->text
);
1956 def
->nolist
= false;
1957 def
->in_progress
= 0;
1958 def
->rep_nest
= NULL
;
1959 def
->nparam_min
= 0;
1960 def
->nparam_max
= 0;
1962 tline
= expand_smacro(tline
->next
);
1964 if (!tok_type_(tline
, TOK_NUMBER
)) {
1965 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1967 def
->nparam_min
= def
->nparam_max
=
1968 readnum(tline
->text
, &err
);
1971 "unable to parse parameter count `%s'", tline
->text
);
1973 if (tline
&& tok_is_(tline
->next
, "-")) {
1974 tline
= tline
->next
->next
;
1975 if (tok_is_(tline
, "*")) {
1976 def
->nparam_max
= INT_MAX
;
1977 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1979 "`%s' expects a parameter count after `-'", directive
);
1981 def
->nparam_max
= readnum(tline
->text
, &err
);
1983 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1986 if (def
->nparam_min
> def
->nparam_max
) {
1987 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1991 if (tline
&& tok_is_(tline
->next
, "+")) {
1992 tline
= tline
->next
;
1995 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1996 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1997 tline
= tline
->next
;
2002 * Handle default parameters.
2004 if (tline
&& tline
->next
) {
2005 def
->dlist
= tline
->next
;
2007 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2010 def
->defaults
= NULL
;
2012 def
->expansion
= NULL
;
2014 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2016 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2017 "too many default macro parameters");
2024 * Decode a size directive
2026 static int parse_size(const char *str
) {
2027 static const char *size_names
[] =
2028 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2029 static const int sizes
[] =
2030 { 0, 1, 4, 16, 8, 10, 2, 32 };
2032 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2036 * nasm_unquote with error if the string contains NUL characters.
2037 * If the string contains NUL characters, issue an error and return
2038 * the C len, i.e. truncate at the NUL.
2040 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
2042 size_t len
= nasm_unquote(qstr
, NULL
);
2043 size_t clen
= strlen(qstr
);
2046 error(ERR_NONFATAL
, "NUL character in `%s' directive",
2047 pp_directives
[directive
]);
2053 * find and process preprocessor directive in passed line
2054 * Find out if a line contains a preprocessor directive, and deal
2057 * If a directive _is_ found, it is the responsibility of this routine
2058 * (and not the caller) to free_tlist() the line.
2060 * @param tline a pointer to the current tokeninzed line linked list
2061 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2064 static int do_directive(Token
* tline
)
2066 enum preproc_token i
;
2079 MMacro
*mmac
, **mmhead
;
2080 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2082 struct tokenval tokval
;
2084 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2092 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2093 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2094 || tline
->text
[1] == '!'))
2095 return NO_DIRECTIVE_FOUND
;
2097 i
= pp_token_hash(tline
->text
);
2100 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2101 * since they are known to be buggy at moment, we need to fix them
2102 * in future release (2.09-2.10)
2104 if (i
== PP_RMACRO
|| i
== PP_RMACRO
|| i
== PP_EXITMACRO
) {
2105 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2107 return NO_DIRECTIVE_FOUND
;
2111 * If we're in a non-emitting branch of a condition construct,
2112 * or walking to the end of an already terminated %rep block,
2113 * we should ignore all directives except for condition
2116 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2117 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2118 return NO_DIRECTIVE_FOUND
;
2122 * If we're defining a macro or reading a %rep block, we should
2123 * ignore all directives except for %macro/%imacro (which nest),
2124 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2125 * If we're in a %rep block, another %rep nests, so should be let through.
2127 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2128 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2129 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2130 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2131 return NO_DIRECTIVE_FOUND
;
2135 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2136 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2138 return NO_DIRECTIVE_FOUND
;
2139 } else if (nested_mac_count
> 0) {
2140 if (i
== PP_ENDMACRO
) {
2142 return NO_DIRECTIVE_FOUND
;
2145 if (!defining
->name
) {
2148 return NO_DIRECTIVE_FOUND
;
2149 } else if (nested_rep_count
> 0) {
2150 if (i
== PP_ENDREP
) {
2152 return NO_DIRECTIVE_FOUND
;
2160 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2162 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2165 /* Directive to tell NASM what the default stack size is. The
2166 * default is for a 16-bit stack, and this can be overriden with
2169 tline
= tline
->next
;
2170 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2171 tline
= tline
->next
;
2172 if (!tline
|| tline
->type
!= TOK_ID
) {
2173 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2174 free_tlist(origline
);
2175 return DIRECTIVE_FOUND
;
2177 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2178 /* All subsequent ARG directives are for a 32-bit stack */
2180 StackPointer
= "ebp";
2183 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2184 /* All subsequent ARG directives are for a 64-bit stack */
2186 StackPointer
= "rbp";
2189 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2190 /* All subsequent ARG directives are for a 16-bit stack,
2191 * far function call.
2194 StackPointer
= "bp";
2197 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2198 /* All subsequent ARG directives are for a 16-bit stack,
2199 * far function call. We don't support near functions.
2202 StackPointer
= "bp";
2206 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2207 free_tlist(origline
);
2208 return DIRECTIVE_FOUND
;
2210 free_tlist(origline
);
2211 return DIRECTIVE_FOUND
;
2214 /* TASM like ARG directive to define arguments to functions, in
2215 * the following form:
2217 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2221 char *arg
, directive
[256];
2222 int size
= StackSize
;
2224 /* Find the argument name */
2225 tline
= tline
->next
;
2226 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2227 tline
= tline
->next
;
2228 if (!tline
|| tline
->type
!= TOK_ID
) {
2229 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2230 free_tlist(origline
);
2231 return DIRECTIVE_FOUND
;
2235 /* Find the argument size type */
2236 tline
= tline
->next
;
2237 if (!tline
|| tline
->type
!= TOK_OTHER
2238 || tline
->text
[0] != ':') {
2240 "Syntax error processing `%%arg' directive");
2241 free_tlist(origline
);
2242 return DIRECTIVE_FOUND
;
2244 tline
= tline
->next
;
2245 if (!tline
|| tline
->type
!= TOK_ID
) {
2246 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2247 free_tlist(origline
);
2248 return DIRECTIVE_FOUND
;
2251 /* Allow macro expansion of type parameter */
2252 tt
= tokenize(tline
->text
);
2253 tt
= expand_smacro(tt
);
2254 size
= parse_size(tt
->text
);
2257 "Invalid size type for `%%arg' missing directive");
2259 free_tlist(origline
);
2260 return DIRECTIVE_FOUND
;
2264 /* Round up to even stack slots */
2265 size
= ALIGN(size
, StackSize
);
2267 /* Now define the macro for the argument */
2268 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2269 arg
, StackPointer
, offset
);
2270 do_directive(tokenize(directive
));
2273 /* Move to the next argument in the list */
2274 tline
= tline
->next
;
2275 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2276 tline
= tline
->next
;
2277 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2279 free_tlist(origline
);
2280 return DIRECTIVE_FOUND
;
2283 /* TASM like LOCAL directive to define local variables for a
2284 * function, in the following form:
2286 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2288 * The '= LocalSize' at the end is ignored by NASM, but is
2289 * required by TASM to define the local parameter size (and used
2290 * by the TASM macro package).
2292 offset
= LocalOffset
;
2294 char *local
, directive
[256];
2295 int size
= StackSize
;
2297 /* Find the argument name */
2298 tline
= tline
->next
;
2299 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2300 tline
= tline
->next
;
2301 if (!tline
|| tline
->type
!= TOK_ID
) {
2303 "`%%local' missing argument parameter");
2304 free_tlist(origline
);
2305 return DIRECTIVE_FOUND
;
2307 local
= tline
->text
;
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 `%%local' directive");
2315 free_tlist(origline
);
2316 return DIRECTIVE_FOUND
;
2318 tline
= tline
->next
;
2319 if (!tline
|| tline
->type
!= TOK_ID
) {
2321 "`%%local' missing size type parameter");
2322 free_tlist(origline
);
2323 return DIRECTIVE_FOUND
;
2326 /* Allow macro expansion of type parameter */
2327 tt
= tokenize(tline
->text
);
2328 tt
= expand_smacro(tt
);
2329 size
= parse_size(tt
->text
);
2332 "Invalid size type for `%%local' missing directive");
2334 free_tlist(origline
);
2335 return DIRECTIVE_FOUND
;
2339 /* Round up to even stack slots */
2340 size
= ALIGN(size
, StackSize
);
2342 offset
+= size
; /* Negative offset, increment before */
2344 /* Now define the macro for the argument */
2345 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2346 local
, StackPointer
, offset
);
2347 do_directive(tokenize(directive
));
2349 /* Now define the assign to setup the enter_c macro correctly */
2350 snprintf(directive
, sizeof(directive
),
2351 "%%assign %%$localsize %%$localsize+%d", size
);
2352 do_directive(tokenize(directive
));
2354 /* Move to the next argument in the list */
2355 tline
= tline
->next
;
2356 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2357 tline
= tline
->next
;
2358 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2359 LocalOffset
= offset
;
2360 free_tlist(origline
);
2361 return DIRECTIVE_FOUND
;
2365 error(ERR_WARNING
|ERR_PASS1
,
2366 "trailing garbage after `%%clear' ignored");
2369 free_tlist(origline
);
2370 return DIRECTIVE_FOUND
;
2373 t
= tline
->next
= expand_smacro(tline
->next
);
2375 if (!t
|| (t
->type
!= TOK_STRING
&&
2376 t
->type
!= TOK_INTERNAL_STRING
)) {
2377 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2378 free_tlist(origline
);
2379 return DIRECTIVE_FOUND
; /* but we did _something_ */
2382 error(ERR_WARNING
|ERR_PASS1
,
2383 "trailing garbage after `%%depend' ignored");
2385 if (t
->type
!= TOK_INTERNAL_STRING
)
2386 nasm_unquote_cstr(p
, i
);
2387 if (dephead
&& !in_list(*dephead
, p
)) {
2388 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2392 deptail
= &sl
->next
;
2394 free_tlist(origline
);
2395 return DIRECTIVE_FOUND
;
2398 t
= tline
->next
= expand_smacro(tline
->next
);
2401 if (!t
|| (t
->type
!= TOK_STRING
&&
2402 t
->type
!= TOK_INTERNAL_STRING
)) {
2403 error(ERR_NONFATAL
, "`%%include' expects a file name");
2404 free_tlist(origline
);
2405 return DIRECTIVE_FOUND
; /* but we did _something_ */
2408 error(ERR_WARNING
|ERR_PASS1
,
2409 "trailing garbage after `%%include' ignored");
2411 if (t
->type
!= TOK_INTERNAL_STRING
)
2412 nasm_unquote_cstr(p
, i
);
2413 inc
= nasm_malloc(sizeof(Include
));
2416 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2418 /* -MG given but file not found */
2421 inc
->fname
= src_set_fname(nasm_strdup(p
));
2422 inc
->lineno
= src_set_linnum(0);
2424 inc
->expansion
= NULL
;
2427 list
->uplevel(LIST_INCLUDE
);
2429 free_tlist(origline
);
2430 return DIRECTIVE_FOUND
;
2434 static macros_t
*use_pkg
;
2435 const char *pkg_macro
= NULL
;
2437 tline
= tline
->next
;
2439 tline
= expand_id(tline
);
2441 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2442 tline
->type
!= TOK_INTERNAL_STRING
&&
2443 tline
->type
!= TOK_ID
)) {
2444 error(ERR_NONFATAL
, "`%%use' expects a package name");
2445 free_tlist(origline
);
2446 return DIRECTIVE_FOUND
; /* but we did _something_ */
2449 error(ERR_WARNING
|ERR_PASS1
,
2450 "trailing garbage after `%%use' ignored");
2451 if (tline
->type
== TOK_STRING
)
2452 nasm_unquote_cstr(tline
->text
, i
);
2453 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2455 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2457 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2458 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2459 /* Not already included, go ahead and include it */
2460 stdmacpos
= use_pkg
;
2462 free_tlist(origline
);
2463 return DIRECTIVE_FOUND
;
2468 tline
= tline
->next
;
2470 tline
= expand_id(tline
);
2472 if (!tok_type_(tline
, TOK_ID
)) {
2473 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2475 free_tlist(origline
);
2476 return DIRECTIVE_FOUND
; /* but we did _something_ */
2479 error(ERR_WARNING
|ERR_PASS1
,
2480 "trailing garbage after `%s' ignored",
2482 p
= nasm_strdup(tline
->text
);
2484 p
= NULL
; /* Anonymous */
2488 ctx
= nasm_malloc(sizeof(Context
));
2490 hash_init(&ctx
->localmac
, HASH_SMALL
);
2492 ctx
->number
= unique
++;
2497 error(ERR_NONFATAL
, "`%s': context stack is empty",
2499 } else if (i
== PP_POP
) {
2500 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2501 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2503 cstk
->name
? cstk
->name
: "anonymous", p
);
2508 nasm_free(cstk
->name
);
2514 free_tlist(origline
);
2515 return DIRECTIVE_FOUND
;
2517 severity
= ERR_FATAL
;
2520 severity
= ERR_NONFATAL
;
2523 severity
= ERR_WARNING
|ERR_WARN_USER
;
2528 /* Only error out if this is the final pass */
2529 if (pass
!= 2 && i
!= PP_FATAL
)
2530 return DIRECTIVE_FOUND
;
2532 tline
->next
= expand_smacro(tline
->next
);
2533 tline
= tline
->next
;
2535 t
= tline
? tline
->next
: NULL
;
2537 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2538 /* The line contains only a quoted string */
2540 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2541 error(severity
, "%s", p
);
2543 /* Not a quoted string, or more than a quoted string */
2544 p
= detoken(tline
, false);
2545 error(severity
, "%s", p
);
2548 free_tlist(origline
);
2549 return DIRECTIVE_FOUND
;
2553 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2556 j
= if_condition(tline
->next
, i
);
2557 tline
->next
= NULL
; /* it got freed */
2558 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2560 cond
= nasm_malloc(sizeof(Cond
));
2561 cond
->next
= istk
->conds
;
2565 istk
->mstk
->condcnt
++;
2566 free_tlist(origline
);
2567 return DIRECTIVE_FOUND
;
2571 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2572 switch(istk
->conds
->state
) {
2574 istk
->conds
->state
= COND_DONE
;
2581 case COND_ELSE_TRUE
:
2582 case COND_ELSE_FALSE
:
2583 error_precond(ERR_WARNING
|ERR_PASS1
,
2584 "`%%elif' after `%%else' ignored");
2585 istk
->conds
->state
= COND_NEVER
;
2590 * IMPORTANT: In the case of %if, we will already have
2591 * called expand_mmac_params(); however, if we're
2592 * processing an %elif we must have been in a
2593 * non-emitting mode, which would have inhibited
2594 * the normal invocation of expand_mmac_params().
2595 * Therefore, we have to do it explicitly here.
2597 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2598 tline
->next
= NULL
; /* it got freed */
2599 istk
->conds
->state
=
2600 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2603 free_tlist(origline
);
2604 return DIRECTIVE_FOUND
;
2608 error_precond(ERR_WARNING
|ERR_PASS1
,
2609 "trailing garbage after `%%else' ignored");
2611 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2612 switch(istk
->conds
->state
) {
2615 istk
->conds
->state
= COND_ELSE_FALSE
;
2622 istk
->conds
->state
= COND_ELSE_TRUE
;
2625 case COND_ELSE_TRUE
:
2626 case COND_ELSE_FALSE
:
2627 error_precond(ERR_WARNING
|ERR_PASS1
,
2628 "`%%else' after `%%else' ignored.");
2629 istk
->conds
->state
= COND_NEVER
;
2632 free_tlist(origline
);
2633 return DIRECTIVE_FOUND
;
2637 error_precond(ERR_WARNING
|ERR_PASS1
,
2638 "trailing garbage after `%%endif' ignored");
2640 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2642 istk
->conds
= cond
->next
;
2645 istk
->mstk
->condcnt
--;
2646 free_tlist(origline
);
2647 return DIRECTIVE_FOUND
;
2654 error(ERR_FATAL
, "`%s': already defining a macro",
2656 return DIRECTIVE_FOUND
;
2658 defining
= nasm_malloc(sizeof(MMacro
));
2659 defining
->max_depth
=
2660 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2661 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2662 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2663 nasm_free(defining
);
2665 return DIRECTIVE_FOUND
;
2668 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2670 if (!strcmp(mmac
->name
, defining
->name
) &&
2671 (mmac
->nparam_min
<= defining
->nparam_max
2673 && (defining
->nparam_min
<= mmac
->nparam_max
2675 error(ERR_WARNING
|ERR_PASS1
,
2676 "redefining multi-line macro `%s'", defining
->name
);
2677 return DIRECTIVE_FOUND
;
2681 free_tlist(origline
);
2682 return DIRECTIVE_FOUND
;
2686 if (! (defining
&& defining
->name
)) {
2687 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2688 return DIRECTIVE_FOUND
;
2690 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2691 defining
->next
= *mmhead
;
2694 free_tlist(origline
);
2695 return DIRECTIVE_FOUND
;
2699 * We must search along istk->expansion until we hit a
2700 * macro-end marker for a macro with a name. Then we
2701 * bypass all lines between exitmacro and endmacro.
2703 list_for_each(l
, istk
->expansion
)
2704 if (l
->finishes
&& l
->finishes
->name
)
2709 * Remove all conditional entries relative to this
2710 * macro invocation. (safe to do in this context)
2712 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2714 istk
->conds
= cond
->next
;
2717 istk
->expansion
= l
;
2719 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2721 free_tlist(origline
);
2722 return DIRECTIVE_FOUND
;
2730 spec
.casesense
= (i
== PP_UNMACRO
);
2731 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2732 return DIRECTIVE_FOUND
;
2734 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2735 while (mmac_p
&& *mmac_p
) {
2737 if (mmac
->casesense
== spec
.casesense
&&
2738 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2739 mmac
->nparam_min
== spec
.nparam_min
&&
2740 mmac
->nparam_max
== spec
.nparam_max
&&
2741 mmac
->plus
== spec
.plus
) {
2742 *mmac_p
= mmac
->next
;
2745 mmac_p
= &mmac
->next
;
2748 free_tlist(origline
);
2749 free_tlist(spec
.dlist
);
2750 return DIRECTIVE_FOUND
;
2754 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2755 tline
= tline
->next
;
2757 free_tlist(origline
);
2758 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2759 return DIRECTIVE_FOUND
;
2761 t
= expand_smacro(tline
->next
);
2763 free_tlist(origline
);
2766 tokval
.t_type
= TOKEN_INVALID
;
2768 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2771 return DIRECTIVE_FOUND
;
2773 error(ERR_WARNING
|ERR_PASS1
,
2774 "trailing garbage after expression ignored");
2775 if (!is_simple(evalresult
)) {
2776 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2777 return DIRECTIVE_FOUND
;
2780 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2781 mmac
= mmac
->next_active
;
2783 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2784 } else if (mmac
->nparam
== 0) {
2786 "`%%rotate' invoked within macro without parameters");
2788 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2790 rotate
%= (int)mmac
->nparam
;
2792 rotate
+= mmac
->nparam
;
2794 mmac
->rotate
= rotate
;
2796 return DIRECTIVE_FOUND
;
2801 tline
= tline
->next
;
2802 } while (tok_type_(tline
, TOK_WHITESPACE
));
2804 if (tok_type_(tline
, TOK_ID
) &&
2805 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2808 tline
= tline
->next
;
2809 } while (tok_type_(tline
, TOK_WHITESPACE
));
2813 t
= expand_smacro(tline
);
2815 tokval
.t_type
= TOKEN_INVALID
;
2817 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2819 free_tlist(origline
);
2820 return DIRECTIVE_FOUND
;
2823 error(ERR_WARNING
|ERR_PASS1
,
2824 "trailing garbage after expression ignored");
2825 if (!is_simple(evalresult
)) {
2826 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2827 return DIRECTIVE_FOUND
;
2829 count
= reloc_value(evalresult
) + 1;
2831 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2834 free_tlist(origline
);
2836 tmp_defining
= defining
;
2837 defining
= nasm_malloc(sizeof(MMacro
));
2838 defining
->prev
= NULL
;
2839 defining
->name
= NULL
; /* flags this macro as a %rep block */
2840 defining
->casesense
= false;
2841 defining
->plus
= false;
2842 defining
->nolist
= nolist
;
2843 defining
->in_progress
= count
;
2844 defining
->max_depth
= 0;
2845 defining
->nparam_min
= defining
->nparam_max
= 0;
2846 defining
->defaults
= NULL
;
2847 defining
->dlist
= NULL
;
2848 defining
->expansion
= NULL
;
2849 defining
->next_active
= istk
->mstk
;
2850 defining
->rep_nest
= tmp_defining
;
2851 return DIRECTIVE_FOUND
;
2854 if (!defining
|| defining
->name
) {
2855 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2856 return DIRECTIVE_FOUND
;
2860 * Now we have a "macro" defined - although it has no name
2861 * and we won't be entering it in the hash tables - we must
2862 * push a macro-end marker for it on to istk->expansion.
2863 * After that, it will take care of propagating itself (a
2864 * macro-end marker line for a macro which is really a %rep
2865 * block will cause the macro to be re-expanded, complete
2866 * with another macro-end marker to ensure the process
2867 * continues) until the whole expansion is forcibly removed
2868 * from istk->expansion by a %exitrep.
2870 l
= nasm_malloc(sizeof(Line
));
2871 l
->next
= istk
->expansion
;
2872 l
->finishes
= defining
;
2874 istk
->expansion
= l
;
2876 istk
->mstk
= defining
;
2878 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2879 tmp_defining
= defining
;
2880 defining
= defining
->rep_nest
;
2881 free_tlist(origline
);
2882 return DIRECTIVE_FOUND
;
2886 * We must search along istk->expansion until we hit a
2887 * macro-end marker for a macro with no name. Then we set
2888 * its `in_progress' flag to 0.
2890 list_for_each(l
, istk
->expansion
)
2891 if (l
->finishes
&& !l
->finishes
->name
)
2895 l
->finishes
->in_progress
= 1;
2897 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2898 free_tlist(origline
);
2899 return DIRECTIVE_FOUND
;
2905 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2907 tline
= tline
->next
;
2909 tline
= expand_id(tline
);
2910 if (!tline
|| (tline
->type
!= TOK_ID
&&
2911 (tline
->type
!= TOK_PREPROC_ID
||
2912 tline
->text
[1] != '$'))) {
2913 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2915 free_tlist(origline
);
2916 return DIRECTIVE_FOUND
;
2919 ctx
= get_ctx(tline
->text
, &mname
, false);
2921 param_start
= tline
= tline
->next
;
2924 /* Expand the macro definition now for %xdefine and %ixdefine */
2925 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2926 tline
= expand_smacro(tline
);
2928 if (tok_is_(tline
, "(")) {
2930 * This macro has parameters.
2933 tline
= tline
->next
;
2937 error(ERR_NONFATAL
, "parameter identifier expected");
2938 free_tlist(origline
);
2939 return DIRECTIVE_FOUND
;
2941 if (tline
->type
!= TOK_ID
) {
2943 "`%s': parameter identifier expected",
2945 free_tlist(origline
);
2946 return DIRECTIVE_FOUND
;
2948 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2949 tline
= tline
->next
;
2951 if (tok_is_(tline
, ",")) {
2952 tline
= tline
->next
;
2954 if (!tok_is_(tline
, ")")) {
2956 "`)' expected to terminate macro template");
2957 free_tlist(origline
);
2958 return DIRECTIVE_FOUND
;
2964 tline
= tline
->next
;
2966 if (tok_type_(tline
, TOK_WHITESPACE
))
2967 last
= tline
, tline
= tline
->next
;
2972 if (t
->type
== TOK_ID
) {
2973 list_for_each(tt
, param_start
)
2974 if (tt
->type
>= TOK_SMAC_PARAM
&&
2975 !strcmp(tt
->text
, t
->text
))
2979 t
->next
= macro_start
;
2984 * Good. We now have a macro name, a parameter count, and a
2985 * token list (in reverse order) for an expansion. We ought
2986 * to be OK just to create an SMacro, store it, and let
2987 * free_tlist have the rest of the line (which we have
2988 * carefully re-terminated after chopping off the expansion
2991 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2992 free_tlist(origline
);
2993 return DIRECTIVE_FOUND
;
2996 tline
= tline
->next
;
2998 tline
= expand_id(tline
);
2999 if (!tline
|| (tline
->type
!= TOK_ID
&&
3000 (tline
->type
!= TOK_PREPROC_ID
||
3001 tline
->text
[1] != '$'))) {
3002 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3003 free_tlist(origline
);
3004 return DIRECTIVE_FOUND
;
3007 error(ERR_WARNING
|ERR_PASS1
,
3008 "trailing garbage after macro name ignored");
3011 /* Find the context that symbol belongs to */
3012 ctx
= get_ctx(tline
->text
, &mname
, false);
3013 undef_smacro(ctx
, mname
);
3014 free_tlist(origline
);
3015 return DIRECTIVE_FOUND
;
3019 casesense
= (i
== PP_DEFSTR
);
3021 tline
= tline
->next
;
3023 tline
= expand_id(tline
);
3024 if (!tline
|| (tline
->type
!= TOK_ID
&&
3025 (tline
->type
!= TOK_PREPROC_ID
||
3026 tline
->text
[1] != '$'))) {
3027 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3029 free_tlist(origline
);
3030 return DIRECTIVE_FOUND
;
3033 ctx
= get_ctx(tline
->text
, &mname
, false);
3035 tline
= expand_smacro(tline
->next
);
3038 while (tok_type_(tline
, TOK_WHITESPACE
))
3039 tline
= delete_Token(tline
);
3041 p
= detoken(tline
, false);
3042 macro_start
= nasm_malloc(sizeof(*macro_start
));
3043 macro_start
->next
= NULL
;
3044 macro_start
->text
= nasm_quote(p
, strlen(p
));
3045 macro_start
->type
= TOK_STRING
;
3046 macro_start
->a
.mac
= NULL
;
3050 * We now have a macro name, an implicit parameter count of
3051 * zero, and a string token to use as an expansion. Create
3052 * and store an SMacro.
3054 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3055 free_tlist(origline
);
3056 return DIRECTIVE_FOUND
;
3060 casesense
= (i
== PP_DEFTOK
);
3062 tline
= tline
->next
;
3064 tline
= expand_id(tline
);
3065 if (!tline
|| (tline
->type
!= TOK_ID
&&
3066 (tline
->type
!= TOK_PREPROC_ID
||
3067 tline
->text
[1] != '$'))) {
3069 "`%s' expects a macro identifier as first parameter",
3071 free_tlist(origline
);
3072 return DIRECTIVE_FOUND
;
3074 ctx
= get_ctx(tline
->text
, &mname
, false);
3076 tline
= expand_smacro(tline
->next
);
3080 while (tok_type_(t
, TOK_WHITESPACE
))
3082 /* t should now point to the string */
3083 if (t
->type
!= TOK_STRING
) {
3085 "`%s` requires string as second parameter",
3088 free_tlist(origline
);
3089 return DIRECTIVE_FOUND
;
3092 nasm_unquote_cstr(t
->text
, i
);
3093 macro_start
= tokenize(t
->text
);
3096 * We now have a macro name, an implicit parameter count of
3097 * zero, and a numeric token to use as an expansion. Create
3098 * and store an SMacro.
3100 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3102 free_tlist(origline
);
3103 return DIRECTIVE_FOUND
;
3108 StrList
*xsl
= NULL
;
3109 StrList
**xst
= &xsl
;
3113 tline
= tline
->next
;
3115 tline
= expand_id(tline
);
3116 if (!tline
|| (tline
->type
!= TOK_ID
&&
3117 (tline
->type
!= TOK_PREPROC_ID
||
3118 tline
->text
[1] != '$'))) {
3120 "`%%pathsearch' expects a macro identifier as first parameter");
3121 free_tlist(origline
);
3122 return DIRECTIVE_FOUND
;
3124 ctx
= get_ctx(tline
->text
, &mname
, false);
3126 tline
= expand_smacro(tline
->next
);
3130 while (tok_type_(t
, TOK_WHITESPACE
))
3133 if (!t
|| (t
->type
!= TOK_STRING
&&
3134 t
->type
!= TOK_INTERNAL_STRING
)) {
3135 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3137 free_tlist(origline
);
3138 return DIRECTIVE_FOUND
; /* but we did _something_ */
3141 error(ERR_WARNING
|ERR_PASS1
,
3142 "trailing garbage after `%%pathsearch' ignored");
3144 if (t
->type
!= TOK_INTERNAL_STRING
)
3145 nasm_unquote(p
, NULL
);
3147 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3150 fclose(fp
); /* Don't actually care about the file */
3152 macro_start
= nasm_malloc(sizeof(*macro_start
));
3153 macro_start
->next
= NULL
;
3154 macro_start
->text
= nasm_quote(p
, strlen(p
));
3155 macro_start
->type
= TOK_STRING
;
3156 macro_start
->a
.mac
= NULL
;
3161 * We now have a macro name, an implicit parameter count of
3162 * zero, and a string token to use as an expansion. Create
3163 * and store an SMacro.
3165 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3167 free_tlist(origline
);
3168 return DIRECTIVE_FOUND
;
3174 tline
= tline
->next
;
3176 tline
= expand_id(tline
);
3177 if (!tline
|| (tline
->type
!= TOK_ID
&&
3178 (tline
->type
!= TOK_PREPROC_ID
||
3179 tline
->text
[1] != '$'))) {
3181 "`%%strlen' expects a macro identifier as first parameter");
3182 free_tlist(origline
);
3183 return DIRECTIVE_FOUND
;
3185 ctx
= get_ctx(tline
->text
, &mname
, false);
3187 tline
= expand_smacro(tline
->next
);
3191 while (tok_type_(t
, TOK_WHITESPACE
))
3193 /* t should now point to the string */
3194 if (t
->type
!= TOK_STRING
) {
3196 "`%%strlen` requires string as second parameter");
3198 free_tlist(origline
);
3199 return DIRECTIVE_FOUND
;
3202 macro_start
= nasm_malloc(sizeof(*macro_start
));
3203 macro_start
->next
= NULL
;
3204 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3205 macro_start
->a
.mac
= NULL
;
3208 * We now have a macro name, an implicit parameter count of
3209 * zero, and a numeric token to use as an expansion. Create
3210 * and store an SMacro.
3212 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3214 free_tlist(origline
);
3215 return DIRECTIVE_FOUND
;
3220 tline
= tline
->next
;
3222 tline
= expand_id(tline
);
3223 if (!tline
|| (tline
->type
!= TOK_ID
&&
3224 (tline
->type
!= TOK_PREPROC_ID
||
3225 tline
->text
[1] != '$'))) {
3227 "`%%strcat' expects a macro identifier as first parameter");
3228 free_tlist(origline
);
3229 return DIRECTIVE_FOUND
;
3231 ctx
= get_ctx(tline
->text
, &mname
, false);
3233 tline
= expand_smacro(tline
->next
);
3237 list_for_each(t
, tline
) {
3239 case TOK_WHITESPACE
:
3242 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3245 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3247 /* else fall through */
3250 "non-string passed to `%%strcat' (%d)", t
->type
);
3252 free_tlist(origline
);
3253 return DIRECTIVE_FOUND
;
3257 p
= pp
= nasm_malloc(len
);
3258 list_for_each(t
, tline
) {
3259 if (t
->type
== TOK_STRING
) {
3260 memcpy(p
, t
->text
, t
->a
.len
);
3266 * We now have a macro name, an implicit parameter count of
3267 * zero, and a numeric token to use as an expansion. Create
3268 * and store an SMacro.
3270 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3271 macro_start
->text
= nasm_quote(pp
, len
);
3273 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3275 free_tlist(origline
);
3276 return DIRECTIVE_FOUND
;
3285 tline
= tline
->next
;
3287 tline
= expand_id(tline
);
3288 if (!tline
|| (tline
->type
!= TOK_ID
&&
3289 (tline
->type
!= TOK_PREPROC_ID
||
3290 tline
->text
[1] != '$'))) {
3292 "`%%substr' expects a macro identifier as first parameter");
3293 free_tlist(origline
);
3294 return DIRECTIVE_FOUND
;
3296 ctx
= get_ctx(tline
->text
, &mname
, false);
3298 tline
= expand_smacro(tline
->next
);
3302 while (tok_type_(t
, TOK_WHITESPACE
))
3305 /* t should now point to the string */
3306 if (t
->type
!= TOK_STRING
) {
3308 "`%%substr` requires string as second parameter");
3310 free_tlist(origline
);
3311 return DIRECTIVE_FOUND
;
3316 tokval
.t_type
= TOKEN_INVALID
;
3317 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3321 free_tlist(origline
);
3322 return DIRECTIVE_FOUND
;
3323 } else if (!is_simple(evalresult
)) {
3324 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3326 free_tlist(origline
);
3327 return DIRECTIVE_FOUND
;
3329 a1
= evalresult
->value
-1;
3331 while (tok_type_(tt
, TOK_WHITESPACE
))
3334 a2
= 1; /* Backwards compatibility: one character */
3336 tokval
.t_type
= TOKEN_INVALID
;
3337 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3341 free_tlist(origline
);
3342 return DIRECTIVE_FOUND
;
3343 } else if (!is_simple(evalresult
)) {
3344 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3346 free_tlist(origline
);
3347 return DIRECTIVE_FOUND
;
3349 a2
= evalresult
->value
;
3352 len
= nasm_unquote(t
->text
, NULL
);
3355 if (a1
+a2
> (int64_t)len
)
3358 macro_start
= nasm_malloc(sizeof(*macro_start
));
3359 macro_start
->next
= NULL
;
3360 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3361 macro_start
->type
= TOK_STRING
;
3362 macro_start
->a
.mac
= NULL
;
3365 * We now have a macro name, an implicit parameter count of
3366 * zero, and a numeric token to use as an expansion. Create
3367 * and store an SMacro.
3369 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3371 free_tlist(origline
);
3372 return DIRECTIVE_FOUND
;
3377 casesense
= (i
== PP_ASSIGN
);
3379 tline
= tline
->next
;
3381 tline
= expand_id(tline
);
3382 if (!tline
|| (tline
->type
!= TOK_ID
&&
3383 (tline
->type
!= TOK_PREPROC_ID
||
3384 tline
->text
[1] != '$'))) {
3386 "`%%%sassign' expects a macro identifier",
3387 (i
== PP_IASSIGN
? "i" : ""));
3388 free_tlist(origline
);
3389 return DIRECTIVE_FOUND
;
3391 ctx
= get_ctx(tline
->text
, &mname
, false);
3393 tline
= expand_smacro(tline
->next
);
3398 tokval
.t_type
= TOKEN_INVALID
;
3400 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3403 free_tlist(origline
);
3404 return DIRECTIVE_FOUND
;
3408 error(ERR_WARNING
|ERR_PASS1
,
3409 "trailing garbage after expression ignored");
3411 if (!is_simple(evalresult
)) {
3413 "non-constant value given to `%%%sassign'",
3414 (i
== PP_IASSIGN
? "i" : ""));
3415 free_tlist(origline
);
3416 return DIRECTIVE_FOUND
;
3419 macro_start
= nasm_malloc(sizeof(*macro_start
));
3420 macro_start
->next
= NULL
;
3421 make_tok_num(macro_start
, reloc_value(evalresult
));
3422 macro_start
->a
.mac
= NULL
;
3425 * We now have a macro name, an implicit parameter count of
3426 * zero, and a numeric token to use as an expansion. Create
3427 * and store an SMacro.
3429 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3430 free_tlist(origline
);
3431 return DIRECTIVE_FOUND
;
3435 * Syntax is `%line nnn[+mmm] [filename]'
3437 tline
= tline
->next
;
3439 if (!tok_type_(tline
, TOK_NUMBER
)) {
3440 error(ERR_NONFATAL
, "`%%line' expects line number");
3441 free_tlist(origline
);
3442 return DIRECTIVE_FOUND
;
3444 k
= readnum(tline
->text
, &err
);
3446 tline
= tline
->next
;
3447 if (tok_is_(tline
, "+")) {
3448 tline
= tline
->next
;
3449 if (!tok_type_(tline
, TOK_NUMBER
)) {
3450 error(ERR_NONFATAL
, "`%%line' expects line increment");
3451 free_tlist(origline
);
3452 return DIRECTIVE_FOUND
;
3454 m
= readnum(tline
->text
, &err
);
3455 tline
= tline
->next
;
3461 nasm_free(src_set_fname(detoken(tline
, false)));
3463 free_tlist(origline
);
3464 return DIRECTIVE_FOUND
;
3468 "preprocessor directive `%s' not yet implemented",
3470 return DIRECTIVE_FOUND
;
3475 * Ensure that a macro parameter contains a condition code and
3476 * nothing else. Return the condition code index if so, or -1
3479 static int find_cc(Token
* t
)
3485 return -1; /* Probably a %+ without a space */
3488 if (t
->type
!= TOK_ID
)
3492 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3496 j
= ARRAY_SIZE(conditions
);
3499 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3514 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3516 Token
**tail
, *t
, *tt
;
3518 bool did_paste
= false;
3521 /* Now handle token pasting... */
3524 while ((t
= *tail
) && (tt
= t
->next
)) {
3526 case TOK_WHITESPACE
:
3527 if (tt
->type
== TOK_WHITESPACE
) {
3528 /* Zap adjacent whitespace tokens */
3529 t
->next
= delete_Token(tt
);
3531 /* Do not advance paste_head here */
3542 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3543 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3544 tt
->type
== TOK_OTHER
)) {
3545 len
+= strlen(tt
->text
);
3550 * Now tt points to the first token after
3551 * the potential paste area...
3553 if (tt
!= t
->next
) {
3554 /* We have at least two tokens... */
3555 len
+= strlen(t
->text
);
3556 p
= tmp
= nasm_malloc(len
+1);
3560 p
= strchr(p
, '\0');
3561 t
= delete_Token(t
);
3564 t
= *tail
= tokenize(tmp
);
3571 t
->next
= tt
; /* Attach the remaining token chain */
3579 case TOK_PASTE
: /* %+ */
3580 if (handle_paste_tokens
) {
3581 /* Zap %+ and whitespace tokens to the right */
3582 while (t
&& (t
->type
== TOK_WHITESPACE
||
3583 t
->type
== TOK_PASTE
))
3584 t
= *tail
= delete_Token(t
);
3585 if (!paste_head
|| !t
)
3586 break; /* Nothing to paste with */
3590 while (tok_type_(tt
, TOK_WHITESPACE
))
3591 tt
= t
->next
= delete_Token(tt
);
3594 tmp
= nasm_strcat(t
->text
, tt
->text
);
3596 tt
= delete_Token(tt
);
3597 t
= *tail
= tokenize(tmp
);
3603 t
->next
= tt
; /* Attach the remaining token chain */
3610 /* else fall through */
3613 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3622 * expands to a list of tokens from %{x:y}
3624 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3626 Token
*t
= tline
, **tt
, *tm
, *head
;
3630 pos
= strchr(tline
->text
, ':');
3633 lst
= atoi(pos
+ 1);
3634 fst
= atoi(tline
->text
+ 1);
3637 * only macros params are accounted so
3638 * if someone passes %0 -- we reject such
3641 if (lst
== 0 || fst
== 0)
3644 /* the values should be sane */
3645 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3646 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3649 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3650 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3652 /* counted from zero */
3656 * it will be at least one token
3658 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3659 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3660 head
= t
, tt
= &t
->next
;
3662 for (i
= fst
+ 1; i
<= lst
; i
++) {
3663 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3664 *tt
= t
, tt
= &t
->next
;
3665 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3666 tm
= mac
->params
[j
];
3667 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3668 *tt
= t
, tt
= &t
->next
;
3671 for (i
= fst
- 1; i
>= lst
; i
--) {
3672 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3673 *tt
= t
, tt
= &t
->next
;
3674 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3675 tm
= mac
->params
[j
];
3676 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3677 *tt
= t
, tt
= &t
->next
;
3685 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3691 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3692 * %-n) and MMacro-local identifiers (%%foo) as well as
3693 * macro indirection (%[...]) and range (%{..:..}).
3695 static Token
*expand_mmac_params(Token
* tline
)
3697 Token
*t
, *tt
, **tail
, *thead
;
3698 bool changed
= false;
3705 if (tline
->type
== TOK_PREPROC_ID
&&
3706 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3707 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3708 tline
->text
[1] == '%')) {
3710 int type
= 0, cc
; /* type = 0 to placate optimisers */
3717 tline
= tline
->next
;
3720 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3721 mac
= mac
->next_active
;
3723 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3725 pos
= strchr(t
->text
, ':');
3727 switch (t
->text
[1]) {
3729 * We have to make a substitution of one of the
3730 * forms %1, %-1, %+1, %%foo, %0.
3734 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3735 text
= nasm_strdup(tmpbuf
);
3739 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3741 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3744 n
= atoi(t
->text
+ 2) - 1;
3745 if (n
>= mac
->nparam
)
3748 if (mac
->nparam
> 1)
3749 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3750 tt
= mac
->params
[n
];
3755 "macro parameter %d is not a condition code",
3760 if (inverse_ccs
[cc
] == -1) {
3762 "condition code `%s' is not invertible",
3766 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3770 n
= atoi(t
->text
+ 2) - 1;
3771 if (n
>= mac
->nparam
)
3774 if (mac
->nparam
> 1)
3775 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3776 tt
= mac
->params
[n
];
3781 "macro parameter %d is not a condition code",
3786 text
= nasm_strdup(conditions
[cc
]);
3790 n
= atoi(t
->text
+ 1) - 1;
3791 if (n
>= mac
->nparam
)
3794 if (mac
->nparam
> 1)
3795 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3796 tt
= mac
->params
[n
];
3799 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3800 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3801 tail
= &(*tail
)->next
;
3805 text
= NULL
; /* we've done it here */
3810 * seems we have a parameters range here
3812 Token
*head
, **last
;
3813 head
= expand_mmac_params_range(mac
, t
, &last
);
3834 } else if (tline
->type
== TOK_INDIRECT
) {
3836 tline
= tline
->next
;
3837 tt
= tokenize(t
->text
);
3838 tt
= expand_mmac_params(tt
);
3839 tt
= expand_smacro(tt
);
3842 tt
->a
.mac
= NULL
; /* Necessary? */
3850 tline
= tline
->next
;
3858 paste_tokens(&thead
, false);
3864 * Expand all single-line macro calls made in the given line.
3865 * Return the expanded version of the line. The original is deemed
3866 * to be destroyed in the process. (In reality we'll just move
3867 * Tokens from input to output a lot of the time, rather than
3868 * actually bothering to destroy and replicate.)
3871 static Token
*expand_smacro(Token
* tline
)
3873 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3874 SMacro
*head
= NULL
, *m
;
3877 unsigned int nparam
, sparam
;
3879 Token
*org_tline
= tline
;
3882 int deadman
= DEADMAN_LIMIT
;
3886 * Trick: we should avoid changing the start token pointer since it can
3887 * be contained in "next" field of other token. Because of this
3888 * we allocate a copy of first token and work with it; at the end of
3889 * routine we copy it back
3892 tline
= new_Token(org_tline
->next
, org_tline
->type
,
3893 org_tline
->text
, 0);
3894 tline
->a
.mac
= org_tline
->a
.mac
;
3895 nasm_free(org_tline
->text
);
3896 org_tline
->text
= NULL
;
3899 expanded
= true; /* Always expand %+ at least once */
3905 while (tline
) { /* main token loop */
3907 error(ERR_NONFATAL
, "interminable macro recursion");
3911 if ((mname
= tline
->text
)) {
3912 /* if this token is a local macro, look in local context */
3913 if (tline
->type
== TOK_ID
) {
3914 head
= (SMacro
*)hash_findix(&smacros
, mname
);
3915 } else if (tline
->type
== TOK_PREPROC_ID
) {
3916 ctx
= get_ctx(mname
, &mname
, true);
3917 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
3922 * We've hit an identifier. As in is_mmacro below, we first
3923 * check whether the identifier is a single-line macro at
3924 * all, then think about checking for parameters if
3927 list_for_each(m
, head
)
3928 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3934 if (m
->nparam
== 0) {
3936 * Simple case: the macro is parameterless. Discard the
3937 * one token that the macro call took, and push the
3938 * expansion back on the to-do stack.
3940 if (!m
->expansion
) {
3941 if (!strcmp("__FILE__", m
->name
)) {
3944 src_get(&num
, &file
);
3945 tline
->text
= nasm_quote(file
, strlen(file
));
3946 tline
->type
= TOK_STRING
;
3950 if (!strcmp("__LINE__", m
->name
)) {
3951 nasm_free(tline
->text
);
3952 make_tok_num(tline
, src_get_linnum());
3955 if (!strcmp("__BITS__", m
->name
)) {
3956 nasm_free(tline
->text
);
3957 make_tok_num(tline
, globalbits
);
3960 tline
= delete_Token(tline
);
3965 * Complicated case: at least one macro with this name
3966 * exists and takes parameters. We must find the
3967 * parameters in the call, count them, find the SMacro
3968 * that corresponds to that form of the macro call, and
3969 * substitute for the parameters when we expand. What a
3972 /*tline = tline->next;
3973 skip_white_(tline); */
3976 while (tok_type_(t
, TOK_SMAC_END
)) {
3977 t
->a
.mac
->in_progress
= false;
3979 t
= tline
->next
= delete_Token(t
);
3982 } while (tok_type_(tline
, TOK_WHITESPACE
));
3983 if (!tok_is_(tline
, "(")) {
3985 * This macro wasn't called with parameters: ignore
3986 * the call. (Behaviour borrowed from gnu cpp.)
3995 sparam
= PARAM_DELTA
;
3996 params
= nasm_malloc(sparam
* sizeof(Token
*));
3997 params
[0] = tline
->next
;
3998 paramsize
= nasm_malloc(sparam
* sizeof(int));
4000 while (true) { /* parameter loop */
4002 * For some unusual expansions
4003 * which concatenates function call
4006 while (tok_type_(t
, TOK_SMAC_END
)) {
4007 t
->a
.mac
->in_progress
= false;
4009 t
= tline
->next
= delete_Token(t
);
4015 "macro call expects terminating `)'");
4018 if (tline
->type
== TOK_WHITESPACE
4020 if (paramsize
[nparam
])
4023 params
[nparam
] = tline
->next
;
4024 continue; /* parameter loop */
4026 if (tline
->type
== TOK_OTHER
4027 && tline
->text
[1] == 0) {
4028 char ch
= tline
->text
[0];
4029 if (ch
== ',' && !paren
&& brackets
<= 0) {
4030 if (++nparam
>= sparam
) {
4031 sparam
+= PARAM_DELTA
;
4032 params
= nasm_realloc(params
,
4033 sparam
* sizeof(Token
*));
4034 paramsize
= nasm_realloc(paramsize
,
4035 sparam
* sizeof(int));
4037 params
[nparam
] = tline
->next
;
4038 paramsize
[nparam
] = 0;
4040 continue; /* parameter loop */
4043 (brackets
> 0 || (brackets
== 0 &&
4044 !paramsize
[nparam
])))
4046 if (!(brackets
++)) {
4047 params
[nparam
] = tline
->next
;
4048 continue; /* parameter loop */
4051 if (ch
== '}' && brackets
> 0)
4052 if (--brackets
== 0) {
4054 continue; /* parameter loop */
4056 if (ch
== '(' && !brackets
)
4058 if (ch
== ')' && brackets
<= 0)
4064 error(ERR_NONFATAL
, "braces do not "
4065 "enclose all of macro parameter");
4067 paramsize
[nparam
] += white
+ 1;
4069 } /* parameter loop */
4071 while (m
&& (m
->nparam
!= nparam
||
4072 mstrcmp(m
->name
, mname
,
4076 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4077 "macro `%s' exists, "
4078 "but not taking %d parameters",
4079 mstart
->text
, nparam
);
4082 if (m
&& m
->in_progress
)
4084 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4086 * Design question: should we handle !tline, which
4087 * indicates missing ')' here, or expand those
4088 * macros anyway, which requires the (t) test a few
4092 nasm_free(paramsize
);
4096 * Expand the macro: we are placed on the last token of the
4097 * call, so that we can easily split the call from the
4098 * following tokens. We also start by pushing an SMAC_END
4099 * token for the cycle removal.
4106 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4108 m
->in_progress
= true;
4110 list_for_each(t
, m
->expansion
) {
4111 if (t
->type
>= TOK_SMAC_PARAM
) {
4112 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4116 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4117 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4119 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4125 } else if (t
->type
== TOK_PREPROC_Q
) {
4126 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4128 } else if (t
->type
== TOK_PREPROC_QQ
) {
4129 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4132 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4138 * Having done that, get rid of the macro call, and clean
4139 * up the parameters.
4142 nasm_free(paramsize
);
4145 continue; /* main token loop */
4150 if (tline
->type
== TOK_SMAC_END
) {
4151 tline
->a
.mac
->in_progress
= false;
4152 tline
= delete_Token(tline
);
4155 tline
= tline
->next
;
4163 * Now scan the entire line and look for successive TOK_IDs that resulted
4164 * after expansion (they can't be produced by tokenize()). The successive
4165 * TOK_IDs should be concatenated.
4166 * Also we look for %+ tokens and concatenate the tokens before and after
4167 * them (without white spaces in between).
4169 if (expanded
&& paste_tokens(&thead
, true)) {
4171 * If we concatenated something, *and* we had previously expanded
4172 * an actual macro, scan the lines again for macros...
4182 *org_tline
= *thead
;
4183 /* since we just gave text to org_line, don't free it */
4185 delete_Token(thead
);
4187 /* the expression expanded to empty line;
4188 we can't return NULL for some reasons
4189 we just set the line to a single WHITESPACE token. */
4190 memset(org_tline
, 0, sizeof(*org_tline
));
4191 org_tline
->text
= NULL
;
4192 org_tline
->type
= TOK_WHITESPACE
;
4201 * Similar to expand_smacro but used exclusively with macro identifiers
4202 * right before they are fetched in. The reason is that there can be
4203 * identifiers consisting of several subparts. We consider that if there
4204 * are more than one element forming the name, user wants a expansion,
4205 * otherwise it will be left as-is. Example:
4209 * the identifier %$abc will be left as-is so that the handler for %define
4210 * will suck it and define the corresponding value. Other case:
4212 * %define _%$abc cde
4214 * In this case user wants name to be expanded *before* %define starts
4215 * working, so we'll expand %$abc into something (if it has a value;
4216 * otherwise it will be left as-is) then concatenate all successive
4219 static Token
*expand_id(Token
* tline
)
4221 Token
*cur
, *oldnext
= NULL
;
4223 if (!tline
|| !tline
->next
)
4228 (cur
->next
->type
== TOK_ID
||
4229 cur
->next
->type
== TOK_PREPROC_ID
4230 || cur
->next
->type
== TOK_NUMBER
))
4233 /* If identifier consists of just one token, don't expand */
4238 oldnext
= cur
->next
; /* Detach the tail past identifier */
4239 cur
->next
= NULL
; /* so that expand_smacro stops here */
4242 tline
= expand_smacro(tline
);
4245 /* expand_smacro possibly changhed tline; re-scan for EOL */
4247 while (cur
&& cur
->next
)
4250 cur
->next
= oldnext
;
4257 * Determine whether the given line constitutes a multi-line macro
4258 * call, and return the MMacro structure called if so. Doesn't have
4259 * to check for an initial label - that's taken care of in
4260 * expand_mmacro - but must check numbers of parameters. Guaranteed
4261 * to be called with tline->type == TOK_ID, so the putative macro
4262 * name is easy to find.
4264 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4270 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4273 * Efficiency: first we see if any macro exists with the given
4274 * name. If not, we can return NULL immediately. _Then_ we
4275 * count the parameters, and then we look further along the
4276 * list if necessary to find the proper MMacro.
4278 list_for_each(m
, head
)
4279 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4285 * OK, we have a potential macro. Count and demarcate the
4288 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4291 * So we know how many parameters we've got. Find the MMacro
4292 * structure that handles this number.
4295 if (m
->nparam_min
<= nparam
4296 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4298 * This one is right. Just check if cycle removal
4299 * prohibits us using it before we actually celebrate...
4301 if (m
->in_progress
> m
->max_depth
) {
4302 if (m
->max_depth
> 0) {
4304 "reached maximum recursion depth of %i",
4311 * It's right, and we can use it. Add its default
4312 * parameters to the end of our list if necessary.
4314 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4316 nasm_realloc(params
,
4317 ((m
->nparam_min
+ m
->ndefs
+
4318 1) * sizeof(*params
)));
4319 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4320 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4325 * If we've gone over the maximum parameter count (and
4326 * we're in Plus mode), ignore parameters beyond
4329 if (m
->plus
&& nparam
> m
->nparam_max
)
4330 nparam
= m
->nparam_max
;
4332 * Then terminate the parameter list, and leave.
4334 if (!params
) { /* need this special case */
4335 params
= nasm_malloc(sizeof(*params
));
4338 params
[nparam
] = NULL
;
4339 *params_array
= params
;
4343 * This one wasn't right: look for the next one with the
4346 list_for_each(m
, m
->next
)
4347 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4352 * After all that, we didn't find one with the right number of
4353 * parameters. Issue a warning, and fail to expand the macro.
4355 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4356 "macro `%s' exists, but not taking %d parameters",
4357 tline
->text
, nparam
);
4364 * Save MMacro invocation specific fields in
4365 * preparation for a recursive macro expansion
4367 static void push_mmacro(MMacro
*m
)
4369 MMacroInvocation
*i
;
4371 i
= nasm_malloc(sizeof(MMacroInvocation
));
4373 i
->params
= m
->params
;
4374 i
->iline
= m
->iline
;
4375 i
->nparam
= m
->nparam
;
4376 i
->rotate
= m
->rotate
;
4377 i
->paramlen
= m
->paramlen
;
4378 i
->unique
= m
->unique
;
4379 i
->condcnt
= m
->condcnt
;
4385 * Restore MMacro invocation specific fields that were
4386 * saved during a previous recursive macro expansion
4388 static void pop_mmacro(MMacro
*m
)
4390 MMacroInvocation
*i
;
4395 m
->params
= i
->params
;
4396 m
->iline
= i
->iline
;
4397 m
->nparam
= i
->nparam
;
4398 m
->rotate
= i
->rotate
;
4399 m
->paramlen
= i
->paramlen
;
4400 m
->unique
= i
->unique
;
4401 m
->condcnt
= i
->condcnt
;
4408 * Expand the multi-line macro call made by the given line, if
4409 * there is one to be expanded. If there is, push the expansion on
4410 * istk->expansion and return 1. Otherwise return 0.
4412 static int expand_mmacro(Token
* tline
)
4414 Token
*startline
= tline
;
4415 Token
*label
= NULL
;
4416 int dont_prepend
= 0;
4417 Token
**params
, *t
, *mtok
, *tt
;
4420 int i
, nparam
, *paramlen
;
4425 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4426 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4429 m
= is_mmacro(t
, ¶ms
);
4435 * We have an id which isn't a macro call. We'll assume
4436 * it might be a label; we'll also check to see if a
4437 * colon follows it. Then, if there's another id after
4438 * that lot, we'll check it again for macro-hood.
4442 if (tok_type_(t
, TOK_WHITESPACE
))
4443 last
= t
, t
= t
->next
;
4444 if (tok_is_(t
, ":")) {
4446 last
= t
, t
= t
->next
;
4447 if (tok_type_(t
, TOK_WHITESPACE
))
4448 last
= t
, t
= t
->next
;
4450 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4458 * Fix up the parameters: this involves stripping leading and
4459 * trailing whitespace, then stripping braces if they are
4462 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4463 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4465 for (i
= 0; params
[i
]; i
++) {
4467 int comma
= (!m
->plus
|| i
< nparam
- 1);
4471 if (tok_is_(t
, "{"))
4472 t
= t
->next
, brace
= true, comma
= false;
4476 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4477 break; /* ... because we have hit a comma */
4478 if (comma
&& t
->type
== TOK_WHITESPACE
4479 && tok_is_(t
->next
, ","))
4480 break; /* ... or a space then a comma */
4481 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4482 break; /* ... or a brace */
4489 * OK, we have a MMacro structure together with a set of
4490 * parameters. We must now go through the expansion and push
4491 * copies of each Line on to istk->expansion. Substitution of
4492 * parameter tokens and macro-local tokens doesn't get done
4493 * until the single-line macro substitution process; this is
4494 * because delaying them allows us to change the semantics
4495 * later through %rotate.
4497 * First, push an end marker on to istk->expansion, mark this
4498 * macro as in progress, and set up its invocation-specific
4501 ll
= nasm_malloc(sizeof(Line
));
4502 ll
->next
= istk
->expansion
;
4505 istk
->expansion
= ll
;
4508 * Save the previous MMacro expansion in the case of
4511 if (m
->max_depth
&& m
->in_progress
)
4519 m
->paramlen
= paramlen
;
4520 m
->unique
= unique
++;
4524 m
->next_active
= istk
->mstk
;
4527 list_for_each(l
, m
->expansion
) {
4530 ll
= nasm_malloc(sizeof(Line
));
4531 ll
->finishes
= NULL
;
4532 ll
->next
= istk
->expansion
;
4533 istk
->expansion
= ll
;
4536 list_for_each(t
, l
->first
) {
4540 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4542 case TOK_PREPROC_QQ
:
4543 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4545 case TOK_PREPROC_ID
:
4546 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4554 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4563 * If we had a label, push it on as the first line of
4564 * the macro expansion.
4567 if (dont_prepend
< 0)
4568 free_tlist(startline
);
4570 ll
= nasm_malloc(sizeof(Line
));
4571 ll
->finishes
= NULL
;
4572 ll
->next
= istk
->expansion
;
4573 istk
->expansion
= ll
;
4574 ll
->first
= startline
;
4575 if (!dont_prepend
) {
4577 label
= label
->next
;
4578 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4583 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4588 /* The function that actually does the error reporting */
4589 static void verror(int severity
, const char *fmt
, va_list arg
)
4593 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4595 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4596 nasm_error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4597 istk
->mstk
->lineno
, buff
);
4599 nasm_error(severity
, "%s", buff
);
4603 * Since preprocessor always operate only on the line that didn't
4604 * arrived yet, we should always use ERR_OFFBY1.
4606 static void error(int severity
, const char *fmt
, ...)
4610 /* If we're in a dead branch of IF or something like it, ignore the error */
4611 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4615 verror(severity
, fmt
, arg
);
4620 * Because %else etc are evaluated in the state context
4621 * of the previous branch, errors might get lost with error():
4622 * %if 0 ... %else trailing garbage ... %endif
4623 * So %else etc should report errors with this function.
4625 static void error_precond(int severity
, const char *fmt
, ...)
4629 /* Only ignore the error if it's really in a dead branch */
4630 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4634 verror(severity
, fmt
, arg
);
4639 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4644 istk
= nasm_malloc(sizeof(Include
));
4647 istk
->expansion
= NULL
;
4649 istk
->fp
= fopen(file
, "r");
4651 src_set_fname(nasm_strdup(file
));
4655 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4658 nested_mac_count
= 0;
4659 nested_rep_count
= 0;
4662 if (tasm_compatible_mode
) {
4663 stdmacpos
= nasm_stdmac
;
4665 stdmacpos
= nasm_stdmac_after_tasm
;
4667 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4672 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4673 * The caller, however, will also pass in 3 for preprocess-only so
4674 * we can set __PASS__ accordingly.
4676 pass
= apass
> 2 ? 2 : apass
;
4678 dephead
= deptail
= deplist
;
4680 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4682 strcpy(sl
->str
, file
);
4684 deptail
= &sl
->next
;
4688 * Define the __PASS__ macro. This is defined here unlike
4689 * all the other builtins, because it is special -- it varies between
4692 t
= nasm_malloc(sizeof(*t
));
4694 make_tok_num(t
, apass
);
4696 define_smacro(NULL
, "__PASS__", true, 0, t
);
4699 static char *pp_getline(void)
4706 * Fetch a tokenized line, either from the macro-expansion
4707 * buffer or from the input file.
4710 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4711 Line
*l
= istk
->expansion
;
4712 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4716 * This is a macro-end marker for a macro with no
4717 * name, which means it's not really a macro at all
4718 * but a %rep block, and the `in_progress' field is
4719 * more than 1, meaning that we still need to
4720 * repeat. (1 means the natural last repetition; 0
4721 * means termination by %exitrep.) We have
4722 * therefore expanded up to the %endrep, and must
4723 * push the whole block on to the expansion buffer
4724 * again. We don't bother to remove the macro-end
4725 * marker: we'd only have to generate another one
4728 l
->finishes
->in_progress
--;
4729 list_for_each(l
, l
->finishes
->expansion
) {
4730 Token
*t
, *tt
, **tail
;
4732 ll
= nasm_malloc(sizeof(Line
));
4733 ll
->next
= istk
->expansion
;
4734 ll
->finishes
= NULL
;
4738 list_for_each(t
, l
->first
) {
4739 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4740 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4745 istk
->expansion
= ll
;
4749 * Check whether a `%rep' was started and not ended
4750 * within this macro expansion. This can happen and
4751 * should be detected. It's a fatal error because
4752 * I'm too confused to work out how to recover
4758 "defining with name in expansion");
4759 else if (istk
->mstk
->name
)
4761 "`%%rep' without `%%endrep' within"
4762 " expansion of macro `%s'",
4767 * FIXME: investigate the relationship at this point between
4768 * istk->mstk and l->finishes
4771 MMacro
*m
= istk
->mstk
;
4772 istk
->mstk
= m
->next_active
;
4775 * This was a real macro call, not a %rep, and
4776 * therefore the parameter information needs to
4781 l
->finishes
->in_progress
--;
4783 nasm_free(m
->params
);
4784 free_tlist(m
->iline
);
4785 nasm_free(m
->paramlen
);
4786 l
->finishes
->in_progress
= 0;
4791 istk
->expansion
= l
->next
;
4793 list
->downlevel(LIST_MACRO
);
4796 while (1) { /* until we get a line we can use */
4798 if (istk
->expansion
) { /* from a macro expansion */
4800 Line
*l
= istk
->expansion
;
4802 istk
->mstk
->lineno
++;
4804 istk
->expansion
= l
->next
;
4806 p
= detoken(tline
, false);
4807 list
->line(LIST_MACRO
, p
);
4812 if (line
) { /* from the current input file */
4813 line
= prepreproc(line
);
4814 tline
= tokenize(line
);
4819 * The current file has ended; work down the istk
4826 "expected `%%endif' before end of file");
4827 /* only set line and file name if there's a next node */
4829 src_set_linnum(i
->lineno
);
4830 nasm_free(src_set_fname(i
->fname
));
4833 list
->downlevel(LIST_INCLUDE
);
4837 if (istk
->expansion
&& istk
->expansion
->finishes
)
4843 * We must expand MMacro parameters and MMacro-local labels
4844 * _before_ we plunge into directive processing, to cope
4845 * with things like `%define something %1' such as STRUC
4846 * uses. Unless we're _defining_ a MMacro, in which case
4847 * those tokens should be left alone to go into the
4848 * definition; and unless we're in a non-emitting
4849 * condition, in which case we don't want to meddle with
4852 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4853 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4854 tline
= expand_mmac_params(tline
);
4858 * Check the line to see if it's a preprocessor directive.
4860 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4862 } else if (defining
) {
4864 * We're defining a multi-line macro. We emit nothing
4866 * shove the tokenized line on to the macro definition.
4868 Line
*l
= nasm_malloc(sizeof(Line
));
4869 l
->next
= defining
->expansion
;
4872 defining
->expansion
= l
;
4874 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4876 * We're in a non-emitting branch of a condition block.
4877 * Emit nothing at all, not even a blank line: when we
4878 * emerge from the condition we'll give a line-number
4879 * directive so we keep our place correctly.
4883 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4885 * We're in a %rep block which has been terminated, so
4886 * we're walking through to the %endrep without
4887 * emitting anything. Emit nothing at all, not even a
4888 * blank line: when we emerge from the %rep block we'll
4889 * give a line-number directive so we keep our place
4895 tline
= expand_smacro(tline
);
4896 if (!expand_mmacro(tline
)) {
4898 * De-tokenize the line again, and emit it.
4900 line
= detoken(tline
, true);
4904 continue; /* expand_mmacro calls free_tlist */
4912 static void pp_cleanup(int pass
)
4915 if (defining
->name
) {
4917 "end of file while still defining macro `%s'",
4920 error(ERR_NONFATAL
, "end of file while still in %%rep");
4923 free_mmacro(defining
);
4933 nasm_free(i
->fname
);
4938 nasm_free(src_set_fname(NULL
));
4943 while ((i
= ipath
)) {
4952 void pp_include_path(char *path
)
4956 i
= nasm_malloc(sizeof(IncPath
));
4957 i
->path
= path
? nasm_strdup(path
) : NULL
;
4970 void pp_pre_include(char *fname
)
4972 Token
*inc
, *space
, *name
;
4975 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4976 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4977 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4979 l
= nasm_malloc(sizeof(Line
));
4986 void pp_pre_define(char *definition
)
4992 equals
= strchr(definition
, '=');
4993 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4994 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4997 space
->next
= tokenize(definition
);
5001 l
= nasm_malloc(sizeof(Line
));
5008 void pp_pre_undefine(char *definition
)
5013 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5014 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5015 space
->next
= tokenize(definition
);
5017 l
= nasm_malloc(sizeof(Line
));
5025 * Added by Keith Kanios:
5027 * This function is used to assist with "runtime" preprocessor
5028 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5030 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5031 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5034 void pp_runtime(char *definition
)
5038 def
= tokenize(definition
);
5039 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5044 void pp_extra_stdmac(macros_t
*macros
)
5046 extrastdmac
= macros
;
5049 static void make_tok_num(Token
* tok
, int64_t val
)
5052 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5053 tok
->text
= nasm_strdup(numbuf
);
5054 tok
->type
= TOK_NUMBER
;