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;
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
);
1190 /* Expand local macros here and not during preprocessing */
1191 if (expand_locals
&&
1192 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1193 t
->text
[0] == '%' && t
->text
[1] == '$') {
1196 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1199 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1200 p
= nasm_strcat(buffer
, q
);
1205 if (t
->type
== TOK_WHITESPACE
)
1208 len
+= strlen(t
->text
);
1211 p
= line
= nasm_malloc(len
+ 1);
1213 list_for_each(t
, tlist
) {
1214 if (t
->type
== TOK_WHITESPACE
) {
1216 } else if (t
->text
) {
1228 * A scanner, suitable for use by the expression evaluator, which
1229 * operates on a line of Tokens. Expects a pointer to a pointer to
1230 * the first token in the line to be passed in as its private_data
1233 * FIX: This really needs to be unified with stdscan.
1235 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1237 Token
**tlineptr
= private_data
;
1239 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1243 *tlineptr
= tline
? tline
->next
: NULL
;
1244 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1245 tline
->type
== TOK_COMMENT
));
1248 return tokval
->t_type
= TOKEN_EOS
;
1250 tokval
->t_charptr
= tline
->text
;
1252 if (tline
->text
[0] == '$' && !tline
->text
[1])
1253 return tokval
->t_type
= TOKEN_HERE
;
1254 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1255 return tokval
->t_type
= TOKEN_BASE
;
1257 if (tline
->type
== TOK_ID
) {
1258 p
= tokval
->t_charptr
= tline
->text
;
1260 tokval
->t_charptr
++;
1261 return tokval
->t_type
= TOKEN_ID
;
1264 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1265 if (r
>= p
+MAX_KEYWORD
)
1266 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1267 *s
++ = nasm_tolower(*r
);
1270 /* right, so we have an identifier sitting in temp storage. now,
1271 * is it actually a register or instruction name, or what? */
1272 return nasm_token_hash(ourcopy
, tokval
);
1275 if (tline
->type
== TOK_NUMBER
) {
1277 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1278 tokval
->t_charptr
= tline
->text
;
1280 return tokval
->t_type
= TOKEN_ERRNUM
;
1282 return tokval
->t_type
= TOKEN_NUM
;
1285 if (tline
->type
== TOK_FLOAT
) {
1286 return tokval
->t_type
= TOKEN_FLOAT
;
1289 if (tline
->type
== TOK_STRING
) {
1292 bq
= tline
->text
[0];
1293 tokval
->t_charptr
= tline
->text
;
1294 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1296 if (ep
[0] != bq
|| ep
[1] != '\0')
1297 return tokval
->t_type
= TOKEN_ERRSTR
;
1299 return tokval
->t_type
= TOKEN_STR
;
1302 if (tline
->type
== TOK_OTHER
) {
1303 if (!strcmp(tline
->text
, "<<"))
1304 return tokval
->t_type
= TOKEN_SHL
;
1305 if (!strcmp(tline
->text
, ">>"))
1306 return tokval
->t_type
= TOKEN_SHR
;
1307 if (!strcmp(tline
->text
, "//"))
1308 return tokval
->t_type
= TOKEN_SDIV
;
1309 if (!strcmp(tline
->text
, "%%"))
1310 return tokval
->t_type
= TOKEN_SMOD
;
1311 if (!strcmp(tline
->text
, "=="))
1312 return tokval
->t_type
= TOKEN_EQ
;
1313 if (!strcmp(tline
->text
, "<>"))
1314 return tokval
->t_type
= TOKEN_NE
;
1315 if (!strcmp(tline
->text
, "!="))
1316 return tokval
->t_type
= TOKEN_NE
;
1317 if (!strcmp(tline
->text
, "<="))
1318 return tokval
->t_type
= TOKEN_LE
;
1319 if (!strcmp(tline
->text
, ">="))
1320 return tokval
->t_type
= TOKEN_GE
;
1321 if (!strcmp(tline
->text
, "&&"))
1322 return tokval
->t_type
= TOKEN_DBL_AND
;
1323 if (!strcmp(tline
->text
, "^^"))
1324 return tokval
->t_type
= TOKEN_DBL_XOR
;
1325 if (!strcmp(tline
->text
, "||"))
1326 return tokval
->t_type
= TOKEN_DBL_OR
;
1330 * We have no other options: just return the first character of
1333 return tokval
->t_type
= tline
->text
[0];
1337 * Compare a string to the name of an existing macro; this is a
1338 * simple wrapper which calls either strcmp or nasm_stricmp
1339 * depending on the value of the `casesense' parameter.
1341 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1343 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1347 * Compare a string to the name of an existing macro; this is a
1348 * simple wrapper which calls either strcmp or nasm_stricmp
1349 * depending on the value of the `casesense' parameter.
1351 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1353 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1357 * Return the Context structure associated with a %$ token. Return
1358 * NULL, having _already_ reported an error condition, if the
1359 * context stack isn't deep enough for the supplied number of $
1361 * If all_contexts == true, contexts that enclose current are
1362 * also scanned for such smacro, until it is found; if not -
1363 * only the context that directly results from the number of $'s
1364 * in variable's name.
1366 * If "namep" is non-NULL, set it to the pointer to the macro name
1367 * tail, i.e. the part beyond %$...
1369 static Context
*get_ctx(const char *name
, const char **namep
,
1379 if (!name
|| name
[0] != '%' || name
[1] != '$')
1383 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1390 while (ctx
&& *name
== '$') {
1396 error(ERR_NONFATAL
, "`%s': context stack is only"
1397 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1408 /* Search for this smacro in found context */
1409 m
= hash_findix(&ctx
->localmac
, name
);
1411 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1422 * Check to see if a file is already in a string list
1424 static bool in_list(const StrList
*list
, const char *str
)
1427 if (!strcmp(list
->str
, str
))
1435 * Open an include file. This routine must always return a valid
1436 * file pointer if it returns - it's responsible for throwing an
1437 * ERR_FATAL and bombing out completely if not. It should also try
1438 * the include path one by one until it finds the file or reaches
1439 * the end of the path.
1441 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1446 IncPath
*ip
= ipath
;
1447 int len
= strlen(file
);
1448 size_t prefix_len
= 0;
1452 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1453 memcpy(sl
->str
, prefix
, prefix_len
);
1454 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1455 fp
= fopen(sl
->str
, "r");
1456 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1474 prefix_len
= strlen(prefix
);
1476 /* -MG given and file not found */
1477 if (dhead
&& !in_list(*dhead
, file
)) {
1478 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1480 strcpy(sl
->str
, file
);
1488 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1493 * Determine if we should warn on defining a single-line macro of
1494 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1495 * return true if _any_ single-line macro of that name is defined.
1496 * Otherwise, will return true if a single-line macro with either
1497 * `nparam' or no parameters is defined.
1499 * If a macro with precisely the right number of parameters is
1500 * defined, or nparam is -1, the address of the definition structure
1501 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1502 * is NULL, no action will be taken regarding its contents, and no
1505 * Note that this is also called with nparam zero to resolve
1508 * If you already know which context macro belongs to, you can pass
1509 * the context pointer as first parameter; if you won't but name begins
1510 * with %$ the context will be automatically computed. If all_contexts
1511 * is true, macro will be searched in outer contexts as well.
1514 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1517 struct hash_table
*smtbl
;
1521 smtbl
= &ctx
->localmac
;
1522 } else if (name
[0] == '%' && name
[1] == '$') {
1524 ctx
= get_ctx(name
, &name
, false);
1526 return false; /* got to return _something_ */
1527 smtbl
= &ctx
->localmac
;
1531 m
= (SMacro
*) hash_findix(smtbl
, name
);
1534 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1535 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1537 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1551 * Count and mark off the parameters in a multi-line macro call.
1552 * This is called both from within the multi-line macro expansion
1553 * code, and also to mark off the default parameters when provided
1554 * in a %macro definition line.
1556 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1558 int paramsize
, brace
;
1560 *nparam
= paramsize
= 0;
1563 /* +1: we need space for the final NULL */
1564 if (*nparam
+1 >= paramsize
) {
1565 paramsize
+= PARAM_DELTA
;
1566 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1570 if (tok_is_(t
, "{"))
1572 (*params
)[(*nparam
)++] = t
;
1573 while (tok_isnt_(t
, brace
? "}" : ","))
1575 if (t
) { /* got a comma/brace */
1579 * Now we've found the closing brace, look further
1583 if (tok_isnt_(t
, ",")) {
1585 "braces do not enclose all of macro parameter");
1586 while (tok_isnt_(t
, ","))
1590 t
= t
->next
; /* eat the comma */
1597 * Determine whether one of the various `if' conditions is true or
1600 * We must free the tline we get passed.
1602 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1604 enum pp_conditional i
= PP_COND(ct
);
1606 Token
*t
, *tt
, **tptr
, *origline
;
1607 struct tokenval tokval
;
1609 enum pp_token_type needtype
;
1615 j
= false; /* have we matched yet? */
1620 if (tline
->type
!= TOK_ID
) {
1622 "`%s' expects context identifiers", pp_directives
[ct
]);
1623 free_tlist(origline
);
1626 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1628 tline
= tline
->next
;
1633 j
= false; /* have we matched yet? */
1636 if (!tline
|| (tline
->type
!= TOK_ID
&&
1637 (tline
->type
!= TOK_PREPROC_ID
||
1638 tline
->text
[1] != '$'))) {
1640 "`%s' expects macro identifiers", pp_directives
[ct
]);
1643 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1645 tline
= tline
->next
;
1651 tline
= expand_smacro(tline
);
1653 while (tok_isnt_(tt
, ","))
1657 "`%s' expects two comma-separated arguments",
1662 j
= true; /* assume equality unless proved not */
1663 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1664 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1665 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1669 if (t
->type
== TOK_WHITESPACE
) {
1673 if (tt
->type
== TOK_WHITESPACE
) {
1677 if (tt
->type
!= t
->type
) {
1678 j
= false; /* found mismatching tokens */
1681 /* When comparing strings, need to unquote them first */
1682 if (t
->type
== TOK_STRING
) {
1683 size_t l1
= nasm_unquote(t
->text
, NULL
);
1684 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1690 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1694 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1695 j
= false; /* found mismatching tokens */
1702 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1703 j
= false; /* trailing gunk on one end or other */
1709 MMacro searching
, *mmac
;
1712 tline
= expand_id(tline
);
1713 if (!tok_type_(tline
, TOK_ID
)) {
1715 "`%s' expects a macro name", pp_directives
[ct
]);
1718 searching
.name
= nasm_strdup(tline
->text
);
1719 searching
.casesense
= true;
1720 searching
.plus
= false;
1721 searching
.nolist
= false;
1722 searching
.in_progress
= 0;
1723 searching
.max_depth
= 0;
1724 searching
.rep_nest
= NULL
;
1725 searching
.nparam_min
= 0;
1726 searching
.nparam_max
= INT_MAX
;
1727 tline
= expand_smacro(tline
->next
);
1730 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1732 "`%s' expects a parameter count or nothing",
1735 searching
.nparam_min
= searching
.nparam_max
=
1736 readnum(tline
->text
, &j
);
1739 "unable to parse parameter count `%s'",
1742 if (tline
&& tok_is_(tline
->next
, "-")) {
1743 tline
= tline
->next
->next
;
1744 if (tok_is_(tline
, "*"))
1745 searching
.nparam_max
= INT_MAX
;
1746 else if (!tok_type_(tline
, TOK_NUMBER
))
1748 "`%s' expects a parameter count after `-'",
1751 searching
.nparam_max
= readnum(tline
->text
, &j
);
1754 "unable to parse parameter count `%s'",
1756 if (searching
.nparam_min
> searching
.nparam_max
)
1758 "minimum parameter count exceeds maximum");
1761 if (tline
&& tok_is_(tline
->next
, "+")) {
1762 tline
= tline
->next
;
1763 searching
.plus
= true;
1765 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1767 if (!strcmp(mmac
->name
, searching
.name
) &&
1768 (mmac
->nparam_min
<= searching
.nparam_max
1770 && (searching
.nparam_min
<= mmac
->nparam_max
1777 if (tline
&& tline
->next
)
1778 error(ERR_WARNING
|ERR_PASS1
,
1779 "trailing garbage after %%ifmacro ignored");
1780 nasm_free(searching
.name
);
1789 needtype
= TOK_NUMBER
;
1792 needtype
= TOK_STRING
;
1796 t
= tline
= expand_smacro(tline
);
1798 while (tok_type_(t
, TOK_WHITESPACE
) ||
1799 (needtype
== TOK_NUMBER
&&
1800 tok_type_(t
, TOK_OTHER
) &&
1801 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1805 j
= tok_type_(t
, needtype
);
1809 t
= tline
= expand_smacro(tline
);
1810 while (tok_type_(t
, TOK_WHITESPACE
))
1815 t
= t
->next
; /* Skip the actual token */
1816 while (tok_type_(t
, TOK_WHITESPACE
))
1818 j
= !t
; /* Should be nothing left */
1823 t
= tline
= expand_smacro(tline
);
1824 while (tok_type_(t
, TOK_WHITESPACE
))
1827 j
= !t
; /* Should be empty */
1831 t
= tline
= expand_smacro(tline
);
1833 tokval
.t_type
= TOKEN_INVALID
;
1834 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1835 NULL
, pass
| CRITICAL
, error
, NULL
);
1839 error(ERR_WARNING
|ERR_PASS1
,
1840 "trailing garbage after expression ignored");
1841 if (!is_simple(evalresult
)) {
1843 "non-constant value given to `%s'", pp_directives
[ct
]);
1846 j
= reloc_value(evalresult
) != 0;
1851 "preprocessor directive `%s' not yet implemented",
1856 free_tlist(origline
);
1857 return j
^ PP_NEGATIVE(ct
);
1860 free_tlist(origline
);
1865 * Common code for defining an smacro
1867 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1868 int nparam
, Token
*expansion
)
1870 SMacro
*smac
, **smhead
;
1871 struct hash_table
*smtbl
;
1873 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1875 error(ERR_WARNING
|ERR_PASS1
,
1876 "single-line macro `%s' defined both with and"
1877 " without parameters", mname
);
1879 * Some instances of the old code considered this a failure,
1880 * some others didn't. What is the right thing to do here?
1882 free_tlist(expansion
);
1883 return false; /* Failure */
1886 * We're redefining, so we have to take over an
1887 * existing SMacro structure. This means freeing
1888 * what was already in it.
1890 nasm_free(smac
->name
);
1891 free_tlist(smac
->expansion
);
1894 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1895 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1896 smac
= nasm_malloc(sizeof(SMacro
));
1897 smac
->next
= *smhead
;
1900 smac
->name
= nasm_strdup(mname
);
1901 smac
->casesense
= casesense
;
1902 smac
->nparam
= nparam
;
1903 smac
->expansion
= expansion
;
1904 smac
->in_progress
= false;
1905 return true; /* Success */
1909 * Undefine an smacro
1911 static void undef_smacro(Context
*ctx
, const char *mname
)
1913 SMacro
**smhead
, *s
, **sp
;
1914 struct hash_table
*smtbl
;
1916 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1917 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1921 * We now have a macro name... go hunt for it.
1924 while ((s
= *sp
) != NULL
) {
1925 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1928 free_tlist(s
->expansion
);
1938 * Parse a mmacro specification.
1940 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1944 tline
= tline
->next
;
1946 tline
= expand_id(tline
);
1947 if (!tok_type_(tline
, TOK_ID
)) {
1948 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1953 def
->name
= nasm_strdup(tline
->text
);
1955 def
->nolist
= false;
1956 def
->in_progress
= 0;
1957 def
->rep_nest
= NULL
;
1958 def
->nparam_min
= 0;
1959 def
->nparam_max
= 0;
1961 tline
= expand_smacro(tline
->next
);
1963 if (!tok_type_(tline
, TOK_NUMBER
)) {
1964 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1966 def
->nparam_min
= def
->nparam_max
=
1967 readnum(tline
->text
, &err
);
1970 "unable to parse parameter count `%s'", tline
->text
);
1972 if (tline
&& tok_is_(tline
->next
, "-")) {
1973 tline
= tline
->next
->next
;
1974 if (tok_is_(tline
, "*")) {
1975 def
->nparam_max
= INT_MAX
;
1976 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1978 "`%s' expects a parameter count after `-'", directive
);
1980 def
->nparam_max
= readnum(tline
->text
, &err
);
1982 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1985 if (def
->nparam_min
> def
->nparam_max
) {
1986 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1990 if (tline
&& tok_is_(tline
->next
, "+")) {
1991 tline
= tline
->next
;
1994 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1995 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1996 tline
= tline
->next
;
2001 * Handle default parameters.
2003 if (tline
&& tline
->next
) {
2004 def
->dlist
= tline
->next
;
2006 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2009 def
->defaults
= NULL
;
2011 def
->expansion
= NULL
;
2013 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2015 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2016 "too many default macro parameters");
2023 * Decode a size directive
2025 static int parse_size(const char *str
) {
2026 static const char *size_names
[] =
2027 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2028 static const int sizes
[] =
2029 { 0, 1, 4, 16, 8, 10, 2, 32 };
2031 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2035 * nasm_unquote with error if the string contains NUL characters.
2036 * If the string contains NUL characters, issue an error and return
2037 * the C len, i.e. truncate at the NUL.
2039 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
2041 size_t len
= nasm_unquote(qstr
, NULL
);
2042 size_t clen
= strlen(qstr
);
2045 error(ERR_NONFATAL
, "NUL character in `%s' directive",
2046 pp_directives
[directive
]);
2052 * find and process preprocessor directive in passed line
2053 * Find out if a line contains a preprocessor directive, and deal
2056 * If a directive _is_ found, it is the responsibility of this routine
2057 * (and not the caller) to free_tlist() the line.
2059 * @param tline a pointer to the current tokeninzed line linked list
2060 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2063 static int do_directive(Token
* tline
)
2065 enum preproc_token i
;
2078 MMacro
*mmac
, **mmhead
;
2079 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2081 struct tokenval tokval
;
2083 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2091 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2092 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2093 || tline
->text
[1] == '!'))
2094 return NO_DIRECTIVE_FOUND
;
2096 i
= pp_token_hash(tline
->text
);
2099 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2100 * since they are known to be buggy at moment, we need to fix them
2101 * in future release (2.09-2.10)
2103 if (i
== PP_RMACRO
|| i
== PP_RMACRO
|| i
== PP_EXITMACRO
) {
2104 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2106 return NO_DIRECTIVE_FOUND
;
2110 * If we're in a non-emitting branch of a condition construct,
2111 * or walking to the end of an already terminated %rep block,
2112 * we should ignore all directives except for condition
2115 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2116 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2117 return NO_DIRECTIVE_FOUND
;
2121 * If we're defining a macro or reading a %rep block, we should
2122 * ignore all directives except for %macro/%imacro (which nest),
2123 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2124 * If we're in a %rep block, another %rep nests, so should be let through.
2126 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2127 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2128 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2129 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2130 return NO_DIRECTIVE_FOUND
;
2134 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2135 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2137 return NO_DIRECTIVE_FOUND
;
2138 } else if (nested_mac_count
> 0) {
2139 if (i
== PP_ENDMACRO
) {
2141 return NO_DIRECTIVE_FOUND
;
2144 if (!defining
->name
) {
2147 return NO_DIRECTIVE_FOUND
;
2148 } else if (nested_rep_count
> 0) {
2149 if (i
== PP_ENDREP
) {
2151 return NO_DIRECTIVE_FOUND
;
2159 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2161 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2164 /* Directive to tell NASM what the default stack size is. The
2165 * default is for a 16-bit stack, and this can be overriden with
2168 tline
= tline
->next
;
2169 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2170 tline
= tline
->next
;
2171 if (!tline
|| tline
->type
!= TOK_ID
) {
2172 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2173 free_tlist(origline
);
2174 return DIRECTIVE_FOUND
;
2176 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2177 /* All subsequent ARG directives are for a 32-bit stack */
2179 StackPointer
= "ebp";
2182 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2183 /* All subsequent ARG directives are for a 64-bit stack */
2185 StackPointer
= "rbp";
2188 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2189 /* All subsequent ARG directives are for a 16-bit stack,
2190 * far function call.
2193 StackPointer
= "bp";
2196 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2197 /* All subsequent ARG directives are for a 16-bit stack,
2198 * far function call. We don't support near functions.
2201 StackPointer
= "bp";
2205 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2206 free_tlist(origline
);
2207 return DIRECTIVE_FOUND
;
2209 free_tlist(origline
);
2210 return DIRECTIVE_FOUND
;
2213 /* TASM like ARG directive to define arguments to functions, in
2214 * the following form:
2216 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2220 char *arg
, directive
[256];
2221 int size
= StackSize
;
2223 /* Find the argument name */
2224 tline
= tline
->next
;
2225 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2226 tline
= tline
->next
;
2227 if (!tline
|| tline
->type
!= TOK_ID
) {
2228 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2229 free_tlist(origline
);
2230 return DIRECTIVE_FOUND
;
2234 /* Find the argument size type */
2235 tline
= tline
->next
;
2236 if (!tline
|| tline
->type
!= TOK_OTHER
2237 || tline
->text
[0] != ':') {
2239 "Syntax error processing `%%arg' directive");
2240 free_tlist(origline
);
2241 return DIRECTIVE_FOUND
;
2243 tline
= tline
->next
;
2244 if (!tline
|| tline
->type
!= TOK_ID
) {
2245 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2246 free_tlist(origline
);
2247 return DIRECTIVE_FOUND
;
2250 /* Allow macro expansion of type parameter */
2251 tt
= tokenize(tline
->text
);
2252 tt
= expand_smacro(tt
);
2253 size
= parse_size(tt
->text
);
2256 "Invalid size type for `%%arg' missing directive");
2258 free_tlist(origline
);
2259 return DIRECTIVE_FOUND
;
2263 /* Round up to even stack slots */
2264 size
= ALIGN(size
, StackSize
);
2266 /* Now define the macro for the argument */
2267 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2268 arg
, StackPointer
, offset
);
2269 do_directive(tokenize(directive
));
2272 /* Move to the next argument in the list */
2273 tline
= tline
->next
;
2274 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2275 tline
= tline
->next
;
2276 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2278 free_tlist(origline
);
2279 return DIRECTIVE_FOUND
;
2282 /* TASM like LOCAL directive to define local variables for a
2283 * function, in the following form:
2285 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2287 * The '= LocalSize' at the end is ignored by NASM, but is
2288 * required by TASM to define the local parameter size (and used
2289 * by the TASM macro package).
2291 offset
= LocalOffset
;
2293 char *local
, directive
[256];
2294 int size
= StackSize
;
2296 /* Find the argument name */
2297 tline
= tline
->next
;
2298 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2299 tline
= tline
->next
;
2300 if (!tline
|| tline
->type
!= TOK_ID
) {
2302 "`%%local' missing argument parameter");
2303 free_tlist(origline
);
2304 return DIRECTIVE_FOUND
;
2306 local
= tline
->text
;
2308 /* Find the argument size type */
2309 tline
= tline
->next
;
2310 if (!tline
|| tline
->type
!= TOK_OTHER
2311 || tline
->text
[0] != ':') {
2313 "Syntax error processing `%%local' directive");
2314 free_tlist(origline
);
2315 return DIRECTIVE_FOUND
;
2317 tline
= tline
->next
;
2318 if (!tline
|| tline
->type
!= TOK_ID
) {
2320 "`%%local' missing size type parameter");
2321 free_tlist(origline
);
2322 return DIRECTIVE_FOUND
;
2325 /* Allow macro expansion of type parameter */
2326 tt
= tokenize(tline
->text
);
2327 tt
= expand_smacro(tt
);
2328 size
= parse_size(tt
->text
);
2331 "Invalid size type for `%%local' missing directive");
2333 free_tlist(origline
);
2334 return DIRECTIVE_FOUND
;
2338 /* Round up to even stack slots */
2339 size
= ALIGN(size
, StackSize
);
2341 offset
+= size
; /* Negative offset, increment before */
2343 /* Now define the macro for the argument */
2344 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2345 local
, StackPointer
, offset
);
2346 do_directive(tokenize(directive
));
2348 /* Now define the assign to setup the enter_c macro correctly */
2349 snprintf(directive
, sizeof(directive
),
2350 "%%assign %%$localsize %%$localsize+%d", size
);
2351 do_directive(tokenize(directive
));
2353 /* Move to the next argument in the list */
2354 tline
= tline
->next
;
2355 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2356 tline
= tline
->next
;
2357 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2358 LocalOffset
= offset
;
2359 free_tlist(origline
);
2360 return DIRECTIVE_FOUND
;
2364 error(ERR_WARNING
|ERR_PASS1
,
2365 "trailing garbage after `%%clear' ignored");
2368 free_tlist(origline
);
2369 return DIRECTIVE_FOUND
;
2372 t
= tline
->next
= expand_smacro(tline
->next
);
2374 if (!t
|| (t
->type
!= TOK_STRING
&&
2375 t
->type
!= TOK_INTERNAL_STRING
)) {
2376 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2377 free_tlist(origline
);
2378 return DIRECTIVE_FOUND
; /* but we did _something_ */
2381 error(ERR_WARNING
|ERR_PASS1
,
2382 "trailing garbage after `%%depend' ignored");
2384 if (t
->type
!= TOK_INTERNAL_STRING
)
2385 nasm_unquote_cstr(p
, i
);
2386 if (dephead
&& !in_list(*dephead
, p
)) {
2387 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2391 deptail
= &sl
->next
;
2393 free_tlist(origline
);
2394 return DIRECTIVE_FOUND
;
2397 t
= tline
->next
= expand_smacro(tline
->next
);
2400 if (!t
|| (t
->type
!= TOK_STRING
&&
2401 t
->type
!= TOK_INTERNAL_STRING
)) {
2402 error(ERR_NONFATAL
, "`%%include' expects a file name");
2403 free_tlist(origline
);
2404 return DIRECTIVE_FOUND
; /* but we did _something_ */
2407 error(ERR_WARNING
|ERR_PASS1
,
2408 "trailing garbage after `%%include' ignored");
2410 if (t
->type
!= TOK_INTERNAL_STRING
)
2411 nasm_unquote_cstr(p
, i
);
2412 inc
= nasm_malloc(sizeof(Include
));
2415 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2417 /* -MG given but file not found */
2420 inc
->fname
= src_set_fname(nasm_strdup(p
));
2421 inc
->lineno
= src_set_linnum(0);
2423 inc
->expansion
= NULL
;
2426 list
->uplevel(LIST_INCLUDE
);
2428 free_tlist(origline
);
2429 return DIRECTIVE_FOUND
;
2433 static macros_t
*use_pkg
;
2434 const char *pkg_macro
= NULL
;
2436 tline
= tline
->next
;
2438 tline
= expand_id(tline
);
2440 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2441 tline
->type
!= TOK_INTERNAL_STRING
&&
2442 tline
->type
!= TOK_ID
)) {
2443 error(ERR_NONFATAL
, "`%%use' expects a package name");
2444 free_tlist(origline
);
2445 return DIRECTIVE_FOUND
; /* but we did _something_ */
2448 error(ERR_WARNING
|ERR_PASS1
,
2449 "trailing garbage after `%%use' ignored");
2450 if (tline
->type
== TOK_STRING
)
2451 nasm_unquote_cstr(tline
->text
, i
);
2452 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2454 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2456 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2457 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2458 /* Not already included, go ahead and include it */
2459 stdmacpos
= use_pkg
;
2461 free_tlist(origline
);
2462 return DIRECTIVE_FOUND
;
2467 tline
= tline
->next
;
2469 tline
= expand_id(tline
);
2471 if (!tok_type_(tline
, TOK_ID
)) {
2472 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2474 free_tlist(origline
);
2475 return DIRECTIVE_FOUND
; /* but we did _something_ */
2478 error(ERR_WARNING
|ERR_PASS1
,
2479 "trailing garbage after `%s' ignored",
2481 p
= nasm_strdup(tline
->text
);
2483 p
= NULL
; /* Anonymous */
2487 ctx
= nasm_malloc(sizeof(Context
));
2489 hash_init(&ctx
->localmac
, HASH_SMALL
);
2491 ctx
->number
= unique
++;
2496 error(ERR_NONFATAL
, "`%s': context stack is empty",
2498 } else if (i
== PP_POP
) {
2499 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2500 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2502 cstk
->name
? cstk
->name
: "anonymous", p
);
2507 nasm_free(cstk
->name
);
2513 free_tlist(origline
);
2514 return DIRECTIVE_FOUND
;
2516 severity
= ERR_FATAL
;
2519 severity
= ERR_NONFATAL
;
2522 severity
= ERR_WARNING
|ERR_WARN_USER
;
2527 /* Only error out if this is the final pass */
2528 if (pass
!= 2 && i
!= PP_FATAL
)
2529 return DIRECTIVE_FOUND
;
2531 tline
->next
= expand_smacro(tline
->next
);
2532 tline
= tline
->next
;
2534 t
= tline
? tline
->next
: NULL
;
2536 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2537 /* The line contains only a quoted string */
2539 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2540 error(severity
, "%s", p
);
2542 /* Not a quoted string, or more than a quoted string */
2543 p
= detoken(tline
, false);
2544 error(severity
, "%s", p
);
2547 free_tlist(origline
);
2548 return DIRECTIVE_FOUND
;
2552 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2555 j
= if_condition(tline
->next
, i
);
2556 tline
->next
= NULL
; /* it got freed */
2557 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2559 cond
= nasm_malloc(sizeof(Cond
));
2560 cond
->next
= istk
->conds
;
2564 istk
->mstk
->condcnt
++;
2565 free_tlist(origline
);
2566 return DIRECTIVE_FOUND
;
2570 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2571 switch(istk
->conds
->state
) {
2573 istk
->conds
->state
= COND_DONE
;
2580 case COND_ELSE_TRUE
:
2581 case COND_ELSE_FALSE
:
2582 error_precond(ERR_WARNING
|ERR_PASS1
,
2583 "`%%elif' after `%%else' ignored");
2584 istk
->conds
->state
= COND_NEVER
;
2589 * IMPORTANT: In the case of %if, we will already have
2590 * called expand_mmac_params(); however, if we're
2591 * processing an %elif we must have been in a
2592 * non-emitting mode, which would have inhibited
2593 * the normal invocation of expand_mmac_params().
2594 * Therefore, we have to do it explicitly here.
2596 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2597 tline
->next
= NULL
; /* it got freed */
2598 istk
->conds
->state
=
2599 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2602 free_tlist(origline
);
2603 return DIRECTIVE_FOUND
;
2607 error_precond(ERR_WARNING
|ERR_PASS1
,
2608 "trailing garbage after `%%else' ignored");
2610 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2611 switch(istk
->conds
->state
) {
2614 istk
->conds
->state
= COND_ELSE_FALSE
;
2621 istk
->conds
->state
= COND_ELSE_TRUE
;
2624 case COND_ELSE_TRUE
:
2625 case COND_ELSE_FALSE
:
2626 error_precond(ERR_WARNING
|ERR_PASS1
,
2627 "`%%else' after `%%else' ignored.");
2628 istk
->conds
->state
= COND_NEVER
;
2631 free_tlist(origline
);
2632 return DIRECTIVE_FOUND
;
2636 error_precond(ERR_WARNING
|ERR_PASS1
,
2637 "trailing garbage after `%%endif' ignored");
2639 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2641 istk
->conds
= cond
->next
;
2644 istk
->mstk
->condcnt
--;
2645 free_tlist(origline
);
2646 return DIRECTIVE_FOUND
;
2653 error(ERR_FATAL
, "`%s': already defining a macro",
2655 return DIRECTIVE_FOUND
;
2657 defining
= nasm_malloc(sizeof(MMacro
));
2658 defining
->max_depth
=
2659 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2660 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2661 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2662 nasm_free(defining
);
2664 return DIRECTIVE_FOUND
;
2667 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2669 if (!strcmp(mmac
->name
, defining
->name
) &&
2670 (mmac
->nparam_min
<= defining
->nparam_max
2672 && (defining
->nparam_min
<= mmac
->nparam_max
2674 error(ERR_WARNING
|ERR_PASS1
,
2675 "redefining multi-line macro `%s'", defining
->name
);
2676 return DIRECTIVE_FOUND
;
2680 free_tlist(origline
);
2681 return DIRECTIVE_FOUND
;
2685 if (! (defining
&& defining
->name
)) {
2686 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2687 return DIRECTIVE_FOUND
;
2689 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2690 defining
->next
= *mmhead
;
2693 free_tlist(origline
);
2694 return DIRECTIVE_FOUND
;
2698 * We must search along istk->expansion until we hit a
2699 * macro-end marker for a macro with a name. Then we
2700 * bypass all lines between exitmacro and endmacro.
2702 list_for_each(l
, istk
->expansion
)
2703 if (l
->finishes
&& l
->finishes
->name
)
2708 * Remove all conditional entries relative to this
2709 * macro invocation. (safe to do in this context)
2711 for ( ; l
->finishes
->condcnt
> 0; l
->finishes
->condcnt
--) {
2713 istk
->conds
= cond
->next
;
2716 istk
->expansion
= l
;
2718 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2720 free_tlist(origline
);
2721 return DIRECTIVE_FOUND
;
2729 spec
.casesense
= (i
== PP_UNMACRO
);
2730 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2731 return DIRECTIVE_FOUND
;
2733 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2734 while (mmac_p
&& *mmac_p
) {
2736 if (mmac
->casesense
== spec
.casesense
&&
2737 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2738 mmac
->nparam_min
== spec
.nparam_min
&&
2739 mmac
->nparam_max
== spec
.nparam_max
&&
2740 mmac
->plus
== spec
.plus
) {
2741 *mmac_p
= mmac
->next
;
2744 mmac_p
= &mmac
->next
;
2747 free_tlist(origline
);
2748 free_tlist(spec
.dlist
);
2749 return DIRECTIVE_FOUND
;
2753 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2754 tline
= tline
->next
;
2756 free_tlist(origline
);
2757 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2758 return DIRECTIVE_FOUND
;
2760 t
= expand_smacro(tline
->next
);
2762 free_tlist(origline
);
2765 tokval
.t_type
= TOKEN_INVALID
;
2767 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2770 return DIRECTIVE_FOUND
;
2772 error(ERR_WARNING
|ERR_PASS1
,
2773 "trailing garbage after expression ignored");
2774 if (!is_simple(evalresult
)) {
2775 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2776 return DIRECTIVE_FOUND
;
2779 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2780 mmac
= mmac
->next_active
;
2782 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2783 } else if (mmac
->nparam
== 0) {
2785 "`%%rotate' invoked within macro without parameters");
2787 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2789 rotate
%= (int)mmac
->nparam
;
2791 rotate
+= mmac
->nparam
;
2793 mmac
->rotate
= rotate
;
2795 return DIRECTIVE_FOUND
;
2800 tline
= tline
->next
;
2801 } while (tok_type_(tline
, TOK_WHITESPACE
));
2803 if (tok_type_(tline
, TOK_ID
) &&
2804 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2807 tline
= tline
->next
;
2808 } while (tok_type_(tline
, TOK_WHITESPACE
));
2812 t
= expand_smacro(tline
);
2814 tokval
.t_type
= TOKEN_INVALID
;
2816 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2818 free_tlist(origline
);
2819 return DIRECTIVE_FOUND
;
2822 error(ERR_WARNING
|ERR_PASS1
,
2823 "trailing garbage after expression ignored");
2824 if (!is_simple(evalresult
)) {
2825 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2826 return DIRECTIVE_FOUND
;
2828 count
= reloc_value(evalresult
) + 1;
2830 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2833 free_tlist(origline
);
2835 tmp_defining
= defining
;
2836 defining
= nasm_malloc(sizeof(MMacro
));
2837 defining
->prev
= NULL
;
2838 defining
->name
= NULL
; /* flags this macro as a %rep block */
2839 defining
->casesense
= false;
2840 defining
->plus
= false;
2841 defining
->nolist
= nolist
;
2842 defining
->in_progress
= count
;
2843 defining
->max_depth
= 0;
2844 defining
->nparam_min
= defining
->nparam_max
= 0;
2845 defining
->defaults
= NULL
;
2846 defining
->dlist
= NULL
;
2847 defining
->expansion
= NULL
;
2848 defining
->next_active
= istk
->mstk
;
2849 defining
->rep_nest
= tmp_defining
;
2850 return DIRECTIVE_FOUND
;
2853 if (!defining
|| defining
->name
) {
2854 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2855 return DIRECTIVE_FOUND
;
2859 * Now we have a "macro" defined - although it has no name
2860 * and we won't be entering it in the hash tables - we must
2861 * push a macro-end marker for it on to istk->expansion.
2862 * After that, it will take care of propagating itself (a
2863 * macro-end marker line for a macro which is really a %rep
2864 * block will cause the macro to be re-expanded, complete
2865 * with another macro-end marker to ensure the process
2866 * continues) until the whole expansion is forcibly removed
2867 * from istk->expansion by a %exitrep.
2869 l
= nasm_malloc(sizeof(Line
));
2870 l
->next
= istk
->expansion
;
2871 l
->finishes
= defining
;
2873 istk
->expansion
= l
;
2875 istk
->mstk
= defining
;
2877 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2878 tmp_defining
= defining
;
2879 defining
= defining
->rep_nest
;
2880 free_tlist(origline
);
2881 return DIRECTIVE_FOUND
;
2885 * We must search along istk->expansion until we hit a
2886 * macro-end marker for a macro with no name. Then we set
2887 * its `in_progress' flag to 0.
2889 list_for_each(l
, istk
->expansion
)
2890 if (l
->finishes
&& !l
->finishes
->name
)
2894 l
->finishes
->in_progress
= 1;
2896 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2897 free_tlist(origline
);
2898 return DIRECTIVE_FOUND
;
2904 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2906 tline
= tline
->next
;
2908 tline
= expand_id(tline
);
2909 if (!tline
|| (tline
->type
!= TOK_ID
&&
2910 (tline
->type
!= TOK_PREPROC_ID
||
2911 tline
->text
[1] != '$'))) {
2912 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2914 free_tlist(origline
);
2915 return DIRECTIVE_FOUND
;
2918 ctx
= get_ctx(tline
->text
, &mname
, false);
2920 param_start
= tline
= tline
->next
;
2923 /* Expand the macro definition now for %xdefine and %ixdefine */
2924 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2925 tline
= expand_smacro(tline
);
2927 if (tok_is_(tline
, "(")) {
2929 * This macro has parameters.
2932 tline
= tline
->next
;
2936 error(ERR_NONFATAL
, "parameter identifier expected");
2937 free_tlist(origline
);
2938 return DIRECTIVE_FOUND
;
2940 if (tline
->type
!= TOK_ID
) {
2942 "`%s': parameter identifier expected",
2944 free_tlist(origline
);
2945 return DIRECTIVE_FOUND
;
2947 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2948 tline
= tline
->next
;
2950 if (tok_is_(tline
, ",")) {
2951 tline
= tline
->next
;
2953 if (!tok_is_(tline
, ")")) {
2955 "`)' expected to terminate macro template");
2956 free_tlist(origline
);
2957 return DIRECTIVE_FOUND
;
2963 tline
= tline
->next
;
2965 if (tok_type_(tline
, TOK_WHITESPACE
))
2966 last
= tline
, tline
= tline
->next
;
2971 if (t
->type
== TOK_ID
) {
2972 list_for_each(tt
, param_start
)
2973 if (tt
->type
>= TOK_SMAC_PARAM
&&
2974 !strcmp(tt
->text
, t
->text
))
2978 t
->next
= macro_start
;
2983 * Good. We now have a macro name, a parameter count, and a
2984 * token list (in reverse order) for an expansion. We ought
2985 * to be OK just to create an SMacro, store it, and let
2986 * free_tlist have the rest of the line (which we have
2987 * carefully re-terminated after chopping off the expansion
2990 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2991 free_tlist(origline
);
2992 return DIRECTIVE_FOUND
;
2995 tline
= tline
->next
;
2997 tline
= expand_id(tline
);
2998 if (!tline
|| (tline
->type
!= TOK_ID
&&
2999 (tline
->type
!= TOK_PREPROC_ID
||
3000 tline
->text
[1] != '$'))) {
3001 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3002 free_tlist(origline
);
3003 return DIRECTIVE_FOUND
;
3006 error(ERR_WARNING
|ERR_PASS1
,
3007 "trailing garbage after macro name ignored");
3010 /* Find the context that symbol belongs to */
3011 ctx
= get_ctx(tline
->text
, &mname
, false);
3012 undef_smacro(ctx
, mname
);
3013 free_tlist(origline
);
3014 return DIRECTIVE_FOUND
;
3018 casesense
= (i
== PP_DEFSTR
);
3020 tline
= tline
->next
;
3022 tline
= expand_id(tline
);
3023 if (!tline
|| (tline
->type
!= TOK_ID
&&
3024 (tline
->type
!= TOK_PREPROC_ID
||
3025 tline
->text
[1] != '$'))) {
3026 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3028 free_tlist(origline
);
3029 return DIRECTIVE_FOUND
;
3032 ctx
= get_ctx(tline
->text
, &mname
, false);
3034 tline
= expand_smacro(tline
->next
);
3037 while (tok_type_(tline
, TOK_WHITESPACE
))
3038 tline
= delete_Token(tline
);
3040 p
= detoken(tline
, false);
3041 macro_start
= nasm_malloc(sizeof(*macro_start
));
3042 macro_start
->next
= NULL
;
3043 macro_start
->text
= nasm_quote(p
, strlen(p
));
3044 macro_start
->type
= TOK_STRING
;
3045 macro_start
->a
.mac
= NULL
;
3049 * We now have a macro name, an implicit parameter count of
3050 * zero, and a string token to use as an expansion. Create
3051 * and store an SMacro.
3053 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3054 free_tlist(origline
);
3055 return DIRECTIVE_FOUND
;
3059 casesense
= (i
== PP_DEFTOK
);
3061 tline
= tline
->next
;
3063 tline
= expand_id(tline
);
3064 if (!tline
|| (tline
->type
!= TOK_ID
&&
3065 (tline
->type
!= TOK_PREPROC_ID
||
3066 tline
->text
[1] != '$'))) {
3068 "`%s' expects a macro identifier as first parameter",
3070 free_tlist(origline
);
3071 return DIRECTIVE_FOUND
;
3073 ctx
= get_ctx(tline
->text
, &mname
, false);
3075 tline
= expand_smacro(tline
->next
);
3079 while (tok_type_(t
, TOK_WHITESPACE
))
3081 /* t should now point to the string */
3082 if (t
->type
!= TOK_STRING
) {
3084 "`%s` requires string as second parameter",
3087 free_tlist(origline
);
3088 return DIRECTIVE_FOUND
;
3091 nasm_unquote_cstr(t
->text
, i
);
3092 macro_start
= tokenize(t
->text
);
3095 * We now have a macro name, an implicit parameter count of
3096 * zero, and a numeric token to use as an expansion. Create
3097 * and store an SMacro.
3099 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3101 free_tlist(origline
);
3102 return DIRECTIVE_FOUND
;
3107 StrList
*xsl
= NULL
;
3108 StrList
**xst
= &xsl
;
3112 tline
= tline
->next
;
3114 tline
= expand_id(tline
);
3115 if (!tline
|| (tline
->type
!= TOK_ID
&&
3116 (tline
->type
!= TOK_PREPROC_ID
||
3117 tline
->text
[1] != '$'))) {
3119 "`%%pathsearch' expects a macro identifier as first parameter");
3120 free_tlist(origline
);
3121 return DIRECTIVE_FOUND
;
3123 ctx
= get_ctx(tline
->text
, &mname
, false);
3125 tline
= expand_smacro(tline
->next
);
3129 while (tok_type_(t
, TOK_WHITESPACE
))
3132 if (!t
|| (t
->type
!= TOK_STRING
&&
3133 t
->type
!= TOK_INTERNAL_STRING
)) {
3134 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3136 free_tlist(origline
);
3137 return DIRECTIVE_FOUND
; /* but we did _something_ */
3140 error(ERR_WARNING
|ERR_PASS1
,
3141 "trailing garbage after `%%pathsearch' ignored");
3143 if (t
->type
!= TOK_INTERNAL_STRING
)
3144 nasm_unquote(p
, NULL
);
3146 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3149 fclose(fp
); /* Don't actually care about the file */
3151 macro_start
= nasm_malloc(sizeof(*macro_start
));
3152 macro_start
->next
= NULL
;
3153 macro_start
->text
= nasm_quote(p
, strlen(p
));
3154 macro_start
->type
= TOK_STRING
;
3155 macro_start
->a
.mac
= NULL
;
3160 * We now have a macro name, an implicit parameter count of
3161 * zero, and a string token to use as an expansion. Create
3162 * and store an SMacro.
3164 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3166 free_tlist(origline
);
3167 return DIRECTIVE_FOUND
;
3173 tline
= tline
->next
;
3175 tline
= expand_id(tline
);
3176 if (!tline
|| (tline
->type
!= TOK_ID
&&
3177 (tline
->type
!= TOK_PREPROC_ID
||
3178 tline
->text
[1] != '$'))) {
3180 "`%%strlen' expects a macro identifier as first parameter");
3181 free_tlist(origline
);
3182 return DIRECTIVE_FOUND
;
3184 ctx
= get_ctx(tline
->text
, &mname
, false);
3186 tline
= expand_smacro(tline
->next
);
3190 while (tok_type_(t
, TOK_WHITESPACE
))
3192 /* t should now point to the string */
3193 if (t
->type
!= TOK_STRING
) {
3195 "`%%strlen` requires string as second parameter");
3197 free_tlist(origline
);
3198 return DIRECTIVE_FOUND
;
3201 macro_start
= nasm_malloc(sizeof(*macro_start
));
3202 macro_start
->next
= NULL
;
3203 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3204 macro_start
->a
.mac
= NULL
;
3207 * We now have a macro name, an implicit parameter count of
3208 * zero, and a numeric token to use as an expansion. Create
3209 * and store an SMacro.
3211 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3213 free_tlist(origline
);
3214 return DIRECTIVE_FOUND
;
3219 tline
= tline
->next
;
3221 tline
= expand_id(tline
);
3222 if (!tline
|| (tline
->type
!= TOK_ID
&&
3223 (tline
->type
!= TOK_PREPROC_ID
||
3224 tline
->text
[1] != '$'))) {
3226 "`%%strcat' expects a macro identifier as first parameter");
3227 free_tlist(origline
);
3228 return DIRECTIVE_FOUND
;
3230 ctx
= get_ctx(tline
->text
, &mname
, false);
3232 tline
= expand_smacro(tline
->next
);
3236 list_for_each(t
, tline
) {
3238 case TOK_WHITESPACE
:
3241 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3244 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3246 /* else fall through */
3249 "non-string passed to `%%strcat' (%d)", t
->type
);
3251 free_tlist(origline
);
3252 return DIRECTIVE_FOUND
;
3256 p
= pp
= nasm_malloc(len
);
3257 list_for_each(t
, tline
) {
3258 if (t
->type
== TOK_STRING
) {
3259 memcpy(p
, t
->text
, t
->a
.len
);
3265 * We now have a macro name, an implicit parameter count of
3266 * zero, and a numeric token to use as an expansion. Create
3267 * and store an SMacro.
3269 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3270 macro_start
->text
= nasm_quote(pp
, len
);
3272 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3274 free_tlist(origline
);
3275 return DIRECTIVE_FOUND
;
3284 tline
= tline
->next
;
3286 tline
= expand_id(tline
);
3287 if (!tline
|| (tline
->type
!= TOK_ID
&&
3288 (tline
->type
!= TOK_PREPROC_ID
||
3289 tline
->text
[1] != '$'))) {
3291 "`%%substr' expects a macro identifier as first parameter");
3292 free_tlist(origline
);
3293 return DIRECTIVE_FOUND
;
3295 ctx
= get_ctx(tline
->text
, &mname
, false);
3297 tline
= expand_smacro(tline
->next
);
3301 while (tok_type_(t
, TOK_WHITESPACE
))
3304 /* t should now point to the string */
3305 if (t
->type
!= TOK_STRING
) {
3307 "`%%substr` requires string as second parameter");
3309 free_tlist(origline
);
3310 return DIRECTIVE_FOUND
;
3315 tokval
.t_type
= TOKEN_INVALID
;
3316 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3320 free_tlist(origline
);
3321 return DIRECTIVE_FOUND
;
3322 } else if (!is_simple(evalresult
)) {
3323 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3325 free_tlist(origline
);
3326 return DIRECTIVE_FOUND
;
3328 a1
= evalresult
->value
-1;
3330 while (tok_type_(tt
, TOK_WHITESPACE
))
3333 a2
= 1; /* Backwards compatibility: one character */
3335 tokval
.t_type
= TOKEN_INVALID
;
3336 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3340 free_tlist(origline
);
3341 return DIRECTIVE_FOUND
;
3342 } else if (!is_simple(evalresult
)) {
3343 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3345 free_tlist(origline
);
3346 return DIRECTIVE_FOUND
;
3348 a2
= evalresult
->value
;
3351 len
= nasm_unquote(t
->text
, NULL
);
3354 if (a1
+a2
> (int64_t)len
)
3357 macro_start
= nasm_malloc(sizeof(*macro_start
));
3358 macro_start
->next
= NULL
;
3359 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3360 macro_start
->type
= TOK_STRING
;
3361 macro_start
->a
.mac
= NULL
;
3364 * We now have a macro name, an implicit parameter count of
3365 * zero, and a numeric token to use as an expansion. Create
3366 * and store an SMacro.
3368 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3370 free_tlist(origline
);
3371 return DIRECTIVE_FOUND
;
3376 casesense
= (i
== PP_ASSIGN
);
3378 tline
= tline
->next
;
3380 tline
= expand_id(tline
);
3381 if (!tline
|| (tline
->type
!= TOK_ID
&&
3382 (tline
->type
!= TOK_PREPROC_ID
||
3383 tline
->text
[1] != '$'))) {
3385 "`%%%sassign' expects a macro identifier",
3386 (i
== PP_IASSIGN
? "i" : ""));
3387 free_tlist(origline
);
3388 return DIRECTIVE_FOUND
;
3390 ctx
= get_ctx(tline
->text
, &mname
, false);
3392 tline
= expand_smacro(tline
->next
);
3397 tokval
.t_type
= TOKEN_INVALID
;
3399 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3402 free_tlist(origline
);
3403 return DIRECTIVE_FOUND
;
3407 error(ERR_WARNING
|ERR_PASS1
,
3408 "trailing garbage after expression ignored");
3410 if (!is_simple(evalresult
)) {
3412 "non-constant value given to `%%%sassign'",
3413 (i
== PP_IASSIGN
? "i" : ""));
3414 free_tlist(origline
);
3415 return DIRECTIVE_FOUND
;
3418 macro_start
= nasm_malloc(sizeof(*macro_start
));
3419 macro_start
->next
= NULL
;
3420 make_tok_num(macro_start
, reloc_value(evalresult
));
3421 macro_start
->a
.mac
= NULL
;
3424 * We now have a macro name, an implicit parameter count of
3425 * zero, and a numeric token to use as an expansion. Create
3426 * and store an SMacro.
3428 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3429 free_tlist(origline
);
3430 return DIRECTIVE_FOUND
;
3434 * Syntax is `%line nnn[+mmm] [filename]'
3436 tline
= tline
->next
;
3438 if (!tok_type_(tline
, TOK_NUMBER
)) {
3439 error(ERR_NONFATAL
, "`%%line' expects line number");
3440 free_tlist(origline
);
3441 return DIRECTIVE_FOUND
;
3443 k
= readnum(tline
->text
, &err
);
3445 tline
= tline
->next
;
3446 if (tok_is_(tline
, "+")) {
3447 tline
= tline
->next
;
3448 if (!tok_type_(tline
, TOK_NUMBER
)) {
3449 error(ERR_NONFATAL
, "`%%line' expects line increment");
3450 free_tlist(origline
);
3451 return DIRECTIVE_FOUND
;
3453 m
= readnum(tline
->text
, &err
);
3454 tline
= tline
->next
;
3460 nasm_free(src_set_fname(detoken(tline
, false)));
3462 free_tlist(origline
);
3463 return DIRECTIVE_FOUND
;
3467 "preprocessor directive `%s' not yet implemented",
3469 return DIRECTIVE_FOUND
;
3474 * Ensure that a macro parameter contains a condition code and
3475 * nothing else. Return the condition code index if so, or -1
3478 static int find_cc(Token
* t
)
3484 return -1; /* Probably a %+ without a space */
3487 if (t
->type
!= TOK_ID
)
3491 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3495 j
= ARRAY_SIZE(conditions
);
3498 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3513 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3515 Token
**tail
, *t
, *tt
;
3517 bool did_paste
= false;
3520 /* Now handle token pasting... */
3523 while ((t
= *tail
) && (tt
= t
->next
)) {
3525 case TOK_WHITESPACE
:
3526 if (tt
->type
== TOK_WHITESPACE
) {
3527 /* Zap adjacent whitespace tokens */
3528 t
->next
= delete_Token(tt
);
3530 /* Do not advance paste_head here */
3541 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3542 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3543 tt
->type
== TOK_OTHER
)) {
3544 len
+= strlen(tt
->text
);
3549 * Now tt points to the first token after
3550 * the potential paste area...
3552 if (tt
!= t
->next
) {
3553 /* We have at least two tokens... */
3554 len
+= strlen(t
->text
);
3555 p
= tmp
= nasm_malloc(len
+1);
3559 p
= strchr(p
, '\0');
3560 t
= delete_Token(t
);
3563 t
= *tail
= tokenize(tmp
);
3570 t
->next
= tt
; /* Attach the remaining token chain */
3578 case TOK_PASTE
: /* %+ */
3579 if (handle_paste_tokens
) {
3580 /* Zap %+ and whitespace tokens to the right */
3581 while (t
&& (t
->type
== TOK_WHITESPACE
||
3582 t
->type
== TOK_PASTE
))
3583 t
= *tail
= delete_Token(t
);
3584 if (!paste_head
|| !t
)
3585 break; /* Nothing to paste with */
3589 while (tok_type_(tt
, TOK_WHITESPACE
))
3590 tt
= t
->next
= delete_Token(tt
);
3593 tmp
= nasm_strcat(t
->text
, tt
->text
);
3595 tt
= delete_Token(tt
);
3596 t
= *tail
= tokenize(tmp
);
3602 t
->next
= tt
; /* Attach the remaining token chain */
3609 /* else fall through */
3612 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3621 * expands to a list of tokens from %{x:y}
3623 static Token
*expand_mmac_params_range(MMacro
*mac
, Token
*tline
, Token
***last
)
3625 Token
*t
= tline
, **tt
, *tm
, *head
;
3629 pos
= strchr(tline
->text
, ':');
3632 lst
= atoi(pos
+ 1);
3633 fst
= atoi(tline
->text
+ 1);
3636 * only macros params are accounted so
3637 * if someone passes %0 -- we reject such
3640 if (lst
== 0 || fst
== 0)
3643 /* the values should be sane */
3644 if ((fst
> (int)mac
->nparam
|| fst
< (-(int)mac
->nparam
)) ||
3645 (lst
> (int)mac
->nparam
|| lst
< (-(int)mac
->nparam
)))
3648 fst
= fst
< 0 ? fst
+ (int)mac
->nparam
+ 1: fst
;
3649 lst
= lst
< 0 ? lst
+ (int)mac
->nparam
+ 1: lst
;
3651 /* counted from zero */
3655 * it will be at least one token
3657 tm
= mac
->params
[(fst
+ mac
->rotate
) % mac
->nparam
];
3658 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3659 head
= t
, tt
= &t
->next
;
3661 for (i
= fst
+ 1; i
<= lst
; i
++) {
3662 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3663 *tt
= t
, tt
= &t
->next
;
3664 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3665 tm
= mac
->params
[j
];
3666 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3667 *tt
= t
, tt
= &t
->next
;
3670 for (i
= fst
- 1; i
>= lst
; i
--) {
3671 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
3672 *tt
= t
, tt
= &t
->next
;
3673 j
= (i
+ mac
->rotate
) % mac
->nparam
;
3674 tm
= mac
->params
[j
];
3675 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
3676 *tt
= t
, tt
= &t
->next
;
3684 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
3690 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3691 * %-n) and MMacro-local identifiers (%%foo) as well as
3692 * macro indirection (%[...]) and range (%{..:..}).
3694 static Token
*expand_mmac_params(Token
* tline
)
3696 Token
*t
, *tt
, **tail
, *thead
;
3697 bool changed
= false;
3704 if (tline
->type
== TOK_PREPROC_ID
&&
3705 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
3706 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
3707 tline
->text
[1] == '%')) {
3709 int type
= 0, cc
; /* type = 0 to placate optimisers */
3716 tline
= tline
->next
;
3719 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3720 mac
= mac
->next_active
;
3722 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3724 pos
= strchr(t
->text
, ':');
3726 switch (t
->text
[1]) {
3728 * We have to make a substitution of one of the
3729 * forms %1, %-1, %+1, %%foo, %0.
3733 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3734 text
= nasm_strdup(tmpbuf
);
3738 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3740 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3743 n
= atoi(t
->text
+ 2) - 1;
3744 if (n
>= mac
->nparam
)
3747 if (mac
->nparam
> 1)
3748 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3749 tt
= mac
->params
[n
];
3754 "macro parameter %d is not a condition code",
3759 if (inverse_ccs
[cc
] == -1) {
3761 "condition code `%s' is not invertible",
3765 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3769 n
= atoi(t
->text
+ 2) - 1;
3770 if (n
>= mac
->nparam
)
3773 if (mac
->nparam
> 1)
3774 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3775 tt
= mac
->params
[n
];
3780 "macro parameter %d is not a condition code",
3785 text
= nasm_strdup(conditions
[cc
]);
3789 n
= atoi(t
->text
+ 1) - 1;
3790 if (n
>= mac
->nparam
)
3793 if (mac
->nparam
> 1)
3794 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3795 tt
= mac
->params
[n
];
3798 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3799 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3800 tail
= &(*tail
)->next
;
3804 text
= NULL
; /* we've done it here */
3809 * seems we have a parameters range here
3811 Token
*head
, **last
;
3812 head
= expand_mmac_params_range(mac
, t
, &last
);
3833 } else if (tline
->type
== TOK_INDIRECT
) {
3835 tline
= tline
->next
;
3836 tt
= tokenize(t
->text
);
3837 tt
= expand_mmac_params(tt
);
3838 tt
= expand_smacro(tt
);
3841 tt
->a
.mac
= NULL
; /* Necessary? */
3849 tline
= tline
->next
;
3857 paste_tokens(&thead
, false);
3863 * Expand all single-line macro calls made in the given line.
3864 * Return the expanded version of the line. The original is deemed
3865 * to be destroyed in the process. (In reality we'll just move
3866 * Tokens from input to output a lot of the time, rather than
3867 * actually bothering to destroy and replicate.)
3870 static Token
*expand_smacro(Token
* tline
)
3872 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3873 SMacro
*head
= NULL
, *m
;
3876 unsigned int nparam
, sparam
;
3878 Token
*org_tline
= tline
;
3881 int deadman
= DEADMAN_LIMIT
;
3885 * Trick: we should avoid changing the start token pointer since it can
3886 * be contained in "next" field of other token. Because of this
3887 * we allocate a copy of first token and work with it; at the end of
3888 * routine we copy it back
3891 tline
= new_Token(org_tline
->next
, org_tline
->type
,
3892 org_tline
->text
, 0);
3893 tline
->a
.mac
= org_tline
->a
.mac
;
3894 nasm_free(org_tline
->text
);
3895 org_tline
->text
= NULL
;
3898 expanded
= true; /* Always expand %+ at least once */
3904 while (tline
) { /* main token loop */
3906 error(ERR_NONFATAL
, "interminable macro recursion");
3910 if ((mname
= tline
->text
)) {
3911 /* if this token is a local macro, look in local context */
3912 if (tline
->type
== TOK_ID
) {
3913 head
= (SMacro
*)hash_findix(&smacros
, mname
);
3914 } else if (tline
->type
== TOK_PREPROC_ID
) {
3915 ctx
= get_ctx(mname
, &mname
, true);
3916 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
3921 * We've hit an identifier. As in is_mmacro below, we first
3922 * check whether the identifier is a single-line macro at
3923 * all, then think about checking for parameters if
3926 list_for_each(m
, head
)
3927 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3933 if (m
->nparam
== 0) {
3935 * Simple case: the macro is parameterless. Discard the
3936 * one token that the macro call took, and push the
3937 * expansion back on the to-do stack.
3939 if (!m
->expansion
) {
3940 if (!strcmp("__FILE__", m
->name
)) {
3943 src_get(&num
, &file
);
3944 tline
->text
= nasm_quote(file
, strlen(file
));
3945 tline
->type
= TOK_STRING
;
3949 if (!strcmp("__LINE__", m
->name
)) {
3950 nasm_free(tline
->text
);
3951 make_tok_num(tline
, src_get_linnum());
3954 if (!strcmp("__BITS__", m
->name
)) {
3955 nasm_free(tline
->text
);
3956 make_tok_num(tline
, globalbits
);
3959 tline
= delete_Token(tline
);
3964 * Complicated case: at least one macro with this name
3965 * exists and takes parameters. We must find the
3966 * parameters in the call, count them, find the SMacro
3967 * that corresponds to that form of the macro call, and
3968 * substitute for the parameters when we expand. What a
3971 /*tline = tline->next;
3972 skip_white_(tline); */
3975 while (tok_type_(t
, TOK_SMAC_END
)) {
3976 t
->a
.mac
->in_progress
= false;
3978 t
= tline
->next
= delete_Token(t
);
3981 } while (tok_type_(tline
, TOK_WHITESPACE
));
3982 if (!tok_is_(tline
, "(")) {
3984 * This macro wasn't called with parameters: ignore
3985 * the call. (Behaviour borrowed from gnu cpp.)
3994 sparam
= PARAM_DELTA
;
3995 params
= nasm_malloc(sparam
* sizeof(Token
*));
3996 params
[0] = tline
->next
;
3997 paramsize
= nasm_malloc(sparam
* sizeof(int));
3999 while (true) { /* parameter loop */
4001 * For some unusual expansions
4002 * which concatenates function call
4005 while (tok_type_(t
, TOK_SMAC_END
)) {
4006 t
->a
.mac
->in_progress
= false;
4008 t
= tline
->next
= delete_Token(t
);
4014 "macro call expects terminating `)'");
4017 if (tline
->type
== TOK_WHITESPACE
4019 if (paramsize
[nparam
])
4022 params
[nparam
] = tline
->next
;
4023 continue; /* parameter loop */
4025 if (tline
->type
== TOK_OTHER
4026 && tline
->text
[1] == 0) {
4027 char ch
= tline
->text
[0];
4028 if (ch
== ',' && !paren
&& brackets
<= 0) {
4029 if (++nparam
>= sparam
) {
4030 sparam
+= PARAM_DELTA
;
4031 params
= nasm_realloc(params
,
4032 sparam
* sizeof(Token
*));
4033 paramsize
= nasm_realloc(paramsize
,
4034 sparam
* sizeof(int));
4036 params
[nparam
] = tline
->next
;
4037 paramsize
[nparam
] = 0;
4039 continue; /* parameter loop */
4042 (brackets
> 0 || (brackets
== 0 &&
4043 !paramsize
[nparam
])))
4045 if (!(brackets
++)) {
4046 params
[nparam
] = tline
->next
;
4047 continue; /* parameter loop */
4050 if (ch
== '}' && brackets
> 0)
4051 if (--brackets
== 0) {
4053 continue; /* parameter loop */
4055 if (ch
== '(' && !brackets
)
4057 if (ch
== ')' && brackets
<= 0)
4063 error(ERR_NONFATAL
, "braces do not "
4064 "enclose all of macro parameter");
4066 paramsize
[nparam
] += white
+ 1;
4068 } /* parameter loop */
4070 while (m
&& (m
->nparam
!= nparam
||
4071 mstrcmp(m
->name
, mname
,
4075 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4076 "macro `%s' exists, "
4077 "but not taking %d parameters",
4078 mstart
->text
, nparam
);
4081 if (m
&& m
->in_progress
)
4083 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4085 * Design question: should we handle !tline, which
4086 * indicates missing ')' here, or expand those
4087 * macros anyway, which requires the (t) test a few
4091 nasm_free(paramsize
);
4095 * Expand the macro: we are placed on the last token of the
4096 * call, so that we can easily split the call from the
4097 * following tokens. We also start by pushing an SMAC_END
4098 * token for the cycle removal.
4105 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4107 m
->in_progress
= true;
4109 list_for_each(t
, m
->expansion
) {
4110 if (t
->type
>= TOK_SMAC_PARAM
) {
4111 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4115 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4116 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4118 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4124 } else if (t
->type
== TOK_PREPROC_Q
) {
4125 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4127 } else if (t
->type
== TOK_PREPROC_QQ
) {
4128 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4131 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4137 * Having done that, get rid of the macro call, and clean
4138 * up the parameters.
4141 nasm_free(paramsize
);
4144 continue; /* main token loop */
4149 if (tline
->type
== TOK_SMAC_END
) {
4150 tline
->a
.mac
->in_progress
= false;
4151 tline
= delete_Token(tline
);
4154 tline
= tline
->next
;
4162 * Now scan the entire line and look for successive TOK_IDs that resulted
4163 * after expansion (they can't be produced by tokenize()). The successive
4164 * TOK_IDs should be concatenated.
4165 * Also we look for %+ tokens and concatenate the tokens before and after
4166 * them (without white spaces in between).
4168 if (expanded
&& paste_tokens(&thead
, true)) {
4170 * If we concatenated something, *and* we had previously expanded
4171 * an actual macro, scan the lines again for macros...
4181 *org_tline
= *thead
;
4182 /* since we just gave text to org_line, don't free it */
4184 delete_Token(thead
);
4186 /* the expression expanded to empty line;
4187 we can't return NULL for some reasons
4188 we just set the line to a single WHITESPACE token. */
4189 memset(org_tline
, 0, sizeof(*org_tline
));
4190 org_tline
->text
= NULL
;
4191 org_tline
->type
= TOK_WHITESPACE
;
4200 * Similar to expand_smacro but used exclusively with macro identifiers
4201 * right before they are fetched in. The reason is that there can be
4202 * identifiers consisting of several subparts. We consider that if there
4203 * are more than one element forming the name, user wants a expansion,
4204 * otherwise it will be left as-is. Example:
4208 * the identifier %$abc will be left as-is so that the handler for %define
4209 * will suck it and define the corresponding value. Other case:
4211 * %define _%$abc cde
4213 * In this case user wants name to be expanded *before* %define starts
4214 * working, so we'll expand %$abc into something (if it has a value;
4215 * otherwise it will be left as-is) then concatenate all successive
4218 static Token
*expand_id(Token
* tline
)
4220 Token
*cur
, *oldnext
= NULL
;
4222 if (!tline
|| !tline
->next
)
4227 (cur
->next
->type
== TOK_ID
||
4228 cur
->next
->type
== TOK_PREPROC_ID
4229 || cur
->next
->type
== TOK_NUMBER
))
4232 /* If identifier consists of just one token, don't expand */
4237 oldnext
= cur
->next
; /* Detach the tail past identifier */
4238 cur
->next
= NULL
; /* so that expand_smacro stops here */
4241 tline
= expand_smacro(tline
);
4244 /* expand_smacro possibly changhed tline; re-scan for EOL */
4246 while (cur
&& cur
->next
)
4249 cur
->next
= oldnext
;
4256 * Determine whether the given line constitutes a multi-line macro
4257 * call, and return the MMacro structure called if so. Doesn't have
4258 * to check for an initial label - that's taken care of in
4259 * expand_mmacro - but must check numbers of parameters. Guaranteed
4260 * to be called with tline->type == TOK_ID, so the putative macro
4261 * name is easy to find.
4263 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4269 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4272 * Efficiency: first we see if any macro exists with the given
4273 * name. If not, we can return NULL immediately. _Then_ we
4274 * count the parameters, and then we look further along the
4275 * list if necessary to find the proper MMacro.
4277 list_for_each(m
, head
)
4278 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4284 * OK, we have a potential macro. Count and demarcate the
4287 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4290 * So we know how many parameters we've got. Find the MMacro
4291 * structure that handles this number.
4294 if (m
->nparam_min
<= nparam
4295 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4297 * This one is right. Just check if cycle removal
4298 * prohibits us using it before we actually celebrate...
4300 if (m
->in_progress
> m
->max_depth
) {
4301 if (m
->max_depth
> 0) {
4303 "reached maximum recursion depth of %i",
4310 * It's right, and we can use it. Add its default
4311 * parameters to the end of our list if necessary.
4313 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4315 nasm_realloc(params
,
4316 ((m
->nparam_min
+ m
->ndefs
+
4317 1) * sizeof(*params
)));
4318 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4319 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4324 * If we've gone over the maximum parameter count (and
4325 * we're in Plus mode), ignore parameters beyond
4328 if (m
->plus
&& nparam
> m
->nparam_max
)
4329 nparam
= m
->nparam_max
;
4331 * Then terminate the parameter list, and leave.
4333 if (!params
) { /* need this special case */
4334 params
= nasm_malloc(sizeof(*params
));
4337 params
[nparam
] = NULL
;
4338 *params_array
= params
;
4342 * This one wasn't right: look for the next one with the
4345 list_for_each(m
, m
->next
)
4346 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4351 * After all that, we didn't find one with the right number of
4352 * parameters. Issue a warning, and fail to expand the macro.
4354 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4355 "macro `%s' exists, but not taking %d parameters",
4356 tline
->text
, nparam
);
4363 * Save MMacro invocation specific fields in
4364 * preparation for a recursive macro expansion
4366 static void push_mmacro(MMacro
*m
)
4368 MMacroInvocation
*i
;
4370 i
= nasm_malloc(sizeof(MMacroInvocation
));
4372 i
->params
= m
->params
;
4373 i
->iline
= m
->iline
;
4374 i
->nparam
= m
->nparam
;
4375 i
->rotate
= m
->rotate
;
4376 i
->paramlen
= m
->paramlen
;
4377 i
->unique
= m
->unique
;
4378 i
->condcnt
= m
->condcnt
;
4384 * Restore MMacro invocation specific fields that were
4385 * saved during a previous recursive macro expansion
4387 static void pop_mmacro(MMacro
*m
)
4389 MMacroInvocation
*i
;
4394 m
->params
= i
->params
;
4395 m
->iline
= i
->iline
;
4396 m
->nparam
= i
->nparam
;
4397 m
->rotate
= i
->rotate
;
4398 m
->paramlen
= i
->paramlen
;
4399 m
->unique
= i
->unique
;
4400 m
->condcnt
= i
->condcnt
;
4407 * Expand the multi-line macro call made by the given line, if
4408 * there is one to be expanded. If there is, push the expansion on
4409 * istk->expansion and return 1. Otherwise return 0.
4411 static int expand_mmacro(Token
* tline
)
4413 Token
*startline
= tline
;
4414 Token
*label
= NULL
;
4415 int dont_prepend
= 0;
4416 Token
**params
, *t
, *mtok
, *tt
;
4419 int i
, nparam
, *paramlen
;
4424 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4425 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4428 m
= is_mmacro(t
, ¶ms
);
4434 * We have an id which isn't a macro call. We'll assume
4435 * it might be a label; we'll also check to see if a
4436 * colon follows it. Then, if there's another id after
4437 * that lot, we'll check it again for macro-hood.
4441 if (tok_type_(t
, TOK_WHITESPACE
))
4442 last
= t
, t
= t
->next
;
4443 if (tok_is_(t
, ":")) {
4445 last
= t
, t
= t
->next
;
4446 if (tok_type_(t
, TOK_WHITESPACE
))
4447 last
= t
, t
= t
->next
;
4449 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4457 * Fix up the parameters: this involves stripping leading and
4458 * trailing whitespace, then stripping braces if they are
4461 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4462 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4464 for (i
= 0; params
[i
]; i
++) {
4466 int comma
= (!m
->plus
|| i
< nparam
- 1);
4470 if (tok_is_(t
, "{"))
4471 t
= t
->next
, brace
= true, comma
= false;
4475 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4476 break; /* ... because we have hit a comma */
4477 if (comma
&& t
->type
== TOK_WHITESPACE
4478 && tok_is_(t
->next
, ","))
4479 break; /* ... or a space then a comma */
4480 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4481 break; /* ... or a brace */
4488 * OK, we have a MMacro structure together with a set of
4489 * parameters. We must now go through the expansion and push
4490 * copies of each Line on to istk->expansion. Substitution of
4491 * parameter tokens and macro-local tokens doesn't get done
4492 * until the single-line macro substitution process; this is
4493 * because delaying them allows us to change the semantics
4494 * later through %rotate.
4496 * First, push an end marker on to istk->expansion, mark this
4497 * macro as in progress, and set up its invocation-specific
4500 ll
= nasm_malloc(sizeof(Line
));
4501 ll
->next
= istk
->expansion
;
4504 istk
->expansion
= ll
;
4507 * Save the previous MMacro expansion in the case of
4510 if (m
->max_depth
&& m
->in_progress
)
4518 m
->paramlen
= paramlen
;
4519 m
->unique
= unique
++;
4523 m
->next_active
= istk
->mstk
;
4526 list_for_each(l
, m
->expansion
) {
4529 ll
= nasm_malloc(sizeof(Line
));
4530 ll
->finishes
= NULL
;
4531 ll
->next
= istk
->expansion
;
4532 istk
->expansion
= ll
;
4535 list_for_each(t
, l
->first
) {
4539 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4541 case TOK_PREPROC_QQ
:
4542 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4544 case TOK_PREPROC_ID
:
4545 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4553 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4562 * If we had a label, push it on as the first line of
4563 * the macro expansion.
4566 if (dont_prepend
< 0)
4567 free_tlist(startline
);
4569 ll
= nasm_malloc(sizeof(Line
));
4570 ll
->finishes
= NULL
;
4571 ll
->next
= istk
->expansion
;
4572 istk
->expansion
= ll
;
4573 ll
->first
= startline
;
4574 if (!dont_prepend
) {
4576 label
= label
->next
;
4577 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4582 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4587 /* The function that actually does the error reporting */
4588 static void verror(int severity
, const char *fmt
, va_list arg
)
4592 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4594 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4595 nasm_error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4596 istk
->mstk
->lineno
, buff
);
4598 nasm_error(severity
, "%s", buff
);
4602 * Since preprocessor always operate only on the line that didn't
4603 * arrived yet, we should always use ERR_OFFBY1.
4605 static void error(int severity
, const char *fmt
, ...)
4609 /* If we're in a dead branch of IF or something like it, ignore the error */
4610 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4614 verror(severity
, fmt
, arg
);
4619 * Because %else etc are evaluated in the state context
4620 * of the previous branch, errors might get lost with error():
4621 * %if 0 ... %else trailing garbage ... %endif
4622 * So %else etc should report errors with this function.
4624 static void error_precond(int severity
, const char *fmt
, ...)
4628 /* Only ignore the error if it's really in a dead branch */
4629 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4633 verror(severity
, fmt
, arg
);
4638 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4643 istk
= nasm_malloc(sizeof(Include
));
4646 istk
->expansion
= NULL
;
4648 istk
->fp
= fopen(file
, "r");
4650 src_set_fname(nasm_strdup(file
));
4654 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4657 nested_mac_count
= 0;
4658 nested_rep_count
= 0;
4661 if (tasm_compatible_mode
) {
4662 stdmacpos
= nasm_stdmac
;
4664 stdmacpos
= nasm_stdmac_after_tasm
;
4666 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4671 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4672 * The caller, however, will also pass in 3 for preprocess-only so
4673 * we can set __PASS__ accordingly.
4675 pass
= apass
> 2 ? 2 : apass
;
4677 dephead
= deptail
= deplist
;
4679 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4681 strcpy(sl
->str
, file
);
4683 deptail
= &sl
->next
;
4687 * Define the __PASS__ macro. This is defined here unlike
4688 * all the other builtins, because it is special -- it varies between
4691 t
= nasm_malloc(sizeof(*t
));
4693 make_tok_num(t
, apass
);
4695 define_smacro(NULL
, "__PASS__", true, 0, t
);
4698 static char *pp_getline(void)
4705 * Fetch a tokenized line, either from the macro-expansion
4706 * buffer or from the input file.
4709 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4710 Line
*l
= istk
->expansion
;
4711 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4715 * This is a macro-end marker for a macro with no
4716 * name, which means it's not really a macro at all
4717 * but a %rep block, and the `in_progress' field is
4718 * more than 1, meaning that we still need to
4719 * repeat. (1 means the natural last repetition; 0
4720 * means termination by %exitrep.) We have
4721 * therefore expanded up to the %endrep, and must
4722 * push the whole block on to the expansion buffer
4723 * again. We don't bother to remove the macro-end
4724 * marker: we'd only have to generate another one
4727 l
->finishes
->in_progress
--;
4728 list_for_each(l
, l
->finishes
->expansion
) {
4729 Token
*t
, *tt
, **tail
;
4731 ll
= nasm_malloc(sizeof(Line
));
4732 ll
->next
= istk
->expansion
;
4733 ll
->finishes
= NULL
;
4737 list_for_each(t
, l
->first
) {
4738 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4739 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4744 istk
->expansion
= ll
;
4748 * Check whether a `%rep' was started and not ended
4749 * within this macro expansion. This can happen and
4750 * should be detected. It's a fatal error because
4751 * I'm too confused to work out how to recover
4757 "defining with name in expansion");
4758 else if (istk
->mstk
->name
)
4760 "`%%rep' without `%%endrep' within"
4761 " expansion of macro `%s'",
4766 * FIXME: investigate the relationship at this point between
4767 * istk->mstk and l->finishes
4770 MMacro
*m
= istk
->mstk
;
4771 istk
->mstk
= m
->next_active
;
4774 * This was a real macro call, not a %rep, and
4775 * therefore the parameter information needs to
4780 l
->finishes
->in_progress
--;
4782 nasm_free(m
->params
);
4783 free_tlist(m
->iline
);
4784 nasm_free(m
->paramlen
);
4785 l
->finishes
->in_progress
= 0;
4790 istk
->expansion
= l
->next
;
4792 list
->downlevel(LIST_MACRO
);
4795 while (1) { /* until we get a line we can use */
4797 if (istk
->expansion
) { /* from a macro expansion */
4799 Line
*l
= istk
->expansion
;
4801 istk
->mstk
->lineno
++;
4803 istk
->expansion
= l
->next
;
4805 p
= detoken(tline
, false);
4806 list
->line(LIST_MACRO
, p
);
4811 if (line
) { /* from the current input file */
4812 line
= prepreproc(line
);
4813 tline
= tokenize(line
);
4818 * The current file has ended; work down the istk
4825 "expected `%%endif' before end of file");
4826 /* only set line and file name if there's a next node */
4828 src_set_linnum(i
->lineno
);
4829 nasm_free(src_set_fname(i
->fname
));
4832 list
->downlevel(LIST_INCLUDE
);
4836 if (istk
->expansion
&& istk
->expansion
->finishes
)
4842 * We must expand MMacro parameters and MMacro-local labels
4843 * _before_ we plunge into directive processing, to cope
4844 * with things like `%define something %1' such as STRUC
4845 * uses. Unless we're _defining_ a MMacro, in which case
4846 * those tokens should be left alone to go into the
4847 * definition; and unless we're in a non-emitting
4848 * condition, in which case we don't want to meddle with
4851 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4852 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4853 tline
= expand_mmac_params(tline
);
4857 * Check the line to see if it's a preprocessor directive.
4859 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4861 } else if (defining
) {
4863 * We're defining a multi-line macro. We emit nothing
4865 * shove the tokenized line on to the macro definition.
4867 Line
*l
= nasm_malloc(sizeof(Line
));
4868 l
->next
= defining
->expansion
;
4871 defining
->expansion
= l
;
4873 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4875 * We're in a non-emitting branch of a condition block.
4876 * Emit nothing at all, not even a blank line: when we
4877 * emerge from the condition we'll give a line-number
4878 * directive so we keep our place correctly.
4882 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4884 * We're in a %rep block which has been terminated, so
4885 * we're walking through to the %endrep without
4886 * emitting anything. Emit nothing at all, not even a
4887 * blank line: when we emerge from the %rep block we'll
4888 * give a line-number directive so we keep our place
4894 tline
= expand_smacro(tline
);
4895 if (!expand_mmacro(tline
)) {
4897 * De-tokenize the line again, and emit it.
4899 line
= detoken(tline
, true);
4903 continue; /* expand_mmacro calls free_tlist */
4911 static void pp_cleanup(int pass
)
4914 if (defining
->name
) {
4916 "end of file while still defining macro `%s'",
4919 error(ERR_NONFATAL
, "end of file while still in %%rep");
4922 free_mmacro(defining
);
4932 nasm_free(i
->fname
);
4937 nasm_free(src_set_fname(NULL
));
4942 while ((i
= ipath
)) {
4951 void pp_include_path(char *path
)
4955 i
= nasm_malloc(sizeof(IncPath
));
4956 i
->path
= path
? nasm_strdup(path
) : NULL
;
4969 void pp_pre_include(char *fname
)
4971 Token
*inc
, *space
, *name
;
4974 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4975 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4976 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4978 l
= nasm_malloc(sizeof(Line
));
4985 void pp_pre_define(char *definition
)
4991 equals
= strchr(definition
, '=');
4992 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4993 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4996 space
->next
= tokenize(definition
);
5000 l
= nasm_malloc(sizeof(Line
));
5007 void pp_pre_undefine(char *definition
)
5012 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5013 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5014 space
->next
= tokenize(definition
);
5016 l
= nasm_malloc(sizeof(Line
));
5024 * Added by Keith Kanios:
5026 * This function is used to assist with "runtime" preprocessor
5027 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5029 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5030 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5033 void pp_runtime(char *definition
)
5037 def
= tokenize(definition
);
5038 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5043 void pp_extra_stdmac(macros_t
*macros
)
5045 extrastdmac
= macros
;
5048 static void make_tok_num(Token
* tok
, int64_t val
)
5051 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5052 tok
->text
= nasm_strdup(numbuf
);
5053 tok
->type
= TOK_NUMBER
;