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
!= NULL
) ? 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 Line
*new_Line(void)
1381 Line
*l
= nasm_malloc(sizeof(Line
));
1389 * Initialize a new Expansion Definition
1391 static ExpDef
*new_ExpDef(int exp_type
)
1393 ExpDef
*ed
= nasm_malloc(sizeof(ExpDef
));
1396 ed
->type
= exp_type
;
1400 ed
->casesense
= true;
1408 ed
->defaults
= NULL
;
1410 ed
->state
= COND_NEVER
;
1415 ed
->ignoring
= false;
1421 * Initialize a new Expansion Instance
1423 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
)
1426 ExpInv
*ei
= nasm_malloc(sizeof(ExpInv
));
1428 ei
->type
= exp_type
;
1432 ei
->label_text
= NULL
;
1438 ei
->paramlen
= NULL
;
1439 ei
->unique
= unique
;
1440 ei
->emitting
= false;
1442 if ((istk
->mmac_depth
< 1) &&
1443 (istk
->expansion
== NULL
) &&
1445 (ed
->type
!= EXP_MMACRO
) &&
1446 (ed
->type
!= EXP_REP
) &&
1447 (ed
->type
!= EXP_WHILE
)) {
1448 ei
->linnum
= src_get_linnum();
1449 src_set_linnum(ei
->linnum
- ed
->linecount
- 1);
1453 if ((istk
->expansion
== NULL
) ||
1454 (ei
->type
== EXP_MMACRO
)) {
1457 ei
->relno
= istk
->expansion
->lineno
;
1459 ei
->relno
-= (ed
->linecount
+ 1);
1466 * A scanner, suitable for use by the expression evaluator, which
1467 * operates on a line of Tokens. Expects a pointer to a pointer to
1468 * the first token in the line to be passed in as its private_data
1471 * FIX: This really needs to be unified with stdscan.
1473 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1475 Token
**tlineptr
= private_data
;
1477 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1481 *tlineptr
= tline
? tline
->next
: NULL
;
1482 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1483 tline
->type
== TOK_COMMENT
));
1486 return tokval
->t_type
= TOKEN_EOS
;
1488 tokval
->t_charptr
= tline
->text
;
1490 if (tline
->text
[0] == '$' && !tline
->text
[1])
1491 return tokval
->t_type
= TOKEN_HERE
;
1492 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1493 return tokval
->t_type
= TOKEN_BASE
;
1495 if (tline
->type
== TOK_ID
) {
1496 p
= tokval
->t_charptr
= tline
->text
;
1498 tokval
->t_charptr
++;
1499 return tokval
->t_type
= TOKEN_ID
;
1502 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1503 if (r
>= p
+MAX_KEYWORD
)
1504 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1505 *s
++ = nasm_tolower(*r
);
1508 /* right, so we have an identifier sitting in temp storage. now,
1509 * is it actually a register or instruction name, or what? */
1510 return nasm_token_hash(ourcopy
, tokval
);
1513 if (tline
->type
== TOK_NUMBER
) {
1515 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1516 tokval
->t_charptr
= tline
->text
;
1518 return tokval
->t_type
= TOKEN_ERRNUM
;
1520 return tokval
->t_type
= TOKEN_NUM
;
1523 if (tline
->type
== TOK_FLOAT
) {
1524 return tokval
->t_type
= TOKEN_FLOAT
;
1527 if (tline
->type
== TOK_STRING
) {
1530 bq
= tline
->text
[0];
1531 tokval
->t_charptr
= tline
->text
;
1532 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1534 if (ep
[0] != bq
|| ep
[1] != '\0')
1535 return tokval
->t_type
= TOKEN_ERRSTR
;
1537 return tokval
->t_type
= TOKEN_STR
;
1540 if (tline
->type
== TOK_OTHER
) {
1541 if (!strcmp(tline
->text
, "<<"))
1542 return tokval
->t_type
= TOKEN_SHL
;
1543 if (!strcmp(tline
->text
, ">>"))
1544 return tokval
->t_type
= TOKEN_SHR
;
1545 if (!strcmp(tline
->text
, "//"))
1546 return tokval
->t_type
= TOKEN_SDIV
;
1547 if (!strcmp(tline
->text
, "%%"))
1548 return tokval
->t_type
= TOKEN_SMOD
;
1549 if (!strcmp(tline
->text
, "=="))
1550 return tokval
->t_type
= TOKEN_EQ
;
1551 if (!strcmp(tline
->text
, "<>"))
1552 return tokval
->t_type
= TOKEN_NE
;
1553 if (!strcmp(tline
->text
, "!="))
1554 return tokval
->t_type
= TOKEN_NE
;
1555 if (!strcmp(tline
->text
, "<="))
1556 return tokval
->t_type
= TOKEN_LE
;
1557 if (!strcmp(tline
->text
, ">="))
1558 return tokval
->t_type
= TOKEN_GE
;
1559 if (!strcmp(tline
->text
, "&&"))
1560 return tokval
->t_type
= TOKEN_DBL_AND
;
1561 if (!strcmp(tline
->text
, "^^"))
1562 return tokval
->t_type
= TOKEN_DBL_XOR
;
1563 if (!strcmp(tline
->text
, "||"))
1564 return tokval
->t_type
= TOKEN_DBL_OR
;
1568 * We have no other options: just return the first character of
1571 return tokval
->t_type
= tline
->text
[0];
1575 * Compare a string to the name of an existing macro; this is a
1576 * simple wrapper which calls either strcmp or nasm_stricmp
1577 * depending on the value of the `casesense' parameter.
1579 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1581 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1585 * Compare a string to the name of an existing macro; this is a
1586 * simple wrapper which calls either strcmp or nasm_stricmp
1587 * depending on the value of the `casesense' parameter.
1589 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1591 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1595 * Return the Context structure associated with a %$ token. Return
1596 * NULL, having _already_ reported an error condition, if the
1597 * context stack isn't deep enough for the supplied number of $
1599 * If all_contexts == true, contexts that enclose current are
1600 * also scanned for such smacro, until it is found; if not -
1601 * only the context that directly results from the number of $'s
1602 * in variable's name.
1604 * If "namep" is non-NULL, set it to the pointer to the macro name
1605 * tail, i.e. the part beyond %$...
1607 static Context
*get_ctx(const char *name
, const char **namep
,
1617 if (!name
|| name
[0] != '%' || name
[1] != '$')
1621 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1628 while (ctx
&& *name
== '$') {
1634 error(ERR_NONFATAL
, "`%s': context stack is only"
1635 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1646 /* Search for this smacro in found context */
1647 m
= hash_findix(&ctx
->localmac
, name
);
1649 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1660 * Check to see if a file is already in a string list
1662 static bool in_list(const StrList
*list
, const char *str
)
1665 if (!strcmp(list
->str
, str
))
1673 * Open an include file. This routine must always return a valid
1674 * file pointer if it returns - it's responsible for throwing an
1675 * ERR_FATAL and bombing out completely if not. It should also try
1676 * the include path one by one until it finds the file or reaches
1677 * the end of the path.
1679 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1684 IncPath
*ip
= ipath
;
1685 int len
= strlen(file
);
1686 size_t prefix_len
= 0;
1690 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1691 memcpy(sl
->str
, prefix
, prefix_len
);
1692 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1693 fp
= fopen(sl
->str
, "r");
1694 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1712 prefix_len
= strlen(prefix
);
1714 /* -MG given and file not found */
1715 if (dhead
&& !in_list(*dhead
, file
)) {
1716 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1718 strcpy(sl
->str
, file
);
1726 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1731 * Determine if we should warn on defining a single-line macro of
1732 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1733 * return true if _any_ single-line macro of that name is defined.
1734 * Otherwise, will return true if a single-line macro with either
1735 * `nparam' or no parameters is defined.
1737 * If a macro with precisely the right number of parameters is
1738 * defined, or nparam is -1, the address of the definition structure
1739 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1740 * is NULL, no action will be taken regarding its contents, and no
1743 * Note that this is also called with nparam zero to resolve
1746 * If you already know which context macro belongs to, you can pass
1747 * the context pointer as first parameter; if you won't but name begins
1748 * with %$ the context will be automatically computed. If all_contexts
1749 * is true, macro will be searched in outer contexts as well.
1752 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1755 struct hash_table
*smtbl
;
1759 smtbl
= &ctx
->localmac
;
1760 } else if (name
[0] == '%' && name
[1] == '$') {
1762 ctx
= get_ctx(name
, &name
, false);
1764 return false; /* got to return _something_ */
1765 smtbl
= &ctx
->localmac
;
1769 m
= (SMacro
*) hash_findix(smtbl
, name
);
1772 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1773 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1775 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1789 * Count and mark off the parameters in a multi-line macro call.
1790 * This is called both from within the multi-line macro expansion
1791 * code, and also to mark off the default parameters when provided
1792 * in a %macro definition line.
1794 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1796 int paramsize
, brace
;
1798 *nparam
= paramsize
= 0;
1801 /* +1: we need space for the final NULL */
1802 if (*nparam
+1 >= paramsize
) {
1803 paramsize
+= PARAM_DELTA
;
1804 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1808 if (tok_is_(t
, "{"))
1810 (*params
)[(*nparam
)++] = t
;
1811 while (tok_isnt_(t
, brace
? "}" : ","))
1813 if (t
) { /* got a comma/brace */
1817 * Now we've found the closing brace, look further
1821 if (tok_isnt_(t
, ",")) {
1823 "braces do not enclose all of macro parameter");
1824 while (tok_isnt_(t
, ","))
1828 t
= t
->next
; /* eat the comma */
1835 * Determine whether one of the various `if' conditions is true or
1838 * We must free the tline we get passed.
1840 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1842 enum pp_conditional i
= PP_COND(ct
);
1844 Token
*t
, *tt
, **tptr
, *origline
;
1845 struct tokenval tokval
;
1847 enum pp_token_type needtype
;
1854 j
= false; /* have we matched yet? */
1859 if (tline
->type
!= TOK_ID
) {
1861 "`%s' expects context identifiers", pp_directives
[ct
]);
1862 free_tlist(origline
);
1865 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1867 tline
= tline
->next
;
1872 j
= false; /* have we matched yet? */
1875 if (!tline
|| (tline
->type
!= TOK_ID
&&
1876 (tline
->type
!= TOK_PREPROC_ID
||
1877 tline
->text
[1] != '$'))) {
1879 "`%s' expects macro identifiers", pp_directives
[ct
]);
1882 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1884 tline
= tline
->next
;
1889 tline
= expand_smacro(tline
);
1890 j
= false; /* have we matched yet? */
1893 if (!tline
|| (tline
->type
!= TOK_ID
&&
1894 tline
->type
!= TOK_STRING
&&
1895 (tline
->type
!= TOK_PREPROC_ID
||
1896 tline
->text
[1] != '!'))) {
1898 "`%s' expects environment variable names",
1903 if (tline
->type
== TOK_PREPROC_ID
)
1904 p
+= 2; /* Skip leading %! */
1905 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1906 nasm_unquote_cstr(p
, ct
);
1909 tline
= tline
->next
;
1915 tline
= expand_smacro(tline
);
1917 while (tok_isnt_(tt
, ","))
1921 "`%s' expects two comma-separated arguments",
1926 j
= true; /* assume equality unless proved not */
1927 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1928 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1929 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1933 if (t
->type
== TOK_WHITESPACE
) {
1937 if (tt
->type
== TOK_WHITESPACE
) {
1941 if (tt
->type
!= t
->type
) {
1942 j
= false; /* found mismatching tokens */
1945 /* When comparing strings, need to unquote them first */
1946 if (t
->type
== TOK_STRING
) {
1947 size_t l1
= nasm_unquote(t
->text
, NULL
);
1948 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1954 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1958 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1959 j
= false; /* found mismatching tokens */
1966 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1967 j
= false; /* trailing gunk on one end or other */
1973 ExpDef searching
, *ed
;
1976 tline
= expand_id(tline
);
1977 if (!tok_type_(tline
, TOK_ID
)) {
1979 "`%s' expects a macro name", pp_directives
[ct
]);
1982 searching
.name
= nasm_strdup(tline
->text
);
1983 searching
.casesense
= true;
1984 searching
.plus
= false;
1985 searching
.nolist
= false;
1986 //searching.in_progress = 0;
1987 searching
.max_depth
= 0;
1988 //searching.rep_nest = NULL;
1989 searching
.nparam_min
= 0;
1990 searching
.nparam_max
= INT_MAX
;
1991 tline
= expand_smacro(tline
->next
);
1994 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1996 "`%s' expects a parameter count or nothing",
1999 searching
.nparam_min
= searching
.nparam_max
=
2000 readnum(tline
->text
, &j
);
2003 "unable to parse parameter count `%s'",
2006 if (tline
&& tok_is_(tline
->next
, "-")) {
2007 tline
= tline
->next
->next
;
2008 if (tok_is_(tline
, "*"))
2009 searching
.nparam_max
= INT_MAX
;
2010 else if (!tok_type_(tline
, TOK_NUMBER
))
2012 "`%s' expects a parameter count after `-'",
2015 searching
.nparam_max
= readnum(tline
->text
, &j
);
2018 "unable to parse parameter count `%s'",
2020 if (searching
.nparam_min
> searching
.nparam_max
)
2022 "minimum parameter count exceeds maximum");
2025 if (tline
&& tok_is_(tline
->next
, "+")) {
2026 tline
= tline
->next
;
2027 searching
.plus
= true;
2029 ed
= (ExpDef
*) hash_findix(&expdefs
, searching
.name
);
2030 while (ed
!= NULL
) {
2031 if (!strcmp(ed
->name
, searching
.name
) &&
2032 (ed
->nparam_min
<= searching
.nparam_max
2034 && (searching
.nparam_min
<= ed
->nparam_max
2041 if (tline
&& tline
->next
)
2042 error(ERR_WARNING
|ERR_PASS1
,
2043 "trailing garbage after %%ifmacro ignored");
2044 nasm_free(searching
.name
);
2053 needtype
= TOK_NUMBER
;
2056 needtype
= TOK_STRING
;
2060 t
= tline
= expand_smacro(tline
);
2062 while (tok_type_(t
, TOK_WHITESPACE
) ||
2063 (needtype
== TOK_NUMBER
&&
2064 tok_type_(t
, TOK_OTHER
) &&
2065 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
2069 j
= tok_type_(t
, needtype
);
2073 t
= tline
= expand_smacro(tline
);
2074 while (tok_type_(t
, TOK_WHITESPACE
))
2079 t
= t
->next
; /* Skip the actual token */
2080 while (tok_type_(t
, TOK_WHITESPACE
))
2082 j
= !t
; /* Should be nothing left */
2087 t
= tline
= expand_smacro(tline
);
2088 while (tok_type_(t
, TOK_WHITESPACE
))
2091 j
= !t
; /* Should be empty */
2095 t
= tline
= expand_smacro(tline
);
2097 tokval
.t_type
= TOKEN_INVALID
;
2098 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2099 NULL
, pass
| CRITICAL
, error
, NULL
);
2103 error(ERR_WARNING
|ERR_PASS1
,
2104 "trailing garbage after expression ignored");
2105 if (!is_simple(evalresult
)) {
2107 "non-constant value given to `%s'", pp_directives
[ct
]);
2110 j
= reloc_value(evalresult
) != 0;
2115 "preprocessor directive `%s' not yet implemented",
2120 free_tlist(origline
);
2121 return j
^ PP_NEGATIVE(ct
);
2124 free_tlist(origline
);
2129 * Common code for defining an smacro
2131 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2132 int nparam
, Token
*expansion
)
2134 SMacro
*smac
, **smhead
;
2135 struct hash_table
*smtbl
;
2137 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2139 error(ERR_WARNING
|ERR_PASS1
,
2140 "single-line macro `%s' defined both with and"
2141 " without parameters", mname
);
2143 * Some instances of the old code considered this a failure,
2144 * some others didn't. What is the right thing to do here?
2146 free_tlist(expansion
);
2147 return false; /* Failure */
2150 * We're redefining, so we have to take over an
2151 * existing SMacro structure. This means freeing
2152 * what was already in it.
2154 nasm_free(smac
->name
);
2155 free_tlist(smac
->expansion
);
2158 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2159 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2160 smac
= nasm_malloc(sizeof(SMacro
));
2161 smac
->next
= *smhead
;
2164 smac
->name
= nasm_strdup(mname
);
2165 smac
->casesense
= casesense
;
2166 smac
->nparam
= nparam
;
2167 smac
->expansion
= expansion
;
2168 smac
->in_progress
= false;
2169 return true; /* Success */
2173 * Undefine an smacro
2175 static void undef_smacro(Context
*ctx
, const char *mname
)
2177 SMacro
**smhead
, *s
, **sp
;
2178 struct hash_table
*smtbl
;
2180 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2181 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2185 * We now have a macro name... go hunt for it.
2188 while ((s
= *sp
) != NULL
) {
2189 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2192 free_tlist(s
->expansion
);
2202 * Parse a mmacro specification.
2204 static bool parse_mmacro_spec(Token
*tline
, ExpDef
*def
, const char *directive
)
2208 tline
= tline
->next
;
2210 tline
= expand_id(tline
);
2211 if (!tok_type_(tline
, TOK_ID
)) {
2212 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2216 def
->name
= nasm_strdup(tline
->text
);
2218 def
->nolist
= false;
2219 // def->in_progress = 0;
2220 // def->rep_nest = NULL;
2221 def
->nparam_min
= 0;
2222 def
->nparam_max
= 0;
2224 tline
= expand_smacro(tline
->next
);
2226 if (!tok_type_(tline
, TOK_NUMBER
)) {
2227 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2229 def
->nparam_min
= def
->nparam_max
=
2230 readnum(tline
->text
, &err
);
2233 "unable to parse parameter count `%s'", tline
->text
);
2235 if (tline
&& tok_is_(tline
->next
, "-")) {
2236 tline
= tline
->next
->next
;
2237 if (tok_is_(tline
, "*")) {
2238 def
->nparam_max
= INT_MAX
;
2239 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2241 "`%s' expects a parameter count after `-'", directive
);
2243 def
->nparam_max
= readnum(tline
->text
, &err
);
2245 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2248 if (def
->nparam_min
> def
->nparam_max
) {
2249 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2253 if (tline
&& tok_is_(tline
->next
, "+")) {
2254 tline
= tline
->next
;
2257 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2258 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2259 tline
= tline
->next
;
2264 * Handle default parameters.
2266 if (tline
&& tline
->next
) {
2267 def
->dlist
= tline
->next
;
2269 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2272 def
->defaults
= NULL
;
2276 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2278 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2279 "too many default macro parameters");
2286 * Decode a size directive
2288 static int parse_size(const char *str
) {
2289 static const char *size_names
[] =
2290 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2291 static const int sizes
[] =
2292 { 0, 1, 4, 16, 8, 10, 2, 32 };
2294 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2298 * find and process preprocessor directive in passed line
2299 * Find out if a line contains a preprocessor directive, and deal
2302 * If a directive _is_ found, it is the responsibility of this routine
2303 * (and not the caller) to free_tlist() the line.
2305 * @param tline a pointer to the current tokeninzed line linked list
2306 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2309 static int do_directive(Token
* tline
)
2311 enum preproc_token i
;
2324 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2325 struct tokenval tokval
;
2327 ExpDef
*ed
, *eed
, **edhead
;
2336 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2337 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2338 || tline
->text
[1] == '!'))
2339 return NO_DIRECTIVE_FOUND
;
2341 i
= pp_token_hash(tline
->text
);
2345 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2346 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2348 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2351 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2352 /* Directive to tell NASM what the default stack size is. The
2353 * default is for a 16-bit stack, and this can be overriden with
2356 tline
= tline
->next
;
2357 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2358 tline
= tline
->next
;
2359 if (!tline
|| tline
->type
!= TOK_ID
) {
2360 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2361 free_tlist(origline
);
2362 return DIRECTIVE_FOUND
;
2364 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2365 /* All subsequent ARG directives are for a 32-bit stack */
2367 StackPointer
= "ebp";
2370 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2371 /* All subsequent ARG directives are for a 64-bit stack */
2373 StackPointer
= "rbp";
2376 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2377 /* All subsequent ARG directives are for a 16-bit stack,
2378 * far function call.
2381 StackPointer
= "bp";
2384 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2385 /* All subsequent ARG directives are for a 16-bit stack,
2386 * far function call. We don't support near functions.
2389 StackPointer
= "bp";
2393 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2394 free_tlist(origline
);
2395 return DIRECTIVE_FOUND
;
2397 free_tlist(origline
);
2398 return DIRECTIVE_FOUND
;
2401 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2402 /* TASM like ARG directive to define arguments to functions, in
2403 * the following form:
2405 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2409 char *arg
, directive
[256];
2410 int size
= StackSize
;
2412 /* Find the argument name */
2413 tline
= tline
->next
;
2414 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2415 tline
= tline
->next
;
2416 if (!tline
|| tline
->type
!= TOK_ID
) {
2417 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2418 free_tlist(origline
);
2419 return DIRECTIVE_FOUND
;
2423 /* Find the argument size type */
2424 tline
= tline
->next
;
2425 if (!tline
|| tline
->type
!= TOK_OTHER
2426 || tline
->text
[0] != ':') {
2428 "Syntax error processing `%%arg' directive");
2429 free_tlist(origline
);
2430 return DIRECTIVE_FOUND
;
2432 tline
= tline
->next
;
2433 if (!tline
|| tline
->type
!= TOK_ID
) {
2434 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2435 free_tlist(origline
);
2436 return DIRECTIVE_FOUND
;
2439 /* Allow macro expansion of type parameter */
2440 tt
= tokenize(tline
->text
);
2441 tt
= expand_smacro(tt
);
2442 size
= parse_size(tt
->text
);
2445 "Invalid size type for `%%arg' missing directive");
2447 free_tlist(origline
);
2448 return DIRECTIVE_FOUND
;
2452 /* Round up to even stack slots */
2453 size
= ALIGN(size
, StackSize
);
2455 /* Now define the macro for the argument */
2456 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2457 arg
, StackPointer
, offset
);
2458 do_directive(tokenize(directive
));
2461 /* Move to the next argument in the list */
2462 tline
= tline
->next
;
2463 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2464 tline
= tline
->next
;
2465 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2467 free_tlist(origline
);
2468 return DIRECTIVE_FOUND
;
2471 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2472 /* TASM like LOCAL directive to define local variables for a
2473 * function, in the following form:
2475 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2477 * The '= LocalSize' at the end is ignored by NASM, but is
2478 * required by TASM to define the local parameter size (and used
2479 * by the TASM macro package).
2481 offset
= LocalOffset
;
2483 char *local
, directive
[256];
2484 int size
= StackSize
;
2486 /* Find the argument name */
2487 tline
= tline
->next
;
2488 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2489 tline
= tline
->next
;
2490 if (!tline
|| tline
->type
!= TOK_ID
) {
2492 "`%%local' missing argument parameter");
2493 free_tlist(origline
);
2494 return DIRECTIVE_FOUND
;
2496 local
= tline
->text
;
2498 /* Find the argument size type */
2499 tline
= tline
->next
;
2500 if (!tline
|| tline
->type
!= TOK_OTHER
2501 || tline
->text
[0] != ':') {
2503 "Syntax error processing `%%local' directive");
2504 free_tlist(origline
);
2505 return DIRECTIVE_FOUND
;
2507 tline
= tline
->next
;
2508 if (!tline
|| tline
->type
!= TOK_ID
) {
2510 "`%%local' missing size type parameter");
2511 free_tlist(origline
);
2512 return DIRECTIVE_FOUND
;
2515 /* Allow macro expansion of type parameter */
2516 tt
= tokenize(tline
->text
);
2517 tt
= expand_smacro(tt
);
2518 size
= parse_size(tt
->text
);
2521 "Invalid size type for `%%local' missing directive");
2523 free_tlist(origline
);
2524 return DIRECTIVE_FOUND
;
2528 /* Round up to even stack slots */
2529 size
= ALIGN(size
, StackSize
);
2531 offset
+= size
; /* Negative offset, increment before */
2533 /* Now define the macro for the argument */
2534 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2535 local
, StackPointer
, offset
);
2536 do_directive(tokenize(directive
));
2538 /* Now define the assign to setup the enter_c macro correctly */
2539 snprintf(directive
, sizeof(directive
),
2540 "%%assign %%$localsize %%$localsize+%d", size
);
2541 do_directive(tokenize(directive
));
2543 /* Move to the next argument in the list */
2544 tline
= tline
->next
;
2545 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2546 tline
= tline
->next
;
2547 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2548 LocalOffset
= offset
;
2549 free_tlist(origline
);
2550 return DIRECTIVE_FOUND
;
2553 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2555 error(ERR_WARNING
|ERR_PASS1
,
2556 "trailing garbage after `%%clear' ignored");
2559 free_tlist(origline
);
2560 return DIRECTIVE_FOUND
;
2563 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2564 t
= tline
->next
= expand_smacro(tline
->next
);
2566 if (!t
|| (t
->type
!= TOK_STRING
&&
2567 t
->type
!= TOK_INTERNAL_STRING
)) {
2568 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2569 free_tlist(origline
);
2570 return DIRECTIVE_FOUND
; /* but we did _something_ */
2573 error(ERR_WARNING
|ERR_PASS1
,
2574 "trailing garbage after `%%depend' ignored");
2576 if (t
->type
!= TOK_INTERNAL_STRING
)
2577 nasm_unquote_cstr(p
, i
);
2578 if (dephead
&& !in_list(*dephead
, p
)) {
2579 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2583 deptail
= &sl
->next
;
2585 free_tlist(origline
);
2586 return DIRECTIVE_FOUND
;
2589 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2590 t
= tline
->next
= expand_smacro(tline
->next
);
2593 if (!t
|| (t
->type
!= TOK_STRING
&&
2594 t
->type
!= TOK_INTERNAL_STRING
)) {
2595 error(ERR_NONFATAL
, "`%%include' expects a file name");
2596 free_tlist(origline
);
2597 return DIRECTIVE_FOUND
; /* but we did _something_ */
2600 error(ERR_WARNING
|ERR_PASS1
,
2601 "trailing garbage after `%%include' ignored");
2603 if (t
->type
!= TOK_INTERNAL_STRING
)
2604 nasm_unquote_cstr(p
, i
);
2605 inc
= nasm_malloc(sizeof(Include
));
2607 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2609 /* -MG given but file not found */
2612 inc
->fname
= src_set_fname(nasm_strdup(p
));
2613 inc
->lineno
= src_set_linnum(0);
2615 inc
->expansion
= NULL
;
2617 list
->uplevel(LIST_INCLUDE
);
2619 free_tlist(origline
);
2620 return DIRECTIVE_FOUND
;
2623 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2625 static macros_t
*use_pkg
;
2626 const char *pkg_macro
= NULL
;
2628 tline
= tline
->next
;
2630 tline
= expand_id(tline
);
2632 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2633 tline
->type
!= TOK_INTERNAL_STRING
&&
2634 tline
->type
!= TOK_ID
)) {
2635 error(ERR_NONFATAL
, "`%%use' expects a package name");
2636 free_tlist(origline
);
2637 return DIRECTIVE_FOUND
; /* but we did _something_ */
2640 error(ERR_WARNING
|ERR_PASS1
,
2641 "trailing garbage after `%%use' ignored");
2642 if (tline
->type
== TOK_STRING
)
2643 nasm_unquote_cstr(tline
->text
, i
);
2644 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2646 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2648 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2649 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2650 /* Not already included, go ahead and include it */
2651 stdmacpos
= use_pkg
;
2653 free_tlist(origline
);
2654 return DIRECTIVE_FOUND
;
2659 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2660 tline
= tline
->next
;
2662 tline
= expand_id(tline
);
2664 if (!tok_type_(tline
, TOK_ID
)) {
2665 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2667 free_tlist(origline
);
2668 return DIRECTIVE_FOUND
; /* but we did _something_ */
2671 error(ERR_WARNING
|ERR_PASS1
,
2672 "trailing garbage after `%s' ignored",
2674 p
= nasm_strdup(tline
->text
);
2676 p
= NULL
; /* Anonymous */
2680 ctx
= nasm_malloc(sizeof(Context
));
2682 hash_init(&ctx
->localmac
, HASH_SMALL
);
2684 ctx
->number
= unique
++;
2689 error(ERR_NONFATAL
, "`%s': context stack is empty",
2691 } else if (i
== PP_POP
) {
2692 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2693 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2695 cstk
->name
? cstk
->name
: "anonymous", p
);
2700 nasm_free(cstk
->name
);
2706 free_tlist(origline
);
2707 return DIRECTIVE_FOUND
;
2709 severity
= ERR_FATAL
;
2712 severity
= ERR_NONFATAL
;
2715 severity
= ERR_WARNING
|ERR_WARN_USER
;
2719 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2721 /* Only error out if this is the final pass */
2722 if (pass
!= 2 && i
!= PP_FATAL
)
2723 return DIRECTIVE_FOUND
;
2725 tline
->next
= expand_smacro(tline
->next
);
2726 tline
= tline
->next
;
2728 t
= tline
? tline
->next
: NULL
;
2730 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2731 /* The line contains only a quoted string */
2733 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2734 error(severity
, "%s", p
);
2736 /* Not a quoted string, or more than a quoted string */
2737 p
= detoken(tline
, false);
2738 error(severity
, "%s", p
);
2741 free_tlist(origline
);
2742 return DIRECTIVE_FOUND
;
2746 if (defining
!= NULL
) {
2747 if (defining
->type
== EXP_IF
) {
2748 defining
->def_depth
++;
2750 return NO_DIRECTIVE_FOUND
;
2752 if ((istk
->expansion
!= NULL
) &&
2753 (istk
->expansion
->emitting
== false)) {
2756 j
= if_condition(tline
->next
, i
);
2757 tline
->next
= NULL
; /* it got freed */
2758 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
2760 ed
= new_ExpDef(EXP_IF
);
2766 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
2767 ed
->prev
= defining
;
2769 free_tlist(origline
);
2770 return DIRECTIVE_FOUND
;
2773 if (defining
!= NULL
) {
2774 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2775 return NO_DIRECTIVE_FOUND
;
2778 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2779 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2781 switch (defining
->state
) {
2783 defining
->state
= COND_DONE
;
2784 defining
->ignoring
= true;
2789 defining
->ignoring
= true;
2792 case COND_ELSE_TRUE
:
2793 case COND_ELSE_FALSE
:
2794 error_precond(ERR_WARNING
|ERR_PASS1
,
2795 "`%%elif' after `%%else' ignored");
2796 defining
->state
= COND_NEVER
;
2797 defining
->ignoring
= true;
2802 * IMPORTANT: In the case of %if, we will already have
2803 * called expand_mmac_params(); however, if we're
2804 * processing an %elif we must have been in a
2805 * non-emitting mode, which would have inhibited
2806 * the normal invocation of expand_mmac_params().
2807 * Therefore, we have to do it explicitly here.
2809 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2810 tline
->next
= NULL
; /* it got freed */
2812 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2813 defining
->ignoring
= ((defining
->state
== COND_IF_TRUE
) ? false : true);
2816 free_tlist(origline
);
2817 return DIRECTIVE_FOUND
;
2820 if (defining
!= NULL
) {
2821 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2822 return NO_DIRECTIVE_FOUND
;
2826 error_precond(ERR_WARNING
|ERR_PASS1
,
2827 "trailing garbage after `%%else' ignored");
2828 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2829 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2831 switch (defining
->state
) {
2834 defining
->state
= COND_ELSE_FALSE
;
2835 defining
->ignoring
= true;
2839 defining
->ignoring
= true;
2843 defining
->state
= COND_ELSE_TRUE
;
2844 defining
->ignoring
= false;
2847 case COND_ELSE_TRUE
:
2848 case COND_ELSE_FALSE
:
2849 error_precond(ERR_WARNING
|ERR_PASS1
,
2850 "`%%else' after `%%else' ignored.");
2851 defining
->state
= COND_NEVER
;
2852 defining
->ignoring
= true;
2855 free_tlist(origline
);
2856 return DIRECTIVE_FOUND
;
2859 if (defining
!= NULL
) {
2860 if (defining
->type
== EXP_IF
) {
2861 if (defining
->def_depth
> 0) {
2862 defining
->def_depth
--;
2863 return NO_DIRECTIVE_FOUND
;
2866 return NO_DIRECTIVE_FOUND
;
2870 error_precond(ERR_WARNING
|ERR_PASS1
,
2871 "trailing garbage after `%%endif' ignored");
2872 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2873 error(ERR_NONFATAL
, "`%%endif': no matching `%%if'");
2874 return DIRECTIVE_FOUND
;
2877 defining
= ed
->prev
;
2878 ed
->prev
= expansions
;
2880 ei
= new_ExpInv(EXP_IF
, ed
);
2881 ei
->current
= ed
->line
;
2882 ei
->emitting
= true;
2883 ei
->prev
= istk
->expansion
;
2884 istk
->expansion
= ei
;
2885 free_tlist(origline
);
2886 return DIRECTIVE_FOUND
;
2892 if (defining
!= NULL
) {
2893 if (defining
->type
== EXP_MMACRO
) {
2894 defining
->def_depth
++;
2896 return NO_DIRECTIVE_FOUND
;
2898 ed
= new_ExpDef(EXP_MMACRO
);
2900 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2901 ed
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2902 if (!parse_mmacro_spec(tline
, ed
, pp_directives
[i
])) {
2905 return DIRECTIVE_FOUND
;
2909 ed
->max_depth
= (ed
->max_depth
+ 1);
2910 ed
->ignoring
= false;
2911 ed
->prev
= defining
;
2914 eed
= (ExpDef
*) hash_findix(&expdefs
, ed
->name
);
2916 if (!strcmp(eed
->name
, ed
->name
) &&
2917 (eed
->nparam_min
<= ed
->nparam_max
2919 && (ed
->nparam_min
<= eed
->nparam_max
2921 error(ERR_WARNING
|ERR_PASS1
,
2922 "redefining multi-line macro `%s'", ed
->name
);
2923 return DIRECTIVE_FOUND
;
2927 free_tlist(origline
);
2928 return DIRECTIVE_FOUND
;
2932 if (defining
!= NULL
) {
2933 if (defining
->type
== EXP_MMACRO
) {
2934 if (defining
->def_depth
> 0) {
2935 defining
->def_depth
--;
2936 return NO_DIRECTIVE_FOUND
;
2939 return NO_DIRECTIVE_FOUND
;
2942 if (!(defining
) || (defining
->type
!= EXP_MMACRO
)) {
2943 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2944 return DIRECTIVE_FOUND
;
2946 edhead
= (ExpDef
**) hash_findi_add(&expdefs
, defining
->name
);
2947 defining
->next
= *edhead
;
2950 defining
= ed
->prev
;
2951 ed
->prev
= expansions
;
2954 free_tlist(origline
);
2955 return DIRECTIVE_FOUND
;
2958 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2960 * We must search along istk->expansion until we hit a
2961 * macro invocation. Then we disable the emitting state(s)
2962 * between exitmacro and endmacro.
2964 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
2965 if(ei
->type
== EXP_MMACRO
) {
2972 * Set all invocations leading back to the macro
2973 * invocation to a non-emitting state.
2975 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
2976 eei
->emitting
= false;
2978 eei
->emitting
= false;
2980 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
2982 free_tlist(origline
);
2983 return DIRECTIVE_FOUND
;
2987 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2992 spec
.casesense
= (i
== PP_UNMACRO
);
2993 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2994 return DIRECTIVE_FOUND
;
2996 ed_p
= (ExpDef
**) hash_findi(&expdefs
, spec
.name
, NULL
);
2997 while (ed_p
&& *ed_p
) {
2999 if (ed
->casesense
== spec
.casesense
&&
3000 !mstrcmp(ed
->name
, spec
.name
, spec
.casesense
) &&
3001 ed
->nparam_min
== spec
.nparam_min
&&
3002 ed
->nparam_max
== spec
.nparam_max
&&
3003 ed
->plus
== spec
.plus
) {
3010 free_tlist(origline
);
3011 free_tlist(spec
.dlist
);
3012 return DIRECTIVE_FOUND
;
3016 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3017 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
3018 tline
= tline
->next
;
3020 free_tlist(origline
);
3021 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
3022 return DIRECTIVE_FOUND
;
3024 t
= expand_smacro(tline
->next
);
3026 free_tlist(origline
);
3029 tokval
.t_type
= TOKEN_INVALID
;
3031 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3034 return DIRECTIVE_FOUND
;
3036 error(ERR_WARNING
|ERR_PASS1
,
3037 "trailing garbage after expression ignored");
3038 if (!is_simple(evalresult
)) {
3039 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
3040 return DIRECTIVE_FOUND
;
3042 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3043 if (ei
->type
== EXP_MMACRO
) {
3048 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
3049 } else if (ei
->nparam
== 0) {
3051 "`%%rotate' invoked within macro without parameters");
3053 int rotate
= ei
->rotate
+ reloc_value(evalresult
);
3055 rotate
%= (int)ei
->nparam
;
3057 rotate
+= ei
->nparam
;
3058 ei
->rotate
= rotate
;
3060 return DIRECTIVE_FOUND
;
3063 if (defining
!= NULL
) {
3064 if (defining
->type
== EXP_REP
) {
3065 defining
->def_depth
++;
3067 return NO_DIRECTIVE_FOUND
;
3071 tline
= tline
->next
;
3072 } while (tok_type_(tline
, TOK_WHITESPACE
));
3074 if (tok_type_(tline
, TOK_ID
) &&
3075 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3078 tline
= tline
->next
;
3079 } while (tok_type_(tline
, TOK_WHITESPACE
));
3083 t
= expand_smacro(tline
);
3085 tokval
.t_type
= TOKEN_INVALID
;
3087 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3089 free_tlist(origline
);
3090 return DIRECTIVE_FOUND
;
3093 error(ERR_WARNING
|ERR_PASS1
,
3094 "trailing garbage after expression ignored");
3095 if (!is_simple(evalresult
)) {
3096 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3097 return DIRECTIVE_FOUND
;
3099 count
= reloc_value(evalresult
);
3100 if (count
>= REP_LIMIT
) {
3101 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
3106 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3109 free_tlist(origline
);
3110 ed
= new_ExpDef(EXP_REP
);
3111 ed
->nolist
= nolist
;
3114 ed
->max_depth
= (count
- 1);
3115 ed
->ignoring
= false;
3116 ed
->prev
= defining
;
3118 return DIRECTIVE_FOUND
;
3121 if (defining
!= NULL
) {
3122 if (defining
->type
== EXP_REP
) {
3123 if (defining
->def_depth
> 0) {
3124 defining
->def_depth
--;
3125 return NO_DIRECTIVE_FOUND
;
3128 return NO_DIRECTIVE_FOUND
;
3131 if ((defining
== NULL
) || (defining
->type
!= EXP_REP
)) {
3132 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3133 return DIRECTIVE_FOUND
;
3137 * Now we have a "macro" defined - although it has no name
3138 * and we won't be entering it in the hash tables - we must
3139 * push a macro-end marker for it on to istk->expansion.
3140 * After that, it will take care of propagating itself (a
3141 * macro-end marker line for a macro which is really a %rep
3142 * block will cause the macro to be re-expanded, complete
3143 * with another macro-end marker to ensure the process
3144 * continues) until the whole expansion is forcibly removed
3145 * from istk->expansion by a %exitrep.
3148 defining
= ed
->prev
;
3149 ed
->prev
= expansions
;
3151 ei
= new_ExpInv(EXP_REP
, ed
);
3152 ei
->current
= ed
->line
;
3153 ei
->emitting
= ((ed
->max_depth
> 0) ? true : false);
3154 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3155 ei
->prev
= istk
->expansion
;
3156 istk
->expansion
= ei
;
3157 free_tlist(origline
);
3158 return DIRECTIVE_FOUND
;
3161 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3163 * We must search along istk->expansion until we hit a
3164 * rep invocation. Then we disable the emitting state(s)
3165 * between exitrep and endrep.
3167 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3168 if (ei
->type
== EXP_REP
) {
3175 * Set all invocations leading back to the rep
3176 * invocation to a non-emitting state.
3178 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3179 eei
->emitting
= false;
3181 eei
->emitting
= false;
3182 eei
->current
= NULL
;
3183 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3185 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3187 free_tlist(origline
);
3188 return DIRECTIVE_FOUND
;
3194 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3195 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3197 tline
= tline
->next
;
3199 tline
= expand_id(tline
);
3200 if (!tline
|| (tline
->type
!= TOK_ID
&&
3201 (tline
->type
!= TOK_PREPROC_ID
||
3202 tline
->text
[1] != '$'))) {
3203 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3205 free_tlist(origline
);
3206 return DIRECTIVE_FOUND
;
3209 ctx
= get_ctx(tline
->text
, &mname
, false);
3211 param_start
= tline
= tline
->next
;
3214 /* Expand the macro definition now for %xdefine and %ixdefine */
3215 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3216 tline
= expand_smacro(tline
);
3218 if (tok_is_(tline
, "(")) {
3220 * This macro has parameters.
3223 tline
= tline
->next
;
3227 error(ERR_NONFATAL
, "parameter identifier expected");
3228 free_tlist(origline
);
3229 return DIRECTIVE_FOUND
;
3231 if (tline
->type
!= TOK_ID
) {
3233 "`%s': parameter identifier expected",
3235 free_tlist(origline
);
3236 return DIRECTIVE_FOUND
;
3238 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
3239 tline
= tline
->next
;
3241 if (tok_is_(tline
, ",")) {
3242 tline
= tline
->next
;
3244 if (!tok_is_(tline
, ")")) {
3246 "`)' expected to terminate macro template");
3247 free_tlist(origline
);
3248 return DIRECTIVE_FOUND
;
3254 tline
= tline
->next
;
3256 if (tok_type_(tline
, TOK_WHITESPACE
))
3257 last
= tline
, tline
= tline
->next
;
3262 if (t
->type
== TOK_ID
) {
3263 list_for_each(tt
, param_start
)
3264 if (tt
->type
>= TOK_SMAC_PARAM
&&
3265 !strcmp(tt
->text
, t
->text
))
3269 t
->next
= macro_start
;
3274 * Good. We now have a macro name, a parameter count, and a
3275 * token list (in reverse order) for an expansion. We ought
3276 * to be OK just to create an SMacro, store it, and let
3277 * free_tlist have the rest of the line (which we have
3278 * carefully re-terminated after chopping off the expansion
3281 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3282 free_tlist(origline
);
3283 return DIRECTIVE_FOUND
;
3286 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3287 tline
= tline
->next
;
3289 tline
= expand_id(tline
);
3290 if (!tline
|| (tline
->type
!= TOK_ID
&&
3291 (tline
->type
!= TOK_PREPROC_ID
||
3292 tline
->text
[1] != '$'))) {
3293 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3294 free_tlist(origline
);
3295 return DIRECTIVE_FOUND
;
3298 error(ERR_WARNING
|ERR_PASS1
,
3299 "trailing garbage after macro name ignored");
3302 /* Find the context that symbol belongs to */
3303 ctx
= get_ctx(tline
->text
, &mname
, false);
3304 undef_smacro(ctx
, mname
);
3305 free_tlist(origline
);
3306 return DIRECTIVE_FOUND
;
3310 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3311 casesense
= (i
== PP_DEFSTR
);
3313 tline
= tline
->next
;
3315 tline
= expand_id(tline
);
3316 if (!tline
|| (tline
->type
!= TOK_ID
&&
3317 (tline
->type
!= TOK_PREPROC_ID
||
3318 tline
->text
[1] != '$'))) {
3319 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3321 free_tlist(origline
);
3322 return DIRECTIVE_FOUND
;
3325 ctx
= get_ctx(tline
->text
, &mname
, false);
3327 tline
= expand_smacro(tline
->next
);
3330 while (tok_type_(tline
, TOK_WHITESPACE
))
3331 tline
= delete_Token(tline
);
3333 p
= detoken(tline
, false);
3334 macro_start
= nasm_malloc(sizeof(*macro_start
));
3335 macro_start
->next
= NULL
;
3336 macro_start
->text
= nasm_quote(p
, strlen(p
));
3337 macro_start
->type
= TOK_STRING
;
3338 macro_start
->a
.mac
= NULL
;
3342 * We now have a macro name, an implicit parameter count of
3343 * zero, and a string token to use as an expansion. Create
3344 * and store an SMacro.
3346 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3347 free_tlist(origline
);
3348 return DIRECTIVE_FOUND
;
3352 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3353 casesense
= (i
== PP_DEFTOK
);
3355 tline
= tline
->next
;
3357 tline
= expand_id(tline
);
3358 if (!tline
|| (tline
->type
!= TOK_ID
&&
3359 (tline
->type
!= TOK_PREPROC_ID
||
3360 tline
->text
[1] != '$'))) {
3362 "`%s' expects a macro identifier as first parameter",
3364 free_tlist(origline
);
3365 return DIRECTIVE_FOUND
;
3367 ctx
= get_ctx(tline
->text
, &mname
, false);
3369 tline
= expand_smacro(tline
->next
);
3373 while (tok_type_(t
, TOK_WHITESPACE
))
3375 /* t should now point to the string */
3376 if (!tok_type_(t
, TOK_STRING
)) {
3378 "`%s` requires string as second parameter",
3381 free_tlist(origline
);
3382 return DIRECTIVE_FOUND
;
3386 * Convert the string to a token stream. Note that smacros
3387 * are stored with the token stream reversed, so we have to
3388 * reverse the output of tokenize().
3390 nasm_unquote_cstr(t
->text
, i
);
3391 macro_start
= reverse_tokens(tokenize(t
->text
));
3394 * We now have a macro name, an implicit parameter count of
3395 * zero, and a numeric token to use as an expansion. Create
3396 * and store an SMacro.
3398 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3400 free_tlist(origline
);
3401 return DIRECTIVE_FOUND
;
3404 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3407 StrList
*xsl
= NULL
;
3408 StrList
**xst
= &xsl
;
3412 tline
= tline
->next
;
3414 tline
= expand_id(tline
);
3415 if (!tline
|| (tline
->type
!= TOK_ID
&&
3416 (tline
->type
!= TOK_PREPROC_ID
||
3417 tline
->text
[1] != '$'))) {
3419 "`%%pathsearch' expects a macro identifier as first parameter");
3420 free_tlist(origline
);
3421 return DIRECTIVE_FOUND
;
3423 ctx
= get_ctx(tline
->text
, &mname
, false);
3425 tline
= expand_smacro(tline
->next
);
3429 while (tok_type_(t
, TOK_WHITESPACE
))
3432 if (!t
|| (t
->type
!= TOK_STRING
&&
3433 t
->type
!= TOK_INTERNAL_STRING
)) {
3434 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3436 free_tlist(origline
);
3437 return DIRECTIVE_FOUND
; /* but we did _something_ */
3440 error(ERR_WARNING
|ERR_PASS1
,
3441 "trailing garbage after `%%pathsearch' ignored");
3443 if (t
->type
!= TOK_INTERNAL_STRING
)
3444 nasm_unquote(p
, NULL
);
3446 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3449 fclose(fp
); /* Don't actually care about the file */
3451 macro_start
= nasm_malloc(sizeof(*macro_start
));
3452 macro_start
->next
= NULL
;
3453 macro_start
->text
= nasm_quote(p
, strlen(p
));
3454 macro_start
->type
= TOK_STRING
;
3455 macro_start
->a
.mac
= NULL
;
3460 * We now have a macro name, an implicit parameter count of
3461 * zero, and a string token to use as an expansion. Create
3462 * and store an SMacro.
3464 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3466 free_tlist(origline
);
3467 return DIRECTIVE_FOUND
;
3471 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3474 tline
= tline
->next
;
3476 tline
= expand_id(tline
);
3477 if (!tline
|| (tline
->type
!= TOK_ID
&&
3478 (tline
->type
!= TOK_PREPROC_ID
||
3479 tline
->text
[1] != '$'))) {
3481 "`%%strlen' expects a macro identifier as first parameter");
3482 free_tlist(origline
);
3483 return DIRECTIVE_FOUND
;
3485 ctx
= get_ctx(tline
->text
, &mname
, false);
3487 tline
= expand_smacro(tline
->next
);
3491 while (tok_type_(t
, TOK_WHITESPACE
))
3493 /* t should now point to the string */
3494 if (!tok_type_(t
, TOK_STRING
)) {
3496 "`%%strlen` requires string as second parameter");
3498 free_tlist(origline
);
3499 return DIRECTIVE_FOUND
;
3502 macro_start
= nasm_malloc(sizeof(*macro_start
));
3503 macro_start
->next
= NULL
;
3504 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3505 macro_start
->a
.mac
= NULL
;
3508 * We now have a macro name, an implicit parameter count of
3509 * zero, and a numeric token to use as an expansion. Create
3510 * and store an SMacro.
3512 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3514 free_tlist(origline
);
3515 return DIRECTIVE_FOUND
;
3518 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3521 tline
= tline
->next
;
3523 tline
= expand_id(tline
);
3524 if (!tline
|| (tline
->type
!= TOK_ID
&&
3525 (tline
->type
!= TOK_PREPROC_ID
||
3526 tline
->text
[1] != '$'))) {
3528 "`%%strcat' expects a macro identifier as first parameter");
3529 free_tlist(origline
);
3530 return DIRECTIVE_FOUND
;
3532 ctx
= get_ctx(tline
->text
, &mname
, false);
3534 tline
= expand_smacro(tline
->next
);
3538 list_for_each(t
, tline
) {
3540 case TOK_WHITESPACE
:
3543 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3546 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3548 /* else fall through */
3551 "non-string passed to `%%strcat' (%d)", t
->type
);
3553 free_tlist(origline
);
3554 return DIRECTIVE_FOUND
;
3558 p
= pp
= nasm_malloc(len
);
3559 list_for_each(t
, tline
) {
3560 if (t
->type
== TOK_STRING
) {
3561 memcpy(p
, t
->text
, t
->a
.len
);
3567 * We now have a macro name, an implicit parameter count of
3568 * zero, and a numeric token to use as an expansion. Create
3569 * and store an SMacro.
3571 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3572 macro_start
->text
= nasm_quote(pp
, len
);
3574 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3576 free_tlist(origline
);
3577 return DIRECTIVE_FOUND
;
3580 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3582 int64_t start
, count
;
3587 tline
= tline
->next
;
3589 tline
= expand_id(tline
);
3590 if (!tline
|| (tline
->type
!= TOK_ID
&&
3591 (tline
->type
!= TOK_PREPROC_ID
||
3592 tline
->text
[1] != '$'))) {
3594 "`%%substr' expects a macro identifier as first parameter");
3595 free_tlist(origline
);
3596 return DIRECTIVE_FOUND
;
3598 ctx
= get_ctx(tline
->text
, &mname
, false);
3600 tline
= expand_smacro(tline
->next
);
3603 if (tline
) /* skip expanded id */
3605 while (tok_type_(t
, TOK_WHITESPACE
))
3608 /* t should now point to the string */
3609 if (!tok_type_(t
, TOK_STRING
)) {
3611 "`%%substr` requires string as second parameter");
3613 free_tlist(origline
);
3614 return DIRECTIVE_FOUND
;
3619 tokval
.t_type
= TOKEN_INVALID
;
3620 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3624 free_tlist(origline
);
3625 return DIRECTIVE_FOUND
;
3626 } else if (!is_simple(evalresult
)) {
3627 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3629 free_tlist(origline
);
3630 return DIRECTIVE_FOUND
;
3632 start
= evalresult
->value
- 1;
3634 while (tok_type_(tt
, TOK_WHITESPACE
))
3637 count
= 1; /* Backwards compatibility: one character */
3639 tokval
.t_type
= TOKEN_INVALID
;
3640 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3644 free_tlist(origline
);
3645 return DIRECTIVE_FOUND
;
3646 } else if (!is_simple(evalresult
)) {
3647 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3649 free_tlist(origline
);
3650 return DIRECTIVE_FOUND
;
3652 count
= evalresult
->value
;
3655 len
= nasm_unquote(t
->text
, NULL
);
3656 /* make start and count being in range */
3660 count
= len
+ count
+ 1 - start
;
3661 if (start
+ count
> (int64_t)len
)
3662 count
= len
- start
;
3663 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3664 start
= -1, count
= 0; /* empty string */
3666 macro_start
= nasm_malloc(sizeof(*macro_start
));
3667 macro_start
->next
= NULL
;
3668 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3669 macro_start
->type
= TOK_STRING
;
3670 macro_start
->a
.mac
= NULL
;
3673 * We now have a macro name, an implicit parameter count of
3674 * zero, and a numeric token to use as an expansion. Create
3675 * and store an SMacro.
3677 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3679 free_tlist(origline
);
3680 return DIRECTIVE_FOUND
;
3685 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3686 casesense
= (i
== PP_ASSIGN
);
3688 tline
= tline
->next
;
3690 tline
= expand_id(tline
);
3691 if (!tline
|| (tline
->type
!= TOK_ID
&&
3692 (tline
->type
!= TOK_PREPROC_ID
||
3693 tline
->text
[1] != '$'))) {
3695 "`%%%sassign' expects a macro identifier",
3696 (i
== PP_IASSIGN
? "i" : ""));
3697 free_tlist(origline
);
3698 return DIRECTIVE_FOUND
;
3700 ctx
= get_ctx(tline
->text
, &mname
, false);
3702 tline
= expand_smacro(tline
->next
);
3707 tokval
.t_type
= TOKEN_INVALID
;
3709 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3712 free_tlist(origline
);
3713 return DIRECTIVE_FOUND
;
3717 error(ERR_WARNING
|ERR_PASS1
,
3718 "trailing garbage after expression ignored");
3720 if (!is_simple(evalresult
)) {
3722 "non-constant value given to `%%%sassign'",
3723 (i
== PP_IASSIGN
? "i" : ""));
3724 free_tlist(origline
);
3725 return DIRECTIVE_FOUND
;
3728 macro_start
= nasm_malloc(sizeof(*macro_start
));
3729 macro_start
->next
= NULL
;
3730 make_tok_num(macro_start
, reloc_value(evalresult
));
3731 macro_start
->a
.mac
= NULL
;
3734 * We now have a macro name, an implicit parameter count of
3735 * zero, and a numeric token to use as an expansion. Create
3736 * and store an SMacro.
3738 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3739 free_tlist(origline
);
3740 return DIRECTIVE_FOUND
;
3743 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3745 * Syntax is `%line nnn[+mmm] [filename]'
3747 tline
= tline
->next
;
3749 if (!tok_type_(tline
, TOK_NUMBER
)) {
3750 error(ERR_NONFATAL
, "`%%line' expects line number");
3751 free_tlist(origline
);
3752 return DIRECTIVE_FOUND
;
3754 k
= readnum(tline
->text
, &err
);
3756 tline
= tline
->next
;
3757 if (tok_is_(tline
, "+")) {
3758 tline
= tline
->next
;
3759 if (!tok_type_(tline
, TOK_NUMBER
)) {
3760 error(ERR_NONFATAL
, "`%%line' expects line increment");
3761 free_tlist(origline
);
3762 return DIRECTIVE_FOUND
;
3764 m
= readnum(tline
->text
, &err
);
3765 tline
= tline
->next
;
3771 nasm_free(src_set_fname(detoken(tline
, false)));
3773 free_tlist(origline
);
3774 return DIRECTIVE_FOUND
;
3777 if (defining
!= NULL
) {
3778 if (defining
->type
== EXP_WHILE
) {
3779 defining
->def_depth
++;
3781 return NO_DIRECTIVE_FOUND
;
3784 if ((istk
->expansion
!= NULL
) &&
3785 (istk
->expansion
->emitting
== false)) {
3789 l
->first
= copy_Token(tline
->next
);
3790 j
= if_condition(tline
->next
, i
);
3791 tline
->next
= NULL
; /* it got freed */
3792 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
3794 ed
= new_ExpDef(EXP_WHILE
);
3797 ed
->max_depth
= DEADMAN_LIMIT
;
3798 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
3799 if (ed
->ignoring
== false) {
3802 } else if (l
!= NULL
) {
3803 delete_Token(l
->first
);
3807 ed
->prev
= defining
;
3809 free_tlist(origline
);
3810 return DIRECTIVE_FOUND
;
3813 if (defining
!= NULL
) {
3814 if (defining
->type
== EXP_WHILE
) {
3815 if (defining
->def_depth
> 0) {
3816 defining
->def_depth
--;
3817 return NO_DIRECTIVE_FOUND
;
3820 return NO_DIRECTIVE_FOUND
;
3823 if (tline
->next
!= NULL
) {
3824 error_precond(ERR_WARNING
|ERR_PASS1
,
3825 "trailing garbage after `%%endwhile' ignored");
3827 if ((defining
== NULL
) || (defining
->type
!= EXP_WHILE
)) {
3828 error(ERR_NONFATAL
, "`%%endwhile': no matching `%%while'");
3829 return DIRECTIVE_FOUND
;
3832 defining
= ed
->prev
;
3833 if (ed
->ignoring
== false) {
3834 ed
->prev
= expansions
;
3836 ei
= new_ExpInv(EXP_WHILE
, ed
);
3837 ei
->current
= ed
->line
->next
;
3838 ei
->emitting
= true;
3839 ei
->prev
= istk
->expansion
;
3840 istk
->expansion
= ei
;
3844 free_tlist(origline
);
3845 return DIRECTIVE_FOUND
;
3848 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3850 * We must search along istk->expansion until we hit a
3851 * while invocation. Then we disable the emitting state(s)
3852 * between exitwhile and endwhile.
3854 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3855 if (ei
->type
== EXP_WHILE
) {
3862 * Set all invocations leading back to the while
3863 * invocation to a non-emitting state.
3865 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3866 eei
->emitting
= false;
3868 eei
->emitting
= false;
3869 eei
->current
= NULL
;
3870 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3872 error(ERR_NONFATAL
, "`%%exitwhile' not within `%%while' block");
3874 free_tlist(origline
);
3875 return DIRECTIVE_FOUND
;
3878 if (defining
!= NULL
) {
3879 if (defining
->type
== EXP_COMMENT
) {
3880 defining
->def_depth
++;
3882 return NO_DIRECTIVE_FOUND
;
3884 ed
= new_ExpDef(EXP_COMMENT
);
3885 ed
->ignoring
= true;
3886 ed
->prev
= defining
;
3888 free_tlist(origline
);
3889 return DIRECTIVE_FOUND
;
3892 if (defining
!= NULL
) {
3893 if (defining
->type
== EXP_COMMENT
) {
3894 if (defining
->def_depth
> 0) {
3895 defining
->def_depth
--;
3896 return NO_DIRECTIVE_FOUND
;
3899 return NO_DIRECTIVE_FOUND
;
3902 if ((defining
== NULL
) || (defining
->type
!= EXP_COMMENT
)) {
3903 error(ERR_NONFATAL
, "`%%endcomment': no matching `%%comment'");
3904 return DIRECTIVE_FOUND
;
3907 defining
= ed
->prev
;
3909 free_tlist(origline
);
3910 return DIRECTIVE_FOUND
;
3913 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3914 if (in_final
!= false) {
3915 error(ERR_FATAL
, "`%%final' cannot be used recursively");
3917 tline
= tline
->next
;
3919 if (tline
== NULL
) {
3920 error(ERR_NONFATAL
, "`%%final' expects at least one parameter");
3923 l
->first
= copy_Token(tline
);
3927 free_tlist(origline
);
3928 return DIRECTIVE_FOUND
;
3932 "preprocessor directive `%s' not yet implemented",
3934 return DIRECTIVE_FOUND
;
3939 * Ensure that a macro parameter contains a condition code and
3940 * nothing else. Return the condition code index if so, or -1
3943 static int find_cc(Token
* t
)
3949 return -1; /* Probably a %+ without a space */
3952 if (t
->type
!= TOK_ID
)
3956 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3960 j
= ARRAY_SIZE(conditions
);
3963 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3978 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
3979 int mnum
, bool handle_paste_tokens
)
3981 Token
**tail
, *t
, *tt
;
3983 bool did_paste
= false;
3987 /* Now handle token pasting... */
3990 while ((t
= *tail
) && (tt
= t
->next
)) {
3992 case TOK_WHITESPACE
:
3993 if (tt
->type
== TOK_WHITESPACE
) {
3994 /* Zap adjacent whitespace tokens */
3995 t
->next
= delete_Token(tt
);
3997 /* Do not advance paste_head here */
4001 case TOK_PASTE
: /* %+ */
4002 if (handle_paste_tokens
) {
4003 /* Zap %+ and whitespace tokens to the right */
4004 while (t
&& (t
->type
== TOK_WHITESPACE
||
4005 t
->type
== TOK_PASTE
))
4006 t
= *tail
= delete_Token(t
);
4007 if (!paste_head
|| !t
)
4008 break; /* Nothing to paste with */
4012 while (tok_type_(tt
, TOK_WHITESPACE
))
4013 tt
= t
->next
= delete_Token(tt
);
4015 tmp
= nasm_strcat(t
->text
, tt
->text
);
4017 tt
= delete_Token(tt
);
4018 t
= *tail
= tokenize(tmp
);
4024 t
->next
= tt
; /* Attach the remaining token chain */
4031 /* else fall through */
4034 * Concatenation of tokens might look nontrivial
4035 * but in real it's pretty simple -- the caller
4036 * prepares the masks of token types to be concatenated
4037 * and we simply find matched sequences and slip
4040 for (i
= 0; i
< mnum
; i
++) {
4041 if (PP_CONCAT_MASK(t
->type
) & m
[i
].mask_head
) {
4045 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & m
[i
].mask_tail
)) {
4046 len
+= strlen(tt
->text
);
4051 * Now tt points to the first token after
4052 * the potential paste area...
4054 if (tt
!= t
->next
) {
4055 /* We have at least two tokens... */
4056 len
+= strlen(t
->text
);
4057 p
= tmp
= nasm_malloc(len
+1);
4060 p
= strchr(p
, '\0');
4061 t
= delete_Token(t
);
4063 t
= *tail
= tokenize(tmp
);
4069 t
->next
= tt
; /* Attach the remaining token chain */
4077 if (i
>= mnum
) { /* no match */
4079 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
4089 * expands to a list of tokens from %{x:y}
4091 static Token
*expand_mmac_params_range(ExpInv
*ei
, Token
*tline
, Token
***last
)
4093 Token
*t
= tline
, **tt
, *tm
, *head
;
4097 pos
= strchr(tline
->text
, ':');
4100 lst
= atoi(pos
+ 1);
4101 fst
= atoi(tline
->text
+ 1);
4104 * only macros params are accounted so
4105 * if someone passes %0 -- we reject such
4108 if (lst
== 0 || fst
== 0)
4111 /* the values should be sane */
4112 if ((fst
> (int)ei
->nparam
|| fst
< (-(int)ei
->nparam
)) ||
4113 (lst
> (int)ei
->nparam
|| lst
< (-(int)ei
->nparam
)))
4116 fst
= fst
< 0 ? fst
+ (int)ei
->nparam
+ 1: fst
;
4117 lst
= lst
< 0 ? lst
+ (int)ei
->nparam
+ 1: lst
;
4119 /* counted from zero */
4123 * it will be at least one token
4125 tm
= ei
->params
[(fst
+ ei
->rotate
) % ei
->nparam
];
4126 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4127 head
= t
, tt
= &t
->next
;
4129 for (i
= fst
+ 1; i
<= lst
; i
++) {
4130 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4131 *tt
= t
, tt
= &t
->next
;
4132 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4134 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4135 *tt
= t
, tt
= &t
->next
;
4138 for (i
= fst
- 1; i
>= lst
; i
--) {
4139 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4140 *tt
= t
, tt
= &t
->next
;
4141 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4143 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4144 *tt
= t
, tt
= &t
->next
;
4152 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
4158 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4159 * %-n) and MMacro-local identifiers (%%foo) as well as
4160 * macro indirection (%[...]) and range (%{..:..}).
4162 static Token
*expand_mmac_params(Token
* tline
)
4164 Token
*t
, *tt
, **tail
, *thead
;
4165 bool changed
= false;
4172 if (tline
->type
== TOK_PREPROC_ID
&&
4173 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4174 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4175 tline
->text
[1] == '%')) {
4177 int type
= 0, cc
; /* type = 0 to placate optimisers */
4184 tline
= tline
->next
;
4186 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
4187 if (ei
->type
== EXP_MMACRO
) {
4192 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4194 pos
= strchr(t
->text
, ':');
4196 switch (t
->text
[1]) {
4198 * We have to make a substitution of one of the
4199 * forms %1, %-1, %+1, %%foo, %0.
4202 if ((strlen(t
->text
) > 2) && (t
->text
[2] == '0')) {
4204 text
= nasm_strdup(ei
->label_text
);
4207 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", ei
->nparam
);
4208 text
= nasm_strdup(tmpbuf
);
4213 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4215 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4218 n
= atoi(t
->text
+ 2) - 1;
4219 if (n
>= ei
->nparam
)
4223 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4229 "macro parameter %d is not a condition code",
4234 if (inverse_ccs
[cc
] == -1) {
4236 "condition code `%s' is not invertible",
4240 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4244 n
= atoi(t
->text
+ 2) - 1;
4245 if (n
>= ei
->nparam
)
4249 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4255 "macro parameter %d is not a condition code",
4260 text
= nasm_strdup(conditions
[cc
]);
4264 n
= atoi(t
->text
+ 1) - 1;
4265 if (n
>= ei
->nparam
)
4269 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4273 for (i
= 0; i
< ei
->paramlen
[n
]; i
++) {
4274 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4275 tail
= &(*tail
)->next
;
4279 text
= NULL
; /* we've done it here */
4284 * seems we have a parameters range here
4286 Token
*head
, **last
;
4287 head
= expand_mmac_params_range(ei
, t
, &last
);
4308 } else if (tline
->type
== TOK_INDIRECT
) {
4310 tline
= tline
->next
;
4311 tt
= tokenize(t
->text
);
4312 tt
= expand_mmac_params(tt
);
4313 tt
= expand_smacro(tt
);
4316 tt
->a
.mac
= NULL
; /* Necessary? */
4324 tline
= tline
->next
;
4332 const struct tokseq_match t
[] = {
4334 PP_CONCAT_MASK(TOK_ID
) |
4335 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4336 PP_CONCAT_MASK(TOK_ID
) |
4337 PP_CONCAT_MASK(TOK_NUMBER
) |
4338 PP_CONCAT_MASK(TOK_FLOAT
) |
4339 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4342 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4343 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4346 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4353 * Expand all single-line macro calls made in the given line.
4354 * Return the expanded version of the line. The original is deemed
4355 * to be destroyed in the process. (In reality we'll just move
4356 * Tokens from input to output a lot of the time, rather than
4357 * actually bothering to destroy and replicate.)
4360 static Token
*expand_smacro(Token
* tline
)
4362 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4363 SMacro
*head
= NULL
, *m
;
4366 unsigned int nparam
, sparam
;
4368 Token
*org_tline
= tline
;
4371 int deadman
= DEADMAN_LIMIT
;
4375 * Trick: we should avoid changing the start token pointer since it can
4376 * be contained in "next" field of other token. Because of this
4377 * we allocate a copy of first token and work with it; at the end of
4378 * routine we copy it back
4381 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4382 org_tline
->text
, 0);
4383 tline
->a
.mac
= org_tline
->a
.mac
;
4384 nasm_free(org_tline
->text
);
4385 org_tline
->text
= NULL
;
4388 expanded
= true; /* Always expand %+ at least once */
4394 while (tline
) { /* main token loop */
4396 error(ERR_NONFATAL
, "interminable macro recursion");
4400 if ((mname
= tline
->text
)) {
4401 /* if this token is a local macro, look in local context */
4402 if (tline
->type
== TOK_ID
) {
4403 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4404 } else if (tline
->type
== TOK_PREPROC_ID
) {
4405 ctx
= get_ctx(mname
, &mname
, false);
4406 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4411 * We've hit an identifier. As in is_mmacro below, we first
4412 * check whether the identifier is a single-line macro at
4413 * all, then think about checking for parameters if
4416 list_for_each(m
, head
)
4417 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4423 if (m
->nparam
== 0) {
4425 * Simple case: the macro is parameterless. Discard the
4426 * one token that the macro call took, and push the
4427 * expansion back on the to-do stack.
4429 if (!m
->expansion
) {
4430 if (!strcmp("__FILE__", m
->name
)) {
4433 src_get(&num
, &file
);
4434 tline
->text
= nasm_quote(file
, strlen(file
));
4435 tline
->type
= TOK_STRING
;
4439 if (!strcmp("__LINE__", m
->name
)) {
4440 nasm_free(tline
->text
);
4441 make_tok_num(tline
, src_get_linnum());
4444 if (!strcmp("__BITS__", m
->name
)) {
4445 nasm_free(tline
->text
);
4446 make_tok_num(tline
, globalbits
);
4449 tline
= delete_Token(tline
);
4454 * Complicated case: at least one macro with this name
4455 * exists and takes parameters. We must find the
4456 * parameters in the call, count them, find the SMacro
4457 * that corresponds to that form of the macro call, and
4458 * substitute for the parameters when we expand. What a
4461 /*tline = tline->next;
4462 skip_white_(tline); */
4465 while (tok_type_(t
, TOK_SMAC_END
)) {
4466 t
->a
.mac
->in_progress
= false;
4468 t
= tline
->next
= delete_Token(t
);
4471 } while (tok_type_(tline
, TOK_WHITESPACE
));
4472 if (!tok_is_(tline
, "(")) {
4474 * This macro wasn't called with parameters: ignore
4475 * the call. (Behaviour borrowed from gnu cpp.)
4484 sparam
= PARAM_DELTA
;
4485 params
= nasm_malloc(sparam
* sizeof(Token
*));
4486 params
[0] = tline
->next
;
4487 paramsize
= nasm_malloc(sparam
* sizeof(int));
4489 while (true) { /* parameter loop */
4491 * For some unusual expansions
4492 * which concatenates function call
4495 while (tok_type_(t
, TOK_SMAC_END
)) {
4496 t
->a
.mac
->in_progress
= false;
4498 t
= tline
->next
= delete_Token(t
);
4504 "macro call expects terminating `)'");
4507 if (tline
->type
== TOK_WHITESPACE
4509 if (paramsize
[nparam
])
4512 params
[nparam
] = tline
->next
;
4513 continue; /* parameter loop */
4515 if (tline
->type
== TOK_OTHER
4516 && tline
->text
[1] == 0) {
4517 char ch
= tline
->text
[0];
4518 if (ch
== ',' && !paren
&& brackets
<= 0) {
4519 if (++nparam
>= sparam
) {
4520 sparam
+= PARAM_DELTA
;
4521 params
= nasm_realloc(params
,
4522 sparam
* sizeof(Token
*));
4523 paramsize
= nasm_realloc(paramsize
,
4524 sparam
* sizeof(int));
4526 params
[nparam
] = tline
->next
;
4527 paramsize
[nparam
] = 0;
4529 continue; /* parameter loop */
4532 (brackets
> 0 || (brackets
== 0 &&
4533 !paramsize
[nparam
])))
4535 if (!(brackets
++)) {
4536 params
[nparam
] = tline
->next
;
4537 continue; /* parameter loop */
4540 if (ch
== '}' && brackets
> 0)
4541 if (--brackets
== 0) {
4543 continue; /* parameter loop */
4545 if (ch
== '(' && !brackets
)
4547 if (ch
== ')' && brackets
<= 0)
4553 error(ERR_NONFATAL
, "braces do not "
4554 "enclose all of macro parameter");
4556 paramsize
[nparam
] += white
+ 1;
4558 } /* parameter loop */
4560 while (m
&& (m
->nparam
!= nparam
||
4561 mstrcmp(m
->name
, mname
,
4565 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4566 "macro `%s' exists, "
4567 "but not taking %d parameters",
4568 mstart
->text
, nparam
);
4571 if (m
&& m
->in_progress
)
4573 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4575 * Design question: should we handle !tline, which
4576 * indicates missing ')' here, or expand those
4577 * macros anyway, which requires the (t) test a few
4581 nasm_free(paramsize
);
4585 * Expand the macro: we are placed on the last token of the
4586 * call, so that we can easily split the call from the
4587 * following tokens. We also start by pushing an SMAC_END
4588 * token for the cycle removal.
4595 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4597 m
->in_progress
= true;
4599 list_for_each(t
, m
->expansion
) {
4600 if (t
->type
>= TOK_SMAC_PARAM
) {
4601 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4605 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
4606 i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
4608 pt
= *ptail
= new_Token(tline
, ttt
->type
,
4614 } else if (t
->type
== TOK_PREPROC_Q
) {
4615 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4617 } else if (t
->type
== TOK_PREPROC_QQ
) {
4618 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4621 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4627 * Having done that, get rid of the macro call, and clean
4628 * up the parameters.
4631 nasm_free(paramsize
);
4634 continue; /* main token loop */
4639 if (tline
->type
== TOK_SMAC_END
) {
4640 tline
->a
.mac
->in_progress
= false;
4641 tline
= delete_Token(tline
);
4644 tline
= tline
->next
;
4652 * Now scan the entire line and look for successive TOK_IDs that resulted
4653 * after expansion (they can't be produced by tokenize()). The successive
4654 * TOK_IDs should be concatenated.
4655 * Also we look for %+ tokens and concatenate the tokens before and after
4656 * them (without white spaces in between).
4659 const struct tokseq_match t
[] = {
4661 PP_CONCAT_MASK(TOK_ID
) |
4662 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4663 PP_CONCAT_MASK(TOK_ID
) |
4664 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4665 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4668 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4670 * If we concatenated something, *and* we had previously expanded
4671 * an actual macro, scan the lines again for macros...
4682 *org_tline
= *thead
;
4683 /* since we just gave text to org_line, don't free it */
4685 delete_Token(thead
);
4687 /* the expression expanded to empty line;
4688 we can't return NULL for some reasons
4689 we just set the line to a single WHITESPACE token. */
4690 memset(org_tline
, 0, sizeof(*org_tline
));
4691 org_tline
->text
= NULL
;
4692 org_tline
->type
= TOK_WHITESPACE
;
4701 * Similar to expand_smacro but used exclusively with macro identifiers
4702 * right before they are fetched in. The reason is that there can be
4703 * identifiers consisting of several subparts. We consider that if there
4704 * are more than one element forming the name, user wants a expansion,
4705 * otherwise it will be left as-is. Example:
4709 * the identifier %$abc will be left as-is so that the handler for %define
4710 * will suck it and define the corresponding value. Other case:
4712 * %define _%$abc cde
4714 * In this case user wants name to be expanded *before* %define starts
4715 * working, so we'll expand %$abc into something (if it has a value;
4716 * otherwise it will be left as-is) then concatenate all successive
4719 static Token
*expand_id(Token
* tline
)
4721 Token
*cur
, *oldnext
= NULL
;
4723 if (!tline
|| !tline
->next
)
4728 (cur
->next
->type
== TOK_ID
||
4729 cur
->next
->type
== TOK_PREPROC_ID
4730 || cur
->next
->type
== TOK_NUMBER
))
4733 /* If identifier consists of just one token, don't expand */
4738 oldnext
= cur
->next
; /* Detach the tail past identifier */
4739 cur
->next
= NULL
; /* so that expand_smacro stops here */
4742 tline
= expand_smacro(tline
);
4745 /* expand_smacro possibly changhed tline; re-scan for EOL */
4747 while (cur
&& cur
->next
)
4750 cur
->next
= oldnext
;
4757 * Determine whether the given line constitutes a multi-line macro
4758 * call, and return the ExpDef structure called if so. Doesn't have
4759 * to check for an initial label - that's taken care of in
4760 * expand_mmacro - but must check numbers of parameters. Guaranteed
4761 * to be called with tline->type == TOK_ID, so the putative macro
4762 * name is easy to find.
4764 static ExpDef
*is_mmacro(Token
* tline
, Token
*** params_array
)
4770 head
= (ExpDef
*) hash_findix(&expdefs
, tline
->text
);
4773 * Efficiency: first we see if any macro exists with the given
4774 * name. If not, we can return NULL immediately. _Then_ we
4775 * count the parameters, and then we look further along the
4776 * list if necessary to find the proper ExpDef.
4778 list_for_each(ed
, head
)
4779 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4785 * OK, we have a potential macro. Count and demarcate the
4788 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4791 * So we know how many parameters we've got. Find the ExpDef
4792 * structure that handles this number.
4795 if (ed
->nparam_min
<= nparam
4796 && (ed
->plus
|| nparam
<= ed
->nparam_max
)) {
4798 * It's right, and we can use it. Add its default
4799 * parameters to the end of our list if necessary.
4801 if (ed
->defaults
&& nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4803 nasm_realloc(params
,
4804 ((ed
->nparam_min
+ ed
->ndefs
+
4805 1) * sizeof(*params
)));
4806 while (nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4807 params
[nparam
] = ed
->defaults
[nparam
- ed
->nparam_min
];
4812 * If we've gone over the maximum parameter count (and
4813 * we're in Plus mode), ignore parameters beyond
4816 if (ed
->plus
&& nparam
> ed
->nparam_max
)
4817 nparam
= ed
->nparam_max
;
4819 * Then terminate the parameter list, and leave.
4821 if (!params
) { /* need this special case */
4822 params
= nasm_malloc(sizeof(*params
));
4825 params
[nparam
] = NULL
;
4826 *params_array
= params
;
4830 * This one wasn't right: look for the next one with the
4833 list_for_each(ed
, ed
->next
)
4834 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4839 * After all that, we didn't find one with the right number of
4840 * parameters. Issue a warning, and fail to expand the macro.
4842 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4843 "macro `%s' exists, but not taking %d parameters",
4844 tline
->text
, nparam
);
4850 * Expand the multi-line macro call made by the given line, if
4851 * there is one to be expanded. If there is, push the expansion on
4852 * istk->expansion and return true. Otherwise return false.
4854 static bool expand_mmacro(Token
* tline
)
4856 Token
*label
= NULL
;
4857 int dont_prepend
= 0;
4858 Token
**params
, *t
, *mtok
;
4862 int i
, nparam
, *paramlen
;
4867 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4868 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4871 ed
= is_mmacro(t
, ¶ms
);
4877 * We have an id which isn't a macro call. We'll assume
4878 * it might be a label; we'll also check to see if a
4879 * colon follows it. Then, if there's another id after
4880 * that lot, we'll check it again for macro-hood.
4884 if (tok_type_(t
, TOK_WHITESPACE
))
4885 last
= t
, t
= t
->next
;
4886 if (tok_is_(t
, ":")) {
4888 last
= t
, t
= t
->next
;
4889 if (tok_type_(t
, TOK_WHITESPACE
))
4890 last
= t
, t
= t
->next
;
4892 if (!tok_type_(t
, TOK_ID
) || !(ed
= is_mmacro(t
, ¶ms
)))
4900 * Fix up the parameters: this involves stripping leading and
4901 * trailing whitespace, then stripping braces if they are
4904 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4905 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4907 for (i
= 0; params
[i
]; i
++) {
4909 int comma
= (!ed
->plus
|| i
< nparam
- 1);
4913 if (tok_is_(t
, "{"))
4914 t
= t
->next
, brace
= true, comma
= false;
4918 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4919 break; /* ... because we have hit a comma */
4920 if (comma
&& t
->type
== TOK_WHITESPACE
4921 && tok_is_(t
->next
, ","))
4922 break; /* ... or a space then a comma */
4923 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4924 break; /* ... or a brace */
4930 if (ed
->cur_depth
>= ed
->max_depth
) {
4931 if (ed
->max_depth
> 1) {
4933 "reached maximum macro recursion depth of %i for %s",
4934 ed
->max_depth
,ed
->name
);
4942 * OK, we have found a ExpDef structure representing a
4943 * previously defined mmacro. Create an expansion invocation
4944 * and point it back to the expansion definition. Substitution of
4945 * parameter tokens and macro-local tokens doesn't get done
4946 * until the single-line macro substitution process; this is
4947 * because delaying them allows us to change the semantics
4948 * later through %rotate.
4950 ei
= new_ExpInv(EXP_MMACRO
, ed
);
4951 ei
->name
= nasm_strdup(mname
);
4952 // ei->label = label;
4953 // ei->label_text = detoken(label, false);
4954 ei
->current
= ed
->line
;
4955 ei
->emitting
= true;
4956 // ei->iline = tline;
4957 ei
->params
= params
;
4958 ei
->nparam
= nparam
;
4960 ei
->paramlen
= paramlen
;
4963 ei
->prev
= istk
->expansion
;
4964 istk
->expansion
= ei
;
4967 * Special case: detect %00 on first invocation; if found,
4968 * avoid emitting any labels that precede the mmacro call.
4969 * ed->prepend is set to -1 when %00 is detected, else 1.
4971 if (ed
->prepend
== 0) {
4972 for (l
= ed
->line
; l
!= NULL
; l
= l
->next
) {
4973 for (t
= l
->first
; t
!= NULL
; t
= t
->next
) {
4974 if ((t
->type
== TOK_PREPROC_ID
) &&
4975 (strlen(t
->text
) == 3) &&
4976 (t
->text
[1] == '0') && (t
->text
[2] == '0')) {
4981 if (dont_prepend
< 0) {
4985 ed
->prepend
= ((dont_prepend
< 0) ? -1 : 1);
4989 * If we had a label, push it on as the first line of
4990 * the macro expansion.
4992 if (label
!= NULL
) {
4993 if (ed
->prepend
< 0) {
4994 ei
->label_text
= detoken(label
, false);
4996 if (dont_prepend
== 0) {
4998 while (t
->next
!= NULL
) {
5001 t
->next
= new_Token(NULL
, TOK_OTHER
, ":", 0);
5003 Line
*l
= new_Line();
5004 l
->first
= copy_Token(label
);
5005 l
->next
= ei
->current
;
5010 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
5016 /* The function that actually does the error reporting */
5017 static void verror(int severity
, const char *fmt
, va_list arg
)
5021 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
5023 if ((istk
!= NULL
) && (istk
->mmac_depth
> 0)) {
5024 ExpInv
*ei
= istk
->expansion
;
5025 int lineno
= ei
->lineno
;
5026 while (ei
!= NULL
) {
5027 if (ei
->type
== EXP_MMACRO
) {
5030 lineno
+= ei
->relno
;
5033 nasm_error(severity
, "(%s:%d) %s", ei
->def
->name
,
5036 nasm_error(severity
, "%s", buff
);
5041 * Since preprocessor always operate only on the line that didn't
5042 * arrived yet, we should always use ERR_OFFBY1.
5044 static void error(int severity
, const char *fmt
, ...)
5048 verror(severity
, fmt
, arg
);
5053 * Because %else etc are evaluated in the state context
5054 * of the previous branch, errors might get lost with error():
5055 * %if 0 ... %else trailing garbage ... %endif
5056 * So %else etc should report errors with this function.
5058 static void error_precond(int severity
, const char *fmt
, ...)
5062 /* Only ignore the error if it's really in a dead branch */
5063 if ((istk
!= NULL
) &&
5064 (istk
->expansion
!= NULL
) &&
5065 (istk
->expansion
->type
== EXP_IF
) &&
5066 (istk
->expansion
->def
->state
== COND_NEVER
))
5070 verror(severity
, fmt
, arg
);
5075 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
5080 istk
= nasm_malloc(sizeof(Include
));
5082 istk
->expansion
= NULL
;
5083 istk
->fp
= fopen(file
, "r");
5085 src_set_fname(nasm_strdup(file
));
5088 istk
->mmac_depth
= 0;
5090 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
5095 nested_mac_count
= 0;
5096 nested_rep_count
= 0;
5099 if (tasm_compatible_mode
) {
5100 stdmacpos
= nasm_stdmac
;
5102 stdmacpos
= nasm_stdmac_after_tasm
;
5104 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
5109 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5110 * The caller, however, will also pass in 3 for preprocess-only so
5111 * we can set __PASS__ accordingly.
5113 pass
= apass
> 2 ? 2 : apass
;
5115 dephead
= deptail
= deplist
;
5117 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
5119 strcpy(sl
->str
, file
);
5121 deptail
= &sl
->next
;
5125 * Define the __PASS__ macro. This is defined here unlike
5126 * all the other builtins, because it is special -- it varies between
5129 t
= nasm_malloc(sizeof(*t
));
5131 make_tok_num(t
, apass
);
5133 define_smacro(NULL
, "__PASS__", true, 0, t
);
5136 static char *pp_getline(void)
5144 * Fetch a tokenized line, either from the expansion
5145 * buffer or from the input file.
5149 while (1) { /* until we get a line we can use */
5151 * Fetch a tokenized line from the expansion buffer
5153 if (istk
->expansion
!= NULL
) {
5154 ei
= istk
->expansion
;
5155 if (ei
->current
!= NULL
) {
5156 if (ei
->emitting
== false) {
5162 ei
->current
= l
->next
;
5164 tline
= copy_Token(l
->first
);
5165 if (((ei
->type
== EXP_REP
) ||
5166 (ei
->type
== EXP_MMACRO
) ||
5167 (ei
->type
== EXP_WHILE
))
5168 && (ei
->def
->nolist
== false)) {
5169 char *p
= detoken(tline
, false);
5170 list
->line(LIST_MACRO
, p
);
5173 if (ei
->linnum
> -1) {
5174 src_set_linnum(src_get_linnum() + 1);
5177 } else if ((ei
->type
== EXP_REP
) &&
5178 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5179 ei
->def
->cur_depth
++;
5180 ei
->current
= ei
->def
->line
;
5183 } else if ((ei
->type
== EXP_WHILE
) &&
5184 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5185 ei
->current
= ei
->def
->line
;
5187 tline
= copy_Token(ei
->current
->first
);
5188 int j
= if_condition(tline
, PP_WHILE
);
5190 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
5191 if (j
== COND_IF_TRUE
) {
5192 ei
->current
= ei
->current
->next
;
5193 ei
->def
->cur_depth
++;
5195 ei
->emitting
= false;
5197 ei
->def
->cur_depth
= ei
->def
->max_depth
;
5201 istk
->expansion
= ei
->prev
;
5202 ExpDef
*ed
= ei
->def
;
5204 if ((ei
->emitting
== true) &&
5205 (ed
->max_depth
== DEADMAN_LIMIT
) &&
5206 (ed
->cur_depth
== DEADMAN_LIMIT
)
5208 error(ERR_FATAL
, "runaway expansion detected, aborting");
5210 if (ed
->cur_depth
> 0) {
5212 } else if ((ed
->type
!= EXP_MMACRO
) && (ed
->type
!= EXP_IF
)) {
5213 /***** should this really be right here??? *****/
5215 Line *l = NULL, *ll = NULL;
5216 for (l = ed->line; l != NULL;) {
5217 if (l->first != NULL) {
5218 free_tlist(l->first);
5225 expansions = ed->prev;
5229 if ((ei
->type
== EXP_REP
) ||
5230 (ei
->type
== EXP_MMACRO
) ||
5231 (ei
->type
== EXP_WHILE
)) {
5232 list
->downlevel(LIST_MACRO
);
5233 if (ei
->type
== EXP_MMACRO
) {
5238 if (ei
->linnum
> -1) {
5239 src_set_linnum(ei
->linnum
);
5247 * Read in line from input and tokenize
5250 if (line
) { /* from the current input file */
5251 line
= prepreproc(line
);
5252 tline
= tokenize(line
);
5258 * The current file has ended; work down the istk
5263 if (i
->expansion
!= NULL
) {
5265 "end of file while still in an expansion");
5267 /* only set line and file name if there's a next node */
5269 src_set_linnum(i
->lineno
);
5270 nasm_free(src_set_fname(i
->fname
));
5272 if ((i
->next
== NULL
) && (finals
!= NULL
)) {
5274 ei
= new_ExpInv(EXP_FINAL
, NULL
);
5275 ei
->emitting
= true;
5276 ei
->current
= finals
;
5277 istk
->expansion
= ei
;
5282 list
->downlevel(LIST_INCLUDE
);
5285 if (finals
!= NULL
) {
5295 if (defining
== NULL
) {
5296 tline
= expand_mmac_params(tline
);
5300 * Check the line to see if it's a preprocessor directive.
5302 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5304 } else if (defining
!= NULL
) {
5306 * We're defining an expansion. We emit nothing at all,
5307 * and just shove the tokenized line on to the definition.
5309 if (defining
->ignoring
== false) {
5310 Line
*l
= new_Line();
5312 if (defining
->line
== NULL
) {
5316 defining
->last
->next
= l
;
5320 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5322 defining
->linecount
++;
5324 } else if ((istk
->expansion
!= NULL
) &&
5325 (istk
->expansion
->emitting
!= true)) {
5327 * We're in a non-emitting branch of an expansion.
5328 * Emit nothing at all, not even a blank line: when we
5329 * emerge from the expansion we'll give a line-number
5330 * directive so we keep our place correctly.
5335 tline
= expand_smacro(tline
);
5336 if (expand_mmacro(tline
) != true) {
5338 * De-tokenize the line again, and emit it.
5340 line
= detoken(tline
, true);
5351 static void pp_cleanup(int pass
)
5353 if (defining
!= NULL
) {
5354 error(ERR_NONFATAL
, "end of file while still defining an expansion");
5355 while (defining
!= NULL
) {
5356 ExpDef
*ed
= defining
;
5357 defining
= ed
->prev
;
5362 while (cstk
!= NULL
)
5365 while (istk
!= NULL
) {
5369 nasm_free(i
->fname
);
5371 while (i
->expansion
!= NULL
) {
5372 ExpInv
*ei
= i
->expansion
;
5373 i
->expansion
= ei
->prev
;
5379 nasm_free(src_set_fname(NULL
));
5384 while ((i
= ipath
)) {
5393 void pp_include_path(char *path
)
5397 i
= nasm_malloc(sizeof(IncPath
));
5398 i
->path
= path
? nasm_strdup(path
) : NULL
;
5411 void pp_pre_include(char *fname
)
5413 Token
*inc
, *space
, *name
;
5416 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5417 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5418 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5426 void pp_pre_define(char *definition
)
5432 equals
= strchr(definition
, '=');
5433 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5434 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5437 space
->next
= tokenize(definition
);
5447 void pp_pre_undefine(char *definition
)
5452 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5453 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5454 space
->next
= tokenize(definition
);
5463 * This function is used to assist with "runtime" preprocessor
5464 * directives, e.g. pp_runtime("%define __BITS__ 64");
5466 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5467 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5470 void pp_runtime(char *definition
)
5474 def
= tokenize(definition
);
5475 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5480 void pp_extra_stdmac(macros_t
*macros
)
5482 extrastdmac
= macros
;
5485 static void make_tok_num(Token
* tok
, int64_t val
)
5488 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5489 tok
->text
= nasm_strdup(numbuf
);
5490 tok
->type
= TOK_NUMBER
;