1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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 */
159 /* Store the definition of a multi-line macro, as defined in a
160 * previous recursive macro expansion.
162 struct MMacroInvocation
{
163 MMacroInvocation
*prev
; /* previous invocation */
164 Token
**params
; /* actual parameters */
165 Token
*iline
; /* invocation line */
166 unsigned int nparam
, rotate
;
173 * The context stack is composed of a linked list of these.
178 struct hash_table localmac
;
183 * This is the internal form which we break input lines up into.
184 * Typically stored in linked lists.
186 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
187 * necessarily used as-is, but is intended to denote the number of
188 * the substituted parameter. So in the definition
190 * %define a(x,y) ( (x) & ~(y) )
192 * the token representing `x' will have its type changed to
193 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
197 * which doesn't need quotes around it. Used in the pre-include
198 * mechanism as an alternative to trying to find a sensible type of
199 * quote to use on the filename we were passed.
202 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
203 TOK_PREPROC_ID
, TOK_STRING
,
204 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
206 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
208 TOK_INDIRECT
, /* %[...] */
209 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
210 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
217 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
218 size_t len
; /* scratch length field */
219 } a
; /* Auxiliary data */
220 enum pp_token_type type
;
224 * Multi-line macro definitions are stored as a linked list of
225 * these, which is essentially a container to allow several linked
228 * Note that in this module, linked lists are treated as stacks
229 * wherever possible. For this reason, Lines are _pushed_ on to the
230 * `expansion' field in MMacro structures, so that the linked list,
231 * if walked, would give the macro lines in reverse order; this
232 * means that we can walk the list when expanding a macro, and thus
233 * push the lines on to the `expansion' field in _istk_ in reverse
234 * order (so that when popped back off they are in the right
235 * order). It may seem cockeyed, and it relies on my design having
236 * an even number of steps in, but it works...
238 * Some of these structures, rather than being actual lines, are
239 * markers delimiting the end of the expansion of a given macro.
240 * This is for use in the cycle-tracking and %rep-handling code.
241 * Such structures have `finishes' non-NULL, and `first' NULL. All
242 * others have `finishes' NULL, but `first' may still be NULL if
252 * To handle an arbitrary level of file inclusion, we maintain a
253 * stack (ie linked list) of these things.
262 MMacro
*mstk
; /* stack of active macros/reps */
266 * Include search path. This is simply a list of strings which get
267 * prepended, in turn, to the name of an include file, in an
268 * attempt to find the file if it's not in the current directory.
276 * Conditional assembly: we maintain a separate stack of these for
277 * each level of file inclusion. (The only reason we keep the
278 * stacks separate is to ensure that a stray `%endif' in a file
279 * included from within the true branch of a `%if' won't terminate
280 * it and cause confusion: instead, rightly, it'll cause an error.)
288 * These states are for use just after %if or %elif: IF_TRUE
289 * means the condition has evaluated to truth so we are
290 * currently emitting, whereas IF_FALSE means we are not
291 * currently emitting but will start doing so if a %else comes
292 * up. In these states, all directives are admissible: %elif,
293 * %else and %endif. (And of course %if.)
295 COND_IF_TRUE
, COND_IF_FALSE
,
297 * These states come up after a %else: ELSE_TRUE means we're
298 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
299 * any %elif or %else will cause an error.
301 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
303 * These states mean that we're not emitting now, and also that
304 * nothing until %endif will be emitted at all. COND_DONE is
305 * used when we've had our moment of emission
306 * and have now started seeing %elifs. COND_NEVER is used when
307 * the condition construct in question is contained within a
308 * non-emitting branch of a larger condition construct,
309 * or if there is an error.
311 COND_DONE
, COND_NEVER
313 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316 * These defines are used as the possible return values for do_directive
318 #define NO_DIRECTIVE_FOUND 0
319 #define DIRECTIVE_FOUND 1
322 * This define sets the upper limit for smacro and recursive mmacro
325 #define DEADMAN_LIMIT (1 << 20)
328 * Condition codes. Note that we use c_ prefix not C_ because C_ is
329 * used in nasm.h for the "real" condition codes. At _this_ level,
330 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
331 * ones, so we need a different enum...
333 static const char * const conditions
[] = {
334 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
335 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
336 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
339 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
340 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
341 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
344 static const enum pp_conds inverse_ccs
[] = {
345 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
346 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
,
347 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
353 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
354 static int is_condition(enum preproc_token arg
)
356 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
359 /* For TASM compatibility we need to be able to recognise TASM compatible
360 * conditional compilation directives. Using the NASM pre-processor does
361 * not work, so we look for them specifically from the following list and
362 * then jam in the equivalent NASM directive into the input stream.
366 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
367 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
370 static const char * const tasm_directives
[] = {
371 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
372 "ifndef", "include", "local"
375 static int StackSize
= 4;
376 static char *StackPointer
= "ebp";
377 static int ArgOffset
= 8;
378 static int LocalOffset
= 0;
380 static Context
*cstk
;
381 static Include
*istk
;
382 static IncPath
*ipath
= NULL
;
384 static int pass
; /* HACK: pass 0 = generate dependencies only */
385 static StrList
**dephead
, **deptail
; /* Dependency list */
387 static uint64_t unique
; /* unique identifier numbers */
389 static Line
*predef
= NULL
;
390 static bool do_predef
;
392 static ListGen
*list
;
395 * The current set of multi-line macros we have defined.
397 static struct hash_table mmacros
;
400 * The current set of single-line macros we have defined.
402 static struct hash_table smacros
;
405 * The multi-line macro we are currently defining, or the %rep
406 * block we are currently reading, if any.
408 static MMacro
*defining
;
410 static uint64_t nested_mac_count
;
411 static uint64_t nested_rep_count
;
414 * The number of macro parameters to allocate space for at a time.
416 #define PARAM_DELTA 16
419 * The standard macro set: defined in macros.c in the array nasm_stdmac.
420 * This gives our position in the macro set, when we're processing it.
422 static macros_t
*stdmacpos
;
425 * The extra standard macros that come from the object format, if
428 static macros_t
*extrastdmac
= NULL
;
429 static bool any_extrastdmac
;
432 * Tokens are allocated in blocks to improve speed
434 #define TOKEN_BLOCKSIZE 4096
435 static Token
*freeTokens
= NULL
;
441 static Blocks blocks
= { NULL
, NULL
};
444 * Forward declarations.
446 static Token
*expand_mmac_params(Token
* tline
);
447 static Token
*expand_smacro(Token
* tline
);
448 static Token
*expand_id(Token
* tline
);
449 static Context
*get_ctx(const char *name
, const char **namep
,
451 static void make_tok_num(Token
* tok
, int64_t val
);
452 static void error(int severity
, const char *fmt
, ...);
453 static void error_precond(int severity
, const char *fmt
, ...);
454 static void *new_Block(size_t size
);
455 static void delete_Blocks(void);
456 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
457 const char *text
, int txtlen
);
458 static Token
*delete_Token(Token
* t
);
461 * Macros for safe checking of token pointers, avoid *(NULL)
463 #define tok_type_(x,t) ((x) && (x)->type == (t))
464 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
465 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
466 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
468 /* Handle TASM specific directives, which do not contain a % in
469 * front of them. We do it here because I could not find any other
470 * place to do it for the moment, and it is a hack (ideally it would
471 * be nice to be able to use the NASM pre-processor to do it).
473 static char *check_tasm_directive(char *line
)
475 int32_t i
, j
, k
, m
, len
;
476 char *p
= line
, *oldline
, oldchar
;
478 /* Skip whitespace */
479 while (nasm_isspace(*p
) && *p
!= 0)
482 /* Binary search for the directive name */
484 j
= elements(tasm_directives
);
486 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
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
);
565 * Free a linked list of lines.
567 static void free_llist(Line
* list
)
573 free_tlist(l
->first
);
581 static void free_mmacro(MMacro
* m
)
584 free_tlist(m
->dlist
);
585 nasm_free(m
->defaults
);
586 free_llist(m
->expansion
);
591 * Free all currently defined macros, and free the hash tables
593 static void free_smacro_table(struct hash_table
*smt
)
597 struct hash_tbl_node
*it
= NULL
;
599 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
600 nasm_free((void *)key
);
602 SMacro
*ns
= s
->next
;
604 free_tlist(s
->expansion
);
612 static void free_mmacro_table(struct hash_table
*mmt
)
616 struct hash_tbl_node
*it
= NULL
;
619 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
620 nasm_free((void *)key
);
622 MMacro
*nm
= m
->next
;
630 static void free_macros(void)
632 free_smacro_table(&smacros
);
633 free_mmacro_table(&mmacros
);
637 * Initialize the hash tables
639 static void init_macros(void)
641 hash_init(&smacros
, HASH_LARGE
);
642 hash_init(&mmacros
, HASH_LARGE
);
646 * Pop the context stack.
648 static void ctx_pop(void)
653 free_smacro_table(&c
->localmac
);
659 * Search for a key in the hash index; adding it if necessary
660 * (in which case we initialize the data pointer to NULL.)
663 hash_findi_add(struct hash_table
*hash
, const char *str
)
665 struct hash_insert hi
;
669 r
= hash_findi(hash
, str
, &hi
);
673 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
674 return hash_add(&hi
, strx
, NULL
);
678 * Like hash_findi, but returns the data element rather than a pointer
679 * to it. Used only when not adding a new element, hence no third
683 hash_findix(struct hash_table
*hash
, const char *str
)
687 p
= hash_findi(hash
, str
, NULL
);
688 return p
? *p
: NULL
;
691 #define BUF_DELTA 512
693 * Read a line from the top file in istk, handling multiple CR/LFs
694 * at the end of the line read, and handling spurious ^Zs. Will
695 * return lines from the standard macro set if this has not already
698 static char *read_line(void)
700 char *buffer
, *p
, *q
;
701 int bufsize
, continued_count
;
705 const unsigned char *p
= stdmacpos
;
710 len
+= pp_directives_len
[c
-0x80]+1;
714 ret
= nasm_malloc(len
+1);
716 while ((c
= *stdmacpos
++)) {
718 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
719 q
+= pp_directives_len
[c
-0x80];
729 /* This was the last of the standard macro chain... */
731 if (any_extrastdmac
) {
732 stdmacpos
= extrastdmac
;
733 any_extrastdmac
= false;
734 } else if (do_predef
) {
736 Token
*head
, **tail
, *t
;
739 * Nasty hack: here we push the contents of
740 * `predef' on to the top-level expansion stack,
741 * since this is the most convenient way to
742 * implement the pre-include and pre-define
745 for (pd
= predef
; pd
; pd
= pd
->next
) {
748 for (t
= pd
->first
; t
; t
= t
->next
) {
749 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
750 tail
= &(*tail
)->next
;
752 l
= nasm_malloc(sizeof(Line
));
753 l
->next
= istk
->expansion
;
765 buffer
= nasm_malloc(BUF_DELTA
);
769 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
773 if (p
> buffer
&& p
[-1] == '\n') {
774 /* Convert backslash-CRLF line continuation sequences into
775 nothing at all (for DOS and Windows) */
776 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
781 /* Also convert backslash-LF line continuation sequences into
782 nothing at all (for Unix) */
783 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
791 if (p
- buffer
> bufsize
- 10) {
792 int32_t offset
= p
- buffer
;
793 bufsize
+= BUF_DELTA
;
794 buffer
= nasm_realloc(buffer
, bufsize
);
795 p
= buffer
+ offset
; /* prevent stale-pointer problems */
799 if (!q
&& p
== buffer
) {
804 src_set_linnum(src_get_linnum() + istk
->lineinc
+
805 (continued_count
* istk
->lineinc
));
808 * Play safe: remove CRs as well as LFs, if any of either are
809 * present at the end of the line.
811 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
815 * Handle spurious ^Z, which may be inserted into source files
816 * by some file transfer utilities.
818 buffer
[strcspn(buffer
, "\032")] = '\0';
820 list
->line(LIST_READ
, buffer
);
826 * Tokenize a line of text. This is a very simple process since we
827 * don't need to parse the value out of e.g. numeric tokens: we
828 * simply split one string into many.
830 static Token
*tokenize(char *line
)
833 enum pp_token_type type
;
835 Token
*t
, **tail
= &list
;
841 if (*p
== '+' && !nasm_isdigit(p
[1])) {
844 } else if (nasm_isdigit(*p
) ||
845 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
849 while (nasm_isdigit(*p
));
850 type
= TOK_PREPROC_ID
;
851 } else if (*p
== '{') {
853 while (*p
&& *p
!= '}') {
860 type
= TOK_PREPROC_ID
;
861 } else if (*p
== '[') {
863 line
+= 2; /* Skip the leading %[ */
865 while (lvl
&& (c
= *p
++)) {
877 p
= nasm_skip_string(p
)+1;
887 error(ERR_NONFATAL
, "unterminated %[ construct");
889 } else if (*p
== '?') {
890 type
= TOK_PREPROC_Q
; /* %? */
893 type
= TOK_PREPROC_QQ
; /* %?? */
896 } else if (isidchar(*p
) ||
897 ((*p
== '!' || *p
== '%' || *p
== '$') &&
902 while (isidchar(*p
));
903 type
= TOK_PREPROC_ID
;
909 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
912 while (*p
&& isidchar(*p
))
914 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
919 p
= nasm_skip_string(p
);
924 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
925 /* Handling unterminated strings by UNV */
928 } else if (p
[0] == '$' && p
[1] == '$') {
929 type
= TOK_OTHER
; /* TOKEN_BASE */
931 } else if (isnumstart(*p
)) {
933 bool is_float
= false;
949 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
951 if (*p
== '+' || *p
== '-') {
952 /* e can only be followed by +/- if it is either a
953 prefixed hex number or a floating-point number */
957 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
959 } else if (c
== 'P' || c
== 'p') {
961 if (*p
== '+' || *p
== '-')
963 } else if (isnumchar(c
) || c
== '_')
966 /* we need to deal with consequences of the legacy
967 parser, like "1.nolist" being two tokens
968 (TOK_NUMBER, TOK_ID) here; at least give it
969 a shot for now. In the future, we probably need
970 a flex-based scanner with proper pattern matching
971 to do it as well as it can be done. Nothing in
972 the world is going to help the person who wants
973 0x123.p16 interpreted as two tokens, though. */
978 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
979 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
980 (*r
== 'p' || *r
== 'P')) {
984 break; /* Terminate the token */
988 p
--; /* Point to first character beyond number */
990 if (p
== line
+1 && *line
== '$') {
991 type
= TOK_OTHER
; /* TOKEN_HERE */
993 if (has_e
&& !is_hex
) {
994 /* 1e13 is floating-point, but 1e13h is not */
998 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1000 } else if (nasm_isspace(*p
)) {
1001 type
= TOK_WHITESPACE
;
1003 while (*p
&& nasm_isspace(*p
))
1006 * Whitespace just before end-of-line is discarded by
1007 * pretending it's a comment; whitespace just before a
1008 * comment gets lumped into the comment.
1010 if (!*p
|| *p
== ';') {
1015 } else if (*p
== ';') {
1021 * Anything else is an operator of some kind. We check
1022 * for all the double-character operators (>>, <<, //,
1023 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1024 * else is a single-character operator.
1027 if ((p
[0] == '>' && p
[1] == '>') ||
1028 (p
[0] == '<' && p
[1] == '<') ||
1029 (p
[0] == '/' && p
[1] == '/') ||
1030 (p
[0] == '<' && p
[1] == '=') ||
1031 (p
[0] == '>' && p
[1] == '=') ||
1032 (p
[0] == '=' && p
[1] == '=') ||
1033 (p
[0] == '!' && p
[1] == '=') ||
1034 (p
[0] == '<' && p
[1] == '>') ||
1035 (p
[0] == '&' && p
[1] == '&') ||
1036 (p
[0] == '|' && p
[1] == '|') ||
1037 (p
[0] == '^' && p
[1] == '^')) {
1043 /* Handling unterminated string by UNV */
1046 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1047 t->text[p-line] = *line;
1051 if (type
!= TOK_COMMENT
) {
1052 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1061 * this function allocates a new managed block of memory and
1062 * returns a pointer to the block. The managed blocks are
1063 * deleted only all at once by the delete_Blocks function.
1065 static void *new_Block(size_t size
)
1067 Blocks
*b
= &blocks
;
1069 /* first, get to the end of the linked list */
1072 /* now allocate the requested chunk */
1073 b
->chunk
= nasm_malloc(size
);
1075 /* now allocate a new block for the next request */
1076 b
->next
= nasm_malloc(sizeof(Blocks
));
1077 /* and initialize the contents of the new block */
1078 b
->next
->next
= NULL
;
1079 b
->next
->chunk
= NULL
;
1084 * this function deletes all managed blocks of memory
1086 static void delete_Blocks(void)
1088 Blocks
*a
, *b
= &blocks
;
1091 * keep in mind that the first block, pointed to by blocks
1092 * is a static and not dynamically allocated, so we don't
1097 nasm_free(b
->chunk
);
1106 * this function creates a new Token and passes a pointer to it
1107 * back to the caller. It sets the type and text elements, and
1108 * also the a.mac and next elements to NULL.
1110 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1111 const char *text
, int txtlen
)
1117 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1118 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1119 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1120 freeTokens
[i
].next
= NULL
;
1123 freeTokens
= t
->next
;
1127 if (type
== TOK_WHITESPACE
|| !text
) {
1131 txtlen
= strlen(text
);
1132 t
->text
= nasm_malloc(txtlen
+1);
1133 memcpy(t
->text
, text
, txtlen
);
1134 t
->text
[txtlen
] = '\0';
1139 static Token
*delete_Token(Token
* t
)
1141 Token
*next
= t
->next
;
1143 t
->next
= freeTokens
;
1149 * Convert a line of tokens back into text.
1150 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1151 * will be transformed into ..@ctxnum.xxx
1153 static char *detoken(Token
* tlist
, bool expand_locals
)
1161 for (t
= tlist
; t
; t
= t
->next
) {
1162 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1163 char *p
= getenv(t
->text
+ 2);
1166 t
->text
= nasm_strdup(p
);
1170 /* Expand local macros here and not during preprocessing */
1171 if (expand_locals
&&
1172 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1173 t
->text
[0] == '%' && t
->text
[1] == '$') {
1176 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1179 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1180 p
= nasm_strcat(buffer
, q
);
1185 if (t
->type
== TOK_WHITESPACE
) {
1187 } else if (t
->text
) {
1188 len
+= strlen(t
->text
);
1191 p
= line
= nasm_malloc(len
+ 1);
1192 for (t
= tlist
; t
; t
= t
->next
) {
1193 if (t
->type
== TOK_WHITESPACE
) {
1195 } else if (t
->text
) {
1206 * A scanner, suitable for use by the expression evaluator, which
1207 * operates on a line of Tokens. Expects a pointer to a pointer to
1208 * the first token in the line to be passed in as its private_data
1211 * FIX: This really needs to be unified with stdscan.
1213 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1215 Token
**tlineptr
= private_data
;
1217 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1221 *tlineptr
= tline
? tline
->next
: NULL
;
1223 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1224 tline
->type
== TOK_COMMENT
));
1227 return tokval
->t_type
= TOKEN_EOS
;
1229 tokval
->t_charptr
= tline
->text
;
1231 if (tline
->text
[0] == '$' && !tline
->text
[1])
1232 return tokval
->t_type
= TOKEN_HERE
;
1233 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1234 return tokval
->t_type
= TOKEN_BASE
;
1236 if (tline
->type
== TOK_ID
) {
1237 p
= tokval
->t_charptr
= tline
->text
;
1239 tokval
->t_charptr
++;
1240 return tokval
->t_type
= TOKEN_ID
;
1243 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1244 if (r
>= p
+MAX_KEYWORD
)
1245 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1246 *s
++ = nasm_tolower(*r
);
1249 /* right, so we have an identifier sitting in temp storage. now,
1250 * is it actually a register or instruction name, or what? */
1251 return nasm_token_hash(ourcopy
, tokval
);
1254 if (tline
->type
== TOK_NUMBER
) {
1256 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1257 tokval
->t_charptr
= tline
->text
;
1259 return tokval
->t_type
= TOKEN_ERRNUM
;
1261 return tokval
->t_type
= TOKEN_NUM
;
1264 if (tline
->type
== TOK_FLOAT
) {
1265 return tokval
->t_type
= TOKEN_FLOAT
;
1268 if (tline
->type
== TOK_STRING
) {
1271 bq
= tline
->text
[0];
1272 tokval
->t_charptr
= tline
->text
;
1273 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1275 if (ep
[0] != bq
|| ep
[1] != '\0')
1276 return tokval
->t_type
= TOKEN_ERRSTR
;
1278 return tokval
->t_type
= TOKEN_STR
;
1281 if (tline
->type
== TOK_OTHER
) {
1282 if (!strcmp(tline
->text
, "<<"))
1283 return tokval
->t_type
= TOKEN_SHL
;
1284 if (!strcmp(tline
->text
, ">>"))
1285 return tokval
->t_type
= TOKEN_SHR
;
1286 if (!strcmp(tline
->text
, "//"))
1287 return tokval
->t_type
= TOKEN_SDIV
;
1288 if (!strcmp(tline
->text
, "%%"))
1289 return tokval
->t_type
= TOKEN_SMOD
;
1290 if (!strcmp(tline
->text
, "=="))
1291 return tokval
->t_type
= TOKEN_EQ
;
1292 if (!strcmp(tline
->text
, "<>"))
1293 return tokval
->t_type
= TOKEN_NE
;
1294 if (!strcmp(tline
->text
, "!="))
1295 return tokval
->t_type
= TOKEN_NE
;
1296 if (!strcmp(tline
->text
, "<="))
1297 return tokval
->t_type
= TOKEN_LE
;
1298 if (!strcmp(tline
->text
, ">="))
1299 return tokval
->t_type
= TOKEN_GE
;
1300 if (!strcmp(tline
->text
, "&&"))
1301 return tokval
->t_type
= TOKEN_DBL_AND
;
1302 if (!strcmp(tline
->text
, "^^"))
1303 return tokval
->t_type
= TOKEN_DBL_XOR
;
1304 if (!strcmp(tline
->text
, "||"))
1305 return tokval
->t_type
= TOKEN_DBL_OR
;
1309 * We have no other options: just return the first character of
1312 return tokval
->t_type
= tline
->text
[0];
1316 * Compare a string to the name of an existing macro; this is a
1317 * simple wrapper which calls either strcmp or nasm_stricmp
1318 * depending on the value of the `casesense' parameter.
1320 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1322 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1326 * Compare a string to the name of an existing macro; this is a
1327 * simple wrapper which calls either strcmp or nasm_stricmp
1328 * depending on the value of the `casesense' parameter.
1330 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1332 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1336 * Return the Context structure associated with a %$ token. Return
1337 * NULL, having _already_ reported an error condition, if the
1338 * context stack isn't deep enough for the supplied number of $
1340 * If all_contexts == true, contexts that enclose current are
1341 * also scanned for such smacro, until it is found; if not -
1342 * only the context that directly results from the number of $'s
1343 * in variable's name.
1345 * If "namep" is non-NULL, set it to the pointer to the macro name
1346 * tail, i.e. the part beyond %$...
1348 static Context
*get_ctx(const char *name
, const char **namep
,
1358 if (!name
|| name
[0] != '%' || name
[1] != '$')
1362 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1369 while (ctx
&& *name
== '$') {
1375 error(ERR_NONFATAL
, "`%s': context stack is only"
1376 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1387 /* Search for this smacro in found context */
1388 m
= hash_findix(&ctx
->localmac
, name
);
1390 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1401 * Check to see if a file is already in a string list
1403 static bool in_list(const StrList
*list
, const char *str
)
1406 if (!strcmp(list
->str
, str
))
1414 * Open an include file. This routine must always return a valid
1415 * file pointer if it returns - it's responsible for throwing an
1416 * ERR_FATAL and bombing out completely if not. It should also try
1417 * the include path one by one until it finds the file or reaches
1418 * the end of the path.
1420 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1425 IncPath
*ip
= ipath
;
1426 int len
= strlen(file
);
1427 size_t prefix_len
= 0;
1431 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1432 memcpy(sl
->str
, prefix
, prefix_len
);
1433 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1434 fp
= fopen(sl
->str
, "r");
1435 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1453 prefix_len
= strlen(prefix
);
1455 /* -MG given and file not found */
1456 if (dhead
&& !in_list(*dhead
, file
)) {
1457 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1459 strcpy(sl
->str
, file
);
1467 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1468 return NULL
; /* never reached - placate compilers */
1472 * Determine if we should warn on defining a single-line macro of
1473 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1474 * return true if _any_ single-line macro of that name is defined.
1475 * Otherwise, will return true if a single-line macro with either
1476 * `nparam' or no parameters is defined.
1478 * If a macro with precisely the right number of parameters is
1479 * defined, or nparam is -1, the address of the definition structure
1480 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1481 * is NULL, no action will be taken regarding its contents, and no
1484 * Note that this is also called with nparam zero to resolve
1487 * If you already know which context macro belongs to, you can pass
1488 * the context pointer as first parameter; if you won't but name begins
1489 * with %$ the context will be automatically computed. If all_contexts
1490 * is true, macro will be searched in outer contexts as well.
1493 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1496 struct hash_table
*smtbl
;
1500 smtbl
= &ctx
->localmac
;
1501 } else if (name
[0] == '%' && name
[1] == '$') {
1503 ctx
= get_ctx(name
, &name
, false);
1505 return false; /* got to return _something_ */
1506 smtbl
= &ctx
->localmac
;
1510 m
= (SMacro
*) hash_findix(smtbl
, name
);
1513 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1514 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1516 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1530 * Count and mark off the parameters in a multi-line macro call.
1531 * This is called both from within the multi-line macro expansion
1532 * code, and also to mark off the default parameters when provided
1533 * in a %macro definition line.
1535 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1537 int paramsize
, brace
;
1539 *nparam
= paramsize
= 0;
1542 /* +1: we need space for the final NULL */
1543 if (*nparam
+1 >= paramsize
) {
1544 paramsize
+= PARAM_DELTA
;
1545 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1549 if (tok_is_(t
, "{"))
1551 (*params
)[(*nparam
)++] = t
;
1552 while (tok_isnt_(t
, brace
? "}" : ","))
1554 if (t
) { /* got a comma/brace */
1558 * Now we've found the closing brace, look further
1562 if (tok_isnt_(t
, ",")) {
1564 "braces do not enclose all of macro parameter");
1565 while (tok_isnt_(t
, ","))
1569 t
= t
->next
; /* eat the comma */
1576 * Determine whether one of the various `if' conditions is true or
1579 * We must free the tline we get passed.
1581 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1583 enum pp_conditional i
= PP_COND(ct
);
1585 Token
*t
, *tt
, **tptr
, *origline
;
1586 struct tokenval tokval
;
1588 enum pp_token_type needtype
;
1594 j
= false; /* have we matched yet? */
1599 if (tline
->type
!= TOK_ID
) {
1601 "`%s' expects context identifiers", pp_directives
[ct
]);
1602 free_tlist(origline
);
1605 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1607 tline
= tline
->next
;
1612 j
= false; /* have we matched yet? */
1615 if (!tline
|| (tline
->type
!= TOK_ID
&&
1616 (tline
->type
!= TOK_PREPROC_ID
||
1617 tline
->text
[1] != '$'))) {
1619 "`%s' expects macro identifiers", pp_directives
[ct
]);
1622 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1624 tline
= tline
->next
;
1630 tline
= expand_smacro(tline
);
1632 while (tok_isnt_(tt
, ","))
1636 "`%s' expects two comma-separated arguments",
1641 j
= true; /* assume equality unless proved not */
1642 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1643 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1644 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1648 if (t
->type
== TOK_WHITESPACE
) {
1652 if (tt
->type
== TOK_WHITESPACE
) {
1656 if (tt
->type
!= t
->type
) {
1657 j
= false; /* found mismatching tokens */
1660 /* When comparing strings, need to unquote them first */
1661 if (t
->type
== TOK_STRING
) {
1662 size_t l1
= nasm_unquote(t
->text
, NULL
);
1663 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1669 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1673 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1674 j
= false; /* found mismatching tokens */
1681 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1682 j
= false; /* trailing gunk on one end or other */
1688 MMacro searching
, *mmac
;
1691 tline
= expand_id(tline
);
1692 if (!tok_type_(tline
, TOK_ID
)) {
1694 "`%s' expects a macro name", pp_directives
[ct
]);
1697 searching
.name
= nasm_strdup(tline
->text
);
1698 searching
.casesense
= true;
1699 searching
.plus
= false;
1700 searching
.nolist
= false;
1701 searching
.in_progress
= 0;
1702 searching
.max_depth
= 0;
1703 searching
.rep_nest
= NULL
;
1704 searching
.nparam_min
= 0;
1705 searching
.nparam_max
= INT_MAX
;
1706 tline
= expand_smacro(tline
->next
);
1709 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1711 "`%s' expects a parameter count or nothing",
1714 searching
.nparam_min
= searching
.nparam_max
=
1715 readnum(tline
->text
, &j
);
1718 "unable to parse parameter count `%s'",
1721 if (tline
&& tok_is_(tline
->next
, "-")) {
1722 tline
= tline
->next
->next
;
1723 if (tok_is_(tline
, "*"))
1724 searching
.nparam_max
= INT_MAX
;
1725 else if (!tok_type_(tline
, TOK_NUMBER
))
1727 "`%s' expects a parameter count after `-'",
1730 searching
.nparam_max
= readnum(tline
->text
, &j
);
1733 "unable to parse parameter count `%s'",
1735 if (searching
.nparam_min
> searching
.nparam_max
)
1737 "minimum parameter count exceeds maximum");
1740 if (tline
&& tok_is_(tline
->next
, "+")) {
1741 tline
= tline
->next
;
1742 searching
.plus
= true;
1744 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1746 if (!strcmp(mmac
->name
, searching
.name
) &&
1747 (mmac
->nparam_min
<= searching
.nparam_max
1749 && (searching
.nparam_min
<= mmac
->nparam_max
1756 if (tline
&& tline
->next
)
1757 error(ERR_WARNING
|ERR_PASS1
,
1758 "trailing garbage after %%ifmacro ignored");
1759 nasm_free(searching
.name
);
1768 needtype
= TOK_NUMBER
;
1771 needtype
= TOK_STRING
;
1775 t
= tline
= expand_smacro(tline
);
1777 while (tok_type_(t
, TOK_WHITESPACE
) ||
1778 (needtype
== TOK_NUMBER
&&
1779 tok_type_(t
, TOK_OTHER
) &&
1780 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1784 j
= tok_type_(t
, needtype
);
1788 t
= tline
= expand_smacro(tline
);
1789 while (tok_type_(t
, TOK_WHITESPACE
))
1794 t
= t
->next
; /* Skip the actual token */
1795 while (tok_type_(t
, TOK_WHITESPACE
))
1797 j
= !t
; /* Should be nothing left */
1802 t
= tline
= expand_smacro(tline
);
1803 while (tok_type_(t
, TOK_WHITESPACE
))
1806 j
= !t
; /* Should be empty */
1810 t
= tline
= expand_smacro(tline
);
1812 tokval
.t_type
= TOKEN_INVALID
;
1813 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1814 NULL
, pass
| CRITICAL
, error
, NULL
);
1818 error(ERR_WARNING
|ERR_PASS1
,
1819 "trailing garbage after expression ignored");
1820 if (!is_simple(evalresult
)) {
1822 "non-constant value given to `%s'", pp_directives
[ct
]);
1825 j
= reloc_value(evalresult
) != 0;
1830 "preprocessor directive `%s' not yet implemented",
1835 free_tlist(origline
);
1836 return j
^ PP_NEGATIVE(ct
);
1839 free_tlist(origline
);
1844 * Common code for defining an smacro
1846 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1847 int nparam
, Token
*expansion
)
1849 SMacro
*smac
, **smhead
;
1850 struct hash_table
*smtbl
;
1852 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1854 error(ERR_WARNING
|ERR_PASS1
,
1855 "single-line macro `%s' defined both with and"
1856 " without parameters", mname
);
1858 /* Some instances of the old code considered this a failure,
1859 some others didn't. What is the right thing to do here? */
1860 free_tlist(expansion
);
1861 return false; /* Failure */
1864 * We're redefining, so we have to take over an
1865 * existing SMacro structure. This means freeing
1866 * what was already in it.
1868 nasm_free(smac
->name
);
1869 free_tlist(smac
->expansion
);
1872 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1873 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1874 smac
= nasm_malloc(sizeof(SMacro
));
1875 smac
->next
= *smhead
;
1878 smac
->name
= nasm_strdup(mname
);
1879 smac
->casesense
= casesense
;
1880 smac
->nparam
= nparam
;
1881 smac
->expansion
= expansion
;
1882 smac
->in_progress
= false;
1883 return true; /* Success */
1887 * Undefine an smacro
1889 static void undef_smacro(Context
*ctx
, const char *mname
)
1891 SMacro
**smhead
, *s
, **sp
;
1892 struct hash_table
*smtbl
;
1894 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1895 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1899 * We now have a macro name... go hunt for it.
1902 while ((s
= *sp
) != NULL
) {
1903 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1906 free_tlist(s
->expansion
);
1916 * Parse a mmacro specification.
1918 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1922 tline
= tline
->next
;
1924 tline
= expand_id(tline
);
1925 if (!tok_type_(tline
, TOK_ID
)) {
1926 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1931 def
->name
= nasm_strdup(tline
->text
);
1933 def
->nolist
= false;
1934 def
->in_progress
= 0;
1935 def
->rep_nest
= NULL
;
1936 def
->nparam_min
= 0;
1937 def
->nparam_max
= 0;
1939 tline
= expand_smacro(tline
->next
);
1941 if (!tok_type_(tline
, TOK_NUMBER
)) {
1942 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1944 def
->nparam_min
= def
->nparam_max
=
1945 readnum(tline
->text
, &err
);
1948 "unable to parse parameter count `%s'", tline
->text
);
1950 if (tline
&& tok_is_(tline
->next
, "-")) {
1951 tline
= tline
->next
->next
;
1952 if (tok_is_(tline
, "*")) {
1953 def
->nparam_max
= INT_MAX
;
1954 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1956 "`%s' expects a parameter count after `-'", directive
);
1958 def
->nparam_max
= readnum(tline
->text
, &err
);
1960 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1963 if (def
->nparam_min
> def
->nparam_max
) {
1964 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1968 if (tline
&& tok_is_(tline
->next
, "+")) {
1969 tline
= tline
->next
;
1972 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1973 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1974 tline
= tline
->next
;
1979 * Handle default parameters.
1981 if (tline
&& tline
->next
) {
1982 def
->dlist
= tline
->next
;
1984 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1987 def
->defaults
= NULL
;
1989 def
->expansion
= NULL
;
1991 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1993 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1994 "too many default macro parameters");
2001 * Decode a size directive
2003 static int parse_size(const char *str
) {
2004 static const char *size_names
[] =
2005 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2006 static const int sizes
[] =
2007 { 0, 1, 4, 16, 8, 10, 2, 32 };
2009 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
2013 * nasm_unquote with error if the string contains NUL characters.
2014 * If the string contains NUL characters, issue an error and return
2015 * the C len, i.e. truncate at the NUL.
2017 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
2019 size_t len
= nasm_unquote(qstr
, NULL
);
2020 size_t clen
= strlen(qstr
);
2023 error(ERR_NONFATAL
, "NUL character in `%s' directive",
2024 pp_directives
[directive
]);
2030 * find and process preprocessor directive in passed line
2031 * Find out if a line contains a preprocessor directive, and deal
2034 * If a directive _is_ found, it is the responsibility of this routine
2035 * (and not the caller) to free_tlist() the line.
2037 * @param tline a pointer to the current tokeninzed line linked list
2038 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2041 static int do_directive(Token
* tline
)
2043 enum preproc_token i
;
2056 MMacro
*mmac
, **mmhead
;
2057 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2059 struct tokenval tokval
;
2061 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2069 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2070 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2071 || tline
->text
[1] == '!'))
2072 return NO_DIRECTIVE_FOUND
;
2074 i
= pp_token_hash(tline
->text
);
2077 * If we're in a non-emitting branch of a condition construct,
2078 * or walking to the end of an already terminated %rep block,
2079 * we should ignore all directives except for condition
2082 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2083 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2084 return NO_DIRECTIVE_FOUND
;
2088 * If we're defining a macro or reading a %rep block, we should
2089 * ignore all directives except for %macro/%imacro (which nest),
2090 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2091 * If we're in a %rep block, another %rep nests, so should be let through.
2093 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2094 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2095 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2096 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2097 return NO_DIRECTIVE_FOUND
;
2101 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2102 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2104 return NO_DIRECTIVE_FOUND
;
2105 } else if (nested_mac_count
> 0) {
2106 if (i
== PP_ENDMACRO
) {
2108 return NO_DIRECTIVE_FOUND
;
2111 if (!defining
->name
) {
2114 return NO_DIRECTIVE_FOUND
;
2115 } else if (nested_rep_count
> 0) {
2116 if (i
== PP_ENDREP
) {
2118 return NO_DIRECTIVE_FOUND
;
2126 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2128 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2131 /* Directive to tell NASM what the default stack size is. The
2132 * default is for a 16-bit stack, and this can be overriden with
2134 * the following form:
2136 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2138 tline
= tline
->next
;
2139 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2140 tline
= tline
->next
;
2141 if (!tline
|| tline
->type
!= TOK_ID
) {
2142 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2143 free_tlist(origline
);
2144 return DIRECTIVE_FOUND
;
2146 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2147 /* All subsequent ARG directives are for a 32-bit stack */
2149 StackPointer
= "ebp";
2152 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2153 /* All subsequent ARG directives are for a 64-bit stack */
2155 StackPointer
= "rbp";
2158 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2159 /* All subsequent ARG directives are for a 16-bit stack,
2160 * far function call.
2163 StackPointer
= "bp";
2166 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2167 /* All subsequent ARG directives are for a 16-bit stack,
2168 * far function call. We don't support near functions.
2171 StackPointer
= "bp";
2175 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2176 free_tlist(origline
);
2177 return DIRECTIVE_FOUND
;
2179 free_tlist(origline
);
2180 return DIRECTIVE_FOUND
;
2183 /* TASM like ARG directive to define arguments to functions, in
2184 * the following form:
2186 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2190 char *arg
, directive
[256];
2191 int size
= StackSize
;
2193 /* Find the argument name */
2194 tline
= tline
->next
;
2195 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2196 tline
= tline
->next
;
2197 if (!tline
|| tline
->type
!= TOK_ID
) {
2198 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2199 free_tlist(origline
);
2200 return DIRECTIVE_FOUND
;
2204 /* Find the argument size type */
2205 tline
= tline
->next
;
2206 if (!tline
|| tline
->type
!= TOK_OTHER
2207 || tline
->text
[0] != ':') {
2209 "Syntax error processing `%%arg' directive");
2210 free_tlist(origline
);
2211 return DIRECTIVE_FOUND
;
2213 tline
= tline
->next
;
2214 if (!tline
|| tline
->type
!= TOK_ID
) {
2215 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2216 free_tlist(origline
);
2217 return DIRECTIVE_FOUND
;
2220 /* Allow macro expansion of type parameter */
2221 tt
= tokenize(tline
->text
);
2222 tt
= expand_smacro(tt
);
2223 size
= parse_size(tt
->text
);
2226 "Invalid size type for `%%arg' missing directive");
2228 free_tlist(origline
);
2229 return DIRECTIVE_FOUND
;
2233 /* Round up to even stack slots */
2234 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2236 /* Now define the macro for the argument */
2237 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2238 arg
, StackPointer
, offset
);
2239 do_directive(tokenize(directive
));
2242 /* Move to the next argument in the list */
2243 tline
= tline
->next
;
2244 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2245 tline
= tline
->next
;
2246 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2248 free_tlist(origline
);
2249 return DIRECTIVE_FOUND
;
2252 /* TASM like LOCAL directive to define local variables for a
2253 * function, in the following form:
2255 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2257 * The '= LocalSize' at the end is ignored by NASM, but is
2258 * required by TASM to define the local parameter size (and used
2259 * by the TASM macro package).
2261 offset
= LocalOffset
;
2263 char *local
, directive
[256];
2264 int size
= StackSize
;
2266 /* Find the argument name */
2267 tline
= tline
->next
;
2268 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2269 tline
= tline
->next
;
2270 if (!tline
|| tline
->type
!= TOK_ID
) {
2272 "`%%local' missing argument parameter");
2273 free_tlist(origline
);
2274 return DIRECTIVE_FOUND
;
2276 local
= tline
->text
;
2278 /* Find the argument size type */
2279 tline
= tline
->next
;
2280 if (!tline
|| tline
->type
!= TOK_OTHER
2281 || tline
->text
[0] != ':') {
2283 "Syntax error processing `%%local' directive");
2284 free_tlist(origline
);
2285 return DIRECTIVE_FOUND
;
2287 tline
= tline
->next
;
2288 if (!tline
|| tline
->type
!= TOK_ID
) {
2290 "`%%local' missing size type parameter");
2291 free_tlist(origline
);
2292 return DIRECTIVE_FOUND
;
2295 /* Allow macro expansion of type parameter */
2296 tt
= tokenize(tline
->text
);
2297 tt
= expand_smacro(tt
);
2298 size
= parse_size(tt
->text
);
2301 "Invalid size type for `%%local' missing directive");
2303 free_tlist(origline
);
2304 return DIRECTIVE_FOUND
;
2308 /* Round up to even stack slots */
2309 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2311 offset
+= size
; /* Negative offset, increment before */
2313 /* Now define the macro for the argument */
2314 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2315 local
, StackPointer
, offset
);
2316 do_directive(tokenize(directive
));
2318 /* Now define the assign to setup the enter_c macro correctly */
2319 snprintf(directive
, sizeof(directive
),
2320 "%%assign %%$localsize %%$localsize+%d", size
);
2321 do_directive(tokenize(directive
));
2323 /* Move to the next argument in the list */
2324 tline
= tline
->next
;
2325 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2326 tline
= tline
->next
;
2327 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2328 LocalOffset
= offset
;
2329 free_tlist(origline
);
2330 return DIRECTIVE_FOUND
;
2334 error(ERR_WARNING
|ERR_PASS1
,
2335 "trailing garbage after `%%clear' ignored");
2338 free_tlist(origline
);
2339 return DIRECTIVE_FOUND
;
2342 t
= tline
->next
= expand_smacro(tline
->next
);
2344 if (!t
|| (t
->type
!= TOK_STRING
&&
2345 t
->type
!= TOK_INTERNAL_STRING
)) {
2346 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2347 free_tlist(origline
);
2348 return DIRECTIVE_FOUND
; /* but we did _something_ */
2351 error(ERR_WARNING
|ERR_PASS1
,
2352 "trailing garbage after `%%depend' ignored");
2354 if (t
->type
!= TOK_INTERNAL_STRING
)
2355 nasm_unquote_cstr(p
, i
);
2356 if (dephead
&& !in_list(*dephead
, p
)) {
2357 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2361 deptail
= &sl
->next
;
2363 free_tlist(origline
);
2364 return DIRECTIVE_FOUND
;
2367 t
= tline
->next
= expand_smacro(tline
->next
);
2370 if (!t
|| (t
->type
!= TOK_STRING
&&
2371 t
->type
!= TOK_INTERNAL_STRING
)) {
2372 error(ERR_NONFATAL
, "`%%include' expects a file name");
2373 free_tlist(origline
);
2374 return DIRECTIVE_FOUND
; /* but we did _something_ */
2377 error(ERR_WARNING
|ERR_PASS1
,
2378 "trailing garbage after `%%include' ignored");
2380 if (t
->type
!= TOK_INTERNAL_STRING
)
2381 nasm_unquote_cstr(p
, i
);
2382 inc
= nasm_malloc(sizeof(Include
));
2385 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2387 /* -MG given but file not found */
2390 inc
->fname
= src_set_fname(nasm_strdup(p
));
2391 inc
->lineno
= src_set_linnum(0);
2393 inc
->expansion
= NULL
;
2396 list
->uplevel(LIST_INCLUDE
);
2398 free_tlist(origline
);
2399 return DIRECTIVE_FOUND
;
2403 static macros_t
*use_pkg
;
2404 const char *pkg_macro
;
2406 tline
= tline
->next
;
2408 tline
= expand_id(tline
);
2410 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2411 tline
->type
!= TOK_INTERNAL_STRING
&&
2412 tline
->type
!= TOK_ID
)) {
2413 error(ERR_NONFATAL
, "`%%use' expects a package name");
2414 free_tlist(origline
);
2415 return DIRECTIVE_FOUND
; /* but we did _something_ */
2418 error(ERR_WARNING
|ERR_PASS1
,
2419 "trailing garbage after `%%use' ignored");
2420 if (tline
->type
== TOK_STRING
)
2421 nasm_unquote_cstr(tline
->text
, i
);
2422 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2424 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2425 /* The first string will be <%define>__USE_*__ */
2426 pkg_macro
= (char *)use_pkg
+ 1;
2427 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2428 /* Not already included, go ahead and include it */
2429 stdmacpos
= use_pkg
;
2431 free_tlist(origline
);
2432 return DIRECTIVE_FOUND
;
2437 tline
= tline
->next
;
2439 tline
= expand_id(tline
);
2441 if (!tok_type_(tline
, TOK_ID
)) {
2442 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2444 free_tlist(origline
);
2445 return DIRECTIVE_FOUND
; /* but we did _something_ */
2448 error(ERR_WARNING
|ERR_PASS1
,
2449 "trailing garbage after `%s' ignored",
2451 p
= nasm_strdup(tline
->text
);
2453 p
= NULL
; /* Anonymous */
2457 ctx
= nasm_malloc(sizeof(Context
));
2459 hash_init(&ctx
->localmac
, HASH_SMALL
);
2461 ctx
->number
= unique
++;
2466 error(ERR_NONFATAL
, "`%s': context stack is empty",
2468 } else if (i
== PP_POP
) {
2469 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2470 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2472 cstk
->name
? cstk
->name
: "anonymous", p
);
2477 nasm_free(cstk
->name
);
2483 free_tlist(origline
);
2484 return DIRECTIVE_FOUND
;
2486 severity
= ERR_FATAL
;
2489 severity
= ERR_NONFATAL
;
2492 severity
= ERR_WARNING
|ERR_WARN_USER
;
2497 /* Only error out if this is the final pass */
2498 if (pass
!= 2 && i
!= PP_FATAL
)
2499 return DIRECTIVE_FOUND
;
2501 tline
->next
= expand_smacro(tline
->next
);
2502 tline
= tline
->next
;
2504 t
= tline
? tline
->next
: NULL
;
2506 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2507 /* The line contains only a quoted string */
2509 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2510 error(severity
, "%s", p
);
2512 /* Not a quoted string, or more than a quoted string */
2513 p
= detoken(tline
, false);
2514 error(severity
, "%s", p
);
2517 free_tlist(origline
);
2518 return DIRECTIVE_FOUND
;
2522 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2525 j
= if_condition(tline
->next
, i
);
2526 tline
->next
= NULL
; /* it got freed */
2527 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2529 cond
= nasm_malloc(sizeof(Cond
));
2530 cond
->next
= istk
->conds
;
2533 free_tlist(origline
);
2534 return DIRECTIVE_FOUND
;
2538 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2539 switch(istk
->conds
->state
) {
2541 istk
->conds
->state
= COND_DONE
;
2548 case COND_ELSE_TRUE
:
2549 case COND_ELSE_FALSE
:
2550 error_precond(ERR_WARNING
|ERR_PASS1
,
2551 "`%%elif' after `%%else' ignored");
2552 istk
->conds
->state
= COND_NEVER
;
2557 * IMPORTANT: In the case of %if, we will already have
2558 * called expand_mmac_params(); however, if we're
2559 * processing an %elif we must have been in a
2560 * non-emitting mode, which would have inhibited
2561 * the normal invocation of expand_mmac_params().
2562 * Therefore, we have to do it explicitly here.
2564 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2565 tline
->next
= NULL
; /* it got freed */
2566 istk
->conds
->state
=
2567 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2570 free_tlist(origline
);
2571 return DIRECTIVE_FOUND
;
2575 error_precond(ERR_WARNING
|ERR_PASS1
,
2576 "trailing garbage after `%%else' ignored");
2578 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2579 switch(istk
->conds
->state
) {
2582 istk
->conds
->state
= COND_ELSE_FALSE
;
2589 istk
->conds
->state
= COND_ELSE_TRUE
;
2592 case COND_ELSE_TRUE
:
2593 case COND_ELSE_FALSE
:
2594 error_precond(ERR_WARNING
|ERR_PASS1
,
2595 "`%%else' after `%%else' ignored.");
2596 istk
->conds
->state
= COND_NEVER
;
2599 free_tlist(origline
);
2600 return DIRECTIVE_FOUND
;
2604 error_precond(ERR_WARNING
|ERR_PASS1
,
2605 "trailing garbage after `%%endif' ignored");
2607 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2609 istk
->conds
= cond
->next
;
2611 free_tlist(origline
);
2612 return DIRECTIVE_FOUND
;
2619 error(ERR_FATAL
, "`%s': already defining a macro",
2621 return DIRECTIVE_FOUND
;
2623 defining
= nasm_malloc(sizeof(MMacro
));
2624 defining
->max_depth
=
2625 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2626 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2627 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2628 nasm_free(defining
);
2630 return DIRECTIVE_FOUND
;
2633 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2635 if (!strcmp(mmac
->name
, defining
->name
) &&
2636 (mmac
->nparam_min
<= defining
->nparam_max
2638 && (defining
->nparam_min
<= mmac
->nparam_max
2640 error(ERR_WARNING
|ERR_PASS1
,
2641 "redefining multi-line macro `%s'", defining
->name
);
2642 return DIRECTIVE_FOUND
;
2646 free_tlist(origline
);
2647 return DIRECTIVE_FOUND
;
2651 if (! (defining
&& defining
->name
)) {
2652 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2653 return DIRECTIVE_FOUND
;
2655 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2656 defining
->next
= *mmhead
;
2659 free_tlist(origline
);
2660 return DIRECTIVE_FOUND
;
2664 * We must search along istk->expansion until we hit a
2665 * macro-end marker for a macro with a name. Then we set
2666 * its `in_progress' flag to 0.
2668 for (l
= istk
->expansion
; l
; l
= l
->next
)
2669 if (l
->finishes
&& l
->finishes
->name
)
2673 l
->finishes
->in_progress
= 0;
2675 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2677 free_tlist(origline
);
2678 return DIRECTIVE_FOUND
;
2686 spec
.casesense
= (i
== PP_UNMACRO
);
2687 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2688 return DIRECTIVE_FOUND
;
2690 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2691 while (mmac_p
&& *mmac_p
) {
2693 if (mmac
->casesense
== spec
.casesense
&&
2694 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2695 mmac
->nparam_min
== spec
.nparam_min
&&
2696 mmac
->nparam_max
== spec
.nparam_max
&&
2697 mmac
->plus
== spec
.plus
) {
2698 *mmac_p
= mmac
->next
;
2701 mmac_p
= &mmac
->next
;
2704 free_tlist(origline
);
2705 free_tlist(spec
.dlist
);
2706 return DIRECTIVE_FOUND
;
2710 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2711 tline
= tline
->next
;
2713 free_tlist(origline
);
2714 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2715 return DIRECTIVE_FOUND
;
2717 t
= expand_smacro(tline
->next
);
2719 free_tlist(origline
);
2722 tokval
.t_type
= TOKEN_INVALID
;
2724 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2727 return DIRECTIVE_FOUND
;
2729 error(ERR_WARNING
|ERR_PASS1
,
2730 "trailing garbage after expression ignored");
2731 if (!is_simple(evalresult
)) {
2732 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2733 return DIRECTIVE_FOUND
;
2736 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2737 mmac
= mmac
->next_active
;
2739 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2740 } else if (mmac
->nparam
== 0) {
2742 "`%%rotate' invoked within macro without parameters");
2744 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2746 rotate
%= (int)mmac
->nparam
;
2748 rotate
+= mmac
->nparam
;
2750 mmac
->rotate
= rotate
;
2752 return DIRECTIVE_FOUND
;
2757 tline
= tline
->next
;
2758 } while (tok_type_(tline
, TOK_WHITESPACE
));
2760 if (tok_type_(tline
, TOK_ID
) &&
2761 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2764 tline
= tline
->next
;
2765 } while (tok_type_(tline
, TOK_WHITESPACE
));
2769 t
= expand_smacro(tline
);
2771 tokval
.t_type
= TOKEN_INVALID
;
2773 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2775 free_tlist(origline
);
2776 return DIRECTIVE_FOUND
;
2779 error(ERR_WARNING
|ERR_PASS1
,
2780 "trailing garbage after expression ignored");
2781 if (!is_simple(evalresult
)) {
2782 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2783 return DIRECTIVE_FOUND
;
2785 count
= reloc_value(evalresult
) + 1;
2787 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2790 free_tlist(origline
);
2792 tmp_defining
= defining
;
2793 defining
= nasm_malloc(sizeof(MMacro
));
2794 defining
->prev
= NULL
;
2795 defining
->name
= NULL
; /* flags this macro as a %rep block */
2796 defining
->casesense
= false;
2797 defining
->plus
= false;
2798 defining
->nolist
= nolist
;
2799 defining
->in_progress
= count
;
2800 defining
->max_depth
= 0;
2801 defining
->nparam_min
= defining
->nparam_max
= 0;
2802 defining
->defaults
= NULL
;
2803 defining
->dlist
= NULL
;
2804 defining
->expansion
= NULL
;
2805 defining
->next_active
= istk
->mstk
;
2806 defining
->rep_nest
= tmp_defining
;
2807 return DIRECTIVE_FOUND
;
2810 if (!defining
|| defining
->name
) {
2811 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2812 return DIRECTIVE_FOUND
;
2816 * Now we have a "macro" defined - although it has no name
2817 * and we won't be entering it in the hash tables - we must
2818 * push a macro-end marker for it on to istk->expansion.
2819 * After that, it will take care of propagating itself (a
2820 * macro-end marker line for a macro which is really a %rep
2821 * block will cause the macro to be re-expanded, complete
2822 * with another macro-end marker to ensure the process
2823 * continues) until the whole expansion is forcibly removed
2824 * from istk->expansion by a %exitrep.
2826 l
= nasm_malloc(sizeof(Line
));
2827 l
->next
= istk
->expansion
;
2828 l
->finishes
= defining
;
2830 istk
->expansion
= l
;
2832 istk
->mstk
= defining
;
2834 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2835 tmp_defining
= defining
;
2836 defining
= defining
->rep_nest
;
2837 free_tlist(origline
);
2838 return DIRECTIVE_FOUND
;
2842 * We must search along istk->expansion until we hit a
2843 * macro-end marker for a macro with no name. Then we set
2844 * its `in_progress' flag to 0.
2846 for (l
= istk
->expansion
; l
; l
= l
->next
)
2847 if (l
->finishes
&& !l
->finishes
->name
)
2851 l
->finishes
->in_progress
= 1;
2853 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2854 free_tlist(origline
);
2855 return DIRECTIVE_FOUND
;
2861 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2863 tline
= tline
->next
;
2865 tline
= expand_id(tline
);
2866 if (!tline
|| (tline
->type
!= TOK_ID
&&
2867 (tline
->type
!= TOK_PREPROC_ID
||
2868 tline
->text
[1] != '$'))) {
2869 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2871 free_tlist(origline
);
2872 return DIRECTIVE_FOUND
;
2875 ctx
= get_ctx(tline
->text
, &mname
, false);
2877 param_start
= tline
= tline
->next
;
2880 /* Expand the macro definition now for %xdefine and %ixdefine */
2881 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2882 tline
= expand_smacro(tline
);
2884 if (tok_is_(tline
, "(")) {
2886 * This macro has parameters.
2889 tline
= tline
->next
;
2893 error(ERR_NONFATAL
, "parameter identifier expected");
2894 free_tlist(origline
);
2895 return DIRECTIVE_FOUND
;
2897 if (tline
->type
!= TOK_ID
) {
2899 "`%s': parameter identifier expected",
2901 free_tlist(origline
);
2902 return DIRECTIVE_FOUND
;
2904 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2905 tline
= tline
->next
;
2907 if (tok_is_(tline
, ",")) {
2908 tline
= tline
->next
;
2910 if (!tok_is_(tline
, ")")) {
2912 "`)' expected to terminate macro template");
2913 free_tlist(origline
);
2914 return DIRECTIVE_FOUND
;
2920 tline
= tline
->next
;
2922 if (tok_type_(tline
, TOK_WHITESPACE
))
2923 last
= tline
, tline
= tline
->next
;
2928 if (t
->type
== TOK_ID
) {
2929 for (tt
= param_start
; tt
; tt
= tt
->next
)
2930 if (tt
->type
>= TOK_SMAC_PARAM
&&
2931 !strcmp(tt
->text
, t
->text
))
2935 t
->next
= macro_start
;
2940 * Good. We now have a macro name, a parameter count, and a
2941 * token list (in reverse order) for an expansion. We ought
2942 * to be OK just to create an SMacro, store it, and let
2943 * free_tlist have the rest of the line (which we have
2944 * carefully re-terminated after chopping off the expansion
2947 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2948 free_tlist(origline
);
2949 return DIRECTIVE_FOUND
;
2952 tline
= tline
->next
;
2954 tline
= expand_id(tline
);
2955 if (!tline
|| (tline
->type
!= TOK_ID
&&
2956 (tline
->type
!= TOK_PREPROC_ID
||
2957 tline
->text
[1] != '$'))) {
2958 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2959 free_tlist(origline
);
2960 return DIRECTIVE_FOUND
;
2963 error(ERR_WARNING
|ERR_PASS1
,
2964 "trailing garbage after macro name ignored");
2967 /* Find the context that symbol belongs to */
2968 ctx
= get_ctx(tline
->text
, &mname
, false);
2969 undef_smacro(ctx
, mname
);
2970 free_tlist(origline
);
2971 return DIRECTIVE_FOUND
;
2975 casesense
= (i
== PP_DEFSTR
);
2977 tline
= tline
->next
;
2979 tline
= expand_id(tline
);
2980 if (!tline
|| (tline
->type
!= TOK_ID
&&
2981 (tline
->type
!= TOK_PREPROC_ID
||
2982 tline
->text
[1] != '$'))) {
2983 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2985 free_tlist(origline
);
2986 return DIRECTIVE_FOUND
;
2989 ctx
= get_ctx(tline
->text
, &mname
, false);
2991 tline
= expand_smacro(tline
->next
);
2994 while (tok_type_(tline
, TOK_WHITESPACE
))
2995 tline
= delete_Token(tline
);
2997 p
= detoken(tline
, false);
2998 macro_start
= nasm_malloc(sizeof(*macro_start
));
2999 macro_start
->next
= NULL
;
3000 macro_start
->text
= nasm_quote(p
, strlen(p
));
3001 macro_start
->type
= TOK_STRING
;
3002 macro_start
->a
.mac
= NULL
;
3006 * We now have a macro name, an implicit parameter count of
3007 * zero, and a string token to use as an expansion. Create
3008 * and store an SMacro.
3010 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3011 free_tlist(origline
);
3012 return DIRECTIVE_FOUND
;
3016 casesense
= (i
== PP_DEFTOK
);
3018 tline
= tline
->next
;
3020 tline
= expand_id(tline
);
3021 if (!tline
|| (tline
->type
!= TOK_ID
&&
3022 (tline
->type
!= TOK_PREPROC_ID
||
3023 tline
->text
[1] != '$'))) {
3025 "`%s' expects a macro identifier as first parameter",
3027 free_tlist(origline
);
3028 return DIRECTIVE_FOUND
;
3030 ctx
= get_ctx(tline
->text
, &mname
, false);
3032 tline
= expand_smacro(tline
->next
);
3036 while (tok_type_(t
, TOK_WHITESPACE
))
3038 /* t should now point to the string */
3039 if (t
->type
!= TOK_STRING
) {
3041 "`%s` requires string as second parameter",
3044 free_tlist(origline
);
3045 return DIRECTIVE_FOUND
;
3048 nasm_unquote_cstr(t
->text
, i
);
3049 macro_start
= tokenize(t
->text
);
3052 * We now have a macro name, an implicit parameter count of
3053 * zero, and a numeric token to use as an expansion. Create
3054 * and store an SMacro.
3056 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3058 free_tlist(origline
);
3059 return DIRECTIVE_FOUND
;
3064 StrList
*xsl
= NULL
;
3065 StrList
**xst
= &xsl
;
3069 tline
= tline
->next
;
3071 tline
= expand_id(tline
);
3072 if (!tline
|| (tline
->type
!= TOK_ID
&&
3073 (tline
->type
!= TOK_PREPROC_ID
||
3074 tline
->text
[1] != '$'))) {
3076 "`%%pathsearch' expects a macro identifier as first parameter");
3077 free_tlist(origline
);
3078 return DIRECTIVE_FOUND
;
3080 ctx
= get_ctx(tline
->text
, &mname
, false);
3082 tline
= expand_smacro(tline
->next
);
3086 while (tok_type_(t
, TOK_WHITESPACE
))
3089 if (!t
|| (t
->type
!= TOK_STRING
&&
3090 t
->type
!= TOK_INTERNAL_STRING
)) {
3091 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3093 free_tlist(origline
);
3094 return DIRECTIVE_FOUND
; /* but we did _something_ */
3097 error(ERR_WARNING
|ERR_PASS1
,
3098 "trailing garbage after `%%pathsearch' ignored");
3100 if (t
->type
!= TOK_INTERNAL_STRING
)
3101 nasm_unquote(p
, NULL
);
3103 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3106 fclose(fp
); /* Don't actually care about the file */
3108 macro_start
= nasm_malloc(sizeof(*macro_start
));
3109 macro_start
->next
= NULL
;
3110 macro_start
->text
= nasm_quote(p
, strlen(p
));
3111 macro_start
->type
= TOK_STRING
;
3112 macro_start
->a
.mac
= NULL
;
3117 * We now have a macro name, an implicit parameter count of
3118 * zero, and a string token to use as an expansion. Create
3119 * and store an SMacro.
3121 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3123 free_tlist(origline
);
3124 return DIRECTIVE_FOUND
;
3130 tline
= tline
->next
;
3132 tline
= expand_id(tline
);
3133 if (!tline
|| (tline
->type
!= TOK_ID
&&
3134 (tline
->type
!= TOK_PREPROC_ID
||
3135 tline
->text
[1] != '$'))) {
3137 "`%%strlen' expects a macro identifier as first parameter");
3138 free_tlist(origline
);
3139 return DIRECTIVE_FOUND
;
3141 ctx
= get_ctx(tline
->text
, &mname
, false);
3143 tline
= expand_smacro(tline
->next
);
3147 while (tok_type_(t
, TOK_WHITESPACE
))
3149 /* t should now point to the string */
3150 if (t
->type
!= TOK_STRING
) {
3152 "`%%strlen` requires string as second parameter");
3154 free_tlist(origline
);
3155 return DIRECTIVE_FOUND
;
3158 macro_start
= nasm_malloc(sizeof(*macro_start
));
3159 macro_start
->next
= NULL
;
3160 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3161 macro_start
->a
.mac
= NULL
;
3164 * We now have a macro name, an implicit parameter count of
3165 * zero, and a numeric token to use as an expansion. Create
3166 * and store an SMacro.
3168 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3170 free_tlist(origline
);
3171 return DIRECTIVE_FOUND
;
3176 tline
= tline
->next
;
3178 tline
= expand_id(tline
);
3179 if (!tline
|| (tline
->type
!= TOK_ID
&&
3180 (tline
->type
!= TOK_PREPROC_ID
||
3181 tline
->text
[1] != '$'))) {
3183 "`%%strcat' expects a macro identifier as first parameter");
3184 free_tlist(origline
);
3185 return DIRECTIVE_FOUND
;
3187 ctx
= get_ctx(tline
->text
, &mname
, false);
3189 tline
= expand_smacro(tline
->next
);
3193 for (t
= tline
; t
; t
= t
->next
) {
3195 case TOK_WHITESPACE
:
3198 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3201 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3203 /* else fall through */
3206 "non-string passed to `%%strcat' (%d)", t
->type
);
3208 free_tlist(origline
);
3209 return DIRECTIVE_FOUND
;
3213 p
= pp
= nasm_malloc(len
);
3215 for (t
= tline
; t
; t
= t
->next
) {
3216 if (t
->type
== TOK_STRING
) {
3217 memcpy(p
, t
->text
, t
->a
.len
);
3223 * We now have a macro name, an implicit parameter count of
3224 * zero, and a numeric token to use as an expansion. Create
3225 * and store an SMacro.
3227 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3228 macro_start
->text
= nasm_quote(pp
, len
);
3230 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3232 free_tlist(origline
);
3233 return DIRECTIVE_FOUND
;
3242 tline
= tline
->next
;
3244 tline
= expand_id(tline
);
3245 if (!tline
|| (tline
->type
!= TOK_ID
&&
3246 (tline
->type
!= TOK_PREPROC_ID
||
3247 tline
->text
[1] != '$'))) {
3249 "`%%substr' expects a macro identifier as first parameter");
3250 free_tlist(origline
);
3251 return DIRECTIVE_FOUND
;
3253 ctx
= get_ctx(tline
->text
, &mname
, false);
3255 tline
= expand_smacro(tline
->next
);
3259 while (tok_type_(t
, TOK_WHITESPACE
))
3262 /* t should now point to the string */
3263 if (t
->type
!= TOK_STRING
) {
3265 "`%%substr` requires string as second parameter");
3267 free_tlist(origline
);
3268 return DIRECTIVE_FOUND
;
3273 tokval
.t_type
= TOKEN_INVALID
;
3274 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3278 free_tlist(origline
);
3279 return DIRECTIVE_FOUND
;
3280 } else if (!is_simple(evalresult
)) {
3281 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3283 free_tlist(origline
);
3284 return DIRECTIVE_FOUND
;
3286 a1
= evalresult
->value
-1;
3288 while (tok_type_(tt
, TOK_WHITESPACE
))
3291 a2
= 1; /* Backwards compatibility: one character */
3293 tokval
.t_type
= TOKEN_INVALID
;
3294 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3298 free_tlist(origline
);
3299 return DIRECTIVE_FOUND
;
3300 } else if (!is_simple(evalresult
)) {
3301 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3303 free_tlist(origline
);
3304 return DIRECTIVE_FOUND
;
3306 a2
= evalresult
->value
;
3309 len
= nasm_unquote(t
->text
, NULL
);
3312 if (a1
+a2
> (int64_t)len
)
3315 macro_start
= nasm_malloc(sizeof(*macro_start
));
3316 macro_start
->next
= NULL
;
3317 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3318 macro_start
->type
= TOK_STRING
;
3319 macro_start
->a
.mac
= NULL
;
3322 * We now have a macro name, an implicit parameter count of
3323 * zero, and a numeric token to use as an expansion. Create
3324 * and store an SMacro.
3326 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3328 free_tlist(origline
);
3329 return DIRECTIVE_FOUND
;
3334 casesense
= (i
== PP_ASSIGN
);
3336 tline
= tline
->next
;
3338 tline
= expand_id(tline
);
3339 if (!tline
|| (tline
->type
!= TOK_ID
&&
3340 (tline
->type
!= TOK_PREPROC_ID
||
3341 tline
->text
[1] != '$'))) {
3343 "`%%%sassign' expects a macro identifier",
3344 (i
== PP_IASSIGN
? "i" : ""));
3345 free_tlist(origline
);
3346 return DIRECTIVE_FOUND
;
3348 ctx
= get_ctx(tline
->text
, &mname
, false);
3350 tline
= expand_smacro(tline
->next
);
3355 tokval
.t_type
= TOKEN_INVALID
;
3357 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3360 free_tlist(origline
);
3361 return DIRECTIVE_FOUND
;
3365 error(ERR_WARNING
|ERR_PASS1
,
3366 "trailing garbage after expression ignored");
3368 if (!is_simple(evalresult
)) {
3370 "non-constant value given to `%%%sassign'",
3371 (i
== PP_IASSIGN
? "i" : ""));
3372 free_tlist(origline
);
3373 return DIRECTIVE_FOUND
;
3376 macro_start
= nasm_malloc(sizeof(*macro_start
));
3377 macro_start
->next
= NULL
;
3378 make_tok_num(macro_start
, reloc_value(evalresult
));
3379 macro_start
->a
.mac
= NULL
;
3382 * We now have a macro name, an implicit parameter count of
3383 * zero, and a numeric token to use as an expansion. Create
3384 * and store an SMacro.
3386 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3387 free_tlist(origline
);
3388 return DIRECTIVE_FOUND
;
3392 * Syntax is `%line nnn[+mmm] [filename]'
3394 tline
= tline
->next
;
3396 if (!tok_type_(tline
, TOK_NUMBER
)) {
3397 error(ERR_NONFATAL
, "`%%line' expects line number");
3398 free_tlist(origline
);
3399 return DIRECTIVE_FOUND
;
3401 k
= readnum(tline
->text
, &err
);
3403 tline
= tline
->next
;
3404 if (tok_is_(tline
, "+")) {
3405 tline
= tline
->next
;
3406 if (!tok_type_(tline
, TOK_NUMBER
)) {
3407 error(ERR_NONFATAL
, "`%%line' expects line increment");
3408 free_tlist(origline
);
3409 return DIRECTIVE_FOUND
;
3411 m
= readnum(tline
->text
, &err
);
3412 tline
= tline
->next
;
3418 nasm_free(src_set_fname(detoken(tline
, false)));
3420 free_tlist(origline
);
3421 return DIRECTIVE_FOUND
;
3425 "preprocessor directive `%s' not yet implemented",
3427 return DIRECTIVE_FOUND
;
3432 * Ensure that a macro parameter contains a condition code and
3433 * nothing else. Return the condition code index if so, or -1
3436 static int find_cc(Token
* t
)
3442 return -1; /* Probably a %+ without a space */
3445 if (t
->type
!= TOK_ID
)
3449 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3453 j
= elements(conditions
);
3456 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3471 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3473 Token
**tail
, *t
, *tt
;
3475 bool did_paste
= false;
3478 /* Now handle token pasting... */
3481 while ((t
= *tail
) && (tt
= t
->next
)) {
3483 case TOK_WHITESPACE
:
3484 if (tt
->type
== TOK_WHITESPACE
) {
3485 /* Zap adjacent whitespace tokens */
3486 t
->next
= delete_Token(tt
);
3488 /* Do not advance paste_head here */
3493 case TOK_PREPROC_ID
:
3500 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3501 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3502 tt
->type
== TOK_OTHER
)) {
3503 len
+= strlen(tt
->text
);
3507 /* Now tt points to the first token after the potential
3509 if (tt
!= t
->next
) {
3510 /* We have at least two tokens... */
3511 len
+= strlen(t
->text
);
3512 p
= tmp
= nasm_malloc(len
+1);
3516 p
= strchr(p
, '\0');
3517 t
= delete_Token(t
);
3520 t
= *tail
= tokenize(tmp
);
3527 t
->next
= tt
; /* Attach the remaining token chain */
3535 case TOK_PASTE
: /* %+ */
3536 if (handle_paste_tokens
) {
3537 /* Zap %+ and whitespace tokens to the right */
3538 while (t
&& (t
->type
== TOK_WHITESPACE
||
3539 t
->type
== TOK_PASTE
))
3540 t
= *tail
= delete_Token(t
);
3541 if (!paste_head
|| !t
)
3542 break; /* Nothing to paste with */
3546 while (tok_type_(tt
, TOK_WHITESPACE
))
3547 tt
= t
->next
= delete_Token(tt
);
3550 tmp
= nasm_strcat(t
->text
, tt
->text
);
3552 tt
= delete_Token(tt
);
3553 t
= *tail
= tokenize(tmp
);
3559 t
->next
= tt
; /* Attach the remaining token chain */
3566 /* else fall through */
3568 tail
= paste_head
= &t
->next
;
3575 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3576 * %-n) and MMacro-local identifiers (%%foo) as well as
3577 * macro indirection (%[...]).
3579 static Token
*expand_mmac_params(Token
* tline
)
3581 Token
*t
, *tt
, **tail
, *thead
;
3582 bool changed
= false;
3588 if (tline
->type
== TOK_PREPROC_ID
&&
3589 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3590 && tline
->text
[2]) || tline
->text
[1] == '%'
3591 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3593 int type
= 0, cc
; /* type = 0 to placate optimisers */
3600 tline
= tline
->next
;
3603 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3604 mac
= mac
->next_active
;
3606 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3608 switch (t
->text
[1]) {
3610 * We have to make a substitution of one of the
3611 * forms %1, %-1, %+1, %%foo, %0.
3615 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3616 text
= nasm_strdup(tmpbuf
);
3620 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3622 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3625 n
= atoi(t
->text
+ 2) - 1;
3626 if (n
>= mac
->nparam
)
3629 if (mac
->nparam
> 1)
3630 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3631 tt
= mac
->params
[n
];
3636 "macro parameter %d is not a condition code",
3641 if (inverse_ccs
[cc
] == -1) {
3643 "condition code `%s' is not invertible",
3647 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3651 n
= atoi(t
->text
+ 2) - 1;
3652 if (n
>= mac
->nparam
)
3655 if (mac
->nparam
> 1)
3656 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3657 tt
= mac
->params
[n
];
3662 "macro parameter %d is not a condition code",
3667 text
= nasm_strdup(conditions
[cc
]);
3671 n
= atoi(t
->text
+ 1) - 1;
3672 if (n
>= mac
->nparam
)
3675 if (mac
->nparam
> 1)
3676 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3677 tt
= mac
->params
[n
];
3680 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3681 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3682 tail
= &(*tail
)->next
;
3686 text
= NULL
; /* we've done it here */
3701 } else if (tline
->type
== TOK_INDIRECT
) {
3703 tline
= tline
->next
;
3704 tt
= tokenize(t
->text
);
3705 tt
= expand_mmac_params(tt
);
3706 tt
= expand_smacro(tt
);
3709 tt
->a
.mac
= NULL
; /* Necessary? */
3717 tline
= tline
->next
;
3725 paste_tokens(&thead
, false);
3731 * Expand all single-line macro calls made in the given line.
3732 * Return the expanded version of the line. The original is deemed
3733 * to be destroyed in the process. (In reality we'll just move
3734 * Tokens from input to output a lot of the time, rather than
3735 * actually bothering to destroy and replicate.)
3738 static Token
*expand_smacro(Token
* tline
)
3740 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3741 struct hash_table
*smtbl
;
3742 SMacro
*head
= NULL
, *m
;
3745 unsigned int nparam
, sparam
;
3747 Token
*org_tline
= tline
;
3750 int deadman
= DEADMAN_LIMIT
;
3754 * Trick: we should avoid changing the start token pointer since it can
3755 * be contained in "next" field of other token. Because of this
3756 * we allocate a copy of first token and work with it; at the end of
3757 * routine we copy it back
3761 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3763 tline
->a
.mac
= org_tline
->a
.mac
;
3764 nasm_free(org_tline
->text
);
3765 org_tline
->text
= NULL
;
3768 expanded
= true; /* Always expand %+ at least once */
3774 while (tline
) { /* main token loop */
3776 error(ERR_NONFATAL
, "interminable macro recursion");
3780 if ((mname
= tline
->text
)) {
3781 /* if this token is a local macro, look in local context */
3782 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3783 ctx
= get_ctx(mname
, &mname
, true);
3786 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3787 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3790 * We've hit an identifier. As in is_mmacro below, we first
3791 * check whether the identifier is a single-line macro at
3792 * all, then think about checking for parameters if
3795 for (m
= head
; m
; m
= m
->next
)
3796 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3802 if (m
->nparam
== 0) {
3804 * Simple case: the macro is parameterless. Discard the
3805 * one token that the macro call took, and push the
3806 * expansion back on the to-do stack.
3808 if (!m
->expansion
) {
3809 if (!strcmp("__FILE__", m
->name
)) {
3812 src_get(&num
, &file
);
3813 tline
->text
= nasm_quote(file
, strlen(file
));
3814 tline
->type
= TOK_STRING
;
3818 if (!strcmp("__LINE__", m
->name
)) {
3819 nasm_free(tline
->text
);
3820 make_tok_num(tline
, src_get_linnum());
3823 if (!strcmp("__BITS__", m
->name
)) {
3824 nasm_free(tline
->text
);
3825 make_tok_num(tline
, globalbits
);
3828 tline
= delete_Token(tline
);
3833 * Complicated case: at least one macro with this name
3834 * exists and takes parameters. We must find the
3835 * parameters in the call, count them, find the SMacro
3836 * that corresponds to that form of the macro call, and
3837 * substitute for the parameters when we expand. What a
3840 /*tline = tline->next;
3841 skip_white_(tline); */
3844 while (tok_type_(t
, TOK_SMAC_END
)) {
3845 t
->a
.mac
->in_progress
= false;
3847 t
= tline
->next
= delete_Token(t
);
3850 } while (tok_type_(tline
, TOK_WHITESPACE
));
3851 if (!tok_is_(tline
, "(")) {
3853 * This macro wasn't called with parameters: ignore
3854 * the call. (Behaviour borrowed from gnu cpp.)
3863 sparam
= PARAM_DELTA
;
3864 params
= nasm_malloc(sparam
* sizeof(Token
*));
3865 params
[0] = tline
->next
;
3866 paramsize
= nasm_malloc(sparam
* sizeof(int));
3868 while (true) { /* parameter loop */
3870 * For some unusual expansions
3871 * which concatenates function call
3874 while (tok_type_(t
, TOK_SMAC_END
)) {
3875 t
->a
.mac
->in_progress
= false;
3877 t
= tline
->next
= delete_Token(t
);
3883 "macro call expects terminating `)'");
3886 if (tline
->type
== TOK_WHITESPACE
3888 if (paramsize
[nparam
])
3891 params
[nparam
] = tline
->next
;
3892 continue; /* parameter loop */
3894 if (tline
->type
== TOK_OTHER
3895 && tline
->text
[1] == 0) {
3896 char ch
= tline
->text
[0];
3897 if (ch
== ',' && !paren
&& brackets
<= 0) {
3898 if (++nparam
>= sparam
) {
3899 sparam
+= PARAM_DELTA
;
3900 params
= nasm_realloc(params
,
3905 nasm_realloc(paramsize
,
3909 params
[nparam
] = tline
->next
;
3910 paramsize
[nparam
] = 0;
3912 continue; /* parameter loop */
3915 (brackets
> 0 || (brackets
== 0 &&
3916 !paramsize
[nparam
])))
3918 if (!(brackets
++)) {
3919 params
[nparam
] = tline
->next
;
3920 continue; /* parameter loop */
3923 if (ch
== '}' && brackets
> 0)
3924 if (--brackets
== 0) {
3926 continue; /* parameter loop */
3928 if (ch
== '(' && !brackets
)
3930 if (ch
== ')' && brackets
<= 0)
3936 error(ERR_NONFATAL
, "braces do not "
3937 "enclose all of macro parameter");
3939 paramsize
[nparam
] += white
+ 1;
3941 } /* parameter loop */
3943 while (m
&& (m
->nparam
!= nparam
||
3944 mstrcmp(m
->name
, mname
,
3948 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3949 "macro `%s' exists, "
3950 "but not taking %d parameters",
3951 mstart
->text
, nparam
);
3954 if (m
&& m
->in_progress
)
3956 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3958 * Design question: should we handle !tline, which
3959 * indicates missing ')' here, or expand those
3960 * macros anyway, which requires the (t) test a few
3964 nasm_free(paramsize
);
3968 * Expand the macro: we are placed on the last token of the
3969 * call, so that we can easily split the call from the
3970 * following tokens. We also start by pushing an SMAC_END
3971 * token for the cycle removal.
3978 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3980 m
->in_progress
= true;
3982 for (t
= m
->expansion
; t
; t
= t
->next
) {
3983 if (t
->type
>= TOK_SMAC_PARAM
) {
3984 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3988 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3989 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3992 new_Token(tline
, ttt
->type
, ttt
->text
,
3998 } else if (t
->type
== TOK_PREPROC_Q
) {
3999 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4001 } else if (t
->type
== TOK_PREPROC_QQ
) {
4002 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4005 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4011 * Having done that, get rid of the macro call, and clean
4012 * up the parameters.
4015 nasm_free(paramsize
);
4018 continue; /* main token loop */
4023 if (tline
->type
== TOK_SMAC_END
) {
4024 tline
->a
.mac
->in_progress
= false;
4025 tline
= delete_Token(tline
);
4028 tline
= tline
->next
;
4036 * Now scan the entire line and look for successive TOK_IDs that resulted
4037 * after expansion (they can't be produced by tokenize()). The successive
4038 * TOK_IDs should be concatenated.
4039 * Also we look for %+ tokens and concatenate the tokens before and after
4040 * them (without white spaces in between).
4042 if (expanded
&& paste_tokens(&thead
, true)) {
4044 * If we concatenated something, *and* we had previously expanded
4045 * an actual macro, scan the lines again for macros...
4054 *org_tline
= *thead
;
4055 /* since we just gave text to org_line, don't free it */
4057 delete_Token(thead
);
4059 /* the expression expanded to empty line;
4060 we can't return NULL for some reasons
4061 we just set the line to a single WHITESPACE token. */
4062 memset(org_tline
, 0, sizeof(*org_tline
));
4063 org_tline
->text
= NULL
;
4064 org_tline
->type
= TOK_WHITESPACE
;
4073 * Similar to expand_smacro but used exclusively with macro identifiers
4074 * right before they are fetched in. The reason is that there can be
4075 * identifiers consisting of several subparts. We consider that if there
4076 * are more than one element forming the name, user wants a expansion,
4077 * otherwise it will be left as-is. Example:
4081 * the identifier %$abc will be left as-is so that the handler for %define
4082 * will suck it and define the corresponding value. Other case:
4084 * %define _%$abc cde
4086 * In this case user wants name to be expanded *before* %define starts
4087 * working, so we'll expand %$abc into something (if it has a value;
4088 * otherwise it will be left as-is) then concatenate all successive
4091 static Token
*expand_id(Token
* tline
)
4093 Token
*cur
, *oldnext
= NULL
;
4095 if (!tline
|| !tline
->next
)
4100 (cur
->next
->type
== TOK_ID
||
4101 cur
->next
->type
== TOK_PREPROC_ID
4102 || cur
->next
->type
== TOK_NUMBER
))
4105 /* If identifier consists of just one token, don't expand */
4110 oldnext
= cur
->next
; /* Detach the tail past identifier */
4111 cur
->next
= NULL
; /* so that expand_smacro stops here */
4114 tline
= expand_smacro(tline
);
4117 /* expand_smacro possibly changhed tline; re-scan for EOL */
4119 while (cur
&& cur
->next
)
4122 cur
->next
= oldnext
;
4129 * Determine whether the given line constitutes a multi-line macro
4130 * call, and return the MMacro structure called if so. Doesn't have
4131 * to check for an initial label - that's taken care of in
4132 * expand_mmacro - but must check numbers of parameters. Guaranteed
4133 * to be called with tline->type == TOK_ID, so the putative macro
4134 * name is easy to find.
4136 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4142 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4145 * Efficiency: first we see if any macro exists with the given
4146 * name. If not, we can return NULL immediately. _Then_ we
4147 * count the parameters, and then we look further along the
4148 * list if necessary to find the proper MMacro.
4150 for (m
= head
; m
; m
= m
->next
)
4151 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4157 * OK, we have a potential macro. Count and demarcate the
4160 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4163 * So we know how many parameters we've got. Find the MMacro
4164 * structure that handles this number.
4167 if (m
->nparam_min
<= nparam
4168 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4170 * This one is right. Just check if cycle removal
4171 * prohibits us using it before we actually celebrate...
4173 if (m
->in_progress
> m
->max_depth
) {
4174 if (m
->max_depth
> 0) {
4176 "reached maximum recursion depth of %i",
4183 * It's right, and we can use it. Add its default
4184 * parameters to the end of our list if necessary.
4186 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4188 nasm_realloc(params
,
4189 ((m
->nparam_min
+ m
->ndefs
+
4190 1) * sizeof(*params
)));
4191 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4192 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4197 * If we've gone over the maximum parameter count (and
4198 * we're in Plus mode), ignore parameters beyond
4201 if (m
->plus
&& nparam
> m
->nparam_max
)
4202 nparam
= m
->nparam_max
;
4204 * Then terminate the parameter list, and leave.
4206 if (!params
) { /* need this special case */
4207 params
= nasm_malloc(sizeof(*params
));
4210 params
[nparam
] = NULL
;
4211 *params_array
= params
;
4215 * This one wasn't right: look for the next one with the
4218 for (m
= m
->next
; m
; m
= m
->next
)
4219 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4224 * After all that, we didn't find one with the right number of
4225 * parameters. Issue a warning, and fail to expand the macro.
4227 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4228 "macro `%s' exists, but not taking %d parameters",
4229 tline
->text
, nparam
);
4236 * Save MMacro invocation specific fields in
4237 * preparation for a recursive macro expansion
4239 static void push_mmacro(MMacro
*m
)
4241 MMacroInvocation
*i
;
4243 i
= nasm_malloc(sizeof(MMacroInvocation
));
4245 i
->params
= m
->params
;
4246 i
->iline
= m
->iline
;
4247 i
->nparam
= m
->nparam
;
4248 i
->rotate
= m
->rotate
;
4249 i
->paramlen
= m
->paramlen
;
4250 i
->unique
= m
->unique
;
4256 * Restore MMacro invocation specific fields that were
4257 * saved during a previous recursive macro expansion
4259 static void pop_mmacro(MMacro
*m
)
4261 MMacroInvocation
*i
;
4266 m
->params
= i
->params
;
4267 m
->iline
= i
->iline
;
4268 m
->nparam
= i
->nparam
;
4269 m
->rotate
= i
->rotate
;
4270 m
->paramlen
= i
->paramlen
;
4271 m
->unique
= i
->unique
;
4278 * Expand the multi-line macro call made by the given line, if
4279 * there is one to be expanded. If there is, push the expansion on
4280 * istk->expansion and return 1. Otherwise return 0.
4282 static int expand_mmacro(Token
* tline
)
4284 Token
*startline
= tline
;
4285 Token
*label
= NULL
;
4286 int dont_prepend
= 0;
4287 Token
**params
, *t
, *mtok
, *tt
;
4290 int i
, nparam
, *paramlen
;
4295 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4296 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4299 m
= is_mmacro(t
, ¶ms
);
4305 * We have an id which isn't a macro call. We'll assume
4306 * it might be a label; we'll also check to see if a
4307 * colon follows it. Then, if there's another id after
4308 * that lot, we'll check it again for macro-hood.
4312 if (tok_type_(t
, TOK_WHITESPACE
))
4313 last
= t
, t
= t
->next
;
4314 if (tok_is_(t
, ":")) {
4316 last
= t
, t
= t
->next
;
4317 if (tok_type_(t
, TOK_WHITESPACE
))
4318 last
= t
, t
= t
->next
;
4320 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4328 * Fix up the parameters: this involves stripping leading and
4329 * trailing whitespace, then stripping braces if they are
4332 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4333 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4335 for (i
= 0; params
[i
]; i
++) {
4337 int comma
= (!m
->plus
|| i
< nparam
- 1);
4341 if (tok_is_(t
, "{"))
4342 t
= t
->next
, brace
= true, comma
= false;
4346 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4347 break; /* ... because we have hit a comma */
4348 if (comma
&& t
->type
== TOK_WHITESPACE
4349 && tok_is_(t
->next
, ","))
4350 break; /* ... or a space then a comma */
4351 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4352 break; /* ... or a brace */
4359 * OK, we have a MMacro structure together with a set of
4360 * parameters. We must now go through the expansion and push
4361 * copies of each Line on to istk->expansion. Substitution of
4362 * parameter tokens and macro-local tokens doesn't get done
4363 * until the single-line macro substitution process; this is
4364 * because delaying them allows us to change the semantics
4365 * later through %rotate.
4367 * First, push an end marker on to istk->expansion, mark this
4368 * macro as in progress, and set up its invocation-specific
4371 ll
= nasm_malloc(sizeof(Line
));
4372 ll
->next
= istk
->expansion
;
4375 istk
->expansion
= ll
;
4378 * Save the previous MMacro expansion in the case of
4381 if (m
->max_depth
&& m
->in_progress
)
4389 m
->paramlen
= paramlen
;
4390 m
->unique
= unique
++;
4393 m
->next_active
= istk
->mstk
;
4396 for (l
= m
->expansion
; l
; l
= l
->next
) {
4399 ll
= nasm_malloc(sizeof(Line
));
4400 ll
->finishes
= NULL
;
4401 ll
->next
= istk
->expansion
;
4402 istk
->expansion
= ll
;
4405 for (t
= l
->first
; t
; t
= t
->next
) {
4409 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4411 case TOK_PREPROC_QQ
:
4412 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4414 case TOK_PREPROC_ID
:
4415 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4423 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4432 * If we had a label, push it on as the first line of
4433 * the macro expansion.
4436 if (dont_prepend
< 0)
4437 free_tlist(startline
);
4439 ll
= nasm_malloc(sizeof(Line
));
4440 ll
->finishes
= NULL
;
4441 ll
->next
= istk
->expansion
;
4442 istk
->expansion
= ll
;
4443 ll
->first
= startline
;
4444 if (!dont_prepend
) {
4446 label
= label
->next
;
4447 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4452 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4457 /* The function that actually does the error reporting */
4458 static void verror(int severity
, const char *fmt
, va_list arg
)
4462 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4464 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4465 nasm_error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4466 istk
->mstk
->lineno
, buff
);
4468 nasm_error(severity
, "%s", buff
);
4472 * Since preprocessor always operate only on the line that didn't
4473 * arrived yet, we should always use ERR_OFFBY1.
4475 static void error(int severity
, const char *fmt
, ...)
4479 /* If we're in a dead branch of IF or something like it, ignore the error */
4480 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4484 verror(severity
, fmt
, arg
);
4489 * Because %else etc are evaluated in the state context
4490 * of the previous branch, errors might get lost with error():
4491 * %if 0 ... %else trailing garbage ... %endif
4492 * So %else etc should report errors with this function.
4494 static void error_precond(int severity
, const char *fmt
, ...)
4498 /* Only ignore the error if it's really in a dead branch */
4499 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4503 verror(severity
, fmt
, arg
);
4508 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4513 istk
= nasm_malloc(sizeof(Include
));
4516 istk
->expansion
= NULL
;
4518 istk
->fp
= fopen(file
, "r");
4520 src_set_fname(nasm_strdup(file
));
4524 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4527 nested_mac_count
= 0;
4528 nested_rep_count
= 0;
4531 if (tasm_compatible_mode
) {
4532 stdmacpos
= nasm_stdmac
;
4534 stdmacpos
= nasm_stdmac_after_tasm
;
4536 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4541 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4542 * The caller, however, will also pass in 3 for preprocess-only so
4543 * we can set __PASS__ accordingly.
4545 pass
= apass
> 2 ? 2 : apass
;
4547 dephead
= deptail
= deplist
;
4549 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4551 strcpy(sl
->str
, file
);
4553 deptail
= &sl
->next
;
4557 * Define the __PASS__ macro. This is defined here unlike
4558 * all the other builtins, because it is special -- it varies between
4561 t
= nasm_malloc(sizeof(*t
));
4563 make_tok_num(t
, apass
);
4565 define_smacro(NULL
, "__PASS__", true, 0, t
);
4568 static char *pp_getline(void)
4575 * Fetch a tokenized line, either from the macro-expansion
4576 * buffer or from the input file.
4579 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4580 Line
*l
= istk
->expansion
;
4581 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4585 * This is a macro-end marker for a macro with no
4586 * name, which means it's not really a macro at all
4587 * but a %rep block, and the `in_progress' field is
4588 * more than 1, meaning that we still need to
4589 * repeat. (1 means the natural last repetition; 0
4590 * means termination by %exitrep.) We have
4591 * therefore expanded up to the %endrep, and must
4592 * push the whole block on to the expansion buffer
4593 * again. We don't bother to remove the macro-end
4594 * marker: we'd only have to generate another one
4597 l
->finishes
->in_progress
--;
4598 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4599 Token
*t
, *tt
, **tail
;
4601 ll
= nasm_malloc(sizeof(Line
));
4602 ll
->next
= istk
->expansion
;
4603 ll
->finishes
= NULL
;
4607 for (t
= l
->first
; t
; t
= t
->next
) {
4608 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4610 new_Token(NULL
, t
->type
, t
->text
, 0);
4615 istk
->expansion
= ll
;
4619 * Check whether a `%rep' was started and not ended
4620 * within this macro expansion. This can happen and
4621 * should be detected. It's a fatal error because
4622 * I'm too confused to work out how to recover
4628 "defining with name in expansion");
4629 else if (istk
->mstk
->name
)
4631 "`%%rep' without `%%endrep' within"
4632 " expansion of macro `%s'",
4637 * FIXME: investigate the relationship at this point between
4638 * istk->mstk and l->finishes
4641 MMacro
*m
= istk
->mstk
;
4642 istk
->mstk
= m
->next_active
;
4645 * This was a real macro call, not a %rep, and
4646 * therefore the parameter information needs to
4651 l
->finishes
->in_progress
--;
4653 nasm_free(m
->params
);
4654 free_tlist(m
->iline
);
4655 nasm_free(m
->paramlen
);
4656 l
->finishes
->in_progress
= 0;
4661 istk
->expansion
= l
->next
;
4663 list
->downlevel(LIST_MACRO
);
4666 while (1) { /* until we get a line we can use */
4668 if (istk
->expansion
) { /* from a macro expansion */
4670 Line
*l
= istk
->expansion
;
4672 istk
->mstk
->lineno
++;
4674 istk
->expansion
= l
->next
;
4676 p
= detoken(tline
, false);
4677 list
->line(LIST_MACRO
, p
);
4682 if (line
) { /* from the current input file */
4683 line
= prepreproc(line
);
4684 tline
= tokenize(line
);
4689 * The current file has ended; work down the istk
4696 "expected `%%endif' before end of file");
4697 /* only set line and file name if there's a next node */
4699 src_set_linnum(i
->lineno
);
4700 nasm_free(src_set_fname(i
->fname
));
4703 list
->downlevel(LIST_INCLUDE
);
4707 if (istk
->expansion
&& istk
->expansion
->finishes
)
4713 * We must expand MMacro parameters and MMacro-local labels
4714 * _before_ we plunge into directive processing, to cope
4715 * with things like `%define something %1' such as STRUC
4716 * uses. Unless we're _defining_ a MMacro, in which case
4717 * those tokens should be left alone to go into the
4718 * definition; and unless we're in a non-emitting
4719 * condition, in which case we don't want to meddle with
4722 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4723 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4724 tline
= expand_mmac_params(tline
);
4728 * Check the line to see if it's a preprocessor directive.
4730 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4732 } else if (defining
) {
4734 * We're defining a multi-line macro. We emit nothing
4736 * shove the tokenized line on to the macro definition.
4738 Line
*l
= nasm_malloc(sizeof(Line
));
4739 l
->next
= defining
->expansion
;
4742 defining
->expansion
= l
;
4744 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4746 * We're in a non-emitting branch of a condition block.
4747 * Emit nothing at all, not even a blank line: when we
4748 * emerge from the condition we'll give a line-number
4749 * directive so we keep our place correctly.
4753 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4755 * We're in a %rep block which has been terminated, so
4756 * we're walking through to the %endrep without
4757 * emitting anything. Emit nothing at all, not even a
4758 * blank line: when we emerge from the %rep block we'll
4759 * give a line-number directive so we keep our place
4765 tline
= expand_smacro(tline
);
4766 if (!expand_mmacro(tline
)) {
4768 * De-tokenize the line again, and emit it.
4770 line
= detoken(tline
, true);
4774 continue; /* expand_mmacro calls free_tlist */
4782 static void pp_cleanup(int pass
)
4785 if (defining
->name
) {
4787 "end of file while still defining macro `%s'",
4790 error(ERR_NONFATAL
, "end of file while still in %%rep");
4793 free_mmacro(defining
);
4802 nasm_free(i
->fname
);
4807 nasm_free(src_set_fname(NULL
));
4812 while ((i
= ipath
)) {
4821 void pp_include_path(char *path
)
4825 i
= nasm_malloc(sizeof(IncPath
));
4826 i
->path
= path
? nasm_strdup(path
) : NULL
;
4839 void pp_pre_include(char *fname
)
4841 Token
*inc
, *space
, *name
;
4844 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4845 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4846 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4848 l
= nasm_malloc(sizeof(Line
));
4855 void pp_pre_define(char *definition
)
4861 equals
= strchr(definition
, '=');
4862 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4863 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4866 space
->next
= tokenize(definition
);
4870 l
= nasm_malloc(sizeof(Line
));
4877 void pp_pre_undefine(char *definition
)
4882 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4883 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4884 space
->next
= tokenize(definition
);
4886 l
= nasm_malloc(sizeof(Line
));
4894 * Added by Keith Kanios:
4896 * This function is used to assist with "runtime" preprocessor
4897 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4899 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4900 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4903 void pp_runtime(char *definition
)
4907 def
= tokenize(definition
);
4908 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
4913 void pp_extra_stdmac(macros_t
*macros
)
4915 extrastdmac
= macros
;
4918 static void make_tok_num(Token
* tok
, int64_t val
)
4921 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4922 tok
->text
= nasm_strdup(numbuf
);
4923 tok
->type
= TOK_NUMBER
;