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
, *q
, *oldline
, oldchar
;
478 p
= nasm_skip_spaces(line
);
480 /* Binary search for the directive name */
482 j
= elements(tasm_directives
);
483 q
= nasm_skip_word(p
);
490 m
= nasm_stricmp(p
, tasm_directives
[k
]);
492 /* We have found a directive, so jam a % in front of it
493 * so that NASM will then recognise it as one if it's own.
498 line
= nasm_malloc(len
+ 2);
500 if (k
== TM_IFDIFI
) {
502 * NASM does not recognise IFDIFI, so we convert
503 * it to %if 0. This is not used in NASM
504 * compatible code, but does need to parse for the
505 * TASM macro package.
507 strcpy(line
+ 1, "if 0");
509 memcpy(line
+ 1, p
, len
+ 1);
524 * The pre-preprocessing stage... This function translates line
525 * number indications as they emerge from GNU cpp (`# lineno "file"
526 * flags') into NASM preprocessor line number indications (`%line
529 static char *prepreproc(char *line
)
532 char *fname
, *oldline
;
534 if (line
[0] == '#' && line
[1] == ' ') {
537 lineno
= atoi(fname
);
538 fname
+= strspn(fname
, "0123456789 ");
541 fnlen
= strcspn(fname
, "\"");
542 line
= nasm_malloc(20 + fnlen
);
543 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
546 if (tasm_compatible_mode
)
547 return check_tasm_directive(line
);
552 * Free a linked list of tokens.
554 static void free_tlist(Token
* list
)
557 list
= delete_Token(list
);
562 * Free a linked list of lines.
564 static void free_llist(Line
* list
)
570 free_tlist(l
->first
);
578 static void free_mmacro(MMacro
* m
)
581 free_tlist(m
->dlist
);
582 nasm_free(m
->defaults
);
583 free_llist(m
->expansion
);
588 * Free all currently defined macros, and free the hash tables
590 static void free_smacro_table(struct hash_table
*smt
)
594 struct hash_tbl_node
*it
= NULL
;
596 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
597 nasm_free((void *)key
);
599 SMacro
*ns
= s
->next
;
601 free_tlist(s
->expansion
);
609 static void free_mmacro_table(struct hash_table
*mmt
)
613 struct hash_tbl_node
*it
= NULL
;
616 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
617 nasm_free((void *)key
);
619 MMacro
*nm
= m
->next
;
627 static void free_macros(void)
629 free_smacro_table(&smacros
);
630 free_mmacro_table(&mmacros
);
634 * Initialize the hash tables
636 static void init_macros(void)
638 hash_init(&smacros
, HASH_LARGE
);
639 hash_init(&mmacros
, HASH_LARGE
);
643 * Pop the context stack.
645 static void ctx_pop(void)
650 free_smacro_table(&c
->localmac
);
656 * Search for a key in the hash index; adding it if necessary
657 * (in which case we initialize the data pointer to NULL.)
660 hash_findi_add(struct hash_table
*hash
, const char *str
)
662 struct hash_insert hi
;
666 r
= hash_findi(hash
, str
, &hi
);
670 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
671 return hash_add(&hi
, strx
, NULL
);
675 * Like hash_findi, but returns the data element rather than a pointer
676 * to it. Used only when not adding a new element, hence no third
680 hash_findix(struct hash_table
*hash
, const char *str
)
684 p
= hash_findi(hash
, str
, NULL
);
685 return p
? *p
: NULL
;
688 #define BUF_DELTA 512
690 * Read a line from the top file in istk, handling multiple CR/LFs
691 * at the end of the line read, and handling spurious ^Zs. Will
692 * return lines from the standard macro set if this has not already
695 static char *read_line(void)
697 char *buffer
, *p
, *q
;
698 int bufsize
, continued_count
;
702 const unsigned char *p
= stdmacpos
;
707 len
+= pp_directives_len
[c
-0x80]+1;
711 ret
= nasm_malloc(len
+1);
713 while ((c
= *stdmacpos
++)) {
715 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
716 q
+= pp_directives_len
[c
-0x80];
726 /* This was the last of the standard macro chain... */
728 if (any_extrastdmac
) {
729 stdmacpos
= extrastdmac
;
730 any_extrastdmac
= false;
731 } else if (do_predef
) {
733 Token
*head
, **tail
, *t
;
736 * Nasty hack: here we push the contents of
737 * `predef' on to the top-level expansion stack,
738 * since this is the most convenient way to
739 * implement the pre-include and pre-define
742 for (pd
= predef
; pd
; pd
= pd
->next
) {
745 for (t
= pd
->first
; t
; t
= t
->next
) {
746 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
747 tail
= &(*tail
)->next
;
749 l
= nasm_malloc(sizeof(Line
));
750 l
->next
= istk
->expansion
;
762 buffer
= nasm_malloc(BUF_DELTA
);
766 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
770 if (p
> buffer
&& p
[-1] == '\n') {
771 /* Convert backslash-CRLF line continuation sequences into
772 nothing at all (for DOS and Windows) */
773 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
778 /* Also convert backslash-LF line continuation sequences into
779 nothing at all (for Unix) */
780 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
788 if (p
- buffer
> bufsize
- 10) {
789 int32_t offset
= p
- buffer
;
790 bufsize
+= BUF_DELTA
;
791 buffer
= nasm_realloc(buffer
, bufsize
);
792 p
= buffer
+ offset
; /* prevent stale-pointer problems */
796 if (!q
&& p
== buffer
) {
801 src_set_linnum(src_get_linnum() + istk
->lineinc
+
802 (continued_count
* istk
->lineinc
));
805 * Play safe: remove CRs as well as LFs, if any of either are
806 * present at the end of the line.
808 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
812 * Handle spurious ^Z, which may be inserted into source files
813 * by some file transfer utilities.
815 buffer
[strcspn(buffer
, "\032")] = '\0';
817 list
->line(LIST_READ
, buffer
);
823 * Tokenize a line of text. This is a very simple process since we
824 * don't need to parse the value out of e.g. numeric tokens: we
825 * simply split one string into many.
827 static Token
*tokenize(char *line
)
830 enum pp_token_type type
;
832 Token
*t
, **tail
= &list
;
838 if (*p
== '+' && !nasm_isdigit(p
[1])) {
841 } else if (nasm_isdigit(*p
) ||
842 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
846 while (nasm_isdigit(*p
));
847 type
= TOK_PREPROC_ID
;
848 } else if (*p
== '{') {
850 while (*p
&& *p
!= '}') {
857 type
= TOK_PREPROC_ID
;
858 } else if (*p
== '[') {
860 line
+= 2; /* Skip the leading %[ */
862 while (lvl
&& (c
= *p
++)) {
874 p
= nasm_skip_string(p
)+1;
884 error(ERR_NONFATAL
, "unterminated %[ construct");
886 } else if (*p
== '?') {
887 type
= TOK_PREPROC_Q
; /* %? */
890 type
= TOK_PREPROC_QQ
; /* %?? */
893 } else if (isidchar(*p
) ||
894 ((*p
== '!' || *p
== '%' || *p
== '$') &&
899 while (isidchar(*p
));
900 type
= TOK_PREPROC_ID
;
906 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
909 while (*p
&& isidchar(*p
))
911 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
916 p
= nasm_skip_string(p
);
921 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
922 /* Handling unterminated strings by UNV */
925 } else if (p
[0] == '$' && p
[1] == '$') {
926 type
= TOK_OTHER
; /* TOKEN_BASE */
928 } else if (isnumstart(*p
)) {
930 bool is_float
= false;
946 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
948 if (*p
== '+' || *p
== '-') {
949 /* e can only be followed by +/- if it is either a
950 prefixed hex number or a floating-point number */
954 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
956 } else if (c
== 'P' || c
== 'p') {
958 if (*p
== '+' || *p
== '-')
960 } else if (isnumchar(c
) || c
== '_')
963 /* we need to deal with consequences of the legacy
964 parser, like "1.nolist" being two tokens
965 (TOK_NUMBER, TOK_ID) here; at least give it
966 a shot for now. In the future, we probably need
967 a flex-based scanner with proper pattern matching
968 to do it as well as it can be done. Nothing in
969 the world is going to help the person who wants
970 0x123.p16 interpreted as two tokens, though. */
975 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
976 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
977 (*r
== 'p' || *r
== 'P')) {
981 break; /* Terminate the token */
985 p
--; /* Point to first character beyond number */
987 if (p
== line
+1 && *line
== '$') {
988 type
= TOK_OTHER
; /* TOKEN_HERE */
990 if (has_e
&& !is_hex
) {
991 /* 1e13 is floating-point, but 1e13h is not */
995 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
997 } else if (nasm_isspace(*p
)) {
998 type
= TOK_WHITESPACE
;
999 p
= nasm_skip_spaces(p
);
1001 * Whitespace just before end-of-line is discarded by
1002 * pretending it's a comment; whitespace just before a
1003 * comment gets lumped into the comment.
1005 if (!*p
|| *p
== ';') {
1010 } else if (*p
== ';') {
1016 * Anything else is an operator of some kind. We check
1017 * for all the double-character operators (>>, <<, //,
1018 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1019 * else is a single-character operator.
1022 if ((p
[0] == '>' && p
[1] == '>') ||
1023 (p
[0] == '<' && p
[1] == '<') ||
1024 (p
[0] == '/' && p
[1] == '/') ||
1025 (p
[0] == '<' && p
[1] == '=') ||
1026 (p
[0] == '>' && p
[1] == '=') ||
1027 (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] == '^')) {
1038 /* Handling unterminated string by UNV */
1041 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1042 t->text[p-line] = *line;
1046 if (type
!= TOK_COMMENT
) {
1047 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1056 * this function allocates a new managed block of memory and
1057 * returns a pointer to the block. The managed blocks are
1058 * deleted only all at once by the delete_Blocks function.
1060 static void *new_Block(size_t size
)
1062 Blocks
*b
= &blocks
;
1064 /* first, get to the end of the linked list */
1067 /* now allocate the requested chunk */
1068 b
->chunk
= nasm_malloc(size
);
1070 /* now allocate a new block for the next request */
1071 b
->next
= nasm_malloc(sizeof(Blocks
));
1072 /* and initialize the contents of the new block */
1073 b
->next
->next
= NULL
;
1074 b
->next
->chunk
= NULL
;
1079 * this function deletes all managed blocks of memory
1081 static void delete_Blocks(void)
1083 Blocks
*a
, *b
= &blocks
;
1086 * keep in mind that the first block, pointed to by blocks
1087 * is a static and not dynamically allocated, so we don't
1092 nasm_free(b
->chunk
);
1101 * this function creates a new Token and passes a pointer to it
1102 * back to the caller. It sets the type and text elements, and
1103 * also the a.mac and next elements to NULL.
1105 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1106 const char *text
, int txtlen
)
1112 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1113 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1114 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1115 freeTokens
[i
].next
= NULL
;
1118 freeTokens
= t
->next
;
1122 if (type
== TOK_WHITESPACE
|| !text
) {
1126 txtlen
= strlen(text
);
1127 t
->text
= nasm_malloc(txtlen
+1);
1128 memcpy(t
->text
, text
, txtlen
);
1129 t
->text
[txtlen
] = '\0';
1134 static Token
*delete_Token(Token
* t
)
1136 Token
*next
= t
->next
;
1138 t
->next
= freeTokens
;
1144 * Convert a line of tokens back into text.
1145 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1146 * will be transformed into ..@ctxnum.xxx
1148 static char *detoken(Token
* tlist
, bool expand_locals
)
1156 for (t
= tlist
; t
; t
= t
->next
) {
1157 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1158 char *p
= getenv(t
->text
+ 2);
1161 t
->text
= nasm_strdup(p
);
1165 /* Expand local macros here and not during preprocessing */
1166 if (expand_locals
&&
1167 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1168 t
->text
[0] == '%' && t
->text
[1] == '$') {
1171 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1174 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1175 p
= nasm_strcat(buffer
, q
);
1180 if (t
->type
== TOK_WHITESPACE
) {
1182 } else if (t
->text
) {
1183 len
+= strlen(t
->text
);
1186 p
= line
= nasm_malloc(len
+ 1);
1187 for (t
= tlist
; t
; t
= t
->next
) {
1188 if (t
->type
== TOK_WHITESPACE
) {
1190 } else if (t
->text
) {
1201 * A scanner, suitable for use by the expression evaluator, which
1202 * operates on a line of Tokens. Expects a pointer to a pointer to
1203 * the first token in the line to be passed in as its private_data
1206 * FIX: This really needs to be unified with stdscan.
1208 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1210 Token
**tlineptr
= private_data
;
1212 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1216 *tlineptr
= tline
? tline
->next
: NULL
;
1218 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1219 tline
->type
== TOK_COMMENT
));
1222 return tokval
->t_type
= TOKEN_EOS
;
1224 tokval
->t_charptr
= tline
->text
;
1226 if (tline
->text
[0] == '$' && !tline
->text
[1])
1227 return tokval
->t_type
= TOKEN_HERE
;
1228 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1229 return tokval
->t_type
= TOKEN_BASE
;
1231 if (tline
->type
== TOK_ID
) {
1232 p
= tokval
->t_charptr
= tline
->text
;
1234 tokval
->t_charptr
++;
1235 return tokval
->t_type
= TOKEN_ID
;
1238 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1239 if (r
>= p
+MAX_KEYWORD
)
1240 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1241 *s
++ = nasm_tolower(*r
);
1244 /* right, so we have an identifier sitting in temp storage. now,
1245 * is it actually a register or instruction name, or what? */
1246 return nasm_token_hash(ourcopy
, tokval
);
1249 if (tline
->type
== TOK_NUMBER
) {
1251 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1252 tokval
->t_charptr
= tline
->text
;
1254 return tokval
->t_type
= TOKEN_ERRNUM
;
1256 return tokval
->t_type
= TOKEN_NUM
;
1259 if (tline
->type
== TOK_FLOAT
) {
1260 return tokval
->t_type
= TOKEN_FLOAT
;
1263 if (tline
->type
== TOK_STRING
) {
1266 bq
= tline
->text
[0];
1267 tokval
->t_charptr
= tline
->text
;
1268 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1270 if (ep
[0] != bq
|| ep
[1] != '\0')
1271 return tokval
->t_type
= TOKEN_ERRSTR
;
1273 return tokval
->t_type
= TOKEN_STR
;
1276 if (tline
->type
== TOK_OTHER
) {
1277 if (!strcmp(tline
->text
, "<<"))
1278 return tokval
->t_type
= TOKEN_SHL
;
1279 if (!strcmp(tline
->text
, ">>"))
1280 return tokval
->t_type
= TOKEN_SHR
;
1281 if (!strcmp(tline
->text
, "//"))
1282 return tokval
->t_type
= TOKEN_SDIV
;
1283 if (!strcmp(tline
->text
, "%%"))
1284 return tokval
->t_type
= TOKEN_SMOD
;
1285 if (!strcmp(tline
->text
, "=="))
1286 return tokval
->t_type
= TOKEN_EQ
;
1287 if (!strcmp(tline
->text
, "<>"))
1288 return tokval
->t_type
= TOKEN_NE
;
1289 if (!strcmp(tline
->text
, "!="))
1290 return tokval
->t_type
= TOKEN_NE
;
1291 if (!strcmp(tline
->text
, "<="))
1292 return tokval
->t_type
= TOKEN_LE
;
1293 if (!strcmp(tline
->text
, ">="))
1294 return tokval
->t_type
= TOKEN_GE
;
1295 if (!strcmp(tline
->text
, "&&"))
1296 return tokval
->t_type
= TOKEN_DBL_AND
;
1297 if (!strcmp(tline
->text
, "^^"))
1298 return tokval
->t_type
= TOKEN_DBL_XOR
;
1299 if (!strcmp(tline
->text
, "||"))
1300 return tokval
->t_type
= TOKEN_DBL_OR
;
1304 * We have no other options: just return the first character of
1307 return tokval
->t_type
= tline
->text
[0];
1311 * Compare a string to the name of an existing macro; this is a
1312 * simple wrapper which calls either strcmp or nasm_stricmp
1313 * depending on the value of the `casesense' parameter.
1315 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1317 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1321 * Compare a string to the name of an existing macro; this is a
1322 * simple wrapper which calls either strcmp or nasm_stricmp
1323 * depending on the value of the `casesense' parameter.
1325 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1327 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1331 * Return the Context structure associated with a %$ token. Return
1332 * NULL, having _already_ reported an error condition, if the
1333 * context stack isn't deep enough for the supplied number of $
1335 * If all_contexts == true, contexts that enclose current are
1336 * also scanned for such smacro, until it is found; if not -
1337 * only the context that directly results from the number of $'s
1338 * in variable's name.
1340 * If "namep" is non-NULL, set it to the pointer to the macro name
1341 * tail, i.e. the part beyond %$...
1343 static Context
*get_ctx(const char *name
, const char **namep
,
1353 if (!name
|| name
[0] != '%' || name
[1] != '$')
1357 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1364 while (ctx
&& *name
== '$') {
1370 error(ERR_NONFATAL
, "`%s': context stack is only"
1371 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1382 /* Search for this smacro in found context */
1383 m
= hash_findix(&ctx
->localmac
, name
);
1385 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1396 * Check to see if a file is already in a string list
1398 static bool in_list(const StrList
*list
, const char *str
)
1401 if (!strcmp(list
->str
, str
))
1409 * Open an include file. This routine must always return a valid
1410 * file pointer if it returns - it's responsible for throwing an
1411 * ERR_FATAL and bombing out completely if not. It should also try
1412 * the include path one by one until it finds the file or reaches
1413 * the end of the path.
1415 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1420 IncPath
*ip
= ipath
;
1421 int len
= strlen(file
);
1422 size_t prefix_len
= 0;
1426 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1427 memcpy(sl
->str
, prefix
, prefix_len
);
1428 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1429 fp
= fopen(sl
->str
, "r");
1430 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1448 prefix_len
= strlen(prefix
);
1450 /* -MG given and file not found */
1451 if (dhead
&& !in_list(*dhead
, file
)) {
1452 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1454 strcpy(sl
->str
, file
);
1462 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1463 return NULL
; /* never reached - placate compilers */
1467 * Determine if we should warn on defining a single-line macro of
1468 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1469 * return true if _any_ single-line macro of that name is defined.
1470 * Otherwise, will return true if a single-line macro with either
1471 * `nparam' or no parameters is defined.
1473 * If a macro with precisely the right number of parameters is
1474 * defined, or nparam is -1, the address of the definition structure
1475 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1476 * is NULL, no action will be taken regarding its contents, and no
1479 * Note that this is also called with nparam zero to resolve
1482 * If you already know which context macro belongs to, you can pass
1483 * the context pointer as first parameter; if you won't but name begins
1484 * with %$ the context will be automatically computed. If all_contexts
1485 * is true, macro will be searched in outer contexts as well.
1488 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1491 struct hash_table
*smtbl
;
1495 smtbl
= &ctx
->localmac
;
1496 } else if (name
[0] == '%' && name
[1] == '$') {
1498 ctx
= get_ctx(name
, &name
, false);
1500 return false; /* got to return _something_ */
1501 smtbl
= &ctx
->localmac
;
1505 m
= (SMacro
*) hash_findix(smtbl
, name
);
1508 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1509 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1511 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1525 * Count and mark off the parameters in a multi-line macro call.
1526 * This is called both from within the multi-line macro expansion
1527 * code, and also to mark off the default parameters when provided
1528 * in a %macro definition line.
1530 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1532 int paramsize
, brace
;
1534 *nparam
= paramsize
= 0;
1537 /* +1: we need space for the final NULL */
1538 if (*nparam
+1 >= paramsize
) {
1539 paramsize
+= PARAM_DELTA
;
1540 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1544 if (tok_is_(t
, "{"))
1546 (*params
)[(*nparam
)++] = t
;
1547 while (tok_isnt_(t
, brace
? "}" : ","))
1549 if (t
) { /* got a comma/brace */
1553 * Now we've found the closing brace, look further
1557 if (tok_isnt_(t
, ",")) {
1559 "braces do not enclose all of macro parameter");
1560 while (tok_isnt_(t
, ","))
1564 t
= t
->next
; /* eat the comma */
1571 * Determine whether one of the various `if' conditions is true or
1574 * We must free the tline we get passed.
1576 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1578 enum pp_conditional i
= PP_COND(ct
);
1580 Token
*t
, *tt
, **tptr
, *origline
;
1581 struct tokenval tokval
;
1583 enum pp_token_type needtype
;
1589 j
= false; /* have we matched yet? */
1594 if (tline
->type
!= TOK_ID
) {
1596 "`%s' expects context identifiers", pp_directives
[ct
]);
1597 free_tlist(origline
);
1600 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1602 tline
= tline
->next
;
1607 j
= false; /* have we matched yet? */
1610 if (!tline
|| (tline
->type
!= TOK_ID
&&
1611 (tline
->type
!= TOK_PREPROC_ID
||
1612 tline
->text
[1] != '$'))) {
1614 "`%s' expects macro identifiers", pp_directives
[ct
]);
1617 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1619 tline
= tline
->next
;
1625 tline
= expand_smacro(tline
);
1627 while (tok_isnt_(tt
, ","))
1631 "`%s' expects two comma-separated arguments",
1636 j
= true; /* assume equality unless proved not */
1637 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1638 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1639 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1643 if (t
->type
== TOK_WHITESPACE
) {
1647 if (tt
->type
== TOK_WHITESPACE
) {
1651 if (tt
->type
!= t
->type
) {
1652 j
= false; /* found mismatching tokens */
1655 /* When comparing strings, need to unquote them first */
1656 if (t
->type
== TOK_STRING
) {
1657 size_t l1
= nasm_unquote(t
->text
, NULL
);
1658 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1664 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1668 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1669 j
= false; /* found mismatching tokens */
1676 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1677 j
= false; /* trailing gunk on one end or other */
1683 MMacro searching
, *mmac
;
1686 tline
= expand_id(tline
);
1687 if (!tok_type_(tline
, TOK_ID
)) {
1689 "`%s' expects a macro name", pp_directives
[ct
]);
1692 searching
.name
= nasm_strdup(tline
->text
);
1693 searching
.casesense
= true;
1694 searching
.plus
= false;
1695 searching
.nolist
= false;
1696 searching
.in_progress
= 0;
1697 searching
.max_depth
= 0;
1698 searching
.rep_nest
= NULL
;
1699 searching
.nparam_min
= 0;
1700 searching
.nparam_max
= INT_MAX
;
1701 tline
= expand_smacro(tline
->next
);
1704 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1706 "`%s' expects a parameter count or nothing",
1709 searching
.nparam_min
= searching
.nparam_max
=
1710 readnum(tline
->text
, &j
);
1713 "unable to parse parameter count `%s'",
1716 if (tline
&& tok_is_(tline
->next
, "-")) {
1717 tline
= tline
->next
->next
;
1718 if (tok_is_(tline
, "*"))
1719 searching
.nparam_max
= INT_MAX
;
1720 else if (!tok_type_(tline
, TOK_NUMBER
))
1722 "`%s' expects a parameter count after `-'",
1725 searching
.nparam_max
= readnum(tline
->text
, &j
);
1728 "unable to parse parameter count `%s'",
1730 if (searching
.nparam_min
> searching
.nparam_max
)
1732 "minimum parameter count exceeds maximum");
1735 if (tline
&& tok_is_(tline
->next
, "+")) {
1736 tline
= tline
->next
;
1737 searching
.plus
= true;
1739 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1741 if (!strcmp(mmac
->name
, searching
.name
) &&
1742 (mmac
->nparam_min
<= searching
.nparam_max
1744 && (searching
.nparam_min
<= mmac
->nparam_max
1751 if (tline
&& tline
->next
)
1752 error(ERR_WARNING
|ERR_PASS1
,
1753 "trailing garbage after %%ifmacro ignored");
1754 nasm_free(searching
.name
);
1763 needtype
= TOK_NUMBER
;
1766 needtype
= TOK_STRING
;
1770 t
= tline
= expand_smacro(tline
);
1772 while (tok_type_(t
, TOK_WHITESPACE
) ||
1773 (needtype
== TOK_NUMBER
&&
1774 tok_type_(t
, TOK_OTHER
) &&
1775 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1779 j
= tok_type_(t
, needtype
);
1783 t
= tline
= expand_smacro(tline
);
1784 while (tok_type_(t
, TOK_WHITESPACE
))
1789 t
= t
->next
; /* Skip the actual token */
1790 while (tok_type_(t
, TOK_WHITESPACE
))
1792 j
= !t
; /* Should be nothing left */
1797 t
= tline
= expand_smacro(tline
);
1798 while (tok_type_(t
, TOK_WHITESPACE
))
1801 j
= !t
; /* Should be empty */
1805 t
= tline
= expand_smacro(tline
);
1807 tokval
.t_type
= TOKEN_INVALID
;
1808 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1809 NULL
, pass
| CRITICAL
, error
, NULL
);
1813 error(ERR_WARNING
|ERR_PASS1
,
1814 "trailing garbage after expression ignored");
1815 if (!is_simple(evalresult
)) {
1817 "non-constant value given to `%s'", pp_directives
[ct
]);
1820 j
= reloc_value(evalresult
) != 0;
1825 "preprocessor directive `%s' not yet implemented",
1830 free_tlist(origline
);
1831 return j
^ PP_NEGATIVE(ct
);
1834 free_tlist(origline
);
1839 * Common code for defining an smacro
1841 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1842 int nparam
, Token
*expansion
)
1844 SMacro
*smac
, **smhead
;
1845 struct hash_table
*smtbl
;
1847 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1849 error(ERR_WARNING
|ERR_PASS1
,
1850 "single-line macro `%s' defined both with and"
1851 " without parameters", mname
);
1853 /* Some instances of the old code considered this a failure,
1854 some others didn't. What is the right thing to do here? */
1855 free_tlist(expansion
);
1856 return false; /* Failure */
1859 * We're redefining, so we have to take over an
1860 * existing SMacro structure. This means freeing
1861 * what was already in it.
1863 nasm_free(smac
->name
);
1864 free_tlist(smac
->expansion
);
1867 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1868 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1869 smac
= nasm_malloc(sizeof(SMacro
));
1870 smac
->next
= *smhead
;
1873 smac
->name
= nasm_strdup(mname
);
1874 smac
->casesense
= casesense
;
1875 smac
->nparam
= nparam
;
1876 smac
->expansion
= expansion
;
1877 smac
->in_progress
= false;
1878 return true; /* Success */
1882 * Undefine an smacro
1884 static void undef_smacro(Context
*ctx
, const char *mname
)
1886 SMacro
**smhead
, *s
, **sp
;
1887 struct hash_table
*smtbl
;
1889 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1890 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1894 * We now have a macro name... go hunt for it.
1897 while ((s
= *sp
) != NULL
) {
1898 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1901 free_tlist(s
->expansion
);
1911 * Parse a mmacro specification.
1913 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1917 tline
= tline
->next
;
1919 tline
= expand_id(tline
);
1920 if (!tok_type_(tline
, TOK_ID
)) {
1921 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1926 def
->name
= nasm_strdup(tline
->text
);
1928 def
->nolist
= false;
1929 def
->in_progress
= 0;
1930 def
->rep_nest
= NULL
;
1931 def
->nparam_min
= 0;
1932 def
->nparam_max
= 0;
1934 tline
= expand_smacro(tline
->next
);
1936 if (!tok_type_(tline
, TOK_NUMBER
)) {
1937 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1939 def
->nparam_min
= def
->nparam_max
=
1940 readnum(tline
->text
, &err
);
1943 "unable to parse parameter count `%s'", tline
->text
);
1945 if (tline
&& tok_is_(tline
->next
, "-")) {
1946 tline
= tline
->next
->next
;
1947 if (tok_is_(tline
, "*")) {
1948 def
->nparam_max
= INT_MAX
;
1949 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1951 "`%s' expects a parameter count after `-'", directive
);
1953 def
->nparam_max
= readnum(tline
->text
, &err
);
1955 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1958 if (def
->nparam_min
> def
->nparam_max
) {
1959 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1963 if (tline
&& tok_is_(tline
->next
, "+")) {
1964 tline
= tline
->next
;
1967 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1968 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1969 tline
= tline
->next
;
1974 * Handle default parameters.
1976 if (tline
&& tline
->next
) {
1977 def
->dlist
= tline
->next
;
1979 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1982 def
->defaults
= NULL
;
1984 def
->expansion
= NULL
;
1986 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1988 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1989 "too many default macro parameters");
1996 * Decode a size directive
1998 static int parse_size(const char *str
) {
1999 static const char *size_names
[] =
2000 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2001 static const int sizes
[] =
2002 { 0, 1, 4, 16, 8, 10, 2, 32 };
2004 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
2008 * nasm_unquote with error if the string contains NUL characters.
2009 * If the string contains NUL characters, issue an error and return
2010 * the C len, i.e. truncate at the NUL.
2012 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
2014 size_t len
= nasm_unquote(qstr
, NULL
);
2015 size_t clen
= strlen(qstr
);
2018 error(ERR_NONFATAL
, "NUL character in `%s' directive",
2019 pp_directives
[directive
]);
2025 * find and process preprocessor directive in passed line
2026 * Find out if a line contains a preprocessor directive, and deal
2029 * If a directive _is_ found, it is the responsibility of this routine
2030 * (and not the caller) to free_tlist() the line.
2032 * @param tline a pointer to the current tokeninzed line linked list
2033 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2036 static int do_directive(Token
* tline
)
2038 enum preproc_token i
;
2051 MMacro
*mmac
, **mmhead
;
2052 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2054 struct tokenval tokval
;
2056 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2064 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2065 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2066 || tline
->text
[1] == '!'))
2067 return NO_DIRECTIVE_FOUND
;
2069 i
= pp_token_hash(tline
->text
);
2072 * If we're in a non-emitting branch of a condition construct,
2073 * or walking to the end of an already terminated %rep block,
2074 * we should ignore all directives except for condition
2077 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2078 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2079 return NO_DIRECTIVE_FOUND
;
2083 * If we're defining a macro or reading a %rep block, we should
2084 * ignore all directives except for %macro/%imacro (which nest),
2085 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2086 * If we're in a %rep block, another %rep nests, so should be let through.
2088 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2089 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2090 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2091 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2092 return NO_DIRECTIVE_FOUND
;
2096 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2097 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2099 return NO_DIRECTIVE_FOUND
;
2100 } else if (nested_mac_count
> 0) {
2101 if (i
== PP_ENDMACRO
) {
2103 return NO_DIRECTIVE_FOUND
;
2106 if (!defining
->name
) {
2109 return NO_DIRECTIVE_FOUND
;
2110 } else if (nested_rep_count
> 0) {
2111 if (i
== PP_ENDREP
) {
2113 return NO_DIRECTIVE_FOUND
;
2121 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2123 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2126 /* Directive to tell NASM what the default stack size is. The
2127 * default is for a 16-bit stack, and this can be overriden with
2129 * the following form:
2131 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2133 tline
= tline
->next
;
2134 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2135 tline
= tline
->next
;
2136 if (!tline
|| tline
->type
!= TOK_ID
) {
2137 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2138 free_tlist(origline
);
2139 return DIRECTIVE_FOUND
;
2141 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2142 /* All subsequent ARG directives are for a 32-bit stack */
2144 StackPointer
= "ebp";
2147 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2148 /* All subsequent ARG directives are for a 64-bit stack */
2150 StackPointer
= "rbp";
2153 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2154 /* All subsequent ARG directives are for a 16-bit stack,
2155 * far function call.
2158 StackPointer
= "bp";
2161 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2162 /* All subsequent ARG directives are for a 16-bit stack,
2163 * far function call. We don't support near functions.
2166 StackPointer
= "bp";
2170 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2171 free_tlist(origline
);
2172 return DIRECTIVE_FOUND
;
2174 free_tlist(origline
);
2175 return DIRECTIVE_FOUND
;
2178 /* TASM like ARG directive to define arguments to functions, in
2179 * the following form:
2181 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2185 char *arg
, directive
[256];
2186 int size
= StackSize
;
2188 /* Find the argument name */
2189 tline
= tline
->next
;
2190 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2191 tline
= tline
->next
;
2192 if (!tline
|| tline
->type
!= TOK_ID
) {
2193 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2194 free_tlist(origline
);
2195 return DIRECTIVE_FOUND
;
2199 /* Find the argument size type */
2200 tline
= tline
->next
;
2201 if (!tline
|| tline
->type
!= TOK_OTHER
2202 || tline
->text
[0] != ':') {
2204 "Syntax error processing `%%arg' directive");
2205 free_tlist(origline
);
2206 return DIRECTIVE_FOUND
;
2208 tline
= tline
->next
;
2209 if (!tline
|| tline
->type
!= TOK_ID
) {
2210 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2211 free_tlist(origline
);
2212 return DIRECTIVE_FOUND
;
2215 /* Allow macro expansion of type parameter */
2216 tt
= tokenize(tline
->text
);
2217 tt
= expand_smacro(tt
);
2218 size
= parse_size(tt
->text
);
2221 "Invalid size type for `%%arg' missing directive");
2223 free_tlist(origline
);
2224 return DIRECTIVE_FOUND
;
2228 /* Round up to even stack slots */
2229 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2231 /* Now define the macro for the argument */
2232 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2233 arg
, StackPointer
, offset
);
2234 do_directive(tokenize(directive
));
2237 /* Move to the next argument in the list */
2238 tline
= tline
->next
;
2239 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2240 tline
= tline
->next
;
2241 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2243 free_tlist(origline
);
2244 return DIRECTIVE_FOUND
;
2247 /* TASM like LOCAL directive to define local variables for a
2248 * function, in the following form:
2250 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2252 * The '= LocalSize' at the end is ignored by NASM, but is
2253 * required by TASM to define the local parameter size (and used
2254 * by the TASM macro package).
2256 offset
= LocalOffset
;
2258 char *local
, directive
[256];
2259 int size
= StackSize
;
2261 /* Find the argument name */
2262 tline
= tline
->next
;
2263 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2264 tline
= tline
->next
;
2265 if (!tline
|| tline
->type
!= TOK_ID
) {
2267 "`%%local' missing argument parameter");
2268 free_tlist(origline
);
2269 return DIRECTIVE_FOUND
;
2271 local
= tline
->text
;
2273 /* Find the argument size type */
2274 tline
= tline
->next
;
2275 if (!tline
|| tline
->type
!= TOK_OTHER
2276 || tline
->text
[0] != ':') {
2278 "Syntax error processing `%%local' directive");
2279 free_tlist(origline
);
2280 return DIRECTIVE_FOUND
;
2282 tline
= tline
->next
;
2283 if (!tline
|| tline
->type
!= TOK_ID
) {
2285 "`%%local' missing size type parameter");
2286 free_tlist(origline
);
2287 return DIRECTIVE_FOUND
;
2290 /* Allow macro expansion of type parameter */
2291 tt
= tokenize(tline
->text
);
2292 tt
= expand_smacro(tt
);
2293 size
= parse_size(tt
->text
);
2296 "Invalid size type for `%%local' missing directive");
2298 free_tlist(origline
);
2299 return DIRECTIVE_FOUND
;
2303 /* Round up to even stack slots */
2304 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2306 offset
+= size
; /* Negative offset, increment before */
2308 /* Now define the macro for the argument */
2309 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2310 local
, StackPointer
, offset
);
2311 do_directive(tokenize(directive
));
2313 /* Now define the assign to setup the enter_c macro correctly */
2314 snprintf(directive
, sizeof(directive
),
2315 "%%assign %%$localsize %%$localsize+%d", size
);
2316 do_directive(tokenize(directive
));
2318 /* Move to the next argument in the list */
2319 tline
= tline
->next
;
2320 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2321 tline
= tline
->next
;
2322 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2323 LocalOffset
= offset
;
2324 free_tlist(origline
);
2325 return DIRECTIVE_FOUND
;
2329 error(ERR_WARNING
|ERR_PASS1
,
2330 "trailing garbage after `%%clear' ignored");
2333 free_tlist(origline
);
2334 return DIRECTIVE_FOUND
;
2337 t
= tline
->next
= expand_smacro(tline
->next
);
2339 if (!t
|| (t
->type
!= TOK_STRING
&&
2340 t
->type
!= TOK_INTERNAL_STRING
)) {
2341 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2342 free_tlist(origline
);
2343 return DIRECTIVE_FOUND
; /* but we did _something_ */
2346 error(ERR_WARNING
|ERR_PASS1
,
2347 "trailing garbage after `%%depend' ignored");
2349 if (t
->type
!= TOK_INTERNAL_STRING
)
2350 nasm_unquote_cstr(p
, i
);
2351 if (dephead
&& !in_list(*dephead
, p
)) {
2352 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2356 deptail
= &sl
->next
;
2358 free_tlist(origline
);
2359 return DIRECTIVE_FOUND
;
2362 t
= tline
->next
= expand_smacro(tline
->next
);
2365 if (!t
|| (t
->type
!= TOK_STRING
&&
2366 t
->type
!= TOK_INTERNAL_STRING
)) {
2367 error(ERR_NONFATAL
, "`%%include' expects a file name");
2368 free_tlist(origline
);
2369 return DIRECTIVE_FOUND
; /* but we did _something_ */
2372 error(ERR_WARNING
|ERR_PASS1
,
2373 "trailing garbage after `%%include' ignored");
2375 if (t
->type
!= TOK_INTERNAL_STRING
)
2376 nasm_unquote_cstr(p
, i
);
2377 inc
= nasm_malloc(sizeof(Include
));
2380 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2382 /* -MG given but file not found */
2385 inc
->fname
= src_set_fname(nasm_strdup(p
));
2386 inc
->lineno
= src_set_linnum(0);
2388 inc
->expansion
= NULL
;
2391 list
->uplevel(LIST_INCLUDE
);
2393 free_tlist(origline
);
2394 return DIRECTIVE_FOUND
;
2398 static macros_t
*use_pkg
;
2399 const char *pkg_macro
;
2401 tline
= tline
->next
;
2403 tline
= expand_id(tline
);
2405 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2406 tline
->type
!= TOK_INTERNAL_STRING
&&
2407 tline
->type
!= TOK_ID
)) {
2408 error(ERR_NONFATAL
, "`%%use' expects a package name");
2409 free_tlist(origline
);
2410 return DIRECTIVE_FOUND
; /* but we did _something_ */
2413 error(ERR_WARNING
|ERR_PASS1
,
2414 "trailing garbage after `%%use' ignored");
2415 if (tline
->type
== TOK_STRING
)
2416 nasm_unquote_cstr(tline
->text
, i
);
2417 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2419 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2420 /* The first string will be <%define>__USE_*__ */
2421 pkg_macro
= (char *)use_pkg
+ 1;
2422 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2423 /* Not already included, go ahead and include it */
2424 stdmacpos
= use_pkg
;
2426 free_tlist(origline
);
2427 return DIRECTIVE_FOUND
;
2432 tline
= tline
->next
;
2434 tline
= expand_id(tline
);
2436 if (!tok_type_(tline
, TOK_ID
)) {
2437 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2439 free_tlist(origline
);
2440 return DIRECTIVE_FOUND
; /* but we did _something_ */
2443 error(ERR_WARNING
|ERR_PASS1
,
2444 "trailing garbage after `%s' ignored",
2446 p
= nasm_strdup(tline
->text
);
2448 p
= NULL
; /* Anonymous */
2452 ctx
= nasm_malloc(sizeof(Context
));
2454 hash_init(&ctx
->localmac
, HASH_SMALL
);
2456 ctx
->number
= unique
++;
2461 error(ERR_NONFATAL
, "`%s': context stack is empty",
2463 } else if (i
== PP_POP
) {
2464 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2465 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2467 cstk
->name
? cstk
->name
: "anonymous", p
);
2472 nasm_free(cstk
->name
);
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
;
2481 severity
= ERR_FATAL
;
2484 severity
= ERR_NONFATAL
;
2487 severity
= ERR_WARNING
|ERR_WARN_USER
;
2492 /* Only error out if this is the final pass */
2493 if (pass
!= 2 && i
!= PP_FATAL
)
2494 return DIRECTIVE_FOUND
;
2496 tline
->next
= expand_smacro(tline
->next
);
2497 tline
= tline
->next
;
2499 t
= tline
? tline
->next
: NULL
;
2501 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2502 /* The line contains only a quoted string */
2504 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2505 error(severity
, "%s", p
);
2507 /* Not a quoted string, or more than a quoted string */
2508 p
= detoken(tline
, false);
2509 error(severity
, "%s", p
);
2512 free_tlist(origline
);
2513 return DIRECTIVE_FOUND
;
2517 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2520 j
= if_condition(tline
->next
, i
);
2521 tline
->next
= NULL
; /* it got freed */
2522 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2524 cond
= nasm_malloc(sizeof(Cond
));
2525 cond
->next
= istk
->conds
;
2528 free_tlist(origline
);
2529 return DIRECTIVE_FOUND
;
2533 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2534 switch(istk
->conds
->state
) {
2536 istk
->conds
->state
= COND_DONE
;
2543 case COND_ELSE_TRUE
:
2544 case COND_ELSE_FALSE
:
2545 error_precond(ERR_WARNING
|ERR_PASS1
,
2546 "`%%elif' after `%%else' ignored");
2547 istk
->conds
->state
= COND_NEVER
;
2552 * IMPORTANT: In the case of %if, we will already have
2553 * called expand_mmac_params(); however, if we're
2554 * processing an %elif we must have been in a
2555 * non-emitting mode, which would have inhibited
2556 * the normal invocation of expand_mmac_params().
2557 * Therefore, we have to do it explicitly here.
2559 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2560 tline
->next
= NULL
; /* it got freed */
2561 istk
->conds
->state
=
2562 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2565 free_tlist(origline
);
2566 return DIRECTIVE_FOUND
;
2570 error_precond(ERR_WARNING
|ERR_PASS1
,
2571 "trailing garbage after `%%else' ignored");
2573 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2574 switch(istk
->conds
->state
) {
2577 istk
->conds
->state
= COND_ELSE_FALSE
;
2584 istk
->conds
->state
= COND_ELSE_TRUE
;
2587 case COND_ELSE_TRUE
:
2588 case COND_ELSE_FALSE
:
2589 error_precond(ERR_WARNING
|ERR_PASS1
,
2590 "`%%else' after `%%else' ignored.");
2591 istk
->conds
->state
= COND_NEVER
;
2594 free_tlist(origline
);
2595 return DIRECTIVE_FOUND
;
2599 error_precond(ERR_WARNING
|ERR_PASS1
,
2600 "trailing garbage after `%%endif' ignored");
2602 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2604 istk
->conds
= cond
->next
;
2606 free_tlist(origline
);
2607 return DIRECTIVE_FOUND
;
2614 error(ERR_FATAL
, "`%s': already defining a macro",
2616 return DIRECTIVE_FOUND
;
2618 defining
= nasm_malloc(sizeof(MMacro
));
2619 defining
->max_depth
=
2620 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2621 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2622 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2623 nasm_free(defining
);
2625 return DIRECTIVE_FOUND
;
2628 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2630 if (!strcmp(mmac
->name
, defining
->name
) &&
2631 (mmac
->nparam_min
<= defining
->nparam_max
2633 && (defining
->nparam_min
<= mmac
->nparam_max
2635 error(ERR_WARNING
|ERR_PASS1
,
2636 "redefining multi-line macro `%s'", defining
->name
);
2637 return DIRECTIVE_FOUND
;
2641 free_tlist(origline
);
2642 return DIRECTIVE_FOUND
;
2646 if (! (defining
&& defining
->name
)) {
2647 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2648 return DIRECTIVE_FOUND
;
2650 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2651 defining
->next
= *mmhead
;
2654 free_tlist(origline
);
2655 return DIRECTIVE_FOUND
;
2659 * We must search along istk->expansion until we hit a
2660 * macro-end marker for a macro with a name. Then we set
2661 * its `in_progress' flag to 0.
2663 for (l
= istk
->expansion
; l
; l
= l
->next
)
2664 if (l
->finishes
&& l
->finishes
->name
)
2668 l
->finishes
->in_progress
= 0;
2670 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2672 free_tlist(origline
);
2673 return DIRECTIVE_FOUND
;
2681 spec
.casesense
= (i
== PP_UNMACRO
);
2682 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2683 return DIRECTIVE_FOUND
;
2685 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2686 while (mmac_p
&& *mmac_p
) {
2688 if (mmac
->casesense
== spec
.casesense
&&
2689 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2690 mmac
->nparam_min
== spec
.nparam_min
&&
2691 mmac
->nparam_max
== spec
.nparam_max
&&
2692 mmac
->plus
== spec
.plus
) {
2693 *mmac_p
= mmac
->next
;
2696 mmac_p
= &mmac
->next
;
2699 free_tlist(origline
);
2700 free_tlist(spec
.dlist
);
2701 return DIRECTIVE_FOUND
;
2705 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2706 tline
= tline
->next
;
2708 free_tlist(origline
);
2709 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2710 return DIRECTIVE_FOUND
;
2712 t
= expand_smacro(tline
->next
);
2714 free_tlist(origline
);
2717 tokval
.t_type
= TOKEN_INVALID
;
2719 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2722 return DIRECTIVE_FOUND
;
2724 error(ERR_WARNING
|ERR_PASS1
,
2725 "trailing garbage after expression ignored");
2726 if (!is_simple(evalresult
)) {
2727 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2728 return DIRECTIVE_FOUND
;
2731 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2732 mmac
= mmac
->next_active
;
2734 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2735 } else if (mmac
->nparam
== 0) {
2737 "`%%rotate' invoked within macro without parameters");
2739 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2741 rotate
%= (int)mmac
->nparam
;
2743 rotate
+= mmac
->nparam
;
2745 mmac
->rotate
= rotate
;
2747 return DIRECTIVE_FOUND
;
2752 tline
= tline
->next
;
2753 } while (tok_type_(tline
, TOK_WHITESPACE
));
2755 if (tok_type_(tline
, TOK_ID
) &&
2756 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2759 tline
= tline
->next
;
2760 } while (tok_type_(tline
, TOK_WHITESPACE
));
2764 t
= expand_smacro(tline
);
2766 tokval
.t_type
= TOKEN_INVALID
;
2768 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2770 free_tlist(origline
);
2771 return DIRECTIVE_FOUND
;
2774 error(ERR_WARNING
|ERR_PASS1
,
2775 "trailing garbage after expression ignored");
2776 if (!is_simple(evalresult
)) {
2777 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2778 return DIRECTIVE_FOUND
;
2780 count
= reloc_value(evalresult
) + 1;
2782 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2785 free_tlist(origline
);
2787 tmp_defining
= defining
;
2788 defining
= nasm_malloc(sizeof(MMacro
));
2789 defining
->prev
= NULL
;
2790 defining
->name
= NULL
; /* flags this macro as a %rep block */
2791 defining
->casesense
= false;
2792 defining
->plus
= false;
2793 defining
->nolist
= nolist
;
2794 defining
->in_progress
= count
;
2795 defining
->max_depth
= 0;
2796 defining
->nparam_min
= defining
->nparam_max
= 0;
2797 defining
->defaults
= NULL
;
2798 defining
->dlist
= NULL
;
2799 defining
->expansion
= NULL
;
2800 defining
->next_active
= istk
->mstk
;
2801 defining
->rep_nest
= tmp_defining
;
2802 return DIRECTIVE_FOUND
;
2805 if (!defining
|| defining
->name
) {
2806 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2807 return DIRECTIVE_FOUND
;
2811 * Now we have a "macro" defined - although it has no name
2812 * and we won't be entering it in the hash tables - we must
2813 * push a macro-end marker for it on to istk->expansion.
2814 * After that, it will take care of propagating itself (a
2815 * macro-end marker line for a macro which is really a %rep
2816 * block will cause the macro to be re-expanded, complete
2817 * with another macro-end marker to ensure the process
2818 * continues) until the whole expansion is forcibly removed
2819 * from istk->expansion by a %exitrep.
2821 l
= nasm_malloc(sizeof(Line
));
2822 l
->next
= istk
->expansion
;
2823 l
->finishes
= defining
;
2825 istk
->expansion
= l
;
2827 istk
->mstk
= defining
;
2829 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2830 tmp_defining
= defining
;
2831 defining
= defining
->rep_nest
;
2832 free_tlist(origline
);
2833 return DIRECTIVE_FOUND
;
2837 * We must search along istk->expansion until we hit a
2838 * macro-end marker for a macro with no name. Then we set
2839 * its `in_progress' flag to 0.
2841 for (l
= istk
->expansion
; l
; l
= l
->next
)
2842 if (l
->finishes
&& !l
->finishes
->name
)
2846 l
->finishes
->in_progress
= 1;
2848 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2849 free_tlist(origline
);
2850 return DIRECTIVE_FOUND
;
2856 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2858 tline
= tline
->next
;
2860 tline
= expand_id(tline
);
2861 if (!tline
|| (tline
->type
!= TOK_ID
&&
2862 (tline
->type
!= TOK_PREPROC_ID
||
2863 tline
->text
[1] != '$'))) {
2864 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2866 free_tlist(origline
);
2867 return DIRECTIVE_FOUND
;
2870 ctx
= get_ctx(tline
->text
, &mname
, false);
2872 param_start
= tline
= tline
->next
;
2875 /* Expand the macro definition now for %xdefine and %ixdefine */
2876 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2877 tline
= expand_smacro(tline
);
2879 if (tok_is_(tline
, "(")) {
2881 * This macro has parameters.
2884 tline
= tline
->next
;
2888 error(ERR_NONFATAL
, "parameter identifier expected");
2889 free_tlist(origline
);
2890 return DIRECTIVE_FOUND
;
2892 if (tline
->type
!= TOK_ID
) {
2894 "`%s': parameter identifier expected",
2896 free_tlist(origline
);
2897 return DIRECTIVE_FOUND
;
2899 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2900 tline
= tline
->next
;
2902 if (tok_is_(tline
, ",")) {
2903 tline
= tline
->next
;
2905 if (!tok_is_(tline
, ")")) {
2907 "`)' expected to terminate macro template");
2908 free_tlist(origline
);
2909 return DIRECTIVE_FOUND
;
2915 tline
= tline
->next
;
2917 if (tok_type_(tline
, TOK_WHITESPACE
))
2918 last
= tline
, tline
= tline
->next
;
2923 if (t
->type
== TOK_ID
) {
2924 for (tt
= param_start
; tt
; tt
= tt
->next
)
2925 if (tt
->type
>= TOK_SMAC_PARAM
&&
2926 !strcmp(tt
->text
, t
->text
))
2930 t
->next
= macro_start
;
2935 * Good. We now have a macro name, a parameter count, and a
2936 * token list (in reverse order) for an expansion. We ought
2937 * to be OK just to create an SMacro, store it, and let
2938 * free_tlist have the rest of the line (which we have
2939 * carefully re-terminated after chopping off the expansion
2942 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2943 free_tlist(origline
);
2944 return DIRECTIVE_FOUND
;
2947 tline
= tline
->next
;
2949 tline
= expand_id(tline
);
2950 if (!tline
|| (tline
->type
!= TOK_ID
&&
2951 (tline
->type
!= TOK_PREPROC_ID
||
2952 tline
->text
[1] != '$'))) {
2953 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2954 free_tlist(origline
);
2955 return DIRECTIVE_FOUND
;
2958 error(ERR_WARNING
|ERR_PASS1
,
2959 "trailing garbage after macro name ignored");
2962 /* Find the context that symbol belongs to */
2963 ctx
= get_ctx(tline
->text
, &mname
, false);
2964 undef_smacro(ctx
, mname
);
2965 free_tlist(origline
);
2966 return DIRECTIVE_FOUND
;
2970 casesense
= (i
== PP_DEFSTR
);
2972 tline
= tline
->next
;
2974 tline
= expand_id(tline
);
2975 if (!tline
|| (tline
->type
!= TOK_ID
&&
2976 (tline
->type
!= TOK_PREPROC_ID
||
2977 tline
->text
[1] != '$'))) {
2978 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2980 free_tlist(origline
);
2981 return DIRECTIVE_FOUND
;
2984 ctx
= get_ctx(tline
->text
, &mname
, false);
2986 tline
= expand_smacro(tline
->next
);
2989 while (tok_type_(tline
, TOK_WHITESPACE
))
2990 tline
= delete_Token(tline
);
2992 p
= detoken(tline
, false);
2993 macro_start
= nasm_malloc(sizeof(*macro_start
));
2994 macro_start
->next
= NULL
;
2995 macro_start
->text
= nasm_quote(p
, strlen(p
));
2996 macro_start
->type
= TOK_STRING
;
2997 macro_start
->a
.mac
= NULL
;
3001 * We now have a macro name, an implicit parameter count of
3002 * zero, and a string token to use as an expansion. Create
3003 * and store an SMacro.
3005 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3006 free_tlist(origline
);
3007 return DIRECTIVE_FOUND
;
3011 casesense
= (i
== PP_DEFTOK
);
3013 tline
= tline
->next
;
3015 tline
= expand_id(tline
);
3016 if (!tline
|| (tline
->type
!= TOK_ID
&&
3017 (tline
->type
!= TOK_PREPROC_ID
||
3018 tline
->text
[1] != '$'))) {
3020 "`%s' expects a macro identifier as first parameter",
3022 free_tlist(origline
);
3023 return DIRECTIVE_FOUND
;
3025 ctx
= get_ctx(tline
->text
, &mname
, false);
3027 tline
= expand_smacro(tline
->next
);
3031 while (tok_type_(t
, TOK_WHITESPACE
))
3033 /* t should now point to the string */
3034 if (t
->type
!= TOK_STRING
) {
3036 "`%s` requires string as second parameter",
3039 free_tlist(origline
);
3040 return DIRECTIVE_FOUND
;
3043 nasm_unquote_cstr(t
->text
, i
);
3044 macro_start
= tokenize(t
->text
);
3047 * We now have a macro name, an implicit parameter count of
3048 * zero, and a numeric token to use as an expansion. Create
3049 * and store an SMacro.
3051 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3053 free_tlist(origline
);
3054 return DIRECTIVE_FOUND
;
3059 StrList
*xsl
= NULL
;
3060 StrList
**xst
= &xsl
;
3064 tline
= tline
->next
;
3066 tline
= expand_id(tline
);
3067 if (!tline
|| (tline
->type
!= TOK_ID
&&
3068 (tline
->type
!= TOK_PREPROC_ID
||
3069 tline
->text
[1] != '$'))) {
3071 "`%%pathsearch' expects a macro identifier as first parameter");
3072 free_tlist(origline
);
3073 return DIRECTIVE_FOUND
;
3075 ctx
= get_ctx(tline
->text
, &mname
, false);
3077 tline
= expand_smacro(tline
->next
);
3081 while (tok_type_(t
, TOK_WHITESPACE
))
3084 if (!t
|| (t
->type
!= TOK_STRING
&&
3085 t
->type
!= TOK_INTERNAL_STRING
)) {
3086 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3088 free_tlist(origline
);
3089 return DIRECTIVE_FOUND
; /* but we did _something_ */
3092 error(ERR_WARNING
|ERR_PASS1
,
3093 "trailing garbage after `%%pathsearch' ignored");
3095 if (t
->type
!= TOK_INTERNAL_STRING
)
3096 nasm_unquote(p
, NULL
);
3098 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3101 fclose(fp
); /* Don't actually care about the file */
3103 macro_start
= nasm_malloc(sizeof(*macro_start
));
3104 macro_start
->next
= NULL
;
3105 macro_start
->text
= nasm_quote(p
, strlen(p
));
3106 macro_start
->type
= TOK_STRING
;
3107 macro_start
->a
.mac
= NULL
;
3112 * We now have a macro name, an implicit parameter count of
3113 * zero, and a string token to use as an expansion. Create
3114 * and store an SMacro.
3116 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3118 free_tlist(origline
);
3119 return DIRECTIVE_FOUND
;
3125 tline
= tline
->next
;
3127 tline
= expand_id(tline
);
3128 if (!tline
|| (tline
->type
!= TOK_ID
&&
3129 (tline
->type
!= TOK_PREPROC_ID
||
3130 tline
->text
[1] != '$'))) {
3132 "`%%strlen' expects a macro identifier as first parameter");
3133 free_tlist(origline
);
3134 return DIRECTIVE_FOUND
;
3136 ctx
= get_ctx(tline
->text
, &mname
, false);
3138 tline
= expand_smacro(tline
->next
);
3142 while (tok_type_(t
, TOK_WHITESPACE
))
3144 /* t should now point to the string */
3145 if (t
->type
!= TOK_STRING
) {
3147 "`%%strlen` requires string as second parameter");
3149 free_tlist(origline
);
3150 return DIRECTIVE_FOUND
;
3153 macro_start
= nasm_malloc(sizeof(*macro_start
));
3154 macro_start
->next
= NULL
;
3155 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3156 macro_start
->a
.mac
= NULL
;
3159 * We now have a macro name, an implicit parameter count of
3160 * zero, and a numeric token to use as an expansion. Create
3161 * and store an SMacro.
3163 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3165 free_tlist(origline
);
3166 return DIRECTIVE_FOUND
;
3171 tline
= tline
->next
;
3173 tline
= expand_id(tline
);
3174 if (!tline
|| (tline
->type
!= TOK_ID
&&
3175 (tline
->type
!= TOK_PREPROC_ID
||
3176 tline
->text
[1] != '$'))) {
3178 "`%%strcat' expects a macro identifier as first parameter");
3179 free_tlist(origline
);
3180 return DIRECTIVE_FOUND
;
3182 ctx
= get_ctx(tline
->text
, &mname
, false);
3184 tline
= expand_smacro(tline
->next
);
3188 for (t
= tline
; t
; t
= t
->next
) {
3190 case TOK_WHITESPACE
:
3193 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3196 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3198 /* else fall through */
3201 "non-string passed to `%%strcat' (%d)", t
->type
);
3203 free_tlist(origline
);
3204 return DIRECTIVE_FOUND
;
3208 p
= pp
= nasm_malloc(len
);
3209 for (t
= tline
; t
; t
= t
->next
) {
3210 if (t
->type
== TOK_STRING
) {
3211 memcpy(p
, t
->text
, t
->a
.len
);
3217 * We now have a macro name, an implicit parameter count of
3218 * zero, and a numeric token to use as an expansion. Create
3219 * and store an SMacro.
3221 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3222 macro_start
->text
= nasm_quote(pp
, len
);
3224 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3226 free_tlist(origline
);
3227 return DIRECTIVE_FOUND
;
3236 tline
= tline
->next
;
3238 tline
= expand_id(tline
);
3239 if (!tline
|| (tline
->type
!= TOK_ID
&&
3240 (tline
->type
!= TOK_PREPROC_ID
||
3241 tline
->text
[1] != '$'))) {
3243 "`%%substr' expects a macro identifier as first parameter");
3244 free_tlist(origline
);
3245 return DIRECTIVE_FOUND
;
3247 ctx
= get_ctx(tline
->text
, &mname
, false);
3249 tline
= expand_smacro(tline
->next
);
3253 while (tok_type_(t
, TOK_WHITESPACE
))
3256 /* t should now point to the string */
3257 if (t
->type
!= TOK_STRING
) {
3259 "`%%substr` requires string as second parameter");
3261 free_tlist(origline
);
3262 return DIRECTIVE_FOUND
;
3267 tokval
.t_type
= TOKEN_INVALID
;
3268 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3272 free_tlist(origline
);
3273 return DIRECTIVE_FOUND
;
3274 } else if (!is_simple(evalresult
)) {
3275 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3277 free_tlist(origline
);
3278 return DIRECTIVE_FOUND
;
3280 a1
= evalresult
->value
-1;
3282 while (tok_type_(tt
, TOK_WHITESPACE
))
3285 a2
= 1; /* Backwards compatibility: one character */
3287 tokval
.t_type
= TOKEN_INVALID
;
3288 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3292 free_tlist(origline
);
3293 return DIRECTIVE_FOUND
;
3294 } else if (!is_simple(evalresult
)) {
3295 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3297 free_tlist(origline
);
3298 return DIRECTIVE_FOUND
;
3300 a2
= evalresult
->value
;
3303 len
= nasm_unquote(t
->text
, NULL
);
3306 if (a1
+a2
> (int64_t)len
)
3309 macro_start
= nasm_malloc(sizeof(*macro_start
));
3310 macro_start
->next
= NULL
;
3311 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3312 macro_start
->type
= TOK_STRING
;
3313 macro_start
->a
.mac
= NULL
;
3316 * We now have a macro name, an implicit parameter count of
3317 * zero, and a numeric token to use as an expansion. Create
3318 * and store an SMacro.
3320 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3322 free_tlist(origline
);
3323 return DIRECTIVE_FOUND
;
3328 casesense
= (i
== PP_ASSIGN
);
3330 tline
= tline
->next
;
3332 tline
= expand_id(tline
);
3333 if (!tline
|| (tline
->type
!= TOK_ID
&&
3334 (tline
->type
!= TOK_PREPROC_ID
||
3335 tline
->text
[1] != '$'))) {
3337 "`%%%sassign' expects a macro identifier",
3338 (i
== PP_IASSIGN
? "i" : ""));
3339 free_tlist(origline
);
3340 return DIRECTIVE_FOUND
;
3342 ctx
= get_ctx(tline
->text
, &mname
, false);
3344 tline
= expand_smacro(tline
->next
);
3349 tokval
.t_type
= TOKEN_INVALID
;
3351 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3354 free_tlist(origline
);
3355 return DIRECTIVE_FOUND
;
3359 error(ERR_WARNING
|ERR_PASS1
,
3360 "trailing garbage after expression ignored");
3362 if (!is_simple(evalresult
)) {
3364 "non-constant value given to `%%%sassign'",
3365 (i
== PP_IASSIGN
? "i" : ""));
3366 free_tlist(origline
);
3367 return DIRECTIVE_FOUND
;
3370 macro_start
= nasm_malloc(sizeof(*macro_start
));
3371 macro_start
->next
= NULL
;
3372 make_tok_num(macro_start
, reloc_value(evalresult
));
3373 macro_start
->a
.mac
= NULL
;
3376 * We now have a macro name, an implicit parameter count of
3377 * zero, and a numeric token to use as an expansion. Create
3378 * and store an SMacro.
3380 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3381 free_tlist(origline
);
3382 return DIRECTIVE_FOUND
;
3386 * Syntax is `%line nnn[+mmm] [filename]'
3388 tline
= tline
->next
;
3390 if (!tok_type_(tline
, TOK_NUMBER
)) {
3391 error(ERR_NONFATAL
, "`%%line' expects line number");
3392 free_tlist(origline
);
3393 return DIRECTIVE_FOUND
;
3395 k
= readnum(tline
->text
, &err
);
3397 tline
= tline
->next
;
3398 if (tok_is_(tline
, "+")) {
3399 tline
= tline
->next
;
3400 if (!tok_type_(tline
, TOK_NUMBER
)) {
3401 error(ERR_NONFATAL
, "`%%line' expects line increment");
3402 free_tlist(origline
);
3403 return DIRECTIVE_FOUND
;
3405 m
= readnum(tline
->text
, &err
);
3406 tline
= tline
->next
;
3412 nasm_free(src_set_fname(detoken(tline
, false)));
3414 free_tlist(origline
);
3415 return DIRECTIVE_FOUND
;
3419 "preprocessor directive `%s' not yet implemented",
3421 return DIRECTIVE_FOUND
;
3426 * Ensure that a macro parameter contains a condition code and
3427 * nothing else. Return the condition code index if so, or -1
3430 static int find_cc(Token
* t
)
3436 return -1; /* Probably a %+ without a space */
3439 if (t
->type
!= TOK_ID
)
3443 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3447 j
= elements(conditions
);
3450 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3465 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3467 Token
**tail
, *t
, *tt
;
3469 bool did_paste
= false;
3472 /* Now handle token pasting... */
3475 while ((t
= *tail
) && (tt
= t
->next
)) {
3477 case TOK_WHITESPACE
:
3478 if (tt
->type
== TOK_WHITESPACE
) {
3479 /* Zap adjacent whitespace tokens */
3480 t
->next
= delete_Token(tt
);
3482 /* Do not advance paste_head here */
3487 case TOK_PREPROC_ID
:
3494 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3495 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3496 tt
->type
== TOK_OTHER
)) {
3497 len
+= strlen(tt
->text
);
3501 /* Now tt points to the first token after the potential
3503 if (tt
!= t
->next
) {
3504 /* We have at least two tokens... */
3505 len
+= strlen(t
->text
);
3506 p
= tmp
= nasm_malloc(len
+1);
3510 p
= strchr(p
, '\0');
3511 t
= delete_Token(t
);
3514 t
= *tail
= tokenize(tmp
);
3521 t
->next
= tt
; /* Attach the remaining token chain */
3529 case TOK_PASTE
: /* %+ */
3530 if (handle_paste_tokens
) {
3531 /* Zap %+ and whitespace tokens to the right */
3532 while (t
&& (t
->type
== TOK_WHITESPACE
||
3533 t
->type
== TOK_PASTE
))
3534 t
= *tail
= delete_Token(t
);
3535 if (!paste_head
|| !t
)
3536 break; /* Nothing to paste with */
3540 while (tok_type_(tt
, TOK_WHITESPACE
))
3541 tt
= t
->next
= delete_Token(tt
);
3544 tmp
= nasm_strcat(t
->text
, tt
->text
);
3546 tt
= delete_Token(tt
);
3547 t
= *tail
= tokenize(tmp
);
3553 t
->next
= tt
; /* Attach the remaining token chain */
3560 /* else fall through */
3562 tail
= paste_head
= &t
->next
;
3569 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3570 * %-n) and MMacro-local identifiers (%%foo) as well as
3571 * macro indirection (%[...]).
3573 static Token
*expand_mmac_params(Token
* tline
)
3575 Token
*t
, *tt
, **tail
, *thead
;
3576 bool changed
= false;
3582 if (tline
->type
== TOK_PREPROC_ID
&&
3583 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3584 && tline
->text
[2]) || tline
->text
[1] == '%'
3585 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3587 int type
= 0, cc
; /* type = 0 to placate optimisers */
3594 tline
= tline
->next
;
3597 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3598 mac
= mac
->next_active
;
3600 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3602 switch (t
->text
[1]) {
3604 * We have to make a substitution of one of the
3605 * forms %1, %-1, %+1, %%foo, %0.
3609 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3610 text
= nasm_strdup(tmpbuf
);
3614 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3616 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3619 n
= atoi(t
->text
+ 2) - 1;
3620 if (n
>= mac
->nparam
)
3623 if (mac
->nparam
> 1)
3624 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3625 tt
= mac
->params
[n
];
3630 "macro parameter %d is not a condition code",
3635 if (inverse_ccs
[cc
] == -1) {
3637 "condition code `%s' is not invertible",
3641 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3645 n
= atoi(t
->text
+ 2) - 1;
3646 if (n
>= mac
->nparam
)
3649 if (mac
->nparam
> 1)
3650 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3651 tt
= mac
->params
[n
];
3656 "macro parameter %d is not a condition code",
3661 text
= nasm_strdup(conditions
[cc
]);
3665 n
= atoi(t
->text
+ 1) - 1;
3666 if (n
>= mac
->nparam
)
3669 if (mac
->nparam
> 1)
3670 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3671 tt
= mac
->params
[n
];
3674 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3675 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3676 tail
= &(*tail
)->next
;
3680 text
= NULL
; /* we've done it here */
3695 } else if (tline
->type
== TOK_INDIRECT
) {
3697 tline
= tline
->next
;
3698 tt
= tokenize(t
->text
);
3699 tt
= expand_mmac_params(tt
);
3700 tt
= expand_smacro(tt
);
3703 tt
->a
.mac
= NULL
; /* Necessary? */
3711 tline
= tline
->next
;
3719 paste_tokens(&thead
, false);
3725 * Expand all single-line macro calls made in the given line.
3726 * Return the expanded version of the line. The original is deemed
3727 * to be destroyed in the process. (In reality we'll just move
3728 * Tokens from input to output a lot of the time, rather than
3729 * actually bothering to destroy and replicate.)
3732 static Token
*expand_smacro(Token
* tline
)
3734 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3735 struct hash_table
*smtbl
;
3736 SMacro
*head
= NULL
, *m
;
3739 unsigned int nparam
, sparam
;
3741 Token
*org_tline
= tline
;
3744 int deadman
= DEADMAN_LIMIT
;
3748 * Trick: we should avoid changing the start token pointer since it can
3749 * be contained in "next" field of other token. Because of this
3750 * we allocate a copy of first token and work with it; at the end of
3751 * routine we copy it back
3755 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3757 tline
->a
.mac
= org_tline
->a
.mac
;
3758 nasm_free(org_tline
->text
);
3759 org_tline
->text
= NULL
;
3762 expanded
= true; /* Always expand %+ at least once */
3768 while (tline
) { /* main token loop */
3770 error(ERR_NONFATAL
, "interminable macro recursion");
3774 if ((mname
= tline
->text
)) {
3775 /* if this token is a local macro, look in local context */
3776 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3777 ctx
= get_ctx(mname
, &mname
, true);
3780 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3781 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3784 * We've hit an identifier. As in is_mmacro below, we first
3785 * check whether the identifier is a single-line macro at
3786 * all, then think about checking for parameters if
3789 for (m
= head
; m
; m
= m
->next
)
3790 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3796 if (m
->nparam
== 0) {
3798 * Simple case: the macro is parameterless. Discard the
3799 * one token that the macro call took, and push the
3800 * expansion back on the to-do stack.
3802 if (!m
->expansion
) {
3803 if (!strcmp("__FILE__", m
->name
)) {
3806 src_get(&num
, &file
);
3807 tline
->text
= nasm_quote(file
, strlen(file
));
3808 tline
->type
= TOK_STRING
;
3812 if (!strcmp("__LINE__", m
->name
)) {
3813 nasm_free(tline
->text
);
3814 make_tok_num(tline
, src_get_linnum());
3817 if (!strcmp("__BITS__", m
->name
)) {
3818 nasm_free(tline
->text
);
3819 make_tok_num(tline
, globalbits
);
3822 tline
= delete_Token(tline
);
3827 * Complicated case: at least one macro with this name
3828 * exists and takes parameters. We must find the
3829 * parameters in the call, count them, find the SMacro
3830 * that corresponds to that form of the macro call, and
3831 * substitute for the parameters when we expand. What a
3834 /*tline = tline->next;
3835 skip_white_(tline); */
3838 while (tok_type_(t
, TOK_SMAC_END
)) {
3839 t
->a
.mac
->in_progress
= false;
3841 t
= tline
->next
= delete_Token(t
);
3844 } while (tok_type_(tline
, TOK_WHITESPACE
));
3845 if (!tok_is_(tline
, "(")) {
3847 * This macro wasn't called with parameters: ignore
3848 * the call. (Behaviour borrowed from gnu cpp.)
3857 sparam
= PARAM_DELTA
;
3858 params
= nasm_malloc(sparam
* sizeof(Token
*));
3859 params
[0] = tline
->next
;
3860 paramsize
= nasm_malloc(sparam
* sizeof(int));
3862 while (true) { /* parameter loop */
3864 * For some unusual expansions
3865 * which concatenates function call
3868 while (tok_type_(t
, TOK_SMAC_END
)) {
3869 t
->a
.mac
->in_progress
= false;
3871 t
= tline
->next
= delete_Token(t
);
3877 "macro call expects terminating `)'");
3880 if (tline
->type
== TOK_WHITESPACE
3882 if (paramsize
[nparam
])
3885 params
[nparam
] = tline
->next
;
3886 continue; /* parameter loop */
3888 if (tline
->type
== TOK_OTHER
3889 && tline
->text
[1] == 0) {
3890 char ch
= tline
->text
[0];
3891 if (ch
== ',' && !paren
&& brackets
<= 0) {
3892 if (++nparam
>= sparam
) {
3893 sparam
+= PARAM_DELTA
;
3894 params
= nasm_realloc(params
,
3899 nasm_realloc(paramsize
,
3903 params
[nparam
] = tline
->next
;
3904 paramsize
[nparam
] = 0;
3906 continue; /* parameter loop */
3909 (brackets
> 0 || (brackets
== 0 &&
3910 !paramsize
[nparam
])))
3912 if (!(brackets
++)) {
3913 params
[nparam
] = tline
->next
;
3914 continue; /* parameter loop */
3917 if (ch
== '}' && brackets
> 0)
3918 if (--brackets
== 0) {
3920 continue; /* parameter loop */
3922 if (ch
== '(' && !brackets
)
3924 if (ch
== ')' && brackets
<= 0)
3930 error(ERR_NONFATAL
, "braces do not "
3931 "enclose all of macro parameter");
3933 paramsize
[nparam
] += white
+ 1;
3935 } /* parameter loop */
3937 while (m
&& (m
->nparam
!= nparam
||
3938 mstrcmp(m
->name
, mname
,
3942 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3943 "macro `%s' exists, "
3944 "but not taking %d parameters",
3945 mstart
->text
, nparam
);
3948 if (m
&& m
->in_progress
)
3950 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3952 * Design question: should we handle !tline, which
3953 * indicates missing ')' here, or expand those
3954 * macros anyway, which requires the (t) test a few
3958 nasm_free(paramsize
);
3962 * Expand the macro: we are placed on the last token of the
3963 * call, so that we can easily split the call from the
3964 * following tokens. We also start by pushing an SMAC_END
3965 * token for the cycle removal.
3972 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3974 m
->in_progress
= true;
3976 for (t
= m
->expansion
; t
; t
= t
->next
) {
3977 if (t
->type
>= TOK_SMAC_PARAM
) {
3978 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3982 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3983 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3986 new_Token(tline
, ttt
->type
, ttt
->text
,
3992 } else if (t
->type
== TOK_PREPROC_Q
) {
3993 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3995 } else if (t
->type
== TOK_PREPROC_QQ
) {
3996 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3999 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4005 * Having done that, get rid of the macro call, and clean
4006 * up the parameters.
4009 nasm_free(paramsize
);
4012 continue; /* main token loop */
4017 if (tline
->type
== TOK_SMAC_END
) {
4018 tline
->a
.mac
->in_progress
= false;
4019 tline
= delete_Token(tline
);
4022 tline
= tline
->next
;
4030 * Now scan the entire line and look for successive TOK_IDs that resulted
4031 * after expansion (they can't be produced by tokenize()). The successive
4032 * TOK_IDs should be concatenated.
4033 * Also we look for %+ tokens and concatenate the tokens before and after
4034 * them (without white spaces in between).
4036 if (expanded
&& paste_tokens(&thead
, true)) {
4038 * If we concatenated something, *and* we had previously expanded
4039 * an actual macro, scan the lines again for macros...
4048 *org_tline
= *thead
;
4049 /* since we just gave text to org_line, don't free it */
4051 delete_Token(thead
);
4053 /* the expression expanded to empty line;
4054 we can't return NULL for some reasons
4055 we just set the line to a single WHITESPACE token. */
4056 memset(org_tline
, 0, sizeof(*org_tline
));
4057 org_tline
->text
= NULL
;
4058 org_tline
->type
= TOK_WHITESPACE
;
4067 * Similar to expand_smacro but used exclusively with macro identifiers
4068 * right before they are fetched in. The reason is that there can be
4069 * identifiers consisting of several subparts. We consider that if there
4070 * are more than one element forming the name, user wants a expansion,
4071 * otherwise it will be left as-is. Example:
4075 * the identifier %$abc will be left as-is so that the handler for %define
4076 * will suck it and define the corresponding value. Other case:
4078 * %define _%$abc cde
4080 * In this case user wants name to be expanded *before* %define starts
4081 * working, so we'll expand %$abc into something (if it has a value;
4082 * otherwise it will be left as-is) then concatenate all successive
4085 static Token
*expand_id(Token
* tline
)
4087 Token
*cur
, *oldnext
= NULL
;
4089 if (!tline
|| !tline
->next
)
4094 (cur
->next
->type
== TOK_ID
||
4095 cur
->next
->type
== TOK_PREPROC_ID
4096 || cur
->next
->type
== TOK_NUMBER
))
4099 /* If identifier consists of just one token, don't expand */
4104 oldnext
= cur
->next
; /* Detach the tail past identifier */
4105 cur
->next
= NULL
; /* so that expand_smacro stops here */
4108 tline
= expand_smacro(tline
);
4111 /* expand_smacro possibly changhed tline; re-scan for EOL */
4113 while (cur
&& cur
->next
)
4116 cur
->next
= oldnext
;
4123 * Determine whether the given line constitutes a multi-line macro
4124 * call, and return the MMacro structure called if so. Doesn't have
4125 * to check for an initial label - that's taken care of in
4126 * expand_mmacro - but must check numbers of parameters. Guaranteed
4127 * to be called with tline->type == TOK_ID, so the putative macro
4128 * name is easy to find.
4130 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4136 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4139 * Efficiency: first we see if any macro exists with the given
4140 * name. If not, we can return NULL immediately. _Then_ we
4141 * count the parameters, and then we look further along the
4142 * list if necessary to find the proper MMacro.
4144 for (m
= head
; m
; m
= m
->next
)
4145 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4151 * OK, we have a potential macro. Count and demarcate the
4154 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4157 * So we know how many parameters we've got. Find the MMacro
4158 * structure that handles this number.
4161 if (m
->nparam_min
<= nparam
4162 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4164 * This one is right. Just check if cycle removal
4165 * prohibits us using it before we actually celebrate...
4167 if (m
->in_progress
> m
->max_depth
) {
4168 if (m
->max_depth
> 0) {
4170 "reached maximum recursion depth of %i",
4177 * It's right, and we can use it. Add its default
4178 * parameters to the end of our list if necessary.
4180 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4182 nasm_realloc(params
,
4183 ((m
->nparam_min
+ m
->ndefs
+
4184 1) * sizeof(*params
)));
4185 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4186 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4191 * If we've gone over the maximum parameter count (and
4192 * we're in Plus mode), ignore parameters beyond
4195 if (m
->plus
&& nparam
> m
->nparam_max
)
4196 nparam
= m
->nparam_max
;
4198 * Then terminate the parameter list, and leave.
4200 if (!params
) { /* need this special case */
4201 params
= nasm_malloc(sizeof(*params
));
4204 params
[nparam
] = NULL
;
4205 *params_array
= params
;
4209 * This one wasn't right: look for the next one with the
4212 for (m
= m
->next
; m
; m
= m
->next
)
4213 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4218 * After all that, we didn't find one with the right number of
4219 * parameters. Issue a warning, and fail to expand the macro.
4221 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4222 "macro `%s' exists, but not taking %d parameters",
4223 tline
->text
, nparam
);
4230 * Save MMacro invocation specific fields in
4231 * preparation for a recursive macro expansion
4233 static void push_mmacro(MMacro
*m
)
4235 MMacroInvocation
*i
;
4237 i
= nasm_malloc(sizeof(MMacroInvocation
));
4239 i
->params
= m
->params
;
4240 i
->iline
= m
->iline
;
4241 i
->nparam
= m
->nparam
;
4242 i
->rotate
= m
->rotate
;
4243 i
->paramlen
= m
->paramlen
;
4244 i
->unique
= m
->unique
;
4250 * Restore MMacro invocation specific fields that were
4251 * saved during a previous recursive macro expansion
4253 static void pop_mmacro(MMacro
*m
)
4255 MMacroInvocation
*i
;
4260 m
->params
= i
->params
;
4261 m
->iline
= i
->iline
;
4262 m
->nparam
= i
->nparam
;
4263 m
->rotate
= i
->rotate
;
4264 m
->paramlen
= i
->paramlen
;
4265 m
->unique
= i
->unique
;
4272 * Expand the multi-line macro call made by the given line, if
4273 * there is one to be expanded. If there is, push the expansion on
4274 * istk->expansion and return 1. Otherwise return 0.
4276 static int expand_mmacro(Token
* tline
)
4278 Token
*startline
= tline
;
4279 Token
*label
= NULL
;
4280 int dont_prepend
= 0;
4281 Token
**params
, *t
, *mtok
, *tt
;
4284 int i
, nparam
, *paramlen
;
4289 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4290 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4293 m
= is_mmacro(t
, ¶ms
);
4299 * We have an id which isn't a macro call. We'll assume
4300 * it might be a label; we'll also check to see if a
4301 * colon follows it. Then, if there's another id after
4302 * that lot, we'll check it again for macro-hood.
4306 if (tok_type_(t
, TOK_WHITESPACE
))
4307 last
= t
, t
= t
->next
;
4308 if (tok_is_(t
, ":")) {
4310 last
= t
, t
= t
->next
;
4311 if (tok_type_(t
, TOK_WHITESPACE
))
4312 last
= t
, t
= t
->next
;
4314 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4322 * Fix up the parameters: this involves stripping leading and
4323 * trailing whitespace, then stripping braces if they are
4326 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4327 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4329 for (i
= 0; params
[i
]; i
++) {
4331 int comma
= (!m
->plus
|| i
< nparam
- 1);
4335 if (tok_is_(t
, "{"))
4336 t
= t
->next
, brace
= true, comma
= false;
4340 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4341 break; /* ... because we have hit a comma */
4342 if (comma
&& t
->type
== TOK_WHITESPACE
4343 && tok_is_(t
->next
, ","))
4344 break; /* ... or a space then a comma */
4345 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4346 break; /* ... or a brace */
4353 * OK, we have a MMacro structure together with a set of
4354 * parameters. We must now go through the expansion and push
4355 * copies of each Line on to istk->expansion. Substitution of
4356 * parameter tokens and macro-local tokens doesn't get done
4357 * until the single-line macro substitution process; this is
4358 * because delaying them allows us to change the semantics
4359 * later through %rotate.
4361 * First, push an end marker on to istk->expansion, mark this
4362 * macro as in progress, and set up its invocation-specific
4365 ll
= nasm_malloc(sizeof(Line
));
4366 ll
->next
= istk
->expansion
;
4369 istk
->expansion
= ll
;
4372 * Save the previous MMacro expansion in the case of
4375 if (m
->max_depth
&& m
->in_progress
)
4383 m
->paramlen
= paramlen
;
4384 m
->unique
= unique
++;
4387 m
->next_active
= istk
->mstk
;
4390 for (l
= m
->expansion
; l
; l
= l
->next
) {
4393 ll
= nasm_malloc(sizeof(Line
));
4394 ll
->finishes
= NULL
;
4395 ll
->next
= istk
->expansion
;
4396 istk
->expansion
= ll
;
4399 for (t
= l
->first
; t
; t
= t
->next
) {
4403 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4405 case TOK_PREPROC_QQ
:
4406 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4408 case TOK_PREPROC_ID
:
4409 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4417 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4426 * If we had a label, push it on as the first line of
4427 * the macro expansion.
4430 if (dont_prepend
< 0)
4431 free_tlist(startline
);
4433 ll
= nasm_malloc(sizeof(Line
));
4434 ll
->finishes
= NULL
;
4435 ll
->next
= istk
->expansion
;
4436 istk
->expansion
= ll
;
4437 ll
->first
= startline
;
4438 if (!dont_prepend
) {
4440 label
= label
->next
;
4441 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4446 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4451 /* The function that actually does the error reporting */
4452 static void verror(int severity
, const char *fmt
, va_list arg
)
4456 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4458 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4459 nasm_error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4460 istk
->mstk
->lineno
, buff
);
4462 nasm_error(severity
, "%s", buff
);
4466 * Since preprocessor always operate only on the line that didn't
4467 * arrived yet, we should always use ERR_OFFBY1.
4469 static void error(int severity
, const char *fmt
, ...)
4473 /* If we're in a dead branch of IF or something like it, ignore the error */
4474 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4478 verror(severity
, fmt
, arg
);
4483 * Because %else etc are evaluated in the state context
4484 * of the previous branch, errors might get lost with error():
4485 * %if 0 ... %else trailing garbage ... %endif
4486 * So %else etc should report errors with this function.
4488 static void error_precond(int severity
, const char *fmt
, ...)
4492 /* Only ignore the error if it's really in a dead branch */
4493 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4497 verror(severity
, fmt
, arg
);
4502 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4507 istk
= nasm_malloc(sizeof(Include
));
4510 istk
->expansion
= NULL
;
4512 istk
->fp
= fopen(file
, "r");
4514 src_set_fname(nasm_strdup(file
));
4518 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4521 nested_mac_count
= 0;
4522 nested_rep_count
= 0;
4525 if (tasm_compatible_mode
) {
4526 stdmacpos
= nasm_stdmac
;
4528 stdmacpos
= nasm_stdmac_after_tasm
;
4530 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4535 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4536 * The caller, however, will also pass in 3 for preprocess-only so
4537 * we can set __PASS__ accordingly.
4539 pass
= apass
> 2 ? 2 : apass
;
4541 dephead
= deptail
= deplist
;
4543 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4545 strcpy(sl
->str
, file
);
4547 deptail
= &sl
->next
;
4551 * Define the __PASS__ macro. This is defined here unlike
4552 * all the other builtins, because it is special -- it varies between
4555 t
= nasm_malloc(sizeof(*t
));
4557 make_tok_num(t
, apass
);
4559 define_smacro(NULL
, "__PASS__", true, 0, t
);
4562 static char *pp_getline(void)
4569 * Fetch a tokenized line, either from the macro-expansion
4570 * buffer or from the input file.
4573 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4574 Line
*l
= istk
->expansion
;
4575 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4579 * This is a macro-end marker for a macro with no
4580 * name, which means it's not really a macro at all
4581 * but a %rep block, and the `in_progress' field is
4582 * more than 1, meaning that we still need to
4583 * repeat. (1 means the natural last repetition; 0
4584 * means termination by %exitrep.) We have
4585 * therefore expanded up to the %endrep, and must
4586 * push the whole block on to the expansion buffer
4587 * again. We don't bother to remove the macro-end
4588 * marker: we'd only have to generate another one
4591 l
->finishes
->in_progress
--;
4592 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4593 Token
*t
, *tt
, **tail
;
4595 ll
= nasm_malloc(sizeof(Line
));
4596 ll
->next
= istk
->expansion
;
4597 ll
->finishes
= NULL
;
4601 for (t
= l
->first
; t
; t
= t
->next
) {
4602 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4604 new_Token(NULL
, t
->type
, t
->text
, 0);
4609 istk
->expansion
= ll
;
4613 * Check whether a `%rep' was started and not ended
4614 * within this macro expansion. This can happen and
4615 * should be detected. It's a fatal error because
4616 * I'm too confused to work out how to recover
4622 "defining with name in expansion");
4623 else if (istk
->mstk
->name
)
4625 "`%%rep' without `%%endrep' within"
4626 " expansion of macro `%s'",
4631 * FIXME: investigate the relationship at this point between
4632 * istk->mstk and l->finishes
4635 MMacro
*m
= istk
->mstk
;
4636 istk
->mstk
= m
->next_active
;
4639 * This was a real macro call, not a %rep, and
4640 * therefore the parameter information needs to
4645 l
->finishes
->in_progress
--;
4647 nasm_free(m
->params
);
4648 free_tlist(m
->iline
);
4649 nasm_free(m
->paramlen
);
4650 l
->finishes
->in_progress
= 0;
4655 istk
->expansion
= l
->next
;
4657 list
->downlevel(LIST_MACRO
);
4660 while (1) { /* until we get a line we can use */
4662 if (istk
->expansion
) { /* from a macro expansion */
4664 Line
*l
= istk
->expansion
;
4666 istk
->mstk
->lineno
++;
4668 istk
->expansion
= l
->next
;
4670 p
= detoken(tline
, false);
4671 list
->line(LIST_MACRO
, p
);
4676 if (line
) { /* from the current input file */
4677 line
= prepreproc(line
);
4678 tline
= tokenize(line
);
4683 * The current file has ended; work down the istk
4690 "expected `%%endif' before end of file");
4691 /* only set line and file name if there's a next node */
4693 src_set_linnum(i
->lineno
);
4694 nasm_free(src_set_fname(i
->fname
));
4697 list
->downlevel(LIST_INCLUDE
);
4701 if (istk
->expansion
&& istk
->expansion
->finishes
)
4707 * We must expand MMacro parameters and MMacro-local labels
4708 * _before_ we plunge into directive processing, to cope
4709 * with things like `%define something %1' such as STRUC
4710 * uses. Unless we're _defining_ a MMacro, in which case
4711 * those tokens should be left alone to go into the
4712 * definition; and unless we're in a non-emitting
4713 * condition, in which case we don't want to meddle with
4716 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4717 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4718 tline
= expand_mmac_params(tline
);
4722 * Check the line to see if it's a preprocessor directive.
4724 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4726 } else if (defining
) {
4728 * We're defining a multi-line macro. We emit nothing
4730 * shove the tokenized line on to the macro definition.
4732 Line
*l
= nasm_malloc(sizeof(Line
));
4733 l
->next
= defining
->expansion
;
4736 defining
->expansion
= l
;
4738 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4740 * We're in a non-emitting branch of a condition block.
4741 * Emit nothing at all, not even a blank line: when we
4742 * emerge from the condition we'll give a line-number
4743 * directive so we keep our place correctly.
4747 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4749 * We're in a %rep block which has been terminated, so
4750 * we're walking through to the %endrep without
4751 * emitting anything. Emit nothing at all, not even a
4752 * blank line: when we emerge from the %rep block we'll
4753 * give a line-number directive so we keep our place
4759 tline
= expand_smacro(tline
);
4760 if (!expand_mmacro(tline
)) {
4762 * De-tokenize the line again, and emit it.
4764 line
= detoken(tline
, true);
4768 continue; /* expand_mmacro calls free_tlist */
4776 static void pp_cleanup(int pass
)
4779 if (defining
->name
) {
4781 "end of file while still defining macro `%s'",
4784 error(ERR_NONFATAL
, "end of file while still in %%rep");
4787 free_mmacro(defining
);
4796 nasm_free(i
->fname
);
4801 nasm_free(src_set_fname(NULL
));
4806 while ((i
= ipath
)) {
4815 void pp_include_path(char *path
)
4819 i
= nasm_malloc(sizeof(IncPath
));
4820 i
->path
= path
? nasm_strdup(path
) : NULL
;
4833 void pp_pre_include(char *fname
)
4835 Token
*inc
, *space
, *name
;
4838 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4839 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4840 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4842 l
= nasm_malloc(sizeof(Line
));
4849 void pp_pre_define(char *definition
)
4855 equals
= strchr(definition
, '=');
4856 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4857 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4860 space
->next
= tokenize(definition
);
4864 l
= nasm_malloc(sizeof(Line
));
4871 void pp_pre_undefine(char *definition
)
4876 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4877 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4878 space
->next
= tokenize(definition
);
4880 l
= nasm_malloc(sizeof(Line
));
4888 * Added by Keith Kanios:
4890 * This function is used to assist with "runtime" preprocessor
4891 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4893 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4894 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4897 void pp_runtime(char *definition
)
4901 def
= tokenize(definition
);
4902 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
4907 void pp_extra_stdmac(macros_t
*macros
)
4909 extrastdmac
= macros
;
4912 static void make_tok_num(Token
* tok
, int64_t val
)
4915 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4916 tok
->text
= nasm_strdup(numbuf
);
4917 tok
->type
= TOK_NUMBER
;