1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
55 typedef struct SMacro SMacro
;
56 typedef struct MMacro MMacro
;
57 typedef struct Context Context
;
58 typedef struct Token Token
;
59 typedef struct Blocks Blocks
;
60 typedef struct Line Line
;
61 typedef struct Include Include
;
62 typedef struct Cond Cond
;
63 typedef struct IncPath IncPath
;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
106 int nparam_min
, nparam_max
;
108 bool plus
; /* is the last parameter greedy? */
109 bool nolist
; /* is this macro listing-inhibited? */
111 Token
*dlist
; /* All defaults as one list */
112 Token
**defaults
; /* Parameter default pointers */
113 int ndefs
; /* number of default parameters */
117 MMacro
*rep_nest
; /* used for nesting %rep */
118 Token
**params
; /* actual parameters */
119 Token
*iline
; /* invocation line */
120 unsigned int nparam
, rotate
;
123 int lineno
; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
131 struct hash_table
*localmac
;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
156 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
157 TOK_PREPROC_ID
, TOK_STRING
,
158 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
165 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
166 enum pp_token_type type
;
170 * Multi-line macro definitions are stored as a linked list of
171 * these, which is essentially a container to allow several linked
174 * Note that in this module, linked lists are treated as stacks
175 * wherever possible. For this reason, Lines are _pushed_ on to the
176 * `expansion' field in MMacro structures, so that the linked list,
177 * if walked, would give the macro lines in reverse order; this
178 * means that we can walk the list when expanding a macro, and thus
179 * push the lines on to the `expansion' field in _istk_ in reverse
180 * order (so that when popped back off they are in the right
181 * order). It may seem cockeyed, and it relies on my design having
182 * an even number of steps in, but it works...
184 * Some of these structures, rather than being actual lines, are
185 * markers delimiting the end of the expansion of a given macro.
186 * This is for use in the cycle-tracking and %rep-handling code.
187 * Such structures have `finishes' non-NULL, and `first' NULL. All
188 * others have `finishes' NULL, but `first' may still be NULL if
198 * To handle an arbitrary level of file inclusion, we maintain a
199 * stack (ie linked list) of these things.
208 MMacro
*mstk
; /* stack of active macros/reps */
212 * Include search path. This is simply a list of strings which get
213 * prepended, in turn, to the name of an include file, in an
214 * attempt to find the file if it's not in the current directory.
222 * Conditional assembly: we maintain a separate stack of these for
223 * each level of file inclusion. (The only reason we keep the
224 * stacks separate is to ensure that a stray `%endif' in a file
225 * included from within the true branch of a `%if' won't terminate
226 * it and cause confusion: instead, rightly, it'll cause an error.)
234 * These states are for use just after %if or %elif: IF_TRUE
235 * means the condition has evaluated to truth so we are
236 * currently emitting, whereas IF_FALSE means we are not
237 * currently emitting but will start doing so if a %else comes
238 * up. In these states, all directives are admissible: %elif,
239 * %else and %endif. (And of course %if.)
241 COND_IF_TRUE
, COND_IF_FALSE
,
243 * These states come up after a %else: ELSE_TRUE means we're
244 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
245 * any %elif or %else will cause an error.
247 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
249 * This state means that we're not emitting now, and also that
250 * nothing until %endif will be emitted at all. It's for use in
251 * two circumstances: (i) when we've had our moment of emission
252 * and have now started seeing %elifs, and (ii) when the
253 * condition construct in question is contained within a
254 * non-emitting branch of a larger condition construct.
258 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
261 * These defines are used as the possible return values for do_directive
263 #define NO_DIRECTIVE_FOUND 0
264 #define DIRECTIVE_FOUND 1
267 * Condition codes. Note that we use c_ prefix not C_ because C_ is
268 * used in nasm.h for the "real" condition codes. At _this_ level,
269 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
270 * ones, so we need a different enum...
272 static const char * const conditions
[] = {
273 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
274 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
275 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
278 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
279 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
280 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
283 static const enum pp_conds inverse_ccs
[] = {
284 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
285 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
,
286 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
292 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
293 static int is_condition(enum preproc_token arg
)
295 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
298 /* For TASM compatibility we need to be able to recognise TASM compatible
299 * conditional compilation directives. Using the NASM pre-processor does
300 * not work, so we look for them specifically from the following list and
301 * then jam in the equivalent NASM directive into the input stream.
305 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
306 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
309 static const char * const tasm_directives
[] = {
310 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
311 "ifndef", "include", "local"
314 static int StackSize
= 4;
315 static char *StackPointer
= "ebp";
316 static int ArgOffset
= 8;
317 static int LocalOffset
= 0;
319 static Context
*cstk
;
320 static Include
*istk
;
321 static IncPath
*ipath
= NULL
;
323 static efunc _error
; /* Pointer to client-provided error reporting function */
324 static evalfunc evaluate
;
326 static int pass
; /* HACK: pass 0 = generate dependencies only */
328 static uint64_t unique
; /* unique identifier numbers */
330 static Line
*predef
= NULL
;
332 static ListGen
*list
;
335 * The current set of multi-line macros we have defined.
337 static struct hash_table
*mmacros
;
340 * The current set of single-line macros we have defined.
342 static struct hash_table
*smacros
;
345 * The multi-line macro we are currently defining, or the %rep
346 * block we are currently reading, if any.
348 static MMacro
*defining
;
351 * The number of macro parameters to allocate space for at a time.
353 #define PARAM_DELTA 16
356 * The standard macro set: defined in macros.c in the array nasm_stdmac.
357 * This gives our position in the macro set, when we're processing it.
359 static const char * const *stdmacpos
;
362 * The extra standard macros that come from the object format, if
365 static const char * const *extrastdmac
= NULL
;
366 bool any_extrastdmac
;
369 * Tokens are allocated in blocks to improve speed
371 #define TOKEN_BLOCKSIZE 4096
372 static Token
*freeTokens
= NULL
;
378 static Blocks blocks
= { NULL
, NULL
};
381 * Forward declarations.
383 static Token
*expand_mmac_params(Token
* tline
);
384 static Token
*expand_smacro(Token
* tline
);
385 static Token
*expand_id(Token
* tline
);
386 static Context
*get_ctx(char *name
, bool all_contexts
);
387 static void make_tok_num(Token
* tok
, int64_t val
);
388 static void error(int severity
, const char *fmt
, ...);
389 static void *new_Block(size_t size
);
390 static void delete_Blocks(void);
391 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
392 static Token
*delete_Token(Token
* t
);
395 * Macros for safe checking of token pointers, avoid *(NULL)
397 #define tok_type_(x,t) ((x) && (x)->type == (t))
398 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
402 /* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
407 static char *check_tasm_directive(char *line
)
409 int32_t i
, j
, k
, m
, len
;
410 char *p
= line
, *oldline
, oldchar
;
412 /* Skip whitespace */
413 while (isspace(*p
) && *p
!= 0)
416 /* Binary search for the directive name */
418 j
= elements(tasm_directives
);
420 while (!isspace(p
[len
]) && p
[len
] != 0)
427 m
= nasm_stricmp(p
, tasm_directives
[k
]);
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
435 line
= nasm_malloc(len
+ 2);
437 if (k
== TM_IFDIFI
) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
443 strcpy(line
+ 1, "ifdef BOGUS");
445 memcpy(line
+ 1, p
, len
+ 1);
460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
465 static char *prepreproc(char *line
)
468 char *fname
, *oldline
;
470 if (line
[0] == '#' && line
[1] == ' ') {
473 lineno
= atoi(fname
);
474 fname
+= strspn(fname
, "0123456789 ");
477 fnlen
= strcspn(fname
, "\"");
478 line
= nasm_malloc(20 + fnlen
);
479 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
482 if (tasm_compatible_mode
)
483 return check_tasm_directive(line
);
488 * Free a linked list of tokens.
490 static void free_tlist(Token
* list
)
493 list
= delete_Token(list
);
498 * Free a linked list of lines.
500 static void free_llist(Line
* list
)
506 free_tlist(l
->first
);
514 static void free_mmacro(MMacro
* m
)
517 free_tlist(m
->dlist
);
518 nasm_free(m
->defaults
);
519 free_llist(m
->expansion
);
524 * Free all currently defined macros, and free the hash tables
526 static void free_smacro_table(struct hash_table
*smt
)
530 struct hash_tbl_node
*it
= NULL
;
532 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
533 nasm_free((void *)key
);
535 SMacro
*ns
= s
->next
;
537 free_tlist(s
->expansion
);
545 static void free_mmacro_table(struct hash_table
*mmt
)
549 struct hash_tbl_node
*it
= NULL
;
552 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
553 nasm_free((void *)key
);
555 MMacro
*nm
= m
->next
;
563 static void free_macros(void)
565 free_smacro_table(smacros
);
566 free_mmacro_table(mmacros
);
570 * Initialize the hash tables
572 static void init_macros(void)
574 smacros
= hash_init(HASH_LARGE
);
575 mmacros
= hash_init(HASH_LARGE
);
579 * Pop the context stack.
581 static void ctx_pop(void)
586 free_smacro_table(c
->localmac
);
592 * Search for a key in the hash index; adding it if necessary
593 * (in which case we initialize the data pointer to NULL.)
596 hash_findi_add(struct hash_table
*hash
, const char *str
)
598 struct hash_insert hi
;
602 r
= hash_findi(hash
, str
, &hi
);
606 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
607 return hash_add(&hi
, strx
, NULL
);
611 * Like hash_findi, but returns the data element rather than a pointer
612 * to it. Used only when not adding a new element, hence no third
616 hash_findix(struct hash_table
*hash
, const char *str
)
620 p
= hash_findi(hash
, str
, NULL
);
621 return p
? *p
: NULL
;
624 #define BUF_DELTA 512
626 * Read a line from the top file in istk, handling multiple CR/LFs
627 * at the end of the line read, and handling spurious ^Zs. Will
628 * return lines from the standard macro set if this has not already
631 static char *read_line(void)
633 char *buffer
, *p
, *q
;
634 int bufsize
, continued_count
;
638 char *ret
= nasm_strdup(*stdmacpos
++);
639 if (!*stdmacpos
&& any_extrastdmac
) {
640 stdmacpos
= extrastdmac
;
641 any_extrastdmac
= false;
645 * Nasty hack: here we push the contents of `predef' on
646 * to the top-level expansion stack, since this is the
647 * most convenient way to implement the pre-include and
648 * pre-define features.
652 Token
*head
, **tail
, *t
;
654 for (pd
= predef
; pd
; pd
= pd
->next
) {
657 for (t
= pd
->first
; t
; t
= t
->next
) {
658 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
659 tail
= &(*tail
)->next
;
661 l
= nasm_malloc(sizeof(Line
));
662 l
->next
= istk
->expansion
;
675 buffer
= nasm_malloc(BUF_DELTA
);
679 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
683 if (p
> buffer
&& p
[-1] == '\n') {
684 /* Convert backslash-CRLF line continuation sequences into
685 nothing at all (for DOS and Windows) */
686 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
691 /* Also convert backslash-LF line continuation sequences into
692 nothing at all (for Unix) */
693 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
701 if (p
- buffer
> bufsize
- 10) {
702 int32_t offset
= p
- buffer
;
703 bufsize
+= BUF_DELTA
;
704 buffer
= nasm_realloc(buffer
, bufsize
);
705 p
= buffer
+ offset
; /* prevent stale-pointer problems */
709 if (!q
&& p
== buffer
) {
714 src_set_linnum(src_get_linnum() + istk
->lineinc
+
715 (continued_count
* istk
->lineinc
));
718 * Play safe: remove CRs as well as LFs, if any of either are
719 * present at the end of the line.
721 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
725 * Handle spurious ^Z, which may be inserted into source files
726 * by some file transfer utilities.
728 buffer
[strcspn(buffer
, "\032")] = '\0';
730 list
->line(LIST_READ
, buffer
);
736 * Tokenize a line of text. This is a very simple process since we
737 * don't need to parse the value out of e.g. numeric tokens: we
738 * simply split one string into many.
740 static Token
*tokenize(char *line
)
743 enum pp_token_type type
;
745 Token
*t
, **tail
= &list
;
752 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
753 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
758 type
= TOK_PREPROC_ID
;
759 } else if (*p
== '{') {
761 while (*p
&& *p
!= '}') {
768 type
= TOK_PREPROC_ID
;
769 } else if (isidchar(*p
) ||
770 ((*p
== '!' || *p
== '%' || *p
== '$') &&
775 while (isidchar(*p
));
776 type
= TOK_PREPROC_ID
;
782 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
785 while (*p
&& isidchar(*p
))
787 } else if (*p
== '\'' || *p
== '"') {
794 while (*p
&& *p
!= c
)
800 error(ERR_WARNING
, "unterminated string");
801 /* Handling unterminated strings by UNV */
804 } else if (isnumstart(*p
)) {
806 bool is_float
= false;
822 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
824 if (*p
== '+' || *p
== '-') {
825 /* e can only be followed by +/- if it is either a
826 prefixed hex number or a floating-point number */
830 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
832 } else if (c
== 'P' || c
== 'p') {
834 if (*p
== '+' || *p
== '-')
836 } else if (isnumchar(c
) || c
== '_')
839 /* we need to deal with consequences of the legacy
840 parser, like "1.nolist" being two tokens
841 (TOK_NUMBER, TOK_ID) here; at least give it
842 a shot for now. In the future, we probably need
843 a flex-based scanner with proper pattern matching
844 to do it as well as it can be done. Nothing in
845 the world is going to help the person who wants
846 0x123.p16 interpreted as two tokens, though. */
851 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
852 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
853 (*r
== 'p' || *r
== 'P')) {
857 break; /* Terminate the token */
861 p
--; /* Point to first character beyond number */
863 if (has_e
&& !is_hex
) {
864 /* 1e13 is floating-point, but 1e13h is not */
868 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
869 } else if (isspace(*p
)) {
870 type
= TOK_WHITESPACE
;
872 while (*p
&& isspace(*p
))
875 * Whitespace just before end-of-line is discarded by
876 * pretending it's a comment; whitespace just before a
877 * comment gets lumped into the comment.
879 if (!*p
|| *p
== ';') {
884 } else if (*p
== ';') {
890 * Anything else is an operator of some kind. We check
891 * for all the double-character operators (>>, <<, //,
892 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
893 * else is a single-character operator.
896 if ((p
[0] == '>' && p
[1] == '>') ||
897 (p
[0] == '<' && p
[1] == '<') ||
898 (p
[0] == '/' && p
[1] == '/') ||
899 (p
[0] == '<' && p
[1] == '=') ||
900 (p
[0] == '>' && p
[1] == '=') ||
901 (p
[0] == '=' && p
[1] == '=') ||
902 (p
[0] == '!' && p
[1] == '=') ||
903 (p
[0] == '<' && p
[1] == '>') ||
904 (p
[0] == '&' && p
[1] == '&') ||
905 (p
[0] == '|' && p
[1] == '|') ||
906 (p
[0] == '^' && p
[1] == '^')) {
912 /* Handling unterminated string by UNV */
915 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
916 t->text[p-line] = *line;
920 if (type
!= TOK_COMMENT
) {
921 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
930 * this function allocates a new managed block of memory and
931 * returns a pointer to the block. The managed blocks are
932 * deleted only all at once by the delete_Blocks function.
934 static void *new_Block(size_t size
)
938 /* first, get to the end of the linked list */
941 /* now allocate the requested chunk */
942 b
->chunk
= nasm_malloc(size
);
944 /* now allocate a new block for the next request */
945 b
->next
= nasm_malloc(sizeof(Blocks
));
946 /* and initialize the contents of the new block */
947 b
->next
->next
= NULL
;
948 b
->next
->chunk
= NULL
;
953 * this function deletes all managed blocks of memory
955 static void delete_Blocks(void)
957 Blocks
*a
, *b
= &blocks
;
960 * keep in mind that the first block, pointed to by blocks
961 * is a static and not dynamically allocated, so we don't
975 * this function creates a new Token and passes a pointer to it
976 * back to the caller. It sets the type and text elements, and
977 * also the mac and next elements to NULL.
979 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
984 if (freeTokens
== NULL
) {
985 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
986 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
987 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
988 freeTokens
[i
].next
= NULL
;
991 freeTokens
= t
->next
;
995 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
999 txtlen
= strlen(text
);
1000 t
->text
= nasm_malloc(1 + txtlen
);
1001 strncpy(t
->text
, text
, txtlen
);
1002 t
->text
[txtlen
] = '\0';
1007 static Token
*delete_Token(Token
* t
)
1009 Token
*next
= t
->next
;
1011 t
->next
= freeTokens
;
1017 * Convert a line of tokens back into text.
1018 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1019 * will be transformed into ..@ctxnum.xxx
1021 static char *detoken(Token
* tlist
, int expand_locals
)
1029 for (t
= tlist
; t
; t
= t
->next
) {
1030 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1031 char *p
= getenv(t
->text
+ 2);
1034 t
->text
= nasm_strdup(p
);
1038 /* Expand local macros here and not during preprocessing */
1039 if (expand_locals
&&
1040 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1041 t
->text
[0] == '%' && t
->text
[1] == '$') {
1042 Context
*ctx
= get_ctx(t
->text
, false);
1045 char *p
, *q
= t
->text
+ 2;
1047 q
+= strspn(q
, "$");
1048 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1049 p
= nasm_strcat(buffer
, q
);
1054 if (t
->type
== TOK_WHITESPACE
) {
1056 } else if (t
->text
) {
1057 len
+= strlen(t
->text
);
1060 p
= line
= nasm_malloc(len
+ 1);
1061 for (t
= tlist
; t
; t
= t
->next
) {
1062 if (t
->type
== TOK_WHITESPACE
) {
1064 } else if (t
->text
) {
1075 * A scanner, suitable for use by the expression evaluator, which
1076 * operates on a line of Tokens. Expects a pointer to a pointer to
1077 * the first token in the line to be passed in as its private_data
1080 * FIX: This really needs to be unified with stdscan.
1082 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1084 Token
**tlineptr
= private_data
;
1086 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1090 *tlineptr
= tline
? tline
->next
: NULL
;
1092 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1093 tline
->type
== TOK_COMMENT
));
1096 return tokval
->t_type
= TOKEN_EOS
;
1098 tokval
->t_charptr
= tline
->text
;
1100 if (tline
->text
[0] == '$' && !tline
->text
[1])
1101 return tokval
->t_type
= TOKEN_HERE
;
1102 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1103 return tokval
->t_type
= TOKEN_BASE
;
1105 if (tline
->type
== TOK_ID
) {
1106 p
= tokval
->t_charptr
= tline
->text
;
1108 tokval
->t_charptr
++;
1109 return tokval
->t_type
= TOKEN_ID
;
1112 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1113 if (r
>= p
+MAX_KEYWORD
)
1114 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1118 /* right, so we have an identifier sitting in temp storage. now,
1119 * is it actually a register or instruction name, or what? */
1120 return nasm_token_hash(ourcopy
, tokval
);
1123 if (tline
->type
== TOK_NUMBER
) {
1125 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1127 return tokval
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
1128 tokval
->t_charptr
= tline
->text
;
1129 return tokval
->t_type
= TOKEN_NUM
;
1132 if (tline
->type
== TOK_FLOAT
) {
1133 return tokval
->t_type
= TOKEN_FLOAT
;
1136 if (tline
->type
== TOK_STRING
) {
1145 if (l
== 0 || r
[l
- 1] != q
)
1146 return tokval
->t_type
= TOKEN_ERRNUM
;
1147 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1149 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1150 tokval
->t_charptr
= NULL
;
1151 return tokval
->t_type
= TOKEN_NUM
;
1154 if (tline
->type
== TOK_OTHER
) {
1155 if (!strcmp(tline
->text
, "<<"))
1156 return tokval
->t_type
= TOKEN_SHL
;
1157 if (!strcmp(tline
->text
, ">>"))
1158 return tokval
->t_type
= TOKEN_SHR
;
1159 if (!strcmp(tline
->text
, "//"))
1160 return tokval
->t_type
= TOKEN_SDIV
;
1161 if (!strcmp(tline
->text
, "%%"))
1162 return tokval
->t_type
= TOKEN_SMOD
;
1163 if (!strcmp(tline
->text
, "=="))
1164 return tokval
->t_type
= TOKEN_EQ
;
1165 if (!strcmp(tline
->text
, "<>"))
1166 return tokval
->t_type
= TOKEN_NE
;
1167 if (!strcmp(tline
->text
, "!="))
1168 return tokval
->t_type
= TOKEN_NE
;
1169 if (!strcmp(tline
->text
, "<="))
1170 return tokval
->t_type
= TOKEN_LE
;
1171 if (!strcmp(tline
->text
, ">="))
1172 return tokval
->t_type
= TOKEN_GE
;
1173 if (!strcmp(tline
->text
, "&&"))
1174 return tokval
->t_type
= TOKEN_DBL_AND
;
1175 if (!strcmp(tline
->text
, "^^"))
1176 return tokval
->t_type
= TOKEN_DBL_XOR
;
1177 if (!strcmp(tline
->text
, "||"))
1178 return tokval
->t_type
= TOKEN_DBL_OR
;
1182 * We have no other options: just return the first character of
1185 return tokval
->t_type
= tline
->text
[0];
1189 * Compare a string to the name of an existing macro; this is a
1190 * simple wrapper which calls either strcmp or nasm_stricmp
1191 * depending on the value of the `casesense' parameter.
1193 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1195 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1199 * Return the Context structure associated with a %$ token. Return
1200 * NULL, having _already_ reported an error condition, if the
1201 * context stack isn't deep enough for the supplied number of $
1203 * If all_contexts == true, contexts that enclose current are
1204 * also scanned for such smacro, until it is found; if not -
1205 * only the context that directly results from the number of $'s
1206 * in variable's name.
1208 static Context
*get_ctx(char *name
, bool all_contexts
)
1214 if (!name
|| name
[0] != '%' || name
[1] != '$')
1218 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1222 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1224 /* i--; Lino - 02/25/02 */
1227 error(ERR_NONFATAL
, "`%s': context stack is only"
1228 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1235 /* Search for this smacro in found context */
1236 m
= hash_findix(ctx
->localmac
, name
);
1238 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1249 * Open an include file. This routine must always return a valid
1250 * file pointer if it returns - it's responsible for throwing an
1251 * ERR_FATAL and bombing out completely if not. It should also try
1252 * the include path one by one until it finds the file or reaches
1253 * the end of the path.
1255 static FILE *inc_fopen(char *file
)
1258 char *prefix
= "", *combine
;
1259 IncPath
*ip
= ipath
;
1260 static int namelen
= 0;
1261 int len
= strlen(file
);
1264 combine
= nasm_malloc(strlen(prefix
) + len
+ 1);
1265 strcpy(combine
, prefix
);
1266 strcat(combine
, file
);
1267 fp
= fopen(combine
, "r");
1268 if (pass
== 0 && fp
) {
1269 namelen
+= strlen(combine
) + 1;
1274 printf(" %s", combine
);
1285 /* -MG given and file not found */
1287 namelen
+= strlen(file
) + 1;
1292 printf(" %s", file
);
1298 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1299 return NULL
; /* never reached - placate compilers */
1303 * Determine if we should warn on defining a single-line macro of
1304 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1305 * return true if _any_ single-line macro of that name is defined.
1306 * Otherwise, will return true if a single-line macro with either
1307 * `nparam' or no parameters is defined.
1309 * If a macro with precisely the right number of parameters is
1310 * defined, or nparam is -1, the address of the definition structure
1311 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1312 * is NULL, no action will be taken regarding its contents, and no
1315 * Note that this is also called with nparam zero to resolve
1318 * If you already know which context macro belongs to, you can pass
1319 * the context pointer as first parameter; if you won't but name begins
1320 * with %$ the context will be automatically computed. If all_contexts
1321 * is true, macro will be searched in outer contexts as well.
1324 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1330 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1331 } else if (name
[0] == '%' && name
[1] == '$') {
1333 ctx
= get_ctx(name
, false);
1335 return false; /* got to return _something_ */
1336 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1338 m
= (SMacro
*) hash_findix(smacros
, name
);
1342 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1343 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1345 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1359 * Count and mark off the parameters in a multi-line macro call.
1360 * This is called both from within the multi-line macro expansion
1361 * code, and also to mark off the default parameters when provided
1362 * in a %macro definition line.
1364 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1366 int paramsize
, brace
;
1368 *nparam
= paramsize
= 0;
1371 if (*nparam
>= paramsize
) {
1372 paramsize
+= PARAM_DELTA
;
1373 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1377 if (tok_is_(t
, "{"))
1379 (*params
)[(*nparam
)++] = t
;
1380 while (tok_isnt_(t
, brace
? "}" : ","))
1382 if (t
) { /* got a comma/brace */
1386 * Now we've found the closing brace, look further
1390 if (tok_isnt_(t
, ",")) {
1392 "braces do not enclose all of macro parameter");
1393 while (tok_isnt_(t
, ","))
1397 t
= t
->next
; /* eat the comma */
1404 * Determine whether one of the various `if' conditions is true or
1407 * We must free the tline we get passed.
1409 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1411 enum pp_conditional i
= PP_COND(ct
);
1413 Token
*t
, *tt
, **tptr
, *origline
;
1414 struct tokenval tokval
;
1416 enum pp_token_type needtype
;
1422 j
= false; /* have we matched yet? */
1423 while (cstk
&& tline
) {
1425 if (!tline
|| tline
->type
!= TOK_ID
) {
1427 "`%s' expects context identifiers", pp_directives
[ct
]);
1428 free_tlist(origline
);
1431 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1433 tline
= tline
->next
;
1438 j
= false; /* have we matched yet? */
1441 if (!tline
|| (tline
->type
!= TOK_ID
&&
1442 (tline
->type
!= TOK_PREPROC_ID
||
1443 tline
->text
[1] != '$'))) {
1445 "`%s' expects macro identifiers", pp_directives
[ct
]);
1448 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1450 tline
= tline
->next
;
1456 tline
= expand_smacro(tline
);
1458 while (tok_isnt_(tt
, ","))
1462 "`%s' expects two comma-separated arguments",
1467 j
= true; /* assume equality unless proved not */
1468 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1469 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1470 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1474 if (t
->type
== TOK_WHITESPACE
) {
1478 if (tt
->type
== TOK_WHITESPACE
) {
1482 if (tt
->type
!= t
->type
) {
1483 j
= false; /* found mismatching tokens */
1486 /* Unify surrounding quotes for strings */
1487 if (t
->type
== TOK_STRING
) {
1488 tt
->text
[0] = t
->text
[0];
1489 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1491 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1492 j
= false; /* found mismatching tokens */
1499 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1500 j
= false; /* trailing gunk on one end or other */
1506 MMacro searching
, *mmac
;
1508 tline
= tline
->next
;
1510 tline
= expand_id(tline
);
1511 if (!tok_type_(tline
, TOK_ID
)) {
1513 "`%s' expects a macro name", pp_directives
[ct
]);
1516 searching
.name
= nasm_strdup(tline
->text
);
1517 searching
.casesense
= true;
1518 searching
.plus
= false;
1519 searching
.nolist
= false;
1520 searching
.in_progress
= 0;
1521 searching
.rep_nest
= NULL
;
1522 searching
.nparam_min
= 0;
1523 searching
.nparam_max
= INT_MAX
;
1524 tline
= expand_smacro(tline
->next
);
1527 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1529 "`%s' expects a parameter count or nothing",
1532 searching
.nparam_min
= searching
.nparam_max
=
1533 readnum(tline
->text
, &j
);
1536 "unable to parse parameter count `%s'",
1539 if (tline
&& tok_is_(tline
->next
, "-")) {
1540 tline
= tline
->next
->next
;
1541 if (tok_is_(tline
, "*"))
1542 searching
.nparam_max
= INT_MAX
;
1543 else if (!tok_type_(tline
, TOK_NUMBER
))
1545 "`%s' expects a parameter count after `-'",
1548 searching
.nparam_max
= readnum(tline
->text
, &j
);
1551 "unable to parse parameter count `%s'",
1553 if (searching
.nparam_min
> searching
.nparam_max
)
1555 "minimum parameter count exceeds maximum");
1558 if (tline
&& tok_is_(tline
->next
, "+")) {
1559 tline
= tline
->next
;
1560 searching
.plus
= true;
1562 mmac
= (MMacro
*) hash_findix(mmacros
, searching
.name
);
1564 if (!strcmp(mmac
->name
, searching
.name
) &&
1565 (mmac
->nparam_min
<= searching
.nparam_max
1567 && (searching
.nparam_min
<= mmac
->nparam_max
1574 nasm_free(searching
.name
);
1583 needtype
= TOK_NUMBER
;
1586 needtype
= TOK_STRING
;
1590 t
= tline
= expand_smacro(tline
);
1592 while (tok_type_(t
, TOK_WHITESPACE
) ||
1593 (needtype
== TOK_NUMBER
&&
1594 tok_type_(t
, TOK_OTHER
) &&
1595 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1599 j
= tok_type_(t
, needtype
);
1603 t
= tline
= expand_smacro(tline
);
1604 while (tok_type_(t
, TOK_WHITESPACE
))
1609 t
= t
->next
; /* Skip the actual token */
1610 while (tok_type_(t
, TOK_WHITESPACE
))
1612 j
= !t
; /* Should be nothing left */
1617 t
= tline
= expand_smacro(tline
);
1618 while (tok_type_(t
, TOK_WHITESPACE
))
1621 j
= !t
; /* Should be empty */
1625 t
= tline
= expand_smacro(tline
);
1627 tokval
.t_type
= TOKEN_INVALID
;
1628 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1629 NULL
, pass
| CRITICAL
, error
, NULL
);
1634 "trailing garbage after expression ignored");
1635 if (!is_simple(evalresult
)) {
1637 "non-constant value given to `%s'", pp_directives
[ct
]);
1640 j
= reloc_value(evalresult
) != 0;
1645 "preprocessor directive `%s' not yet implemented",
1650 free_tlist(origline
);
1651 return j
^ PP_NEGATIVE(ct
);
1654 free_tlist(origline
);
1659 * Expand macros in a string. Used in %error and %include directives.
1660 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1661 * The returned variable should ALWAYS be freed after usage.
1663 void expand_macros_in_string(char **p
)
1665 Token
*line
= tokenize(*p
);
1666 line
= expand_smacro(line
);
1667 *p
= detoken(line
, false);
1671 * Common code for defining an smacro
1673 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1674 int nparam
, Token
*expansion
)
1676 SMacro
*smac
, **smhead
;
1678 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1681 "single-line macro `%s' defined both with and"
1682 " without parameters", mname
);
1684 /* Some instances of the old code considered this a failure,
1685 some others didn't. What is the right thing to do here? */
1686 free_tlist(expansion
);
1687 return false; /* Failure */
1690 * We're redefining, so we have to take over an
1691 * existing SMacro structure. This means freeing
1692 * what was already in it.
1694 nasm_free(smac
->name
);
1695 free_tlist(smac
->expansion
);
1698 smhead
= (SMacro
**) hash_findi_add(ctx
? ctx
->localmac
: smacros
,
1700 smac
= nasm_malloc(sizeof(SMacro
));
1701 smac
->next
= *smhead
;
1704 smac
->name
= nasm_strdup(mname
);
1705 smac
->casesense
= casesense
;
1706 smac
->nparam
= nparam
;
1707 smac
->expansion
= expansion
;
1708 smac
->in_progress
= false;
1709 return true; /* Success */
1713 * Undefine an smacro
1715 static void undef_smacro(Context
*ctx
, const char *mname
)
1717 SMacro
**smhead
, *s
, **sp
;
1719 smhead
= (SMacro
**)hash_findi(ctx
? ctx
->localmac
: smacros
, mname
, NULL
);
1723 * We now have a macro name... go hunt for it.
1726 while ((s
= *sp
) != NULL
) {
1727 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1730 free_tlist(s
->expansion
);
1740 * Decode a size directive
1742 static int parse_size(const char *str
) {
1743 static const char *size_names
[] =
1744 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1745 static const int sizes
[] =
1746 { 0, 1, 4, 16, 8, 10, 2, 32 };
1748 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1752 * find and process preprocessor directive in passed line
1753 * Find out if a line contains a preprocessor directive, and deal
1756 * If a directive _is_ found, it is the responsibility of this routine
1757 * (and not the caller) to free_tlist() the line.
1759 * @param tline a pointer to the current tokeninzed line linked list
1760 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1763 static int do_directive(Token
* tline
)
1765 enum preproc_token i
;
1777 MMacro
*mmac
, **mmhead
;
1778 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1780 struct tokenval tokval
;
1782 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1788 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1789 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1790 || tline
->text
[1] == '!'))
1791 return NO_DIRECTIVE_FOUND
;
1793 i
= pp_token_hash(tline
->text
);
1796 * If we're in a non-emitting branch of a condition construct,
1797 * or walking to the end of an already terminated %rep block,
1798 * we should ignore all directives except for condition
1801 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1802 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1803 return NO_DIRECTIVE_FOUND
;
1807 * If we're defining a macro or reading a %rep block, we should
1808 * ignore all directives except for %macro/%imacro (which
1809 * generate an error), %endm/%endmacro, and (only if we're in a
1810 * %rep block) %endrep. If we're in a %rep block, another %rep
1811 * causes an error, so should be let through.
1813 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1814 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1815 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1816 return NO_DIRECTIVE_FOUND
;
1821 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1823 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1826 /* Directive to tell NASM what the default stack size is. The
1827 * default is for a 16-bit stack, and this can be overriden with
1829 * the following form:
1831 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1833 tline
= tline
->next
;
1834 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1835 tline
= tline
->next
;
1836 if (!tline
|| tline
->type
!= TOK_ID
) {
1837 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1838 free_tlist(origline
);
1839 return DIRECTIVE_FOUND
;
1841 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1842 /* All subsequent ARG directives are for a 32-bit stack */
1844 StackPointer
= "ebp";
1847 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1848 /* All subsequent ARG directives are for a 64-bit stack */
1850 StackPointer
= "rbp";
1853 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1854 /* All subsequent ARG directives are for a 16-bit stack,
1855 * far function call.
1858 StackPointer
= "bp";
1861 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1862 /* All subsequent ARG directives are for a 16-bit stack,
1863 * far function call. We don't support near functions.
1866 StackPointer
= "bp";
1870 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1871 free_tlist(origline
);
1872 return DIRECTIVE_FOUND
;
1874 free_tlist(origline
);
1875 return DIRECTIVE_FOUND
;
1878 /* TASM like ARG directive to define arguments to functions, in
1879 * the following form:
1881 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1885 char *arg
, directive
[256];
1886 int size
= StackSize
;
1888 /* Find the argument name */
1889 tline
= tline
->next
;
1890 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1891 tline
= tline
->next
;
1892 if (!tline
|| tline
->type
!= TOK_ID
) {
1893 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1894 free_tlist(origline
);
1895 return DIRECTIVE_FOUND
;
1899 /* Find the argument size type */
1900 tline
= tline
->next
;
1901 if (!tline
|| tline
->type
!= TOK_OTHER
1902 || tline
->text
[0] != ':') {
1904 "Syntax error processing `%%arg' directive");
1905 free_tlist(origline
);
1906 return DIRECTIVE_FOUND
;
1908 tline
= tline
->next
;
1909 if (!tline
|| tline
->type
!= TOK_ID
) {
1910 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1911 free_tlist(origline
);
1912 return DIRECTIVE_FOUND
;
1915 /* Allow macro expansion of type parameter */
1916 tt
= tokenize(tline
->text
);
1917 tt
= expand_smacro(tt
);
1918 size
= parse_size(tt
->text
);
1921 "Invalid size type for `%%arg' missing directive");
1923 free_tlist(origline
);
1924 return DIRECTIVE_FOUND
;
1928 /* Round up to even stack slots */
1929 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1931 /* Now define the macro for the argument */
1932 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1933 arg
, StackPointer
, offset
);
1934 do_directive(tokenize(directive
));
1937 /* Move to the next argument in the list */
1938 tline
= tline
->next
;
1939 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1940 tline
= tline
->next
;
1941 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1943 free_tlist(origline
);
1944 return DIRECTIVE_FOUND
;
1947 /* TASM like LOCAL directive to define local variables for a
1948 * function, in the following form:
1950 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1952 * The '= LocalSize' at the end is ignored by NASM, but is
1953 * required by TASM to define the local parameter size (and used
1954 * by the TASM macro package).
1956 offset
= LocalOffset
;
1958 char *local
, directive
[256];
1959 int size
= StackSize
;
1961 /* Find the argument name */
1962 tline
= tline
->next
;
1963 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1964 tline
= tline
->next
;
1965 if (!tline
|| tline
->type
!= TOK_ID
) {
1967 "`%%local' missing argument parameter");
1968 free_tlist(origline
);
1969 return DIRECTIVE_FOUND
;
1971 local
= tline
->text
;
1973 /* Find the argument size type */
1974 tline
= tline
->next
;
1975 if (!tline
|| tline
->type
!= TOK_OTHER
1976 || tline
->text
[0] != ':') {
1978 "Syntax error processing `%%local' directive");
1979 free_tlist(origline
);
1980 return DIRECTIVE_FOUND
;
1982 tline
= tline
->next
;
1983 if (!tline
|| tline
->type
!= TOK_ID
) {
1985 "`%%local' missing size type parameter");
1986 free_tlist(origline
);
1987 return DIRECTIVE_FOUND
;
1990 /* Allow macro expansion of type parameter */
1991 tt
= tokenize(tline
->text
);
1992 tt
= expand_smacro(tt
);
1993 size
= parse_size(tt
->text
);
1996 "Invalid size type for `%%local' missing directive");
1998 free_tlist(origline
);
1999 return DIRECTIVE_FOUND
;
2003 /* Round up to even stack slots */
2004 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2006 offset
+= size
; /* Negative offset, increment before */
2008 /* Now define the macro for the argument */
2009 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2010 local
, StackPointer
, offset
);
2011 do_directive(tokenize(directive
));
2013 /* Now define the assign to setup the enter_c macro correctly */
2014 snprintf(directive
, sizeof(directive
),
2015 "%%assign %%$localsize %%$localsize+%d", size
);
2016 do_directive(tokenize(directive
));
2018 /* Move to the next argument in the list */
2019 tline
= tline
->next
;
2020 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2021 tline
= tline
->next
;
2022 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2023 LocalOffset
= offset
;
2024 free_tlist(origline
);
2025 return DIRECTIVE_FOUND
;
2029 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2032 free_tlist(origline
);
2033 return DIRECTIVE_FOUND
;
2036 tline
= tline
->next
;
2038 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2039 tline
->type
!= TOK_INTERNAL_STRING
)) {
2040 error(ERR_NONFATAL
, "`%%include' expects a file name");
2041 free_tlist(origline
);
2042 return DIRECTIVE_FOUND
; /* but we did _something_ */
2046 "trailing garbage after `%%include' ignored");
2047 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2048 p
= tline
->text
+ 1; /* point past the quote to the name */
2049 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2051 p
= tline
->text
; /* internal_string is easier */
2052 expand_macros_in_string(&p
);
2053 inc
= nasm_malloc(sizeof(Include
));
2056 inc
->fp
= inc_fopen(p
);
2057 if (!inc
->fp
&& pass
== 0) {
2058 /* -MG given but file not found */
2061 inc
->fname
= src_set_fname(p
);
2062 inc
->lineno
= src_set_linnum(0);
2064 inc
->expansion
= NULL
;
2067 list
->uplevel(LIST_INCLUDE
);
2069 free_tlist(origline
);
2070 return DIRECTIVE_FOUND
;
2073 tline
= tline
->next
;
2075 tline
= expand_id(tline
);
2076 if (!tok_type_(tline
, TOK_ID
)) {
2077 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2078 free_tlist(origline
);
2079 return DIRECTIVE_FOUND
; /* but we did _something_ */
2082 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2083 ctx
= nasm_malloc(sizeof(Context
));
2085 ctx
->localmac
= hash_init(HASH_SMALL
);
2086 ctx
->name
= nasm_strdup(tline
->text
);
2087 ctx
->number
= unique
++;
2089 free_tlist(origline
);
2093 tline
= tline
->next
;
2095 tline
= expand_id(tline
);
2096 if (!tok_type_(tline
, TOK_ID
)) {
2097 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2098 free_tlist(origline
);
2099 return DIRECTIVE_FOUND
; /* but we did _something_ */
2102 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2104 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2106 nasm_free(cstk
->name
);
2107 cstk
->name
= nasm_strdup(tline
->text
);
2109 free_tlist(origline
);
2114 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2116 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2119 free_tlist(origline
);
2123 tline
->next
= expand_smacro(tline
->next
);
2124 tline
= tline
->next
;
2126 if (tok_type_(tline
, TOK_STRING
)) {
2127 p
= tline
->text
+ 1; /* point past the quote to the name */
2128 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2129 expand_macros_in_string(&p
);
2130 error(ERR_NONFATAL
, "%s", p
);
2133 p
= detoken(tline
, false);
2134 error(ERR_WARNING
, "%s", p
);
2137 free_tlist(origline
);
2141 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2144 j
= if_condition(tline
->next
, i
);
2145 tline
->next
= NULL
; /* it got freed */
2146 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2148 cond
= nasm_malloc(sizeof(Cond
));
2149 cond
->next
= istk
->conds
;
2152 free_tlist(origline
);
2153 return DIRECTIVE_FOUND
;
2157 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2158 if (emitting(istk
->conds
->state
)
2159 || istk
->conds
->state
== COND_NEVER
)
2160 istk
->conds
->state
= COND_NEVER
;
2163 * IMPORTANT: In the case of %if, we will already have
2164 * called expand_mmac_params(); however, if we're
2165 * processing an %elif we must have been in a
2166 * non-emitting mode, which would have inhibited
2167 * the normal invocation of expand_mmac_params(). Therefore,
2168 * we have to do it explicitly here.
2170 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2171 tline
->next
= NULL
; /* it got freed */
2172 istk
->conds
->state
=
2173 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2175 free_tlist(origline
);
2176 return DIRECTIVE_FOUND
;
2180 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2182 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2183 if (emitting(istk
->conds
->state
)
2184 || istk
->conds
->state
== COND_NEVER
)
2185 istk
->conds
->state
= COND_ELSE_FALSE
;
2187 istk
->conds
->state
= COND_ELSE_TRUE
;
2188 free_tlist(origline
);
2189 return DIRECTIVE_FOUND
;
2193 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2195 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2197 istk
->conds
= cond
->next
;
2199 free_tlist(origline
);
2200 return DIRECTIVE_FOUND
;
2206 "`%%%smacro': already defining a macro",
2207 (i
== PP_IMACRO
? "i" : ""));
2208 tline
= tline
->next
;
2210 tline
= expand_id(tline
);
2211 if (!tok_type_(tline
, TOK_ID
)) {
2213 "`%%%smacro' expects a macro name",
2214 (i
== PP_IMACRO
? "i" : ""));
2215 return DIRECTIVE_FOUND
;
2217 defining
= nasm_malloc(sizeof(MMacro
));
2218 defining
->name
= nasm_strdup(tline
->text
);
2219 defining
->casesense
= (i
== PP_MACRO
);
2220 defining
->plus
= false;
2221 defining
->nolist
= false;
2222 defining
->in_progress
= 0;
2223 defining
->rep_nest
= NULL
;
2224 tline
= expand_smacro(tline
->next
);
2226 if (!tok_type_(tline
, TOK_NUMBER
)) {
2228 "`%%%smacro' expects a parameter count",
2229 (i
== PP_IMACRO
? "i" : ""));
2230 defining
->nparam_min
= defining
->nparam_max
= 0;
2232 defining
->nparam_min
= defining
->nparam_max
=
2233 readnum(tline
->text
, &err
);
2236 "unable to parse parameter count `%s'", tline
->text
);
2238 if (tline
&& tok_is_(tline
->next
, "-")) {
2239 tline
= tline
->next
->next
;
2240 if (tok_is_(tline
, "*"))
2241 defining
->nparam_max
= INT_MAX
;
2242 else if (!tok_type_(tline
, TOK_NUMBER
))
2244 "`%%%smacro' expects a parameter count after `-'",
2245 (i
== PP_IMACRO
? "i" : ""));
2247 defining
->nparam_max
= readnum(tline
->text
, &err
);
2250 "unable to parse parameter count `%s'",
2252 if (defining
->nparam_min
> defining
->nparam_max
)
2254 "minimum parameter count exceeds maximum");
2257 if (tline
&& tok_is_(tline
->next
, "+")) {
2258 tline
= tline
->next
;
2259 defining
->plus
= true;
2261 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2262 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2263 tline
= tline
->next
;
2264 defining
->nolist
= true;
2266 mmac
= (MMacro
*) hash_findix(mmacros
, defining
->name
);
2268 if (!strcmp(mmac
->name
, defining
->name
) &&
2269 (mmac
->nparam_min
<= defining
->nparam_max
2271 && (defining
->nparam_min
<= mmac
->nparam_max
2274 "redefining multi-line macro `%s'", defining
->name
);
2280 * Handle default parameters.
2282 if (tline
&& tline
->next
) {
2283 defining
->dlist
= tline
->next
;
2285 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2286 &defining
->defaults
);
2288 defining
->dlist
= NULL
;
2289 defining
->defaults
= NULL
;
2291 defining
->expansion
= NULL
;
2292 free_tlist(origline
);
2293 return DIRECTIVE_FOUND
;
2298 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2299 return DIRECTIVE_FOUND
;
2301 mmhead
= (MMacro
**) hash_findi_add(mmacros
, defining
->name
);
2302 defining
->next
= *mmhead
;
2305 free_tlist(origline
);
2306 return DIRECTIVE_FOUND
;
2309 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2310 tline
= tline
->next
;
2311 if (tline
->next
== NULL
) {
2312 free_tlist(origline
);
2313 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2314 return DIRECTIVE_FOUND
;
2316 t
= expand_smacro(tline
->next
);
2318 free_tlist(origline
);
2321 tokval
.t_type
= TOKEN_INVALID
;
2323 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2326 return DIRECTIVE_FOUND
;
2329 "trailing garbage after expression ignored");
2330 if (!is_simple(evalresult
)) {
2331 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2332 return DIRECTIVE_FOUND
;
2335 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2336 mmac
= mmac
->next_active
;
2338 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2339 } else if (mmac
->nparam
== 0) {
2341 "`%%rotate' invoked within macro without parameters");
2343 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2345 rotate
%= (int)mmac
->nparam
;
2347 rotate
+= mmac
->nparam
;
2349 mmac
->rotate
= rotate
;
2351 return DIRECTIVE_FOUND
;
2356 tline
= tline
->next
;
2357 } while (tok_type_(tline
, TOK_WHITESPACE
));
2359 if (tok_type_(tline
, TOK_ID
) &&
2360 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2363 tline
= tline
->next
;
2364 } while (tok_type_(tline
, TOK_WHITESPACE
));
2368 t
= expand_smacro(tline
);
2370 tokval
.t_type
= TOKEN_INVALID
;
2372 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2374 free_tlist(origline
);
2375 return DIRECTIVE_FOUND
;
2379 "trailing garbage after expression ignored");
2380 if (!is_simple(evalresult
)) {
2381 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2382 return DIRECTIVE_FOUND
;
2384 count
= reloc_value(evalresult
) + 1;
2386 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2389 free_tlist(origline
);
2391 tmp_defining
= defining
;
2392 defining
= nasm_malloc(sizeof(MMacro
));
2393 defining
->name
= NULL
; /* flags this macro as a %rep block */
2394 defining
->casesense
= false;
2395 defining
->plus
= false;
2396 defining
->nolist
= nolist
;
2397 defining
->in_progress
= count
;
2398 defining
->nparam_min
= defining
->nparam_max
= 0;
2399 defining
->defaults
= NULL
;
2400 defining
->dlist
= NULL
;
2401 defining
->expansion
= NULL
;
2402 defining
->next_active
= istk
->mstk
;
2403 defining
->rep_nest
= tmp_defining
;
2404 return DIRECTIVE_FOUND
;
2407 if (!defining
|| defining
->name
) {
2408 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2409 return DIRECTIVE_FOUND
;
2413 * Now we have a "macro" defined - although it has no name
2414 * and we won't be entering it in the hash tables - we must
2415 * push a macro-end marker for it on to istk->expansion.
2416 * After that, it will take care of propagating itself (a
2417 * macro-end marker line for a macro which is really a %rep
2418 * block will cause the macro to be re-expanded, complete
2419 * with another macro-end marker to ensure the process
2420 * continues) until the whole expansion is forcibly removed
2421 * from istk->expansion by a %exitrep.
2423 l
= nasm_malloc(sizeof(Line
));
2424 l
->next
= istk
->expansion
;
2425 l
->finishes
= defining
;
2427 istk
->expansion
= l
;
2429 istk
->mstk
= defining
;
2431 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2432 tmp_defining
= defining
;
2433 defining
= defining
->rep_nest
;
2434 free_tlist(origline
);
2435 return DIRECTIVE_FOUND
;
2439 * We must search along istk->expansion until we hit a
2440 * macro-end marker for a macro with no name. Then we set
2441 * its `in_progress' flag to 0.
2443 for (l
= istk
->expansion
; l
; l
= l
->next
)
2444 if (l
->finishes
&& !l
->finishes
->name
)
2448 l
->finishes
->in_progress
= 0;
2450 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2451 free_tlist(origline
);
2452 return DIRECTIVE_FOUND
;
2458 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2460 tline
= tline
->next
;
2462 tline
= expand_id(tline
);
2463 if (!tline
|| (tline
->type
!= TOK_ID
&&
2464 (tline
->type
!= TOK_PREPROC_ID
||
2465 tline
->text
[1] != '$'))) {
2466 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2468 free_tlist(origline
);
2469 return DIRECTIVE_FOUND
;
2472 ctx
= get_ctx(tline
->text
, false);
2474 mname
= tline
->text
;
2476 param_start
= tline
= tline
->next
;
2479 /* Expand the macro definition now for %xdefine and %ixdefine */
2480 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2481 tline
= expand_smacro(tline
);
2483 if (tok_is_(tline
, "(")) {
2485 * This macro has parameters.
2488 tline
= tline
->next
;
2492 error(ERR_NONFATAL
, "parameter identifier expected");
2493 free_tlist(origline
);
2494 return DIRECTIVE_FOUND
;
2496 if (tline
->type
!= TOK_ID
) {
2498 "`%s': parameter identifier expected",
2500 free_tlist(origline
);
2501 return DIRECTIVE_FOUND
;
2503 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2504 tline
= tline
->next
;
2506 if (tok_is_(tline
, ",")) {
2507 tline
= tline
->next
;
2510 if (!tok_is_(tline
, ")")) {
2512 "`)' expected to terminate macro template");
2513 free_tlist(origline
);
2514 return DIRECTIVE_FOUND
;
2519 tline
= tline
->next
;
2521 if (tok_type_(tline
, TOK_WHITESPACE
))
2522 last
= tline
, tline
= tline
->next
;
2527 if (t
->type
== TOK_ID
) {
2528 for (tt
= param_start
; tt
; tt
= tt
->next
)
2529 if (tt
->type
>= TOK_SMAC_PARAM
&&
2530 !strcmp(tt
->text
, t
->text
))
2534 t
->next
= macro_start
;
2539 * Good. We now have a macro name, a parameter count, and a
2540 * token list (in reverse order) for an expansion. We ought
2541 * to be OK just to create an SMacro, store it, and let
2542 * free_tlist have the rest of the line (which we have
2543 * carefully re-terminated after chopping off the expansion
2546 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2547 free_tlist(origline
);
2548 return DIRECTIVE_FOUND
;
2551 tline
= tline
->next
;
2553 tline
= expand_id(tline
);
2554 if (!tline
|| (tline
->type
!= TOK_ID
&&
2555 (tline
->type
!= TOK_PREPROC_ID
||
2556 tline
->text
[1] != '$'))) {
2557 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2558 free_tlist(origline
);
2559 return DIRECTIVE_FOUND
;
2563 "trailing garbage after macro name ignored");
2566 /* Find the context that symbol belongs to */
2567 ctx
= get_ctx(tline
->text
, false);
2568 undef_smacro(ctx
, tline
->text
);
2569 free_tlist(origline
);
2570 return DIRECTIVE_FOUND
;
2575 tline
= tline
->next
;
2577 tline
= expand_id(tline
);
2578 if (!tline
|| (tline
->type
!= TOK_ID
&&
2579 (tline
->type
!= TOK_PREPROC_ID
||
2580 tline
->text
[1] != '$'))) {
2582 "`%%strlen' expects a macro identifier as first parameter");
2583 free_tlist(origline
);
2584 return DIRECTIVE_FOUND
;
2586 ctx
= get_ctx(tline
->text
, false);
2588 mname
= tline
->text
;
2590 tline
= expand_smacro(tline
->next
);
2594 while (tok_type_(t
, TOK_WHITESPACE
))
2596 /* t should now point to the string */
2597 if (t
->type
!= TOK_STRING
) {
2599 "`%%strlen` requires string as second parameter");
2601 free_tlist(origline
);
2602 return DIRECTIVE_FOUND
;
2605 macro_start
= nasm_malloc(sizeof(*macro_start
));
2606 macro_start
->next
= NULL
;
2607 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2608 macro_start
->mac
= NULL
;
2611 * We now have a macro name, an implicit parameter count of
2612 * zero, and a numeric token to use as an expansion. Create
2613 * and store an SMacro.
2615 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2617 free_tlist(origline
);
2618 return DIRECTIVE_FOUND
;
2623 tline
= tline
->next
;
2625 tline
= expand_id(tline
);
2626 if (!tline
|| (tline
->type
!= TOK_ID
&&
2627 (tline
->type
!= TOK_PREPROC_ID
||
2628 tline
->text
[1] != '$'))) {
2630 "`%%substr' expects a macro identifier as first parameter");
2631 free_tlist(origline
);
2632 return DIRECTIVE_FOUND
;
2634 ctx
= get_ctx(tline
->text
, false);
2636 mname
= tline
->text
;
2638 tline
= expand_smacro(tline
->next
);
2642 while (tok_type_(t
, TOK_WHITESPACE
))
2645 /* t should now point to the string */
2646 if (t
->type
!= TOK_STRING
) {
2648 "`%%substr` requires string as second parameter");
2650 free_tlist(origline
);
2651 return DIRECTIVE_FOUND
;
2656 tokval
.t_type
= TOKEN_INVALID
;
2658 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2661 free_tlist(origline
);
2662 return DIRECTIVE_FOUND
;
2664 if (!is_simple(evalresult
)) {
2665 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2667 free_tlist(origline
);
2668 return DIRECTIVE_FOUND
;
2671 macro_start
= nasm_malloc(sizeof(*macro_start
));
2672 macro_start
->next
= NULL
;
2673 macro_start
->text
= nasm_strdup("'''");
2674 if (evalresult
->value
> 0
2675 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2676 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2678 macro_start
->text
[2] = '\0';
2680 macro_start
->type
= TOK_STRING
;
2681 macro_start
->mac
= NULL
;
2684 * We now have a macro name, an implicit parameter count of
2685 * zero, and a numeric token to use as an expansion. Create
2686 * and store an SMacro.
2688 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2690 free_tlist(origline
);
2691 return DIRECTIVE_FOUND
;
2695 casesense
= (i
== PP_ASSIGN
);
2697 tline
= tline
->next
;
2699 tline
= expand_id(tline
);
2700 if (!tline
|| (tline
->type
!= TOK_ID
&&
2701 (tline
->type
!= TOK_PREPROC_ID
||
2702 tline
->text
[1] != '$'))) {
2704 "`%%%sassign' expects a macro identifier",
2705 (i
== PP_IASSIGN
? "i" : ""));
2706 free_tlist(origline
);
2707 return DIRECTIVE_FOUND
;
2709 ctx
= get_ctx(tline
->text
, false);
2711 mname
= tline
->text
;
2713 tline
= expand_smacro(tline
->next
);
2718 tokval
.t_type
= TOKEN_INVALID
;
2720 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2723 free_tlist(origline
);
2724 return DIRECTIVE_FOUND
;
2729 "trailing garbage after expression ignored");
2731 if (!is_simple(evalresult
)) {
2733 "non-constant value given to `%%%sassign'",
2734 (i
== PP_IASSIGN
? "i" : ""));
2735 free_tlist(origline
);
2736 return DIRECTIVE_FOUND
;
2739 macro_start
= nasm_malloc(sizeof(*macro_start
));
2740 macro_start
->next
= NULL
;
2741 make_tok_num(macro_start
, reloc_value(evalresult
));
2742 macro_start
->mac
= NULL
;
2745 * We now have a macro name, an implicit parameter count of
2746 * zero, and a numeric token to use as an expansion. Create
2747 * and store an SMacro.
2749 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2750 free_tlist(origline
);
2751 return DIRECTIVE_FOUND
;
2755 * Syntax is `%line nnn[+mmm] [filename]'
2757 tline
= tline
->next
;
2759 if (!tok_type_(tline
, TOK_NUMBER
)) {
2760 error(ERR_NONFATAL
, "`%%line' expects line number");
2761 free_tlist(origline
);
2762 return DIRECTIVE_FOUND
;
2764 k
= readnum(tline
->text
, &err
);
2766 tline
= tline
->next
;
2767 if (tok_is_(tline
, "+")) {
2768 tline
= tline
->next
;
2769 if (!tok_type_(tline
, TOK_NUMBER
)) {
2770 error(ERR_NONFATAL
, "`%%line' expects line increment");
2771 free_tlist(origline
);
2772 return DIRECTIVE_FOUND
;
2774 m
= readnum(tline
->text
, &err
);
2775 tline
= tline
->next
;
2781 nasm_free(src_set_fname(detoken(tline
, false)));
2783 free_tlist(origline
);
2784 return DIRECTIVE_FOUND
;
2788 "preprocessor directive `%s' not yet implemented",
2792 return DIRECTIVE_FOUND
;
2796 * Ensure that a macro parameter contains a condition code and
2797 * nothing else. Return the condition code index if so, or -1
2800 static int find_cc(Token
* t
)
2806 return -1; /* Probably a %+ without a space */
2809 if (t
->type
!= TOK_ID
)
2813 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2817 j
= elements(conditions
);
2820 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2836 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2837 * %-n) and MMacro-local identifiers (%%foo).
2839 static Token
*expand_mmac_params(Token
* tline
)
2841 Token
*t
, *tt
, **tail
, *thead
;
2847 if (tline
->type
== TOK_PREPROC_ID
&&
2848 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2849 && tline
->text
[2]) || tline
->text
[1] == '%'
2850 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2852 int type
= 0, cc
; /* type = 0 to placate optimisers */
2859 tline
= tline
->next
;
2862 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2863 mac
= mac
->next_active
;
2865 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2867 switch (t
->text
[1]) {
2869 * We have to make a substitution of one of the
2870 * forms %1, %-1, %+1, %%foo, %0.
2874 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2875 text
= nasm_strdup(tmpbuf
);
2879 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2881 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
2884 n
= atoi(t
->text
+ 2) - 1;
2885 if (n
>= mac
->nparam
)
2888 if (mac
->nparam
> 1)
2889 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2890 tt
= mac
->params
[n
];
2895 "macro parameter %d is not a condition code",
2900 if (inverse_ccs
[cc
] == -1) {
2902 "condition code `%s' is not invertible",
2907 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2911 n
= atoi(t
->text
+ 2) - 1;
2912 if (n
>= mac
->nparam
)
2915 if (mac
->nparam
> 1)
2916 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2917 tt
= mac
->params
[n
];
2922 "macro parameter %d is not a condition code",
2927 text
= nasm_strdup(conditions
[cc
]);
2931 n
= atoi(t
->text
+ 1) - 1;
2932 if (n
>= mac
->nparam
)
2935 if (mac
->nparam
> 1)
2936 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2937 tt
= mac
->params
[n
];
2940 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2941 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2942 tail
= &(*tail
)->next
;
2946 text
= NULL
; /* we've done it here */
2962 tline
= tline
->next
;
2969 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2971 case TOK_WHITESPACE
:
2972 if (tt
->type
== TOK_WHITESPACE
) {
2973 t
->next
= delete_Token(tt
);
2977 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2978 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2981 t
->next
= delete_Token(tt
);
2985 if (tt
->type
== TOK_NUMBER
) {
2986 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2989 t
->next
= delete_Token(tt
);
3000 * Expand all single-line macro calls made in the given line.
3001 * Return the expanded version of the line. The original is deemed
3002 * to be destroyed in the process. (In reality we'll just move
3003 * Tokens from input to output a lot of the time, rather than
3004 * actually bothering to destroy and replicate.)
3006 #define DEADMAN_LIMIT (1 << 20)
3008 static Token
*expand_smacro(Token
* tline
)
3010 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3011 SMacro
*head
= NULL
, *m
;
3014 unsigned int nparam
, sparam
;
3015 int brackets
, rescan
;
3016 Token
*org_tline
= tline
;
3019 int deadman
= DEADMAN_LIMIT
;
3022 * Trick: we should avoid changing the start token pointer since it can
3023 * be contained in "next" field of other token. Because of this
3024 * we allocate a copy of first token and work with it; at the end of
3025 * routine we copy it back
3029 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3031 tline
->mac
= org_tline
->mac
;
3032 nasm_free(org_tline
->text
);
3033 org_tline
->text
= NULL
;
3040 while (tline
) { /* main token loop */
3042 error(ERR_NONFATAL
, "interminable macro recursion");
3046 if ((mname
= tline
->text
)) {
3047 /* if this token is a local macro, look in local context */
3048 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3049 ctx
= get_ctx(mname
, true);
3053 head
= (SMacro
*) hash_findix(ctx
? ctx
->localmac
: smacros
,
3057 * We've hit an identifier. As in is_mmacro below, we first
3058 * check whether the identifier is a single-line macro at
3059 * all, then think about checking for parameters if
3062 for (m
= head
; m
; m
= m
->next
)
3063 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3069 if (m
->nparam
== 0) {
3071 * Simple case: the macro is parameterless. Discard the
3072 * one token that the macro call took, and push the
3073 * expansion back on the to-do stack.
3075 if (!m
->expansion
) {
3076 if (!strcmp("__FILE__", m
->name
)) {
3078 src_get(&num
, &(tline
->text
));
3079 nasm_quote(&(tline
->text
));
3080 tline
->type
= TOK_STRING
;
3083 if (!strcmp("__LINE__", m
->name
)) {
3084 nasm_free(tline
->text
);
3085 make_tok_num(tline
, src_get_linnum());
3088 if (!strcmp("__BITS__", m
->name
)) {
3089 nasm_free(tline
->text
);
3090 make_tok_num(tline
, globalbits
);
3093 tline
= delete_Token(tline
);
3098 * Complicated case: at least one macro with this name
3099 * exists and takes parameters. We must find the
3100 * parameters in the call, count them, find the SMacro
3101 * that corresponds to that form of the macro call, and
3102 * substitute for the parameters when we expand. What a
3105 /*tline = tline->next;
3106 skip_white_(tline); */
3109 while (tok_type_(t
, TOK_SMAC_END
)) {
3110 t
->mac
->in_progress
= false;
3112 t
= tline
->next
= delete_Token(t
);
3115 } while (tok_type_(tline
, TOK_WHITESPACE
));
3116 if (!tok_is_(tline
, "(")) {
3118 * This macro wasn't called with parameters: ignore
3119 * the call. (Behaviour borrowed from gnu cpp.)
3128 sparam
= PARAM_DELTA
;
3129 params
= nasm_malloc(sparam
* sizeof(Token
*));
3130 params
[0] = tline
->next
;
3131 paramsize
= nasm_malloc(sparam
* sizeof(int));
3133 while (true) { /* parameter loop */
3135 * For some unusual expansions
3136 * which concatenates function call
3139 while (tok_type_(t
, TOK_SMAC_END
)) {
3140 t
->mac
->in_progress
= false;
3142 t
= tline
->next
= delete_Token(t
);
3148 "macro call expects terminating `)'");
3151 if (tline
->type
== TOK_WHITESPACE
3153 if (paramsize
[nparam
])
3156 params
[nparam
] = tline
->next
;
3157 continue; /* parameter loop */
3159 if (tline
->type
== TOK_OTHER
3160 && tline
->text
[1] == 0) {
3161 char ch
= tline
->text
[0];
3162 if (ch
== ',' && !paren
&& brackets
<= 0) {
3163 if (++nparam
>= sparam
) {
3164 sparam
+= PARAM_DELTA
;
3165 params
= nasm_realloc(params
,
3170 nasm_realloc(paramsize
,
3174 params
[nparam
] = tline
->next
;
3175 paramsize
[nparam
] = 0;
3177 continue; /* parameter loop */
3180 (brackets
> 0 || (brackets
== 0 &&
3181 !paramsize
[nparam
])))
3183 if (!(brackets
++)) {
3184 params
[nparam
] = tline
->next
;
3185 continue; /* parameter loop */
3188 if (ch
== '}' && brackets
> 0)
3189 if (--brackets
== 0) {
3191 continue; /* parameter loop */
3193 if (ch
== '(' && !brackets
)
3195 if (ch
== ')' && brackets
<= 0)
3201 error(ERR_NONFATAL
, "braces do not "
3202 "enclose all of macro parameter");
3204 paramsize
[nparam
] += white
+ 1;
3206 } /* parameter loop */
3208 while (m
&& (m
->nparam
!= nparam
||
3209 mstrcmp(m
->name
, mname
,
3213 error(ERR_WARNING
| ERR_WARN_MNP
,
3214 "macro `%s' exists, "
3215 "but not taking %d parameters",
3216 mstart
->text
, nparam
);
3219 if (m
&& m
->in_progress
)
3221 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3223 * Design question: should we handle !tline, which
3224 * indicates missing ')' here, or expand those
3225 * macros anyway, which requires the (t) test a few
3229 nasm_free(paramsize
);
3233 * Expand the macro: we are placed on the last token of the
3234 * call, so that we can easily split the call from the
3235 * following tokens. We also start by pushing an SMAC_END
3236 * token for the cycle removal.
3243 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3245 m
->in_progress
= true;
3247 for (t
= m
->expansion
; t
; t
= t
->next
) {
3248 if (t
->type
>= TOK_SMAC_PARAM
) {
3249 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3253 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3254 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3257 new_Token(tline
, ttt
->type
, ttt
->text
,
3264 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3270 * Having done that, get rid of the macro call, and clean
3271 * up the parameters.
3274 nasm_free(paramsize
);
3276 continue; /* main token loop */
3281 if (tline
->type
== TOK_SMAC_END
) {
3282 tline
->mac
->in_progress
= false;
3283 tline
= delete_Token(tline
);
3286 tline
= tline
->next
;
3294 * Now scan the entire line and look for successive TOK_IDs that resulted
3295 * after expansion (they can't be produced by tokenize()). The successive
3296 * TOK_IDs should be concatenated.
3297 * Also we look for %+ tokens and concatenate the tokens before and after
3298 * them (without white spaces in between).
3303 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3307 if (t
->next
->type
== TOK_ID
||
3308 t
->next
->type
== TOK_PREPROC_ID
||
3309 t
->next
->type
== TOK_NUMBER
) {
3310 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3312 t
->next
= delete_Token(t
->next
);
3315 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3316 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3317 strcmp(t
->next
->next
->text
, "%+") == 0) {
3318 /* free the next whitespace, the %+ token and next whitespace */
3320 for (i
= 1; i
<= 3; i
++) {
3322 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3324 t
->next
= delete_Token(t
->next
);
3329 /* If we concatenaded something, re-scan the line for macros */
3337 *org_tline
= *thead
;
3338 /* since we just gave text to org_line, don't free it */
3340 delete_Token(thead
);
3342 /* the expression expanded to empty line;
3343 we can't return NULL for some reasons
3344 we just set the line to a single WHITESPACE token. */
3345 memset(org_tline
, 0, sizeof(*org_tline
));
3346 org_tline
->text
= NULL
;
3347 org_tline
->type
= TOK_WHITESPACE
;
3356 * Similar to expand_smacro but used exclusively with macro identifiers
3357 * right before they are fetched in. The reason is that there can be
3358 * identifiers consisting of several subparts. We consider that if there
3359 * are more than one element forming the name, user wants a expansion,
3360 * otherwise it will be left as-is. Example:
3364 * the identifier %$abc will be left as-is so that the handler for %define
3365 * will suck it and define the corresponding value. Other case:
3367 * %define _%$abc cde
3369 * In this case user wants name to be expanded *before* %define starts
3370 * working, so we'll expand %$abc into something (if it has a value;
3371 * otherwise it will be left as-is) then concatenate all successive
3374 static Token
*expand_id(Token
* tline
)
3376 Token
*cur
, *oldnext
= NULL
;
3378 if (!tline
|| !tline
->next
)
3383 (cur
->next
->type
== TOK_ID
||
3384 cur
->next
->type
== TOK_PREPROC_ID
3385 || cur
->next
->type
== TOK_NUMBER
))
3388 /* If identifier consists of just one token, don't expand */
3393 oldnext
= cur
->next
; /* Detach the tail past identifier */
3394 cur
->next
= NULL
; /* so that expand_smacro stops here */
3397 tline
= expand_smacro(tline
);
3400 /* expand_smacro possibly changhed tline; re-scan for EOL */
3402 while (cur
&& cur
->next
)
3405 cur
->next
= oldnext
;
3412 * Determine whether the given line constitutes a multi-line macro
3413 * call, and return the MMacro structure called if so. Doesn't have
3414 * to check for an initial label - that's taken care of in
3415 * expand_mmacro - but must check numbers of parameters. Guaranteed
3416 * to be called with tline->type == TOK_ID, so the putative macro
3417 * name is easy to find.
3419 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3425 head
= (MMacro
*) hash_findix(mmacros
, tline
->text
);
3428 * Efficiency: first we see if any macro exists with the given
3429 * name. If not, we can return NULL immediately. _Then_ we
3430 * count the parameters, and then we look further along the
3431 * list if necessary to find the proper MMacro.
3433 for (m
= head
; m
; m
= m
->next
)
3434 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3440 * OK, we have a potential macro. Count and demarcate the
3443 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3446 * So we know how many parameters we've got. Find the MMacro
3447 * structure that handles this number.
3450 if (m
->nparam_min
<= nparam
3451 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3453 * This one is right. Just check if cycle removal
3454 * prohibits us using it before we actually celebrate...
3456 if (m
->in_progress
) {
3459 "self-reference in multi-line macro `%s'", m
->name
);
3465 * It's right, and we can use it. Add its default
3466 * parameters to the end of our list if necessary.
3468 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3470 nasm_realloc(params
,
3471 ((m
->nparam_min
+ m
->ndefs
+
3472 1) * sizeof(*params
)));
3473 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3474 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3479 * If we've gone over the maximum parameter count (and
3480 * we're in Plus mode), ignore parameters beyond
3483 if (m
->plus
&& nparam
> m
->nparam_max
)
3484 nparam
= m
->nparam_max
;
3486 * Then terminate the parameter list, and leave.
3488 if (!params
) { /* need this special case */
3489 params
= nasm_malloc(sizeof(*params
));
3492 params
[nparam
] = NULL
;
3493 *params_array
= params
;
3497 * This one wasn't right: look for the next one with the
3500 for (m
= m
->next
; m
; m
= m
->next
)
3501 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3506 * After all that, we didn't find one with the right number of
3507 * parameters. Issue a warning, and fail to expand the macro.
3509 error(ERR_WARNING
| ERR_WARN_MNP
,
3510 "macro `%s' exists, but not taking %d parameters",
3511 tline
->text
, nparam
);
3517 * Expand the multi-line macro call made by the given line, if
3518 * there is one to be expanded. If there is, push the expansion on
3519 * istk->expansion and return 1. Otherwise return 0.
3521 static int expand_mmacro(Token
* tline
)
3523 Token
*startline
= tline
;
3524 Token
*label
= NULL
;
3525 int dont_prepend
= 0;
3526 Token
**params
, *t
, *tt
;
3529 int i
, nparam
, *paramlen
;
3533 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3534 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3536 m
= is_mmacro(t
, ¶ms
);
3540 * We have an id which isn't a macro call. We'll assume
3541 * it might be a label; we'll also check to see if a
3542 * colon follows it. Then, if there's another id after
3543 * that lot, we'll check it again for macro-hood.
3547 if (tok_type_(t
, TOK_WHITESPACE
))
3548 last
= t
, t
= t
->next
;
3549 if (tok_is_(t
, ":")) {
3551 last
= t
, t
= t
->next
;
3552 if (tok_type_(t
, TOK_WHITESPACE
))
3553 last
= t
, t
= t
->next
;
3555 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3562 * Fix up the parameters: this involves stripping leading and
3563 * trailing whitespace, then stripping braces if they are
3566 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3567 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3569 for (i
= 0; params
[i
]; i
++) {
3571 int comma
= (!m
->plus
|| i
< nparam
- 1);
3575 if (tok_is_(t
, "{"))
3576 t
= t
->next
, brace
= true, comma
= false;
3580 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3581 break; /* ... because we have hit a comma */
3582 if (comma
&& t
->type
== TOK_WHITESPACE
3583 && tok_is_(t
->next
, ","))
3584 break; /* ... or a space then a comma */
3585 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3586 break; /* ... or a brace */
3593 * OK, we have a MMacro structure together with a set of
3594 * parameters. We must now go through the expansion and push
3595 * copies of each Line on to istk->expansion. Substitution of
3596 * parameter tokens and macro-local tokens doesn't get done
3597 * until the single-line macro substitution process; this is
3598 * because delaying them allows us to change the semantics
3599 * later through %rotate.
3601 * First, push an end marker on to istk->expansion, mark this
3602 * macro as in progress, and set up its invocation-specific
3605 ll
= nasm_malloc(sizeof(Line
));
3606 ll
->next
= istk
->expansion
;
3609 istk
->expansion
= ll
;
3611 m
->in_progress
= true;
3616 m
->paramlen
= paramlen
;
3617 m
->unique
= unique
++;
3620 m
->next_active
= istk
->mstk
;
3623 for (l
= m
->expansion
; l
; l
= l
->next
) {
3626 ll
= nasm_malloc(sizeof(Line
));
3627 ll
->finishes
= NULL
;
3628 ll
->next
= istk
->expansion
;
3629 istk
->expansion
= ll
;
3632 for (t
= l
->first
; t
; t
= t
->next
) {
3634 if (t
->type
== TOK_PREPROC_ID
&&
3635 t
->text
[1] == '0' && t
->text
[2] == '0') {
3641 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3648 * If we had a label, push it on as the first line of
3649 * the macro expansion.
3652 if (dont_prepend
< 0)
3653 free_tlist(startline
);
3655 ll
= nasm_malloc(sizeof(Line
));
3656 ll
->finishes
= NULL
;
3657 ll
->next
= istk
->expansion
;
3658 istk
->expansion
= ll
;
3659 ll
->first
= startline
;
3660 if (!dont_prepend
) {
3662 label
= label
->next
;
3663 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3668 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3674 * Since preprocessor always operate only on the line that didn't
3675 * arrived yet, we should always use ERR_OFFBY1. Also since user
3676 * won't want to see same error twice (preprocessing is done once
3677 * per pass) we will want to show errors only during pass one.
3679 static void error(int severity
, const char *fmt
, ...)
3684 /* If we're in a dead branch of IF or something like it, ignore the error */
3685 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3689 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3692 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3693 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3694 istk
->mstk
->lineno
, buff
);
3696 _error(severity
| ERR_PASS1
, "%s", buff
);
3700 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3705 istk
= nasm_malloc(sizeof(Include
));
3708 istk
->expansion
= NULL
;
3710 istk
->fp
= fopen(file
, "r");
3712 src_set_fname(nasm_strdup(file
));
3716 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3721 if (tasm_compatible_mode
) {
3722 stdmacpos
= nasm_stdmac
;
3724 stdmacpos
= nasm_stdmac_after_tasm
;
3726 any_extrastdmac
= (extrastdmac
!= NULL
);
3732 static char *pp_getline(void)
3739 * Fetch a tokenized line, either from the macro-expansion
3740 * buffer or from the input file.
3743 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3744 Line
*l
= istk
->expansion
;
3745 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3749 * This is a macro-end marker for a macro with no
3750 * name, which means it's not really a macro at all
3751 * but a %rep block, and the `in_progress' field is
3752 * more than 1, meaning that we still need to
3753 * repeat. (1 means the natural last repetition; 0
3754 * means termination by %exitrep.) We have
3755 * therefore expanded up to the %endrep, and must
3756 * push the whole block on to the expansion buffer
3757 * again. We don't bother to remove the macro-end
3758 * marker: we'd only have to generate another one
3761 l
->finishes
->in_progress
--;
3762 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3763 Token
*t
, *tt
, **tail
;
3765 ll
= nasm_malloc(sizeof(Line
));
3766 ll
->next
= istk
->expansion
;
3767 ll
->finishes
= NULL
;
3771 for (t
= l
->first
; t
; t
= t
->next
) {
3772 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3774 new_Token(NULL
, t
->type
, t
->text
, 0);
3779 istk
->expansion
= ll
;
3783 * Check whether a `%rep' was started and not ended
3784 * within this macro expansion. This can happen and
3785 * should be detected. It's a fatal error because
3786 * I'm too confused to work out how to recover
3792 "defining with name in expansion");
3793 else if (istk
->mstk
->name
)
3795 "`%%rep' without `%%endrep' within"
3796 " expansion of macro `%s'",
3801 * FIXME: investigate the relationship at this point between
3802 * istk->mstk and l->finishes
3805 MMacro
*m
= istk
->mstk
;
3806 istk
->mstk
= m
->next_active
;
3809 * This was a real macro call, not a %rep, and
3810 * therefore the parameter information needs to
3813 nasm_free(m
->params
);
3814 free_tlist(m
->iline
);
3815 nasm_free(m
->paramlen
);
3816 l
->finishes
->in_progress
= false;
3820 istk
->expansion
= l
->next
;
3822 list
->downlevel(LIST_MACRO
);
3825 while (1) { /* until we get a line we can use */
3827 if (istk
->expansion
) { /* from a macro expansion */
3829 Line
*l
= istk
->expansion
;
3831 istk
->mstk
->lineno
++;
3833 istk
->expansion
= l
->next
;
3835 p
= detoken(tline
, false);
3836 list
->line(LIST_MACRO
, p
);
3841 if (line
) { /* from the current input file */
3842 line
= prepreproc(line
);
3843 tline
= tokenize(line
);
3848 * The current file has ended; work down the istk
3855 "expected `%%endif' before end of file");
3856 /* only set line and file name if there's a next node */
3858 src_set_linnum(i
->lineno
);
3859 nasm_free(src_set_fname(i
->fname
));
3862 list
->downlevel(LIST_INCLUDE
);
3870 * We must expand MMacro parameters and MMacro-local labels
3871 * _before_ we plunge into directive processing, to cope
3872 * with things like `%define something %1' such as STRUC
3873 * uses. Unless we're _defining_ a MMacro, in which case
3874 * those tokens should be left alone to go into the
3875 * definition; and unless we're in a non-emitting
3876 * condition, in which case we don't want to meddle with
3879 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3880 tline
= expand_mmac_params(tline
);
3883 * Check the line to see if it's a preprocessor directive.
3885 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3887 } else if (defining
) {
3889 * We're defining a multi-line macro. We emit nothing
3891 * shove the tokenized line on to the macro definition.
3893 Line
*l
= nasm_malloc(sizeof(Line
));
3894 l
->next
= defining
->expansion
;
3896 l
->finishes
= false;
3897 defining
->expansion
= l
;
3899 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3901 * We're in a non-emitting branch of a condition block.
3902 * Emit nothing at all, not even a blank line: when we
3903 * emerge from the condition we'll give a line-number
3904 * directive so we keep our place correctly.
3908 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3910 * We're in a %rep block which has been terminated, so
3911 * we're walking through to the %endrep without
3912 * emitting anything. Emit nothing at all, not even a
3913 * blank line: when we emerge from the %rep block we'll
3914 * give a line-number directive so we keep our place
3920 tline
= expand_smacro(tline
);
3921 if (!expand_mmacro(tline
)) {
3923 * De-tokenize the line again, and emit it.
3925 line
= detoken(tline
, true);
3929 continue; /* expand_mmacro calls free_tlist */
3937 static void pp_cleanup(int pass
)
3940 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3942 free_mmacro(defining
);
3951 nasm_free(i
->fname
);
3962 void pp_include_path(char *path
)
3966 i
= nasm_malloc(sizeof(IncPath
));
3967 i
->path
= path
? nasm_strdup(path
) : NULL
;
3970 if (ipath
!= NULL
) {
3972 while (j
->next
!= NULL
)
3983 * This function is used to "export" the include paths, e.g.
3984 * the paths specified in the '-I' command switch.
3985 * The need for such exporting is due to the 'incbin' directive,
3986 * which includes raw binary files (unlike '%include', which
3987 * includes text source files). It would be real nice to be
3988 * able to specify paths to search for incbin'ned files also.
3989 * So, this is a simple workaround.
3991 * The function use is simple:
3993 * The 1st call (with NULL argument) returns a pointer to the 1st path
3994 * (char** type) or NULL if none include paths available.
3996 * All subsequent calls take as argument the value returned by this
3997 * function last. The return value is either the next path
3998 * (char** type) or NULL if the end of the paths list is reached.
4000 * It is maybe not the best way to do things, but I didn't want
4001 * to export too much, just one or two functions and no types or
4002 * variables exported.
4004 * Can't say I like the current situation with e.g. this path list either,
4005 * it seems to be never deallocated after creation...
4007 char **pp_get_include_path_ptr(char **pPrevPath
)
4009 /* This macro returns offset of a member of a structure */
4010 #define GetMemberOffset(StructType,MemberName)\
4011 ((size_t)&((StructType*)0)->MemberName)
4014 if (pPrevPath
== NULL
) {
4016 return &ipath
->path
;
4020 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
4026 #undef GetMemberOffset
4029 void pp_pre_include(char *fname
)
4031 Token
*inc
, *space
, *name
;
4034 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4035 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4036 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4038 l
= nasm_malloc(sizeof(Line
));
4041 l
->finishes
= false;
4045 void pp_pre_define(char *definition
)
4051 equals
= strchr(definition
, '=');
4052 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4053 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4056 space
->next
= tokenize(definition
);
4060 l
= nasm_malloc(sizeof(Line
));
4063 l
->finishes
= false;
4067 void pp_pre_undefine(char *definition
)
4072 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4073 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4074 space
->next
= tokenize(definition
);
4076 l
= nasm_malloc(sizeof(Line
));
4079 l
->finishes
= false;
4084 * Added by Keith Kanios:
4086 * This function is used to assist with "runtime" preprocessor
4087 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4089 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4090 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4093 void pp_runtime(char *definition
)
4097 def
= tokenize(definition
);
4098 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4103 void pp_extra_stdmac(const char **macros
)
4105 extrastdmac
= macros
;
4108 static void make_tok_num(Token
* tok
, int64_t val
)
4111 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4112 tok
->text
= nasm_strdup(numbuf
);
4113 tok
->type
= TOK_NUMBER
;