1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
84 typedef struct SMacro SMacro
;
85 typedef struct ExpDef ExpDef
;
86 typedef struct ExpInv ExpInv
;
87 typedef struct Context Context
;
88 typedef struct Token Token
;
89 typedef struct Blocks Blocks
;
90 typedef struct Line Line
;
91 typedef struct Include Include
;
92 typedef struct Cond Cond
;
93 typedef struct IncPath IncPath
;
96 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
105 * Store the definition of a single-line macro.
117 * The context stack is composed of a linked list of these.
122 struct hash_table localmac
;
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
134 * %define a(x,y) ( (x) & ~(y) )
136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
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 */
218 bool plus
; /* is the last parameter greedy? */
219 bool nolist
; /* is this expansion listing-inhibited? */
220 Token
*dlist
; /* all defaults as one list */
221 Token
**defaults
; /* parameter default pointers */
222 int ndefs
; /* number of default parameters */
224 int prepend
; /* label prepend state */
228 int linecount
; /* number of lines within expansion */
230 int64_t def_depth
; /* current number of definition pairs deep */
231 int64_t cur_depth
; /* current number of expansions */
232 int64_t max_depth
; /* maximum number of expansions allowed */
234 int state
; /* condition state */
235 bool ignoring
; /* ignoring definition lines */
239 * Store the invocation of an expansion.
241 * The `prev' field is for the `istk->expansion` linked-list.
243 * When an expansion is being expanded, `params', `iline', `nparam',
244 * `paramlen', `rotate' and `unique' are local to the invocation.
247 ExpInv
*prev
; /* previous invocation */
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 */
261 int lineno
; /* current line number in expansion */
262 int linnum
; /* line number at invocation */
263 int relno
; /* relative line number at invocation */
264 enum pp_exp_type type
; /* expansion type */
269 * To handle an arbitrary level of file inclusion, we maintain a
270 * stack (ie linked list) of these things.
284 * Include search path. This is simply a list of strings which get
285 * prepended, in turn, to the name of an include file, in an
286 * attempt to find the file if it's not in the current directory.
294 * Conditional assembly: we maintain a separate stack of these for
295 * each level of file inclusion. (The only reason we keep the
296 * stacks separate is to ensure that a stray `%endif' in a file
297 * included from within the true branch of a `%if' won't terminate
298 * it and cause confusion: instead, rightly, it'll cause an error.)
302 * These states are for use just after %if or %elif: IF_TRUE
303 * means the condition has evaluated to truth so we are
304 * currently emitting, whereas IF_FALSE means we are not
305 * currently emitting but will start doing so if a %else comes
306 * up. In these states, all directives are admissible: %elif,
307 * %else and %endif. (And of course %if.)
309 COND_IF_TRUE
, COND_IF_FALSE
,
311 * These states come up after a %else: ELSE_TRUE means we're
312 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
313 * any %elif or %else will cause an error.
315 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
317 * These states mean that we're not emitting now, and also that
318 * nothing until %endif will be emitted at all. COND_DONE is
319 * used when we've had our moment of emission
320 * and have now started seeing %elifs. COND_NEVER is used when
321 * the condition construct in question is contained within a
322 * non-emitting branch of a larger condition construct,
323 * or if there is an error.
325 COND_DONE
, COND_NEVER
329 * These defines are used as the possible return values for do_directive
331 #define NO_DIRECTIVE_FOUND 0
332 #define DIRECTIVE_FOUND 1
335 * This define sets the upper limit for smacro and expansions
337 #define DEADMAN_LIMIT (1 << 20)
340 #define REP_LIMIT ((INT64_C(1) << 62))
343 * Condition codes. Note that we use c_ prefix not C_ because C_ is
344 * used in nasm.h for the "real" condition codes. At _this_ level,
345 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
346 * ones, so we need a different enum...
348 static const char * const conditions
[] = {
349 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
350 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
351 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
354 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
355 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
356 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
359 static const enum pp_conds inverse_ccs
[] = {
360 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
361 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
,
362 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
365 /* For TASM compatibility we need to be able to recognise TASM compatible
366 * conditional compilation directives. Using the NASM pre-processor does
367 * not work, so we look for them specifically from the following list and
368 * then jam in the equivalent NASM directive into the input stream.
372 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
373 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
376 static const char * const tasm_directives
[] = {
377 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
378 "ifndef", "include", "local"
381 static int StackSize
= 4;
382 static char *StackPointer
= "ebp";
383 static int ArgOffset
= 8;
384 static int LocalOffset
= 0;
386 static Context
*cstk
;
387 static Include
*istk
;
388 static IncPath
*ipath
= NULL
;
390 static int pass
; /* HACK: pass 0 = generate dependencies only */
391 static StrList
**dephead
, **deptail
; /* Dependency list */
393 static uint64_t unique
; /* unique identifier numbers */
395 static Line
*predef
= NULL
;
396 static bool do_predef
;
398 static ListGen
*list
;
401 * The current set of expansion definitions we have defined.
403 static struct hash_table expdefs
;
406 * The current set of single-line macros we have defined.
408 static struct hash_table smacros
;
411 * Linked List of all active expansion definitions
413 struct ExpDef
*expansions
= NULL
;
416 * The expansion we are currently defining
418 static ExpDef
*defining
= NULL
;
420 static uint64_t nested_mac_count
;
421 static uint64_t nested_rep_count
;
424 * Linked-list of lines to preprocess, prior to cleanup
426 static Line
*finals
= NULL
;
427 static bool in_final
= false;
430 * The number of macro parameters to allocate space for at a time.
432 #define PARAM_DELTA 16
435 * The standard macro set: defined in macros.c in the array nasm_stdmac.
436 * This gives our position in the macro set, when we're processing it.
438 static macros_t
*stdmacpos
;
441 * The extra standard macros that come from the object format, if
444 static macros_t
*extrastdmac
= NULL
;
445 static bool any_extrastdmac
;
448 * Tokens are allocated in blocks to improve speed
450 #define TOKEN_BLOCKSIZE 4096
451 static Token
*freeTokens
= NULL
;
457 static Blocks blocks
= { NULL
, NULL
};
460 * Forward declarations.
462 static Token
*expand_mmac_params(Token
* tline
);
463 static Token
*expand_smacro(Token
* tline
);
464 static Token
*expand_id(Token
* tline
);
465 static Context
*get_ctx(const char *name
, const char **namep
);
466 static void make_tok_num(Token
* tok
, int64_t val
);
467 static void error(int severity
, const char *fmt
, ...);
468 static void error_precond(int severity
, const char *fmt
, ...);
469 static void *new_Block(size_t size
);
470 static void delete_Blocks(void);
471 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
472 const char *text
, int txtlen
);
473 static Token
*copy_Token(Token
* tline
);
474 static Token
*delete_Token(Token
* t
);
475 static Line
*new_Line(void);
476 static ExpDef
*new_ExpDef(int exp_type
);
477 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
);
480 * Macros for safe checking of token pointers, avoid *(NULL)
482 #define tok_type_(x,t) ((x) && (x)->type == (t))
483 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
484 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
485 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
488 * A few helpers for single macros
491 /* We might be not smacro parameter at all */
492 static bool is_smacro_param(Token
*t
)
494 return t
->type
>= TOK_SMAC_PARAM
;
497 /* smacro parameters are counted in a special way */
498 static int smacro_get_param_idx(Token
*t
)
500 return t
->type
- TOK_SMAC_PARAM
;
503 /* encode smacro parameter index */
504 static int smacro_set_param_idx(Token
*t
, unsigned int index
)
506 return t
->type
= TOK_SMAC_PARAM
+ index
;
511 #define stringify(x) #x
513 #define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
514 #define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
515 #define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
517 /* FIXME: we really need some compound type here instead of inplace code */
518 static const char *nasm_get_tok_type_str(enum pp_token_type type
)
520 #define SWITCH_TOK_NAME(type) \
522 return stringify(type)
525 SWITCH_TOK_NAME(TOK_NONE
);
526 SWITCH_TOK_NAME(TOK_WHITESPACE
);
527 SWITCH_TOK_NAME(TOK_COMMENT
);
528 SWITCH_TOK_NAME(TOK_ID
);
529 SWITCH_TOK_NAME(TOK_PREPROC_ID
);
530 SWITCH_TOK_NAME(TOK_STRING
);
531 SWITCH_TOK_NAME(TOK_NUMBER
);
532 SWITCH_TOK_NAME(TOK_FLOAT
);
533 SWITCH_TOK_NAME(TOK_SMAC_END
);
534 SWITCH_TOK_NAME(TOK_OTHER
);
535 SWITCH_TOK_NAME(TOK_INTERNAL_STRING
);
536 SWITCH_TOK_NAME(TOK_PREPROC_Q
);
537 SWITCH_TOK_NAME(TOK_PREPROC_QQ
);
538 SWITCH_TOK_NAME(TOK_PASTE
);
539 SWITCH_TOK_NAME(TOK_INDIRECT
);
540 SWITCH_TOK_NAME(TOK_SMAC_PARAM
);
541 SWITCH_TOK_NAME(TOK_MAX
);
547 static void nasm_raw_dump_token(Token
*token
, const char *file
, int line
, const char *func
)
549 printf("---[%s (%s:%d): %p]---\n", func
, file
, line
, (void *)token
);
552 list_for_each(t
, token
) {
554 printf("'%s'(%s) ", t
->text
,
555 nasm_get_tok_type_str(t
->type
));
561 static void nasm_raw_dump_stream(Token
*token
, const char *file
, int line
, const char *func
)
563 printf("---[%s (%s:%d): %p]---\n", func
, file
, line
, (void *)token
);
566 list_for_each(t
, token
)
567 printf("%s", t
->text
? t
->text
: " ");
573 #define nasm_trace(msg, ...)
574 #define nasm_dump_token(t)
575 #define nasm_dump_stream(t)
579 * nasm_unquote with error if the string contains NUL characters.
580 * If the string contains NUL characters, issue an error and return
581 * the C len, i.e. truncate at the NUL.
583 static size_t nasm_unquote_cstr(char *qstr
, enum preproc_token directive
)
585 size_t len
= nasm_unquote(qstr
, NULL
);
586 size_t clen
= strlen(qstr
);
589 error(ERR_NONFATAL
, "NUL character in `%s' directive",
590 pp_directives
[directive
]);
596 * In-place reverse a list of tokens.
598 static Token
*reverse_tokens(Token
*t
)
602 list_reverse(t
, prev
, next
);
608 * Handle TASM specific directives, which do not contain a % in
609 * front of them. We do it here because I could not find any other
610 * place to do it for the moment, and it is a hack (ideally it would
611 * be nice to be able to use the NASM pre-processor to do it).
613 static char *check_tasm_directive(char *line
)
615 int32_t i
, j
, k
, m
, len
;
616 char *p
, *q
, *oldline
, oldchar
;
618 p
= nasm_skip_spaces(line
);
620 /* Binary search for the directive name */
622 j
= ARRAY_SIZE(tasm_directives
);
623 q
= nasm_skip_word(p
);
630 m
= nasm_stricmp(p
, tasm_directives
[k
]);
632 /* We have found a directive, so jam a % in front of it
633 * so that NASM will then recognise it as one if it's own.
638 line
= nasm_malloc(len
+ 2);
640 if (k
== TM_IFDIFI
) {
642 * NASM does not recognise IFDIFI, so we convert
643 * it to %if 0. This is not used in NASM
644 * compatible code, but does need to parse for the
645 * TASM macro package.
647 strcpy(line
+ 1, "if 0");
649 memcpy(line
+ 1, p
, len
+ 1);
664 * The pre-preprocessing stage... This function translates line
665 * number indications as they emerge from GNU cpp (`# lineno "file"
666 * flags') into NASM preprocessor line number indications (`%line
669 static char *prepreproc(char *line
)
672 char *fname
, *oldline
;
674 if (line
[0] == '#' && line
[1] == ' ') {
677 lineno
= atoi(fname
);
678 fname
+= strspn(fname
, "0123456789 ");
681 fnlen
= strcspn(fname
, "\"");
682 line
= nasm_malloc(20 + fnlen
);
683 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
686 if (tasm_compatible_mode
)
687 return check_tasm_directive(line
);
692 * Free a linked list of tokens.
694 static void free_tlist(Token
* list
)
697 list
= delete_Token(list
);
701 * Free a linked list of lines.
703 static void free_llist(Line
* list
)
706 list_for_each_safe(l
, tmp
, list
) {
707 free_tlist(l
->first
);
715 static void free_expdef(ExpDef
* ed
)
718 free_tlist(ed
->dlist
);
719 nasm_free(ed
->defaults
);
720 free_llist(ed
->line
);
727 static void free_expinv(ExpInv
* ei
)
730 nasm_free(ei
->label_text
);
735 * Free all currently defined macros, and free the hash tables
737 static void free_smacro_table(struct hash_table
*smt
)
741 struct hash_tbl_node
*it
= NULL
;
743 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
744 nasm_free((void *)key
);
745 list_for_each_safe(s
, tmp
, s
) {
747 free_tlist(s
->expansion
);
754 static void free_expdef_table(struct hash_table
*edt
)
758 struct hash_tbl_node
*it
= NULL
;
761 while ((ed
= hash_iterate(edt
, &it
, &key
)) != NULL
) {
762 nasm_free((void *)key
);
763 list_for_each_safe(ed
,tmp
, ed
)
769 static void free_macros(void)
771 free_smacro_table(&smacros
);
772 free_expdef_table(&expdefs
);
776 * Initialize the hash tables
778 static void init_macros(void)
780 hash_init(&smacros
, HASH_LARGE
);
781 hash_init(&expdefs
, HASH_LARGE
);
785 * Pop the context stack.
787 static void ctx_pop(void)
792 free_smacro_table(&c
->localmac
);
798 * Search for a key in the hash index; adding it if necessary
799 * (in which case we initialize the data pointer to NULL.)
802 hash_findi_add(struct hash_table
*hash
, const char *str
)
804 struct hash_insert hi
;
808 r
= hash_findi(hash
, str
, &hi
);
812 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
813 return hash_add(&hi
, strx
, NULL
);
817 * Like hash_findi, but returns the data element rather than a pointer
818 * to it. Used only when not adding a new element, hence no third
822 hash_findix(struct hash_table
*hash
, const char *str
)
826 p
= hash_findi(hash
, str
, NULL
);
827 return p
? *p
: NULL
;
831 * read line from standard macros set,
832 * if there no more left -- return NULL
834 static char *line_from_stdmac(void)
837 const unsigned char *p
= stdmacpos
;
846 len
+= pp_directives_len
[c
- 0x80] + 1;
851 line
= nasm_malloc(len
+ 1);
853 while ((c
= *stdmacpos
++)) {
855 memcpy(q
, pp_directives
[c
- 0x80], pp_directives_len
[c
- 0x80]);
856 q
+= pp_directives_len
[c
- 0x80];
866 /* This was the last of the standard macro chain... */
868 if (any_extrastdmac
) {
869 stdmacpos
= extrastdmac
;
870 any_extrastdmac
= false;
871 } else if (do_predef
) {
874 Token
*head
, **tail
, *t
;
877 * Nasty hack: here we push the contents of
878 * `predef' on to the top-level expansion stack,
879 * since this is the most convenient way to
880 * implement the pre-include and pre-define
883 list_for_each(pd
, predef
) {
886 list_for_each(t
, pd
->first
) {
887 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
888 tail
= &(*tail
)->next
;
893 ei
= new_ExpInv(EXP_PREDEF
, NULL
);
896 ei
->prev
= istk
->expansion
;
897 istk
->expansion
= ei
;
906 #define BUF_DELTA 512
908 * Read a line from the top file in istk, handling multiple CR/LFs
909 * at the end of the line read, and handling spurious ^Zs. Will
910 * return lines from the standard macro set if this has not already
913 static char *read_line(void)
915 char *buffer
, *p
, *q
;
916 int bufsize
, continued_count
;
919 * standart macros set (predefined) goes first
921 p
= line_from_stdmac();
926 * regular read from a file
929 buffer
= nasm_malloc(BUF_DELTA
);
933 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
937 if (p
> buffer
&& p
[-1] == '\n') {
939 * Convert backslash-CRLF line continuation sequences into
940 * nothing at all (for DOS and Windows)
942 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
948 * Also convert backslash-LF line continuation sequences into
949 * nothing at all (for Unix)
951 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
959 if (p
- buffer
> bufsize
- 10) {
960 int32_t offset
= p
- buffer
;
961 bufsize
+= BUF_DELTA
;
962 buffer
= nasm_realloc(buffer
, bufsize
);
963 p
= buffer
+ offset
; /* prevent stale-pointer problems */
967 if (!q
&& p
== buffer
) {
972 src_set_linnum(src_get_linnum() + istk
->lineinc
+
973 (continued_count
* istk
->lineinc
));
976 * Play safe: remove CRs as well as LFs, if any of either are
977 * present at the end of the line.
979 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
983 * Handle spurious ^Z, which may be inserted into source files
984 * by some file transfer utilities.
986 buffer
[strcspn(buffer
, "\032")] = '\0';
988 list
->line(LIST_READ
, buffer
);
994 * Tokenize a line of text. This is a very simple process since we
995 * don't need to parse the value out of e.g. numeric tokens: we
996 * simply split one string into many.
998 static Token
*tokenize(char *line
)
1001 enum pp_token_type type
;
1003 Token
*t
, **tail
= &list
;
1004 bool verbose
= true;
1006 nasm_trace("Tokenize for '%s'", line
);
1008 if ((defining
!= NULL
) && (defining
->ignoring
== true)) {
1016 if (*p
== '+' && !nasm_isdigit(p
[1])) {
1019 } else if (nasm_isdigit(*p
) ||
1020 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
1024 while (nasm_isdigit(*p
));
1025 type
= TOK_PREPROC_ID
;
1026 } else if (*p
== '{') {
1028 while (*p
&& *p
!= '}') {
1035 type
= TOK_PREPROC_ID
;
1036 } else if (*p
== '[') {
1038 line
+= 2; /* Skip the leading %[ */
1040 while (lvl
&& (c
= *p
++)) {
1052 p
= nasm_skip_string(p
- 1) + 1;
1062 error(ERR_NONFATAL
, "unterminated %[ construct");
1063 type
= TOK_INDIRECT
;
1064 } else if (*p
== '?') {
1065 type
= TOK_PREPROC_Q
; /* %? */
1068 type
= TOK_PREPROC_QQ
; /* %?? */
1071 } else if (*p
== '!') {
1072 type
= TOK_PREPROC_ID
;
1077 } while (isidchar(*p
));
1078 } else if (*p
== '\'' || *p
== '\"' || *p
== '`') {
1079 p
= nasm_skip_string(p
);
1083 error(ERR_NONFATAL
|ERR_PASS1
, "unterminated %! string");
1085 /* %! without string or identifier */
1086 type
= TOK_OTHER
; /* Legacy behavior... */
1088 } else if (isidchar(*p
) ||
1089 ((*p
== '!' || *p
== '%' || *p
== '$') &&
1094 while (isidchar(*p
));
1095 type
= TOK_PREPROC_ID
;
1101 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
1104 while (*p
&& isidchar(*p
))
1106 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
1111 p
= nasm_skip_string(p
);
1115 } else if(verbose
) {
1116 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
1117 /* Handling unterminated strings by UNV */
1120 } else if (p
[0] == '$' && p
[1] == '$') {
1121 type
= TOK_OTHER
; /* TOKEN_BASE */
1123 } else if (isnumstart(*p
)) {
1124 bool is_hex
= false;
1125 bool is_float
= false;
1141 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
1143 if (*p
== '+' || *p
== '-') {
1145 * e can only be followed by +/- if it is either a
1146 * prefixed hex number or a floating-point number
1151 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
1153 } else if (c
== 'P' || c
== 'p') {
1155 if (*p
== '+' || *p
== '-')
1157 } else if (isnumchar(c
) || c
== '_')
1158 ; /* just advance */
1159 else if (c
== '.') {
1161 * we need to deal with consequences of the legacy
1162 * parser, like "1.nolist" being two tokens
1163 * (TOK_NUMBER, TOK_ID) here; at least give it
1164 * a shot for now. In the future, we probably need
1165 * a flex-based scanner with proper pattern matching
1166 * to do it as well as it can be done. Nothing in
1167 * the world is going to help the person who wants
1168 * 0x123.p16 interpreted as two tokens, though.
1174 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
1175 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
1176 (*r
== 'p' || *r
== 'P')) {
1180 break; /* Terminate the token */
1184 p
--; /* Point to first character beyond number */
1186 if (p
== line
+1 && *line
== '$') {
1187 type
= TOK_OTHER
; /* TOKEN_HERE */
1189 if (has_e
&& !is_hex
) {
1190 /* 1e13 is floating-point, but 1e13h is not */
1194 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
1196 } else if (nasm_isspace(*p
)) {
1197 type
= TOK_WHITESPACE
;
1198 p
= nasm_skip_spaces(p
);
1200 * Whitespace just before end-of-line is discarded by
1201 * pretending it's a comment; whitespace just before a
1202 * comment gets lumped into the comment.
1204 if (!*p
|| *p
== ';') {
1209 } else if (*p
== ';') {
1215 * Anything else is an operator of some kind. We check
1216 * for all the double-character operators (>>, <<, //,
1217 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1218 * else is a single-character operator.
1221 if ((p
[0] == '>' && p
[1] == '>') ||
1222 (p
[0] == '<' && p
[1] == '<') ||
1223 (p
[0] == '/' && p
[1] == '/') ||
1224 (p
[0] == '<' && p
[1] == '=') ||
1225 (p
[0] == '>' && p
[1] == '=') ||
1226 (p
[0] == '=' && p
[1] == '=') ||
1227 (p
[0] == '!' && p
[1] == '=') ||
1228 (p
[0] == '<' && p
[1] == '>') ||
1229 (p
[0] == '&' && p
[1] == '&') ||
1230 (p
[0] == '|' && p
[1] == '|') ||
1231 (p
[0] == '^' && p
[1] == '^')) {
1237 /* Handling unterminated string by UNV */
1240 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1241 t->text[p-line] = *line;
1245 if (type
!= TOK_COMMENT
) {
1246 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1252 nasm_dump_token(list
);
1258 * this function allocates a new managed block of memory and
1259 * returns a pointer to the block. The managed blocks are
1260 * deleted only all at once by the delete_Blocks function.
1262 static void *new_Block(size_t size
)
1264 Blocks
*b
= &blocks
;
1266 /* first, get to the end of the linked list */
1270 /* now allocate the requested chunk */
1271 b
->chunk
= nasm_malloc(size
);
1273 /* now allocate a new block for the next request */
1274 b
->next
= nasm_zalloc(sizeof(Blocks
));
1280 * this function deletes all managed blocks of memory
1282 static void delete_Blocks(void)
1284 Blocks
*a
, *b
= &blocks
;
1287 * keep in mind that the first block, pointed to by blocks
1288 * is a static and not dynamically allocated, so we don't
1292 nasm_free(b
->chunk
);
1301 * this function creates a new Token and passes a pointer to it
1302 * back to the caller. It sets the type and text elements, and
1303 * also the a.mac and next elements to NULL.
1305 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1306 const char *text
, int txtlen
)
1312 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1313 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1314 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1315 freeTokens
[i
].next
= NULL
;
1318 freeTokens
= t
->next
;
1322 if (type
== TOK_WHITESPACE
|| !text
) {
1326 txtlen
= strlen(text
);
1327 t
->text
= nasm_malloc(txtlen
+1);
1328 memcpy(t
->text
, text
, txtlen
);
1329 t
->text
[txtlen
] = '\0';
1334 static Token
*copy_Token(Token
* tline
)
1336 Token
*t
, *tt
, *first
= NULL
, *prev
= NULL
;
1338 for (tt
= tline
; tt
!= NULL
; tt
= tt
->next
) {
1340 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1341 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1342 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1343 freeTokens
[i
].next
= NULL
;
1346 freeTokens
= t
->next
;
1348 t
->text
= tt
->text
? nasm_strdup(tt
->text
) : NULL
;
1349 t
->a
.mac
= tt
->a
.mac
;
1350 t
->a
.len
= tt
->a
.len
;
1362 static Token
*delete_Token(Token
* t
)
1364 Token
*next
= t
->next
;
1366 t
->next
= freeTokens
;
1372 * Convert a line of tokens back into text.
1373 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1374 * will be transformed into ..@ctxnum.xxx
1376 static char *detoken(Token
* tlist
, bool expand_locals
)
1383 list_for_each(t
, tlist
) {
1384 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1389 if (*v
== '\'' || *v
== '\"' || *v
== '`') {
1390 size_t len
= nasm_unquote(v
, NULL
);
1391 size_t clen
= strlen(v
);
1394 error(ERR_NONFATAL
| ERR_PASS1
,
1395 "NUL character in %! string");
1401 char *p
= getenv(v
);
1403 error(ERR_NONFATAL
| ERR_PASS1
,
1404 "nonexistent environment variable `%s'", v
);
1407 t
->text
= nasm_strdup(p
);
1412 /* Expand local macros here and not during preprocessing */
1413 if (expand_locals
&&
1414 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1415 t
->text
[0] == '%' && t
->text
[1] == '$') {
1418 Context
*ctx
= get_ctx(t
->text
, &q
);
1421 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1422 p
= nasm_strcat(buffer
, q
);
1428 /* Expand %? and %?? directives */
1429 if ((istk
->expansion
!= NULL
) &&
1430 ((t
->type
== TOK_PREPROC_Q
) ||
1431 (t
->type
== TOK_PREPROC_QQ
))) {
1433 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
){
1434 if (ei
->type
== EXP_MMACRO
) {
1436 if (t
->type
== TOK_PREPROC_Q
) {
1437 t
->text
= nasm_strdup(ei
->name
);
1439 t
->text
= nasm_strdup(ei
->def
->name
);
1446 if (t
->type
== TOK_WHITESPACE
)
1449 len
+= strlen(t
->text
);
1452 p
= line
= nasm_malloc(len
+ 1);
1454 list_for_each(t
, tlist
) {
1455 if (t
->type
== TOK_WHITESPACE
) {
1457 } else if (t
->text
) {
1469 * Initialize a new Line
1471 static inline Line
*new_Line(void)
1473 return (Line
*)nasm_zalloc(sizeof(Line
));
1478 * Initialize a new Expansion Definition
1480 static ExpDef
*new_ExpDef(int exp_type
)
1482 ExpDef
*ed
= (ExpDef
*)nasm_zalloc(sizeof(ExpDef
));
1483 ed
->type
= exp_type
;
1484 ed
->casesense
= true;
1485 ed
->state
= COND_NEVER
;
1492 * Initialize a new Expansion Instance
1494 static ExpInv
*new_ExpInv(int exp_type
, ExpDef
*ed
)
1496 ExpInv
*ei
= (ExpInv
*)nasm_zalloc(sizeof(ExpInv
));
1497 ei
->type
= exp_type
;
1499 ei
->unique
= ++unique
;
1501 if ((istk
->mmac_depth
< 1) &&
1502 (istk
->expansion
== NULL
) &&
1504 (ed
->type
!= EXP_MMACRO
) &&
1505 (ed
->type
!= EXP_REP
) &&
1506 (ed
->type
!= EXP_WHILE
)) {
1507 ei
->linnum
= src_get_linnum();
1508 src_set_linnum(ei
->linnum
- ed
->linecount
- 1);
1512 if ((istk
->expansion
== NULL
) ||
1513 (ei
->type
== EXP_MMACRO
)) {
1516 ei
->relno
= istk
->expansion
->lineno
;
1518 ei
->relno
-= (ed
->linecount
+ 1);
1525 * A scanner, suitable for use by the expression evaluator, which
1526 * operates on a line of Tokens. Expects a pointer to a pointer to
1527 * the first token in the line to be passed in as its private_data
1530 * FIX: This really needs to be unified with stdscan.
1532 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1534 Token
**tlineptr
= private_data
;
1536 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1540 *tlineptr
= tline
? tline
->next
: NULL
;
1541 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1542 tline
->type
== TOK_COMMENT
));
1545 return tokval
->t_type
= TOKEN_EOS
;
1547 tokval
->t_charptr
= tline
->text
;
1549 if (tline
->text
[0] == '$' && !tline
->text
[1])
1550 return tokval
->t_type
= TOKEN_HERE
;
1551 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1552 return tokval
->t_type
= TOKEN_BASE
;
1554 if (tline
->type
== TOK_ID
) {
1555 p
= tokval
->t_charptr
= tline
->text
;
1557 tokval
->t_charptr
++;
1558 return tokval
->t_type
= TOKEN_ID
;
1561 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1562 if (r
>= p
+MAX_KEYWORD
)
1563 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1564 *s
++ = nasm_tolower(*r
);
1567 /* right, so we have an identifier sitting in temp storage. now,
1568 * is it actually a register or instruction name, or what? */
1569 return nasm_token_hash(ourcopy
, tokval
);
1572 if (tline
->type
== TOK_NUMBER
) {
1574 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1575 tokval
->t_charptr
= tline
->text
;
1577 return tokval
->t_type
= TOKEN_ERRNUM
;
1579 return tokval
->t_type
= TOKEN_NUM
;
1582 if (tline
->type
== TOK_FLOAT
) {
1583 return tokval
->t_type
= TOKEN_FLOAT
;
1586 if (tline
->type
== TOK_STRING
) {
1589 bq
= tline
->text
[0];
1590 tokval
->t_charptr
= tline
->text
;
1591 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1593 if (ep
[0] != bq
|| ep
[1] != '\0')
1594 return tokval
->t_type
= TOKEN_ERRSTR
;
1596 return tokval
->t_type
= TOKEN_STR
;
1599 if (tline
->type
== TOK_OTHER
) {
1600 if (!strcmp(tline
->text
, "<<"))
1601 return tokval
->t_type
= TOKEN_SHL
;
1602 if (!strcmp(tline
->text
, ">>"))
1603 return tokval
->t_type
= TOKEN_SHR
;
1604 if (!strcmp(tline
->text
, "//"))
1605 return tokval
->t_type
= TOKEN_SDIV
;
1606 if (!strcmp(tline
->text
, "%%"))
1607 return tokval
->t_type
= TOKEN_SMOD
;
1608 if (!strcmp(tline
->text
, "=="))
1609 return tokval
->t_type
= TOKEN_EQ
;
1610 if (!strcmp(tline
->text
, "<>"))
1611 return tokval
->t_type
= TOKEN_NE
;
1612 if (!strcmp(tline
->text
, "!="))
1613 return tokval
->t_type
= TOKEN_NE
;
1614 if (!strcmp(tline
->text
, "<="))
1615 return tokval
->t_type
= TOKEN_LE
;
1616 if (!strcmp(tline
->text
, ">="))
1617 return tokval
->t_type
= TOKEN_GE
;
1618 if (!strcmp(tline
->text
, "&&"))
1619 return tokval
->t_type
= TOKEN_DBL_AND
;
1620 if (!strcmp(tline
->text
, "^^"))
1621 return tokval
->t_type
= TOKEN_DBL_XOR
;
1622 if (!strcmp(tline
->text
, "||"))
1623 return tokval
->t_type
= TOKEN_DBL_OR
;
1627 * We have no other options: just return the first character of
1630 return tokval
->t_type
= tline
->text
[0];
1634 * Compare a string to the name of an existing macro; this is a
1635 * simple wrapper which calls either strcmp or nasm_stricmp
1636 * depending on the value of the `casesense' parameter.
1638 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1640 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1644 * Compare a string to the name of an existing macro; this is a
1645 * simple wrapper which calls either strcmp or nasm_stricmp
1646 * depending on the value of the `casesense' parameter.
1648 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1650 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1654 * Return the Context structure associated with a %$ token. Return
1655 * NULL, having _already_ reported an error condition, if the
1656 * context stack isn't deep enough for the supplied number of $
1659 * If "namep" is non-NULL, set it to the pointer to the macro name
1660 * tail, i.e. the part beyond %$...
1662 static Context
*get_ctx(const char *name
, const char **namep
)
1670 if (!name
|| name
[0] != '%' || name
[1] != '$')
1674 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1681 while (ctx
&& *name
== '$') {
1688 error(ERR_NONFATAL
, "`%s': context stack is only"
1689 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1700 * Check to see if a file is already in a string list
1702 static bool in_list(const StrList
*list
, const char *str
)
1705 if (!strcmp(list
->str
, str
))
1713 * Open an include file. This routine must always return a valid
1714 * file pointer if it returns - it's responsible for throwing an
1715 * ERR_FATAL and bombing out completely if not. It should also try
1716 * the include path one by one until it finds the file or reaches
1717 * the end of the path.
1719 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1724 IncPath
*ip
= ipath
;
1725 int len
= strlen(file
);
1726 size_t prefix_len
= 0;
1730 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1732 memcpy(sl
->str
, prefix
, prefix_len
);
1733 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1734 fp
= fopen(sl
->str
, "r");
1735 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1752 prefix_len
= strlen(prefix
);
1754 /* -MG given and file not found */
1755 if (dhead
&& !in_list(*dhead
, file
)) {
1756 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1758 strcpy(sl
->str
, file
);
1766 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1771 * Determine if we should warn on defining a single-line macro of
1772 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1773 * return true if _any_ single-line macro of that name is defined.
1774 * Otherwise, will return true if a single-line macro with either
1775 * `nparam' or no parameters is defined.
1777 * If a macro with precisely the right number of parameters is
1778 * defined, or nparam is -1, the address of the definition structure
1779 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1780 * is NULL, no action will be taken regarding its contents, and no
1783 * Note that this is also called with nparam zero to resolve
1786 * If you already know which context macro belongs to, you can pass
1787 * the context pointer as first parameter; if you won't but name begins
1788 * with %$ the context will be automatically computed. If all_contexts
1789 * is true, macro will be searched in outer contexts as well.
1792 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1795 struct hash_table
*smtbl
;
1799 smtbl
= &ctx
->localmac
;
1800 } else if (name
[0] == '%' && name
[1] == '$') {
1802 ctx
= get_ctx(name
, &name
);
1804 return false; /* got to return _something_ */
1805 smtbl
= &ctx
->localmac
;
1809 m
= (SMacro
*) hash_findix(smtbl
, name
);
1812 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1813 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1815 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1829 * Count and mark off the parameters in a multi-line macro call.
1830 * This is called both from within the multi-line macro expansion
1831 * code, and also to mark off the default parameters when provided
1832 * in a %macro definition line.
1834 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1836 int paramsize
, brace
;
1838 *nparam
= paramsize
= 0;
1841 /* +1: we need space for the final NULL */
1842 if (*nparam
+1 >= paramsize
) {
1843 paramsize
+= PARAM_DELTA
;
1844 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1848 if (tok_is_(t
, "{"))
1850 (*params
)[(*nparam
)++] = t
;
1851 while (tok_isnt_(t
, brace
? "}" : ","))
1853 if (t
) { /* got a comma/brace */
1857 * Now we've found the closing brace, look further
1861 if (tok_isnt_(t
, ",")) {
1863 "braces do not enclose all of macro parameter");
1864 while (tok_isnt_(t
, ","))
1868 t
= t
->next
; /* eat the comma */
1875 * Determine whether one of the various `if' conditions is true or
1878 * We must free the tline we get passed.
1880 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1882 enum pp_conditional i
= PP_COND(ct
);
1884 Token
*t
, *tt
, **tptr
, *origline
;
1885 struct tokenval tokval
;
1887 enum pp_token_type needtype
;
1894 j
= false; /* have we matched yet? */
1899 if (tline
->type
!= TOK_ID
) {
1901 "`%s' expects context identifiers", pp_directives
[ct
]);
1902 free_tlist(origline
);
1905 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1907 tline
= tline
->next
;
1912 j
= false; /* have we matched yet? */
1915 if (!tline
|| (tline
->type
!= TOK_ID
&&
1916 (tline
->type
!= TOK_PREPROC_ID
||
1917 tline
->text
[1] != '$'))) {
1919 "`%s' expects macro identifiers", pp_directives
[ct
]);
1922 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1924 tline
= tline
->next
;
1929 tline
= expand_smacro(tline
);
1930 j
= false; /* have we matched yet? */
1933 if (!tline
|| (tline
->type
!= TOK_ID
&&
1934 tline
->type
!= TOK_STRING
&&
1935 (tline
->type
!= TOK_PREPROC_ID
||
1936 tline
->text
[1] != '!'))) {
1938 "`%s' expects environment variable names",
1943 if (tline
->type
== TOK_PREPROC_ID
)
1944 p
+= 2; /* Skip leading %! */
1945 if (*p
== '\'' || *p
== '\"' || *p
== '`')
1946 nasm_unquote_cstr(p
, ct
);
1949 tline
= tline
->next
;
1955 tline
= expand_smacro(tline
);
1957 while (tok_isnt_(tt
, ","))
1961 "`%s' expects two comma-separated arguments",
1966 j
= true; /* assume equality unless proved not */
1967 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1968 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1969 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1973 if (t
->type
== TOK_WHITESPACE
) {
1977 if (tt
->type
== TOK_WHITESPACE
) {
1981 if (tt
->type
!= t
->type
) {
1982 j
= false; /* found mismatching tokens */
1985 /* When comparing strings, need to unquote them first */
1986 if (t
->type
== TOK_STRING
) {
1987 size_t l1
= nasm_unquote(t
->text
, NULL
);
1988 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1994 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1998 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1999 j
= false; /* found mismatching tokens */
2006 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
2007 j
= false; /* trailing gunk on one end or other */
2013 ExpDef searching
, *ed
;
2016 tline
= expand_id(tline
);
2017 if (!tok_type_(tline
, TOK_ID
)) {
2019 "`%s' expects a macro name", pp_directives
[ct
]);
2022 memset(&searching
, 0, sizeof(searching
));
2023 searching
.name
= nasm_strdup(tline
->text
);
2024 searching
.casesense
= true;
2025 searching
.nparam_max
= INT_MAX
;
2026 tline
= expand_smacro(tline
->next
);
2029 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2031 "`%s' expects a parameter count or nothing",
2034 searching
.nparam_min
= searching
.nparam_max
=
2035 readnum(tline
->text
, &j
);
2038 "unable to parse parameter count `%s'",
2041 if (tline
&& tok_is_(tline
->next
, "-")) {
2042 tline
= tline
->next
->next
;
2043 if (tok_is_(tline
, "*"))
2044 searching
.nparam_max
= INT_MAX
;
2045 else if (!tok_type_(tline
, TOK_NUMBER
))
2047 "`%s' expects a parameter count after `-'",
2050 searching
.nparam_max
= readnum(tline
->text
, &j
);
2053 "unable to parse parameter count `%s'",
2055 if (searching
.nparam_min
> searching
.nparam_max
)
2057 "minimum parameter count exceeds maximum");
2060 if (tline
&& tok_is_(tline
->next
, "+")) {
2061 tline
= tline
->next
;
2062 searching
.plus
= true;
2064 ed
= (ExpDef
*) hash_findix(&expdefs
, searching
.name
);
2065 while (ed
!= NULL
) {
2066 if (!strcmp(ed
->name
, searching
.name
) &&
2067 (ed
->nparam_min
<= searching
.nparam_max
|| searching
.plus
) &&
2068 (searching
.nparam_min
<= ed
->nparam_max
|| ed
->plus
)) {
2074 if (tline
&& tline
->next
)
2075 error(ERR_WARNING
|ERR_PASS1
,
2076 "trailing garbage after %%ifmacro ignored");
2077 nasm_free(searching
.name
);
2086 needtype
= TOK_NUMBER
;
2089 needtype
= TOK_STRING
;
2093 t
= tline
= expand_smacro(tline
);
2095 while (tok_type_(t
, TOK_WHITESPACE
) ||
2096 (needtype
== TOK_NUMBER
&&
2097 tok_type_(t
, TOK_OTHER
) &&
2098 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
2102 j
= tok_type_(t
, needtype
);
2106 t
= tline
= expand_smacro(tline
);
2107 while (tok_type_(t
, TOK_WHITESPACE
))
2112 t
= t
->next
; /* Skip the actual token */
2113 while (tok_type_(t
, TOK_WHITESPACE
))
2115 j
= !t
; /* Should be nothing left */
2120 t
= tline
= expand_smacro(tline
);
2121 while (tok_type_(t
, TOK_WHITESPACE
))
2124 j
= !t
; /* Should be empty */
2128 t
= tline
= expand_smacro(tline
);
2130 tokval
.t_type
= TOKEN_INVALID
;
2131 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
2132 NULL
, pass
| CRITICAL
, error
, NULL
);
2136 error(ERR_WARNING
|ERR_PASS1
,
2137 "trailing garbage after expression ignored");
2138 if (!is_simple(evalresult
)) {
2140 "non-constant value given to `%s'", pp_directives
[ct
]);
2143 j
= reloc_value(evalresult
) != 0;
2148 "preprocessor directive `%s' not yet implemented",
2153 free_tlist(origline
);
2154 return j
^ PP_NEGATIVE(ct
);
2157 free_tlist(origline
);
2162 * Common code for defining an smacro
2164 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
2165 int nparam
, Token
*expansion
)
2167 SMacro
*smac
, **smhead
;
2168 struct hash_table
*smtbl
;
2170 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
2172 error(ERR_WARNING
|ERR_PASS1
,
2173 "single-line macro `%s' defined both with and"
2174 " without parameters", mname
);
2176 * Some instances of the old code considered this a failure,
2177 * some others didn't. What is the right thing to do here?
2179 free_tlist(expansion
);
2180 return false; /* Failure */
2183 * We're redefining, so we have to take over an
2184 * existing SMacro structure. This means freeing
2185 * what was already in it.
2187 nasm_free(smac
->name
);
2188 free_tlist(smac
->expansion
);
2191 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2192 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
2193 smac
= nasm_zalloc(sizeof(SMacro
));
2194 smac
->next
= *smhead
;
2197 smac
->name
= nasm_strdup(mname
);
2198 smac
->casesense
= casesense
;
2199 smac
->nparam
= nparam
;
2200 smac
->expansion
= expansion
;
2201 smac
->in_progress
= false;
2202 return true; /* Success */
2206 * Undefine an smacro
2208 static void undef_smacro(Context
*ctx
, const char *mname
)
2210 SMacro
**smhead
, *s
, **sp
;
2211 struct hash_table
*smtbl
;
2213 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
2214 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
2218 * We now have a macro name... go hunt for it.
2221 while ((s
= *sp
) != NULL
) {
2222 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
2225 free_tlist(s
->expansion
);
2235 * Parse a mmacro specification.
2237 static bool parse_mmacro_spec(Token
*tline
, ExpDef
*def
, const char *directive
)
2241 tline
= tline
->next
;
2243 tline
= expand_id(tline
);
2244 if (!tok_type_(tline
, TOK_ID
)) {
2245 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
2249 def
->name
= nasm_strdup(tline
->text
);
2251 def
->nolist
= false;
2252 // def->in_progress = 0;
2253 // def->rep_nest = NULL;
2254 def
->nparam_min
= 0;
2255 def
->nparam_max
= 0;
2257 tline
= expand_smacro(tline
->next
);
2259 if (!tok_type_(tline
, TOK_NUMBER
)) {
2260 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
2262 def
->nparam_min
= def
->nparam_max
=
2263 readnum(tline
->text
, &err
);
2266 "unable to parse parameter count `%s'", tline
->text
);
2268 if (tline
&& tok_is_(tline
->next
, "-")) {
2269 tline
= tline
->next
->next
;
2270 if (tok_is_(tline
, "*")) {
2271 def
->nparam_max
= INT_MAX
;
2272 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
2274 "`%s' expects a parameter count after `-'", directive
);
2276 def
->nparam_max
= readnum(tline
->text
, &err
);
2278 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
2281 if (def
->nparam_min
> def
->nparam_max
) {
2282 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
2286 if (tline
&& tok_is_(tline
->next
, "+")) {
2287 tline
= tline
->next
;
2290 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2291 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2292 tline
= tline
->next
;
2297 * Handle default parameters.
2299 if (tline
&& tline
->next
) {
2300 def
->dlist
= tline
->next
;
2302 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2305 def
->defaults
= NULL
;
2309 if (def
->defaults
&& def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2311 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2312 "too many default macro parameters");
2319 * Decode a size directive
2321 static int parse_size(const char *str
) {
2322 static const char *size_names
[] =
2323 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2324 static const int sizes
[] =
2325 { 0, 1, 4, 16, 8, 10, 2, 32 };
2327 return sizes
[bsii(str
, size_names
, ARRAY_SIZE(size_names
))+1];
2331 * find and process preprocessor directive in passed line
2332 * Find out if a line contains a preprocessor directive, and deal
2335 * If a directive _is_ found, it is the responsibility of this routine
2336 * (and not the caller) to free_tlist() the line.
2338 * @param tline a pointer to the current tokeninzed line linked list
2339 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2342 static int do_directive(Token
* tline
)
2344 enum preproc_token i
;
2357 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2358 struct tokenval tokval
;
2360 ExpDef
*ed
, *eed
, **edhead
;
2369 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2370 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2371 || tline
->text
[1] == '!'))
2372 return NO_DIRECTIVE_FOUND
;
2374 i
= pp_token_hash(tline
->text
);
2378 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2379 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2381 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2384 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2385 /* Directive to tell NASM what the default stack size is. The
2386 * default is for a 16-bit stack, and this can be overriden with
2389 tline
= tline
->next
;
2390 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2391 tline
= tline
->next
;
2392 if (!tline
|| tline
->type
!= TOK_ID
) {
2393 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2394 free_tlist(origline
);
2395 return DIRECTIVE_FOUND
;
2397 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2398 /* All subsequent ARG directives are for a 32-bit stack */
2400 StackPointer
= "ebp";
2403 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2404 /* All subsequent ARG directives are for a 64-bit stack */
2406 StackPointer
= "rbp";
2409 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2410 /* All subsequent ARG directives are for a 16-bit stack,
2411 * far function call.
2414 StackPointer
= "bp";
2417 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2418 /* All subsequent ARG directives are for a 16-bit stack,
2419 * far function call. We don't support near functions.
2422 StackPointer
= "bp";
2426 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2427 free_tlist(origline
);
2428 return DIRECTIVE_FOUND
;
2430 free_tlist(origline
);
2431 return DIRECTIVE_FOUND
;
2434 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2435 /* TASM like ARG directive to define arguments to functions, in
2436 * the following form:
2438 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2442 char *arg
, directive
[256];
2443 int size
= StackSize
;
2445 /* Find the argument name */
2446 tline
= tline
->next
;
2447 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2448 tline
= tline
->next
;
2449 if (!tline
|| tline
->type
!= TOK_ID
) {
2450 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2451 free_tlist(origline
);
2452 return DIRECTIVE_FOUND
;
2456 /* Find the argument size type */
2457 tline
= tline
->next
;
2458 if (!tline
|| tline
->type
!= TOK_OTHER
2459 || tline
->text
[0] != ':') {
2461 "Syntax error processing `%%arg' directive");
2462 free_tlist(origline
);
2463 return DIRECTIVE_FOUND
;
2465 tline
= tline
->next
;
2466 if (!tline
|| tline
->type
!= TOK_ID
) {
2467 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2468 free_tlist(origline
);
2469 return DIRECTIVE_FOUND
;
2472 /* Allow macro expansion of type parameter */
2473 tt
= tokenize(tline
->text
);
2474 tt
= expand_smacro(tt
);
2475 size
= parse_size(tt
->text
);
2478 "Invalid size type for `%%arg' missing directive");
2480 free_tlist(origline
);
2481 return DIRECTIVE_FOUND
;
2485 /* Round up to even stack slots */
2486 size
= ALIGN(size
, StackSize
);
2488 /* Now define the macro for the argument */
2489 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2490 arg
, StackPointer
, offset
);
2491 do_directive(tokenize(directive
));
2494 /* Move to the next argument in the list */
2495 tline
= tline
->next
;
2496 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2497 tline
= tline
->next
;
2498 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2500 free_tlist(origline
);
2501 return DIRECTIVE_FOUND
;
2504 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2505 /* TASM like LOCAL directive to define local variables for a
2506 * function, in the following form:
2508 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2510 * The '= LocalSize' at the end is ignored by NASM, but is
2511 * required by TASM to define the local parameter size (and used
2512 * by the TASM macro package).
2514 offset
= LocalOffset
;
2516 char *local
, directive
[256];
2517 int size
= StackSize
;
2519 /* Find the argument name */
2520 tline
= tline
->next
;
2521 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2522 tline
= tline
->next
;
2523 if (!tline
|| tline
->type
!= TOK_ID
) {
2525 "`%%local' missing argument parameter");
2526 free_tlist(origline
);
2527 return DIRECTIVE_FOUND
;
2529 local
= tline
->text
;
2531 /* Find the argument size type */
2532 tline
= tline
->next
;
2533 if (!tline
|| tline
->type
!= TOK_OTHER
2534 || tline
->text
[0] != ':') {
2536 "Syntax error processing `%%local' directive");
2537 free_tlist(origline
);
2538 return DIRECTIVE_FOUND
;
2540 tline
= tline
->next
;
2541 if (!tline
|| tline
->type
!= TOK_ID
) {
2543 "`%%local' missing size type parameter");
2544 free_tlist(origline
);
2545 return DIRECTIVE_FOUND
;
2548 /* Allow macro expansion of type parameter */
2549 tt
= tokenize(tline
->text
);
2550 tt
= expand_smacro(tt
);
2551 size
= parse_size(tt
->text
);
2554 "Invalid size type for `%%local' missing directive");
2556 free_tlist(origline
);
2557 return DIRECTIVE_FOUND
;
2561 /* Round up to even stack slots */
2562 size
= ALIGN(size
, StackSize
);
2564 offset
+= size
; /* Negative offset, increment before */
2566 /* Now define the macro for the argument */
2567 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2568 local
, StackPointer
, offset
);
2569 do_directive(tokenize(directive
));
2571 /* Now define the assign to setup the enter_c macro correctly */
2572 snprintf(directive
, sizeof(directive
),
2573 "%%assign %%$localsize %%$localsize+%d", size
);
2574 do_directive(tokenize(directive
));
2576 /* Move to the next argument in the list */
2577 tline
= tline
->next
;
2578 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2579 tline
= tline
->next
;
2580 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2581 LocalOffset
= offset
;
2582 free_tlist(origline
);
2583 return DIRECTIVE_FOUND
;
2586 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2588 error(ERR_WARNING
|ERR_PASS1
,
2589 "trailing garbage after `%%clear' ignored");
2592 free_tlist(origline
);
2593 return DIRECTIVE_FOUND
;
2596 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2597 t
= tline
->next
= expand_smacro(tline
->next
);
2599 if (!t
|| (t
->type
!= TOK_STRING
&&
2600 t
->type
!= TOK_INTERNAL_STRING
)) {
2601 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2602 free_tlist(origline
);
2603 return DIRECTIVE_FOUND
; /* but we did _something_ */
2606 error(ERR_WARNING
|ERR_PASS1
,
2607 "trailing garbage after `%%depend' ignored");
2609 if (t
->type
!= TOK_INTERNAL_STRING
)
2610 nasm_unquote_cstr(p
, i
);
2611 if (dephead
&& !in_list(*dephead
, p
)) {
2612 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2616 deptail
= &sl
->next
;
2618 free_tlist(origline
);
2619 return DIRECTIVE_FOUND
;
2622 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2623 t
= tline
->next
= expand_smacro(tline
->next
);
2626 if (!t
|| (t
->type
!= TOK_STRING
&&
2627 t
->type
!= TOK_INTERNAL_STRING
)) {
2628 error(ERR_NONFATAL
, "`%%include' expects a file name");
2629 free_tlist(origline
);
2630 return DIRECTIVE_FOUND
; /* but we did _something_ */
2633 error(ERR_WARNING
|ERR_PASS1
,
2634 "trailing garbage after `%%include' ignored");
2636 if (t
->type
!= TOK_INTERNAL_STRING
)
2637 nasm_unquote_cstr(p
, i
);
2638 inc
= nasm_zalloc(sizeof(Include
));
2640 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2642 /* -MG given but file not found */
2645 inc
->fname
= src_set_fname(nasm_strdup(p
));
2646 inc
->lineno
= src_set_linnum(0);
2648 inc
->expansion
= NULL
;
2650 list
->uplevel(LIST_INCLUDE
);
2652 free_tlist(origline
);
2653 return DIRECTIVE_FOUND
;
2656 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2658 static macros_t
*use_pkg
;
2659 const char *pkg_macro
= NULL
;
2661 tline
= tline
->next
;
2663 tline
= expand_id(tline
);
2665 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2666 tline
->type
!= TOK_INTERNAL_STRING
&&
2667 tline
->type
!= TOK_ID
)) {
2668 error(ERR_NONFATAL
, "`%%use' expects a package name");
2669 free_tlist(origline
);
2670 return DIRECTIVE_FOUND
; /* but we did _something_ */
2673 error(ERR_WARNING
|ERR_PASS1
,
2674 "trailing garbage after `%%use' ignored");
2675 if (tline
->type
== TOK_STRING
)
2676 nasm_unquote_cstr(tline
->text
, i
);
2677 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2679 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2681 pkg_macro
= (char *)use_pkg
+ 1; /* The first string will be <%define>__USE_*__ */
2682 if (use_pkg
&& ! smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2683 /* Not already included, go ahead and include it */
2684 stdmacpos
= use_pkg
;
2686 free_tlist(origline
);
2687 return DIRECTIVE_FOUND
;
2692 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2693 tline
= tline
->next
;
2695 tline
= expand_id(tline
);
2697 if (!tok_type_(tline
, TOK_ID
)) {
2698 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2700 free_tlist(origline
);
2701 return DIRECTIVE_FOUND
; /* but we did _something_ */
2704 error(ERR_WARNING
|ERR_PASS1
,
2705 "trailing garbage after `%s' ignored",
2707 p
= nasm_strdup(tline
->text
);
2709 p
= NULL
; /* Anonymous */
2713 ctx
= nasm_zalloc(sizeof(Context
));
2715 hash_init(&ctx
->localmac
, HASH_SMALL
);
2717 ctx
->number
= unique
++;
2722 error(ERR_NONFATAL
, "`%s': context stack is empty",
2724 } else if (i
== PP_POP
) {
2725 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2726 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2728 cstk
->name
? cstk
->name
: "anonymous", p
);
2733 nasm_free(cstk
->name
);
2739 free_tlist(origline
);
2740 return DIRECTIVE_FOUND
;
2742 severity
= ERR_FATAL
;
2745 severity
= ERR_NONFATAL
;
2748 severity
= ERR_WARNING
|ERR_WARN_USER
;
2752 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2754 /* Only error out if this is the final pass */
2755 if (pass
!= 2 && i
!= PP_FATAL
)
2756 return DIRECTIVE_FOUND
;
2758 tline
->next
= expand_smacro(tline
->next
);
2759 tline
= tline
->next
;
2761 t
= tline
? tline
->next
: NULL
;
2763 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2764 /* The line contains only a quoted string */
2766 nasm_unquote(p
, NULL
); /* Ignore NUL character truncation */
2767 error(severity
, "%s", p
);
2769 /* Not a quoted string, or more than a quoted string */
2770 p
= detoken(tline
, false);
2771 error(severity
, "%s", p
);
2774 free_tlist(origline
);
2775 return DIRECTIVE_FOUND
;
2779 if (defining
!= NULL
) {
2780 if (defining
->type
== EXP_IF
) {
2781 defining
->def_depth
++;
2783 return NO_DIRECTIVE_FOUND
;
2785 if ((istk
->expansion
!= NULL
) &&
2786 (istk
->expansion
->emitting
== false)) {
2789 j
= if_condition(tline
->next
, i
);
2790 tline
->next
= NULL
; /* it got freed */
2791 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
2793 ed
= new_ExpDef(EXP_IF
);
2795 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
2796 ed
->prev
= defining
;
2798 free_tlist(origline
);
2799 return DIRECTIVE_FOUND
;
2802 if (defining
!= NULL
) {
2803 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2804 return NO_DIRECTIVE_FOUND
;
2807 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2808 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2810 switch (defining
->state
) {
2812 defining
->state
= COND_DONE
;
2813 defining
->ignoring
= true;
2818 defining
->ignoring
= true;
2821 case COND_ELSE_TRUE
:
2822 case COND_ELSE_FALSE
:
2823 error_precond(ERR_WARNING
|ERR_PASS1
,
2824 "`%%elif' after `%%else' ignored");
2825 defining
->state
= COND_NEVER
;
2826 defining
->ignoring
= true;
2831 * IMPORTANT: In the case of %if, we will already have
2832 * called expand_mmac_params(); however, if we're
2833 * processing an %elif we must have been in a
2834 * non-emitting mode, which would have inhibited
2835 * the normal invocation of expand_mmac_params().
2836 * Therefore, we have to do it explicitly here.
2838 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2839 tline
->next
= NULL
; /* it got freed */
2841 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2842 defining
->ignoring
= ((defining
->state
== COND_IF_TRUE
) ? false : true);
2845 free_tlist(origline
);
2846 return DIRECTIVE_FOUND
;
2849 if (defining
!= NULL
) {
2850 if ((defining
->type
!= EXP_IF
) || (defining
->def_depth
> 0)) {
2851 return NO_DIRECTIVE_FOUND
;
2855 error_precond(ERR_WARNING
|ERR_PASS1
,
2856 "trailing garbage after `%%else' ignored");
2857 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2858 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2860 switch (defining
->state
) {
2863 defining
->state
= COND_ELSE_FALSE
;
2864 defining
->ignoring
= true;
2868 defining
->ignoring
= true;
2872 defining
->state
= COND_ELSE_TRUE
;
2873 defining
->ignoring
= false;
2876 case COND_ELSE_TRUE
:
2877 case COND_ELSE_FALSE
:
2878 error_precond(ERR_WARNING
|ERR_PASS1
,
2879 "`%%else' after `%%else' ignored.");
2880 defining
->state
= COND_NEVER
;
2881 defining
->ignoring
= true;
2884 free_tlist(origline
);
2885 return DIRECTIVE_FOUND
;
2888 if (defining
!= NULL
) {
2889 if (defining
->type
== EXP_IF
) {
2890 if (defining
->def_depth
> 0) {
2891 defining
->def_depth
--;
2892 return NO_DIRECTIVE_FOUND
;
2895 return NO_DIRECTIVE_FOUND
;
2899 error_precond(ERR_WARNING
|ERR_PASS1
,
2900 "trailing garbage after `%%endif' ignored");
2901 if ((defining
== NULL
) || (defining
->type
!= EXP_IF
)) {
2902 error(ERR_NONFATAL
, "`%%endif': no matching `%%if'");
2903 return DIRECTIVE_FOUND
;
2906 defining
= ed
->prev
;
2907 ed
->prev
= expansions
;
2909 ei
= new_ExpInv(EXP_IF
, ed
);
2910 ei
->current
= ed
->line
;
2911 ei
->emitting
= true;
2912 ei
->prev
= istk
->expansion
;
2913 istk
->expansion
= ei
;
2914 free_tlist(origline
);
2915 return DIRECTIVE_FOUND
;
2921 if (defining
!= NULL
) {
2922 if (defining
->type
== EXP_MMACRO
) {
2923 defining
->def_depth
++;
2925 return NO_DIRECTIVE_FOUND
;
2927 ed
= new_ExpDef(EXP_MMACRO
);
2929 (i
== PP_RMACRO
) || (i
== PP_IRMACRO
) ? DEADMAN_LIMIT
: 0;
2930 ed
->casesense
= (i
== PP_MACRO
) || (i
== PP_RMACRO
);
2931 if (!parse_mmacro_spec(tline
, ed
, pp_directives
[i
])) {
2934 return DIRECTIVE_FOUND
;
2938 ed
->max_depth
= (ed
->max_depth
+ 1);
2939 ed
->ignoring
= false;
2940 ed
->prev
= defining
;
2943 eed
= (ExpDef
*) hash_findix(&expdefs
, ed
->name
);
2945 if (!strcmp(eed
->name
, ed
->name
) &&
2946 (eed
->nparam_min
<= ed
->nparam_max
|| ed
->plus
) &&
2947 (ed
->nparam_min
<= eed
->nparam_max
|| eed
->plus
)) {
2948 error(ERR_WARNING
|ERR_PASS1
,
2949 "redefining multi-line macro `%s'", ed
->name
);
2950 return DIRECTIVE_FOUND
;
2954 free_tlist(origline
);
2955 return DIRECTIVE_FOUND
;
2959 if (defining
!= NULL
) {
2960 if (defining
->type
== EXP_MMACRO
) {
2961 if (defining
->def_depth
> 0) {
2962 defining
->def_depth
--;
2963 return NO_DIRECTIVE_FOUND
;
2966 return NO_DIRECTIVE_FOUND
;
2969 if (!(defining
) || (defining
->type
!= EXP_MMACRO
)) {
2970 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2971 return DIRECTIVE_FOUND
;
2973 edhead
= (ExpDef
**) hash_findi_add(&expdefs
, defining
->name
);
2974 defining
->next
= *edhead
;
2977 defining
= ed
->prev
;
2978 ed
->prev
= expansions
;
2981 free_tlist(origline
);
2982 return DIRECTIVE_FOUND
;
2985 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
2987 * We must search along istk->expansion until we hit a
2988 * macro invocation. Then we disable the emitting state(s)
2989 * between exitmacro and endmacro.
2991 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
2992 if(ei
->type
== EXP_MMACRO
) {
2999 * Set all invocations leading back to the macro
3000 * invocation to a non-emitting state.
3002 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3003 eei
->emitting
= false;
3005 eei
->emitting
= false;
3007 error(ERR_NONFATAL
, "`%%exitmacro' not within `%%macro' block");
3009 free_tlist(origline
);
3010 return DIRECTIVE_FOUND
;
3014 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3019 spec
.casesense
= (i
== PP_UNMACRO
);
3020 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
3021 return DIRECTIVE_FOUND
;
3023 ed_p
= (ExpDef
**) hash_findi(&expdefs
, spec
.name
, NULL
);
3024 while (ed_p
&& *ed_p
) {
3026 if (ed
->casesense
== spec
.casesense
&&
3027 !mstrcmp(ed
->name
, spec
.name
, spec
.casesense
) &&
3028 ed
->nparam_min
== spec
.nparam_min
&&
3029 ed
->nparam_max
== spec
.nparam_max
&&
3030 ed
->plus
== spec
.plus
) {
3031 if (ed
->cur_depth
> 0) {
3032 error(ERR_NONFATAL
, "`%s' ignored on active macro",
3043 free_tlist(origline
);
3044 free_tlist(spec
.dlist
);
3045 return DIRECTIVE_FOUND
;
3049 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3050 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
3051 tline
= tline
->next
;
3053 free_tlist(origline
);
3054 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
3055 return DIRECTIVE_FOUND
;
3057 t
= expand_smacro(tline
->next
);
3059 free_tlist(origline
);
3062 tokval
.t_type
= TOKEN_INVALID
;
3064 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3067 return DIRECTIVE_FOUND
;
3069 error(ERR_WARNING
|ERR_PASS1
,
3070 "trailing garbage after expression ignored");
3071 if (!is_simple(evalresult
)) {
3072 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
3073 return DIRECTIVE_FOUND
;
3075 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3076 if (ei
->type
== EXP_MMACRO
) {
3081 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
3082 } else if (ei
->nparam
== 0) {
3084 "`%%rotate' invoked within macro without parameters");
3086 int rotate
= ei
->rotate
+ reloc_value(evalresult
);
3088 rotate
%= (int)ei
->nparam
;
3090 rotate
+= ei
->nparam
;
3091 ei
->rotate
= rotate
;
3093 return DIRECTIVE_FOUND
;
3096 if (defining
!= NULL
) {
3097 if (defining
->type
== EXP_REP
) {
3098 defining
->def_depth
++;
3100 return NO_DIRECTIVE_FOUND
;
3104 tline
= tline
->next
;
3105 } while (tok_type_(tline
, TOK_WHITESPACE
));
3107 if (tok_type_(tline
, TOK_ID
) &&
3108 nasm_stricmp(tline
->text
, ".nolist") == 0) {
3111 tline
= tline
->next
;
3112 } while (tok_type_(tline
, TOK_WHITESPACE
));
3116 t
= expand_smacro(tline
);
3118 tokval
.t_type
= TOKEN_INVALID
;
3120 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3122 free_tlist(origline
);
3123 return DIRECTIVE_FOUND
;
3126 error(ERR_WARNING
|ERR_PASS1
,
3127 "trailing garbage after expression ignored");
3128 if (!is_simple(evalresult
)) {
3129 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
3130 return DIRECTIVE_FOUND
;
3132 count
= reloc_value(evalresult
);
3133 if (count
>= REP_LIMIT
) {
3134 error(ERR_NONFATAL
, "`%%rep' value exceeds limit");
3139 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
3142 free_tlist(origline
);
3143 ed
= new_ExpDef(EXP_REP
);
3144 ed
->nolist
= nolist
;
3147 ed
->max_depth
= (count
- 1);
3148 ed
->ignoring
= false;
3149 ed
->prev
= defining
;
3151 return DIRECTIVE_FOUND
;
3154 if (defining
!= NULL
) {
3155 if (defining
->type
== EXP_REP
) {
3156 if (defining
->def_depth
> 0) {
3157 defining
->def_depth
--;
3158 return NO_DIRECTIVE_FOUND
;
3161 return NO_DIRECTIVE_FOUND
;
3164 if ((defining
== NULL
) || (defining
->type
!= EXP_REP
)) {
3165 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
3166 return DIRECTIVE_FOUND
;
3170 * Now we have a "macro" defined - although it has no name
3171 * and we won't be entering it in the hash tables - we must
3172 * push a macro-end marker for it on to istk->expansion.
3173 * After that, it will take care of propagating itself (a
3174 * macro-end marker line for a macro which is really a %rep
3175 * block will cause the macro to be re-expanded, complete
3176 * with another macro-end marker to ensure the process
3177 * continues) until the whole expansion is forcibly removed
3178 * from istk->expansion by a %exitrep.
3181 defining
= ed
->prev
;
3182 ed
->prev
= expansions
;
3184 ei
= new_ExpInv(EXP_REP
, ed
);
3185 ei
->current
= ed
->line
;
3186 ei
->emitting
= ((ed
->max_depth
> 0) ? true : false);
3187 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3188 ei
->prev
= istk
->expansion
;
3189 istk
->expansion
= ei
;
3190 free_tlist(origline
);
3191 return DIRECTIVE_FOUND
;
3194 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3196 * We must search along istk->expansion until we hit a
3197 * rep invocation. Then we disable the emitting state(s)
3198 * between exitrep and endrep.
3200 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3201 if (ei
->type
== EXP_REP
) {
3208 * Set all invocations leading back to the rep
3209 * invocation to a non-emitting state.
3211 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3212 eei
->emitting
= false;
3214 eei
->emitting
= false;
3215 eei
->current
= NULL
;
3216 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3218 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
3220 free_tlist(origline
);
3221 return DIRECTIVE_FOUND
;
3227 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3228 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
3230 tline
= tline
->next
;
3232 tline
= expand_id(tline
);
3233 if (!tline
|| (tline
->type
!= TOK_ID
&&
3234 (tline
->type
!= TOK_PREPROC_ID
||
3235 tline
->text
[1] != '$'))) {
3236 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3238 free_tlist(origline
);
3239 return DIRECTIVE_FOUND
;
3242 ctx
= get_ctx(tline
->text
, &mname
);
3244 param_start
= tline
= tline
->next
;
3247 /* Expand the macro definition now for %xdefine and %ixdefine */
3248 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
3249 tline
= expand_smacro(tline
);
3251 if (tok_is_(tline
, "(")) {
3253 * This macro has parameters.
3256 tline
= tline
->next
;
3260 error(ERR_NONFATAL
, "parameter identifier expected");
3261 free_tlist(origline
);
3262 return DIRECTIVE_FOUND
;
3264 if (tline
->type
!= TOK_ID
) {
3266 "`%s': parameter identifier expected",
3268 free_tlist(origline
);
3269 return DIRECTIVE_FOUND
;
3272 smacro_set_param_idx(tline
, nparam
);
3275 tline
= tline
->next
;
3277 if (tok_is_(tline
, ",")) {
3278 tline
= tline
->next
;
3280 if (!tok_is_(tline
, ")")) {
3282 "`)' expected to terminate macro template");
3283 free_tlist(origline
);
3284 return DIRECTIVE_FOUND
;
3290 tline
= tline
->next
;
3292 if (tok_type_(tline
, TOK_WHITESPACE
))
3293 last
= tline
, tline
= tline
->next
;
3298 if (t
->type
== TOK_ID
) {
3299 list_for_each(tt
, param_start
)
3300 if (is_smacro_param(tt
) &&
3301 !strcmp(tt
->text
, t
->text
))
3305 t
->next
= macro_start
;
3310 * Good. We now have a macro name, a parameter count, and a
3311 * token list (in reverse order) for an expansion. We ought
3312 * to be OK just to create an SMacro, store it, and let
3313 * free_tlist have the rest of the line (which we have
3314 * carefully re-terminated after chopping off the expansion
3317 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
3318 free_tlist(origline
);
3319 return DIRECTIVE_FOUND
;
3322 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3323 tline
= tline
->next
;
3325 tline
= expand_id(tline
);
3326 if (!tline
|| (tline
->type
!= TOK_ID
&&
3327 (tline
->type
!= TOK_PREPROC_ID
||
3328 tline
->text
[1] != '$'))) {
3329 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
3330 free_tlist(origline
);
3331 return DIRECTIVE_FOUND
;
3334 error(ERR_WARNING
|ERR_PASS1
,
3335 "trailing garbage after macro name ignored");
3338 /* Find the context that symbol belongs to */
3339 ctx
= get_ctx(tline
->text
, &mname
);
3340 undef_smacro(ctx
, mname
);
3341 free_tlist(origline
);
3342 return DIRECTIVE_FOUND
;
3346 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3347 casesense
= (i
== PP_DEFSTR
);
3349 tline
= tline
->next
;
3351 tline
= expand_id(tline
);
3352 if (!tline
|| (tline
->type
!= TOK_ID
&&
3353 (tline
->type
!= TOK_PREPROC_ID
||
3354 tline
->text
[1] != '$'))) {
3355 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
3357 free_tlist(origline
);
3358 return DIRECTIVE_FOUND
;
3361 ctx
= get_ctx(tline
->text
, &mname
);
3363 tline
= expand_smacro(tline
->next
);
3366 while (tok_type_(tline
, TOK_WHITESPACE
))
3367 tline
= delete_Token(tline
);
3369 p
= detoken(tline
, false);
3370 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3371 macro_start
->text
= nasm_quote(p
, strlen(p
));
3372 macro_start
->type
= TOK_STRING
;
3376 * We now have a macro name, an implicit parameter count of
3377 * zero, and a string token to use as an expansion. Create
3378 * and store an SMacro.
3380 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3381 free_tlist(origline
);
3382 return DIRECTIVE_FOUND
;
3386 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3387 casesense
= (i
== PP_DEFTOK
);
3389 tline
= tline
->next
;
3391 tline
= expand_id(tline
);
3392 if (!tline
|| (tline
->type
!= TOK_ID
&&
3393 (tline
->type
!= TOK_PREPROC_ID
||
3394 tline
->text
[1] != '$'))) {
3396 "`%s' expects a macro identifier as first parameter",
3398 free_tlist(origline
);
3399 return DIRECTIVE_FOUND
;
3401 ctx
= get_ctx(tline
->text
, &mname
);
3403 tline
= expand_smacro(tline
->next
);
3407 while (tok_type_(t
, TOK_WHITESPACE
))
3409 /* t should now point to the string */
3410 if (!tok_type_(t
, TOK_STRING
)) {
3412 "`%s` requires string as second parameter",
3415 free_tlist(origline
);
3416 return DIRECTIVE_FOUND
;
3420 * Convert the string to a token stream. Note that smacros
3421 * are stored with the token stream reversed, so we have to
3422 * reverse the output of tokenize().
3424 nasm_unquote_cstr(t
->text
, i
);
3425 macro_start
= reverse_tokens(tokenize(t
->text
));
3428 * We now have a macro name, an implicit parameter count of
3429 * zero, and a numeric token to use as an expansion. Create
3430 * and store an SMacro.
3432 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3434 free_tlist(origline
);
3435 return DIRECTIVE_FOUND
;
3438 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3441 StrList
*xsl
= NULL
;
3442 StrList
**xst
= &xsl
;
3446 tline
= tline
->next
;
3448 tline
= expand_id(tline
);
3449 if (!tline
|| (tline
->type
!= TOK_ID
&&
3450 (tline
->type
!= TOK_PREPROC_ID
||
3451 tline
->text
[1] != '$'))) {
3453 "`%%pathsearch' expects a macro identifier as first parameter");
3454 free_tlist(origline
);
3455 return DIRECTIVE_FOUND
;
3457 ctx
= get_ctx(tline
->text
, &mname
);
3459 tline
= expand_smacro(tline
->next
);
3463 while (tok_type_(t
, TOK_WHITESPACE
))
3466 if (!t
|| (t
->type
!= TOK_STRING
&&
3467 t
->type
!= TOK_INTERNAL_STRING
)) {
3468 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3470 free_tlist(origline
);
3471 return DIRECTIVE_FOUND
; /* but we did _something_ */
3474 error(ERR_WARNING
|ERR_PASS1
,
3475 "trailing garbage after `%%pathsearch' ignored");
3477 if (t
->type
!= TOK_INTERNAL_STRING
)
3478 nasm_unquote(p
, NULL
);
3480 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3483 fclose(fp
); /* Don't actually care about the file */
3485 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3486 macro_start
->text
= nasm_quote(p
, strlen(p
));
3487 macro_start
->type
= TOK_STRING
;
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a string token to use as an expansion. Create
3493 * and store an SMacro.
3495 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3497 free_tlist(origline
);
3498 return DIRECTIVE_FOUND
;
3502 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3505 tline
= tline
->next
;
3507 tline
= expand_id(tline
);
3508 if (!tline
|| (tline
->type
!= TOK_ID
&&
3509 (tline
->type
!= TOK_PREPROC_ID
||
3510 tline
->text
[1] != '$'))) {
3512 "`%%strlen' expects a macro identifier as first parameter");
3513 free_tlist(origline
);
3514 return DIRECTIVE_FOUND
;
3516 ctx
= get_ctx(tline
->text
, &mname
);
3518 tline
= expand_smacro(tline
->next
);
3522 while (tok_type_(t
, TOK_WHITESPACE
))
3524 /* t should now point to the string */
3525 if (!tok_type_(t
, TOK_STRING
)) {
3527 "`%%strlen` requires string as second parameter");
3529 free_tlist(origline
);
3530 return DIRECTIVE_FOUND
;
3533 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3534 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3537 * We now have a macro name, an implicit parameter count of
3538 * zero, and a numeric token to use as an expansion. Create
3539 * and store an SMacro.
3541 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3543 free_tlist(origline
);
3544 return DIRECTIVE_FOUND
;
3547 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3550 tline
= tline
->next
;
3552 tline
= expand_id(tline
);
3553 if (!tline
|| (tline
->type
!= TOK_ID
&&
3554 (tline
->type
!= TOK_PREPROC_ID
||
3555 tline
->text
[1] != '$'))) {
3557 "`%%strcat' expects a macro identifier as first parameter");
3558 free_tlist(origline
);
3559 return DIRECTIVE_FOUND
;
3561 ctx
= get_ctx(tline
->text
, &mname
);
3563 tline
= expand_smacro(tline
->next
);
3567 list_for_each(t
, tline
) {
3569 case TOK_WHITESPACE
:
3572 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3575 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3577 /* else fall through */
3580 "non-string passed to `%%strcat' (%d)", t
->type
);
3582 free_tlist(origline
);
3583 return DIRECTIVE_FOUND
;
3587 p
= pp
= nasm_malloc(len
);
3588 list_for_each(t
, tline
) {
3589 if (t
->type
== TOK_STRING
) {
3590 memcpy(p
, t
->text
, t
->a
.len
);
3596 * We now have a macro name, an implicit parameter count of
3597 * zero, and a numeric token to use as an expansion. Create
3598 * and store an SMacro.
3600 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3601 macro_start
->text
= nasm_quote(pp
, len
);
3603 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3605 free_tlist(origline
);
3606 return DIRECTIVE_FOUND
;
3609 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3611 int64_t start
, count
;
3616 tline
= tline
->next
;
3618 tline
= expand_id(tline
);
3619 if (!tline
|| (tline
->type
!= TOK_ID
&&
3620 (tline
->type
!= TOK_PREPROC_ID
||
3621 tline
->text
[1] != '$'))) {
3623 "`%%substr' expects a macro identifier as first parameter");
3624 free_tlist(origline
);
3625 return DIRECTIVE_FOUND
;
3627 ctx
= get_ctx(tline
->text
, &mname
);
3629 tline
= expand_smacro(tline
->next
);
3632 if (tline
) /* skip expanded id */
3634 while (tok_type_(t
, TOK_WHITESPACE
))
3637 /* t should now point to the string */
3638 if (!tok_type_(t
, TOK_STRING
)) {
3640 "`%%substr` requires string as second parameter");
3642 free_tlist(origline
);
3643 return DIRECTIVE_FOUND
;
3648 tokval
.t_type
= TOKEN_INVALID
;
3649 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3653 free_tlist(origline
);
3654 return DIRECTIVE_FOUND
;
3655 } else if (!is_simple(evalresult
)) {
3656 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3658 free_tlist(origline
);
3659 return DIRECTIVE_FOUND
;
3661 start
= evalresult
->value
- 1;
3663 while (tok_type_(tt
, TOK_WHITESPACE
))
3666 count
= 1; /* Backwards compatibility: one character */
3668 tokval
.t_type
= TOKEN_INVALID
;
3669 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3673 free_tlist(origline
);
3674 return DIRECTIVE_FOUND
;
3675 } else if (!is_simple(evalresult
)) {
3676 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3678 free_tlist(origline
);
3679 return DIRECTIVE_FOUND
;
3681 count
= evalresult
->value
;
3684 len
= nasm_unquote(t
->text
, NULL
);
3685 /* make start and count being in range */
3689 count
= len
+ count
+ 1 - start
;
3690 if (start
+ count
> (int64_t)len
)
3691 count
= len
- start
;
3692 if (!len
|| count
< 0 || start
>=(int64_t)len
)
3693 start
= -1, count
= 0; /* empty string */
3695 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3696 macro_start
->text
= nasm_quote((start
< 0) ? "" : t
->text
+ start
, count
);
3697 macro_start
->type
= TOK_STRING
;
3700 * We now have a macro name, an implicit parameter count of
3701 * zero, and a numeric token to use as an expansion. Create
3702 * and store an SMacro.
3704 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3706 free_tlist(origline
);
3707 return DIRECTIVE_FOUND
;
3712 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3713 casesense
= (i
== PP_ASSIGN
);
3715 tline
= tline
->next
;
3717 tline
= expand_id(tline
);
3718 if (!tline
|| (tline
->type
!= TOK_ID
&&
3719 (tline
->type
!= TOK_PREPROC_ID
||
3720 tline
->text
[1] != '$'))) {
3722 "`%%%sassign' expects a macro identifier",
3723 (i
== PP_IASSIGN
? "i" : ""));
3724 free_tlist(origline
);
3725 return DIRECTIVE_FOUND
;
3727 ctx
= get_ctx(tline
->text
, &mname
);
3729 tline
= expand_smacro(tline
->next
);
3734 tokval
.t_type
= TOKEN_INVALID
;
3736 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3739 free_tlist(origline
);
3740 return DIRECTIVE_FOUND
;
3744 error(ERR_WARNING
|ERR_PASS1
,
3745 "trailing garbage after expression ignored");
3747 if (!is_simple(evalresult
)) {
3749 "non-constant value given to `%%%sassign'",
3750 (i
== PP_IASSIGN
? "i" : ""));
3751 free_tlist(origline
);
3752 return DIRECTIVE_FOUND
;
3755 macro_start
= nasm_zalloc(sizeof(*macro_start
));
3756 make_tok_num(macro_start
, reloc_value(evalresult
));
3759 * We now have a macro name, an implicit parameter count of
3760 * zero, and a numeric token to use as an expansion. Create
3761 * and store an SMacro.
3763 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3764 free_tlist(origline
);
3765 return DIRECTIVE_FOUND
;
3768 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3770 * Syntax is `%line nnn[+mmm] [filename]'
3772 tline
= tline
->next
;
3774 if (!tok_type_(tline
, TOK_NUMBER
)) {
3775 error(ERR_NONFATAL
, "`%%line' expects line number");
3776 free_tlist(origline
);
3777 return DIRECTIVE_FOUND
;
3779 k
= readnum(tline
->text
, &err
);
3781 tline
= tline
->next
;
3782 if (tok_is_(tline
, "+")) {
3783 tline
= tline
->next
;
3784 if (!tok_type_(tline
, TOK_NUMBER
)) {
3785 error(ERR_NONFATAL
, "`%%line' expects line increment");
3786 free_tlist(origline
);
3787 return DIRECTIVE_FOUND
;
3789 m
= readnum(tline
->text
, &err
);
3790 tline
= tline
->next
;
3796 nasm_free(src_set_fname(detoken(tline
, false)));
3798 free_tlist(origline
);
3799 return DIRECTIVE_FOUND
;
3802 if (defining
!= NULL
) {
3803 if (defining
->type
== EXP_WHILE
) {
3804 defining
->def_depth
++;
3806 return NO_DIRECTIVE_FOUND
;
3809 if ((istk
->expansion
!= NULL
) &&
3810 (istk
->expansion
->emitting
== false)) {
3814 l
->first
= copy_Token(tline
->next
);
3815 j
= if_condition(tline
->next
, i
);
3816 tline
->next
= NULL
; /* it got freed */
3817 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
3819 ed
= new_ExpDef(EXP_WHILE
);
3822 ed
->max_depth
= DEADMAN_LIMIT
;
3823 ed
->ignoring
= ((ed
->state
== COND_IF_TRUE
) ? false : true);
3824 if (ed
->ignoring
== false) {
3827 } else if (l
!= NULL
) {
3828 delete_Token(l
->first
);
3832 ed
->prev
= defining
;
3834 free_tlist(origline
);
3835 return DIRECTIVE_FOUND
;
3838 if (defining
!= NULL
) {
3839 if (defining
->type
== EXP_WHILE
) {
3840 if (defining
->def_depth
> 0) {
3841 defining
->def_depth
--;
3842 return NO_DIRECTIVE_FOUND
;
3845 return NO_DIRECTIVE_FOUND
;
3848 if (tline
->next
!= NULL
) {
3849 error_precond(ERR_WARNING
|ERR_PASS1
,
3850 "trailing garbage after `%%endwhile' ignored");
3852 if ((defining
== NULL
) || (defining
->type
!= EXP_WHILE
)) {
3853 error(ERR_NONFATAL
, "`%%endwhile': no matching `%%while'");
3854 return DIRECTIVE_FOUND
;
3857 defining
= ed
->prev
;
3858 if (ed
->ignoring
== false) {
3859 ed
->prev
= expansions
;
3861 ei
= new_ExpInv(EXP_WHILE
, ed
);
3862 ei
->current
= ed
->line
->next
;
3863 ei
->emitting
= true;
3864 ei
->prev
= istk
->expansion
;
3865 istk
->expansion
= ei
;
3869 free_tlist(origline
);
3870 return DIRECTIVE_FOUND
;
3873 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3875 * We must search along istk->expansion until we hit a
3876 * while invocation. Then we disable the emitting state(s)
3877 * between exitwhile and endwhile.
3879 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
3880 if (ei
->type
== EXP_WHILE
) {
3887 * Set all invocations leading back to the while
3888 * invocation to a non-emitting state.
3890 for (eei
= istk
->expansion
; eei
!= ei
; eei
= eei
->prev
) {
3891 eei
->emitting
= false;
3893 eei
->emitting
= false;
3894 eei
->current
= NULL
;
3895 eei
->def
->cur_depth
= eei
->def
->max_depth
;
3897 error(ERR_NONFATAL
, "`%%exitwhile' not within `%%while' block");
3899 free_tlist(origline
);
3900 return DIRECTIVE_FOUND
;
3903 if (defining
!= NULL
) {
3904 if (defining
->type
== EXP_COMMENT
) {
3905 defining
->def_depth
++;
3907 return NO_DIRECTIVE_FOUND
;
3909 ed
= new_ExpDef(EXP_COMMENT
);
3910 ed
->ignoring
= true;
3911 ed
->prev
= defining
;
3913 free_tlist(origline
);
3914 return DIRECTIVE_FOUND
;
3917 if (defining
!= NULL
) {
3918 if (defining
->type
== EXP_COMMENT
) {
3919 if (defining
->def_depth
> 0) {
3920 defining
->def_depth
--;
3921 return NO_DIRECTIVE_FOUND
;
3924 return NO_DIRECTIVE_FOUND
;
3927 if ((defining
== NULL
) || (defining
->type
!= EXP_COMMENT
)) {
3928 error(ERR_NONFATAL
, "`%%endcomment': no matching `%%comment'");
3929 return DIRECTIVE_FOUND
;
3932 defining
= ed
->prev
;
3934 free_tlist(origline
);
3935 return DIRECTIVE_FOUND
;
3938 if (defining
!= NULL
) return NO_DIRECTIVE_FOUND
;
3939 if (in_final
!= false) {
3940 error(ERR_FATAL
, "`%%final' cannot be used recursively");
3942 tline
= tline
->next
;
3944 if (tline
== NULL
) {
3945 error(ERR_NONFATAL
, "`%%final' expects at least one parameter");
3948 l
->first
= copy_Token(tline
);
3952 free_tlist(origline
);
3953 return DIRECTIVE_FOUND
;
3957 "preprocessor directive `%s' not yet implemented",
3959 return DIRECTIVE_FOUND
;
3964 * Ensure that a macro parameter contains a condition code and
3965 * nothing else. Return the condition code index if so, or -1
3968 static int find_cc(Token
* t
)
3974 return -1; /* Probably a %+ without a space */
3977 if (t
->type
!= TOK_ID
)
3981 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3985 j
= ARRAY_SIZE(conditions
);
3988 m
= nasm_stricmp(t
->text
, conditions
[k
]);
4003 static bool paste_tokens(Token
**head
, const struct tokseq_match
*m
,
4004 int mnum
, bool handle_paste_tokens
)
4006 Token
**tail
, *t
, *tt
;
4008 bool did_paste
= false;
4012 /* Now handle token pasting... */
4015 while ((t
= *tail
) && (tt
= t
->next
)) {
4017 case TOK_WHITESPACE
:
4018 if (tt
->type
== TOK_WHITESPACE
) {
4019 /* Zap adjacent whitespace tokens */
4020 t
->next
= delete_Token(tt
);
4022 /* Do not advance paste_head here */
4026 case TOK_PASTE
: /* %+ */
4027 if (handle_paste_tokens
) {
4028 /* Zap %+ and whitespace tokens to the right */
4029 while (t
&& (t
->type
== TOK_WHITESPACE
||
4030 t
->type
== TOK_PASTE
))
4031 t
= *tail
= delete_Token(t
);
4032 if (!paste_head
|| !t
)
4033 break; /* Nothing to paste with */
4037 while (tok_type_(tt
, TOK_WHITESPACE
))
4038 tt
= t
->next
= delete_Token(tt
);
4040 tmp
= nasm_strcat(t
->text
, tt
->text
);
4042 tt
= delete_Token(tt
);
4043 t
= *tail
= tokenize(tmp
);
4049 t
->next
= tt
; /* Attach the remaining token chain */
4056 /* else fall through */
4059 * Concatenation of tokens might look nontrivial
4060 * but in real it's pretty simple -- the caller
4061 * prepares the masks of token types to be concatenated
4062 * and we simply find matched sequences and slip
4065 for (i
= 0; i
< mnum
; i
++) {
4066 if (PP_CONCAT_MASK(t
->type
) & m
[i
].mask_head
) {
4070 while (tt
&& (PP_CONCAT_MASK(tt
->type
) & m
[i
].mask_tail
)) {
4071 len
+= strlen(tt
->text
);
4075 nasm_dump_token(tt
);
4078 * Now tt points to the first token after
4079 * the potential paste area...
4081 if (tt
!= t
->next
) {
4082 /* We have at least two tokens... */
4083 len
+= strlen(t
->text
);
4084 p
= tmp
= nasm_malloc(len
+1);
4087 p
= strchr(p
, '\0');
4088 t
= delete_Token(t
);
4090 t
= *tail
= tokenize(tmp
);
4096 t
->next
= tt
; /* Attach the remaining token chain */
4104 if (i
>= mnum
) { /* no match */
4106 if (!tok_type_(t
->next
, TOK_WHITESPACE
))
4116 * expands to a list of tokens from %{x:y}
4118 static Token
*expand_mmac_params_range(ExpInv
*ei
, Token
*tline
, Token
***last
)
4120 Token
*t
= tline
, **tt
, *tm
, *head
;
4124 pos
= strchr(tline
->text
, ':');
4127 lst
= atoi(pos
+ 1);
4128 fst
= atoi(tline
->text
+ 1);
4131 * only macros params are accounted so
4132 * if someone passes %0 -- we reject such
4135 if (lst
== 0 || fst
== 0)
4138 /* the values should be sane */
4139 if ((fst
> (int)ei
->nparam
|| fst
< (-(int)ei
->nparam
)) ||
4140 (lst
> (int)ei
->nparam
|| lst
< (-(int)ei
->nparam
)))
4143 fst
= fst
< 0 ? fst
+ (int)ei
->nparam
+ 1: fst
;
4144 lst
= lst
< 0 ? lst
+ (int)ei
->nparam
+ 1: lst
;
4146 /* counted from zero */
4150 * it will be at least one token
4152 tm
= ei
->params
[(fst
+ ei
->rotate
) % ei
->nparam
];
4153 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4154 head
= t
, tt
= &t
->next
;
4156 for (i
= fst
+ 1; i
<= lst
; i
++) {
4157 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4158 *tt
= t
, tt
= &t
->next
;
4159 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4161 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4162 *tt
= t
, tt
= &t
->next
;
4165 for (i
= fst
- 1; i
>= lst
; i
--) {
4166 t
= new_Token(NULL
, TOK_OTHER
, ",", 0);
4167 *tt
= t
, tt
= &t
->next
;
4168 j
= (i
+ ei
->rotate
) % ei
->nparam
;
4170 t
= new_Token(NULL
, tm
->type
, tm
->text
, 0);
4171 *tt
= t
, tt
= &t
->next
;
4179 error(ERR_NONFATAL
, "`%%{%s}': macro parameters out of range",
4185 * Expand MMacro-local things: parameter references (%0, %n, %+n,
4186 * %-n) and MMacro-local identifiers (%%foo) as well as
4187 * macro indirection (%[...]) and range (%{..:..}).
4189 static Token
*expand_mmac_params(Token
* tline
)
4191 Token
*t
, *tt
, **tail
, *thead
;
4192 bool changed
= false;
4198 nasm_dump_stream(tline
);
4201 if (tline
->type
== TOK_PREPROC_ID
&&
4202 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
4203 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9') ||
4204 tline
->text
[1] == '%')) {
4206 int type
= 0, cc
; /* type = 0 to placate optimisers */
4213 tline
= tline
->next
;
4215 for (ei
= istk
->expansion
; ei
!= NULL
; ei
= ei
->prev
) {
4216 if (ei
->type
== EXP_MMACRO
) {
4221 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
4223 pos
= strchr(t
->text
, ':');
4225 switch (t
->text
[1]) {
4227 * We have to make a substitution of one of the
4228 * forms %1, %-1, %+1, %%foo, %0.
4231 if ((strlen(t
->text
) > 2) && (t
->text
[2] == '0')) {
4233 text
= nasm_strdup(ei
->label_text
);
4236 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", ei
->nparam
);
4237 text
= nasm_strdup(tmpbuf
);
4242 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
4244 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
4247 n
= atoi(t
->text
+ 2) - 1;
4248 if (n
>= ei
->nparam
)
4252 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4258 "macro parameter %d is not a condition code",
4263 if (inverse_ccs
[cc
] == -1) {
4265 "condition code `%s' is not invertible",
4269 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
4273 n
= atoi(t
->text
+ 2) - 1;
4274 if (n
>= ei
->nparam
)
4278 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4284 "macro parameter %d is not a condition code",
4289 text
= nasm_strdup(conditions
[cc
]);
4293 n
= atoi(t
->text
+ 1) - 1;
4294 if (n
>= ei
->nparam
)
4298 n
= (n
+ ei
->rotate
) % ei
->nparam
;
4302 for (i
= 0; i
< ei
->paramlen
[n
]; i
++) {
4303 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
4304 tail
= &(*tail
)->next
;
4308 text
= NULL
; /* we've done it here */
4313 * seems we have a parameters range here
4315 Token
*head
, **last
;
4316 head
= expand_mmac_params_range(ei
, t
, &last
);
4337 } else if (tline
->type
== TOK_INDIRECT
) {
4339 tline
= tline
->next
;
4340 tt
= tokenize(t
->text
);
4341 tt
= expand_mmac_params(tt
);
4342 tt
= expand_smacro(tt
);
4345 tt
->a
.mac
= NULL
; /* Necessary? */
4353 tline
= tline
->next
;
4361 const struct tokseq_match t
[] = {
4363 PP_CONCAT_MASK(TOK_ID
) |
4364 PP_CONCAT_MASK(TOK_FLOAT
), /* head */
4365 PP_CONCAT_MASK(TOK_ID
) |
4366 PP_CONCAT_MASK(TOK_NUMBER
) |
4367 PP_CONCAT_MASK(TOK_FLOAT
) |
4368 PP_CONCAT_MASK(TOK_OTHER
) /* tail */
4371 PP_CONCAT_MASK(TOK_NUMBER
), /* head */
4372 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4375 paste_tokens(&thead
, t
, ARRAY_SIZE(t
), false);
4378 nasm_dump_token(thead
);
4384 * Expand all single-line macro calls made in the given line.
4385 * Return the expanded version of the line. The original is deemed
4386 * to be destroyed in the process. (In reality we'll just move
4387 * Tokens from input to output a lot of the time, rather than
4388 * actually bothering to destroy and replicate.)
4391 static Token
*expand_smacro(Token
* tline
)
4393 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
4394 SMacro
*head
= NULL
, *m
;
4397 unsigned int nparam
, sparam
;
4399 Token
*org_tline
= tline
;
4402 int deadman
= DEADMAN_LIMIT
;
4406 * Trick: we should avoid changing the start token pointer since it can
4407 * be contained in "next" field of other token. Because of this
4408 * we allocate a copy of first token and work with it; at the end of
4409 * routine we copy it back
4412 tline
= new_Token(org_tline
->next
, org_tline
->type
,
4413 org_tline
->text
, 0);
4414 tline
->a
.mac
= org_tline
->a
.mac
;
4415 nasm_free(org_tline
->text
);
4416 org_tline
->text
= NULL
;
4419 expanded
= true; /* Always expand %+ at least once */
4425 while (tline
) { /* main token loop */
4427 error(ERR_NONFATAL
, "interminable macro recursion");
4431 if ((mname
= tline
->text
)) {
4432 /* if this token is a local macro, look in local context */
4433 if (tline
->type
== TOK_ID
) {
4434 head
= (SMacro
*)hash_findix(&smacros
, mname
);
4435 } else if (tline
->type
== TOK_PREPROC_ID
) {
4436 ctx
= get_ctx(mname
, &mname
);
4437 head
= ctx
? (SMacro
*)hash_findix(&ctx
->localmac
, mname
) : NULL
;
4442 * We've hit an identifier. As in is_mmacro below, we first
4443 * check whether the identifier is a single-line macro at
4444 * all, then think about checking for parameters if
4447 list_for_each(m
, head
)
4448 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
4454 if (m
->nparam
== 0) {
4456 * Simple case: the macro is parameterless. Discard the
4457 * one token that the macro call took, and push the
4458 * expansion back on the to-do stack.
4460 if (!m
->expansion
) {
4461 if (!strcmp("__FILE__", m
->name
)) {
4464 src_get(&num
, &file
);
4465 tline
->text
= nasm_quote(file
, strlen(file
));
4466 tline
->type
= TOK_STRING
;
4470 if (!strcmp("__LINE__", m
->name
)) {
4471 nasm_free(tline
->text
);
4472 make_tok_num(tline
, src_get_linnum());
4475 if (!strcmp("__BITS__", m
->name
)) {
4476 nasm_free(tline
->text
);
4477 make_tok_num(tline
, globalbits
);
4480 tline
= delete_Token(tline
);
4485 * Complicated case: at least one macro with this name
4486 * exists and takes parameters. We must find the
4487 * parameters in the call, count them, find the SMacro
4488 * that corresponds to that form of the macro call, and
4489 * substitute for the parameters when we expand. What a
4492 /*tline = tline->next;
4493 skip_white_(tline); */
4496 while (tok_type_(t
, TOK_SMAC_END
)) {
4497 t
->a
.mac
->in_progress
= false;
4499 t
= tline
->next
= delete_Token(t
);
4502 } while (tok_type_(tline
, TOK_WHITESPACE
));
4503 if (!tok_is_(tline
, "(")) {
4505 * This macro wasn't called with parameters: ignore
4506 * the call. (Behaviour borrowed from gnu cpp.)
4515 sparam
= PARAM_DELTA
;
4516 params
= nasm_malloc(sparam
* sizeof(Token
*));
4517 params
[0] = tline
->next
;
4518 paramsize
= nasm_malloc(sparam
* sizeof(int));
4520 while (true) { /* parameter loop */
4522 * For some unusual expansions
4523 * which concatenates function call
4526 while (tok_type_(t
, TOK_SMAC_END
)) {
4527 t
->a
.mac
->in_progress
= false;
4529 t
= tline
->next
= delete_Token(t
);
4535 "macro call expects terminating `)'");
4538 if (tline
->type
== TOK_WHITESPACE
4540 if (paramsize
[nparam
])
4543 params
[nparam
] = tline
->next
;
4544 continue; /* parameter loop */
4546 if (tline
->type
== TOK_OTHER
4547 && tline
->text
[1] == 0) {
4548 char ch
= tline
->text
[0];
4549 if (ch
== ',' && !paren
&& brackets
<= 0) {
4550 if (++nparam
>= sparam
) {
4551 sparam
+= PARAM_DELTA
;
4552 params
= nasm_realloc(params
,
4553 sparam
* sizeof(Token
*));
4554 paramsize
= nasm_realloc(paramsize
,
4555 sparam
* sizeof(int));
4557 params
[nparam
] = tline
->next
;
4558 paramsize
[nparam
] = 0;
4560 continue; /* parameter loop */
4563 (brackets
> 0 || (brackets
== 0 &&
4564 !paramsize
[nparam
])))
4566 if (!(brackets
++)) {
4567 params
[nparam
] = tline
->next
;
4568 continue; /* parameter loop */
4571 if (ch
== '}' && brackets
> 0)
4572 if (--brackets
== 0) {
4574 continue; /* parameter loop */
4576 if (ch
== '(' && !brackets
)
4578 if (ch
== ')' && brackets
<= 0)
4584 error(ERR_NONFATAL
, "braces do not "
4585 "enclose all of macro parameter");
4587 paramsize
[nparam
] += white
+ 1;
4589 } /* parameter loop */
4591 while (m
&& (m
->nparam
!= nparam
||
4592 mstrcmp(m
->name
, mname
,
4596 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4597 "macro `%s' exists, "
4598 "but not taking %d parameters",
4599 mstart
->text
, nparam
);
4602 if (m
&& m
->in_progress
)
4604 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
4606 * Design question: should we handle !tline, which
4607 * indicates missing ')' here, or expand those
4608 * macros anyway, which requires the (t) test a few
4612 nasm_free(paramsize
);
4616 * Expand the macro: we are placed on the last token of the
4617 * call, so that we can easily split the call from the
4618 * following tokens. We also start by pushing an SMAC_END
4619 * token for the cycle removal.
4626 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
4628 m
->in_progress
= true;
4630 list_for_each(t
, m
->expansion
) {
4631 if (is_smacro_param(t
)) {
4632 Token
*pcopy
= tline
, **ptail
= &pcopy
;
4636 idx
= smacro_get_param_idx(t
);
4640 * We need smacro paramters appended.
4642 for (i
= paramsize
[idx
]; i
> 0; i
--) {
4643 *ptail
= new_Token(tline
, ttt
->type
, ttt
->text
, 0);
4644 ptail
= &(*ptail
)->next
;
4649 } else if (t
->type
== TOK_PREPROC_Q
) {
4650 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
4652 } else if (t
->type
== TOK_PREPROC_QQ
) {
4653 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
4656 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
4662 * Having done that, get rid of the macro call, and clean
4663 * up the parameters.
4666 nasm_free(paramsize
);
4669 continue; /* main token loop */
4674 if (tline
->type
== TOK_SMAC_END
) {
4675 tline
->a
.mac
->in_progress
= false;
4676 tline
= delete_Token(tline
);
4679 tline
= tline
->next
;
4687 * Now scan the entire line and look for successive TOK_IDs that resulted
4688 * after expansion (they can't be produced by tokenize()). The successive
4689 * TOK_IDs should be concatenated.
4690 * Also we look for %+ tokens and concatenate the tokens before and after
4691 * them (without white spaces in between).
4694 const struct tokseq_match t
[] = {
4696 PP_CONCAT_MASK(TOK_ID
) |
4697 PP_CONCAT_MASK(TOK_PREPROC_ID
), /* head */
4698 PP_CONCAT_MASK(TOK_ID
) |
4699 PP_CONCAT_MASK(TOK_PREPROC_ID
) |
4700 PP_CONCAT_MASK(TOK_NUMBER
) /* tail */
4703 if (paste_tokens(&thead
, t
, ARRAY_SIZE(t
), true)) {
4705 * If we concatenated something, *and* we had previously expanded
4706 * an actual macro, scan the lines again for macros...
4717 *org_tline
= *thead
;
4718 /* since we just gave text to org_line, don't free it */
4720 delete_Token(thead
);
4722 /* the expression expanded to empty line;
4723 we can't return NULL for some reasons
4724 we just set the line to a single WHITESPACE token. */
4725 memset(org_tline
, 0, sizeof(*org_tline
));
4726 org_tline
->text
= NULL
;
4727 org_tline
->type
= TOK_WHITESPACE
;
4736 * Similar to expand_smacro but used exclusively with macro identifiers
4737 * right before they are fetched in. The reason is that there can be
4738 * identifiers consisting of several subparts. We consider that if there
4739 * are more than one element forming the name, user wants a expansion,
4740 * otherwise it will be left as-is. Example:
4744 * the identifier %$abc will be left as-is so that the handler for %define
4745 * will suck it and define the corresponding value. Other case:
4747 * %define _%$abc cde
4749 * In this case user wants name to be expanded *before* %define starts
4750 * working, so we'll expand %$abc into something (if it has a value;
4751 * otherwise it will be left as-is) then concatenate all successive
4754 static Token
*expand_id(Token
* tline
)
4756 Token
*cur
, *oldnext
= NULL
;
4758 if (!tline
|| !tline
->next
)
4763 (cur
->next
->type
== TOK_ID
||
4764 cur
->next
->type
== TOK_PREPROC_ID
||
4765 cur
->next
->type
== TOK_NUMBER
))
4768 /* If identifier consists of just one token, don't expand */
4773 oldnext
= cur
->next
; /* Detach the tail past identifier */
4774 cur
->next
= NULL
; /* so that expand_smacro stops here */
4777 tline
= expand_smacro(tline
);
4780 /* expand_smacro possibly changhed tline; re-scan for EOL */
4782 while (cur
&& cur
->next
)
4785 cur
->next
= oldnext
;
4792 * Determine whether the given line constitutes a multi-line macro
4793 * call, and return the ExpDef structure called if so. Doesn't have
4794 * to check for an initial label - that's taken care of in
4795 * expand_mmacro - but must check numbers of parameters. Guaranteed
4796 * to be called with tline->type == TOK_ID, so the putative macro
4797 * name is easy to find.
4799 static ExpDef
*is_mmacro(Token
* tline
, Token
*** params_array
)
4805 head
= (ExpDef
*) hash_findix(&expdefs
, tline
->text
);
4808 * Efficiency: first we see if any macro exists with the given
4809 * name. If not, we can return NULL immediately. _Then_ we
4810 * count the parameters, and then we look further along the
4811 * list if necessary to find the proper ExpDef.
4813 list_for_each(ed
, head
)
4814 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4820 * OK, we have a potential macro. Count and demarcate the
4823 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4826 * So we know how many parameters we've got. Find the ExpDef
4827 * structure that handles this number.
4830 if (ed
->nparam_min
<= nparam
4831 && (ed
->plus
|| nparam
<= ed
->nparam_max
)) {
4833 * It's right, and we can use it. Add its default
4834 * parameters to the end of our list if necessary.
4836 if (ed
->defaults
&& nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4838 nasm_realloc(params
,
4839 ((ed
->nparam_min
+ ed
->ndefs
+
4840 1) * sizeof(*params
)));
4841 while (nparam
< ed
->nparam_min
+ ed
->ndefs
) {
4842 params
[nparam
] = ed
->defaults
[nparam
- ed
->nparam_min
];
4847 * If we've gone over the maximum parameter count (and
4848 * we're in Plus mode), ignore parameters beyond
4851 if (ed
->plus
&& nparam
> ed
->nparam_max
)
4852 nparam
= ed
->nparam_max
;
4854 * Then terminate the parameter list, and leave.
4856 if (!params
) { /* need this special case */
4857 params
= nasm_malloc(sizeof(*params
));
4860 params
[nparam
] = NULL
;
4861 *params_array
= params
;
4865 * This one wasn't right: look for the next one with the
4868 list_for_each(ed
, ed
->next
)
4869 if (!mstrcmp(ed
->name
, tline
->text
, ed
->casesense
))
4874 * After all that, we didn't find one with the right number of
4875 * parameters. Issue a warning, and fail to expand the macro.
4877 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4878 "macro `%s' exists, but not taking %d parameters",
4879 tline
->text
, nparam
);
4885 * Expand the multi-line macro call made by the given line, if
4886 * there is one to be expanded. If there is, push the expansion on
4887 * istk->expansion and return true. Otherwise return false.
4889 static bool expand_mmacro(Token
* tline
)
4891 Token
*label
= NULL
;
4892 int dont_prepend
= 0;
4897 int i
, nparam
, *paramlen
;
4902 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4903 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4905 ed
= is_mmacro(t
, ¶ms
);
4911 * We have an id which isn't a macro call. We'll assume
4912 * it might be a label; we'll also check to see if a
4913 * colon follows it. Then, if there's another id after
4914 * that lot, we'll check it again for macro-hood.
4918 if (tok_type_(t
, TOK_WHITESPACE
))
4919 last
= t
, t
= t
->next
;
4920 if (tok_is_(t
, ":")) {
4922 last
= t
, t
= t
->next
;
4923 if (tok_type_(t
, TOK_WHITESPACE
))
4924 last
= t
, t
= t
->next
;
4926 if (!tok_type_(t
, TOK_ID
) || !(ed
= is_mmacro(t
, ¶ms
)))
4934 * Fix up the parameters: this involves stripping leading and
4935 * trailing whitespace, then stripping braces if they are
4938 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4939 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4941 for (i
= 0; params
[i
]; i
++) {
4943 int comma
= (!ed
->plus
|| i
< nparam
- 1);
4947 if (tok_is_(t
, "{"))
4948 t
= t
->next
, brace
= true, comma
= false;
4952 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4953 break; /* ... because we have hit a comma */
4954 if (comma
&& t
->type
== TOK_WHITESPACE
4955 && tok_is_(t
->next
, ","))
4956 break; /* ... or a space then a comma */
4957 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4958 break; /* ... or a brace */
4964 if (ed
->cur_depth
>= ed
->max_depth
) {
4965 if (ed
->max_depth
> 1) {
4967 "reached maximum macro recursion depth of %i for %s",
4968 ed
->max_depth
,ed
->name
);
4976 * OK, we have found a ExpDef structure representing a
4977 * previously defined mmacro. Create an expansion invocation
4978 * and point it back to the expansion definition. Substitution of
4979 * parameter tokens and macro-local tokens doesn't get done
4980 * until the single-line macro substitution process; this is
4981 * because delaying them allows us to change the semantics
4982 * later through %rotate.
4984 ei
= new_ExpInv(EXP_MMACRO
, ed
);
4985 ei
->name
= nasm_strdup(mname
);
4986 //ei->label = label;
4987 //ei->label_text = detoken(label, false);
4988 ei
->current
= ed
->line
;
4989 ei
->emitting
= true;
4990 //ei->iline = tline;
4991 ei
->params
= params
;
4992 ei
->nparam
= nparam
;
4994 ei
->paramlen
= paramlen
;
4997 ei
->prev
= istk
->expansion
;
4998 istk
->expansion
= ei
;
5001 * Special case: detect %00 on first invocation; if found,
5002 * avoid emitting any labels that precede the mmacro call.
5003 * ed->prepend is set to -1 when %00 is detected, else 1.
5005 if (ed
->prepend
== 0) {
5006 for (l
= ed
->line
; l
!= NULL
; l
= l
->next
) {
5007 for (t
= l
->first
; t
!= NULL
; t
= t
->next
) {
5008 if ((t
->type
== TOK_PREPROC_ID
) &&
5009 (strlen(t
->text
) == 3) &&
5010 (t
->text
[1] == '0') && (t
->text
[2] == '0')) {
5015 if (dont_prepend
< 0) {
5019 ed
->prepend
= ((dont_prepend
< 0) ? -1 : 1);
5023 * If we had a label, push it on as the first line of
5024 * the macro expansion.
5026 if (label
!= NULL
) {
5027 if (ed
->prepend
< 0) {
5028 ei
->label_text
= detoken(label
, false);
5030 if (dont_prepend
== 0) {
5032 while (t
->next
!= NULL
) {
5035 t
->next
= new_Token(NULL
, TOK_OTHER
, ":", 0);
5038 l
->first
= copy_Token(label
);
5039 l
->next
= ei
->current
;
5044 list
->uplevel(ed
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
5050 /* The function that actually does the error reporting */
5051 static void verror(int severity
, const char *fmt
, va_list arg
)
5055 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
5057 if (istk
&& istk
->mmac_depth
> 0) {
5058 ExpInv
*ei
= istk
->expansion
;
5059 int lineno
= ei
->lineno
;
5061 if (ei
->type
== EXP_MMACRO
)
5063 lineno
+= ei
->relno
;
5066 nasm_error(severity
, "(%s:%d) %s", ei
->def
->name
,
5069 nasm_error(severity
, "%s", buff
);
5073 * Since preprocessor always operate only on the line that didn't
5074 * arrived yet, we should always use ERR_OFFBY1.
5076 static void error(int severity
, const char *fmt
, ...)
5080 verror(severity
, fmt
, arg
);
5085 * Because %else etc are evaluated in the state context
5086 * of the previous branch, errors might get lost with error():
5087 * %if 0 ... %else trailing garbage ... %endif
5088 * So %else etc should report errors with this function.
5090 static void error_precond(int severity
, const char *fmt
, ...)
5094 /* Only ignore the error if it's really in a dead branch */
5095 if ((istk
!= NULL
) &&
5096 (istk
->expansion
!= NULL
) &&
5097 (istk
->expansion
->type
== EXP_IF
) &&
5098 (istk
->expansion
->def
->state
== COND_NEVER
))
5102 verror(severity
, fmt
, arg
);
5107 pp_reset(char *file
, int apass
, ListGen
* listgen
, StrList
**deplist
)
5112 istk
= nasm_zalloc(sizeof(Include
));
5113 istk
->fp
= fopen(file
, "r");
5114 src_set_fname(nasm_strdup(file
));
5118 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
5123 nested_mac_count
= 0;
5124 nested_rep_count
= 0;
5127 if (tasm_compatible_mode
) {
5128 stdmacpos
= nasm_stdmac
;
5130 stdmacpos
= nasm_stdmac_after_tasm
;
5132 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
5137 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5138 * The caller, however, will also pass in 3 for preprocess-only so
5139 * we can set __PASS__ accordingly.
5141 pass
= apass
> 2 ? 2 : apass
;
5143 dephead
= deptail
= deplist
;
5145 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
5147 strcpy(sl
->str
, file
);
5149 deptail
= &sl
->next
;
5153 * Define the __PASS__ macro. This is defined here unlike
5154 * all the other builtins, because it is special -- it varies between
5157 t
= nasm_zalloc(sizeof(*t
));
5158 make_tok_num(t
, apass
);
5159 define_smacro(NULL
, "__PASS__", true, 0, t
);
5162 static char *pp_getline(void)
5173 * Fetch a tokenized line, either from the expansion
5174 * buffer or from the input file.
5178 while (1) { /* until we get a line we can use */
5180 * Fetch a tokenized line from the expansion buffer
5182 if (istk
->expansion
!= NULL
) {
5183 ei
= istk
->expansion
;
5184 if (ei
->current
!= NULL
) {
5185 if (ei
->emitting
== false) {
5190 ei
->current
= l
->next
;
5192 tline
= copy_Token(l
->first
);
5193 if (((ei
->type
== EXP_REP
) ||
5194 (ei
->type
== EXP_MMACRO
) ||
5195 (ei
->type
== EXP_WHILE
))
5196 && (ei
->def
->nolist
== false)) {
5197 char *p
= detoken(tline
, false);
5198 list
->line(LIST_MACRO
, p
);
5201 if (ei
->linnum
> -1) {
5202 src_set_linnum(src_get_linnum() + 1);
5205 } else if ((ei
->type
== EXP_REP
) &&
5206 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5207 ei
->def
->cur_depth
++;
5208 ei
->current
= ei
->def
->line
;
5211 } else if ((ei
->type
== EXP_WHILE
) &&
5212 (ei
->def
->cur_depth
< ei
->def
->max_depth
)) {
5213 ei
->current
= ei
->def
->line
;
5215 tline
= copy_Token(ei
->current
->first
);
5216 j
= if_condition(tline
, PP_WHILE
);
5218 j
= (((j
< 0) ? COND_NEVER
: j
) ? COND_IF_TRUE
: COND_IF_FALSE
);
5219 if (j
== COND_IF_TRUE
) {
5220 ei
->current
= ei
->current
->next
;
5221 ei
->def
->cur_depth
++;
5223 ei
->emitting
= false;
5225 ei
->def
->cur_depth
= ei
->def
->max_depth
;
5229 istk
->expansion
= ei
->prev
;
5232 if ((ei
->emitting
== true) &&
5233 (ed
->max_depth
== DEADMAN_LIMIT
) &&
5234 (ed
->cur_depth
== DEADMAN_LIMIT
)
5236 error(ERR_FATAL
, "runaway expansion detected, aborting");
5238 if (ed
->cur_depth
> 0) {
5240 } else if (ed
->type
!= EXP_MMACRO
) {
5241 expansions
= ed
->prev
;
5244 if ((ei
->type
== EXP_REP
) ||
5245 (ei
->type
== EXP_MMACRO
) ||
5246 (ei
->type
== EXP_WHILE
)) {
5247 list
->downlevel(LIST_MACRO
);
5248 if (ei
->type
== EXP_MMACRO
) {
5253 if (ei
->linnum
> -1) {
5254 src_set_linnum(ei
->linnum
);
5262 * Read in line from input and tokenize
5265 if (line
) { /* from the current input file */
5266 line
= prepreproc(line
);
5267 tline
= tokenize(line
);
5273 * The current file has ended; work down the istk
5278 if (i
->expansion
!= NULL
) {
5280 "end of file while still in an expansion");
5282 /* only set line and file name if there's a next node */
5284 src_set_linnum(i
->lineno
);
5285 nasm_free(src_set_fname(nasm_strdup(i
->fname
)));
5287 if ((i
->next
== NULL
) && (finals
!= NULL
)) {
5289 ei
= new_ExpInv(EXP_FINAL
, NULL
);
5290 ei
->emitting
= true;
5291 ei
->current
= finals
;
5292 istk
->expansion
= ei
;
5297 list
->downlevel(LIST_INCLUDE
);
5300 if (finals
!= NULL
) {
5310 if (defining
== NULL
) {
5311 tline
= expand_mmac_params(tline
);
5315 * Check the line to see if it's a preprocessor directive.
5317 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
5319 } else if (defining
!= NULL
) {
5321 * We're defining an expansion. We emit nothing at all,
5322 * and just shove the tokenized line on to the definition.
5324 if (defining
->ignoring
== false) {
5325 Line
*l
= new_Line();
5327 if (defining
->line
== NULL
) {
5331 defining
->last
->next
= l
;
5337 defining
->linecount
++;
5339 } else if ((istk
->expansion
!= NULL
) &&
5340 (istk
->expansion
->emitting
!= true)) {
5342 * We're in a non-emitting branch of an expansion.
5343 * Emit nothing at all, not even a blank line: when we
5344 * emerge from the expansion we'll give a line-number
5345 * directive so we keep our place correctly.
5350 tline
= expand_smacro(tline
);
5351 if (expand_mmacro(tline
) != true) {
5353 * De-tokenize the line again, and emit it.
5355 line
= detoken(tline
, true);
5366 static void pp_cleanup(int pass
)
5368 if (defining
!= NULL
) {
5369 error(ERR_NONFATAL
, "end of file while still defining an expansion");
5370 while (defining
!= NULL
) {
5371 ExpDef
*ed
= defining
;
5372 defining
= ed
->prev
;
5377 while (cstk
!= NULL
)
5380 while (istk
!= NULL
) {
5384 nasm_free(i
->fname
);
5385 while (i
->expansion
!= NULL
) {
5386 ExpInv
*ei
= i
->expansion
;
5387 i
->expansion
= ei
->prev
;
5394 nasm_free(src_set_fname(NULL
));
5399 while ((i
= ipath
)) {
5407 void pp_include_path(char *path
)
5409 IncPath
*i
= nasm_zalloc(sizeof(IncPath
));
5412 i
->path
= nasm_strdup(path
);
5424 void pp_pre_include(char *fname
)
5426 Token
*inc
, *space
, *name
;
5429 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
5430 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
5431 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
5439 void pp_pre_define(char *definition
)
5445 equals
= strchr(definition
, '=');
5446 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5447 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
5450 space
->next
= tokenize(definition
);
5460 void pp_pre_undefine(char *definition
)
5465 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
5466 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
5467 space
->next
= tokenize(definition
);
5476 * This function is used to assist with "runtime" preprocessor
5477 * directives, e.g. pp_runtime("%define __BITS__ 64");
5479 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5480 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5483 void pp_runtime(char *definition
)
5487 def
= tokenize(definition
);
5488 if (do_directive(def
) == NO_DIRECTIVE_FOUND
)
5493 void pp_extra_stdmac(macros_t
*macros
)
5495 extrastdmac
= macros
;
5498 static void make_tok_num(Token
* tok
, int64_t val
)
5501 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
5502 tok
->text
= nasm_strdup(numbuf
);
5503 tok
->type
= TOK_NUMBER
;
5506 struct preproc_ops nasmpp
= {