1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro
;
85 typedef struct ExpDef ExpDef
;
86 typedef struct ExpInv ExpInv
;
87 typedef struct Context Context
;
88 typedef struct Token Token
;
89 typedef struct Blocks Blocks
;
90 typedef struct Line Line
;
91 typedef struct Include Include
;
92 typedef struct Cond Cond
;
93 typedef struct IncPath IncPath
;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * The context stack is composed of a linked list of these.
122 struct hash_table localmac
;
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
134 * %define a(x,y) ( (x) & ~(y) )
136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
146 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
147 TOK_PREPROC_ID
, TOK_STRING
,
148 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
150 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
152 TOK_INDIRECT
, /* %[...] */
153 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
157 #define PP_CONCAT_MASK(x) (1 << (x))
159 struct tokseq_match
{
168 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
169 size_t len
; /* scratch length field */
170 } a
; /* Auxiliary data */
171 enum pp_token_type type
;
175 * Expansion definitions are stored as a linked list of
176 * these, which is essentially a container to allow several linked
179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
193 EXP_NONE
= 0, EXP_PREDEF
,
196 EXP_COMMENT
, EXP_FINAL
,
197 EXP_MAX
= INT_MAX
/* Keep compiler from reducing the range */
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
211 ExpDef
*prev
; /* previous definition */
212 ExpDef
*next
; /* next in hash table */
213 enum pp_exp_type type
; /* expansion type */
214 char *name
; /* definition name */
215 int nparam_min
, nparam_max
;
217 bool plus
; /* is the last parameter greedy? */
218 bool nolist
; /* is this expansion listing-inhibited? */
219 Token
*dlist
; /* all defaults as one list */
220 Token
**defaults
; /* parameter default pointers */
221 int ndefs
; /* number of default parameters */
223 int prepend
; /* label prepend state */
227 int linecount
; /* number of lines within expansion */
229 int64_t def_depth
; /* current number of definition pairs deep */
230 int64_t cur_depth
; /* current number of expansions */
231 int64_t max_depth
; /* maximum number of expansions allowed */
233 int state
; /* condition state */
234 bool ignoring
; /* ignoring definition lines */
238 * Store the invocation of an expansion.
240 * The `prev' field is for the `istk->expansion` linked-list.
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
246 ExpInv
*prev
; /* previous invocation */
247 enum pp_exp_type type
; /* expansion type */
248 ExpDef
*def
; /* pointer to expansion definition */
249 char *name
; /* invocation name */
250 Line
*label
; /* pointer to label */
251 char *label_text
; /* pointer to label text */
252 Line
*current
; /* pointer to current line in invocation */
254 Token
**params
; /* actual parameters */
255 Token
*iline
; /* invocation line */
256 unsigned int nparam
, rotate
;
261 int lineno
; /* current line number in expansion */
262 int linnum
; /* line number at invocation */
263 int relno
; /* relative line number at invocation */
267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
306 COND_IF_TRUE
, COND_IF_FALSE
,
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
312 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
322 COND_DONE
, COND_NEVER
324 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
327 * These defines are used as the possible return values for do_directive
329 #define NO_DIRECTIVE_FOUND 0
330 #define DIRECTIVE_FOUND 1
333 * This define sets the upper limit for smacro and expansions
335 #define DEADMAN_LIMIT (1 << 20)
338 #define REP_LIMIT ((INT64_C(1) << 62))
341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
346 static const char * const conditions
[] = {
347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
352 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
353 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
354 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
357 static const enum pp_conds inverse_ccs
[] = {
358 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
359 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
,
360 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
363 /* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
370 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
371 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
374 static const char * const tasm_directives
[] = {
375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
379 static int StackSize
= 4;
380 static char *StackPointer
= "ebp";
381 static int ArgOffset
= 8;
382 static int LocalOffset
= 0;
384 static Context
*cstk
;
385 static Include
*istk
;
386 static IncPath
*ipath
= NULL
;
388 static int pass
; /* HACK: pass 0 = generate dependencies only */
389 static StrList
**dephead
, **deptail
; /* Dependency list */
391 static uint64_t unique
; /* unique identifier numbers */
393 static Line
*predef
= NULL
;
394 static bool do_predef
;
396 static ListGen
*list
;
399 * The current set of expansion definitions we have defined.
401 static struct hash_table expdefs
;
404 * The current set of single-line macros we have defined.
406 static struct hash_table smacros
;
409 * Linked List of all active expansion definitions
411 struct ExpDef
*expansions
= NULL
;
414 * The expansion we are currently defining
416 static ExpDef
*defining
= NULL
;
418 static uint64_t nested_mac_count
;
419 static uint64_t nested_rep_count
;
422 * Linked-list of lines to preprocess, prior to cleanup
424 static Line
*finals
= NULL
;
425 static bool in_final
= false;
428 * The number of macro parameters to allocate space for at a time.
430 #define PARAM_DELTA 16
433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
436 static macros_t
*stdmacpos
;
439 * The extra standard macros that come from the object format, if
442 static macros_t
*extrastdmac
= NULL
;
443 static bool any_extrastdmac
;
446 * Tokens are allocated in blocks to improve speed
448 #define TOKEN_BLOCKSIZE 4096
449 static Token
*freeTokens
= NULL
;
455 static Blocks blocks
= { NULL
, NULL
};
458 * Forward declarations.
460 static Token
*expand_mmac_params(Token
* tline
);
461 static Token
*expand_smacro(Token
* tline
);
462 static Token
*expand_id(Token
* tline
);
463 static Context
*get_ctx(const char *name
, const char **namep
,
465 static void make_tok_num(Token
* tok
, int64_t val
);
466 static void error(int severity
, const char *fmt
, ...);
467 static void error_precond(int severity
, const char *fmt
, ...);
468 static void *new_Block(size_t size
);
469 static void delete_Blocks(void);
470 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
471 const char *text
, int txtlen
);
472 static Token
*copy_Token(Token
* tline
);
473 static Token
*delete_Token(Token
* t
);
474 static Line
*new_Line(void);
475 static ExpDef
*new_ExpDef(int exp_type
);
476 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
);
479 * Macros for safe checking of token pointers, avoid *(NULL)
481 #define tok_type_(x,t) ((x) && (x)->type == (t))
482 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
487 * nasm_unquote with error if the string contains NUL characters.
488 * If the string contains NUL characters, issue an error and return
489 * the C len, i.e. truncate at the NUL.
491 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
493 size_t len
= nasm_unquote(qstr
, NULL
);
494 size_t clen
= strlen(qstr
);
497 error(ERR_NONFATAL
, "NUL character in `%s' directive",
498 pp_directives
[directive
]);
504 * In-place reverse a list of tokens.
506 static Token
*reverse_tokens(Token
*t
)
522 * Handle TASM specific directives, which do not contain a % in
523 * front of them. We do it here because I could not find any other
524 * place to do it for the moment, and it is a hack (ideally it would
525 * be nice to be able to use the NASM pre-processor to do it).
527 static char *check_tasm_directive(char *line
)
529 int32_t i
, j
, k
, m
, len
;
530 char *p
, *q
, *oldline
, oldchar
;
532 p
= nasm_skip_spaces(line
);
534 /* Binary search for the directive name */
536 j
= ARRAY_SIZE(tasm_directives
);
537 q
= nasm_skip_word(p
);
544 m
= nasm_stricmp(p
, tasm_directives
[k
]);
546 /* We have found a directive, so jam a % in front of it
547 * so that NASM will then recognise it as one if it's own.
552 line
= nasm_malloc(len
+ 2);
554 if (k
== TM_IFDIFI
) {
556 * NASM does not recognise IFDIFI, so we convert
557 * it to %if 0. This is not used in NASM
558 * compatible code, but does need to parse for the
559 * TASM macro package.
561 strcpy(line
+ 1, "if 0");
563 memcpy(line
+ 1, p
, len
+ 1);
578 * The pre-preprocessing stage... This function translates line
579 * number indications as they emerge from GNU cpp (`# lineno "file"
580 * flags') into NASM preprocessor line number indications (`%line
583 static char *prepreproc(char *line
)
586 char *fname
, *oldline
;
588 if (line
[0] == '#' && line
[1] == ' ') {
591 lineno
= atoi(fname
);
592 fname
+= strspn(fname
, "0123456789 ");
595 fnlen
= strcspn(fname
, "\"");
596 line
= nasm_malloc(20 + fnlen
);
597 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
600 if (tasm_compatible_mode
)
601 return check_tasm_directive(line
);
606 * Free a linked list of tokens.
608 static void free_tlist(Token
* list
)
611 list
= delete_Token(list
);
615 * Free a linked list of lines.
617 static void free_llist(Line
* list
)
620 list_for_each_safe(l
, tmp
, list
) {
621 free_tlist(l
->first
);
629 static void free_expdef(ExpDef
* ed
)
632 free_tlist(ed
->dlist
);
633 nasm_free(ed
->defaults
);
634 free_llist(ed
->line
);
641 static void free_expinv(ExpInv
* ei
)
643 if (ei
->name
!= NULL
)
645 if (ei
->label_text
!= NULL
)
646 nasm_free(ei
->label_text
);
651 * Free all currently defined macros, and free the hash tables
653 static void free_smacro_table(struct hash_table
*smt
)
657 struct hash_tbl_node
*it
= NULL
;
659 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
660 nasm_free((void *)key
);
661 list_for_each_safe(s
, tmp
, s
) {
663 free_tlist(s
->expansion
);
670 static void free_expdef_table(struct hash_table
*edt
)
674 struct hash_tbl_node
*it
= NULL
;
677 while ((ed
= hash_iterate(edt
, &it
, &key
)) != NULL
) {
678 nasm_free((void *)key
);
679 list_for_each_safe(ed
,tmp
, ed
)
685 static void free_macros(void)
687 free_smacro_table(&smacros
);
688 free_expdef_table(&expdefs
);
692 * Initialize the hash tables
694 static void init_macros(void)
696 hash_init(&smacros
, HASH_LARGE
);
697 hash_init(&expdefs
, HASH_LARGE
);
701 * Pop the context stack.
703 static void ctx_pop(void)
708 free_smacro_table(&c
->localmac
);
714 * Search for a key in the hash index; adding it if necessary
715 * (in which case we initialize the data pointer to NULL.)
718 hash_findi_add(struct hash_table
*hash
, const char *str
)
720 struct hash_insert hi
;
724 r
= hash_findi(hash
, str
, &hi
);
728 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
729 return hash_add(&hi
, strx
, NULL
);
733 * Like hash_findi, but returns the data element rather than a pointer
734 * to it. Used only when not adding a new element, hence no third
738 hash_findix(struct hash_table
*hash
, const char *str
)
742 p
= hash_findi(hash
, str
, NULL
);
743 return p
? *p
: NULL
;
747 * read line from standard macros set,
748 * if there no more left -- return NULL
750 static char *line_from_stdmac(void)
753 const unsigned char *p
= stdmacpos
;
762 len
+= pp_directives_len
[c
- 0x80] + 1;
767 line
= nasm_malloc(len
+ 1);
769 while ((c
= *stdmacpos
++)) {
771 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
772 q
+= pp_directives_len
[c
- 0x80];
782 /* This was the last of the standard macro chain... */
784 if (any_extrastdmac
) {
785 stdmacpos
= extrastdmac
;
786 any_extrastdmac
= false;
787 } else if (do_predef
) {
790 Token
*head
, **tail
, *t
;
793 * Nasty hack: here we push the contents of
794 * `predef' on to the top-level expansion stack,
795 * since this is the most convenient way to
796 * implement the pre-include and pre-define
799 list_for_each(pd
, predef
) {
802 list_for_each(t
, pd
->first
) {
803 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
804 tail
= &(*tail
)->next
;
809 ei
= new_ExpInv(EXP_PREDEF
, NULL
);
812 ei
->prev
= istk
->expansion
;
813 istk
->expansion
= ei
;
822 #define BUF_DELTA 512
824 * Read a line from the top file in istk, handling multiple CR/LFs
825 * at the end of the line read, and handling spurious ^Zs. Will
826 * return lines from the standard macro set if this has not already
829 static char *read_line(void)
831 char *buffer
, *p
, *q
;
832 int bufsize
, continued_count
;
835 * standart macros set (predefined) goes first
837 p
= line_from_stdmac();
842 * regular read from a file
845 buffer
= nasm_malloc(BUF_DELTA
);
849 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
853 if (p
> buffer
&& p
[-1] == '\n') {
855 * Convert backslash-CRLF line continuation sequences into
856 * nothing at all (for DOS and Windows)
858 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
864 * Also convert backslash-LF line continuation sequences into
865 * nothing at all (for Unix)
867 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
875 if (p
- buffer
> bufsize
- 10) {
876 int32_t offset
= p
- buffer
;
877 bufsize
+= BUF_DELTA
;
878 buffer
= nasm_realloc(buffer
, bufsize
);
879 p
= buffer
+ offset
; /* prevent stale-pointer problems */
883 if (!q
&& p
== buffer
) {
888 src_set_linnum(src_get_linnum() + istk
->lineinc
+
889 (continued_count
* istk
->lineinc
));
892 * Play safe: remove CRs as well as LFs, if any of either are
893 * present at the end of the line.
895 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
899 * Handle spurious ^Z, which may be inserted into source files
900 * by some file transfer utilities.
902 buffer
[strcspn(buffer
, "\032")] = '\0';
904 list
->line(LIST_READ
, buffer
);
910 * Tokenize a line of text. This is a very simple process since we
911 * don't need to parse the value out of e.g. numeric tokens: we
912 * simply split one string into many.
914 static Token
*tokenize(char *line
)
917 enum pp_token_type type
;
919 Token
*t
, **tail
= &list
;
925 if (*p
== '+' && !nasm_isdigit(p
[1])) {
928 } else if (nasm_isdigit(*p
) ||
929 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
933 while (nasm_isdigit(*p
));
934 type
= TOK_PREPROC_ID
;
935 } else if (*p
== '{') {
937 while (*p
&& *p
!= '}') {
944 type
= TOK_PREPROC_ID
;
945 } else if (*p
== '[') {
947 line
+= 2; /* Skip the leading %[ */
949 while (lvl
&& (c
= *p
++)) {
961 p
= nasm_skip_string(p
- 1) + 1;
971 error(ERR_NONFATAL
, "unterminated %[ construct");
973 } else if (*p
== '?') {
974 type
= TOK_PREPROC_Q
; /* %? */
977 type
= TOK_PREPROC_QQ
; /* %?? */
980 } else if (*p
== '!') {
981 type
= TOK_PREPROC_ID
;
986 } while (isidchar(*p
));
987 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
988 p
= nasm_skip_string(p
);
992 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
994 /* %! without string or identifier */
995 type
= TOK_OTHER
; /* Legacy behavior... */
997 } else if (isidchar(*p
) ||
998 ((*p
== '!' || *p
== '%' || *p
== '$') &&
1003 while (isidchar(*p
));
1004 type
= TOK_PREPROC_ID
;
1010 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
1013 while (*p
&& isidchar(*p
))
1015 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1020 p
= nasm_skip_string(p
);
1025 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1026 /* Handling unterminated strings by UNV */
1029 } else if (p
[0] == '$' && p
[1] == '$') {
1030 type
= TOK_OTHER
; /* TOKEN_BASE */
1032 } else if (isnumstart(*p
)) {
1033 bool is_hex
= false;
1034 bool is_float
= false;
1050 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1052 if (*p
== '+' || *p
== '-') {
1054 * e can only be followed by +/- if it is either a
1055 * prefixed hex number or a floating-point number
1060 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1062 } else if (c
== 'P' || c
== 'p') {
1064 if (*p
== '+' || *p
== '-')
1066 } else if (isnumchar(c
) || c
== '_')
1067 ; /* just advance */
1068 else if (c
== '.') {
1070 * we need to deal with consequences of the legacy
1071 * parser, like "1.nolist" being two tokens
1072 * (TOK_NUMBER, TOK_ID) here; at least give it
1073 * a shot for now. In the future, we probably need
1074 * a flex-based scanner with proper pattern matching
1075 * to do it as well as it can be done. Nothing in
1076 * the world is going to help the person who wants
1077 * 0x123.p16 interpreted as two tokens, though.
1083 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1084 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1085 (*r
== 'p' || *r
== 'P')) {
1089 break; /* Terminate the token */
1093 p
--; /* Point to first character beyond number */
1095 if (p
== line
+1 && *line
== '$') {
1096 type
= TOK_OTHER
; /* TOKEN_HERE */
1098 if (has_e
&& !is_hex
) {
1099 /* 1e13 is floating-point, but 1e13h is not */
1103 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1105 } else if (nasm_isspace(*p
)) {
1106 type
= TOK_WHITESPACE
;
1107 p
= nasm_skip_spaces(p
);
1109 * Whitespace just before end-of-line is discarded by
1110 * pretending it's a comment; whitespace just before a
1111 * comment gets lumped into the comment.
1113 if (!*p
|| *p
== ';') {
1118 } else if (*p
== ';') {
1124 * Anything else is an operator of some kind. We check
1125 * for all the double-character operators (>>, <<, //,
1126 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1127 * else is a single-character operator.
1130 if ((p
[0] == '>' && p
[1] == '>') ||
1131 (p
[0] == '<' && p
[1] == '<') ||
1132 (p
[0] == '/' && p
[1] == '/') ||
1133 (p
[0] == '<' && p
[1] == '=') ||
1134 (p
[0] == '>' && p
[1] == '=') ||
1135 (p
[0] == '=' && p
[1] == '=') ||
1136 (p
[0] == '!' && p
[1] == '=') ||
1137 (p
[0] == '<' && p
[1] == '>') ||
1138 (p
[0] == '&' && p
[1] == '&') ||
1139 (p
[0] == '|' && p
[1] == '|') ||
1140 (p
[0] == '^' && p
[1] == '^')) {
1146 /* Handling unterminated string by UNV */
1149 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1150 t->text[p-line] = *line;
1154 if (type
!= TOK_COMMENT
) {
1155 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1164 * this function allocates a new managed block of memory and
1165 * returns a pointer to the block. The managed blocks are
1166 * deleted only all at once by the delete_Blocks function.
1168 static void *new_Block(size_t size
)
1170 Blocks
*b
= &blocks
;
1172 /* first, get to the end of the linked list */
1175 /* now allocate the requested chunk */
1176 b
->chunk
= nasm_malloc(size
);
1178 /* now allocate a new block for the next request */
1179 b
->next
= nasm_malloc(sizeof(Blocks
));
1180 /* and initialize the contents of the new block */
1181 b
->next
->next
= NULL
;
1182 b
->next
->chunk
= NULL
;
1187 * this function deletes all managed blocks of memory
1189 static void delete_Blocks(void)
1191 Blocks
*a
, *b
= &blocks
;
1194 * keep in mind that the first block, pointed to by blocks
1195 * is a static and not dynamically allocated, so we don't
1200 nasm_free(b
->chunk
);
1209 * this function creates a new Token and passes a pointer to it
1210 * back to the caller. It sets the type and text elements, and
1211 * also the a.mac and next elements to NULL.
1213 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1214 const char *text
, int txtlen
)
1220 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1221 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1222 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1223 freeTokens
[i
].next
= NULL
;
1226 freeTokens
= t
->next
;
1230 if (type
== TOK_WHITESPACE
|| !text
) {
1234 txtlen
= strlen(text
);
1235 t
->text
= nasm_malloc(txtlen
+1);
1236 memcpy(t
->text
, text
, txtlen
);
1237 t
->text
[txtlen
] = '\0';
1242 static Token
*copy_Token(Token
* tline
)
1244 Token
*t
, *tt
, *first
= NULL
, *prev
= NULL
;
1246 for (tt
= tline
; tt
!= NULL
; tt
= tt
->next
) {
1248 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1249 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1250 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1251 freeTokens
[i
].next
= NULL
;
1254 freeTokens
= t
->next
;
1256 t
->text
= tt
->text
? nasm_strdup(tt
->text
) : NULL
;
1257 t
->a
.mac
= tt
->a
.mac
;
1258 t
->a
.len
= tt
->a
.len
;
1270 static Token
*delete_Token(Token
* t
)
1272 Token
*next
= t
->next
;
1274 t
->next
= freeTokens
;
1280 * Convert a line of tokens back into text.
1281 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1282 * will be transformed into ..@ctxnum.xxx
1284 static char *detoken(Token
* tlist
, bool expand_locals
)
1291 list_for_each(t
, tlist
) {
1292 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1297 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1298 size_t len
= nasm_unquote(v
, NULL
);
1299 size_t clen
= strlen(v
);
1302 error(ERR_NONFATAL
| ERR_PASS1
,
1303 "NUL character in %! string");
1309 char *p
= getenv(v
);
1311 error(ERR_NONFATAL
| ERR_PASS1
,
1312 "nonexistent environment variable `%s'", v
);
1315 t
->text
= nasm_strdup(p
);
1320 /* Expand local macros here and not during preprocessing */
1321 if (expand_locals
&&
1322 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1323 t
->text
[0] == '%' && t
->text
[1] == '$') {
1326 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1329 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1330 p
= nasm_strcat(buffer
, q
);
1336 /* Expand %? and %?? directives */
1337 if (expand_locals
&& (istk
->expansion
!= NULL
) &&
1338 ((t
->type
== TOK_PREPROC_Q
) ||
1339 (t
->type
== TOK_PREPROC_QQ
))) {
1341 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
){
1342 if (ei
->type
== EXP_MMACRO
) {
1344 if (t
->type
== TOK_PREPROC_Q
) {
1345 t
->text
= nasm_strdup(ei
->name
);
1347 t
->text
= nasm_strdup(ei
->def
->name
);
1354 if (t
->type
== TOK_WHITESPACE
)
1357 len
+= strlen(t
->text
);
1360 p
= line
= nasm_malloc(len
+ 1);
1362 list_for_each(t
, tlist
) {
1363 if (t
->type
== TOK_WHITESPACE
) {
1365 } else if (t
->text
) {
1377 * Initialize a new Line
1379 static inline Line
*new_Line(void)
1381 return (Line
*)nasm_zalloc(sizeof(Line
));
1386 * Initialize a new Expansion Definition
1388 static ExpDef
*new_ExpDef(int exp_type
)
1390 ExpDef
*ed
= (ExpDef
*)nasm_zalloc(sizeof(ExpDef
));
1391 ed
->type
= exp_type
;
1392 ed
->casesense
= true;
1393 ed
->state
= COND_NEVER
;
1400 * Initialize a new Expansion Instance
1402 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
)
1404 ExpInv
*ei
= (ExpInv
*)nasm_zalloc(sizeof(ExpInv
));
1405 ei
->type
= exp_type
;
1407 ei
->unique
= ++unique
;
1409 if ((istk
->mmac_depth
< 1) &&
1410 (istk
->expansion
== NULL
) &&
1412 (ed
->type
!= EXP_MMACRO
) &&
1413 (ed
->type
!= EXP_REP
) &&
1414 (ed
->type
!= EXP_WHILE
)) {
1415 ei
->linnum
= src_get_linnum();
1416 src_set_linnum(ei
->linnum
- ed
->linecount
- 1);
1420 if ((istk
->expansion
== NULL
) ||
1421 (ei
->type
== EXP_MMACRO
)) {
1424 ei
->relno
= istk
->expansion
->lineno
;
1426 ei
->relno
-= (ed
->linecount
+ 1);
1433 * A scanner, suitable for use by the expression evaluator, which
1434 * operates on a line of Tokens. Expects a pointer to a pointer to
1435 * the first token in the line to be passed in as its private_data
1438 * FIX: This really needs to be unified with stdscan.
1440 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1442 Token
**tlineptr
= private_data
;
1444 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1448 *tlineptr
= tline
? tline
->next
: NULL
;
1449 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1450 tline
->type
== TOK_COMMENT
));
1453 return tokval
->t_type
= TOKEN_EOS
;
1455 tokval
->t_charptr
= tline
->text
;
1457 if (tline
->text
[0] == '$' && !tline
->text
[1])
1458 return tokval
->t_type
= TOKEN_HERE
;
1459 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1460 return tokval
->t_type
= TOKEN_BASE
;
1462 if (tline
->type
== TOK_ID
) {
1463 p
= tokval
->t_charptr
= tline
->text
;
1465 tokval
->t_charptr
++;
1466 return tokval
->t_type
= TOKEN_ID
;
1469 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1470 if (r
>= p
+MAX_KEYWORD
)
1471 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1472 *s
++ = nasm_tolower(*r
);
1475 /* right, so we have an identifier sitting in temp storage. now,
1476 * is it actually a register or instruction name, or what? */
1477 return nasm_token_hash(ourcopy
, tokval
);
1480 if (tline
->type
== TOK_NUMBER
) {
1482 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1483 tokval
->t_charptr
= tline
->text
;
1485 return tokval
->t_type
= TOKEN_ERRNUM
;
1487 return tokval
->t_type
= TOKEN_NUM
;
1490 if (tline
->type
== TOK_FLOAT
) {
1491 return tokval
->t_type
= TOKEN_FLOAT
;
1494 if (tline
->type
== TOK_STRING
) {
1497 bq
= tline
->text
[0];
1498 tokval
->t_charptr
= tline
->text
;
1499 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1501 if (ep
[0] != bq
|| ep
[1] != '\0')
1502 return tokval
->t_type
= TOKEN_ERRSTR
;
1504 return tokval
->t_type
= TOKEN_STR
;
1507 if (tline
->type
== TOK_OTHER
) {
1508 if (!strcmp(tline
->text
, "<<"))
1509 return tokval
->t_type
= TOKEN_SHL
;
1510 if (!strcmp(tline
->text
, ">>"))
1511 return tokval
->t_type
= TOKEN_SHR
;
1512 if (!strcmp(tline
->text
, "//"))
1513 return tokval
->t_type
= TOKEN_SDIV
;
1514 if (!strcmp(tline
->text
, "%%"))
1515 return tokval
->t_type
= TOKEN_SMOD
;
1516 if (!strcmp(tline
->text
, "=="))
1517 return tokval
->t_type
= TOKEN_EQ
;
1518 if (!strcmp(tline
->text
, "<>"))
1519 return tokval
->t_type
= TOKEN_NE
;
1520 if (!strcmp(tline
->text
, "!="))
1521 return tokval
->t_type
= TOKEN_NE
;
1522 if (!strcmp(tline
->text
, "<="))
1523 return tokval
->t_type
= TOKEN_LE
;
1524 if (!strcmp(tline
->text
, ">="))
1525 return tokval
->t_type
= TOKEN_GE
;
1526 if (!strcmp(tline
->text
, "&&"))
1527 return tokval
->t_type
= TOKEN_DBL_AND
;
1528 if (!strcmp(tline
->text
, "^^"))
1529 return tokval
->t_type
= TOKEN_DBL_XOR
;
1530 if (!strcmp(tline
->text
, "||"))
1531 return tokval
->t_type
= TOKEN_DBL_OR
;
1535 * We have no other options: just return the first character of
1538 return tokval
->t_type
= tline
->text
[0];
1542 * Compare a string to the name of an existing macro; this is a
1543 * simple wrapper which calls either strcmp or nasm_stricmp
1544 * depending on the value of the `casesense' parameter.
1546 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1548 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1552 * Compare a string to the name of an existing macro; this is a
1553 * simple wrapper which calls either strcmp or nasm_stricmp
1554 * depending on the value of the `casesense' parameter.
1556 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1558 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1562 * Return the Context structure associated with a %$ token. Return
1563 * NULL, having _already_ reported an error condition, if the
1564 * context stack isn't deep enough for the supplied number of $
1566 * If all_contexts == true, contexts that enclose current are
1567 * also scanned for such smacro, until it is found; if not -
1568 * only the context that directly results from the number of $'s
1569 * in variable's name.
1571 * If "namep" is non-NULL, set it to the pointer to the macro name
1572 * tail, i.e. the part beyond %$...
1574 static Context
*get_ctx(const char *name
, const char **namep
,
1584 if (!name
|| name
[0] != '%' || name
[1] != '$')
1588 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1595 while (ctx
&& *name
== '$') {
1601 error(ERR_NONFATAL
, "`%s': context stack is only"
1602 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1613 /* Search for this smacro in found context */
1614 m
= hash_findix(&ctx
->localmac
, name
);
1616 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1627 * Check to see if a file is already in a string list
1629 static bool in_list(const StrList
*list
, const char *str
)
1632 if (!strcmp(list
->str
, str
))
1640 * Open an include file. This routine must always return a valid
1641 * file pointer if it returns - it's responsible for throwing an
1642 * ERR_FATAL and bombing out completely if not. It should also try
1643 * the include path one by one until it finds the file or reaches
1644 * the end of the path.
1646 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1651 IncPath
*ip
= ipath
;
1652 int len
= strlen(file
);
1653 size_t prefix_len
= 0;
1657 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1659 memcpy(sl
->str
, prefix
, prefix_len
);
1660 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1661 fp
= fopen(sl
->str
, "r");
1662 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1679 prefix_len
= strlen(prefix
);
1681 /* -MG given and file not found */
1682 if (dhead
&& !in_list(*dhead
, file
)) {
1683 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1685 strcpy(sl
->str
, file
);
1693 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1698 * Determine if we should warn on defining a single-line macro of
1699 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1700 * return true if _any_ single-line macro of that name is defined.
1701 * Otherwise, will return true if a single-line macro with either
1702 * `nparam' or no parameters is defined.
1704 * If a macro with precisely the right number of parameters is
1705 * defined, or nparam is -1, the address of the definition structure
1706 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1707 * is NULL, no action will be taken regarding its contents, and no
1710 * Note that this is also called with nparam zero to resolve
1713 * If you already know which context macro belongs to, you can pass
1714 * the context pointer as first parameter; if you won't but name begins
1715 * with %$ the context will be automatically computed. If all_contexts
1716 * is true, macro will be searched in outer contexts as well.
1719 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1722 struct hash_table
*smtbl
;
1726 smtbl
= &ctx
->localmac
;
1727 } else if (name
[0] == '%' && name
[1] == '$') {
1729 ctx
= get_ctx(name
, &name
, false);
1731 return false; /* got to return _something_ */
1732 smtbl
= &ctx
->localmac
;
1736 m
= (SMacro
*) hash_findix(smtbl
, name
);
1739 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1740 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1742 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1756 * Count and mark off the parameters in a multi-line macro call.
1757 * This is called both from within the multi-line macro expansion
1758 * code, and also to mark off the default parameters when provided
1759 * in a %macro definition line.
1761 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1763 int paramsize
, brace
;
1765 *nparam
= paramsize
= 0;
1768 /* +1: we need space for the final NULL */
1769 if (*nparam
+1 >= paramsize
) {
1770 paramsize
+= PARAM_DELTA
;
1771 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1775 if (tok_is_(t
, "{"))
1777 (*params
)[(*nparam
)++] = t
;
1778 while (tok_isnt_(t
, brace
? "}" : ","))
1780 if (t
) { /* got a comma/brace */
1784 * Now we've found the closing brace, look further
1788 if (tok_isnt_(t
, ",")) {
1790 "braces do not enclose all of macro parameter");
1791 while (tok_isnt_(t
, ","))
1795 t
= t
->next
; /* eat the comma */
1802 * Determine whether one of the various `if' conditions is true or
1805 * We must free the tline we get passed.
1807 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1809 enum pp_conditional i
= PP_COND(ct
);
1811 Token
*t
, *tt
, **tptr
, *origline
;
1812 struct tokenval tokval
;
1814 enum pp_token_type needtype
;
1821 j
= false; /* have we matched yet? */
1826 if (tline
->type
!= TOK_ID
) {
1828 "`%s' expects context identifiers", pp_directives
[ct
]);
1829 free_tlist(origline
);
1832 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1834 tline
= tline
->next
;
1839 j
= false; /* have we matched yet? */
1842 if (!tline
|| (tline
->type
!= TOK_ID
&&
1843 (tline
->type
!= TOK_PREPROC_ID
||
1844 tline
->text
[1] != '$'))) {
1846 "`%s' expects macro identifiers", pp_directives
[ct
]);
1849 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1851 tline
= tline
->next
;
1856 tline
= expand_smacro(tline
);
1857 j
= false; /* have we matched yet? */
1860 if (!tline
|| (tline
->type
!= TOK_ID
&&
1861 tline
->type
!= TOK_STRING
&&
1862 (tline
->type
!= TOK_PREPROC_ID
||
1863 tline
->text
[1] != '!'))) {
1865 "`%s' expects environment variable names",
1870 if (tline
->type
== TOK_PREPROC_ID
)
1871 p
+= 2; /* Skip leading %! */
1872 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1873 nasm_unquote_cstr(p
, ct
);
1876 tline
= tline
->next
;
1882 tline
= expand_smacro(tline
);
1884 while (tok_isnt_(tt
, ","))
1888 "`%s' expects two comma-separated arguments",
1893 j
= true; /* assume equality unless proved not */
1894 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1895 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1896 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1900 if (t
->type
== TOK_WHITESPACE
) {
1904 if (tt
->type
== TOK_WHITESPACE
) {
1908 if (tt
->type
!= t
->type
) {
1909 j
= false; /* found mismatching tokens */
1912 /* When comparing strings, need to unquote them first */
1913 if (t
->type
== TOK_STRING
) {
1914 size_t l1
= nasm_unquote(t
->text
, NULL
);
1915 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1921 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1925 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1926 j
= false; /* found mismatching tokens */
1933 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1934 j
= false; /* trailing gunk on one end or other */
1940 ExpDef searching
, *ed
;
1943 tline
= expand_id(tline
);
1944 if (!tok_type_(tline
, TOK_ID
)) {
1946 "`%s' expects a macro name", pp_directives
[ct
]);
1949 memset(&searching
, 0, sizeof(searching
));
1950 searching
.name
= nasm_strdup(tline
->text
);
1951 searching
.casesense
= true;
1952 searching
.nparam_max
= INT_MAX
;
1953 tline
= expand_smacro(tline
->next
);
1956 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1958 "`%s' expects a parameter count or nothing",
1961 searching
.nparam_min
= searching
.nparam_max
=
1962 readnum(tline
->text
, &j
);
1965 "unable to parse parameter count `%s'",
1968 if (tline
&& tok_is_(tline
->next
, "-")) {
1969 tline
= tline
->next
->next
;
1970 if (tok_is_(tline
, "*"))
1971 searching
.nparam_max
= INT_MAX
;
1972 else if (!tok_type_(tline
, TOK_NUMBER
))
1974 "`%s' expects a parameter count after `-'",
1977 searching
.nparam_max
= readnum(tline
->text
, &j
);
1980 "unable to parse parameter count `%s'",
1982 if (searching
.nparam_min
> searching
.nparam_max
)
1984 "minimum parameter count exceeds maximum");
1987 if (tline
&& tok_is_(tline
->next
, "+")) {
1988 tline
= tline
->next
;
1989 searching
.plus
= true;
1991 ed
= (ExpDef
*) hash_findix(&expdefs
, searching
.name
);
1992 while (ed
!= NULL
) {
1993 if (!strcmp(ed
->name
, searching
.name
) &&
1994 (ed
->nparam_min
<= searching
.nparam_max
|| searching
.plus
) &&
1995 (searching
.nparam_min
<= ed
->nparam_max
|| ed
->plus
)) {
2001 if (tline
&& tline
->next
)
2002 error(ERR_WARNING
|ERR_PASS1
,
2003 "trailing garbage after %%ifmacro ignored");
2004 nasm_free(searching
.name
);
2013 needtype
= TOK_NUMBER
;
2016 needtype
= TOK_STRING
;
2020 t
= tline
= expand_smacro(tline
);
2022 while (tok_type_(t
, TOK_WHITESPACE
) ||
2023 (needtype
== TOK_NUMBER
&&
2024 tok_type_(t
, TOK_OTHER
) &&
2025 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
2029 j
= tok_type_(t
, needtype
);
2033 t
= tline
= expand_smacro(tline
);
2034 while (tok_type_(t
, TOK_WHITESPACE
))
2039 t
= t
->next
; /* Skip the actual token */
2040 while (tok_type_(t
, TOK_WHITESPACE
))
2042 j
= !t
; /* Should be nothing left */
2047 t
= tline
= expand_smacro(tline
);
2048 while (tok_type_(t
, TOK_WHITESPACE
))
2051 j
= !t
; /* Should be empty */
2055 t
= tline
= expand_smacro(tline
);
2057 tokval
.t_type
= TOKEN_INVALID
;
2058 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2059 NULL
, pass
| CRITICAL
, error
, NULL
);
2063 error(ERR_WARNING
|ERR_PASS1
,
2064 "trailing garbage after expression ignored");
2065 if (!is_simple(evalresult
)) {
2067 "non-constant value given to `%s'", pp_directives
[ct
]);
2070 j
= reloc_value(evalresult
) != 0;
2075 "preprocessor directive `%s' not yet implemented",
2080 free_tlist(origline
);
2081 return j
^ PP_NEGATIVE(ct
);
2084 free_tlist(origline
);
2089 * Common code for defining an smacro
2091 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2092 int nparam
, Token
*expansion
)
2094 SMacro
*smac
, **smhead
;
2095 struct hash_table
*smtbl
;
2097 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2099 error(ERR_WARNING
|ERR_PASS1
,
2100 "single-line macro `%s' defined both with and"
2101 " without parameters", mname
);
2103 * Some instances of the old code considered this a failure,
2104 * some others didn't. What is the right thing to do here?
2106 free_tlist(expansion
);
2107 return false; /* Failure */
2110 * We're redefining, so we have to take over an
2111 * existing SMacro structure. This means freeing
2112 * what was already in it.
2114 nasm_free(smac
->name
);
2115 free_tlist(smac
->expansion
);
2118 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2119 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2120 smac
= nasm_malloc(sizeof(SMacro
));
2121 smac
->next
= *smhead
;
2124 smac
->name
= nasm_strdup(mname
);
2125 smac
->casesense
= casesense
;
2126 smac
->nparam
= nparam
;
2127 smac
->expansion
= expansion
;
2128 smac
->in_progress
= false;
2129 return true; /* Success */
2133 * Undefine an smacro
2135 static void undef_smacro(Context
*ctx
, const char *mname
)
2137 SMacro
**smhead
, *s
, **sp
;
2138 struct hash_table
*smtbl
;
2140 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2141 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2145 * We now have a macro name... go hunt for it.
2148 while ((s
= *sp
) != NULL
) {
2149 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2152 free_tlist(s
->expansion
);
2162 * Parse a mmacro specification.
2164 static bool parse_mmacro_spec(Token
*tline
, ExpDef
*def
, const char *directive
)
2168 tline
= tline
->next
;
2170 tline
= expand_id(tline
);
2171 if (!tok_type_(tline
, TOK_ID
)) {
2172 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2176 def
->name
= nasm_strdup(tline
->text
);
2178 def
->nolist
= false;
2179 // def->in_progress = 0;
2180 // def->rep_nest = NULL;
2181 def
->nparam_min
= 0;
2182 def
->nparam_max
= 0;
2184 tline
= expand_smacro(tline
->next
);
2186 if (!tok_type_(tline
, TOK_NUMBER
)) {
2187 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2189 def
->nparam_min
= def
->nparam_max
=
2190 readnum(tline
->text
, &err
);
2193 "unable to parse parameter count `%s'", tline
->text
);
2195 if (tline
&& tok_is_(tline
->next
, "-")) {
2196 tline
= tline
->next
->next
;
2197 if (tok_is_(tline
, "*")) {
2198 def
->nparam_max
= INT_MAX
;
2199 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2201 "`%s' expects a parameter count after `-'", directive
);
2203 def
->nparam_max
= readnum(tline
->text
, &err
);
2205 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2208 if (def
->nparam_min
> def
->nparam_max
) {
2209 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2213 if (tline
&& tok_is_(tline
->next
, "+")) {
2214 tline
= tline
->next
;
2217 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2218 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2219 tline
= tline
->next
;
2224 * Handle default parameters.
2226 if (tline
&& tline
->next
) {
2227 def
->dlist
= tline
->next
;
2229 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2232 def
->defaults
= NULL
;
2236 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2238 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2239 "too many default macro parameters");
2246 * Decode a size directive
2248 static int parse_size(const char *str
) {
2249 static const char *size_names
[] =
2250 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2251 static const int sizes
[] =
2252 { 0, 1, 4, 16, 8, 10, 2, 32 };
2254 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2258 * find and process preprocessor directive in passed line
2259 * Find out if a line contains a preprocessor directive, and deal
2262 * If a directive _is_ found, it is the responsibility of this routine
2263 * (and not the caller) to free_tlist() the line.
2265 * @param tline a pointer to the current tokeninzed line linked list
2266 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2269 static int do_directive(Token
* tline
)
2271 enum preproc_token i
;
2284 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2285 struct tokenval tokval
;
2287 ExpDef
*ed
, *eed
, **edhead
;
2296 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2297 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2298 || tline
->text
[1] == '!'))
2299 return NO_DIRECTIVE_FOUND
;
2301 i
= pp_token_hash(tline
->text
);
2305 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2306 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2308 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2311 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2312 /* Directive to tell NASM what the default stack size is. The
2313 * default is for a 16-bit stack, and this can be overriden with
2316 tline
= tline
->next
;
2317 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2318 tline
= tline
->next
;
2319 if (!tline
|| tline
->type
!= TOK_ID
) {
2320 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2321 free_tlist(origline
);
2322 return DIRECTIVE_FOUND
;
2324 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2325 /* All subsequent ARG directives are for a 32-bit stack */
2327 StackPointer
= "ebp";
2330 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2331 /* All subsequent ARG directives are for a 64-bit stack */
2333 StackPointer
= "rbp";
2336 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2337 /* All subsequent ARG directives are for a 16-bit stack,
2338 * far function call.
2341 StackPointer
= "bp";
2344 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2345 /* All subsequent ARG directives are for a 16-bit stack,
2346 * far function call. We don't support near functions.
2349 StackPointer
= "bp";
2353 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2354 free_tlist(origline
);
2355 return DIRECTIVE_FOUND
;
2357 free_tlist(origline
);
2358 return DIRECTIVE_FOUND
;
2361 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2362 /* TASM like ARG directive to define arguments to functions, in
2363 * the following form:
2365 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2369 char *arg
, directive
[256];
2370 int size
= StackSize
;
2372 /* Find the argument name */
2373 tline
= tline
->next
;
2374 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2375 tline
= tline
->next
;
2376 if (!tline
|| tline
->type
!= TOK_ID
) {
2377 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2378 free_tlist(origline
);
2379 return DIRECTIVE_FOUND
;
2383 /* Find the argument size type */
2384 tline
= tline
->next
;
2385 if (!tline
|| tline
->type
!= TOK_OTHER
2386 || tline
->text
[0] != ':') {
2388 "Syntax error processing `%%arg' directive");
2389 free_tlist(origline
);
2390 return DIRECTIVE_FOUND
;
2392 tline
= tline
->next
;
2393 if (!tline
|| tline
->type
!= TOK_ID
) {
2394 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2395 free_tlist(origline
);
2396 return DIRECTIVE_FOUND
;
2399 /* Allow macro expansion of type parameter */
2400 tt
= tokenize(tline
->text
);
2401 tt
= expand_smacro(tt
);
2402 size
= parse_size(tt
->text
);
2405 "Invalid size type for `%%arg' missing directive");
2407 free_tlist(origline
);
2408 return DIRECTIVE_FOUND
;
2412 /* Round up to even stack slots */
2413 size
= ALIGN(size
, StackSize
);
2415 /* Now define the macro for the argument */
2416 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2417 arg
, StackPointer
, offset
);
2418 do_directive(tokenize(directive
));
2421 /* Move to the next argument in the list */
2422 tline
= tline
->next
;
2423 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2424 tline
= tline
->next
;
2425 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2427 free_tlist(origline
);
2428 return DIRECTIVE_FOUND
;
2431 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2432 /* TASM like LOCAL directive to define local variables for a
2433 * function, in the following form:
2435 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2437 * The '= LocalSize' at the end is ignored by NASM, but is
2438 * required by TASM to define the local parameter size (and used
2439 * by the TASM macro package).
2441 offset
= LocalOffset
;
2443 char *local
, directive
[256];
2444 int size
= StackSize
;
2446 /* Find the argument name */
2447 tline
= tline
->next
;
2448 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2449 tline
= tline
->next
;
2450 if (!tline
|| tline
->type
!= TOK_ID
) {
2452 "`%%local' missing argument parameter");
2453 free_tlist(origline
);
2454 return DIRECTIVE_FOUND
;
2456 local
= tline
->text
;
2458 /* Find the argument size type */
2459 tline
= tline
->next
;
2460 if (!tline
|| tline
->type
!= TOK_OTHER
2461 || tline
->text
[0] != ':') {
2463 "Syntax error processing `%%local' directive");
2464 free_tlist(origline
);
2465 return DIRECTIVE_FOUND
;
2467 tline
= tline
->next
;
2468 if (!tline
|| tline
->type
!= TOK_ID
) {
2470 "`%%local' missing size type parameter");
2471 free_tlist(origline
);
2472 return DIRECTIVE_FOUND
;
2475 /* Allow macro expansion of type parameter */
2476 tt
= tokenize(tline
->text
);
2477 tt
= expand_smacro(tt
);
2478 size
= parse_size(tt
->text
);
2481 "Invalid size type for `%%local' missing directive");
2483 free_tlist(origline
);
2484 return DIRECTIVE_FOUND
;
2488 /* Round up to even stack slots */
2489 size
= ALIGN(size
, StackSize
);
2491 offset
+= size
; /* Negative offset, increment before */
2493 /* Now define the macro for the argument */
2494 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2495 local
, StackPointer
, offset
);
2496 do_directive(tokenize(directive
));
2498 /* Now define the assign to setup the enter_c macro correctly */
2499 snprintf(directive
, sizeof(directive
),
2500 "%%assign %%$localsize %%$localsize+%d", size
);
2501 do_directive(tokenize(directive
));
2503 /* Move to the next argument in the list */
2504 tline
= tline
->next
;
2505 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2506 tline
= tline
->next
;
2507 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2508 LocalOffset
= offset
;
2509 free_tlist(origline
);
2510 return DIRECTIVE_FOUND
;
2513 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2515 error(ERR_WARNING
|ERR_PASS1
,
2516 "trailing garbage after `%%clear' ignored");
2519 free_tlist(origline
);
2520 return DIRECTIVE_FOUND
;
2523 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2524 t
= tline
->next
= expand_smacro(tline
->next
);
2526 if (!t
|| (t
->type
!= TOK_STRING
&&
2527 t
->type
!= TOK_INTERNAL_STRING
)) {
2528 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2529 free_tlist(origline
);
2530 return DIRECTIVE_FOUND
; /* but we did _something_ */
2533 error(ERR_WARNING
|ERR_PASS1
,
2534 "trailing garbage after `%%depend' ignored");
2536 if (t
->type
!= TOK_INTERNAL_STRING
)
2537 nasm_unquote_cstr(p
, i
);
2538 if (dephead
&& !in_list(*dephead
, p
)) {
2539 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2543 deptail
= &sl
->next
;
2545 free_tlist(origline
);
2546 return DIRECTIVE_FOUND
;
2549 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2550 t
= tline
->next
= expand_smacro(tline
->next
);
2553 if (!t
|| (t
->type
!= TOK_STRING
&&
2554 t
->type
!= TOK_INTERNAL_STRING
)) {
2555 error(ERR_NONFATAL
, "`%%include' expects a file name");
2556 free_tlist(origline
);
2557 return DIRECTIVE_FOUND
; /* but we did _something_ */
2560 error(ERR_WARNING
|ERR_PASS1
,
2561 "trailing garbage after `%%include' ignored");
2563 if (t
->type
!= TOK_INTERNAL_STRING
)
2564 nasm_unquote_cstr(p
, i
);
2565 inc
= nasm_zalloc(sizeof(Include
));
2567 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2569 /* -MG given but file not found */
2572 inc
->fname
= src_set_fname(nasm_strdup(p
));
2573 inc
->lineno
= src_set_linnum(0);
2575 inc
->expansion
= NULL
;
2577 list
->uplevel(LIST_INCLUDE
);
2579 free_tlist(origline
);
2580 return DIRECTIVE_FOUND
;
2583 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2585 static macros_t
*use_pkg
;
2586 const char *pkg_macro
= NULL
;
2588 tline
= tline
->next
;
2590 tline
= expand_id(tline
);
2592 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2593 tline
->type
!= TOK_INTERNAL_STRING
&&
2594 tline
->type
!= TOK_ID
)) {
2595 error(ERR_NONFATAL
, "`%%use' expects a package name");
2596 free_tlist(origline
);
2597 return DIRECTIVE_FOUND
; /* but we did _something_ */
2600 error(ERR_WARNING
|ERR_PASS1
,
2601 "trailing garbage after `%%use' ignored");
2602 if (tline
->type
== TOK_STRING
)
2603 nasm_unquote_cstr(tline
->text
, i
);
2604 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2606 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2608 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2609 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2610 /* Not already included, go ahead and include it */
2611 stdmacpos
= use_pkg
;
2613 free_tlist(origline
);
2614 return DIRECTIVE_FOUND
;
2619 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2620 tline
= tline
->next
;
2622 tline
= expand_id(tline
);
2624 if (!tok_type_(tline
, TOK_ID
)) {
2625 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2627 free_tlist(origline
);
2628 return DIRECTIVE_FOUND
; /* but we did _something_ */
2631 error(ERR_WARNING
|ERR_PASS1
,
2632 "trailing garbage after `%s' ignored",
2634 p
= nasm_strdup(tline
->text
);
2636 p
= NULL
; /* Anonymous */
2640 ctx
= nasm_malloc(sizeof(Context
));
2642 hash_init(&ctx
->localmac
, HASH_SMALL
);
2644 ctx
->number
= unique
++;
2649 error(ERR_NONFATAL
, "`%s': context stack is empty",
2651 } else if (i
== PP_POP
) {
2652 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2653 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2655 cstk
->name
? cstk
->name
: "anonymous", p
);
2660 nasm_free(cstk
->name
);
2666 free_tlist(origline
);
2667 return DIRECTIVE_FOUND
;
2669 severity
= ERR_FATAL
;
2672 severity
= ERR_NONFATAL
;
2675 severity
= ERR_WARNING
|ERR_WARN_USER
;
2679 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2681 /* Only error out if this is the final pass */
2682 if (pass
!= 2 && i
!= PP_FATAL
)
2683 return DIRECTIVE_FOUND
;
2685 tline
->next
= expand_smacro(tline
->next
);
2686 tline
= tline
->next
;
2688 t
= tline
? tline
->next
: NULL
;
2690 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2691 /* The line contains only a quoted string */
2693 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2694 error(severity
, "%s", p
);
2696 /* Not a quoted string, or more than a quoted string */
2697 p
= detoken(tline
, false);
2698 error(severity
, "%s", p
);
2701 free_tlist(origline
);
2702 return DIRECTIVE_FOUND
;
2706 if (defining
!= NULL
) {
2707 if (defining
->type
== EXP_IF
) {
2708 defining
->def_depth
++;
2710 return NO_DIRECTIVE_FOUND
;
2712 if ((istk
->expansion
!= NULL
) &&
2713 (istk
->expansion
->emitting
== false)) {
2716 j
= if_condition(tline
->next
, i
);
2717 tline
->next
= NULL
; /* it got freed */
2718 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
2720 ed
= new_ExpDef(EXP_IF
);
2726 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
2727 ed
->prev
= defining
;
2729 free_tlist(origline
);
2730 return DIRECTIVE_FOUND
;
2733 if (defining
!= NULL
) {
2734 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2735 return NO_DIRECTIVE_FOUND
;
2738 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2739 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2741 switch (defining
->state
) {
2743 defining
->state
= COND_DONE
;
2744 defining
->ignoring
= true;
2749 defining
->ignoring
= true;
2752 case COND_ELSE_TRUE
:
2753 case COND_ELSE_FALSE
:
2754 error_precond(ERR_WARNING
|ERR_PASS1
,
2755 "`%%elif' after `%%else' ignored");
2756 defining
->state
= COND_NEVER
;
2757 defining
->ignoring
= true;
2762 * IMPORTANT: In the case of %if, we will already have
2763 * called expand_mmac_params(); however, if we're
2764 * processing an %elif we must have been in a
2765 * non-emitting mode, which would have inhibited
2766 * the normal invocation of expand_mmac_params().
2767 * Therefore, we have to do it explicitly here.
2769 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2770 tline
->next
= NULL
; /* it got freed */
2772 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2773 defining
->ignoring
= ((defining
->state
== COND_IF_TRUE
) ? false : true);
2776 free_tlist(origline
);
2777 return DIRECTIVE_FOUND
;
2780 if (defining
!= NULL
) {
2781 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2782 return NO_DIRECTIVE_FOUND
;
2786 error_precond(ERR_WARNING
|ERR_PASS1
,
2787 "trailing garbage after `%%else' ignored");
2788 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2789 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2791 switch (defining
->state
) {
2794 defining
->state
= COND_ELSE_FALSE
;
2795 defining
->ignoring
= true;
2799 defining
->ignoring
= true;
2803 defining
->state
= COND_ELSE_TRUE
;
2804 defining
->ignoring
= false;
2807 case COND_ELSE_TRUE
:
2808 case COND_ELSE_FALSE
:
2809 error_precond(ERR_WARNING
|ERR_PASS1
,
2810 "`%%else' after `%%else' ignored.");
2811 defining
->state
= COND_NEVER
;
2812 defining
->ignoring
= true;
2815 free_tlist(origline
);
2816 return DIRECTIVE_FOUND
;
2819 if (defining
!= NULL
) {
2820 if (defining
->type
== EXP_IF
) {
2821 if (defining
->def_depth
> 0) {
2822 defining
->def_depth
--;
2823 return NO_DIRECTIVE_FOUND
;
2826 return NO_DIRECTIVE_FOUND
;
2830 error_precond(ERR_WARNING
|ERR_PASS1
,
2831 "trailing garbage after `%%endif' ignored");
2832 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2833 error(ERR_NONFATAL
, "`%%endif': no matching `%%if'");
2834 return DIRECTIVE_FOUND
;
2837 defining
= ed
->prev
;
2838 ed
->prev
= expansions
;
2840 ei
= new_ExpInv(EXP_IF
, ed
);
2841 ei
->current
= ed
->line
;
2842 ei
->emitting
= true;
2843 ei
->prev
= istk
->expansion
;
2844 istk
->expansion
= ei
;
2845 free_tlist(origline
);
2846 return DIRECTIVE_FOUND
;
2852 if (defining
!= NULL
) {
2853 if (defining
->type
== EXP_MMACRO
) {
2854 defining
->def_depth
++;
2856 return NO_DIRECTIVE_FOUND
;
2858 ed
= new_ExpDef(EXP_MMACRO
);
2860 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2861 ed
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2862 if (!parse_mmacro_spec(tline
, ed
, pp_directives
[i
])) {
2865 return DIRECTIVE_FOUND
;
2869 ed
->max_depth
= (ed
->max_depth
+ 1);
2870 ed
->ignoring
= false;
2871 ed
->prev
= defining
;
2874 eed
= (ExpDef
*) hash_findix(&expdefs
, ed
->name
);
2876 if (!strcmp(eed
->name
, ed
->name
) &&
2877 (eed
->nparam_min
<= ed
->nparam_max
2879 && (ed
->nparam_min
<= eed
->nparam_max
2881 error(ERR_WARNING
|ERR_PASS1
,
2882 "redefining multi-line macro `%s'", ed
->name
);
2883 return DIRECTIVE_FOUND
;
2887 free_tlist(origline
);
2888 return DIRECTIVE_FOUND
;
2892 if (defining
!= NULL
) {
2893 if (defining
->type
== EXP_MMACRO
) {
2894 if (defining
->def_depth
> 0) {
2895 defining
->def_depth
--;
2896 return NO_DIRECTIVE_FOUND
;
2899 return NO_DIRECTIVE_FOUND
;
2902 if (!(defining
) || (defining
->type
!= EXP_MMACRO
)) {
2903 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2904 return DIRECTIVE_FOUND
;
2906 edhead
= (ExpDef
**) hash_findi_add(&expdefs
, defining
->name
);
2907 defining
->next
= *edhead
;
2910 defining
= ed
->prev
;
2911 ed
->prev
= expansions
;
2914 free_tlist(origline
);
2915 return DIRECTIVE_FOUND
;
2918 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2920 * We must search along istk->expansion until we hit a
2921 * macro invocation. Then we disable the emitting state(s)
2922 * between exitmacro and endmacro.
2924 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
2925 if(ei
->type
== EXP_MMACRO
) {
2932 * Set all invocations leading back to the macro
2933 * invocation to a non-emitting state.
2935 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
2936 eei
->emitting
= false;
2938 eei
->emitting
= false;
2940 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2942 free_tlist(origline
);
2943 return DIRECTIVE_FOUND
;
2947 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2952 spec
.casesense
= (i
== PP_UNMACRO
);
2953 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2954 return DIRECTIVE_FOUND
;
2956 ed_p
= (ExpDef
**) hash_findi(&expdefs
, spec
.name
, NULL
);
2957 while (ed_p
&& *ed_p
) {
2959 if (ed
->casesense
== spec
.casesense
&&
2960 !mstrcmp(ed
->name
, spec
.name
, spec
.casesense
) &&
2961 ed
->nparam_min
== spec
.nparam_min
&&
2962 ed
->nparam_max
== spec
.nparam_max
&&
2963 ed
->plus
== spec
.plus
) {
2970 free_tlist(origline
);
2971 free_tlist(spec
.dlist
);
2972 return DIRECTIVE_FOUND
;
2976 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2977 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2978 tline
= tline
->next
;
2980 free_tlist(origline
);
2981 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2982 return DIRECTIVE_FOUND
;
2984 t
= expand_smacro(tline
->next
);
2986 free_tlist(origline
);
2989 tokval
.t_type
= TOKEN_INVALID
;
2991 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2994 return DIRECTIVE_FOUND
;
2996 error(ERR_WARNING
|ERR_PASS1
,
2997 "trailing garbage after expression ignored");
2998 if (!is_simple(evalresult
)) {
2999 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
3000 return DIRECTIVE_FOUND
;
3002 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3003 if (ei
->type
== EXP_MMACRO
) {
3008 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
3009 } else if (ei
->nparam
== 0) {
3011 "`%%rotate' invoked within macro without parameters");
3013 int rotate
= ei
->rotate
+ reloc_value(evalresult
);
3015 rotate
%= (int)ei
->nparam
;
3017 rotate
+= ei
->nparam
;
3018 ei
->rotate
= rotate
;
3020 return DIRECTIVE_FOUND
;
3023 if (defining
!= NULL
) {
3024 if (defining
->type
== EXP_REP
) {
3025 defining
->def_depth
++;
3027 return NO_DIRECTIVE_FOUND
;
3031 tline
= tline
->next
;
3032 } while (tok_type_(tline
, TOK_WHITESPACE
));
3034 if (tok_type_(tline
, TOK_ID
) &&
3035 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3038 tline
= tline
->next
;
3039 } while (tok_type_(tline
, TOK_WHITESPACE
));
3043 t
= expand_smacro(tline
);
3045 tokval
.t_type
= TOKEN_INVALID
;
3047 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3049 free_tlist(origline
);
3050 return DIRECTIVE_FOUND
;
3053 error(ERR_WARNING
|ERR_PASS1
,
3054 "trailing garbage after expression ignored");
3055 if (!is_simple(evalresult
)) {
3056 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3057 return DIRECTIVE_FOUND
;
3059 count
= reloc_value(evalresult
);
3060 if (count
>= REP_LIMIT
) {
3061 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
3066 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3069 free_tlist(origline
);
3070 ed
= new_ExpDef(EXP_REP
);
3071 ed
->nolist
= nolist
;
3074 ed
->max_depth
= (count
- 1);
3075 ed
->ignoring
= false;
3076 ed
->prev
= defining
;
3078 return DIRECTIVE_FOUND
;
3081 if (defining
!= NULL
) {
3082 if (defining
->type
== EXP_REP
) {
3083 if (defining
->def_depth
> 0) {
3084 defining
->def_depth
--;
3085 return NO_DIRECTIVE_FOUND
;
3088 return NO_DIRECTIVE_FOUND
;
3091 if ((defining
== NULL
) || (defining
->type
!= EXP_REP
)) {
3092 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3093 return DIRECTIVE_FOUND
;
3097 * Now we have a "macro" defined - although it has no name
3098 * and we won't be entering it in the hash tables - we must
3099 * push a macro-end marker for it on to istk->expansion.
3100 * After that, it will take care of propagating itself (a
3101 * macro-end marker line for a macro which is really a %rep
3102 * block will cause the macro to be re-expanded, complete
3103 * with another macro-end marker to ensure the process
3104 * continues) until the whole expansion is forcibly removed
3105 * from istk->expansion by a %exitrep.
3108 defining
= ed
->prev
;
3109 ed
->prev
= expansions
;
3111 ei
= new_ExpInv(EXP_REP
, ed
);
3112 ei
->current
= ed
->line
;
3113 ei
->emitting
= ((ed
->max_depth
> 0) ? true : false);
3114 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3115 ei
->prev
= istk
->expansion
;
3116 istk
->expansion
= ei
;
3117 free_tlist(origline
);
3118 return DIRECTIVE_FOUND
;
3121 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3123 * We must search along istk->expansion until we hit a
3124 * rep invocation. Then we disable the emitting state(s)
3125 * between exitrep and endrep.
3127 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3128 if (ei
->type
== EXP_REP
) {
3135 * Set all invocations leading back to the rep
3136 * invocation to a non-emitting state.
3138 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3139 eei
->emitting
= false;
3141 eei
->emitting
= false;
3142 eei
->current
= NULL
;
3143 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3145 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3147 free_tlist(origline
);
3148 return DIRECTIVE_FOUND
;
3154 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3155 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3157 tline
= tline
->next
;
3159 tline
= expand_id(tline
);
3160 if (!tline
|| (tline
->type
!= TOK_ID
&&
3161 (tline
->type
!= TOK_PREPROC_ID
||
3162 tline
->text
[1] != '$'))) {
3163 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3165 free_tlist(origline
);
3166 return DIRECTIVE_FOUND
;
3169 ctx
= get_ctx(tline
->text
, &mname
, false);
3171 param_start
= tline
= tline
->next
;
3174 /* Expand the macro definition now for %xdefine and %ixdefine */
3175 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3176 tline
= expand_smacro(tline
);
3178 if (tok_is_(tline
, "(")) {
3180 * This macro has parameters.
3183 tline
= tline
->next
;
3187 error(ERR_NONFATAL
, "parameter identifier expected");
3188 free_tlist(origline
);
3189 return DIRECTIVE_FOUND
;
3191 if (tline
->type
!= TOK_ID
) {
3193 "`%s': parameter identifier expected",
3195 free_tlist(origline
);
3196 return DIRECTIVE_FOUND
;
3198 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3199 tline
= tline
->next
;
3201 if (tok_is_(tline
, ",")) {
3202 tline
= tline
->next
;
3204 if (!tok_is_(tline
, ")")) {
3206 "`)' expected to terminate macro template");
3207 free_tlist(origline
);
3208 return DIRECTIVE_FOUND
;
3214 tline
= tline
->next
;
3216 if (tok_type_(tline
, TOK_WHITESPACE
))
3217 last
= tline
, tline
= tline
->next
;
3222 if (t
->type
== TOK_ID
) {
3223 list_for_each(tt
, param_start
)
3224 if (tt
->type
>= TOK_SMAC_PARAM
&&
3225 !strcmp(tt
->text
, t
->text
))
3229 t
->next
= macro_start
;
3234 * Good. We now have a macro name, a parameter count, and a
3235 * token list (in reverse order) for an expansion. We ought
3236 * to be OK just to create an SMacro, store it, and let
3237 * free_tlist have the rest of the line (which we have
3238 * carefully re-terminated after chopping off the expansion
3241 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3242 free_tlist(origline
);
3243 return DIRECTIVE_FOUND
;
3246 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
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
, "`%%undef' expects a macro identifier");
3254 free_tlist(origline
);
3255 return DIRECTIVE_FOUND
;
3258 error(ERR_WARNING
|ERR_PASS1
,
3259 "trailing garbage after macro name ignored");
3262 /* Find the context that symbol belongs to */
3263 ctx
= get_ctx(tline
->text
, &mname
, false);
3264 undef_smacro(ctx
, mname
);
3265 free_tlist(origline
);
3266 return DIRECTIVE_FOUND
;
3270 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3271 casesense
= (i
== PP_DEFSTR
);
3273 tline
= tline
->next
;
3275 tline
= expand_id(tline
);
3276 if (!tline
|| (tline
->type
!= TOK_ID
&&
3277 (tline
->type
!= TOK_PREPROC_ID
||
3278 tline
->text
[1] != '$'))) {
3279 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3281 free_tlist(origline
);
3282 return DIRECTIVE_FOUND
;
3285 ctx
= get_ctx(tline
->text
, &mname
, false);
3287 tline
= expand_smacro(tline
->next
);
3290 while (tok_type_(tline
, TOK_WHITESPACE
))
3291 tline
= delete_Token(tline
);
3293 p
= detoken(tline
, false);
3294 macro_start
= nasm_malloc(sizeof(*macro_start
));
3295 macro_start
->next
= NULL
;
3296 macro_start
->text
= nasm_quote(p
, strlen(p
));
3297 macro_start
->type
= TOK_STRING
;
3298 macro_start
->a
.mac
= NULL
;
3302 * We now have a macro name, an implicit parameter count of
3303 * zero, and a string token to use as an expansion. Create
3304 * and store an SMacro.
3306 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3307 free_tlist(origline
);
3308 return DIRECTIVE_FOUND
;
3312 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3313 casesense
= (i
== PP_DEFTOK
);
3315 tline
= tline
->next
;
3317 tline
= expand_id(tline
);
3318 if (!tline
|| (tline
->type
!= TOK_ID
&&
3319 (tline
->type
!= TOK_PREPROC_ID
||
3320 tline
->text
[1] != '$'))) {
3322 "`%s' expects a macro identifier as first parameter",
3324 free_tlist(origline
);
3325 return DIRECTIVE_FOUND
;
3327 ctx
= get_ctx(tline
->text
, &mname
, false);
3329 tline
= expand_smacro(tline
->next
);
3333 while (tok_type_(t
, TOK_WHITESPACE
))
3335 /* t should now point to the string */
3336 if (!tok_type_(t
, TOK_STRING
)) {
3338 "`%s` requires string as second parameter",
3341 free_tlist(origline
);
3342 return DIRECTIVE_FOUND
;
3346 * Convert the string to a token stream. Note that smacros
3347 * are stored with the token stream reversed, so we have to
3348 * reverse the output of tokenize().
3350 nasm_unquote_cstr(t
->text
, i
);
3351 macro_start
= reverse_tokens(tokenize(t
->text
));
3354 * We now have a macro name, an implicit parameter count of
3355 * zero, and a numeric token to use as an expansion. Create
3356 * and store an SMacro.
3358 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3360 free_tlist(origline
);
3361 return DIRECTIVE_FOUND
;
3364 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3367 StrList
*xsl
= NULL
;
3368 StrList
**xst
= &xsl
;
3372 tline
= tline
->next
;
3374 tline
= expand_id(tline
);
3375 if (!tline
|| (tline
->type
!= TOK_ID
&&
3376 (tline
->type
!= TOK_PREPROC_ID
||
3377 tline
->text
[1] != '$'))) {
3379 "`%%pathsearch' expects a macro identifier as first parameter");
3380 free_tlist(origline
);
3381 return DIRECTIVE_FOUND
;
3383 ctx
= get_ctx(tline
->text
, &mname
, false);
3385 tline
= expand_smacro(tline
->next
);
3389 while (tok_type_(t
, TOK_WHITESPACE
))
3392 if (!t
|| (t
->type
!= TOK_STRING
&&
3393 t
->type
!= TOK_INTERNAL_STRING
)) {
3394 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3396 free_tlist(origline
);
3397 return DIRECTIVE_FOUND
; /* but we did _something_ */
3400 error(ERR_WARNING
|ERR_PASS1
,
3401 "trailing garbage after `%%pathsearch' ignored");
3403 if (t
->type
!= TOK_INTERNAL_STRING
)
3404 nasm_unquote(p
, NULL
);
3406 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3409 fclose(fp
); /* Don't actually care about the file */
3411 macro_start
= nasm_malloc(sizeof(*macro_start
));
3412 macro_start
->next
= NULL
;
3413 macro_start
->text
= nasm_quote(p
, strlen(p
));
3414 macro_start
->type
= TOK_STRING
;
3415 macro_start
->a
.mac
= NULL
;
3420 * We now have a macro name, an implicit parameter count of
3421 * zero, and a string token to use as an expansion. Create
3422 * and store an SMacro.
3424 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3426 free_tlist(origline
);
3427 return DIRECTIVE_FOUND
;
3431 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3434 tline
= tline
->next
;
3436 tline
= expand_id(tline
);
3437 if (!tline
|| (tline
->type
!= TOK_ID
&&
3438 (tline
->type
!= TOK_PREPROC_ID
||
3439 tline
->text
[1] != '$'))) {
3441 "`%%strlen' expects a macro identifier as first parameter");
3442 free_tlist(origline
);
3443 return DIRECTIVE_FOUND
;
3445 ctx
= get_ctx(tline
->text
, &mname
, false);
3447 tline
= expand_smacro(tline
->next
);
3451 while (tok_type_(t
, TOK_WHITESPACE
))
3453 /* t should now point to the string */
3454 if (!tok_type_(t
, TOK_STRING
)) {
3456 "`%%strlen` requires string as second parameter");
3458 free_tlist(origline
);
3459 return DIRECTIVE_FOUND
;
3462 macro_start
= nasm_malloc(sizeof(*macro_start
));
3463 macro_start
->next
= NULL
;
3464 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3465 macro_start
->a
.mac
= NULL
;
3468 * We now have a macro name, an implicit parameter count of
3469 * zero, and a numeric token to use as an expansion. Create
3470 * and store an SMacro.
3472 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3474 free_tlist(origline
);
3475 return DIRECTIVE_FOUND
;
3478 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3481 tline
= tline
->next
;
3483 tline
= expand_id(tline
);
3484 if (!tline
|| (tline
->type
!= TOK_ID
&&
3485 (tline
->type
!= TOK_PREPROC_ID
||
3486 tline
->text
[1] != '$'))) {
3488 "`%%strcat' expects a macro identifier as first parameter");
3489 free_tlist(origline
);
3490 return DIRECTIVE_FOUND
;
3492 ctx
= get_ctx(tline
->text
, &mname
, false);
3494 tline
= expand_smacro(tline
->next
);
3498 list_for_each(t
, tline
) {
3500 case TOK_WHITESPACE
:
3503 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3506 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3508 /* else fall through */
3511 "non-string passed to `%%strcat' (%d)", t
->type
);
3513 free_tlist(origline
);
3514 return DIRECTIVE_FOUND
;
3518 p
= pp
= nasm_malloc(len
);
3519 list_for_each(t
, tline
) {
3520 if (t
->type
== TOK_STRING
) {
3521 memcpy(p
, t
->text
, t
->a
.len
);
3527 * We now have a macro name, an implicit parameter count of
3528 * zero, and a numeric token to use as an expansion. Create
3529 * and store an SMacro.
3531 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3532 macro_start
->text
= nasm_quote(pp
, len
);
3534 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3536 free_tlist(origline
);
3537 return DIRECTIVE_FOUND
;
3540 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3542 int64_t start
, count
;
3547 tline
= tline
->next
;
3549 tline
= expand_id(tline
);
3550 if (!tline
|| (tline
->type
!= TOK_ID
&&
3551 (tline
->type
!= TOK_PREPROC_ID
||
3552 tline
->text
[1] != '$'))) {
3554 "`%%substr' expects a macro identifier as first parameter");
3555 free_tlist(origline
);
3556 return DIRECTIVE_FOUND
;
3558 ctx
= get_ctx(tline
->text
, &mname
, false);
3560 tline
= expand_smacro(tline
->next
);
3563 if (tline
) /* skip expanded id */
3565 while (tok_type_(t
, TOK_WHITESPACE
))
3568 /* t should now point to the string */
3569 if (!tok_type_(t
, TOK_STRING
)) {
3571 "`%%substr` requires string as second parameter");
3573 free_tlist(origline
);
3574 return DIRECTIVE_FOUND
;
3579 tokval
.t_type
= TOKEN_INVALID
;
3580 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3584 free_tlist(origline
);
3585 return DIRECTIVE_FOUND
;
3586 } else if (!is_simple(evalresult
)) {
3587 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3589 free_tlist(origline
);
3590 return DIRECTIVE_FOUND
;
3592 start
= evalresult
->value
- 1;
3594 while (tok_type_(tt
, TOK_WHITESPACE
))
3597 count
= 1; /* Backwards compatibility: one character */
3599 tokval
.t_type
= TOKEN_INVALID
;
3600 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3604 free_tlist(origline
);
3605 return DIRECTIVE_FOUND
;
3606 } else if (!is_simple(evalresult
)) {
3607 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3609 free_tlist(origline
);
3610 return DIRECTIVE_FOUND
;
3612 count
= evalresult
->value
;
3615 len
= nasm_unquote(t
->text
, NULL
);
3616 /* make start and count being in range */
3620 count
= len
+ count
+ 1 - start
;
3621 if (start
+ count
> (int64_t)len
)
3622 count
= len
- start
;
3623 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3624 start
= -1, count
= 0; /* empty string */
3626 macro_start
= nasm_malloc(sizeof(*macro_start
));
3627 macro_start
->next
= NULL
;
3628 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3629 macro_start
->type
= TOK_STRING
;
3630 macro_start
->a
.mac
= NULL
;
3633 * We now have a macro name, an implicit parameter count of
3634 * zero, and a numeric token to use as an expansion. Create
3635 * and store an SMacro.
3637 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3639 free_tlist(origline
);
3640 return DIRECTIVE_FOUND
;
3645 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3646 casesense
= (i
== PP_ASSIGN
);
3648 tline
= tline
->next
;
3650 tline
= expand_id(tline
);
3651 if (!tline
|| (tline
->type
!= TOK_ID
&&
3652 (tline
->type
!= TOK_PREPROC_ID
||
3653 tline
->text
[1] != '$'))) {
3655 "`%%%sassign' expects a macro identifier",
3656 (i
== PP_IASSIGN
? "i" : ""));
3657 free_tlist(origline
);
3658 return DIRECTIVE_FOUND
;
3660 ctx
= get_ctx(tline
->text
, &mname
, false);
3662 tline
= expand_smacro(tline
->next
);
3667 tokval
.t_type
= TOKEN_INVALID
;
3669 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3672 free_tlist(origline
);
3673 return DIRECTIVE_FOUND
;
3677 error(ERR_WARNING
|ERR_PASS1
,
3678 "trailing garbage after expression ignored");
3680 if (!is_simple(evalresult
)) {
3682 "non-constant value given to `%%%sassign'",
3683 (i
== PP_IASSIGN
? "i" : ""));
3684 free_tlist(origline
);
3685 return DIRECTIVE_FOUND
;
3688 macro_start
= nasm_malloc(sizeof(*macro_start
));
3689 macro_start
->next
= NULL
;
3690 make_tok_num(macro_start
, reloc_value(evalresult
));
3691 macro_start
->a
.mac
= NULL
;
3694 * We now have a macro name, an implicit parameter count of
3695 * zero, and a numeric token to use as an expansion. Create
3696 * and store an SMacro.
3698 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3699 free_tlist(origline
);
3700 return DIRECTIVE_FOUND
;
3703 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3705 * Syntax is `%line nnn[+mmm] [filename]'
3707 tline
= tline
->next
;
3709 if (!tok_type_(tline
, TOK_NUMBER
)) {
3710 error(ERR_NONFATAL
, "`%%line' expects line number");
3711 free_tlist(origline
);
3712 return DIRECTIVE_FOUND
;
3714 k
= readnum(tline
->text
, &err
);
3716 tline
= tline
->next
;
3717 if (tok_is_(tline
, "+")) {
3718 tline
= tline
->next
;
3719 if (!tok_type_(tline
, TOK_NUMBER
)) {
3720 error(ERR_NONFATAL
, "`%%line' expects line increment");
3721 free_tlist(origline
);
3722 return DIRECTIVE_FOUND
;
3724 m
= readnum(tline
->text
, &err
);
3725 tline
= tline
->next
;
3731 nasm_free(src_set_fname(detoken(tline
, false)));
3733 free_tlist(origline
);
3734 return DIRECTIVE_FOUND
;
3737 if (defining
!= NULL
) {
3738 if (defining
->type
== EXP_WHILE
) {
3739 defining
->def_depth
++;
3741 return NO_DIRECTIVE_FOUND
;
3744 if ((istk
->expansion
!= NULL
) &&
3745 (istk
->expansion
->emitting
== false)) {
3749 l
->first
= copy_Token(tline
->next
);
3750 j
= if_condition(tline
->next
, i
);
3751 tline
->next
= NULL
; /* it got freed */
3752 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
3754 ed
= new_ExpDef(EXP_WHILE
);
3757 ed
->max_depth
= DEADMAN_LIMIT
;
3758 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
3759 if (ed
->ignoring
== false) {
3762 } else if (l
!= NULL
) {
3763 delete_Token(l
->first
);
3767 ed
->prev
= defining
;
3769 free_tlist(origline
);
3770 return DIRECTIVE_FOUND
;
3773 if (defining
!= NULL
) {
3774 if (defining
->type
== EXP_WHILE
) {
3775 if (defining
->def_depth
> 0) {
3776 defining
->def_depth
--;
3777 return NO_DIRECTIVE_FOUND
;
3780 return NO_DIRECTIVE_FOUND
;
3783 if (tline
->next
!= NULL
) {
3784 error_precond(ERR_WARNING
|ERR_PASS1
,
3785 "trailing garbage after `%%endwhile' ignored");
3787 if ((defining
== NULL
) || (defining
->type
!= EXP_WHILE
)) {
3788 error(ERR_NONFATAL
, "`%%endwhile': no matching `%%while'");
3789 return DIRECTIVE_FOUND
;
3792 defining
= ed
->prev
;
3793 if (ed
->ignoring
== false) {
3794 ed
->prev
= expansions
;
3796 ei
= new_ExpInv(EXP_WHILE
, ed
);
3797 ei
->current
= ed
->line
->next
;
3798 ei
->emitting
= true;
3799 ei
->prev
= istk
->expansion
;
3800 istk
->expansion
= ei
;
3804 free_tlist(origline
);
3805 return DIRECTIVE_FOUND
;
3808 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3810 * We must search along istk->expansion until we hit a
3811 * while invocation. Then we disable the emitting state(s)
3812 * between exitwhile and endwhile.
3814 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3815 if (ei
->type
== EXP_WHILE
) {
3822 * Set all invocations leading back to the while
3823 * invocation to a non-emitting state.
3825 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3826 eei
->emitting
= false;
3828 eei
->emitting
= false;
3829 eei
->current
= NULL
;
3830 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3832 error(ERR_NONFATAL
, "`%%exitwhile' not within `%%while' block");
3834 free_tlist(origline
);
3835 return DIRECTIVE_FOUND
;
3838 if (defining
!= NULL
) {
3839 if (defining
->type
== EXP_COMMENT
) {
3840 defining
->def_depth
++;
3842 return NO_DIRECTIVE_FOUND
;
3844 ed
= new_ExpDef(EXP_COMMENT
);
3845 ed
->ignoring
= true;
3846 ed
->prev
= defining
;
3848 free_tlist(origline
);
3849 return DIRECTIVE_FOUND
;
3852 if (defining
!= NULL
) {
3853 if (defining
->type
== EXP_COMMENT
) {
3854 if (defining
->def_depth
> 0) {
3855 defining
->def_depth
--;
3856 return NO_DIRECTIVE_FOUND
;
3859 return NO_DIRECTIVE_FOUND
;
3862 if ((defining
== NULL
) || (defining
->type
!= EXP_COMMENT
)) {
3863 error(ERR_NONFATAL
, "`%%endcomment': no matching `%%comment'");
3864 return DIRECTIVE_FOUND
;
3867 defining
= ed
->prev
;
3869 free_tlist(origline
);
3870 return DIRECTIVE_FOUND
;
3873 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3874 if (in_final
!= false) {
3875 error(ERR_FATAL
, "`%%final' cannot be used recursively");
3877 tline
= tline
->next
;
3879 if (tline
== NULL
) {
3880 error(ERR_NONFATAL
, "`%%final' expects at least one parameter");
3883 l
->first
= copy_Token(tline
);
3887 free_tlist(origline
);
3888 return DIRECTIVE_FOUND
;
3892 "preprocessor directive `%s' not yet implemented",
3894 return DIRECTIVE_FOUND
;
3899 * Ensure that a macro parameter contains a condition code and
3900 * nothing else. Return the condition code index if so, or -1
3903 static int find_cc(Token
* t
)
3909 return -1; /* Probably a %+ without a space */
3912 if (t
->type
!= TOK_ID
)
3916 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3920 j
= ARRAY_SIZE(conditions
);
3923 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3938 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3939 int mnum
, bool handle_paste_tokens
)
3941 Token
**tail
, *t
, *tt
;
3943 bool did_paste
= false;
3947 /* Now handle token pasting... */
3950 while ((t
= *tail
) && (tt
= t
->next
)) {
3952 case TOK_WHITESPACE
:
3953 if (tt
->type
== TOK_WHITESPACE
) {
3954 /* Zap adjacent whitespace tokens */
3955 t
->next
= delete_Token(tt
);
3957 /* Do not advance paste_head here */
3961 case TOK_PASTE
: /* %+ */
3962 if (handle_paste_tokens
) {
3963 /* Zap %+ and whitespace tokens to the right */
3964 while (t
&& (t
->type
== TOK_WHITESPACE
||
3965 t
->type
== TOK_PASTE
))
3966 t
= *tail
= delete_Token(t
);
3967 if (!paste_head
|| !t
)
3968 break; /* Nothing to paste with */
3972 while (tok_type_(tt
, TOK_WHITESPACE
))
3973 tt
= t
->next
= delete_Token(tt
);
3975 tmp
= nasm_strcat(t
->text
, tt
->text
);
3977 tt
= delete_Token(tt
);
3978 t
= *tail
= tokenize(tmp
);
3984 t
->next
= tt
; /* Attach the remaining token chain */
3991 /* else fall through */
3994 * Concatenation of tokens might look nontrivial
3995 * but in real it's pretty simple -- the caller
3996 * prepares the masks of token types to be concatenated
3997 * and we simply find matched sequences and slip
4000 for (i
= 0; i
< mnum
; i
++) {
4001 if (PP_CONCAT_MASK(t
->type
) & m
[i
].mask_head
) {
4005 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & m
[i
].mask_tail
)) {
4006 len
+= strlen(tt
->text
);
4011 * Now tt points to the first token after
4012 * the potential paste area...
4014 if (tt
!= t
->next
) {
4015 /* We have at least two tokens... */
4016 len
+= strlen(t
->text
);
4017 p
= tmp
= nasm_malloc(len
+1);
4020 p
= strchr(p
, '\0');
4021 t
= delete_Token(t
);
4023 t
= *tail
= tokenize(tmp
);
4029 t
->next
= tt
; /* Attach the remaining token chain */
4037 if (i
>= mnum
) { /* no match */
4039 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
4049 * expands to a list of tokens from %{x:y}
4051 static Token
*expand_mmac_params_range(ExpInv
*ei
, Token
*tline
, Token
***last
)
4053 Token
*t
= tline
, **tt
, *tm
, *head
;
4057 pos
= strchr(tline
->text
, ':');
4060 lst
= atoi(pos
+ 1);
4061 fst
= atoi(tline
->text
+ 1);
4064 * only macros params are accounted so
4065 * if someone passes %0 -- we reject such
4068 if (lst
== 0 || fst
== 0)
4071 /* the values should be sane */
4072 if ((fst
> (int)ei
->nparam
|| fst
< (-(int)ei
->nparam
)) ||
4073 (lst
> (int)ei
->nparam
|| lst
< (-(int)ei
->nparam
)))
4076 fst
= fst
< 0 ? fst
+ (int)ei
->nparam
+ 1: fst
;
4077 lst
= lst
< 0 ? lst
+ (int)ei
->nparam
+ 1: lst
;
4079 /* counted from zero */
4083 * it will be at least one token
4085 tm
= ei
->params
[(fst
+ ei
->rotate
) % ei
->nparam
];
4086 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4087 head
= t
, tt
= &t
->next
;
4089 for (i
= fst
+ 1; i
<= lst
; i
++) {
4090 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4091 *tt
= t
, tt
= &t
->next
;
4092 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4094 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4095 *tt
= t
, tt
= &t
->next
;
4098 for (i
= fst
- 1; i
>= lst
; i
--) {
4099 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4100 *tt
= t
, tt
= &t
->next
;
4101 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4103 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4104 *tt
= t
, tt
= &t
->next
;
4112 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
4118 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4119 * %-n) and MMacro-local identifiers (%%foo) as well as
4120 * macro indirection (%[...]) and range (%{..:..}).
4122 static Token
*expand_mmac_params(Token
* tline
)
4124 Token
*t
, *tt
, **tail
, *thead
;
4125 bool changed
= false;
4132 if (tline
->type
== TOK_PREPROC_ID
&&
4133 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4134 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4135 tline
->text
[1] == '%')) {
4137 int type
= 0, cc
; /* type = 0 to placate optimisers */
4144 tline
= tline
->next
;
4146 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
4147 if (ei
->type
== EXP_MMACRO
) {
4152 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4154 pos
= strchr(t
->text
, ':');
4156 switch (t
->text
[1]) {
4158 * We have to make a substitution of one of the
4159 * forms %1, %-1, %+1, %%foo, %0.
4162 if ((strlen(t
->text
) > 2) && (t
->text
[2] == '0')) {
4164 text
= nasm_strdup(ei
->label_text
);
4167 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", ei
->nparam
);
4168 text
= nasm_strdup(tmpbuf
);
4173 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4175 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4178 n
= atoi(t
->text
+ 2) - 1;
4179 if (n
>= ei
->nparam
)
4183 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4189 "macro parameter %d is not a condition code",
4194 if (inverse_ccs
[cc
] == -1) {
4196 "condition code `%s' is not invertible",
4200 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4204 n
= atoi(t
->text
+ 2) - 1;
4205 if (n
>= ei
->nparam
)
4209 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4215 "macro parameter %d is not a condition code",
4220 text
= nasm_strdup(conditions
[cc
]);
4224 n
= atoi(t
->text
+ 1) - 1;
4225 if (n
>= ei
->nparam
)
4229 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4233 for (i
= 0; i
< ei
->paramlen
[n
]; i
++) {
4234 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4235 tail
= &(*tail
)->next
;
4239 text
= NULL
; /* we've done it here */
4244 * seems we have a parameters range here
4246 Token
*head
, **last
;
4247 head
= expand_mmac_params_range(ei
, t
, &last
);
4268 } else if (tline
->type
== TOK_INDIRECT
) {
4270 tline
= tline
->next
;
4271 tt
= tokenize(t
->text
);
4272 tt
= expand_mmac_params(tt
);
4273 tt
= expand_smacro(tt
);
4276 tt
->a
.mac
= NULL
; /* Necessary? */
4284 tline
= tline
->next
;
4292 const struct tokseq_match t
[] = {
4294 PP_CONCAT_MASK(TOK_ID
) |
4295 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4296 PP_CONCAT_MASK(TOK_ID
) |
4297 PP_CONCAT_MASK(TOK_NUMBER
) |
4298 PP_CONCAT_MASK(TOK_FLOAT
) |
4299 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4302 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4303 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4306 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4313 * Expand all single-line macro calls made in the given line.
4314 * Return the expanded version of the line. The original is deemed
4315 * to be destroyed in the process. (In reality we'll just move
4316 * Tokens from input to output a lot of the time, rather than
4317 * actually bothering to destroy and replicate.)
4320 static Token
*expand_smacro(Token
* tline
)
4322 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4323 SMacro
*head
= NULL
, *m
;
4326 unsigned int nparam
, sparam
;
4328 Token
*org_tline
= tline
;
4331 int deadman
= DEADMAN_LIMIT
;
4335 * Trick: we should avoid changing the start token pointer since it can
4336 * be contained in "next" field of other token. Because of this
4337 * we allocate a copy of first token and work with it; at the end of
4338 * routine we copy it back
4341 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4342 org_tline
->text
, 0);
4343 tline
->a
.mac
= org_tline
->a
.mac
;
4344 nasm_free(org_tline
->text
);
4345 org_tline
->text
= NULL
;
4348 expanded
= true; /* Always expand %+ at least once */
4354 while (tline
) { /* main token loop */
4356 error(ERR_NONFATAL
, "interminable macro recursion");
4360 if ((mname
= tline
->text
)) {
4361 /* if this token is a local macro, look in local context */
4362 if (tline
->type
== TOK_ID
) {
4363 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4364 } else if (tline
->type
== TOK_PREPROC_ID
) {
4365 ctx
= get_ctx(mname
, &mname
, false);
4366 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4371 * We've hit an identifier. As in is_mmacro below, we first
4372 * check whether the identifier is a single-line macro at
4373 * all, then think about checking for parameters if
4376 list_for_each(m
, head
)
4377 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4383 if (m
->nparam
== 0) {
4385 * Simple case: the macro is parameterless. Discard the
4386 * one token that the macro call took, and push the
4387 * expansion back on the to-do stack.
4389 if (!m
->expansion
) {
4390 if (!strcmp("__FILE__", m
->name
)) {
4393 src_get(&num
, &file
);
4394 tline
->text
= nasm_quote(file
, strlen(file
));
4395 tline
->type
= TOK_STRING
;
4399 if (!strcmp("__LINE__", m
->name
)) {
4400 nasm_free(tline
->text
);
4401 make_tok_num(tline
, src_get_linnum());
4404 if (!strcmp("__BITS__", m
->name
)) {
4405 nasm_free(tline
->text
);
4406 make_tok_num(tline
, globalbits
);
4409 tline
= delete_Token(tline
);
4414 * Complicated case: at least one macro with this name
4415 * exists and takes parameters. We must find the
4416 * parameters in the call, count them, find the SMacro
4417 * that corresponds to that form of the macro call, and
4418 * substitute for the parameters when we expand. What a
4421 /*tline = tline->next;
4422 skip_white_(tline); */
4425 while (tok_type_(t
, TOK_SMAC_END
)) {
4426 t
->a
.mac
->in_progress
= false;
4428 t
= tline
->next
= delete_Token(t
);
4431 } while (tok_type_(tline
, TOK_WHITESPACE
));
4432 if (!tok_is_(tline
, "(")) {
4434 * This macro wasn't called with parameters: ignore
4435 * the call. (Behaviour borrowed from gnu cpp.)
4444 sparam
= PARAM_DELTA
;
4445 params
= nasm_malloc(sparam
* sizeof(Token
*));
4446 params
[0] = tline
->next
;
4447 paramsize
= nasm_malloc(sparam
* sizeof(int));
4449 while (true) { /* parameter loop */
4451 * For some unusual expansions
4452 * which concatenates function call
4455 while (tok_type_(t
, TOK_SMAC_END
)) {
4456 t
->a
.mac
->in_progress
= false;
4458 t
= tline
->next
= delete_Token(t
);
4464 "macro call expects terminating `)'");
4467 if (tline
->type
== TOK_WHITESPACE
4469 if (paramsize
[nparam
])
4472 params
[nparam
] = tline
->next
;
4473 continue; /* parameter loop */
4475 if (tline
->type
== TOK_OTHER
4476 && tline
->text
[1] == 0) {
4477 char ch
= tline
->text
[0];
4478 if (ch
== ',' && !paren
&& brackets
<= 0) {
4479 if (++nparam
>= sparam
) {
4480 sparam
+= PARAM_DELTA
;
4481 params
= nasm_realloc(params
,
4482 sparam
* sizeof(Token
*));
4483 paramsize
= nasm_realloc(paramsize
,
4484 sparam
* sizeof(int));
4486 params
[nparam
] = tline
->next
;
4487 paramsize
[nparam
] = 0;
4489 continue; /* parameter loop */
4492 (brackets
> 0 || (brackets
== 0 &&
4493 !paramsize
[nparam
])))
4495 if (!(brackets
++)) {
4496 params
[nparam
] = tline
->next
;
4497 continue; /* parameter loop */
4500 if (ch
== '}' && brackets
> 0)
4501 if (--brackets
== 0) {
4503 continue; /* parameter loop */
4505 if (ch
== '(' && !brackets
)
4507 if (ch
== ')' && brackets
<= 0)
4513 error(ERR_NONFATAL
, "braces do not "
4514 "enclose all of macro parameter");
4516 paramsize
[nparam
] += white
+ 1;
4518 } /* parameter loop */
4520 while (m
&& (m
->nparam
!= nparam
||
4521 mstrcmp(m
->name
, mname
,
4525 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4526 "macro `%s' exists, "
4527 "but not taking %d parameters",
4528 mstart
->text
, nparam
);
4531 if (m
&& m
->in_progress
)
4533 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4535 * Design question: should we handle !tline, which
4536 * indicates missing ')' here, or expand those
4537 * macros anyway, which requires the (t) test a few
4541 nasm_free(paramsize
);
4545 * Expand the macro: we are placed on the last token of the
4546 * call, so that we can easily split the call from the
4547 * following tokens. We also start by pushing an SMAC_END
4548 * token for the cycle removal.
4555 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4557 m
->in_progress
= true;
4559 list_for_each(t
, m
->expansion
) {
4560 if (t
->type
>= TOK_SMAC_PARAM
) {
4561 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4565 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4566 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4568 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4574 } else if (t
->type
== TOK_PREPROC_Q
) {
4575 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4577 } else if (t
->type
== TOK_PREPROC_QQ
) {
4578 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4581 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4587 * Having done that, get rid of the macro call, and clean
4588 * up the parameters.
4591 nasm_free(paramsize
);
4594 continue; /* main token loop */
4599 if (tline
->type
== TOK_SMAC_END
) {
4600 tline
->a
.mac
->in_progress
= false;
4601 tline
= delete_Token(tline
);
4604 tline
= tline
->next
;
4612 * Now scan the entire line and look for successive TOK_IDs that resulted
4613 * after expansion (they can't be produced by tokenize()). The successive
4614 * TOK_IDs should be concatenated.
4615 * Also we look for %+ tokens and concatenate the tokens before and after
4616 * them (without white spaces in between).
4619 const struct tokseq_match t
[] = {
4621 PP_CONCAT_MASK(TOK_ID
) |
4622 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4623 PP_CONCAT_MASK(TOK_ID
) |
4624 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4625 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4628 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4630 * If we concatenated something, *and* we had previously expanded
4631 * an actual macro, scan the lines again for macros...
4642 *org_tline
= *thead
;
4643 /* since we just gave text to org_line, don't free it */
4645 delete_Token(thead
);
4647 /* the expression expanded to empty line;
4648 we can't return NULL for some reasons
4649 we just set the line to a single WHITESPACE token. */
4650 memset(org_tline
, 0, sizeof(*org_tline
));
4651 org_tline
->text
= NULL
;
4652 org_tline
->type
= TOK_WHITESPACE
;
4661 * Similar to expand_smacro but used exclusively with macro identifiers
4662 * right before they are fetched in. The reason is that there can be
4663 * identifiers consisting of several subparts. We consider that if there
4664 * are more than one element forming the name, user wants a expansion,
4665 * otherwise it will be left as-is. Example:
4669 * the identifier %$abc will be left as-is so that the handler for %define
4670 * will suck it and define the corresponding value. Other case:
4672 * %define _%$abc cde
4674 * In this case user wants name to be expanded *before* %define starts
4675 * working, so we'll expand %$abc into something (if it has a value;
4676 * otherwise it will be left as-is) then concatenate all successive
4679 static Token
*expand_id(Token
* tline
)
4681 Token
*cur
, *oldnext
= NULL
;
4683 if (!tline
|| !tline
->next
)
4688 (cur
->next
->type
== TOK_ID
||
4689 cur
->next
->type
== TOK_PREPROC_ID
4690 || cur
->next
->type
== TOK_NUMBER
))
4693 /* If identifier consists of just one token, don't expand */
4698 oldnext
= cur
->next
; /* Detach the tail past identifier */
4699 cur
->next
= NULL
; /* so that expand_smacro stops here */
4702 tline
= expand_smacro(tline
);
4705 /* expand_smacro possibly changhed tline; re-scan for EOL */
4707 while (cur
&& cur
->next
)
4710 cur
->next
= oldnext
;
4717 * Determine whether the given line constitutes a multi-line macro
4718 * call, and return the ExpDef structure called if so. Doesn't have
4719 * to check for an initial label - that's taken care of in
4720 * expand_mmacro - but must check numbers of parameters. Guaranteed
4721 * to be called with tline->type == TOK_ID, so the putative macro
4722 * name is easy to find.
4724 static ExpDef
*is_mmacro(Token
* tline
, Token
*** params_array
)
4730 head
= (ExpDef
*) hash_findix(&expdefs
, tline
->text
);
4733 * Efficiency: first we see if any macro exists with the given
4734 * name. If not, we can return NULL immediately. _Then_ we
4735 * count the parameters, and then we look further along the
4736 * list if necessary to find the proper ExpDef.
4738 list_for_each(ed
, head
)
4739 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4745 * OK, we have a potential macro. Count and demarcate the
4748 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4751 * So we know how many parameters we've got. Find the ExpDef
4752 * structure that handles this number.
4755 if (ed
->nparam_min
<= nparam
4756 && (ed
->plus
|| nparam
<= ed
->nparam_max
)) {
4758 * It's right, and we can use it. Add its default
4759 * parameters to the end of our list if necessary.
4761 if (ed
->defaults
&& nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4763 nasm_realloc(params
,
4764 ((ed
->nparam_min
+ ed
->ndefs
+
4765 1) * sizeof(*params
)));
4766 while (nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4767 params
[nparam
] = ed
->defaults
[nparam
- ed
->nparam_min
];
4772 * If we've gone over the maximum parameter count (and
4773 * we're in Plus mode), ignore parameters beyond
4776 if (ed
->plus
&& nparam
> ed
->nparam_max
)
4777 nparam
= ed
->nparam_max
;
4779 * Then terminate the parameter list, and leave.
4781 if (!params
) { /* need this special case */
4782 params
= nasm_malloc(sizeof(*params
));
4785 params
[nparam
] = NULL
;
4786 *params_array
= params
;
4790 * This one wasn't right: look for the next one with the
4793 list_for_each(ed
, ed
->next
)
4794 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4799 * After all that, we didn't find one with the right number of
4800 * parameters. Issue a warning, and fail to expand the macro.
4802 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4803 "macro `%s' exists, but not taking %d parameters",
4804 tline
->text
, nparam
);
4810 * Expand the multi-line macro call made by the given line, if
4811 * there is one to be expanded. If there is, push the expansion on
4812 * istk->expansion and return true. Otherwise return false.
4814 static bool expand_mmacro(Token
* tline
)
4816 Token
*label
= NULL
;
4817 int dont_prepend
= 0;
4818 Token
**params
, *t
, *mtok
;
4822 int i
, nparam
, *paramlen
;
4827 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4828 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4831 ed
= is_mmacro(t
, ¶ms
);
4837 * We have an id which isn't a macro call. We'll assume
4838 * it might be a label; we'll also check to see if a
4839 * colon follows it. Then, if there's another id after
4840 * that lot, we'll check it again for macro-hood.
4844 if (tok_type_(t
, TOK_WHITESPACE
))
4845 last
= t
, t
= t
->next
;
4846 if (tok_is_(t
, ":")) {
4848 last
= t
, t
= t
->next
;
4849 if (tok_type_(t
, TOK_WHITESPACE
))
4850 last
= t
, t
= t
->next
;
4852 if (!tok_type_(t
, TOK_ID
) || !(ed
= is_mmacro(t
, ¶ms
)))
4860 * Fix up the parameters: this involves stripping leading and
4861 * trailing whitespace, then stripping braces if they are
4864 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4865 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4867 for (i
= 0; params
[i
]; i
++) {
4869 int comma
= (!ed
->plus
|| i
< nparam
- 1);
4873 if (tok_is_(t
, "{"))
4874 t
= t
->next
, brace
= true, comma
= false;
4878 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4879 break; /* ... because we have hit a comma */
4880 if (comma
&& t
->type
== TOK_WHITESPACE
4881 && tok_is_(t
->next
, ","))
4882 break; /* ... or a space then a comma */
4883 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4884 break; /* ... or a brace */
4890 if (ed
->cur_depth
>= ed
->max_depth
) {
4891 if (ed
->max_depth
> 1) {
4893 "reached maximum macro recursion depth of %i for %s",
4894 ed
->max_depth
,ed
->name
);
4902 * OK, we have found a ExpDef structure representing a
4903 * previously defined mmacro. Create an expansion invocation
4904 * and point it back to the expansion definition. Substitution of
4905 * parameter tokens and macro-local tokens doesn't get done
4906 * until the single-line macro substitution process; this is
4907 * because delaying them allows us to change the semantics
4908 * later through %rotate.
4910 ei
= new_ExpInv(EXP_MMACRO
, ed
);
4911 ei
->name
= nasm_strdup(mname
);
4912 //ei->label = label;
4913 //ei->label_text = detoken(label, false);
4914 ei
->current
= ed
->line
;
4915 ei
->emitting
= true;
4916 //ei->iline = tline;
4917 ei
->params
= params
;
4918 ei
->nparam
= nparam
;
4920 ei
->paramlen
= paramlen
;
4923 ei
->prev
= istk
->expansion
;
4924 istk
->expansion
= ei
;
4927 * Special case: detect %00 on first invocation; if found,
4928 * avoid emitting any labels that precede the mmacro call.
4929 * ed->prepend is set to -1 when %00 is detected, else 1.
4931 if (ed
->prepend
== 0) {
4932 for (l
= ed
->line
; l
!= NULL
; l
= l
->next
) {
4933 for (t
= l
->first
; t
!= NULL
; t
= t
->next
) {
4934 if ((t
->type
== TOK_PREPROC_ID
) &&
4935 (strlen(t
->text
) == 3) &&
4936 (t
->text
[1] == '0') && (t
->text
[2] == '0')) {
4941 if (dont_prepend
< 0) {
4945 ed
->prepend
= ((dont_prepend
< 0) ? -1 : 1);
4949 * If we had a label, push it on as the first line of
4950 * the macro expansion.
4952 if (label
!= NULL
) {
4953 if (ed
->prepend
< 0) {
4954 ei
->label_text
= detoken(label
, false);
4956 if (dont_prepend
== 0) {
4958 while (t
->next
!= NULL
) {
4961 t
->next
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4964 l
->first
= copy_Token(label
);
4965 l
->next
= ei
->current
;
4970 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4976 /* The function that actually does the error reporting */
4977 static void verror(int severity
, const char *fmt
, va_list arg
)
4981 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4983 if (istk
&& istk
->mmac_depth
> 0) {
4984 ExpInv
*ei
= istk
->expansion
;
4985 int lineno
= ei
->lineno
;
4987 if (ei
->type
== EXP_MMACRO
)
4989 lineno
+= ei
->relno
;
4992 nasm_error(severity
, "(%s:%d) %s", ei
->def
->name
,
4995 nasm_error(severity
, "%s", buff
);
4999 * Since preprocessor always operate only on the line that didn't
5000 * arrived yet, we should always use ERR_OFFBY1.
5002 static void error(int severity
, const char *fmt
, ...)
5006 verror(severity
, fmt
, arg
);
5011 * Because %else etc are evaluated in the state context
5012 * of the previous branch, errors might get lost with error():
5013 * %if 0 ... %else trailing garbage ... %endif
5014 * So %else etc should report errors with this function.
5016 static void error_precond(int severity
, const char *fmt
, ...)
5020 /* Only ignore the error if it's really in a dead branch */
5021 if ((istk
!= NULL
) &&
5022 (istk
->expansion
!= NULL
) &&
5023 (istk
->expansion
->type
== EXP_IF
) &&
5024 (istk
->expansion
->def
->state
== COND_NEVER
))
5028 verror(severity
, fmt
, arg
);
5033 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
5038 istk
= nasm_malloc(sizeof(Include
));
5040 istk
->expansion
= NULL
;
5041 istk
->fp
= fopen(file
, "r");
5043 src_set_fname(nasm_strdup(file
));
5046 istk
->mmac_depth
= 0;
5048 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
5053 nested_mac_count
= 0;
5054 nested_rep_count
= 0;
5057 if (tasm_compatible_mode
) {
5058 stdmacpos
= nasm_stdmac
;
5060 stdmacpos
= nasm_stdmac_after_tasm
;
5062 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
5067 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5068 * The caller, however, will also pass in 3 for preprocess-only so
5069 * we can set __PASS__ accordingly.
5071 pass
= apass
> 2 ? 2 : apass
;
5073 dephead
= deptail
= deplist
;
5075 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
5077 strcpy(sl
->str
, file
);
5079 deptail
= &sl
->next
;
5083 * Define the __PASS__ macro. This is defined here unlike
5084 * all the other builtins, because it is special -- it varies between
5087 t
= nasm_malloc(sizeof(*t
));
5089 make_tok_num(t
, apass
);
5091 define_smacro(NULL
, "__PASS__", true, 0, t
);
5094 static char *pp_getline(void)
5105 * Fetch a tokenized line, either from the expansion
5106 * buffer or from the input file.
5110 while (1) { /* until we get a line we can use */
5112 * Fetch a tokenized line from the expansion buffer
5114 if (istk
->expansion
!= NULL
) {
5115 ei
= istk
->expansion
;
5116 if (ei
->current
!= NULL
) {
5117 if (ei
->emitting
== false) {
5122 ei
->current
= l
->next
;
5124 tline
= copy_Token(l
->first
);
5125 if (((ei
->type
== EXP_REP
) ||
5126 (ei
->type
== EXP_MMACRO
) ||
5127 (ei
->type
== EXP_WHILE
))
5128 && (ei
->def
->nolist
== false)) {
5129 char *p
= detoken(tline
, false);
5130 list
->line(LIST_MACRO
, p
);
5133 if (ei
->linnum
> -1) {
5134 src_set_linnum(src_get_linnum() + 1);
5137 } else if ((ei
->type
== EXP_REP
) &&
5138 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5139 ei
->def
->cur_depth
++;
5140 ei
->current
= ei
->def
->line
;
5143 } else if ((ei
->type
== EXP_WHILE
) &&
5144 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5145 ei
->current
= ei
->def
->line
;
5147 tline
= copy_Token(ei
->current
->first
);
5148 j
= if_condition(tline
, PP_WHILE
);
5150 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
5151 if (j
== COND_IF_TRUE
) {
5152 ei
->current
= ei
->current
->next
;
5153 ei
->def
->cur_depth
++;
5155 ei
->emitting
= false;
5157 ei
->def
->cur_depth
= ei
->def
->max_depth
;
5161 istk
->expansion
= ei
->prev
;
5164 if ((ei
->emitting
== true) &&
5165 (ed
->max_depth
== DEADMAN_LIMIT
) &&
5166 (ed
->cur_depth
== DEADMAN_LIMIT
)
5168 error(ERR_FATAL
, "runaway expansion detected, aborting");
5170 if (ed
->cur_depth
> 0) {
5172 } else if ((ed
->type
!= EXP_MMACRO
) && (ed
->type
!= EXP_IF
)) {
5173 /***** should this really be right here??? *****/
5175 Line *l = NULL, *ll = NULL;
5176 for (l = ed->line; l != NULL;) {
5177 if (l->first != NULL) {
5178 free_tlist(l->first);
5185 expansions = ed->prev;
5189 if ((ei
->type
== EXP_REP
) ||
5190 (ei
->type
== EXP_MMACRO
) ||
5191 (ei
->type
== EXP_WHILE
)) {
5192 list
->downlevel(LIST_MACRO
);
5193 if (ei
->type
== EXP_MMACRO
) {
5198 if (ei
->linnum
> -1) {
5199 src_set_linnum(ei
->linnum
);
5207 * Read in line from input and tokenize
5210 if (line
) { /* from the current input file */
5211 line
= prepreproc(line
);
5212 tline
= tokenize(line
);
5218 * The current file has ended; work down the istk
5223 if (i
->expansion
!= NULL
) {
5225 "end of file while still in an expansion");
5227 /* only set line and file name if there's a next node */
5229 src_set_linnum(i
->lineno
);
5230 nasm_free(src_set_fname(i
->fname
));
5232 if ((i
->next
== NULL
) && (finals
!= NULL
)) {
5234 ei
= new_ExpInv(EXP_FINAL
, NULL
);
5235 ei
->emitting
= true;
5236 ei
->current
= finals
;
5237 istk
->expansion
= ei
;
5242 list
->downlevel(LIST_INCLUDE
);
5245 if (finals
!= NULL
) {
5255 if (defining
== NULL
) {
5256 tline
= expand_mmac_params(tline
);
5260 * Check the line to see if it's a preprocessor directive.
5262 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5264 } else if (defining
!= NULL
) {
5266 * We're defining an expansion. We emit nothing at all,
5267 * and just shove the tokenized line on to the definition.
5269 if (defining
->ignoring
== false) {
5270 Line
*l
= new_Line();
5272 if (defining
->line
== NULL
) {
5276 defining
->last
->next
= l
;
5280 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5282 defining
->linecount
++;
5284 } else if ((istk
->expansion
!= NULL
) &&
5285 (istk
->expansion
->emitting
!= true)) {
5287 * We're in a non-emitting branch of an expansion.
5288 * Emit nothing at all, not even a blank line: when we
5289 * emerge from the expansion we'll give a line-number
5290 * directive so we keep our place correctly.
5295 tline
= expand_smacro(tline
);
5296 if (expand_mmacro(tline
) != true) {
5298 * De-tokenize the line again, and emit it.
5300 line
= detoken(tline
, true);
5311 static void pp_cleanup(int pass
)
5313 if (defining
!= NULL
) {
5314 error(ERR_NONFATAL
, "end of file while still defining an expansion");
5315 while (defining
!= NULL
) {
5316 ExpDef
*ed
= defining
;
5317 defining
= ed
->prev
;
5322 while (cstk
!= NULL
)
5325 while (istk
!= NULL
) {
5329 nasm_free(i
->fname
);
5331 while (i
->expansion
!= NULL
) {
5332 ExpInv
*ei
= i
->expansion
;
5333 i
->expansion
= ei
->prev
;
5339 nasm_free(src_set_fname(NULL
));
5344 while ((i
= ipath
)) {
5353 void pp_include_path(char *path
)
5357 i
= nasm_malloc(sizeof(IncPath
));
5358 i
->path
= path
? nasm_strdup(path
) : NULL
;
5371 void pp_pre_include(char *fname
)
5373 Token
*inc
, *space
, *name
;
5376 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5377 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5378 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5386 void pp_pre_define(char *definition
)
5392 equals
= strchr(definition
, '=');
5393 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5394 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5397 space
->next
= tokenize(definition
);
5407 void pp_pre_undefine(char *definition
)
5412 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5413 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5414 space
->next
= tokenize(definition
);
5423 * This function is used to assist with "runtime" preprocessor
5424 * directives, e.g. pp_runtime("%define __BITS__ 64");
5426 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5427 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5430 void pp_runtime(char *definition
)
5434 def
= tokenize(definition
);
5435 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5440 void pp_extra_stdmac(macros_t
*macros
)
5442 extrastdmac
= macros
;
5445 static void make_tok_num(Token
* tok
, int64_t val
)
5448 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5449 tok
->text
= nasm_strdup(numbuf
);
5450 tok
->type
= TOK_NUMBER
;