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
56 typedef struct SMacro SMacro
;
57 typedef struct MMacro MMacro
;
58 typedef struct Context Context
;
59 typedef struct Token Token
;
60 typedef struct Blocks Blocks
;
61 typedef struct Line Line
;
62 typedef struct Include Include
;
63 typedef struct Cond Cond
;
64 typedef struct IncPath IncPath
;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
107 int nparam_min
, nparam_max
;
109 bool plus
; /* is the last parameter greedy? */
110 bool nolist
; /* is this macro listing-inhibited? */
112 Token
*dlist
; /* All defaults as one list */
113 Token
**defaults
; /* Parameter default pointers */
114 int ndefs
; /* number of default parameters */
118 MMacro
*rep_nest
; /* used for nesting %rep */
119 Token
**params
; /* actual parameters */
120 Token
*iline
; /* invocation line */
121 unsigned int nparam
, rotate
;
124 int lineno
; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
133 struct hash_table localmac
;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
157 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
158 TOK_PREPROC_ID
, TOK_STRING
,
159 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
161 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
162 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
169 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
170 enum pp_token_type type
;
174 * Multi-line macro definitions are stored as a linked list of
175 * these, which is essentially a container to allow several linked
178 * Note that in this module, linked lists are treated as stacks
179 * wherever possible. For this reason, Lines are _pushed_ on to the
180 * `expansion' field in MMacro structures, so that the linked list,
181 * if walked, would give the macro lines in reverse order; this
182 * means that we can walk the list when expanding a macro, and thus
183 * push the lines on to the `expansion' field in _istk_ in reverse
184 * order (so that when popped back off they are in the right
185 * order). It may seem cockeyed, and it relies on my design having
186 * an even number of steps in, but it works...
188 * Some of these structures, rather than being actual lines, are
189 * markers delimiting the end of the expansion of a given macro.
190 * This is for use in the cycle-tracking and %rep-handling code.
191 * Such structures have `finishes' non-NULL, and `first' NULL. All
192 * others have `finishes' NULL, but `first' may still be NULL if
202 * To handle an arbitrary level of file inclusion, we maintain a
203 * stack (ie linked list) of these things.
212 MMacro
*mstk
; /* stack of active macros/reps */
216 * Include search path. This is simply a list of strings which get
217 * prepended, in turn, to the name of an include file, in an
218 * attempt to find the file if it's not in the current directory.
226 * Conditional assembly: we maintain a separate stack of these for
227 * each level of file inclusion. (The only reason we keep the
228 * stacks separate is to ensure that a stray `%endif' in a file
229 * included from within the true branch of a `%if' won't terminate
230 * it and cause confusion: instead, rightly, it'll cause an error.)
238 * These states are for use just after %if or %elif: IF_TRUE
239 * means the condition has evaluated to truth so we are
240 * currently emitting, whereas IF_FALSE means we are not
241 * currently emitting but will start doing so if a %else comes
242 * up. In these states, all directives are admissible: %elif,
243 * %else and %endif. (And of course %if.)
245 COND_IF_TRUE
, COND_IF_FALSE
,
247 * These states come up after a %else: ELSE_TRUE means we're
248 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
249 * any %elif or %else will cause an error.
251 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
253 * This state means that we're not emitting now, and also that
254 * nothing until %endif will be emitted at all. It's for use in
255 * two circumstances: (i) when we've had our moment of emission
256 * and have now started seeing %elifs, and (ii) when the
257 * condition construct in question is contained within a
258 * non-emitting branch of a larger condition construct.
262 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
265 * These defines are used as the possible return values for do_directive
267 #define NO_DIRECTIVE_FOUND 0
268 #define DIRECTIVE_FOUND 1
271 * Condition codes. Note that we use c_ prefix not C_ because C_ is
272 * used in nasm.h for the "real" condition codes. At _this_ level,
273 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
274 * ones, so we need a different enum...
276 static const char * const conditions
[] = {
277 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
278 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
279 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
282 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
283 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
284 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
287 static const enum pp_conds inverse_ccs
[] = {
288 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
289 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
,
290 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
296 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
297 static int is_condition(enum preproc_token arg
)
299 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
302 /* For TASM compatibility we need to be able to recognise TASM compatible
303 * conditional compilation directives. Using the NASM pre-processor does
304 * not work, so we look for them specifically from the following list and
305 * then jam in the equivalent NASM directive into the input stream.
309 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
310 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
313 static const char * const tasm_directives
[] = {
314 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
315 "ifndef", "include", "local"
318 static int StackSize
= 4;
319 static char *StackPointer
= "ebp";
320 static int ArgOffset
= 8;
321 static int LocalOffset
= 0;
323 static Context
*cstk
;
324 static Include
*istk
;
325 static IncPath
*ipath
= NULL
;
327 static efunc _error
; /* Pointer to client-provided error reporting function */
328 static evalfunc evaluate
;
330 static int pass
; /* HACK: pass 0 = generate dependencies only */
331 static StrList
**dephead
, **deptail
; /* Dependency list */
333 static uint64_t unique
; /* unique identifier numbers */
335 static Line
*predef
= NULL
;
337 static ListGen
*list
;
340 * The current set of multi-line macros we have defined.
342 static struct hash_table mmacros
;
345 * The current set of single-line macros we have defined.
347 static struct hash_table smacros
;
350 * The multi-line macro we are currently defining, or the %rep
351 * block we are currently reading, if any.
353 static MMacro
*defining
;
356 * The number of macro parameters to allocate space for at a time.
358 #define PARAM_DELTA 16
361 * The standard macro set: defined in macros.c in the array nasm_stdmac.
362 * This gives our position in the macro set, when we're processing it.
364 static const char * const *stdmacpos
;
367 * The extra standard macros that come from the object format, if
370 static const char * const *extrastdmac
= NULL
;
371 bool any_extrastdmac
;
374 * Tokens are allocated in blocks to improve speed
376 #define TOKEN_BLOCKSIZE 4096
377 static Token
*freeTokens
= NULL
;
383 static Blocks blocks
= { NULL
, NULL
};
386 * Forward declarations.
388 static Token
*expand_mmac_params(Token
* tline
);
389 static Token
*expand_smacro(Token
* tline
);
390 static Token
*expand_id(Token
* tline
);
391 static Context
*get_ctx(char *name
, bool all_contexts
);
392 static void make_tok_num(Token
* tok
, int64_t val
);
393 static void error(int severity
, const char *fmt
, ...);
394 static void *new_Block(size_t size
);
395 static void delete_Blocks(void);
396 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
397 static Token
*delete_Token(Token
* t
);
400 * Macros for safe checking of token pointers, avoid *(NULL)
402 #define tok_type_(x,t) ((x) && (x)->type == (t))
403 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
404 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
405 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
407 /* Handle TASM specific directives, which do not contain a % in
408 * front of them. We do it here because I could not find any other
409 * place to do it for the moment, and it is a hack (ideally it would
410 * be nice to be able to use the NASM pre-processor to do it).
412 static char *check_tasm_directive(char *line
)
414 int32_t i
, j
, k
, m
, len
;
415 char *p
= line
, *oldline
, oldchar
;
417 /* Skip whitespace */
418 while (isspace(*p
) && *p
!= 0)
421 /* Binary search for the directive name */
423 j
= elements(tasm_directives
);
425 while (!isspace(p
[len
]) && p
[len
] != 0)
432 m
= nasm_stricmp(p
, tasm_directives
[k
]);
434 /* We have found a directive, so jam a % in front of it
435 * so that NASM will then recognise it as one if it's own.
440 line
= nasm_malloc(len
+ 2);
442 if (k
== TM_IFDIFI
) {
443 /* NASM does not recognise IFDIFI, so we convert it to
444 * %ifdef BOGUS. This is not used in NASM comaptible
445 * code, but does need to parse for the TASM macro
448 strcpy(line
+ 1, "ifdef BOGUS");
450 memcpy(line
+ 1, p
, len
+ 1);
465 * The pre-preprocessing stage... This function translates line
466 * number indications as they emerge from GNU cpp (`# lineno "file"
467 * flags') into NASM preprocessor line number indications (`%line
470 static char *prepreproc(char *line
)
473 char *fname
, *oldline
;
475 if (line
[0] == '#' && line
[1] == ' ') {
478 lineno
= atoi(fname
);
479 fname
+= strspn(fname
, "0123456789 ");
482 fnlen
= strcspn(fname
, "\"");
483 line
= nasm_malloc(20 + fnlen
);
484 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
487 if (tasm_compatible_mode
)
488 return check_tasm_directive(line
);
493 * Free a linked list of tokens.
495 static void free_tlist(Token
* list
)
498 list
= delete_Token(list
);
503 * Free a linked list of lines.
505 static void free_llist(Line
* list
)
511 free_tlist(l
->first
);
519 static void free_mmacro(MMacro
* m
)
522 free_tlist(m
->dlist
);
523 nasm_free(m
->defaults
);
524 free_llist(m
->expansion
);
529 * Free all currently defined macros, and free the hash tables
531 static void free_smacro_table(struct hash_table
*smt
)
535 struct hash_tbl_node
*it
= NULL
;
537 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
538 nasm_free((void *)key
);
540 SMacro
*ns
= s
->next
;
542 free_tlist(s
->expansion
);
550 static void free_mmacro_table(struct hash_table
*mmt
)
554 struct hash_tbl_node
*it
= NULL
;
557 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
558 nasm_free((void *)key
);
560 MMacro
*nm
= m
->next
;
568 static void free_macros(void)
570 free_smacro_table(&smacros
);
571 free_mmacro_table(&mmacros
);
575 * Initialize the hash tables
577 static void init_macros(void)
579 hash_init(&smacros
, HASH_LARGE
);
580 hash_init(&mmacros
, HASH_LARGE
);
584 * Pop the context stack.
586 static void ctx_pop(void)
591 free_smacro_table(&c
->localmac
);
597 * Search for a key in the hash index; adding it if necessary
598 * (in which case we initialize the data pointer to NULL.)
601 hash_findi_add(struct hash_table
*hash
, const char *str
)
603 struct hash_insert hi
;
607 r
= hash_findi(hash
, str
, &hi
);
611 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
612 return hash_add(&hi
, strx
, NULL
);
616 * Like hash_findi, but returns the data element rather than a pointer
617 * to it. Used only when not adding a new element, hence no third
621 hash_findix(struct hash_table
*hash
, const char *str
)
625 p
= hash_findi(hash
, str
, NULL
);
626 return p
? *p
: NULL
;
629 #define BUF_DELTA 512
631 * Read a line from the top file in istk, handling multiple CR/LFs
632 * at the end of the line read, and handling spurious ^Zs. Will
633 * return lines from the standard macro set if this has not already
636 static char *read_line(void)
638 char *buffer
, *p
, *q
;
639 int bufsize
, continued_count
;
643 char *ret
= nasm_strdup(*stdmacpos
++);
644 if (!*stdmacpos
&& any_extrastdmac
) {
645 stdmacpos
= extrastdmac
;
646 any_extrastdmac
= false;
650 * Nasty hack: here we push the contents of `predef' on
651 * to the top-level expansion stack, since this is the
652 * most convenient way to implement the pre-include and
653 * pre-define features.
657 Token
*head
, **tail
, *t
;
659 for (pd
= predef
; pd
; pd
= pd
->next
) {
662 for (t
= pd
->first
; t
; t
= t
->next
) {
663 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
664 tail
= &(*tail
)->next
;
666 l
= nasm_malloc(sizeof(Line
));
667 l
->next
= istk
->expansion
;
680 buffer
= nasm_malloc(BUF_DELTA
);
684 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
688 if (p
> buffer
&& p
[-1] == '\n') {
689 /* Convert backslash-CRLF line continuation sequences into
690 nothing at all (for DOS and Windows) */
691 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
696 /* Also convert backslash-LF line continuation sequences into
697 nothing at all (for Unix) */
698 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
706 if (p
- buffer
> bufsize
- 10) {
707 int32_t offset
= p
- buffer
;
708 bufsize
+= BUF_DELTA
;
709 buffer
= nasm_realloc(buffer
, bufsize
);
710 p
= buffer
+ offset
; /* prevent stale-pointer problems */
714 if (!q
&& p
== buffer
) {
719 src_set_linnum(src_get_linnum() + istk
->lineinc
+
720 (continued_count
* istk
->lineinc
));
723 * Play safe: remove CRs as well as LFs, if any of either are
724 * present at the end of the line.
726 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
730 * Handle spurious ^Z, which may be inserted into source files
731 * by some file transfer utilities.
733 buffer
[strcspn(buffer
, "\032")] = '\0';
735 list
->line(LIST_READ
, buffer
);
741 * Tokenize a line of text. This is a very simple process since we
742 * don't need to parse the value out of e.g. numeric tokens: we
743 * simply split one string into many.
745 static Token
*tokenize(char *line
)
748 enum pp_token_type type
;
750 Token
*t
, **tail
= &list
;
757 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
758 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
763 type
= TOK_PREPROC_ID
;
764 } else if (*p
== '{') {
766 while (*p
&& *p
!= '}') {
773 type
= TOK_PREPROC_ID
;
774 } else if (*p
== '?') {
775 type
= TOK_PREPROC_Q
; /* %? */
778 type
= TOK_PREPROC_QQ
; /* %?? */
781 } else if (isidchar(*p
) ||
782 ((*p
== '!' || *p
== '%' || *p
== '$') &&
787 while (isidchar(*p
));
788 type
= TOK_PREPROC_ID
;
794 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
797 while (*p
&& isidchar(*p
))
799 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
804 p
= nasm_skip_string(p
);
809 error(ERR_WARNING
, "unterminated string");
810 /* Handling unterminated strings by UNV */
813 } else if (isnumstart(*p
)) {
815 bool is_float
= false;
831 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
833 if (*p
== '+' || *p
== '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
839 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
841 } else if (c
== 'P' || c
== 'p') {
843 if (*p
== '+' || *p
== '-')
845 } else if (isnumchar(c
) || c
== '_')
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
860 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
861 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
862 (*r
== 'p' || *r
== 'P')) {
866 break; /* Terminate the token */
870 p
--; /* Point to first character beyond number */
872 if (has_e
&& !is_hex
) {
873 /* 1e13 is floating-point, but 1e13h is not */
877 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
878 } else if (isspace(*p
)) {
879 type
= TOK_WHITESPACE
;
881 while (*p
&& isspace(*p
))
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
888 if (!*p
|| *p
== ';') {
893 } else if (*p
== ';') {
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
902 * else is a single-character operator.
905 if ((p
[0] == '>' && p
[1] == '>') ||
906 (p
[0] == '<' && p
[1] == '<') ||
907 (p
[0] == '/' && p
[1] == '/') ||
908 (p
[0] == '<' && p
[1] == '=') ||
909 (p
[0] == '>' && p
[1] == '=') ||
910 (p
[0] == '=' && p
[1] == '=') ||
911 (p
[0] == '!' && p
[1] == '=') ||
912 (p
[0] == '<' && p
[1] == '>') ||
913 (p
[0] == '&' && p
[1] == '&') ||
914 (p
[0] == '|' && p
[1] == '|') ||
915 (p
[0] == '^' && p
[1] == '^')) {
921 /* Handling unterminated string by UNV */
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
929 if (type
!= TOK_COMMENT
) {
930 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
939 * this function allocates a new managed block of memory and
940 * returns a pointer to the block. The managed blocks are
941 * deleted only all at once by the delete_Blocks function.
943 static void *new_Block(size_t size
)
947 /* first, get to the end of the linked list */
950 /* now allocate the requested chunk */
951 b
->chunk
= nasm_malloc(size
);
953 /* now allocate a new block for the next request */
954 b
->next
= nasm_malloc(sizeof(Blocks
));
955 /* and initialize the contents of the new block */
956 b
->next
->next
= NULL
;
957 b
->next
->chunk
= NULL
;
962 * this function deletes all managed blocks of memory
964 static void delete_Blocks(void)
966 Blocks
*a
, *b
= &blocks
;
969 * keep in mind that the first block, pointed to by blocks
970 * is a static and not dynamically allocated, so we don't
984 * this function creates a new Token and passes a pointer to it
985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
988 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
989 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(txtlen
+1);
1011 memcpy(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
) {
1152 bq
= tline
->text
[0];
1153 l
= nasm_unquote(tline
->text
, &ep
);
1154 if (ep
[0] != bq
|| ep
[1] != '\0')
1158 return tokval
->t_type
= TOKEN_ERRNUM
;
1160 tokval
->t_integer
= readstrnum(tline
->text
, l
, &rn_warn
);
1162 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1163 tokval
->t_charptr
= NULL
;
1164 return tokval
->t_type
= TOKEN_NUM
;
1167 if (tline
->type
== TOK_OTHER
) {
1168 if (!strcmp(tline
->text
, "<<"))
1169 return tokval
->t_type
= TOKEN_SHL
;
1170 if (!strcmp(tline
->text
, ">>"))
1171 return tokval
->t_type
= TOKEN_SHR
;
1172 if (!strcmp(tline
->text
, "//"))
1173 return tokval
->t_type
= TOKEN_SDIV
;
1174 if (!strcmp(tline
->text
, "%%"))
1175 return tokval
->t_type
= TOKEN_SMOD
;
1176 if (!strcmp(tline
->text
, "=="))
1177 return tokval
->t_type
= TOKEN_EQ
;
1178 if (!strcmp(tline
->text
, "<>"))
1179 return tokval
->t_type
= TOKEN_NE
;
1180 if (!strcmp(tline
->text
, "!="))
1181 return tokval
->t_type
= TOKEN_NE
;
1182 if (!strcmp(tline
->text
, "<="))
1183 return tokval
->t_type
= TOKEN_LE
;
1184 if (!strcmp(tline
->text
, ">="))
1185 return tokval
->t_type
= TOKEN_GE
;
1186 if (!strcmp(tline
->text
, "&&"))
1187 return tokval
->t_type
= TOKEN_DBL_AND
;
1188 if (!strcmp(tline
->text
, "^^"))
1189 return tokval
->t_type
= TOKEN_DBL_XOR
;
1190 if (!strcmp(tline
->text
, "||"))
1191 return tokval
->t_type
= TOKEN_DBL_OR
;
1195 * We have no other options: just return the first character of
1198 return tokval
->t_type
= tline
->text
[0];
1202 * Compare a string to the name of an existing macro; this is a
1203 * simple wrapper which calls either strcmp or nasm_stricmp
1204 * depending on the value of the `casesense' parameter.
1206 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1208 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1212 * Compare a string to the name of an existing macro; this is a
1213 * simple wrapper which calls either strcmp or nasm_stricmp
1214 * depending on the value of the `casesense' parameter.
1216 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1218 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1222 * Return the Context structure associated with a %$ token. Return
1223 * NULL, having _already_ reported an error condition, if the
1224 * context stack isn't deep enough for the supplied number of $
1226 * If all_contexts == true, contexts that enclose current are
1227 * also scanned for such smacro, until it is found; if not -
1228 * only the context that directly results from the number of $'s
1229 * in variable's name.
1231 static Context
*get_ctx(char *name
, bool all_contexts
)
1237 if (!name
|| name
[0] != '%' || name
[1] != '$')
1241 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1245 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1247 /* i--; Lino - 02/25/02 */
1250 error(ERR_NONFATAL
, "`%s': context stack is only"
1251 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1258 /* Search for this smacro in found context */
1259 m
= hash_findix(&ctx
->localmac
, name
);
1261 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1272 * Check to see if a file is already in a string list
1274 static bool in_list(const StrList
*list
, const char *str
)
1277 if (!strcmp(list
->str
, str
))
1285 * Open an include file. This routine must always return a valid
1286 * file pointer if it returns - it's responsible for throwing an
1287 * ERR_FATAL and bombing out completely if not. It should also try
1288 * the include path one by one until it finds the file or reaches
1289 * the end of the path.
1291 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
**dtail
,
1296 IncPath
*ip
= ipath
;
1297 int len
= strlen(file
);
1298 size_t prefix_len
= 0;
1302 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1303 memcpy(sl
->str
, prefix
, prefix_len
);
1304 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1305 fp
= fopen(sl
->str
, "r");
1306 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1324 prefix_len
= strlen(prefix
);
1326 /* -MG given and file not found */
1327 if (dhead
&& !in_list(*dhead
, file
)) {
1328 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1330 strcpy(sl
->str
, file
);
1338 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1339 return NULL
; /* never reached - placate compilers */
1343 * Determine if we should warn on defining a single-line macro of
1344 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1345 * return true if _any_ single-line macro of that name is defined.
1346 * Otherwise, will return true if a single-line macro with either
1347 * `nparam' or no parameters is defined.
1349 * If a macro with precisely the right number of parameters is
1350 * defined, or nparam is -1, the address of the definition structure
1351 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1352 * is NULL, no action will be taken regarding its contents, and no
1355 * Note that this is also called with nparam zero to resolve
1358 * If you already know which context macro belongs to, you can pass
1359 * the context pointer as first parameter; if you won't but name begins
1360 * with %$ the context will be automatically computed. If all_contexts
1361 * is true, macro will be searched in outer contexts as well.
1364 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1367 struct hash_table
*smtbl
;
1371 smtbl
= &ctx
->localmac
;
1372 } else if (name
[0] == '%' && name
[1] == '$') {
1374 ctx
= get_ctx(name
, false);
1376 return false; /* got to return _something_ */
1377 smtbl
= &ctx
->localmac
;
1381 m
= (SMacro
*) hash_findix(smtbl
, name
);
1384 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1385 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1387 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1401 * Count and mark off the parameters in a multi-line macro call.
1402 * This is called both from within the multi-line macro expansion
1403 * code, and also to mark off the default parameters when provided
1404 * in a %macro definition line.
1406 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1408 int paramsize
, brace
;
1410 *nparam
= paramsize
= 0;
1413 if (*nparam
>= paramsize
) {
1414 paramsize
+= PARAM_DELTA
;
1415 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1419 if (tok_is_(t
, "{"))
1421 (*params
)[(*nparam
)++] = t
;
1422 while (tok_isnt_(t
, brace
? "}" : ","))
1424 if (t
) { /* got a comma/brace */
1428 * Now we've found the closing brace, look further
1432 if (tok_isnt_(t
, ",")) {
1434 "braces do not enclose all of macro parameter");
1435 while (tok_isnt_(t
, ","))
1439 t
= t
->next
; /* eat the comma */
1446 * Determine whether one of the various `if' conditions is true or
1449 * We must free the tline we get passed.
1451 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1453 enum pp_conditional i
= PP_COND(ct
);
1455 Token
*t
, *tt
, **tptr
, *origline
;
1456 struct tokenval tokval
;
1458 enum pp_token_type needtype
;
1464 j
= false; /* have we matched yet? */
1465 while (cstk
&& tline
) {
1467 if (!tline
|| tline
->type
!= TOK_ID
) {
1469 "`%s' expects context identifiers", pp_directives
[ct
]);
1470 free_tlist(origline
);
1473 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1475 tline
= tline
->next
;
1480 j
= false; /* have we matched yet? */
1483 if (!tline
|| (tline
->type
!= TOK_ID
&&
1484 (tline
->type
!= TOK_PREPROC_ID
||
1485 tline
->text
[1] != '$'))) {
1487 "`%s' expects macro identifiers", pp_directives
[ct
]);
1490 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1492 tline
= tline
->next
;
1498 tline
= expand_smacro(tline
);
1500 while (tok_isnt_(tt
, ","))
1504 "`%s' expects two comma-separated arguments",
1509 j
= true; /* assume equality unless proved not */
1510 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1511 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1512 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1516 if (t
->type
== TOK_WHITESPACE
) {
1520 if (tt
->type
== TOK_WHITESPACE
) {
1524 if (tt
->type
!= t
->type
) {
1525 j
= false; /* found mismatching tokens */
1528 /* When comparing strings, need to unquote them first */
1529 if (t
->type
== TOK_STRING
) {
1530 size_t l1
= nasm_unquote(t
->text
, NULL
);
1531 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1537 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1541 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1542 j
= false; /* found mismatching tokens */
1549 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1550 j
= false; /* trailing gunk on one end or other */
1556 MMacro searching
, *mmac
;
1558 tline
= tline
->next
;
1560 tline
= expand_id(tline
);
1561 if (!tok_type_(tline
, TOK_ID
)) {
1563 "`%s' expects a macro name", pp_directives
[ct
]);
1566 searching
.name
= nasm_strdup(tline
->text
);
1567 searching
.casesense
= true;
1568 searching
.plus
= false;
1569 searching
.nolist
= false;
1570 searching
.in_progress
= 0;
1571 searching
.rep_nest
= NULL
;
1572 searching
.nparam_min
= 0;
1573 searching
.nparam_max
= INT_MAX
;
1574 tline
= expand_smacro(tline
->next
);
1577 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1579 "`%s' expects a parameter count or nothing",
1582 searching
.nparam_min
= searching
.nparam_max
=
1583 readnum(tline
->text
, &j
);
1586 "unable to parse parameter count `%s'",
1589 if (tline
&& tok_is_(tline
->next
, "-")) {
1590 tline
= tline
->next
->next
;
1591 if (tok_is_(tline
, "*"))
1592 searching
.nparam_max
= INT_MAX
;
1593 else if (!tok_type_(tline
, TOK_NUMBER
))
1595 "`%s' expects a parameter count after `-'",
1598 searching
.nparam_max
= readnum(tline
->text
, &j
);
1601 "unable to parse parameter count `%s'",
1603 if (searching
.nparam_min
> searching
.nparam_max
)
1605 "minimum parameter count exceeds maximum");
1608 if (tline
&& tok_is_(tline
->next
, "+")) {
1609 tline
= tline
->next
;
1610 searching
.plus
= true;
1612 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1614 if (!strcmp(mmac
->name
, searching
.name
) &&
1615 (mmac
->nparam_min
<= searching
.nparam_max
1617 && (searching
.nparam_min
<= mmac
->nparam_max
1624 nasm_free(searching
.name
);
1633 needtype
= TOK_NUMBER
;
1636 needtype
= TOK_STRING
;
1640 t
= tline
= expand_smacro(tline
);
1642 while (tok_type_(t
, TOK_WHITESPACE
) ||
1643 (needtype
== TOK_NUMBER
&&
1644 tok_type_(t
, TOK_OTHER
) &&
1645 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1649 j
= tok_type_(t
, needtype
);
1653 t
= tline
= expand_smacro(tline
);
1654 while (tok_type_(t
, TOK_WHITESPACE
))
1659 t
= t
->next
; /* Skip the actual token */
1660 while (tok_type_(t
, TOK_WHITESPACE
))
1662 j
= !t
; /* Should be nothing left */
1667 t
= tline
= expand_smacro(tline
);
1668 while (tok_type_(t
, TOK_WHITESPACE
))
1671 j
= !t
; /* Should be empty */
1675 t
= tline
= expand_smacro(tline
);
1677 tokval
.t_type
= TOKEN_INVALID
;
1678 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1679 NULL
, pass
| CRITICAL
, error
, NULL
);
1684 "trailing garbage after expression ignored");
1685 if (!is_simple(evalresult
)) {
1687 "non-constant value given to `%s'", pp_directives
[ct
]);
1690 j
= reloc_value(evalresult
) != 0;
1695 "preprocessor directive `%s' not yet implemented",
1700 free_tlist(origline
);
1701 return j
^ PP_NEGATIVE(ct
);
1704 free_tlist(origline
);
1709 * Expand macros in a string. Used in %error directives (and it should
1710 * almost certainly be removed from there, too.)
1712 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1713 * The returned variable should ALWAYS be freed after usage.
1715 void expand_macros_in_string(char **p
)
1717 Token
*line
= tokenize(*p
);
1718 line
= expand_smacro(line
);
1719 *p
= detoken(line
, false);
1723 * Common code for defining an smacro
1725 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1726 int nparam
, Token
*expansion
)
1728 SMacro
*smac
, **smhead
;
1729 struct hash_table
*smtbl
;
1731 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1734 "single-line macro `%s' defined both with and"
1735 " without parameters", mname
);
1737 /* Some instances of the old code considered this a failure,
1738 some others didn't. What is the right thing to do here? */
1739 free_tlist(expansion
);
1740 return false; /* Failure */
1743 * We're redefining, so we have to take over an
1744 * existing SMacro structure. This means freeing
1745 * what was already in it.
1747 nasm_free(smac
->name
);
1748 free_tlist(smac
->expansion
);
1751 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1752 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1753 smac
= nasm_malloc(sizeof(SMacro
));
1754 smac
->next
= *smhead
;
1757 smac
->name
= nasm_strdup(mname
);
1758 smac
->casesense
= casesense
;
1759 smac
->nparam
= nparam
;
1760 smac
->expansion
= expansion
;
1761 smac
->in_progress
= false;
1762 return true; /* Success */
1766 * Undefine an smacro
1768 static void undef_smacro(Context
*ctx
, const char *mname
)
1770 SMacro
**smhead
, *s
, **sp
;
1771 struct hash_table
*smtbl
;
1773 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1774 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1778 * We now have a macro name... go hunt for it.
1781 while ((s
= *sp
) != NULL
) {
1782 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1785 free_tlist(s
->expansion
);
1795 * Decode a size directive
1797 static int parse_size(const char *str
) {
1798 static const char *size_names
[] =
1799 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1800 static const int sizes
[] =
1801 { 0, 1, 4, 16, 8, 10, 2, 32 };
1803 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1807 * find and process preprocessor directive in passed line
1808 * Find out if a line contains a preprocessor directive, and deal
1811 * If a directive _is_ found, it is the responsibility of this routine
1812 * (and not the caller) to free_tlist() the line.
1814 * @param tline a pointer to the current tokeninzed line linked list
1815 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1818 static int do_directive(Token
* tline
)
1820 enum preproc_token i
;
1832 MMacro
*mmac
, **mmhead
;
1833 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1835 struct tokenval tokval
;
1837 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1843 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1844 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1845 || tline
->text
[1] == '!'))
1846 return NO_DIRECTIVE_FOUND
;
1848 i
= pp_token_hash(tline
->text
);
1851 * If we're in a non-emitting branch of a condition construct,
1852 * or walking to the end of an already terminated %rep block,
1853 * we should ignore all directives except for condition
1856 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1857 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1858 return NO_DIRECTIVE_FOUND
;
1862 * If we're defining a macro or reading a %rep block, we should
1863 * ignore all directives except for %macro/%imacro (which
1864 * generate an error), %endm/%endmacro, and (only if we're in a
1865 * %rep block) %endrep. If we're in a %rep block, another %rep
1866 * causes an error, so should be let through.
1868 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1869 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1870 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1871 return NO_DIRECTIVE_FOUND
;
1876 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1878 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1881 /* Directive to tell NASM what the default stack size is. The
1882 * default is for a 16-bit stack, and this can be overriden with
1884 * the following form:
1886 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1888 tline
= tline
->next
;
1889 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1890 tline
= tline
->next
;
1891 if (!tline
|| tline
->type
!= TOK_ID
) {
1892 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1893 free_tlist(origline
);
1894 return DIRECTIVE_FOUND
;
1896 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1897 /* All subsequent ARG directives are for a 32-bit stack */
1899 StackPointer
= "ebp";
1902 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1903 /* All subsequent ARG directives are for a 64-bit stack */
1905 StackPointer
= "rbp";
1908 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1909 /* All subsequent ARG directives are for a 16-bit stack,
1910 * far function call.
1913 StackPointer
= "bp";
1916 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1917 /* All subsequent ARG directives are for a 16-bit stack,
1918 * far function call. We don't support near functions.
1921 StackPointer
= "bp";
1925 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1926 free_tlist(origline
);
1927 return DIRECTIVE_FOUND
;
1929 free_tlist(origline
);
1930 return DIRECTIVE_FOUND
;
1933 /* TASM like ARG directive to define arguments to functions, in
1934 * the following form:
1936 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1940 char *arg
, directive
[256];
1941 int size
= StackSize
;
1943 /* Find the argument name */
1944 tline
= tline
->next
;
1945 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1946 tline
= tline
->next
;
1947 if (!tline
|| tline
->type
!= TOK_ID
) {
1948 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1949 free_tlist(origline
);
1950 return DIRECTIVE_FOUND
;
1954 /* Find the argument size type */
1955 tline
= tline
->next
;
1956 if (!tline
|| tline
->type
!= TOK_OTHER
1957 || tline
->text
[0] != ':') {
1959 "Syntax error processing `%%arg' directive");
1960 free_tlist(origline
);
1961 return DIRECTIVE_FOUND
;
1963 tline
= tline
->next
;
1964 if (!tline
|| tline
->type
!= TOK_ID
) {
1965 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1966 free_tlist(origline
);
1967 return DIRECTIVE_FOUND
;
1970 /* Allow macro expansion of type parameter */
1971 tt
= tokenize(tline
->text
);
1972 tt
= expand_smacro(tt
);
1973 size
= parse_size(tt
->text
);
1976 "Invalid size type for `%%arg' missing directive");
1978 free_tlist(origline
);
1979 return DIRECTIVE_FOUND
;
1983 /* Round up to even stack slots */
1984 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1986 /* Now define the macro for the argument */
1987 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1988 arg
, StackPointer
, offset
);
1989 do_directive(tokenize(directive
));
1992 /* Move to the next argument in the list */
1993 tline
= tline
->next
;
1994 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1995 tline
= tline
->next
;
1996 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1998 free_tlist(origline
);
1999 return DIRECTIVE_FOUND
;
2002 /* TASM like LOCAL directive to define local variables for a
2003 * function, in the following form:
2005 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2007 * The '= LocalSize' at the end is ignored by NASM, but is
2008 * required by TASM to define the local parameter size (and used
2009 * by the TASM macro package).
2011 offset
= LocalOffset
;
2013 char *local
, directive
[256];
2014 int size
= StackSize
;
2016 /* Find the argument name */
2017 tline
= tline
->next
;
2018 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2019 tline
= tline
->next
;
2020 if (!tline
|| tline
->type
!= TOK_ID
) {
2022 "`%%local' missing argument parameter");
2023 free_tlist(origline
);
2024 return DIRECTIVE_FOUND
;
2026 local
= tline
->text
;
2028 /* Find the argument size type */
2029 tline
= tline
->next
;
2030 if (!tline
|| tline
->type
!= TOK_OTHER
2031 || tline
->text
[0] != ':') {
2033 "Syntax error processing `%%local' directive");
2034 free_tlist(origline
);
2035 return DIRECTIVE_FOUND
;
2037 tline
= tline
->next
;
2038 if (!tline
|| tline
->type
!= TOK_ID
) {
2040 "`%%local' missing size type parameter");
2041 free_tlist(origline
);
2042 return DIRECTIVE_FOUND
;
2045 /* Allow macro expansion of type parameter */
2046 tt
= tokenize(tline
->text
);
2047 tt
= expand_smacro(tt
);
2048 size
= parse_size(tt
->text
);
2051 "Invalid size type for `%%local' missing directive");
2053 free_tlist(origline
);
2054 return DIRECTIVE_FOUND
;
2058 /* Round up to even stack slots */
2059 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2061 offset
+= size
; /* Negative offset, increment before */
2063 /* Now define the macro for the argument */
2064 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2065 local
, StackPointer
, offset
);
2066 do_directive(tokenize(directive
));
2068 /* Now define the assign to setup the enter_c macro correctly */
2069 snprintf(directive
, sizeof(directive
),
2070 "%%assign %%$localsize %%$localsize+%d", size
);
2071 do_directive(tokenize(directive
));
2073 /* Move to the next argument in the list */
2074 tline
= tline
->next
;
2075 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2076 tline
= tline
->next
;
2077 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2078 LocalOffset
= offset
;
2079 free_tlist(origline
);
2080 return DIRECTIVE_FOUND
;
2084 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2087 free_tlist(origline
);
2088 return DIRECTIVE_FOUND
;
2091 t
= tline
->next
= expand_smacro(tline
->next
);
2093 if (!t
|| (t
->type
!= TOK_STRING
&&
2094 t
->type
!= TOK_INTERNAL_STRING
)) {
2095 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2097 free_tlist(origline
);
2098 return DIRECTIVE_FOUND
; /* but we did _something_ */
2102 "trailing garbage after `%%depend' ignored");
2104 if (t
->type
!= TOK_INTERNAL_STRING
)
2105 nasm_unquote(p
, NULL
);
2106 if (dephead
&& !in_list(*dephead
, p
)) {
2107 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2111 deptail
= &sl
->next
;
2114 free_tlist(origline
);
2115 return DIRECTIVE_FOUND
;
2118 t
= tline
->next
= expand_smacro(tline
->next
);
2121 if (!t
|| (t
->type
!= TOK_STRING
&&
2122 t
->type
!= TOK_INTERNAL_STRING
)) {
2123 error(ERR_NONFATAL
, "`%%include' expects a file name");
2124 free_tlist(origline
);
2125 return DIRECTIVE_FOUND
; /* but we did _something_ */
2129 "trailing garbage after `%%include' ignored");
2131 if (t
->type
!= TOK_INTERNAL_STRING
)
2132 nasm_unquote(p
, NULL
);
2133 inc
= nasm_malloc(sizeof(Include
));
2136 inc
->fp
= inc_fopen(p
, dephead
, deptail
, pass
== 0);
2138 /* -MG given but file not found */
2141 inc
->fname
= src_set_fname(nasm_strdup(p
));
2142 inc
->lineno
= src_set_linnum(0);
2144 inc
->expansion
= NULL
;
2147 list
->uplevel(LIST_INCLUDE
);
2149 free_tlist(origline
);
2150 return DIRECTIVE_FOUND
;
2153 tline
= tline
->next
;
2155 tline
= expand_id(tline
);
2156 if (!tok_type_(tline
, TOK_ID
)) {
2157 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2158 free_tlist(origline
);
2159 return DIRECTIVE_FOUND
; /* but we did _something_ */
2162 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2163 ctx
= nasm_malloc(sizeof(Context
));
2165 hash_init(&ctx
->localmac
, HASH_SMALL
);
2166 ctx
->name
= nasm_strdup(tline
->text
);
2167 ctx
->number
= unique
++;
2169 free_tlist(origline
);
2173 tline
= tline
->next
;
2175 tline
= expand_id(tline
);
2176 if (!tok_type_(tline
, TOK_ID
)) {
2177 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2178 free_tlist(origline
);
2179 return DIRECTIVE_FOUND
; /* but we did _something_ */
2182 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2184 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2186 nasm_free(cstk
->name
);
2187 cstk
->name
= nasm_strdup(tline
->text
);
2189 free_tlist(origline
);
2194 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2196 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2199 free_tlist(origline
);
2203 tline
->next
= expand_smacro(tline
->next
);
2204 tline
= tline
->next
;
2206 if (tok_type_(tline
, TOK_STRING
)) {
2208 nasm_unquote(p
, NULL
);
2209 expand_macros_in_string(&p
); /* WHY? */
2210 error(ERR_NONFATAL
, "%s", p
);
2213 p
= detoken(tline
, false);
2214 error(ERR_WARNING
, "%s", p
); /* WARNING!??!! */
2217 free_tlist(origline
);
2221 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2224 j
= if_condition(tline
->next
, i
);
2225 tline
->next
= NULL
; /* it got freed */
2226 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2228 cond
= nasm_malloc(sizeof(Cond
));
2229 cond
->next
= istk
->conds
;
2232 free_tlist(origline
);
2233 return DIRECTIVE_FOUND
;
2237 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2238 if (emitting(istk
->conds
->state
)
2239 || istk
->conds
->state
== COND_NEVER
)
2240 istk
->conds
->state
= COND_NEVER
;
2243 * IMPORTANT: In the case of %if, we will already have
2244 * called expand_mmac_params(); however, if we're
2245 * processing an %elif we must have been in a
2246 * non-emitting mode, which would have inhibited
2247 * the normal invocation of expand_mmac_params(). Therefore,
2248 * we have to do it explicitly here.
2250 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2251 tline
->next
= NULL
; /* it got freed */
2252 istk
->conds
->state
=
2253 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2255 free_tlist(origline
);
2256 return DIRECTIVE_FOUND
;
2260 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2262 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2263 if (emitting(istk
->conds
->state
)
2264 || istk
->conds
->state
== COND_NEVER
)
2265 istk
->conds
->state
= COND_ELSE_FALSE
;
2267 istk
->conds
->state
= COND_ELSE_TRUE
;
2268 free_tlist(origline
);
2269 return DIRECTIVE_FOUND
;
2273 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2275 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2277 istk
->conds
= cond
->next
;
2279 free_tlist(origline
);
2280 return DIRECTIVE_FOUND
;
2286 "`%%%smacro': already defining a macro",
2287 (i
== PP_IMACRO
? "i" : ""));
2288 tline
= tline
->next
;
2290 tline
= expand_id(tline
);
2291 if (!tok_type_(tline
, TOK_ID
)) {
2293 "`%%%smacro' expects a macro name",
2294 (i
== PP_IMACRO
? "i" : ""));
2295 return DIRECTIVE_FOUND
;
2297 defining
= nasm_malloc(sizeof(MMacro
));
2298 defining
->name
= nasm_strdup(tline
->text
);
2299 defining
->casesense
= (i
== PP_MACRO
);
2300 defining
->plus
= false;
2301 defining
->nolist
= false;
2302 defining
->in_progress
= 0;
2303 defining
->rep_nest
= NULL
;
2304 tline
= expand_smacro(tline
->next
);
2306 if (!tok_type_(tline
, TOK_NUMBER
)) {
2308 "`%%%smacro' expects a parameter count",
2309 (i
== PP_IMACRO
? "i" : ""));
2310 defining
->nparam_min
= defining
->nparam_max
= 0;
2312 defining
->nparam_min
= defining
->nparam_max
=
2313 readnum(tline
->text
, &err
);
2316 "unable to parse parameter count `%s'", tline
->text
);
2318 if (tline
&& tok_is_(tline
->next
, "-")) {
2319 tline
= tline
->next
->next
;
2320 if (tok_is_(tline
, "*"))
2321 defining
->nparam_max
= INT_MAX
;
2322 else if (!tok_type_(tline
, TOK_NUMBER
))
2324 "`%%%smacro' expects a parameter count after `-'",
2325 (i
== PP_IMACRO
? "i" : ""));
2327 defining
->nparam_max
= readnum(tline
->text
, &err
);
2330 "unable to parse parameter count `%s'",
2332 if (defining
->nparam_min
> defining
->nparam_max
)
2334 "minimum parameter count exceeds maximum");
2337 if (tline
&& tok_is_(tline
->next
, "+")) {
2338 tline
= tline
->next
;
2339 defining
->plus
= true;
2341 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2342 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2343 tline
= tline
->next
;
2344 defining
->nolist
= true;
2346 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2348 if (!strcmp(mmac
->name
, defining
->name
) &&
2349 (mmac
->nparam_min
<= defining
->nparam_max
2351 && (defining
->nparam_min
<= mmac
->nparam_max
2354 "redefining multi-line macro `%s'", defining
->name
);
2360 * Handle default parameters.
2362 if (tline
&& tline
->next
) {
2363 defining
->dlist
= tline
->next
;
2365 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2366 &defining
->defaults
);
2368 defining
->dlist
= NULL
;
2369 defining
->defaults
= NULL
;
2371 defining
->expansion
= NULL
;
2372 free_tlist(origline
);
2373 return DIRECTIVE_FOUND
;
2378 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2379 return DIRECTIVE_FOUND
;
2381 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2382 defining
->next
= *mmhead
;
2385 free_tlist(origline
);
2386 return DIRECTIVE_FOUND
;
2389 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2390 tline
= tline
->next
;
2391 if (tline
->next
== NULL
) {
2392 free_tlist(origline
);
2393 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2394 return DIRECTIVE_FOUND
;
2396 t
= expand_smacro(tline
->next
);
2398 free_tlist(origline
);
2401 tokval
.t_type
= TOKEN_INVALID
;
2403 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2406 return DIRECTIVE_FOUND
;
2409 "trailing garbage after expression ignored");
2410 if (!is_simple(evalresult
)) {
2411 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2412 return DIRECTIVE_FOUND
;
2415 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2416 mmac
= mmac
->next_active
;
2418 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2419 } else if (mmac
->nparam
== 0) {
2421 "`%%rotate' invoked within macro without parameters");
2423 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2425 rotate
%= (int)mmac
->nparam
;
2427 rotate
+= mmac
->nparam
;
2429 mmac
->rotate
= rotate
;
2431 return DIRECTIVE_FOUND
;
2436 tline
= tline
->next
;
2437 } while (tok_type_(tline
, TOK_WHITESPACE
));
2439 if (tok_type_(tline
, TOK_ID
) &&
2440 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2443 tline
= tline
->next
;
2444 } while (tok_type_(tline
, TOK_WHITESPACE
));
2448 t
= expand_smacro(tline
);
2450 tokval
.t_type
= TOKEN_INVALID
;
2452 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2454 free_tlist(origline
);
2455 return DIRECTIVE_FOUND
;
2459 "trailing garbage after expression ignored");
2460 if (!is_simple(evalresult
)) {
2461 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2462 return DIRECTIVE_FOUND
;
2464 count
= reloc_value(evalresult
) + 1;
2466 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2469 free_tlist(origline
);
2471 tmp_defining
= defining
;
2472 defining
= nasm_malloc(sizeof(MMacro
));
2473 defining
->name
= NULL
; /* flags this macro as a %rep block */
2474 defining
->casesense
= false;
2475 defining
->plus
= false;
2476 defining
->nolist
= nolist
;
2477 defining
->in_progress
= count
;
2478 defining
->nparam_min
= defining
->nparam_max
= 0;
2479 defining
->defaults
= NULL
;
2480 defining
->dlist
= NULL
;
2481 defining
->expansion
= NULL
;
2482 defining
->next_active
= istk
->mstk
;
2483 defining
->rep_nest
= tmp_defining
;
2484 return DIRECTIVE_FOUND
;
2487 if (!defining
|| defining
->name
) {
2488 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2489 return DIRECTIVE_FOUND
;
2493 * Now we have a "macro" defined - although it has no name
2494 * and we won't be entering it in the hash tables - we must
2495 * push a macro-end marker for it on to istk->expansion.
2496 * After that, it will take care of propagating itself (a
2497 * macro-end marker line for a macro which is really a %rep
2498 * block will cause the macro to be re-expanded, complete
2499 * with another macro-end marker to ensure the process
2500 * continues) until the whole expansion is forcibly removed
2501 * from istk->expansion by a %exitrep.
2503 l
= nasm_malloc(sizeof(Line
));
2504 l
->next
= istk
->expansion
;
2505 l
->finishes
= defining
;
2507 istk
->expansion
= l
;
2509 istk
->mstk
= defining
;
2511 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2512 tmp_defining
= defining
;
2513 defining
= defining
->rep_nest
;
2514 free_tlist(origline
);
2515 return DIRECTIVE_FOUND
;
2519 * We must search along istk->expansion until we hit a
2520 * macro-end marker for a macro with no name. Then we set
2521 * its `in_progress' flag to 0.
2523 for (l
= istk
->expansion
; l
; l
= l
->next
)
2524 if (l
->finishes
&& !l
->finishes
->name
)
2528 l
->finishes
->in_progress
= 0;
2530 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2531 free_tlist(origline
);
2532 return DIRECTIVE_FOUND
;
2538 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2540 tline
= tline
->next
;
2542 tline
= expand_id(tline
);
2543 if (!tline
|| (tline
->type
!= TOK_ID
&&
2544 (tline
->type
!= TOK_PREPROC_ID
||
2545 tline
->text
[1] != '$'))) {
2546 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2548 free_tlist(origline
);
2549 return DIRECTIVE_FOUND
;
2552 ctx
= get_ctx(tline
->text
, false);
2554 mname
= tline
->text
;
2556 param_start
= tline
= tline
->next
;
2559 /* Expand the macro definition now for %xdefine and %ixdefine */
2560 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2561 tline
= expand_smacro(tline
);
2563 if (tok_is_(tline
, "(")) {
2565 * This macro has parameters.
2568 tline
= tline
->next
;
2572 error(ERR_NONFATAL
, "parameter identifier expected");
2573 free_tlist(origline
);
2574 return DIRECTIVE_FOUND
;
2576 if (tline
->type
!= TOK_ID
) {
2578 "`%s': parameter identifier expected",
2580 free_tlist(origline
);
2581 return DIRECTIVE_FOUND
;
2583 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2584 tline
= tline
->next
;
2586 if (tok_is_(tline
, ",")) {
2587 tline
= tline
->next
;
2590 if (!tok_is_(tline
, ")")) {
2592 "`)' expected to terminate macro template");
2593 free_tlist(origline
);
2594 return DIRECTIVE_FOUND
;
2599 tline
= tline
->next
;
2601 if (tok_type_(tline
, TOK_WHITESPACE
))
2602 last
= tline
, tline
= tline
->next
;
2607 if (t
->type
== TOK_ID
) {
2608 for (tt
= param_start
; tt
; tt
= tt
->next
)
2609 if (tt
->type
>= TOK_SMAC_PARAM
&&
2610 !strcmp(tt
->text
, t
->text
))
2614 t
->next
= macro_start
;
2619 * Good. We now have a macro name, a parameter count, and a
2620 * token list (in reverse order) for an expansion. We ought
2621 * to be OK just to create an SMacro, store it, and let
2622 * free_tlist have the rest of the line (which we have
2623 * carefully re-terminated after chopping off the expansion
2626 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2627 free_tlist(origline
);
2628 return DIRECTIVE_FOUND
;
2631 tline
= tline
->next
;
2633 tline
= expand_id(tline
);
2634 if (!tline
|| (tline
->type
!= TOK_ID
&&
2635 (tline
->type
!= TOK_PREPROC_ID
||
2636 tline
->text
[1] != '$'))) {
2637 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2638 free_tlist(origline
);
2639 return DIRECTIVE_FOUND
;
2643 "trailing garbage after macro name ignored");
2646 /* Find the context that symbol belongs to */
2647 ctx
= get_ctx(tline
->text
, false);
2648 undef_smacro(ctx
, tline
->text
);
2649 free_tlist(origline
);
2650 return DIRECTIVE_FOUND
;
2655 StrList
*xsl
= NULL
;
2659 tline
= tline
->next
;
2661 tline
= expand_id(tline
);
2662 if (!tline
|| (tline
->type
!= TOK_ID
&&
2663 (tline
->type
!= TOK_PREPROC_ID
||
2664 tline
->text
[1] != '$'))) {
2666 "`%%pathsearch' expects a macro identifier as first parameter");
2667 free_tlist(origline
);
2668 return DIRECTIVE_FOUND
;
2670 ctx
= get_ctx(tline
->text
, false);
2672 mname
= tline
->text
;
2674 tline
= expand_smacro(tline
->next
);
2678 while (tok_type_(t
, TOK_WHITESPACE
))
2681 if (!t
|| (t
->type
!= TOK_STRING
&&
2682 t
->type
!= TOK_INTERNAL_STRING
)) {
2683 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2685 free_tlist(origline
);
2686 return DIRECTIVE_FOUND
; /* but we did _something_ */
2690 "trailing garbage after `%%pathsearch' ignored");
2692 if (t
->type
!= TOK_INTERNAL_STRING
)
2693 nasm_unquote(p
, NULL
);
2695 fp
= inc_fopen(p
, &xsl
, &xsl
, true);
2698 fclose(fp
); /* Don't actually care about the file */
2700 macro_start
= nasm_malloc(sizeof(*macro_start
));
2701 macro_start
->next
= NULL
;
2702 macro_start
->text
= nasm_quote(p
, strlen(p
));
2703 macro_start
->type
= TOK_STRING
;
2704 macro_start
->mac
= NULL
;
2709 * We now have a macro name, an implicit parameter count of
2710 * zero, and a string token to use as an expansion. Create
2711 * and store an SMacro.
2713 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2715 free_tlist(origline
);
2716 return DIRECTIVE_FOUND
;
2722 tline
= tline
->next
;
2724 tline
= expand_id(tline
);
2725 if (!tline
|| (tline
->type
!= TOK_ID
&&
2726 (tline
->type
!= TOK_PREPROC_ID
||
2727 tline
->text
[1] != '$'))) {
2729 "`%%strlen' expects a macro identifier as first parameter");
2730 free_tlist(origline
);
2731 return DIRECTIVE_FOUND
;
2733 ctx
= get_ctx(tline
->text
, false);
2735 mname
= tline
->text
;
2737 tline
= expand_smacro(tline
->next
);
2741 while (tok_type_(t
, TOK_WHITESPACE
))
2743 /* t should now point to the string */
2744 if (t
->type
!= TOK_STRING
) {
2746 "`%%strlen` requires string as second parameter");
2748 free_tlist(origline
);
2749 return DIRECTIVE_FOUND
;
2752 macro_start
= nasm_malloc(sizeof(*macro_start
));
2753 macro_start
->next
= NULL
;
2754 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2755 macro_start
->mac
= NULL
;
2758 * We now have a macro name, an implicit parameter count of
2759 * zero, and a numeric token to use as an expansion. Create
2760 * and store an SMacro.
2762 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2764 free_tlist(origline
);
2765 return DIRECTIVE_FOUND
;
2774 tline
= tline
->next
;
2776 tline
= expand_id(tline
);
2777 if (!tline
|| (tline
->type
!= TOK_ID
&&
2778 (tline
->type
!= TOK_PREPROC_ID
||
2779 tline
->text
[1] != '$'))) {
2781 "`%%substr' expects a macro identifier as first parameter");
2782 free_tlist(origline
);
2783 return DIRECTIVE_FOUND
;
2785 ctx
= get_ctx(tline
->text
, false);
2787 mname
= tline
->text
;
2789 tline
= expand_smacro(tline
->next
);
2793 while (tok_type_(t
, TOK_WHITESPACE
))
2796 /* t should now point to the string */
2797 if (t
->type
!= TOK_STRING
) {
2799 "`%%substr` requires string as second parameter");
2801 free_tlist(origline
);
2802 return DIRECTIVE_FOUND
;
2807 tokval
.t_type
= TOKEN_INVALID
;
2808 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2812 free_tlist(origline
);
2813 return DIRECTIVE_FOUND
;
2814 } else if (!is_simple(evalresult
)) {
2815 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2817 free_tlist(origline
);
2818 return DIRECTIVE_FOUND
;
2820 a1
= evalresult
->value
-1;
2822 while (tok_type_(tt
, TOK_WHITESPACE
))
2825 a2
= 1; /* Backwards compatibility: one character */
2827 tokval
.t_type
= TOKEN_INVALID
;
2828 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2832 free_tlist(origline
);
2833 return DIRECTIVE_FOUND
;
2834 } else if (!is_simple(evalresult
)) {
2835 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2837 free_tlist(origline
);
2838 return DIRECTIVE_FOUND
;
2840 a2
= evalresult
->value
;
2843 len
= nasm_unquote(t
->text
, NULL
);
2846 if (a1
+a2
> (int64_t)len
)
2849 macro_start
= nasm_malloc(sizeof(*macro_start
));
2850 macro_start
->next
= NULL
;
2851 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
2852 macro_start
->type
= TOK_STRING
;
2853 macro_start
->mac
= NULL
;
2856 * We now have a macro name, an implicit parameter count of
2857 * zero, and a numeric token to use as an expansion. Create
2858 * and store an SMacro.
2860 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2862 free_tlist(origline
);
2863 return DIRECTIVE_FOUND
;
2868 casesense
= (i
== PP_ASSIGN
);
2870 tline
= tline
->next
;
2872 tline
= expand_id(tline
);
2873 if (!tline
|| (tline
->type
!= TOK_ID
&&
2874 (tline
->type
!= TOK_PREPROC_ID
||
2875 tline
->text
[1] != '$'))) {
2877 "`%%%sassign' expects a macro identifier",
2878 (i
== PP_IASSIGN
? "i" : ""));
2879 free_tlist(origline
);
2880 return DIRECTIVE_FOUND
;
2882 ctx
= get_ctx(tline
->text
, false);
2884 mname
= tline
->text
;
2886 tline
= expand_smacro(tline
->next
);
2891 tokval
.t_type
= TOKEN_INVALID
;
2893 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2896 free_tlist(origline
);
2897 return DIRECTIVE_FOUND
;
2902 "trailing garbage after expression ignored");
2904 if (!is_simple(evalresult
)) {
2906 "non-constant value given to `%%%sassign'",
2907 (i
== PP_IASSIGN
? "i" : ""));
2908 free_tlist(origline
);
2909 return DIRECTIVE_FOUND
;
2912 macro_start
= nasm_malloc(sizeof(*macro_start
));
2913 macro_start
->next
= NULL
;
2914 make_tok_num(macro_start
, reloc_value(evalresult
));
2915 macro_start
->mac
= NULL
;
2918 * We now have a macro name, an implicit parameter count of
2919 * zero, and a numeric token to use as an expansion. Create
2920 * and store an SMacro.
2922 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2923 free_tlist(origline
);
2924 return DIRECTIVE_FOUND
;
2928 * Syntax is `%line nnn[+mmm] [filename]'
2930 tline
= tline
->next
;
2932 if (!tok_type_(tline
, TOK_NUMBER
)) {
2933 error(ERR_NONFATAL
, "`%%line' expects line number");
2934 free_tlist(origline
);
2935 return DIRECTIVE_FOUND
;
2937 k
= readnum(tline
->text
, &err
);
2939 tline
= tline
->next
;
2940 if (tok_is_(tline
, "+")) {
2941 tline
= tline
->next
;
2942 if (!tok_type_(tline
, TOK_NUMBER
)) {
2943 error(ERR_NONFATAL
, "`%%line' expects line increment");
2944 free_tlist(origline
);
2945 return DIRECTIVE_FOUND
;
2947 m
= readnum(tline
->text
, &err
);
2948 tline
= tline
->next
;
2954 nasm_free(src_set_fname(detoken(tline
, false)));
2956 free_tlist(origline
);
2957 return DIRECTIVE_FOUND
;
2961 "preprocessor directive `%s' not yet implemented",
2965 return DIRECTIVE_FOUND
;
2969 * Ensure that a macro parameter contains a condition code and
2970 * nothing else. Return the condition code index if so, or -1
2973 static int find_cc(Token
* t
)
2979 return -1; /* Probably a %+ without a space */
2982 if (t
->type
!= TOK_ID
)
2986 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2990 j
= elements(conditions
);
2993 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3009 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3010 * %-n) and MMacro-local identifiers (%%foo).
3012 static Token
*expand_mmac_params(Token
* tline
)
3014 Token
*t
, *tt
, **tail
, *thead
;
3020 if (tline
->type
== TOK_PREPROC_ID
&&
3021 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3022 && tline
->text
[2]) || tline
->text
[1] == '%'
3023 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3025 int type
= 0, cc
; /* type = 0 to placate optimisers */
3032 tline
= tline
->next
;
3035 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3036 mac
= mac
->next_active
;
3038 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3040 switch (t
->text
[1]) {
3042 * We have to make a substitution of one of the
3043 * forms %1, %-1, %+1, %%foo, %0.
3047 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3048 text
= nasm_strdup(tmpbuf
);
3052 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3054 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3057 n
= atoi(t
->text
+ 2) - 1;
3058 if (n
>= mac
->nparam
)
3061 if (mac
->nparam
> 1)
3062 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3063 tt
= mac
->params
[n
];
3068 "macro parameter %d is not a condition code",
3073 if (inverse_ccs
[cc
] == -1) {
3075 "condition code `%s' is not invertible",
3080 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3084 n
= atoi(t
->text
+ 2) - 1;
3085 if (n
>= mac
->nparam
)
3088 if (mac
->nparam
> 1)
3089 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3090 tt
= mac
->params
[n
];
3095 "macro parameter %d is not a condition code",
3100 text
= nasm_strdup(conditions
[cc
]);
3104 n
= atoi(t
->text
+ 1) - 1;
3105 if (n
>= mac
->nparam
)
3108 if (mac
->nparam
> 1)
3109 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3110 tt
= mac
->params
[n
];
3113 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3114 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3115 tail
= &(*tail
)->next
;
3119 text
= NULL
; /* we've done it here */
3135 tline
= tline
->next
;
3142 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3144 case TOK_WHITESPACE
:
3145 if (tt
->type
== TOK_WHITESPACE
) {
3146 t
->next
= delete_Token(tt
);
3150 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3151 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3154 t
->next
= delete_Token(tt
);
3158 if (tt
->type
== TOK_NUMBER
) {
3159 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3162 t
->next
= delete_Token(tt
);
3173 * Expand all single-line macro calls made in the given line.
3174 * Return the expanded version of the line. The original is deemed
3175 * to be destroyed in the process. (In reality we'll just move
3176 * Tokens from input to output a lot of the time, rather than
3177 * actually bothering to destroy and replicate.)
3179 #define DEADMAN_LIMIT (1 << 20)
3181 static Token
*expand_smacro(Token
* tline
)
3183 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3184 struct hash_table
*smtbl
;
3185 SMacro
*head
= NULL
, *m
;
3188 unsigned int nparam
, sparam
;
3189 int brackets
, rescan
;
3190 Token
*org_tline
= tline
;
3193 int deadman
= DEADMAN_LIMIT
;
3196 * Trick: we should avoid changing the start token pointer since it can
3197 * be contained in "next" field of other token. Because of this
3198 * we allocate a copy of first token and work with it; at the end of
3199 * routine we copy it back
3203 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3205 tline
->mac
= org_tline
->mac
;
3206 nasm_free(org_tline
->text
);
3207 org_tline
->text
= NULL
;
3214 while (tline
) { /* main token loop */
3216 error(ERR_NONFATAL
, "interminable macro recursion");
3220 if ((mname
= tline
->text
)) {
3221 /* if this token is a local macro, look in local context */
3224 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3225 ctx
= get_ctx(mname
, true);
3227 smtbl
= &ctx
->localmac
;
3229 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3232 * We've hit an identifier. As in is_mmacro below, we first
3233 * check whether the identifier is a single-line macro at
3234 * all, then think about checking for parameters if
3237 for (m
= head
; m
; m
= m
->next
)
3238 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3244 if (m
->nparam
== 0) {
3246 * Simple case: the macro is parameterless. Discard the
3247 * one token that the macro call took, and push the
3248 * expansion back on the to-do stack.
3250 if (!m
->expansion
) {
3251 if (!strcmp("__FILE__", m
->name
)) {
3254 src_get(&num
, &file
);
3255 tline
->text
= nasm_quote(file
, strlen(file
));
3256 tline
->type
= TOK_STRING
;
3260 if (!strcmp("__LINE__", m
->name
)) {
3261 nasm_free(tline
->text
);
3262 make_tok_num(tline
, src_get_linnum());
3265 if (!strcmp("__BITS__", m
->name
)) {
3266 nasm_free(tline
->text
);
3267 make_tok_num(tline
, globalbits
);
3270 tline
= delete_Token(tline
);
3275 * Complicated case: at least one macro with this name
3276 * exists and takes parameters. We must find the
3277 * parameters in the call, count them, find the SMacro
3278 * that corresponds to that form of the macro call, and
3279 * substitute for the parameters when we expand. What a
3282 /*tline = tline->next;
3283 skip_white_(tline); */
3286 while (tok_type_(t
, TOK_SMAC_END
)) {
3287 t
->mac
->in_progress
= false;
3289 t
= tline
->next
= delete_Token(t
);
3292 } while (tok_type_(tline
, TOK_WHITESPACE
));
3293 if (!tok_is_(tline
, "(")) {
3295 * This macro wasn't called with parameters: ignore
3296 * the call. (Behaviour borrowed from gnu cpp.)
3305 sparam
= PARAM_DELTA
;
3306 params
= nasm_malloc(sparam
* sizeof(Token
*));
3307 params
[0] = tline
->next
;
3308 paramsize
= nasm_malloc(sparam
* sizeof(int));
3310 while (true) { /* parameter loop */
3312 * For some unusual expansions
3313 * which concatenates function call
3316 while (tok_type_(t
, TOK_SMAC_END
)) {
3317 t
->mac
->in_progress
= false;
3319 t
= tline
->next
= delete_Token(t
);
3325 "macro call expects terminating `)'");
3328 if (tline
->type
== TOK_WHITESPACE
3330 if (paramsize
[nparam
])
3333 params
[nparam
] = tline
->next
;
3334 continue; /* parameter loop */
3336 if (tline
->type
== TOK_OTHER
3337 && tline
->text
[1] == 0) {
3338 char ch
= tline
->text
[0];
3339 if (ch
== ',' && !paren
&& brackets
<= 0) {
3340 if (++nparam
>= sparam
) {
3341 sparam
+= PARAM_DELTA
;
3342 params
= nasm_realloc(params
,
3347 nasm_realloc(paramsize
,
3351 params
[nparam
] = tline
->next
;
3352 paramsize
[nparam
] = 0;
3354 continue; /* parameter loop */
3357 (brackets
> 0 || (brackets
== 0 &&
3358 !paramsize
[nparam
])))
3360 if (!(brackets
++)) {
3361 params
[nparam
] = tline
->next
;
3362 continue; /* parameter loop */
3365 if (ch
== '}' && brackets
> 0)
3366 if (--brackets
== 0) {
3368 continue; /* parameter loop */
3370 if (ch
== '(' && !brackets
)
3372 if (ch
== ')' && brackets
<= 0)
3378 error(ERR_NONFATAL
, "braces do not "
3379 "enclose all of macro parameter");
3381 paramsize
[nparam
] += white
+ 1;
3383 } /* parameter loop */
3385 while (m
&& (m
->nparam
!= nparam
||
3386 mstrcmp(m
->name
, mname
,
3390 error(ERR_WARNING
| ERR_WARN_MNP
,
3391 "macro `%s' exists, "
3392 "but not taking %d parameters",
3393 mstart
->text
, nparam
);
3396 if (m
&& m
->in_progress
)
3398 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3400 * Design question: should we handle !tline, which
3401 * indicates missing ')' here, or expand those
3402 * macros anyway, which requires the (t) test a few
3406 nasm_free(paramsize
);
3410 * Expand the macro: we are placed on the last token of the
3411 * call, so that we can easily split the call from the
3412 * following tokens. We also start by pushing an SMAC_END
3413 * token for the cycle removal.
3420 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3422 m
->in_progress
= true;
3424 for (t
= m
->expansion
; t
; t
= t
->next
) {
3425 if (t
->type
>= TOK_SMAC_PARAM
) {
3426 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3430 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3431 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3434 new_Token(tline
, ttt
->type
, ttt
->text
,
3440 } else if (t
->type
== TOK_PREPROC_Q
) {
3441 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3443 } else if (t
->type
== TOK_PREPROC_QQ
) {
3444 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3447 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3453 * Having done that, get rid of the macro call, and clean
3454 * up the parameters.
3457 nasm_free(paramsize
);
3459 continue; /* main token loop */
3464 if (tline
->type
== TOK_SMAC_END
) {
3465 tline
->mac
->in_progress
= false;
3466 tline
= delete_Token(tline
);
3469 tline
= tline
->next
;
3477 * Now scan the entire line and look for successive TOK_IDs that resulted
3478 * after expansion (they can't be produced by tokenize()). The successive
3479 * TOK_IDs should be concatenated.
3480 * Also we look for %+ tokens and concatenate the tokens before and after
3481 * them (without white spaces in between).
3486 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3490 if (t
->next
->type
== TOK_ID
||
3491 t
->next
->type
== TOK_PREPROC_ID
||
3492 t
->next
->type
== TOK_NUMBER
) {
3493 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3495 t
->next
= delete_Token(t
->next
);
3498 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3499 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3500 strcmp(t
->next
->next
->text
, "%+") == 0) {
3501 /* free the next whitespace, the %+ token and next whitespace */
3503 for (i
= 1; i
<= 3; i
++) {
3505 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3507 t
->next
= delete_Token(t
->next
);
3512 /* If we concatenaded something, re-scan the line for macros */
3520 *org_tline
= *thead
;
3521 /* since we just gave text to org_line, don't free it */
3523 delete_Token(thead
);
3525 /* the expression expanded to empty line;
3526 we can't return NULL for some reasons
3527 we just set the line to a single WHITESPACE token. */
3528 memset(org_tline
, 0, sizeof(*org_tline
));
3529 org_tline
->text
= NULL
;
3530 org_tline
->type
= TOK_WHITESPACE
;
3539 * Similar to expand_smacro but used exclusively with macro identifiers
3540 * right before they are fetched in. The reason is that there can be
3541 * identifiers consisting of several subparts. We consider that if there
3542 * are more than one element forming the name, user wants a expansion,
3543 * otherwise it will be left as-is. Example:
3547 * the identifier %$abc will be left as-is so that the handler for %define
3548 * will suck it and define the corresponding value. Other case:
3550 * %define _%$abc cde
3552 * In this case user wants name to be expanded *before* %define starts
3553 * working, so we'll expand %$abc into something (if it has a value;
3554 * otherwise it will be left as-is) then concatenate all successive
3557 static Token
*expand_id(Token
* tline
)
3559 Token
*cur
, *oldnext
= NULL
;
3561 if (!tline
|| !tline
->next
)
3566 (cur
->next
->type
== TOK_ID
||
3567 cur
->next
->type
== TOK_PREPROC_ID
3568 || cur
->next
->type
== TOK_NUMBER
))
3571 /* If identifier consists of just one token, don't expand */
3576 oldnext
= cur
->next
; /* Detach the tail past identifier */
3577 cur
->next
= NULL
; /* so that expand_smacro stops here */
3580 tline
= expand_smacro(tline
);
3583 /* expand_smacro possibly changhed tline; re-scan for EOL */
3585 while (cur
&& cur
->next
)
3588 cur
->next
= oldnext
;
3595 * Determine whether the given line constitutes a multi-line macro
3596 * call, and return the MMacro structure called if so. Doesn't have
3597 * to check for an initial label - that's taken care of in
3598 * expand_mmacro - but must check numbers of parameters. Guaranteed
3599 * to be called with tline->type == TOK_ID, so the putative macro
3600 * name is easy to find.
3602 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3608 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3611 * Efficiency: first we see if any macro exists with the given
3612 * name. If not, we can return NULL immediately. _Then_ we
3613 * count the parameters, and then we look further along the
3614 * list if necessary to find the proper MMacro.
3616 for (m
= head
; m
; m
= m
->next
)
3617 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3623 * OK, we have a potential macro. Count and demarcate the
3626 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3629 * So we know how many parameters we've got. Find the MMacro
3630 * structure that handles this number.
3633 if (m
->nparam_min
<= nparam
3634 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3636 * This one is right. Just check if cycle removal
3637 * prohibits us using it before we actually celebrate...
3639 if (m
->in_progress
) {
3642 "self-reference in multi-line macro `%s'", m
->name
);
3648 * It's right, and we can use it. Add its default
3649 * parameters to the end of our list if necessary.
3651 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3653 nasm_realloc(params
,
3654 ((m
->nparam_min
+ m
->ndefs
+
3655 1) * sizeof(*params
)));
3656 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3657 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3662 * If we've gone over the maximum parameter count (and
3663 * we're in Plus mode), ignore parameters beyond
3666 if (m
->plus
&& nparam
> m
->nparam_max
)
3667 nparam
= m
->nparam_max
;
3669 * Then terminate the parameter list, and leave.
3671 if (!params
) { /* need this special case */
3672 params
= nasm_malloc(sizeof(*params
));
3675 params
[nparam
] = NULL
;
3676 *params_array
= params
;
3680 * This one wasn't right: look for the next one with the
3683 for (m
= m
->next
; m
; m
= m
->next
)
3684 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3689 * After all that, we didn't find one with the right number of
3690 * parameters. Issue a warning, and fail to expand the macro.
3692 error(ERR_WARNING
| ERR_WARN_MNP
,
3693 "macro `%s' exists, but not taking %d parameters",
3694 tline
->text
, nparam
);
3700 * Expand the multi-line macro call made by the given line, if
3701 * there is one to be expanded. If there is, push the expansion on
3702 * istk->expansion and return 1. Otherwise return 0.
3704 static int expand_mmacro(Token
* tline
)
3706 Token
*startline
= tline
;
3707 Token
*label
= NULL
;
3708 int dont_prepend
= 0;
3709 Token
**params
, *t
, *mtok
, *tt
;
3712 int i
, nparam
, *paramlen
;
3716 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3717 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3720 m
= is_mmacro(t
, ¶ms
);
3724 * We have an id which isn't a macro call. We'll assume
3725 * it might be a label; we'll also check to see if a
3726 * colon follows it. Then, if there's another id after
3727 * that lot, we'll check it again for macro-hood.
3731 if (tok_type_(t
, TOK_WHITESPACE
))
3732 last
= t
, t
= t
->next
;
3733 if (tok_is_(t
, ":")) {
3735 last
= t
, t
= t
->next
;
3736 if (tok_type_(t
, TOK_WHITESPACE
))
3737 last
= t
, t
= t
->next
;
3739 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3746 * Fix up the parameters: this involves stripping leading and
3747 * trailing whitespace, then stripping braces if they are
3750 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3751 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3753 for (i
= 0; params
[i
]; i
++) {
3755 int comma
= (!m
->plus
|| i
< nparam
- 1);
3759 if (tok_is_(t
, "{"))
3760 t
= t
->next
, brace
= true, comma
= false;
3764 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3765 break; /* ... because we have hit a comma */
3766 if (comma
&& t
->type
== TOK_WHITESPACE
3767 && tok_is_(t
->next
, ","))
3768 break; /* ... or a space then a comma */
3769 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3770 break; /* ... or a brace */
3777 * OK, we have a MMacro structure together with a set of
3778 * parameters. We must now go through the expansion and push
3779 * copies of each Line on to istk->expansion. Substitution of
3780 * parameter tokens and macro-local tokens doesn't get done
3781 * until the single-line macro substitution process; this is
3782 * because delaying them allows us to change the semantics
3783 * later through %rotate.
3785 * First, push an end marker on to istk->expansion, mark this
3786 * macro as in progress, and set up its invocation-specific
3789 ll
= nasm_malloc(sizeof(Line
));
3790 ll
->next
= istk
->expansion
;
3793 istk
->expansion
= ll
;
3795 m
->in_progress
= true;
3800 m
->paramlen
= paramlen
;
3801 m
->unique
= unique
++;
3804 m
->next_active
= istk
->mstk
;
3807 for (l
= m
->expansion
; l
; l
= l
->next
) {
3810 ll
= nasm_malloc(sizeof(Line
));
3811 ll
->finishes
= NULL
;
3812 ll
->next
= istk
->expansion
;
3813 istk
->expansion
= ll
;
3816 for (t
= l
->first
; t
; t
= t
->next
) {
3820 tt
= *tail
= new_Token(NULL
, TOK_ID
, mtok
->text
, 0);
3822 case TOK_PREPROC_QQ
:
3823 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3825 case TOK_PREPROC_ID
:
3826 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3834 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3843 * If we had a label, push it on as the first line of
3844 * the macro expansion.
3847 if (dont_prepend
< 0)
3848 free_tlist(startline
);
3850 ll
= nasm_malloc(sizeof(Line
));
3851 ll
->finishes
= NULL
;
3852 ll
->next
= istk
->expansion
;
3853 istk
->expansion
= ll
;
3854 ll
->first
= startline
;
3855 if (!dont_prepend
) {
3857 label
= label
->next
;
3858 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3863 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3869 * Since preprocessor always operate only on the line that didn't
3870 * arrived yet, we should always use ERR_OFFBY1. Also since user
3871 * won't want to see same error twice (preprocessing is done once
3872 * per pass) we will want to show errors only during pass one.
3874 static void error(int severity
, const char *fmt
, ...)
3879 /* If we're in a dead branch of IF or something like it, ignore the error */
3880 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3884 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3887 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3888 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3889 istk
->mstk
->lineno
, buff
);
3891 _error(severity
| ERR_PASS1
, "%s", buff
);
3895 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3896 ListGen
* listgen
, StrList
**deplist
)
3900 istk
= nasm_malloc(sizeof(Include
));
3903 istk
->expansion
= NULL
;
3905 istk
->fp
= fopen(file
, "r");
3907 src_set_fname(nasm_strdup(file
));
3911 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3916 if (tasm_compatible_mode
) {
3917 stdmacpos
= nasm_stdmac
;
3919 stdmacpos
= nasm_stdmac_after_tasm
;
3921 any_extrastdmac
= (extrastdmac
!= NULL
);
3925 dephead
= deptail
= deplist
;
3927 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
3929 strcpy(sl
->str
, file
);
3931 deptail
= &sl
->next
;
3935 static char *pp_getline(void)
3942 * Fetch a tokenized line, either from the macro-expansion
3943 * buffer or from the input file.
3946 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3947 Line
*l
= istk
->expansion
;
3948 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3952 * This is a macro-end marker for a macro with no
3953 * name, which means it's not really a macro at all
3954 * but a %rep block, and the `in_progress' field is
3955 * more than 1, meaning that we still need to
3956 * repeat. (1 means the natural last repetition; 0
3957 * means termination by %exitrep.) We have
3958 * therefore expanded up to the %endrep, and must
3959 * push the whole block on to the expansion buffer
3960 * again. We don't bother to remove the macro-end
3961 * marker: we'd only have to generate another one
3964 l
->finishes
->in_progress
--;
3965 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3966 Token
*t
, *tt
, **tail
;
3968 ll
= nasm_malloc(sizeof(Line
));
3969 ll
->next
= istk
->expansion
;
3970 ll
->finishes
= NULL
;
3974 for (t
= l
->first
; t
; t
= t
->next
) {
3975 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3977 new_Token(NULL
, t
->type
, t
->text
, 0);
3982 istk
->expansion
= ll
;
3986 * Check whether a `%rep' was started and not ended
3987 * within this macro expansion. This can happen and
3988 * should be detected. It's a fatal error because
3989 * I'm too confused to work out how to recover
3995 "defining with name in expansion");
3996 else if (istk
->mstk
->name
)
3998 "`%%rep' without `%%endrep' within"
3999 " expansion of macro `%s'",
4004 * FIXME: investigate the relationship at this point between
4005 * istk->mstk and l->finishes
4008 MMacro
*m
= istk
->mstk
;
4009 istk
->mstk
= m
->next_active
;
4012 * This was a real macro call, not a %rep, and
4013 * therefore the parameter information needs to
4016 nasm_free(m
->params
);
4017 free_tlist(m
->iline
);
4018 nasm_free(m
->paramlen
);
4019 l
->finishes
->in_progress
= false;
4023 istk
->expansion
= l
->next
;
4025 list
->downlevel(LIST_MACRO
);
4028 while (1) { /* until we get a line we can use */
4030 if (istk
->expansion
) { /* from a macro expansion */
4032 Line
*l
= istk
->expansion
;
4034 istk
->mstk
->lineno
++;
4036 istk
->expansion
= l
->next
;
4038 p
= detoken(tline
, false);
4039 list
->line(LIST_MACRO
, p
);
4044 if (line
) { /* from the current input file */
4045 line
= prepreproc(line
);
4046 tline
= tokenize(line
);
4051 * The current file has ended; work down the istk
4058 "expected `%%endif' before end of file");
4059 /* only set line and file name if there's a next node */
4061 src_set_linnum(i
->lineno
);
4062 nasm_free(src_set_fname(i
->fname
));
4065 list
->downlevel(LIST_INCLUDE
);
4073 * We must expand MMacro parameters and MMacro-local labels
4074 * _before_ we plunge into directive processing, to cope
4075 * with things like `%define something %1' such as STRUC
4076 * uses. Unless we're _defining_ a MMacro, in which case
4077 * those tokens should be left alone to go into the
4078 * definition; and unless we're in a non-emitting
4079 * condition, in which case we don't want to meddle with
4082 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4083 tline
= expand_mmac_params(tline
);
4086 * Check the line to see if it's a preprocessor directive.
4088 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4090 } else if (defining
) {
4092 * We're defining a multi-line macro. We emit nothing
4094 * shove the tokenized line on to the macro definition.
4096 Line
*l
= nasm_malloc(sizeof(Line
));
4097 l
->next
= defining
->expansion
;
4099 l
->finishes
= false;
4100 defining
->expansion
= l
;
4102 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4104 * We're in a non-emitting branch of a condition block.
4105 * Emit nothing at all, not even a blank line: when we
4106 * emerge from the condition we'll give a line-number
4107 * directive so we keep our place correctly.
4111 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4113 * We're in a %rep block which has been terminated, so
4114 * we're walking through to the %endrep without
4115 * emitting anything. Emit nothing at all, not even a
4116 * blank line: when we emerge from the %rep block we'll
4117 * give a line-number directive so we keep our place
4123 tline
= expand_smacro(tline
);
4124 if (!expand_mmacro(tline
)) {
4126 * De-tokenize the line again, and emit it.
4128 line
= detoken(tline
, true);
4132 continue; /* expand_mmacro calls free_tlist */
4140 static void pp_cleanup(int pass
)
4143 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4145 free_mmacro(defining
);
4154 nasm_free(i
->fname
);
4165 void pp_include_path(char *path
)
4169 i
= nasm_malloc(sizeof(IncPath
));
4170 i
->path
= path
? nasm_strdup(path
) : NULL
;
4173 if (ipath
!= NULL
) {
4175 while (j
->next
!= NULL
)
4183 void pp_pre_include(char *fname
)
4185 Token
*inc
, *space
, *name
;
4188 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4189 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4190 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4192 l
= nasm_malloc(sizeof(Line
));
4195 l
->finishes
= false;
4199 void pp_pre_define(char *definition
)
4205 equals
= strchr(definition
, '=');
4206 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4207 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4210 space
->next
= tokenize(definition
);
4214 l
= nasm_malloc(sizeof(Line
));
4217 l
->finishes
= false;
4221 void pp_pre_undefine(char *definition
)
4226 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4227 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4228 space
->next
= tokenize(definition
);
4230 l
= nasm_malloc(sizeof(Line
));
4233 l
->finishes
= false;
4238 * Added by Keith Kanios:
4240 * This function is used to assist with "runtime" preprocessor
4241 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4243 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4244 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4247 void pp_runtime(char *definition
)
4251 def
= tokenize(definition
);
4252 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4257 void pp_extra_stdmac(const char **macros
)
4259 extrastdmac
= macros
;
4262 static void make_tok_num(Token
* tok
, int64_t val
)
4265 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4266 tok
->text
= nasm_strdup(numbuf
);
4267 tok
->type
= TOK_NUMBER
;