1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2011 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.
160 TOK_INDIRECT
, /* %[...] */
161 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
165 #define PP_CONCAT_MASK(x) (1 << (x))
167 struct tokseq_match
{
176 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
177 size_t len
; /* scratch length field */
178 } a
; /* Auxiliary data */
179 enum pp_token_type type
;
183 * Expansion definitions are stored as a linked list of
184 * these, which is essentially a container to allow several linked
187 * Note that in this module, linked lists are treated as stacks
188 * wherever possible. For this reason, Lines are _pushed_ on to the
189 * `last' field in ExpDef structures, so that the linked list,
190 * if walked, would emit the expansion lines in the proper order.
209 EXP_MAX
= INT_MAX
/* Keep compiler from reducing the range */
213 * Store the definition of an expansion, in which is any
214 * preprocessor directive that has an ending pair.
216 * This design allows for arbitrary expansion/recursion depth,
217 * upto the DEADMAN_LIMIT.
219 * The `next' field is used for storing ExpDef in hash tables; the
220 * `prev' field is for the global `expansions` linked-list.
223 ExpDef
*prev
; /* previous definition */
224 ExpDef
*next
; /* next in hash table */
225 enum pp_exp_type type
; /* expansion type */
226 char *name
; /* definition name */
230 bool plus
; /* is the last parameter greedy? */
231 bool nolist
; /* is this expansion listing-inhibited? */
232 Token
*dlist
; /* all defaults as one list */
233 Token
**defaults
; /* parameter default pointers */
234 int ndefs
; /* number of default parameters */
236 int prepend
; /* label prepend state */
240 int linecount
; /* number of lines within expansion */
242 int64_t def_depth
; /* current number of definition pairs deep */
243 int64_t cur_depth
; /* current number of expansions */
244 int64_t max_depth
; /* maximum number of expansions allowed */
246 int state
; /* condition state */
247 bool ignoring
; /* ignoring definition lines */
251 * Store the invocation of an expansion.
253 * The `prev' field is for the `istk->expansion` linked-list.
255 * When an expansion is being expanded, `params', `iline', `nparam',
256 * `paramlen', `rotate' and `unique' are local to the invocation.
259 ExpInv
*prev
; /* previous invocation */
260 ExpDef
*def
; /* pointer to expansion definition */
261 char *name
; /* invocation name */
262 Line
*label
; /* pointer to label */
263 char *label_text
; /* pointer to label text */
264 Line
*current
; /* pointer to current line in invocation */
266 Token
**params
; /* actual parameters */
267 Token
*iline
; /* invocation line */
273 int lineno
; /* current line number in expansion */
274 int linnum
; /* line number at invocation */
275 int relno
; /* relative line number at invocation */
276 enum pp_exp_type type
; /* expansion type */
281 * To handle an arbitrary level of file inclusion, we maintain a
282 * stack (ie linked list) of these things.
296 * Include search path. This is simply a list of strings which get
297 * prepended, in turn, to the name of an include file, in an
298 * attempt to find the file if it's not in the current directory.
306 * Conditional assembly: we maintain a separate stack of these for
307 * each level of file inclusion. (The only reason we keep the
308 * stacks separate is to ensure that a stray `%endif' in a file
309 * included from within the true branch of a `%if' won't terminate
310 * it and cause confusion: instead, rightly, it'll cause an error.)
314 * These states are for use just after %if or %elif: IF_TRUE
315 * means the condition has evaluated to truth so we are
316 * currently emitting, whereas IF_FALSE means we are not
317 * currently emitting but will start doing so if a %else comes
318 * up. In these states, all directives are admissible: %elif,
319 * %else and %endif. (And of course %if.)
324 * These states come up after a %else: ELSE_TRUE means we're
325 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
326 * any %elif or %else will cause an error.
331 * These states mean that we're not emitting now, and also that
332 * nothing until %endif will be emitted at all. COND_DONE is
333 * used when we've had our moment of emission
334 * and have now started seeing %elifs. COND_NEVER is used when
335 * the condition construct in question is contained within a
336 * non-emitting branch of a larger condition construct,
337 * or if there is an error.
344 * These defines are used as the possible return values for do_directive
346 #define NO_DIRECTIVE_FOUND 0
347 #define DIRECTIVE_FOUND 1
350 * This define sets the upper limit for smacro and expansions
352 #define DEADMAN_LIMIT (1 << 20)
355 #define REP_LIMIT ((INT64_C(1) << 62))
358 * Condition codes. Note that we use c_ prefix not C_ because C_ is
359 * used in nasm.h for the "real" condition codes. At _this_ level,
360 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
361 * ones, so we need a different enum...
363 static const char * const conditions
[] = {
364 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
365 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
366 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
369 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
370 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
371 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
374 static const enum pp_conds inverse_ccs
[] = {
375 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
376 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
,
377 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
380 /* For TASM compatibility we need to be able to recognise TASM compatible
381 * conditional compilation directives. Using the NASM pre-processor does
382 * not work, so we look for them specifically from the following list and
383 * then jam in the equivalent NASM directive into the input stream.
387 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
388 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
391 static const char * const tasm_directives
[] = {
392 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
393 "ifndef", "include", "local"
396 static int StackSize
= 4;
397 static char *StackPointer
= "ebp";
398 static int ArgOffset
= 8;
399 static int LocalOffset
= 0;
401 static Context
*cstk
;
402 static Include
*istk
;
403 static IncPath
*ipath
;
405 static int pass
; /* HACK: pass 0 = generate dependencies only */
406 static StrList
**dephead
, **deptail
; /* Dependency list */
408 static uint64_t unique
; /* unique identifier numbers */
411 static bool do_predef
;
413 static ListGen
*list
;
416 * The current set of expansion definitions we have defined.
418 static struct hash_table expdefs
;
421 * The current set of single-line macros we have defined.
423 static struct hash_table smacros
;
426 * Linked List of all active expansion definitions
428 struct ExpDef
*expansions
;
431 * The expansion we are currently defining
433 static ExpDef
*defining
;
435 static uint64_t nested_mac_count
;
436 static uint64_t nested_rep_count
;
439 * Linked-list of lines to preprocess, prior to cleanup
442 static bool in_final
= false;
445 * The number of macro parameters to allocate space for at a time.
447 #define PARAM_DELTA 16
450 * The standard macro set: defined in macros.c in the array nasm_stdmac.
451 * This gives our position in the macro set, when we're processing it.
453 static macros_t
*stdmacpos
;
456 * The extra standard macros that come from the object format, if
459 static macros_t
*extrastdmac
;
460 static bool any_extrastdmac
;
463 * Tokens are allocated in blocks to improve speed
465 #define TOKEN_BLOCKSIZE 4096
466 static Token
*freeTokens
;
472 static Blocks blocks
;
475 * Forward declarations.
477 static Token
*expand_mmac_params(Token
* tline
);
478 static Token
*expand_smacro(Token
* tline
);
479 static Token
*expand_id(Token
* tline
);
480 static Context
*get_ctx(const char *name
, const char **namep
);
481 static void make_tok_num(Token
* tok
, int64_t val
);
482 static void error(int severity
, const char *fmt
, ...);
483 static void error_precond(int severity
, const char *fmt
, ...);
484 static void *new_Block(size_t size
);
485 static void delete_Blocks(void);
486 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
487 const char *text
, int txtlen
);
488 static Token
*copy_Token(Token
* tline
);
489 static Token
*delete_Token(Token
* t
);
490 static Line
*new_Line(void);
491 static ExpDef
*new_ExpDef(int exp_type
);
492 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
);
495 * Macros for safe checking of token pointers, avoid *(NULL)
497 #define tok_type_(x,t) ((x) && (x)->type == (t))
498 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
499 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
500 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
503 * A few helpers for single macros
506 /* We might be not smacro parameter at all */
507 static bool is_smacro_param(Token
*t
)
509 return t
->type
>= TOK_SMAC_PARAM
;
512 /* smacro parameters are counted in a special way */
513 static int smacro_get_param_idx(Token
*t
)
515 return t
->type
- TOK_SMAC_PARAM
;
518 /* encode smacro parameter index */
519 static int smacro_set_param_idx(Token
*t
, unsigned int index
)
521 return t
->type
= TOK_SMAC_PARAM
+ index
;
526 #define stringify(x) #x
528 #define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
529 #define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
530 #define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
532 /* FIXME: we really need some compound type here instead of inplace code */
533 static const char *nasm_get_tok_type_str(enum pp_token_type type
)
535 #define SWITCH_TOK_NAME(type) \
537 return stringify(type)
540 SWITCH_TOK_NAME(TOK_NONE
);
541 SWITCH_TOK_NAME(TOK_WHITESPACE
);
542 SWITCH_TOK_NAME(TOK_COMMENT
);
543 SWITCH_TOK_NAME(TOK_ID
);
544 SWITCH_TOK_NAME(TOK_PREPROC_ID
);
545 SWITCH_TOK_NAME(TOK_STRING
);
546 SWITCH_TOK_NAME(TOK_NUMBER
);
547 SWITCH_TOK_NAME(TOK_FLOAT
);
548 SWITCH_TOK_NAME(TOK_SMAC_END
);
549 SWITCH_TOK_NAME(TOK_OTHER
);
550 SWITCH_TOK_NAME(TOK_INTERNAL_STRING
);
551 SWITCH_TOK_NAME(TOK_PREPROC_Q
);
552 SWITCH_TOK_NAME(TOK_PREPROC_QQ
);
553 SWITCH_TOK_NAME(TOK_PASTE
);
554 SWITCH_TOK_NAME(TOK_INDIRECT
);
555 SWITCH_TOK_NAME(TOK_SMAC_PARAM
);
556 SWITCH_TOK_NAME(TOK_MAX
);
562 static void nasm_raw_dump_token(Token
*token
, const char *file
, int line
, const char *func
)
564 printf("---[%s (%s:%d): %p]---\n", func
, file
, line
, (void *)token
);
567 list_for_each(t
, token
) {
569 printf("'%s'(%s) ", t
->text
,
570 nasm_get_tok_type_str(t
->type
));
576 static void nasm_raw_dump_stream(Token
*token
, const char *file
, int line
, const char *func
)
578 printf("---[%s (%s:%d): %p]---\n", func
, file
, line
, (void *)token
);
581 list_for_each(t
, token
)
582 printf("%s", t
->text
? t
->text
: " ");
588 #define nasm_trace(msg, ...)
589 #define nasm_dump_token(t)
590 #define nasm_dump_stream(t)
594 * nasm_unquote with error if the string contains NUL characters.
595 * If the string contains NUL characters, issue an error and return
596 * the C len, i.e. truncate at the NUL.
598 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
600 size_t len
= nasm_unquote(qstr
, NULL
);
601 size_t clen
= strlen(qstr
);
604 error(ERR_NONFATAL
, "NUL character in `%s' directive",
605 pp_directives
[directive
]);
611 * In-place reverse a list of tokens.
613 static Token
*reverse_tokens(Token
*t
)
617 list_reverse(t
, prev
, next
);
623 * Handle TASM specific directives, which do not contain a % in
624 * front of them. We do it here because I could not find any other
625 * place to do it for the moment, and it is a hack (ideally it would
626 * be nice to be able to use the NASM pre-processor to do it).
628 static char *check_tasm_directive(char *line
)
630 int32_t i
, j
, k
, m
, len
;
631 char *p
, *q
, *oldline
, oldchar
;
633 p
= nasm_skip_spaces(line
);
635 /* Binary search for the directive name */
637 j
= ARRAY_SIZE(tasm_directives
);
638 q
= nasm_skip_word(p
);
645 m
= nasm_stricmp(p
, tasm_directives
[k
]);
647 /* We have found a directive, so jam a % in front of it
648 * so that NASM will then recognise it as one if it's own.
653 line
= nasm_malloc(len
+ 2);
655 if (k
== TM_IFDIFI
) {
657 * NASM does not recognise IFDIFI, so we convert
658 * it to %if 0. This is not used in NASM
659 * compatible code, but does need to parse for the
660 * TASM macro package.
662 strcpy(line
+ 1, "if 0");
664 memcpy(line
+ 1, p
, len
+ 1);
679 * The pre-preprocessing stage... This function translates line
680 * number indications as they emerge from GNU cpp (`# lineno "file"
681 * flags') into NASM preprocessor line number indications (`%line
684 static char *prepreproc(char *line
)
687 char *fname
, *oldline
;
689 if (line
[0] == '#' && line
[1] == ' ') {
692 lineno
= atoi(fname
);
693 fname
+= strspn(fname
, "0123456789 ");
696 fnlen
= strcspn(fname
, "\"");
697 line
= nasm_malloc(20 + fnlen
);
698 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
701 if (tasm_compatible_mode
)
702 return check_tasm_directive(line
);
707 * Free a linked list of tokens.
709 static void free_tlist(Token
* list
)
712 list
= delete_Token(list
);
716 * Free a linked list of lines.
718 static void free_llist(Line
* list
)
721 list_for_each_safe(l
, tmp
, list
) {
722 free_tlist(l
->first
);
730 static void free_expdef(ExpDef
* ed
)
733 free_tlist(ed
->dlist
);
734 nasm_free(ed
->defaults
);
735 free_llist(ed
->line
);
742 static void free_expinv(ExpInv
* ei
)
745 nasm_free(ei
->label_text
);
750 * Free all currently defined macros, and free the hash tables
752 static void free_smacro_table(struct hash_table
*smt
)
756 struct hash_tbl_node
*it
= NULL
;
758 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
759 nasm_free((void *)key
);
760 list_for_each_safe(s
, tmp
, s
) {
762 free_tlist(s
->expansion
);
769 static void free_expdef_table(struct hash_table
*edt
)
773 struct hash_tbl_node
*it
= NULL
;
776 while ((ed
= hash_iterate(edt
, &it
, &key
)) != NULL
) {
777 nasm_free((void *)key
);
778 list_for_each_safe(ed
,tmp
, ed
)
784 static void free_macros(void)
786 free_smacro_table(&smacros
);
787 free_expdef_table(&expdefs
);
791 * Initialize the hash tables
793 static void init_macros(void)
795 hash_init(&smacros
, HASH_LARGE
);
796 hash_init(&expdefs
, HASH_LARGE
);
800 * Pop the context stack.
802 static void ctx_pop(void)
807 free_smacro_table(&c
->localmac
);
813 * Search for a key in the hash index; adding it if necessary
814 * (in which case we initialize the data pointer to NULL.)
817 hash_findi_add(struct hash_table
*hash
, const char *str
)
819 struct hash_insert hi
;
823 r
= hash_findi(hash
, str
, &hi
);
827 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
828 return hash_add(&hi
, strx
, NULL
);
832 * Like hash_findi, but returns the data element rather than a pointer
833 * to it. Used only when not adding a new element, hence no third
837 hash_findix(struct hash_table
*hash
, const char *str
)
841 p
= hash_findi(hash
, str
, NULL
);
842 return p
? *p
: NULL
;
846 * read line from standard macros set,
847 * if there no more left -- return NULL
849 static char *line_from_stdmac(void)
852 const unsigned char *p
= stdmacpos
;
861 len
+= pp_directives_len
[c
- 0x80] + 1;
866 line
= nasm_malloc(len
+ 1);
868 while ((c
= *stdmacpos
++)) {
870 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
871 q
+= pp_directives_len
[c
- 0x80];
881 /* This was the last of the standard macro chain... */
883 if (any_extrastdmac
) {
884 stdmacpos
= extrastdmac
;
885 any_extrastdmac
= false;
886 } else if (do_predef
) {
889 Token
*head
, **tail
, *t
;
892 * Nasty hack: here we push the contents of
893 * `predef' on to the top-level expansion stack,
894 * since this is the most convenient way to
895 * implement the pre-include and pre-define
898 list_for_each(pd
, predef
) {
901 list_for_each(t
, pd
->first
) {
902 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
903 tail
= &(*tail
)->next
;
908 ei
= new_ExpInv(EXP_PREDEF
, NULL
);
911 ei
->prev
= istk
->expansion
;
912 istk
->expansion
= ei
;
921 #define BUF_DELTA 512
923 * Read a line from the top file in istk, handling multiple CR/LFs
924 * at the end of the line read, and handling spurious ^Zs. Will
925 * return lines from the standard macro set if this has not already
928 static char *read_line(void)
930 char *buffer
, *p
, *q
;
931 int bufsize
, continued_count
;
934 * standart macros set (predefined) goes first
936 p
= line_from_stdmac();
941 * regular read from a file
944 buffer
= nasm_malloc(BUF_DELTA
);
948 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
952 if (p
> buffer
&& p
[-1] == '\n') {
954 * Convert backslash-CRLF line continuation sequences into
955 * nothing at all (for DOS and Windows)
957 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
963 * Also convert backslash-LF line continuation sequences into
964 * nothing at all (for Unix)
966 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
974 if (p
- buffer
> bufsize
- 10) {
975 int32_t offset
= p
- buffer
;
976 bufsize
+= BUF_DELTA
;
977 buffer
= nasm_realloc(buffer
, bufsize
);
978 p
= buffer
+ offset
; /* prevent stale-pointer problems */
982 if (!q
&& p
== buffer
) {
987 src_set_linnum(src_get_linnum() + istk
->lineinc
+
988 (continued_count
* istk
->lineinc
));
991 * Play safe: remove CRs as well as LFs, if any of either are
992 * present at the end of the line.
994 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
998 * Handle spurious ^Z, which may be inserted into source files
999 * by some file transfer utilities.
1001 buffer
[strcspn(buffer
, "\032")] = '\0';
1003 list
->line(LIST_READ
, buffer
);
1009 * Tokenize a line of text. This is a very simple process since we
1010 * don't need to parse the value out of e.g. numeric tokens: we
1011 * simply split one string into many.
1013 static Token
*tokenize(char *line
)
1016 enum pp_token_type type
;
1018 Token
*t
, **tail
= &list
;
1019 bool verbose
= true;
1021 nasm_trace("Tokenize for '%s'", line
);
1023 if ((defining
!= NULL
) && (defining
->ignoring
== true)) {
1031 if (*p
== '+' && !nasm_isdigit(p
[1])) {
1034 } else if (nasm_isdigit(*p
) ||
1035 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
1039 while (nasm_isdigit(*p
));
1040 type
= TOK_PREPROC_ID
;
1041 } else if (*p
== '{') {
1043 while (*p
&& *p
!= '}') {
1050 type
= TOK_PREPROC_ID
;
1051 } else if (*p
== '[') {
1053 line
+= 2; /* Skip the leading %[ */
1055 while (lvl
&& (c
= *p
++)) {
1067 p
= nasm_skip_string(p
- 1) + 1;
1077 error(ERR_NONFATAL
, "unterminated %[ construct");
1078 type
= TOK_INDIRECT
;
1079 } else if (*p
== '?') {
1080 type
= TOK_PREPROC_Q
; /* %? */
1083 type
= TOK_PREPROC_QQ
; /* %?? */
1086 } else if (*p
== '!') {
1087 type
= TOK_PREPROC_ID
;
1092 } while (isidchar(*p
));
1093 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
1094 p
= nasm_skip_string(p
);
1098 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
1100 /* %! without string or identifier */
1101 type
= TOK_OTHER
; /* Legacy behavior... */
1103 } else if (isidchar(*p
) ||
1104 ((*p
== '!' || *p
== '%' || *p
== '$') &&
1109 while (isidchar(*p
));
1110 type
= TOK_PREPROC_ID
;
1116 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
1119 while (*p
&& isidchar(*p
))
1121 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1126 p
= nasm_skip_string(p
);
1130 } else if(verbose
) {
1131 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1132 /* Handling unterminated strings by UNV */
1135 } else if (p
[0] == '$' && p
[1] == '$') {
1136 type
= TOK_OTHER
; /* TOKEN_BASE */
1138 } else if (isnumstart(*p
)) {
1139 bool is_hex
= false;
1140 bool is_float
= false;
1156 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1158 if (*p
== '+' || *p
== '-') {
1160 * e can only be followed by +/- if it is either a
1161 * prefixed hex number or a floating-point number
1166 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1168 } else if (c
== 'P' || c
== 'p') {
1170 if (*p
== '+' || *p
== '-')
1172 } else if (isnumchar(c
) || c
== '_')
1173 ; /* just advance */
1174 else if (c
== '.') {
1176 * we need to deal with consequences of the legacy
1177 * parser, like "1.nolist" being two tokens
1178 * (TOK_NUMBER, TOK_ID) here; at least give it
1179 * a shot for now. In the future, we probably need
1180 * a flex-based scanner with proper pattern matching
1181 * to do it as well as it can be done. Nothing in
1182 * the world is going to help the person who wants
1183 * 0x123.p16 interpreted as two tokens, though.
1189 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1190 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1191 (*r
== 'p' || *r
== 'P')) {
1195 break; /* Terminate the token */
1199 p
--; /* Point to first character beyond number */
1201 if (p
== line
+1 && *line
== '$') {
1202 type
= TOK_OTHER
; /* TOKEN_HERE */
1204 if (has_e
&& !is_hex
) {
1205 /* 1e13 is floating-point, but 1e13h is not */
1209 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1211 } else if (nasm_isspace(*p
)) {
1212 type
= TOK_WHITESPACE
;
1213 p
= nasm_skip_spaces(p
);
1215 * Whitespace just before end-of-line is discarded by
1216 * pretending it's a comment; whitespace just before a
1217 * comment gets lumped into the comment.
1219 if (!*p
|| *p
== ';') {
1224 } else if (*p
== ';') {
1230 * Anything else is an operator of some kind. We check
1231 * for all the double-character operators (>>, <<, //,
1232 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1233 * else is a single-character operator.
1236 if ((p
[0] == '>' && p
[1] == '>') ||
1237 (p
[0] == '<' && p
[1] == '<') ||
1238 (p
[0] == '/' && p
[1] == '/') ||
1239 (p
[0] == '<' && p
[1] == '=') ||
1240 (p
[0] == '>' && p
[1] == '=') ||
1241 (p
[0] == '=' && p
[1] == '=') ||
1242 (p
[0] == '!' && p
[1] == '=') ||
1243 (p
[0] == '<' && p
[1] == '>') ||
1244 (p
[0] == '&' && p
[1] == '&') ||
1245 (p
[0] == '|' && p
[1] == '|') ||
1246 (p
[0] == '^' && p
[1] == '^')) {
1252 /* Handling unterminated string by UNV */
1255 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1256 t->text[p-line] = *line;
1260 if (type
!= TOK_COMMENT
) {
1261 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1267 nasm_dump_token(list
);
1273 * this function allocates a new managed block of memory and
1274 * returns a pointer to the block. The managed blocks are
1275 * deleted only all at once by the delete_Blocks function.
1277 static void *new_Block(size_t size
)
1279 Blocks
*b
= &blocks
;
1281 /* first, get to the end of the linked list */
1285 /* now allocate the requested chunk */
1286 b
->chunk
= nasm_malloc(size
);
1288 /* now allocate a new block for the next request */
1289 b
->next
= nasm_zalloc(sizeof(Blocks
));
1295 * this function deletes all managed blocks of memory
1297 static void delete_Blocks(void)
1299 Blocks
*a
, *b
= &blocks
;
1302 * keep in mind that the first block, pointed to by blocks
1303 * is a static and not dynamically allocated, so we don't
1307 nasm_free(b
->chunk
);
1316 * this function creates a new Token and passes a pointer to it
1317 * back to the caller. It sets the type and text elements, and
1318 * also the a.mac and next elements to NULL.
1320 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1321 const char *text
, int txtlen
)
1327 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1328 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1329 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1330 freeTokens
[i
].next
= NULL
;
1333 freeTokens
= t
->next
;
1337 if (type
== TOK_WHITESPACE
|| !text
) {
1341 txtlen
= strlen(text
);
1342 t
->text
= nasm_malloc(txtlen
+1);
1343 memcpy(t
->text
, text
, txtlen
);
1344 t
->text
[txtlen
] = '\0';
1349 static Token
*copy_Token(Token
* tline
)
1351 Token
*t
, *tt
, *first
= NULL
, *prev
= NULL
;
1353 for (tt
= tline
; tt
!= NULL
; tt
= tt
->next
) {
1355 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1356 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1357 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1358 freeTokens
[i
].next
= NULL
;
1361 freeTokens
= t
->next
;
1363 t
->text
= tt
->text
? nasm_strdup(tt
->text
) : NULL
;
1364 t
->a
.mac
= tt
->a
.mac
;
1365 t
->a
.len
= tt
->a
.len
;
1377 static Token
*delete_Token(Token
* t
)
1379 Token
*next
= t
->next
;
1381 t
->next
= freeTokens
;
1387 * Convert a line of tokens back into text.
1388 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1389 * will be transformed into ..@ctxnum.xxx
1391 static char *detoken(Token
* tlist
, bool expand_locals
)
1398 list_for_each(t
, tlist
) {
1399 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1404 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1405 size_t len
= nasm_unquote(v
, NULL
);
1406 size_t clen
= strlen(v
);
1409 error(ERR_NONFATAL
| ERR_PASS1
,
1410 "NUL character in %! string");
1416 char *p
= getenv(v
);
1418 error(ERR_NONFATAL
| ERR_PASS1
,
1419 "nonexistent environment variable `%s'", v
);
1422 t
->text
= nasm_strdup(p
);
1427 /* Expand local macros here and not during preprocessing */
1428 if (expand_locals
&&
1429 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1430 t
->text
[0] == '%' && t
->text
[1] == '$') {
1433 Context
*ctx
= get_ctx(t
->text
, &q
);
1436 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1437 p
= nasm_strcat(buffer
, q
);
1443 /* Expand %? and %?? directives */
1444 if ((istk
->expansion
!= NULL
) &&
1445 ((t
->type
== TOK_PREPROC_Q
) ||
1446 (t
->type
== TOK_PREPROC_QQ
))) {
1448 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
){
1449 if (ei
->type
== EXP_MMACRO
) {
1451 if (t
->type
== TOK_PREPROC_Q
) {
1452 t
->text
= nasm_strdup(ei
->name
);
1454 t
->text
= nasm_strdup(ei
->def
->name
);
1461 if (t
->type
== TOK_WHITESPACE
)
1464 len
+= strlen(t
->text
);
1467 p
= line
= nasm_malloc(len
+ 1);
1469 list_for_each(t
, tlist
) {
1470 if (t
->type
== TOK_WHITESPACE
) {
1472 } else if (t
->text
) {
1484 * Initialize a new Line
1486 static inline Line
*new_Line(void)
1488 return (Line
*)nasm_zalloc(sizeof(Line
));
1493 * Initialize a new Expansion Definition
1495 static ExpDef
*new_ExpDef(int exp_type
)
1497 ExpDef
*ed
= (ExpDef
*)nasm_zalloc(sizeof(ExpDef
));
1498 ed
->type
= exp_type
;
1499 ed
->casesense
= true;
1500 ed
->state
= COND_NEVER
;
1507 * Initialize a new Expansion Instance
1509 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
)
1511 ExpInv
*ei
= (ExpInv
*)nasm_zalloc(sizeof(ExpInv
));
1512 ei
->type
= exp_type
;
1514 ei
->unique
= ++unique
;
1516 if ((istk
->mmac_depth
< 1) &&
1517 (istk
->expansion
== NULL
) &&
1519 (ed
->type
!= EXP_MMACRO
) &&
1520 (ed
->type
!= EXP_REP
) &&
1521 (ed
->type
!= EXP_WHILE
)) {
1522 ei
->linnum
= src_get_linnum();
1523 src_set_linnum(ei
->linnum
- ed
->linecount
- 1);
1527 if ((istk
->expansion
== NULL
) ||
1528 (ei
->type
== EXP_MMACRO
)) {
1531 ei
->relno
= istk
->expansion
->lineno
;
1533 ei
->relno
-= (ed
->linecount
+ 1);
1540 * A scanner, suitable for use by the expression evaluator, which
1541 * operates on a line of Tokens. Expects a pointer to a pointer to
1542 * the first token in the line to be passed in as its private_data
1545 * FIX: This really needs to be unified with stdscan.
1547 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1549 Token
**tlineptr
= private_data
;
1551 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1555 *tlineptr
= tline
? tline
->next
: NULL
;
1556 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1557 tline
->type
== TOK_COMMENT
));
1560 return tokval
->t_type
= TOKEN_EOS
;
1562 tokval
->t_charptr
= tline
->text
;
1564 if (tline
->text
[0] == '$' && !tline
->text
[1])
1565 return tokval
->t_type
= TOKEN_HERE
;
1566 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1567 return tokval
->t_type
= TOKEN_BASE
;
1569 if (tline
->type
== TOK_ID
) {
1570 p
= tokval
->t_charptr
= tline
->text
;
1572 tokval
->t_charptr
++;
1573 return tokval
->t_type
= TOKEN_ID
;
1576 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1577 if (r
>= p
+MAX_KEYWORD
)
1578 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1579 *s
++ = nasm_tolower(*r
);
1582 /* right, so we have an identifier sitting in temp storage. now,
1583 * is it actually a register or instruction name, or what? */
1584 return nasm_token_hash(ourcopy
, tokval
);
1587 if (tline
->type
== TOK_NUMBER
) {
1589 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1590 tokval
->t_charptr
= tline
->text
;
1592 return tokval
->t_type
= TOKEN_ERRNUM
;
1594 return tokval
->t_type
= TOKEN_NUM
;
1597 if (tline
->type
== TOK_FLOAT
) {
1598 return tokval
->t_type
= TOKEN_FLOAT
;
1601 if (tline
->type
== TOK_STRING
) {
1604 bq
= tline
->text
[0];
1605 tokval
->t_charptr
= tline
->text
;
1606 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1608 if (ep
[0] != bq
|| ep
[1] != '\0')
1609 return tokval
->t_type
= TOKEN_ERRSTR
;
1611 return tokval
->t_type
= TOKEN_STR
;
1614 if (tline
->type
== TOK_OTHER
) {
1615 if (!strcmp(tline
->text
, "<<"))
1616 return tokval
->t_type
= TOKEN_SHL
;
1617 if (!strcmp(tline
->text
, ">>"))
1618 return tokval
->t_type
= TOKEN_SHR
;
1619 if (!strcmp(tline
->text
, "//"))
1620 return tokval
->t_type
= TOKEN_SDIV
;
1621 if (!strcmp(tline
->text
, "%%"))
1622 return tokval
->t_type
= TOKEN_SMOD
;
1623 if (!strcmp(tline
->text
, "=="))
1624 return tokval
->t_type
= TOKEN_EQ
;
1625 if (!strcmp(tline
->text
, "<>"))
1626 return tokval
->t_type
= TOKEN_NE
;
1627 if (!strcmp(tline
->text
, "!="))
1628 return tokval
->t_type
= TOKEN_NE
;
1629 if (!strcmp(tline
->text
, "<="))
1630 return tokval
->t_type
= TOKEN_LE
;
1631 if (!strcmp(tline
->text
, ">="))
1632 return tokval
->t_type
= TOKEN_GE
;
1633 if (!strcmp(tline
->text
, "&&"))
1634 return tokval
->t_type
= TOKEN_DBL_AND
;
1635 if (!strcmp(tline
->text
, "^^"))
1636 return tokval
->t_type
= TOKEN_DBL_XOR
;
1637 if (!strcmp(tline
->text
, "||"))
1638 return tokval
->t_type
= TOKEN_DBL_OR
;
1642 * We have no other options: just return the first character of
1645 return tokval
->t_type
= tline
->text
[0];
1649 * Compare a string to the name of an existing macro; this is a
1650 * simple wrapper which calls either strcmp or nasm_stricmp
1651 * depending on the value of the `casesense' parameter.
1653 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1655 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1659 * Compare a string to the name of an existing macro; this is a
1660 * simple wrapper which calls either strcmp or nasm_stricmp
1661 * depending on the value of the `casesense' parameter.
1663 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1665 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1669 * Return the Context structure associated with a %$ token. Return
1670 * NULL, having _already_ reported an error condition, if the
1671 * context stack isn't deep enough for the supplied number of $
1674 * If "namep" is non-NULL, set it to the pointer to the macro name
1675 * tail, i.e. the part beyond %$...
1677 static Context
*get_ctx(const char *name
, const char **namep
)
1685 if (!name
|| name
[0] != '%' || name
[1] != '$')
1689 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1696 while (ctx
&& *name
== '$') {
1703 error(ERR_NONFATAL
, "`%s': context stack is only"
1704 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1715 * Check to see if a file is already in a string list
1717 static bool in_list(const StrList
*list
, const char *str
)
1720 if (!strcmp(list
->str
, str
))
1728 * Open an include file. This routine must always return a valid
1729 * file pointer if it returns - it's responsible for throwing an
1730 * ERR_FATAL and bombing out completely if not. It should also try
1731 * the include path one by one until it finds the file or reaches
1732 * the end of the path.
1734 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1739 IncPath
*ip
= ipath
;
1740 int len
= strlen(file
);
1741 size_t prefix_len
= 0;
1745 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1747 memcpy(sl
->str
, prefix
, prefix_len
);
1748 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1749 fp
= fopen(sl
->str
, "r");
1750 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1767 prefix_len
= strlen(prefix
);
1769 /* -MG given and file not found */
1770 if (dhead
&& !in_list(*dhead
, file
)) {
1771 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1773 strcpy(sl
->str
, file
);
1781 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1786 * Determine if we should warn on defining a single-line macro of
1787 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1788 * return true if _any_ single-line macro of that name is defined.
1789 * Otherwise, will return true if a single-line macro with either
1790 * `nparam' or no parameters is defined.
1792 * If a macro with precisely the right number of parameters is
1793 * defined, or nparam is -1, the address of the definition structure
1794 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1795 * is NULL, no action will be taken regarding its contents, and no
1798 * Note that this is also called with nparam zero to resolve
1801 * If you already know which context macro belongs to, you can pass
1802 * the context pointer as first parameter; if you won't but name begins
1803 * with %$ the context will be automatically computed. If all_contexts
1804 * is true, macro will be searched in outer contexts as well.
1807 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1810 struct hash_table
*smtbl
;
1814 smtbl
= &ctx
->localmac
;
1815 } else if (name
[0] == '%' && name
[1] == '$') {
1817 ctx
= get_ctx(name
, &name
);
1819 return false; /* got to return _something_ */
1820 smtbl
= &ctx
->localmac
;
1824 m
= (SMacro
*) hash_findix(smtbl
, name
);
1827 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1828 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1830 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1844 * Count and mark off the parameters in a multi-line macro call.
1845 * This is called both from within the multi-line macro expansion
1846 * code, and also to mark off the default parameters when provided
1847 * in a %macro definition line.
1849 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1851 int paramsize
, brace
;
1853 *nparam
= paramsize
= 0;
1856 /* +1: we need space for the final NULL */
1857 if (*nparam
+1 >= paramsize
) {
1858 paramsize
+= PARAM_DELTA
;
1859 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1863 if (tok_is_(t
, "{"))
1865 (*params
)[(*nparam
)++] = t
;
1866 while (tok_isnt_(t
, brace
? "}" : ","))
1868 if (t
) { /* got a comma/brace */
1872 * Now we've found the closing brace, look further
1876 if (tok_isnt_(t
, ",")) {
1878 "braces do not enclose all of macro parameter");
1879 while (tok_isnt_(t
, ","))
1883 t
= t
->next
; /* eat the comma */
1890 * Determine whether one of the various `if' conditions is true or
1893 * We must free the tline we get passed.
1895 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1897 enum pp_conditional i
= PP_COND(ct
);
1899 Token
*t
, *tt
, **tptr
, *origline
;
1900 struct tokenval tokval
;
1902 enum pp_token_type needtype
;
1909 j
= false; /* have we matched yet? */
1914 if (tline
->type
!= TOK_ID
) {
1916 "`%s' expects context identifiers", pp_directives
[ct
]);
1917 free_tlist(origline
);
1920 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1922 tline
= tline
->next
;
1927 j
= false; /* have we matched yet? */
1930 if (!tline
|| (tline
->type
!= TOK_ID
&&
1931 (tline
->type
!= TOK_PREPROC_ID
||
1932 tline
->text
[1] != '$'))) {
1934 "`%s' expects macro identifiers", pp_directives
[ct
]);
1937 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1939 tline
= tline
->next
;
1945 tline
= expand_smacro(tline
);
1946 j
= false; /* have we matched yet? */
1949 if (!tline
|| (tline
->type
!= TOK_ID
&&
1950 tline
->type
!= TOK_STRING
&&
1951 (tline
->type
!= TOK_PREPROC_ID
||
1952 tline
->text
[1] != '!'))) {
1954 "`%s' expects environment variable names",
1959 if (tline
->type
== TOK_PREPROC_ID
)
1960 p
+= 2; /* Skip leading %! */
1961 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1962 nasm_unquote_cstr(p
, ct
);
1965 tline
= tline
->next
;
1972 tline
= expand_smacro(tline
);
1974 while (tok_isnt_(tt
, ","))
1978 "`%s' expects two comma-separated arguments",
1983 j
= true; /* assume equality unless proved not */
1984 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1985 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1986 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1990 if (t
->type
== TOK_WHITESPACE
) {
1994 if (tt
->type
== TOK_WHITESPACE
) {
1998 if (tt
->type
!= t
->type
) {
1999 j
= false; /* found mismatching tokens */
2002 /* When comparing strings, need to unquote them first */
2003 if (t
->type
== TOK_STRING
) {
2004 size_t l1
= nasm_unquote(t
->text
, NULL
);
2005 size_t l2
= nasm_unquote(tt
->text
, NULL
);
2011 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
2015 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
2016 j
= false; /* found mismatching tokens */
2023 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
2024 j
= false; /* trailing gunk on one end or other */
2030 ExpDef searching
, *ed
;
2033 tline
= expand_id(tline
);
2034 if (!tok_type_(tline
, TOK_ID
)) {
2036 "`%s' expects a macro name", pp_directives
[ct
]);
2039 memset(&searching
, 0, sizeof(searching
));
2040 searching
.name
= nasm_strdup(tline
->text
);
2041 searching
.casesense
= true;
2042 searching
.nparam_max
= INT_MAX
;
2043 tline
= expand_smacro(tline
->next
);
2046 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2048 "`%s' expects a parameter count or nothing",
2051 searching
.nparam_min
= searching
.nparam_max
=
2052 readnum(tline
->text
, &j
);
2055 "unable to parse parameter count `%s'",
2058 if (tline
&& tok_is_(tline
->next
, "-")) {
2059 tline
= tline
->next
->next
;
2060 if (tok_is_(tline
, "*"))
2061 searching
.nparam_max
= INT_MAX
;
2062 else if (!tok_type_(tline
, TOK_NUMBER
))
2064 "`%s' expects a parameter count after `-'",
2067 searching
.nparam_max
= readnum(tline
->text
, &j
);
2070 "unable to parse parameter count `%s'",
2072 if (searching
.nparam_min
> searching
.nparam_max
)
2074 "minimum parameter count exceeds maximum");
2077 if (tline
&& tok_is_(tline
->next
, "+")) {
2078 tline
= tline
->next
;
2079 searching
.plus
= true;
2081 ed
= (ExpDef
*) hash_findix(&expdefs
, searching
.name
);
2082 while (ed
!= NULL
) {
2083 if (!strcmp(ed
->name
, searching
.name
) &&
2084 (ed
->nparam_min
<= searching
.nparam_max
|| searching
.plus
) &&
2085 (searching
.nparam_min
<= ed
->nparam_max
|| ed
->plus
)) {
2091 if (tline
&& tline
->next
)
2092 error(ERR_WARNING
|ERR_PASS1
,
2093 "trailing garbage after %%ifmacro ignored");
2094 nasm_free(searching
.name
);
2103 needtype
= TOK_NUMBER
;
2106 needtype
= TOK_STRING
;
2110 t
= tline
= expand_smacro(tline
);
2112 while (tok_type_(t
, TOK_WHITESPACE
) ||
2113 (needtype
== TOK_NUMBER
&&
2114 tok_type_(t
, TOK_OTHER
) &&
2115 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
2119 j
= tok_type_(t
, needtype
);
2123 t
= tline
= expand_smacro(tline
);
2124 while (tok_type_(t
, TOK_WHITESPACE
))
2129 t
= t
->next
; /* Skip the actual token */
2130 while (tok_type_(t
, TOK_WHITESPACE
))
2132 j
= !t
; /* Should be nothing left */
2137 t
= tline
= expand_smacro(tline
);
2138 while (tok_type_(t
, TOK_WHITESPACE
))
2141 j
= !t
; /* Should be empty */
2145 t
= tline
= expand_smacro(tline
);
2147 tokval
.t_type
= TOKEN_INVALID
;
2148 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2149 NULL
, pass
| CRITICAL
, error
, NULL
);
2153 error(ERR_WARNING
|ERR_PASS1
,
2154 "trailing garbage after expression ignored");
2155 if (!is_simple(evalresult
)) {
2157 "non-constant value given to `%s'", pp_directives
[ct
]);
2160 j
= reloc_value(evalresult
) != 0;
2165 "preprocessor directive `%s' not yet implemented",
2170 free_tlist(origline
);
2171 return j
^ PP_NEGATIVE(ct
);
2174 free_tlist(origline
);
2179 * Common code for defining an smacro
2181 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2182 int nparam
, Token
*expansion
)
2184 SMacro
*smac
, **smhead
;
2185 struct hash_table
*smtbl
;
2187 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2189 error(ERR_WARNING
|ERR_PASS1
,
2190 "single-line macro `%s' defined both with and"
2191 " without parameters", mname
);
2193 * Some instances of the old code considered this a failure,
2194 * some others didn't. What is the right thing to do here?
2196 free_tlist(expansion
);
2197 return false; /* Failure */
2200 * We're redefining, so we have to take over an
2201 * existing SMacro structure. This means freeing
2202 * what was already in it.
2204 nasm_free(smac
->name
);
2205 free_tlist(smac
->expansion
);
2208 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2209 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2210 smac
= nasm_zalloc(sizeof(SMacro
));
2211 smac
->next
= *smhead
;
2214 smac
->name
= nasm_strdup(mname
);
2215 smac
->casesense
= casesense
;
2216 smac
->nparam
= nparam
;
2217 smac
->expansion
= expansion
;
2218 smac
->in_progress
= false;
2219 return true; /* Success */
2223 * Undefine an smacro
2225 static void undef_smacro(Context
*ctx
, const char *mname
)
2227 SMacro
**smhead
, *s
, **sp
;
2228 struct hash_table
*smtbl
;
2230 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2231 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2235 * We now have a macro name... go hunt for it.
2238 while ((s
= *sp
) != NULL
) {
2239 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2242 free_tlist(s
->expansion
);
2252 * Parse a mmacro specification.
2254 static bool parse_mmacro_spec(Token
*tline
, ExpDef
*def
, const char *directive
)
2258 tline
= tline
->next
;
2260 tline
= expand_id(tline
);
2261 if (!tok_type_(tline
, TOK_ID
)) {
2262 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2266 def
->name
= nasm_strdup(tline
->text
);
2268 def
->nolist
= false;
2269 // def->in_progress = 0;
2270 // def->rep_nest = NULL;
2271 def
->nparam_min
= 0;
2272 def
->nparam_max
= 0;
2274 tline
= expand_smacro(tline
->next
);
2276 if (!tok_type_(tline
, TOK_NUMBER
)) {
2277 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2279 def
->nparam_min
= def
->nparam_max
=
2280 readnum(tline
->text
, &err
);
2283 "unable to parse parameter count `%s'", tline
->text
);
2285 if (tline
&& tok_is_(tline
->next
, "-")) {
2286 tline
= tline
->next
->next
;
2287 if (tok_is_(tline
, "*")) {
2288 def
->nparam_max
= INT_MAX
;
2289 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2291 "`%s' expects a parameter count after `-'", directive
);
2293 def
->nparam_max
= readnum(tline
->text
, &err
);
2295 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2298 if (def
->nparam_min
> def
->nparam_max
) {
2299 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2303 if (tline
&& tok_is_(tline
->next
, "+")) {
2304 tline
= tline
->next
;
2307 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2308 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2309 tline
= tline
->next
;
2314 * Handle default parameters.
2316 if (tline
&& tline
->next
) {
2317 def
->dlist
= tline
->next
;
2319 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2322 def
->defaults
= NULL
;
2326 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2328 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2329 "too many default macro parameters");
2336 * Decode a size directive
2338 static int parse_size(const char *str
) {
2339 static const char *size_names
[] =
2340 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2341 static const int sizes
[] =
2342 { 0, 1, 4, 16, 8, 10, 2, 32 };
2344 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2348 * find and process preprocessor directive in passed line
2349 * Find out if a line contains a preprocessor directive, and deal
2352 * If a directive _is_ found, it is the responsibility of this routine
2353 * (and not the caller) to free_tlist() the line.
2355 * @param tline a pointer to the current tokeninzed line linked list
2356 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2359 static int do_directive(Token
* tline
)
2361 enum preproc_token i
;
2374 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2375 struct tokenval tokval
;
2377 ExpDef
*ed
, *eed
, **edhead
;
2386 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2387 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2388 || tline
->text
[1] == '!'))
2389 return NO_DIRECTIVE_FOUND
;
2391 i
= pp_token_hash(tline
->text
);
2395 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2396 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2398 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2401 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2402 /* Directive to tell NASM what the default stack size is. The
2403 * default is for a 16-bit stack, and this can be overriden with
2406 tline
= tline
->next
;
2407 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2408 tline
= tline
->next
;
2409 if (!tline
|| tline
->type
!= TOK_ID
) {
2410 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2411 free_tlist(origline
);
2412 return DIRECTIVE_FOUND
;
2414 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2415 /* All subsequent ARG directives are for a 32-bit stack */
2417 StackPointer
= "ebp";
2420 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2421 /* All subsequent ARG directives are for a 64-bit stack */
2423 StackPointer
= "rbp";
2426 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2427 /* All subsequent ARG directives are for a 16-bit stack,
2428 * far function call.
2431 StackPointer
= "bp";
2434 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2435 /* All subsequent ARG directives are for a 16-bit stack,
2436 * far function call. We don't support near functions.
2439 StackPointer
= "bp";
2443 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2444 free_tlist(origline
);
2445 return DIRECTIVE_FOUND
;
2447 free_tlist(origline
);
2448 return DIRECTIVE_FOUND
;
2451 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2452 /* TASM like ARG directive to define arguments to functions, in
2453 * the following form:
2455 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2459 char *arg
, directive
[256];
2460 int size
= StackSize
;
2462 /* Find the argument name */
2463 tline
= tline
->next
;
2464 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2465 tline
= tline
->next
;
2466 if (!tline
|| tline
->type
!= TOK_ID
) {
2467 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2468 free_tlist(origline
);
2469 return DIRECTIVE_FOUND
;
2473 /* Find the argument size type */
2474 tline
= tline
->next
;
2475 if (!tline
|| tline
->type
!= TOK_OTHER
2476 || tline
->text
[0] != ':') {
2478 "Syntax error processing `%%arg' directive");
2479 free_tlist(origline
);
2480 return DIRECTIVE_FOUND
;
2482 tline
= tline
->next
;
2483 if (!tline
|| tline
->type
!= TOK_ID
) {
2484 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2485 free_tlist(origline
);
2486 return DIRECTIVE_FOUND
;
2489 /* Allow macro expansion of type parameter */
2490 tt
= tokenize(tline
->text
);
2491 tt
= expand_smacro(tt
);
2492 size
= parse_size(tt
->text
);
2495 "Invalid size type for `%%arg' missing directive");
2497 free_tlist(origline
);
2498 return DIRECTIVE_FOUND
;
2502 /* Round up to even stack slots */
2503 size
= ALIGN(size
, StackSize
);
2505 /* Now define the macro for the argument */
2506 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2507 arg
, StackPointer
, offset
);
2508 do_directive(tokenize(directive
));
2511 /* Move to the next argument in the list */
2512 tline
= tline
->next
;
2513 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2514 tline
= tline
->next
;
2515 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2517 free_tlist(origline
);
2518 return DIRECTIVE_FOUND
;
2521 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2522 /* TASM like LOCAL directive to define local variables for a
2523 * function, in the following form:
2525 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2527 * The '= LocalSize' at the end is ignored by NASM, but is
2528 * required by TASM to define the local parameter size (and used
2529 * by the TASM macro package).
2531 offset
= LocalOffset
;
2533 char *local
, directive
[256];
2534 int size
= StackSize
;
2536 /* Find the argument name */
2537 tline
= tline
->next
;
2538 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2539 tline
= tline
->next
;
2540 if (!tline
|| tline
->type
!= TOK_ID
) {
2542 "`%%local' missing argument parameter");
2543 free_tlist(origline
);
2544 return DIRECTIVE_FOUND
;
2546 local
= tline
->text
;
2548 /* Find the argument size type */
2549 tline
= tline
->next
;
2550 if (!tline
|| tline
->type
!= TOK_OTHER
2551 || tline
->text
[0] != ':') {
2553 "Syntax error processing `%%local' directive");
2554 free_tlist(origline
);
2555 return DIRECTIVE_FOUND
;
2557 tline
= tline
->next
;
2558 if (!tline
|| tline
->type
!= TOK_ID
) {
2560 "`%%local' missing size type parameter");
2561 free_tlist(origline
);
2562 return DIRECTIVE_FOUND
;
2565 /* Allow macro expansion of type parameter */
2566 tt
= tokenize(tline
->text
);
2567 tt
= expand_smacro(tt
);
2568 size
= parse_size(tt
->text
);
2571 "Invalid size type for `%%local' missing directive");
2573 free_tlist(origline
);
2574 return DIRECTIVE_FOUND
;
2578 /* Round up to even stack slots */
2579 size
= ALIGN(size
, StackSize
);
2581 offset
+= size
; /* Negative offset, increment before */
2583 /* Now define the macro for the argument */
2584 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2585 local
, StackPointer
, offset
);
2586 do_directive(tokenize(directive
));
2588 /* Now define the assign to setup the enter_c macro correctly */
2589 snprintf(directive
, sizeof(directive
),
2590 "%%assign %%$localsize %%$localsize+%d", size
);
2591 do_directive(tokenize(directive
));
2593 /* Move to the next argument in the list */
2594 tline
= tline
->next
;
2595 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2596 tline
= tline
->next
;
2597 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2598 LocalOffset
= offset
;
2599 free_tlist(origline
);
2600 return DIRECTIVE_FOUND
;
2603 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2605 error(ERR_WARNING
|ERR_PASS1
,
2606 "trailing garbage after `%%clear' ignored");
2609 free_tlist(origline
);
2610 return DIRECTIVE_FOUND
;
2613 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2614 t
= tline
->next
= expand_smacro(tline
->next
);
2616 if (!t
|| (t
->type
!= TOK_STRING
&&
2617 t
->type
!= TOK_INTERNAL_STRING
)) {
2618 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2619 free_tlist(origline
);
2620 return DIRECTIVE_FOUND
; /* but we did _something_ */
2623 error(ERR_WARNING
|ERR_PASS1
,
2624 "trailing garbage after `%%depend' ignored");
2626 if (t
->type
!= TOK_INTERNAL_STRING
)
2627 nasm_unquote_cstr(p
, i
);
2628 if (dephead
&& !in_list(*dephead
, p
)) {
2629 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2633 deptail
= &sl
->next
;
2635 free_tlist(origline
);
2636 return DIRECTIVE_FOUND
;
2639 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2640 t
= tline
->next
= expand_smacro(tline
->next
);
2643 if (!t
|| (t
->type
!= TOK_STRING
&&
2644 t
->type
!= TOK_INTERNAL_STRING
)) {
2645 error(ERR_NONFATAL
, "`%%include' expects a file name");
2646 free_tlist(origline
);
2647 return DIRECTIVE_FOUND
; /* but we did _something_ */
2650 error(ERR_WARNING
|ERR_PASS1
,
2651 "trailing garbage after `%%include' ignored");
2653 if (t
->type
!= TOK_INTERNAL_STRING
)
2654 nasm_unquote_cstr(p
, i
);
2655 inc
= nasm_zalloc(sizeof(Include
));
2657 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2659 /* -MG given but file not found */
2662 inc
->fname
= src_set_fname(nasm_strdup(p
));
2663 inc
->lineno
= src_set_linnum(0);
2665 inc
->expansion
= NULL
;
2667 list
->uplevel(LIST_INCLUDE
);
2669 free_tlist(origline
);
2670 return DIRECTIVE_FOUND
;
2673 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2675 static macros_t
*use_pkg
;
2676 const char *pkg_macro
= NULL
;
2678 tline
= tline
->next
;
2680 tline
= expand_id(tline
);
2682 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2683 tline
->type
!= TOK_INTERNAL_STRING
&&
2684 tline
->type
!= TOK_ID
)) {
2685 error(ERR_NONFATAL
, "`%%use' expects a package name");
2686 free_tlist(origline
);
2687 return DIRECTIVE_FOUND
; /* but we did _something_ */
2690 error(ERR_WARNING
|ERR_PASS1
,
2691 "trailing garbage after `%%use' ignored");
2692 if (tline
->type
== TOK_STRING
)
2693 nasm_unquote_cstr(tline
->text
, i
);
2694 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2696 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2698 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2699 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2700 /* Not already included, go ahead and include it */
2701 stdmacpos
= use_pkg
;
2703 free_tlist(origline
);
2704 return DIRECTIVE_FOUND
;
2709 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2710 tline
= tline
->next
;
2712 tline
= expand_id(tline
);
2714 if (!tok_type_(tline
, TOK_ID
)) {
2715 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2717 free_tlist(origline
);
2718 return DIRECTIVE_FOUND
; /* but we did _something_ */
2721 error(ERR_WARNING
|ERR_PASS1
,
2722 "trailing garbage after `%s' ignored",
2724 p
= nasm_strdup(tline
->text
);
2726 p
= NULL
; /* Anonymous */
2730 ctx
= nasm_zalloc(sizeof(Context
));
2732 hash_init(&ctx
->localmac
, HASH_SMALL
);
2734 ctx
->number
= unique
++;
2739 error(ERR_NONFATAL
, "`%s': context stack is empty",
2741 } else if (i
== PP_POP
) {
2742 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2743 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2745 cstk
->name
? cstk
->name
: "anonymous", p
);
2750 nasm_free(cstk
->name
);
2756 free_tlist(origline
);
2757 return DIRECTIVE_FOUND
;
2759 severity
= ERR_FATAL
;
2762 severity
= ERR_NONFATAL
;
2765 severity
= ERR_WARNING
|ERR_WARN_USER
;
2769 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2771 /* Only error out if this is the final pass */
2772 if (pass
!= 2 && i
!= PP_FATAL
)
2773 return DIRECTIVE_FOUND
;
2775 tline
->next
= expand_smacro(tline
->next
);
2776 tline
= tline
->next
;
2778 t
= tline
? tline
->next
: NULL
;
2780 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2781 /* The line contains only a quoted string */
2783 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2784 error(severity
, "%s", p
);
2786 /* Not a quoted string, or more than a quoted string */
2787 p
= detoken(tline
, false);
2788 error(severity
, "%s", p
);
2791 free_tlist(origline
);
2792 return DIRECTIVE_FOUND
;
2796 if (defining
!= NULL
) {
2797 if (defining
->type
== EXP_IF
) {
2798 defining
->def_depth
++;
2800 return NO_DIRECTIVE_FOUND
;
2802 if ((istk
->expansion
!= NULL
) &&
2803 (istk
->expansion
->emitting
== false)) {
2806 j
= if_condition(tline
->next
, i
);
2807 tline
->next
= NULL
; /* it got freed */
2808 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
2810 ed
= new_ExpDef(EXP_IF
);
2812 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
2813 ed
->prev
= defining
;
2815 free_tlist(origline
);
2816 return DIRECTIVE_FOUND
;
2819 if (defining
!= NULL
) {
2820 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2821 return NO_DIRECTIVE_FOUND
;
2824 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2825 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2827 switch (defining
->state
) {
2829 defining
->state
= COND_DONE
;
2830 defining
->ignoring
= true;
2835 defining
->ignoring
= true;
2838 case COND_ELSE_TRUE
:
2839 case COND_ELSE_FALSE
:
2840 error_precond(ERR_WARNING
|ERR_PASS1
,
2841 "`%%elif' after `%%else' ignored");
2842 defining
->state
= COND_NEVER
;
2843 defining
->ignoring
= true;
2848 * IMPORTANT: In the case of %if, we will already have
2849 * called expand_mmac_params(); however, if we're
2850 * processing an %elif we must have been in a
2851 * non-emitting mode, which would have inhibited
2852 * the normal invocation of expand_mmac_params().
2853 * Therefore, we have to do it explicitly here.
2855 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2856 tline
->next
= NULL
; /* it got freed */
2858 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2859 defining
->ignoring
= ((defining
->state
== COND_IF_TRUE
) ? false : true);
2862 free_tlist(origline
);
2863 return DIRECTIVE_FOUND
;
2866 if (defining
!= NULL
) {
2867 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2868 return NO_DIRECTIVE_FOUND
;
2872 error_precond(ERR_WARNING
|ERR_PASS1
,
2873 "trailing garbage after `%%else' ignored");
2874 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2875 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2877 switch (defining
->state
) {
2880 defining
->state
= COND_ELSE_FALSE
;
2881 defining
->ignoring
= true;
2885 defining
->ignoring
= true;
2889 defining
->state
= COND_ELSE_TRUE
;
2890 defining
->ignoring
= false;
2893 case COND_ELSE_TRUE
:
2894 case COND_ELSE_FALSE
:
2895 error_precond(ERR_WARNING
|ERR_PASS1
,
2896 "`%%else' after `%%else' ignored.");
2897 defining
->state
= COND_NEVER
;
2898 defining
->ignoring
= true;
2901 free_tlist(origline
);
2902 return DIRECTIVE_FOUND
;
2905 if (defining
!= NULL
) {
2906 if (defining
->type
== EXP_IF
) {
2907 if (defining
->def_depth
> 0) {
2908 defining
->def_depth
--;
2909 return NO_DIRECTIVE_FOUND
;
2912 return NO_DIRECTIVE_FOUND
;
2916 error_precond(ERR_WARNING
|ERR_PASS1
,
2917 "trailing garbage after `%%endif' ignored");
2918 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2919 error(ERR_NONFATAL
, "`%%endif': no matching `%%if'");
2920 return DIRECTIVE_FOUND
;
2923 defining
= ed
->prev
;
2924 ed
->prev
= expansions
;
2926 ei
= new_ExpInv(EXP_IF
, ed
);
2927 ei
->current
= ed
->line
;
2928 ei
->emitting
= true;
2929 ei
->prev
= istk
->expansion
;
2930 istk
->expansion
= ei
;
2931 free_tlist(origline
);
2932 return DIRECTIVE_FOUND
;
2938 if (defining
!= NULL
) {
2939 if (defining
->type
== EXP_MMACRO
) {
2940 defining
->def_depth
++;
2942 return NO_DIRECTIVE_FOUND
;
2944 ed
= new_ExpDef(EXP_MMACRO
);
2946 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2947 ed
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2948 if (!parse_mmacro_spec(tline
, ed
, pp_directives
[i
])) {
2951 return DIRECTIVE_FOUND
;
2955 ed
->max_depth
= (ed
->max_depth
+ 1);
2956 ed
->ignoring
= false;
2957 ed
->prev
= defining
;
2960 eed
= (ExpDef
*) hash_findix(&expdefs
, ed
->name
);
2962 if (!strcmp(eed
->name
, ed
->name
) &&
2963 (eed
->nparam_min
<= ed
->nparam_max
|| ed
->plus
) &&
2964 (ed
->nparam_min
<= eed
->nparam_max
|| eed
->plus
)) {
2965 error(ERR_WARNING
|ERR_PASS1
,
2966 "redefining multi-line macro `%s'", ed
->name
);
2967 return DIRECTIVE_FOUND
;
2971 free_tlist(origline
);
2972 return DIRECTIVE_FOUND
;
2976 if (defining
!= NULL
) {
2977 if (defining
->type
== EXP_MMACRO
) {
2978 if (defining
->def_depth
> 0) {
2979 defining
->def_depth
--;
2980 return NO_DIRECTIVE_FOUND
;
2983 return NO_DIRECTIVE_FOUND
;
2986 if (!(defining
) || (defining
->type
!= EXP_MMACRO
)) {
2987 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2988 return DIRECTIVE_FOUND
;
2990 edhead
= (ExpDef
**) hash_findi_add(&expdefs
, defining
->name
);
2991 defining
->next
= *edhead
;
2994 defining
= ed
->prev
;
2995 ed
->prev
= expansions
;
2998 free_tlist(origline
);
2999 return DIRECTIVE_FOUND
;
3002 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3004 * We must search along istk->expansion until we hit a
3005 * macro invocation. Then we disable the emitting state(s)
3006 * between exitmacro and endmacro.
3008 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3009 if(ei
->type
== EXP_MMACRO
) {
3016 * Set all invocations leading back to the macro
3017 * invocation to a non-emitting state.
3019 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3020 eei
->emitting
= false;
3022 eei
->emitting
= false;
3024 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
3026 free_tlist(origline
);
3027 return DIRECTIVE_FOUND
;
3031 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3036 spec
.casesense
= (i
== PP_UNMACRO
);
3037 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
3038 return DIRECTIVE_FOUND
;
3040 ed_p
= (ExpDef
**) hash_findi(&expdefs
, spec
.name
, NULL
);
3041 while (ed_p
&& *ed_p
) {
3043 if (ed
->casesense
== spec
.casesense
&&
3044 !mstrcmp(ed
->name
, spec
.name
, spec
.casesense
) &&
3045 ed
->nparam_min
== spec
.nparam_min
&&
3046 ed
->nparam_max
== spec
.nparam_max
&&
3047 ed
->plus
== spec
.plus
) {
3048 if (ed
->cur_depth
> 0) {
3049 error(ERR_NONFATAL
, "`%s' ignored on active macro",
3060 free_tlist(origline
);
3061 free_tlist(spec
.dlist
);
3062 return DIRECTIVE_FOUND
;
3066 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3067 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
3068 tline
= tline
->next
;
3070 free_tlist(origline
);
3071 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
3072 return DIRECTIVE_FOUND
;
3074 t
= expand_smacro(tline
->next
);
3076 free_tlist(origline
);
3079 tokval
.t_type
= TOKEN_INVALID
;
3081 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3084 return DIRECTIVE_FOUND
;
3086 error(ERR_WARNING
|ERR_PASS1
,
3087 "trailing garbage after expression ignored");
3088 if (!is_simple(evalresult
)) {
3089 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
3090 return DIRECTIVE_FOUND
;
3092 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3093 if (ei
->type
== EXP_MMACRO
) {
3098 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
3099 } else if (ei
->nparam
== 0) {
3101 "`%%rotate' invoked within macro without parameters");
3103 int rotate
= ei
->rotate
+ reloc_value(evalresult
);
3105 rotate
%= (int)ei
->nparam
;
3107 rotate
+= ei
->nparam
;
3108 ei
->rotate
= rotate
;
3110 return DIRECTIVE_FOUND
;
3113 if (defining
!= NULL
) {
3114 if (defining
->type
== EXP_REP
) {
3115 defining
->def_depth
++;
3117 return NO_DIRECTIVE_FOUND
;
3121 tline
= tline
->next
;
3122 } while (tok_type_(tline
, TOK_WHITESPACE
));
3124 if (tok_type_(tline
, TOK_ID
) &&
3125 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3128 tline
= tline
->next
;
3129 } while (tok_type_(tline
, TOK_WHITESPACE
));
3133 t
= expand_smacro(tline
);
3135 tokval
.t_type
= TOKEN_INVALID
;
3137 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3139 free_tlist(origline
);
3140 return DIRECTIVE_FOUND
;
3143 error(ERR_WARNING
|ERR_PASS1
,
3144 "trailing garbage after expression ignored");
3145 if (!is_simple(evalresult
)) {
3146 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3147 return DIRECTIVE_FOUND
;
3149 count
= reloc_value(evalresult
);
3150 if (count
>= REP_LIMIT
) {
3151 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
3156 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3159 free_tlist(origline
);
3160 ed
= new_ExpDef(EXP_REP
);
3161 ed
->nolist
= nolist
;
3164 ed
->max_depth
= (count
- 1);
3165 ed
->ignoring
= false;
3166 ed
->prev
= defining
;
3168 return DIRECTIVE_FOUND
;
3171 if (defining
!= NULL
) {
3172 if (defining
->type
== EXP_REP
) {
3173 if (defining
->def_depth
> 0) {
3174 defining
->def_depth
--;
3175 return NO_DIRECTIVE_FOUND
;
3178 return NO_DIRECTIVE_FOUND
;
3181 if ((defining
== NULL
) || (defining
->type
!= EXP_REP
)) {
3182 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3183 return DIRECTIVE_FOUND
;
3187 * Now we have a "macro" defined - although it has no name
3188 * and we won't be entering it in the hash tables - we must
3189 * push a macro-end marker for it on to istk->expansion.
3190 * After that, it will take care of propagating itself (a
3191 * macro-end marker line for a macro which is really a %rep
3192 * block will cause the macro to be re-expanded, complete
3193 * with another macro-end marker to ensure the process
3194 * continues) until the whole expansion is forcibly removed
3195 * from istk->expansion by a %exitrep.
3198 defining
= ed
->prev
;
3199 ed
->prev
= expansions
;
3201 ei
= new_ExpInv(EXP_REP
, ed
);
3202 ei
->current
= ed
->line
;
3203 ei
->emitting
= ((ed
->max_depth
> 0) ? true : false);
3204 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3205 ei
->prev
= istk
->expansion
;
3206 istk
->expansion
= ei
;
3207 free_tlist(origline
);
3208 return DIRECTIVE_FOUND
;
3211 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3213 * We must search along istk->expansion until we hit a
3214 * rep invocation. Then we disable the emitting state(s)
3215 * between exitrep and endrep.
3217 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3218 if (ei
->type
== EXP_REP
) {
3225 * Set all invocations leading back to the rep
3226 * invocation to a non-emitting state.
3228 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3229 eei
->emitting
= false;
3231 eei
->emitting
= false;
3232 eei
->current
= NULL
;
3233 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3235 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3237 free_tlist(origline
);
3238 return DIRECTIVE_FOUND
;
3244 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3245 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3247 tline
= tline
->next
;
3249 tline
= expand_id(tline
);
3250 if (!tline
|| (tline
->type
!= TOK_ID
&&
3251 (tline
->type
!= TOK_PREPROC_ID
||
3252 tline
->text
[1] != '$'))) {
3253 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3255 free_tlist(origline
);
3256 return DIRECTIVE_FOUND
;
3259 ctx
= get_ctx(tline
->text
, &mname
);
3261 param_start
= tline
= tline
->next
;
3264 /* Expand the macro definition now for %xdefine and %ixdefine */
3265 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3266 tline
= expand_smacro(tline
);
3268 if (tok_is_(tline
, "(")) {
3270 * This macro has parameters.
3273 tline
= tline
->next
;
3277 error(ERR_NONFATAL
, "parameter identifier expected");
3278 free_tlist(origline
);
3279 return DIRECTIVE_FOUND
;
3281 if (tline
->type
!= TOK_ID
) {
3283 "`%s': parameter identifier expected",
3285 free_tlist(origline
);
3286 return DIRECTIVE_FOUND
;
3289 smacro_set_param_idx(tline
, nparam
);
3292 tline
= tline
->next
;
3294 if (tok_is_(tline
, ",")) {
3295 tline
= tline
->next
;
3297 if (!tok_is_(tline
, ")")) {
3299 "`)' expected to terminate macro template");
3300 free_tlist(origline
);
3301 return DIRECTIVE_FOUND
;
3307 tline
= tline
->next
;
3309 if (tok_type_(tline
, TOK_WHITESPACE
))
3310 last
= tline
, tline
= tline
->next
;
3315 if (t
->type
== TOK_ID
) {
3316 list_for_each(tt
, param_start
)
3317 if (is_smacro_param(tt
) &&
3318 !strcmp(tt
->text
, t
->text
))
3322 t
->next
= macro_start
;
3327 * Good. We now have a macro name, a parameter count, and a
3328 * token list (in reverse order) for an expansion. We ought
3329 * to be OK just to create an SMacro, store it, and let
3330 * free_tlist have the rest of the line (which we have
3331 * carefully re-terminated after chopping off the expansion
3334 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3335 free_tlist(origline
);
3336 return DIRECTIVE_FOUND
;
3339 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3340 tline
= tline
->next
;
3342 tline
= expand_id(tline
);
3343 if (!tline
|| (tline
->type
!= TOK_ID
&&
3344 (tline
->type
!= TOK_PREPROC_ID
||
3345 tline
->text
[1] != '$'))) {
3346 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3347 free_tlist(origline
);
3348 return DIRECTIVE_FOUND
;
3351 error(ERR_WARNING
|ERR_PASS1
,
3352 "trailing garbage after macro name ignored");
3355 /* Find the context that symbol belongs to */
3356 ctx
= get_ctx(tline
->text
, &mname
);
3357 undef_smacro(ctx
, mname
);
3358 free_tlist(origline
);
3359 return DIRECTIVE_FOUND
;
3363 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3364 casesense
= (i
== PP_DEFSTR
);
3366 tline
= tline
->next
;
3368 tline
= expand_id(tline
);
3369 if (!tline
|| (tline
->type
!= TOK_ID
&&
3370 (tline
->type
!= TOK_PREPROC_ID
||
3371 tline
->text
[1] != '$'))) {
3372 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3374 free_tlist(origline
);
3375 return DIRECTIVE_FOUND
;
3378 ctx
= get_ctx(tline
->text
, &mname
);
3380 tline
= expand_smacro(tline
->next
);
3383 while (tok_type_(tline
, TOK_WHITESPACE
))
3384 tline
= delete_Token(tline
);
3386 p
= detoken(tline
, false);
3387 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3388 macro_start
->text
= nasm_quote(p
, strlen(p
));
3389 macro_start
->type
= TOK_STRING
;
3393 * We now have a macro name, an implicit parameter count of
3394 * zero, and a string token to use as an expansion. Create
3395 * and store an SMacro.
3397 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3398 free_tlist(origline
);
3399 return DIRECTIVE_FOUND
;
3403 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3404 casesense
= (i
== PP_DEFTOK
);
3406 tline
= tline
->next
;
3408 tline
= expand_id(tline
);
3409 if (!tline
|| (tline
->type
!= TOK_ID
&&
3410 (tline
->type
!= TOK_PREPROC_ID
||
3411 tline
->text
[1] != '$'))) {
3413 "`%s' expects a macro identifier as first parameter",
3415 free_tlist(origline
);
3416 return DIRECTIVE_FOUND
;
3418 ctx
= get_ctx(tline
->text
, &mname
);
3420 tline
= expand_smacro(tline
->next
);
3424 while (tok_type_(t
, TOK_WHITESPACE
))
3426 /* t should now point to the string */
3427 if (!tok_type_(t
, TOK_STRING
)) {
3429 "`%s` requires string as second parameter",
3432 free_tlist(origline
);
3433 return DIRECTIVE_FOUND
;
3437 * Convert the string to a token stream. Note that smacros
3438 * are stored with the token stream reversed, so we have to
3439 * reverse the output of tokenize().
3441 nasm_unquote_cstr(t
->text
, i
);
3442 macro_start
= reverse_tokens(tokenize(t
->text
));
3445 * We now have a macro name, an implicit parameter count of
3446 * zero, and a numeric token to use as an expansion. Create
3447 * and store an SMacro.
3449 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3451 free_tlist(origline
);
3452 return DIRECTIVE_FOUND
;
3455 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3458 StrList
*xsl
= NULL
;
3459 StrList
**xst
= &xsl
;
3463 tline
= tline
->next
;
3465 tline
= expand_id(tline
);
3466 if (!tline
|| (tline
->type
!= TOK_ID
&&
3467 (tline
->type
!= TOK_PREPROC_ID
||
3468 tline
->text
[1] != '$'))) {
3470 "`%%pathsearch' expects a macro identifier as first parameter");
3471 free_tlist(origline
);
3472 return DIRECTIVE_FOUND
;
3474 ctx
= get_ctx(tline
->text
, &mname
);
3476 tline
= expand_smacro(tline
->next
);
3480 while (tok_type_(t
, TOK_WHITESPACE
))
3483 if (!t
|| (t
->type
!= TOK_STRING
&&
3484 t
->type
!= TOK_INTERNAL_STRING
)) {
3485 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3487 free_tlist(origline
);
3488 return DIRECTIVE_FOUND
; /* but we did _something_ */
3491 error(ERR_WARNING
|ERR_PASS1
,
3492 "trailing garbage after `%%pathsearch' ignored");
3494 if (t
->type
!= TOK_INTERNAL_STRING
)
3495 nasm_unquote(p
, NULL
);
3497 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3500 fclose(fp
); /* Don't actually care about the file */
3502 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3503 macro_start
->text
= nasm_quote(p
, strlen(p
));
3504 macro_start
->type
= TOK_STRING
;
3508 * We now have a macro name, an implicit parameter count of
3509 * zero, and a string token to use as an expansion. Create
3510 * and store an SMacro.
3512 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3514 free_tlist(origline
);
3515 return DIRECTIVE_FOUND
;
3519 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3522 tline
= tline
->next
;
3524 tline
= expand_id(tline
);
3525 if (!tline
|| (tline
->type
!= TOK_ID
&&
3526 (tline
->type
!= TOK_PREPROC_ID
||
3527 tline
->text
[1] != '$'))) {
3529 "`%%strlen' expects a macro identifier as first parameter");
3530 free_tlist(origline
);
3531 return DIRECTIVE_FOUND
;
3533 ctx
= get_ctx(tline
->text
, &mname
);
3535 tline
= expand_smacro(tline
->next
);
3539 while (tok_type_(t
, TOK_WHITESPACE
))
3541 /* t should now point to the string */
3542 if (!tok_type_(t
, TOK_STRING
)) {
3544 "`%%strlen` requires string as second parameter");
3546 free_tlist(origline
);
3547 return DIRECTIVE_FOUND
;
3550 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3551 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3554 * We now have a macro name, an implicit parameter count of
3555 * zero, and a numeric token to use as an expansion. Create
3556 * and store an SMacro.
3558 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3560 free_tlist(origline
);
3561 return DIRECTIVE_FOUND
;
3564 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3567 tline
= tline
->next
;
3569 tline
= expand_id(tline
);
3570 if (!tline
|| (tline
->type
!= TOK_ID
&&
3571 (tline
->type
!= TOK_PREPROC_ID
||
3572 tline
->text
[1] != '$'))) {
3574 "`%%strcat' expects a macro identifier as first parameter");
3575 free_tlist(origline
);
3576 return DIRECTIVE_FOUND
;
3578 ctx
= get_ctx(tline
->text
, &mname
);
3580 tline
= expand_smacro(tline
->next
);
3584 list_for_each(t
, tline
) {
3586 case TOK_WHITESPACE
:
3589 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3592 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3594 /* else fall through */
3597 "non-string passed to `%%strcat' (%d)", t
->type
);
3599 free_tlist(origline
);
3600 return DIRECTIVE_FOUND
;
3604 p
= pp
= nasm_malloc(len
);
3605 list_for_each(t
, tline
) {
3606 if (t
->type
== TOK_STRING
) {
3607 memcpy(p
, t
->text
, t
->a
.len
);
3613 * We now have a macro name, an implicit parameter count of
3614 * zero, and a numeric token to use as an expansion. Create
3615 * and store an SMacro.
3617 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3618 macro_start
->text
= nasm_quote(pp
, len
);
3620 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3622 free_tlist(origline
);
3623 return DIRECTIVE_FOUND
;
3626 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3628 int64_t start
, count
;
3633 tline
= tline
->next
;
3635 tline
= expand_id(tline
);
3636 if (!tline
|| (tline
->type
!= TOK_ID
&&
3637 (tline
->type
!= TOK_PREPROC_ID
||
3638 tline
->text
[1] != '$'))) {
3640 "`%%substr' expects a macro identifier as first parameter");
3641 free_tlist(origline
);
3642 return DIRECTIVE_FOUND
;
3644 ctx
= get_ctx(tline
->text
, &mname
);
3646 tline
= expand_smacro(tline
->next
);
3649 if (tline
) /* skip expanded id */
3651 while (tok_type_(t
, TOK_WHITESPACE
))
3654 /* t should now point to the string */
3655 if (!tok_type_(t
, TOK_STRING
)) {
3657 "`%%substr` requires string as second parameter");
3659 free_tlist(origline
);
3660 return DIRECTIVE_FOUND
;
3665 tokval
.t_type
= TOKEN_INVALID
;
3666 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3670 free_tlist(origline
);
3671 return DIRECTIVE_FOUND
;
3672 } else if (!is_simple(evalresult
)) {
3673 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3675 free_tlist(origline
);
3676 return DIRECTIVE_FOUND
;
3678 start
= evalresult
->value
- 1;
3680 while (tok_type_(tt
, TOK_WHITESPACE
))
3683 count
= 1; /* Backwards compatibility: one character */
3685 tokval
.t_type
= TOKEN_INVALID
;
3686 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3690 free_tlist(origline
);
3691 return DIRECTIVE_FOUND
;
3692 } else if (!is_simple(evalresult
)) {
3693 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3695 free_tlist(origline
);
3696 return DIRECTIVE_FOUND
;
3698 count
= evalresult
->value
;
3701 len
= nasm_unquote(t
->text
, NULL
);
3702 /* make start and count being in range */
3706 count
= len
+ count
+ 1 - start
;
3707 if (start
+ count
> (int64_t)len
)
3708 count
= len
- start
;
3709 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3710 start
= -1, count
= 0; /* empty string */
3712 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3713 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3714 macro_start
->type
= TOK_STRING
;
3717 * We now have a macro name, an implicit parameter count of
3718 * zero, and a numeric token to use as an expansion. Create
3719 * and store an SMacro.
3721 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3723 free_tlist(origline
);
3724 return DIRECTIVE_FOUND
;
3729 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3730 casesense
= (i
== PP_ASSIGN
);
3732 tline
= tline
->next
;
3734 tline
= expand_id(tline
);
3735 if (!tline
|| (tline
->type
!= TOK_ID
&&
3736 (tline
->type
!= TOK_PREPROC_ID
||
3737 tline
->text
[1] != '$'))) {
3739 "`%%%sassign' expects a macro identifier",
3740 (i
== PP_IASSIGN
? "i" : ""));
3741 free_tlist(origline
);
3742 return DIRECTIVE_FOUND
;
3744 ctx
= get_ctx(tline
->text
, &mname
);
3746 tline
= expand_smacro(tline
->next
);
3751 tokval
.t_type
= TOKEN_INVALID
;
3753 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3756 free_tlist(origline
);
3757 return DIRECTIVE_FOUND
;
3761 error(ERR_WARNING
|ERR_PASS1
,
3762 "trailing garbage after expression ignored");
3764 if (!is_simple(evalresult
)) {
3766 "non-constant value given to `%%%sassign'",
3767 (i
== PP_IASSIGN
? "i" : ""));
3768 free_tlist(origline
);
3769 return DIRECTIVE_FOUND
;
3772 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3773 make_tok_num(macro_start
, reloc_value(evalresult
));
3776 * We now have a macro name, an implicit parameter count of
3777 * zero, and a numeric token to use as an expansion. Create
3778 * and store an SMacro.
3780 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3781 free_tlist(origline
);
3782 return DIRECTIVE_FOUND
;
3785 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3787 * Syntax is `%line nnn[+mmm] [filename]'
3789 tline
= tline
->next
;
3791 if (!tok_type_(tline
, TOK_NUMBER
)) {
3792 error(ERR_NONFATAL
, "`%%line' expects line number");
3793 free_tlist(origline
);
3794 return DIRECTIVE_FOUND
;
3796 k
= readnum(tline
->text
, &err
);
3798 tline
= tline
->next
;
3799 if (tok_is_(tline
, "+")) {
3800 tline
= tline
->next
;
3801 if (!tok_type_(tline
, TOK_NUMBER
)) {
3802 error(ERR_NONFATAL
, "`%%line' expects line increment");
3803 free_tlist(origline
);
3804 return DIRECTIVE_FOUND
;
3806 m
= readnum(tline
->text
, &err
);
3807 tline
= tline
->next
;
3813 nasm_free(src_set_fname(detoken(tline
, false)));
3815 free_tlist(origline
);
3816 return DIRECTIVE_FOUND
;
3819 if (defining
!= NULL
) {
3820 if (defining
->type
== EXP_WHILE
) {
3821 defining
->def_depth
++;
3823 return NO_DIRECTIVE_FOUND
;
3826 if ((istk
->expansion
!= NULL
) &&
3827 (istk
->expansion
->emitting
== false)) {
3831 l
->first
= copy_Token(tline
->next
);
3832 j
= if_condition(tline
->next
, i
);
3833 tline
->next
= NULL
; /* it got freed */
3834 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
3836 ed
= new_ExpDef(EXP_WHILE
);
3839 ed
->max_depth
= DEADMAN_LIMIT
;
3840 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
3841 if (ed
->ignoring
== false) {
3844 } else if (l
!= NULL
) {
3845 delete_Token(l
->first
);
3849 ed
->prev
= defining
;
3851 free_tlist(origline
);
3852 return DIRECTIVE_FOUND
;
3855 if (defining
!= NULL
) {
3856 if (defining
->type
== EXP_WHILE
) {
3857 if (defining
->def_depth
> 0) {
3858 defining
->def_depth
--;
3859 return NO_DIRECTIVE_FOUND
;
3862 return NO_DIRECTIVE_FOUND
;
3865 if (tline
->next
!= NULL
) {
3866 error_precond(ERR_WARNING
|ERR_PASS1
,
3867 "trailing garbage after `%%endwhile' ignored");
3869 if ((defining
== NULL
) || (defining
->type
!= EXP_WHILE
)) {
3870 error(ERR_NONFATAL
, "`%%endwhile': no matching `%%while'");
3871 return DIRECTIVE_FOUND
;
3874 defining
= ed
->prev
;
3875 if (ed
->ignoring
== false) {
3876 ed
->prev
= expansions
;
3878 ei
= new_ExpInv(EXP_WHILE
, ed
);
3879 ei
->current
= ed
->line
->next
;
3880 ei
->emitting
= true;
3881 ei
->prev
= istk
->expansion
;
3882 istk
->expansion
= ei
;
3886 free_tlist(origline
);
3887 return DIRECTIVE_FOUND
;
3890 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3892 * We must search along istk->expansion until we hit a
3893 * while invocation. Then we disable the emitting state(s)
3894 * between exitwhile and endwhile.
3896 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3897 if (ei
->type
== EXP_WHILE
) {
3904 * Set all invocations leading back to the while
3905 * invocation to a non-emitting state.
3907 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3908 eei
->emitting
= false;
3910 eei
->emitting
= false;
3911 eei
->current
= NULL
;
3912 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3914 error(ERR_NONFATAL
, "`%%exitwhile' not within `%%while' block");
3916 free_tlist(origline
);
3917 return DIRECTIVE_FOUND
;
3920 if (defining
!= NULL
) {
3921 if (defining
->type
== EXP_COMMENT
) {
3922 defining
->def_depth
++;
3924 return NO_DIRECTIVE_FOUND
;
3926 ed
= new_ExpDef(EXP_COMMENT
);
3927 ed
->ignoring
= true;
3928 ed
->prev
= defining
;
3930 free_tlist(origline
);
3931 return DIRECTIVE_FOUND
;
3934 if (defining
!= NULL
) {
3935 if (defining
->type
== EXP_COMMENT
) {
3936 if (defining
->def_depth
> 0) {
3937 defining
->def_depth
--;
3938 return NO_DIRECTIVE_FOUND
;
3941 return NO_DIRECTIVE_FOUND
;
3944 if ((defining
== NULL
) || (defining
->type
!= EXP_COMMENT
)) {
3945 error(ERR_NONFATAL
, "`%%endcomment': no matching `%%comment'");
3946 return DIRECTIVE_FOUND
;
3949 defining
= ed
->prev
;
3951 free_tlist(origline
);
3952 return DIRECTIVE_FOUND
;
3955 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3956 if (in_final
!= false) {
3957 error(ERR_FATAL
, "`%%final' cannot be used recursively");
3959 tline
= tline
->next
;
3961 if (tline
== NULL
) {
3962 error(ERR_NONFATAL
, "`%%final' expects at least one parameter");
3965 l
->first
= copy_Token(tline
);
3969 free_tlist(origline
);
3970 return DIRECTIVE_FOUND
;
3974 "preprocessor directive `%s' not yet implemented",
3976 return DIRECTIVE_FOUND
;
3981 * Ensure that a macro parameter contains a condition code and
3982 * nothing else. Return the condition code index if so, or -1
3985 static int find_cc(Token
* t
)
3991 return -1; /* Probably a %+ without a space */
3994 if (t
->type
!= TOK_ID
)
3998 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
4002 j
= ARRAY_SIZE(conditions
);
4005 m
= nasm_stricmp(t
->text
, conditions
[k
]);
4020 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
4021 int mnum
, bool handle_paste_tokens
)
4023 Token
**tail
, *t
, *tt
;
4025 bool did_paste
= false;
4029 nasm_dump_stream(*head
);
4030 nasm_dump_token(*head
);
4032 /* Now handle token pasting... */
4035 while ((t
= *tail
) && (tt
= t
->next
)) {
4037 case TOK_WHITESPACE
:
4038 if (tt
->type
== TOK_WHITESPACE
) {
4039 /* Zap adjacent whitespace tokens */
4040 t
->next
= delete_Token(tt
);
4042 /* Do not advance paste_head here */
4046 case TOK_PASTE
: /* %+ */
4047 if (handle_paste_tokens
) {
4048 /* Zap %+ and whitespace tokens to the right */
4049 while (t
&& (t
->type
== TOK_WHITESPACE
||
4050 t
->type
== TOK_PASTE
))
4051 t
= *tail
= delete_Token(t
);
4052 if (!t
) { /* Dangling %+ term */
4054 (*paste_head
)->next
= NULL
;
4062 while (tok_type_(tt
, TOK_WHITESPACE
))
4063 tt
= t
->next
= delete_Token(tt
);
4065 tmp
= nasm_strcat(t
->text
, tt
->text
);
4067 tt
= delete_Token(tt
);
4068 t
= *tail
= tokenize(tmp
);
4074 t
->next
= tt
; /* Attach the remaining token chain */
4081 /* else fall through */
4084 * Concatenation of tokens might look nontrivial
4085 * but in real it's pretty simple -- the caller
4086 * prepares the masks of token types to be concatenated
4087 * and we simply find matched sequences and slip
4090 for (i
= 0; i
< mnum
; i
++) {
4091 if (PP_CONCAT_MASK(t
->type
) & m
[i
].mask_head
) {
4095 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & m
[i
].mask_tail
)) {
4096 len
+= strlen(tt
->text
);
4100 nasm_dump_token(tt
);
4103 * Now tt points to the first token after
4104 * the potential paste area...
4106 if (tt
!= t
->next
) {
4107 /* We have at least two tokens... */
4108 len
+= strlen(t
->text
);
4109 p
= tmp
= nasm_malloc(len
+1);
4112 p
= strchr(p
, '\0');
4113 t
= delete_Token(t
);
4115 t
= *tail
= tokenize(tmp
);
4121 t
->next
= tt
; /* Attach the remaining token chain */
4129 if (i
>= mnum
) { /* no match */
4131 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
4141 * expands to a list of tokens from %{x:y}
4143 static Token
*expand_mmac_params_range(ExpInv
*ei
, Token
*tline
, Token
***last
)
4145 Token
*t
= tline
, **tt
, *tm
, *head
;
4149 pos
= strchr(tline
->text
, ':');
4152 lst
= atoi(pos
+ 1);
4153 fst
= atoi(tline
->text
+ 1);
4156 * only macros params are accounted so
4157 * if someone passes %0 -- we reject such
4160 if (lst
== 0 || fst
== 0)
4163 /* the values should be sane */
4164 if ((fst
> (int)ei
->nparam
|| fst
< (-(int)ei
->nparam
)) ||
4165 (lst
> (int)ei
->nparam
|| lst
< (-(int)ei
->nparam
)))
4168 fst
= fst
< 0 ? fst
+ (int)ei
->nparam
+ 1: fst
;
4169 lst
= lst
< 0 ? lst
+ (int)ei
->nparam
+ 1: lst
;
4171 /* counted from zero */
4175 * it will be at least one token
4177 tm
= ei
->params
[(fst
+ ei
->rotate
) % ei
->nparam
];
4178 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4179 head
= t
, tt
= &t
->next
;
4181 for (i
= fst
+ 1; i
<= lst
; i
++) {
4182 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4183 *tt
= t
, tt
= &t
->next
;
4184 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4186 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4187 *tt
= t
, tt
= &t
->next
;
4190 for (i
= fst
- 1; i
>= lst
; i
--) {
4191 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4192 *tt
= t
, tt
= &t
->next
;
4193 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4195 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4196 *tt
= t
, tt
= &t
->next
;
4204 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
4210 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4211 * %-n) and MMacro-local identifiers (%%foo) as well as
4212 * macro indirection (%[...]) and range (%{..:..}).
4214 static Token
*expand_mmac_params(Token
* tline
)
4216 Token
*t
, *tt
, **tail
, *thead
;
4217 bool changed
= false;
4223 nasm_dump_stream(tline
);
4226 if (tline
->type
== TOK_PREPROC_ID
&&
4227 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4228 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4229 tline
->text
[1] == '%')) {
4231 int type
= 0, cc
; /* type = 0 to placate optimisers */
4238 tline
= tline
->next
;
4240 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
4241 if (ei
->type
== EXP_MMACRO
) {
4246 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4248 pos
= strchr(t
->text
, ':');
4250 switch (t
->text
[1]) {
4252 * We have to make a substitution of one of the
4253 * forms %1, %-1, %+1, %%foo, %0.
4256 if ((strlen(t
->text
) > 2) && (t
->text
[2] == '0')) {
4258 text
= nasm_strdup(ei
->label_text
);
4261 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", ei
->nparam
);
4262 text
= nasm_strdup(tmpbuf
);
4267 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4269 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4272 n
= atoi(t
->text
+ 2) - 1;
4273 if (n
>= ei
->nparam
)
4277 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4283 "macro parameter %d is not a condition code",
4288 if (inverse_ccs
[cc
] == -1) {
4290 "condition code `%s' is not invertible",
4294 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4298 n
= atoi(t
->text
+ 2) - 1;
4299 if (n
>= ei
->nparam
)
4303 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4309 "macro parameter %d is not a condition code",
4314 text
= nasm_strdup(conditions
[cc
]);
4318 n
= atoi(t
->text
+ 1) - 1;
4319 if (n
>= ei
->nparam
)
4323 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4327 for (i
= 0; i
< ei
->paramlen
[n
]; i
++) {
4328 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4329 tail
= &(*tail
)->next
;
4333 text
= NULL
; /* we've done it here */
4338 * seems we have a parameters range here
4340 Token
*head
, **last
;
4341 head
= expand_mmac_params_range(ei
, t
, &last
);
4362 } else if (tline
->type
== TOK_INDIRECT
) {
4364 tline
= tline
->next
;
4365 tt
= tokenize(t
->text
);
4366 tt
= expand_mmac_params(tt
);
4367 tt
= expand_smacro(tt
);
4370 tt
->a
.mac
= NULL
; /* Necessary? */
4378 tline
= tline
->next
;
4386 const struct tokseq_match t
[] = {
4388 PP_CONCAT_MASK(TOK_ID
) |
4389 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4390 PP_CONCAT_MASK(TOK_ID
) |
4391 PP_CONCAT_MASK(TOK_NUMBER
) |
4392 PP_CONCAT_MASK(TOK_FLOAT
) |
4393 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4396 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4397 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4400 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4403 nasm_dump_token(thead
);
4409 * Expand all single-line macro calls made in the given line.
4410 * Return the expanded version of the line. The original is deemed
4411 * to be destroyed in the process. (In reality we'll just move
4412 * Tokens from input to output a lot of the time, rather than
4413 * actually bothering to destroy and replicate.)
4416 static Token
*expand_smacro(Token
* tline
)
4418 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4419 SMacro
*head
= NULL
, *m
;
4422 unsigned int nparam
, sparam
;
4424 Token
*org_tline
= tline
;
4427 int deadman
= DEADMAN_LIMIT
;
4431 * Trick: we should avoid changing the start token pointer since it can
4432 * be contained in "next" field of other token. Because of this
4433 * we allocate a copy of first token and work with it; at the end of
4434 * routine we copy it back
4437 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4438 org_tline
->text
, 0);
4439 tline
->a
.mac
= org_tline
->a
.mac
;
4440 nasm_free(org_tline
->text
);
4441 org_tline
->text
= NULL
;
4444 expanded
= true; /* Always expand %+ at least once */
4450 while (tline
) { /* main token loop */
4452 error(ERR_NONFATAL
, "interminable macro recursion");
4456 if ((mname
= tline
->text
)) {
4457 /* if this token is a local macro, look in local context */
4458 if (tline
->type
== TOK_ID
) {
4459 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4460 } else if (tline
->type
== TOK_PREPROC_ID
) {
4461 ctx
= get_ctx(mname
, &mname
);
4462 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4467 * We've hit an identifier. As in is_mmacro below, we first
4468 * check whether the identifier is a single-line macro at
4469 * all, then think about checking for parameters if
4472 list_for_each(m
, head
) {
4473 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4480 if (m
->nparam
== 0) {
4482 * Simple case: the macro is parameterless. Discard the
4483 * one token that the macro call took, and push the
4484 * expansion back on the to-do stack.
4486 if (!m
->expansion
) {
4487 if (!strcmp("__FILE__", m
->name
)) {
4490 src_get(&num
, &file
);
4491 tline
->text
= nasm_quote(file
, strlen(file
));
4492 tline
->type
= TOK_STRING
;
4496 if (!strcmp("__LINE__", m
->name
)) {
4497 nasm_free(tline
->text
);
4498 make_tok_num(tline
, src_get_linnum());
4501 if (!strcmp("__BITS__", m
->name
)) {
4502 nasm_free(tline
->text
);
4503 make_tok_num(tline
, globalbits
);
4506 tline
= delete_Token(tline
);
4511 * Complicated case: at least one macro with this name
4512 * exists and takes parameters. We must find the
4513 * parameters in the call, count them, find the SMacro
4514 * that corresponds to that form of the macro call, and
4515 * substitute for the parameters when we expand. What a
4518 /*tline = tline->next;
4519 skip_white_(tline); */
4522 while (tok_type_(t
, TOK_SMAC_END
)) {
4523 t
->a
.mac
->in_progress
= false;
4525 t
= tline
->next
= delete_Token(t
);
4528 } while (tok_type_(tline
, TOK_WHITESPACE
));
4529 if (!tok_is_(tline
, "(")) {
4531 * This macro wasn't called with parameters: ignore
4532 * the call. (Behaviour borrowed from gnu cpp.)
4541 sparam
= PARAM_DELTA
;
4542 params
= nasm_malloc(sparam
* sizeof(Token
*));
4543 params
[0] = tline
->next
;
4544 paramsize
= nasm_malloc(sparam
* sizeof(int));
4546 while (true) { /* parameter loop */
4548 * For some unusual expansions
4549 * which concatenates function call
4552 while (tok_type_(t
, TOK_SMAC_END
)) {
4553 t
->a
.mac
->in_progress
= false;
4555 t
= tline
->next
= delete_Token(t
);
4561 "macro call expects terminating `)'");
4564 if (tline
->type
== TOK_WHITESPACE
4566 if (paramsize
[nparam
])
4569 params
[nparam
] = tline
->next
;
4570 continue; /* parameter loop */
4572 if (tline
->type
== TOK_OTHER
4573 && tline
->text
[1] == 0) {
4574 char ch
= tline
->text
[0];
4575 if (ch
== ',' && !paren
&& brackets
<= 0) {
4576 if (++nparam
>= sparam
) {
4577 sparam
+= PARAM_DELTA
;
4578 params
= nasm_realloc(params
,
4579 sparam
* sizeof(Token
*));
4580 paramsize
= nasm_realloc(paramsize
,
4581 sparam
* sizeof(int));
4583 params
[nparam
] = tline
->next
;
4584 paramsize
[nparam
] = 0;
4586 continue; /* parameter loop */
4589 (brackets
> 0 || (brackets
== 0 &&
4590 !paramsize
[nparam
])))
4592 if (!(brackets
++)) {
4593 params
[nparam
] = tline
->next
;
4594 continue; /* parameter loop */
4597 if (ch
== '}' && brackets
> 0)
4598 if (--brackets
== 0) {
4600 continue; /* parameter loop */
4602 if (ch
== '(' && !brackets
)
4604 if (ch
== ')' && brackets
<= 0)
4610 error(ERR_NONFATAL
, "braces do not "
4611 "enclose all of macro parameter");
4613 paramsize
[nparam
] += white
+ 1;
4615 } /* parameter loop */
4617 while (m
&& (m
->nparam
!= nparam
||
4618 mstrcmp(m
->name
, mname
,
4622 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4623 "macro `%s' exists, "
4624 "but not taking %d parameters",
4625 mstart
->text
, nparam
);
4628 if (m
&& m
->in_progress
)
4630 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4632 * Design question: should we handle !tline, which
4633 * indicates missing ')' here, or expand those
4634 * macros anyway, which requires the (t) test a few
4638 nasm_free(paramsize
);
4642 * Expand the macro: we are placed on the last token of the
4643 * call, so that we can easily split the call from the
4644 * following tokens. We also start by pushing an SMAC_END
4645 * token for the cycle removal.
4652 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4654 m
->in_progress
= true;
4656 list_for_each(t
, m
->expansion
) {
4657 if (is_smacro_param(t
)) {
4658 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4662 idx
= smacro_get_param_idx(t
);
4666 * We need smacro paramters appended.
4668 for (i
= paramsize
[idx
]; i
> 0; i
--) {
4669 *ptail
= new_Token(tline
, ttt
->type
, ttt
->text
, 0);
4670 ptail
= &(*ptail
)->next
;
4675 } else if (t
->type
== TOK_PREPROC_Q
) {
4676 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4678 } else if (t
->type
== TOK_PREPROC_QQ
) {
4679 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4682 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4688 * Having done that, get rid of the macro call, and clean
4689 * up the parameters.
4692 nasm_free(paramsize
);
4695 continue; /* main token loop */
4700 if (tline
->type
== TOK_SMAC_END
) {
4701 tline
->a
.mac
->in_progress
= false;
4702 tline
= delete_Token(tline
);
4705 tline
= tline
->next
;
4713 * Now scan the entire line and look for successive TOK_IDs that resulted
4714 * after expansion (they can't be produced by tokenize()). The successive
4715 * TOK_IDs should be concatenated.
4716 * Also we look for %+ tokens and concatenate the tokens before and after
4717 * them (without white spaces in between).
4720 const struct tokseq_match t
[] = {
4722 PP_CONCAT_MASK(TOK_ID
) |
4723 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4724 PP_CONCAT_MASK(TOK_ID
) |
4725 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4726 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4729 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4731 * If we concatenated something, *and* we had previously expanded
4732 * an actual macro, scan the lines again for macros...
4743 *org_tline
= *thead
;
4744 /* since we just gave text to org_line, don't free it */
4746 delete_Token(thead
);
4748 /* the expression expanded to empty line;
4749 we can't return NULL for some reasons
4750 we just set the line to a single WHITESPACE token. */
4751 memset(org_tline
, 0, sizeof(*org_tline
));
4752 org_tline
->text
= NULL
;
4753 org_tline
->type
= TOK_WHITESPACE
;
4762 * Similar to expand_smacro but used exclusively with macro identifiers
4763 * right before they are fetched in. The reason is that there can be
4764 * identifiers consisting of several subparts. We consider that if there
4765 * are more than one element forming the name, user wants a expansion,
4766 * otherwise it will be left as-is. Example:
4770 * the identifier %$abc will be left as-is so that the handler for %define
4771 * will suck it and define the corresponding value. Other case:
4773 * %define _%$abc cde
4775 * In this case user wants name to be expanded *before* %define starts
4776 * working, so we'll expand %$abc into something (if it has a value;
4777 * otherwise it will be left as-is) then concatenate all successive
4780 static Token
*expand_id(Token
* tline
)
4782 Token
*cur
, *oldnext
= NULL
;
4784 if (!tline
|| !tline
->next
)
4789 (cur
->next
->type
== TOK_ID
||
4790 cur
->next
->type
== TOK_PREPROC_ID
||
4791 cur
->next
->type
== TOK_NUMBER
))
4794 /* If identifier consists of just one token, don't expand */
4799 oldnext
= cur
->next
; /* Detach the tail past identifier */
4800 cur
->next
= NULL
; /* so that expand_smacro stops here */
4803 tline
= expand_smacro(tline
);
4806 /* expand_smacro possibly changhed tline; re-scan for EOL */
4808 while (cur
&& cur
->next
)
4811 cur
->next
= oldnext
;
4818 * Determine whether the given line constitutes a multi-line macro
4819 * call, and return the ExpDef structure called if so. Doesn't have
4820 * to check for an initial label - that's taken care of in
4821 * expand_mmacro - but must check numbers of parameters. Guaranteed
4822 * to be called with tline->type == TOK_ID, so the putative macro
4823 * name is easy to find.
4825 static ExpDef
*is_mmacro(Token
* tline
, Token
*** params_array
)
4831 head
= (ExpDef
*) hash_findix(&expdefs
, tline
->text
);
4834 * Efficiency: first we see if any macro exists with the given
4835 * name. If not, we can return NULL immediately. _Then_ we
4836 * count the parameters, and then we look further along the
4837 * list if necessary to find the proper ExpDef.
4839 list_for_each(ed
, head
) {
4840 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4847 * OK, we have a potential macro. Count and demarcate the
4850 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4853 * So we know how many parameters we've got. Find the ExpDef
4854 * structure that handles this number.
4857 if (ed
->nparam_min
<= nparam
4858 && (ed
->plus
|| nparam
<= ed
->nparam_max
)) {
4860 * It's right, and we can use it. Add its default
4861 * parameters to the end of our list if necessary.
4863 if (ed
->defaults
&& nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4865 nasm_realloc(params
,
4866 ((ed
->nparam_min
+ ed
->ndefs
+
4867 1) * sizeof(*params
)));
4868 while (nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4869 params
[nparam
] = ed
->defaults
[nparam
- ed
->nparam_min
];
4874 * If we've gone over the maximum parameter count (and
4875 * we're in Plus mode), ignore parameters beyond
4878 if (ed
->plus
&& nparam
> ed
->nparam_max
)
4879 nparam
= ed
->nparam_max
;
4881 * Then terminate the parameter list, and leave.
4883 if (!params
) { /* need this special case */
4884 params
= nasm_malloc(sizeof(*params
));
4887 params
[nparam
] = NULL
;
4888 *params_array
= params
;
4892 * This one wasn't right: look for the next one with the
4895 list_for_each(ed
, ed
->next
) {
4896 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4902 * After all that, we didn't find one with the right number of
4903 * parameters. Issue a warning, and fail to expand the macro.
4905 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4906 "macro `%s' exists, but not taking %d parameters",
4907 tline
->text
, nparam
);
4913 * Expand the multi-line macro call made by the given line, if
4914 * there is one to be expanded. If there is, push the expansion on
4915 * istk->expansion and return true. Otherwise return false.
4917 static bool expand_mmacro(Token
* tline
)
4919 Token
*label
= NULL
;
4920 int dont_prepend
= 0;
4925 int i
, nparam
, *paramlen
;
4930 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4931 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4933 ed
= is_mmacro(t
, ¶ms
);
4939 * We have an id which isn't a macro call. We'll assume
4940 * it might be a label; we'll also check to see if a
4941 * colon follows it. Then, if there's another id after
4942 * that lot, we'll check it again for macro-hood.
4946 if (tok_type_(t
, TOK_WHITESPACE
))
4947 last
= t
, t
= t
->next
;
4948 if (tok_is_(t
, ":")) {
4950 last
= t
, t
= t
->next
;
4951 if (tok_type_(t
, TOK_WHITESPACE
))
4952 last
= t
, t
= t
->next
;
4954 if (!tok_type_(t
, TOK_ID
) || !(ed
= is_mmacro(t
, ¶ms
)))
4962 * Fix up the parameters: this involves stripping leading and
4963 * trailing whitespace, then stripping braces if they are
4966 for (nparam
= 0; params
[nparam
]; nparam
++)
4968 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4970 for (i
= 0; params
[i
]; i
++) {
4972 int comma
= (!ed
->plus
|| i
< nparam
- 1);
4976 if (tok_is_(t
, "{"))
4977 t
= t
->next
, brace
= true, comma
= false;
4981 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4982 break; /* ... because we have hit a comma */
4983 if (comma
&& t
->type
== TOK_WHITESPACE
4984 && tok_is_(t
->next
, ","))
4985 break; /* ... or a space then a comma */
4986 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4987 break; /* ... or a brace */
4993 if (ed
->cur_depth
>= ed
->max_depth
) {
4994 if (ed
->max_depth
> 1) {
4996 "reached maximum macro recursion depth of %i for %s",
4997 ed
->max_depth
,ed
->name
);
5005 * OK, we have found a ExpDef structure representing a
5006 * previously defined mmacro. Create an expansion invocation
5007 * and point it back to the expansion definition. Substitution of
5008 * parameter tokens and macro-local tokens doesn't get done
5009 * until the single-line macro substitution process; this is
5010 * because delaying them allows us to change the semantics
5011 * later through %rotate.
5013 ei
= new_ExpInv(EXP_MMACRO
, ed
);
5014 ei
->name
= nasm_strdup(mname
);
5015 //ei->label = label;
5016 //ei->label_text = detoken(label, false);
5017 ei
->current
= ed
->line
;
5018 ei
->emitting
= true;
5019 //ei->iline = tline;
5020 ei
->params
= params
;
5021 ei
->nparam
= nparam
;
5023 ei
->paramlen
= paramlen
;
5026 ei
->prev
= istk
->expansion
;
5027 istk
->expansion
= ei
;
5030 * Special case: detect %00 on first invocation; if found,
5031 * avoid emitting any labels that precede the mmacro call.
5032 * ed->prepend is set to -1 when %00 is detected, else 1.
5034 if (ed
->prepend
== 0) {
5035 for (l
= ed
->line
; l
!= NULL
; l
= l
->next
) {
5036 for (t
= l
->first
; t
!= NULL
; t
= t
->next
) {
5037 if ((t
->type
== TOK_PREPROC_ID
) &&
5038 (strlen(t
->text
) == 3) &&
5039 (t
->text
[1] == '0') && (t
->text
[2] == '0')) {
5044 if (dont_prepend
< 0)
5047 ed
->prepend
= ((dont_prepend
< 0) ? -1 : 1);
5051 * If we had a label, push it on as the first line of
5052 * the macro expansion.
5054 if (label
!= NULL
) {
5055 if (ed
->prepend
< 0) {
5056 ei
->label_text
= detoken(label
, false);
5058 if (dont_prepend
== 0) {
5060 while (t
->next
!= NULL
) {
5063 t
->next
= new_Token(NULL
, TOK_OTHER
, ":", 0);
5066 l
->first
= copy_Token(label
);
5067 l
->next
= ei
->current
;
5072 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
5078 /* The function that actually does the error reporting */
5079 static void verror(int severity
, const char *fmt
, va_list arg
)
5083 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
5085 if (istk
&& istk
->mmac_depth
> 0) {
5086 ExpInv
*ei
= istk
->expansion
;
5087 int lineno
= ei
->lineno
;
5089 if (ei
->type
== EXP_MMACRO
)
5091 lineno
+= ei
->relno
;
5094 nasm_error(severity
, "(%s:%d) %s", ei
->def
->name
,
5097 nasm_error(severity
, "%s", buff
);
5102 * Since preprocessor always operate only on the line that didn't
5103 * arrived yet, we should always use ERR_OFFBY1.
5105 static void error(int severity
, const char *fmt
, ...)
5109 verror(severity
, fmt
, arg
);
5114 * Because %else etc are evaluated in the state context
5115 * of the previous branch, errors might get lost with error():
5116 * %if 0 ... %else trailing garbage ... %endif
5117 * So %else etc should report errors with this function.
5119 static void error_precond(int severity
, const char *fmt
, ...)
5123 /* Only ignore the error if it's really in a dead branch */
5124 if ((istk
!= NULL
) &&
5125 (istk
->expansion
!= NULL
) &&
5126 (istk
->expansion
->type
== EXP_IF
) &&
5127 (istk
->expansion
->def
->state
== COND_NEVER
))
5131 verror(severity
, fmt
, arg
);
5136 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
5141 istk
= nasm_zalloc(sizeof(Include
));
5142 istk
->fp
= fopen(file
, "r");
5143 src_set_fname(nasm_strdup(file
));
5147 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
5152 nested_mac_count
= 0;
5153 nested_rep_count
= 0;
5156 if (tasm_compatible_mode
)
5157 stdmacpos
= nasm_stdmac
;
5159 stdmacpos
= nasm_stdmac_after_tasm
;
5160 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
5165 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5166 * The caller, however, will also pass in 3 for preprocess-only so
5167 * we can set __PASS__ accordingly.
5169 pass
= apass
> 2 ? 2 : apass
;
5171 dephead
= deptail
= deplist
;
5173 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
5175 strcpy(sl
->str
, file
);
5177 deptail
= &sl
->next
;
5181 * Define the __PASS__ macro. This is defined here unlike
5182 * all the other builtins, because it is special -- it varies between
5185 t
= nasm_zalloc(sizeof(*t
));
5186 make_tok_num(t
, apass
);
5187 define_smacro(NULL
, "__PASS__", true, 0, t
);
5190 static char *pp_getline(void)
5201 * Fetch a tokenized line, either from the expansion
5202 * buffer or from the input file.
5206 while (1) { /* until we get a line we can use */
5208 * Fetch a tokenized line from the expansion buffer
5210 if (istk
->expansion
!= NULL
) {
5211 ei
= istk
->expansion
;
5212 if (ei
->current
!= NULL
) {
5213 if (ei
->emitting
== false) {
5218 ei
->current
= l
->next
;
5220 tline
= copy_Token(l
->first
);
5221 if (((ei
->type
== EXP_REP
) ||
5222 (ei
->type
== EXP_MMACRO
) ||
5223 (ei
->type
== EXP_WHILE
))
5224 && (ei
->def
->nolist
== false)) {
5225 char *p
= detoken(tline
, false);
5226 list
->line(LIST_MACRO
, p
);
5229 if (ei
->linnum
> -1) {
5230 src_set_linnum(src_get_linnum() + 1);
5233 } else if ((ei
->type
== EXP_REP
) &&
5234 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5235 ei
->def
->cur_depth
++;
5236 ei
->current
= ei
->def
->line
;
5239 } else if ((ei
->type
== EXP_WHILE
) &&
5240 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5241 ei
->current
= ei
->def
->line
;
5243 tline
= copy_Token(ei
->current
->first
);
5244 j
= if_condition(tline
, PP_WHILE
);
5246 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
5247 if (j
== COND_IF_TRUE
) {
5248 ei
->current
= ei
->current
->next
;
5249 ei
->def
->cur_depth
++;
5251 ei
->emitting
= false;
5253 ei
->def
->cur_depth
= ei
->def
->max_depth
;
5257 istk
->expansion
= ei
->prev
;
5260 if ((ei
->emitting
== true) &&
5261 (ed
->max_depth
== DEADMAN_LIMIT
) &&
5262 (ed
->cur_depth
== DEADMAN_LIMIT
)
5264 error(ERR_FATAL
, "runaway expansion detected, aborting");
5266 if (ed
->cur_depth
> 0) {
5268 } else if (ed
->type
!= EXP_MMACRO
) {
5269 expansions
= ed
->prev
;
5272 if ((ei
->type
== EXP_REP
) ||
5273 (ei
->type
== EXP_MMACRO
) ||
5274 (ei
->type
== EXP_WHILE
)) {
5275 list
->downlevel(LIST_MACRO
);
5276 if (ei
->type
== EXP_MMACRO
) {
5281 if (ei
->linnum
> -1) {
5282 src_set_linnum(ei
->linnum
);
5290 * Read in line from input and tokenize
5293 if (line
) { /* from the current input file */
5294 line
= prepreproc(line
);
5295 tline
= tokenize(line
);
5301 * The current file has ended; work down the istk
5306 if (i
->expansion
!= NULL
) {
5308 "end of file while still in an expansion");
5310 /* only set line and file name if there's a next node */
5312 src_set_linnum(i
->lineno
);
5313 nasm_free(src_set_fname(nasm_strdup(i
->fname
)));
5315 if ((i
->next
== NULL
) && (finals
!= NULL
)) {
5317 ei
= new_ExpInv(EXP_FINAL
, NULL
);
5318 ei
->emitting
= true;
5319 ei
->current
= finals
;
5320 istk
->expansion
= ei
;
5325 list
->downlevel(LIST_INCLUDE
);
5337 if (defining
== NULL
)
5338 tline
= expand_mmac_params(tline
);
5341 * Check the line to see if it's a preprocessor directive.
5343 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5345 } else if (defining
!= NULL
) {
5347 * We're defining an expansion. We emit nothing at all,
5348 * and just shove the tokenized line on to the definition.
5350 if (defining
->ignoring
== false) {
5351 Line
*l
= new_Line();
5353 if (defining
->line
== NULL
) {
5357 defining
->last
->next
= l
;
5363 defining
->linecount
++;
5365 } else if ((istk
->expansion
!= NULL
) &&
5366 (istk
->expansion
->emitting
!= true)) {
5368 * We're in a non-emitting branch of an expansion.
5369 * Emit nothing at all, not even a blank line: when we
5370 * emerge from the expansion we'll give a line-number
5371 * directive so we keep our place correctly.
5376 tline
= expand_smacro(tline
);
5377 if (expand_mmacro(tline
) != true) {
5379 * De-tokenize the line again, and emit it.
5381 line
= detoken(tline
, true);
5392 static void pp_cleanup(int pass
)
5394 if (defining
!= NULL
) {
5395 error(ERR_NONFATAL
, "end of file while still defining an expansion");
5396 while (defining
!= NULL
) {
5397 ExpDef
*ed
= defining
;
5398 defining
= ed
->prev
;
5403 while (cstk
!= NULL
)
5406 while (istk
!= NULL
) {
5410 nasm_free(i
->fname
);
5411 while (i
->expansion
!= NULL
) {
5412 ExpInv
*ei
= i
->expansion
;
5413 i
->expansion
= ei
->prev
;
5420 nasm_free(src_set_fname(NULL
));
5425 while ((i
= ipath
)) {
5433 void pp_include_path(char *path
)
5435 IncPath
*i
= nasm_zalloc(sizeof(IncPath
));
5438 i
->path
= nasm_strdup(path
);
5450 void pp_pre_include(char *fname
)
5452 Token
*inc
, *space
, *name
;
5455 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5456 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5457 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5465 void pp_pre_define(char *definition
)
5471 equals
= strchr(definition
, '=');
5472 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5473 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5476 space
->next
= tokenize(definition
);
5486 void pp_pre_undefine(char *definition
)
5491 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5492 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5493 space
->next
= tokenize(definition
);
5502 * This function is used to assist with "runtime" preprocessor
5503 * directives, e.g. pp_runtime("%define __BITS__ 64");
5505 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5506 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5509 void pp_runtime(char *definition
)
5513 def
= tokenize(definition
);
5514 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5519 void pp_extra_stdmac(macros_t
*macros
)
5521 extrastdmac
= macros
;
5524 static void make_tok_num(Token
* tok
, int64_t val
)
5527 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5528 tok
->text
= nasm_strdup(numbuf
);
5529 tok
->type
= TOK_NUMBER
;
5532 struct preproc_ops nasmpp
= {