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 Context Context
;
86 typedef struct Token Token
;
87 typedef struct Blocks Blocks
;
88 typedef struct Line Line
;
89 typedef struct Include Include
;
90 typedef struct Cond Cond
;
91 typedef struct IncPath IncPath
;
94 * Note on the storage of both SMacro and MMacros: the hash table
95 * indexes them case-insensitively, and we then have to go through a
96 * linked list of potential case aliases (and, for MMacros, parameter
97 * ranges); this is to preserve the matching semantics of the earlier
98 * code. If the number of case aliases for a specific macro is a
99 * performance issue, you may want to reconsider your coding style.
103 * Store the definition of a single-line macro.
115 * Store the definition of a multi-line macro. This is also used to
116 * store the interiors of `%rep...%endrep' blocks, which are
117 * effectively self-re-invoking multi-line macros which simply
118 * don't have a name or bother to appear in the hash tables. %rep
119 * blocks are signified by having a NULL `name' field.
121 * In a MMacro describing a `%rep' block, the `in_progress' field
122 * isn't merely boolean, but gives the number of repeats left to
125 * The `next' field is used for storing MMacros in hash tables; the
126 * `next_active' field is for stacking them on istk entries.
128 * When a MMacro is being expanded, `params', `iline', `nparam',
129 * `paramlen', `rotate' and `unique' are local to the invocation.
134 int nparam_min
, nparam_max
;
136 bool plus
; /* is the last parameter greedy? */
137 bool nolist
; /* is this macro listing-inhibited? */
139 Token
*dlist
; /* All defaults as one list */
140 Token
**defaults
; /* Parameter default pointers */
141 int ndefs
; /* number of default parameters */
145 MMacro
*rep_nest
; /* used for nesting %rep */
146 Token
**params
; /* actual parameters */
147 Token
*iline
; /* invocation line */
148 unsigned int nparam
, rotate
;
151 int lineno
; /* Current line number on expansion */
155 * The context stack is composed of a linked list of these.
160 struct hash_table localmac
;
165 * This is the internal form which we break input lines up into.
166 * Typically stored in linked lists.
168 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
169 * necessarily used as-is, but is intended to denote the number of
170 * the substituted parameter. So in the definition
172 * %define a(x,y) ( (x) & ~(y) )
174 * the token representing `x' will have its type changed to
175 * TOK_SMAC_PARAM, but the one representing `y' will be
178 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
179 * which doesn't need quotes around it. Used in the pre-include
180 * mechanism as an alternative to trying to find a sensible type of
181 * quote to use on the filename we were passed.
184 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
185 TOK_PREPROC_ID
, TOK_STRING
,
186 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
188 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
190 TOK_INDIRECT
, /* %[...] */
191 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
192 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
199 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
200 size_t len
; /* scratch length field */
201 } a
; /* Auxiliary data */
202 enum pp_token_type type
;
206 * Multi-line macro definitions are stored as a linked list of
207 * these, which is essentially a container to allow several linked
210 * Note that in this module, linked lists are treated as stacks
211 * wherever possible. For this reason, Lines are _pushed_ on to the
212 * `expansion' field in MMacro structures, so that the linked list,
213 * if walked, would give the macro lines in reverse order; this
214 * means that we can walk the list when expanding a macro, and thus
215 * push the lines on to the `expansion' field in _istk_ in reverse
216 * order (so that when popped back off they are in the right
217 * order). It may seem cockeyed, and it relies on my design having
218 * an even number of steps in, but it works...
220 * Some of these structures, rather than being actual lines, are
221 * markers delimiting the end of the expansion of a given macro.
222 * This is for use in the cycle-tracking and %rep-handling code.
223 * Such structures have `finishes' non-NULL, and `first' NULL. All
224 * others have `finishes' NULL, but `first' may still be NULL if
234 * To handle an arbitrary level of file inclusion, we maintain a
235 * stack (ie linked list) of these things.
244 MMacro
*mstk
; /* stack of active macros/reps */
248 * Include search path. This is simply a list of strings which get
249 * prepended, in turn, to the name of an include file, in an
250 * attempt to find the file if it's not in the current directory.
258 * Conditional assembly: we maintain a separate stack of these for
259 * each level of file inclusion. (The only reason we keep the
260 * stacks separate is to ensure that a stray `%endif' in a file
261 * included from within the true branch of a `%if' won't terminate
262 * it and cause confusion: instead, rightly, it'll cause an error.)
270 * These states are for use just after %if or %elif: IF_TRUE
271 * means the condition has evaluated to truth so we are
272 * currently emitting, whereas IF_FALSE means we are not
273 * currently emitting but will start doing so if a %else comes
274 * up. In these states, all directives are admissible: %elif,
275 * %else and %endif. (And of course %if.)
277 COND_IF_TRUE
, COND_IF_FALSE
,
279 * These states come up after a %else: ELSE_TRUE means we're
280 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
281 * any %elif or %else will cause an error.
283 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
285 * These states mean that we're not emitting now, and also that
286 * nothing until %endif will be emitted at all. COND_DONE is
287 * used when we've had our moment of emission
288 * and have now started seeing %elifs. COND_NEVER is used when
289 * the condition construct in question is contained within a
290 * non-emitting branch of a larger condition construct,
291 * or if there is an error.
293 COND_DONE
, COND_NEVER
295 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
298 * These defines are used as the possible return values for do_directive
300 #define NO_DIRECTIVE_FOUND 0
301 #define DIRECTIVE_FOUND 1
304 * Condition codes. Note that we use c_ prefix not C_ because C_ is
305 * used in nasm.h for the "real" condition codes. At _this_ level,
306 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
307 * ones, so we need a different enum...
309 static const char * const conditions
[] = {
310 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
311 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
312 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
315 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
316 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
317 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
320 static const enum pp_conds inverse_ccs
[] = {
321 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
322 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
,
323 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
329 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
330 static int is_condition(enum preproc_token arg
)
332 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
335 /* For TASM compatibility we need to be able to recognise TASM compatible
336 * conditional compilation directives. Using the NASM pre-processor does
337 * not work, so we look for them specifically from the following list and
338 * then jam in the equivalent NASM directive into the input stream.
342 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
343 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
346 static const char * const tasm_directives
[] = {
347 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
348 "ifndef", "include", "local"
351 static int StackSize
= 4;
352 static char *StackPointer
= "ebp";
353 static int ArgOffset
= 8;
354 static int LocalOffset
= 0;
356 static Context
*cstk
;
357 static Include
*istk
;
358 static IncPath
*ipath
= NULL
;
360 static efunc _error
; /* Pointer to client-provided error reporting function */
361 static evalfunc evaluate
;
363 static int pass
; /* HACK: pass 0 = generate dependencies only */
364 static StrList
**dephead
, **deptail
; /* Dependency list */
366 static uint64_t unique
; /* unique identifier numbers */
368 static Line
*predef
= NULL
;
369 static bool do_predef
;
371 static ListGen
*list
;
374 * The current set of multi-line macros we have defined.
376 static struct hash_table mmacros
;
379 * The current set of single-line macros we have defined.
381 static struct hash_table smacros
;
384 * The multi-line macro we are currently defining, or the %rep
385 * block we are currently reading, if any.
387 static MMacro
*defining
;
389 static uint64_t nested_mac_count
;
390 static uint64_t nested_rep_count
;
393 * The number of macro parameters to allocate space for at a time.
395 #define PARAM_DELTA 16
398 * The standard macro set: defined in macros.c in the array nasm_stdmac.
399 * This gives our position in the macro set, when we're processing it.
401 static macros_t
*stdmacpos
;
404 * The extra standard macros that come from the object format, if
407 static macros_t
*extrastdmac
= NULL
;
408 static bool any_extrastdmac
;
411 * Tokens are allocated in blocks to improve speed
413 #define TOKEN_BLOCKSIZE 4096
414 static Token
*freeTokens
= NULL
;
420 static Blocks blocks
= { NULL
, NULL
};
423 * Forward declarations.
425 static Token
*expand_mmac_params(Token
* tline
);
426 static Token
*expand_smacro(Token
* tline
);
427 static Token
*expand_id(Token
* tline
);
428 static Context
*get_ctx(const char *name
, const char **namep
,
430 static void make_tok_num(Token
* tok
, int64_t val
);
431 static void error(int severity
, const char *fmt
, ...);
432 static void error_precond(int severity
, const char *fmt
, ...);
433 static void *new_Block(size_t size
);
434 static void delete_Blocks(void);
435 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
436 const char *text
, int txtlen
);
437 static Token
*delete_Token(Token
* t
);
440 * Macros for safe checking of token pointers, avoid *(NULL)
442 #define tok_type_(x,t) ((x) && (x)->type == (t))
443 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
444 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
445 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
447 /* Handle TASM specific directives, which do not contain a % in
448 * front of them. We do it here because I could not find any other
449 * place to do it for the moment, and it is a hack (ideally it would
450 * be nice to be able to use the NASM pre-processor to do it).
452 static char *check_tasm_directive(char *line
)
454 int32_t i
, j
, k
, m
, len
;
455 char *p
= line
, *oldline
, oldchar
;
457 /* Skip whitespace */
458 while (nasm_isspace(*p
) && *p
!= 0)
461 /* Binary search for the directive name */
463 j
= elements(tasm_directives
);
465 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
472 m
= nasm_stricmp(p
, tasm_directives
[k
]);
474 /* We have found a directive, so jam a % in front of it
475 * so that NASM will then recognise it as one if it's own.
480 line
= nasm_malloc(len
+ 2);
482 if (k
== TM_IFDIFI
) {
484 * NASM does not recognise IFDIFI, so we convert
485 * it to %if 0. This is not used in NASM
486 * compatible code, but does need to parse for the
487 * TASM macro package.
489 strcpy(line
+ 1, "if 0");
491 memcpy(line
+ 1, p
, len
+ 1);
506 * The pre-preprocessing stage... This function translates line
507 * number indications as they emerge from GNU cpp (`# lineno "file"
508 * flags') into NASM preprocessor line number indications (`%line
511 static char *prepreproc(char *line
)
514 char *fname
, *oldline
;
516 if (line
[0] == '#' && line
[1] == ' ') {
519 lineno
= atoi(fname
);
520 fname
+= strspn(fname
, "0123456789 ");
523 fnlen
= strcspn(fname
, "\"");
524 line
= nasm_malloc(20 + fnlen
);
525 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
528 if (tasm_compatible_mode
)
529 return check_tasm_directive(line
);
534 * Free a linked list of tokens.
536 static void free_tlist(Token
* list
)
539 list
= delete_Token(list
);
544 * Free a linked list of lines.
546 static void free_llist(Line
* list
)
552 free_tlist(l
->first
);
560 static void free_mmacro(MMacro
* m
)
563 free_tlist(m
->dlist
);
564 nasm_free(m
->defaults
);
565 free_llist(m
->expansion
);
570 * Free all currently defined macros, and free the hash tables
572 static void free_smacro_table(struct hash_table
*smt
)
576 struct hash_tbl_node
*it
= NULL
;
578 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
579 nasm_free((void *)key
);
581 SMacro
*ns
= s
->next
;
583 free_tlist(s
->expansion
);
591 static void free_mmacro_table(struct hash_table
*mmt
)
595 struct hash_tbl_node
*it
= NULL
;
598 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
599 nasm_free((void *)key
);
601 MMacro
*nm
= m
->next
;
609 static void free_macros(void)
611 free_smacro_table(&smacros
);
612 free_mmacro_table(&mmacros
);
616 * Initialize the hash tables
618 static void init_macros(void)
620 hash_init(&smacros
, HASH_LARGE
);
621 hash_init(&mmacros
, HASH_LARGE
);
625 * Pop the context stack.
627 static void ctx_pop(void)
632 free_smacro_table(&c
->localmac
);
638 * Search for a key in the hash index; adding it if necessary
639 * (in which case we initialize the data pointer to NULL.)
642 hash_findi_add(struct hash_table
*hash
, const char *str
)
644 struct hash_insert hi
;
648 r
= hash_findi(hash
, str
, &hi
);
652 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
653 return hash_add(&hi
, strx
, NULL
);
657 * Like hash_findi, but returns the data element rather than a pointer
658 * to it. Used only when not adding a new element, hence no third
662 hash_findix(struct hash_table
*hash
, const char *str
)
666 p
= hash_findi(hash
, str
, NULL
);
667 return p
? *p
: NULL
;
670 #define BUF_DELTA 512
672 * Read a line from the top file in istk, handling multiple CR/LFs
673 * at the end of the line read, and handling spurious ^Zs. Will
674 * return lines from the standard macro set if this has not already
677 static char *read_line(void)
679 char *buffer
, *p
, *q
;
680 int bufsize
, continued_count
;
684 const unsigned char *p
= stdmacpos
;
689 len
+= pp_directives_len
[c
-0x80]+1;
693 ret
= nasm_malloc(len
+1);
695 while ((c
= *stdmacpos
++)) {
697 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
698 q
+= pp_directives_len
[c
-0x80];
708 /* This was the last of the standard macro chain... */
710 if (any_extrastdmac
) {
711 stdmacpos
= extrastdmac
;
712 any_extrastdmac
= false;
713 } else if (do_predef
) {
715 Token
*head
, **tail
, *t
;
718 * Nasty hack: here we push the contents of
719 * `predef' on to the top-level expansion stack,
720 * since this is the most convenient way to
721 * implement the pre-include and pre-define
724 for (pd
= predef
; pd
; pd
= pd
->next
) {
727 for (t
= pd
->first
; t
; t
= t
->next
) {
728 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
729 tail
= &(*tail
)->next
;
731 l
= nasm_malloc(sizeof(Line
));
732 l
->next
= istk
->expansion
;
744 buffer
= nasm_malloc(BUF_DELTA
);
748 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
752 if (p
> buffer
&& p
[-1] == '\n') {
753 /* Convert backslash-CRLF line continuation sequences into
754 nothing at all (for DOS and Windows) */
755 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
760 /* Also convert backslash-LF line continuation sequences into
761 nothing at all (for Unix) */
762 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
770 if (p
- buffer
> bufsize
- 10) {
771 int32_t offset
= p
- buffer
;
772 bufsize
+= BUF_DELTA
;
773 buffer
= nasm_realloc(buffer
, bufsize
);
774 p
= buffer
+ offset
; /* prevent stale-pointer problems */
778 if (!q
&& p
== buffer
) {
783 src_set_linnum(src_get_linnum() + istk
->lineinc
+
784 (continued_count
* istk
->lineinc
));
787 * Play safe: remove CRs as well as LFs, if any of either are
788 * present at the end of the line.
790 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
794 * Handle spurious ^Z, which may be inserted into source files
795 * by some file transfer utilities.
797 buffer
[strcspn(buffer
, "\032")] = '\0';
799 list
->line(LIST_READ
, buffer
);
805 * Tokenize a line of text. This is a very simple process since we
806 * don't need to parse the value out of e.g. numeric tokens: we
807 * simply split one string into many.
809 static Token
*tokenize(char *line
)
812 enum pp_token_type type
;
814 Token
*t
, **tail
= &list
;
820 if (*p
== '+' && !nasm_isdigit(p
[1])) {
823 } else if (nasm_isdigit(*p
) ||
824 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
828 while (nasm_isdigit(*p
));
829 type
= TOK_PREPROC_ID
;
830 } else if (*p
== '{') {
832 while (*p
&& *p
!= '}') {
839 type
= TOK_PREPROC_ID
;
840 } else if (*p
== '[') {
842 line
+= 2; /* Skip the leading %[ */
844 while (lvl
&& (c
= *p
++)) {
856 p
= nasm_skip_string(p
)+1;
866 error(ERR_NONFATAL
, "unterminated %[ construct");
868 } else if (*p
== '?') {
869 type
= TOK_PREPROC_Q
; /* %? */
872 type
= TOK_PREPROC_QQ
; /* %?? */
875 } else if (isidchar(*p
) ||
876 ((*p
== '!' || *p
== '%' || *p
== '$') &&
881 while (isidchar(*p
));
882 type
= TOK_PREPROC_ID
;
888 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
891 while (*p
&& isidchar(*p
))
893 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
898 p
= nasm_skip_string(p
);
903 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
904 /* Handling unterminated strings by UNV */
907 } else if (p
[0] == '$' && p
[1] == '$') {
908 type
= TOK_OTHER
; /* TOKEN_BASE */
910 } else if (isnumstart(*p
)) {
912 bool is_float
= false;
928 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
930 if (*p
== '+' || *p
== '-') {
931 /* e can only be followed by +/- if it is either a
932 prefixed hex number or a floating-point number */
936 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
938 } else if (c
== 'P' || c
== 'p') {
940 if (*p
== '+' || *p
== '-')
942 } else if (isnumchar(c
) || c
== '_')
945 /* we need to deal with consequences of the legacy
946 parser, like "1.nolist" being two tokens
947 (TOK_NUMBER, TOK_ID) here; at least give it
948 a shot for now. In the future, we probably need
949 a flex-based scanner with proper pattern matching
950 to do it as well as it can be done. Nothing in
951 the world is going to help the person who wants
952 0x123.p16 interpreted as two tokens, though. */
957 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
958 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
959 (*r
== 'p' || *r
== 'P')) {
963 break; /* Terminate the token */
967 p
--; /* Point to first character beyond number */
969 if (p
== line
+1 && *line
== '$') {
970 type
= TOK_OTHER
; /* TOKEN_HERE */
972 if (has_e
&& !is_hex
) {
973 /* 1e13 is floating-point, but 1e13h is not */
977 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
979 } else if (nasm_isspace(*p
)) {
980 type
= TOK_WHITESPACE
;
982 while (*p
&& nasm_isspace(*p
))
985 * Whitespace just before end-of-line is discarded by
986 * pretending it's a comment; whitespace just before a
987 * comment gets lumped into the comment.
989 if (!*p
|| *p
== ';') {
994 } else if (*p
== ';') {
1000 * Anything else is an operator of some kind. We check
1001 * for all the double-character operators (>>, <<, //,
1002 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1003 * else is a single-character operator.
1006 if ((p
[0] == '>' && p
[1] == '>') ||
1007 (p
[0] == '<' && p
[1] == '<') ||
1008 (p
[0] == '/' && p
[1] == '/') ||
1009 (p
[0] == '<' && p
[1] == '=') ||
1010 (p
[0] == '>' && p
[1] == '=') ||
1011 (p
[0] == '=' && p
[1] == '=') ||
1012 (p
[0] == '!' && p
[1] == '=') ||
1013 (p
[0] == '<' && p
[1] == '>') ||
1014 (p
[0] == '&' && p
[1] == '&') ||
1015 (p
[0] == '|' && p
[1] == '|') ||
1016 (p
[0] == '^' && p
[1] == '^')) {
1022 /* Handling unterminated string by UNV */
1025 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1026 t->text[p-line] = *line;
1030 if (type
!= TOK_COMMENT
) {
1031 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1040 * this function allocates a new managed block of memory and
1041 * returns a pointer to the block. The managed blocks are
1042 * deleted only all at once by the delete_Blocks function.
1044 static void *new_Block(size_t size
)
1046 Blocks
*b
= &blocks
;
1048 /* first, get to the end of the linked list */
1051 /* now allocate the requested chunk */
1052 b
->chunk
= nasm_malloc(size
);
1054 /* now allocate a new block for the next request */
1055 b
->next
= nasm_malloc(sizeof(Blocks
));
1056 /* and initialize the contents of the new block */
1057 b
->next
->next
= NULL
;
1058 b
->next
->chunk
= NULL
;
1063 * this function deletes all managed blocks of memory
1065 static void delete_Blocks(void)
1067 Blocks
*a
, *b
= &blocks
;
1070 * keep in mind that the first block, pointed to by blocks
1071 * is a static and not dynamically allocated, so we don't
1076 nasm_free(b
->chunk
);
1085 * this function creates a new Token and passes a pointer to it
1086 * back to the caller. It sets the type and text elements, and
1087 * also the a.mac and next elements to NULL.
1089 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1090 const char *text
, int txtlen
)
1095 if (freeTokens
== NULL
) {
1096 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1097 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1098 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1099 freeTokens
[i
].next
= NULL
;
1102 freeTokens
= t
->next
;
1106 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1110 txtlen
= strlen(text
);
1111 t
->text
= nasm_malloc(txtlen
+1);
1112 memcpy(t
->text
, text
, txtlen
);
1113 t
->text
[txtlen
] = '\0';
1118 static Token
*delete_Token(Token
* t
)
1120 Token
*next
= t
->next
;
1122 t
->next
= freeTokens
;
1128 * Convert a line of tokens back into text.
1129 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1130 * will be transformed into ..@ctxnum.xxx
1132 static char *detoken(Token
* tlist
, bool expand_locals
)
1140 for (t
= tlist
; t
; t
= t
->next
) {
1141 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1142 char *p
= getenv(t
->text
+ 2);
1145 t
->text
= nasm_strdup(p
);
1149 /* Expand local macros here and not during preprocessing */
1150 if (expand_locals
&&
1151 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1152 t
->text
[0] == '%' && t
->text
[1] == '$') {
1155 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1158 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1159 p
= nasm_strcat(buffer
, q
);
1164 if (t
->type
== TOK_WHITESPACE
) {
1166 } else if (t
->text
) {
1167 len
+= strlen(t
->text
);
1170 p
= line
= nasm_malloc(len
+ 1);
1171 for (t
= tlist
; t
; t
= t
->next
) {
1172 if (t
->type
== TOK_WHITESPACE
) {
1174 } else if (t
->text
) {
1185 * A scanner, suitable for use by the expression evaluator, which
1186 * operates on a line of Tokens. Expects a pointer to a pointer to
1187 * the first token in the line to be passed in as its private_data
1190 * FIX: This really needs to be unified with stdscan.
1192 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1194 Token
**tlineptr
= private_data
;
1196 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1200 *tlineptr
= tline
? tline
->next
: NULL
;
1202 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1203 tline
->type
== TOK_COMMENT
));
1206 return tokval
->t_type
= TOKEN_EOS
;
1208 tokval
->t_charptr
= tline
->text
;
1210 if (tline
->text
[0] == '$' && !tline
->text
[1])
1211 return tokval
->t_type
= TOKEN_HERE
;
1212 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1213 return tokval
->t_type
= TOKEN_BASE
;
1215 if (tline
->type
== TOK_ID
) {
1216 p
= tokval
->t_charptr
= tline
->text
;
1218 tokval
->t_charptr
++;
1219 return tokval
->t_type
= TOKEN_ID
;
1222 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1223 if (r
>= p
+MAX_KEYWORD
)
1224 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1225 *s
++ = nasm_tolower(*r
);
1228 /* right, so we have an identifier sitting in temp storage. now,
1229 * is it actually a register or instruction name, or what? */
1230 return nasm_token_hash(ourcopy
, tokval
);
1233 if (tline
->type
== TOK_NUMBER
) {
1235 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1236 tokval
->t_charptr
= tline
->text
;
1238 return tokval
->t_type
= TOKEN_ERRNUM
;
1240 return tokval
->t_type
= TOKEN_NUM
;
1243 if (tline
->type
== TOK_FLOAT
) {
1244 return tokval
->t_type
= TOKEN_FLOAT
;
1247 if (tline
->type
== TOK_STRING
) {
1250 bq
= tline
->text
[0];
1251 tokval
->t_charptr
= tline
->text
;
1252 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1254 if (ep
[0] != bq
|| ep
[1] != '\0')
1255 return tokval
->t_type
= TOKEN_ERRSTR
;
1257 return tokval
->t_type
= TOKEN_STR
;
1260 if (tline
->type
== TOK_OTHER
) {
1261 if (!strcmp(tline
->text
, "<<"))
1262 return tokval
->t_type
= TOKEN_SHL
;
1263 if (!strcmp(tline
->text
, ">>"))
1264 return tokval
->t_type
= TOKEN_SHR
;
1265 if (!strcmp(tline
->text
, "//"))
1266 return tokval
->t_type
= TOKEN_SDIV
;
1267 if (!strcmp(tline
->text
, "%%"))
1268 return tokval
->t_type
= TOKEN_SMOD
;
1269 if (!strcmp(tline
->text
, "=="))
1270 return tokval
->t_type
= TOKEN_EQ
;
1271 if (!strcmp(tline
->text
, "<>"))
1272 return tokval
->t_type
= TOKEN_NE
;
1273 if (!strcmp(tline
->text
, "!="))
1274 return tokval
->t_type
= TOKEN_NE
;
1275 if (!strcmp(tline
->text
, "<="))
1276 return tokval
->t_type
= TOKEN_LE
;
1277 if (!strcmp(tline
->text
, ">="))
1278 return tokval
->t_type
= TOKEN_GE
;
1279 if (!strcmp(tline
->text
, "&&"))
1280 return tokval
->t_type
= TOKEN_DBL_AND
;
1281 if (!strcmp(tline
->text
, "^^"))
1282 return tokval
->t_type
= TOKEN_DBL_XOR
;
1283 if (!strcmp(tline
->text
, "||"))
1284 return tokval
->t_type
= TOKEN_DBL_OR
;
1288 * We have no other options: just return the first character of
1291 return tokval
->t_type
= tline
->text
[0];
1295 * Compare a string to the name of an existing macro; this is a
1296 * simple wrapper which calls either strcmp or nasm_stricmp
1297 * depending on the value of the `casesense' parameter.
1299 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1301 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1305 * Compare a string to the name of an existing macro; this is a
1306 * simple wrapper which calls either strcmp or nasm_stricmp
1307 * depending on the value of the `casesense' parameter.
1309 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1311 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1315 * Return the Context structure associated with a %$ token. Return
1316 * NULL, having _already_ reported an error condition, if the
1317 * context stack isn't deep enough for the supplied number of $
1319 * If all_contexts == true, contexts that enclose current are
1320 * also scanned for such smacro, until it is found; if not -
1321 * only the context that directly results from the number of $'s
1322 * in variable's name.
1324 * If "namep" is non-NULL, set it to the pointer to the macro name
1325 * tail, i.e. the part beyond %$...
1327 static Context
*get_ctx(const char *name
, const char **namep
,
1337 if (!name
|| name
[0] != '%' || name
[1] != '$')
1341 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1348 while (ctx
&& *name
== '$') {
1354 error(ERR_NONFATAL
, "`%s': context stack is only"
1355 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1366 /* Search for this smacro in found context */
1367 m
= hash_findix(&ctx
->localmac
, name
);
1369 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1380 * Check to see if a file is already in a string list
1382 static bool in_list(const StrList
*list
, const char *str
)
1385 if (!strcmp(list
->str
, str
))
1393 * Open an include file. This routine must always return a valid
1394 * file pointer if it returns - it's responsible for throwing an
1395 * ERR_FATAL and bombing out completely if not. It should also try
1396 * the include path one by one until it finds the file or reaches
1397 * the end of the path.
1399 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1404 IncPath
*ip
= ipath
;
1405 int len
= strlen(file
);
1406 size_t prefix_len
= 0;
1410 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1411 memcpy(sl
->str
, prefix
, prefix_len
);
1412 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1413 fp
= fopen(sl
->str
, "r");
1414 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1432 prefix_len
= strlen(prefix
);
1434 /* -MG given and file not found */
1435 if (dhead
&& !in_list(*dhead
, file
)) {
1436 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1438 strcpy(sl
->str
, file
);
1446 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1447 return NULL
; /* never reached - placate compilers */
1451 * Determine if we should warn on defining a single-line macro of
1452 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1453 * return true if _any_ single-line macro of that name is defined.
1454 * Otherwise, will return true if a single-line macro with either
1455 * `nparam' or no parameters is defined.
1457 * If a macro with precisely the right number of parameters is
1458 * defined, or nparam is -1, the address of the definition structure
1459 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1460 * is NULL, no action will be taken regarding its contents, and no
1463 * Note that this is also called with nparam zero to resolve
1466 * If you already know which context macro belongs to, you can pass
1467 * the context pointer as first parameter; if you won't but name begins
1468 * with %$ the context will be automatically computed. If all_contexts
1469 * is true, macro will be searched in outer contexts as well.
1472 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1475 struct hash_table
*smtbl
;
1479 smtbl
= &ctx
->localmac
;
1480 } else if (name
[0] == '%' && name
[1] == '$') {
1482 ctx
= get_ctx(name
, &name
, false);
1484 return false; /* got to return _something_ */
1485 smtbl
= &ctx
->localmac
;
1489 m
= (SMacro
*) hash_findix(smtbl
, name
);
1492 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1493 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1495 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1509 * Count and mark off the parameters in a multi-line macro call.
1510 * This is called both from within the multi-line macro expansion
1511 * code, and also to mark off the default parameters when provided
1512 * in a %macro definition line.
1514 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1516 int paramsize
, brace
;
1518 *nparam
= paramsize
= 0;
1521 /* +1: we need space for the final NULL */
1522 if (*nparam
+1 >= paramsize
) {
1523 paramsize
+= PARAM_DELTA
;
1524 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1528 if (tok_is_(t
, "{"))
1530 (*params
)[(*nparam
)++] = t
;
1531 while (tok_isnt_(t
, brace
? "}" : ","))
1533 if (t
) { /* got a comma/brace */
1537 * Now we've found the closing brace, look further
1541 if (tok_isnt_(t
, ",")) {
1543 "braces do not enclose all of macro parameter");
1544 while (tok_isnt_(t
, ","))
1548 t
= t
->next
; /* eat the comma */
1555 * Determine whether one of the various `if' conditions is true or
1558 * We must free the tline we get passed.
1560 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1562 enum pp_conditional i
= PP_COND(ct
);
1564 Token
*t
, *tt
, **tptr
, *origline
;
1565 struct tokenval tokval
;
1567 enum pp_token_type needtype
;
1573 j
= false; /* have we matched yet? */
1578 if (tline
->type
!= TOK_ID
) {
1580 "`%s' expects context identifiers", pp_directives
[ct
]);
1581 free_tlist(origline
);
1584 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1586 tline
= tline
->next
;
1591 j
= false; /* have we matched yet? */
1594 if (!tline
|| (tline
->type
!= TOK_ID
&&
1595 (tline
->type
!= TOK_PREPROC_ID
||
1596 tline
->text
[1] != '$'))) {
1598 "`%s' expects macro identifiers", pp_directives
[ct
]);
1601 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1603 tline
= tline
->next
;
1609 tline
= expand_smacro(tline
);
1611 while (tok_isnt_(tt
, ","))
1615 "`%s' expects two comma-separated arguments",
1620 j
= true; /* assume equality unless proved not */
1621 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1622 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1623 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1627 if (t
->type
== TOK_WHITESPACE
) {
1631 if (tt
->type
== TOK_WHITESPACE
) {
1635 if (tt
->type
!= t
->type
) {
1636 j
= false; /* found mismatching tokens */
1639 /* When comparing strings, need to unquote them first */
1640 if (t
->type
== TOK_STRING
) {
1641 size_t l1
= nasm_unquote(t
->text
, NULL
);
1642 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1648 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1652 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1653 j
= false; /* found mismatching tokens */
1660 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1661 j
= false; /* trailing gunk on one end or other */
1667 MMacro searching
, *mmac
;
1670 tline
= expand_id(tline
);
1671 if (!tok_type_(tline
, TOK_ID
)) {
1673 "`%s' expects a macro name", pp_directives
[ct
]);
1676 searching
.name
= nasm_strdup(tline
->text
);
1677 searching
.casesense
= true;
1678 searching
.plus
= false;
1679 searching
.nolist
= false;
1680 searching
.in_progress
= 0;
1681 searching
.rep_nest
= NULL
;
1682 searching
.nparam_min
= 0;
1683 searching
.nparam_max
= INT_MAX
;
1684 tline
= expand_smacro(tline
->next
);
1687 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1689 "`%s' expects a parameter count or nothing",
1692 searching
.nparam_min
= searching
.nparam_max
=
1693 readnum(tline
->text
, &j
);
1696 "unable to parse parameter count `%s'",
1699 if (tline
&& tok_is_(tline
->next
, "-")) {
1700 tline
= tline
->next
->next
;
1701 if (tok_is_(tline
, "*"))
1702 searching
.nparam_max
= INT_MAX
;
1703 else if (!tok_type_(tline
, TOK_NUMBER
))
1705 "`%s' expects a parameter count after `-'",
1708 searching
.nparam_max
= readnum(tline
->text
, &j
);
1711 "unable to parse parameter count `%s'",
1713 if (searching
.nparam_min
> searching
.nparam_max
)
1715 "minimum parameter count exceeds maximum");
1718 if (tline
&& tok_is_(tline
->next
, "+")) {
1719 tline
= tline
->next
;
1720 searching
.plus
= true;
1722 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1724 if (!strcmp(mmac
->name
, searching
.name
) &&
1725 (mmac
->nparam_min
<= searching
.nparam_max
1727 && (searching
.nparam_min
<= mmac
->nparam_max
1734 if(tline
&& tline
->next
)
1735 error(ERR_WARNING
|ERR_PASS1
,
1736 "trailing garbage after %%ifmacro ignored");
1737 nasm_free(searching
.name
);
1746 needtype
= TOK_NUMBER
;
1749 needtype
= TOK_STRING
;
1753 t
= tline
= expand_smacro(tline
);
1755 while (tok_type_(t
, TOK_WHITESPACE
) ||
1756 (needtype
== TOK_NUMBER
&&
1757 tok_type_(t
, TOK_OTHER
) &&
1758 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1762 j
= tok_type_(t
, needtype
);
1766 t
= tline
= expand_smacro(tline
);
1767 while (tok_type_(t
, TOK_WHITESPACE
))
1772 t
= t
->next
; /* Skip the actual token */
1773 while (tok_type_(t
, TOK_WHITESPACE
))
1775 j
= !t
; /* Should be nothing left */
1780 t
= tline
= expand_smacro(tline
);
1781 while (tok_type_(t
, TOK_WHITESPACE
))
1784 j
= !t
; /* Should be empty */
1788 t
= tline
= expand_smacro(tline
);
1790 tokval
.t_type
= TOKEN_INVALID
;
1791 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1792 NULL
, pass
| CRITICAL
, error
, NULL
);
1796 error(ERR_WARNING
|ERR_PASS1
,
1797 "trailing garbage after expression ignored");
1798 if (!is_simple(evalresult
)) {
1800 "non-constant value given to `%s'", pp_directives
[ct
]);
1803 j
= reloc_value(evalresult
) != 0;
1808 "preprocessor directive `%s' not yet implemented",
1813 free_tlist(origline
);
1814 return j
^ PP_NEGATIVE(ct
);
1817 free_tlist(origline
);
1822 * Common code for defining an smacro
1824 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1825 int nparam
, Token
*expansion
)
1827 SMacro
*smac
, **smhead
;
1828 struct hash_table
*smtbl
;
1830 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1832 error(ERR_WARNING
|ERR_PASS1
,
1833 "single-line macro `%s' defined both with and"
1834 " without parameters", mname
);
1836 /* Some instances of the old code considered this a failure,
1837 some others didn't. What is the right thing to do here? */
1838 free_tlist(expansion
);
1839 return false; /* Failure */
1842 * We're redefining, so we have to take over an
1843 * existing SMacro structure. This means freeing
1844 * what was already in it.
1846 nasm_free(smac
->name
);
1847 free_tlist(smac
->expansion
);
1850 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1851 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1852 smac
= nasm_malloc(sizeof(SMacro
));
1853 smac
->next
= *smhead
;
1856 smac
->name
= nasm_strdup(mname
);
1857 smac
->casesense
= casesense
;
1858 smac
->nparam
= nparam
;
1859 smac
->expansion
= expansion
;
1860 smac
->in_progress
= false;
1861 return true; /* Success */
1865 * Undefine an smacro
1867 static void undef_smacro(Context
*ctx
, const char *mname
)
1869 SMacro
**smhead
, *s
, **sp
;
1870 struct hash_table
*smtbl
;
1872 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1873 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1877 * We now have a macro name... go hunt for it.
1880 while ((s
= *sp
) != NULL
) {
1881 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1884 free_tlist(s
->expansion
);
1894 * Parse a mmacro specification.
1896 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1900 tline
= tline
->next
;
1902 tline
= expand_id(tline
);
1903 if (!tok_type_(tline
, TOK_ID
)) {
1904 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1908 def
->name
= nasm_strdup(tline
->text
);
1910 def
->nolist
= false;
1911 def
->in_progress
= 0;
1912 def
->rep_nest
= NULL
;
1913 def
->nparam_min
= 0;
1914 def
->nparam_max
= 0;
1916 tline
= expand_smacro(tline
->next
);
1918 if (!tok_type_(tline
, TOK_NUMBER
)) {
1919 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1921 def
->nparam_min
= def
->nparam_max
=
1922 readnum(tline
->text
, &err
);
1925 "unable to parse parameter count `%s'", tline
->text
);
1927 if (tline
&& tok_is_(tline
->next
, "-")) {
1928 tline
= tline
->next
->next
;
1929 if (tok_is_(tline
, "*")) {
1930 def
->nparam_max
= INT_MAX
;
1931 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1933 "`%s' expects a parameter count after `-'", directive
);
1935 def
->nparam_max
= readnum(tline
->text
, &err
);
1937 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1940 if (def
->nparam_min
> def
->nparam_max
) {
1941 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1945 if (tline
&& tok_is_(tline
->next
, "+")) {
1946 tline
= tline
->next
;
1949 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1950 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1951 tline
= tline
->next
;
1956 * Handle default parameters.
1958 if (tline
&& tline
->next
) {
1959 def
->dlist
= tline
->next
;
1961 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1964 def
->defaults
= NULL
;
1966 def
->expansion
= NULL
;
1969 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1971 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1972 "too many default macro parameters");
1979 * Decode a size directive
1981 static int parse_size(const char *str
) {
1982 static const char *size_names
[] =
1983 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1984 static const int sizes
[] =
1985 { 0, 1, 4, 16, 8, 10, 2, 32 };
1987 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1991 * find and process preprocessor directive in passed line
1992 * Find out if a line contains a preprocessor directive, and deal
1995 * If a directive _is_ found, it is the responsibility of this routine
1996 * (and not the caller) to free_tlist() the line.
1998 * @param tline a pointer to the current tokeninzed line linked list
1999 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2002 static int do_directive(Token
* tline
)
2004 enum preproc_token i
;
2017 MMacro
*mmac
, **mmhead
;
2018 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2020 struct tokenval tokval
;
2022 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2030 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2031 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2032 || tline
->text
[1] == '!'))
2033 return NO_DIRECTIVE_FOUND
;
2035 i
= pp_token_hash(tline
->text
);
2038 * If we're in a non-emitting branch of a condition construct,
2039 * or walking to the end of an already terminated %rep block,
2040 * we should ignore all directives except for condition
2043 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2044 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2045 return NO_DIRECTIVE_FOUND
;
2049 * If we're defining a macro or reading a %rep block, we should
2050 * ignore all directives except for %macro/%imacro (which nest),
2051 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2052 * If we're in a %rep block, another %rep nests, so should be let through.
2054 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2055 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2056 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2057 return NO_DIRECTIVE_FOUND
;
2061 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
2063 return NO_DIRECTIVE_FOUND
;
2064 } else if (nested_mac_count
> 0) {
2065 if (i
== PP_ENDMACRO
) {
2067 return NO_DIRECTIVE_FOUND
;
2070 if (!defining
->name
) {
2073 return NO_DIRECTIVE_FOUND
;
2074 } else if (nested_rep_count
> 0) {
2075 if (i
== PP_ENDREP
) {
2077 return NO_DIRECTIVE_FOUND
;
2085 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2087 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2090 /* Directive to tell NASM what the default stack size is. The
2091 * default is for a 16-bit stack, and this can be overriden with
2093 * the following form:
2095 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2097 tline
= tline
->next
;
2098 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2099 tline
= tline
->next
;
2100 if (!tline
|| tline
->type
!= TOK_ID
) {
2101 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2102 free_tlist(origline
);
2103 return DIRECTIVE_FOUND
;
2105 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2106 /* All subsequent ARG directives are for a 32-bit stack */
2108 StackPointer
= "ebp";
2111 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2112 /* All subsequent ARG directives are for a 64-bit stack */
2114 StackPointer
= "rbp";
2117 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2118 /* All subsequent ARG directives are for a 16-bit stack,
2119 * far function call.
2122 StackPointer
= "bp";
2125 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2126 /* All subsequent ARG directives are for a 16-bit stack,
2127 * far function call. We don't support near functions.
2130 StackPointer
= "bp";
2134 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2135 free_tlist(origline
);
2136 return DIRECTIVE_FOUND
;
2138 free_tlist(origline
);
2139 return DIRECTIVE_FOUND
;
2142 /* TASM like ARG directive to define arguments to functions, in
2143 * the following form:
2145 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2149 char *arg
, directive
[256];
2150 int size
= StackSize
;
2152 /* Find the argument name */
2153 tline
= tline
->next
;
2154 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2155 tline
= tline
->next
;
2156 if (!tline
|| tline
->type
!= TOK_ID
) {
2157 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2158 free_tlist(origline
);
2159 return DIRECTIVE_FOUND
;
2163 /* Find the argument size type */
2164 tline
= tline
->next
;
2165 if (!tline
|| tline
->type
!= TOK_OTHER
2166 || tline
->text
[0] != ':') {
2168 "Syntax error processing `%%arg' directive");
2169 free_tlist(origline
);
2170 return DIRECTIVE_FOUND
;
2172 tline
= tline
->next
;
2173 if (!tline
|| tline
->type
!= TOK_ID
) {
2174 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2175 free_tlist(origline
);
2176 return DIRECTIVE_FOUND
;
2179 /* Allow macro expansion of type parameter */
2180 tt
= tokenize(tline
->text
);
2181 tt
= expand_smacro(tt
);
2182 size
= parse_size(tt
->text
);
2185 "Invalid size type for `%%arg' missing directive");
2187 free_tlist(origline
);
2188 return DIRECTIVE_FOUND
;
2192 /* Round up to even stack slots */
2193 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2195 /* Now define the macro for the argument */
2196 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2197 arg
, StackPointer
, offset
);
2198 do_directive(tokenize(directive
));
2201 /* Move to the next argument in the list */
2202 tline
= tline
->next
;
2203 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2204 tline
= tline
->next
;
2205 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2207 free_tlist(origline
);
2208 return DIRECTIVE_FOUND
;
2211 /* TASM like LOCAL directive to define local variables for a
2212 * function, in the following form:
2214 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2216 * The '= LocalSize' at the end is ignored by NASM, but is
2217 * required by TASM to define the local parameter size (and used
2218 * by the TASM macro package).
2220 offset
= LocalOffset
;
2222 char *local
, directive
[256];
2223 int size
= StackSize
;
2225 /* Find the argument name */
2226 tline
= tline
->next
;
2227 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2228 tline
= tline
->next
;
2229 if (!tline
|| tline
->type
!= TOK_ID
) {
2231 "`%%local' missing argument parameter");
2232 free_tlist(origline
);
2233 return DIRECTIVE_FOUND
;
2235 local
= tline
->text
;
2237 /* Find the argument size type */
2238 tline
= tline
->next
;
2239 if (!tline
|| tline
->type
!= TOK_OTHER
2240 || tline
->text
[0] != ':') {
2242 "Syntax error processing `%%local' directive");
2243 free_tlist(origline
);
2244 return DIRECTIVE_FOUND
;
2246 tline
= tline
->next
;
2247 if (!tline
|| tline
->type
!= TOK_ID
) {
2249 "`%%local' missing size type parameter");
2250 free_tlist(origline
);
2251 return DIRECTIVE_FOUND
;
2254 /* Allow macro expansion of type parameter */
2255 tt
= tokenize(tline
->text
);
2256 tt
= expand_smacro(tt
);
2257 size
= parse_size(tt
->text
);
2260 "Invalid size type for `%%local' missing directive");
2262 free_tlist(origline
);
2263 return DIRECTIVE_FOUND
;
2267 /* Round up to even stack slots */
2268 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2270 offset
+= size
; /* Negative offset, increment before */
2272 /* Now define the macro for the argument */
2273 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2274 local
, StackPointer
, offset
);
2275 do_directive(tokenize(directive
));
2277 /* Now define the assign to setup the enter_c macro correctly */
2278 snprintf(directive
, sizeof(directive
),
2279 "%%assign %%$localsize %%$localsize+%d", size
);
2280 do_directive(tokenize(directive
));
2282 /* Move to the next argument in the list */
2283 tline
= tline
->next
;
2284 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2285 tline
= tline
->next
;
2286 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2287 LocalOffset
= offset
;
2288 free_tlist(origline
);
2289 return DIRECTIVE_FOUND
;
2293 error(ERR_WARNING
|ERR_PASS1
,
2294 "trailing garbage after `%%clear' ignored");
2297 free_tlist(origline
);
2298 return DIRECTIVE_FOUND
;
2301 t
= tline
->next
= expand_smacro(tline
->next
);
2303 if (!t
|| (t
->type
!= TOK_STRING
&&
2304 t
->type
!= TOK_INTERNAL_STRING
)) {
2305 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2306 free_tlist(origline
);
2307 return DIRECTIVE_FOUND
; /* but we did _something_ */
2310 error(ERR_WARNING
|ERR_PASS1
,
2311 "trailing garbage after `%%depend' ignored");
2313 if (t
->type
!= TOK_INTERNAL_STRING
)
2314 nasm_unquote(p
, NULL
);
2315 if (dephead
&& !in_list(*dephead
, p
)) {
2316 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2320 deptail
= &sl
->next
;
2322 free_tlist(origline
);
2323 return DIRECTIVE_FOUND
;
2326 t
= tline
->next
= expand_smacro(tline
->next
);
2329 if (!t
|| (t
->type
!= TOK_STRING
&&
2330 t
->type
!= TOK_INTERNAL_STRING
)) {
2331 error(ERR_NONFATAL
, "`%%include' expects a file name");
2332 free_tlist(origline
);
2333 return DIRECTIVE_FOUND
; /* but we did _something_ */
2336 error(ERR_WARNING
|ERR_PASS1
,
2337 "trailing garbage after `%%include' ignored");
2339 if (t
->type
!= TOK_INTERNAL_STRING
)
2340 nasm_unquote(p
, NULL
);
2341 inc
= nasm_malloc(sizeof(Include
));
2344 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2346 /* -MG given but file not found */
2349 inc
->fname
= src_set_fname(nasm_strdup(p
));
2350 inc
->lineno
= src_set_linnum(0);
2352 inc
->expansion
= NULL
;
2355 list
->uplevel(LIST_INCLUDE
);
2357 free_tlist(origline
);
2358 return DIRECTIVE_FOUND
;
2362 static macros_t
*use_pkg
;
2363 const char *pkg_macro
;
2365 tline
= tline
->next
;
2367 tline
= expand_id(tline
);
2369 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2370 tline
->type
!= TOK_INTERNAL_STRING
&&
2371 tline
->type
!= TOK_ID
)) {
2372 error(ERR_NONFATAL
, "`%%use' expects a package name");
2373 free_tlist(origline
);
2374 return DIRECTIVE_FOUND
; /* but we did _something_ */
2377 error(ERR_WARNING
|ERR_PASS1
,
2378 "trailing garbage after `%%use' ignored");
2379 if (tline
->type
== TOK_STRING
)
2380 nasm_unquote(tline
->text
, NULL
);
2381 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2383 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2384 /* The first string will be <%define>__USE_*__ */
2385 pkg_macro
= (char *)use_pkg
+ 1;
2386 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2387 /* Not already included, go ahead and include it */
2388 stdmacpos
= use_pkg
;
2390 free_tlist(origline
);
2391 return DIRECTIVE_FOUND
;
2396 tline
= tline
->next
;
2398 tline
= expand_id(tline
);
2400 if (!tok_type_(tline
, TOK_ID
)) {
2401 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2403 free_tlist(origline
);
2404 return DIRECTIVE_FOUND
; /* but we did _something_ */
2407 error(ERR_WARNING
|ERR_PASS1
,
2408 "trailing garbage after `%s' ignored",
2410 p
= nasm_strdup(tline
->text
);
2412 p
= NULL
; /* Anonymous */
2416 ctx
= nasm_malloc(sizeof(Context
));
2418 hash_init(&ctx
->localmac
, HASH_SMALL
);
2420 ctx
->number
= unique
++;
2425 error(ERR_NONFATAL
, "`%s': context stack is empty",
2427 } else if (i
== PP_POP
) {
2428 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2429 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2431 cstk
->name
? cstk
->name
: "anonymous", p
);
2436 nasm_free(cstk
->name
);
2442 free_tlist(origline
);
2443 return DIRECTIVE_FOUND
;
2445 severity
= ERR_FATAL
|ERR_NO_SEVERITY
;
2448 severity
= ERR_NONFATAL
|ERR_NO_SEVERITY
;
2451 severity
= ERR_WARNING
|ERR_NO_SEVERITY
|ERR_WARN_USER
;
2456 /* Only error out if this is the final pass */
2457 if (pass
!= 2 && i
!= PP_FATAL
)
2458 return DIRECTIVE_FOUND
;
2460 tline
->next
= expand_smacro(tline
->next
);
2461 tline
= tline
->next
;
2463 t
= tline
? tline
->next
: NULL
;
2465 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2466 /* The line contains only a quoted string */
2468 nasm_unquote(p
, NULL
);
2469 error(severity
, "%s: %s", pp_directives
[i
], p
);
2471 /* Not a quoted string, or more than a quoted string */
2472 p
= detoken(tline
, false);
2473 error(severity
, "%s: %s", pp_directives
[i
], p
);
2476 free_tlist(origline
);
2477 return DIRECTIVE_FOUND
;
2481 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2484 j
= if_condition(tline
->next
, i
);
2485 tline
->next
= NULL
; /* it got freed */
2486 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2488 cond
= nasm_malloc(sizeof(Cond
));
2489 cond
->next
= istk
->conds
;
2492 free_tlist(origline
);
2493 return DIRECTIVE_FOUND
;
2497 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2498 switch(istk
->conds
->state
) {
2500 istk
->conds
->state
= COND_DONE
;
2507 case COND_ELSE_TRUE
:
2508 case COND_ELSE_FALSE
:
2509 error_precond(ERR_WARNING
|ERR_PASS1
,
2510 "`%%elif' after `%%else' ignored");
2511 istk
->conds
->state
= COND_NEVER
;
2516 * IMPORTANT: In the case of %if, we will already have
2517 * called expand_mmac_params(); however, if we're
2518 * processing an %elif we must have been in a
2519 * non-emitting mode, which would have inhibited
2520 * the normal invocation of expand_mmac_params().
2521 * Therefore, we have to do it explicitly here.
2523 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2524 tline
->next
= NULL
; /* it got freed */
2525 istk
->conds
->state
=
2526 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2529 free_tlist(origline
);
2530 return DIRECTIVE_FOUND
;
2534 error_precond(ERR_WARNING
|ERR_PASS1
,
2535 "trailing garbage after `%%else' ignored");
2537 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2538 switch(istk
->conds
->state
) {
2541 istk
->conds
->state
= COND_ELSE_FALSE
;
2548 istk
->conds
->state
= COND_ELSE_TRUE
;
2551 case COND_ELSE_TRUE
:
2552 case COND_ELSE_FALSE
:
2553 error_precond(ERR_WARNING
|ERR_PASS1
,
2554 "`%%else' after `%%else' ignored.");
2555 istk
->conds
->state
= COND_NEVER
;
2558 free_tlist(origline
);
2559 return DIRECTIVE_FOUND
;
2563 error_precond(ERR_WARNING
|ERR_PASS1
,
2564 "trailing garbage after `%%endif' ignored");
2566 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2568 istk
->conds
= cond
->next
;
2570 free_tlist(origline
);
2571 return DIRECTIVE_FOUND
;
2577 "`%%%smacro': already defining a macro",
2578 (i
== PP_IMACRO
? "i" : ""));
2579 return DIRECTIVE_FOUND
;
2581 defining
= nasm_malloc(sizeof(MMacro
));
2582 defining
->casesense
= (i
== PP_MACRO
);
2583 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2584 nasm_free(defining
);
2586 return DIRECTIVE_FOUND
;
2589 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2591 if (!strcmp(mmac
->name
, defining
->name
) &&
2592 (mmac
->nparam_min
<= defining
->nparam_max
2594 && (defining
->nparam_min
<= mmac
->nparam_max
2596 error(ERR_WARNING
|ERR_PASS1
,
2597 "redefining multi-line macro `%s'", defining
->name
);
2598 return DIRECTIVE_FOUND
;
2602 free_tlist(origline
);
2603 return DIRECTIVE_FOUND
;
2607 if (! (defining
&& defining
->name
)) {
2608 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2609 return DIRECTIVE_FOUND
;
2611 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2612 defining
->next
= *mmhead
;
2615 free_tlist(origline
);
2616 return DIRECTIVE_FOUND
;
2624 spec
.casesense
= (i
== PP_UNMACRO
);
2625 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2626 return DIRECTIVE_FOUND
;
2628 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2629 while (mmac_p
&& *mmac_p
) {
2631 if (mmac
->casesense
== spec
.casesense
&&
2632 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2633 mmac
->nparam_min
== spec
.nparam_min
&&
2634 mmac
->nparam_max
== spec
.nparam_max
&&
2635 mmac
->plus
== spec
.plus
) {
2636 *mmac_p
= mmac
->next
;
2639 mmac_p
= &mmac
->next
;
2642 free_tlist(origline
);
2643 free_tlist(spec
.dlist
);
2644 return DIRECTIVE_FOUND
;
2648 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2649 tline
= tline
->next
;
2650 if (tline
->next
== NULL
) {
2651 free_tlist(origline
);
2652 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2653 return DIRECTIVE_FOUND
;
2655 t
= expand_smacro(tline
->next
);
2657 free_tlist(origline
);
2660 tokval
.t_type
= TOKEN_INVALID
;
2662 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2665 return DIRECTIVE_FOUND
;
2667 error(ERR_WARNING
|ERR_PASS1
,
2668 "trailing garbage after expression ignored");
2669 if (!is_simple(evalresult
)) {
2670 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2671 return DIRECTIVE_FOUND
;
2674 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2675 mmac
= mmac
->next_active
;
2677 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2678 } else if (mmac
->nparam
== 0) {
2680 "`%%rotate' invoked within macro without parameters");
2682 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2684 rotate
%= (int)mmac
->nparam
;
2686 rotate
+= mmac
->nparam
;
2688 mmac
->rotate
= rotate
;
2690 return DIRECTIVE_FOUND
;
2695 tline
= tline
->next
;
2696 } while (tok_type_(tline
, TOK_WHITESPACE
));
2698 if (tok_type_(tline
, TOK_ID
) &&
2699 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2702 tline
= tline
->next
;
2703 } while (tok_type_(tline
, TOK_WHITESPACE
));
2707 t
= expand_smacro(tline
);
2709 tokval
.t_type
= TOKEN_INVALID
;
2711 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2713 free_tlist(origline
);
2714 return DIRECTIVE_FOUND
;
2717 error(ERR_WARNING
|ERR_PASS1
,
2718 "trailing garbage after expression ignored");
2719 if (!is_simple(evalresult
)) {
2720 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2721 return DIRECTIVE_FOUND
;
2723 count
= reloc_value(evalresult
) + 1;
2725 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2728 free_tlist(origline
);
2730 tmp_defining
= defining
;
2731 defining
= nasm_malloc(sizeof(MMacro
));
2732 defining
->name
= NULL
; /* flags this macro as a %rep block */
2733 defining
->casesense
= false;
2734 defining
->plus
= false;
2735 defining
->nolist
= nolist
;
2736 defining
->in_progress
= count
;
2737 defining
->nparam_min
= defining
->nparam_max
= 0;
2738 defining
->defaults
= NULL
;
2739 defining
->dlist
= NULL
;
2740 defining
->expansion
= NULL
;
2741 defining
->next_active
= istk
->mstk
;
2742 defining
->rep_nest
= tmp_defining
;
2743 return DIRECTIVE_FOUND
;
2746 if (!defining
|| defining
->name
) {
2747 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2748 return DIRECTIVE_FOUND
;
2752 * Now we have a "macro" defined - although it has no name
2753 * and we won't be entering it in the hash tables - we must
2754 * push a macro-end marker for it on to istk->expansion.
2755 * After that, it will take care of propagating itself (a
2756 * macro-end marker line for a macro which is really a %rep
2757 * block will cause the macro to be re-expanded, complete
2758 * with another macro-end marker to ensure the process
2759 * continues) until the whole expansion is forcibly removed
2760 * from istk->expansion by a %exitrep.
2762 l
= nasm_malloc(sizeof(Line
));
2763 l
->next
= istk
->expansion
;
2764 l
->finishes
= defining
;
2766 istk
->expansion
= l
;
2768 istk
->mstk
= defining
;
2770 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2771 tmp_defining
= defining
;
2772 defining
= defining
->rep_nest
;
2773 free_tlist(origline
);
2774 return DIRECTIVE_FOUND
;
2778 * We must search along istk->expansion until we hit a
2779 * macro-end marker for a macro with no name. Then we set
2780 * its `in_progress' flag to 0.
2782 for (l
= istk
->expansion
; l
; l
= l
->next
)
2783 if (l
->finishes
&& !l
->finishes
->name
)
2787 l
->finishes
->in_progress
= 1;
2789 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2790 free_tlist(origline
);
2791 return DIRECTIVE_FOUND
;
2797 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2799 tline
= tline
->next
;
2801 tline
= expand_id(tline
);
2802 if (!tline
|| (tline
->type
!= TOK_ID
&&
2803 (tline
->type
!= TOK_PREPROC_ID
||
2804 tline
->text
[1] != '$'))) {
2805 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2807 free_tlist(origline
);
2808 return DIRECTIVE_FOUND
;
2811 ctx
= get_ctx(tline
->text
, &mname
, false);
2813 param_start
= tline
= tline
->next
;
2816 /* Expand the macro definition now for %xdefine and %ixdefine */
2817 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2818 tline
= expand_smacro(tline
);
2820 if (tok_is_(tline
, "(")) {
2822 * This macro has parameters.
2825 tline
= tline
->next
;
2829 error(ERR_NONFATAL
, "parameter identifier expected");
2830 free_tlist(origline
);
2831 return DIRECTIVE_FOUND
;
2833 if (tline
->type
!= TOK_ID
) {
2835 "`%s': parameter identifier expected",
2837 free_tlist(origline
);
2838 return DIRECTIVE_FOUND
;
2840 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2841 tline
= tline
->next
;
2843 if (tok_is_(tline
, ",")) {
2844 tline
= tline
->next
;
2846 if (!tok_is_(tline
, ")")) {
2848 "`)' expected to terminate macro template");
2849 free_tlist(origline
);
2850 return DIRECTIVE_FOUND
;
2856 tline
= tline
->next
;
2858 if (tok_type_(tline
, TOK_WHITESPACE
))
2859 last
= tline
, tline
= tline
->next
;
2864 if (t
->type
== TOK_ID
) {
2865 for (tt
= param_start
; tt
; tt
= tt
->next
)
2866 if (tt
->type
>= TOK_SMAC_PARAM
&&
2867 !strcmp(tt
->text
, t
->text
))
2871 t
->next
= macro_start
;
2876 * Good. We now have a macro name, a parameter count, and a
2877 * token list (in reverse order) for an expansion. We ought
2878 * to be OK just to create an SMacro, store it, and let
2879 * free_tlist have the rest of the line (which we have
2880 * carefully re-terminated after chopping off the expansion
2883 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2884 free_tlist(origline
);
2885 return DIRECTIVE_FOUND
;
2888 tline
= tline
->next
;
2890 tline
= expand_id(tline
);
2891 if (!tline
|| (tline
->type
!= TOK_ID
&&
2892 (tline
->type
!= TOK_PREPROC_ID
||
2893 tline
->text
[1] != '$'))) {
2894 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2895 free_tlist(origline
);
2896 return DIRECTIVE_FOUND
;
2899 error(ERR_WARNING
|ERR_PASS1
,
2900 "trailing garbage after macro name ignored");
2903 /* Find the context that symbol belongs to */
2904 ctx
= get_ctx(tline
->text
, &mname
, false);
2905 undef_smacro(ctx
, mname
);
2906 free_tlist(origline
);
2907 return DIRECTIVE_FOUND
;
2911 casesense
= (i
== PP_DEFSTR
);
2913 tline
= tline
->next
;
2915 tline
= expand_id(tline
);
2916 if (!tline
|| (tline
->type
!= TOK_ID
&&
2917 (tline
->type
!= TOK_PREPROC_ID
||
2918 tline
->text
[1] != '$'))) {
2919 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2921 free_tlist(origline
);
2922 return DIRECTIVE_FOUND
;
2925 ctx
= get_ctx(tline
->text
, &mname
, false);
2927 tline
= expand_smacro(tline
->next
);
2930 while (tok_type_(tline
, TOK_WHITESPACE
))
2931 tline
= delete_Token(tline
);
2933 p
= detoken(tline
, false);
2934 macro_start
= nasm_malloc(sizeof(*macro_start
));
2935 macro_start
->next
= NULL
;
2936 macro_start
->text
= nasm_quote(p
, strlen(p
));
2937 macro_start
->type
= TOK_STRING
;
2938 macro_start
->a
.mac
= NULL
;
2942 * We now have a macro name, an implicit parameter count of
2943 * zero, and a string token to use as an expansion. Create
2944 * and store an SMacro.
2946 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2947 free_tlist(origline
);
2948 return DIRECTIVE_FOUND
;
2953 StrList
*xsl
= NULL
;
2954 StrList
**xst
= &xsl
;
2958 tline
= tline
->next
;
2960 tline
= expand_id(tline
);
2961 if (!tline
|| (tline
->type
!= TOK_ID
&&
2962 (tline
->type
!= TOK_PREPROC_ID
||
2963 tline
->text
[1] != '$'))) {
2965 "`%%pathsearch' expects a macro identifier as first parameter");
2966 free_tlist(origline
);
2967 return DIRECTIVE_FOUND
;
2969 ctx
= get_ctx(tline
->text
, &mname
, false);
2971 tline
= expand_smacro(tline
->next
);
2975 while (tok_type_(t
, TOK_WHITESPACE
))
2978 if (!t
|| (t
->type
!= TOK_STRING
&&
2979 t
->type
!= TOK_INTERNAL_STRING
)) {
2980 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2982 free_tlist(origline
);
2983 return DIRECTIVE_FOUND
; /* but we did _something_ */
2986 error(ERR_WARNING
|ERR_PASS1
,
2987 "trailing garbage after `%%pathsearch' ignored");
2989 if (t
->type
!= TOK_INTERNAL_STRING
)
2990 nasm_unquote(p
, NULL
);
2992 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2995 fclose(fp
); /* Don't actually care about the file */
2997 macro_start
= nasm_malloc(sizeof(*macro_start
));
2998 macro_start
->next
= NULL
;
2999 macro_start
->text
= nasm_quote(p
, strlen(p
));
3000 macro_start
->type
= TOK_STRING
;
3001 macro_start
->a
.mac
= NULL
;
3006 * We now have a macro name, an implicit parameter count of
3007 * zero, and a string token to use as an expansion. Create
3008 * and store an SMacro.
3010 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3012 free_tlist(origline
);
3013 return DIRECTIVE_FOUND
;
3019 tline
= tline
->next
;
3021 tline
= expand_id(tline
);
3022 if (!tline
|| (tline
->type
!= TOK_ID
&&
3023 (tline
->type
!= TOK_PREPROC_ID
||
3024 tline
->text
[1] != '$'))) {
3026 "`%%strlen' expects a macro identifier as first parameter");
3027 free_tlist(origline
);
3028 return DIRECTIVE_FOUND
;
3030 ctx
= get_ctx(tline
->text
, &mname
, false);
3032 tline
= expand_smacro(tline
->next
);
3036 while (tok_type_(t
, TOK_WHITESPACE
))
3038 /* t should now point to the string */
3039 if (t
->type
!= TOK_STRING
) {
3041 "`%%strlen` requires string as second parameter");
3043 free_tlist(origline
);
3044 return DIRECTIVE_FOUND
;
3047 macro_start
= nasm_malloc(sizeof(*macro_start
));
3048 macro_start
->next
= NULL
;
3049 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3050 macro_start
->a
.mac
= NULL
;
3053 * We now have a macro name, an implicit parameter count of
3054 * zero, and a numeric token to use as an expansion. Create
3055 * and store an SMacro.
3057 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3059 free_tlist(origline
);
3060 return DIRECTIVE_FOUND
;
3065 tline
= tline
->next
;
3067 tline
= expand_id(tline
);
3068 if (!tline
|| (tline
->type
!= TOK_ID
&&
3069 (tline
->type
!= TOK_PREPROC_ID
||
3070 tline
->text
[1] != '$'))) {
3072 "`%%strcat' expects a macro identifier as first parameter");
3073 free_tlist(origline
);
3074 return DIRECTIVE_FOUND
;
3076 ctx
= get_ctx(tline
->text
, &mname
, false);
3078 tline
= expand_smacro(tline
->next
);
3082 for (t
= tline
; t
; t
= t
->next
) {
3084 case TOK_WHITESPACE
:
3087 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3090 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3092 /* else fall through */
3095 "non-string passed to `%%strcat' (%d)", t
->type
);
3097 free_tlist(origline
);
3098 return DIRECTIVE_FOUND
;
3102 p
= pp
= nasm_malloc(len
);
3104 for (t
= tline
; t
; t
= t
->next
) {
3105 if (t
->type
== TOK_STRING
) {
3106 memcpy(p
, t
->text
, t
->a
.len
);
3112 * We now have a macro name, an implicit parameter count of
3113 * zero, and a numeric token to use as an expansion. Create
3114 * and store an SMacro.
3116 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3117 macro_start
->text
= nasm_quote(pp
, len
);
3119 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3121 free_tlist(origline
);
3122 return DIRECTIVE_FOUND
;
3131 tline
= tline
->next
;
3133 tline
= expand_id(tline
);
3134 if (!tline
|| (tline
->type
!= TOK_ID
&&
3135 (tline
->type
!= TOK_PREPROC_ID
||
3136 tline
->text
[1] != '$'))) {
3138 "`%%substr' expects a macro identifier as first parameter");
3139 free_tlist(origline
);
3140 return DIRECTIVE_FOUND
;
3142 ctx
= get_ctx(tline
->text
, &mname
, false);
3144 tline
= expand_smacro(tline
->next
);
3148 while (tok_type_(t
, TOK_WHITESPACE
))
3151 /* t should now point to the string */
3152 if (t
->type
!= TOK_STRING
) {
3154 "`%%substr` requires string as second parameter");
3156 free_tlist(origline
);
3157 return DIRECTIVE_FOUND
;
3162 tokval
.t_type
= TOKEN_INVALID
;
3163 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3167 free_tlist(origline
);
3168 return DIRECTIVE_FOUND
;
3169 } else if (!is_simple(evalresult
)) {
3170 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3172 free_tlist(origline
);
3173 return DIRECTIVE_FOUND
;
3175 a1
= evalresult
->value
-1;
3177 while (tok_type_(tt
, TOK_WHITESPACE
))
3180 a2
= 1; /* Backwards compatibility: one character */
3182 tokval
.t_type
= TOKEN_INVALID
;
3183 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3187 free_tlist(origline
);
3188 return DIRECTIVE_FOUND
;
3189 } else if (!is_simple(evalresult
)) {
3190 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3192 free_tlist(origline
);
3193 return DIRECTIVE_FOUND
;
3195 a2
= evalresult
->value
;
3198 len
= nasm_unquote(t
->text
, NULL
);
3201 if (a1
+a2
> (int64_t)len
)
3204 macro_start
= nasm_malloc(sizeof(*macro_start
));
3205 macro_start
->next
= NULL
;
3206 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3207 macro_start
->type
= TOK_STRING
;
3208 macro_start
->a
.mac
= NULL
;
3211 * We now have a macro name, an implicit parameter count of
3212 * zero, and a numeric token to use as an expansion. Create
3213 * and store an SMacro.
3215 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3217 free_tlist(origline
);
3218 return DIRECTIVE_FOUND
;
3223 casesense
= (i
== PP_ASSIGN
);
3225 tline
= tline
->next
;
3227 tline
= expand_id(tline
);
3228 if (!tline
|| (tline
->type
!= TOK_ID
&&
3229 (tline
->type
!= TOK_PREPROC_ID
||
3230 tline
->text
[1] != '$'))) {
3232 "`%%%sassign' expects a macro identifier",
3233 (i
== PP_IASSIGN
? "i" : ""));
3234 free_tlist(origline
);
3235 return DIRECTIVE_FOUND
;
3237 ctx
= get_ctx(tline
->text
, &mname
, false);
3239 tline
= expand_smacro(tline
->next
);
3244 tokval
.t_type
= TOKEN_INVALID
;
3246 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3249 free_tlist(origline
);
3250 return DIRECTIVE_FOUND
;
3254 error(ERR_WARNING
|ERR_PASS1
,
3255 "trailing garbage after expression ignored");
3257 if (!is_simple(evalresult
)) {
3259 "non-constant value given to `%%%sassign'",
3260 (i
== PP_IASSIGN
? "i" : ""));
3261 free_tlist(origline
);
3262 return DIRECTIVE_FOUND
;
3265 macro_start
= nasm_malloc(sizeof(*macro_start
));
3266 macro_start
->next
= NULL
;
3267 make_tok_num(macro_start
, reloc_value(evalresult
));
3268 macro_start
->a
.mac
= NULL
;
3271 * We now have a macro name, an implicit parameter count of
3272 * zero, and a numeric token to use as an expansion. Create
3273 * and store an SMacro.
3275 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3276 free_tlist(origline
);
3277 return DIRECTIVE_FOUND
;
3281 * Syntax is `%line nnn[+mmm] [filename]'
3283 tline
= tline
->next
;
3285 if (!tok_type_(tline
, TOK_NUMBER
)) {
3286 error(ERR_NONFATAL
, "`%%line' expects line number");
3287 free_tlist(origline
);
3288 return DIRECTIVE_FOUND
;
3290 k
= readnum(tline
->text
, &err
);
3292 tline
= tline
->next
;
3293 if (tok_is_(tline
, "+")) {
3294 tline
= tline
->next
;
3295 if (!tok_type_(tline
, TOK_NUMBER
)) {
3296 error(ERR_NONFATAL
, "`%%line' expects line increment");
3297 free_tlist(origline
);
3298 return DIRECTIVE_FOUND
;
3300 m
= readnum(tline
->text
, &err
);
3301 tline
= tline
->next
;
3307 nasm_free(src_set_fname(detoken(tline
, false)));
3309 free_tlist(origline
);
3310 return DIRECTIVE_FOUND
;
3314 "preprocessor directive `%s' not yet implemented",
3316 return DIRECTIVE_FOUND
;
3321 * Ensure that a macro parameter contains a condition code and
3322 * nothing else. Return the condition code index if so, or -1
3325 static int find_cc(Token
* t
)
3331 return -1; /* Probably a %+ without a space */
3334 if (t
->type
!= TOK_ID
)
3338 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3342 j
= elements(conditions
);
3345 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3360 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3362 Token
**tail
, *t
, *tt
;
3364 bool did_paste
= false;
3367 /* Now handle token pasting... */
3370 while ((t
= *tail
) && (tt
= t
->next
)) {
3372 case TOK_WHITESPACE
:
3373 if (tt
->type
== TOK_WHITESPACE
) {
3374 /* Zap adjacent whitespace tokens */
3375 t
->next
= delete_Token(tt
);
3377 /* Do not advance paste_head here */
3382 case TOK_PREPROC_ID
:
3389 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3390 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3391 tt
->type
== TOK_OTHER
)) {
3392 len
+= strlen(tt
->text
);
3396 /* Now tt points to the first token after the potential
3398 if (tt
!= t
->next
) {
3399 /* We have at least two tokens... */
3400 len
+= strlen(t
->text
);
3401 p
= tmp
= nasm_malloc(len
+1);
3405 p
= strchr(p
, '\0');
3406 t
= delete_Token(t
);
3409 t
= *tail
= tokenize(tmp
);
3416 t
->next
= tt
; /* Attach the remaining token chain */
3424 case TOK_PASTE
: /* %+ */
3425 if (handle_paste_tokens
) {
3426 /* Zap %+ and whitespace tokens to the right */
3427 while (t
&& (t
->type
== TOK_WHITESPACE
||
3428 t
->type
== TOK_PASTE
))
3429 t
= *tail
= delete_Token(t
);
3430 if (!paste_head
|| !t
)
3431 break; /* Nothing to paste with */
3435 while (tok_type_(tt
, TOK_WHITESPACE
))
3436 tt
= t
->next
= delete_Token(tt
);
3439 tmp
= nasm_strcat(t
->text
, tt
->text
);
3441 tt
= delete_Token(tt
);
3442 t
= *tail
= tokenize(tmp
);
3448 t
->next
= tt
; /* Attach the remaining token chain */
3455 /* else fall through */
3457 tail
= paste_head
= &t
->next
;
3464 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3465 * %-n) and MMacro-local identifiers (%%foo) as well as
3466 * macro indirection (%[...]).
3468 static Token
*expand_mmac_params(Token
* tline
)
3470 Token
*t
, *tt
, **tail
, *thead
;
3471 bool changed
= false;
3477 if (tline
->type
== TOK_PREPROC_ID
&&
3478 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3479 && tline
->text
[2]) || tline
->text
[1] == '%'
3480 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3482 int type
= 0, cc
; /* type = 0 to placate optimisers */
3489 tline
= tline
->next
;
3492 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3493 mac
= mac
->next_active
;
3495 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3497 switch (t
->text
[1]) {
3499 * We have to make a substitution of one of the
3500 * forms %1, %-1, %+1, %%foo, %0.
3504 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3505 text
= nasm_strdup(tmpbuf
);
3509 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3511 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3514 n
= atoi(t
->text
+ 2) - 1;
3515 if (n
>= mac
->nparam
)
3518 if (mac
->nparam
> 1)
3519 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3520 tt
= mac
->params
[n
];
3525 "macro parameter %d is not a condition code",
3530 if (inverse_ccs
[cc
] == -1) {
3532 "condition code `%s' is not invertible",
3536 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3540 n
= atoi(t
->text
+ 2) - 1;
3541 if (n
>= mac
->nparam
)
3544 if (mac
->nparam
> 1)
3545 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3546 tt
= mac
->params
[n
];
3551 "macro parameter %d is not a condition code",
3556 text
= nasm_strdup(conditions
[cc
]);
3560 n
= atoi(t
->text
+ 1) - 1;
3561 if (n
>= mac
->nparam
)
3564 if (mac
->nparam
> 1)
3565 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3566 tt
= mac
->params
[n
];
3569 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3570 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3571 tail
= &(*tail
)->next
;
3575 text
= NULL
; /* we've done it here */
3590 } else if (tline
->type
== TOK_INDIRECT
) {
3592 tline
= tline
->next
;
3593 tt
= tokenize(t
->text
);
3594 tt
= expand_mmac_params(tt
);
3595 tt
= expand_smacro(tt
);
3598 tt
->a
.mac
= NULL
; /* Necessary? */
3606 tline
= tline
->next
;
3614 paste_tokens(&thead
, false);
3620 * Expand all single-line macro calls made in the given line.
3621 * Return the expanded version of the line. The original is deemed
3622 * to be destroyed in the process. (In reality we'll just move
3623 * Tokens from input to output a lot of the time, rather than
3624 * actually bothering to destroy and replicate.)
3626 #define DEADMAN_LIMIT (1 << 20)
3628 static Token
*expand_smacro(Token
* tline
)
3630 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3631 struct hash_table
*smtbl
;
3632 SMacro
*head
= NULL
, *m
;
3635 unsigned int nparam
, sparam
;
3637 Token
*org_tline
= tline
;
3640 int deadman
= DEADMAN_LIMIT
;
3644 * Trick: we should avoid changing the start token pointer since it can
3645 * be contained in "next" field of other token. Because of this
3646 * we allocate a copy of first token and work with it; at the end of
3647 * routine we copy it back
3651 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3653 tline
->a
.mac
= org_tline
->a
.mac
;
3654 nasm_free(org_tline
->text
);
3655 org_tline
->text
= NULL
;
3658 expanded
= true; /* Always expand %+ at least once */
3664 while (tline
) { /* main token loop */
3666 error(ERR_NONFATAL
, "interminable macro recursion");
3670 if ((mname
= tline
->text
)) {
3671 /* if this token is a local macro, look in local context */
3672 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3673 ctx
= get_ctx(mname
, &mname
, true);
3676 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3677 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3680 * We've hit an identifier. As in is_mmacro below, we first
3681 * check whether the identifier is a single-line macro at
3682 * all, then think about checking for parameters if
3685 for (m
= head
; m
; m
= m
->next
)
3686 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3692 if (m
->nparam
== 0) {
3694 * Simple case: the macro is parameterless. Discard the
3695 * one token that the macro call took, and push the
3696 * expansion back on the to-do stack.
3698 if (!m
->expansion
) {
3699 if (!strcmp("__FILE__", m
->name
)) {
3702 src_get(&num
, &file
);
3703 tline
->text
= nasm_quote(file
, strlen(file
));
3704 tline
->type
= TOK_STRING
;
3708 if (!strcmp("__LINE__", m
->name
)) {
3709 nasm_free(tline
->text
);
3710 make_tok_num(tline
, src_get_linnum());
3713 if (!strcmp("__BITS__", m
->name
)) {
3714 nasm_free(tline
->text
);
3715 make_tok_num(tline
, globalbits
);
3718 tline
= delete_Token(tline
);
3723 * Complicated case: at least one macro with this name
3724 * exists and takes parameters. We must find the
3725 * parameters in the call, count them, find the SMacro
3726 * that corresponds to that form of the macro call, and
3727 * substitute for the parameters when we expand. What a
3730 /*tline = tline->next;
3731 skip_white_(tline); */
3734 while (tok_type_(t
, TOK_SMAC_END
)) {
3735 t
->a
.mac
->in_progress
= false;
3737 t
= tline
->next
= delete_Token(t
);
3740 } while (tok_type_(tline
, TOK_WHITESPACE
));
3741 if (!tok_is_(tline
, "(")) {
3743 * This macro wasn't called with parameters: ignore
3744 * the call. (Behaviour borrowed from gnu cpp.)
3753 sparam
= PARAM_DELTA
;
3754 params
= nasm_malloc(sparam
* sizeof(Token
*));
3755 params
[0] = tline
->next
;
3756 paramsize
= nasm_malloc(sparam
* sizeof(int));
3758 while (true) { /* parameter loop */
3760 * For some unusual expansions
3761 * which concatenates function call
3764 while (tok_type_(t
, TOK_SMAC_END
)) {
3765 t
->a
.mac
->in_progress
= false;
3767 t
= tline
->next
= delete_Token(t
);
3773 "macro call expects terminating `)'");
3776 if (tline
->type
== TOK_WHITESPACE
3778 if (paramsize
[nparam
])
3781 params
[nparam
] = tline
->next
;
3782 continue; /* parameter loop */
3784 if (tline
->type
== TOK_OTHER
3785 && tline
->text
[1] == 0) {
3786 char ch
= tline
->text
[0];
3787 if (ch
== ',' && !paren
&& brackets
<= 0) {
3788 if (++nparam
>= sparam
) {
3789 sparam
+= PARAM_DELTA
;
3790 params
= nasm_realloc(params
,
3795 nasm_realloc(paramsize
,
3799 params
[nparam
] = tline
->next
;
3800 paramsize
[nparam
] = 0;
3802 continue; /* parameter loop */
3805 (brackets
> 0 || (brackets
== 0 &&
3806 !paramsize
[nparam
])))
3808 if (!(brackets
++)) {
3809 params
[nparam
] = tline
->next
;
3810 continue; /* parameter loop */
3813 if (ch
== '}' && brackets
> 0)
3814 if (--brackets
== 0) {
3816 continue; /* parameter loop */
3818 if (ch
== '(' && !brackets
)
3820 if (ch
== ')' && brackets
<= 0)
3826 error(ERR_NONFATAL
, "braces do not "
3827 "enclose all of macro parameter");
3829 paramsize
[nparam
] += white
+ 1;
3831 } /* parameter loop */
3833 while (m
&& (m
->nparam
!= nparam
||
3834 mstrcmp(m
->name
, mname
,
3838 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3839 "macro `%s' exists, "
3840 "but not taking %d parameters",
3841 mstart
->text
, nparam
);
3844 if (m
&& m
->in_progress
)
3846 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3848 * Design question: should we handle !tline, which
3849 * indicates missing ')' here, or expand those
3850 * macros anyway, which requires the (t) test a few
3854 nasm_free(paramsize
);
3858 * Expand the macro: we are placed on the last token of the
3859 * call, so that we can easily split the call from the
3860 * following tokens. We also start by pushing an SMAC_END
3861 * token for the cycle removal.
3868 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3870 m
->in_progress
= true;
3872 for (t
= m
->expansion
; t
; t
= t
->next
) {
3873 if (t
->type
>= TOK_SMAC_PARAM
) {
3874 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3878 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3879 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3882 new_Token(tline
, ttt
->type
, ttt
->text
,
3888 } else if (t
->type
== TOK_PREPROC_Q
) {
3889 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3891 } else if (t
->type
== TOK_PREPROC_QQ
) {
3892 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3895 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3901 * Having done that, get rid of the macro call, and clean
3902 * up the parameters.
3905 nasm_free(paramsize
);
3908 continue; /* main token loop */
3913 if (tline
->type
== TOK_SMAC_END
) {
3914 tline
->a
.mac
->in_progress
= false;
3915 tline
= delete_Token(tline
);
3918 tline
= tline
->next
;
3926 * Now scan the entire line and look for successive TOK_IDs that resulted
3927 * after expansion (they can't be produced by tokenize()). The successive
3928 * TOK_IDs should be concatenated.
3929 * Also we look for %+ tokens and concatenate the tokens before and after
3930 * them (without white spaces in between).
3932 if (expanded
&& paste_tokens(&thead
, true)) {
3934 * If we concatenated something, *and* we had previously expanded
3935 * an actual macro, scan the lines again for macros...
3944 *org_tline
= *thead
;
3945 /* since we just gave text to org_line, don't free it */
3947 delete_Token(thead
);
3949 /* the expression expanded to empty line;
3950 we can't return NULL for some reasons
3951 we just set the line to a single WHITESPACE token. */
3952 memset(org_tline
, 0, sizeof(*org_tline
));
3953 org_tline
->text
= NULL
;
3954 org_tline
->type
= TOK_WHITESPACE
;
3963 * Similar to expand_smacro but used exclusively with macro identifiers
3964 * right before they are fetched in. The reason is that there can be
3965 * identifiers consisting of several subparts. We consider that if there
3966 * are more than one element forming the name, user wants a expansion,
3967 * otherwise it will be left as-is. Example:
3971 * the identifier %$abc will be left as-is so that the handler for %define
3972 * will suck it and define the corresponding value. Other case:
3974 * %define _%$abc cde
3976 * In this case user wants name to be expanded *before* %define starts
3977 * working, so we'll expand %$abc into something (if it has a value;
3978 * otherwise it will be left as-is) then concatenate all successive
3981 static Token
*expand_id(Token
* tline
)
3983 Token
*cur
, *oldnext
= NULL
;
3985 if (!tline
|| !tline
->next
)
3990 (cur
->next
->type
== TOK_ID
||
3991 cur
->next
->type
== TOK_PREPROC_ID
3992 || cur
->next
->type
== TOK_NUMBER
))
3995 /* If identifier consists of just one token, don't expand */
4000 oldnext
= cur
->next
; /* Detach the tail past identifier */
4001 cur
->next
= NULL
; /* so that expand_smacro stops here */
4004 tline
= expand_smacro(tline
);
4007 /* expand_smacro possibly changhed tline; re-scan for EOL */
4009 while (cur
&& cur
->next
)
4012 cur
->next
= oldnext
;
4019 * Determine whether the given line constitutes a multi-line macro
4020 * call, and return the MMacro structure called if so. Doesn't have
4021 * to check for an initial label - that's taken care of in
4022 * expand_mmacro - but must check numbers of parameters. Guaranteed
4023 * to be called with tline->type == TOK_ID, so the putative macro
4024 * name is easy to find.
4026 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4032 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4035 * Efficiency: first we see if any macro exists with the given
4036 * name. If not, we can return NULL immediately. _Then_ we
4037 * count the parameters, and then we look further along the
4038 * list if necessary to find the proper MMacro.
4040 for (m
= head
; m
; m
= m
->next
)
4041 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4047 * OK, we have a potential macro. Count and demarcate the
4050 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4053 * So we know how many parameters we've got. Find the MMacro
4054 * structure that handles this number.
4057 if (m
->nparam_min
<= nparam
4058 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4060 * This one is right. Just check if cycle removal
4061 * prohibits us using it before we actually celebrate...
4063 if (m
->in_progress
) {
4066 "self-reference in multi-line macro `%s'", m
->name
);
4072 * It's right, and we can use it. Add its default
4073 * parameters to the end of our list if necessary.
4075 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4077 nasm_realloc(params
,
4078 ((m
->nparam_min
+ m
->ndefs
+
4079 1) * sizeof(*params
)));
4080 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4081 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4086 * If we've gone over the maximum parameter count (and
4087 * we're in Plus mode), ignore parameters beyond
4090 if (m
->plus
&& nparam
> m
->nparam_max
)
4091 nparam
= m
->nparam_max
;
4093 * Then terminate the parameter list, and leave.
4095 if (!params
) { /* need this special case */
4096 params
= nasm_malloc(sizeof(*params
));
4099 params
[nparam
] = NULL
;
4100 *params_array
= params
;
4104 * This one wasn't right: look for the next one with the
4107 for (m
= m
->next
; m
; m
= m
->next
)
4108 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4113 * After all that, we didn't find one with the right number of
4114 * parameters. Issue a warning, and fail to expand the macro.
4116 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4117 "macro `%s' exists, but not taking %d parameters",
4118 tline
->text
, nparam
);
4124 * Expand the multi-line macro call made by the given line, if
4125 * there is one to be expanded. If there is, push the expansion on
4126 * istk->expansion and return 1. Otherwise return 0.
4128 static int expand_mmacro(Token
* tline
)
4130 Token
*startline
= tline
;
4131 Token
*label
= NULL
;
4132 int dont_prepend
= 0;
4133 Token
**params
, *t
, *mtok
, *tt
;
4136 int i
, nparam
, *paramlen
;
4141 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4142 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4145 m
= is_mmacro(t
, ¶ms
);
4151 * We have an id which isn't a macro call. We'll assume
4152 * it might be a label; we'll also check to see if a
4153 * colon follows it. Then, if there's another id after
4154 * that lot, we'll check it again for macro-hood.
4158 if (tok_type_(t
, TOK_WHITESPACE
))
4159 last
= t
, t
= t
->next
;
4160 if (tok_is_(t
, ":")) {
4162 last
= t
, t
= t
->next
;
4163 if (tok_type_(t
, TOK_WHITESPACE
))
4164 last
= t
, t
= t
->next
;
4166 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4174 * Fix up the parameters: this involves stripping leading and
4175 * trailing whitespace, then stripping braces if they are
4178 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4179 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4181 for (i
= 0; params
[i
]; i
++) {
4183 int comma
= (!m
->plus
|| i
< nparam
- 1);
4187 if (tok_is_(t
, "{"))
4188 t
= t
->next
, brace
= true, comma
= false;
4192 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4193 break; /* ... because we have hit a comma */
4194 if (comma
&& t
->type
== TOK_WHITESPACE
4195 && tok_is_(t
->next
, ","))
4196 break; /* ... or a space then a comma */
4197 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4198 break; /* ... or a brace */
4205 * OK, we have a MMacro structure together with a set of
4206 * parameters. We must now go through the expansion and push
4207 * copies of each Line on to istk->expansion. Substitution of
4208 * parameter tokens and macro-local tokens doesn't get done
4209 * until the single-line macro substitution process; this is
4210 * because delaying them allows us to change the semantics
4211 * later through %rotate.
4213 * First, push an end marker on to istk->expansion, mark this
4214 * macro as in progress, and set up its invocation-specific
4217 ll
= nasm_malloc(sizeof(Line
));
4218 ll
->next
= istk
->expansion
;
4221 istk
->expansion
= ll
;
4223 m
->in_progress
= true;
4228 m
->paramlen
= paramlen
;
4229 m
->unique
= unique
++;
4232 m
->next_active
= istk
->mstk
;
4235 for (l
= m
->expansion
; l
; l
= l
->next
) {
4238 ll
= nasm_malloc(sizeof(Line
));
4239 ll
->finishes
= NULL
;
4240 ll
->next
= istk
->expansion
;
4241 istk
->expansion
= ll
;
4244 for (t
= l
->first
; t
; t
= t
->next
) {
4248 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4250 case TOK_PREPROC_QQ
:
4251 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4253 case TOK_PREPROC_ID
:
4254 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4262 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4271 * If we had a label, push it on as the first line of
4272 * the macro expansion.
4275 if (dont_prepend
< 0)
4276 free_tlist(startline
);
4278 ll
= nasm_malloc(sizeof(Line
));
4279 ll
->finishes
= NULL
;
4280 ll
->next
= istk
->expansion
;
4281 istk
->expansion
= ll
;
4282 ll
->first
= startline
;
4283 if (!dont_prepend
) {
4285 label
= label
->next
;
4286 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4291 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4296 /* The function that actually does the error reporting */
4297 static void verror(int severity
, const char *fmt
, va_list arg
)
4301 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4303 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4304 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4305 istk
->mstk
->lineno
, buff
);
4307 _error(severity
, "%s", buff
);
4311 * Since preprocessor always operate only on the line that didn't
4312 * arrived yet, we should always use ERR_OFFBY1.
4314 static void error(int severity
, const char *fmt
, ...)
4318 /* If we're in a dead branch of IF or something like it, ignore the error */
4319 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4323 verror(severity
, fmt
, arg
);
4328 * Because %else etc are evaluated in the state context
4329 * of the previous branch, errors might get lost with error():
4330 * %if 0 ... %else trailing garbage ... %endif
4331 * So %else etc should report errors with this function.
4333 static void error_precond(int severity
, const char *fmt
, ...)
4337 /* Only ignore the error if it's really in a dead branch */
4338 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4342 verror(severity
, fmt
, arg
);
4347 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4348 ListGen
* listgen
, StrList
**deplist
)
4354 istk
= nasm_malloc(sizeof(Include
));
4357 istk
->expansion
= NULL
;
4359 istk
->fp
= fopen(file
, "r");
4361 src_set_fname(nasm_strdup(file
));
4365 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4368 nested_mac_count
= 0;
4369 nested_rep_count
= 0;
4372 if (tasm_compatible_mode
) {
4373 stdmacpos
= nasm_stdmac
;
4375 stdmacpos
= nasm_stdmac_after_tasm
;
4377 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4383 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4384 * The caller, however, will also pass in 3 for preprocess-only so
4385 * we can set __PASS__ accordingly.
4387 pass
= apass
> 2 ? 2 : apass
;
4389 dephead
= deptail
= deplist
;
4391 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4393 strcpy(sl
->str
, file
);
4395 deptail
= &sl
->next
;
4399 * Define the __PASS__ macro. This is defined here unlike
4400 * all the other builtins, because it is special -- it varies between
4403 t
= nasm_malloc(sizeof(*t
));
4405 make_tok_num(t
, apass
);
4407 define_smacro(NULL
, "__PASS__", true, 0, t
);
4410 static char *pp_getline(void)
4417 * Fetch a tokenized line, either from the macro-expansion
4418 * buffer or from the input file.
4421 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4422 Line
*l
= istk
->expansion
;
4423 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4427 * This is a macro-end marker for a macro with no
4428 * name, which means it's not really a macro at all
4429 * but a %rep block, and the `in_progress' field is
4430 * more than 1, meaning that we still need to
4431 * repeat. (1 means the natural last repetition; 0
4432 * means termination by %exitrep.) We have
4433 * therefore expanded up to the %endrep, and must
4434 * push the whole block on to the expansion buffer
4435 * again. We don't bother to remove the macro-end
4436 * marker: we'd only have to generate another one
4439 l
->finishes
->in_progress
--;
4440 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4441 Token
*t
, *tt
, **tail
;
4443 ll
= nasm_malloc(sizeof(Line
));
4444 ll
->next
= istk
->expansion
;
4445 ll
->finishes
= NULL
;
4449 for (t
= l
->first
; t
; t
= t
->next
) {
4450 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4452 new_Token(NULL
, t
->type
, t
->text
, 0);
4457 istk
->expansion
= ll
;
4461 * Check whether a `%rep' was started and not ended
4462 * within this macro expansion. This can happen and
4463 * should be detected. It's a fatal error because
4464 * I'm too confused to work out how to recover
4470 "defining with name in expansion");
4471 else if (istk
->mstk
->name
)
4473 "`%%rep' without `%%endrep' within"
4474 " expansion of macro `%s'",
4479 * FIXME: investigate the relationship at this point between
4480 * istk->mstk and l->finishes
4483 MMacro
*m
= istk
->mstk
;
4484 istk
->mstk
= m
->next_active
;
4487 * This was a real macro call, not a %rep, and
4488 * therefore the parameter information needs to
4491 nasm_free(m
->params
);
4492 free_tlist(m
->iline
);
4493 nasm_free(m
->paramlen
);
4494 l
->finishes
->in_progress
= false;
4498 istk
->expansion
= l
->next
;
4500 list
->downlevel(LIST_MACRO
);
4503 while (1) { /* until we get a line we can use */
4505 if (istk
->expansion
) { /* from a macro expansion */
4507 Line
*l
= istk
->expansion
;
4509 istk
->mstk
->lineno
++;
4511 istk
->expansion
= l
->next
;
4513 p
= detoken(tline
, false);
4514 list
->line(LIST_MACRO
, p
);
4519 if (line
) { /* from the current input file */
4520 line
= prepreproc(line
);
4521 tline
= tokenize(line
);
4526 * The current file has ended; work down the istk
4533 "expected `%%endif' before end of file");
4534 /* only set line and file name if there's a next node */
4536 src_set_linnum(i
->lineno
);
4537 nasm_free(src_set_fname(i
->fname
));
4540 list
->downlevel(LIST_INCLUDE
);
4544 if (istk
->expansion
&& istk
->expansion
->finishes
)
4550 * We must expand MMacro parameters and MMacro-local labels
4551 * _before_ we plunge into directive processing, to cope
4552 * with things like `%define something %1' such as STRUC
4553 * uses. Unless we're _defining_ a MMacro, in which case
4554 * those tokens should be left alone to go into the
4555 * definition; and unless we're in a non-emitting
4556 * condition, in which case we don't want to meddle with
4559 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4560 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4561 tline
= expand_mmac_params(tline
);
4565 * Check the line to see if it's a preprocessor directive.
4567 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4569 } else if (defining
) {
4571 * We're defining a multi-line macro. We emit nothing
4573 * shove the tokenized line on to the macro definition.
4575 Line
*l
= nasm_malloc(sizeof(Line
));
4576 l
->next
= defining
->expansion
;
4579 defining
->expansion
= l
;
4581 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4583 * We're in a non-emitting branch of a condition block.
4584 * Emit nothing at all, not even a blank line: when we
4585 * emerge from the condition we'll give a line-number
4586 * directive so we keep our place correctly.
4590 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4592 * We're in a %rep block which has been terminated, so
4593 * we're walking through to the %endrep without
4594 * emitting anything. Emit nothing at all, not even a
4595 * blank line: when we emerge from the %rep block we'll
4596 * give a line-number directive so we keep our place
4602 tline
= expand_smacro(tline
);
4603 if (!expand_mmacro(tline
)) {
4605 * De-tokenize the line again, and emit it.
4607 line
= detoken(tline
, true);
4611 continue; /* expand_mmacro calls free_tlist */
4619 static void pp_cleanup(int pass
)
4622 if(defining
->name
) {
4624 "end of file while still defining macro `%s'",
4627 error(ERR_NONFATAL
, "end of file while still in %%rep");
4630 free_mmacro(defining
);
4639 nasm_free(i
->fname
);
4644 nasm_free(src_set_fname(NULL
));
4649 while ((i
= ipath
)) {
4658 void pp_include_path(char *path
)
4662 i
= nasm_malloc(sizeof(IncPath
));
4663 i
->path
= path
? nasm_strdup(path
) : NULL
;
4666 if (ipath
!= NULL
) {
4668 while (j
->next
!= NULL
)
4676 void pp_pre_include(char *fname
)
4678 Token
*inc
, *space
, *name
;
4681 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4682 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4683 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4685 l
= nasm_malloc(sizeof(Line
));
4692 void pp_pre_define(char *definition
)
4698 equals
= strchr(definition
, '=');
4699 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4700 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4703 space
->next
= tokenize(definition
);
4707 l
= nasm_malloc(sizeof(Line
));
4714 void pp_pre_undefine(char *definition
)
4719 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4720 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4721 space
->next
= tokenize(definition
);
4723 l
= nasm_malloc(sizeof(Line
));
4731 * Added by Keith Kanios:
4733 * This function is used to assist with "runtime" preprocessor
4734 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4736 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4737 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4740 void pp_runtime(char *definition
)
4744 def
= tokenize(definition
);
4745 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4750 void pp_extra_stdmac(macros_t
*macros
)
4752 extrastdmac
= macros
;
4755 static void make_tok_num(Token
* tok
, int64_t val
)
4758 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4759 tok
->text
= nasm_strdup(numbuf
);
4760 tok
->type
= TOK_NUMBER
;