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
54 typedef struct SMacro SMacro
;
55 typedef struct MMacro MMacro
;
56 typedef struct Context Context
;
57 typedef struct Token Token
;
58 typedef struct Blocks Blocks
;
59 typedef struct Line Line
;
60 typedef struct Include Include
;
61 typedef struct Cond Cond
;
62 typedef struct IncPath IncPath
;
65 * Note on the storage of both SMacro and MMacros: the hash table
66 * indexes them case-insensitively, and we then have to go through a
67 * linked list of potential case aliases (and, for MMacros, parameter
68 * ranges); this is to preserve the matching semantics of the earlier
69 * code. If the number of case aliases for a specific macro is a
70 * performance issue, you may want to reconsider your coding style.
74 * Store the definition of a single-line macro.
86 * Store the definition of a multi-line macro. This is also used to
87 * store the interiors of `%rep...%endrep' blocks, which are
88 * effectively self-re-invoking multi-line macros which simply
89 * don't have a name or bother to appear in the hash tables. %rep
90 * blocks are signified by having a NULL `name' field.
92 * In a MMacro describing a `%rep' block, the `in_progress' field
93 * isn't merely boolean, but gives the number of repeats left to
96 * The `next' field is used for storing MMacros in hash tables; the
97 * `next_active' field is for stacking them on istk entries.
99 * When a MMacro is being expanded, `params', `iline', `nparam',
100 * `paramlen', `rotate' and `unique' are local to the invocation.
105 int nparam_min
, nparam_max
;
107 bool plus
; /* is the last parameter greedy? */
108 bool nolist
; /* is this macro listing-inhibited? */
110 Token
*dlist
; /* All defaults as one list */
111 Token
**defaults
; /* Parameter default pointers */
112 int ndefs
; /* number of default parameters */
116 MMacro
*rep_nest
; /* used for nesting %rep */
117 Token
**params
; /* actual parameters */
118 Token
*iline
; /* invocation line */
119 unsigned int nparam
, rotate
;
122 int lineno
; /* Current line number on expansion */
126 * The context stack is composed of a linked list of these.
136 * This is the internal form which we break input lines up into.
137 * Typically stored in linked lists.
139 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
140 * necessarily used as-is, but is intended to denote the number of
141 * the substituted parameter. So in the definition
143 * %define a(x,y) ( (x) & ~(y) )
145 * the token representing `x' will have its type changed to
146 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
150 * which doesn't need quotes around it. Used in the pre-include
151 * mechanism as an alternative to trying to find a sensible type of
152 * quote to use on the filename we were passed.
155 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
156 TOK_PREPROC_ID
, TOK_STRING
,
157 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
164 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
165 enum pp_token_type type
;
169 * Multi-line macro definitions are stored as a linked list of
170 * these, which is essentially a container to allow several linked
173 * Note that in this module, linked lists are treated as stacks
174 * wherever possible. For this reason, Lines are _pushed_ on to the
175 * `expansion' field in MMacro structures, so that the linked list,
176 * if walked, would give the macro lines in reverse order; this
177 * means that we can walk the list when expanding a macro, and thus
178 * push the lines on to the `expansion' field in _istk_ in reverse
179 * order (so that when popped back off they are in the right
180 * order). It may seem cockeyed, and it relies on my design having
181 * an even number of steps in, but it works...
183 * Some of these structures, rather than being actual lines, are
184 * markers delimiting the end of the expansion of a given macro.
185 * This is for use in the cycle-tracking and %rep-handling code.
186 * Such structures have `finishes' non-NULL, and `first' NULL. All
187 * others have `finishes' NULL, but `first' may still be NULL if
197 * To handle an arbitrary level of file inclusion, we maintain a
198 * stack (ie linked list) of these things.
207 MMacro
*mstk
; /* stack of active macros/reps */
211 * Include search path. This is simply a list of strings which get
212 * prepended, in turn, to the name of an include file, in an
213 * attempt to find the file if it's not in the current directory.
221 * Conditional assembly: we maintain a separate stack of these for
222 * each level of file inclusion. (The only reason we keep the
223 * stacks separate is to ensure that a stray `%endif' in a file
224 * included from within the true branch of a `%if' won't terminate
225 * it and cause confusion: instead, rightly, it'll cause an error.)
233 * These states are for use just after %if or %elif: IF_TRUE
234 * means the condition has evaluated to truth so we are
235 * currently emitting, whereas IF_FALSE means we are not
236 * currently emitting but will start doing so if a %else comes
237 * up. In these states, all directives are admissible: %elif,
238 * %else and %endif. (And of course %if.)
240 COND_IF_TRUE
, COND_IF_FALSE
,
242 * These states come up after a %else: ELSE_TRUE means we're
243 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
244 * any %elif or %else will cause an error.
246 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
248 * This state means that we're not emitting now, and also that
249 * nothing until %endif will be emitted at all. It's for use in
250 * two circumstances: (i) when we've had our moment of emission
251 * and have now started seeing %elifs, and (ii) when the
252 * condition construct in question is contained within a
253 * non-emitting branch of a larger condition construct.
257 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
260 * These defines are used as the possible return values for do_directive
262 #define NO_DIRECTIVE_FOUND 0
263 #define DIRECTIVE_FOUND 1
266 * Condition codes. Note that we use c_ prefix not C_ because C_ is
267 * used in nasm.h for the "real" condition codes. At _this_ level,
268 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
269 * ones, so we need a different enum...
271 static const char * const conditions
[] = {
272 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
273 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
274 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
277 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
278 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
279 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
282 static const enum pp_conds inverse_ccs
[] = {
283 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
284 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
,
285 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
291 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
292 static int is_condition(enum preproc_token arg
)
294 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
297 /* For TASM compatibility we need to be able to recognise TASM compatible
298 * conditional compilation directives. Using the NASM pre-processor does
299 * not work, so we look for them specifically from the following list and
300 * then jam in the equivalent NASM directive into the input stream.
304 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
305 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
308 static const char * const tasm_directives
[] = {
309 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
310 "ifndef", "include", "local"
313 static int StackSize
= 4;
314 static char *StackPointer
= "ebp";
315 static int ArgOffset
= 8;
316 static int LocalOffset
= 0;
318 static Context
*cstk
;
319 static Include
*istk
;
320 static IncPath
*ipath
= NULL
;
322 static efunc _error
; /* Pointer to client-provided error reporting function */
323 static evalfunc evaluate
;
325 static int pass
; /* HACK: pass 0 = generate dependencies only */
327 static uint64_t unique
; /* unique identifier numbers */
329 static Line
*predef
= NULL
;
331 static ListGen
*list
;
334 * The current set of multi-line macros we have defined.
336 static struct hash_table
*mmacros
;
339 * The current set of single-line macros we have defined.
341 static struct hash_table
*smacros
;
344 * The multi-line macro we are currently defining, or the %rep
345 * block we are currently reading, if any.
347 static MMacro
*defining
;
350 * The number of macro parameters to allocate space for at a time.
352 #define PARAM_DELTA 16
355 * The standard macro set: defined as `static char *stdmac[]'. Also
356 * 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_macros(void)
528 struct hash_tbl_node
*it
;
534 while ((s
= hash_iterate(smacros
, &it
, &key
)) != NULL
) {
535 nasm_free((void *)key
);
537 SMacro
*ns
= s
->next
;
539 free_tlist(s
->expansion
);
547 while ((m
= hash_iterate(mmacros
, &it
, &key
)) != NULL
) {
548 nasm_free((void *)key
);
550 MMacro
*nm
= m
->next
;
559 * Initialize the hash tables
561 static void init_macros(void)
563 smacros
= hash_init();
564 mmacros
= hash_init();
568 * Pop the context stack.
570 static void ctx_pop(void)
581 free_tlist(s
->expansion
);
588 #define BUF_DELTA 512
590 * Read a line from the top file in istk, handling multiple CR/LFs
591 * at the end of the line read, and handling spurious ^Zs. Will
592 * return lines from the standard macro set if this has not already
595 static char *read_line(void)
597 char *buffer
, *p
, *q
;
598 int bufsize
, continued_count
;
602 char *ret
= nasm_strdup(*stdmacpos
++);
603 if (!*stdmacpos
&& any_extrastdmac
) {
604 stdmacpos
= extrastdmac
;
605 any_extrastdmac
= false;
609 * Nasty hack: here we push the contents of `predef' on
610 * to the top-level expansion stack, since this is the
611 * most convenient way to implement the pre-include and
612 * pre-define features.
616 Token
*head
, **tail
, *t
;
618 for (pd
= predef
; pd
; pd
= pd
->next
) {
621 for (t
= pd
->first
; t
; t
= t
->next
) {
622 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
623 tail
= &(*tail
)->next
;
625 l
= nasm_malloc(sizeof(Line
));
626 l
->next
= istk
->expansion
;
639 buffer
= nasm_malloc(BUF_DELTA
);
643 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
647 if (p
> buffer
&& p
[-1] == '\n') {
648 /* Convert backslash-CRLF line continuation sequences into
649 nothing at all (for DOS and Windows) */
650 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
655 /* Also convert backslash-LF line continuation sequences into
656 nothing at all (for Unix) */
657 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
665 if (p
- buffer
> bufsize
- 10) {
666 int32_t offset
= p
- buffer
;
667 bufsize
+= BUF_DELTA
;
668 buffer
= nasm_realloc(buffer
, bufsize
);
669 p
= buffer
+ offset
; /* prevent stale-pointer problems */
673 if (!q
&& p
== buffer
) {
678 src_set_linnum(src_get_linnum() + istk
->lineinc
+
679 (continued_count
* istk
->lineinc
));
682 * Play safe: remove CRs as well as LFs, if any of either are
683 * present at the end of the line.
685 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
689 * Handle spurious ^Z, which may be inserted into source files
690 * by some file transfer utilities.
692 buffer
[strcspn(buffer
, "\032")] = '\0';
694 list
->line(LIST_READ
, buffer
);
700 * Tokenize a line of text. This is a very simple process since we
701 * don't need to parse the value out of e.g. numeric tokens: we
702 * simply split one string into many.
704 static Token
*tokenize(char *line
)
707 enum pp_token_type type
;
709 Token
*t
, **tail
= &list
;
716 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
717 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
722 type
= TOK_PREPROC_ID
;
723 } else if (*p
== '{') {
725 while (*p
&& *p
!= '}') {
732 type
= TOK_PREPROC_ID
;
733 } else if (isidchar(*p
) ||
734 ((*p
== '!' || *p
== '%' || *p
== '$') &&
739 while (isidchar(*p
));
740 type
= TOK_PREPROC_ID
;
746 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
749 while (*p
&& isidchar(*p
))
751 } else if (*p
== '\'' || *p
== '"') {
758 while (*p
&& *p
!= c
)
764 error(ERR_WARNING
, "unterminated string");
765 /* Handling unterminated strings by UNV */
768 } else if (isnumstart(*p
)) {
770 bool is_float
= false;
786 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
788 if (*p
== '+' || *p
== '-') {
789 /* e can only be followed by +/- if it is either a
790 prefixed hex number or a floating-point number */
794 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
796 } else if (c
== 'P' || c
== 'p') {
798 if (*p
== '+' || *p
== '-')
800 } else if (isnumchar(c
) || c
== '_')
803 /* we need to deal with consequences of the legacy
804 parser, like "1.nolist" being two tokens
805 (TOK_NUMBER, TOK_ID) here; at least give it
806 a shot for now. In the future, we probably need
807 a flex-based scanner with proper pattern matching
808 to do it as well as it can be done. Nothing in
809 the world is going to help the person who wants
810 0x123.p16 interpreted as two tokens, though. */
815 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
816 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
817 (*r
== 'p' || *r
== 'P')) {
821 break; /* Terminate the token */
825 p
--; /* Point to first character beyond number */
827 if (has_e
&& !is_hex
) {
828 /* 1e13 is floating-point, but 1e13h is not */
832 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
833 } else if (isspace(*p
)) {
834 type
= TOK_WHITESPACE
;
836 while (*p
&& isspace(*p
))
839 * Whitespace just before end-of-line is discarded by
840 * pretending it's a comment; whitespace just before a
841 * comment gets lumped into the comment.
843 if (!*p
|| *p
== ';') {
848 } else if (*p
== ';') {
854 * Anything else is an operator of some kind. We check
855 * for all the double-character operators (>>, <<, //,
856 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
857 * else is a single-character operator.
860 if ((p
[0] == '>' && p
[1] == '>') ||
861 (p
[0] == '<' && p
[1] == '<') ||
862 (p
[0] == '/' && p
[1] == '/') ||
863 (p
[0] == '<' && p
[1] == '=') ||
864 (p
[0] == '>' && p
[1] == '=') ||
865 (p
[0] == '=' && p
[1] == '=') ||
866 (p
[0] == '!' && p
[1] == '=') ||
867 (p
[0] == '<' && p
[1] == '>') ||
868 (p
[0] == '&' && p
[1] == '&') ||
869 (p
[0] == '|' && p
[1] == '|') ||
870 (p
[0] == '^' && p
[1] == '^')) {
876 /* Handling unterminated string by UNV */
879 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
880 t->text[p-line] = *line;
884 if (type
!= TOK_COMMENT
) {
885 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
894 * this function allocates a new managed block of memory and
895 * returns a pointer to the block. The managed blocks are
896 * deleted only all at once by the delete_Blocks function.
898 static void *new_Block(size_t size
)
902 /* first, get to the end of the linked list */
905 /* now allocate the requested chunk */
906 b
->chunk
= nasm_malloc(size
);
908 /* now allocate a new block for the next request */
909 b
->next
= nasm_malloc(sizeof(Blocks
));
910 /* and initialize the contents of the new block */
911 b
->next
->next
= NULL
;
912 b
->next
->chunk
= NULL
;
917 * this function deletes all managed blocks of memory
919 static void delete_Blocks(void)
921 Blocks
*a
, *b
= &blocks
;
924 * keep in mind that the first block, pointed to by blocks
925 * is a static and not dynamically allocated, so we don't
939 * this function creates a new Token and passes a pointer to it
940 * back to the caller. It sets the type and text elements, and
941 * also the mac and next elements to NULL.
943 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
948 if (freeTokens
== NULL
) {
949 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
950 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
951 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
952 freeTokens
[i
].next
= NULL
;
955 freeTokens
= t
->next
;
959 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
963 txtlen
= strlen(text
);
964 t
->text
= nasm_malloc(1 + txtlen
);
965 strncpy(t
->text
, text
, txtlen
);
966 t
->text
[txtlen
] = '\0';
971 static Token
*delete_Token(Token
* t
)
973 Token
*next
= t
->next
;
975 t
->next
= freeTokens
;
981 * Convert a line of tokens back into text.
982 * If expand_locals is not zero, identifiers of the form "%$*xxx"
983 * will be transformed into ..@ctxnum.xxx
985 static char *detoken(Token
* tlist
, int expand_locals
)
993 for (t
= tlist
; t
; t
= t
->next
) {
994 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
995 char *p
= getenv(t
->text
+ 2);
998 t
->text
= nasm_strdup(p
);
1002 /* Expand local macros here and not during preprocessing */
1003 if (expand_locals
&&
1004 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1005 t
->text
[0] == '%' && t
->text
[1] == '$') {
1006 Context
*ctx
= get_ctx(t
->text
, false);
1009 char *p
, *q
= t
->text
+ 2;
1011 q
+= strspn(q
, "$");
1012 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1013 p
= nasm_strcat(buffer
, q
);
1018 if (t
->type
== TOK_WHITESPACE
) {
1020 } else if (t
->text
) {
1021 len
+= strlen(t
->text
);
1024 p
= line
= nasm_malloc(len
+ 1);
1025 for (t
= tlist
; t
; t
= t
->next
) {
1026 if (t
->type
== TOK_WHITESPACE
) {
1028 } else if (t
->text
) {
1039 * A scanner, suitable for use by the expression evaluator, which
1040 * operates on a line of Tokens. Expects a pointer to a pointer to
1041 * the first token in the line to be passed in as its private_data
1044 * FIX: This really needs to be unified with stdscan.
1046 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1048 Token
**tlineptr
= private_data
;
1050 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1054 *tlineptr
= tline
? tline
->next
: NULL
;
1056 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1057 tline
->type
== TOK_COMMENT
));
1060 return tokval
->t_type
= TOKEN_EOS
;
1062 tokval
->t_charptr
= tline
->text
;
1064 if (tline
->text
[0] == '$' && !tline
->text
[1])
1065 return tokval
->t_type
= TOKEN_HERE
;
1066 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1067 return tokval
->t_type
= TOKEN_BASE
;
1069 if (tline
->type
== TOK_ID
) {
1070 p
= tokval
->t_charptr
= tline
->text
;
1072 tokval
->t_charptr
++;
1073 return tokval
->t_type
= TOKEN_ID
;
1076 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1077 if (r
> p
+MAX_KEYWORD
)
1078 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1082 /* right, so we have an identifier sitting in temp storage. now,
1083 * is it actually a register or instruction name, or what? */
1084 return nasm_token_hash(ourcopy
, tokval
);
1087 if (tline
->type
== TOK_NUMBER
) {
1089 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1091 return tokval
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
1092 tokval
->t_charptr
= tline
->text
;
1093 return tokval
->t_type
= TOKEN_NUM
;
1096 if (tline
->type
== TOK_FLOAT
) {
1097 return tokval
->t_type
= TOKEN_FLOAT
;
1100 if (tline
->type
== TOK_STRING
) {
1109 if (l
== 0 || r
[l
- 1] != q
)
1110 return tokval
->t_type
= TOKEN_ERRNUM
;
1111 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1113 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1114 tokval
->t_charptr
= NULL
;
1115 return tokval
->t_type
= TOKEN_NUM
;
1118 if (tline
->type
== TOK_OTHER
) {
1119 if (!strcmp(tline
->text
, "<<"))
1120 return tokval
->t_type
= TOKEN_SHL
;
1121 if (!strcmp(tline
->text
, ">>"))
1122 return tokval
->t_type
= TOKEN_SHR
;
1123 if (!strcmp(tline
->text
, "//"))
1124 return tokval
->t_type
= TOKEN_SDIV
;
1125 if (!strcmp(tline
->text
, "%%"))
1126 return tokval
->t_type
= TOKEN_SMOD
;
1127 if (!strcmp(tline
->text
, "=="))
1128 return tokval
->t_type
= TOKEN_EQ
;
1129 if (!strcmp(tline
->text
, "<>"))
1130 return tokval
->t_type
= TOKEN_NE
;
1131 if (!strcmp(tline
->text
, "!="))
1132 return tokval
->t_type
= TOKEN_NE
;
1133 if (!strcmp(tline
->text
, "<="))
1134 return tokval
->t_type
= TOKEN_LE
;
1135 if (!strcmp(tline
->text
, ">="))
1136 return tokval
->t_type
= TOKEN_GE
;
1137 if (!strcmp(tline
->text
, "&&"))
1138 return tokval
->t_type
= TOKEN_DBL_AND
;
1139 if (!strcmp(tline
->text
, "^^"))
1140 return tokval
->t_type
= TOKEN_DBL_XOR
;
1141 if (!strcmp(tline
->text
, "||"))
1142 return tokval
->t_type
= TOKEN_DBL_OR
;
1146 * We have no other options: just return the first character of
1149 return tokval
->t_type
= tline
->text
[0];
1153 * Compare a string to the name of an existing macro; this is a
1154 * simple wrapper which calls either strcmp or nasm_stricmp
1155 * depending on the value of the `casesense' parameter.
1157 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1159 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1163 * Return the Context structure associated with a %$ token. Return
1164 * NULL, having _already_ reported an error condition, if the
1165 * context stack isn't deep enough for the supplied number of $
1167 * If all_contexts == true, contexts that enclose current are
1168 * also scanned for such smacro, until it is found; if not -
1169 * only the context that directly results from the number of $'s
1170 * in variable's name.
1172 static Context
*get_ctx(char *name
, bool all_contexts
)
1178 if (!name
|| name
[0] != '%' || name
[1] != '$')
1182 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1186 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1188 /* i--; Lino - 02/25/02 */
1191 error(ERR_NONFATAL
, "`%s': context stack is only"
1192 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1199 /* Search for this smacro in found context */
1202 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1213 * Open an include file. This routine must always return a valid
1214 * file pointer if it returns - it's responsible for throwing an
1215 * ERR_FATAL and bombing out completely if not. It should also try
1216 * the include path one by one until it finds the file or reaches
1217 * the end of the path.
1219 static FILE *inc_fopen(char *file
)
1222 char *prefix
= "", *combine
;
1223 IncPath
*ip
= ipath
;
1224 static int namelen
= 0;
1225 int len
= strlen(file
);
1228 combine
= nasm_malloc(strlen(prefix
) + len
+ 1);
1229 strcpy(combine
, prefix
);
1230 strcat(combine
, file
);
1231 fp
= fopen(combine
, "r");
1232 if (pass
== 0 && fp
) {
1233 namelen
+= strlen(combine
) + 1;
1238 printf(" %s", combine
);
1249 /* -MG given and file not found */
1251 namelen
+= strlen(file
) + 1;
1256 printf(" %s", file
);
1262 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1263 return NULL
; /* never reached - placate compilers */
1267 * Search for a key in the hash index; adding it if necessary
1268 * (in which case we initialize the data pointer to NULL.)
1271 hash_findi_add(struct hash_table
*hash
, const char *str
)
1273 struct hash_insert hi
;
1277 r
= hash_findi(hash
, str
, &hi
);
1281 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
1282 return hash_add(&hi
, strx
, NULL
);
1286 * Like hash_findi, but returns the data element rather than a pointer
1287 * to it. Used only when not adding a new element, hence no third
1291 hash_findix(struct hash_table
*hash
, const char *str
)
1295 p
= hash_findi(hash
, str
, NULL
);
1296 return p
? *p
: NULL
;
1300 * Determine if we should warn on defining a single-line macro of
1301 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1302 * return true if _any_ single-line macro of that name is defined.
1303 * Otherwise, will return true if a single-line macro with either
1304 * `nparam' or no parameters is defined.
1306 * If a macro with precisely the right number of parameters is
1307 * defined, or nparam is -1, the address of the definition structure
1308 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1309 * is NULL, no action will be taken regarding its contents, and no
1312 * Note that this is also called with nparam zero to resolve
1315 * If you already know which context macro belongs to, you can pass
1316 * the context pointer as first parameter; if you won't but name begins
1317 * with %$ the context will be automatically computed. If all_contexts
1318 * is true, macro will be searched in outer contexts as well.
1321 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1328 } else if (name
[0] == '%' && name
[1] == '$') {
1330 ctx
= get_ctx(name
, false);
1332 return false; /* got to return _something_ */
1335 m
= (SMacro
*) hash_findix(smacros
, name
);
1339 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1340 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1342 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1356 * Count and mark off the parameters in a multi-line macro call.
1357 * This is called both from within the multi-line macro expansion
1358 * code, and also to mark off the default parameters when provided
1359 * in a %macro definition line.
1361 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1363 int paramsize
, brace
;
1365 *nparam
= paramsize
= 0;
1368 if (*nparam
>= paramsize
) {
1369 paramsize
+= PARAM_DELTA
;
1370 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1374 if (tok_is_(t
, "{"))
1376 (*params
)[(*nparam
)++] = t
;
1377 while (tok_isnt_(t
, brace
? "}" : ","))
1379 if (t
) { /* got a comma/brace */
1383 * Now we've found the closing brace, look further
1387 if (tok_isnt_(t
, ",")) {
1389 "braces do not enclose all of macro parameter");
1390 while (tok_isnt_(t
, ","))
1394 t
= t
->next
; /* eat the comma */
1401 * Determine whether one of the various `if' conditions is true or
1404 * We must free the tline we get passed.
1406 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1408 enum pp_conditional i
= PP_COND(ct
);
1410 Token
*t
, *tt
, **tptr
, *origline
;
1411 struct tokenval tokval
;
1413 enum pp_token_type needtype
;
1419 j
= false; /* have we matched yet? */
1420 while (cstk
&& tline
) {
1422 if (!tline
|| tline
->type
!= TOK_ID
) {
1424 "`%s' expects context identifiers", pp_directives
[ct
]);
1425 free_tlist(origline
);
1428 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1430 tline
= tline
->next
;
1435 j
= false; /* have we matched yet? */
1438 if (!tline
|| (tline
->type
!= TOK_ID
&&
1439 (tline
->type
!= TOK_PREPROC_ID
||
1440 tline
->text
[1] != '$'))) {
1442 "`%s' expects macro identifiers", pp_directives
[ct
]);
1445 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1447 tline
= tline
->next
;
1453 tline
= expand_smacro(tline
);
1455 while (tok_isnt_(tt
, ","))
1459 "`%s' expects two comma-separated arguments",
1464 j
= true; /* assume equality unless proved not */
1465 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1466 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1467 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1471 if (t
->type
== TOK_WHITESPACE
) {
1475 if (tt
->type
== TOK_WHITESPACE
) {
1479 if (tt
->type
!= t
->type
) {
1480 j
= false; /* found mismatching tokens */
1483 /* Unify surrounding quotes for strings */
1484 if (t
->type
== TOK_STRING
) {
1485 tt
->text
[0] = t
->text
[0];
1486 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1488 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1489 j
= false; /* found mismatching tokens */
1496 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1497 j
= false; /* trailing gunk on one end or other */
1503 MMacro searching
, *mmac
;
1505 tline
= tline
->next
;
1507 tline
= expand_id(tline
);
1508 if (!tok_type_(tline
, TOK_ID
)) {
1510 "`%s' expects a macro name", pp_directives
[ct
]);
1513 searching
.name
= nasm_strdup(tline
->text
);
1514 searching
.casesense
= true;
1515 searching
.plus
= false;
1516 searching
.nolist
= false;
1517 searching
.in_progress
= 0;
1518 searching
.rep_nest
= NULL
;
1519 searching
.nparam_min
= 0;
1520 searching
.nparam_max
= INT_MAX
;
1521 tline
= expand_smacro(tline
->next
);
1524 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1526 "`%s' expects a parameter count or nothing",
1529 searching
.nparam_min
= searching
.nparam_max
=
1530 readnum(tline
->text
, &j
);
1533 "unable to parse parameter count `%s'",
1536 if (tline
&& tok_is_(tline
->next
, "-")) {
1537 tline
= tline
->next
->next
;
1538 if (tok_is_(tline
, "*"))
1539 searching
.nparam_max
= INT_MAX
;
1540 else if (!tok_type_(tline
, TOK_NUMBER
))
1542 "`%s' expects a parameter count after `-'",
1545 searching
.nparam_max
= readnum(tline
->text
, &j
);
1548 "unable to parse parameter count `%s'",
1550 if (searching
.nparam_min
> searching
.nparam_max
)
1552 "minimum parameter count exceeds maximum");
1555 if (tline
&& tok_is_(tline
->next
, "+")) {
1556 tline
= tline
->next
;
1557 searching
.plus
= true;
1559 mmac
= (MMacro
*) hash_findix(mmacros
, searching
.name
);
1561 if (!strcmp(mmac
->name
, searching
.name
) &&
1562 (mmac
->nparam_min
<= searching
.nparam_max
1564 && (searching
.nparam_min
<= mmac
->nparam_max
1571 nasm_free(searching
.name
);
1580 needtype
= TOK_NUMBER
;
1583 needtype
= TOK_STRING
;
1587 tline
= expand_smacro(tline
);
1590 while (tok_type_(t
, TOK_WHITESPACE
) ||
1591 (needtype
== TOK_NUMBER
&&
1592 tok_type_(t
, TOK_OTHER
) &&
1593 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1597 j
= t
&& t
->type
== needtype
;
1601 t
= tline
= expand_smacro(tline
);
1603 tokval
.t_type
= TOKEN_INVALID
;
1604 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1605 NULL
, pass
| CRITICAL
, error
, NULL
);
1610 "trailing garbage after expression ignored");
1611 if (!is_simple(evalresult
)) {
1613 "non-constant value given to `%s'", pp_directives
[ct
]);
1616 j
= reloc_value(evalresult
) != 0;
1621 "preprocessor directive `%s' not yet implemented",
1626 free_tlist(origline
);
1627 return j
^ PP_NEGATIVE(ct
);
1630 free_tlist(origline
);
1635 * Expand macros in a string. Used in %error and %include directives.
1636 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1637 * The returned variable should ALWAYS be freed after usage.
1639 void expand_macros_in_string(char **p
)
1641 Token
*line
= tokenize(*p
);
1642 line
= expand_smacro(line
);
1643 *p
= detoken(line
, false);
1647 * Common code for defining an smacro
1649 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1650 int nparam
, Token
*expansion
)
1652 SMacro
*smac
, **smhead
;
1654 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1657 "single-line macro `%s' defined both with and"
1658 " without parameters", mname
);
1660 /* Some instances of the old code considered this a failure,
1661 some others didn't. What is the right thing to do here? */
1662 free_tlist(expansion
);
1663 return false; /* Failure */
1666 * We're redefining, so we have to take over an
1667 * existing SMacro structure. This means freeing
1668 * what was already in it.
1670 nasm_free(smac
->name
);
1671 free_tlist(smac
->expansion
);
1675 smhead
= (SMacro
**) hash_findi_add(smacros
, mname
);
1677 smhead
= &ctx
->localmac
;
1679 smac
= nasm_malloc(sizeof(SMacro
));
1680 smac
->next
= *smhead
;
1683 smac
->name
= nasm_strdup(mname
);
1684 smac
->casesense
= casesense
;
1685 smac
->nparam
= nparam
;
1686 smac
->expansion
= expansion
;
1687 smac
->in_progress
= false;
1688 return true; /* Success */
1692 * Undefine an smacro
1694 static void undef_smacro(Context
*ctx
, const char *mname
)
1696 SMacro
**smhead
, *s
, **sp
;
1699 smhead
= (SMacro
**) hash_findi(smacros
, mname
, NULL
);
1701 smhead
= &ctx
->localmac
;
1705 * We now have a macro name... go hunt for it.
1708 while ((s
= *sp
) != NULL
) {
1709 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1712 free_tlist(s
->expansion
);
1722 * Decode a size directive
1724 static int parse_size(const char *str
) {
1725 static const char *size_names
[] =
1726 { "byte", "dword", "oword", "qword", "tword", "word" };
1727 static const int sizes
[] =
1728 { 0, 1, 4, 16, 8, 10, 2 };
1730 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1734 * find and process preprocessor directive in passed line
1735 * Find out if a line contains a preprocessor directive, and deal
1738 * If a directive _is_ found, it is the responsibility of this routine
1739 * (and not the caller) to free_tlist() the line.
1741 * @param tline a pointer to the current tokeninzed line linked list
1742 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1745 static int do_directive(Token
* tline
)
1747 enum preproc_token i
;
1759 MMacro
*mmac
, **mmhead
;
1760 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1762 struct tokenval tokval
;
1764 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1770 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1771 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1772 || tline
->text
[1] == '!'))
1773 return NO_DIRECTIVE_FOUND
;
1775 i
= pp_token_hash(tline
->text
);
1778 * If we're in a non-emitting branch of a condition construct,
1779 * or walking to the end of an already terminated %rep block,
1780 * we should ignore all directives except for condition
1783 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1784 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1785 return NO_DIRECTIVE_FOUND
;
1789 * If we're defining a macro or reading a %rep block, we should
1790 * ignore all directives except for %macro/%imacro (which
1791 * generate an error), %endm/%endmacro, and (only if we're in a
1792 * %rep block) %endrep. If we're in a %rep block, another %rep
1793 * causes an error, so should be let through.
1795 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1796 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1797 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1798 return NO_DIRECTIVE_FOUND
;
1803 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1805 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1808 /* Directive to tell NASM what the default stack size is. The
1809 * default is for a 16-bit stack, and this can be overriden with
1811 * the following form:
1813 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1815 tline
= tline
->next
;
1816 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1817 tline
= tline
->next
;
1818 if (!tline
|| tline
->type
!= TOK_ID
) {
1819 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1820 free_tlist(origline
);
1821 return DIRECTIVE_FOUND
;
1823 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1824 /* All subsequent ARG directives are for a 32-bit stack */
1826 StackPointer
= "ebp";
1829 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1830 /* All subsequent ARG directives are for a 64-bit stack */
1832 StackPointer
= "rbp";
1835 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1836 /* All subsequent ARG directives are for a 16-bit stack,
1837 * far function call.
1840 StackPointer
= "bp";
1843 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1844 /* All subsequent ARG directives are for a 16-bit stack,
1845 * far function call. We don't support near functions.
1848 StackPointer
= "bp";
1852 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1853 free_tlist(origline
);
1854 return DIRECTIVE_FOUND
;
1856 free_tlist(origline
);
1857 return DIRECTIVE_FOUND
;
1860 /* TASM like ARG directive to define arguments to functions, in
1861 * the following form:
1863 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1867 char *arg
, directive
[256];
1868 int size
= StackSize
;
1870 /* Find the argument name */
1871 tline
= tline
->next
;
1872 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1873 tline
= tline
->next
;
1874 if (!tline
|| tline
->type
!= TOK_ID
) {
1875 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1876 free_tlist(origline
);
1877 return DIRECTIVE_FOUND
;
1881 /* Find the argument size type */
1882 tline
= tline
->next
;
1883 if (!tline
|| tline
->type
!= TOK_OTHER
1884 || tline
->text
[0] != ':') {
1886 "Syntax error processing `%%arg' directive");
1887 free_tlist(origline
);
1888 return DIRECTIVE_FOUND
;
1890 tline
= tline
->next
;
1891 if (!tline
|| tline
->type
!= TOK_ID
) {
1892 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1893 free_tlist(origline
);
1894 return DIRECTIVE_FOUND
;
1897 /* Allow macro expansion of type parameter */
1898 tt
= tokenize(tline
->text
);
1899 tt
= expand_smacro(tt
);
1900 size
= parse_size(tt
->text
);
1903 "Invalid size type for `%%arg' missing directive");
1905 free_tlist(origline
);
1906 return DIRECTIVE_FOUND
;
1910 /* Round up to even stack slots */
1911 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1913 /* Now define the macro for the argument */
1914 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1915 arg
, StackPointer
, offset
);
1916 do_directive(tokenize(directive
));
1919 /* Move to the next argument in the list */
1920 tline
= tline
->next
;
1921 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1922 tline
= tline
->next
;
1923 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1925 free_tlist(origline
);
1926 return DIRECTIVE_FOUND
;
1929 /* TASM like LOCAL directive to define local variables for a
1930 * function, in the following form:
1932 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1934 * The '= LocalSize' at the end is ignored by NASM, but is
1935 * required by TASM to define the local parameter size (and used
1936 * by the TASM macro package).
1938 offset
= LocalOffset
;
1940 char *local
, directive
[256];
1941 int size
= StackSize
;
1943 /* Find the argument name */
1944 tline
= tline
->next
;
1945 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1946 tline
= tline
->next
;
1947 if (!tline
|| tline
->type
!= TOK_ID
) {
1949 "`%%local' missing argument parameter");
1950 free_tlist(origline
);
1951 return DIRECTIVE_FOUND
;
1953 local
= tline
->text
;
1955 /* Find the argument size type */
1956 tline
= tline
->next
;
1957 if (!tline
|| tline
->type
!= TOK_OTHER
1958 || tline
->text
[0] != ':') {
1960 "Syntax error processing `%%local' directive");
1961 free_tlist(origline
);
1962 return DIRECTIVE_FOUND
;
1964 tline
= tline
->next
;
1965 if (!tline
|| tline
->type
!= TOK_ID
) {
1967 "`%%local' missing size type parameter");
1968 free_tlist(origline
);
1969 return DIRECTIVE_FOUND
;
1972 /* Allow macro expansion of type parameter */
1973 tt
= tokenize(tline
->text
);
1974 tt
= expand_smacro(tt
);
1975 size
= parse_size(tt
->text
);
1978 "Invalid size type for `%%local' missing directive");
1980 free_tlist(origline
);
1981 return DIRECTIVE_FOUND
;
1985 /* Round up to even stack slots */
1986 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1988 offset
+= size
; /* Negative offset, increment before */
1990 /* Now define the macro for the argument */
1991 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
1992 local
, StackPointer
, offset
);
1993 do_directive(tokenize(directive
));
1995 /* Now define the assign to setup the enter_c macro correctly */
1996 snprintf(directive
, sizeof(directive
),
1997 "%%assign %%$localsize %%$localsize+%d", size
);
1998 do_directive(tokenize(directive
));
2000 /* Move to the next argument in the list */
2001 tline
= tline
->next
;
2002 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2003 tline
= tline
->next
;
2004 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2005 LocalOffset
= offset
;
2006 free_tlist(origline
);
2007 return DIRECTIVE_FOUND
;
2011 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2014 free_tlist(origline
);
2015 return DIRECTIVE_FOUND
;
2018 tline
= tline
->next
;
2020 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2021 tline
->type
!= TOK_INTERNAL_STRING
)) {
2022 error(ERR_NONFATAL
, "`%%include' expects a file name");
2023 free_tlist(origline
);
2024 return DIRECTIVE_FOUND
; /* but we did _something_ */
2028 "trailing garbage after `%%include' ignored");
2029 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2030 p
= tline
->text
+ 1; /* point past the quote to the name */
2031 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2033 p
= tline
->text
; /* internal_string is easier */
2034 expand_macros_in_string(&p
);
2035 inc
= nasm_malloc(sizeof(Include
));
2038 inc
->fp
= inc_fopen(p
);
2039 if (!inc
->fp
&& pass
== 0) {
2040 /* -MG given but file not found */
2043 inc
->fname
= src_set_fname(p
);
2044 inc
->lineno
= src_set_linnum(0);
2046 inc
->expansion
= NULL
;
2049 list
->uplevel(LIST_INCLUDE
);
2051 free_tlist(origline
);
2052 return DIRECTIVE_FOUND
;
2055 tline
= tline
->next
;
2057 tline
= expand_id(tline
);
2058 if (!tok_type_(tline
, TOK_ID
)) {
2059 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2060 free_tlist(origline
);
2061 return DIRECTIVE_FOUND
; /* but we did _something_ */
2064 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2065 ctx
= nasm_malloc(sizeof(Context
));
2067 ctx
->localmac
= NULL
;
2068 ctx
->name
= nasm_strdup(tline
->text
);
2069 ctx
->number
= unique
++;
2071 free_tlist(origline
);
2075 tline
= tline
->next
;
2077 tline
= expand_id(tline
);
2078 if (!tok_type_(tline
, TOK_ID
)) {
2079 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2080 free_tlist(origline
);
2081 return DIRECTIVE_FOUND
; /* but we did _something_ */
2084 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2086 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2088 nasm_free(cstk
->name
);
2089 cstk
->name
= nasm_strdup(tline
->text
);
2091 free_tlist(origline
);
2096 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2098 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2101 free_tlist(origline
);
2105 tline
->next
= expand_smacro(tline
->next
);
2106 tline
= tline
->next
;
2108 if (tok_type_(tline
, TOK_STRING
)) {
2109 p
= tline
->text
+ 1; /* point past the quote to the name */
2110 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2111 expand_macros_in_string(&p
);
2112 error(ERR_NONFATAL
, "%s", p
);
2115 p
= detoken(tline
, false);
2116 error(ERR_WARNING
, "%s", p
);
2119 free_tlist(origline
);
2123 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2126 j
= if_condition(tline
->next
, i
);
2127 tline
->next
= NULL
; /* it got freed */
2128 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2130 cond
= nasm_malloc(sizeof(Cond
));
2131 cond
->next
= istk
->conds
;
2134 free_tlist(origline
);
2135 return DIRECTIVE_FOUND
;
2139 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2140 if (emitting(istk
->conds
->state
)
2141 || istk
->conds
->state
== COND_NEVER
)
2142 istk
->conds
->state
= COND_NEVER
;
2145 * IMPORTANT: In the case of %if, we will already have
2146 * called expand_mmac_params(); however, if we're
2147 * processing an %elif we must have been in a
2148 * non-emitting mode, which would have inhibited
2149 * the normal invocation of expand_mmac_params(). Therefore,
2150 * we have to do it explicitly here.
2152 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2153 tline
->next
= NULL
; /* it got freed */
2154 istk
->conds
->state
=
2155 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2157 free_tlist(origline
);
2158 return DIRECTIVE_FOUND
;
2162 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2164 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2165 if (emitting(istk
->conds
->state
)
2166 || istk
->conds
->state
== COND_NEVER
)
2167 istk
->conds
->state
= COND_ELSE_FALSE
;
2169 istk
->conds
->state
= COND_ELSE_TRUE
;
2170 free_tlist(origline
);
2171 return DIRECTIVE_FOUND
;
2175 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2177 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2179 istk
->conds
= cond
->next
;
2181 free_tlist(origline
);
2182 return DIRECTIVE_FOUND
;
2188 "`%%%smacro': already defining a macro",
2189 (i
== PP_IMACRO
? "i" : ""));
2190 tline
= tline
->next
;
2192 tline
= expand_id(tline
);
2193 if (!tok_type_(tline
, TOK_ID
)) {
2195 "`%%%smacro' expects a macro name",
2196 (i
== PP_IMACRO
? "i" : ""));
2197 return DIRECTIVE_FOUND
;
2199 defining
= nasm_malloc(sizeof(MMacro
));
2200 defining
->name
= nasm_strdup(tline
->text
);
2201 defining
->casesense
= (i
== PP_MACRO
);
2202 defining
->plus
= false;
2203 defining
->nolist
= false;
2204 defining
->in_progress
= 0;
2205 defining
->rep_nest
= NULL
;
2206 tline
= expand_smacro(tline
->next
);
2208 if (!tok_type_(tline
, TOK_NUMBER
)) {
2210 "`%%%smacro' expects a parameter count",
2211 (i
== PP_IMACRO
? "i" : ""));
2212 defining
->nparam_min
= defining
->nparam_max
= 0;
2214 defining
->nparam_min
= defining
->nparam_max
=
2215 readnum(tline
->text
, &err
);
2218 "unable to parse parameter count `%s'", tline
->text
);
2220 if (tline
&& tok_is_(tline
->next
, "-")) {
2221 tline
= tline
->next
->next
;
2222 if (tok_is_(tline
, "*"))
2223 defining
->nparam_max
= INT_MAX
;
2224 else if (!tok_type_(tline
, TOK_NUMBER
))
2226 "`%%%smacro' expects a parameter count after `-'",
2227 (i
== PP_IMACRO
? "i" : ""));
2229 defining
->nparam_max
= readnum(tline
->text
, &err
);
2232 "unable to parse parameter count `%s'",
2234 if (defining
->nparam_min
> defining
->nparam_max
)
2236 "minimum parameter count exceeds maximum");
2239 if (tline
&& tok_is_(tline
->next
, "+")) {
2240 tline
= tline
->next
;
2241 defining
->plus
= true;
2243 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2244 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2245 tline
= tline
->next
;
2246 defining
->nolist
= true;
2248 mmac
= (MMacro
*) hash_findix(mmacros
, defining
->name
);
2250 if (!strcmp(mmac
->name
, defining
->name
) &&
2251 (mmac
->nparam_min
<= defining
->nparam_max
2253 && (defining
->nparam_min
<= mmac
->nparam_max
2256 "redefining multi-line macro `%s'", defining
->name
);
2262 * Handle default parameters.
2264 if (tline
&& tline
->next
) {
2265 defining
->dlist
= tline
->next
;
2267 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2268 &defining
->defaults
);
2270 defining
->dlist
= NULL
;
2271 defining
->defaults
= NULL
;
2273 defining
->expansion
= NULL
;
2274 free_tlist(origline
);
2275 return DIRECTIVE_FOUND
;
2280 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2281 return DIRECTIVE_FOUND
;
2283 mmhead
= (MMacro
**) hash_findi_add(mmacros
, defining
->name
);
2284 defining
->next
= *mmhead
;
2287 free_tlist(origline
);
2288 return DIRECTIVE_FOUND
;
2291 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2292 tline
= tline
->next
;
2293 if (tline
->next
== NULL
) {
2294 free_tlist(origline
);
2295 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2296 return DIRECTIVE_FOUND
;
2298 t
= expand_smacro(tline
->next
);
2300 free_tlist(origline
);
2303 tokval
.t_type
= TOKEN_INVALID
;
2305 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2308 return DIRECTIVE_FOUND
;
2311 "trailing garbage after expression ignored");
2312 if (!is_simple(evalresult
)) {
2313 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2314 return DIRECTIVE_FOUND
;
2317 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2318 mmac
= mmac
->next_active
;
2320 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2321 } else if (mmac
->nparam
== 0) {
2323 "`%%rotate' invoked within macro without parameters");
2325 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2327 rotate
%= (int)mmac
->nparam
;
2329 rotate
+= mmac
->nparam
;
2331 mmac
->rotate
= rotate
;
2333 return DIRECTIVE_FOUND
;
2338 tline
= tline
->next
;
2339 } while (tok_type_(tline
, TOK_WHITESPACE
));
2341 if (tok_type_(tline
, TOK_ID
) &&
2342 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2345 tline
= tline
->next
;
2346 } while (tok_type_(tline
, TOK_WHITESPACE
));
2350 t
= expand_smacro(tline
);
2352 tokval
.t_type
= TOKEN_INVALID
;
2354 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2356 free_tlist(origline
);
2357 return DIRECTIVE_FOUND
;
2361 "trailing garbage after expression ignored");
2362 if (!is_simple(evalresult
)) {
2363 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2364 return DIRECTIVE_FOUND
;
2366 count
= reloc_value(evalresult
) + 1;
2368 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2371 free_tlist(origline
);
2373 tmp_defining
= defining
;
2374 defining
= nasm_malloc(sizeof(MMacro
));
2375 defining
->name
= NULL
; /* flags this macro as a %rep block */
2376 defining
->casesense
= false;
2377 defining
->plus
= false;
2378 defining
->nolist
= nolist
;
2379 defining
->in_progress
= count
;
2380 defining
->nparam_min
= defining
->nparam_max
= 0;
2381 defining
->defaults
= NULL
;
2382 defining
->dlist
= NULL
;
2383 defining
->expansion
= NULL
;
2384 defining
->next_active
= istk
->mstk
;
2385 defining
->rep_nest
= tmp_defining
;
2386 return DIRECTIVE_FOUND
;
2389 if (!defining
|| defining
->name
) {
2390 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2391 return DIRECTIVE_FOUND
;
2395 * Now we have a "macro" defined - although it has no name
2396 * and we won't be entering it in the hash tables - we must
2397 * push a macro-end marker for it on to istk->expansion.
2398 * After that, it will take care of propagating itself (a
2399 * macro-end marker line for a macro which is really a %rep
2400 * block will cause the macro to be re-expanded, complete
2401 * with another macro-end marker to ensure the process
2402 * continues) until the whole expansion is forcibly removed
2403 * from istk->expansion by a %exitrep.
2405 l
= nasm_malloc(sizeof(Line
));
2406 l
->next
= istk
->expansion
;
2407 l
->finishes
= defining
;
2409 istk
->expansion
= l
;
2411 istk
->mstk
= defining
;
2413 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2414 tmp_defining
= defining
;
2415 defining
= defining
->rep_nest
;
2416 free_tlist(origline
);
2417 return DIRECTIVE_FOUND
;
2421 * We must search along istk->expansion until we hit a
2422 * macro-end marker for a macro with no name. Then we set
2423 * its `in_progress' flag to 0.
2425 for (l
= istk
->expansion
; l
; l
= l
->next
)
2426 if (l
->finishes
&& !l
->finishes
->name
)
2430 l
->finishes
->in_progress
= 0;
2432 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2433 free_tlist(origline
);
2434 return DIRECTIVE_FOUND
;
2440 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2442 tline
= tline
->next
;
2444 tline
= expand_id(tline
);
2445 if (!tline
|| (tline
->type
!= TOK_ID
&&
2446 (tline
->type
!= TOK_PREPROC_ID
||
2447 tline
->text
[1] != '$'))) {
2448 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2450 free_tlist(origline
);
2451 return DIRECTIVE_FOUND
;
2454 ctx
= get_ctx(tline
->text
, false);
2456 mname
= tline
->text
;
2458 param_start
= tline
= tline
->next
;
2461 /* Expand the macro definition now for %xdefine and %ixdefine */
2462 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2463 tline
= expand_smacro(tline
);
2465 if (tok_is_(tline
, "(")) {
2467 * This macro has parameters.
2470 tline
= tline
->next
;
2474 error(ERR_NONFATAL
, "parameter identifier expected");
2475 free_tlist(origline
);
2476 return DIRECTIVE_FOUND
;
2478 if (tline
->type
!= TOK_ID
) {
2480 "`%s': parameter identifier expected",
2482 free_tlist(origline
);
2483 return DIRECTIVE_FOUND
;
2485 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2486 tline
= tline
->next
;
2488 if (tok_is_(tline
, ",")) {
2489 tline
= tline
->next
;
2492 if (!tok_is_(tline
, ")")) {
2494 "`)' expected to terminate macro template");
2495 free_tlist(origline
);
2496 return DIRECTIVE_FOUND
;
2501 tline
= tline
->next
;
2503 if (tok_type_(tline
, TOK_WHITESPACE
))
2504 last
= tline
, tline
= tline
->next
;
2509 if (t
->type
== TOK_ID
) {
2510 for (tt
= param_start
; tt
; tt
= tt
->next
)
2511 if (tt
->type
>= TOK_SMAC_PARAM
&&
2512 !strcmp(tt
->text
, t
->text
))
2516 t
->next
= macro_start
;
2521 * Good. We now have a macro name, a parameter count, and a
2522 * token list (in reverse order) for an expansion. We ought
2523 * to be OK just to create an SMacro, store it, and let
2524 * free_tlist have the rest of the line (which we have
2525 * carefully re-terminated after chopping off the expansion
2528 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2529 free_tlist(origline
);
2530 return DIRECTIVE_FOUND
;
2533 tline
= tline
->next
;
2535 tline
= expand_id(tline
);
2536 if (!tline
|| (tline
->type
!= TOK_ID
&&
2537 (tline
->type
!= TOK_PREPROC_ID
||
2538 tline
->text
[1] != '$'))) {
2539 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2540 free_tlist(origline
);
2541 return DIRECTIVE_FOUND
;
2545 "trailing garbage after macro name ignored");
2548 /* Find the context that symbol belongs to */
2549 ctx
= get_ctx(tline
->text
, false);
2550 undef_smacro(ctx
, tline
->text
);
2551 free_tlist(origline
);
2552 return DIRECTIVE_FOUND
;
2557 tline
= tline
->next
;
2559 tline
= expand_id(tline
);
2560 if (!tline
|| (tline
->type
!= TOK_ID
&&
2561 (tline
->type
!= TOK_PREPROC_ID
||
2562 tline
->text
[1] != '$'))) {
2564 "`%%strlen' expects a macro identifier as first parameter");
2565 free_tlist(origline
);
2566 return DIRECTIVE_FOUND
;
2568 ctx
= get_ctx(tline
->text
, false);
2570 mname
= tline
->text
;
2572 tline
= expand_smacro(tline
->next
);
2576 while (tok_type_(t
, TOK_WHITESPACE
))
2578 /* t should now point to the string */
2579 if (t
->type
!= TOK_STRING
) {
2581 "`%%strlen` requires string as second parameter");
2583 free_tlist(origline
);
2584 return DIRECTIVE_FOUND
;
2587 macro_start
= nasm_malloc(sizeof(*macro_start
));
2588 macro_start
->next
= NULL
;
2589 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2590 macro_start
->mac
= NULL
;
2593 * We now have a macro name, an implicit parameter count of
2594 * zero, and a numeric token to use as an expansion. Create
2595 * and store an SMacro.
2597 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2599 free_tlist(origline
);
2600 return DIRECTIVE_FOUND
;
2605 tline
= tline
->next
;
2607 tline
= expand_id(tline
);
2608 if (!tline
|| (tline
->type
!= TOK_ID
&&
2609 (tline
->type
!= TOK_PREPROC_ID
||
2610 tline
->text
[1] != '$'))) {
2612 "`%%substr' expects a macro identifier as first parameter");
2613 free_tlist(origline
);
2614 return DIRECTIVE_FOUND
;
2616 ctx
= get_ctx(tline
->text
, false);
2618 mname
= tline
->text
;
2620 tline
= expand_smacro(tline
->next
);
2624 while (tok_type_(t
, TOK_WHITESPACE
))
2627 /* t should now point to the string */
2628 if (t
->type
!= TOK_STRING
) {
2630 "`%%substr` requires string as second parameter");
2632 free_tlist(origline
);
2633 return DIRECTIVE_FOUND
;
2638 tokval
.t_type
= TOKEN_INVALID
;
2640 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2643 free_tlist(origline
);
2644 return DIRECTIVE_FOUND
;
2646 if (!is_simple(evalresult
)) {
2647 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2649 free_tlist(origline
);
2650 return DIRECTIVE_FOUND
;
2653 macro_start
= nasm_malloc(sizeof(*macro_start
));
2654 macro_start
->next
= NULL
;
2655 macro_start
->text
= nasm_strdup("'''");
2656 if (evalresult
->value
> 0
2657 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2658 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2660 macro_start
->text
[2] = '\0';
2662 macro_start
->type
= TOK_STRING
;
2663 macro_start
->mac
= NULL
;
2666 * We now have a macro name, an implicit parameter count of
2667 * zero, and a numeric token to use as an expansion. Create
2668 * and store an SMacro.
2670 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2672 free_tlist(origline
);
2673 return DIRECTIVE_FOUND
;
2677 casesense
= (i
== PP_ASSIGN
);
2679 tline
= tline
->next
;
2681 tline
= expand_id(tline
);
2682 if (!tline
|| (tline
->type
!= TOK_ID
&&
2683 (tline
->type
!= TOK_PREPROC_ID
||
2684 tline
->text
[1] != '$'))) {
2686 "`%%%sassign' expects a macro identifier",
2687 (i
== PP_IASSIGN
? "i" : ""));
2688 free_tlist(origline
);
2689 return DIRECTIVE_FOUND
;
2691 ctx
= get_ctx(tline
->text
, false);
2693 mname
= tline
->text
;
2695 tline
= expand_smacro(tline
->next
);
2700 tokval
.t_type
= TOKEN_INVALID
;
2702 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2705 free_tlist(origline
);
2706 return DIRECTIVE_FOUND
;
2711 "trailing garbage after expression ignored");
2713 if (!is_simple(evalresult
)) {
2715 "non-constant value given to `%%%sassign'",
2716 (i
== PP_IASSIGN
? "i" : ""));
2717 free_tlist(origline
);
2718 return DIRECTIVE_FOUND
;
2721 macro_start
= nasm_malloc(sizeof(*macro_start
));
2722 macro_start
->next
= NULL
;
2723 make_tok_num(macro_start
, reloc_value(evalresult
));
2724 macro_start
->mac
= NULL
;
2727 * We now have a macro name, an implicit parameter count of
2728 * zero, and a numeric token to use as an expansion. Create
2729 * and store an SMacro.
2731 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2732 free_tlist(origline
);
2733 return DIRECTIVE_FOUND
;
2737 * Syntax is `%line nnn[+mmm] [filename]'
2739 tline
= tline
->next
;
2741 if (!tok_type_(tline
, TOK_NUMBER
)) {
2742 error(ERR_NONFATAL
, "`%%line' expects line number");
2743 free_tlist(origline
);
2744 return DIRECTIVE_FOUND
;
2746 k
= readnum(tline
->text
, &err
);
2748 tline
= tline
->next
;
2749 if (tok_is_(tline
, "+")) {
2750 tline
= tline
->next
;
2751 if (!tok_type_(tline
, TOK_NUMBER
)) {
2752 error(ERR_NONFATAL
, "`%%line' expects line increment");
2753 free_tlist(origline
);
2754 return DIRECTIVE_FOUND
;
2756 m
= readnum(tline
->text
, &err
);
2757 tline
= tline
->next
;
2763 nasm_free(src_set_fname(detoken(tline
, false)));
2765 free_tlist(origline
);
2766 return DIRECTIVE_FOUND
;
2770 "preprocessor directive `%s' not yet implemented",
2774 return DIRECTIVE_FOUND
;
2778 * Ensure that a macro parameter contains a condition code and
2779 * nothing else. Return the condition code index if so, or -1
2782 static int find_cc(Token
* t
)
2788 return -1; /* Probably a %+ without a space */
2791 if (t
->type
!= TOK_ID
)
2795 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2799 j
= elements(conditions
);
2802 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2818 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2819 * %-n) and MMacro-local identifiers (%%foo).
2821 static Token
*expand_mmac_params(Token
* tline
)
2823 Token
*t
, *tt
, **tail
, *thead
;
2829 if (tline
->type
== TOK_PREPROC_ID
&&
2830 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2831 && tline
->text
[2]) || tline
->text
[1] == '%'
2832 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2834 int type
= 0, cc
; /* type = 0 to placate optimisers */
2841 tline
= tline
->next
;
2844 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2845 mac
= mac
->next_active
;
2847 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2849 switch (t
->text
[1]) {
2851 * We have to make a substitution of one of the
2852 * forms %1, %-1, %+1, %%foo, %0.
2856 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2857 text
= nasm_strdup(tmpbuf
);
2861 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2863 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
2866 n
= atoi(t
->text
+ 2) - 1;
2867 if (n
>= mac
->nparam
)
2870 if (mac
->nparam
> 1)
2871 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2872 tt
= mac
->params
[n
];
2877 "macro parameter %d is not a condition code",
2882 if (inverse_ccs
[cc
] == -1) {
2884 "condition code `%s' is not invertible",
2889 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2893 n
= atoi(t
->text
+ 2) - 1;
2894 if (n
>= mac
->nparam
)
2897 if (mac
->nparam
> 1)
2898 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2899 tt
= mac
->params
[n
];
2904 "macro parameter %d is not a condition code",
2909 text
= nasm_strdup(conditions
[cc
]);
2913 n
= atoi(t
->text
+ 1) - 1;
2914 if (n
>= mac
->nparam
)
2917 if (mac
->nparam
> 1)
2918 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2919 tt
= mac
->params
[n
];
2922 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2923 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2924 tail
= &(*tail
)->next
;
2928 text
= NULL
; /* we've done it here */
2944 tline
= tline
->next
;
2951 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2953 case TOK_WHITESPACE
:
2954 if (tt
->type
== TOK_WHITESPACE
) {
2955 t
->next
= delete_Token(tt
);
2959 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2960 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2963 t
->next
= delete_Token(tt
);
2967 if (tt
->type
== TOK_NUMBER
) {
2968 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2971 t
->next
= delete_Token(tt
);
2982 * Expand all single-line macro calls made in the given line.
2983 * Return the expanded version of the line. The original is deemed
2984 * to be destroyed in the process. (In reality we'll just move
2985 * Tokens from input to output a lot of the time, rather than
2986 * actually bothering to destroy and replicate.)
2988 #define DEADMAN_LIMIT (1 << 20)
2990 static Token
*expand_smacro(Token
* tline
)
2992 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2993 SMacro
*head
= NULL
, *m
;
2996 unsigned int nparam
, sparam
;
2997 int brackets
, rescan
;
2998 Token
*org_tline
= tline
;
3001 int deadman
= DEADMAN_LIMIT
;
3004 * Trick: we should avoid changing the start token pointer since it can
3005 * be contained in "next" field of other token. Because of this
3006 * we allocate a copy of first token and work with it; at the end of
3007 * routine we copy it back
3011 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3013 tline
->mac
= org_tline
->mac
;
3014 nasm_free(org_tline
->text
);
3015 org_tline
->text
= NULL
;
3022 while (tline
) { /* main token loop */
3024 error(ERR_NONFATAL
, "interminable macro recursion");
3028 if ((mname
= tline
->text
)) {
3029 /* if this token is a local macro, look in local context */
3030 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3031 ctx
= get_ctx(mname
, true);
3035 head
= (SMacro
*) hash_findix(smacros
, mname
);
3037 head
= ctx
->localmac
;
3040 * We've hit an identifier. As in is_mmacro below, we first
3041 * check whether the identifier is a single-line macro at
3042 * all, then think about checking for parameters if
3045 for (m
= head
; m
; m
= m
->next
)
3046 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3052 if (m
->nparam
== 0) {
3054 * Simple case: the macro is parameterless. Discard the
3055 * one token that the macro call took, and push the
3056 * expansion back on the to-do stack.
3058 if (!m
->expansion
) {
3059 if (!strcmp("__FILE__", m
->name
)) {
3061 src_get(&num
, &(tline
->text
));
3062 nasm_quote(&(tline
->text
));
3063 tline
->type
= TOK_STRING
;
3066 if (!strcmp("__LINE__", m
->name
)) {
3067 nasm_free(tline
->text
);
3068 make_tok_num(tline
, src_get_linnum());
3071 if (!strcmp("__BITS__", m
->name
)) {
3072 nasm_free(tline
->text
);
3073 make_tok_num(tline
, globalbits
);
3076 tline
= delete_Token(tline
);
3081 * Complicated case: at least one macro with this name
3082 * exists and takes parameters. We must find the
3083 * parameters in the call, count them, find the SMacro
3084 * that corresponds to that form of the macro call, and
3085 * substitute for the parameters when we expand. What a
3088 /*tline = tline->next;
3089 skip_white_(tline); */
3092 while (tok_type_(t
, TOK_SMAC_END
)) {
3093 t
->mac
->in_progress
= false;
3095 t
= tline
->next
= delete_Token(t
);
3098 } while (tok_type_(tline
, TOK_WHITESPACE
));
3099 if (!tok_is_(tline
, "(")) {
3101 * This macro wasn't called with parameters: ignore
3102 * the call. (Behaviour borrowed from gnu cpp.)
3111 sparam
= PARAM_DELTA
;
3112 params
= nasm_malloc(sparam
* sizeof(Token
*));
3113 params
[0] = tline
->next
;
3114 paramsize
= nasm_malloc(sparam
* sizeof(int));
3116 while (true) { /* parameter loop */
3118 * For some unusual expansions
3119 * which concatenates function call
3122 while (tok_type_(t
, TOK_SMAC_END
)) {
3123 t
->mac
->in_progress
= false;
3125 t
= tline
->next
= delete_Token(t
);
3131 "macro call expects terminating `)'");
3134 if (tline
->type
== TOK_WHITESPACE
3136 if (paramsize
[nparam
])
3139 params
[nparam
] = tline
->next
;
3140 continue; /* parameter loop */
3142 if (tline
->type
== TOK_OTHER
3143 && tline
->text
[1] == 0) {
3144 char ch
= tline
->text
[0];
3145 if (ch
== ',' && !paren
&& brackets
<= 0) {
3146 if (++nparam
>= sparam
) {
3147 sparam
+= PARAM_DELTA
;
3148 params
= nasm_realloc(params
,
3153 nasm_realloc(paramsize
,
3157 params
[nparam
] = tline
->next
;
3158 paramsize
[nparam
] = 0;
3160 continue; /* parameter loop */
3163 (brackets
> 0 || (brackets
== 0 &&
3164 !paramsize
[nparam
])))
3166 if (!(brackets
++)) {
3167 params
[nparam
] = tline
->next
;
3168 continue; /* parameter loop */
3171 if (ch
== '}' && brackets
> 0)
3172 if (--brackets
== 0) {
3174 continue; /* parameter loop */
3176 if (ch
== '(' && !brackets
)
3178 if (ch
== ')' && brackets
<= 0)
3184 error(ERR_NONFATAL
, "braces do not "
3185 "enclose all of macro parameter");
3187 paramsize
[nparam
] += white
+ 1;
3189 } /* parameter loop */
3191 while (m
&& (m
->nparam
!= nparam
||
3192 mstrcmp(m
->name
, mname
,
3196 error(ERR_WARNING
| ERR_WARN_MNP
,
3197 "macro `%s' exists, "
3198 "but not taking %d parameters",
3199 mstart
->text
, nparam
);
3202 if (m
&& m
->in_progress
)
3204 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3206 * Design question: should we handle !tline, which
3207 * indicates missing ')' here, or expand those
3208 * macros anyway, which requires the (t) test a few
3212 nasm_free(paramsize
);
3216 * Expand the macro: we are placed on the last token of the
3217 * call, so that we can easily split the call from the
3218 * following tokens. We also start by pushing an SMAC_END
3219 * token for the cycle removal.
3226 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3228 m
->in_progress
= true;
3230 for (t
= m
->expansion
; t
; t
= t
->next
) {
3231 if (t
->type
>= TOK_SMAC_PARAM
) {
3232 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3236 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3237 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3240 new_Token(tline
, ttt
->type
, ttt
->text
,
3247 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3253 * Having done that, get rid of the macro call, and clean
3254 * up the parameters.
3257 nasm_free(paramsize
);
3259 continue; /* main token loop */
3264 if (tline
->type
== TOK_SMAC_END
) {
3265 tline
->mac
->in_progress
= false;
3266 tline
= delete_Token(tline
);
3269 tline
= tline
->next
;
3277 * Now scan the entire line and look for successive TOK_IDs that resulted
3278 * after expansion (they can't be produced by tokenize()). The successive
3279 * TOK_IDs should be concatenated.
3280 * Also we look for %+ tokens and concatenate the tokens before and after
3281 * them (without white spaces in between).
3286 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3290 if (t
->next
->type
== TOK_ID
||
3291 t
->next
->type
== TOK_PREPROC_ID
||
3292 t
->next
->type
== TOK_NUMBER
) {
3293 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3295 t
->next
= delete_Token(t
->next
);
3298 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3299 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3300 strcmp(t
->next
->next
->text
, "%+") == 0) {
3301 /* free the next whitespace, the %+ token and next whitespace */
3303 for (i
= 1; i
<= 3; i
++) {
3305 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3307 t
->next
= delete_Token(t
->next
);
3312 /* If we concatenaded something, re-scan the line for macros */
3320 *org_tline
= *thead
;
3321 /* since we just gave text to org_line, don't free it */
3323 delete_Token(thead
);
3325 /* the expression expanded to empty line;
3326 we can't return NULL for some reasons
3327 we just set the line to a single WHITESPACE token. */
3328 memset(org_tline
, 0, sizeof(*org_tline
));
3329 org_tline
->text
= NULL
;
3330 org_tline
->type
= TOK_WHITESPACE
;
3339 * Similar to expand_smacro but used exclusively with macro identifiers
3340 * right before they are fetched in. The reason is that there can be
3341 * identifiers consisting of several subparts. We consider that if there
3342 * are more than one element forming the name, user wants a expansion,
3343 * otherwise it will be left as-is. Example:
3347 * the identifier %$abc will be left as-is so that the handler for %define
3348 * will suck it and define the corresponding value. Other case:
3350 * %define _%$abc cde
3352 * In this case user wants name to be expanded *before* %define starts
3353 * working, so we'll expand %$abc into something (if it has a value;
3354 * otherwise it will be left as-is) then concatenate all successive
3357 static Token
*expand_id(Token
* tline
)
3359 Token
*cur
, *oldnext
= NULL
;
3361 if (!tline
|| !tline
->next
)
3366 (cur
->next
->type
== TOK_ID
||
3367 cur
->next
->type
== TOK_PREPROC_ID
3368 || cur
->next
->type
== TOK_NUMBER
))
3371 /* If identifier consists of just one token, don't expand */
3376 oldnext
= cur
->next
; /* Detach the tail past identifier */
3377 cur
->next
= NULL
; /* so that expand_smacro stops here */
3380 tline
= expand_smacro(tline
);
3383 /* expand_smacro possibly changhed tline; re-scan for EOL */
3385 while (cur
&& cur
->next
)
3388 cur
->next
= oldnext
;
3395 * Determine whether the given line constitutes a multi-line macro
3396 * call, and return the MMacro structure called if so. Doesn't have
3397 * to check for an initial label - that's taken care of in
3398 * expand_mmacro - but must check numbers of parameters. Guaranteed
3399 * to be called with tline->type == TOK_ID, so the putative macro
3400 * name is easy to find.
3402 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3408 head
= (MMacro
*) hash_findix(mmacros
, tline
->text
);
3411 * Efficiency: first we see if any macro exists with the given
3412 * name. If not, we can return NULL immediately. _Then_ we
3413 * count the parameters, and then we look further along the
3414 * list if necessary to find the proper MMacro.
3416 for (m
= head
; m
; m
= m
->next
)
3417 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3423 * OK, we have a potential macro. Count and demarcate the
3426 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3429 * So we know how many parameters we've got. Find the MMacro
3430 * structure that handles this number.
3433 if (m
->nparam_min
<= nparam
3434 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3436 * This one is right. Just check if cycle removal
3437 * prohibits us using it before we actually celebrate...
3439 if (m
->in_progress
) {
3442 "self-reference in multi-line macro `%s'", m
->name
);
3448 * It's right, and we can use it. Add its default
3449 * parameters to the end of our list if necessary.
3451 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3453 nasm_realloc(params
,
3454 ((m
->nparam_min
+ m
->ndefs
+
3455 1) * sizeof(*params
)));
3456 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3457 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3462 * If we've gone over the maximum parameter count (and
3463 * we're in Plus mode), ignore parameters beyond
3466 if (m
->plus
&& nparam
> m
->nparam_max
)
3467 nparam
= m
->nparam_max
;
3469 * Then terminate the parameter list, and leave.
3471 if (!params
) { /* need this special case */
3472 params
= nasm_malloc(sizeof(*params
));
3475 params
[nparam
] = NULL
;
3476 *params_array
= params
;
3480 * This one wasn't right: look for the next one with the
3483 for (m
= m
->next
; m
; m
= m
->next
)
3484 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3489 * After all that, we didn't find one with the right number of
3490 * parameters. Issue a warning, and fail to expand the macro.
3492 error(ERR_WARNING
| ERR_WARN_MNP
,
3493 "macro `%s' exists, but not taking %d parameters",
3494 tline
->text
, nparam
);
3500 * Expand the multi-line macro call made by the given line, if
3501 * there is one to be expanded. If there is, push the expansion on
3502 * istk->expansion and return 1. Otherwise return 0.
3504 static int expand_mmacro(Token
* tline
)
3506 Token
*startline
= tline
;
3507 Token
*label
= NULL
;
3508 int dont_prepend
= 0;
3509 Token
**params
, *t
, *tt
;
3512 int i
, nparam
, *paramlen
;
3516 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3517 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3519 m
= is_mmacro(t
, ¶ms
);
3523 * We have an id which isn't a macro call. We'll assume
3524 * it might be a label; we'll also check to see if a
3525 * colon follows it. Then, if there's another id after
3526 * that lot, we'll check it again for macro-hood.
3530 if (tok_type_(t
, TOK_WHITESPACE
))
3531 last
= t
, t
= t
->next
;
3532 if (tok_is_(t
, ":")) {
3534 last
= t
, t
= t
->next
;
3535 if (tok_type_(t
, TOK_WHITESPACE
))
3536 last
= t
, t
= t
->next
;
3538 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3545 * Fix up the parameters: this involves stripping leading and
3546 * trailing whitespace, then stripping braces if they are
3549 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3550 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3552 for (i
= 0; params
[i
]; i
++) {
3554 int comma
= (!m
->plus
|| i
< nparam
- 1);
3558 if (tok_is_(t
, "{"))
3559 t
= t
->next
, brace
= true, comma
= false;
3563 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3564 break; /* ... because we have hit a comma */
3565 if (comma
&& t
->type
== TOK_WHITESPACE
3566 && tok_is_(t
->next
, ","))
3567 break; /* ... or a space then a comma */
3568 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3569 break; /* ... or a brace */
3576 * OK, we have a MMacro structure together with a set of
3577 * parameters. We must now go through the expansion and push
3578 * copies of each Line on to istk->expansion. Substitution of
3579 * parameter tokens and macro-local tokens doesn't get done
3580 * until the single-line macro substitution process; this is
3581 * because delaying them allows us to change the semantics
3582 * later through %rotate.
3584 * First, push an end marker on to istk->expansion, mark this
3585 * macro as in progress, and set up its invocation-specific
3588 ll
= nasm_malloc(sizeof(Line
));
3589 ll
->next
= istk
->expansion
;
3592 istk
->expansion
= ll
;
3594 m
->in_progress
= true;
3599 m
->paramlen
= paramlen
;
3600 m
->unique
= unique
++;
3603 m
->next_active
= istk
->mstk
;
3606 for (l
= m
->expansion
; l
; l
= l
->next
) {
3609 ll
= nasm_malloc(sizeof(Line
));
3610 ll
->finishes
= NULL
;
3611 ll
->next
= istk
->expansion
;
3612 istk
->expansion
= ll
;
3615 for (t
= l
->first
; t
; t
= t
->next
) {
3617 if (t
->type
== TOK_PREPROC_ID
&&
3618 t
->text
[1] == '0' && t
->text
[2] == '0') {
3624 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3631 * If we had a label, push it on as the first line of
3632 * the macro expansion.
3635 if (dont_prepend
< 0)
3636 free_tlist(startline
);
3638 ll
= nasm_malloc(sizeof(Line
));
3639 ll
->finishes
= NULL
;
3640 ll
->next
= istk
->expansion
;
3641 istk
->expansion
= ll
;
3642 ll
->first
= startline
;
3643 if (!dont_prepend
) {
3645 label
= label
->next
;
3646 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3651 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3657 * Since preprocessor always operate only on the line that didn't
3658 * arrived yet, we should always use ERR_OFFBY1. Also since user
3659 * won't want to see same error twice (preprocessing is done once
3660 * per pass) we will want to show errors only during pass one.
3662 static void error(int severity
, const char *fmt
, ...)
3667 /* If we're in a dead branch of IF or something like it, ignore the error */
3668 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3672 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3675 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3676 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3677 istk
->mstk
->lineno
, buff
);
3679 _error(severity
| ERR_PASS1
, "%s", buff
);
3683 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3688 istk
= nasm_malloc(sizeof(Include
));
3691 istk
->expansion
= NULL
;
3693 istk
->fp
= fopen(file
, "r");
3695 src_set_fname(nasm_strdup(file
));
3699 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3704 if (tasm_compatible_mode
) {
3707 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
3709 any_extrastdmac
= (extrastdmac
!= NULL
);
3715 static char *pp_getline(void)
3722 * Fetch a tokenized line, either from the macro-expansion
3723 * buffer or from the input file.
3726 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3727 Line
*l
= istk
->expansion
;
3728 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3732 * This is a macro-end marker for a macro with no
3733 * name, which means it's not really a macro at all
3734 * but a %rep block, and the `in_progress' field is
3735 * more than 1, meaning that we still need to
3736 * repeat. (1 means the natural last repetition; 0
3737 * means termination by %exitrep.) We have
3738 * therefore expanded up to the %endrep, and must
3739 * push the whole block on to the expansion buffer
3740 * again. We don't bother to remove the macro-end
3741 * marker: we'd only have to generate another one
3744 l
->finishes
->in_progress
--;
3745 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3746 Token
*t
, *tt
, **tail
;
3748 ll
= nasm_malloc(sizeof(Line
));
3749 ll
->next
= istk
->expansion
;
3750 ll
->finishes
= NULL
;
3754 for (t
= l
->first
; t
; t
= t
->next
) {
3755 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3757 new_Token(NULL
, t
->type
, t
->text
, 0);
3762 istk
->expansion
= ll
;
3766 * Check whether a `%rep' was started and not ended
3767 * within this macro expansion. This can happen and
3768 * should be detected. It's a fatal error because
3769 * I'm too confused to work out how to recover
3775 "defining with name in expansion");
3776 else if (istk
->mstk
->name
)
3778 "`%%rep' without `%%endrep' within"
3779 " expansion of macro `%s'",
3784 * FIXME: investigate the relationship at this point between
3785 * istk->mstk and l->finishes
3788 MMacro
*m
= istk
->mstk
;
3789 istk
->mstk
= m
->next_active
;
3792 * This was a real macro call, not a %rep, and
3793 * therefore the parameter information needs to
3796 nasm_free(m
->params
);
3797 free_tlist(m
->iline
);
3798 nasm_free(m
->paramlen
);
3799 l
->finishes
->in_progress
= false;
3803 istk
->expansion
= l
->next
;
3805 list
->downlevel(LIST_MACRO
);
3808 while (1) { /* until we get a line we can use */
3810 if (istk
->expansion
) { /* from a macro expansion */
3812 Line
*l
= istk
->expansion
;
3814 istk
->mstk
->lineno
++;
3816 istk
->expansion
= l
->next
;
3818 p
= detoken(tline
, false);
3819 list
->line(LIST_MACRO
, p
);
3824 if (line
) { /* from the current input file */
3825 line
= prepreproc(line
);
3826 tline
= tokenize(line
);
3831 * The current file has ended; work down the istk
3838 "expected `%%endif' before end of file");
3839 /* only set line and file name if there's a next node */
3841 src_set_linnum(i
->lineno
);
3842 nasm_free(src_set_fname(i
->fname
));
3845 list
->downlevel(LIST_INCLUDE
);
3853 * We must expand MMacro parameters and MMacro-local labels
3854 * _before_ we plunge into directive processing, to cope
3855 * with things like `%define something %1' such as STRUC
3856 * uses. Unless we're _defining_ a MMacro, in which case
3857 * those tokens should be left alone to go into the
3858 * definition; and unless we're in a non-emitting
3859 * condition, in which case we don't want to meddle with
3862 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3863 tline
= expand_mmac_params(tline
);
3866 * Check the line to see if it's a preprocessor directive.
3868 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3870 } else if (defining
) {
3872 * We're defining a multi-line macro. We emit nothing
3874 * shove the tokenized line on to the macro definition.
3876 Line
*l
= nasm_malloc(sizeof(Line
));
3877 l
->next
= defining
->expansion
;
3879 l
->finishes
= false;
3880 defining
->expansion
= l
;
3882 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3884 * We're in a non-emitting branch of a condition block.
3885 * Emit nothing at all, not even a blank line: when we
3886 * emerge from the condition we'll give a line-number
3887 * directive so we keep our place correctly.
3891 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3893 * We're in a %rep block which has been terminated, so
3894 * we're walking through to the %endrep without
3895 * emitting anything. Emit nothing at all, not even a
3896 * blank line: when we emerge from the %rep block we'll
3897 * give a line-number directive so we keep our place
3903 tline
= expand_smacro(tline
);
3904 if (!expand_mmacro(tline
)) {
3906 * De-tokenize the line again, and emit it.
3908 line
= detoken(tline
, true);
3912 continue; /* expand_mmacro calls free_tlist */
3920 static void pp_cleanup(int pass
)
3923 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3925 free_mmacro(defining
);
3934 nasm_free(i
->fname
);
3945 void pp_include_path(char *path
)
3949 i
= nasm_malloc(sizeof(IncPath
));
3950 i
->path
= path
? nasm_strdup(path
) : NULL
;
3953 if (ipath
!= NULL
) {
3955 while (j
->next
!= NULL
)
3966 * This function is used to "export" the include paths, e.g.
3967 * the paths specified in the '-I' command switch.
3968 * The need for such exporting is due to the 'incbin' directive,
3969 * which includes raw binary files (unlike '%include', which
3970 * includes text source files). It would be real nice to be
3971 * able to specify paths to search for incbin'ned files also.
3972 * So, this is a simple workaround.
3974 * The function use is simple:
3976 * The 1st call (with NULL argument) returns a pointer to the 1st path
3977 * (char** type) or NULL if none include paths available.
3979 * All subsequent calls take as argument the value returned by this
3980 * function last. The return value is either the next path
3981 * (char** type) or NULL if the end of the paths list is reached.
3983 * It is maybe not the best way to do things, but I didn't want
3984 * to export too much, just one or two functions and no types or
3985 * variables exported.
3987 * Can't say I like the current situation with e.g. this path list either,
3988 * it seems to be never deallocated after creation...
3990 char **pp_get_include_path_ptr(char **pPrevPath
)
3992 /* This macro returns offset of a member of a structure */
3993 #define GetMemberOffset(StructType,MemberName)\
3994 ((size_t)&((StructType*)0)->MemberName)
3997 if (pPrevPath
== NULL
) {
3999 return &ipath
->path
;
4003 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
4009 #undef GetMemberOffset
4012 void pp_pre_include(char *fname
)
4014 Token
*inc
, *space
, *name
;
4017 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4018 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4019 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4021 l
= nasm_malloc(sizeof(Line
));
4024 l
->finishes
= false;
4028 void pp_pre_define(char *definition
)
4034 equals
= strchr(definition
, '=');
4035 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4036 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4039 space
->next
= tokenize(definition
);
4043 l
= nasm_malloc(sizeof(Line
));
4046 l
->finishes
= false;
4050 void pp_pre_undefine(char *definition
)
4055 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4056 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4057 space
->next
= tokenize(definition
);
4059 l
= nasm_malloc(sizeof(Line
));
4062 l
->finishes
= false;
4067 * Added by Keith Kanios:
4069 * This function is used to assist with "runtime" preprocessor
4070 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4072 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4073 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4076 void pp_runtime(char *definition
)
4080 def
= tokenize(definition
);
4081 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4086 void pp_extra_stdmac(const char **macros
)
4088 extrastdmac
= macros
;
4091 static void make_tok_num(Token
* tok
, int64_t val
)
4094 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4095 tok
->text
= nasm_strdup(numbuf
);
4096 tok
->type
= TOK_NUMBER
;