1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro
;
85 typedef struct ExpDef ExpDef
;
86 typedef struct ExpInv ExpInv
;
87 typedef struct Context Context
;
88 typedef struct Token Token
;
89 typedef struct Blocks Blocks
;
90 typedef struct Line Line
;
91 typedef struct Include Include
;
92 typedef struct Cond Cond
;
93 typedef struct IncPath IncPath
;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * The context stack is composed of a linked list of these.
122 struct hash_table localmac
;
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
134 * %define a(x,y) ( (x) & ~(y) )
136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
146 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
147 TOK_PREPROC_ID
, TOK_STRING
,
148 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
150 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
152 TOK_INDIRECT
, /* %[...] */
153 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
161 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
162 size_t len
; /* scratch length field */
163 } a
; /* Auxiliary data */
164 enum pp_token_type type
;
168 * Expansion definitions are stored as a linked list of
169 * these, which is essentially a container to allow several linked
172 * Note that in this module, linked lists are treated as stacks
173 * wherever possible. For this reason, Lines are _pushed_ on to the
174 * `last' field in ExpDef structures, so that the linked list,
175 * if walked, would emit the expansion lines in the proper order.
186 EXP_NONE
= 0, EXP_PREDEF
,
189 EXP_COMMENT
, EXP_FINAL
,
190 EXP_MAX
= INT_MAX
/* Keep compiler from reducing the range */
194 * Store the definition of an expansion, in which is any
195 * preprocessor directive that has an ending pair.
197 * This design allows for arbitrary expansion/recursion depth,
198 * upto the DEADMAN_LIMIT.
200 * The `next' field is used for storing ExpDef in hash tables; the
201 * `prev' field is for the global `expansions` linked-list.
204 ExpDef
*prev
; /* previous definition */
205 ExpDef
*next
; /* next in hash table */
206 enum pp_exp_type type
; /* expansion type */
208 int nparam_min
, nparam_max
;
210 bool plus
; /* is the last parameter greedy? */
211 bool nolist
; /* is this expansion listing-inhibited? */
212 Token
*dlist
; /* all defaults as one list */
213 Token
**defaults
; /* parameter default pointers */
214 int ndefs
; /* number of default parameters */
220 uint32_t def_depth
; /* current number of definition pairs deep */
221 uint32_t cur_depth
; /* current number of expansions */
222 uint32_t max_depth
; /* maximum number of expansions allowed */
224 int state
; /* condition state */
225 bool ignoring
; /* ignoring definition lines */
229 * Store the invocation of an expansion.
231 * The `prev' field is for the `istk->expansion` linked-list.
233 * When an expansion is being expanded, `params', `iline', `nparam',
234 * `paramlen', `rotate' and `unique' are local to the invocation.
237 ExpInv
*prev
; /* previous invocation */
238 enum pp_exp_type type
; /* expansion type */
239 ExpDef
*def
; /* pointer to expansion definition */
240 Line
*label
; /* pointer to label */
241 char *label_text
; /* pointer to label text */
242 Line
*current
; /* pointer to current line in invocation */
244 Token
**params
; /* actual parameters */
245 Token
*iline
; /* invocation line */
246 unsigned int nparam
, rotate
;
251 int lineno
; /* current line number in expansion */
255 * To handle an arbitrary level of file inclusion, we maintain a
256 * stack (ie linked list) of these things.
268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
286 * These states are for use just after %if or %elif: IF_TRUE
287 * means the condition has evaluated to truth so we are
288 * currently emitting, whereas IF_FALSE means we are not
289 * currently emitting but will start doing so if a %else comes
290 * up. In these states, all directives are admissible: %elif,
291 * %else and %endif. (And of course %if.)
293 COND_IF_TRUE
, COND_IF_FALSE
,
295 * These states come up after a %else: ELSE_TRUE means we're
296 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
297 * any %elif or %else will cause an error.
299 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
301 * These states mean that we're not emitting now, and also that
302 * nothing until %endif will be emitted at all. COND_DONE is
303 * used when we've had our moment of emission
304 * and have now started seeing %elifs. COND_NEVER is used when
305 * the condition construct in question is contained within a
306 * non-emitting branch of a larger condition construct,
307 * or if there is an error.
309 COND_DONE
, COND_NEVER
311 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
314 * These defines are used as the possible return values for do_directive
316 #define NO_DIRECTIVE_FOUND 0
317 #define DIRECTIVE_FOUND 1
320 * This define sets the upper limit for smacro and expansions
322 #define DEADMAN_LIMIT (1 << 20)
325 * Condition codes. Note that we use c_ prefix not C_ because C_ is
326 * used in nasm.h for the "real" condition codes. At _this_ level,
327 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
328 * ones, so we need a different enum...
330 static const char * const conditions
[] = {
331 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
332 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
333 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
336 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
337 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
338 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
341 static const enum pp_conds inverse_ccs
[] = {
342 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
343 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
,
344 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
347 /* For TASM compatibility we need to be able to recognise TASM compatible
348 * conditional compilation directives. Using the NASM pre-processor does
349 * not work, so we look for them specifically from the following list and
350 * then jam in the equivalent NASM directive into the input stream.
354 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
355 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
358 static const char * const tasm_directives
[] = {
359 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
360 "ifndef", "include", "local"
363 static int StackSize
= 4;
364 static char *StackPointer
= "ebp";
365 static int ArgOffset
= 8;
366 static int LocalOffset
= 0;
368 static Context
*cstk
;
369 static Include
*istk
;
370 static IncPath
*ipath
= NULL
;
372 static int pass
; /* HACK: pass 0 = generate dependencies only */
373 static StrList
**dephead
, **deptail
; /* Dependency list */
375 static uint64_t unique
; /* unique identifier numbers */
377 static Line
*predef
= NULL
;
378 static bool do_predef
;
380 static ListGen
*list
;
383 * The current set of expansion definitions we have defined.
385 static struct hash_table expdefs
;
388 * The current set of single-line macros we have defined.
390 static struct hash_table smacros
;
393 * Linked List of all active expansion definitions
395 struct ExpDef
*expansions
= NULL
;
398 * The expansion we are currently defining
400 static ExpDef
*defining
= NULL
;
402 static uint64_t nested_mac_count
;
403 static uint64_t nested_rep_count
;
406 * Linked-list of lines to preprocess, prior to cleanup
408 static Line
*finals
= NULL
;
409 static bool in_final
= false;
412 * The number of macro parameters to allocate space for at a time.
414 #define PARAM_DELTA 16
417 * The standard macro set: defined in macros.c in the array nasm_stdmac.
418 * This gives our position in the macro set, when we're processing it.
420 static macros_t
*stdmacpos
;
423 * The extra standard macros that come from the object format, if
426 static macros_t
*extrastdmac
= NULL
;
427 static bool any_extrastdmac
;
430 * Tokens are allocated in blocks to improve speed
432 #define TOKEN_BLOCKSIZE 4096
433 static Token
*freeTokens
= NULL
;
439 static Blocks blocks
= { NULL
, NULL
};
442 * Forward declarations.
444 static Token
*expand_mmac_params(Token
* tline
);
445 static Token
*expand_smacro(Token
* tline
);
446 static Token
*expand_id(Token
* tline
);
447 static Context
*get_ctx(const char *name
, const char **namep
,
449 static void make_tok_num(Token
* tok
, int64_t val
);
450 static void error(int severity
, const char *fmt
, ...);
451 static void error_precond(int severity
, const char *fmt
, ...);
452 static void *new_Block(size_t size
);
453 static void delete_Blocks(void);
454 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
455 const char *text
, int txtlen
);
456 static Token
*copy_Token(Token
* tline
);
457 static Token
*delete_Token(Token
* t
);
458 static Line
*new_Line(void);
459 static ExpDef
*new_ExpDef(void);
460 static ExpInv
*new_ExpInv(void);
463 * Macros for safe checking of token pointers, avoid *(NULL)
465 #define tok_type_(x,t) ((x) && (x)->type == (t))
466 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
471 * nasm_unquote with error if the string contains NUL characters.
472 * If the string contains NUL characters, issue an error and return
473 * the C len, i.e. truncate at the NUL.
475 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
477 size_t len
= nasm_unquote(qstr
, NULL
);
478 size_t clen
= strlen(qstr
);
481 error(ERR_NONFATAL
, "NUL character in `%s' directive",
482 pp_directives
[directive
]);
488 * Handle TASM specific directives, which do not contain a % in
489 * front of them. We do it here because I could not find any other
490 * place to do it for the moment, and it is a hack (ideally it would
491 * be nice to be able to use the NASM pre-processor to do it).
493 static char *check_tasm_directive(char *line
)
495 int32_t i
, j
, k
, m
, len
;
496 char *p
, *q
, *oldline
, oldchar
;
498 p
= nasm_skip_spaces(line
);
500 /* Binary search for the directive name */
502 j
= ARRAY_SIZE(tasm_directives
);
503 q
= nasm_skip_word(p
);
510 m
= nasm_stricmp(p
, tasm_directives
[k
]);
512 /* We have found a directive, so jam a % in front of it
513 * so that NASM will then recognise it as one if it's own.
518 line
= nasm_malloc(len
+ 2);
520 if (k
== TM_IFDIFI
) {
522 * NASM does not recognise IFDIFI, so we convert
523 * it to %if 0. This is not used in NASM
524 * compatible code, but does need to parse for the
525 * TASM macro package.
527 strcpy(line
+ 1, "if 0");
529 memcpy(line
+ 1, p
, len
+ 1);
544 * The pre-preprocessing stage... This function translates line
545 * number indications as they emerge from GNU cpp (`# lineno "file"
546 * flags') into NASM preprocessor line number indications (`%line
549 static char *prepreproc(char *line
)
552 char *fname
, *oldline
;
554 if (line
[0] == '#' && line
[1] == ' ') {
557 lineno
= atoi(fname
);
558 fname
+= strspn(fname
, "0123456789 ");
561 fnlen
= strcspn(fname
, "\"");
562 line
= nasm_malloc(20 + fnlen
);
563 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
566 if (tasm_compatible_mode
)
567 return check_tasm_directive(line
);
572 * Free a linked list of tokens.
574 static void free_tlist(Token
* list
)
577 list
= delete_Token(list
);
581 * Free a linked list of lines.
583 static void free_llist(Line
* list
)
586 list_for_each_safe(l
, tmp
, list
) {
587 free_tlist(l
->first
);
595 static void free_expdef(ExpDef
* ed
)
598 free_tlist(ed
->dlist
);
599 nasm_free(ed
->defaults
);
600 free_llist(ed
->line
);
605 * Free all currently defined macros, and free the hash tables
607 static void free_smacro_table(struct hash_table
*smt
)
611 struct hash_tbl_node
*it
= NULL
;
613 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
614 nasm_free((void *)key
);
615 list_for_each_safe(s
, tmp
, s
) {
617 free_tlist(s
->expansion
);
624 static void free_expdef_table(struct hash_table
*edt
)
628 struct hash_tbl_node
*it
= NULL
;
631 while ((ed
= hash_iterate(edt
, &it
, &key
)) != NULL
) {
632 nasm_free((void *)key
);
633 list_for_each_safe(ed
,tmp
, ed
)
639 static void free_macros(void)
641 free_smacro_table(&smacros
);
642 free_expdef_table(&expdefs
);
646 * Initialize the hash tables
648 static void init_macros(void)
650 hash_init(&smacros
, HASH_LARGE
);
651 hash_init(&expdefs
, HASH_LARGE
);
655 * Pop the context stack.
657 static void ctx_pop(void)
662 free_smacro_table(&c
->localmac
);
668 * Search for a key in the hash index; adding it if necessary
669 * (in which case we initialize the data pointer to NULL.)
672 hash_findi_add(struct hash_table
*hash
, const char *str
)
674 struct hash_insert hi
;
678 r
= hash_findi(hash
, str
, &hi
);
682 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
683 return hash_add(&hi
, strx
, NULL
);
687 * Like hash_findi, but returns the data element rather than a pointer
688 * to it. Used only when not adding a new element, hence no third
692 hash_findix(struct hash_table
*hash
, const char *str
)
696 p
= hash_findi(hash
, str
, NULL
);
697 return p
? *p
: NULL
;
701 * read line from standard macros set,
702 * if there no more left -- return NULL
704 static char *line_from_stdmac(void)
707 const unsigned char *p
= stdmacpos
;
716 len
+= pp_directives_len
[c
- 0x80] + 1;
721 line
= nasm_malloc(len
+ 1);
723 while ((c
= *stdmacpos
++)) {
725 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
726 q
+= pp_directives_len
[c
- 0x80];
736 /* This was the last of the standard macro chain... */
738 if (any_extrastdmac
) {
739 stdmacpos
= extrastdmac
;
740 any_extrastdmac
= false;
741 } else if (do_predef
) {
744 Token
*head
, **tail
, *t
;
747 * Nasty hack: here we push the contents of
748 * `predef' on to the top-level expansion stack,
749 * since this is the most convenient way to
750 * implement the pre-include and pre-define
753 list_for_each(pd
, predef
) {
756 list_for_each(t
, pd
->first
) {
757 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
758 tail
= &(*tail
)->next
;
764 ei
->type
= EXP_PREDEF
;
767 ei
->prev
= istk
->expansion
;
768 istk
->expansion
= ei
;
777 #define BUF_DELTA 512
779 * Read a line from the top file in istk, handling multiple CR/LFs
780 * at the end of the line read, and handling spurious ^Zs. Will
781 * return lines from the standard macro set if this has not already
784 static char *read_line(void)
786 char *buffer
, *p
, *q
;
787 int bufsize
, continued_count
;
790 * standart macros set (predefined) goes first
792 p
= line_from_stdmac();
797 * regular read from a file
800 buffer
= nasm_malloc(BUF_DELTA
);
804 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
808 if (p
> buffer
&& p
[-1] == '\n') {
810 * Convert backslash-CRLF line continuation sequences into
811 * nothing at all (for DOS and Windows)
813 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
819 * Also convert backslash-LF line continuation sequences into
820 * nothing at all (for Unix)
822 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
830 if (p
- buffer
> bufsize
- 10) {
831 int32_t offset
= p
- buffer
;
832 bufsize
+= BUF_DELTA
;
833 buffer
= nasm_realloc(buffer
, bufsize
);
834 p
= buffer
+ offset
; /* prevent stale-pointer problems */
838 if (!q
&& p
== buffer
) {
843 src_set_linnum(src_get_linnum() + istk
->lineinc
+
844 (continued_count
* istk
->lineinc
));
847 * Play safe: remove CRs as well as LFs, if any of either are
848 * present at the end of the line.
850 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
854 * Handle spurious ^Z, which may be inserted into source files
855 * by some file transfer utilities.
857 buffer
[strcspn(buffer
, "\032")] = '\0';
859 list
->line(LIST_READ
, buffer
);
865 * Tokenize a line of text. This is a very simple process since we
866 * don't need to parse the value out of e.g. numeric tokens: we
867 * simply split one string into many.
869 static Token
*tokenize(char *line
)
872 enum pp_token_type type
;
874 Token
*t
, **tail
= &list
;
880 if (*p
== '+' && !nasm_isdigit(p
[1])) {
883 } else if (nasm_isdigit(*p
) ||
884 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
888 while (nasm_isdigit(*p
));
889 type
= TOK_PREPROC_ID
;
890 } else if (*p
== '{') {
892 while (*p
&& *p
!= '}') {
899 type
= TOK_PREPROC_ID
;
900 } else if (*p
== '[') {
902 line
+= 2; /* Skip the leading %[ */
904 while (lvl
&& (c
= *p
++)) {
916 p
= nasm_skip_string(p
- 1) + 1;
926 error(ERR_NONFATAL
, "unterminated %[ construct");
928 } else if (*p
== '?') {
929 type
= TOK_PREPROC_Q
; /* %? */
932 type
= TOK_PREPROC_QQ
; /* %?? */
935 } else if (*p
== '!') {
936 type
= TOK_PREPROC_ID
;
942 while (isidchar(*p
));
943 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
944 p
= nasm_skip_string(p
);
948 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
950 /* %! without string or identifier */
951 type
= TOK_OTHER
; /* Legacy behavior... */
953 } else if (isidchar(*p
) ||
954 ((*p
== '!' || *p
== '%' || *p
== '$') &&
959 while (isidchar(*p
));
960 type
= TOK_PREPROC_ID
;
966 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
969 while (*p
&& isidchar(*p
))
971 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
976 p
= nasm_skip_string(p
);
981 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
982 /* Handling unterminated strings by UNV */
985 } else if (p
[0] == '$' && p
[1] == '$') {
986 type
= TOK_OTHER
; /* TOKEN_BASE */
988 } else if (isnumstart(*p
)) {
990 bool is_float
= false;
1006 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1008 if (*p
== '+' || *p
== '-') {
1010 * e can only be followed by +/- if it is either a
1011 * prefixed hex number or a floating-point number
1016 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1018 } else if (c
== 'P' || c
== 'p') {
1020 if (*p
== '+' || *p
== '-')
1022 } else if (isnumchar(c
) || c
== '_')
1023 ; /* just advance */
1024 else if (c
== '.') {
1026 * we need to deal with consequences of the legacy
1027 * parser, like "1.nolist" being two tokens
1028 * (TOK_NUMBER, TOK_ID) here; at least give it
1029 * a shot for now. In the future, we probably need
1030 * a flex-based scanner with proper pattern matching
1031 * to do it as well as it can be done. Nothing in
1032 * the world is going to help the person who wants
1033 * 0x123.p16 interpreted as two tokens, though.
1039 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1040 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1041 (*r
== 'p' || *r
== 'P')) {
1045 break; /* Terminate the token */
1049 p
--; /* Point to first character beyond number */
1051 if (p
== line
+1 && *line
== '$') {
1052 type
= TOK_OTHER
; /* TOKEN_HERE */
1054 if (has_e
&& !is_hex
) {
1055 /* 1e13 is floating-point, but 1e13h is not */
1059 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1061 } else if (nasm_isspace(*p
)) {
1062 type
= TOK_WHITESPACE
;
1063 p
= nasm_skip_spaces(p
);
1065 * Whitespace just before end-of-line is discarded by
1066 * pretending it's a comment; whitespace just before a
1067 * comment gets lumped into the comment.
1069 if (!*p
|| *p
== ';') {
1074 } else if (*p
== ';') {
1080 * Anything else is an operator of some kind. We check
1081 * for all the double-character operators (>>, <<, //,
1082 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1083 * else is a single-character operator.
1086 if ((p
[0] == '>' && p
[1] == '>') ||
1087 (p
[0] == '<' && p
[1] == '<') ||
1088 (p
[0] == '/' && p
[1] == '/') ||
1089 (p
[0] == '<' && p
[1] == '=') ||
1090 (p
[0] == '>' && p
[1] == '=') ||
1091 (p
[0] == '=' && p
[1] == '=') ||
1092 (p
[0] == '!' && p
[1] == '=') ||
1093 (p
[0] == '<' && p
[1] == '>') ||
1094 (p
[0] == '&' && p
[1] == '&') ||
1095 (p
[0] == '|' && p
[1] == '|') ||
1096 (p
[0] == '^' && p
[1] == '^')) {
1102 /* Handling unterminated string by UNV */
1105 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1106 t->text[p-line] = *line;
1110 if (type
!= TOK_COMMENT
) {
1111 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1120 * this function allocates a new managed block of memory and
1121 * returns a pointer to the block. The managed blocks are
1122 * deleted only all at once by the delete_Blocks function.
1124 static void *new_Block(size_t size
)
1126 Blocks
*b
= &blocks
;
1128 /* first, get to the end of the linked list */
1131 /* now allocate the requested chunk */
1132 b
->chunk
= nasm_malloc(size
);
1134 /* now allocate a new block for the next request */
1135 b
->next
= nasm_malloc(sizeof(Blocks
));
1136 /* and initialize the contents of the new block */
1137 b
->next
->next
= NULL
;
1138 b
->next
->chunk
= NULL
;
1143 * this function deletes all managed blocks of memory
1145 static void delete_Blocks(void)
1147 Blocks
*a
, *b
= &blocks
;
1150 * keep in mind that the first block, pointed to by blocks
1151 * is a static and not dynamically allocated, so we don't
1156 nasm_free(b
->chunk
);
1165 * this function creates a new Token and passes a pointer to it
1166 * back to the caller. It sets the type and text elements, and
1167 * also the a.mac and next elements to NULL.
1169 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1170 const char *text
, int txtlen
)
1176 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1177 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1178 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1179 freeTokens
[i
].next
= NULL
;
1182 freeTokens
= t
->next
;
1186 if (type
== TOK_WHITESPACE
|| !text
) {
1190 txtlen
= strlen(text
);
1191 t
->text
= nasm_malloc(txtlen
+1);
1192 memcpy(t
->text
, text
, txtlen
);
1193 t
->text
[txtlen
] = '\0';
1198 static Token
*copy_Token(Token
* tline
)
1200 Token
*t
, *tt
, *first
= NULL
, *prev
= NULL
;
1202 for (tt
= tline
; tt
!= NULL
; tt
= tt
->next
) {
1204 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1205 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1206 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1207 freeTokens
[i
].next
= NULL
;
1210 freeTokens
= t
->next
;
1212 t
->text
= ((tt
->text
!= NULL
) ? strdup(tt
->text
) : NULL
);
1213 t
->a
.mac
= tt
->a
.mac
;
1214 t
->a
.len
= tt
->a
.len
;
1226 static Token
*delete_Token(Token
* t
)
1228 Token
*next
= t
->next
;
1230 t
->next
= freeTokens
;
1236 * Convert a line of tokens back into text.
1237 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1238 * will be transformed into ..@ctxnum.xxx
1240 static char *detoken(Token
* tlist
, bool expand_locals
)
1247 list_for_each(t
, tlist
) {
1248 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1253 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1254 size_t len
= nasm_unquote(v
, NULL
);
1255 size_t clen
= strlen(v
);
1258 error(ERR_NONFATAL
| ERR_PASS1
,
1259 "NUL character in %! string");
1265 char *p
= getenv(v
);
1267 error(ERR_NONFATAL
| ERR_PASS1
,
1268 "nonexistent environment variable `%s'", v
);
1271 t
->text
= nasm_strdup(p
);
1276 /* Expand local macros here and not during preprocessing */
1277 if (expand_locals
&&
1278 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1279 t
->text
[0] == '%' && t
->text
[1] == '$') {
1282 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1285 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1286 p
= nasm_strcat(buffer
, q
);
1291 if (t
->type
== TOK_WHITESPACE
)
1294 len
+= strlen(t
->text
);
1297 p
= line
= nasm_malloc(len
+ 1);
1299 list_for_each(t
, tlist
) {
1300 if (t
->type
== TOK_WHITESPACE
) {
1302 } else if (t
->text
) {
1314 * Initialize a new Line
1316 static Line
*new_Line(void)
1318 Line
*l
= nasm_malloc(sizeof(Line
));
1326 * Initialize a new Expansion Definition
1328 static ExpDef
*new_ExpDef(void)
1330 ExpDef
*ed
= nasm_malloc(sizeof(ExpDef
));
1333 ed
->type
= EXP_NONE
;
1337 ed
->casesense
= true;
1343 ed
->defaults
= NULL
;
1345 ed
->state
= COND_NEVER
;
1350 ed
->ignoring
= false;
1356 * Initialize a new Expansion Instance
1358 static ExpInv
*new_ExpInv(void)
1361 ExpInv
*ei
= nasm_malloc(sizeof(ExpInv
));
1363 ei
->type
= EXP_NONE
;
1366 ei
->label_text
= NULL
;
1372 ei
->paramlen
= NULL
;
1373 ei
->unique
= unique
;
1374 ei
->emitting
= false;
1380 * A scanner, suitable for use by the expression evaluator, which
1381 * operates on a line of Tokens. Expects a pointer to a pointer to
1382 * the first token in the line to be passed in as its private_data
1385 * FIX: This really needs to be unified with stdscan.
1387 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1389 Token
**tlineptr
= private_data
;
1391 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1395 *tlineptr
= tline
? tline
->next
: NULL
;
1396 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1397 tline
->type
== TOK_COMMENT
));
1400 return tokval
->t_type
= TOKEN_EOS
;
1402 tokval
->t_charptr
= tline
->text
;
1404 if (tline
->text
[0] == '$' && !tline
->text
[1])
1405 return tokval
->t_type
= TOKEN_HERE
;
1406 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1407 return tokval
->t_type
= TOKEN_BASE
;
1409 if (tline
->type
== TOK_ID
) {
1410 p
= tokval
->t_charptr
= tline
->text
;
1412 tokval
->t_charptr
++;
1413 return tokval
->t_type
= TOKEN_ID
;
1416 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1417 if (r
>= p
+MAX_KEYWORD
)
1418 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1419 *s
++ = nasm_tolower(*r
);
1422 /* right, so we have an identifier sitting in temp storage. now,
1423 * is it actually a register or instruction name, or what? */
1424 return nasm_token_hash(ourcopy
, tokval
);
1427 if (tline
->type
== TOK_NUMBER
) {
1429 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1430 tokval
->t_charptr
= tline
->text
;
1432 return tokval
->t_type
= TOKEN_ERRNUM
;
1434 return tokval
->t_type
= TOKEN_NUM
;
1437 if (tline
->type
== TOK_FLOAT
) {
1438 return tokval
->t_type
= TOKEN_FLOAT
;
1441 if (tline
->type
== TOK_STRING
) {
1444 bq
= tline
->text
[0];
1445 tokval
->t_charptr
= tline
->text
;
1446 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1448 if (ep
[0] != bq
|| ep
[1] != '\0')
1449 return tokval
->t_type
= TOKEN_ERRSTR
;
1451 return tokval
->t_type
= TOKEN_STR
;
1454 if (tline
->type
== TOK_OTHER
) {
1455 if (!strcmp(tline
->text
, "<<"))
1456 return tokval
->t_type
= TOKEN_SHL
;
1457 if (!strcmp(tline
->text
, ">>"))
1458 return tokval
->t_type
= TOKEN_SHR
;
1459 if (!strcmp(tline
->text
, "//"))
1460 return tokval
->t_type
= TOKEN_SDIV
;
1461 if (!strcmp(tline
->text
, "%%"))
1462 return tokval
->t_type
= TOKEN_SMOD
;
1463 if (!strcmp(tline
->text
, "=="))
1464 return tokval
->t_type
= TOKEN_EQ
;
1465 if (!strcmp(tline
->text
, "<>"))
1466 return tokval
->t_type
= TOKEN_NE
;
1467 if (!strcmp(tline
->text
, "!="))
1468 return tokval
->t_type
= TOKEN_NE
;
1469 if (!strcmp(tline
->text
, "<="))
1470 return tokval
->t_type
= TOKEN_LE
;
1471 if (!strcmp(tline
->text
, ">="))
1472 return tokval
->t_type
= TOKEN_GE
;
1473 if (!strcmp(tline
->text
, "&&"))
1474 return tokval
->t_type
= TOKEN_DBL_AND
;
1475 if (!strcmp(tline
->text
, "^^"))
1476 return tokval
->t_type
= TOKEN_DBL_XOR
;
1477 if (!strcmp(tline
->text
, "||"))
1478 return tokval
->t_type
= TOKEN_DBL_OR
;
1482 * We have no other options: just return the first character of
1485 return tokval
->t_type
= tline
->text
[0];
1489 * Compare a string to the name of an existing macro; this is a
1490 * simple wrapper which calls either strcmp or nasm_stricmp
1491 * depending on the value of the `casesense' parameter.
1493 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1495 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1499 * Compare a string to the name of an existing macro; this is a
1500 * simple wrapper which calls either strcmp or nasm_stricmp
1501 * depending on the value of the `casesense' parameter.
1503 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1505 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1509 * Return the Context structure associated with a %$ token. Return
1510 * NULL, having _already_ reported an error condition, if the
1511 * context stack isn't deep enough for the supplied number of $
1513 * If all_contexts == true, contexts that enclose current are
1514 * also scanned for such smacro, until it is found; if not -
1515 * only the context that directly results from the number of $'s
1516 * in variable's name.
1518 * If "namep" is non-NULL, set it to the pointer to the macro name
1519 * tail, i.e. the part beyond %$...
1521 static Context
*get_ctx(const char *name
, const char **namep
,
1531 if (!name
|| name
[0] != '%' || name
[1] != '$')
1535 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1542 while (ctx
&& *name
== '$') {
1548 error(ERR_NONFATAL
, "`%s': context stack is only"
1549 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1560 /* Search for this smacro in found context */
1561 m
= hash_findix(&ctx
->localmac
, name
);
1563 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1574 * Check to see if a file is already in a string list
1576 static bool in_list(const StrList
*list
, const char *str
)
1579 if (!strcmp(list
->str
, str
))
1587 * Open an include file. This routine must always return a valid
1588 * file pointer if it returns - it's responsible for throwing an
1589 * ERR_FATAL and bombing out completely if not. It should also try
1590 * the include path one by one until it finds the file or reaches
1591 * the end of the path.
1593 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1598 IncPath
*ip
= ipath
;
1599 int len
= strlen(file
);
1600 size_t prefix_len
= 0;
1604 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1605 memcpy(sl
->str
, prefix
, prefix_len
);
1606 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1607 fp
= fopen(sl
->str
, "r");
1608 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1626 prefix_len
= strlen(prefix
);
1628 /* -MG given and file not found */
1629 if (dhead
&& !in_list(*dhead
, file
)) {
1630 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1632 strcpy(sl
->str
, file
);
1640 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1645 * Determine if we should warn on defining a single-line macro of
1646 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1647 * return true if _any_ single-line macro of that name is defined.
1648 * Otherwise, will return true if a single-line macro with either
1649 * `nparam' or no parameters is defined.
1651 * If a macro with precisely the right number of parameters is
1652 * defined, or nparam is -1, the address of the definition structure
1653 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1654 * is NULL, no action will be taken regarding its contents, and no
1657 * Note that this is also called with nparam zero to resolve
1660 * If you already know which context macro belongs to, you can pass
1661 * the context pointer as first parameter; if you won't but name begins
1662 * with %$ the context will be automatically computed. If all_contexts
1663 * is true, macro will be searched in outer contexts as well.
1666 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1669 struct hash_table
*smtbl
;
1673 smtbl
= &ctx
->localmac
;
1674 } else if (name
[0] == '%' && name
[1] == '$') {
1676 ctx
= get_ctx(name
, &name
, false);
1678 return false; /* got to return _something_ */
1679 smtbl
= &ctx
->localmac
;
1683 m
= (SMacro
*) hash_findix(smtbl
, name
);
1686 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1687 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1689 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1703 * Count and mark off the parameters in a multi-line macro call.
1704 * This is called both from within the multi-line macro expansion
1705 * code, and also to mark off the default parameters when provided
1706 * in a %macro definition line.
1708 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1710 int paramsize
, brace
;
1712 *nparam
= paramsize
= 0;
1715 /* +1: we need space for the final NULL */
1716 if (*nparam
+1 >= paramsize
) {
1717 paramsize
+= PARAM_DELTA
;
1718 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1722 if (tok_is_(t
, "{"))
1724 (*params
)[(*nparam
)++] = t
;
1725 while (tok_isnt_(t
, brace
? "}" : ","))
1727 if (t
) { /* got a comma/brace */
1731 * Now we've found the closing brace, look further
1735 if (tok_isnt_(t
, ",")) {
1737 "braces do not enclose all of macro parameter");
1738 while (tok_isnt_(t
, ","))
1742 t
= t
->next
; /* eat the comma */
1749 * Determine whether one of the various `if' conditions is true or
1752 * We must free the tline we get passed.
1754 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1756 enum pp_conditional i
= PP_COND(ct
);
1758 Token
*t
, *tt
, **tptr
, *origline
;
1759 struct tokenval tokval
;
1761 enum pp_token_type needtype
;
1768 j
= false; /* have we matched yet? */
1773 if (tline
->type
!= TOK_ID
) {
1775 "`%s' expects context identifiers", pp_directives
[ct
]);
1776 free_tlist(origline
);
1779 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1781 tline
= tline
->next
;
1786 j
= false; /* have we matched yet? */
1789 if (!tline
|| (tline
->type
!= TOK_ID
&&
1790 (tline
->type
!= TOK_PREPROC_ID
||
1791 tline
->text
[1] != '$'))) {
1793 "`%s' expects macro identifiers", pp_directives
[ct
]);
1796 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1798 tline
= tline
->next
;
1803 tline
= expand_smacro(tline
);
1804 j
= false; /* have we matched yet? */
1807 if (!tline
|| (tline
->type
!= TOK_ID
&&
1808 tline
->type
!= TOK_STRING
&&
1809 (tline
->type
!= TOK_PREPROC_ID
||
1810 tline
->text
[1] != '!'))) {
1812 "`%s' expects environment variable names",
1817 if (tline
->type
== TOK_PREPROC_ID
)
1818 p
+= 2; /* Skip leading %! */
1819 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1820 nasm_unquote_cstr(p
, ct
);
1823 tline
= tline
->next
;
1829 tline
= expand_smacro(tline
);
1831 while (tok_isnt_(tt
, ","))
1835 "`%s' expects two comma-separated arguments",
1840 j
= true; /* assume equality unless proved not */
1841 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1842 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1843 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1847 if (t
->type
== TOK_WHITESPACE
) {
1851 if (tt
->type
== TOK_WHITESPACE
) {
1855 if (tt
->type
!= t
->type
) {
1856 j
= false; /* found mismatching tokens */
1859 /* When comparing strings, need to unquote them first */
1860 if (t
->type
== TOK_STRING
) {
1861 size_t l1
= nasm_unquote(t
->text
, NULL
);
1862 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1868 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1872 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1873 j
= false; /* found mismatching tokens */
1880 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1881 j
= false; /* trailing gunk on one end or other */
1887 ExpDef searching
, *ed
;
1890 tline
= expand_id(tline
);
1891 if (!tok_type_(tline
, TOK_ID
)) {
1893 "`%s' expects a macro name", pp_directives
[ct
]);
1896 searching
.name
= nasm_strdup(tline
->text
);
1897 searching
.casesense
= true;
1898 searching
.plus
= false;
1899 searching
.nolist
= false;
1900 //searching.in_progress = 0;
1901 searching
.max_depth
= 0;
1902 //searching.rep_nest = NULL;
1903 searching
.nparam_min
= 0;
1904 searching
.nparam_max
= INT_MAX
;
1905 tline
= expand_smacro(tline
->next
);
1908 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1910 "`%s' expects a parameter count or nothing",
1913 searching
.nparam_min
= searching
.nparam_max
=
1914 readnum(tline
->text
, &j
);
1917 "unable to parse parameter count `%s'",
1920 if (tline
&& tok_is_(tline
->next
, "-")) {
1921 tline
= tline
->next
->next
;
1922 if (tok_is_(tline
, "*"))
1923 searching
.nparam_max
= INT_MAX
;
1924 else if (!tok_type_(tline
, TOK_NUMBER
))
1926 "`%s' expects a parameter count after `-'",
1929 searching
.nparam_max
= readnum(tline
->text
, &j
);
1932 "unable to parse parameter count `%s'",
1934 if (searching
.nparam_min
> searching
.nparam_max
)
1936 "minimum parameter count exceeds maximum");
1939 if (tline
&& tok_is_(tline
->next
, "+")) {
1940 tline
= tline
->next
;
1941 searching
.plus
= true;
1943 ed
= (ExpDef
*) hash_findix(&expdefs
, searching
.name
);
1944 while (ed
!= NULL
) {
1945 if (!strcmp(ed
->name
, searching
.name
) &&
1946 (ed
->nparam_min
<= searching
.nparam_max
1948 && (searching
.nparam_min
<= ed
->nparam_max
1955 if (tline
&& tline
->next
)
1956 error(ERR_WARNING
|ERR_PASS1
,
1957 "trailing garbage after %%ifmacro ignored");
1958 nasm_free(searching
.name
);
1967 needtype
= TOK_NUMBER
;
1970 needtype
= TOK_STRING
;
1974 t
= tline
= expand_smacro(tline
);
1976 while (tok_type_(t
, TOK_WHITESPACE
) ||
1977 (needtype
== TOK_NUMBER
&&
1978 tok_type_(t
, TOK_OTHER
) &&
1979 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1983 j
= tok_type_(t
, needtype
);
1987 t
= tline
= expand_smacro(tline
);
1988 while (tok_type_(t
, TOK_WHITESPACE
))
1993 t
= t
->next
; /* Skip the actual token */
1994 while (tok_type_(t
, TOK_WHITESPACE
))
1996 j
= !t
; /* Should be nothing left */
2001 t
= tline
= expand_smacro(tline
);
2002 while (tok_type_(t
, TOK_WHITESPACE
))
2005 j
= !t
; /* Should be empty */
2009 t
= tline
= expand_smacro(tline
);
2011 tokval
.t_type
= TOKEN_INVALID
;
2012 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2013 NULL
, pass
| CRITICAL
, error
, NULL
);
2017 error(ERR_WARNING
|ERR_PASS1
,
2018 "trailing garbage after expression ignored");
2019 if (!is_simple(evalresult
)) {
2021 "non-constant value given to `%s'", pp_directives
[ct
]);
2024 j
= reloc_value(evalresult
) != 0;
2029 "preprocessor directive `%s' not yet implemented",
2034 free_tlist(origline
);
2035 return j
^ PP_NEGATIVE(ct
);
2038 free_tlist(origline
);
2043 * Common code for defining an smacro
2045 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2046 int nparam
, Token
*expansion
)
2048 SMacro
*smac
, **smhead
;
2049 struct hash_table
*smtbl
;
2051 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2053 error(ERR_WARNING
|ERR_PASS1
,
2054 "single-line macro `%s' defined both with and"
2055 " without parameters", mname
);
2057 * Some instances of the old code considered this a failure,
2058 * some others didn't. What is the right thing to do here?
2060 free_tlist(expansion
);
2061 return false; /* Failure */
2064 * We're redefining, so we have to take over an
2065 * existing SMacro structure. This means freeing
2066 * what was already in it.
2068 nasm_free(smac
->name
);
2069 free_tlist(smac
->expansion
);
2072 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2073 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2074 smac
= nasm_malloc(sizeof(SMacro
));
2075 smac
->next
= *smhead
;
2078 smac
->name
= nasm_strdup(mname
);
2079 smac
->casesense
= casesense
;
2080 smac
->nparam
= nparam
;
2081 smac
->expansion
= expansion
;
2082 smac
->in_progress
= false;
2083 return true; /* Success */
2087 * Undefine an smacro
2089 static void undef_smacro(Context
*ctx
, const char *mname
)
2091 SMacro
**smhead
, *s
, **sp
;
2092 struct hash_table
*smtbl
;
2094 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2095 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2099 * We now have a macro name... go hunt for it.
2102 while ((s
= *sp
) != NULL
) {
2103 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2106 free_tlist(s
->expansion
);
2116 * Parse a mmacro specification.
2118 static bool parse_mmacro_spec(Token
*tline
, ExpDef
*def
, const char *directive
)
2122 tline
= tline
->next
;
2124 tline
= expand_id(tline
);
2125 if (!tok_type_(tline
, TOK_ID
)) {
2126 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2130 def
->name
= nasm_strdup(tline
->text
);
2132 def
->nolist
= false;
2133 // def->in_progress = 0;
2134 // def->rep_nest = NULL;
2135 def
->nparam_min
= 0;
2136 def
->nparam_max
= 0;
2138 tline
= expand_smacro(tline
->next
);
2140 if (!tok_type_(tline
, TOK_NUMBER
)) {
2141 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2143 def
->nparam_min
= def
->nparam_max
=
2144 readnum(tline
->text
, &err
);
2147 "unable to parse parameter count `%s'", tline
->text
);
2149 if (tline
&& tok_is_(tline
->next
, "-")) {
2150 tline
= tline
->next
->next
;
2151 if (tok_is_(tline
, "*")) {
2152 def
->nparam_max
= INT_MAX
;
2153 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2155 "`%s' expects a parameter count after `-'", directive
);
2157 def
->nparam_max
= readnum(tline
->text
, &err
);
2159 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2162 if (def
->nparam_min
> def
->nparam_max
) {
2163 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2167 if (tline
&& tok_is_(tline
->next
, "+")) {
2168 tline
= tline
->next
;
2171 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2172 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2173 tline
= tline
->next
;
2178 * Handle default parameters.
2180 if (tline
&& tline
->next
) {
2181 def
->dlist
= tline
->next
;
2183 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2186 def
->defaults
= NULL
;
2190 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2192 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2193 "too many default macro parameters");
2200 * Decode a size directive
2202 static int parse_size(const char *str
) {
2203 static const char *size_names
[] =
2204 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2205 static const int sizes
[] =
2206 { 0, 1, 4, 16, 8, 10, 2, 32 };
2208 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2212 * find and process preprocessor directive in passed line
2213 * Find out if a line contains a preprocessor directive, and deal
2216 * If a directive _is_ found, it is the responsibility of this routine
2217 * (and not the caller) to free_tlist() the line.
2219 * @param tline a pointer to the current tokeninzed line linked list
2220 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2223 static int do_directive(Token
* tline
)
2225 enum preproc_token i
;
2238 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2239 struct tokenval tokval
;
2241 ExpDef
*ed
, *eed
, **edhead
;
2250 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2251 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2252 || tline
->text
[1] == '!'))
2253 return NO_DIRECTIVE_FOUND
;
2255 i
= pp_token_hash(tline
->text
);
2259 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2260 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2262 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2265 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2266 /* Directive to tell NASM what the default stack size is. The
2267 * default is for a 16-bit stack, and this can be overriden with
2270 tline
= tline
->next
;
2271 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2272 tline
= tline
->next
;
2273 if (!tline
|| tline
->type
!= TOK_ID
) {
2274 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2275 free_tlist(origline
);
2276 return DIRECTIVE_FOUND
;
2278 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2279 /* All subsequent ARG directives are for a 32-bit stack */
2281 StackPointer
= "ebp";
2284 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2285 /* All subsequent ARG directives are for a 64-bit stack */
2287 StackPointer
= "rbp";
2290 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2291 /* All subsequent ARG directives are for a 16-bit stack,
2292 * far function call.
2295 StackPointer
= "bp";
2298 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2299 /* All subsequent ARG directives are for a 16-bit stack,
2300 * far function call. We don't support near functions.
2303 StackPointer
= "bp";
2307 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2308 free_tlist(origline
);
2309 return DIRECTIVE_FOUND
;
2311 free_tlist(origline
);
2312 return DIRECTIVE_FOUND
;
2315 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2316 /* TASM like ARG directive to define arguments to functions, in
2317 * the following form:
2319 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2323 char *arg
, directive
[256];
2324 int size
= StackSize
;
2326 /* Find the argument name */
2327 tline
= tline
->next
;
2328 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2329 tline
= tline
->next
;
2330 if (!tline
|| tline
->type
!= TOK_ID
) {
2331 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2332 free_tlist(origline
);
2333 return DIRECTIVE_FOUND
;
2337 /* Find the argument size type */
2338 tline
= tline
->next
;
2339 if (!tline
|| tline
->type
!= TOK_OTHER
2340 || tline
->text
[0] != ':') {
2342 "Syntax error processing `%%arg' directive");
2343 free_tlist(origline
);
2344 return DIRECTIVE_FOUND
;
2346 tline
= tline
->next
;
2347 if (!tline
|| tline
->type
!= TOK_ID
) {
2348 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2349 free_tlist(origline
);
2350 return DIRECTIVE_FOUND
;
2353 /* Allow macro expansion of type parameter */
2354 tt
= tokenize(tline
->text
);
2355 tt
= expand_smacro(tt
);
2356 size
= parse_size(tt
->text
);
2359 "Invalid size type for `%%arg' missing directive");
2361 free_tlist(origline
);
2362 return DIRECTIVE_FOUND
;
2366 /* Round up to even stack slots */
2367 size
= ALIGN(size
, StackSize
);
2369 /* Now define the macro for the argument */
2370 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2371 arg
, StackPointer
, offset
);
2372 do_directive(tokenize(directive
));
2375 /* Move to the next argument in the list */
2376 tline
= tline
->next
;
2377 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2378 tline
= tline
->next
;
2379 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2381 free_tlist(origline
);
2382 return DIRECTIVE_FOUND
;
2385 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2386 /* TASM like LOCAL directive to define local variables for a
2387 * function, in the following form:
2389 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2391 * The '= LocalSize' at the end is ignored by NASM, but is
2392 * required by TASM to define the local parameter size (and used
2393 * by the TASM macro package).
2395 offset
= LocalOffset
;
2397 char *local
, directive
[256];
2398 int size
= StackSize
;
2400 /* Find the argument name */
2401 tline
= tline
->next
;
2402 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2403 tline
= tline
->next
;
2404 if (!tline
|| tline
->type
!= TOK_ID
) {
2406 "`%%local' missing argument parameter");
2407 free_tlist(origline
);
2408 return DIRECTIVE_FOUND
;
2410 local
= tline
->text
;
2412 /* Find the argument size type */
2413 tline
= tline
->next
;
2414 if (!tline
|| tline
->type
!= TOK_OTHER
2415 || tline
->text
[0] != ':') {
2417 "Syntax error processing `%%local' directive");
2418 free_tlist(origline
);
2419 return DIRECTIVE_FOUND
;
2421 tline
= tline
->next
;
2422 if (!tline
|| tline
->type
!= TOK_ID
) {
2424 "`%%local' missing size type parameter");
2425 free_tlist(origline
);
2426 return DIRECTIVE_FOUND
;
2429 /* Allow macro expansion of type parameter */
2430 tt
= tokenize(tline
->text
);
2431 tt
= expand_smacro(tt
);
2432 size
= parse_size(tt
->text
);
2435 "Invalid size type for `%%local' missing directive");
2437 free_tlist(origline
);
2438 return DIRECTIVE_FOUND
;
2442 /* Round up to even stack slots */
2443 size
= ALIGN(size
, StackSize
);
2445 offset
+= size
; /* Negative offset, increment before */
2447 /* Now define the macro for the argument */
2448 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2449 local
, StackPointer
, offset
);
2450 do_directive(tokenize(directive
));
2452 /* Now define the assign to setup the enter_c macro correctly */
2453 snprintf(directive
, sizeof(directive
),
2454 "%%assign %%$localsize %%$localsize+%d", size
);
2455 do_directive(tokenize(directive
));
2457 /* Move to the next argument in the list */
2458 tline
= tline
->next
;
2459 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2460 tline
= tline
->next
;
2461 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2462 LocalOffset
= offset
;
2463 free_tlist(origline
);
2464 return DIRECTIVE_FOUND
;
2467 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2469 error(ERR_WARNING
|ERR_PASS1
,
2470 "trailing garbage after `%%clear' ignored");
2473 free_tlist(origline
);
2474 return DIRECTIVE_FOUND
;
2477 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2478 t
= tline
->next
= expand_smacro(tline
->next
);
2480 if (!t
|| (t
->type
!= TOK_STRING
&&
2481 t
->type
!= TOK_INTERNAL_STRING
)) {
2482 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2483 free_tlist(origline
);
2484 return DIRECTIVE_FOUND
; /* but we did _something_ */
2487 error(ERR_WARNING
|ERR_PASS1
,
2488 "trailing garbage after `%%depend' ignored");
2490 if (t
->type
!= TOK_INTERNAL_STRING
)
2491 nasm_unquote_cstr(p
, i
);
2492 if (dephead
&& !in_list(*dephead
, p
)) {
2493 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2497 deptail
= &sl
->next
;
2499 free_tlist(origline
);
2500 return DIRECTIVE_FOUND
;
2503 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2504 t
= tline
->next
= expand_smacro(tline
->next
);
2507 if (!t
|| (t
->type
!= TOK_STRING
&&
2508 t
->type
!= TOK_INTERNAL_STRING
)) {
2509 error(ERR_NONFATAL
, "`%%include' expects a file name");
2510 free_tlist(origline
);
2511 return DIRECTIVE_FOUND
; /* but we did _something_ */
2514 error(ERR_WARNING
|ERR_PASS1
,
2515 "trailing garbage after `%%include' ignored");
2517 if (t
->type
!= TOK_INTERNAL_STRING
)
2518 nasm_unquote_cstr(p
, i
);
2519 inc
= nasm_malloc(sizeof(Include
));
2521 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2523 /* -MG given but file not found */
2526 inc
->fname
= src_set_fname(nasm_strdup(p
));
2527 inc
->lineno
= src_set_linnum(0);
2529 inc
->expansion
= NULL
;
2531 list
->uplevel(LIST_INCLUDE
);
2533 free_tlist(origline
);
2534 return DIRECTIVE_FOUND
;
2537 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2539 static macros_t
*use_pkg
;
2540 const char *pkg_macro
= NULL
;
2542 tline
= tline
->next
;
2544 tline
= expand_id(tline
);
2546 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2547 tline
->type
!= TOK_INTERNAL_STRING
&&
2548 tline
->type
!= TOK_ID
)) {
2549 error(ERR_NONFATAL
, "`%%use' expects a package name");
2550 free_tlist(origline
);
2551 return DIRECTIVE_FOUND
; /* but we did _something_ */
2554 error(ERR_WARNING
|ERR_PASS1
,
2555 "trailing garbage after `%%use' ignored");
2556 if (tline
->type
== TOK_STRING
)
2557 nasm_unquote_cstr(tline
->text
, i
);
2558 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2560 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2562 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2563 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2564 /* Not already included, go ahead and include it */
2565 stdmacpos
= use_pkg
;
2567 free_tlist(origline
);
2568 return DIRECTIVE_FOUND
;
2573 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2574 tline
= tline
->next
;
2576 tline
= expand_id(tline
);
2578 if (!tok_type_(tline
, TOK_ID
)) {
2579 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2581 free_tlist(origline
);
2582 return DIRECTIVE_FOUND
; /* but we did _something_ */
2585 error(ERR_WARNING
|ERR_PASS1
,
2586 "trailing garbage after `%s' ignored",
2588 p
= nasm_strdup(tline
->text
);
2590 p
= NULL
; /* Anonymous */
2594 ctx
= nasm_malloc(sizeof(Context
));
2596 hash_init(&ctx
->localmac
, HASH_SMALL
);
2598 ctx
->number
= unique
++;
2603 error(ERR_NONFATAL
, "`%s': context stack is empty",
2605 } else if (i
== PP_POP
) {
2606 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2607 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2609 cstk
->name
? cstk
->name
: "anonymous", p
);
2614 nasm_free(cstk
->name
);
2620 free_tlist(origline
);
2621 return DIRECTIVE_FOUND
;
2623 severity
= ERR_FATAL
;
2626 severity
= ERR_NONFATAL
;
2629 severity
= ERR_WARNING
|ERR_WARN_USER
;
2633 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2635 /* Only error out if this is the final pass */
2636 if (pass
!= 2 && i
!= PP_FATAL
)
2637 return DIRECTIVE_FOUND
;
2639 tline
->next
= expand_smacro(tline
->next
);
2640 tline
= tline
->next
;
2642 t
= tline
? tline
->next
: NULL
;
2644 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2645 /* The line contains only a quoted string */
2647 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2648 error(severity
, "%s", p
);
2650 /* Not a quoted string, or more than a quoted string */
2651 p
= detoken(tline
, false);
2652 error(severity
, "%s", p
);
2655 free_tlist(origline
);
2656 return DIRECTIVE_FOUND
;
2660 if (defining
!= NULL
) {
2661 if (defining
->type
== EXP_IF
) {
2662 defining
->def_depth
++;
2664 return NO_DIRECTIVE_FOUND
;
2666 if ((istk
->expansion
!= NULL
) &&
2667 (istk
->expansion
->emitting
== false)) {
2670 j
= if_condition(tline
->next
, i
);
2671 tline
->next
= NULL
; /* it got freed */
2672 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
2681 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
2682 ed
->prev
= defining
;
2684 free_tlist(origline
);
2685 return DIRECTIVE_FOUND
;
2688 if (defining
!= NULL
) {
2689 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2690 return NO_DIRECTIVE_FOUND
;
2693 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2694 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2696 switch (defining
->state
) {
2698 defining
->state
= COND_DONE
;
2699 defining
->ignoring
= true;
2704 defining
->ignoring
= true;
2707 case COND_ELSE_TRUE
:
2708 case COND_ELSE_FALSE
:
2709 error_precond(ERR_WARNING
|ERR_PASS1
,
2710 "`%%elif' after `%%else' ignored");
2711 defining
->state
= COND_NEVER
;
2712 defining
->ignoring
= true;
2717 * IMPORTANT: In the case of %if, we will already have
2718 * called expand_mmac_params(); however, if we're
2719 * processing an %elif we must have been in a
2720 * non-emitting mode, which would have inhibited
2721 * the normal invocation of expand_mmac_params().
2722 * Therefore, we have to do it explicitly here.
2724 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2725 tline
->next
= NULL
; /* it got freed */
2727 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2728 defining
->ignoring
= ((defining
->state
== COND_IF_TRUE
) ? false : true);
2731 free_tlist(origline
);
2732 return DIRECTIVE_FOUND
;
2735 if (defining
!= NULL
) {
2736 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2737 return NO_DIRECTIVE_FOUND
;
2741 error_precond(ERR_WARNING
|ERR_PASS1
,
2742 "trailing garbage after `%%else' ignored");
2743 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2744 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2746 switch (defining
->state
) {
2749 defining
->state
= COND_ELSE_FALSE
;
2750 defining
->ignoring
= true;
2754 defining
->ignoring
= true;
2758 defining
->state
= COND_ELSE_TRUE
;
2759 defining
->ignoring
= false;
2762 case COND_ELSE_TRUE
:
2763 case COND_ELSE_FALSE
:
2764 error_precond(ERR_WARNING
|ERR_PASS1
,
2765 "`%%else' after `%%else' ignored.");
2766 defining
->state
= COND_NEVER
;
2767 defining
->ignoring
= true;
2770 free_tlist(origline
);
2771 return DIRECTIVE_FOUND
;
2774 if (defining
!= NULL
) {
2775 if (defining
->type
== EXP_IF
) {
2776 if (defining
->def_depth
> 0) {
2777 defining
->def_depth
--;
2778 return NO_DIRECTIVE_FOUND
;
2781 return NO_DIRECTIVE_FOUND
;
2785 error_precond(ERR_WARNING
|ERR_PASS1
,
2786 "trailing garbage after `%%endif' ignored");
2787 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2788 error(ERR_NONFATAL
, "`%%endif': no matching `%%if'");
2791 defining
= ed
->prev
;
2792 ed
->prev
= expansions
;
2797 ei
->current
= ed
->line
;
2798 ei
->emitting
= true;
2799 ei
->prev
= istk
->expansion
;
2800 istk
->expansion
= ei
;
2801 free_tlist(origline
);
2802 return DIRECTIVE_FOUND
;
2808 if (defining
!= NULL
) {
2809 if (defining
->type
== EXP_MMACRO
) {
2810 defining
->def_depth
++;
2812 return NO_DIRECTIVE_FOUND
;
2816 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2817 ed
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2818 if (!parse_mmacro_spec(tline
, ed
, pp_directives
[i
])) {
2821 return DIRECTIVE_FOUND
;
2823 ed
->type
= EXP_MMACRO
;
2826 ed
->max_depth
= (ed
->max_depth
+ 1);
2827 ed
->ignoring
= false;
2828 ed
->prev
= defining
;
2831 eed
= (ExpDef
*) hash_findix(&expdefs
, ed
->name
);
2833 if (!strcmp(eed
->name
, ed
->name
) &&
2834 (eed
->nparam_min
<= ed
->nparam_max
2836 && (ed
->nparam_min
<= eed
->nparam_max
2838 error(ERR_WARNING
|ERR_PASS1
,
2839 "redefining multi-line macro `%s'", ed
->name
);
2840 return DIRECTIVE_FOUND
;
2844 free_tlist(origline
);
2845 return DIRECTIVE_FOUND
;
2849 if (defining
!= NULL
) {
2850 if (defining
->type
== EXP_MMACRO
) {
2851 if (defining
->def_depth
> 0) {
2852 defining
->def_depth
--;
2853 return NO_DIRECTIVE_FOUND
;
2856 return NO_DIRECTIVE_FOUND
;
2859 if (!(defining
) || (defining
->type
!= EXP_MMACRO
)) {
2860 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2861 return DIRECTIVE_FOUND
;
2863 edhead
= (ExpDef
**) hash_findi_add(&expdefs
, defining
->name
);
2864 defining
->next
= *edhead
;
2867 defining
= ed
->prev
;
2868 ed
->prev
= expansions
;
2871 free_tlist(origline
);
2872 return DIRECTIVE_FOUND
;
2875 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2877 * We must search along istk->expansion until we hit a
2878 * macro invocation. Then we disable the emitting state(s)
2879 * between exitmacro and endmacro.
2881 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
2882 if(ei
->type
== EXP_MMACRO
) {
2889 * Set all invocations leading back to the macro
2890 * invocation to a non-emitting state.
2892 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
2893 eei
->emitting
= false;
2895 eei
->emitting
= false;
2897 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2899 free_tlist(origline
);
2900 return DIRECTIVE_FOUND
;
2904 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2909 spec
.casesense
= (i
== PP_UNMACRO
);
2910 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2911 return DIRECTIVE_FOUND
;
2913 ed_p
= (ExpDef
**) hash_findi(&expdefs
, spec
.name
, NULL
);
2914 while (ed_p
&& *ed_p
) {
2916 if (ed
->casesense
== spec
.casesense
&&
2917 !mstrcmp(ed
->name
, spec
.name
, spec
.casesense
) &&
2918 ed
->nparam_min
== spec
.nparam_min
&&
2919 ed
->nparam_max
== spec
.nparam_max
&&
2920 ed
->plus
== spec
.plus
) {
2927 free_tlist(origline
);
2928 free_tlist(spec
.dlist
);
2929 return DIRECTIVE_FOUND
;
2933 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2934 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2935 tline
= tline
->next
;
2937 free_tlist(origline
);
2938 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2939 return DIRECTIVE_FOUND
;
2941 t
= expand_smacro(tline
->next
);
2943 free_tlist(origline
);
2946 tokval
.t_type
= TOKEN_INVALID
;
2948 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2951 return DIRECTIVE_FOUND
;
2953 error(ERR_WARNING
|ERR_PASS1
,
2954 "trailing garbage after expression ignored");
2955 if (!is_simple(evalresult
)) {
2956 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2957 return DIRECTIVE_FOUND
;
2959 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
2960 if (ei
->type
== EXP_MMACRO
) {
2965 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2966 } else if (ei
->nparam
== 0) {
2968 "`%%rotate' invoked within macro without parameters");
2970 int rotate
= ei
->rotate
+ reloc_value(evalresult
);
2972 rotate
%= (int)ei
->nparam
;
2974 rotate
+= ei
->nparam
;
2975 ei
->rotate
= rotate
;
2977 return DIRECTIVE_FOUND
;
2980 if (defining
!= NULL
) {
2981 if (defining
->type
== EXP_REP
) {
2982 defining
->def_depth
++;
2984 return NO_DIRECTIVE_FOUND
;
2988 tline
= tline
->next
;
2989 } while (tok_type_(tline
, TOK_WHITESPACE
));
2991 if (tok_type_(tline
, TOK_ID
) &&
2992 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2995 tline
= tline
->next
;
2996 } while (tok_type_(tline
, TOK_WHITESPACE
));
3000 t
= expand_smacro(tline
);
3002 tokval
.t_type
= TOKEN_INVALID
;
3004 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3006 free_tlist(origline
);
3007 return DIRECTIVE_FOUND
;
3010 error(ERR_WARNING
|ERR_PASS1
,
3011 "trailing garbage after expression ignored");
3012 if (!is_simple(evalresult
)) {
3013 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3014 return DIRECTIVE_FOUND
;
3016 count
= reloc_value(evalresult
) + 1;
3018 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3021 free_tlist(origline
);
3024 ed
->nolist
= nolist
;
3027 ed
->max_depth
= (count
- 1);
3028 ed
->ignoring
= false;
3029 ed
->prev
= defining
;
3031 return DIRECTIVE_FOUND
;
3034 if (defining
!= NULL
) {
3035 if (defining
->type
== EXP_REP
) {
3036 if (defining
->def_depth
> 0) {
3037 defining
->def_depth
--;
3038 return NO_DIRECTIVE_FOUND
;
3041 return NO_DIRECTIVE_FOUND
;
3044 if ((defining
== NULL
) || (defining
->type
!= EXP_REP
)) {
3045 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3046 return DIRECTIVE_FOUND
;
3050 * Now we have a "macro" defined - although it has no name
3051 * and we won't be entering it in the hash tables - we must
3052 * push a macro-end marker for it on to istk->expansion.
3053 * After that, it will take care of propagating itself (a
3054 * macro-end marker line for a macro which is really a %rep
3055 * block will cause the macro to be re-expanded, complete
3056 * with another macro-end marker to ensure the process
3057 * continues) until the whole expansion is forcibly removed
3058 * from istk->expansion by a %exitrep.
3061 defining
= ed
->prev
;
3062 ed
->prev
= expansions
;
3067 ei
->current
= ed
->line
;
3068 ei
->emitting
= ((ed
->max_depth
> 0) ? true : false);
3069 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3070 ei
->prev
= istk
->expansion
;
3071 istk
->expansion
= ei
;
3072 free_tlist(origline
);
3073 return DIRECTIVE_FOUND
;
3076 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3078 * We must search along istk->expansion until we hit a
3079 * rep invocation. Then we disable the emitting state(s)
3080 * between exitrep and endrep.
3082 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3083 if (ei
->type
== EXP_REP
) {
3090 * Set all invocations leading back to the rep
3091 * invocation to a non-emitting state.
3093 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3094 eei
->emitting
= false;
3096 eei
->emitting
= false;
3097 eei
->current
= NULL
;
3098 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3100 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3102 free_tlist(origline
);
3103 return DIRECTIVE_FOUND
;
3109 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3110 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3112 tline
= tline
->next
;
3114 tline
= expand_id(tline
);
3115 if (!tline
|| (tline
->type
!= TOK_ID
&&
3116 (tline
->type
!= TOK_PREPROC_ID
||
3117 tline
->text
[1] != '$'))) {
3118 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3120 free_tlist(origline
);
3121 return DIRECTIVE_FOUND
;
3124 ctx
= get_ctx(tline
->text
, &mname
, false);
3126 param_start
= tline
= tline
->next
;
3129 /* Expand the macro definition now for %xdefine and %ixdefine */
3130 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3131 tline
= expand_smacro(tline
);
3133 if (tok_is_(tline
, "(")) {
3135 * This macro has parameters.
3138 tline
= tline
->next
;
3142 error(ERR_NONFATAL
, "parameter identifier expected");
3143 free_tlist(origline
);
3144 return DIRECTIVE_FOUND
;
3146 if (tline
->type
!= TOK_ID
) {
3148 "`%s': parameter identifier expected",
3150 free_tlist(origline
);
3151 return DIRECTIVE_FOUND
;
3153 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3154 tline
= tline
->next
;
3156 if (tok_is_(tline
, ",")) {
3157 tline
= tline
->next
;
3159 if (!tok_is_(tline
, ")")) {
3161 "`)' expected to terminate macro template");
3162 free_tlist(origline
);
3163 return DIRECTIVE_FOUND
;
3169 tline
= tline
->next
;
3171 if (tok_type_(tline
, TOK_WHITESPACE
))
3172 last
= tline
, tline
= tline
->next
;
3177 if (t
->type
== TOK_ID
) {
3178 list_for_each(tt
, param_start
)
3179 if (tt
->type
>= TOK_SMAC_PARAM
&&
3180 !strcmp(tt
->text
, t
->text
))
3184 t
->next
= macro_start
;
3189 * Good. We now have a macro name, a parameter count, and a
3190 * token list (in reverse order) for an expansion. We ought
3191 * to be OK just to create an SMacro, store it, and let
3192 * free_tlist have the rest of the line (which we have
3193 * carefully re-terminated after chopping off the expansion
3196 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3197 free_tlist(origline
);
3198 return DIRECTIVE_FOUND
;
3201 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3202 tline
= tline
->next
;
3204 tline
= expand_id(tline
);
3205 if (!tline
|| (tline
->type
!= TOK_ID
&&
3206 (tline
->type
!= TOK_PREPROC_ID
||
3207 tline
->text
[1] != '$'))) {
3208 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3209 free_tlist(origline
);
3210 return DIRECTIVE_FOUND
;
3213 error(ERR_WARNING
|ERR_PASS1
,
3214 "trailing garbage after macro name ignored");
3217 /* Find the context that symbol belongs to */
3218 ctx
= get_ctx(tline
->text
, &mname
, false);
3219 undef_smacro(ctx
, mname
);
3220 free_tlist(origline
);
3221 return DIRECTIVE_FOUND
;
3225 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3226 casesense
= (i
== PP_DEFSTR
);
3228 tline
= tline
->next
;
3230 tline
= expand_id(tline
);
3231 if (!tline
|| (tline
->type
!= TOK_ID
&&
3232 (tline
->type
!= TOK_PREPROC_ID
||
3233 tline
->text
[1] != '$'))) {
3234 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3236 free_tlist(origline
);
3237 return DIRECTIVE_FOUND
;
3240 ctx
= get_ctx(tline
->text
, &mname
, false);
3242 tline
= expand_smacro(tline
->next
);
3245 while (tok_type_(tline
, TOK_WHITESPACE
))
3246 tline
= delete_Token(tline
);
3248 p
= detoken(tline
, false);
3249 macro_start
= nasm_malloc(sizeof(*macro_start
));
3250 macro_start
->next
= NULL
;
3251 macro_start
->text
= nasm_quote(p
, strlen(p
));
3252 macro_start
->type
= TOK_STRING
;
3253 macro_start
->a
.mac
= NULL
;
3257 * We now have a macro name, an implicit parameter count of
3258 * zero, and a string token to use as an expansion. Create
3259 * and store an SMacro.
3261 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3262 free_tlist(origline
);
3263 return DIRECTIVE_FOUND
;
3267 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3268 casesense
= (i
== PP_DEFTOK
);
3270 tline
= tline
->next
;
3272 tline
= expand_id(tline
);
3273 if (!tline
|| (tline
->type
!= TOK_ID
&&
3274 (tline
->type
!= TOK_PREPROC_ID
||
3275 tline
->text
[1] != '$'))) {
3277 "`%s' expects a macro identifier as first parameter",
3279 free_tlist(origline
);
3280 return DIRECTIVE_FOUND
;
3282 ctx
= get_ctx(tline
->text
, &mname
, false);
3284 tline
= expand_smacro(tline
->next
);
3288 while (tok_type_(t
, TOK_WHITESPACE
))
3290 /* t should now point to the string */
3291 if (t
->type
!= TOK_STRING
) {
3293 "`%s` requires string as second parameter",
3296 free_tlist(origline
);
3297 return DIRECTIVE_FOUND
;
3300 nasm_unquote_cstr(t
->text
, i
);
3301 macro_start
= tokenize(t
->text
);
3304 * We now have a macro name, an implicit parameter count of
3305 * zero, and a numeric token to use as an expansion. Create
3306 * and store an SMacro.
3308 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3310 free_tlist(origline
);
3311 return DIRECTIVE_FOUND
;
3314 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3317 StrList
*xsl
= NULL
;
3318 StrList
**xst
= &xsl
;
3322 tline
= tline
->next
;
3324 tline
= expand_id(tline
);
3325 if (!tline
|| (tline
->type
!= TOK_ID
&&
3326 (tline
->type
!= TOK_PREPROC_ID
||
3327 tline
->text
[1] != '$'))) {
3329 "`%%pathsearch' expects a macro identifier as first parameter");
3330 free_tlist(origline
);
3331 return DIRECTIVE_FOUND
;
3333 ctx
= get_ctx(tline
->text
, &mname
, false);
3335 tline
= expand_smacro(tline
->next
);
3339 while (tok_type_(t
, TOK_WHITESPACE
))
3342 if (!t
|| (t
->type
!= TOK_STRING
&&
3343 t
->type
!= TOK_INTERNAL_STRING
)) {
3344 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3346 free_tlist(origline
);
3347 return DIRECTIVE_FOUND
; /* but we did _something_ */
3350 error(ERR_WARNING
|ERR_PASS1
,
3351 "trailing garbage after `%%pathsearch' ignored");
3353 if (t
->type
!= TOK_INTERNAL_STRING
)
3354 nasm_unquote(p
, NULL
);
3356 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3359 fclose(fp
); /* Don't actually care about the file */
3361 macro_start
= nasm_malloc(sizeof(*macro_start
));
3362 macro_start
->next
= NULL
;
3363 macro_start
->text
= nasm_quote(p
, strlen(p
));
3364 macro_start
->type
= TOK_STRING
;
3365 macro_start
->a
.mac
= NULL
;
3370 * We now have a macro name, an implicit parameter count of
3371 * zero, and a string token to use as an expansion. Create
3372 * and store an SMacro.
3374 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3376 free_tlist(origline
);
3377 return DIRECTIVE_FOUND
;
3381 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3384 tline
= tline
->next
;
3386 tline
= expand_id(tline
);
3387 if (!tline
|| (tline
->type
!= TOK_ID
&&
3388 (tline
->type
!= TOK_PREPROC_ID
||
3389 tline
->text
[1] != '$'))) {
3391 "`%%strlen' expects a macro identifier as first parameter");
3392 free_tlist(origline
);
3393 return DIRECTIVE_FOUND
;
3395 ctx
= get_ctx(tline
->text
, &mname
, false);
3397 tline
= expand_smacro(tline
->next
);
3401 while (tok_type_(t
, TOK_WHITESPACE
))
3403 /* t should now point to the string */
3404 if (!tok_type_(t
, TOK_STRING
)) {
3406 "`%%strlen` requires string as second parameter");
3408 free_tlist(origline
);
3409 return DIRECTIVE_FOUND
;
3412 macro_start
= nasm_malloc(sizeof(*macro_start
));
3413 macro_start
->next
= NULL
;
3414 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3415 macro_start
->a
.mac
= NULL
;
3418 * We now have a macro name, an implicit parameter count of
3419 * zero, and a numeric token to use as an expansion. Create
3420 * and store an SMacro.
3422 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3424 free_tlist(origline
);
3425 return DIRECTIVE_FOUND
;
3428 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3431 tline
= tline
->next
;
3433 tline
= expand_id(tline
);
3434 if (!tline
|| (tline
->type
!= TOK_ID
&&
3435 (tline
->type
!= TOK_PREPROC_ID
||
3436 tline
->text
[1] != '$'))) {
3438 "`%%strcat' expects a macro identifier as first parameter");
3439 free_tlist(origline
);
3440 return DIRECTIVE_FOUND
;
3442 ctx
= get_ctx(tline
->text
, &mname
, false);
3444 tline
= expand_smacro(tline
->next
);
3448 list_for_each(t
, tline
) {
3450 case TOK_WHITESPACE
:
3453 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3456 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3458 /* else fall through */
3461 "non-string passed to `%%strcat' (%d)", t
->type
);
3463 free_tlist(origline
);
3464 return DIRECTIVE_FOUND
;
3468 p
= pp
= nasm_malloc(len
);
3469 list_for_each(t
, tline
) {
3470 if (t
->type
== TOK_STRING
) {
3471 memcpy(p
, t
->text
, t
->a
.len
);
3477 * We now have a macro name, an implicit parameter count of
3478 * zero, and a numeric token to use as an expansion. Create
3479 * and store an SMacro.
3481 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3482 macro_start
->text
= nasm_quote(pp
, len
);
3484 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3486 free_tlist(origline
);
3487 return DIRECTIVE_FOUND
;
3490 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3497 tline
= tline
->next
;
3499 tline
= expand_id(tline
);
3500 if (!tline
|| (tline
->type
!= TOK_ID
&&
3501 (tline
->type
!= TOK_PREPROC_ID
||
3502 tline
->text
[1] != '$'))) {
3504 "`%%substr' expects a macro identifier as first parameter");
3505 free_tlist(origline
);
3506 return DIRECTIVE_FOUND
;
3508 ctx
= get_ctx(tline
->text
, &mname
, false);
3510 tline
= expand_smacro(tline
->next
);
3514 while (tok_type_(t
, TOK_WHITESPACE
))
3517 /* t should now point to the string */
3518 if (t
->type
!= TOK_STRING
) {
3520 "`%%substr` requires string as second parameter");
3522 free_tlist(origline
);
3523 return DIRECTIVE_FOUND
;
3528 tokval
.t_type
= TOKEN_INVALID
;
3529 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3533 free_tlist(origline
);
3534 return DIRECTIVE_FOUND
;
3535 } else if (!is_simple(evalresult
)) {
3536 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3538 free_tlist(origline
);
3539 return DIRECTIVE_FOUND
;
3541 a1
= evalresult
->value
-1;
3543 while (tok_type_(tt
, TOK_WHITESPACE
))
3546 a2
= 1; /* Backwards compatibility: one character */
3548 tokval
.t_type
= TOKEN_INVALID
;
3549 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3553 free_tlist(origline
);
3554 return DIRECTIVE_FOUND
;
3555 } else if (!is_simple(evalresult
)) {
3556 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3558 free_tlist(origline
);
3559 return DIRECTIVE_FOUND
;
3561 a2
= evalresult
->value
;
3564 len
= nasm_unquote(t
->text
, NULL
);
3567 if (a1
+a2
> (int64_t)len
)
3570 macro_start
= nasm_malloc(sizeof(*macro_start
));
3571 macro_start
->next
= NULL
;
3572 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3573 macro_start
->type
= TOK_STRING
;
3574 macro_start
->a
.mac
= NULL
;
3577 * We now have a macro name, an implicit parameter count of
3578 * zero, and a numeric token to use as an expansion. Create
3579 * and store an SMacro.
3581 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3583 free_tlist(origline
);
3584 return DIRECTIVE_FOUND
;
3589 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3590 casesense
= (i
== PP_ASSIGN
);
3592 tline
= tline
->next
;
3594 tline
= expand_id(tline
);
3595 if (!tline
|| (tline
->type
!= TOK_ID
&&
3596 (tline
->type
!= TOK_PREPROC_ID
||
3597 tline
->text
[1] != '$'))) {
3599 "`%%%sassign' expects a macro identifier",
3600 (i
== PP_IASSIGN
? "i" : ""));
3601 free_tlist(origline
);
3602 return DIRECTIVE_FOUND
;
3604 ctx
= get_ctx(tline
->text
, &mname
, false);
3606 tline
= expand_smacro(tline
->next
);
3611 tokval
.t_type
= TOKEN_INVALID
;
3613 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3616 free_tlist(origline
);
3617 return DIRECTIVE_FOUND
;
3621 error(ERR_WARNING
|ERR_PASS1
,
3622 "trailing garbage after expression ignored");
3624 if (!is_simple(evalresult
)) {
3626 "non-constant value given to `%%%sassign'",
3627 (i
== PP_IASSIGN
? "i" : ""));
3628 free_tlist(origline
);
3629 return DIRECTIVE_FOUND
;
3632 macro_start
= nasm_malloc(sizeof(*macro_start
));
3633 macro_start
->next
= NULL
;
3634 make_tok_num(macro_start
, reloc_value(evalresult
));
3635 macro_start
->a
.mac
= NULL
;
3638 * We now have a macro name, an implicit parameter count of
3639 * zero, and a numeric token to use as an expansion. Create
3640 * and store an SMacro.
3642 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3643 free_tlist(origline
);
3644 return DIRECTIVE_FOUND
;
3647 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3649 * Syntax is `%line nnn[+mmm] [filename]'
3651 tline
= tline
->next
;
3653 if (!tok_type_(tline
, TOK_NUMBER
)) {
3654 error(ERR_NONFATAL
, "`%%line' expects line number");
3655 free_tlist(origline
);
3656 return DIRECTIVE_FOUND
;
3658 k
= readnum(tline
->text
, &err
);
3660 tline
= tline
->next
;
3661 if (tok_is_(tline
, "+")) {
3662 tline
= tline
->next
;
3663 if (!tok_type_(tline
, TOK_NUMBER
)) {
3664 error(ERR_NONFATAL
, "`%%line' expects line increment");
3665 free_tlist(origline
);
3666 return DIRECTIVE_FOUND
;
3668 m
= readnum(tline
->text
, &err
);
3669 tline
= tline
->next
;
3675 nasm_free(src_set_fname(detoken(tline
, false)));
3677 free_tlist(origline
);
3678 return DIRECTIVE_FOUND
;
3681 if (defining
!= NULL
) {
3682 if (defining
->type
== EXP_WHILE
) {
3683 defining
->def_depth
++;
3685 return NO_DIRECTIVE_FOUND
;
3688 if ((istk
->expansion
!= NULL
) &&
3689 (istk
->expansion
->emitting
== false)) {
3693 l
->first
= copy_Token(tline
->next
);
3694 j
= if_condition(tline
->next
, i
);
3695 tline
->next
= NULL
; /* it got freed */
3696 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
3699 ed
->type
= EXP_WHILE
;
3702 ed
->max_depth
= DEADMAN_LIMIT
;
3703 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
3704 if (ed
->ignoring
== false) {
3707 } else if (l
!= NULL
) {
3708 delete_Token(l
->first
);
3712 ed
->prev
= defining
;
3714 free_tlist(origline
);
3715 return DIRECTIVE_FOUND
;
3718 if (defining
!= NULL
) {
3719 if (defining
->type
== EXP_WHILE
) {
3720 if (defining
->def_depth
> 0) {
3721 defining
->def_depth
--;
3722 return NO_DIRECTIVE_FOUND
;
3725 return NO_DIRECTIVE_FOUND
;
3728 if (tline
->next
!= NULL
) {
3729 error_precond(ERR_WARNING
|ERR_PASS1
,
3730 "trailing garbage after `%%endwhile' ignored");
3732 if ((defining
== NULL
) || (defining
->type
!= EXP_WHILE
)) {
3733 error(ERR_NONFATAL
, "`%%endwhile': no matching `%%while'");
3734 return DIRECTIVE_FOUND
;
3737 defining
= ed
->prev
;
3738 if (ed
->ignoring
== false) {
3739 ed
->prev
= expansions
;
3742 ei
->type
= EXP_WHILE
;
3744 ei
->current
= ed
->line
->next
;
3745 ei
->emitting
= true;
3746 ei
->prev
= istk
->expansion
;
3747 istk
->expansion
= ei
;
3751 free_tlist(origline
);
3752 return DIRECTIVE_FOUND
;
3755 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3757 * We must search along istk->expansion until we hit a
3758 * while invocation. Then we disable the emitting state(s)
3759 * between exitwhile and endwhile.
3761 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3762 if (ei
->type
== EXP_WHILE
) {
3769 * Set all invocations leading back to the while
3770 * invocation to a non-emitting state.
3772 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3773 eei
->emitting
= false;
3775 eei
->emitting
= false;
3776 eei
->current
= NULL
;
3777 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3779 error(ERR_NONFATAL
, "`%%exitwhile' not within `%%while' block");
3781 free_tlist(origline
);
3782 return DIRECTIVE_FOUND
;
3785 if (defining
!= NULL
) {
3786 if (defining
->type
== EXP_COMMENT
) {
3787 defining
->def_depth
++;
3789 return NO_DIRECTIVE_FOUND
;
3792 ed
->type
= EXP_COMMENT
;
3793 ed
->ignoring
= true;
3794 ed
->prev
= defining
;
3796 free_tlist(origline
);
3797 return DIRECTIVE_FOUND
;
3800 if (defining
!= NULL
) {
3801 if (defining
->type
== EXP_COMMENT
) {
3802 if (defining
->def_depth
> 0) {
3803 defining
->def_depth
--;
3804 return NO_DIRECTIVE_FOUND
;
3807 return NO_DIRECTIVE_FOUND
;
3810 if ((defining
== NULL
) || (defining
->type
!= EXP_COMMENT
)) {
3811 error(ERR_NONFATAL
, "`%%endcomment': no matching `%%comment'");
3812 return DIRECTIVE_FOUND
;
3815 defining
= ed
->prev
;
3817 free_tlist(origline
);
3818 return DIRECTIVE_FOUND
;
3821 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3822 if (in_final
!= false) {
3823 error(ERR_FATAL
, "`%%final' cannot be used recursively");
3825 tline
= tline
->next
;
3827 if (tline
== NULL
) {
3828 error(ERR_NONFATAL
, "`%%final' expects at least one parameter");
3831 l
->first
= copy_Token(tline
);
3835 free_tlist(origline
);
3836 return DIRECTIVE_FOUND
;
3840 "preprocessor directive `%s' not yet implemented",
3842 return DIRECTIVE_FOUND
;
3847 * Ensure that a macro parameter contains a condition code and
3848 * nothing else. Return the condition code index if so, or -1
3851 static int find_cc(Token
* t
)
3857 return -1; /* Probably a %+ without a space */
3860 if (t
->type
!= TOK_ID
)
3864 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3868 j
= ARRAY_SIZE(conditions
);
3871 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3886 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3888 Token
**tail
, *t
, *tt
;
3890 bool did_paste
= false;
3893 /* Now handle token pasting... */
3896 while ((t
= *tail
) && (tt
= t
->next
)) {
3898 case TOK_WHITESPACE
:
3899 if (tt
->type
== TOK_WHITESPACE
) {
3900 /* Zap adjacent whitespace tokens */
3901 t
->next
= delete_Token(tt
);
3903 /* Do not advance paste_head here */
3914 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3915 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3916 tt
->type
== TOK_OTHER
)) {
3917 len
+= strlen(tt
->text
);
3922 * Now tt points to the first token after
3923 * the potential paste area...
3925 if (tt
!= t
->next
) {
3926 /* We have at least two tokens... */
3927 len
+= strlen(t
->text
);
3928 p
= tmp
= nasm_malloc(len
+1);
3932 p
= strchr(p
, '\0');
3933 t
= delete_Token(t
);
3936 t
= *tail
= tokenize(tmp
);
3943 t
->next
= tt
; /* Attach the remaining token chain */
3951 case TOK_PASTE
: /* %+ */
3952 if (handle_paste_tokens
) {
3953 /* Zap %+ and whitespace tokens to the right */
3954 while (t
&& (t
->type
== TOK_WHITESPACE
||
3955 t
->type
== TOK_PASTE
))
3956 t
= *tail
= delete_Token(t
);
3957 if (!paste_head
|| !t
)
3958 break; /* Nothing to paste with */
3962 while (tok_type_(tt
, TOK_WHITESPACE
))
3963 tt
= t
->next
= delete_Token(tt
);
3966 tmp
= nasm_strcat(t
->text
, tt
->text
);
3968 tt
= delete_Token(tt
);
3969 t
= *tail
= tokenize(tmp
);
3975 t
->next
= tt
; /* Attach the remaining token chain */
3982 /* else fall through */
3985 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
3994 * expands to a list of tokens from %{x:y}
3996 static Token
*expand_mmac_params_range(ExpInv
*ei
, Token
*tline
, Token
***last
)
3998 Token
*t
= tline
, **tt
, *tm
, *head
;
4002 pos
= strchr(tline
->text
, ':');
4005 lst
= atoi(pos
+ 1);
4006 fst
= atoi(tline
->text
+ 1);
4009 * only macros params are accounted so
4010 * if someone passes %0 -- we reject such
4013 if (lst
== 0 || fst
== 0)
4016 /* the values should be sane */
4017 if ((fst
> (int)ei
->nparam
|| fst
< (-(int)ei
->nparam
)) ||
4018 (lst
> (int)ei
->nparam
|| lst
< (-(int)ei
->nparam
)))
4021 fst
= fst
< 0 ? fst
+ (int)ei
->nparam
+ 1: fst
;
4022 lst
= lst
< 0 ? lst
+ (int)ei
->nparam
+ 1: lst
;
4024 /* counted from zero */
4028 * it will be at least one token
4030 tm
= ei
->params
[(fst
+ ei
->rotate
) % ei
->nparam
];
4031 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4032 head
= t
, tt
= &t
->next
;
4034 for (i
= fst
+ 1; i
<= lst
; i
++) {
4035 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4036 *tt
= t
, tt
= &t
->next
;
4037 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4039 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4040 *tt
= t
, tt
= &t
->next
;
4043 for (i
= fst
- 1; i
>= lst
; i
--) {
4044 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4045 *tt
= t
, tt
= &t
->next
;
4046 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4048 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4049 *tt
= t
, tt
= &t
->next
;
4057 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
4063 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4064 * %-n) and MMacro-local identifiers (%%foo) as well as
4065 * macro indirection (%[...]) and range (%{..:..}).
4067 static Token
*expand_mmac_params(Token
* tline
)
4069 Token
*t
, *tt
, **tail
, *thead
;
4070 bool changed
= false;
4077 if (tline
->type
== TOK_PREPROC_ID
&&
4078 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4079 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4080 tline
->text
[1] == '%')) {
4082 int type
= 0, cc
; /* type = 0 to placate optimisers */
4089 tline
= tline
->next
;
4091 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
4092 if (ei
->type
== EXP_MMACRO
) {
4097 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4099 pos
= strchr(t
->text
, ':');
4101 switch (t
->text
[1]) {
4103 * We have to make a substitution of one of the
4104 * forms %1, %-1, %+1, %%foo, %0.
4107 if ((strlen(t
->text
) > 2) && (t
->text
[2] == '0')) {
4109 text
= nasm_strdup(ei
->label_text
);
4112 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", ei
->nparam
);
4113 text
= nasm_strdup(tmpbuf
);
4118 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4120 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4123 n
= atoi(t
->text
+ 2) - 1;
4124 if (n
>= ei
->nparam
)
4128 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4134 "macro parameter %d is not a condition code",
4139 if (inverse_ccs
[cc
] == -1) {
4141 "condition code `%s' is not invertible",
4145 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4149 n
= atoi(t
->text
+ 2) - 1;
4150 if (n
>= ei
->nparam
)
4154 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4160 "macro parameter %d is not a condition code",
4165 text
= nasm_strdup(conditions
[cc
]);
4169 n
= atoi(t
->text
+ 1) - 1;
4170 if (n
>= ei
->nparam
)
4174 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4178 for (i
= 0; i
< ei
->paramlen
[n
]; i
++) {
4179 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4180 tail
= &(*tail
)->next
;
4184 text
= NULL
; /* we've done it here */
4189 * seems we have a parameters range here
4191 Token
*head
, **last
;
4192 head
= expand_mmac_params_range(ei
, t
, &last
);
4213 } else if (tline
->type
== TOK_INDIRECT
) {
4215 tline
= tline
->next
;
4216 tt
= tokenize(t
->text
);
4217 tt
= expand_mmac_params(tt
);
4218 tt
= expand_smacro(tt
);
4221 tt
->a
.mac
= NULL
; /* Necessary? */
4229 tline
= tline
->next
;
4237 paste_tokens(&thead
, false);
4243 * Expand all single-line macro calls made in the given line.
4244 * Return the expanded version of the line. The original is deemed
4245 * to be destroyed in the process. (In reality we'll just move
4246 * Tokens from input to output a lot of the time, rather than
4247 * actually bothering to destroy and replicate.)
4250 static Token
*expand_smacro(Token
* tline
)
4252 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4253 SMacro
*head
= NULL
, *m
;
4256 unsigned int nparam
, sparam
;
4258 Token
*org_tline
= tline
;
4261 int deadman
= DEADMAN_LIMIT
;
4265 * Trick: we should avoid changing the start token pointer since it can
4266 * be contained in "next" field of other token. Because of this
4267 * we allocate a copy of first token and work with it; at the end of
4268 * routine we copy it back
4271 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4272 org_tline
->text
, 0);
4273 tline
->a
.mac
= org_tline
->a
.mac
;
4274 nasm_free(org_tline
->text
);
4275 org_tline
->text
= NULL
;
4278 expanded
= true; /* Always expand %+ at least once */
4284 while (tline
) { /* main token loop */
4286 error(ERR_NONFATAL
, "interminable macro recursion");
4290 if ((mname
= tline
->text
)) {
4291 /* if this token is a local macro, look in local context */
4292 if (tline
->type
== TOK_ID
) {
4293 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4294 } else if (tline
->type
== TOK_PREPROC_ID
) {
4295 ctx
= get_ctx(mname
, &mname
, true);
4296 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4301 * We've hit an identifier. As in is_mmacro below, we first
4302 * check whether the identifier is a single-line macro at
4303 * all, then think about checking for parameters if
4306 list_for_each(m
, head
)
4307 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4313 if (m
->nparam
== 0) {
4315 * Simple case: the macro is parameterless. Discard the
4316 * one token that the macro call took, and push the
4317 * expansion back on the to-do stack.
4319 if (!m
->expansion
) {
4320 if (!strcmp("__FILE__", m
->name
)) {
4323 src_get(&num
, &file
);
4324 tline
->text
= nasm_quote(file
, strlen(file
));
4325 tline
->type
= TOK_STRING
;
4329 if (!strcmp("__LINE__", m
->name
)) {
4330 nasm_free(tline
->text
);
4331 make_tok_num(tline
, src_get_linnum());
4334 if (!strcmp("__BITS__", m
->name
)) {
4335 nasm_free(tline
->text
);
4336 make_tok_num(tline
, globalbits
);
4339 tline
= delete_Token(tline
);
4344 * Complicated case: at least one macro with this name
4345 * exists and takes parameters. We must find the
4346 * parameters in the call, count them, find the SMacro
4347 * that corresponds to that form of the macro call, and
4348 * substitute for the parameters when we expand. What a
4351 /*tline = tline->next;
4352 skip_white_(tline); */
4355 while (tok_type_(t
, TOK_SMAC_END
)) {
4356 t
->a
.mac
->in_progress
= false;
4358 t
= tline
->next
= delete_Token(t
);
4361 } while (tok_type_(tline
, TOK_WHITESPACE
));
4362 if (!tok_is_(tline
, "(")) {
4364 * This macro wasn't called with parameters: ignore
4365 * the call. (Behaviour borrowed from gnu cpp.)
4374 sparam
= PARAM_DELTA
;
4375 params
= nasm_malloc(sparam
* sizeof(Token
*));
4376 params
[0] = tline
->next
;
4377 paramsize
= nasm_malloc(sparam
* sizeof(int));
4379 while (true) { /* parameter loop */
4381 * For some unusual expansions
4382 * which concatenates function call
4385 while (tok_type_(t
, TOK_SMAC_END
)) {
4386 t
->a
.mac
->in_progress
= false;
4388 t
= tline
->next
= delete_Token(t
);
4394 "macro call expects terminating `)'");
4397 if (tline
->type
== TOK_WHITESPACE
4399 if (paramsize
[nparam
])
4402 params
[nparam
] = tline
->next
;
4403 continue; /* parameter loop */
4405 if (tline
->type
== TOK_OTHER
4406 && tline
->text
[1] == 0) {
4407 char ch
= tline
->text
[0];
4408 if (ch
== ',' && !paren
&& brackets
<= 0) {
4409 if (++nparam
>= sparam
) {
4410 sparam
+= PARAM_DELTA
;
4411 params
= nasm_realloc(params
,
4412 sparam
* sizeof(Token
*));
4413 paramsize
= nasm_realloc(paramsize
,
4414 sparam
* sizeof(int));
4416 params
[nparam
] = tline
->next
;
4417 paramsize
[nparam
] = 0;
4419 continue; /* parameter loop */
4422 (brackets
> 0 || (brackets
== 0 &&
4423 !paramsize
[nparam
])))
4425 if (!(brackets
++)) {
4426 params
[nparam
] = tline
->next
;
4427 continue; /* parameter loop */
4430 if (ch
== '}' && brackets
> 0)
4431 if (--brackets
== 0) {
4433 continue; /* parameter loop */
4435 if (ch
== '(' && !brackets
)
4437 if (ch
== ')' && brackets
<= 0)
4443 error(ERR_NONFATAL
, "braces do not "
4444 "enclose all of macro parameter");
4446 paramsize
[nparam
] += white
+ 1;
4448 } /* parameter loop */
4450 while (m
&& (m
->nparam
!= nparam
||
4451 mstrcmp(m
->name
, mname
,
4455 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4456 "macro `%s' exists, "
4457 "but not taking %d parameters",
4458 mstart
->text
, nparam
);
4461 if (m
&& m
->in_progress
)
4463 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4465 * Design question: should we handle !tline, which
4466 * indicates missing ')' here, or expand those
4467 * macros anyway, which requires the (t) test a few
4471 nasm_free(paramsize
);
4475 * Expand the macro: we are placed on the last token of the
4476 * call, so that we can easily split the call from the
4477 * following tokens. We also start by pushing an SMAC_END
4478 * token for the cycle removal.
4485 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4487 m
->in_progress
= true;
4489 list_for_each(t
, m
->expansion
) {
4490 if (t
->type
>= TOK_SMAC_PARAM
) {
4491 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4495 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4496 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4498 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4504 } else if (t
->type
== TOK_PREPROC_Q
) {
4505 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4507 } else if (t
->type
== TOK_PREPROC_QQ
) {
4508 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4511 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4517 * Having done that, get rid of the macro call, and clean
4518 * up the parameters.
4521 nasm_free(paramsize
);
4524 continue; /* main token loop */
4529 if (tline
->type
== TOK_SMAC_END
) {
4530 tline
->a
.mac
->in_progress
= false;
4531 tline
= delete_Token(tline
);
4534 tline
= tline
->next
;
4542 * Now scan the entire line and look for successive TOK_IDs that resulted
4543 * after expansion (they can't be produced by tokenize()). The successive
4544 * TOK_IDs should be concatenated.
4545 * Also we look for %+ tokens and concatenate the tokens before and after
4546 * them (without white spaces in between).
4548 if (expanded
&& paste_tokens(&thead
, true)) {
4550 * If we concatenated something, *and* we had previously expanded
4551 * an actual macro, scan the lines again for macros...
4561 *org_tline
= *thead
;
4562 /* since we just gave text to org_line, don't free it */
4564 delete_Token(thead
);
4566 /* the expression expanded to empty line;
4567 we can't return NULL for some reasons
4568 we just set the line to a single WHITESPACE token. */
4569 memset(org_tline
, 0, sizeof(*org_tline
));
4570 org_tline
->text
= NULL
;
4571 org_tline
->type
= TOK_WHITESPACE
;
4580 * Similar to expand_smacro but used exclusively with macro identifiers
4581 * right before they are fetched in. The reason is that there can be
4582 * identifiers consisting of several subparts. We consider that if there
4583 * are more than one element forming the name, user wants a expansion,
4584 * otherwise it will be left as-is. Example:
4588 * the identifier %$abc will be left as-is so that the handler for %define
4589 * will suck it and define the corresponding value. Other case:
4591 * %define _%$abc cde
4593 * In this case user wants name to be expanded *before* %define starts
4594 * working, so we'll expand %$abc into something (if it has a value;
4595 * otherwise it will be left as-is) then concatenate all successive
4598 static Token
*expand_id(Token
* tline
)
4600 Token
*cur
, *oldnext
= NULL
;
4602 if (!tline
|| !tline
->next
)
4607 (cur
->next
->type
== TOK_ID
||
4608 cur
->next
->type
== TOK_PREPROC_ID
4609 || cur
->next
->type
== TOK_NUMBER
))
4612 /* If identifier consists of just one token, don't expand */
4617 oldnext
= cur
->next
; /* Detach the tail past identifier */
4618 cur
->next
= NULL
; /* so that expand_smacro stops here */
4621 tline
= expand_smacro(tline
);
4624 /* expand_smacro possibly changhed tline; re-scan for EOL */
4626 while (cur
&& cur
->next
)
4629 cur
->next
= oldnext
;
4636 * Determine whether the given line constitutes a multi-line macro
4637 * call, and return the ExpDef structure called if so. Doesn't have
4638 * to check for an initial label - that's taken care of in
4639 * expand_mmacro - but must check numbers of parameters. Guaranteed
4640 * to be called with tline->type == TOK_ID, so the putative macro
4641 * name is easy to find.
4643 static ExpDef
*is_mmacro(Token
* tline
, Token
*** params_array
)
4649 head
= (ExpDef
*) hash_findix(&expdefs
, tline
->text
);
4652 * Efficiency: first we see if any macro exists with the given
4653 * name. If not, we can return NULL immediately. _Then_ we
4654 * count the parameters, and then we look further along the
4655 * list if necessary to find the proper ExpDef.
4657 list_for_each(ed
, head
)
4658 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4664 * OK, we have a potential macro. Count and demarcate the
4667 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4670 * So we know how many parameters we've got. Find the ExpDef
4671 * structure that handles this number.
4674 if (ed
->nparam_min
<= nparam
4675 && (ed
->plus
|| nparam
<= ed
->nparam_max
)) {
4677 * It's right, and we can use it. Add its default
4678 * parameters to the end of our list if necessary.
4680 if (ed
->defaults
&& nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4682 nasm_realloc(params
,
4683 ((ed
->nparam_min
+ ed
->ndefs
+
4684 1) * sizeof(*params
)));
4685 while (nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4686 params
[nparam
] = ed
->defaults
[nparam
- ed
->nparam_min
];
4691 * If we've gone over the maximum parameter count (and
4692 * we're in Plus mode), ignore parameters beyond
4695 if (ed
->plus
&& nparam
> ed
->nparam_max
)
4696 nparam
= ed
->nparam_max
;
4698 * Then terminate the parameter list, and leave.
4700 if (!params
) { /* need this special case */
4701 params
= nasm_malloc(sizeof(*params
));
4704 params
[nparam
] = NULL
;
4705 *params_array
= params
;
4709 * This one wasn't right: look for the next one with the
4712 list_for_each(ed
, ed
->next
)
4713 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4718 * After all that, we didn't find one with the right number of
4719 * parameters. Issue a warning, and fail to expand the macro.
4721 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4722 "macro `%s' exists, but not taking %d parameters",
4723 tline
->text
, nparam
);
4729 * Expand the multi-line macro call made by the given line, if
4730 * there is one to be expanded. If there is, push the expansion on
4731 * istk->expansion and return 1. Otherwise return 0.
4733 static int expand_mmacro(Token
* tline
)
4735 Token
*startline
= tline
;
4736 Token
*label
= NULL
;
4737 int dont_prepend
= 0;
4738 Token
**params
, *t
, *mtok
, *tt
;
4742 int i
, nparam
, *paramlen
;
4747 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4748 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4751 ed
= is_mmacro(t
, ¶ms
);
4757 * We have an id which isn't a macro call. We'll assume
4758 * it might be a label; we'll also check to see if a
4759 * colon follows it. Then, if there's another id after
4760 * that lot, we'll check it again for macro-hood.
4764 if (tok_type_(t
, TOK_WHITESPACE
))
4765 last
= t
, t
= t
->next
;
4766 if (tok_is_(t
, ":")) {
4768 last
= t
, t
= t
->next
;
4769 if (tok_type_(t
, TOK_WHITESPACE
))
4770 last
= t
, t
= t
->next
;
4772 if (!tok_type_(t
, TOK_ID
) || !(ed
= is_mmacro(t
, ¶ms
)))
4780 * Fix up the parameters: this involves stripping leading and
4781 * trailing whitespace, then stripping braces if they are
4784 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4785 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4787 for (i
= 0; params
[i
]; i
++) {
4789 int comma
= (!ed
->plus
|| i
< nparam
- 1);
4793 if (tok_is_(t
, "{"))
4794 t
= t
->next
, brace
= true, comma
= false;
4798 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4799 break; /* ... because we have hit a comma */
4800 if (comma
&& t
->type
== TOK_WHITESPACE
4801 && tok_is_(t
->next
, ","))
4802 break; /* ... or a space then a comma */
4803 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4804 break; /* ... or a brace */
4810 if (ed
->cur_depth
>= ed
->max_depth
) {
4811 if (ed
->max_depth
> 1) {
4813 "reached maximum macro recursion depth of %i for %s",
4814 ed
->max_depth
,ed
->name
);
4822 * OK, we have found a ExpDef structure representing a
4823 * previously defined mmacro. Create an expansion invocation
4824 * and point it back to the expansion definition. Substitution of
4825 * parameter tokens and macro-local tokens doesn't get done
4826 * until the single-line macro substitution process; this is
4827 * because delaying them allows us to change the semantics
4828 * later through %rotate.
4831 ei
->type
= EXP_MMACRO
;
4833 // ei->label = label;
4834 ei
->label_text
= detoken(label
, false);
4835 ei
->current
= ed
->line
;
4836 ei
->emitting
= true;
4837 // ei->iline = tline;
4838 ei
->params
= params
;
4839 ei
->nparam
= nparam
;
4841 ei
->paramlen
= paramlen
;
4844 ei
->prev
= istk
->expansion
;
4845 istk
->expansion
= ei
;
4847 /***** todo: relocate %? (Q) and %?? (QQ); %00 already relocated *****/
4849 list_for_each(l, m->expansion) {
4853 l->next = istk->expansion;
4854 istk->expansion = l;
4857 list_for_each(t, ei->current->first) {
4861 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4863 case TOK_PREPROC_QQ:
4864 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4866 case TOK_PREPROC_ID:
4867 if (t->text[1] == '0' && t->text[2] == '0') {
4875 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4884 * If we had a label, push it on as the first line of
4885 * the macro expansion.
4888 if (dont_prepend
< 0)
4889 free_tlist(startline
);
4893 l
->first
= startline
;
4894 if (!dont_prepend
) {
4896 label
= label
->next
;
4897 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4902 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4907 /* The function that actually does the error reporting */
4908 static void verror(int severity
, const char *fmt
, va_list arg
)
4912 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4914 if ((istk
!= NULL
) &&
4915 (istk
->expansion
!= NULL
) &&
4916 (istk
->expansion
->type
== EXP_MMACRO
)) {
4917 ExpDef
*ed
= istk
->expansion
->def
;
4918 nasm_error(severity
, "(%s:%d) %s", ed
->name
,
4919 istk
->expansion
->lineno
, buff
);
4921 nasm_error(severity
, "%s", buff
);
4926 * Since preprocessor always operate only on the line that didn't
4927 * arrived yet, we should always use ERR_OFFBY1.
4929 static void error(int severity
, const char *fmt
, ...)
4933 verror(severity
, fmt
, arg
);
4938 * Because %else etc are evaluated in the state context
4939 * of the previous branch, errors might get lost with error():
4940 * %if 0 ... %else trailing garbage ... %endif
4941 * So %else etc should report errors with this function.
4943 static void error_precond(int severity
, const char *fmt
, ...)
4947 /* Only ignore the error if it's really in a dead branch */
4948 if ((istk
!= NULL
) &&
4949 (istk
->expansion
!= NULL
) &&
4950 (istk
->expansion
->type
== EXP_IF
) &&
4951 (istk
->expansion
->def
->state
== COND_NEVER
))
4955 verror(severity
, fmt
, arg
);
4960 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
4965 istk
= nasm_malloc(sizeof(Include
));
4967 istk
->expansion
= NULL
;
4968 istk
->fp
= fopen(file
, "r");
4970 src_set_fname(nasm_strdup(file
));
4974 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4979 nested_mac_count
= 0;
4980 nested_rep_count
= 0;
4983 if (tasm_compatible_mode
) {
4984 stdmacpos
= nasm_stdmac
;
4986 stdmacpos
= nasm_stdmac_after_tasm
;
4988 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4993 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4994 * The caller, however, will also pass in 3 for preprocess-only so
4995 * we can set __PASS__ accordingly.
4997 pass
= apass
> 2 ? 2 : apass
;
4999 dephead
= deptail
= deplist
;
5001 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
5003 strcpy(sl
->str
, file
);
5005 deptail
= &sl
->next
;
5009 * Define the __PASS__ macro. This is defined here unlike
5010 * all the other builtins, because it is special -- it varies between
5013 t
= nasm_malloc(sizeof(*t
));
5015 make_tok_num(t
, apass
);
5017 define_smacro(NULL
, "__PASS__", true, 0, t
);
5020 static char *pp_getline(void)
5027 * Fetch a tokenized line, either from the expansion
5028 * buffer or from the input file.
5032 while (1) { /* until we get a line we can use */
5034 * Fetch a tokenized line from the expansion buffer
5036 if (istk
->expansion
!= NULL
) {
5038 ExpInv
*e
= istk
->expansion
;
5039 if (e
->current
!= NULL
) {
5040 if (e
->emitting
== false) {
5046 e
->current
= l
->next
;
5048 tline
= copy_Token(l
->first
);
5049 if (((e
->type
== EXP_REP
) ||
5050 (e
->type
== EXP_MMACRO
) ||
5051 (e
->type
== EXP_WHILE
))
5052 && (e
->def
->nolist
== false)) {
5053 char *p
= detoken(tline
, false);
5054 list
->line(LIST_MACRO
, p
);
5058 } else if ((e
->type
== EXP_REP
) &&
5059 (e
->def
->cur_depth
< e
->def
->max_depth
)) {
5060 e
->def
->cur_depth
++;
5061 e
->current
= e
->def
->line
;
5064 } else if ((e
->type
== EXP_WHILE
) &&
5065 (e
->def
->cur_depth
< e
->def
->max_depth
)) {
5066 e
->current
= e
->def
->line
;
5068 tline
= copy_Token(e
->current
->first
);
5069 int j
= if_condition(tline
, PP_WHILE
);
5071 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
5072 if (j
== COND_IF_TRUE
) {
5073 e
->current
= e
->current
->next
;
5074 e
->def
->cur_depth
++;
5076 e
->emitting
= false;
5078 e
->def
->cur_depth
= e
->def
->max_depth
;
5082 istk
->expansion
= e
->prev
;
5083 ExpDef
*ed
= e
->def
;
5085 if ((e
->emitting
== true) &&
5086 (ed
->max_depth
== DEADMAN_LIMIT
) &&
5087 (ed
->cur_depth
== DEADMAN_LIMIT
)
5089 error(ERR_FATAL
, "runaway expansion detected, aborting");
5091 if (ed
->cur_depth
> 0) {
5093 } else if ((ed
->type
!= EXP_MMACRO
) && (ed
->type
!= EXP_IF
)) {
5094 /***** should this really be right here??? *****/
5096 Line *l = NULL, *ll = NULL;
5097 for (l = ed->line; l != NULL;) {
5098 if (l->first != NULL) {
5099 free_tlist(l->first);
5106 expansions = ed->prev;
5110 if ((e
->type
== EXP_REP
) ||
5111 (e
->type
== EXP_MMACRO
) ||
5112 (e
->type
== EXP_WHILE
)) {
5113 list
->downlevel(LIST_MACRO
);
5122 * Read in line from input and tokenize
5125 if (line
) { /* from the current input file */
5126 line
= prepreproc(line
);
5127 tline
= tokenize(line
);
5133 * The current file has ended; work down the istk
5138 if (i
->expansion
!= NULL
) {
5140 "end of file while still in an expansion");
5142 /* only set line and file name if there's a next node */
5144 src_set_linnum(i
->lineno
);
5145 nasm_free(src_set_fname(i
->fname
));
5147 if ((i
->next
== NULL
) && (finals
!= NULL
)) {
5149 ExpInv
*ei
= new_ExpInv();
5150 ei
->type
= EXP_FINAL
;
5151 ei
->emitting
= true;
5152 ei
->current
= finals
;
5153 istk
->expansion
= ei
;
5158 list
->downlevel(LIST_INCLUDE
);
5161 if (finals
!= NULL
) {
5171 if (defining
== NULL
) {
5172 tline
= expand_mmac_params(tline
);
5176 * Check the line to see if it's a preprocessor directive.
5178 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5180 } else if (defining
!= NULL
) {
5182 * We're defining an expansion. We emit nothing at all,
5183 * and just shove the tokenized line on to the definition.
5185 if (defining
->ignoring
== false) {
5186 Line
*l
= new_Line();
5188 if (defining
->line
== NULL
) {
5192 defining
->last
->next
= l
;
5196 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5199 } else if ((istk
->expansion
!= NULL
) &&
5200 (istk
->expansion
->emitting
!= true)) {
5202 * We're in a non-emitting branch of an expansion.
5203 * Emit nothing at all, not even a blank line: when we
5204 * emerge from the expansion we'll give a line-number
5205 * directive so we keep our place correctly.
5210 tline
= expand_smacro(tline
);
5211 if (!expand_mmacro(tline
)) {
5213 * De-tokenize the line again, and emit it.
5215 line
= detoken(tline
, true);
5219 continue; /* expand_mmacro calls free_tlist */
5226 static void pp_cleanup(int pass
)
5228 if (defining
!= NULL
) {
5229 error(ERR_NONFATAL
, "end of file while still defining an expansion");
5230 nasm_free(defining
); /***** todo: free everything to avoid mem leaks *****/
5233 while (cstk
!= NULL
)
5236 while (istk
!= NULL
) {
5240 nasm_free(i
->fname
);
5245 nasm_free(src_set_fname(NULL
));
5250 while ((i
= ipath
)) {
5259 void pp_include_path(char *path
)
5263 i
= nasm_malloc(sizeof(IncPath
));
5264 i
->path
= path
? nasm_strdup(path
) : NULL
;
5277 void pp_pre_include(char *fname
)
5279 Token
*inc
, *space
, *name
;
5282 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5283 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5284 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5292 void pp_pre_define(char *definition
)
5298 equals
= strchr(definition
, '=');
5299 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5300 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5303 space
->next
= tokenize(definition
);
5313 void pp_pre_undefine(char *definition
)
5318 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5319 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5320 space
->next
= tokenize(definition
);
5329 * This function is used to assist with "runtime" preprocessor
5330 * directives, e.g. pp_runtime("%define __BITS__ 64");
5332 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5333 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5336 void pp_runtime(char *definition
)
5340 def
= tokenize(definition
);
5341 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5346 void pp_extra_stdmac(macros_t
*macros
)
5348 extrastdmac
= macros
;
5351 static void make_tok_num(Token
* tok
, int64_t val
)
5354 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5355 tok
->text
= nasm_strdup(numbuf
);
5356 tok
->type
= TOK_NUMBER
;