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.
132 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!!! */
162 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
168 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
169 enum pp_token_type type
;
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
211 MMacro
*mstk
; /* stack of active macros/reps */
215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
244 COND_IF_TRUE
, COND_IF_FALSE
,
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
250 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
261 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
264 * These defines are used as the possible return values for do_directive
266 #define NO_DIRECTIVE_FOUND 0
267 #define DIRECTIVE_FOUND 1
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
275 static const char * const conditions
[] = {
276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
281 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
282 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
283 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
286 static const enum pp_conds inverse_ccs
[] = {
287 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
288 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
,
289 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
295 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
296 static int is_condition(enum preproc_token arg
)
298 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
301 /* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
308 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
309 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
312 static const char * const tasm_directives
[] = {
313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
317 static int StackSize
= 4;
318 static char *StackPointer
= "ebp";
319 static int ArgOffset
= 8;
320 static int LocalOffset
= 0;
322 static Context
*cstk
;
323 static Include
*istk
;
324 static IncPath
*ipath
= NULL
;
326 static efunc _error
; /* Pointer to client-provided error reporting function */
327 static evalfunc evaluate
;
329 static int pass
; /* HACK: pass 0 = generate dependencies only */
331 static uint64_t unique
; /* unique identifier numbers */
333 static Line
*predef
= NULL
;
335 static ListGen
*list
;
338 * The current set of multi-line macros we have defined.
340 static struct hash_table mmacros
;
343 * The current set of single-line macros we have defined.
345 static struct hash_table smacros
;
348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
351 static MMacro
*defining
;
354 * The number of macro parameters to allocate space for at a time.
356 #define PARAM_DELTA 16
359 * The standard macro set: defined in macros.c in the array nasm_stdmac.
360 * This gives our position in the macro set, when we're processing it.
362 static const char * const *stdmacpos
;
365 * The extra standard macros that come from the object format, if
368 static const char * const *extrastdmac
= NULL
;
369 bool any_extrastdmac
;
372 * Tokens are allocated in blocks to improve speed
374 #define TOKEN_BLOCKSIZE 4096
375 static Token
*freeTokens
= NULL
;
381 static Blocks blocks
= { NULL
, NULL
};
384 * Forward declarations.
386 static Token
*expand_mmac_params(Token
* tline
);
387 static Token
*expand_smacro(Token
* tline
);
388 static Token
*expand_id(Token
* tline
);
389 static Context
*get_ctx(char *name
, bool all_contexts
);
390 static void make_tok_num(Token
* tok
, int64_t val
);
391 static void error(int severity
, const char *fmt
, ...);
392 static void *new_Block(size_t size
);
393 static void delete_Blocks(void);
394 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
395 static Token
*delete_Token(Token
* t
);
398 * Macros for safe checking of token pointers, avoid *(NULL)
400 #define tok_type_(x,t) ((x) && (x)->type == (t))
401 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
402 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
403 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
405 /* Handle TASM specific directives, which do not contain a % in
406 * front of them. We do it here because I could not find any other
407 * place to do it for the moment, and it is a hack (ideally it would
408 * be nice to be able to use the NASM pre-processor to do it).
410 static char *check_tasm_directive(char *line
)
412 int32_t i
, j
, k
, m
, len
;
413 char *p
= line
, *oldline
, oldchar
;
415 /* Skip whitespace */
416 while (isspace(*p
) && *p
!= 0)
419 /* Binary search for the directive name */
421 j
= elements(tasm_directives
);
423 while (!isspace(p
[len
]) && p
[len
] != 0)
430 m
= nasm_stricmp(p
, tasm_directives
[k
]);
432 /* We have found a directive, so jam a % in front of it
433 * so that NASM will then recognise it as one if it's own.
438 line
= nasm_malloc(len
+ 2);
440 if (k
== TM_IFDIFI
) {
441 /* NASM does not recognise IFDIFI, so we convert it to
442 * %ifdef BOGUS. This is not used in NASM comaptible
443 * code, but does need to parse for the TASM macro
446 strcpy(line
+ 1, "ifdef BOGUS");
448 memcpy(line
+ 1, p
, len
+ 1);
463 * The pre-preprocessing stage... This function translates line
464 * number indications as they emerge from GNU cpp (`# lineno "file"
465 * flags') into NASM preprocessor line number indications (`%line
468 static char *prepreproc(char *line
)
471 char *fname
, *oldline
;
473 if (line
[0] == '#' && line
[1] == ' ') {
476 lineno
= atoi(fname
);
477 fname
+= strspn(fname
, "0123456789 ");
480 fnlen
= strcspn(fname
, "\"");
481 line
= nasm_malloc(20 + fnlen
);
482 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
485 if (tasm_compatible_mode
)
486 return check_tasm_directive(line
);
491 * Free a linked list of tokens.
493 static void free_tlist(Token
* list
)
496 list
= delete_Token(list
);
501 * Free a linked list of lines.
503 static void free_llist(Line
* list
)
509 free_tlist(l
->first
);
517 static void free_mmacro(MMacro
* m
)
520 free_tlist(m
->dlist
);
521 nasm_free(m
->defaults
);
522 free_llist(m
->expansion
);
527 * Free all currently defined macros, and free the hash tables
529 static void free_smacro_table(struct hash_table
*smt
)
533 struct hash_tbl_node
*it
= NULL
;
535 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
536 nasm_free((void *)key
);
538 SMacro
*ns
= s
->next
;
540 free_tlist(s
->expansion
);
548 static void free_mmacro_table(struct hash_table
*mmt
)
552 struct hash_tbl_node
*it
= NULL
;
555 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
556 nasm_free((void *)key
);
558 MMacro
*nm
= m
->next
;
566 static void free_macros(void)
568 free_smacro_table(&smacros
);
569 free_mmacro_table(&mmacros
);
573 * Initialize the hash tables
575 static void init_macros(void)
577 hash_init(&smacros
, HASH_LARGE
);
578 hash_init(&mmacros
, HASH_LARGE
);
582 * Pop the context stack.
584 static void ctx_pop(void)
589 free_smacro_table(&c
->localmac
);
595 * Search for a key in the hash index; adding it if necessary
596 * (in which case we initialize the data pointer to NULL.)
599 hash_findi_add(struct hash_table
*hash
, const char *str
)
601 struct hash_insert hi
;
605 r
= hash_findi(hash
, str
, &hi
);
609 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
610 return hash_add(&hi
, strx
, NULL
);
614 * Like hash_findi, but returns the data element rather than a pointer
615 * to it. Used only when not adding a new element, hence no third
619 hash_findix(struct hash_table
*hash
, const char *str
)
623 p
= hash_findi(hash
, str
, NULL
);
624 return p
? *p
: NULL
;
627 #define BUF_DELTA 512
629 * Read a line from the top file in istk, handling multiple CR/LFs
630 * at the end of the line read, and handling spurious ^Zs. Will
631 * return lines from the standard macro set if this has not already
634 static char *read_line(void)
636 char *buffer
, *p
, *q
;
637 int bufsize
, continued_count
;
641 char *ret
= nasm_strdup(*stdmacpos
++);
642 if (!*stdmacpos
&& any_extrastdmac
) {
643 stdmacpos
= extrastdmac
;
644 any_extrastdmac
= false;
648 * Nasty hack: here we push the contents of `predef' on
649 * to the top-level expansion stack, since this is the
650 * most convenient way to implement the pre-include and
651 * pre-define features.
655 Token
*head
, **tail
, *t
;
657 for (pd
= predef
; pd
; pd
= pd
->next
) {
660 for (t
= pd
->first
; t
; t
= t
->next
) {
661 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
662 tail
= &(*tail
)->next
;
664 l
= nasm_malloc(sizeof(Line
));
665 l
->next
= istk
->expansion
;
678 buffer
= nasm_malloc(BUF_DELTA
);
682 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
686 if (p
> buffer
&& p
[-1] == '\n') {
687 /* Convert backslash-CRLF line continuation sequences into
688 nothing at all (for DOS and Windows) */
689 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
694 /* Also convert backslash-LF line continuation sequences into
695 nothing at all (for Unix) */
696 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
704 if (p
- buffer
> bufsize
- 10) {
705 int32_t offset
= p
- buffer
;
706 bufsize
+= BUF_DELTA
;
707 buffer
= nasm_realloc(buffer
, bufsize
);
708 p
= buffer
+ offset
; /* prevent stale-pointer problems */
712 if (!q
&& p
== buffer
) {
717 src_set_linnum(src_get_linnum() + istk
->lineinc
+
718 (continued_count
* istk
->lineinc
));
721 * Play safe: remove CRs as well as LFs, if any of either are
722 * present at the end of the line.
724 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
728 * Handle spurious ^Z, which may be inserted into source files
729 * by some file transfer utilities.
731 buffer
[strcspn(buffer
, "\032")] = '\0';
733 list
->line(LIST_READ
, buffer
);
739 * Tokenize a line of text. This is a very simple process since we
740 * don't need to parse the value out of e.g. numeric tokens: we
741 * simply split one string into many.
743 static Token
*tokenize(char *line
)
746 enum pp_token_type type
;
748 Token
*t
, **tail
= &list
;
755 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
756 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
761 type
= TOK_PREPROC_ID
;
762 } else if (*p
== '{') {
764 while (*p
&& *p
!= '}') {
771 type
= TOK_PREPROC_ID
;
772 } else if (*p
== '?') {
773 type
= TOK_PREPROC_Q
; /* %? */
776 type
= TOK_PREPROC_QQ
; /* %?? */
779 } else if (isidchar(*p
) ||
780 ((*p
== '!' || *p
== '%' || *p
== '$') &&
785 while (isidchar(*p
));
786 type
= TOK_PREPROC_ID
;
792 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
795 while (*p
&& isidchar(*p
))
797 } else if (*p
== '\'' || *p
== '"') {
804 while (*p
&& *p
!= c
)
810 error(ERR_WARNING
, "unterminated string");
811 /* Handling unterminated strings by UNV */
814 } else if (isnumstart(*p
)) {
816 bool is_float
= false;
832 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
834 if (*p
== '+' || *p
== '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
840 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
842 } else if (c
== 'P' || c
== 'p') {
844 if (*p
== '+' || *p
== '-')
846 } else if (isnumchar(c
) || c
== '_')
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
861 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
862 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
863 (*r
== 'p' || *r
== 'P')) {
867 break; /* Terminate the token */
871 p
--; /* Point to first character beyond number */
873 if (has_e
&& !is_hex
) {
874 /* 1e13 is floating-point, but 1e13h is not */
878 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
879 } else if (isspace(*p
)) {
880 type
= TOK_WHITESPACE
;
882 while (*p
&& isspace(*p
))
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
889 if (!*p
|| *p
== ';') {
894 } else if (*p
== ';') {
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
903 * else is a single-character operator.
906 if ((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] == '|') ||
916 (p
[0] == '^' && p
[1] == '^')) {
922 /* Handling unterminated string by UNV */
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
930 if (type
!= TOK_COMMENT
) {
931 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
940 * this function allocates a new managed block of memory and
941 * returns a pointer to the block. The managed blocks are
942 * deleted only all at once by the delete_Blocks function.
944 static void *new_Block(size_t size
)
948 /* first, get to the end of the linked list */
951 /* now allocate the requested chunk */
952 b
->chunk
= nasm_malloc(size
);
954 /* now allocate a new block for the next request */
955 b
->next
= nasm_malloc(sizeof(Blocks
));
956 /* and initialize the contents of the new block */
957 b
->next
->next
= NULL
;
958 b
->next
->chunk
= NULL
;
963 * this function deletes all managed blocks of memory
965 static void delete_Blocks(void)
967 Blocks
*a
, *b
= &blocks
;
970 * keep in mind that the first block, pointed to by blocks
971 * is a static and not dynamically allocated, so we don't
985 * this function creates a new Token and passes a pointer to it
986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
989 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
994 if (freeTokens
== NULL
) {
995 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
996 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
997 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
998 freeTokens
[i
].next
= NULL
;
1001 freeTokens
= t
->next
;
1005 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1009 txtlen
= strlen(text
);
1010 t
->text
= nasm_malloc(1 + txtlen
);
1011 strncpy(t
->text
, text
, txtlen
);
1012 t
->text
[txtlen
] = '\0';
1017 static Token
*delete_Token(Token
* t
)
1019 Token
*next
= t
->next
;
1021 t
->next
= freeTokens
;
1027 * Convert a line of tokens back into text.
1028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
1031 static char *detoken(Token
* tlist
, int expand_locals
)
1039 for (t
= tlist
; t
; t
= t
->next
) {
1040 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1041 char *p
= getenv(t
->text
+ 2);
1044 t
->text
= nasm_strdup(p
);
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals
&&
1050 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1051 t
->text
[0] == '%' && t
->text
[1] == '$') {
1052 Context
*ctx
= get_ctx(t
->text
, false);
1055 char *p
, *q
= t
->text
+ 2;
1057 q
+= strspn(q
, "$");
1058 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1059 p
= nasm_strcat(buffer
, q
);
1064 if (t
->type
== TOK_WHITESPACE
) {
1066 } else if (t
->text
) {
1067 len
+= strlen(t
->text
);
1070 p
= line
= nasm_malloc(len
+ 1);
1071 for (t
= tlist
; t
; t
= t
->next
) {
1072 if (t
->type
== TOK_WHITESPACE
) {
1074 } else if (t
->text
) {
1085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1090 * FIX: This really needs to be unified with stdscan.
1092 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1094 Token
**tlineptr
= private_data
;
1096 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1100 *tlineptr
= tline
? tline
->next
: NULL
;
1102 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1103 tline
->type
== TOK_COMMENT
));
1106 return tokval
->t_type
= TOKEN_EOS
;
1108 tokval
->t_charptr
= tline
->text
;
1110 if (tline
->text
[0] == '$' && !tline
->text
[1])
1111 return tokval
->t_type
= TOKEN_HERE
;
1112 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1113 return tokval
->t_type
= TOKEN_BASE
;
1115 if (tline
->type
== TOK_ID
) {
1116 p
= tokval
->t_charptr
= tline
->text
;
1118 tokval
->t_charptr
++;
1119 return tokval
->t_type
= TOKEN_ID
;
1122 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1123 if (r
>= p
+MAX_KEYWORD
)
1124 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy
, tokval
);
1133 if (tline
->type
== TOK_NUMBER
) {
1135 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1137 return tokval
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
1138 tokval
->t_charptr
= tline
->text
;
1139 return tokval
->t_type
= TOKEN_NUM
;
1142 if (tline
->type
== TOK_FLOAT
) {
1143 return tokval
->t_type
= TOKEN_FLOAT
;
1146 if (tline
->type
== TOK_STRING
) {
1155 if (l
== 0 || r
[l
- 1] != q
)
1156 return tokval
->t_type
= TOKEN_ERRNUM
;
1157 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1159 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1160 tokval
->t_charptr
= NULL
;
1161 return tokval
->t_type
= TOKEN_NUM
;
1164 if (tline
->type
== TOK_OTHER
) {
1165 if (!strcmp(tline
->text
, "<<"))
1166 return tokval
->t_type
= TOKEN_SHL
;
1167 if (!strcmp(tline
->text
, ">>"))
1168 return tokval
->t_type
= TOKEN_SHR
;
1169 if (!strcmp(tline
->text
, "//"))
1170 return tokval
->t_type
= TOKEN_SDIV
;
1171 if (!strcmp(tline
->text
, "%%"))
1172 return tokval
->t_type
= TOKEN_SMOD
;
1173 if (!strcmp(tline
->text
, "=="))
1174 return tokval
->t_type
= TOKEN_EQ
;
1175 if (!strcmp(tline
->text
, "<>"))
1176 return tokval
->t_type
= TOKEN_NE
;
1177 if (!strcmp(tline
->text
, "!="))
1178 return tokval
->t_type
= TOKEN_NE
;
1179 if (!strcmp(tline
->text
, "<="))
1180 return tokval
->t_type
= TOKEN_LE
;
1181 if (!strcmp(tline
->text
, ">="))
1182 return tokval
->t_type
= TOKEN_GE
;
1183 if (!strcmp(tline
->text
, "&&"))
1184 return tokval
->t_type
= TOKEN_DBL_AND
;
1185 if (!strcmp(tline
->text
, "^^"))
1186 return tokval
->t_type
= TOKEN_DBL_XOR
;
1187 if (!strcmp(tline
->text
, "||"))
1188 return tokval
->t_type
= TOKEN_DBL_OR
;
1192 * We have no other options: just return the first character of
1195 return tokval
->t_type
= tline
->text
[0];
1199 * Compare a string to the name of an existing macro; this is a
1200 * simple wrapper which calls either strcmp or nasm_stricmp
1201 * depending on the value of the `casesense' parameter.
1203 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1205 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1209 * Return the Context structure associated with a %$ token. Return
1210 * NULL, having _already_ reported an error condition, if the
1211 * context stack isn't deep enough for the supplied number of $
1213 * If all_contexts == true, contexts that enclose current are
1214 * also scanned for such smacro, until it is found; if not -
1215 * only the context that directly results from the number of $'s
1216 * in variable's name.
1218 static Context
*get_ctx(char *name
, bool all_contexts
)
1224 if (!name
|| name
[0] != '%' || name
[1] != '$')
1228 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1232 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1234 /* i--; Lino - 02/25/02 */
1237 error(ERR_NONFATAL
, "`%s': context stack is only"
1238 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1245 /* Search for this smacro in found context */
1246 m
= hash_findix(&ctx
->localmac
, name
);
1248 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1259 * Open an include file. This routine must always return a valid
1260 * file pointer if it returns - it's responsible for throwing an
1261 * ERR_FATAL and bombing out completely if not. It should also try
1262 * the include path one by one until it finds the file or reaches
1263 * the end of the path.
1265 static FILE *inc_fopen(char *file
)
1268 char *prefix
= "", *combine
;
1269 IncPath
*ip
= ipath
;
1270 static int namelen
= 0;
1271 int len
= strlen(file
);
1274 combine
= nasm_malloc(strlen(prefix
) + len
+ 1);
1275 strcpy(combine
, prefix
);
1276 strcat(combine
, file
);
1277 fp
= fopen(combine
, "r");
1278 if (pass
== 0 && fp
) {
1279 namelen
+= strlen(combine
) + 1;
1284 printf(" %s", combine
);
1295 /* -MG given and file not found */
1297 namelen
+= strlen(file
) + 1;
1302 printf(" %s", file
);
1308 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1309 return NULL
; /* never reached - placate compilers */
1313 * Determine if we should warn on defining a single-line macro of
1314 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1315 * return true if _any_ single-line macro of that name is defined.
1316 * Otherwise, will return true if a single-line macro with either
1317 * `nparam' or no parameters is defined.
1319 * If a macro with precisely the right number of parameters is
1320 * defined, or nparam is -1, the address of the definition structure
1321 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1322 * is NULL, no action will be taken regarding its contents, and no
1325 * Note that this is also called with nparam zero to resolve
1328 * If you already know which context macro belongs to, you can pass
1329 * the context pointer as first parameter; if you won't but name begins
1330 * with %$ the context will be automatically computed. If all_contexts
1331 * is true, macro will be searched in outer contexts as well.
1334 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1337 struct hash_table
*smtbl
;
1341 smtbl
= &ctx
->localmac
;
1342 } else if (name
[0] == '%' && name
[1] == '$') {
1344 ctx
= get_ctx(name
, false);
1346 return false; /* got to return _something_ */
1347 smtbl
= &ctx
->localmac
;
1351 m
= (SMacro
*) hash_findix(smtbl
, name
);
1354 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1355 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1357 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1371 * Count and mark off the parameters in a multi-line macro call.
1372 * This is called both from within the multi-line macro expansion
1373 * code, and also to mark off the default parameters when provided
1374 * in a %macro definition line.
1376 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1378 int paramsize
, brace
;
1380 *nparam
= paramsize
= 0;
1383 if (*nparam
>= paramsize
) {
1384 paramsize
+= PARAM_DELTA
;
1385 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1389 if (tok_is_(t
, "{"))
1391 (*params
)[(*nparam
)++] = t
;
1392 while (tok_isnt_(t
, brace
? "}" : ","))
1394 if (t
) { /* got a comma/brace */
1398 * Now we've found the closing brace, look further
1402 if (tok_isnt_(t
, ",")) {
1404 "braces do not enclose all of macro parameter");
1405 while (tok_isnt_(t
, ","))
1409 t
= t
->next
; /* eat the comma */
1416 * Determine whether one of the various `if' conditions is true or
1419 * We must free the tline we get passed.
1421 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1423 enum pp_conditional i
= PP_COND(ct
);
1425 Token
*t
, *tt
, **tptr
, *origline
;
1426 struct tokenval tokval
;
1428 enum pp_token_type needtype
;
1434 j
= false; /* have we matched yet? */
1435 while (cstk
&& tline
) {
1437 if (!tline
|| tline
->type
!= TOK_ID
) {
1439 "`%s' expects context identifiers", pp_directives
[ct
]);
1440 free_tlist(origline
);
1443 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1445 tline
= tline
->next
;
1450 j
= false; /* have we matched yet? */
1453 if (!tline
|| (tline
->type
!= TOK_ID
&&
1454 (tline
->type
!= TOK_PREPROC_ID
||
1455 tline
->text
[1] != '$'))) {
1457 "`%s' expects macro identifiers", pp_directives
[ct
]);
1460 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1462 tline
= tline
->next
;
1468 tline
= expand_smacro(tline
);
1470 while (tok_isnt_(tt
, ","))
1474 "`%s' expects two comma-separated arguments",
1479 j
= true; /* assume equality unless proved not */
1480 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1481 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1482 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1486 if (t
->type
== TOK_WHITESPACE
) {
1490 if (tt
->type
== TOK_WHITESPACE
) {
1494 if (tt
->type
!= t
->type
) {
1495 j
= false; /* found mismatching tokens */
1498 /* Unify surrounding quotes for strings */
1499 if (t
->type
== TOK_STRING
) {
1500 tt
->text
[0] = t
->text
[0];
1501 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1503 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1504 j
= false; /* found mismatching tokens */
1511 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1512 j
= false; /* trailing gunk on one end or other */
1518 MMacro searching
, *mmac
;
1520 tline
= tline
->next
;
1522 tline
= expand_id(tline
);
1523 if (!tok_type_(tline
, TOK_ID
)) {
1525 "`%s' expects a macro name", pp_directives
[ct
]);
1528 searching
.name
= nasm_strdup(tline
->text
);
1529 searching
.casesense
= true;
1530 searching
.plus
= false;
1531 searching
.nolist
= false;
1532 searching
.in_progress
= 0;
1533 searching
.rep_nest
= NULL
;
1534 searching
.nparam_min
= 0;
1535 searching
.nparam_max
= INT_MAX
;
1536 tline
= expand_smacro(tline
->next
);
1539 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1541 "`%s' expects a parameter count or nothing",
1544 searching
.nparam_min
= searching
.nparam_max
=
1545 readnum(tline
->text
, &j
);
1548 "unable to parse parameter count `%s'",
1551 if (tline
&& tok_is_(tline
->next
, "-")) {
1552 tline
= tline
->next
->next
;
1553 if (tok_is_(tline
, "*"))
1554 searching
.nparam_max
= INT_MAX
;
1555 else if (!tok_type_(tline
, TOK_NUMBER
))
1557 "`%s' expects a parameter count after `-'",
1560 searching
.nparam_max
= readnum(tline
->text
, &j
);
1563 "unable to parse parameter count `%s'",
1565 if (searching
.nparam_min
> searching
.nparam_max
)
1567 "minimum parameter count exceeds maximum");
1570 if (tline
&& tok_is_(tline
->next
, "+")) {
1571 tline
= tline
->next
;
1572 searching
.plus
= true;
1574 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1576 if (!strcmp(mmac
->name
, searching
.name
) &&
1577 (mmac
->nparam_min
<= searching
.nparam_max
1579 && (searching
.nparam_min
<= mmac
->nparam_max
1586 nasm_free(searching
.name
);
1595 needtype
= TOK_NUMBER
;
1598 needtype
= TOK_STRING
;
1602 t
= tline
= expand_smacro(tline
);
1604 while (tok_type_(t
, TOK_WHITESPACE
) ||
1605 (needtype
== TOK_NUMBER
&&
1606 tok_type_(t
, TOK_OTHER
) &&
1607 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1611 j
= tok_type_(t
, needtype
);
1615 t
= tline
= expand_smacro(tline
);
1616 while (tok_type_(t
, TOK_WHITESPACE
))
1621 t
= t
->next
; /* Skip the actual token */
1622 while (tok_type_(t
, TOK_WHITESPACE
))
1624 j
= !t
; /* Should be nothing left */
1629 t
= tline
= expand_smacro(tline
);
1630 while (tok_type_(t
, TOK_WHITESPACE
))
1633 j
= !t
; /* Should be empty */
1637 t
= tline
= expand_smacro(tline
);
1639 tokval
.t_type
= TOKEN_INVALID
;
1640 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1641 NULL
, pass
| CRITICAL
, error
, NULL
);
1646 "trailing garbage after expression ignored");
1647 if (!is_simple(evalresult
)) {
1649 "non-constant value given to `%s'", pp_directives
[ct
]);
1652 j
= reloc_value(evalresult
) != 0;
1657 "preprocessor directive `%s' not yet implemented",
1662 free_tlist(origline
);
1663 return j
^ PP_NEGATIVE(ct
);
1666 free_tlist(origline
);
1671 * Expand macros in a string. Used in %error and %include directives.
1672 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1673 * The returned variable should ALWAYS be freed after usage.
1675 void expand_macros_in_string(char **p
)
1677 Token
*line
= tokenize(*p
);
1678 line
= expand_smacro(line
);
1679 *p
= detoken(line
, false);
1683 * Common code for defining an smacro
1685 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1686 int nparam
, Token
*expansion
)
1688 SMacro
*smac
, **smhead
;
1689 struct hash_table
*smtbl
;
1691 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1694 "single-line macro `%s' defined both with and"
1695 " without parameters", mname
);
1697 /* Some instances of the old code considered this a failure,
1698 some others didn't. What is the right thing to do here? */
1699 free_tlist(expansion
);
1700 return false; /* Failure */
1703 * We're redefining, so we have to take over an
1704 * existing SMacro structure. This means freeing
1705 * what was already in it.
1707 nasm_free(smac
->name
);
1708 free_tlist(smac
->expansion
);
1711 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1712 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1713 smac
= nasm_malloc(sizeof(SMacro
));
1714 smac
->next
= *smhead
;
1717 smac
->name
= nasm_strdup(mname
);
1718 smac
->casesense
= casesense
;
1719 smac
->nparam
= nparam
;
1720 smac
->expansion
= expansion
;
1721 smac
->in_progress
= false;
1722 return true; /* Success */
1726 * Undefine an smacro
1728 static void undef_smacro(Context
*ctx
, const char *mname
)
1730 SMacro
**smhead
, *s
, **sp
;
1731 struct hash_table
*smtbl
;
1733 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1734 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1738 * We now have a macro name... go hunt for it.
1741 while ((s
= *sp
) != NULL
) {
1742 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1745 free_tlist(s
->expansion
);
1755 * Decode a size directive
1757 static int parse_size(const char *str
) {
1758 static const char *size_names
[] =
1759 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1760 static const int sizes
[] =
1761 { 0, 1, 4, 16, 8, 10, 2, 32 };
1763 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1767 * find and process preprocessor directive in passed line
1768 * Find out if a line contains a preprocessor directive, and deal
1771 * If a directive _is_ found, it is the responsibility of this routine
1772 * (and not the caller) to free_tlist() the line.
1774 * @param tline a pointer to the current tokeninzed line linked list
1775 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1778 static int do_directive(Token
* tline
)
1780 enum preproc_token i
;
1792 MMacro
*mmac
, **mmhead
;
1793 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1795 struct tokenval tokval
;
1797 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1803 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1804 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1805 || tline
->text
[1] == '!'))
1806 return NO_DIRECTIVE_FOUND
;
1808 i
= pp_token_hash(tline
->text
);
1811 * If we're in a non-emitting branch of a condition construct,
1812 * or walking to the end of an already terminated %rep block,
1813 * we should ignore all directives except for condition
1816 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1817 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1818 return NO_DIRECTIVE_FOUND
;
1822 * If we're defining a macro or reading a %rep block, we should
1823 * ignore all directives except for %macro/%imacro (which
1824 * generate an error), %endm/%endmacro, and (only if we're in a
1825 * %rep block) %endrep. If we're in a %rep block, another %rep
1826 * causes an error, so should be let through.
1828 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1829 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1830 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1831 return NO_DIRECTIVE_FOUND
;
1836 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1838 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1841 /* Directive to tell NASM what the default stack size is. The
1842 * default is for a 16-bit stack, and this can be overriden with
1844 * the following form:
1846 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1848 tline
= tline
->next
;
1849 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1850 tline
= tline
->next
;
1851 if (!tline
|| tline
->type
!= TOK_ID
) {
1852 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1853 free_tlist(origline
);
1854 return DIRECTIVE_FOUND
;
1856 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1857 /* All subsequent ARG directives are for a 32-bit stack */
1859 StackPointer
= "ebp";
1862 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1863 /* All subsequent ARG directives are for a 64-bit stack */
1865 StackPointer
= "rbp";
1868 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1869 /* All subsequent ARG directives are for a 16-bit stack,
1870 * far function call.
1873 StackPointer
= "bp";
1876 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1877 /* All subsequent ARG directives are for a 16-bit stack,
1878 * far function call. We don't support near functions.
1881 StackPointer
= "bp";
1885 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1886 free_tlist(origline
);
1887 return DIRECTIVE_FOUND
;
1889 free_tlist(origline
);
1890 return DIRECTIVE_FOUND
;
1893 /* TASM like ARG directive to define arguments to functions, in
1894 * the following form:
1896 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1900 char *arg
, directive
[256];
1901 int size
= StackSize
;
1903 /* Find the argument name */
1904 tline
= tline
->next
;
1905 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1906 tline
= tline
->next
;
1907 if (!tline
|| tline
->type
!= TOK_ID
) {
1908 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1909 free_tlist(origline
);
1910 return DIRECTIVE_FOUND
;
1914 /* Find the argument size type */
1915 tline
= tline
->next
;
1916 if (!tline
|| tline
->type
!= TOK_OTHER
1917 || tline
->text
[0] != ':') {
1919 "Syntax error processing `%%arg' directive");
1920 free_tlist(origline
);
1921 return DIRECTIVE_FOUND
;
1923 tline
= tline
->next
;
1924 if (!tline
|| tline
->type
!= TOK_ID
) {
1925 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1926 free_tlist(origline
);
1927 return DIRECTIVE_FOUND
;
1930 /* Allow macro expansion of type parameter */
1931 tt
= tokenize(tline
->text
);
1932 tt
= expand_smacro(tt
);
1933 size
= parse_size(tt
->text
);
1936 "Invalid size type for `%%arg' missing directive");
1938 free_tlist(origline
);
1939 return DIRECTIVE_FOUND
;
1943 /* Round up to even stack slots */
1944 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1946 /* Now define the macro for the argument */
1947 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1948 arg
, StackPointer
, offset
);
1949 do_directive(tokenize(directive
));
1952 /* Move to the next argument in the list */
1953 tline
= tline
->next
;
1954 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1955 tline
= tline
->next
;
1956 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1958 free_tlist(origline
);
1959 return DIRECTIVE_FOUND
;
1962 /* TASM like LOCAL directive to define local variables for a
1963 * function, in the following form:
1965 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1967 * The '= LocalSize' at the end is ignored by NASM, but is
1968 * required by TASM to define the local parameter size (and used
1969 * by the TASM macro package).
1971 offset
= LocalOffset
;
1973 char *local
, directive
[256];
1974 int size
= StackSize
;
1976 /* Find the argument name */
1977 tline
= tline
->next
;
1978 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1979 tline
= tline
->next
;
1980 if (!tline
|| tline
->type
!= TOK_ID
) {
1982 "`%%local' missing argument parameter");
1983 free_tlist(origline
);
1984 return DIRECTIVE_FOUND
;
1986 local
= tline
->text
;
1988 /* Find the argument size type */
1989 tline
= tline
->next
;
1990 if (!tline
|| tline
->type
!= TOK_OTHER
1991 || tline
->text
[0] != ':') {
1993 "Syntax error processing `%%local' directive");
1994 free_tlist(origline
);
1995 return DIRECTIVE_FOUND
;
1997 tline
= tline
->next
;
1998 if (!tline
|| tline
->type
!= TOK_ID
) {
2000 "`%%local' missing size type parameter");
2001 free_tlist(origline
);
2002 return DIRECTIVE_FOUND
;
2005 /* Allow macro expansion of type parameter */
2006 tt
= tokenize(tline
->text
);
2007 tt
= expand_smacro(tt
);
2008 size
= parse_size(tt
->text
);
2011 "Invalid size type for `%%local' missing directive");
2013 free_tlist(origline
);
2014 return DIRECTIVE_FOUND
;
2018 /* Round up to even stack slots */
2019 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2021 offset
+= size
; /* Negative offset, increment before */
2023 /* Now define the macro for the argument */
2024 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2025 local
, StackPointer
, offset
);
2026 do_directive(tokenize(directive
));
2028 /* Now define the assign to setup the enter_c macro correctly */
2029 snprintf(directive
, sizeof(directive
),
2030 "%%assign %%$localsize %%$localsize+%d", size
);
2031 do_directive(tokenize(directive
));
2033 /* Move to the next argument in the list */
2034 tline
= tline
->next
;
2035 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2036 tline
= tline
->next
;
2037 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2038 LocalOffset
= offset
;
2039 free_tlist(origline
);
2040 return DIRECTIVE_FOUND
;
2044 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2047 free_tlist(origline
);
2048 return DIRECTIVE_FOUND
;
2051 tline
= tline
->next
;
2053 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2054 tline
->type
!= TOK_INTERNAL_STRING
)) {
2055 error(ERR_NONFATAL
, "`%%include' expects a file name");
2056 free_tlist(origline
);
2057 return DIRECTIVE_FOUND
; /* but we did _something_ */
2061 "trailing garbage after `%%include' ignored");
2062 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2063 p
= tline
->text
+ 1; /* point past the quote to the name */
2064 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2066 p
= tline
->text
; /* internal_string is easier */
2067 expand_macros_in_string(&p
);
2068 inc
= nasm_malloc(sizeof(Include
));
2071 inc
->fp
= inc_fopen(p
);
2072 if (!inc
->fp
&& pass
== 0) {
2073 /* -MG given but file not found */
2076 inc
->fname
= src_set_fname(p
);
2077 inc
->lineno
= src_set_linnum(0);
2079 inc
->expansion
= NULL
;
2082 list
->uplevel(LIST_INCLUDE
);
2084 free_tlist(origline
);
2085 return DIRECTIVE_FOUND
;
2088 tline
= tline
->next
;
2090 tline
= expand_id(tline
);
2091 if (!tok_type_(tline
, TOK_ID
)) {
2092 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2093 free_tlist(origline
);
2094 return DIRECTIVE_FOUND
; /* but we did _something_ */
2097 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2098 ctx
= nasm_malloc(sizeof(Context
));
2100 hash_init(&ctx
->localmac
, HASH_SMALL
);
2101 ctx
->name
= nasm_strdup(tline
->text
);
2102 ctx
->number
= unique
++;
2104 free_tlist(origline
);
2108 tline
= tline
->next
;
2110 tline
= expand_id(tline
);
2111 if (!tok_type_(tline
, TOK_ID
)) {
2112 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2113 free_tlist(origline
);
2114 return DIRECTIVE_FOUND
; /* but we did _something_ */
2117 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2119 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2121 nasm_free(cstk
->name
);
2122 cstk
->name
= nasm_strdup(tline
->text
);
2124 free_tlist(origline
);
2129 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2131 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2134 free_tlist(origline
);
2138 tline
->next
= expand_smacro(tline
->next
);
2139 tline
= tline
->next
;
2141 if (tok_type_(tline
, TOK_STRING
)) {
2142 p
= tline
->text
+ 1; /* point past the quote to the name */
2143 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2144 expand_macros_in_string(&p
);
2145 error(ERR_NONFATAL
, "%s", p
);
2148 p
= detoken(tline
, false);
2149 error(ERR_WARNING
, "%s", p
);
2152 free_tlist(origline
);
2156 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2159 j
= if_condition(tline
->next
, i
);
2160 tline
->next
= NULL
; /* it got freed */
2161 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2163 cond
= nasm_malloc(sizeof(Cond
));
2164 cond
->next
= istk
->conds
;
2167 free_tlist(origline
);
2168 return DIRECTIVE_FOUND
;
2172 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2173 if (emitting(istk
->conds
->state
)
2174 || istk
->conds
->state
== COND_NEVER
)
2175 istk
->conds
->state
= COND_NEVER
;
2178 * IMPORTANT: In the case of %if, we will already have
2179 * called expand_mmac_params(); however, if we're
2180 * processing an %elif we must have been in a
2181 * non-emitting mode, which would have inhibited
2182 * the normal invocation of expand_mmac_params(). Therefore,
2183 * we have to do it explicitly here.
2185 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2186 tline
->next
= NULL
; /* it got freed */
2187 istk
->conds
->state
=
2188 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2190 free_tlist(origline
);
2191 return DIRECTIVE_FOUND
;
2195 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2197 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2198 if (emitting(istk
->conds
->state
)
2199 || istk
->conds
->state
== COND_NEVER
)
2200 istk
->conds
->state
= COND_ELSE_FALSE
;
2202 istk
->conds
->state
= COND_ELSE_TRUE
;
2203 free_tlist(origline
);
2204 return DIRECTIVE_FOUND
;
2208 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2210 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2212 istk
->conds
= cond
->next
;
2214 free_tlist(origline
);
2215 return DIRECTIVE_FOUND
;
2221 "`%%%smacro': already defining a macro",
2222 (i
== PP_IMACRO
? "i" : ""));
2223 tline
= tline
->next
;
2225 tline
= expand_id(tline
);
2226 if (!tok_type_(tline
, TOK_ID
)) {
2228 "`%%%smacro' expects a macro name",
2229 (i
== PP_IMACRO
? "i" : ""));
2230 return DIRECTIVE_FOUND
;
2232 defining
= nasm_malloc(sizeof(MMacro
));
2233 defining
->name
= nasm_strdup(tline
->text
);
2234 defining
->casesense
= (i
== PP_MACRO
);
2235 defining
->plus
= false;
2236 defining
->nolist
= false;
2237 defining
->in_progress
= 0;
2238 defining
->rep_nest
= NULL
;
2239 tline
= expand_smacro(tline
->next
);
2241 if (!tok_type_(tline
, TOK_NUMBER
)) {
2243 "`%%%smacro' expects a parameter count",
2244 (i
== PP_IMACRO
? "i" : ""));
2245 defining
->nparam_min
= defining
->nparam_max
= 0;
2247 defining
->nparam_min
= defining
->nparam_max
=
2248 readnum(tline
->text
, &err
);
2251 "unable to parse parameter count `%s'", tline
->text
);
2253 if (tline
&& tok_is_(tline
->next
, "-")) {
2254 tline
= tline
->next
->next
;
2255 if (tok_is_(tline
, "*"))
2256 defining
->nparam_max
= INT_MAX
;
2257 else if (!tok_type_(tline
, TOK_NUMBER
))
2259 "`%%%smacro' expects a parameter count after `-'",
2260 (i
== PP_IMACRO
? "i" : ""));
2262 defining
->nparam_max
= readnum(tline
->text
, &err
);
2265 "unable to parse parameter count `%s'",
2267 if (defining
->nparam_min
> defining
->nparam_max
)
2269 "minimum parameter count exceeds maximum");
2272 if (tline
&& tok_is_(tline
->next
, "+")) {
2273 tline
= tline
->next
;
2274 defining
->plus
= true;
2276 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2277 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2278 tline
= tline
->next
;
2279 defining
->nolist
= true;
2281 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2283 if (!strcmp(mmac
->name
, defining
->name
) &&
2284 (mmac
->nparam_min
<= defining
->nparam_max
2286 && (defining
->nparam_min
<= mmac
->nparam_max
2289 "redefining multi-line macro `%s'", defining
->name
);
2295 * Handle default parameters.
2297 if (tline
&& tline
->next
) {
2298 defining
->dlist
= tline
->next
;
2300 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2301 &defining
->defaults
);
2303 defining
->dlist
= NULL
;
2304 defining
->defaults
= NULL
;
2306 defining
->expansion
= NULL
;
2307 free_tlist(origline
);
2308 return DIRECTIVE_FOUND
;
2313 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2314 return DIRECTIVE_FOUND
;
2316 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2317 defining
->next
= *mmhead
;
2320 free_tlist(origline
);
2321 return DIRECTIVE_FOUND
;
2324 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2325 tline
= tline
->next
;
2326 if (tline
->next
== NULL
) {
2327 free_tlist(origline
);
2328 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2329 return DIRECTIVE_FOUND
;
2331 t
= expand_smacro(tline
->next
);
2333 free_tlist(origline
);
2336 tokval
.t_type
= TOKEN_INVALID
;
2338 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2341 return DIRECTIVE_FOUND
;
2344 "trailing garbage after expression ignored");
2345 if (!is_simple(evalresult
)) {
2346 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2347 return DIRECTIVE_FOUND
;
2350 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2351 mmac
= mmac
->next_active
;
2353 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2354 } else if (mmac
->nparam
== 0) {
2356 "`%%rotate' invoked within macro without parameters");
2358 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2360 rotate
%= (int)mmac
->nparam
;
2362 rotate
+= mmac
->nparam
;
2364 mmac
->rotate
= rotate
;
2366 return DIRECTIVE_FOUND
;
2371 tline
= tline
->next
;
2372 } while (tok_type_(tline
, TOK_WHITESPACE
));
2374 if (tok_type_(tline
, TOK_ID
) &&
2375 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2378 tline
= tline
->next
;
2379 } while (tok_type_(tline
, TOK_WHITESPACE
));
2383 t
= expand_smacro(tline
);
2385 tokval
.t_type
= TOKEN_INVALID
;
2387 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2389 free_tlist(origline
);
2390 return DIRECTIVE_FOUND
;
2394 "trailing garbage after expression ignored");
2395 if (!is_simple(evalresult
)) {
2396 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2397 return DIRECTIVE_FOUND
;
2399 count
= reloc_value(evalresult
) + 1;
2401 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2404 free_tlist(origline
);
2406 tmp_defining
= defining
;
2407 defining
= nasm_malloc(sizeof(MMacro
));
2408 defining
->name
= NULL
; /* flags this macro as a %rep block */
2409 defining
->casesense
= false;
2410 defining
->plus
= false;
2411 defining
->nolist
= nolist
;
2412 defining
->in_progress
= count
;
2413 defining
->nparam_min
= defining
->nparam_max
= 0;
2414 defining
->defaults
= NULL
;
2415 defining
->dlist
= NULL
;
2416 defining
->expansion
= NULL
;
2417 defining
->next_active
= istk
->mstk
;
2418 defining
->rep_nest
= tmp_defining
;
2419 return DIRECTIVE_FOUND
;
2422 if (!defining
|| defining
->name
) {
2423 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2424 return DIRECTIVE_FOUND
;
2428 * Now we have a "macro" defined - although it has no name
2429 * and we won't be entering it in the hash tables - we must
2430 * push a macro-end marker for it on to istk->expansion.
2431 * After that, it will take care of propagating itself (a
2432 * macro-end marker line for a macro which is really a %rep
2433 * block will cause the macro to be re-expanded, complete
2434 * with another macro-end marker to ensure the process
2435 * continues) until the whole expansion is forcibly removed
2436 * from istk->expansion by a %exitrep.
2438 l
= nasm_malloc(sizeof(Line
));
2439 l
->next
= istk
->expansion
;
2440 l
->finishes
= defining
;
2442 istk
->expansion
= l
;
2444 istk
->mstk
= defining
;
2446 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2447 tmp_defining
= defining
;
2448 defining
= defining
->rep_nest
;
2449 free_tlist(origline
);
2450 return DIRECTIVE_FOUND
;
2454 * We must search along istk->expansion until we hit a
2455 * macro-end marker for a macro with no name. Then we set
2456 * its `in_progress' flag to 0.
2458 for (l
= istk
->expansion
; l
; l
= l
->next
)
2459 if (l
->finishes
&& !l
->finishes
->name
)
2463 l
->finishes
->in_progress
= 0;
2465 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2466 free_tlist(origline
);
2467 return DIRECTIVE_FOUND
;
2473 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2475 tline
= tline
->next
;
2477 tline
= expand_id(tline
);
2478 if (!tline
|| (tline
->type
!= TOK_ID
&&
2479 (tline
->type
!= TOK_PREPROC_ID
||
2480 tline
->text
[1] != '$'))) {
2481 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2483 free_tlist(origline
);
2484 return DIRECTIVE_FOUND
;
2487 ctx
= get_ctx(tline
->text
, false);
2489 mname
= tline
->text
;
2491 param_start
= tline
= tline
->next
;
2494 /* Expand the macro definition now for %xdefine and %ixdefine */
2495 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2496 tline
= expand_smacro(tline
);
2498 if (tok_is_(tline
, "(")) {
2500 * This macro has parameters.
2503 tline
= tline
->next
;
2507 error(ERR_NONFATAL
, "parameter identifier expected");
2508 free_tlist(origline
);
2509 return DIRECTIVE_FOUND
;
2511 if (tline
->type
!= TOK_ID
) {
2513 "`%s': parameter identifier expected",
2515 free_tlist(origline
);
2516 return DIRECTIVE_FOUND
;
2518 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2519 tline
= tline
->next
;
2521 if (tok_is_(tline
, ",")) {
2522 tline
= tline
->next
;
2525 if (!tok_is_(tline
, ")")) {
2527 "`)' expected to terminate macro template");
2528 free_tlist(origline
);
2529 return DIRECTIVE_FOUND
;
2534 tline
= tline
->next
;
2536 if (tok_type_(tline
, TOK_WHITESPACE
))
2537 last
= tline
, tline
= tline
->next
;
2542 if (t
->type
== TOK_ID
) {
2543 for (tt
= param_start
; tt
; tt
= tt
->next
)
2544 if (tt
->type
>= TOK_SMAC_PARAM
&&
2545 !strcmp(tt
->text
, t
->text
))
2549 t
->next
= macro_start
;
2554 * Good. We now have a macro name, a parameter count, and a
2555 * token list (in reverse order) for an expansion. We ought
2556 * to be OK just to create an SMacro, store it, and let
2557 * free_tlist have the rest of the line (which we have
2558 * carefully re-terminated after chopping off the expansion
2561 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2562 free_tlist(origline
);
2563 return DIRECTIVE_FOUND
;
2566 tline
= tline
->next
;
2568 tline
= expand_id(tline
);
2569 if (!tline
|| (tline
->type
!= TOK_ID
&&
2570 (tline
->type
!= TOK_PREPROC_ID
||
2571 tline
->text
[1] != '$'))) {
2572 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2573 free_tlist(origline
);
2574 return DIRECTIVE_FOUND
;
2578 "trailing garbage after macro name ignored");
2581 /* Find the context that symbol belongs to */
2582 ctx
= get_ctx(tline
->text
, false);
2583 undef_smacro(ctx
, tline
->text
);
2584 free_tlist(origline
);
2585 return DIRECTIVE_FOUND
;
2590 tline
= tline
->next
;
2592 tline
= expand_id(tline
);
2593 if (!tline
|| (tline
->type
!= TOK_ID
&&
2594 (tline
->type
!= TOK_PREPROC_ID
||
2595 tline
->text
[1] != '$'))) {
2597 "`%%strlen' expects a macro identifier as first parameter");
2598 free_tlist(origline
);
2599 return DIRECTIVE_FOUND
;
2601 ctx
= get_ctx(tline
->text
, false);
2603 mname
= tline
->text
;
2605 tline
= expand_smacro(tline
->next
);
2609 while (tok_type_(t
, TOK_WHITESPACE
))
2611 /* t should now point to the string */
2612 if (t
->type
!= TOK_STRING
) {
2614 "`%%strlen` requires string as second parameter");
2616 free_tlist(origline
);
2617 return DIRECTIVE_FOUND
;
2620 macro_start
= nasm_malloc(sizeof(*macro_start
));
2621 macro_start
->next
= NULL
;
2622 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2623 macro_start
->mac
= NULL
;
2626 * We now have a macro name, an implicit parameter count of
2627 * zero, and a numeric token to use as an expansion. Create
2628 * and store an SMacro.
2630 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2632 free_tlist(origline
);
2633 return DIRECTIVE_FOUND
;
2638 tline
= tline
->next
;
2640 tline
= expand_id(tline
);
2641 if (!tline
|| (tline
->type
!= TOK_ID
&&
2642 (tline
->type
!= TOK_PREPROC_ID
||
2643 tline
->text
[1] != '$'))) {
2645 "`%%substr' expects a macro identifier as first parameter");
2646 free_tlist(origline
);
2647 return DIRECTIVE_FOUND
;
2649 ctx
= get_ctx(tline
->text
, false);
2651 mname
= tline
->text
;
2653 tline
= expand_smacro(tline
->next
);
2657 while (tok_type_(t
, TOK_WHITESPACE
))
2660 /* t should now point to the string */
2661 if (t
->type
!= TOK_STRING
) {
2663 "`%%substr` requires string as second parameter");
2665 free_tlist(origline
);
2666 return DIRECTIVE_FOUND
;
2671 tokval
.t_type
= TOKEN_INVALID
;
2673 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2676 free_tlist(origline
);
2677 return DIRECTIVE_FOUND
;
2679 if (!is_simple(evalresult
)) {
2680 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2682 free_tlist(origline
);
2683 return DIRECTIVE_FOUND
;
2686 macro_start
= nasm_malloc(sizeof(*macro_start
));
2687 macro_start
->next
= NULL
;
2688 macro_start
->text
= nasm_strdup("'''");
2689 if (evalresult
->value
> 0
2690 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2691 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2693 macro_start
->text
[2] = '\0';
2695 macro_start
->type
= TOK_STRING
;
2696 macro_start
->mac
= NULL
;
2699 * We now have a macro name, an implicit parameter count of
2700 * zero, and a numeric token to use as an expansion. Create
2701 * and store an SMacro.
2703 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2705 free_tlist(origline
);
2706 return DIRECTIVE_FOUND
;
2710 casesense
= (i
== PP_ASSIGN
);
2712 tline
= tline
->next
;
2714 tline
= expand_id(tline
);
2715 if (!tline
|| (tline
->type
!= TOK_ID
&&
2716 (tline
->type
!= TOK_PREPROC_ID
||
2717 tline
->text
[1] != '$'))) {
2719 "`%%%sassign' expects a macro identifier",
2720 (i
== PP_IASSIGN
? "i" : ""));
2721 free_tlist(origline
);
2722 return DIRECTIVE_FOUND
;
2724 ctx
= get_ctx(tline
->text
, false);
2726 mname
= tline
->text
;
2728 tline
= expand_smacro(tline
->next
);
2733 tokval
.t_type
= TOKEN_INVALID
;
2735 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2738 free_tlist(origline
);
2739 return DIRECTIVE_FOUND
;
2744 "trailing garbage after expression ignored");
2746 if (!is_simple(evalresult
)) {
2748 "non-constant value given to `%%%sassign'",
2749 (i
== PP_IASSIGN
? "i" : ""));
2750 free_tlist(origline
);
2751 return DIRECTIVE_FOUND
;
2754 macro_start
= nasm_malloc(sizeof(*macro_start
));
2755 macro_start
->next
= NULL
;
2756 make_tok_num(macro_start
, reloc_value(evalresult
));
2757 macro_start
->mac
= NULL
;
2760 * We now have a macro name, an implicit parameter count of
2761 * zero, and a numeric token to use as an expansion. Create
2762 * and store an SMacro.
2764 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2765 free_tlist(origline
);
2766 return DIRECTIVE_FOUND
;
2770 * Syntax is `%line nnn[+mmm] [filename]'
2772 tline
= tline
->next
;
2774 if (!tok_type_(tline
, TOK_NUMBER
)) {
2775 error(ERR_NONFATAL
, "`%%line' expects line number");
2776 free_tlist(origline
);
2777 return DIRECTIVE_FOUND
;
2779 k
= readnum(tline
->text
, &err
);
2781 tline
= tline
->next
;
2782 if (tok_is_(tline
, "+")) {
2783 tline
= tline
->next
;
2784 if (!tok_type_(tline
, TOK_NUMBER
)) {
2785 error(ERR_NONFATAL
, "`%%line' expects line increment");
2786 free_tlist(origline
);
2787 return DIRECTIVE_FOUND
;
2789 m
= readnum(tline
->text
, &err
);
2790 tline
= tline
->next
;
2796 nasm_free(src_set_fname(detoken(tline
, false)));
2798 free_tlist(origline
);
2799 return DIRECTIVE_FOUND
;
2803 "preprocessor directive `%s' not yet implemented",
2807 return DIRECTIVE_FOUND
;
2811 * Ensure that a macro parameter contains a condition code and
2812 * nothing else. Return the condition code index if so, or -1
2815 static int find_cc(Token
* t
)
2821 return -1; /* Probably a %+ without a space */
2824 if (t
->type
!= TOK_ID
)
2828 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2832 j
= elements(conditions
);
2835 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2851 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2852 * %-n) and MMacro-local identifiers (%%foo).
2854 static Token
*expand_mmac_params(Token
* tline
)
2856 Token
*t
, *tt
, **tail
, *thead
;
2862 if (tline
->type
== TOK_PREPROC_ID
&&
2863 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2864 && tline
->text
[2]) || tline
->text
[1] == '%'
2865 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2867 int type
= 0, cc
; /* type = 0 to placate optimisers */
2874 tline
= tline
->next
;
2877 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2878 mac
= mac
->next_active
;
2880 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2882 switch (t
->text
[1]) {
2884 * We have to make a substitution of one of the
2885 * forms %1, %-1, %+1, %%foo, %0.
2889 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2890 text
= nasm_strdup(tmpbuf
);
2894 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2896 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
2899 n
= atoi(t
->text
+ 2) - 1;
2900 if (n
>= mac
->nparam
)
2903 if (mac
->nparam
> 1)
2904 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2905 tt
= mac
->params
[n
];
2910 "macro parameter %d is not a condition code",
2915 if (inverse_ccs
[cc
] == -1) {
2917 "condition code `%s' is not invertible",
2922 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2926 n
= atoi(t
->text
+ 2) - 1;
2927 if (n
>= mac
->nparam
)
2930 if (mac
->nparam
> 1)
2931 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2932 tt
= mac
->params
[n
];
2937 "macro parameter %d is not a condition code",
2942 text
= nasm_strdup(conditions
[cc
]);
2946 n
= atoi(t
->text
+ 1) - 1;
2947 if (n
>= mac
->nparam
)
2950 if (mac
->nparam
> 1)
2951 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2952 tt
= mac
->params
[n
];
2955 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2956 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2957 tail
= &(*tail
)->next
;
2961 text
= NULL
; /* we've done it here */
2977 tline
= tline
->next
;
2984 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2986 case TOK_WHITESPACE
:
2987 if (tt
->type
== TOK_WHITESPACE
) {
2988 t
->next
= delete_Token(tt
);
2992 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2993 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2996 t
->next
= delete_Token(tt
);
3000 if (tt
->type
== TOK_NUMBER
) {
3001 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3004 t
->next
= delete_Token(tt
);
3015 * Expand all single-line macro calls made in the given line.
3016 * Return the expanded version of the line. The original is deemed
3017 * to be destroyed in the process. (In reality we'll just move
3018 * Tokens from input to output a lot of the time, rather than
3019 * actually bothering to destroy and replicate.)
3021 #define DEADMAN_LIMIT (1 << 20)
3023 static Token
*expand_smacro(Token
* tline
)
3025 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3026 struct hash_table
*smtbl
;
3027 SMacro
*head
= NULL
, *m
;
3030 unsigned int nparam
, sparam
;
3031 int brackets
, rescan
;
3032 Token
*org_tline
= tline
;
3035 int deadman
= DEADMAN_LIMIT
;
3038 * Trick: we should avoid changing the start token pointer since it can
3039 * be contained in "next" field of other token. Because of this
3040 * we allocate a copy of first token and work with it; at the end of
3041 * routine we copy it back
3045 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3047 tline
->mac
= org_tline
->mac
;
3048 nasm_free(org_tline
->text
);
3049 org_tline
->text
= NULL
;
3056 while (tline
) { /* main token loop */
3058 error(ERR_NONFATAL
, "interminable macro recursion");
3062 if ((mname
= tline
->text
)) {
3063 /* if this token is a local macro, look in local context */
3064 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3065 ctx
= get_ctx(mname
, true);
3066 smtbl
= &ctx
->localmac
;
3072 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3075 * We've hit an identifier. As in is_mmacro below, we first
3076 * check whether the identifier is a single-line macro at
3077 * all, then think about checking for parameters if
3080 for (m
= head
; m
; m
= m
->next
)
3081 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3087 if (m
->nparam
== 0) {
3089 * Simple case: the macro is parameterless. Discard the
3090 * one token that the macro call took, and push the
3091 * expansion back on the to-do stack.
3093 if (!m
->expansion
) {
3094 if (!strcmp("__FILE__", m
->name
)) {
3096 src_get(&num
, &(tline
->text
));
3097 nasm_quote(&(tline
->text
));
3098 tline
->type
= TOK_STRING
;
3101 if (!strcmp("__LINE__", m
->name
)) {
3102 nasm_free(tline
->text
);
3103 make_tok_num(tline
, src_get_linnum());
3106 if (!strcmp("__BITS__", m
->name
)) {
3107 nasm_free(tline
->text
);
3108 make_tok_num(tline
, globalbits
);
3111 tline
= delete_Token(tline
);
3116 * Complicated case: at least one macro with this name
3117 * exists and takes parameters. We must find the
3118 * parameters in the call, count them, find the SMacro
3119 * that corresponds to that form of the macro call, and
3120 * substitute for the parameters when we expand. What a
3123 /*tline = tline->next;
3124 skip_white_(tline); */
3127 while (tok_type_(t
, TOK_SMAC_END
)) {
3128 t
->mac
->in_progress
= false;
3130 t
= tline
->next
= delete_Token(t
);
3133 } while (tok_type_(tline
, TOK_WHITESPACE
));
3134 if (!tok_is_(tline
, "(")) {
3136 * This macro wasn't called with parameters: ignore
3137 * the call. (Behaviour borrowed from gnu cpp.)
3146 sparam
= PARAM_DELTA
;
3147 params
= nasm_malloc(sparam
* sizeof(Token
*));
3148 params
[0] = tline
->next
;
3149 paramsize
= nasm_malloc(sparam
* sizeof(int));
3151 while (true) { /* parameter loop */
3153 * For some unusual expansions
3154 * which concatenates function call
3157 while (tok_type_(t
, TOK_SMAC_END
)) {
3158 t
->mac
->in_progress
= false;
3160 t
= tline
->next
= delete_Token(t
);
3166 "macro call expects terminating `)'");
3169 if (tline
->type
== TOK_WHITESPACE
3171 if (paramsize
[nparam
])
3174 params
[nparam
] = tline
->next
;
3175 continue; /* parameter loop */
3177 if (tline
->type
== TOK_OTHER
3178 && tline
->text
[1] == 0) {
3179 char ch
= tline
->text
[0];
3180 if (ch
== ',' && !paren
&& brackets
<= 0) {
3181 if (++nparam
>= sparam
) {
3182 sparam
+= PARAM_DELTA
;
3183 params
= nasm_realloc(params
,
3188 nasm_realloc(paramsize
,
3192 params
[nparam
] = tline
->next
;
3193 paramsize
[nparam
] = 0;
3195 continue; /* parameter loop */
3198 (brackets
> 0 || (brackets
== 0 &&
3199 !paramsize
[nparam
])))
3201 if (!(brackets
++)) {
3202 params
[nparam
] = tline
->next
;
3203 continue; /* parameter loop */
3206 if (ch
== '}' && brackets
> 0)
3207 if (--brackets
== 0) {
3209 continue; /* parameter loop */
3211 if (ch
== '(' && !brackets
)
3213 if (ch
== ')' && brackets
<= 0)
3219 error(ERR_NONFATAL
, "braces do not "
3220 "enclose all of macro parameter");
3222 paramsize
[nparam
] += white
+ 1;
3224 } /* parameter loop */
3226 while (m
&& (m
->nparam
!= nparam
||
3227 mstrcmp(m
->name
, mname
,
3231 error(ERR_WARNING
| ERR_WARN_MNP
,
3232 "macro `%s' exists, "
3233 "but not taking %d parameters",
3234 mstart
->text
, nparam
);
3237 if (m
&& m
->in_progress
)
3239 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3241 * Design question: should we handle !tline, which
3242 * indicates missing ')' here, or expand those
3243 * macros anyway, which requires the (t) test a few
3247 nasm_free(paramsize
);
3251 * Expand the macro: we are placed on the last token of the
3252 * call, so that we can easily split the call from the
3253 * following tokens. We also start by pushing an SMAC_END
3254 * token for the cycle removal.
3261 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3263 m
->in_progress
= true;
3265 for (t
= m
->expansion
; t
; t
= t
->next
) {
3266 if (t
->type
>= TOK_SMAC_PARAM
) {
3267 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3271 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3272 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3275 new_Token(tline
, ttt
->type
, ttt
->text
,
3281 } else if (t
->type
== TOK_PREPROC_Q
) {
3282 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3284 } else if (t
->type
== TOK_PREPROC_QQ
) {
3285 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3288 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3294 * Having done that, get rid of the macro call, and clean
3295 * up the parameters.
3298 nasm_free(paramsize
);
3300 continue; /* main token loop */
3305 if (tline
->type
== TOK_SMAC_END
) {
3306 tline
->mac
->in_progress
= false;
3307 tline
= delete_Token(tline
);
3310 tline
= tline
->next
;
3318 * Now scan the entire line and look for successive TOK_IDs that resulted
3319 * after expansion (they can't be produced by tokenize()). The successive
3320 * TOK_IDs should be concatenated.
3321 * Also we look for %+ tokens and concatenate the tokens before and after
3322 * them (without white spaces in between).
3327 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3331 if (t
->next
->type
== TOK_ID
||
3332 t
->next
->type
== TOK_PREPROC_ID
||
3333 t
->next
->type
== TOK_NUMBER
) {
3334 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3336 t
->next
= delete_Token(t
->next
);
3339 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3340 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3341 strcmp(t
->next
->next
->text
, "%+") == 0) {
3342 /* free the next whitespace, the %+ token and next whitespace */
3344 for (i
= 1; i
<= 3; i
++) {
3346 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3348 t
->next
= delete_Token(t
->next
);
3353 /* If we concatenaded something, re-scan the line for macros */
3361 *org_tline
= *thead
;
3362 /* since we just gave text to org_line, don't free it */
3364 delete_Token(thead
);
3366 /* the expression expanded to empty line;
3367 we can't return NULL for some reasons
3368 we just set the line to a single WHITESPACE token. */
3369 memset(org_tline
, 0, sizeof(*org_tline
));
3370 org_tline
->text
= NULL
;
3371 org_tline
->type
= TOK_WHITESPACE
;
3380 * Similar to expand_smacro but used exclusively with macro identifiers
3381 * right before they are fetched in. The reason is that there can be
3382 * identifiers consisting of several subparts. We consider that if there
3383 * are more than one element forming the name, user wants a expansion,
3384 * otherwise it will be left as-is. Example:
3388 * the identifier %$abc will be left as-is so that the handler for %define
3389 * will suck it and define the corresponding value. Other case:
3391 * %define _%$abc cde
3393 * In this case user wants name to be expanded *before* %define starts
3394 * working, so we'll expand %$abc into something (if it has a value;
3395 * otherwise it will be left as-is) then concatenate all successive
3398 static Token
*expand_id(Token
* tline
)
3400 Token
*cur
, *oldnext
= NULL
;
3402 if (!tline
|| !tline
->next
)
3407 (cur
->next
->type
== TOK_ID
||
3408 cur
->next
->type
== TOK_PREPROC_ID
3409 || cur
->next
->type
== TOK_NUMBER
))
3412 /* If identifier consists of just one token, don't expand */
3417 oldnext
= cur
->next
; /* Detach the tail past identifier */
3418 cur
->next
= NULL
; /* so that expand_smacro stops here */
3421 tline
= expand_smacro(tline
);
3424 /* expand_smacro possibly changhed tline; re-scan for EOL */
3426 while (cur
&& cur
->next
)
3429 cur
->next
= oldnext
;
3436 * Determine whether the given line constitutes a multi-line macro
3437 * call, and return the MMacro structure called if so. Doesn't have
3438 * to check for an initial label - that's taken care of in
3439 * expand_mmacro - but must check numbers of parameters. Guaranteed
3440 * to be called with tline->type == TOK_ID, so the putative macro
3441 * name is easy to find.
3443 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3449 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3452 * Efficiency: first we see if any macro exists with the given
3453 * name. If not, we can return NULL immediately. _Then_ we
3454 * count the parameters, and then we look further along the
3455 * list if necessary to find the proper MMacro.
3457 for (m
= head
; m
; m
= m
->next
)
3458 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3464 * OK, we have a potential macro. Count and demarcate the
3467 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3470 * So we know how many parameters we've got. Find the MMacro
3471 * structure that handles this number.
3474 if (m
->nparam_min
<= nparam
3475 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3477 * This one is right. Just check if cycle removal
3478 * prohibits us using it before we actually celebrate...
3480 if (m
->in_progress
) {
3483 "self-reference in multi-line macro `%s'", m
->name
);
3489 * It's right, and we can use it. Add its default
3490 * parameters to the end of our list if necessary.
3492 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3494 nasm_realloc(params
,
3495 ((m
->nparam_min
+ m
->ndefs
+
3496 1) * sizeof(*params
)));
3497 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3498 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3503 * If we've gone over the maximum parameter count (and
3504 * we're in Plus mode), ignore parameters beyond
3507 if (m
->plus
&& nparam
> m
->nparam_max
)
3508 nparam
= m
->nparam_max
;
3510 * Then terminate the parameter list, and leave.
3512 if (!params
) { /* need this special case */
3513 params
= nasm_malloc(sizeof(*params
));
3516 params
[nparam
] = NULL
;
3517 *params_array
= params
;
3521 * This one wasn't right: look for the next one with the
3524 for (m
= m
->next
; m
; m
= m
->next
)
3525 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3530 * After all that, we didn't find one with the right number of
3531 * parameters. Issue a warning, and fail to expand the macro.
3533 error(ERR_WARNING
| ERR_WARN_MNP
,
3534 "macro `%s' exists, but not taking %d parameters",
3535 tline
->text
, nparam
);
3541 * Expand the multi-line macro call made by the given line, if
3542 * there is one to be expanded. If there is, push the expansion on
3543 * istk->expansion and return 1. Otherwise return 0.
3545 static int expand_mmacro(Token
* tline
)
3547 Token
*startline
= tline
;
3548 Token
*label
= NULL
;
3549 int dont_prepend
= 0;
3550 Token
**params
, *t
, *mtok
, *tt
;
3553 int i
, nparam
, *paramlen
;
3557 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3558 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3561 m
= is_mmacro(t
, ¶ms
);
3565 * We have an id which isn't a macro call. We'll assume
3566 * it might be a label; we'll also check to see if a
3567 * colon follows it. Then, if there's another id after
3568 * that lot, we'll check it again for macro-hood.
3572 if (tok_type_(t
, TOK_WHITESPACE
))
3573 last
= t
, t
= t
->next
;
3574 if (tok_is_(t
, ":")) {
3576 last
= t
, t
= t
->next
;
3577 if (tok_type_(t
, TOK_WHITESPACE
))
3578 last
= t
, t
= t
->next
;
3580 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3587 * Fix up the parameters: this involves stripping leading and
3588 * trailing whitespace, then stripping braces if they are
3591 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3592 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3594 for (i
= 0; params
[i
]; i
++) {
3596 int comma
= (!m
->plus
|| i
< nparam
- 1);
3600 if (tok_is_(t
, "{"))
3601 t
= t
->next
, brace
= true, comma
= false;
3605 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3606 break; /* ... because we have hit a comma */
3607 if (comma
&& t
->type
== TOK_WHITESPACE
3608 && tok_is_(t
->next
, ","))
3609 break; /* ... or a space then a comma */
3610 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3611 break; /* ... or a brace */
3618 * OK, we have a MMacro structure together with a set of
3619 * parameters. We must now go through the expansion and push
3620 * copies of each Line on to istk->expansion. Substitution of
3621 * parameter tokens and macro-local tokens doesn't get done
3622 * until the single-line macro substitution process; this is
3623 * because delaying them allows us to change the semantics
3624 * later through %rotate.
3626 * First, push an end marker on to istk->expansion, mark this
3627 * macro as in progress, and set up its invocation-specific
3630 ll
= nasm_malloc(sizeof(Line
));
3631 ll
->next
= istk
->expansion
;
3634 istk
->expansion
= ll
;
3636 m
->in_progress
= true;
3641 m
->paramlen
= paramlen
;
3642 m
->unique
= unique
++;
3645 m
->next_active
= istk
->mstk
;
3648 for (l
= m
->expansion
; l
; l
= l
->next
) {
3651 ll
= nasm_malloc(sizeof(Line
));
3652 ll
->finishes
= NULL
;
3653 ll
->next
= istk
->expansion
;
3654 istk
->expansion
= ll
;
3657 for (t
= l
->first
; t
; t
= t
->next
) {
3661 tt
= *tail
= new_Token(NULL
, TOK_ID
, mtok
->text
, 0);
3663 case TOK_PREPROC_QQ
:
3664 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3666 case TOK_PREPROC_ID
:
3667 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3675 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3684 * If we had a label, push it on as the first line of
3685 * the macro expansion.
3688 if (dont_prepend
< 0)
3689 free_tlist(startline
);
3691 ll
= nasm_malloc(sizeof(Line
));
3692 ll
->finishes
= NULL
;
3693 ll
->next
= istk
->expansion
;
3694 istk
->expansion
= ll
;
3695 ll
->first
= startline
;
3696 if (!dont_prepend
) {
3698 label
= label
->next
;
3699 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3704 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3710 * Since preprocessor always operate only on the line that didn't
3711 * arrived yet, we should always use ERR_OFFBY1. Also since user
3712 * won't want to see same error twice (preprocessing is done once
3713 * per pass) we will want to show errors only during pass one.
3715 static void error(int severity
, const char *fmt
, ...)
3720 /* If we're in a dead branch of IF or something like it, ignore the error */
3721 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3725 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3728 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3729 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3730 istk
->mstk
->lineno
, buff
);
3732 _error(severity
| ERR_PASS1
, "%s", buff
);
3736 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3741 istk
= nasm_malloc(sizeof(Include
));
3744 istk
->expansion
= NULL
;
3746 istk
->fp
= fopen(file
, "r");
3748 src_set_fname(nasm_strdup(file
));
3752 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3757 if (tasm_compatible_mode
) {
3758 stdmacpos
= nasm_stdmac
;
3760 stdmacpos
= nasm_stdmac_after_tasm
;
3762 any_extrastdmac
= (extrastdmac
!= NULL
);
3768 static char *pp_getline(void)
3775 * Fetch a tokenized line, either from the macro-expansion
3776 * buffer or from the input file.
3779 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3780 Line
*l
= istk
->expansion
;
3781 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3785 * This is a macro-end marker for a macro with no
3786 * name, which means it's not really a macro at all
3787 * but a %rep block, and the `in_progress' field is
3788 * more than 1, meaning that we still need to
3789 * repeat. (1 means the natural last repetition; 0
3790 * means termination by %exitrep.) We have
3791 * therefore expanded up to the %endrep, and must
3792 * push the whole block on to the expansion buffer
3793 * again. We don't bother to remove the macro-end
3794 * marker: we'd only have to generate another one
3797 l
->finishes
->in_progress
--;
3798 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3799 Token
*t
, *tt
, **tail
;
3801 ll
= nasm_malloc(sizeof(Line
));
3802 ll
->next
= istk
->expansion
;
3803 ll
->finishes
= NULL
;
3807 for (t
= l
->first
; t
; t
= t
->next
) {
3808 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3810 new_Token(NULL
, t
->type
, t
->text
, 0);
3815 istk
->expansion
= ll
;
3819 * Check whether a `%rep' was started and not ended
3820 * within this macro expansion. This can happen and
3821 * should be detected. It's a fatal error because
3822 * I'm too confused to work out how to recover
3828 "defining with name in expansion");
3829 else if (istk
->mstk
->name
)
3831 "`%%rep' without `%%endrep' within"
3832 " expansion of macro `%s'",
3837 * FIXME: investigate the relationship at this point between
3838 * istk->mstk and l->finishes
3841 MMacro
*m
= istk
->mstk
;
3842 istk
->mstk
= m
->next_active
;
3845 * This was a real macro call, not a %rep, and
3846 * therefore the parameter information needs to
3849 nasm_free(m
->params
);
3850 free_tlist(m
->iline
);
3851 nasm_free(m
->paramlen
);
3852 l
->finishes
->in_progress
= false;
3856 istk
->expansion
= l
->next
;
3858 list
->downlevel(LIST_MACRO
);
3861 while (1) { /* until we get a line we can use */
3863 if (istk
->expansion
) { /* from a macro expansion */
3865 Line
*l
= istk
->expansion
;
3867 istk
->mstk
->lineno
++;
3869 istk
->expansion
= l
->next
;
3871 p
= detoken(tline
, false);
3872 list
->line(LIST_MACRO
, p
);
3877 if (line
) { /* from the current input file */
3878 line
= prepreproc(line
);
3879 tline
= tokenize(line
);
3884 * The current file has ended; work down the istk
3891 "expected `%%endif' before end of file");
3892 /* only set line and file name if there's a next node */
3894 src_set_linnum(i
->lineno
);
3895 nasm_free(src_set_fname(i
->fname
));
3898 list
->downlevel(LIST_INCLUDE
);
3906 * We must expand MMacro parameters and MMacro-local labels
3907 * _before_ we plunge into directive processing, to cope
3908 * with things like `%define something %1' such as STRUC
3909 * uses. Unless we're _defining_ a MMacro, in which case
3910 * those tokens should be left alone to go into the
3911 * definition; and unless we're in a non-emitting
3912 * condition, in which case we don't want to meddle with
3915 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3916 tline
= expand_mmac_params(tline
);
3919 * Check the line to see if it's a preprocessor directive.
3921 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3923 } else if (defining
) {
3925 * We're defining a multi-line macro. We emit nothing
3927 * shove the tokenized line on to the macro definition.
3929 Line
*l
= nasm_malloc(sizeof(Line
));
3930 l
->next
= defining
->expansion
;
3932 l
->finishes
= false;
3933 defining
->expansion
= l
;
3935 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3937 * We're in a non-emitting branch of a condition block.
3938 * Emit nothing at all, not even a blank line: when we
3939 * emerge from the condition we'll give a line-number
3940 * directive so we keep our place correctly.
3944 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3946 * We're in a %rep block which has been terminated, so
3947 * we're walking through to the %endrep without
3948 * emitting anything. Emit nothing at all, not even a
3949 * blank line: when we emerge from the %rep block we'll
3950 * give a line-number directive so we keep our place
3956 tline
= expand_smacro(tline
);
3957 if (!expand_mmacro(tline
)) {
3959 * De-tokenize the line again, and emit it.
3961 line
= detoken(tline
, true);
3965 continue; /* expand_mmacro calls free_tlist */
3973 static void pp_cleanup(int pass
)
3976 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3978 free_mmacro(defining
);
3987 nasm_free(i
->fname
);
3998 void pp_include_path(char *path
)
4002 i
= nasm_malloc(sizeof(IncPath
));
4003 i
->path
= path
? nasm_strdup(path
) : NULL
;
4006 if (ipath
!= NULL
) {
4008 while (j
->next
!= NULL
)
4019 * This function is used to "export" the include paths, e.g.
4020 * the paths specified in the '-I' command switch.
4021 * The need for such exporting is due to the 'incbin' directive,
4022 * which includes raw binary files (unlike '%include', which
4023 * includes text source files). It would be real nice to be
4024 * able to specify paths to search for incbin'ned files also.
4025 * So, this is a simple workaround.
4027 * The function use is simple:
4029 * The 1st call (with NULL argument) returns a pointer to the 1st path
4030 * (char** type) or NULL if none include paths available.
4032 * All subsequent calls take as argument the value returned by this
4033 * function last. The return value is either the next path
4034 * (char** type) or NULL if the end of the paths list is reached.
4036 * It is maybe not the best way to do things, but I didn't want
4037 * to export too much, just one or two functions and no types or
4038 * variables exported.
4040 * Can't say I like the current situation with e.g. this path list either,
4041 * it seems to be never deallocated after creation...
4043 char **pp_get_include_path_ptr(char **pPrevPath
)
4045 /* This macro returns offset of a member of a structure */
4046 #define GetMemberOffset(StructType,MemberName)\
4047 ((size_t)&((StructType*)0)->MemberName)
4050 if (pPrevPath
== NULL
) {
4052 return &ipath
->path
;
4056 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
4062 #undef GetMemberOffset
4065 void pp_pre_include(char *fname
)
4067 Token
*inc
, *space
, *name
;
4070 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4071 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4072 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4074 l
= nasm_malloc(sizeof(Line
));
4077 l
->finishes
= false;
4081 void pp_pre_define(char *definition
)
4087 equals
= strchr(definition
, '=');
4088 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4089 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4092 space
->next
= tokenize(definition
);
4096 l
= nasm_malloc(sizeof(Line
));
4099 l
->finishes
= false;
4103 void pp_pre_undefine(char *definition
)
4108 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4109 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4110 space
->next
= tokenize(definition
);
4112 l
= nasm_malloc(sizeof(Line
));
4115 l
->finishes
= false;
4120 * Added by Keith Kanios:
4122 * This function is used to assist with "runtime" preprocessor
4123 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4125 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4126 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4129 void pp_runtime(char *definition
)
4133 def
= tokenize(definition
);
4134 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4139 void pp_extra_stdmac(const char **macros
)
4141 extrastdmac
= macros
;
4144 static void make_tok_num(Token
* tok
, int64_t val
)
4147 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4148 tok
->text
= nasm_strdup(numbuf
);
4149 tok
->type
= TOK_NUMBER
;