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
83 typedef struct SMacro SMacro
;
84 typedef struct MMacro MMacro
;
85 typedef struct MMacroInvocation MMacroInvocation
;
86 typedef struct Context Context
;
87 typedef struct Token Token
;
88 typedef struct Blocks Blocks
;
89 typedef struct Line Line
;
90 typedef struct Include Include
;
91 typedef struct Cond Cond
;
92 typedef struct IncPath IncPath
;
95 * Note on the storage of both SMacro and MMacros: the hash table
96 * indexes them case-insensitively, and we then have to go through a
97 * linked list of potential case aliases (and, for MMacros, parameter
98 * ranges); this is to preserve the matching semantics of the earlier
99 * code. If the number of case aliases for a specific macro is a
100 * performance issue, you may want to reconsider your coding style.
104 * Store the definition of a single-line macro.
116 * Store the definition of a multi-line macro. This is also used to
117 * store the interiors of `%rep...%endrep' blocks, which are
118 * effectively self-re-invoking multi-line macros which simply
119 * don't have a name or bother to appear in the hash tables. %rep
120 * blocks are signified by having a NULL `name' field.
122 * In a MMacro describing a `%rep' block, the `in_progress' field
123 * isn't merely boolean, but gives the number of repeats left to
126 * The `next' field is used for storing MMacros in hash tables; the
127 * `next_active' field is for stacking them on istk entries.
129 * When a MMacro is being expanded, `params', `iline', `nparam',
130 * `paramlen', `rotate' and `unique' are local to the invocation.
134 MMacroInvocation
*prev
; /* previous invocation */
136 int nparam_min
, nparam_max
;
138 bool plus
; /* is the last parameter greedy? */
139 bool nolist
; /* is this macro listing-inhibited? */
140 int64_t in_progress
; /* is this macro currently being expanded? */
141 int32_t max_depth
; /* maximum number of recursive expansions allowed */
142 Token
*dlist
; /* All defaults as one list */
143 Token
**defaults
; /* Parameter default pointers */
144 int ndefs
; /* number of default parameters */
148 MMacro
*rep_nest
; /* used for nesting %rep */
149 Token
**params
; /* actual parameters */
150 Token
*iline
; /* invocation line */
151 unsigned int nparam
, rotate
;
154 int lineno
; /* Current line number on expansion */
158 /* Store the definition of a multi-line macro, as defined in a
159 * previous recursive macro expansion.
161 struct MMacroInvocation
{
162 MMacroInvocation
*prev
; /* previous invocation */
163 Token
**params
; /* actual parameters */
164 Token
*iline
; /* invocation line */
165 unsigned int nparam
, rotate
;
172 * The context stack is composed of a linked list of these.
177 struct hash_table localmac
;
182 * This is the internal form which we break input lines up into.
183 * Typically stored in linked lists.
185 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
186 * necessarily used as-is, but is intended to denote the number of
187 * the substituted parameter. So in the definition
189 * %define a(x,y) ( (x) & ~(y) )
191 * the token representing `x' will have its type changed to
192 * TOK_SMAC_PARAM, but the one representing `y' will be
195 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
196 * which doesn't need quotes around it. Used in the pre-include
197 * mechanism as an alternative to trying to find a sensible type of
198 * quote to use on the filename we were passed.
201 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
202 TOK_PREPROC_ID
, TOK_STRING
,
203 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
205 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
207 TOK_INDIRECT
, /* %[...] */
208 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
209 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
216 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
217 size_t len
; /* scratch length field */
218 } a
; /* Auxiliary data */
219 enum pp_token_type type
;
223 * Multi-line macro definitions are stored as a linked list of
224 * these, which is essentially a container to allow several linked
227 * Note that in this module, linked lists are treated as stacks
228 * wherever possible. For this reason, Lines are _pushed_ on to the
229 * `expansion' field in MMacro structures, so that the linked list,
230 * if walked, would give the macro lines in reverse order; this
231 * means that we can walk the list when expanding a macro, and thus
232 * push the lines on to the `expansion' field in _istk_ in reverse
233 * order (so that when popped back off they are in the right
234 * order). It may seem cockeyed, and it relies on my design having
235 * an even number of steps in, but it works...
237 * Some of these structures, rather than being actual lines, are
238 * markers delimiting the end of the expansion of a given macro.
239 * This is for use in the cycle-tracking and %rep-handling code.
240 * Such structures have `finishes' non-NULL, and `first' NULL. All
241 * others have `finishes' NULL, but `first' may still be NULL if
251 * To handle an arbitrary level of file inclusion, we maintain a
252 * stack (ie linked list) of these things.
261 MMacro
*mstk
; /* stack of active macros/reps */
265 * Include search path. This is simply a list of strings which get
266 * prepended, in turn, to the name of an include file, in an
267 * attempt to find the file if it's not in the current directory.
275 * Conditional assembly: we maintain a separate stack of these for
276 * each level of file inclusion. (The only reason we keep the
277 * stacks separate is to ensure that a stray `%endif' in a file
278 * included from within the true branch of a `%if' won't terminate
279 * it and cause confusion: instead, rightly, it'll cause an error.)
287 * These states are for use just after %if or %elif: IF_TRUE
288 * means the condition has evaluated to truth so we are
289 * currently emitting, whereas IF_FALSE means we are not
290 * currently emitting but will start doing so if a %else comes
291 * up. In these states, all directives are admissible: %elif,
292 * %else and %endif. (And of course %if.)
294 COND_IF_TRUE
, COND_IF_FALSE
,
296 * These states come up after a %else: ELSE_TRUE means we're
297 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
298 * any %elif or %else will cause an error.
300 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
302 * These states mean that we're not emitting now, and also that
303 * nothing until %endif will be emitted at all. COND_DONE is
304 * used when we've had our moment of emission
305 * and have now started seeing %elifs. COND_NEVER is used when
306 * the condition construct in question is contained within a
307 * non-emitting branch of a larger condition construct,
308 * or if there is an error.
310 COND_DONE
, COND_NEVER
312 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
315 * These defines are used as the possible return values for do_directive
317 #define NO_DIRECTIVE_FOUND 0
318 #define DIRECTIVE_FOUND 1
321 * This define sets the upper limit for smacro and recursive mmacro
324 #define DEADMAN_LIMIT (1 << 20)
327 * Condition codes. Note that we use c_ prefix not C_ because C_ is
328 * used in nasm.h for the "real" condition codes. At _this_ level,
329 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
330 * ones, so we need a different enum...
332 static const char * const conditions
[] = {
333 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
334 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
335 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
338 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
339 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
340 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
343 static const enum pp_conds inverse_ccs
[] = {
344 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
345 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
,
346 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
352 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
353 static int is_condition(enum preproc_token arg
)
355 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
358 /* For TASM compatibility we need to be able to recognise TASM compatible
359 * conditional compilation directives. Using the NASM pre-processor does
360 * not work, so we look for them specifically from the following list and
361 * then jam in the equivalent NASM directive into the input stream.
365 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
366 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
369 static const char * const tasm_directives
[] = {
370 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
371 "ifndef", "include", "local"
374 static int StackSize
= 4;
375 static char *StackPointer
= "ebp";
376 static int ArgOffset
= 8;
377 static int LocalOffset
= 0;
379 static Context
*cstk
;
380 static Include
*istk
;
381 static IncPath
*ipath
= NULL
;
383 static efunc _error
; /* Pointer to client-provided error reporting function */
384 static evalfunc evaluate
;
386 static int pass
; /* HACK: pass 0 = generate dependencies only */
387 static StrList
**dephead
, **deptail
; /* Dependency list */
389 static uint64_t unique
; /* unique identifier numbers */
391 static Line
*predef
= NULL
;
392 static bool do_predef
;
394 static ListGen
*list
;
397 * The current set of multi-line macros we have defined.
399 static struct hash_table mmacros
;
402 * The current set of single-line macros we have defined.
404 static struct hash_table smacros
;
407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
410 static MMacro
*defining
;
412 static uint64_t nested_mac_count
;
413 static uint64_t nested_rep_count
;
416 * The number of macro parameters to allocate space for at a time.
418 #define PARAM_DELTA 16
421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
424 static macros_t
*stdmacpos
;
427 * The extra standard macros that come from the object format, if
430 static macros_t
*extrastdmac
= NULL
;
431 static bool any_extrastdmac
;
434 * Tokens are allocated in blocks to improve speed
436 #define TOKEN_BLOCKSIZE 4096
437 static Token
*freeTokens
= NULL
;
443 static Blocks blocks
= { NULL
, NULL
};
446 * Forward declarations.
448 static Token
*expand_mmac_params(Token
* tline
);
449 static Token
*expand_smacro(Token
* tline
);
450 static Token
*expand_id(Token
* tline
);
451 static Context
*get_ctx(const char *name
, const char **namep
,
453 static void make_tok_num(Token
* tok
, int64_t val
);
454 static void error(int severity
, const char *fmt
, ...);
455 static void error_precond(int severity
, const char *fmt
, ...);
456 static void *new_Block(size_t size
);
457 static void delete_Blocks(void);
458 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
459 const char *text
, int txtlen
);
460 static Token
*delete_Token(Token
* t
);
463 * Macros for safe checking of token pointers, avoid *(NULL)
465 #define tok_type_(x,t) ((x) && (x)->type == (t))
466 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
470 /* Handle TASM specific directives, which do not contain a % in
471 * front of them. We do it here because I could not find any other
472 * place to do it for the moment, and it is a hack (ideally it would
473 * be nice to be able to use the NASM pre-processor to do it).
475 static char *check_tasm_directive(char *line
)
477 int32_t i
, j
, k
, m
, len
;
478 char *p
= line
, *oldline
, oldchar
;
480 /* Skip whitespace */
481 while (nasm_isspace(*p
) && *p
!= 0)
484 /* Binary search for the directive name */
486 j
= elements(tasm_directives
);
488 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
495 m
= nasm_stricmp(p
, tasm_directives
[k
]);
497 /* We have found a directive, so jam a % in front of it
498 * so that NASM will then recognise it as one if it's own.
503 line
= nasm_malloc(len
+ 2);
505 if (k
== TM_IFDIFI
) {
507 * NASM does not recognise IFDIFI, so we convert
508 * it to %if 0. This is not used in NASM
509 * compatible code, but does need to parse for the
510 * TASM macro package.
512 strcpy(line
+ 1, "if 0");
514 memcpy(line
+ 1, p
, len
+ 1);
529 * The pre-preprocessing stage... This function translates line
530 * number indications as they emerge from GNU cpp (`# lineno "file"
531 * flags') into NASM preprocessor line number indications (`%line
534 static char *prepreproc(char *line
)
537 char *fname
, *oldline
;
539 if (line
[0] == '#' && line
[1] == ' ') {
542 lineno
= atoi(fname
);
543 fname
+= strspn(fname
, "0123456789 ");
546 fnlen
= strcspn(fname
, "\"");
547 line
= nasm_malloc(20 + fnlen
);
548 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
551 if (tasm_compatible_mode
)
552 return check_tasm_directive(line
);
557 * Free a linked list of tokens.
559 static void free_tlist(Token
* list
)
562 list
= delete_Token(list
);
567 * Free a linked list of lines.
569 static void free_llist(Line
* list
)
575 free_tlist(l
->first
);
583 static void free_mmacro(MMacro
* m
)
586 free_tlist(m
->dlist
);
587 nasm_free(m
->defaults
);
588 free_llist(m
->expansion
);
593 * Free all currently defined macros, and free the hash tables
595 static void free_smacro_table(struct hash_table
*smt
)
599 struct hash_tbl_node
*it
= NULL
;
601 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
602 nasm_free((void *)key
);
604 SMacro
*ns
= s
->next
;
606 free_tlist(s
->expansion
);
614 static void free_mmacro_table(struct hash_table
*mmt
)
618 struct hash_tbl_node
*it
= NULL
;
621 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
622 nasm_free((void *)key
);
624 MMacro
*nm
= m
->next
;
632 static void free_macros(void)
634 free_smacro_table(&smacros
);
635 free_mmacro_table(&mmacros
);
639 * Initialize the hash tables
641 static void init_macros(void)
643 hash_init(&smacros
, HASH_LARGE
);
644 hash_init(&mmacros
, HASH_LARGE
);
648 * Pop the context stack.
650 static void ctx_pop(void)
655 free_smacro_table(&c
->localmac
);
661 * Search for a key in the hash index; adding it if necessary
662 * (in which case we initialize the data pointer to NULL.)
665 hash_findi_add(struct hash_table
*hash
, const char *str
)
667 struct hash_insert hi
;
671 r
= hash_findi(hash
, str
, &hi
);
675 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
676 return hash_add(&hi
, strx
, NULL
);
680 * Like hash_findi, but returns the data element rather than a pointer
681 * to it. Used only when not adding a new element, hence no third
685 hash_findix(struct hash_table
*hash
, const char *str
)
689 p
= hash_findi(hash
, str
, NULL
);
690 return p
? *p
: NULL
;
693 #define BUF_DELTA 512
695 * Read a line from the top file in istk, handling multiple CR/LFs
696 * at the end of the line read, and handling spurious ^Zs. Will
697 * return lines from the standard macro set if this has not already
700 static char *read_line(void)
702 char *buffer
, *p
, *q
;
703 int bufsize
, continued_count
;
707 const unsigned char *p
= stdmacpos
;
712 len
+= pp_directives_len
[c
-0x80]+1;
716 ret
= nasm_malloc(len
+1);
718 while ((c
= *stdmacpos
++)) {
720 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
721 q
+= pp_directives_len
[c
-0x80];
731 /* This was the last of the standard macro chain... */
733 if (any_extrastdmac
) {
734 stdmacpos
= extrastdmac
;
735 any_extrastdmac
= false;
736 } else if (do_predef
) {
738 Token
*head
, **tail
, *t
;
741 * Nasty hack: here we push the contents of
742 * `predef' on to the top-level expansion stack,
743 * since this is the most convenient way to
744 * implement the pre-include and pre-define
747 for (pd
= predef
; pd
; pd
= pd
->next
) {
750 for (t
= pd
->first
; t
; t
= t
->next
) {
751 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
752 tail
= &(*tail
)->next
;
754 l
= nasm_malloc(sizeof(Line
));
755 l
->next
= istk
->expansion
;
767 buffer
= nasm_malloc(BUF_DELTA
);
771 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
775 if (p
> buffer
&& p
[-1] == '\n') {
776 /* Convert backslash-CRLF line continuation sequences into
777 nothing at all (for DOS and Windows) */
778 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
783 /* Also convert backslash-LF line continuation sequences into
784 nothing at all (for Unix) */
785 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
793 if (p
- buffer
> bufsize
- 10) {
794 int32_t offset
= p
- buffer
;
795 bufsize
+= BUF_DELTA
;
796 buffer
= nasm_realloc(buffer
, bufsize
);
797 p
= buffer
+ offset
; /* prevent stale-pointer problems */
801 if (!q
&& p
== buffer
) {
806 src_set_linnum(src_get_linnum() + istk
->lineinc
+
807 (continued_count
* istk
->lineinc
));
810 * Play safe: remove CRs as well as LFs, if any of either are
811 * present at the end of the line.
813 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
817 * Handle spurious ^Z, which may be inserted into source files
818 * by some file transfer utilities.
820 buffer
[strcspn(buffer
, "\032")] = '\0';
822 list
->line(LIST_READ
, buffer
);
828 * Tokenize a line of text. This is a very simple process since we
829 * don't need to parse the value out of e.g. numeric tokens: we
830 * simply split one string into many.
832 static Token
*tokenize(char *line
)
835 enum pp_token_type type
;
837 Token
*t
, **tail
= &list
;
843 if (*p
== '+' && !nasm_isdigit(p
[1])) {
846 } else if (nasm_isdigit(*p
) ||
847 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
851 while (nasm_isdigit(*p
));
852 type
= TOK_PREPROC_ID
;
853 } else if (*p
== '{') {
855 while (*p
&& *p
!= '}') {
862 type
= TOK_PREPROC_ID
;
863 } else if (*p
== '[') {
865 line
+= 2; /* Skip the leading %[ */
867 while (lvl
&& (c
= *p
++)) {
879 p
= nasm_skip_string(p
)+1;
889 error(ERR_NONFATAL
, "unterminated %[ construct");
891 } else if (*p
== '?') {
892 type
= TOK_PREPROC_Q
; /* %? */
895 type
= TOK_PREPROC_QQ
; /* %?? */
898 } else if (isidchar(*p
) ||
899 ((*p
== '!' || *p
== '%' || *p
== '$') &&
904 while (isidchar(*p
));
905 type
= TOK_PREPROC_ID
;
911 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
914 while (*p
&& isidchar(*p
))
916 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
921 p
= nasm_skip_string(p
);
926 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
927 /* Handling unterminated strings by UNV */
930 } else if (p
[0] == '$' && p
[1] == '$') {
931 type
= TOK_OTHER
; /* TOKEN_BASE */
933 } else if (isnumstart(*p
)) {
935 bool is_float
= false;
951 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
953 if (*p
== '+' || *p
== '-') {
954 /* e can only be followed by +/- if it is either a
955 prefixed hex number or a floating-point number */
959 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
961 } else if (c
== 'P' || c
== 'p') {
963 if (*p
== '+' || *p
== '-')
965 } else if (isnumchar(c
) || c
== '_')
968 /* we need to deal with consequences of the legacy
969 parser, like "1.nolist" being two tokens
970 (TOK_NUMBER, TOK_ID) here; at least give it
971 a shot for now. In the future, we probably need
972 a flex-based scanner with proper pattern matching
973 to do it as well as it can be done. Nothing in
974 the world is going to help the person who wants
975 0x123.p16 interpreted as two tokens, though. */
980 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
981 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
982 (*r
== 'p' || *r
== 'P')) {
986 break; /* Terminate the token */
990 p
--; /* Point to first character beyond number */
992 if (p
== line
+1 && *line
== '$') {
993 type
= TOK_OTHER
; /* TOKEN_HERE */
995 if (has_e
&& !is_hex
) {
996 /* 1e13 is floating-point, but 1e13h is not */
1000 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1002 } else if (nasm_isspace(*p
)) {
1003 type
= TOK_WHITESPACE
;
1005 while (*p
&& nasm_isspace(*p
))
1008 * Whitespace just before end-of-line is discarded by
1009 * pretending it's a comment; whitespace just before a
1010 * comment gets lumped into the comment.
1012 if (!*p
|| *p
== ';') {
1017 } else if (*p
== ';') {
1023 * Anything else is an operator of some kind. We check
1024 * for all the double-character operators (>>, <<, //,
1025 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1026 * else is a single-character operator.
1029 if ((p
[0] == '>' && p
[1] == '>') ||
1030 (p
[0] == '<' && p
[1] == '<') ||
1031 (p
[0] == '/' && p
[1] == '/') ||
1032 (p
[0] == '<' && p
[1] == '=') ||
1033 (p
[0] == '>' && p
[1] == '=') ||
1034 (p
[0] == '=' && p
[1] == '=') ||
1035 (p
[0] == '!' && p
[1] == '=') ||
1036 (p
[0] == '<' && p
[1] == '>') ||
1037 (p
[0] == '&' && p
[1] == '&') ||
1038 (p
[0] == '|' && p
[1] == '|') ||
1039 (p
[0] == '^' && p
[1] == '^')) {
1045 /* Handling unterminated string by UNV */
1048 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1049 t->text[p-line] = *line;
1053 if (type
!= TOK_COMMENT
) {
1054 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1063 * this function allocates a new managed block of memory and
1064 * returns a pointer to the block. The managed blocks are
1065 * deleted only all at once by the delete_Blocks function.
1067 static void *new_Block(size_t size
)
1069 Blocks
*b
= &blocks
;
1071 /* first, get to the end of the linked list */
1074 /* now allocate the requested chunk */
1075 b
->chunk
= nasm_malloc(size
);
1077 /* now allocate a new block for the next request */
1078 b
->next
= nasm_malloc(sizeof(Blocks
));
1079 /* and initialize the contents of the new block */
1080 b
->next
->next
= NULL
;
1081 b
->next
->chunk
= NULL
;
1086 * this function deletes all managed blocks of memory
1088 static void delete_Blocks(void)
1090 Blocks
*a
, *b
= &blocks
;
1093 * keep in mind that the first block, pointed to by blocks
1094 * is a static and not dynamically allocated, so we don't
1099 nasm_free(b
->chunk
);
1108 * this function creates a new Token and passes a pointer to it
1109 * back to the caller. It sets the type and text elements, and
1110 * also the a.mac and next elements to NULL.
1112 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1113 const char *text
, int txtlen
)
1119 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1120 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1121 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1122 freeTokens
[i
].next
= NULL
;
1125 freeTokens
= t
->next
;
1129 if (type
== TOK_WHITESPACE
|| !text
) {
1133 txtlen
= strlen(text
);
1134 t
->text
= nasm_malloc(txtlen
+1);
1135 memcpy(t
->text
, text
, txtlen
);
1136 t
->text
[txtlen
] = '\0';
1141 static Token
*delete_Token(Token
* t
)
1143 Token
*next
= t
->next
;
1145 t
->next
= freeTokens
;
1151 * Convert a line of tokens back into text.
1152 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1153 * will be transformed into ..@ctxnum.xxx
1155 static char *detoken(Token
* tlist
, bool expand_locals
)
1163 for (t
= tlist
; t
; t
= t
->next
) {
1164 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1165 char *p
= getenv(t
->text
+ 2);
1168 t
->text
= nasm_strdup(p
);
1172 /* Expand local macros here and not during preprocessing */
1173 if (expand_locals
&&
1174 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1175 t
->text
[0] == '%' && t
->text
[1] == '$') {
1178 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1181 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1182 p
= nasm_strcat(buffer
, q
);
1187 if (t
->type
== TOK_WHITESPACE
) {
1189 } else if (t
->text
) {
1190 len
+= strlen(t
->text
);
1193 p
= line
= nasm_malloc(len
+ 1);
1194 for (t
= tlist
; t
; t
= t
->next
) {
1195 if (t
->type
== TOK_WHITESPACE
) {
1197 } else if (t
->text
) {
1208 * A scanner, suitable for use by the expression evaluator, which
1209 * operates on a line of Tokens. Expects a pointer to a pointer to
1210 * the first token in the line to be passed in as its private_data
1213 * FIX: This really needs to be unified with stdscan.
1215 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1217 Token
**tlineptr
= private_data
;
1219 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1223 *tlineptr
= tline
? tline
->next
: NULL
;
1225 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1226 tline
->type
== TOK_COMMENT
));
1229 return tokval
->t_type
= TOKEN_EOS
;
1231 tokval
->t_charptr
= tline
->text
;
1233 if (tline
->text
[0] == '$' && !tline
->text
[1])
1234 return tokval
->t_type
= TOKEN_HERE
;
1235 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1236 return tokval
->t_type
= TOKEN_BASE
;
1238 if (tline
->type
== TOK_ID
) {
1239 p
= tokval
->t_charptr
= tline
->text
;
1241 tokval
->t_charptr
++;
1242 return tokval
->t_type
= TOKEN_ID
;
1245 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1246 if (r
>= p
+MAX_KEYWORD
)
1247 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1248 *s
++ = nasm_tolower(*r
);
1251 /* right, so we have an identifier sitting in temp storage. now,
1252 * is it actually a register or instruction name, or what? */
1253 return nasm_token_hash(ourcopy
, tokval
);
1256 if (tline
->type
== TOK_NUMBER
) {
1258 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1259 tokval
->t_charptr
= tline
->text
;
1261 return tokval
->t_type
= TOKEN_ERRNUM
;
1263 return tokval
->t_type
= TOKEN_NUM
;
1266 if (tline
->type
== TOK_FLOAT
) {
1267 return tokval
->t_type
= TOKEN_FLOAT
;
1270 if (tline
->type
== TOK_STRING
) {
1273 bq
= tline
->text
[0];
1274 tokval
->t_charptr
= tline
->text
;
1275 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1277 if (ep
[0] != bq
|| ep
[1] != '\0')
1278 return tokval
->t_type
= TOKEN_ERRSTR
;
1280 return tokval
->t_type
= TOKEN_STR
;
1283 if (tline
->type
== TOK_OTHER
) {
1284 if (!strcmp(tline
->text
, "<<"))
1285 return tokval
->t_type
= TOKEN_SHL
;
1286 if (!strcmp(tline
->text
, ">>"))
1287 return tokval
->t_type
= TOKEN_SHR
;
1288 if (!strcmp(tline
->text
, "//"))
1289 return tokval
->t_type
= TOKEN_SDIV
;
1290 if (!strcmp(tline
->text
, "%%"))
1291 return tokval
->t_type
= TOKEN_SMOD
;
1292 if (!strcmp(tline
->text
, "=="))
1293 return tokval
->t_type
= TOKEN_EQ
;
1294 if (!strcmp(tline
->text
, "<>"))
1295 return tokval
->t_type
= TOKEN_NE
;
1296 if (!strcmp(tline
->text
, "!="))
1297 return tokval
->t_type
= TOKEN_NE
;
1298 if (!strcmp(tline
->text
, "<="))
1299 return tokval
->t_type
= TOKEN_LE
;
1300 if (!strcmp(tline
->text
, ">="))
1301 return tokval
->t_type
= TOKEN_GE
;
1302 if (!strcmp(tline
->text
, "&&"))
1303 return tokval
->t_type
= TOKEN_DBL_AND
;
1304 if (!strcmp(tline
->text
, "^^"))
1305 return tokval
->t_type
= TOKEN_DBL_XOR
;
1306 if (!strcmp(tline
->text
, "||"))
1307 return tokval
->t_type
= TOKEN_DBL_OR
;
1311 * We have no other options: just return the first character of
1314 return tokval
->t_type
= tline
->text
[0];
1318 * Compare a string to the name of an existing macro; this is a
1319 * simple wrapper which calls either strcmp or nasm_stricmp
1320 * depending on the value of the `casesense' parameter.
1322 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1324 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1328 * Compare a string to the name of an existing macro; this is a
1329 * simple wrapper which calls either strcmp or nasm_stricmp
1330 * depending on the value of the `casesense' parameter.
1332 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1334 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1338 * Return the Context structure associated with a %$ token. Return
1339 * NULL, having _already_ reported an error condition, if the
1340 * context stack isn't deep enough for the supplied number of $
1342 * If all_contexts == true, contexts that enclose current are
1343 * also scanned for such smacro, until it is found; if not -
1344 * only the context that directly results from the number of $'s
1345 * in variable's name.
1347 * If "namep" is non-NULL, set it to the pointer to the macro name
1348 * tail, i.e. the part beyond %$...
1350 static Context
*get_ctx(const char *name
, const char **namep
,
1360 if (!name
|| name
[0] != '%' || name
[1] != '$')
1364 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1371 while (ctx
&& *name
== '$') {
1377 error(ERR_NONFATAL
, "`%s': context stack is only"
1378 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1389 /* Search for this smacro in found context */
1390 m
= hash_findix(&ctx
->localmac
, name
);
1392 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1403 * Check to see if a file is already in a string list
1405 static bool in_list(const StrList
*list
, const char *str
)
1408 if (!strcmp(list
->str
, str
))
1416 * Open an include file. This routine must always return a valid
1417 * file pointer if it returns - it's responsible for throwing an
1418 * ERR_FATAL and bombing out completely if not. It should also try
1419 * the include path one by one until it finds the file or reaches
1420 * the end of the path.
1422 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1427 IncPath
*ip
= ipath
;
1428 int len
= strlen(file
);
1429 size_t prefix_len
= 0;
1433 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1434 memcpy(sl
->str
, prefix
, prefix_len
);
1435 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1436 fp
= fopen(sl
->str
, "r");
1437 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1455 prefix_len
= strlen(prefix
);
1457 /* -MG given and file not found */
1458 if (dhead
&& !in_list(*dhead
, file
)) {
1459 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1461 strcpy(sl
->str
, file
);
1469 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1470 return NULL
; /* never reached - placate compilers */
1474 * Determine if we should warn on defining a single-line macro of
1475 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1476 * return true if _any_ single-line macro of that name is defined.
1477 * Otherwise, will return true if a single-line macro with either
1478 * `nparam' or no parameters is defined.
1480 * If a macro with precisely the right number of parameters is
1481 * defined, or nparam is -1, the address of the definition structure
1482 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1483 * is NULL, no action will be taken regarding its contents, and no
1486 * Note that this is also called with nparam zero to resolve
1489 * If you already know which context macro belongs to, you can pass
1490 * the context pointer as first parameter; if you won't but name begins
1491 * with %$ the context will be automatically computed. If all_contexts
1492 * is true, macro will be searched in outer contexts as well.
1495 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1498 struct hash_table
*smtbl
;
1502 smtbl
= &ctx
->localmac
;
1503 } else if (name
[0] == '%' && name
[1] == '$') {
1505 ctx
= get_ctx(name
, &name
, false);
1507 return false; /* got to return _something_ */
1508 smtbl
= &ctx
->localmac
;
1512 m
= (SMacro
*) hash_findix(smtbl
, name
);
1515 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1516 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1518 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1532 * Count and mark off the parameters in a multi-line macro call.
1533 * This is called both from within the multi-line macro expansion
1534 * code, and also to mark off the default parameters when provided
1535 * in a %macro definition line.
1537 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1539 int paramsize
, brace
;
1541 *nparam
= paramsize
= 0;
1544 /* +1: we need space for the final NULL */
1545 if (*nparam
+1 >= paramsize
) {
1546 paramsize
+= PARAM_DELTA
;
1547 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1551 if (tok_is_(t
, "{"))
1553 (*params
)[(*nparam
)++] = t
;
1554 while (tok_isnt_(t
, brace
? "}" : ","))
1556 if (t
) { /* got a comma/brace */
1560 * Now we've found the closing brace, look further
1564 if (tok_isnt_(t
, ",")) {
1566 "braces do not enclose all of macro parameter");
1567 while (tok_isnt_(t
, ","))
1571 t
= t
->next
; /* eat the comma */
1578 * Determine whether one of the various `if' conditions is true or
1581 * We must free the tline we get passed.
1583 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1585 enum pp_conditional i
= PP_COND(ct
);
1587 Token
*t
, *tt
, **tptr
, *origline
;
1588 struct tokenval tokval
;
1590 enum pp_token_type needtype
;
1596 j
= false; /* have we matched yet? */
1601 if (tline
->type
!= TOK_ID
) {
1603 "`%s' expects context identifiers", pp_directives
[ct
]);
1604 free_tlist(origline
);
1607 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1609 tline
= tline
->next
;
1614 j
= false; /* have we matched yet? */
1617 if (!tline
|| (tline
->type
!= TOK_ID
&&
1618 (tline
->type
!= TOK_PREPROC_ID
||
1619 tline
->text
[1] != '$'))) {
1621 "`%s' expects macro identifiers", pp_directives
[ct
]);
1624 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1626 tline
= tline
->next
;
1632 tline
= expand_smacro(tline
);
1634 while (tok_isnt_(tt
, ","))
1638 "`%s' expects two comma-separated arguments",
1643 j
= true; /* assume equality unless proved not */
1644 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1645 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1646 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1650 if (t
->type
== TOK_WHITESPACE
) {
1654 if (tt
->type
== TOK_WHITESPACE
) {
1658 if (tt
->type
!= t
->type
) {
1659 j
= false; /* found mismatching tokens */
1662 /* When comparing strings, need to unquote them first */
1663 if (t
->type
== TOK_STRING
) {
1664 size_t l1
= nasm_unquote(t
->text
, NULL
);
1665 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1671 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1675 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1676 j
= false; /* found mismatching tokens */
1683 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1684 j
= false; /* trailing gunk on one end or other */
1690 MMacro searching
, *mmac
;
1693 tline
= expand_id(tline
);
1694 if (!tok_type_(tline
, TOK_ID
)) {
1696 "`%s' expects a macro name", pp_directives
[ct
]);
1699 searching
.name
= nasm_strdup(tline
->text
);
1700 searching
.casesense
= true;
1701 searching
.plus
= false;
1702 searching
.nolist
= false;
1703 searching
.in_progress
= 0;
1704 searching
.max_depth
= 0;
1705 searching
.rep_nest
= NULL
;
1706 searching
.nparam_min
= 0;
1707 searching
.nparam_max
= INT_MAX
;
1708 tline
= expand_smacro(tline
->next
);
1711 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1713 "`%s' expects a parameter count or nothing",
1716 searching
.nparam_min
= searching
.nparam_max
=
1717 readnum(tline
->text
, &j
);
1720 "unable to parse parameter count `%s'",
1723 if (tline
&& tok_is_(tline
->next
, "-")) {
1724 tline
= tline
->next
->next
;
1725 if (tok_is_(tline
, "*"))
1726 searching
.nparam_max
= INT_MAX
;
1727 else if (!tok_type_(tline
, TOK_NUMBER
))
1729 "`%s' expects a parameter count after `-'",
1732 searching
.nparam_max
= readnum(tline
->text
, &j
);
1735 "unable to parse parameter count `%s'",
1737 if (searching
.nparam_min
> searching
.nparam_max
)
1739 "minimum parameter count exceeds maximum");
1742 if (tline
&& tok_is_(tline
->next
, "+")) {
1743 tline
= tline
->next
;
1744 searching
.plus
= true;
1746 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1748 if (!strcmp(mmac
->name
, searching
.name
) &&
1749 (mmac
->nparam_min
<= searching
.nparam_max
1751 && (searching
.nparam_min
<= mmac
->nparam_max
1758 if (tline
&& tline
->next
)
1759 error(ERR_WARNING
|ERR_PASS1
,
1760 "trailing garbage after %%ifmacro ignored");
1761 nasm_free(searching
.name
);
1770 needtype
= TOK_NUMBER
;
1773 needtype
= TOK_STRING
;
1777 t
= tline
= expand_smacro(tline
);
1779 while (tok_type_(t
, TOK_WHITESPACE
) ||
1780 (needtype
== TOK_NUMBER
&&
1781 tok_type_(t
, TOK_OTHER
) &&
1782 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1786 j
= tok_type_(t
, needtype
);
1790 t
= tline
= expand_smacro(tline
);
1791 while (tok_type_(t
, TOK_WHITESPACE
))
1796 t
= t
->next
; /* Skip the actual token */
1797 while (tok_type_(t
, TOK_WHITESPACE
))
1799 j
= !t
; /* Should be nothing left */
1804 t
= tline
= expand_smacro(tline
);
1805 while (tok_type_(t
, TOK_WHITESPACE
))
1808 j
= !t
; /* Should be empty */
1812 t
= tline
= expand_smacro(tline
);
1814 tokval
.t_type
= TOKEN_INVALID
;
1815 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1816 NULL
, pass
| CRITICAL
, error
, NULL
);
1820 error(ERR_WARNING
|ERR_PASS1
,
1821 "trailing garbage after expression ignored");
1822 if (!is_simple(evalresult
)) {
1824 "non-constant value given to `%s'", pp_directives
[ct
]);
1827 j
= reloc_value(evalresult
) != 0;
1832 "preprocessor directive `%s' not yet implemented",
1837 free_tlist(origline
);
1838 return j
^ PP_NEGATIVE(ct
);
1841 free_tlist(origline
);
1846 * Common code for defining an smacro
1848 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1849 int nparam
, Token
*expansion
)
1851 SMacro
*smac
, **smhead
;
1852 struct hash_table
*smtbl
;
1854 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1856 error(ERR_WARNING
|ERR_PASS1
,
1857 "single-line macro `%s' defined both with and"
1858 " without parameters", mname
);
1860 /* Some instances of the old code considered this a failure,
1861 some others didn't. What is the right thing to do here? */
1862 free_tlist(expansion
);
1863 return false; /* Failure */
1866 * We're redefining, so we have to take over an
1867 * existing SMacro structure. This means freeing
1868 * what was already in it.
1870 nasm_free(smac
->name
);
1871 free_tlist(smac
->expansion
);
1874 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1875 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1876 smac
= nasm_malloc(sizeof(SMacro
));
1877 smac
->next
= *smhead
;
1880 smac
->name
= nasm_strdup(mname
);
1881 smac
->casesense
= casesense
;
1882 smac
->nparam
= nparam
;
1883 smac
->expansion
= expansion
;
1884 smac
->in_progress
= false;
1885 return true; /* Success */
1889 * Undefine an smacro
1891 static void undef_smacro(Context
*ctx
, const char *mname
)
1893 SMacro
**smhead
, *s
, **sp
;
1894 struct hash_table
*smtbl
;
1896 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1897 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1901 * We now have a macro name... go hunt for it.
1904 while ((s
= *sp
) != NULL
) {
1905 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1908 free_tlist(s
->expansion
);
1918 * Parse a mmacro specification.
1920 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1924 tline
= tline
->next
;
1926 tline
= expand_id(tline
);
1927 if (!tok_type_(tline
, TOK_ID
)) {
1928 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1933 def
->name
= nasm_strdup(tline
->text
);
1935 def
->nolist
= false;
1936 def
->in_progress
= 0;
1937 def
->rep_nest
= NULL
;
1938 def
->nparam_min
= 0;
1939 def
->nparam_max
= 0;
1941 tline
= expand_smacro(tline
->next
);
1943 if (!tok_type_(tline
, TOK_NUMBER
)) {
1944 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1946 def
->nparam_min
= def
->nparam_max
=
1947 readnum(tline
->text
, &err
);
1950 "unable to parse parameter count `%s'", tline
->text
);
1952 if (tline
&& tok_is_(tline
->next
, "-")) {
1953 tline
= tline
->next
->next
;
1954 if (tok_is_(tline
, "*")) {
1955 def
->nparam_max
= INT_MAX
;
1956 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1958 "`%s' expects a parameter count after `-'", directive
);
1960 def
->nparam_max
= readnum(tline
->text
, &err
);
1962 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1965 if (def
->nparam_min
> def
->nparam_max
) {
1966 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1970 if (tline
&& tok_is_(tline
->next
, "+")) {
1971 tline
= tline
->next
;
1974 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1975 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1976 tline
= tline
->next
;
1981 * Handle default parameters.
1983 if (tline
&& tline
->next
) {
1984 def
->dlist
= tline
->next
;
1986 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1989 def
->defaults
= NULL
;
1991 def
->expansion
= NULL
;
1993 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1995 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1996 "too many default macro parameters");
2003 * Decode a size directive
2005 static int parse_size(const char *str
) {
2006 static const char *size_names
[] =
2007 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2008 static const int sizes
[] =
2009 { 0, 1, 4, 16, 8, 10, 2, 32 };
2011 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
2015 * nasm_unquote with error if the string contains NUL characters.
2016 * If the string contains NUL characters, issue an error and return
2017 * the C len, i.e. truncate at the NUL.
2019 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
2021 size_t len
= nasm_unquote(qstr
, NULL
);
2022 size_t clen
= strlen(qstr
);
2025 error(ERR_NONFATAL
, "NUL character in `%s' directive",
2026 pp_directives
[directive
]);
2032 * find and process preprocessor directive in passed line
2033 * Find out if a line contains a preprocessor directive, and deal
2036 * If a directive _is_ found, it is the responsibility of this routine
2037 * (and not the caller) to free_tlist() the line.
2039 * @param tline a pointer to the current tokeninzed line linked list
2040 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2043 static int do_directive(Token
* tline
)
2045 enum preproc_token i
;
2058 MMacro
*mmac
, **mmhead
;
2059 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2061 struct tokenval tokval
;
2063 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2071 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2072 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2073 || tline
->text
[1] == '!'))
2074 return NO_DIRECTIVE_FOUND
;
2076 i
= pp_token_hash(tline
->text
);
2079 * If we're in a non-emitting branch of a condition construct,
2080 * or walking to the end of an already terminated %rep block,
2081 * we should ignore all directives except for condition
2084 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2085 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2086 return NO_DIRECTIVE_FOUND
;
2090 * If we're defining a macro or reading a %rep block, we should
2091 * ignore all directives except for %macro/%imacro (which nest),
2092 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2093 * If we're in a %rep block, another %rep nests, so should be let through.
2095 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2096 i
!= PP_RMACRO
&& i
!= PP_IRMACRO
&&
2097 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2098 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2099 return NO_DIRECTIVE_FOUND
;
2103 if (i
== PP_MACRO
|| i
== PP_IMACRO
||
2104 i
== PP_RMACRO
|| i
== PP_IRMACRO
) {
2106 return NO_DIRECTIVE_FOUND
;
2107 } else if (nested_mac_count
> 0) {
2108 if (i
== PP_ENDMACRO
) {
2110 return NO_DIRECTIVE_FOUND
;
2113 if (!defining
->name
) {
2116 return NO_DIRECTIVE_FOUND
;
2117 } else if (nested_rep_count
> 0) {
2118 if (i
== PP_ENDREP
) {
2120 return NO_DIRECTIVE_FOUND
;
2128 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2130 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2133 /* Directive to tell NASM what the default stack size is. The
2134 * default is for a 16-bit stack, and this can be overriden with
2136 * the following form:
2138 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2140 tline
= tline
->next
;
2141 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2142 tline
= tline
->next
;
2143 if (!tline
|| tline
->type
!= TOK_ID
) {
2144 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2145 free_tlist(origline
);
2146 return DIRECTIVE_FOUND
;
2148 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2149 /* All subsequent ARG directives are for a 32-bit stack */
2151 StackPointer
= "ebp";
2154 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2155 /* All subsequent ARG directives are for a 64-bit stack */
2157 StackPointer
= "rbp";
2160 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2161 /* All subsequent ARG directives are for a 16-bit stack,
2162 * far function call.
2165 StackPointer
= "bp";
2168 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2169 /* All subsequent ARG directives are for a 16-bit stack,
2170 * far function call. We don't support near functions.
2173 StackPointer
= "bp";
2177 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2178 free_tlist(origline
);
2179 return DIRECTIVE_FOUND
;
2181 free_tlist(origline
);
2182 return DIRECTIVE_FOUND
;
2185 /* TASM like ARG directive to define arguments to functions, in
2186 * the following form:
2188 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2192 char *arg
, directive
[256];
2193 int size
= StackSize
;
2195 /* Find the argument name */
2196 tline
= tline
->next
;
2197 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2198 tline
= tline
->next
;
2199 if (!tline
|| tline
->type
!= TOK_ID
) {
2200 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2201 free_tlist(origline
);
2202 return DIRECTIVE_FOUND
;
2206 /* Find the argument size type */
2207 tline
= tline
->next
;
2208 if (!tline
|| tline
->type
!= TOK_OTHER
2209 || tline
->text
[0] != ':') {
2211 "Syntax error processing `%%arg' directive");
2212 free_tlist(origline
);
2213 return DIRECTIVE_FOUND
;
2215 tline
= tline
->next
;
2216 if (!tline
|| tline
->type
!= TOK_ID
) {
2217 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2218 free_tlist(origline
);
2219 return DIRECTIVE_FOUND
;
2222 /* Allow macro expansion of type parameter */
2223 tt
= tokenize(tline
->text
);
2224 tt
= expand_smacro(tt
);
2225 size
= parse_size(tt
->text
);
2228 "Invalid size type for `%%arg' missing directive");
2230 free_tlist(origline
);
2231 return DIRECTIVE_FOUND
;
2235 /* Round up to even stack slots */
2236 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2238 /* Now define the macro for the argument */
2239 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2240 arg
, StackPointer
, offset
);
2241 do_directive(tokenize(directive
));
2244 /* Move to the next argument in the list */
2245 tline
= tline
->next
;
2246 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2247 tline
= tline
->next
;
2248 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2250 free_tlist(origline
);
2251 return DIRECTIVE_FOUND
;
2254 /* TASM like LOCAL directive to define local variables for a
2255 * function, in the following form:
2257 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2259 * The '= LocalSize' at the end is ignored by NASM, but is
2260 * required by TASM to define the local parameter size (and used
2261 * by the TASM macro package).
2263 offset
= LocalOffset
;
2265 char *local
, directive
[256];
2266 int size
= StackSize
;
2268 /* Find the argument name */
2269 tline
= tline
->next
;
2270 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2271 tline
= tline
->next
;
2272 if (!tline
|| tline
->type
!= TOK_ID
) {
2274 "`%%local' missing argument parameter");
2275 free_tlist(origline
);
2276 return DIRECTIVE_FOUND
;
2278 local
= tline
->text
;
2280 /* Find the argument size type */
2281 tline
= tline
->next
;
2282 if (!tline
|| tline
->type
!= TOK_OTHER
2283 || tline
->text
[0] != ':') {
2285 "Syntax error processing `%%local' directive");
2286 free_tlist(origline
);
2287 return DIRECTIVE_FOUND
;
2289 tline
= tline
->next
;
2290 if (!tline
|| tline
->type
!= TOK_ID
) {
2292 "`%%local' missing size type parameter");
2293 free_tlist(origline
);
2294 return DIRECTIVE_FOUND
;
2297 /* Allow macro expansion of type parameter */
2298 tt
= tokenize(tline
->text
);
2299 tt
= expand_smacro(tt
);
2300 size
= parse_size(tt
->text
);
2303 "Invalid size type for `%%local' missing directive");
2305 free_tlist(origline
);
2306 return DIRECTIVE_FOUND
;
2310 /* Round up to even stack slots */
2311 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2313 offset
+= size
; /* Negative offset, increment before */
2315 /* Now define the macro for the argument */
2316 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2317 local
, StackPointer
, offset
);
2318 do_directive(tokenize(directive
));
2320 /* Now define the assign to setup the enter_c macro correctly */
2321 snprintf(directive
, sizeof(directive
),
2322 "%%assign %%$localsize %%$localsize+%d", size
);
2323 do_directive(tokenize(directive
));
2325 /* Move to the next argument in the list */
2326 tline
= tline
->next
;
2327 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2328 tline
= tline
->next
;
2329 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2330 LocalOffset
= offset
;
2331 free_tlist(origline
);
2332 return DIRECTIVE_FOUND
;
2336 error(ERR_WARNING
|ERR_PASS1
,
2337 "trailing garbage after `%%clear' ignored");
2340 free_tlist(origline
);
2341 return DIRECTIVE_FOUND
;
2344 t
= tline
->next
= expand_smacro(tline
->next
);
2346 if (!t
|| (t
->type
!= TOK_STRING
&&
2347 t
->type
!= TOK_INTERNAL_STRING
)) {
2348 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2349 free_tlist(origline
);
2350 return DIRECTIVE_FOUND
; /* but we did _something_ */
2353 error(ERR_WARNING
|ERR_PASS1
,
2354 "trailing garbage after `%%depend' ignored");
2356 if (t
->type
!= TOK_INTERNAL_STRING
)
2357 nasm_unquote_cstr(p
, i
);
2358 if (dephead
&& !in_list(*dephead
, p
)) {
2359 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2363 deptail
= &sl
->next
;
2365 free_tlist(origline
);
2366 return DIRECTIVE_FOUND
;
2369 t
= tline
->next
= expand_smacro(tline
->next
);
2372 if (!t
|| (t
->type
!= TOK_STRING
&&
2373 t
->type
!= TOK_INTERNAL_STRING
)) {
2374 error(ERR_NONFATAL
, "`%%include' expects a file name");
2375 free_tlist(origline
);
2376 return DIRECTIVE_FOUND
; /* but we did _something_ */
2379 error(ERR_WARNING
|ERR_PASS1
,
2380 "trailing garbage after `%%include' ignored");
2382 if (t
->type
!= TOK_INTERNAL_STRING
)
2383 nasm_unquote_cstr(p
, i
);
2384 inc
= nasm_malloc(sizeof(Include
));
2387 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2389 /* -MG given but file not found */
2392 inc
->fname
= src_set_fname(nasm_strdup(p
));
2393 inc
->lineno
= src_set_linnum(0);
2395 inc
->expansion
= NULL
;
2398 list
->uplevel(LIST_INCLUDE
);
2400 free_tlist(origline
);
2401 return DIRECTIVE_FOUND
;
2405 static macros_t
*use_pkg
;
2406 const char *pkg_macro
;
2408 tline
= tline
->next
;
2410 tline
= expand_id(tline
);
2412 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2413 tline
->type
!= TOK_INTERNAL_STRING
&&
2414 tline
->type
!= TOK_ID
)) {
2415 error(ERR_NONFATAL
, "`%%use' expects a package name");
2416 free_tlist(origline
);
2417 return DIRECTIVE_FOUND
; /* but we did _something_ */
2420 error(ERR_WARNING
|ERR_PASS1
,
2421 "trailing garbage after `%%use' ignored");
2422 if (tline
->type
== TOK_STRING
)
2423 nasm_unquote_cstr(tline
->text
, i
);
2424 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2426 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2427 /* The first string will be <%define>__USE_*__ */
2428 pkg_macro
= (char *)use_pkg
+ 1;
2429 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2430 /* Not already included, go ahead and include it */
2431 stdmacpos
= use_pkg
;
2433 free_tlist(origline
);
2434 return DIRECTIVE_FOUND
;
2439 tline
= tline
->next
;
2441 tline
= expand_id(tline
);
2443 if (!tok_type_(tline
, TOK_ID
)) {
2444 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2446 free_tlist(origline
);
2447 return DIRECTIVE_FOUND
; /* but we did _something_ */
2450 error(ERR_WARNING
|ERR_PASS1
,
2451 "trailing garbage after `%s' ignored",
2453 p
= nasm_strdup(tline
->text
);
2455 p
= NULL
; /* Anonymous */
2459 ctx
= nasm_malloc(sizeof(Context
));
2461 hash_init(&ctx
->localmac
, HASH_SMALL
);
2463 ctx
->number
= unique
++;
2468 error(ERR_NONFATAL
, "`%s': context stack is empty",
2470 } else if (i
== PP_POP
) {
2471 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2472 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2474 cstk
->name
? cstk
->name
: "anonymous", p
);
2479 nasm_free(cstk
->name
);
2485 free_tlist(origline
);
2486 return DIRECTIVE_FOUND
;
2488 severity
= ERR_FATAL
;
2491 severity
= ERR_NONFATAL
;
2494 severity
= ERR_WARNING
|ERR_WARN_USER
;
2499 /* Only error out if this is the final pass */
2500 if (pass
!= 2 && i
!= PP_FATAL
)
2501 return DIRECTIVE_FOUND
;
2503 tline
->next
= expand_smacro(tline
->next
);
2504 tline
= tline
->next
;
2506 t
= tline
? tline
->next
: NULL
;
2508 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2509 /* The line contains only a quoted string */
2511 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2512 error(severity
, "%s", p
);
2514 /* Not a quoted string, or more than a quoted string */
2515 p
= detoken(tline
, false);
2516 error(severity
, "%s", p
);
2519 free_tlist(origline
);
2520 return DIRECTIVE_FOUND
;
2524 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2527 j
= if_condition(tline
->next
, i
);
2528 tline
->next
= NULL
; /* it got freed */
2529 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2531 cond
= nasm_malloc(sizeof(Cond
));
2532 cond
->next
= istk
->conds
;
2535 free_tlist(origline
);
2536 return DIRECTIVE_FOUND
;
2540 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2541 switch(istk
->conds
->state
) {
2543 istk
->conds
->state
= COND_DONE
;
2550 case COND_ELSE_TRUE
:
2551 case COND_ELSE_FALSE
:
2552 error_precond(ERR_WARNING
|ERR_PASS1
,
2553 "`%%elif' after `%%else' ignored");
2554 istk
->conds
->state
= COND_NEVER
;
2559 * IMPORTANT: In the case of %if, we will already have
2560 * called expand_mmac_params(); however, if we're
2561 * processing an %elif we must have been in a
2562 * non-emitting mode, which would have inhibited
2563 * the normal invocation of expand_mmac_params().
2564 * Therefore, we have to do it explicitly here.
2566 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2567 tline
->next
= NULL
; /* it got freed */
2568 istk
->conds
->state
=
2569 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2572 free_tlist(origline
);
2573 return DIRECTIVE_FOUND
;
2577 error_precond(ERR_WARNING
|ERR_PASS1
,
2578 "trailing garbage after `%%else' ignored");
2580 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2581 switch(istk
->conds
->state
) {
2584 istk
->conds
->state
= COND_ELSE_FALSE
;
2591 istk
->conds
->state
= COND_ELSE_TRUE
;
2594 case COND_ELSE_TRUE
:
2595 case COND_ELSE_FALSE
:
2596 error_precond(ERR_WARNING
|ERR_PASS1
,
2597 "`%%else' after `%%else' ignored.");
2598 istk
->conds
->state
= COND_NEVER
;
2601 free_tlist(origline
);
2602 return DIRECTIVE_FOUND
;
2606 error_precond(ERR_WARNING
|ERR_PASS1
,
2607 "trailing garbage after `%%endif' ignored");
2609 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2611 istk
->conds
= cond
->next
;
2613 free_tlist(origline
);
2614 return DIRECTIVE_FOUND
;
2621 error(ERR_FATAL
, "`%s': already defining a macro",
2623 return DIRECTIVE_FOUND
;
2625 defining
= nasm_malloc(sizeof(MMacro
));
2626 defining
->max_depth
=
2627 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2628 defining
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2629 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2630 nasm_free(defining
);
2632 return DIRECTIVE_FOUND
;
2635 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2637 if (!strcmp(mmac
->name
, defining
->name
) &&
2638 (mmac
->nparam_min
<= defining
->nparam_max
2640 && (defining
->nparam_min
<= mmac
->nparam_max
2642 error(ERR_WARNING
|ERR_PASS1
,
2643 "redefining multi-line macro `%s'", defining
->name
);
2644 return DIRECTIVE_FOUND
;
2648 free_tlist(origline
);
2649 return DIRECTIVE_FOUND
;
2653 if (! (defining
&& defining
->name
)) {
2654 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2655 return DIRECTIVE_FOUND
;
2657 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2658 defining
->next
= *mmhead
;
2661 free_tlist(origline
);
2662 return DIRECTIVE_FOUND
;
2666 * We must search along istk->expansion until we hit a
2667 * macro-end marker for a macro with a name. Then we set
2668 * its `in_progress' flag to 0.
2670 for (l
= istk
->expansion
; l
; l
= l
->next
)
2671 if (l
->finishes
&& l
->finishes
->name
)
2675 l
->finishes
->in_progress
= 0;
2677 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2679 free_tlist(origline
);
2680 return DIRECTIVE_FOUND
;
2688 spec
.casesense
= (i
== PP_UNMACRO
);
2689 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2690 return DIRECTIVE_FOUND
;
2692 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2693 while (mmac_p
&& *mmac_p
) {
2695 if (mmac
->casesense
== spec
.casesense
&&
2696 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2697 mmac
->nparam_min
== spec
.nparam_min
&&
2698 mmac
->nparam_max
== spec
.nparam_max
&&
2699 mmac
->plus
== spec
.plus
) {
2700 *mmac_p
= mmac
->next
;
2703 mmac_p
= &mmac
->next
;
2706 free_tlist(origline
);
2707 free_tlist(spec
.dlist
);
2708 return DIRECTIVE_FOUND
;
2712 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2713 tline
= tline
->next
;
2715 free_tlist(origline
);
2716 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2717 return DIRECTIVE_FOUND
;
2719 t
= expand_smacro(tline
->next
);
2721 free_tlist(origline
);
2724 tokval
.t_type
= TOKEN_INVALID
;
2726 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2729 return DIRECTIVE_FOUND
;
2731 error(ERR_WARNING
|ERR_PASS1
,
2732 "trailing garbage after expression ignored");
2733 if (!is_simple(evalresult
)) {
2734 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2735 return DIRECTIVE_FOUND
;
2738 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2739 mmac
= mmac
->next_active
;
2741 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2742 } else if (mmac
->nparam
== 0) {
2744 "`%%rotate' invoked within macro without parameters");
2746 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2748 rotate
%= (int)mmac
->nparam
;
2750 rotate
+= mmac
->nparam
;
2752 mmac
->rotate
= rotate
;
2754 return DIRECTIVE_FOUND
;
2759 tline
= tline
->next
;
2760 } while (tok_type_(tline
, TOK_WHITESPACE
));
2762 if (tok_type_(tline
, TOK_ID
) &&
2763 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2766 tline
= tline
->next
;
2767 } while (tok_type_(tline
, TOK_WHITESPACE
));
2771 t
= expand_smacro(tline
);
2773 tokval
.t_type
= TOKEN_INVALID
;
2775 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2777 free_tlist(origline
);
2778 return DIRECTIVE_FOUND
;
2781 error(ERR_WARNING
|ERR_PASS1
,
2782 "trailing garbage after expression ignored");
2783 if (!is_simple(evalresult
)) {
2784 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2785 return DIRECTIVE_FOUND
;
2787 count
= reloc_value(evalresult
) + 1;
2789 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2792 free_tlist(origline
);
2794 tmp_defining
= defining
;
2795 defining
= nasm_malloc(sizeof(MMacro
));
2796 defining
->prev
= NULL
;
2797 defining
->name
= NULL
; /* flags this macro as a %rep block */
2798 defining
->casesense
= false;
2799 defining
->plus
= false;
2800 defining
->nolist
= nolist
;
2801 defining
->in_progress
= count
;
2802 defining
->max_depth
= 0;
2803 defining
->nparam_min
= defining
->nparam_max
= 0;
2804 defining
->defaults
= NULL
;
2805 defining
->dlist
= NULL
;
2806 defining
->expansion
= NULL
;
2807 defining
->next_active
= istk
->mstk
;
2808 defining
->rep_nest
= tmp_defining
;
2809 return DIRECTIVE_FOUND
;
2812 if (!defining
|| defining
->name
) {
2813 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2814 return DIRECTIVE_FOUND
;
2818 * Now we have a "macro" defined - although it has no name
2819 * and we won't be entering it in the hash tables - we must
2820 * push a macro-end marker for it on to istk->expansion.
2821 * After that, it will take care of propagating itself (a
2822 * macro-end marker line for a macro which is really a %rep
2823 * block will cause the macro to be re-expanded, complete
2824 * with another macro-end marker to ensure the process
2825 * continues) until the whole expansion is forcibly removed
2826 * from istk->expansion by a %exitrep.
2828 l
= nasm_malloc(sizeof(Line
));
2829 l
->next
= istk
->expansion
;
2830 l
->finishes
= defining
;
2832 istk
->expansion
= l
;
2834 istk
->mstk
= defining
;
2836 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2837 tmp_defining
= defining
;
2838 defining
= defining
->rep_nest
;
2839 free_tlist(origline
);
2840 return DIRECTIVE_FOUND
;
2844 * We must search along istk->expansion until we hit a
2845 * macro-end marker for a macro with no name. Then we set
2846 * its `in_progress' flag to 0.
2848 for (l
= istk
->expansion
; l
; l
= l
->next
)
2849 if (l
->finishes
&& !l
->finishes
->name
)
2853 l
->finishes
->in_progress
= 1;
2855 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2856 free_tlist(origline
);
2857 return DIRECTIVE_FOUND
;
2863 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2865 tline
= tline
->next
;
2867 tline
= expand_id(tline
);
2868 if (!tline
|| (tline
->type
!= TOK_ID
&&
2869 (tline
->type
!= TOK_PREPROC_ID
||
2870 tline
->text
[1] != '$'))) {
2871 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2873 free_tlist(origline
);
2874 return DIRECTIVE_FOUND
;
2877 ctx
= get_ctx(tline
->text
, &mname
, false);
2879 param_start
= tline
= tline
->next
;
2882 /* Expand the macro definition now for %xdefine and %ixdefine */
2883 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2884 tline
= expand_smacro(tline
);
2886 if (tok_is_(tline
, "(")) {
2888 * This macro has parameters.
2891 tline
= tline
->next
;
2895 error(ERR_NONFATAL
, "parameter identifier expected");
2896 free_tlist(origline
);
2897 return DIRECTIVE_FOUND
;
2899 if (tline
->type
!= TOK_ID
) {
2901 "`%s': parameter identifier expected",
2903 free_tlist(origline
);
2904 return DIRECTIVE_FOUND
;
2906 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2907 tline
= tline
->next
;
2909 if (tok_is_(tline
, ",")) {
2910 tline
= tline
->next
;
2912 if (!tok_is_(tline
, ")")) {
2914 "`)' expected to terminate macro template");
2915 free_tlist(origline
);
2916 return DIRECTIVE_FOUND
;
2922 tline
= tline
->next
;
2924 if (tok_type_(tline
, TOK_WHITESPACE
))
2925 last
= tline
, tline
= tline
->next
;
2930 if (t
->type
== TOK_ID
) {
2931 for (tt
= param_start
; tt
; tt
= tt
->next
)
2932 if (tt
->type
>= TOK_SMAC_PARAM
&&
2933 !strcmp(tt
->text
, t
->text
))
2937 t
->next
= macro_start
;
2942 * Good. We now have a macro name, a parameter count, and a
2943 * token list (in reverse order) for an expansion. We ought
2944 * to be OK just to create an SMacro, store it, and let
2945 * free_tlist have the rest of the line (which we have
2946 * carefully re-terminated after chopping off the expansion
2949 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2950 free_tlist(origline
);
2951 return DIRECTIVE_FOUND
;
2954 tline
= tline
->next
;
2956 tline
= expand_id(tline
);
2957 if (!tline
|| (tline
->type
!= TOK_ID
&&
2958 (tline
->type
!= TOK_PREPROC_ID
||
2959 tline
->text
[1] != '$'))) {
2960 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2961 free_tlist(origline
);
2962 return DIRECTIVE_FOUND
;
2965 error(ERR_WARNING
|ERR_PASS1
,
2966 "trailing garbage after macro name ignored");
2969 /* Find the context that symbol belongs to */
2970 ctx
= get_ctx(tline
->text
, &mname
, false);
2971 undef_smacro(ctx
, mname
);
2972 free_tlist(origline
);
2973 return DIRECTIVE_FOUND
;
2977 casesense
= (i
== PP_DEFSTR
);
2979 tline
= tline
->next
;
2981 tline
= expand_id(tline
);
2982 if (!tline
|| (tline
->type
!= TOK_ID
&&
2983 (tline
->type
!= TOK_PREPROC_ID
||
2984 tline
->text
[1] != '$'))) {
2985 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2987 free_tlist(origline
);
2988 return DIRECTIVE_FOUND
;
2991 ctx
= get_ctx(tline
->text
, &mname
, false);
2993 tline
= expand_smacro(tline
->next
);
2996 while (tok_type_(tline
, TOK_WHITESPACE
))
2997 tline
= delete_Token(tline
);
2999 p
= detoken(tline
, false);
3000 macro_start
= nasm_malloc(sizeof(*macro_start
));
3001 macro_start
->next
= NULL
;
3002 macro_start
->text
= nasm_quote(p
, strlen(p
));
3003 macro_start
->type
= TOK_STRING
;
3004 macro_start
->a
.mac
= NULL
;
3008 * We now have a macro name, an implicit parameter count of
3009 * zero, and a string token to use as an expansion. Create
3010 * and store an SMacro.
3012 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3013 free_tlist(origline
);
3014 return DIRECTIVE_FOUND
;
3018 casesense
= (i
== PP_DEFTOK
);
3020 tline
= tline
->next
;
3022 tline
= expand_id(tline
);
3023 if (!tline
|| (tline
->type
!= TOK_ID
&&
3024 (tline
->type
!= TOK_PREPROC_ID
||
3025 tline
->text
[1] != '$'))) {
3027 "`%s' expects a macro identifier as first parameter",
3029 free_tlist(origline
);
3030 return DIRECTIVE_FOUND
;
3032 ctx
= get_ctx(tline
->text
, &mname
, false);
3034 tline
= expand_smacro(tline
->next
);
3038 while (tok_type_(t
, TOK_WHITESPACE
))
3040 /* t should now point to the string */
3041 if (t
->type
!= TOK_STRING
) {
3043 "`%s` requires string as second parameter",
3046 free_tlist(origline
);
3047 return DIRECTIVE_FOUND
;
3050 nasm_unquote_cstr(t
->text
, i
);
3051 macro_start
= tokenize(t
->text
);
3054 * We now have a macro name, an implicit parameter count of
3055 * zero, and a numeric token to use as an expansion. Create
3056 * and store an SMacro.
3058 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3060 free_tlist(origline
);
3061 return DIRECTIVE_FOUND
;
3066 StrList
*xsl
= NULL
;
3067 StrList
**xst
= &xsl
;
3071 tline
= tline
->next
;
3073 tline
= expand_id(tline
);
3074 if (!tline
|| (tline
->type
!= TOK_ID
&&
3075 (tline
->type
!= TOK_PREPROC_ID
||
3076 tline
->text
[1] != '$'))) {
3078 "`%%pathsearch' expects a macro identifier as first parameter");
3079 free_tlist(origline
);
3080 return DIRECTIVE_FOUND
;
3082 ctx
= get_ctx(tline
->text
, &mname
, false);
3084 tline
= expand_smacro(tline
->next
);
3088 while (tok_type_(t
, TOK_WHITESPACE
))
3091 if (!t
|| (t
->type
!= TOK_STRING
&&
3092 t
->type
!= TOK_INTERNAL_STRING
)) {
3093 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3095 free_tlist(origline
);
3096 return DIRECTIVE_FOUND
; /* but we did _something_ */
3099 error(ERR_WARNING
|ERR_PASS1
,
3100 "trailing garbage after `%%pathsearch' ignored");
3102 if (t
->type
!= TOK_INTERNAL_STRING
)
3103 nasm_unquote(p
, NULL
);
3105 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3108 fclose(fp
); /* Don't actually care about the file */
3110 macro_start
= nasm_malloc(sizeof(*macro_start
));
3111 macro_start
->next
= NULL
;
3112 macro_start
->text
= nasm_quote(p
, strlen(p
));
3113 macro_start
->type
= TOK_STRING
;
3114 macro_start
->a
.mac
= NULL
;
3119 * We now have a macro name, an implicit parameter count of
3120 * zero, and a string token to use as an expansion. Create
3121 * and store an SMacro.
3123 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3125 free_tlist(origline
);
3126 return DIRECTIVE_FOUND
;
3132 tline
= tline
->next
;
3134 tline
= expand_id(tline
);
3135 if (!tline
|| (tline
->type
!= TOK_ID
&&
3136 (tline
->type
!= TOK_PREPROC_ID
||
3137 tline
->text
[1] != '$'))) {
3139 "`%%strlen' expects a macro identifier as first parameter");
3140 free_tlist(origline
);
3141 return DIRECTIVE_FOUND
;
3143 ctx
= get_ctx(tline
->text
, &mname
, false);
3145 tline
= expand_smacro(tline
->next
);
3149 while (tok_type_(t
, TOK_WHITESPACE
))
3151 /* t should now point to the string */
3152 if (t
->type
!= TOK_STRING
) {
3154 "`%%strlen` requires string as second parameter");
3156 free_tlist(origline
);
3157 return DIRECTIVE_FOUND
;
3160 macro_start
= nasm_malloc(sizeof(*macro_start
));
3161 macro_start
->next
= NULL
;
3162 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3163 macro_start
->a
.mac
= NULL
;
3166 * We now have a macro name, an implicit parameter count of
3167 * zero, and a numeric token to use as an expansion. Create
3168 * and store an SMacro.
3170 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3172 free_tlist(origline
);
3173 return DIRECTIVE_FOUND
;
3178 tline
= tline
->next
;
3180 tline
= expand_id(tline
);
3181 if (!tline
|| (tline
->type
!= TOK_ID
&&
3182 (tline
->type
!= TOK_PREPROC_ID
||
3183 tline
->text
[1] != '$'))) {
3185 "`%%strcat' expects a macro identifier as first parameter");
3186 free_tlist(origline
);
3187 return DIRECTIVE_FOUND
;
3189 ctx
= get_ctx(tline
->text
, &mname
, false);
3191 tline
= expand_smacro(tline
->next
);
3195 for (t
= tline
; t
; t
= t
->next
) {
3197 case TOK_WHITESPACE
:
3200 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3203 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3205 /* else fall through */
3208 "non-string passed to `%%strcat' (%d)", t
->type
);
3210 free_tlist(origline
);
3211 return DIRECTIVE_FOUND
;
3215 p
= pp
= nasm_malloc(len
);
3217 for (t
= tline
; t
; t
= t
->next
) {
3218 if (t
->type
== TOK_STRING
) {
3219 memcpy(p
, t
->text
, t
->a
.len
);
3225 * We now have a macro name, an implicit parameter count of
3226 * zero, and a numeric token to use as an expansion. Create
3227 * and store an SMacro.
3229 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3230 macro_start
->text
= nasm_quote(pp
, len
);
3232 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3234 free_tlist(origline
);
3235 return DIRECTIVE_FOUND
;
3244 tline
= tline
->next
;
3246 tline
= expand_id(tline
);
3247 if (!tline
|| (tline
->type
!= TOK_ID
&&
3248 (tline
->type
!= TOK_PREPROC_ID
||
3249 tline
->text
[1] != '$'))) {
3251 "`%%substr' expects a macro identifier as first parameter");
3252 free_tlist(origline
);
3253 return DIRECTIVE_FOUND
;
3255 ctx
= get_ctx(tline
->text
, &mname
, false);
3257 tline
= expand_smacro(tline
->next
);
3261 while (tok_type_(t
, TOK_WHITESPACE
))
3264 /* t should now point to the string */
3265 if (t
->type
!= TOK_STRING
) {
3267 "`%%substr` requires string as second parameter");
3269 free_tlist(origline
);
3270 return DIRECTIVE_FOUND
;
3275 tokval
.t_type
= TOKEN_INVALID
;
3276 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3280 free_tlist(origline
);
3281 return DIRECTIVE_FOUND
;
3282 } else if (!is_simple(evalresult
)) {
3283 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3285 free_tlist(origline
);
3286 return DIRECTIVE_FOUND
;
3288 a1
= evalresult
->value
-1;
3290 while (tok_type_(tt
, TOK_WHITESPACE
))
3293 a2
= 1; /* Backwards compatibility: one character */
3295 tokval
.t_type
= TOKEN_INVALID
;
3296 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3300 free_tlist(origline
);
3301 return DIRECTIVE_FOUND
;
3302 } else if (!is_simple(evalresult
)) {
3303 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3305 free_tlist(origline
);
3306 return DIRECTIVE_FOUND
;
3308 a2
= evalresult
->value
;
3311 len
= nasm_unquote(t
->text
, NULL
);
3314 if (a1
+a2
> (int64_t)len
)
3317 macro_start
= nasm_malloc(sizeof(*macro_start
));
3318 macro_start
->next
= NULL
;
3319 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3320 macro_start
->type
= TOK_STRING
;
3321 macro_start
->a
.mac
= NULL
;
3324 * We now have a macro name, an implicit parameter count of
3325 * zero, and a numeric token to use as an expansion. Create
3326 * and store an SMacro.
3328 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3330 free_tlist(origline
);
3331 return DIRECTIVE_FOUND
;
3336 casesense
= (i
== PP_ASSIGN
);
3338 tline
= tline
->next
;
3340 tline
= expand_id(tline
);
3341 if (!tline
|| (tline
->type
!= TOK_ID
&&
3342 (tline
->type
!= TOK_PREPROC_ID
||
3343 tline
->text
[1] != '$'))) {
3345 "`%%%sassign' expects a macro identifier",
3346 (i
== PP_IASSIGN
? "i" : ""));
3347 free_tlist(origline
);
3348 return DIRECTIVE_FOUND
;
3350 ctx
= get_ctx(tline
->text
, &mname
, false);
3352 tline
= expand_smacro(tline
->next
);
3357 tokval
.t_type
= TOKEN_INVALID
;
3359 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3362 free_tlist(origline
);
3363 return DIRECTIVE_FOUND
;
3367 error(ERR_WARNING
|ERR_PASS1
,
3368 "trailing garbage after expression ignored");
3370 if (!is_simple(evalresult
)) {
3372 "non-constant value given to `%%%sassign'",
3373 (i
== PP_IASSIGN
? "i" : ""));
3374 free_tlist(origline
);
3375 return DIRECTIVE_FOUND
;
3378 macro_start
= nasm_malloc(sizeof(*macro_start
));
3379 macro_start
->next
= NULL
;
3380 make_tok_num(macro_start
, reloc_value(evalresult
));
3381 macro_start
->a
.mac
= NULL
;
3384 * We now have a macro name, an implicit parameter count of
3385 * zero, and a numeric token to use as an expansion. Create
3386 * and store an SMacro.
3388 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3389 free_tlist(origline
);
3390 return DIRECTIVE_FOUND
;
3394 * Syntax is `%line nnn[+mmm] [filename]'
3396 tline
= tline
->next
;
3398 if (!tok_type_(tline
, TOK_NUMBER
)) {
3399 error(ERR_NONFATAL
, "`%%line' expects line number");
3400 free_tlist(origline
);
3401 return DIRECTIVE_FOUND
;
3403 k
= readnum(tline
->text
, &err
);
3405 tline
= tline
->next
;
3406 if (tok_is_(tline
, "+")) {
3407 tline
= tline
->next
;
3408 if (!tok_type_(tline
, TOK_NUMBER
)) {
3409 error(ERR_NONFATAL
, "`%%line' expects line increment");
3410 free_tlist(origline
);
3411 return DIRECTIVE_FOUND
;
3413 m
= readnum(tline
->text
, &err
);
3414 tline
= tline
->next
;
3420 nasm_free(src_set_fname(detoken(tline
, false)));
3422 free_tlist(origline
);
3423 return DIRECTIVE_FOUND
;
3427 "preprocessor directive `%s' not yet implemented",
3429 return DIRECTIVE_FOUND
;
3434 * Ensure that a macro parameter contains a condition code and
3435 * nothing else. Return the condition code index if so, or -1
3438 static int find_cc(Token
* t
)
3444 return -1; /* Probably a %+ without a space */
3447 if (t
->type
!= TOK_ID
)
3451 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3455 j
= elements(conditions
);
3458 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3473 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3475 Token
**tail
, *t
, *tt
;
3477 bool did_paste
= false;
3480 /* Now handle token pasting... */
3483 while ((t
= *tail
) && (tt
= t
->next
)) {
3485 case TOK_WHITESPACE
:
3486 if (tt
->type
== TOK_WHITESPACE
) {
3487 /* Zap adjacent whitespace tokens */
3488 t
->next
= delete_Token(tt
);
3490 /* Do not advance paste_head here */
3495 case TOK_PREPROC_ID
:
3502 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3503 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3504 tt
->type
== TOK_OTHER
)) {
3505 len
+= strlen(tt
->text
);
3509 /* Now tt points to the first token after the potential
3511 if (tt
!= t
->next
) {
3512 /* We have at least two tokens... */
3513 len
+= strlen(t
->text
);
3514 p
= tmp
= nasm_malloc(len
+1);
3518 p
= strchr(p
, '\0');
3519 t
= delete_Token(t
);
3522 t
= *tail
= tokenize(tmp
);
3529 t
->next
= tt
; /* Attach the remaining token chain */
3537 case TOK_PASTE
: /* %+ */
3538 if (handle_paste_tokens
) {
3539 /* Zap %+ and whitespace tokens to the right */
3540 while (t
&& (t
->type
== TOK_WHITESPACE
||
3541 t
->type
== TOK_PASTE
))
3542 t
= *tail
= delete_Token(t
);
3543 if (!paste_head
|| !t
)
3544 break; /* Nothing to paste with */
3548 while (tok_type_(tt
, TOK_WHITESPACE
))
3549 tt
= t
->next
= delete_Token(tt
);
3552 tmp
= nasm_strcat(t
->text
, tt
->text
);
3554 tt
= delete_Token(tt
);
3555 t
= *tail
= tokenize(tmp
);
3561 t
->next
= tt
; /* Attach the remaining token chain */
3568 /* else fall through */
3570 tail
= paste_head
= &t
->next
;
3577 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3578 * %-n) and MMacro-local identifiers (%%foo) as well as
3579 * macro indirection (%[...]).
3581 static Token
*expand_mmac_params(Token
* tline
)
3583 Token
*t
, *tt
, **tail
, *thead
;
3584 bool changed
= false;
3590 if (tline
->type
== TOK_PREPROC_ID
&&
3591 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3592 && tline
->text
[2]) || tline
->text
[1] == '%'
3593 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3595 int type
= 0, cc
; /* type = 0 to placate optimisers */
3602 tline
= tline
->next
;
3605 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3606 mac
= mac
->next_active
;
3608 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3610 switch (t
->text
[1]) {
3612 * We have to make a substitution of one of the
3613 * forms %1, %-1, %+1, %%foo, %0.
3617 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3618 text
= nasm_strdup(tmpbuf
);
3622 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3624 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3627 n
= atoi(t
->text
+ 2) - 1;
3628 if (n
>= mac
->nparam
)
3631 if (mac
->nparam
> 1)
3632 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3633 tt
= mac
->params
[n
];
3638 "macro parameter %d is not a condition code",
3643 if (inverse_ccs
[cc
] == -1) {
3645 "condition code `%s' is not invertible",
3649 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3653 n
= atoi(t
->text
+ 2) - 1;
3654 if (n
>= mac
->nparam
)
3657 if (mac
->nparam
> 1)
3658 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3659 tt
= mac
->params
[n
];
3664 "macro parameter %d is not a condition code",
3669 text
= nasm_strdup(conditions
[cc
]);
3673 n
= atoi(t
->text
+ 1) - 1;
3674 if (n
>= mac
->nparam
)
3677 if (mac
->nparam
> 1)
3678 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3679 tt
= mac
->params
[n
];
3682 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3683 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3684 tail
= &(*tail
)->next
;
3688 text
= NULL
; /* we've done it here */
3703 } else if (tline
->type
== TOK_INDIRECT
) {
3705 tline
= tline
->next
;
3706 tt
= tokenize(t
->text
);
3707 tt
= expand_mmac_params(tt
);
3708 tt
= expand_smacro(tt
);
3711 tt
->a
.mac
= NULL
; /* Necessary? */
3719 tline
= tline
->next
;
3727 paste_tokens(&thead
, false);
3733 * Expand all single-line macro calls made in the given line.
3734 * Return the expanded version of the line. The original is deemed
3735 * to be destroyed in the process. (In reality we'll just move
3736 * Tokens from input to output a lot of the time, rather than
3737 * actually bothering to destroy and replicate.)
3740 static Token
*expand_smacro(Token
* tline
)
3742 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3743 struct hash_table
*smtbl
;
3744 SMacro
*head
= NULL
, *m
;
3747 unsigned int nparam
, sparam
;
3749 Token
*org_tline
= tline
;
3752 int deadman
= DEADMAN_LIMIT
;
3756 * Trick: we should avoid changing the start token pointer since it can
3757 * be contained in "next" field of other token. Because of this
3758 * we allocate a copy of first token and work with it; at the end of
3759 * routine we copy it back
3763 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3765 tline
->a
.mac
= org_tline
->a
.mac
;
3766 nasm_free(org_tline
->text
);
3767 org_tline
->text
= NULL
;
3770 expanded
= true; /* Always expand %+ at least once */
3776 while (tline
) { /* main token loop */
3778 error(ERR_NONFATAL
, "interminable macro recursion");
3782 if ((mname
= tline
->text
)) {
3783 /* if this token is a local macro, look in local context */
3784 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3785 ctx
= get_ctx(mname
, &mname
, true);
3788 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3789 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3792 * We've hit an identifier. As in is_mmacro below, we first
3793 * check whether the identifier is a single-line macro at
3794 * all, then think about checking for parameters if
3797 for (m
= head
; m
; m
= m
->next
)
3798 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3804 if (m
->nparam
== 0) {
3806 * Simple case: the macro is parameterless. Discard the
3807 * one token that the macro call took, and push the
3808 * expansion back on the to-do stack.
3810 if (!m
->expansion
) {
3811 if (!strcmp("__FILE__", m
->name
)) {
3814 src_get(&num
, &file
);
3815 tline
->text
= nasm_quote(file
, strlen(file
));
3816 tline
->type
= TOK_STRING
;
3820 if (!strcmp("__LINE__", m
->name
)) {
3821 nasm_free(tline
->text
);
3822 make_tok_num(tline
, src_get_linnum());
3825 if (!strcmp("__BITS__", m
->name
)) {
3826 nasm_free(tline
->text
);
3827 make_tok_num(tline
, globalbits
);
3830 tline
= delete_Token(tline
);
3835 * Complicated case: at least one macro with this name
3836 * exists and takes parameters. We must find the
3837 * parameters in the call, count them, find the SMacro
3838 * that corresponds to that form of the macro call, and
3839 * substitute for the parameters when we expand. What a
3842 /*tline = tline->next;
3843 skip_white_(tline); */
3846 while (tok_type_(t
, TOK_SMAC_END
)) {
3847 t
->a
.mac
->in_progress
= false;
3849 t
= tline
->next
= delete_Token(t
);
3852 } while (tok_type_(tline
, TOK_WHITESPACE
));
3853 if (!tok_is_(tline
, "(")) {
3855 * This macro wasn't called with parameters: ignore
3856 * the call. (Behaviour borrowed from gnu cpp.)
3865 sparam
= PARAM_DELTA
;
3866 params
= nasm_malloc(sparam
* sizeof(Token
*));
3867 params
[0] = tline
->next
;
3868 paramsize
= nasm_malloc(sparam
* sizeof(int));
3870 while (true) { /* parameter loop */
3872 * For some unusual expansions
3873 * which concatenates function call
3876 while (tok_type_(t
, TOK_SMAC_END
)) {
3877 t
->a
.mac
->in_progress
= false;
3879 t
= tline
->next
= delete_Token(t
);
3885 "macro call expects terminating `)'");
3888 if (tline
->type
== TOK_WHITESPACE
3890 if (paramsize
[nparam
])
3893 params
[nparam
] = tline
->next
;
3894 continue; /* parameter loop */
3896 if (tline
->type
== TOK_OTHER
3897 && tline
->text
[1] == 0) {
3898 char ch
= tline
->text
[0];
3899 if (ch
== ',' && !paren
&& brackets
<= 0) {
3900 if (++nparam
>= sparam
) {
3901 sparam
+= PARAM_DELTA
;
3902 params
= nasm_realloc(params
,
3907 nasm_realloc(paramsize
,
3911 params
[nparam
] = tline
->next
;
3912 paramsize
[nparam
] = 0;
3914 continue; /* parameter loop */
3917 (brackets
> 0 || (brackets
== 0 &&
3918 !paramsize
[nparam
])))
3920 if (!(brackets
++)) {
3921 params
[nparam
] = tline
->next
;
3922 continue; /* parameter loop */
3925 if (ch
== '}' && brackets
> 0)
3926 if (--brackets
== 0) {
3928 continue; /* parameter loop */
3930 if (ch
== '(' && !brackets
)
3932 if (ch
== ')' && brackets
<= 0)
3938 error(ERR_NONFATAL
, "braces do not "
3939 "enclose all of macro parameter");
3941 paramsize
[nparam
] += white
+ 1;
3943 } /* parameter loop */
3945 while (m
&& (m
->nparam
!= nparam
||
3946 mstrcmp(m
->name
, mname
,
3950 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3951 "macro `%s' exists, "
3952 "but not taking %d parameters",
3953 mstart
->text
, nparam
);
3956 if (m
&& m
->in_progress
)
3958 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3960 * Design question: should we handle !tline, which
3961 * indicates missing ')' here, or expand those
3962 * macros anyway, which requires the (t) test a few
3966 nasm_free(paramsize
);
3970 * Expand the macro: we are placed on the last token of the
3971 * call, so that we can easily split the call from the
3972 * following tokens. We also start by pushing an SMAC_END
3973 * token for the cycle removal.
3980 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3982 m
->in_progress
= true;
3984 for (t
= m
->expansion
; t
; t
= t
->next
) {
3985 if (t
->type
>= TOK_SMAC_PARAM
) {
3986 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3990 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3991 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3994 new_Token(tline
, ttt
->type
, ttt
->text
,
4000 } else if (t
->type
== TOK_PREPROC_Q
) {
4001 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4003 } else if (t
->type
== TOK_PREPROC_QQ
) {
4004 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4007 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4013 * Having done that, get rid of the macro call, and clean
4014 * up the parameters.
4017 nasm_free(paramsize
);
4020 continue; /* main token loop */
4025 if (tline
->type
== TOK_SMAC_END
) {
4026 tline
->a
.mac
->in_progress
= false;
4027 tline
= delete_Token(tline
);
4030 tline
= tline
->next
;
4038 * Now scan the entire line and look for successive TOK_IDs that resulted
4039 * after expansion (they can't be produced by tokenize()). The successive
4040 * TOK_IDs should be concatenated.
4041 * Also we look for %+ tokens and concatenate the tokens before and after
4042 * them (without white spaces in between).
4044 if (expanded
&& paste_tokens(&thead
, true)) {
4046 * If we concatenated something, *and* we had previously expanded
4047 * an actual macro, scan the lines again for macros...
4056 *org_tline
= *thead
;
4057 /* since we just gave text to org_line, don't free it */
4059 delete_Token(thead
);
4061 /* the expression expanded to empty line;
4062 we can't return NULL for some reasons
4063 we just set the line to a single WHITESPACE token. */
4064 memset(org_tline
, 0, sizeof(*org_tline
));
4065 org_tline
->text
= NULL
;
4066 org_tline
->type
= TOK_WHITESPACE
;
4075 * Similar to expand_smacro but used exclusively with macro identifiers
4076 * right before they are fetched in. The reason is that there can be
4077 * identifiers consisting of several subparts. We consider that if there
4078 * are more than one element forming the name, user wants a expansion,
4079 * otherwise it will be left as-is. Example:
4083 * the identifier %$abc will be left as-is so that the handler for %define
4084 * will suck it and define the corresponding value. Other case:
4086 * %define _%$abc cde
4088 * In this case user wants name to be expanded *before* %define starts
4089 * working, so we'll expand %$abc into something (if it has a value;
4090 * otherwise it will be left as-is) then concatenate all successive
4093 static Token
*expand_id(Token
* tline
)
4095 Token
*cur
, *oldnext
= NULL
;
4097 if (!tline
|| !tline
->next
)
4102 (cur
->next
->type
== TOK_ID
||
4103 cur
->next
->type
== TOK_PREPROC_ID
4104 || cur
->next
->type
== TOK_NUMBER
))
4107 /* If identifier consists of just one token, don't expand */
4112 oldnext
= cur
->next
; /* Detach the tail past identifier */
4113 cur
->next
= NULL
; /* so that expand_smacro stops here */
4116 tline
= expand_smacro(tline
);
4119 /* expand_smacro possibly changhed tline; re-scan for EOL */
4121 while (cur
&& cur
->next
)
4124 cur
->next
= oldnext
;
4131 * Determine whether the given line constitutes a multi-line macro
4132 * call, and return the MMacro structure called if so. Doesn't have
4133 * to check for an initial label - that's taken care of in
4134 * expand_mmacro - but must check numbers of parameters. Guaranteed
4135 * to be called with tline->type == TOK_ID, so the putative macro
4136 * name is easy to find.
4138 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4144 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4147 * Efficiency: first we see if any macro exists with the given
4148 * name. If not, we can return NULL immediately. _Then_ we
4149 * count the parameters, and then we look further along the
4150 * list if necessary to find the proper MMacro.
4152 for (m
= head
; m
; m
= m
->next
)
4153 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4159 * OK, we have a potential macro. Count and demarcate the
4162 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4165 * So we know how many parameters we've got. Find the MMacro
4166 * structure that handles this number.
4169 if (m
->nparam_min
<= nparam
4170 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4172 * This one is right. Just check if cycle removal
4173 * prohibits us using it before we actually celebrate...
4175 if (m
->in_progress
> m
->max_depth
) {
4176 if (m
->max_depth
> 0) {
4178 "reached maximum recursion depth of %i",
4185 * It's right, and we can use it. Add its default
4186 * parameters to the end of our list if necessary.
4188 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4190 nasm_realloc(params
,
4191 ((m
->nparam_min
+ m
->ndefs
+
4192 1) * sizeof(*params
)));
4193 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4194 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4199 * If we've gone over the maximum parameter count (and
4200 * we're in Plus mode), ignore parameters beyond
4203 if (m
->plus
&& nparam
> m
->nparam_max
)
4204 nparam
= m
->nparam_max
;
4206 * Then terminate the parameter list, and leave.
4208 if (!params
) { /* need this special case */
4209 params
= nasm_malloc(sizeof(*params
));
4212 params
[nparam
] = NULL
;
4213 *params_array
= params
;
4217 * This one wasn't right: look for the next one with the
4220 for (m
= m
->next
; m
; m
= m
->next
)
4221 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4226 * After all that, we didn't find one with the right number of
4227 * parameters. Issue a warning, and fail to expand the macro.
4229 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4230 "macro `%s' exists, but not taking %d parameters",
4231 tline
->text
, nparam
);
4238 * Save MMacro invocation specific fields in
4239 * preparation for a recursive macro expansion
4241 static void push_mmacro(MMacro
*m
)
4243 MMacroInvocation
*i
;
4245 i
= nasm_malloc(sizeof(MMacroInvocation
));
4247 i
->params
= m
->params
;
4248 i
->iline
= m
->iline
;
4249 i
->nparam
= m
->nparam
;
4250 i
->rotate
= m
->rotate
;
4251 i
->paramlen
= m
->paramlen
;
4252 i
->unique
= m
->unique
;
4258 * Restore MMacro invocation specific fields that were
4259 * saved during a previous recursive macro expansion
4261 static void pop_mmacro(MMacro
*m
)
4263 MMacroInvocation
*i
;
4268 m
->params
= i
->params
;
4269 m
->iline
= i
->iline
;
4270 m
->nparam
= i
->nparam
;
4271 m
->rotate
= i
->rotate
;
4272 m
->paramlen
= i
->paramlen
;
4273 m
->unique
= i
->unique
;
4280 * Expand the multi-line macro call made by the given line, if
4281 * there is one to be expanded. If there is, push the expansion on
4282 * istk->expansion and return 1. Otherwise return 0.
4284 static int expand_mmacro(Token
* tline
)
4286 Token
*startline
= tline
;
4287 Token
*label
= NULL
;
4288 int dont_prepend
= 0;
4289 Token
**params
, *t
, *mtok
, *tt
;
4292 int i
, nparam
, *paramlen
;
4297 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4298 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4301 m
= is_mmacro(t
, ¶ms
);
4307 * We have an id which isn't a macro call. We'll assume
4308 * it might be a label; we'll also check to see if a
4309 * colon follows it. Then, if there's another id after
4310 * that lot, we'll check it again for macro-hood.
4314 if (tok_type_(t
, TOK_WHITESPACE
))
4315 last
= t
, t
= t
->next
;
4316 if (tok_is_(t
, ":")) {
4318 last
= t
, t
= t
->next
;
4319 if (tok_type_(t
, TOK_WHITESPACE
))
4320 last
= t
, t
= t
->next
;
4322 if (!tok_type_(t
, TOK_ID
) || !(m
= is_mmacro(t
, ¶ms
)))
4330 * Fix up the parameters: this involves stripping leading and
4331 * trailing whitespace, then stripping braces if they are
4334 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4335 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4337 for (i
= 0; params
[i
]; i
++) {
4339 int comma
= (!m
->plus
|| i
< nparam
- 1);
4343 if (tok_is_(t
, "{"))
4344 t
= t
->next
, brace
= true, comma
= false;
4348 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4349 break; /* ... because we have hit a comma */
4350 if (comma
&& t
->type
== TOK_WHITESPACE
4351 && tok_is_(t
->next
, ","))
4352 break; /* ... or a space then a comma */
4353 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4354 break; /* ... or a brace */
4361 * OK, we have a MMacro structure together with a set of
4362 * parameters. We must now go through the expansion and push
4363 * copies of each Line on to istk->expansion. Substitution of
4364 * parameter tokens and macro-local tokens doesn't get done
4365 * until the single-line macro substitution process; this is
4366 * because delaying them allows us to change the semantics
4367 * later through %rotate.
4369 * First, push an end marker on to istk->expansion, mark this
4370 * macro as in progress, and set up its invocation-specific
4373 ll
= nasm_malloc(sizeof(Line
));
4374 ll
->next
= istk
->expansion
;
4377 istk
->expansion
= ll
;
4380 * Save the previous MMacro expansion in the case of
4383 if (m
->max_depth
&& m
->in_progress
)
4391 m
->paramlen
= paramlen
;
4392 m
->unique
= unique
++;
4395 m
->next_active
= istk
->mstk
;
4398 for (l
= m
->expansion
; l
; l
= l
->next
) {
4401 ll
= nasm_malloc(sizeof(Line
));
4402 ll
->finishes
= NULL
;
4403 ll
->next
= istk
->expansion
;
4404 istk
->expansion
= ll
;
4407 for (t
= l
->first
; t
; t
= t
->next
) {
4411 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4413 case TOK_PREPROC_QQ
:
4414 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4416 case TOK_PREPROC_ID
:
4417 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4425 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4434 * If we had a label, push it on as the first line of
4435 * the macro expansion.
4438 if (dont_prepend
< 0)
4439 free_tlist(startline
);
4441 ll
= nasm_malloc(sizeof(Line
));
4442 ll
->finishes
= NULL
;
4443 ll
->next
= istk
->expansion
;
4444 istk
->expansion
= ll
;
4445 ll
->first
= startline
;
4446 if (!dont_prepend
) {
4448 label
= label
->next
;
4449 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4454 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4459 /* The function that actually does the error reporting */
4460 static void verror(int severity
, const char *fmt
, va_list arg
)
4464 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4466 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4467 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4468 istk
->mstk
->lineno
, buff
);
4470 _error(severity
, "%s", buff
);
4474 * Since preprocessor always operate only on the line that didn't
4475 * arrived yet, we should always use ERR_OFFBY1.
4477 static void error(int severity
, const char *fmt
, ...)
4481 /* If we're in a dead branch of IF or something like it, ignore the error */
4482 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4486 verror(severity
, fmt
, arg
);
4491 * Because %else etc are evaluated in the state context
4492 * of the previous branch, errors might get lost with error():
4493 * %if 0 ... %else trailing garbage ... %endif
4494 * So %else etc should report errors with this function.
4496 static void error_precond(int severity
, const char *fmt
, ...)
4500 /* Only ignore the error if it's really in a dead branch */
4501 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4505 verror(severity
, fmt
, arg
);
4510 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4511 ListGen
* listgen
, StrList
**deplist
)
4517 istk
= nasm_malloc(sizeof(Include
));
4520 istk
->expansion
= NULL
;
4522 istk
->fp
= fopen(file
, "r");
4524 src_set_fname(nasm_strdup(file
));
4528 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4531 nested_mac_count
= 0;
4532 nested_rep_count
= 0;
4535 if (tasm_compatible_mode
) {
4536 stdmacpos
= nasm_stdmac
;
4538 stdmacpos
= nasm_stdmac_after_tasm
;
4540 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4546 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4547 * The caller, however, will also pass in 3 for preprocess-only so
4548 * we can set __PASS__ accordingly.
4550 pass
= apass
> 2 ? 2 : apass
;
4552 dephead
= deptail
= deplist
;
4554 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4556 strcpy(sl
->str
, file
);
4558 deptail
= &sl
->next
;
4562 * Define the __PASS__ macro. This is defined here unlike
4563 * all the other builtins, because it is special -- it varies between
4566 t
= nasm_malloc(sizeof(*t
));
4568 make_tok_num(t
, apass
);
4570 define_smacro(NULL
, "__PASS__", true, 0, t
);
4573 static char *pp_getline(void)
4580 * Fetch a tokenized line, either from the macro-expansion
4581 * buffer or from the input file.
4584 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4585 Line
*l
= istk
->expansion
;
4586 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4590 * This is a macro-end marker for a macro with no
4591 * name, which means it's not really a macro at all
4592 * but a %rep block, and the `in_progress' field is
4593 * more than 1, meaning that we still need to
4594 * repeat. (1 means the natural last repetition; 0
4595 * means termination by %exitrep.) We have
4596 * therefore expanded up to the %endrep, and must
4597 * push the whole block on to the expansion buffer
4598 * again. We don't bother to remove the macro-end
4599 * marker: we'd only have to generate another one
4602 l
->finishes
->in_progress
--;
4603 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4604 Token
*t
, *tt
, **tail
;
4606 ll
= nasm_malloc(sizeof(Line
));
4607 ll
->next
= istk
->expansion
;
4608 ll
->finishes
= NULL
;
4612 for (t
= l
->first
; t
; t
= t
->next
) {
4613 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4615 new_Token(NULL
, t
->type
, t
->text
, 0);
4620 istk
->expansion
= ll
;
4624 * Check whether a `%rep' was started and not ended
4625 * within this macro expansion. This can happen and
4626 * should be detected. It's a fatal error because
4627 * I'm too confused to work out how to recover
4633 "defining with name in expansion");
4634 else if (istk
->mstk
->name
)
4636 "`%%rep' without `%%endrep' within"
4637 " expansion of macro `%s'",
4642 * FIXME: investigate the relationship at this point between
4643 * istk->mstk and l->finishes
4646 MMacro
*m
= istk
->mstk
;
4647 istk
->mstk
= m
->next_active
;
4650 * This was a real macro call, not a %rep, and
4651 * therefore the parameter information needs to
4656 l
->finishes
->in_progress
--;
4658 nasm_free(m
->params
);
4659 free_tlist(m
->iline
);
4660 nasm_free(m
->paramlen
);
4661 l
->finishes
->in_progress
= 0;
4666 istk
->expansion
= l
->next
;
4668 list
->downlevel(LIST_MACRO
);
4671 while (1) { /* until we get a line we can use */
4673 if (istk
->expansion
) { /* from a macro expansion */
4675 Line
*l
= istk
->expansion
;
4677 istk
->mstk
->lineno
++;
4679 istk
->expansion
= l
->next
;
4681 p
= detoken(tline
, false);
4682 list
->line(LIST_MACRO
, p
);
4687 if (line
) { /* from the current input file */
4688 line
= prepreproc(line
);
4689 tline
= tokenize(line
);
4694 * The current file has ended; work down the istk
4701 "expected `%%endif' before end of file");
4702 /* only set line and file name if there's a next node */
4704 src_set_linnum(i
->lineno
);
4705 nasm_free(src_set_fname(i
->fname
));
4708 list
->downlevel(LIST_INCLUDE
);
4712 if (istk
->expansion
&& istk
->expansion
->finishes
)
4718 * We must expand MMacro parameters and MMacro-local labels
4719 * _before_ we plunge into directive processing, to cope
4720 * with things like `%define something %1' such as STRUC
4721 * uses. Unless we're _defining_ a MMacro, in which case
4722 * those tokens should be left alone to go into the
4723 * definition; and unless we're in a non-emitting
4724 * condition, in which case we don't want to meddle with
4727 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4728 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4729 tline
= expand_mmac_params(tline
);
4733 * Check the line to see if it's a preprocessor directive.
4735 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4737 } else if (defining
) {
4739 * We're defining a multi-line macro. We emit nothing
4741 * shove the tokenized line on to the macro definition.
4743 Line
*l
= nasm_malloc(sizeof(Line
));
4744 l
->next
= defining
->expansion
;
4747 defining
->expansion
= l
;
4749 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4751 * We're in a non-emitting branch of a condition block.
4752 * Emit nothing at all, not even a blank line: when we
4753 * emerge from the condition we'll give a line-number
4754 * directive so we keep our place correctly.
4758 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4760 * We're in a %rep block which has been terminated, so
4761 * we're walking through to the %endrep without
4762 * emitting anything. Emit nothing at all, not even a
4763 * blank line: when we emerge from the %rep block we'll
4764 * give a line-number directive so we keep our place
4770 tline
= expand_smacro(tline
);
4771 if (!expand_mmacro(tline
)) {
4773 * De-tokenize the line again, and emit it.
4775 line
= detoken(tline
, true);
4779 continue; /* expand_mmacro calls free_tlist */
4787 static void pp_cleanup(int pass
)
4790 if (defining
->name
) {
4792 "end of file while still defining macro `%s'",
4795 error(ERR_NONFATAL
, "end of file while still in %%rep");
4798 free_mmacro(defining
);
4807 nasm_free(i
->fname
);
4812 nasm_free(src_set_fname(NULL
));
4817 while ((i
= ipath
)) {
4826 void pp_include_path(char *path
)
4830 i
= nasm_malloc(sizeof(IncPath
));
4831 i
->path
= path
? nasm_strdup(path
) : NULL
;
4844 void pp_pre_include(char *fname
)
4846 Token
*inc
, *space
, *name
;
4849 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4850 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4851 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4853 l
= nasm_malloc(sizeof(Line
));
4860 void pp_pre_define(char *definition
)
4866 equals
= strchr(definition
, '=');
4867 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4868 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4871 space
->next
= tokenize(definition
);
4875 l
= nasm_malloc(sizeof(Line
));
4882 void pp_pre_undefine(char *definition
)
4887 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4888 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4889 space
->next
= tokenize(definition
);
4891 l
= nasm_malloc(sizeof(Line
));
4899 * Added by Keith Kanios:
4901 * This function is used to assist with "runtime" preprocessor
4902 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4904 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4905 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4908 void pp_runtime(char *definition
)
4912 def
= tokenize(definition
);
4913 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
4918 void pp_extra_stdmac(macros_t
*macros
)
4920 extrastdmac
= macros
;
4923 static void make_tok_num(Token
* tok
, int64_t val
)
4926 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4927 tok
->text
= nasm_strdup(numbuf
);
4928 tok
->type
= TOK_NUMBER
;