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!!! */
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 smacros
= hash_init(HASH_LARGE
);
578 mmacros
= hash_init(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
,
1340 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1341 } else if (name
[0] == '%' && name
[1] == '$') {
1343 ctx
= get_ctx(name
, false);
1345 return false; /* got to return _something_ */
1346 m
= (SMacro
*) hash_findix(ctx
->localmac
, name
);
1348 m
= (SMacro
*) hash_findix(smacros
, name
);
1352 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1353 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1355 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1369 * Count and mark off the parameters in a multi-line macro call.
1370 * This is called both from within the multi-line macro expansion
1371 * code, and also to mark off the default parameters when provided
1372 * in a %macro definition line.
1374 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1376 int paramsize
, brace
;
1378 *nparam
= paramsize
= 0;
1381 if (*nparam
>= paramsize
) {
1382 paramsize
+= PARAM_DELTA
;
1383 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1387 if (tok_is_(t
, "{"))
1389 (*params
)[(*nparam
)++] = t
;
1390 while (tok_isnt_(t
, brace
? "}" : ","))
1392 if (t
) { /* got a comma/brace */
1396 * Now we've found the closing brace, look further
1400 if (tok_isnt_(t
, ",")) {
1402 "braces do not enclose all of macro parameter");
1403 while (tok_isnt_(t
, ","))
1407 t
= t
->next
; /* eat the comma */
1414 * Determine whether one of the various `if' conditions is true or
1417 * We must free the tline we get passed.
1419 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1421 enum pp_conditional i
= PP_COND(ct
);
1423 Token
*t
, *tt
, **tptr
, *origline
;
1424 struct tokenval tokval
;
1426 enum pp_token_type needtype
;
1432 j
= false; /* have we matched yet? */
1433 while (cstk
&& tline
) {
1435 if (!tline
|| tline
->type
!= TOK_ID
) {
1437 "`%s' expects context identifiers", pp_directives
[ct
]);
1438 free_tlist(origline
);
1441 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1443 tline
= tline
->next
;
1448 j
= false; /* have we matched yet? */
1451 if (!tline
|| (tline
->type
!= TOK_ID
&&
1452 (tline
->type
!= TOK_PREPROC_ID
||
1453 tline
->text
[1] != '$'))) {
1455 "`%s' expects macro identifiers", pp_directives
[ct
]);
1458 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1460 tline
= tline
->next
;
1466 tline
= expand_smacro(tline
);
1468 while (tok_isnt_(tt
, ","))
1472 "`%s' expects two comma-separated arguments",
1477 j
= true; /* assume equality unless proved not */
1478 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1479 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1480 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1484 if (t
->type
== TOK_WHITESPACE
) {
1488 if (tt
->type
== TOK_WHITESPACE
) {
1492 if (tt
->type
!= t
->type
) {
1493 j
= false; /* found mismatching tokens */
1496 /* Unify surrounding quotes for strings */
1497 if (t
->type
== TOK_STRING
) {
1498 tt
->text
[0] = t
->text
[0];
1499 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1501 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1502 j
= false; /* found mismatching tokens */
1509 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1510 j
= false; /* trailing gunk on one end or other */
1516 MMacro searching
, *mmac
;
1518 tline
= tline
->next
;
1520 tline
= expand_id(tline
);
1521 if (!tok_type_(tline
, TOK_ID
)) {
1523 "`%s' expects a macro name", pp_directives
[ct
]);
1526 searching
.name
= nasm_strdup(tline
->text
);
1527 searching
.casesense
= true;
1528 searching
.plus
= false;
1529 searching
.nolist
= false;
1530 searching
.in_progress
= 0;
1531 searching
.rep_nest
= NULL
;
1532 searching
.nparam_min
= 0;
1533 searching
.nparam_max
= INT_MAX
;
1534 tline
= expand_smacro(tline
->next
);
1537 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1539 "`%s' expects a parameter count or nothing",
1542 searching
.nparam_min
= searching
.nparam_max
=
1543 readnum(tline
->text
, &j
);
1546 "unable to parse parameter count `%s'",
1549 if (tline
&& tok_is_(tline
->next
, "-")) {
1550 tline
= tline
->next
->next
;
1551 if (tok_is_(tline
, "*"))
1552 searching
.nparam_max
= INT_MAX
;
1553 else if (!tok_type_(tline
, TOK_NUMBER
))
1555 "`%s' expects a parameter count after `-'",
1558 searching
.nparam_max
= readnum(tline
->text
, &j
);
1561 "unable to parse parameter count `%s'",
1563 if (searching
.nparam_min
> searching
.nparam_max
)
1565 "minimum parameter count exceeds maximum");
1568 if (tline
&& tok_is_(tline
->next
, "+")) {
1569 tline
= tline
->next
;
1570 searching
.plus
= true;
1572 mmac
= (MMacro
*) hash_findix(mmacros
, searching
.name
);
1574 if (!strcmp(mmac
->name
, searching
.name
) &&
1575 (mmac
->nparam_min
<= searching
.nparam_max
1577 && (searching
.nparam_min
<= mmac
->nparam_max
1584 nasm_free(searching
.name
);
1593 needtype
= TOK_NUMBER
;
1596 needtype
= TOK_STRING
;
1600 t
= tline
= expand_smacro(tline
);
1602 while (tok_type_(t
, TOK_WHITESPACE
) ||
1603 (needtype
== TOK_NUMBER
&&
1604 tok_type_(t
, TOK_OTHER
) &&
1605 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1609 j
= tok_type_(t
, needtype
);
1613 t
= tline
= expand_smacro(tline
);
1614 while (tok_type_(t
, TOK_WHITESPACE
))
1619 t
= t
->next
; /* Skip the actual token */
1620 while (tok_type_(t
, TOK_WHITESPACE
))
1622 j
= !t
; /* Should be nothing left */
1627 t
= tline
= expand_smacro(tline
);
1628 while (tok_type_(t
, TOK_WHITESPACE
))
1631 j
= !t
; /* Should be empty */
1635 t
= tline
= expand_smacro(tline
);
1637 tokval
.t_type
= TOKEN_INVALID
;
1638 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1639 NULL
, pass
| CRITICAL
, error
, NULL
);
1644 "trailing garbage after expression ignored");
1645 if (!is_simple(evalresult
)) {
1647 "non-constant value given to `%s'", pp_directives
[ct
]);
1650 j
= reloc_value(evalresult
) != 0;
1655 "preprocessor directive `%s' not yet implemented",
1660 free_tlist(origline
);
1661 return j
^ PP_NEGATIVE(ct
);
1664 free_tlist(origline
);
1669 * Expand macros in a string. Used in %error and %include directives.
1670 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1671 * The returned variable should ALWAYS be freed after usage.
1673 void expand_macros_in_string(char **p
)
1675 Token
*line
= tokenize(*p
);
1676 line
= expand_smacro(line
);
1677 *p
= detoken(line
, false);
1681 * Common code for defining an smacro
1683 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1684 int nparam
, Token
*expansion
)
1686 SMacro
*smac
, **smhead
;
1688 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1691 "single-line macro `%s' defined both with and"
1692 " without parameters", mname
);
1694 /* Some instances of the old code considered this a failure,
1695 some others didn't. What is the right thing to do here? */
1696 free_tlist(expansion
);
1697 return false; /* Failure */
1700 * We're redefining, so we have to take over an
1701 * existing SMacro structure. This means freeing
1702 * what was already in it.
1704 nasm_free(smac
->name
);
1705 free_tlist(smac
->expansion
);
1708 smhead
= (SMacro
**) hash_findi_add(ctx
? ctx
->localmac
: smacros
,
1710 smac
= nasm_malloc(sizeof(SMacro
));
1711 smac
->next
= *smhead
;
1714 smac
->name
= nasm_strdup(mname
);
1715 smac
->casesense
= casesense
;
1716 smac
->nparam
= nparam
;
1717 smac
->expansion
= expansion
;
1718 smac
->in_progress
= false;
1719 return true; /* Success */
1723 * Undefine an smacro
1725 static void undef_smacro(Context
*ctx
, const char *mname
)
1727 SMacro
**smhead
, *s
, **sp
;
1729 smhead
= (SMacro
**)hash_findi(ctx
? ctx
->localmac
: smacros
, mname
, NULL
);
1733 * We now have a macro name... go hunt for it.
1736 while ((s
= *sp
) != NULL
) {
1737 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1740 free_tlist(s
->expansion
);
1750 * Decode a size directive
1752 static int parse_size(const char *str
) {
1753 static const char *size_names
[] =
1754 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1755 static const int sizes
[] =
1756 { 0, 1, 4, 16, 8, 10, 2, 32 };
1758 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1762 * find and process preprocessor directive in passed line
1763 * Find out if a line contains a preprocessor directive, and deal
1766 * If a directive _is_ found, it is the responsibility of this routine
1767 * (and not the caller) to free_tlist() the line.
1769 * @param tline a pointer to the current tokeninzed line linked list
1770 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1773 static int do_directive(Token
* tline
)
1775 enum preproc_token i
;
1787 MMacro
*mmac
, **mmhead
;
1788 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1790 struct tokenval tokval
;
1792 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1798 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1799 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1800 || tline
->text
[1] == '!'))
1801 return NO_DIRECTIVE_FOUND
;
1803 i
= pp_token_hash(tline
->text
);
1806 * If we're in a non-emitting branch of a condition construct,
1807 * or walking to the end of an already terminated %rep block,
1808 * we should ignore all directives except for condition
1811 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1812 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1813 return NO_DIRECTIVE_FOUND
;
1817 * If we're defining a macro or reading a %rep block, we should
1818 * ignore all directives except for %macro/%imacro (which
1819 * generate an error), %endm/%endmacro, and (only if we're in a
1820 * %rep block) %endrep. If we're in a %rep block, another %rep
1821 * causes an error, so should be let through.
1823 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1824 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1825 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1826 return NO_DIRECTIVE_FOUND
;
1831 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1833 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1836 /* Directive to tell NASM what the default stack size is. The
1837 * default is for a 16-bit stack, and this can be overriden with
1839 * the following form:
1841 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1843 tline
= tline
->next
;
1844 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1845 tline
= tline
->next
;
1846 if (!tline
|| tline
->type
!= TOK_ID
) {
1847 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1848 free_tlist(origline
);
1849 return DIRECTIVE_FOUND
;
1851 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1852 /* All subsequent ARG directives are for a 32-bit stack */
1854 StackPointer
= "ebp";
1857 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1858 /* All subsequent ARG directives are for a 64-bit stack */
1860 StackPointer
= "rbp";
1863 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1864 /* All subsequent ARG directives are for a 16-bit stack,
1865 * far function call.
1868 StackPointer
= "bp";
1871 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1872 /* All subsequent ARG directives are for a 16-bit stack,
1873 * far function call. We don't support near functions.
1876 StackPointer
= "bp";
1880 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1881 free_tlist(origline
);
1882 return DIRECTIVE_FOUND
;
1884 free_tlist(origline
);
1885 return DIRECTIVE_FOUND
;
1888 /* TASM like ARG directive to define arguments to functions, in
1889 * the following form:
1891 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1895 char *arg
, directive
[256];
1896 int size
= StackSize
;
1898 /* Find the argument name */
1899 tline
= tline
->next
;
1900 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1901 tline
= tline
->next
;
1902 if (!tline
|| tline
->type
!= TOK_ID
) {
1903 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1904 free_tlist(origline
);
1905 return DIRECTIVE_FOUND
;
1909 /* Find the argument size type */
1910 tline
= tline
->next
;
1911 if (!tline
|| tline
->type
!= TOK_OTHER
1912 || tline
->text
[0] != ':') {
1914 "Syntax error processing `%%arg' directive");
1915 free_tlist(origline
);
1916 return DIRECTIVE_FOUND
;
1918 tline
= tline
->next
;
1919 if (!tline
|| tline
->type
!= TOK_ID
) {
1920 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1921 free_tlist(origline
);
1922 return DIRECTIVE_FOUND
;
1925 /* Allow macro expansion of type parameter */
1926 tt
= tokenize(tline
->text
);
1927 tt
= expand_smacro(tt
);
1928 size
= parse_size(tt
->text
);
1931 "Invalid size type for `%%arg' missing directive");
1933 free_tlist(origline
);
1934 return DIRECTIVE_FOUND
;
1938 /* Round up to even stack slots */
1939 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1941 /* Now define the macro for the argument */
1942 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1943 arg
, StackPointer
, offset
);
1944 do_directive(tokenize(directive
));
1947 /* Move to the next argument in the list */
1948 tline
= tline
->next
;
1949 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1950 tline
= tline
->next
;
1951 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1953 free_tlist(origline
);
1954 return DIRECTIVE_FOUND
;
1957 /* TASM like LOCAL directive to define local variables for a
1958 * function, in the following form:
1960 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1962 * The '= LocalSize' at the end is ignored by NASM, but is
1963 * required by TASM to define the local parameter size (and used
1964 * by the TASM macro package).
1966 offset
= LocalOffset
;
1968 char *local
, directive
[256];
1969 int size
= StackSize
;
1971 /* Find the argument name */
1972 tline
= tline
->next
;
1973 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1974 tline
= tline
->next
;
1975 if (!tline
|| tline
->type
!= TOK_ID
) {
1977 "`%%local' missing argument parameter");
1978 free_tlist(origline
);
1979 return DIRECTIVE_FOUND
;
1981 local
= tline
->text
;
1983 /* Find the argument size type */
1984 tline
= tline
->next
;
1985 if (!tline
|| tline
->type
!= TOK_OTHER
1986 || tline
->text
[0] != ':') {
1988 "Syntax error processing `%%local' directive");
1989 free_tlist(origline
);
1990 return DIRECTIVE_FOUND
;
1992 tline
= tline
->next
;
1993 if (!tline
|| tline
->type
!= TOK_ID
) {
1995 "`%%local' missing size type parameter");
1996 free_tlist(origline
);
1997 return DIRECTIVE_FOUND
;
2000 /* Allow macro expansion of type parameter */
2001 tt
= tokenize(tline
->text
);
2002 tt
= expand_smacro(tt
);
2003 size
= parse_size(tt
->text
);
2006 "Invalid size type for `%%local' missing directive");
2008 free_tlist(origline
);
2009 return DIRECTIVE_FOUND
;
2013 /* Round up to even stack slots */
2014 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2016 offset
+= size
; /* Negative offset, increment before */
2018 /* Now define the macro for the argument */
2019 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2020 local
, StackPointer
, offset
);
2021 do_directive(tokenize(directive
));
2023 /* Now define the assign to setup the enter_c macro correctly */
2024 snprintf(directive
, sizeof(directive
),
2025 "%%assign %%$localsize %%$localsize+%d", size
);
2026 do_directive(tokenize(directive
));
2028 /* Move to the next argument in the list */
2029 tline
= tline
->next
;
2030 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2031 tline
= tline
->next
;
2032 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2033 LocalOffset
= offset
;
2034 free_tlist(origline
);
2035 return DIRECTIVE_FOUND
;
2039 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2042 free_tlist(origline
);
2043 return DIRECTIVE_FOUND
;
2046 tline
= tline
->next
;
2048 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2049 tline
->type
!= TOK_INTERNAL_STRING
)) {
2050 error(ERR_NONFATAL
, "`%%include' expects a file name");
2051 free_tlist(origline
);
2052 return DIRECTIVE_FOUND
; /* but we did _something_ */
2056 "trailing garbage after `%%include' ignored");
2057 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2058 p
= tline
->text
+ 1; /* point past the quote to the name */
2059 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2061 p
= tline
->text
; /* internal_string is easier */
2062 expand_macros_in_string(&p
);
2063 inc
= nasm_malloc(sizeof(Include
));
2066 inc
->fp
= inc_fopen(p
);
2067 if (!inc
->fp
&& pass
== 0) {
2068 /* -MG given but file not found */
2071 inc
->fname
= src_set_fname(p
);
2072 inc
->lineno
= src_set_linnum(0);
2074 inc
->expansion
= NULL
;
2077 list
->uplevel(LIST_INCLUDE
);
2079 free_tlist(origline
);
2080 return DIRECTIVE_FOUND
;
2083 tline
= tline
->next
;
2085 tline
= expand_id(tline
);
2086 if (!tok_type_(tline
, TOK_ID
)) {
2087 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2088 free_tlist(origline
);
2089 return DIRECTIVE_FOUND
; /* but we did _something_ */
2092 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2093 ctx
= nasm_malloc(sizeof(Context
));
2095 ctx
->localmac
= hash_init(HASH_SMALL
);
2096 ctx
->name
= nasm_strdup(tline
->text
);
2097 ctx
->number
= unique
++;
2099 free_tlist(origline
);
2103 tline
= tline
->next
;
2105 tline
= expand_id(tline
);
2106 if (!tok_type_(tline
, TOK_ID
)) {
2107 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2108 free_tlist(origline
);
2109 return DIRECTIVE_FOUND
; /* but we did _something_ */
2112 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2114 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2116 nasm_free(cstk
->name
);
2117 cstk
->name
= nasm_strdup(tline
->text
);
2119 free_tlist(origline
);
2124 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2126 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2129 free_tlist(origline
);
2133 tline
->next
= expand_smacro(tline
->next
);
2134 tline
= tline
->next
;
2136 if (tok_type_(tline
, TOK_STRING
)) {
2137 p
= tline
->text
+ 1; /* point past the quote to the name */
2138 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2139 expand_macros_in_string(&p
);
2140 error(ERR_NONFATAL
, "%s", p
);
2143 p
= detoken(tline
, false);
2144 error(ERR_WARNING
, "%s", p
);
2147 free_tlist(origline
);
2151 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2154 j
= if_condition(tline
->next
, i
);
2155 tline
->next
= NULL
; /* it got freed */
2156 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2158 cond
= nasm_malloc(sizeof(Cond
));
2159 cond
->next
= istk
->conds
;
2162 free_tlist(origline
);
2163 return DIRECTIVE_FOUND
;
2167 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2168 if (emitting(istk
->conds
->state
)
2169 || istk
->conds
->state
== COND_NEVER
)
2170 istk
->conds
->state
= COND_NEVER
;
2173 * IMPORTANT: In the case of %if, we will already have
2174 * called expand_mmac_params(); however, if we're
2175 * processing an %elif we must have been in a
2176 * non-emitting mode, which would have inhibited
2177 * the normal invocation of expand_mmac_params(). Therefore,
2178 * we have to do it explicitly here.
2180 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2181 tline
->next
= NULL
; /* it got freed */
2182 istk
->conds
->state
=
2183 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2185 free_tlist(origline
);
2186 return DIRECTIVE_FOUND
;
2190 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2192 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2193 if (emitting(istk
->conds
->state
)
2194 || istk
->conds
->state
== COND_NEVER
)
2195 istk
->conds
->state
= COND_ELSE_FALSE
;
2197 istk
->conds
->state
= COND_ELSE_TRUE
;
2198 free_tlist(origline
);
2199 return DIRECTIVE_FOUND
;
2203 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2205 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2207 istk
->conds
= cond
->next
;
2209 free_tlist(origline
);
2210 return DIRECTIVE_FOUND
;
2216 "`%%%smacro': already defining a macro",
2217 (i
== PP_IMACRO
? "i" : ""));
2218 tline
= tline
->next
;
2220 tline
= expand_id(tline
);
2221 if (!tok_type_(tline
, TOK_ID
)) {
2223 "`%%%smacro' expects a macro name",
2224 (i
== PP_IMACRO
? "i" : ""));
2225 return DIRECTIVE_FOUND
;
2227 defining
= nasm_malloc(sizeof(MMacro
));
2228 defining
->name
= nasm_strdup(tline
->text
);
2229 defining
->casesense
= (i
== PP_MACRO
);
2230 defining
->plus
= false;
2231 defining
->nolist
= false;
2232 defining
->in_progress
= 0;
2233 defining
->rep_nest
= NULL
;
2234 tline
= expand_smacro(tline
->next
);
2236 if (!tok_type_(tline
, TOK_NUMBER
)) {
2238 "`%%%smacro' expects a parameter count",
2239 (i
== PP_IMACRO
? "i" : ""));
2240 defining
->nparam_min
= defining
->nparam_max
= 0;
2242 defining
->nparam_min
= defining
->nparam_max
=
2243 readnum(tline
->text
, &err
);
2246 "unable to parse parameter count `%s'", tline
->text
);
2248 if (tline
&& tok_is_(tline
->next
, "-")) {
2249 tline
= tline
->next
->next
;
2250 if (tok_is_(tline
, "*"))
2251 defining
->nparam_max
= INT_MAX
;
2252 else if (!tok_type_(tline
, TOK_NUMBER
))
2254 "`%%%smacro' expects a parameter count after `-'",
2255 (i
== PP_IMACRO
? "i" : ""));
2257 defining
->nparam_max
= readnum(tline
->text
, &err
);
2260 "unable to parse parameter count `%s'",
2262 if (defining
->nparam_min
> defining
->nparam_max
)
2264 "minimum parameter count exceeds maximum");
2267 if (tline
&& tok_is_(tline
->next
, "+")) {
2268 tline
= tline
->next
;
2269 defining
->plus
= true;
2271 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2272 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2273 tline
= tline
->next
;
2274 defining
->nolist
= true;
2276 mmac
= (MMacro
*) hash_findix(mmacros
, defining
->name
);
2278 if (!strcmp(mmac
->name
, defining
->name
) &&
2279 (mmac
->nparam_min
<= defining
->nparam_max
2281 && (defining
->nparam_min
<= mmac
->nparam_max
2284 "redefining multi-line macro `%s'", defining
->name
);
2290 * Handle default parameters.
2292 if (tline
&& tline
->next
) {
2293 defining
->dlist
= tline
->next
;
2295 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2296 &defining
->defaults
);
2298 defining
->dlist
= NULL
;
2299 defining
->defaults
= NULL
;
2301 defining
->expansion
= NULL
;
2302 free_tlist(origline
);
2303 return DIRECTIVE_FOUND
;
2308 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2309 return DIRECTIVE_FOUND
;
2311 mmhead
= (MMacro
**) hash_findi_add(mmacros
, defining
->name
);
2312 defining
->next
= *mmhead
;
2315 free_tlist(origline
);
2316 return DIRECTIVE_FOUND
;
2319 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2320 tline
= tline
->next
;
2321 if (tline
->next
== NULL
) {
2322 free_tlist(origline
);
2323 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2324 return DIRECTIVE_FOUND
;
2326 t
= expand_smacro(tline
->next
);
2328 free_tlist(origline
);
2331 tokval
.t_type
= TOKEN_INVALID
;
2333 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2336 return DIRECTIVE_FOUND
;
2339 "trailing garbage after expression ignored");
2340 if (!is_simple(evalresult
)) {
2341 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2342 return DIRECTIVE_FOUND
;
2345 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2346 mmac
= mmac
->next_active
;
2348 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2349 } else if (mmac
->nparam
== 0) {
2351 "`%%rotate' invoked within macro without parameters");
2353 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2355 rotate
%= (int)mmac
->nparam
;
2357 rotate
+= mmac
->nparam
;
2359 mmac
->rotate
= rotate
;
2361 return DIRECTIVE_FOUND
;
2366 tline
= tline
->next
;
2367 } while (tok_type_(tline
, TOK_WHITESPACE
));
2369 if (tok_type_(tline
, TOK_ID
) &&
2370 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2373 tline
= tline
->next
;
2374 } while (tok_type_(tline
, TOK_WHITESPACE
));
2378 t
= expand_smacro(tline
);
2380 tokval
.t_type
= TOKEN_INVALID
;
2382 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2384 free_tlist(origline
);
2385 return DIRECTIVE_FOUND
;
2389 "trailing garbage after expression ignored");
2390 if (!is_simple(evalresult
)) {
2391 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2392 return DIRECTIVE_FOUND
;
2394 count
= reloc_value(evalresult
) + 1;
2396 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2399 free_tlist(origline
);
2401 tmp_defining
= defining
;
2402 defining
= nasm_malloc(sizeof(MMacro
));
2403 defining
->name
= NULL
; /* flags this macro as a %rep block */
2404 defining
->casesense
= false;
2405 defining
->plus
= false;
2406 defining
->nolist
= nolist
;
2407 defining
->in_progress
= count
;
2408 defining
->nparam_min
= defining
->nparam_max
= 0;
2409 defining
->defaults
= NULL
;
2410 defining
->dlist
= NULL
;
2411 defining
->expansion
= NULL
;
2412 defining
->next_active
= istk
->mstk
;
2413 defining
->rep_nest
= tmp_defining
;
2414 return DIRECTIVE_FOUND
;
2417 if (!defining
|| defining
->name
) {
2418 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2419 return DIRECTIVE_FOUND
;
2423 * Now we have a "macro" defined - although it has no name
2424 * and we won't be entering it in the hash tables - we must
2425 * push a macro-end marker for it on to istk->expansion.
2426 * After that, it will take care of propagating itself (a
2427 * macro-end marker line for a macro which is really a %rep
2428 * block will cause the macro to be re-expanded, complete
2429 * with another macro-end marker to ensure the process
2430 * continues) until the whole expansion is forcibly removed
2431 * from istk->expansion by a %exitrep.
2433 l
= nasm_malloc(sizeof(Line
));
2434 l
->next
= istk
->expansion
;
2435 l
->finishes
= defining
;
2437 istk
->expansion
= l
;
2439 istk
->mstk
= defining
;
2441 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2442 tmp_defining
= defining
;
2443 defining
= defining
->rep_nest
;
2444 free_tlist(origline
);
2445 return DIRECTIVE_FOUND
;
2449 * We must search along istk->expansion until we hit a
2450 * macro-end marker for a macro with no name. Then we set
2451 * its `in_progress' flag to 0.
2453 for (l
= istk
->expansion
; l
; l
= l
->next
)
2454 if (l
->finishes
&& !l
->finishes
->name
)
2458 l
->finishes
->in_progress
= 0;
2460 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2461 free_tlist(origline
);
2462 return DIRECTIVE_FOUND
;
2468 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2470 tline
= tline
->next
;
2472 tline
= expand_id(tline
);
2473 if (!tline
|| (tline
->type
!= TOK_ID
&&
2474 (tline
->type
!= TOK_PREPROC_ID
||
2475 tline
->text
[1] != '$'))) {
2476 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
;
2482 ctx
= get_ctx(tline
->text
, false);
2484 mname
= tline
->text
;
2486 param_start
= tline
= tline
->next
;
2489 /* Expand the macro definition now for %xdefine and %ixdefine */
2490 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2491 tline
= expand_smacro(tline
);
2493 if (tok_is_(tline
, "(")) {
2495 * This macro has parameters.
2498 tline
= tline
->next
;
2502 error(ERR_NONFATAL
, "parameter identifier expected");
2503 free_tlist(origline
);
2504 return DIRECTIVE_FOUND
;
2506 if (tline
->type
!= TOK_ID
) {
2508 "`%s': parameter identifier expected",
2510 free_tlist(origline
);
2511 return DIRECTIVE_FOUND
;
2513 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2514 tline
= tline
->next
;
2516 if (tok_is_(tline
, ",")) {
2517 tline
= tline
->next
;
2520 if (!tok_is_(tline
, ")")) {
2522 "`)' expected to terminate macro template");
2523 free_tlist(origline
);
2524 return DIRECTIVE_FOUND
;
2529 tline
= tline
->next
;
2531 if (tok_type_(tline
, TOK_WHITESPACE
))
2532 last
= tline
, tline
= tline
->next
;
2537 if (t
->type
== TOK_ID
) {
2538 for (tt
= param_start
; tt
; tt
= tt
->next
)
2539 if (tt
->type
>= TOK_SMAC_PARAM
&&
2540 !strcmp(tt
->text
, t
->text
))
2544 t
->next
= macro_start
;
2549 * Good. We now have a macro name, a parameter count, and a
2550 * token list (in reverse order) for an expansion. We ought
2551 * to be OK just to create an SMacro, store it, and let
2552 * free_tlist have the rest of the line (which we have
2553 * carefully re-terminated after chopping off the expansion
2556 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2557 free_tlist(origline
);
2558 return DIRECTIVE_FOUND
;
2561 tline
= tline
->next
;
2563 tline
= expand_id(tline
);
2564 if (!tline
|| (tline
->type
!= TOK_ID
&&
2565 (tline
->type
!= TOK_PREPROC_ID
||
2566 tline
->text
[1] != '$'))) {
2567 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2568 free_tlist(origline
);
2569 return DIRECTIVE_FOUND
;
2573 "trailing garbage after macro name ignored");
2576 /* Find the context that symbol belongs to */
2577 ctx
= get_ctx(tline
->text
, false);
2578 undef_smacro(ctx
, tline
->text
);
2579 free_tlist(origline
);
2580 return DIRECTIVE_FOUND
;
2585 tline
= tline
->next
;
2587 tline
= expand_id(tline
);
2588 if (!tline
|| (tline
->type
!= TOK_ID
&&
2589 (tline
->type
!= TOK_PREPROC_ID
||
2590 tline
->text
[1] != '$'))) {
2592 "`%%strlen' expects a macro identifier as first parameter");
2593 free_tlist(origline
);
2594 return DIRECTIVE_FOUND
;
2596 ctx
= get_ctx(tline
->text
, false);
2598 mname
= tline
->text
;
2600 tline
= expand_smacro(tline
->next
);
2604 while (tok_type_(t
, TOK_WHITESPACE
))
2606 /* t should now point to the string */
2607 if (t
->type
!= TOK_STRING
) {
2609 "`%%strlen` requires string as second parameter");
2611 free_tlist(origline
);
2612 return DIRECTIVE_FOUND
;
2615 macro_start
= nasm_malloc(sizeof(*macro_start
));
2616 macro_start
->next
= NULL
;
2617 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2618 macro_start
->mac
= NULL
;
2621 * We now have a macro name, an implicit parameter count of
2622 * zero, and a numeric token to use as an expansion. Create
2623 * and store an SMacro.
2625 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2627 free_tlist(origline
);
2628 return DIRECTIVE_FOUND
;
2633 tline
= tline
->next
;
2635 tline
= expand_id(tline
);
2636 if (!tline
|| (tline
->type
!= TOK_ID
&&
2637 (tline
->type
!= TOK_PREPROC_ID
||
2638 tline
->text
[1] != '$'))) {
2640 "`%%substr' expects a macro identifier as first parameter");
2641 free_tlist(origline
);
2642 return DIRECTIVE_FOUND
;
2644 ctx
= get_ctx(tline
->text
, false);
2646 mname
= tline
->text
;
2648 tline
= expand_smacro(tline
->next
);
2652 while (tok_type_(t
, TOK_WHITESPACE
))
2655 /* t should now point to the string */
2656 if (t
->type
!= TOK_STRING
) {
2658 "`%%substr` requires string as second parameter");
2660 free_tlist(origline
);
2661 return DIRECTIVE_FOUND
;
2666 tokval
.t_type
= TOKEN_INVALID
;
2668 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2671 free_tlist(origline
);
2672 return DIRECTIVE_FOUND
;
2674 if (!is_simple(evalresult
)) {
2675 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2677 free_tlist(origline
);
2678 return DIRECTIVE_FOUND
;
2681 macro_start
= nasm_malloc(sizeof(*macro_start
));
2682 macro_start
->next
= NULL
;
2683 macro_start
->text
= nasm_strdup("'''");
2684 if (evalresult
->value
> 0
2685 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2686 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2688 macro_start
->text
[2] = '\0';
2690 macro_start
->type
= TOK_STRING
;
2691 macro_start
->mac
= NULL
;
2694 * We now have a macro name, an implicit parameter count of
2695 * zero, and a numeric token to use as an expansion. Create
2696 * and store an SMacro.
2698 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2700 free_tlist(origline
);
2701 return DIRECTIVE_FOUND
;
2705 casesense
= (i
== PP_ASSIGN
);
2707 tline
= tline
->next
;
2709 tline
= expand_id(tline
);
2710 if (!tline
|| (tline
->type
!= TOK_ID
&&
2711 (tline
->type
!= TOK_PREPROC_ID
||
2712 tline
->text
[1] != '$'))) {
2714 "`%%%sassign' expects a macro identifier",
2715 (i
== PP_IASSIGN
? "i" : ""));
2716 free_tlist(origline
);
2717 return DIRECTIVE_FOUND
;
2719 ctx
= get_ctx(tline
->text
, false);
2721 mname
= tline
->text
;
2723 tline
= expand_smacro(tline
->next
);
2728 tokval
.t_type
= TOKEN_INVALID
;
2730 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2733 free_tlist(origline
);
2734 return DIRECTIVE_FOUND
;
2739 "trailing garbage after expression ignored");
2741 if (!is_simple(evalresult
)) {
2743 "non-constant value given to `%%%sassign'",
2744 (i
== PP_IASSIGN
? "i" : ""));
2745 free_tlist(origline
);
2746 return DIRECTIVE_FOUND
;
2749 macro_start
= nasm_malloc(sizeof(*macro_start
));
2750 macro_start
->next
= NULL
;
2751 make_tok_num(macro_start
, reloc_value(evalresult
));
2752 macro_start
->mac
= NULL
;
2755 * We now have a macro name, an implicit parameter count of
2756 * zero, and a numeric token to use as an expansion. Create
2757 * and store an SMacro.
2759 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2760 free_tlist(origline
);
2761 return DIRECTIVE_FOUND
;
2765 * Syntax is `%line nnn[+mmm] [filename]'
2767 tline
= tline
->next
;
2769 if (!tok_type_(tline
, TOK_NUMBER
)) {
2770 error(ERR_NONFATAL
, "`%%line' expects line number");
2771 free_tlist(origline
);
2772 return DIRECTIVE_FOUND
;
2774 k
= readnum(tline
->text
, &err
);
2776 tline
= tline
->next
;
2777 if (tok_is_(tline
, "+")) {
2778 tline
= tline
->next
;
2779 if (!tok_type_(tline
, TOK_NUMBER
)) {
2780 error(ERR_NONFATAL
, "`%%line' expects line increment");
2781 free_tlist(origline
);
2782 return DIRECTIVE_FOUND
;
2784 m
= readnum(tline
->text
, &err
);
2785 tline
= tline
->next
;
2791 nasm_free(src_set_fname(detoken(tline
, false)));
2793 free_tlist(origline
);
2794 return DIRECTIVE_FOUND
;
2798 "preprocessor directive `%s' not yet implemented",
2802 return DIRECTIVE_FOUND
;
2806 * Ensure that a macro parameter contains a condition code and
2807 * nothing else. Return the condition code index if so, or -1
2810 static int find_cc(Token
* t
)
2816 return -1; /* Probably a %+ without a space */
2819 if (t
->type
!= TOK_ID
)
2823 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2827 j
= elements(conditions
);
2830 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2846 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2847 * %-n) and MMacro-local identifiers (%%foo).
2849 static Token
*expand_mmac_params(Token
* tline
)
2851 Token
*t
, *tt
, **tail
, *thead
;
2857 if (tline
->type
== TOK_PREPROC_ID
&&
2858 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2859 && tline
->text
[2]) || tline
->text
[1] == '%'
2860 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2862 int type
= 0, cc
; /* type = 0 to placate optimisers */
2869 tline
= tline
->next
;
2872 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2873 mac
= mac
->next_active
;
2875 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2877 switch (t
->text
[1]) {
2879 * We have to make a substitution of one of the
2880 * forms %1, %-1, %+1, %%foo, %0.
2884 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2885 text
= nasm_strdup(tmpbuf
);
2889 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2891 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
2894 n
= atoi(t
->text
+ 2) - 1;
2895 if (n
>= mac
->nparam
)
2898 if (mac
->nparam
> 1)
2899 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2900 tt
= mac
->params
[n
];
2905 "macro parameter %d is not a condition code",
2910 if (inverse_ccs
[cc
] == -1) {
2912 "condition code `%s' is not invertible",
2917 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2921 n
= atoi(t
->text
+ 2) - 1;
2922 if (n
>= mac
->nparam
)
2925 if (mac
->nparam
> 1)
2926 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2927 tt
= mac
->params
[n
];
2932 "macro parameter %d is not a condition code",
2937 text
= nasm_strdup(conditions
[cc
]);
2941 n
= atoi(t
->text
+ 1) - 1;
2942 if (n
>= mac
->nparam
)
2945 if (mac
->nparam
> 1)
2946 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2947 tt
= mac
->params
[n
];
2950 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2951 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2952 tail
= &(*tail
)->next
;
2956 text
= NULL
; /* we've done it here */
2972 tline
= tline
->next
;
2979 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2981 case TOK_WHITESPACE
:
2982 if (tt
->type
== TOK_WHITESPACE
) {
2983 t
->next
= delete_Token(tt
);
2987 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2988 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2991 t
->next
= delete_Token(tt
);
2995 if (tt
->type
== TOK_NUMBER
) {
2996 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2999 t
->next
= delete_Token(tt
);
3010 * Expand all single-line macro calls made in the given line.
3011 * Return the expanded version of the line. The original is deemed
3012 * to be destroyed in the process. (In reality we'll just move
3013 * Tokens from input to output a lot of the time, rather than
3014 * actually bothering to destroy and replicate.)
3016 #define DEADMAN_LIMIT (1 << 20)
3018 static Token
*expand_smacro(Token
* tline
)
3020 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3021 SMacro
*head
= NULL
, *m
;
3024 unsigned int nparam
, sparam
;
3025 int brackets
, rescan
;
3026 Token
*org_tline
= tline
;
3029 int deadman
= DEADMAN_LIMIT
;
3032 * Trick: we should avoid changing the start token pointer since it can
3033 * be contained in "next" field of other token. Because of this
3034 * we allocate a copy of first token and work with it; at the end of
3035 * routine we copy it back
3039 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3041 tline
->mac
= org_tline
->mac
;
3042 nasm_free(org_tline
->text
);
3043 org_tline
->text
= NULL
;
3050 while (tline
) { /* main token loop */
3052 error(ERR_NONFATAL
, "interminable macro recursion");
3056 if ((mname
= tline
->text
)) {
3057 /* if this token is a local macro, look in local context */
3058 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3059 ctx
= get_ctx(mname
, true);
3063 head
= (SMacro
*) hash_findix(ctx
? ctx
->localmac
: smacros
,
3067 * We've hit an identifier. As in is_mmacro below, we first
3068 * check whether the identifier is a single-line macro at
3069 * all, then think about checking for parameters if
3072 for (m
= head
; m
; m
= m
->next
)
3073 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3079 if (m
->nparam
== 0) {
3081 * Simple case: the macro is parameterless. Discard the
3082 * one token that the macro call took, and push the
3083 * expansion back on the to-do stack.
3085 if (!m
->expansion
) {
3086 if (!strcmp("__FILE__", m
->name
)) {
3088 src_get(&num
, &(tline
->text
));
3089 nasm_quote(&(tline
->text
));
3090 tline
->type
= TOK_STRING
;
3093 if (!strcmp("__LINE__", m
->name
)) {
3094 nasm_free(tline
->text
);
3095 make_tok_num(tline
, src_get_linnum());
3098 if (!strcmp("__BITS__", m
->name
)) {
3099 nasm_free(tline
->text
);
3100 make_tok_num(tline
, globalbits
);
3103 tline
= delete_Token(tline
);
3108 * Complicated case: at least one macro with this name
3109 * exists and takes parameters. We must find the
3110 * parameters in the call, count them, find the SMacro
3111 * that corresponds to that form of the macro call, and
3112 * substitute for the parameters when we expand. What a
3115 /*tline = tline->next;
3116 skip_white_(tline); */
3119 while (tok_type_(t
, TOK_SMAC_END
)) {
3120 t
->mac
->in_progress
= false;
3122 t
= tline
->next
= delete_Token(t
);
3125 } while (tok_type_(tline
, TOK_WHITESPACE
));
3126 if (!tok_is_(tline
, "(")) {
3128 * This macro wasn't called with parameters: ignore
3129 * the call. (Behaviour borrowed from gnu cpp.)
3138 sparam
= PARAM_DELTA
;
3139 params
= nasm_malloc(sparam
* sizeof(Token
*));
3140 params
[0] = tline
->next
;
3141 paramsize
= nasm_malloc(sparam
* sizeof(int));
3143 while (true) { /* parameter loop */
3145 * For some unusual expansions
3146 * which concatenates function call
3149 while (tok_type_(t
, TOK_SMAC_END
)) {
3150 t
->mac
->in_progress
= false;
3152 t
= tline
->next
= delete_Token(t
);
3158 "macro call expects terminating `)'");
3161 if (tline
->type
== TOK_WHITESPACE
3163 if (paramsize
[nparam
])
3166 params
[nparam
] = tline
->next
;
3167 continue; /* parameter loop */
3169 if (tline
->type
== TOK_OTHER
3170 && tline
->text
[1] == 0) {
3171 char ch
= tline
->text
[0];
3172 if (ch
== ',' && !paren
&& brackets
<= 0) {
3173 if (++nparam
>= sparam
) {
3174 sparam
+= PARAM_DELTA
;
3175 params
= nasm_realloc(params
,
3180 nasm_realloc(paramsize
,
3184 params
[nparam
] = tline
->next
;
3185 paramsize
[nparam
] = 0;
3187 continue; /* parameter loop */
3190 (brackets
> 0 || (brackets
== 0 &&
3191 !paramsize
[nparam
])))
3193 if (!(brackets
++)) {
3194 params
[nparam
] = tline
->next
;
3195 continue; /* parameter loop */
3198 if (ch
== '}' && brackets
> 0)
3199 if (--brackets
== 0) {
3201 continue; /* parameter loop */
3203 if (ch
== '(' && !brackets
)
3205 if (ch
== ')' && brackets
<= 0)
3211 error(ERR_NONFATAL
, "braces do not "
3212 "enclose all of macro parameter");
3214 paramsize
[nparam
] += white
+ 1;
3216 } /* parameter loop */
3218 while (m
&& (m
->nparam
!= nparam
||
3219 mstrcmp(m
->name
, mname
,
3223 error(ERR_WARNING
| ERR_WARN_MNP
,
3224 "macro `%s' exists, "
3225 "but not taking %d parameters",
3226 mstart
->text
, nparam
);
3229 if (m
&& m
->in_progress
)
3231 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3233 * Design question: should we handle !tline, which
3234 * indicates missing ')' here, or expand those
3235 * macros anyway, which requires the (t) test a few
3239 nasm_free(paramsize
);
3243 * Expand the macro: we are placed on the last token of the
3244 * call, so that we can easily split the call from the
3245 * following tokens. We also start by pushing an SMAC_END
3246 * token for the cycle removal.
3253 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3255 m
->in_progress
= true;
3257 for (t
= m
->expansion
; t
; t
= t
->next
) {
3258 if (t
->type
>= TOK_SMAC_PARAM
) {
3259 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3263 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3264 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3267 new_Token(tline
, ttt
->type
, ttt
->text
,
3273 } else if (t
->type
== TOK_PREPROC_Q
) {
3274 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3276 } else if (t
->type
== TOK_PREPROC_QQ
) {
3277 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3280 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3286 * Having done that, get rid of the macro call, and clean
3287 * up the parameters.
3290 nasm_free(paramsize
);
3292 continue; /* main token loop */
3297 if (tline
->type
== TOK_SMAC_END
) {
3298 tline
->mac
->in_progress
= false;
3299 tline
= delete_Token(tline
);
3302 tline
= tline
->next
;
3310 * Now scan the entire line and look for successive TOK_IDs that resulted
3311 * after expansion (they can't be produced by tokenize()). The successive
3312 * TOK_IDs should be concatenated.
3313 * Also we look for %+ tokens and concatenate the tokens before and after
3314 * them (without white spaces in between).
3319 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3323 if (t
->next
->type
== TOK_ID
||
3324 t
->next
->type
== TOK_PREPROC_ID
||
3325 t
->next
->type
== TOK_NUMBER
) {
3326 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3328 t
->next
= delete_Token(t
->next
);
3331 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3332 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3333 strcmp(t
->next
->next
->text
, "%+") == 0) {
3334 /* free the next whitespace, the %+ token and next whitespace */
3336 for (i
= 1; i
<= 3; i
++) {
3338 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3340 t
->next
= delete_Token(t
->next
);
3345 /* If we concatenaded something, re-scan the line for macros */
3353 *org_tline
= *thead
;
3354 /* since we just gave text to org_line, don't free it */
3356 delete_Token(thead
);
3358 /* the expression expanded to empty line;
3359 we can't return NULL for some reasons
3360 we just set the line to a single WHITESPACE token. */
3361 memset(org_tline
, 0, sizeof(*org_tline
));
3362 org_tline
->text
= NULL
;
3363 org_tline
->type
= TOK_WHITESPACE
;
3372 * Similar to expand_smacro but used exclusively with macro identifiers
3373 * right before they are fetched in. The reason is that there can be
3374 * identifiers consisting of several subparts. We consider that if there
3375 * are more than one element forming the name, user wants a expansion,
3376 * otherwise it will be left as-is. Example:
3380 * the identifier %$abc will be left as-is so that the handler for %define
3381 * will suck it and define the corresponding value. Other case:
3383 * %define _%$abc cde
3385 * In this case user wants name to be expanded *before* %define starts
3386 * working, so we'll expand %$abc into something (if it has a value;
3387 * otherwise it will be left as-is) then concatenate all successive
3390 static Token
*expand_id(Token
* tline
)
3392 Token
*cur
, *oldnext
= NULL
;
3394 if (!tline
|| !tline
->next
)
3399 (cur
->next
->type
== TOK_ID
||
3400 cur
->next
->type
== TOK_PREPROC_ID
3401 || cur
->next
->type
== TOK_NUMBER
))
3404 /* If identifier consists of just one token, don't expand */
3409 oldnext
= cur
->next
; /* Detach the tail past identifier */
3410 cur
->next
= NULL
; /* so that expand_smacro stops here */
3413 tline
= expand_smacro(tline
);
3416 /* expand_smacro possibly changhed tline; re-scan for EOL */
3418 while (cur
&& cur
->next
)
3421 cur
->next
= oldnext
;
3428 * Determine whether the given line constitutes a multi-line macro
3429 * call, and return the MMacro structure called if so. Doesn't have
3430 * to check for an initial label - that's taken care of in
3431 * expand_mmacro - but must check numbers of parameters. Guaranteed
3432 * to be called with tline->type == TOK_ID, so the putative macro
3433 * name is easy to find.
3435 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3441 head
= (MMacro
*) hash_findix(mmacros
, tline
->text
);
3444 * Efficiency: first we see if any macro exists with the given
3445 * name. If not, we can return NULL immediately. _Then_ we
3446 * count the parameters, and then we look further along the
3447 * list if necessary to find the proper MMacro.
3449 for (m
= head
; m
; m
= m
->next
)
3450 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3456 * OK, we have a potential macro. Count and demarcate the
3459 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3462 * So we know how many parameters we've got. Find the MMacro
3463 * structure that handles this number.
3466 if (m
->nparam_min
<= nparam
3467 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3469 * This one is right. Just check if cycle removal
3470 * prohibits us using it before we actually celebrate...
3472 if (m
->in_progress
) {
3475 "self-reference in multi-line macro `%s'", m
->name
);
3481 * It's right, and we can use it. Add its default
3482 * parameters to the end of our list if necessary.
3484 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3486 nasm_realloc(params
,
3487 ((m
->nparam_min
+ m
->ndefs
+
3488 1) * sizeof(*params
)));
3489 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3490 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3495 * If we've gone over the maximum parameter count (and
3496 * we're in Plus mode), ignore parameters beyond
3499 if (m
->plus
&& nparam
> m
->nparam_max
)
3500 nparam
= m
->nparam_max
;
3502 * Then terminate the parameter list, and leave.
3504 if (!params
) { /* need this special case */
3505 params
= nasm_malloc(sizeof(*params
));
3508 params
[nparam
] = NULL
;
3509 *params_array
= params
;
3513 * This one wasn't right: look for the next one with the
3516 for (m
= m
->next
; m
; m
= m
->next
)
3517 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3522 * After all that, we didn't find one with the right number of
3523 * parameters. Issue a warning, and fail to expand the macro.
3525 error(ERR_WARNING
| ERR_WARN_MNP
,
3526 "macro `%s' exists, but not taking %d parameters",
3527 tline
->text
, nparam
);
3533 * Expand the multi-line macro call made by the given line, if
3534 * there is one to be expanded. If there is, push the expansion on
3535 * istk->expansion and return 1. Otherwise return 0.
3537 static int expand_mmacro(Token
* tline
)
3539 Token
*startline
= tline
;
3540 Token
*label
= NULL
;
3541 int dont_prepend
= 0;
3542 Token
**params
, *t
, *mtok
, *tt
;
3545 int i
, nparam
, *paramlen
;
3549 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3550 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3553 m
= is_mmacro(t
, ¶ms
);
3557 * We have an id which isn't a macro call. We'll assume
3558 * it might be a label; we'll also check to see if a
3559 * colon follows it. Then, if there's another id after
3560 * that lot, we'll check it again for macro-hood.
3564 if (tok_type_(t
, TOK_WHITESPACE
))
3565 last
= t
, t
= t
->next
;
3566 if (tok_is_(t
, ":")) {
3568 last
= t
, t
= t
->next
;
3569 if (tok_type_(t
, TOK_WHITESPACE
))
3570 last
= t
, t
= t
->next
;
3572 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3579 * Fix up the parameters: this involves stripping leading and
3580 * trailing whitespace, then stripping braces if they are
3583 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3584 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3586 for (i
= 0; params
[i
]; i
++) {
3588 int comma
= (!m
->plus
|| i
< nparam
- 1);
3592 if (tok_is_(t
, "{"))
3593 t
= t
->next
, brace
= true, comma
= false;
3597 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3598 break; /* ... because we have hit a comma */
3599 if (comma
&& t
->type
== TOK_WHITESPACE
3600 && tok_is_(t
->next
, ","))
3601 break; /* ... or a space then a comma */
3602 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3603 break; /* ... or a brace */
3610 * OK, we have a MMacro structure together with a set of
3611 * parameters. We must now go through the expansion and push
3612 * copies of each Line on to istk->expansion. Substitution of
3613 * parameter tokens and macro-local tokens doesn't get done
3614 * until the single-line macro substitution process; this is
3615 * because delaying them allows us to change the semantics
3616 * later through %rotate.
3618 * First, push an end marker on to istk->expansion, mark this
3619 * macro as in progress, and set up its invocation-specific
3622 ll
= nasm_malloc(sizeof(Line
));
3623 ll
->next
= istk
->expansion
;
3626 istk
->expansion
= ll
;
3628 m
->in_progress
= true;
3633 m
->paramlen
= paramlen
;
3634 m
->unique
= unique
++;
3637 m
->next_active
= istk
->mstk
;
3640 for (l
= m
->expansion
; l
; l
= l
->next
) {
3643 ll
= nasm_malloc(sizeof(Line
));
3644 ll
->finishes
= NULL
;
3645 ll
->next
= istk
->expansion
;
3646 istk
->expansion
= ll
;
3649 for (t
= l
->first
; t
; t
= t
->next
) {
3653 tt
= *tail
= new_Token(NULL
, TOK_ID
, mtok
->text
, 0);
3655 case TOK_PREPROC_QQ
:
3656 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3658 case TOK_PREPROC_ID
:
3659 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3667 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3676 * If we had a label, push it on as the first line of
3677 * the macro expansion.
3680 if (dont_prepend
< 0)
3681 free_tlist(startline
);
3683 ll
= nasm_malloc(sizeof(Line
));
3684 ll
->finishes
= NULL
;
3685 ll
->next
= istk
->expansion
;
3686 istk
->expansion
= ll
;
3687 ll
->first
= startline
;
3688 if (!dont_prepend
) {
3690 label
= label
->next
;
3691 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3696 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3702 * Since preprocessor always operate only on the line that didn't
3703 * arrived yet, we should always use ERR_OFFBY1. Also since user
3704 * won't want to see same error twice (preprocessing is done once
3705 * per pass) we will want to show errors only during pass one.
3707 static void error(int severity
, const char *fmt
, ...)
3712 /* If we're in a dead branch of IF or something like it, ignore the error */
3713 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3717 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3720 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3721 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3722 istk
->mstk
->lineno
, buff
);
3724 _error(severity
| ERR_PASS1
, "%s", buff
);
3728 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3733 istk
= nasm_malloc(sizeof(Include
));
3736 istk
->expansion
= NULL
;
3738 istk
->fp
= fopen(file
, "r");
3740 src_set_fname(nasm_strdup(file
));
3744 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3749 if (tasm_compatible_mode
) {
3750 stdmacpos
= nasm_stdmac
;
3752 stdmacpos
= nasm_stdmac_after_tasm
;
3754 any_extrastdmac
= (extrastdmac
!= NULL
);
3760 static char *pp_getline(void)
3767 * Fetch a tokenized line, either from the macro-expansion
3768 * buffer or from the input file.
3771 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3772 Line
*l
= istk
->expansion
;
3773 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3777 * This is a macro-end marker for a macro with no
3778 * name, which means it's not really a macro at all
3779 * but a %rep block, and the `in_progress' field is
3780 * more than 1, meaning that we still need to
3781 * repeat. (1 means the natural last repetition; 0
3782 * means termination by %exitrep.) We have
3783 * therefore expanded up to the %endrep, and must
3784 * push the whole block on to the expansion buffer
3785 * again. We don't bother to remove the macro-end
3786 * marker: we'd only have to generate another one
3789 l
->finishes
->in_progress
--;
3790 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3791 Token
*t
, *tt
, **tail
;
3793 ll
= nasm_malloc(sizeof(Line
));
3794 ll
->next
= istk
->expansion
;
3795 ll
->finishes
= NULL
;
3799 for (t
= l
->first
; t
; t
= t
->next
) {
3800 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3802 new_Token(NULL
, t
->type
, t
->text
, 0);
3807 istk
->expansion
= ll
;
3811 * Check whether a `%rep' was started and not ended
3812 * within this macro expansion. This can happen and
3813 * should be detected. It's a fatal error because
3814 * I'm too confused to work out how to recover
3820 "defining with name in expansion");
3821 else if (istk
->mstk
->name
)
3823 "`%%rep' without `%%endrep' within"
3824 " expansion of macro `%s'",
3829 * FIXME: investigate the relationship at this point between
3830 * istk->mstk and l->finishes
3833 MMacro
*m
= istk
->mstk
;
3834 istk
->mstk
= m
->next_active
;
3837 * This was a real macro call, not a %rep, and
3838 * therefore the parameter information needs to
3841 nasm_free(m
->params
);
3842 free_tlist(m
->iline
);
3843 nasm_free(m
->paramlen
);
3844 l
->finishes
->in_progress
= false;
3848 istk
->expansion
= l
->next
;
3850 list
->downlevel(LIST_MACRO
);
3853 while (1) { /* until we get a line we can use */
3855 if (istk
->expansion
) { /* from a macro expansion */
3857 Line
*l
= istk
->expansion
;
3859 istk
->mstk
->lineno
++;
3861 istk
->expansion
= l
->next
;
3863 p
= detoken(tline
, false);
3864 list
->line(LIST_MACRO
, p
);
3869 if (line
) { /* from the current input file */
3870 line
= prepreproc(line
);
3871 tline
= tokenize(line
);
3876 * The current file has ended; work down the istk
3883 "expected `%%endif' before end of file");
3884 /* only set line and file name if there's a next node */
3886 src_set_linnum(i
->lineno
);
3887 nasm_free(src_set_fname(i
->fname
));
3890 list
->downlevel(LIST_INCLUDE
);
3898 * We must expand MMacro parameters and MMacro-local labels
3899 * _before_ we plunge into directive processing, to cope
3900 * with things like `%define something %1' such as STRUC
3901 * uses. Unless we're _defining_ a MMacro, in which case
3902 * those tokens should be left alone to go into the
3903 * definition; and unless we're in a non-emitting
3904 * condition, in which case we don't want to meddle with
3907 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3908 tline
= expand_mmac_params(tline
);
3911 * Check the line to see if it's a preprocessor directive.
3913 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3915 } else if (defining
) {
3917 * We're defining a multi-line macro. We emit nothing
3919 * shove the tokenized line on to the macro definition.
3921 Line
*l
= nasm_malloc(sizeof(Line
));
3922 l
->next
= defining
->expansion
;
3924 l
->finishes
= false;
3925 defining
->expansion
= l
;
3927 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3929 * We're in a non-emitting branch of a condition block.
3930 * Emit nothing at all, not even a blank line: when we
3931 * emerge from the condition we'll give a line-number
3932 * directive so we keep our place correctly.
3936 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3938 * We're in a %rep block which has been terminated, so
3939 * we're walking through to the %endrep without
3940 * emitting anything. Emit nothing at all, not even a
3941 * blank line: when we emerge from the %rep block we'll
3942 * give a line-number directive so we keep our place
3948 tline
= expand_smacro(tline
);
3949 if (!expand_mmacro(tline
)) {
3951 * De-tokenize the line again, and emit it.
3953 line
= detoken(tline
, true);
3957 continue; /* expand_mmacro calls free_tlist */
3965 static void pp_cleanup(int pass
)
3968 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3970 free_mmacro(defining
);
3979 nasm_free(i
->fname
);
3990 void pp_include_path(char *path
)
3994 i
= nasm_malloc(sizeof(IncPath
));
3995 i
->path
= path
? nasm_strdup(path
) : NULL
;
3998 if (ipath
!= NULL
) {
4000 while (j
->next
!= NULL
)
4011 * This function is used to "export" the include paths, e.g.
4012 * the paths specified in the '-I' command switch.
4013 * The need for such exporting is due to the 'incbin' directive,
4014 * which includes raw binary files (unlike '%include', which
4015 * includes text source files). It would be real nice to be
4016 * able to specify paths to search for incbin'ned files also.
4017 * So, this is a simple workaround.
4019 * The function use is simple:
4021 * The 1st call (with NULL argument) returns a pointer to the 1st path
4022 * (char** type) or NULL if none include paths available.
4024 * All subsequent calls take as argument the value returned by this
4025 * function last. The return value is either the next path
4026 * (char** type) or NULL if the end of the paths list is reached.
4028 * It is maybe not the best way to do things, but I didn't want
4029 * to export too much, just one or two functions and no types or
4030 * variables exported.
4032 * Can't say I like the current situation with e.g. this path list either,
4033 * it seems to be never deallocated after creation...
4035 char **pp_get_include_path_ptr(char **pPrevPath
)
4037 /* This macro returns offset of a member of a structure */
4038 #define GetMemberOffset(StructType,MemberName)\
4039 ((size_t)&((StructType*)0)->MemberName)
4042 if (pPrevPath
== NULL
) {
4044 return &ipath
->path
;
4048 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
4054 #undef GetMemberOffset
4057 void pp_pre_include(char *fname
)
4059 Token
*inc
, *space
, *name
;
4062 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4063 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4064 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4066 l
= nasm_malloc(sizeof(Line
));
4069 l
->finishes
= false;
4073 void pp_pre_define(char *definition
)
4079 equals
= strchr(definition
, '=');
4080 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4081 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4084 space
->next
= tokenize(definition
);
4088 l
= nasm_malloc(sizeof(Line
));
4091 l
->finishes
= false;
4095 void pp_pre_undefine(char *definition
)
4100 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4101 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4102 space
->next
= tokenize(definition
);
4104 l
= nasm_malloc(sizeof(Line
));
4107 l
->finishes
= false;
4112 * Added by Keith Kanios:
4114 * This function is used to assist with "runtime" preprocessor
4115 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4117 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4118 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4121 void pp_runtime(char *definition
)
4125 def
= tokenize(definition
);
4126 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4131 void pp_extra_stdmac(const char **macros
)
4133 extrastdmac
= macros
;
4136 static void make_tok_num(Token
* tok
, int64_t val
)
4139 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4140 tok
->text
= nasm_strdup(numbuf
);
4141 tok
->type
= TOK_NUMBER
;