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
,
160 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
161 TOK_SMAC_PARAM
/* MUST BE LAST IN THE LIST!!! */
167 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
168 enum pp_token_type type
;
172 * Multi-line macro definitions are stored as a linked list of
173 * these, which is essentially a container to allow several linked
176 * Note that in this module, linked lists are treated as stacks
177 * wherever possible. For this reason, Lines are _pushed_ on to the
178 * `expansion' field in MMacro structures, so that the linked list,
179 * if walked, would give the macro lines in reverse order; this
180 * means that we can walk the list when expanding a macro, and thus
181 * push the lines on to the `expansion' field in _istk_ in reverse
182 * order (so that when popped back off they are in the right
183 * order). It may seem cockeyed, and it relies on my design having
184 * an even number of steps in, but it works...
186 * Some of these structures, rather than being actual lines, are
187 * markers delimiting the end of the expansion of a given macro.
188 * This is for use in the cycle-tracking and %rep-handling code.
189 * Such structures have `finishes' non-NULL, and `first' NULL. All
190 * others have `finishes' NULL, but `first' may still be NULL if
200 * To handle an arbitrary level of file inclusion, we maintain a
201 * stack (ie linked list) of these things.
210 MMacro
*mstk
; /* stack of active macros/reps */
214 * Include search path. This is simply a list of strings which get
215 * prepended, in turn, to the name of an include file, in an
216 * attempt to find the file if it's not in the current directory.
224 * Conditional assembly: we maintain a separate stack of these for
225 * each level of file inclusion. (The only reason we keep the
226 * stacks separate is to ensure that a stray `%endif' in a file
227 * included from within the true branch of a `%if' won't terminate
228 * it and cause confusion: instead, rightly, it'll cause an error.)
236 * These states are for use just after %if or %elif: IF_TRUE
237 * means the condition has evaluated to truth so we are
238 * currently emitting, whereas IF_FALSE means we are not
239 * currently emitting but will start doing so if a %else comes
240 * up. In these states, all directives are admissible: %elif,
241 * %else and %endif. (And of course %if.)
243 COND_IF_TRUE
, COND_IF_FALSE
,
245 * These states come up after a %else: ELSE_TRUE means we're
246 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
247 * any %elif or %else will cause an error.
249 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
251 * This state means that we're not emitting now, and also that
252 * nothing until %endif will be emitted at all. It's for use in
253 * two circumstances: (i) when we've had our moment of emission
254 * and have now started seeing %elifs, and (ii) when the
255 * condition construct in question is contained within a
256 * non-emitting branch of a larger condition construct.
260 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
263 * These defines are used as the possible return values for do_directive
265 #define NO_DIRECTIVE_FOUND 0
266 #define DIRECTIVE_FOUND 1
269 * Condition codes. Note that we use c_ prefix not C_ because C_ is
270 * used in nasm.h for the "real" condition codes. At _this_ level,
271 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
272 * ones, so we need a different enum...
274 static const char * const conditions
[] = {
275 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
276 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
277 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
280 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
281 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
282 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
285 static const enum pp_conds inverse_ccs
[] = {
286 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
287 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
,
288 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
294 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
295 static int is_condition(enum preproc_token arg
)
297 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
300 /* For TASM compatibility we need to be able to recognise TASM compatible
301 * conditional compilation directives. Using the NASM pre-processor does
302 * not work, so we look for them specifically from the following list and
303 * then jam in the equivalent NASM directive into the input stream.
307 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
308 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
311 static const char * const tasm_directives
[] = {
312 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
313 "ifndef", "include", "local"
316 static int StackSize
= 4;
317 static char *StackPointer
= "ebp";
318 static int ArgOffset
= 8;
319 static int LocalOffset
= 0;
321 static Context
*cstk
;
322 static Include
*istk
;
323 static IncPath
*ipath
= NULL
;
325 static efunc _error
; /* Pointer to client-provided error reporting function */
326 static evalfunc evaluate
;
328 static int pass
; /* HACK: pass 0 = generate dependencies only */
330 static uint64_t unique
; /* unique identifier numbers */
332 static Line
*predef
= NULL
;
334 static ListGen
*list
;
337 * The current set of multi-line macros we have defined.
339 static struct hash_table
*mmacros
;
342 * The current set of single-line macros we have defined.
344 static struct hash_table
*smacros
;
347 * The multi-line macro we are currently defining, or the %rep
348 * block we are currently reading, if any.
350 static MMacro
*defining
;
353 * The number of macro parameters to allocate space for at a time.
355 #define PARAM_DELTA 16
358 * The standard macro set: defined in macros.c in the array nasm_stdmac.
359 * This gives our position in the macro set, when we're processing it.
361 static const char * const *stdmacpos
;
364 * The extra standard macros that come from the object format, if
367 static const char * const *extrastdmac
= NULL
;
368 bool any_extrastdmac
;
371 * Tokens are allocated in blocks to improve speed
373 #define TOKEN_BLOCKSIZE 4096
374 static Token
*freeTokens
= NULL
;
380 static Blocks blocks
= { NULL
, NULL
};
383 * Forward declarations.
385 static Token
*expand_mmac_params(Token
* tline
);
386 static Token
*expand_smacro(Token
* tline
);
387 static Token
*expand_id(Token
* tline
);
388 static Context
*get_ctx(char *name
, bool all_contexts
);
389 static void make_tok_num(Token
* tok
, int64_t val
);
390 static void error(int severity
, const char *fmt
, ...);
391 static void *new_Block(size_t size
);
392 static void delete_Blocks(void);
393 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
394 static Token
*delete_Token(Token
* t
);
397 * Macros for safe checking of token pointers, avoid *(NULL)
399 #define tok_type_(x,t) ((x) && (x)->type == (t))
400 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
404 /* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
409 static char *check_tasm_directive(char *line
)
411 int32_t i
, j
, k
, m
, len
;
412 char *p
= line
, *oldline
, oldchar
;
414 /* Skip whitespace */
415 while (isspace(*p
) && *p
!= 0)
418 /* Binary search for the directive name */
420 j
= elements(tasm_directives
);
422 while (!isspace(p
[len
]) && p
[len
] != 0)
429 m
= nasm_stricmp(p
, tasm_directives
[k
]);
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
437 line
= nasm_malloc(len
+ 2);
439 if (k
== TM_IFDIFI
) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
445 strcpy(line
+ 1, "ifdef BOGUS");
447 memcpy(line
+ 1, p
, len
+ 1);
462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
467 static char *prepreproc(char *line
)
470 char *fname
, *oldline
;
472 if (line
[0] == '#' && line
[1] == ' ') {
475 lineno
= atoi(fname
);
476 fname
+= strspn(fname
, "0123456789 ");
479 fnlen
= strcspn(fname
, "\"");
480 line
= nasm_malloc(20 + fnlen
);
481 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
484 if (tasm_compatible_mode
)
485 return check_tasm_directive(line
);
490 * Free a linked list of tokens.
492 static void free_tlist(Token
* list
)
495 list
= delete_Token(list
);
500 * Free a linked list of lines.
502 static void free_llist(Line
* list
)
508 free_tlist(l
->first
);
516 static void free_mmacro(MMacro
* m
)
519 free_tlist(m
->dlist
);
520 nasm_free(m
->defaults
);
521 free_llist(m
->expansion
);
526 * Free all currently defined macros, and free the hash tables
528 static void free_smacro_table(struct hash_table
*smt
)
532 struct hash_tbl_node
*it
= NULL
;
534 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
535 nasm_free((void *)key
);
537 SMacro
*ns
= s
->next
;
539 free_tlist(s
->expansion
);
547 static void free_mmacro_table(struct hash_table
*mmt
)
551 struct hash_tbl_node
*it
= NULL
;
554 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
555 nasm_free((void *)key
);
557 MMacro
*nm
= m
->next
;
565 static void free_macros(void)
567 free_smacro_table(smacros
);
568 free_mmacro_table(mmacros
);
572 * Initialize the hash tables
574 static void init_macros(void)
576 smacros
= hash_init(HASH_LARGE
);
577 mmacros
= hash_init(HASH_LARGE
);
581 * Pop the context stack.
583 static void ctx_pop(void)
588 free_smacro_table(c
->localmac
);
594 * Search for a key in the hash index; adding it if necessary
595 * (in which case we initialize the data pointer to NULL.)
598 hash_findi_add(struct hash_table
*hash
, const char *str
)
600 struct hash_insert hi
;
604 r
= hash_findi(hash
, str
, &hi
);
608 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
609 return hash_add(&hi
, strx
, NULL
);
613 * Like hash_findi, but returns the data element rather than a pointer
614 * to it. Used only when not adding a new element, hence no third
618 hash_findix(struct hash_table
*hash
, const char *str
)
622 p
= hash_findi(hash
, str
, NULL
);
623 return p
? *p
: NULL
;
626 #define BUF_DELTA 512
628 * Read a line from the top file in istk, handling multiple CR/LFs
629 * at the end of the line read, and handling spurious ^Zs. Will
630 * return lines from the standard macro set if this has not already
633 static char *read_line(void)
635 char *buffer
, *p
, *q
;
636 int bufsize
, continued_count
;
640 char *ret
= nasm_strdup(*stdmacpos
++);
641 if (!*stdmacpos
&& any_extrastdmac
) {
642 stdmacpos
= extrastdmac
;
643 any_extrastdmac
= false;
647 * Nasty hack: here we push the contents of `predef' on
648 * to the top-level expansion stack, since this is the
649 * most convenient way to implement the pre-include and
650 * pre-define features.
654 Token
*head
, **tail
, *t
;
656 for (pd
= predef
; pd
; pd
= pd
->next
) {
659 for (t
= pd
->first
; t
; t
= t
->next
) {
660 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
661 tail
= &(*tail
)->next
;
663 l
= nasm_malloc(sizeof(Line
));
664 l
->next
= istk
->expansion
;
677 buffer
= nasm_malloc(BUF_DELTA
);
681 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
685 if (p
> buffer
&& p
[-1] == '\n') {
686 /* Convert backslash-CRLF line continuation sequences into
687 nothing at all (for DOS and Windows) */
688 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
693 /* Also convert backslash-LF line continuation sequences into
694 nothing at all (for Unix) */
695 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
703 if (p
- buffer
> bufsize
- 10) {
704 int32_t offset
= p
- buffer
;
705 bufsize
+= BUF_DELTA
;
706 buffer
= nasm_realloc(buffer
, bufsize
);
707 p
= buffer
+ offset
; /* prevent stale-pointer problems */
711 if (!q
&& p
== buffer
) {
716 src_set_linnum(src_get_linnum() + istk
->lineinc
+
717 (continued_count
* istk
->lineinc
));
720 * Play safe: remove CRs as well as LFs, if any of either are
721 * present at the end of the line.
723 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
727 * Handle spurious ^Z, which may be inserted into source files
728 * by some file transfer utilities.
730 buffer
[strcspn(buffer
, "\032")] = '\0';
732 list
->line(LIST_READ
, buffer
);
738 * Tokenize a line of text. This is a very simple process since we
739 * don't need to parse the value out of e.g. numeric tokens: we
740 * simply split one string into many.
742 static Token
*tokenize(char *line
)
745 enum pp_token_type type
;
747 Token
*t
, **tail
= &list
;
754 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
755 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
760 type
= TOK_PREPROC_ID
;
761 } else if (*p
== '{') {
763 while (*p
&& *p
!= '}') {
770 type
= TOK_PREPROC_ID
;
771 } else if (*p
== '?') {
772 type
= TOK_PREPROC_Q
; /* %? */
775 type
= TOK_PREPROC_QQ
; /* %?? */
778 } else if (isidchar(*p
) ||
779 ((*p
== '!' || *p
== '%' || *p
== '$') &&
784 while (isidchar(*p
));
785 type
= TOK_PREPROC_ID
;
791 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
794 while (*p
&& isidchar(*p
))
796 } else if (*p
== '\'' || *p
== '"') {
803 while (*p
&& *p
!= c
)
809 error(ERR_WARNING
, "unterminated string");
810 /* Handling unterminated strings by UNV */
813 } else if (isnumstart(*p
)) {
815 bool is_float
= false;
831 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
833 if (*p
== '+' || *p
== '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
839 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
841 } else if (c
== 'P' || c
== 'p') {
843 if (*p
== '+' || *p
== '-')
845 } else if (isnumchar(c
) || c
== '_')
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
860 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
861 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
862 (*r
== 'p' || *r
== 'P')) {
866 break; /* Terminate the token */
870 p
--; /* Point to first character beyond number */
872 if (has_e
&& !is_hex
) {
873 /* 1e13 is floating-point, but 1e13h is not */
877 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
878 } else if (isspace(*p
)) {
879 type
= TOK_WHITESPACE
;
881 while (*p
&& isspace(*p
))
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
888 if (!*p
|| *p
== ';') {
893 } else if (*p
== ';') {
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
902 * else is a single-character operator.
905 if ((p
[0] == '>' && p
[1] == '>') ||
906 (p
[0] == '<' && p
[1] == '<') ||
907 (p
[0] == '/' && p
[1] == '/') ||
908 (p
[0] == '<' && p
[1] == '=') ||
909 (p
[0] == '>' && p
[1] == '=') ||
910 (p
[0] == '=' && p
[1] == '=') ||
911 (p
[0] == '!' && p
[1] == '=') ||
912 (p
[0] == '<' && p
[1] == '>') ||
913 (p
[0] == '&' && p
[1] == '&') ||
914 (p
[0] == '|' && p
[1] == '|') ||
915 (p
[0] == '^' && p
[1] == '^')) {
921 /* Handling unterminated string by UNV */
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
929 if (type
!= TOK_COMMENT
) {
930 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
939 * this function allocates a new managed block of memory and
940 * returns a pointer to the block. The managed blocks are
941 * deleted only all at once by the delete_Blocks function.
943 static void *new_Block(size_t size
)
947 /* first, get to the end of the linked list */
950 /* now allocate the requested chunk */
951 b
->chunk
= nasm_malloc(size
);
953 /* now allocate a new block for the next request */
954 b
->next
= nasm_malloc(sizeof(Blocks
));
955 /* and initialize the contents of the new block */
956 b
->next
->next
= NULL
;
957 b
->next
->chunk
= NULL
;
962 * this function deletes all managed blocks of memory
964 static void delete_Blocks(void)
966 Blocks
*a
, *b
= &blocks
;
969 * keep in mind that the first block, pointed to by blocks
970 * is a static and not dynamically allocated, so we don't
984 * this function creates a new Token and passes a pointer to it
985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
988 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
993 if (freeTokens
== NULL
) {
994 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
995 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
996 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
997 freeTokens
[i
].next
= NULL
;
1000 freeTokens
= t
->next
;
1004 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1008 txtlen
= strlen(text
);
1009 t
->text
= nasm_malloc(1 + txtlen
);
1010 strncpy(t
->text
, text
, txtlen
);
1011 t
->text
[txtlen
] = '\0';
1016 static Token
*delete_Token(Token
* t
)
1018 Token
*next
= t
->next
;
1020 t
->next
= freeTokens
;
1026 * Convert a line of tokens back into text.
1027 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1028 * will be transformed into ..@ctxnum.xxx
1030 static char *detoken(Token
* tlist
, int expand_locals
)
1038 for (t
= tlist
; t
; t
= t
->next
) {
1039 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1040 char *p
= getenv(t
->text
+ 2);
1043 t
->text
= nasm_strdup(p
);
1047 /* Expand local macros here and not during preprocessing */
1048 if (expand_locals
&&
1049 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1050 t
->text
[0] == '%' && t
->text
[1] == '$') {
1051 Context
*ctx
= get_ctx(t
->text
, false);
1054 char *p
, *q
= t
->text
+ 2;
1056 q
+= strspn(q
, "$");
1057 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1058 p
= nasm_strcat(buffer
, q
);
1063 if (t
->type
== TOK_WHITESPACE
) {
1065 } else if (t
->text
) {
1066 len
+= strlen(t
->text
);
1069 p
= line
= nasm_malloc(len
+ 1);
1070 for (t
= tlist
; t
; t
= t
->next
) {
1071 if (t
->type
== TOK_WHITESPACE
) {
1073 } else if (t
->text
) {
1084 * A scanner, suitable for use by the expression evaluator, which
1085 * operates on a line of Tokens. Expects a pointer to a pointer to
1086 * the first token in the line to be passed in as its private_data
1089 * FIX: This really needs to be unified with stdscan.
1091 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1093 Token
**tlineptr
= private_data
;
1095 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1099 *tlineptr
= tline
? tline
->next
: NULL
;
1101 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1102 tline
->type
== TOK_COMMENT
));
1105 return tokval
->t_type
= TOKEN_EOS
;
1107 tokval
->t_charptr
= tline
->text
;
1109 if (tline
->text
[0] == '$' && !tline
->text
[1])
1110 return tokval
->t_type
= TOKEN_HERE
;
1111 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1112 return tokval
->t_type
= TOKEN_BASE
;
1114 if (tline
->type
== TOK_ID
) {
1115 p
= tokval
->t_charptr
= tline
->text
;
1117 tokval
->t_charptr
++;
1118 return tokval
->t_type
= TOKEN_ID
;
1121 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1122 if (r
>= p
+MAX_KEYWORD
)
1123 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1127 /* right, so we have an identifier sitting in temp storage. now,
1128 * is it actually a register or instruction name, or what? */
1129 return nasm_token_hash(ourcopy
, tokval
);
1132 if (tline
->type
== TOK_NUMBER
) {
1134 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1136 return tokval
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
1137 tokval
->t_charptr
= tline
->text
;
1138 return tokval
->t_type
= TOKEN_NUM
;
1141 if (tline
->type
== TOK_FLOAT
) {
1142 return tokval
->t_type
= TOKEN_FLOAT
;
1145 if (tline
->type
== TOK_STRING
) {
1154 if (l
== 0 || r
[l
- 1] != q
)
1155 return tokval
->t_type
= TOKEN_ERRNUM
;
1156 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1158 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1159 tokval
->t_charptr
= NULL
;
1160 return tokval
->t_type
= TOKEN_NUM
;
1163 if (tline
->type
== TOK_OTHER
) {
1164 if (!strcmp(tline
->text
, "<<"))
1165 return tokval
->t_type
= TOKEN_SHL
;
1166 if (!strcmp(tline
->text
, ">>"))
1167 return tokval
->t_type
= TOKEN_SHR
;
1168 if (!strcmp(tline
->text
, "//"))
1169 return tokval
->t_type
= TOKEN_SDIV
;
1170 if (!strcmp(tline
->text
, "%%"))
1171 return tokval
->t_type
= TOKEN_SMOD
;
1172 if (!strcmp(tline
->text
, "=="))
1173 return tokval
->t_type
= TOKEN_EQ
;
1174 if (!strcmp(tline
->text
, "<>"))
1175 return tokval
->t_type
= TOKEN_NE
;
1176 if (!strcmp(tline
->text
, "!="))
1177 return tokval
->t_type
= TOKEN_NE
;
1178 if (!strcmp(tline
->text
, "<="))
1179 return tokval
->t_type
= TOKEN_LE
;
1180 if (!strcmp(tline
->text
, ">="))
1181 return tokval
->t_type
= TOKEN_GE
;
1182 if (!strcmp(tline
->text
, "&&"))
1183 return tokval
->t_type
= TOKEN_DBL_AND
;
1184 if (!strcmp(tline
->text
, "^^"))
1185 return tokval
->t_type
= TOKEN_DBL_XOR
;
1186 if (!strcmp(tline
->text
, "||"))
1187 return tokval
->t_type
= TOKEN_DBL_OR
;
1191 * We have no other options: just return the first character of
1194 return tokval
->t_type
= tline
->text
[0];
1198 * Compare a string to the name of an existing macro; this is a
1199 * simple wrapper which calls either strcmp or nasm_stricmp
1200 * depending on the value of the `casesense' parameter.
1202 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1204 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1208 * Return the Context structure associated with a %$ token. Return
1209 * NULL, having _already_ reported an error condition, if the
1210 * context stack isn't deep enough for the supplied number of $
1212 * If all_contexts == true, contexts that enclose current are
1213 * also scanned for such smacro, until it is found; if not -
1214 * only the context that directly results from the number of $'s
1215 * in variable's name.
1217 static Context
*get_ctx(char *name
, bool all_contexts
)
1223 if (!name
|| name
[0] != '%' || name
[1] != '$')
1227 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1231 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1233 /* i--; Lino - 02/25/02 */
1236 error(ERR_NONFATAL
, "`%s': context stack is only"
1237 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1244 /* Search for this smacro in found context */
1245 m
= hash_findix(ctx
->localmac
, name
);
1247 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1258 * Open an include file. This routine must always return a valid
1259 * file pointer if it returns - it's responsible for throwing an
1260 * ERR_FATAL and bombing out completely if not. It should also try
1261 * the include path one by one until it finds the file or reaches
1262 * the end of the path.
1264 static FILE *inc_fopen(char *file
)
1267 char *prefix
= "", *combine
;
1268 IncPath
*ip
= ipath
;
1269 static int namelen
= 0;
1270 int len
= strlen(file
);
1273 combine
= nasm_malloc(strlen(prefix
) + len
+ 1);
1274 strcpy(combine
, prefix
);
1275 strcat(combine
, file
);
1276 fp
= fopen(combine
, "r");
1277 if (pass
== 0 && fp
) {
1278 namelen
+= strlen(combine
) + 1;
1283 printf(" %s", combine
);
1294 /* -MG given and file not found */
1296 namelen
+= strlen(file
) + 1;
1301 printf(" %s", file
);
1307 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1308 return NULL
; /* never reached - placate compilers */
1312 * Determine if we should warn on defining a single-line macro of
1313 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1314 * return true if _any_ single-line macro of that name is defined.
1315 * Otherwise, will return true if a single-line macro with either
1316 * `nparam' or no parameters is defined.
1318 * If a macro with precisely the right number of parameters is
1319 * defined, or nparam is -1, the address of the definition structure
1320 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1321 * is NULL, no action will be taken regarding its contents, and no
1324 * Note that this is also called with nparam zero to resolve
1327 * If you already know which context macro belongs to, you can pass
1328 * the context pointer as first parameter; if you won't but name begins
1329 * with %$ the context will be automatically computed. If all_contexts
1330 * is true, macro will be searched in outer contexts as well.
1333 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1339 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1340 } else if (name
[0] == '%' && name
[1] == '$') {
1342 ctx
= get_ctx(name
, false);
1344 return false; /* got to return _something_ */
1345 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1347 m
= (SMacro
*) hash_findix(smacros
, name
);
1351 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1352 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1354 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1368 * Count and mark off the parameters in a multi-line macro call.
1369 * This is called both from within the multi-line macro expansion
1370 * code, and also to mark off the default parameters when provided
1371 * in a %macro definition line.
1373 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1375 int paramsize
, brace
;
1377 *nparam
= paramsize
= 0;
1380 if (*nparam
>= paramsize
) {
1381 paramsize
+= PARAM_DELTA
;
1382 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1386 if (tok_is_(t
, "{"))
1388 (*params
)[(*nparam
)++] = t
;
1389 while (tok_isnt_(t
, brace
? "}" : ","))
1391 if (t
) { /* got a comma/brace */
1395 * Now we've found the closing brace, look further
1399 if (tok_isnt_(t
, ",")) {
1401 "braces do not enclose all of macro parameter");
1402 while (tok_isnt_(t
, ","))
1406 t
= t
->next
; /* eat the comma */
1413 * Determine whether one of the various `if' conditions is true or
1416 * We must free the tline we get passed.
1418 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1420 enum pp_conditional i
= PP_COND(ct
);
1422 Token
*t
, *tt
, **tptr
, *origline
;
1423 struct tokenval tokval
;
1425 enum pp_token_type needtype
;
1431 j
= false; /* have we matched yet? */
1432 while (cstk
&& tline
) {
1434 if (!tline
|| tline
->type
!= TOK_ID
) {
1436 "`%s' expects context identifiers", pp_directives
[ct
]);
1437 free_tlist(origline
);
1440 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1442 tline
= tline
->next
;
1447 j
= false; /* have we matched yet? */
1450 if (!tline
|| (tline
->type
!= TOK_ID
&&
1451 (tline
->type
!= TOK_PREPROC_ID
||
1452 tline
->text
[1] != '$'))) {
1454 "`%s' expects macro identifiers", pp_directives
[ct
]);
1457 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1459 tline
= tline
->next
;
1465 tline
= expand_smacro(tline
);
1467 while (tok_isnt_(tt
, ","))
1471 "`%s' expects two comma-separated arguments",
1476 j
= true; /* assume equality unless proved not */
1477 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1478 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1479 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1483 if (t
->type
== TOK_WHITESPACE
) {
1487 if (tt
->type
== TOK_WHITESPACE
) {
1491 if (tt
->type
!= t
->type
) {
1492 j
= false; /* found mismatching tokens */
1495 /* Unify surrounding quotes for strings */
1496 if (t
->type
== TOK_STRING
) {
1497 tt
->text
[0] = t
->text
[0];
1498 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1500 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1501 j
= false; /* found mismatching tokens */
1508 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1509 j
= false; /* trailing gunk on one end or other */
1515 MMacro searching
, *mmac
;
1517 tline
= tline
->next
;
1519 tline
= expand_id(tline
);
1520 if (!tok_type_(tline
, TOK_ID
)) {
1522 "`%s' expects a macro name", pp_directives
[ct
]);
1525 searching
.name
= nasm_strdup(tline
->text
);
1526 searching
.casesense
= true;
1527 searching
.plus
= false;
1528 searching
.nolist
= false;
1529 searching
.in_progress
= 0;
1530 searching
.rep_nest
= NULL
;
1531 searching
.nparam_min
= 0;
1532 searching
.nparam_max
= INT_MAX
;
1533 tline
= expand_smacro(tline
->next
);
1536 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1538 "`%s' expects a parameter count or nothing",
1541 searching
.nparam_min
= searching
.nparam_max
=
1542 readnum(tline
->text
, &j
);
1545 "unable to parse parameter count `%s'",
1548 if (tline
&& tok_is_(tline
->next
, "-")) {
1549 tline
= tline
->next
->next
;
1550 if (tok_is_(tline
, "*"))
1551 searching
.nparam_max
= INT_MAX
;
1552 else if (!tok_type_(tline
, TOK_NUMBER
))
1554 "`%s' expects a parameter count after `-'",
1557 searching
.nparam_max
= readnum(tline
->text
, &j
);
1560 "unable to parse parameter count `%s'",
1562 if (searching
.nparam_min
> searching
.nparam_max
)
1564 "minimum parameter count exceeds maximum");
1567 if (tline
&& tok_is_(tline
->next
, "+")) {
1568 tline
= tline
->next
;
1569 searching
.plus
= true;
1571 mmac
= (MMacro
*) hash_findix(mmacros
, searching
.name
);
1573 if (!strcmp(mmac
->name
, searching
.name
) &&
1574 (mmac
->nparam_min
<= searching
.nparam_max
1576 && (searching
.nparam_min
<= mmac
->nparam_max
1583 nasm_free(searching
.name
);
1592 needtype
= TOK_NUMBER
;
1595 needtype
= TOK_STRING
;
1599 t
= tline
= expand_smacro(tline
);
1601 while (tok_type_(t
, TOK_WHITESPACE
) ||
1602 (needtype
== TOK_NUMBER
&&
1603 tok_type_(t
, TOK_OTHER
) &&
1604 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1608 j
= tok_type_(t
, needtype
);
1612 t
= tline
= expand_smacro(tline
);
1613 while (tok_type_(t
, TOK_WHITESPACE
))
1618 t
= t
->next
; /* Skip the actual token */
1619 while (tok_type_(t
, TOK_WHITESPACE
))
1621 j
= !t
; /* Should be nothing left */
1626 t
= tline
= expand_smacro(tline
);
1627 while (tok_type_(t
, TOK_WHITESPACE
))
1630 j
= !t
; /* Should be empty */
1634 t
= tline
= expand_smacro(tline
);
1636 tokval
.t_type
= TOKEN_INVALID
;
1637 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1638 NULL
, pass
| CRITICAL
, error
, NULL
);
1643 "trailing garbage after expression ignored");
1644 if (!is_simple(evalresult
)) {
1646 "non-constant value given to `%s'", pp_directives
[ct
]);
1649 j
= reloc_value(evalresult
) != 0;
1654 "preprocessor directive `%s' not yet implemented",
1659 free_tlist(origline
);
1660 return j
^ PP_NEGATIVE(ct
);
1663 free_tlist(origline
);
1668 * Expand macros in a string. Used in %error and %include directives.
1669 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1670 * The returned variable should ALWAYS be freed after usage.
1672 void expand_macros_in_string(char **p
)
1674 Token
*line
= tokenize(*p
);
1675 line
= expand_smacro(line
);
1676 *p
= detoken(line
, false);
1680 * Common code for defining an smacro
1682 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1683 int nparam
, Token
*expansion
)
1685 SMacro
*smac
, **smhead
;
1687 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1690 "single-line macro `%s' defined both with and"
1691 " without parameters", mname
);
1693 /* Some instances of the old code considered this a failure,
1694 some others didn't. What is the right thing to do here? */
1695 free_tlist(expansion
);
1696 return false; /* Failure */
1699 * We're redefining, so we have to take over an
1700 * existing SMacro structure. This means freeing
1701 * what was already in it.
1703 nasm_free(smac
->name
);
1704 free_tlist(smac
->expansion
);
1707 smhead
= (SMacro
**) hash_findi_add(ctx
? ctx
->localmac
: smacros
,
1709 smac
= nasm_malloc(sizeof(SMacro
));
1710 smac
->next
= *smhead
;
1713 smac
->name
= nasm_strdup(mname
);
1714 smac
->casesense
= casesense
;
1715 smac
->nparam
= nparam
;
1716 smac
->expansion
= expansion
;
1717 smac
->in_progress
= false;
1718 return true; /* Success */
1722 * Undefine an smacro
1724 static void undef_smacro(Context
*ctx
, const char *mname
)
1726 SMacro
**smhead
, *s
, **sp
;
1728 smhead
= (SMacro
**)hash_findi(ctx
? ctx
->localmac
: smacros
, mname
, NULL
);
1732 * We now have a macro name... go hunt for it.
1735 while ((s
= *sp
) != NULL
) {
1736 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1739 free_tlist(s
->expansion
);
1749 * Decode a size directive
1751 static int parse_size(const char *str
) {
1752 static const char *size_names
[] =
1753 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1754 static const int sizes
[] =
1755 { 0, 1, 4, 16, 8, 10, 2, 32 };
1757 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1761 * find and process preprocessor directive in passed line
1762 * Find out if a line contains a preprocessor directive, and deal
1765 * If a directive _is_ found, it is the responsibility of this routine
1766 * (and not the caller) to free_tlist() the line.
1768 * @param tline a pointer to the current tokeninzed line linked list
1769 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1772 static int do_directive(Token
* tline
)
1774 enum preproc_token i
;
1786 MMacro
*mmac
, **mmhead
;
1787 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1789 struct tokenval tokval
;
1791 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1797 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1798 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1799 || tline
->text
[1] == '!'))
1800 return NO_DIRECTIVE_FOUND
;
1802 i
= pp_token_hash(tline
->text
);
1805 * If we're in a non-emitting branch of a condition construct,
1806 * or walking to the end of an already terminated %rep block,
1807 * we should ignore all directives except for condition
1810 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1811 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1812 return NO_DIRECTIVE_FOUND
;
1816 * If we're defining a macro or reading a %rep block, we should
1817 * ignore all directives except for %macro/%imacro (which
1818 * generate an error), %endm/%endmacro, and (only if we're in a
1819 * %rep block) %endrep. If we're in a %rep block, another %rep
1820 * causes an error, so should be let through.
1822 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1823 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1824 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1825 return NO_DIRECTIVE_FOUND
;
1830 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1832 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1835 /* Directive to tell NASM what the default stack size is. The
1836 * default is for a 16-bit stack, and this can be overriden with
1838 * the following form:
1840 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1842 tline
= tline
->next
;
1843 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1844 tline
= tline
->next
;
1845 if (!tline
|| tline
->type
!= TOK_ID
) {
1846 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1847 free_tlist(origline
);
1848 return DIRECTIVE_FOUND
;
1850 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1851 /* All subsequent ARG directives are for a 32-bit stack */
1853 StackPointer
= "ebp";
1856 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1857 /* All subsequent ARG directives are for a 64-bit stack */
1859 StackPointer
= "rbp";
1862 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1863 /* All subsequent ARG directives are for a 16-bit stack,
1864 * far function call.
1867 StackPointer
= "bp";
1870 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1871 /* All subsequent ARG directives are for a 16-bit stack,
1872 * far function call. We don't support near functions.
1875 StackPointer
= "bp";
1879 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1880 free_tlist(origline
);
1881 return DIRECTIVE_FOUND
;
1883 free_tlist(origline
);
1884 return DIRECTIVE_FOUND
;
1887 /* TASM like ARG directive to define arguments to functions, in
1888 * the following form:
1890 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1894 char *arg
, directive
[256];
1895 int size
= StackSize
;
1897 /* Find the argument name */
1898 tline
= tline
->next
;
1899 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1900 tline
= tline
->next
;
1901 if (!tline
|| tline
->type
!= TOK_ID
) {
1902 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1903 free_tlist(origline
);
1904 return DIRECTIVE_FOUND
;
1908 /* Find the argument size type */
1909 tline
= tline
->next
;
1910 if (!tline
|| tline
->type
!= TOK_OTHER
1911 || tline
->text
[0] != ':') {
1913 "Syntax error processing `%%arg' directive");
1914 free_tlist(origline
);
1915 return DIRECTIVE_FOUND
;
1917 tline
= tline
->next
;
1918 if (!tline
|| tline
->type
!= TOK_ID
) {
1919 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1920 free_tlist(origline
);
1921 return DIRECTIVE_FOUND
;
1924 /* Allow macro expansion of type parameter */
1925 tt
= tokenize(tline
->text
);
1926 tt
= expand_smacro(tt
);
1927 size
= parse_size(tt
->text
);
1930 "Invalid size type for `%%arg' missing directive");
1932 free_tlist(origline
);
1933 return DIRECTIVE_FOUND
;
1937 /* Round up to even stack slots */
1938 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1940 /* Now define the macro for the argument */
1941 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1942 arg
, StackPointer
, offset
);
1943 do_directive(tokenize(directive
));
1946 /* Move to the next argument in the list */
1947 tline
= tline
->next
;
1948 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1949 tline
= tline
->next
;
1950 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1952 free_tlist(origline
);
1953 return DIRECTIVE_FOUND
;
1956 /* TASM like LOCAL directive to define local variables for a
1957 * function, in the following form:
1959 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1961 * The '= LocalSize' at the end is ignored by NASM, but is
1962 * required by TASM to define the local parameter size (and used
1963 * by the TASM macro package).
1965 offset
= LocalOffset
;
1967 char *local
, directive
[256];
1968 int size
= StackSize
;
1970 /* Find the argument name */
1971 tline
= tline
->next
;
1972 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1973 tline
= tline
->next
;
1974 if (!tline
|| tline
->type
!= TOK_ID
) {
1976 "`%%local' missing argument parameter");
1977 free_tlist(origline
);
1978 return DIRECTIVE_FOUND
;
1980 local
= tline
->text
;
1982 /* Find the argument size type */
1983 tline
= tline
->next
;
1984 if (!tline
|| tline
->type
!= TOK_OTHER
1985 || tline
->text
[0] != ':') {
1987 "Syntax error processing `%%local' directive");
1988 free_tlist(origline
);
1989 return DIRECTIVE_FOUND
;
1991 tline
= tline
->next
;
1992 if (!tline
|| tline
->type
!= TOK_ID
) {
1994 "`%%local' missing size type parameter");
1995 free_tlist(origline
);
1996 return DIRECTIVE_FOUND
;
1999 /* Allow macro expansion of type parameter */
2000 tt
= tokenize(tline
->text
);
2001 tt
= expand_smacro(tt
);
2002 size
= parse_size(tt
->text
);
2005 "Invalid size type for `%%local' missing directive");
2007 free_tlist(origline
);
2008 return DIRECTIVE_FOUND
;
2012 /* Round up to even stack slots */
2013 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2015 offset
+= size
; /* Negative offset, increment before */
2017 /* Now define the macro for the argument */
2018 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2019 local
, StackPointer
, offset
);
2020 do_directive(tokenize(directive
));
2022 /* Now define the assign to setup the enter_c macro correctly */
2023 snprintf(directive
, sizeof(directive
),
2024 "%%assign %%$localsize %%$localsize+%d", size
);
2025 do_directive(tokenize(directive
));
2027 /* Move to the next argument in the list */
2028 tline
= tline
->next
;
2029 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2030 tline
= tline
->next
;
2031 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2032 LocalOffset
= offset
;
2033 free_tlist(origline
);
2034 return DIRECTIVE_FOUND
;
2038 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2041 free_tlist(origline
);
2042 return DIRECTIVE_FOUND
;
2045 tline
= tline
->next
;
2047 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2048 tline
->type
!= TOK_INTERNAL_STRING
)) {
2049 error(ERR_NONFATAL
, "`%%include' expects a file name");
2050 free_tlist(origline
);
2051 return DIRECTIVE_FOUND
; /* but we did _something_ */
2055 "trailing garbage after `%%include' ignored");
2056 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2057 p
= tline
->text
+ 1; /* point past the quote to the name */
2058 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2060 p
= tline
->text
; /* internal_string is easier */
2061 expand_macros_in_string(&p
);
2062 inc
= nasm_malloc(sizeof(Include
));
2065 inc
->fp
= inc_fopen(p
);
2066 if (!inc
->fp
&& pass
== 0) {
2067 /* -MG given but file not found */
2070 inc
->fname
= src_set_fname(p
);
2071 inc
->lineno
= src_set_linnum(0);
2073 inc
->expansion
= NULL
;
2076 list
->uplevel(LIST_INCLUDE
);
2078 free_tlist(origline
);
2079 return DIRECTIVE_FOUND
;
2082 tline
= tline
->next
;
2084 tline
= expand_id(tline
);
2085 if (!tok_type_(tline
, TOK_ID
)) {
2086 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2087 free_tlist(origline
);
2088 return DIRECTIVE_FOUND
; /* but we did _something_ */
2091 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2092 ctx
= nasm_malloc(sizeof(Context
));
2094 ctx
->localmac
= hash_init(HASH_SMALL
);
2095 ctx
->name
= nasm_strdup(tline
->text
);
2096 ctx
->number
= unique
++;
2098 free_tlist(origline
);
2102 tline
= tline
->next
;
2104 tline
= expand_id(tline
);
2105 if (!tok_type_(tline
, TOK_ID
)) {
2106 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2107 free_tlist(origline
);
2108 return DIRECTIVE_FOUND
; /* but we did _something_ */
2111 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2113 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2115 nasm_free(cstk
->name
);
2116 cstk
->name
= nasm_strdup(tline
->text
);
2118 free_tlist(origline
);
2123 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2125 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2128 free_tlist(origline
);
2132 tline
->next
= expand_smacro(tline
->next
);
2133 tline
= tline
->next
;
2135 if (tok_type_(tline
, TOK_STRING
)) {
2136 p
= tline
->text
+ 1; /* point past the quote to the name */
2137 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2138 expand_macros_in_string(&p
);
2139 error(ERR_NONFATAL
, "%s", p
);
2142 p
= detoken(tline
, false);
2143 error(ERR_WARNING
, "%s", p
);
2146 free_tlist(origline
);
2150 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2153 j
= if_condition(tline
->next
, i
);
2154 tline
->next
= NULL
; /* it got freed */
2155 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2157 cond
= nasm_malloc(sizeof(Cond
));
2158 cond
->next
= istk
->conds
;
2161 free_tlist(origline
);
2162 return DIRECTIVE_FOUND
;
2166 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2167 if (emitting(istk
->conds
->state
)
2168 || istk
->conds
->state
== COND_NEVER
)
2169 istk
->conds
->state
= COND_NEVER
;
2172 * IMPORTANT: In the case of %if, we will already have
2173 * called expand_mmac_params(); however, if we're
2174 * processing an %elif we must have been in a
2175 * non-emitting mode, which would have inhibited
2176 * the normal invocation of expand_mmac_params(). Therefore,
2177 * we have to do it explicitly here.
2179 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2180 tline
->next
= NULL
; /* it got freed */
2181 istk
->conds
->state
=
2182 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2184 free_tlist(origline
);
2185 return DIRECTIVE_FOUND
;
2189 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2191 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2192 if (emitting(istk
->conds
->state
)
2193 || istk
->conds
->state
== COND_NEVER
)
2194 istk
->conds
->state
= COND_ELSE_FALSE
;
2196 istk
->conds
->state
= COND_ELSE_TRUE
;
2197 free_tlist(origline
);
2198 return DIRECTIVE_FOUND
;
2202 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2204 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2206 istk
->conds
= cond
->next
;
2208 free_tlist(origline
);
2209 return DIRECTIVE_FOUND
;
2215 "`%%%smacro': already defining a macro",
2216 (i
== PP_IMACRO
? "i" : ""));
2217 tline
= tline
->next
;
2219 tline
= expand_id(tline
);
2220 if (!tok_type_(tline
, TOK_ID
)) {
2222 "`%%%smacro' expects a macro name",
2223 (i
== PP_IMACRO
? "i" : ""));
2224 return DIRECTIVE_FOUND
;
2226 defining
= nasm_malloc(sizeof(MMacro
));
2227 defining
->name
= nasm_strdup(tline
->text
);
2228 defining
->casesense
= (i
== PP_MACRO
);
2229 defining
->plus
= false;
2230 defining
->nolist
= false;
2231 defining
->in_progress
= 0;
2232 defining
->rep_nest
= NULL
;
2233 tline
= expand_smacro(tline
->next
);
2235 if (!tok_type_(tline
, TOK_NUMBER
)) {
2237 "`%%%smacro' expects a parameter count",
2238 (i
== PP_IMACRO
? "i" : ""));
2239 defining
->nparam_min
= defining
->nparam_max
= 0;
2241 defining
->nparam_min
= defining
->nparam_max
=
2242 readnum(tline
->text
, &err
);
2245 "unable to parse parameter count `%s'", tline
->text
);
2247 if (tline
&& tok_is_(tline
->next
, "-")) {
2248 tline
= tline
->next
->next
;
2249 if (tok_is_(tline
, "*"))
2250 defining
->nparam_max
= INT_MAX
;
2251 else if (!tok_type_(tline
, TOK_NUMBER
))
2253 "`%%%smacro' expects a parameter count after `-'",
2254 (i
== PP_IMACRO
? "i" : ""));
2256 defining
->nparam_max
= readnum(tline
->text
, &err
);
2259 "unable to parse parameter count `%s'",
2261 if (defining
->nparam_min
> defining
->nparam_max
)
2263 "minimum parameter count exceeds maximum");
2266 if (tline
&& tok_is_(tline
->next
, "+")) {
2267 tline
= tline
->next
;
2268 defining
->plus
= true;
2270 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2271 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2272 tline
= tline
->next
;
2273 defining
->nolist
= true;
2275 mmac
= (MMacro
*) hash_findix(mmacros
, defining
->name
);
2277 if (!strcmp(mmac
->name
, defining
->name
) &&
2278 (mmac
->nparam_min
<= defining
->nparam_max
2280 && (defining
->nparam_min
<= mmac
->nparam_max
2283 "redefining multi-line macro `%s'", defining
->name
);
2289 * Handle default parameters.
2291 if (tline
&& tline
->next
) {
2292 defining
->dlist
= tline
->next
;
2294 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2295 &defining
->defaults
);
2297 defining
->dlist
= NULL
;
2298 defining
->defaults
= NULL
;
2300 defining
->expansion
= NULL
;
2301 free_tlist(origline
);
2302 return DIRECTIVE_FOUND
;
2307 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2308 return DIRECTIVE_FOUND
;
2310 mmhead
= (MMacro
**) hash_findi_add(mmacros
, defining
->name
);
2311 defining
->next
= *mmhead
;
2314 free_tlist(origline
);
2315 return DIRECTIVE_FOUND
;
2318 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2319 tline
= tline
->next
;
2320 if (tline
->next
== NULL
) {
2321 free_tlist(origline
);
2322 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2323 return DIRECTIVE_FOUND
;
2325 t
= expand_smacro(tline
->next
);
2327 free_tlist(origline
);
2330 tokval
.t_type
= TOKEN_INVALID
;
2332 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2335 return DIRECTIVE_FOUND
;
2338 "trailing garbage after expression ignored");
2339 if (!is_simple(evalresult
)) {
2340 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2341 return DIRECTIVE_FOUND
;
2344 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2345 mmac
= mmac
->next_active
;
2347 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2348 } else if (mmac
->nparam
== 0) {
2350 "`%%rotate' invoked within macro without parameters");
2352 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2354 rotate
%= (int)mmac
->nparam
;
2356 rotate
+= mmac
->nparam
;
2358 mmac
->rotate
= rotate
;
2360 return DIRECTIVE_FOUND
;
2365 tline
= tline
->next
;
2366 } while (tok_type_(tline
, TOK_WHITESPACE
));
2368 if (tok_type_(tline
, TOK_ID
) &&
2369 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2372 tline
= tline
->next
;
2373 } while (tok_type_(tline
, TOK_WHITESPACE
));
2377 t
= expand_smacro(tline
);
2379 tokval
.t_type
= TOKEN_INVALID
;
2381 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2383 free_tlist(origline
);
2384 return DIRECTIVE_FOUND
;
2388 "trailing garbage after expression ignored");
2389 if (!is_simple(evalresult
)) {
2390 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2391 return DIRECTIVE_FOUND
;
2393 count
= reloc_value(evalresult
) + 1;
2395 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2398 free_tlist(origline
);
2400 tmp_defining
= defining
;
2401 defining
= nasm_malloc(sizeof(MMacro
));
2402 defining
->name
= NULL
; /* flags this macro as a %rep block */
2403 defining
->casesense
= false;
2404 defining
->plus
= false;
2405 defining
->nolist
= nolist
;
2406 defining
->in_progress
= count
;
2407 defining
->nparam_min
= defining
->nparam_max
= 0;
2408 defining
->defaults
= NULL
;
2409 defining
->dlist
= NULL
;
2410 defining
->expansion
= NULL
;
2411 defining
->next_active
= istk
->mstk
;
2412 defining
->rep_nest
= tmp_defining
;
2413 return DIRECTIVE_FOUND
;
2416 if (!defining
|| defining
->name
) {
2417 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2418 return DIRECTIVE_FOUND
;
2422 * Now we have a "macro" defined - although it has no name
2423 * and we won't be entering it in the hash tables - we must
2424 * push a macro-end marker for it on to istk->expansion.
2425 * After that, it will take care of propagating itself (a
2426 * macro-end marker line for a macro which is really a %rep
2427 * block will cause the macro to be re-expanded, complete
2428 * with another macro-end marker to ensure the process
2429 * continues) until the whole expansion is forcibly removed
2430 * from istk->expansion by a %exitrep.
2432 l
= nasm_malloc(sizeof(Line
));
2433 l
->next
= istk
->expansion
;
2434 l
->finishes
= defining
;
2436 istk
->expansion
= l
;
2438 istk
->mstk
= defining
;
2440 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2441 tmp_defining
= defining
;
2442 defining
= defining
->rep_nest
;
2443 free_tlist(origline
);
2444 return DIRECTIVE_FOUND
;
2448 * We must search along istk->expansion until we hit a
2449 * macro-end marker for a macro with no name. Then we set
2450 * its `in_progress' flag to 0.
2452 for (l
= istk
->expansion
; l
; l
= l
->next
)
2453 if (l
->finishes
&& !l
->finishes
->name
)
2457 l
->finishes
->in_progress
= 0;
2459 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2460 free_tlist(origline
);
2461 return DIRECTIVE_FOUND
;
2467 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2469 tline
= tline
->next
;
2471 tline
= expand_id(tline
);
2472 if (!tline
|| (tline
->type
!= TOK_ID
&&
2473 (tline
->type
!= TOK_PREPROC_ID
||
2474 tline
->text
[1] != '$'))) {
2475 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2477 free_tlist(origline
);
2478 return DIRECTIVE_FOUND
;
2481 ctx
= get_ctx(tline
->text
, false);
2483 mname
= tline
->text
;
2485 param_start
= tline
= tline
->next
;
2488 /* Expand the macro definition now for %xdefine and %ixdefine */
2489 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2490 tline
= expand_smacro(tline
);
2492 if (tok_is_(tline
, "(")) {
2494 * This macro has parameters.
2497 tline
= tline
->next
;
2501 error(ERR_NONFATAL
, "parameter identifier expected");
2502 free_tlist(origline
);
2503 return DIRECTIVE_FOUND
;
2505 if (tline
->type
!= TOK_ID
) {
2507 "`%s': parameter identifier expected",
2509 free_tlist(origline
);
2510 return DIRECTIVE_FOUND
;
2512 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2513 tline
= tline
->next
;
2515 if (tok_is_(tline
, ",")) {
2516 tline
= tline
->next
;
2519 if (!tok_is_(tline
, ")")) {
2521 "`)' expected to terminate macro template");
2522 free_tlist(origline
);
2523 return DIRECTIVE_FOUND
;
2528 tline
= tline
->next
;
2530 if (tok_type_(tline
, TOK_WHITESPACE
))
2531 last
= tline
, tline
= tline
->next
;
2536 if (t
->type
== TOK_ID
) {
2537 for (tt
= param_start
; tt
; tt
= tt
->next
)
2538 if (tt
->type
>= TOK_SMAC_PARAM
&&
2539 !strcmp(tt
->text
, t
->text
))
2543 t
->next
= macro_start
;
2548 * Good. We now have a macro name, a parameter count, and a
2549 * token list (in reverse order) for an expansion. We ought
2550 * to be OK just to create an SMacro, store it, and let
2551 * free_tlist have the rest of the line (which we have
2552 * carefully re-terminated after chopping off the expansion
2555 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2556 free_tlist(origline
);
2557 return DIRECTIVE_FOUND
;
2560 tline
= tline
->next
;
2562 tline
= expand_id(tline
);
2563 if (!tline
|| (tline
->type
!= TOK_ID
&&
2564 (tline
->type
!= TOK_PREPROC_ID
||
2565 tline
->text
[1] != '$'))) {
2566 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2567 free_tlist(origline
);
2568 return DIRECTIVE_FOUND
;
2572 "trailing garbage after macro name ignored");
2575 /* Find the context that symbol belongs to */
2576 ctx
= get_ctx(tline
->text
, false);
2577 undef_smacro(ctx
, tline
->text
);
2578 free_tlist(origline
);
2579 return DIRECTIVE_FOUND
;
2584 tline
= tline
->next
;
2586 tline
= expand_id(tline
);
2587 if (!tline
|| (tline
->type
!= TOK_ID
&&
2588 (tline
->type
!= TOK_PREPROC_ID
||
2589 tline
->text
[1] != '$'))) {
2591 "`%%strlen' expects a macro identifier as first parameter");
2592 free_tlist(origline
);
2593 return DIRECTIVE_FOUND
;
2595 ctx
= get_ctx(tline
->text
, false);
2597 mname
= tline
->text
;
2599 tline
= expand_smacro(tline
->next
);
2603 while (tok_type_(t
, TOK_WHITESPACE
))
2605 /* t should now point to the string */
2606 if (t
->type
!= TOK_STRING
) {
2608 "`%%strlen` requires string as second parameter");
2610 free_tlist(origline
);
2611 return DIRECTIVE_FOUND
;
2614 macro_start
= nasm_malloc(sizeof(*macro_start
));
2615 macro_start
->next
= NULL
;
2616 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2617 macro_start
->mac
= NULL
;
2620 * We now have a macro name, an implicit parameter count of
2621 * zero, and a numeric token to use as an expansion. Create
2622 * and store an SMacro.
2624 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2626 free_tlist(origline
);
2627 return DIRECTIVE_FOUND
;
2632 tline
= tline
->next
;
2634 tline
= expand_id(tline
);
2635 if (!tline
|| (tline
->type
!= TOK_ID
&&
2636 (tline
->type
!= TOK_PREPROC_ID
||
2637 tline
->text
[1] != '$'))) {
2639 "`%%substr' expects a macro identifier as first parameter");
2640 free_tlist(origline
);
2641 return DIRECTIVE_FOUND
;
2643 ctx
= get_ctx(tline
->text
, false);
2645 mname
= tline
->text
;
2647 tline
= expand_smacro(tline
->next
);
2651 while (tok_type_(t
, TOK_WHITESPACE
))
2654 /* t should now point to the string */
2655 if (t
->type
!= TOK_STRING
) {
2657 "`%%substr` requires string as second parameter");
2659 free_tlist(origline
);
2660 return DIRECTIVE_FOUND
;
2665 tokval
.t_type
= TOKEN_INVALID
;
2667 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2670 free_tlist(origline
);
2671 return DIRECTIVE_FOUND
;
2673 if (!is_simple(evalresult
)) {
2674 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2676 free_tlist(origline
);
2677 return DIRECTIVE_FOUND
;
2680 macro_start
= nasm_malloc(sizeof(*macro_start
));
2681 macro_start
->next
= NULL
;
2682 macro_start
->text
= nasm_strdup("'''");
2683 if (evalresult
->value
> 0
2684 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2685 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2687 macro_start
->text
[2] = '\0';
2689 macro_start
->type
= TOK_STRING
;
2690 macro_start
->mac
= NULL
;
2693 * We now have a macro name, an implicit parameter count of
2694 * zero, and a numeric token to use as an expansion. Create
2695 * and store an SMacro.
2697 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2699 free_tlist(origline
);
2700 return DIRECTIVE_FOUND
;
2704 casesense
= (i
== PP_ASSIGN
);
2706 tline
= tline
->next
;
2708 tline
= expand_id(tline
);
2709 if (!tline
|| (tline
->type
!= TOK_ID
&&
2710 (tline
->type
!= TOK_PREPROC_ID
||
2711 tline
->text
[1] != '$'))) {
2713 "`%%%sassign' expects a macro identifier",
2714 (i
== PP_IASSIGN
? "i" : ""));
2715 free_tlist(origline
);
2716 return DIRECTIVE_FOUND
;
2718 ctx
= get_ctx(tline
->text
, false);
2720 mname
= tline
->text
;
2722 tline
= expand_smacro(tline
->next
);
2727 tokval
.t_type
= TOKEN_INVALID
;
2729 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2732 free_tlist(origline
);
2733 return DIRECTIVE_FOUND
;
2738 "trailing garbage after expression ignored");
2740 if (!is_simple(evalresult
)) {
2742 "non-constant value given to `%%%sassign'",
2743 (i
== PP_IASSIGN
? "i" : ""));
2744 free_tlist(origline
);
2745 return DIRECTIVE_FOUND
;
2748 macro_start
= nasm_malloc(sizeof(*macro_start
));
2749 macro_start
->next
= NULL
;
2750 make_tok_num(macro_start
, reloc_value(evalresult
));
2751 macro_start
->mac
= NULL
;
2754 * We now have a macro name, an implicit parameter count of
2755 * zero, and a numeric token to use as an expansion. Create
2756 * and store an SMacro.
2758 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2759 free_tlist(origline
);
2760 return DIRECTIVE_FOUND
;
2764 * Syntax is `%line nnn[+mmm] [filename]'
2766 tline
= tline
->next
;
2768 if (!tok_type_(tline
, TOK_NUMBER
)) {
2769 error(ERR_NONFATAL
, "`%%line' expects line number");
2770 free_tlist(origline
);
2771 return DIRECTIVE_FOUND
;
2773 k
= readnum(tline
->text
, &err
);
2775 tline
= tline
->next
;
2776 if (tok_is_(tline
, "+")) {
2777 tline
= tline
->next
;
2778 if (!tok_type_(tline
, TOK_NUMBER
)) {
2779 error(ERR_NONFATAL
, "`%%line' expects line increment");
2780 free_tlist(origline
);
2781 return DIRECTIVE_FOUND
;
2783 m
= readnum(tline
->text
, &err
);
2784 tline
= tline
->next
;
2790 nasm_free(src_set_fname(detoken(tline
, false)));
2792 free_tlist(origline
);
2793 return DIRECTIVE_FOUND
;
2797 "preprocessor directive `%s' not yet implemented",
2801 return DIRECTIVE_FOUND
;
2805 * Ensure that a macro parameter contains a condition code and
2806 * nothing else. Return the condition code index if so, or -1
2809 static int find_cc(Token
* t
)
2815 return -1; /* Probably a %+ without a space */
2818 if (t
->type
!= TOK_ID
)
2822 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2826 j
= elements(conditions
);
2829 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2845 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2846 * %-n) and MMacro-local identifiers (%%foo).
2848 static Token
*expand_mmac_params(Token
* tline
)
2850 Token
*t
, *tt
, **tail
, *thead
;
2856 if (tline
->type
== TOK_PREPROC_ID
&&
2857 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2858 && tline
->text
[2]) || tline
->text
[1] == '%'
2859 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2861 int type
= 0, cc
; /* type = 0 to placate optimisers */
2868 tline
= tline
->next
;
2871 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2872 mac
= mac
->next_active
;
2874 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2876 switch (t
->text
[1]) {
2878 * We have to make a substitution of one of the
2879 * forms %1, %-1, %+1, %%foo, %0.
2883 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2884 text
= nasm_strdup(tmpbuf
);
2888 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2890 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
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 if (inverse_ccs
[cc
] == -1) {
2911 "condition code `%s' is not invertible",
2916 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2920 n
= atoi(t
->text
+ 2) - 1;
2921 if (n
>= mac
->nparam
)
2924 if (mac
->nparam
> 1)
2925 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2926 tt
= mac
->params
[n
];
2931 "macro parameter %d is not a condition code",
2936 text
= nasm_strdup(conditions
[cc
]);
2940 n
= atoi(t
->text
+ 1) - 1;
2941 if (n
>= mac
->nparam
)
2944 if (mac
->nparam
> 1)
2945 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2946 tt
= mac
->params
[n
];
2949 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2950 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2951 tail
= &(*tail
)->next
;
2955 text
= NULL
; /* we've done it here */
2971 tline
= tline
->next
;
2978 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2980 case TOK_WHITESPACE
:
2981 if (tt
->type
== TOK_WHITESPACE
) {
2982 t
->next
= delete_Token(tt
);
2986 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2987 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2990 t
->next
= delete_Token(tt
);
2994 if (tt
->type
== TOK_NUMBER
) {
2995 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2998 t
->next
= delete_Token(tt
);
3009 * Expand all single-line macro calls made in the given line.
3010 * Return the expanded version of the line. The original is deemed
3011 * to be destroyed in the process. (In reality we'll just move
3012 * Tokens from input to output a lot of the time, rather than
3013 * actually bothering to destroy and replicate.)
3015 #define DEADMAN_LIMIT (1 << 20)
3017 static Token
*expand_smacro(Token
* tline
)
3019 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3020 SMacro
*head
= NULL
, *m
;
3023 unsigned int nparam
, sparam
;
3024 int brackets
, rescan
;
3025 Token
*org_tline
= tline
;
3028 int deadman
= DEADMAN_LIMIT
;
3031 * Trick: we should avoid changing the start token pointer since it can
3032 * be contained in "next" field of other token. Because of this
3033 * we allocate a copy of first token and work with it; at the end of
3034 * routine we copy it back
3038 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3040 tline
->mac
= org_tline
->mac
;
3041 nasm_free(org_tline
->text
);
3042 org_tline
->text
= NULL
;
3049 while (tline
) { /* main token loop */
3051 error(ERR_NONFATAL
, "interminable macro recursion");
3055 if ((mname
= tline
->text
)) {
3056 /* if this token is a local macro, look in local context */
3057 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3058 ctx
= get_ctx(mname
, true);
3062 head
= (SMacro
*) hash_findix(ctx
? ctx
->localmac
: smacros
,
3066 * We've hit an identifier. As in is_mmacro below, we first
3067 * check whether the identifier is a single-line macro at
3068 * all, then think about checking for parameters if
3071 for (m
= head
; m
; m
= m
->next
)
3072 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3078 if (m
->nparam
== 0) {
3080 * Simple case: the macro is parameterless. Discard the
3081 * one token that the macro call took, and push the
3082 * expansion back on the to-do stack.
3084 if (!m
->expansion
) {
3085 if (!strcmp("__FILE__", m
->name
)) {
3087 src_get(&num
, &(tline
->text
));
3088 nasm_quote(&(tline
->text
));
3089 tline
->type
= TOK_STRING
;
3092 if (!strcmp("__LINE__", m
->name
)) {
3093 nasm_free(tline
->text
);
3094 make_tok_num(tline
, src_get_linnum());
3097 if (!strcmp("__BITS__", m
->name
)) {
3098 nasm_free(tline
->text
);
3099 make_tok_num(tline
, globalbits
);
3102 tline
= delete_Token(tline
);
3107 * Complicated case: at least one macro with this name
3108 * exists and takes parameters. We must find the
3109 * parameters in the call, count them, find the SMacro
3110 * that corresponds to that form of the macro call, and
3111 * substitute for the parameters when we expand. What a
3114 /*tline = tline->next;
3115 skip_white_(tline); */
3118 while (tok_type_(t
, TOK_SMAC_END
)) {
3119 t
->mac
->in_progress
= false;
3121 t
= tline
->next
= delete_Token(t
);
3124 } while (tok_type_(tline
, TOK_WHITESPACE
));
3125 if (!tok_is_(tline
, "(")) {
3127 * This macro wasn't called with parameters: ignore
3128 * the call. (Behaviour borrowed from gnu cpp.)
3137 sparam
= PARAM_DELTA
;
3138 params
= nasm_malloc(sparam
* sizeof(Token
*));
3139 params
[0] = tline
->next
;
3140 paramsize
= nasm_malloc(sparam
* sizeof(int));
3142 while (true) { /* parameter loop */
3144 * For some unusual expansions
3145 * which concatenates function call
3148 while (tok_type_(t
, TOK_SMAC_END
)) {
3149 t
->mac
->in_progress
= false;
3151 t
= tline
->next
= delete_Token(t
);
3157 "macro call expects terminating `)'");
3160 if (tline
->type
== TOK_WHITESPACE
3162 if (paramsize
[nparam
])
3165 params
[nparam
] = tline
->next
;
3166 continue; /* parameter loop */
3168 if (tline
->type
== TOK_OTHER
3169 && tline
->text
[1] == 0) {
3170 char ch
= tline
->text
[0];
3171 if (ch
== ',' && !paren
&& brackets
<= 0) {
3172 if (++nparam
>= sparam
) {
3173 sparam
+= PARAM_DELTA
;
3174 params
= nasm_realloc(params
,
3179 nasm_realloc(paramsize
,
3183 params
[nparam
] = tline
->next
;
3184 paramsize
[nparam
] = 0;
3186 continue; /* parameter loop */
3189 (brackets
> 0 || (brackets
== 0 &&
3190 !paramsize
[nparam
])))
3192 if (!(brackets
++)) {
3193 params
[nparam
] = tline
->next
;
3194 continue; /* parameter loop */
3197 if (ch
== '}' && brackets
> 0)
3198 if (--brackets
== 0) {
3200 continue; /* parameter loop */
3202 if (ch
== '(' && !brackets
)
3204 if (ch
== ')' && brackets
<= 0)
3210 error(ERR_NONFATAL
, "braces do not "
3211 "enclose all of macro parameter");
3213 paramsize
[nparam
] += white
+ 1;
3215 } /* parameter loop */
3217 while (m
&& (m
->nparam
!= nparam
||
3218 mstrcmp(m
->name
, mname
,
3222 error(ERR_WARNING
| ERR_WARN_MNP
,
3223 "macro `%s' exists, "
3224 "but not taking %d parameters",
3225 mstart
->text
, nparam
);
3228 if (m
&& m
->in_progress
)
3230 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3232 * Design question: should we handle !tline, which
3233 * indicates missing ')' here, or expand those
3234 * macros anyway, which requires the (t) test a few
3238 nasm_free(paramsize
);
3242 * Expand the macro: we are placed on the last token of the
3243 * call, so that we can easily split the call from the
3244 * following tokens. We also start by pushing an SMAC_END
3245 * token for the cycle removal.
3252 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3254 m
->in_progress
= true;
3256 for (t
= m
->expansion
; t
; t
= t
->next
) {
3257 if (t
->type
>= TOK_SMAC_PARAM
) {
3258 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3262 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3263 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3266 new_Token(tline
, ttt
->type
, ttt
->text
,
3272 } else if (t
->type
== TOK_PREPROC_Q
) {
3273 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3275 } else if (t
->type
== TOK_PREPROC_QQ
) {
3276 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3279 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3285 * Having done that, get rid of the macro call, and clean
3286 * up the parameters.
3289 nasm_free(paramsize
);
3291 continue; /* main token loop */
3296 if (tline
->type
== TOK_SMAC_END
) {
3297 tline
->mac
->in_progress
= false;
3298 tline
= delete_Token(tline
);
3301 tline
= tline
->next
;
3309 * Now scan the entire line and look for successive TOK_IDs that resulted
3310 * after expansion (they can't be produced by tokenize()). The successive
3311 * TOK_IDs should be concatenated.
3312 * Also we look for %+ tokens and concatenate the tokens before and after
3313 * them (without white spaces in between).
3318 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3322 if (t
->next
->type
== TOK_ID
||
3323 t
->next
->type
== TOK_PREPROC_ID
||
3324 t
->next
->type
== TOK_NUMBER
) {
3325 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3327 t
->next
= delete_Token(t
->next
);
3330 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3331 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3332 strcmp(t
->next
->next
->text
, "%+") == 0) {
3333 /* free the next whitespace, the %+ token and next whitespace */
3335 for (i
= 1; i
<= 3; i
++) {
3337 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3339 t
->next
= delete_Token(t
->next
);
3344 /* If we concatenaded something, re-scan the line for macros */
3352 *org_tline
= *thead
;
3353 /* since we just gave text to org_line, don't free it */
3355 delete_Token(thead
);
3357 /* the expression expanded to empty line;
3358 we can't return NULL for some reasons
3359 we just set the line to a single WHITESPACE token. */
3360 memset(org_tline
, 0, sizeof(*org_tline
));
3361 org_tline
->text
= NULL
;
3362 org_tline
->type
= TOK_WHITESPACE
;
3371 * Similar to expand_smacro but used exclusively with macro identifiers
3372 * right before they are fetched in. The reason is that there can be
3373 * identifiers consisting of several subparts. We consider that if there
3374 * are more than one element forming the name, user wants a expansion,
3375 * otherwise it will be left as-is. Example:
3379 * the identifier %$abc will be left as-is so that the handler for %define
3380 * will suck it and define the corresponding value. Other case:
3382 * %define _%$abc cde
3384 * In this case user wants name to be expanded *before* %define starts
3385 * working, so we'll expand %$abc into something (if it has a value;
3386 * otherwise it will be left as-is) then concatenate all successive
3389 static Token
*expand_id(Token
* tline
)
3391 Token
*cur
, *oldnext
= NULL
;
3393 if (!tline
|| !tline
->next
)
3398 (cur
->next
->type
== TOK_ID
||
3399 cur
->next
->type
== TOK_PREPROC_ID
3400 || cur
->next
->type
== TOK_NUMBER
))
3403 /* If identifier consists of just one token, don't expand */
3408 oldnext
= cur
->next
; /* Detach the tail past identifier */
3409 cur
->next
= NULL
; /* so that expand_smacro stops here */
3412 tline
= expand_smacro(tline
);
3415 /* expand_smacro possibly changhed tline; re-scan for EOL */
3417 while (cur
&& cur
->next
)
3420 cur
->next
= oldnext
;
3427 * Determine whether the given line constitutes a multi-line macro
3428 * call, and return the MMacro structure called if so. Doesn't have
3429 * to check for an initial label - that's taken care of in
3430 * expand_mmacro - but must check numbers of parameters. Guaranteed
3431 * to be called with tline->type == TOK_ID, so the putative macro
3432 * name is easy to find.
3434 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3440 head
= (MMacro
*) hash_findix(mmacros
, tline
->text
);
3443 * Efficiency: first we see if any macro exists with the given
3444 * name. If not, we can return NULL immediately. _Then_ we
3445 * count the parameters, and then we look further along the
3446 * list if necessary to find the proper MMacro.
3448 for (m
= head
; m
; m
= m
->next
)
3449 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3455 * OK, we have a potential macro. Count and demarcate the
3458 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3461 * So we know how many parameters we've got. Find the MMacro
3462 * structure that handles this number.
3465 if (m
->nparam_min
<= nparam
3466 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3468 * This one is right. Just check if cycle removal
3469 * prohibits us using it before we actually celebrate...
3471 if (m
->in_progress
) {
3474 "self-reference in multi-line macro `%s'", m
->name
);
3480 * It's right, and we can use it. Add its default
3481 * parameters to the end of our list if necessary.
3483 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3485 nasm_realloc(params
,
3486 ((m
->nparam_min
+ m
->ndefs
+
3487 1) * sizeof(*params
)));
3488 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3489 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3494 * If we've gone over the maximum parameter count (and
3495 * we're in Plus mode), ignore parameters beyond
3498 if (m
->plus
&& nparam
> m
->nparam_max
)
3499 nparam
= m
->nparam_max
;
3501 * Then terminate the parameter list, and leave.
3503 if (!params
) { /* need this special case */
3504 params
= nasm_malloc(sizeof(*params
));
3507 params
[nparam
] = NULL
;
3508 *params_array
= params
;
3512 * This one wasn't right: look for the next one with the
3515 for (m
= m
->next
; m
; m
= m
->next
)
3516 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3521 * After all that, we didn't find one with the right number of
3522 * parameters. Issue a warning, and fail to expand the macro.
3524 error(ERR_WARNING
| ERR_WARN_MNP
,
3525 "macro `%s' exists, but not taking %d parameters",
3526 tline
->text
, nparam
);
3532 * Expand the multi-line macro call made by the given line, if
3533 * there is one to be expanded. If there is, push the expansion on
3534 * istk->expansion and return 1. Otherwise return 0.
3536 static int expand_mmacro(Token
* tline
)
3538 Token
*startline
= tline
;
3539 Token
*label
= NULL
;
3540 int dont_prepend
= 0;
3541 Token
**params
, *t
, *mtok
, *tt
;
3544 int i
, nparam
, *paramlen
;
3548 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3549 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3552 m
= is_mmacro(t
, ¶ms
);
3556 * We have an id which isn't a macro call. We'll assume
3557 * it might be a label; we'll also check to see if a
3558 * colon follows it. Then, if there's another id after
3559 * that lot, we'll check it again for macro-hood.
3563 if (tok_type_(t
, TOK_WHITESPACE
))
3564 last
= t
, t
= t
->next
;
3565 if (tok_is_(t
, ":")) {
3567 last
= t
, t
= t
->next
;
3568 if (tok_type_(t
, TOK_WHITESPACE
))
3569 last
= t
, t
= t
->next
;
3571 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3578 * Fix up the parameters: this involves stripping leading and
3579 * trailing whitespace, then stripping braces if they are
3582 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3583 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3585 for (i
= 0; params
[i
]; i
++) {
3587 int comma
= (!m
->plus
|| i
< nparam
- 1);
3591 if (tok_is_(t
, "{"))
3592 t
= t
->next
, brace
= true, comma
= false;
3596 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3597 break; /* ... because we have hit a comma */
3598 if (comma
&& t
->type
== TOK_WHITESPACE
3599 && tok_is_(t
->next
, ","))
3600 break; /* ... or a space then a comma */
3601 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3602 break; /* ... or a brace */
3609 * OK, we have a MMacro structure together with a set of
3610 * parameters. We must now go through the expansion and push
3611 * copies of each Line on to istk->expansion. Substitution of
3612 * parameter tokens and macro-local tokens doesn't get done
3613 * until the single-line macro substitution process; this is
3614 * because delaying them allows us to change the semantics
3615 * later through %rotate.
3617 * First, push an end marker on to istk->expansion, mark this
3618 * macro as in progress, and set up its invocation-specific
3621 ll
= nasm_malloc(sizeof(Line
));
3622 ll
->next
= istk
->expansion
;
3625 istk
->expansion
= ll
;
3627 m
->in_progress
= true;
3632 m
->paramlen
= paramlen
;
3633 m
->unique
= unique
++;
3636 m
->next_active
= istk
->mstk
;
3639 for (l
= m
->expansion
; l
; l
= l
->next
) {
3642 ll
= nasm_malloc(sizeof(Line
));
3643 ll
->finishes
= NULL
;
3644 ll
->next
= istk
->expansion
;
3645 istk
->expansion
= ll
;
3648 for (t
= l
->first
; t
; t
= t
->next
) {
3652 tt
= *tail
= new_Token(NULL
, TOK_ID
, mtok
->text
, 0);
3654 case TOK_PREPROC_QQ
:
3655 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3657 case TOK_PREPROC_ID
:
3658 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3666 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3675 * If we had a label, push it on as the first line of
3676 * the macro expansion.
3679 if (dont_prepend
< 0)
3680 free_tlist(startline
);
3682 ll
= nasm_malloc(sizeof(Line
));
3683 ll
->finishes
= NULL
;
3684 ll
->next
= istk
->expansion
;
3685 istk
->expansion
= ll
;
3686 ll
->first
= startline
;
3687 if (!dont_prepend
) {
3689 label
= label
->next
;
3690 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3695 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3701 * Since preprocessor always operate only on the line that didn't
3702 * arrived yet, we should always use ERR_OFFBY1. Also since user
3703 * won't want to see same error twice (preprocessing is done once
3704 * per pass) we will want to show errors only during pass one.
3706 static void error(int severity
, const char *fmt
, ...)
3711 /* If we're in a dead branch of IF or something like it, ignore the error */
3712 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3716 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3719 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3720 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3721 istk
->mstk
->lineno
, buff
);
3723 _error(severity
| ERR_PASS1
, "%s", buff
);
3727 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3732 istk
= nasm_malloc(sizeof(Include
));
3735 istk
->expansion
= NULL
;
3737 istk
->fp
= fopen(file
, "r");
3739 src_set_fname(nasm_strdup(file
));
3743 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3748 if (tasm_compatible_mode
) {
3749 stdmacpos
= nasm_stdmac
;
3751 stdmacpos
= nasm_stdmac_after_tasm
;
3753 any_extrastdmac
= (extrastdmac
!= NULL
);
3759 static char *pp_getline(void)
3766 * Fetch a tokenized line, either from the macro-expansion
3767 * buffer or from the input file.
3770 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3771 Line
*l
= istk
->expansion
;
3772 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3776 * This is a macro-end marker for a macro with no
3777 * name, which means it's not really a macro at all
3778 * but a %rep block, and the `in_progress' field is
3779 * more than 1, meaning that we still need to
3780 * repeat. (1 means the natural last repetition; 0
3781 * means termination by %exitrep.) We have
3782 * therefore expanded up to the %endrep, and must
3783 * push the whole block on to the expansion buffer
3784 * again. We don't bother to remove the macro-end
3785 * marker: we'd only have to generate another one
3788 l
->finishes
->in_progress
--;
3789 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3790 Token
*t
, *tt
, **tail
;
3792 ll
= nasm_malloc(sizeof(Line
));
3793 ll
->next
= istk
->expansion
;
3794 ll
->finishes
= NULL
;
3798 for (t
= l
->first
; t
; t
= t
->next
) {
3799 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3801 new_Token(NULL
, t
->type
, t
->text
, 0);
3806 istk
->expansion
= ll
;
3810 * Check whether a `%rep' was started and not ended
3811 * within this macro expansion. This can happen and
3812 * should be detected. It's a fatal error because
3813 * I'm too confused to work out how to recover
3819 "defining with name in expansion");
3820 else if (istk
->mstk
->name
)
3822 "`%%rep' without `%%endrep' within"
3823 " expansion of macro `%s'",
3828 * FIXME: investigate the relationship at this point between
3829 * istk->mstk and l->finishes
3832 MMacro
*m
= istk
->mstk
;
3833 istk
->mstk
= m
->next_active
;
3836 * This was a real macro call, not a %rep, and
3837 * therefore the parameter information needs to
3840 nasm_free(m
->params
);
3841 free_tlist(m
->iline
);
3842 nasm_free(m
->paramlen
);
3843 l
->finishes
->in_progress
= false;
3847 istk
->expansion
= l
->next
;
3849 list
->downlevel(LIST_MACRO
);
3852 while (1) { /* until we get a line we can use */
3854 if (istk
->expansion
) { /* from a macro expansion */
3856 Line
*l
= istk
->expansion
;
3858 istk
->mstk
->lineno
++;
3860 istk
->expansion
= l
->next
;
3862 p
= detoken(tline
, false);
3863 list
->line(LIST_MACRO
, p
);
3868 if (line
) { /* from the current input file */
3869 line
= prepreproc(line
);
3870 tline
= tokenize(line
);
3875 * The current file has ended; work down the istk
3882 "expected `%%endif' before end of file");
3883 /* only set line and file name if there's a next node */
3885 src_set_linnum(i
->lineno
);
3886 nasm_free(src_set_fname(i
->fname
));
3889 list
->downlevel(LIST_INCLUDE
);
3897 * We must expand MMacro parameters and MMacro-local labels
3898 * _before_ we plunge into directive processing, to cope
3899 * with things like `%define something %1' such as STRUC
3900 * uses. Unless we're _defining_ a MMacro, in which case
3901 * those tokens should be left alone to go into the
3902 * definition; and unless we're in a non-emitting
3903 * condition, in which case we don't want to meddle with
3906 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3907 tline
= expand_mmac_params(tline
);
3910 * Check the line to see if it's a preprocessor directive.
3912 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3914 } else if (defining
) {
3916 * We're defining a multi-line macro. We emit nothing
3918 * shove the tokenized line on to the macro definition.
3920 Line
*l
= nasm_malloc(sizeof(Line
));
3921 l
->next
= defining
->expansion
;
3923 l
->finishes
= false;
3924 defining
->expansion
= l
;
3926 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3928 * We're in a non-emitting branch of a condition block.
3929 * Emit nothing at all, not even a blank line: when we
3930 * emerge from the condition we'll give a line-number
3931 * directive so we keep our place correctly.
3935 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3937 * We're in a %rep block which has been terminated, so
3938 * we're walking through to the %endrep without
3939 * emitting anything. Emit nothing at all, not even a
3940 * blank line: when we emerge from the %rep block we'll
3941 * give a line-number directive so we keep our place
3947 tline
= expand_smacro(tline
);
3948 if (!expand_mmacro(tline
)) {
3950 * De-tokenize the line again, and emit it.
3952 line
= detoken(tline
, true);
3956 continue; /* expand_mmacro calls free_tlist */
3964 static void pp_cleanup(int pass
)
3967 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3969 free_mmacro(defining
);
3978 nasm_free(i
->fname
);
3989 void pp_include_path(char *path
)
3993 i
= nasm_malloc(sizeof(IncPath
));
3994 i
->path
= path
? nasm_strdup(path
) : NULL
;
3997 if (ipath
!= NULL
) {
3999 while (j
->next
!= NULL
)
4010 * This function is used to "export" the include paths, e.g.
4011 * the paths specified in the '-I' command switch.
4012 * The need for such exporting is due to the 'incbin' directive,
4013 * which includes raw binary files (unlike '%include', which
4014 * includes text source files). It would be real nice to be
4015 * able to specify paths to search for incbin'ned files also.
4016 * So, this is a simple workaround.
4018 * The function use is simple:
4020 * The 1st call (with NULL argument) returns a pointer to the 1st path
4021 * (char** type) or NULL if none include paths available.
4023 * All subsequent calls take as argument the value returned by this
4024 * function last. The return value is either the next path
4025 * (char** type) or NULL if the end of the paths list is reached.
4027 * It is maybe not the best way to do things, but I didn't want
4028 * to export too much, just one or two functions and no types or
4029 * variables exported.
4031 * Can't say I like the current situation with e.g. this path list either,
4032 * it seems to be never deallocated after creation...
4034 char **pp_get_include_path_ptr(char **pPrevPath
)
4036 /* This macro returns offset of a member of a structure */
4037 #define GetMemberOffset(StructType,MemberName)\
4038 ((size_t)&((StructType*)0)->MemberName)
4041 if (pPrevPath
== NULL
) {
4043 return &ipath
->path
;
4047 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
4053 #undef GetMemberOffset
4056 void pp_pre_include(char *fname
)
4058 Token
*inc
, *space
, *name
;
4061 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4062 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4063 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4065 l
= nasm_malloc(sizeof(Line
));
4068 l
->finishes
= false;
4072 void pp_pre_define(char *definition
)
4078 equals
= strchr(definition
, '=');
4079 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4080 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4083 space
->next
= tokenize(definition
);
4087 l
= nasm_malloc(sizeof(Line
));
4090 l
->finishes
= false;
4094 void pp_pre_undefine(char *definition
)
4099 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4100 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4101 space
->next
= tokenize(definition
);
4103 l
= nasm_malloc(sizeof(Line
));
4106 l
->finishes
= false;
4111 * Added by Keith Kanios:
4113 * This function is used to assist with "runtime" preprocessor
4114 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4116 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4117 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4120 void pp_runtime(char *definition
)
4124 def
= tokenize(definition
);
4125 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4130 void pp_extra_stdmac(const char **macros
)
4132 extrastdmac
= macros
;
4135 static void make_tok_num(Token
* tok
, int64_t val
)
4138 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4139 tok
->text
= nasm_strdup(numbuf
);
4140 tok
->type
= TOK_NUMBER
;