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
,
397 const char *text
, int txtlen
);
398 static Token
*delete_Token(Token
* t
);
401 * Macros for safe checking of token pointers, avoid *(NULL)
403 #define tok_type_(x,t) ((x) && (x)->type == (t))
404 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
405 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
406 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
408 /* Handle TASM specific directives, which do not contain a % in
409 * front of them. We do it here because I could not find any other
410 * place to do it for the moment, and it is a hack (ideally it would
411 * be nice to be able to use the NASM pre-processor to do it).
413 static char *check_tasm_directive(char *line
)
415 int32_t i
, j
, k
, m
, len
;
416 char *p
= line
, *oldline
, oldchar
;
418 /* Skip whitespace */
419 while (isspace(*p
) && *p
!= 0)
422 /* Binary search for the directive name */
424 j
= elements(tasm_directives
);
426 while (!isspace(p
[len
]) && p
[len
] != 0)
433 m
= nasm_stricmp(p
, tasm_directives
[k
]);
435 /* We have found a directive, so jam a % in front of it
436 * so that NASM will then recognise it as one if it's own.
441 line
= nasm_malloc(len
+ 2);
443 if (k
== TM_IFDIFI
) {
444 /* NASM does not recognise IFDIFI, so we convert it to
445 * %ifdef BOGUS. This is not used in NASM comaptible
446 * code, but does need to parse for the TASM macro
449 strcpy(line
+ 1, "ifdef BOGUS");
451 memcpy(line
+ 1, p
, len
+ 1);
466 * The pre-preprocessing stage... This function translates line
467 * number indications as they emerge from GNU cpp (`# lineno "file"
468 * flags') into NASM preprocessor line number indications (`%line
471 static char *prepreproc(char *line
)
474 char *fname
, *oldline
;
476 if (line
[0] == '#' && line
[1] == ' ') {
479 lineno
= atoi(fname
);
480 fname
+= strspn(fname
, "0123456789 ");
483 fnlen
= strcspn(fname
, "\"");
484 line
= nasm_malloc(20 + fnlen
);
485 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
488 if (tasm_compatible_mode
)
489 return check_tasm_directive(line
);
494 * Free a linked list of tokens.
496 static void free_tlist(Token
* list
)
499 list
= delete_Token(list
);
504 * Free a linked list of lines.
506 static void free_llist(Line
* list
)
512 free_tlist(l
->first
);
520 static void free_mmacro(MMacro
* m
)
523 free_tlist(m
->dlist
);
524 nasm_free(m
->defaults
);
525 free_llist(m
->expansion
);
530 * Free all currently defined macros, and free the hash tables
532 static void free_smacro_table(struct hash_table
*smt
)
536 struct hash_tbl_node
*it
= NULL
;
538 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
539 nasm_free((void *)key
);
541 SMacro
*ns
= s
->next
;
543 free_tlist(s
->expansion
);
551 static void free_mmacro_table(struct hash_table
*mmt
)
555 struct hash_tbl_node
*it
= NULL
;
558 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
559 nasm_free((void *)key
);
561 MMacro
*nm
= m
->next
;
569 static void free_macros(void)
571 free_smacro_table(&smacros
);
572 free_mmacro_table(&mmacros
);
576 * Initialize the hash tables
578 static void init_macros(void)
580 hash_init(&smacros
, HASH_LARGE
);
581 hash_init(&mmacros
, HASH_LARGE
);
585 * Pop the context stack.
587 static void ctx_pop(void)
592 free_smacro_table(&c
->localmac
);
598 * Search for a key in the hash index; adding it if necessary
599 * (in which case we initialize the data pointer to NULL.)
602 hash_findi_add(struct hash_table
*hash
, const char *str
)
604 struct hash_insert hi
;
608 r
= hash_findi(hash
, str
, &hi
);
612 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
613 return hash_add(&hi
, strx
, NULL
);
617 * Like hash_findi, but returns the data element rather than a pointer
618 * to it. Used only when not adding a new element, hence no third
622 hash_findix(struct hash_table
*hash
, const char *str
)
626 p
= hash_findi(hash
, str
, NULL
);
627 return p
? *p
: NULL
;
630 #define BUF_DELTA 512
632 * Read a line from the top file in istk, handling multiple CR/LFs
633 * at the end of the line read, and handling spurious ^Zs. Will
634 * return lines from the standard macro set if this has not already
637 static char *read_line(void)
639 char *buffer
, *p
, *q
;
640 int bufsize
, continued_count
;
644 char *ret
= nasm_strdup(*stdmacpos
++);
645 if (!*stdmacpos
&& any_extrastdmac
) {
646 stdmacpos
= extrastdmac
;
647 any_extrastdmac
= false;
651 * Nasty hack: here we push the contents of `predef' on
652 * to the top-level expansion stack, since this is the
653 * most convenient way to implement the pre-include and
654 * pre-define features.
658 Token
*head
, **tail
, *t
;
660 for (pd
= predef
; pd
; pd
= pd
->next
) {
663 for (t
= pd
->first
; t
; t
= t
->next
) {
664 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
665 tail
= &(*tail
)->next
;
667 l
= nasm_malloc(sizeof(Line
));
668 l
->next
= istk
->expansion
;
681 buffer
= nasm_malloc(BUF_DELTA
);
685 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
689 if (p
> buffer
&& p
[-1] == '\n') {
690 /* Convert backslash-CRLF line continuation sequences into
691 nothing at all (for DOS and Windows) */
692 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
697 /* Also convert backslash-LF line continuation sequences into
698 nothing at all (for Unix) */
699 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
707 if (p
- buffer
> bufsize
- 10) {
708 int32_t offset
= p
- buffer
;
709 bufsize
+= BUF_DELTA
;
710 buffer
= nasm_realloc(buffer
, bufsize
);
711 p
= buffer
+ offset
; /* prevent stale-pointer problems */
715 if (!q
&& p
== buffer
) {
720 src_set_linnum(src_get_linnum() + istk
->lineinc
+
721 (continued_count
* istk
->lineinc
));
724 * Play safe: remove CRs as well as LFs, if any of either are
725 * present at the end of the line.
727 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
731 * Handle spurious ^Z, which may be inserted into source files
732 * by some file transfer utilities.
734 buffer
[strcspn(buffer
, "\032")] = '\0';
736 list
->line(LIST_READ
, buffer
);
742 * Tokenize a line of text. This is a very simple process since we
743 * don't need to parse the value out of e.g. numeric tokens: we
744 * simply split one string into many.
746 static Token
*tokenize(char *line
)
749 enum pp_token_type type
;
751 Token
*t
, **tail
= &list
;
758 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
759 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
764 type
= TOK_PREPROC_ID
;
765 } else if (*p
== '{') {
767 while (*p
&& *p
!= '}') {
774 type
= TOK_PREPROC_ID
;
775 } else if (*p
== '?') {
776 type
= TOK_PREPROC_Q
; /* %? */
779 type
= TOK_PREPROC_QQ
; /* %?? */
782 } else if (isidchar(*p
) ||
783 ((*p
== '!' || *p
== '%' || *p
== '$') &&
788 while (isidchar(*p
));
789 type
= TOK_PREPROC_ID
;
795 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
798 while (*p
&& isidchar(*p
))
800 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
805 p
= nasm_skip_string(p
);
810 error(ERR_WARNING
, "unterminated string");
811 /* Handling unterminated strings by UNV */
814 } else if (isnumstart(*p
)) {
816 bool is_float
= false;
832 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
834 if (*p
== '+' || *p
== '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
840 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
842 } else if (c
== 'P' || c
== 'p') {
844 if (*p
== '+' || *p
== '-')
846 } else if (isnumchar(c
) || c
== '_')
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
861 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
862 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
863 (*r
== 'p' || *r
== 'P')) {
867 break; /* Terminate the token */
871 p
--; /* Point to first character beyond number */
873 if (has_e
&& !is_hex
) {
874 /* 1e13 is floating-point, but 1e13h is not */
878 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
879 } else if (isspace(*p
)) {
880 type
= TOK_WHITESPACE
;
882 while (*p
&& isspace(*p
))
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
889 if (!*p
|| *p
== ';') {
894 } else if (*p
== ';') {
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
903 * else is a single-character operator.
906 if ((p
[0] == '>' && p
[1] == '>') ||
907 (p
[0] == '<' && p
[1] == '<') ||
908 (p
[0] == '/' && p
[1] == '/') ||
909 (p
[0] == '<' && p
[1] == '=') ||
910 (p
[0] == '>' && p
[1] == '=') ||
911 (p
[0] == '=' && p
[1] == '=') ||
912 (p
[0] == '!' && p
[1] == '=') ||
913 (p
[0] == '<' && p
[1] == '>') ||
914 (p
[0] == '&' && p
[1] == '&') ||
915 (p
[0] == '|' && p
[1] == '|') ||
916 (p
[0] == '^' && p
[1] == '^')) {
922 /* Handling unterminated string by UNV */
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
930 if (type
!= TOK_COMMENT
) {
931 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
940 * this function allocates a new managed block of memory and
941 * returns a pointer to the block. The managed blocks are
942 * deleted only all at once by the delete_Blocks function.
944 static void *new_Block(size_t size
)
948 /* first, get to the end of the linked list */
951 /* now allocate the requested chunk */
952 b
->chunk
= nasm_malloc(size
);
954 /* now allocate a new block for the next request */
955 b
->next
= nasm_malloc(sizeof(Blocks
));
956 /* and initialize the contents of the new block */
957 b
->next
->next
= NULL
;
958 b
->next
->chunk
= NULL
;
963 * this function deletes all managed blocks of memory
965 static void delete_Blocks(void)
967 Blocks
*a
, *b
= &blocks
;
970 * keep in mind that the first block, pointed to by blocks
971 * is a static and not dynamically allocated, so we don't
985 * this function creates a new Token and passes a pointer to it
986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
989 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
990 const char *text
, int txtlen
)
995 if (freeTokens
== NULL
) {
996 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
997 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
998 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
999 freeTokens
[i
].next
= NULL
;
1002 freeTokens
= t
->next
;
1006 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1010 txtlen
= strlen(text
);
1011 t
->text
= nasm_malloc(txtlen
+1);
1012 memcpy(t
->text
, text
, txtlen
);
1013 t
->text
[txtlen
] = '\0';
1018 static Token
*delete_Token(Token
* t
)
1020 Token
*next
= t
->next
;
1022 t
->next
= freeTokens
;
1028 * Convert a line of tokens back into text.
1029 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1030 * will be transformed into ..@ctxnum.xxx
1032 static char *detoken(Token
* tlist
, bool expand_locals
)
1040 for (t
= tlist
; t
; t
= t
->next
) {
1041 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1042 char *p
= getenv(t
->text
+ 2);
1045 t
->text
= nasm_strdup(p
);
1049 /* Expand local macros here and not during preprocessing */
1050 if (expand_locals
&&
1051 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1052 t
->text
[0] == '%' && t
->text
[1] == '$') {
1053 Context
*ctx
= get_ctx(t
->text
, false);
1056 char *p
, *q
= t
->text
+ 2;
1058 q
+= strspn(q
, "$");
1059 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1060 p
= nasm_strcat(buffer
, q
);
1065 if (t
->type
== TOK_WHITESPACE
) {
1067 } else if (t
->text
) {
1068 len
+= strlen(t
->text
);
1071 p
= line
= nasm_malloc(len
+ 1);
1072 for (t
= tlist
; t
; t
= t
->next
) {
1073 if (t
->type
== TOK_WHITESPACE
) {
1075 } else if (t
->text
) {
1086 * A scanner, suitable for use by the expression evaluator, which
1087 * operates on a line of Tokens. Expects a pointer to a pointer to
1088 * the first token in the line to be passed in as its private_data
1091 * FIX: This really needs to be unified with stdscan.
1093 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1095 Token
**tlineptr
= private_data
;
1097 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1101 *tlineptr
= tline
? tline
->next
: NULL
;
1103 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1104 tline
->type
== TOK_COMMENT
));
1107 return tokval
->t_type
= TOKEN_EOS
;
1109 tokval
->t_charptr
= tline
->text
;
1111 if (tline
->text
[0] == '$' && !tline
->text
[1])
1112 return tokval
->t_type
= TOKEN_HERE
;
1113 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1114 return tokval
->t_type
= TOKEN_BASE
;
1116 if (tline
->type
== TOK_ID
) {
1117 p
= tokval
->t_charptr
= tline
->text
;
1119 tokval
->t_charptr
++;
1120 return tokval
->t_type
= TOKEN_ID
;
1123 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1124 if (r
>= p
+MAX_KEYWORD
)
1125 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1129 /* right, so we have an identifier sitting in temp storage. now,
1130 * is it actually a register or instruction name, or what? */
1131 return nasm_token_hash(ourcopy
, tokval
);
1134 if (tline
->type
== TOK_NUMBER
) {
1136 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1137 tokval
->t_charptr
= tline
->text
;
1139 return tokval
->t_type
= TOKEN_ERRNUM
;
1141 return tokval
->t_type
= TOKEN_NUM
;
1144 if (tline
->type
== TOK_FLOAT
) {
1145 return tokval
->t_type
= TOKEN_FLOAT
;
1148 if (tline
->type
== TOK_STRING
) {
1151 bq
= tline
->text
[0];
1152 tokval
->t_charptr
= tline
->text
;
1153 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1155 if (ep
[0] != bq
|| ep
[1] != '\0')
1156 return tokval
->t_type
= TOKEN_ERRSTR
;
1158 return tokval
->t_type
= TOKEN_STR
;
1161 if (tline
->type
== TOK_OTHER
) {
1162 if (!strcmp(tline
->text
, "<<"))
1163 return tokval
->t_type
= TOKEN_SHL
;
1164 if (!strcmp(tline
->text
, ">>"))
1165 return tokval
->t_type
= TOKEN_SHR
;
1166 if (!strcmp(tline
->text
, "//"))
1167 return tokval
->t_type
= TOKEN_SDIV
;
1168 if (!strcmp(tline
->text
, "%%"))
1169 return tokval
->t_type
= TOKEN_SMOD
;
1170 if (!strcmp(tline
->text
, "=="))
1171 return tokval
->t_type
= TOKEN_EQ
;
1172 if (!strcmp(tline
->text
, "<>"))
1173 return tokval
->t_type
= TOKEN_NE
;
1174 if (!strcmp(tline
->text
, "!="))
1175 return tokval
->t_type
= TOKEN_NE
;
1176 if (!strcmp(tline
->text
, "<="))
1177 return tokval
->t_type
= TOKEN_LE
;
1178 if (!strcmp(tline
->text
, ">="))
1179 return tokval
->t_type
= TOKEN_GE
;
1180 if (!strcmp(tline
->text
, "&&"))
1181 return tokval
->t_type
= TOKEN_DBL_AND
;
1182 if (!strcmp(tline
->text
, "^^"))
1183 return tokval
->t_type
= TOKEN_DBL_XOR
;
1184 if (!strcmp(tline
->text
, "||"))
1185 return tokval
->t_type
= TOKEN_DBL_OR
;
1189 * We have no other options: just return the first character of
1192 return tokval
->t_type
= tline
->text
[0];
1196 * Compare a string to the name of an existing macro; this is a
1197 * simple wrapper which calls either strcmp or nasm_stricmp
1198 * depending on the value of the `casesense' parameter.
1200 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1202 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1206 * Compare a string to the name of an existing macro; this is a
1207 * simple wrapper which calls either strcmp or nasm_stricmp
1208 * depending on the value of the `casesense' parameter.
1210 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1212 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1216 * Return the Context structure associated with a %$ token. Return
1217 * NULL, having _already_ reported an error condition, if the
1218 * context stack isn't deep enough for the supplied number of $
1220 * If all_contexts == true, contexts that enclose current are
1221 * also scanned for such smacro, until it is found; if not -
1222 * only the context that directly results from the number of $'s
1223 * in variable's name.
1225 static Context
*get_ctx(char *name
, bool all_contexts
)
1231 if (!name
|| name
[0] != '%' || name
[1] != '$')
1235 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1239 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1241 /* i--; Lino - 02/25/02 */
1244 error(ERR_NONFATAL
, "`%s': context stack is only"
1245 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1252 /* Search for this smacro in found context */
1253 m
= hash_findix(&ctx
->localmac
, name
);
1255 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1266 * Check to see if a file is already in a string list
1268 static bool in_list(const StrList
*list
, const char *str
)
1271 if (!strcmp(list
->str
, str
))
1279 * Open an include file. This routine must always return a valid
1280 * file pointer if it returns - it's responsible for throwing an
1281 * ERR_FATAL and bombing out completely if not. It should also try
1282 * the include path one by one until it finds the file or reaches
1283 * the end of the path.
1285 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1290 IncPath
*ip
= ipath
;
1291 int len
= strlen(file
);
1292 size_t prefix_len
= 0;
1296 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1297 memcpy(sl
->str
, prefix
, prefix_len
);
1298 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1299 fp
= fopen(sl
->str
, "r");
1300 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1318 prefix_len
= strlen(prefix
);
1320 /* -MG given and file not found */
1321 if (dhead
&& !in_list(*dhead
, file
)) {
1322 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1324 strcpy(sl
->str
, file
);
1332 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1333 return NULL
; /* never reached - placate compilers */
1337 * Determine if we should warn on defining a single-line macro of
1338 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1339 * return true if _any_ single-line macro of that name is defined.
1340 * Otherwise, will return true if a single-line macro with either
1341 * `nparam' or no parameters is defined.
1343 * If a macro with precisely the right number of parameters is
1344 * defined, or nparam is -1, the address of the definition structure
1345 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1346 * is NULL, no action will be taken regarding its contents, and no
1349 * Note that this is also called with nparam zero to resolve
1352 * If you already know which context macro belongs to, you can pass
1353 * the context pointer as first parameter; if you won't but name begins
1354 * with %$ the context will be automatically computed. If all_contexts
1355 * is true, macro will be searched in outer contexts as well.
1358 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1361 struct hash_table
*smtbl
;
1365 smtbl
= &ctx
->localmac
;
1366 } else if (name
[0] == '%' && name
[1] == '$') {
1368 ctx
= get_ctx(name
, false);
1370 return false; /* got to return _something_ */
1371 smtbl
= &ctx
->localmac
;
1375 m
= (SMacro
*) hash_findix(smtbl
, name
);
1378 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1379 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1381 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1395 * Count and mark off the parameters in a multi-line macro call.
1396 * This is called both from within the multi-line macro expansion
1397 * code, and also to mark off the default parameters when provided
1398 * in a %macro definition line.
1400 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1402 int paramsize
, brace
;
1404 *nparam
= paramsize
= 0;
1407 if (*nparam
>= paramsize
) {
1408 paramsize
+= PARAM_DELTA
;
1409 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1413 if (tok_is_(t
, "{"))
1415 (*params
)[(*nparam
)++] = t
;
1416 while (tok_isnt_(t
, brace
? "}" : ","))
1418 if (t
) { /* got a comma/brace */
1422 * Now we've found the closing brace, look further
1426 if (tok_isnt_(t
, ",")) {
1428 "braces do not enclose all of macro parameter");
1429 while (tok_isnt_(t
, ","))
1433 t
= t
->next
; /* eat the comma */
1440 * Determine whether one of the various `if' conditions is true or
1443 * We must free the tline we get passed.
1445 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1447 enum pp_conditional i
= PP_COND(ct
);
1449 Token
*t
, *tt
, **tptr
, *origline
;
1450 struct tokenval tokval
;
1452 enum pp_token_type needtype
;
1458 j
= false; /* have we matched yet? */
1459 while (cstk
&& tline
) {
1461 if (!tline
|| tline
->type
!= TOK_ID
) {
1463 "`%s' expects context identifiers", pp_directives
[ct
]);
1464 free_tlist(origline
);
1467 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1469 tline
= tline
->next
;
1474 j
= false; /* have we matched yet? */
1477 if (!tline
|| (tline
->type
!= TOK_ID
&&
1478 (tline
->type
!= TOK_PREPROC_ID
||
1479 tline
->text
[1] != '$'))) {
1481 "`%s' expects macro identifiers", pp_directives
[ct
]);
1484 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1486 tline
= tline
->next
;
1492 tline
= expand_smacro(tline
);
1494 while (tok_isnt_(tt
, ","))
1498 "`%s' expects two comma-separated arguments",
1503 j
= true; /* assume equality unless proved not */
1504 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1505 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1506 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1510 if (t
->type
== TOK_WHITESPACE
) {
1514 if (tt
->type
== TOK_WHITESPACE
) {
1518 if (tt
->type
!= t
->type
) {
1519 j
= false; /* found mismatching tokens */
1522 /* When comparing strings, need to unquote them first */
1523 if (t
->type
== TOK_STRING
) {
1524 size_t l1
= nasm_unquote(t
->text
, NULL
);
1525 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1531 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1535 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1536 j
= false; /* found mismatching tokens */
1543 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1544 j
= false; /* trailing gunk on one end or other */
1550 MMacro searching
, *mmac
;
1552 tline
= tline
->next
;
1554 tline
= expand_id(tline
);
1555 if (!tok_type_(tline
, TOK_ID
)) {
1557 "`%s' expects a macro name", pp_directives
[ct
]);
1560 searching
.name
= nasm_strdup(tline
->text
);
1561 searching
.casesense
= true;
1562 searching
.plus
= false;
1563 searching
.nolist
= false;
1564 searching
.in_progress
= 0;
1565 searching
.rep_nest
= NULL
;
1566 searching
.nparam_min
= 0;
1567 searching
.nparam_max
= INT_MAX
;
1568 tline
= expand_smacro(tline
->next
);
1571 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1573 "`%s' expects a parameter count or nothing",
1576 searching
.nparam_min
= searching
.nparam_max
=
1577 readnum(tline
->text
, &j
);
1580 "unable to parse parameter count `%s'",
1583 if (tline
&& tok_is_(tline
->next
, "-")) {
1584 tline
= tline
->next
->next
;
1585 if (tok_is_(tline
, "*"))
1586 searching
.nparam_max
= INT_MAX
;
1587 else if (!tok_type_(tline
, TOK_NUMBER
))
1589 "`%s' expects a parameter count after `-'",
1592 searching
.nparam_max
= readnum(tline
->text
, &j
);
1595 "unable to parse parameter count `%s'",
1597 if (searching
.nparam_min
> searching
.nparam_max
)
1599 "minimum parameter count exceeds maximum");
1602 if (tline
&& tok_is_(tline
->next
, "+")) {
1603 tline
= tline
->next
;
1604 searching
.plus
= true;
1606 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1608 if (!strcmp(mmac
->name
, searching
.name
) &&
1609 (mmac
->nparam_min
<= searching
.nparam_max
1611 && (searching
.nparam_min
<= mmac
->nparam_max
1618 nasm_free(searching
.name
);
1627 needtype
= TOK_NUMBER
;
1630 needtype
= TOK_STRING
;
1634 t
= tline
= expand_smacro(tline
);
1636 while (tok_type_(t
, TOK_WHITESPACE
) ||
1637 (needtype
== TOK_NUMBER
&&
1638 tok_type_(t
, TOK_OTHER
) &&
1639 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1643 j
= tok_type_(t
, needtype
);
1647 t
= tline
= expand_smacro(tline
);
1648 while (tok_type_(t
, TOK_WHITESPACE
))
1653 t
= t
->next
; /* Skip the actual token */
1654 while (tok_type_(t
, TOK_WHITESPACE
))
1656 j
= !t
; /* Should be nothing left */
1661 t
= tline
= expand_smacro(tline
);
1662 while (tok_type_(t
, TOK_WHITESPACE
))
1665 j
= !t
; /* Should be empty */
1669 t
= tline
= expand_smacro(tline
);
1671 tokval
.t_type
= TOKEN_INVALID
;
1672 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1673 NULL
, pass
| CRITICAL
, error
, NULL
);
1678 "trailing garbage after expression ignored");
1679 if (!is_simple(evalresult
)) {
1681 "non-constant value given to `%s'", pp_directives
[ct
]);
1684 j
= reloc_value(evalresult
) != 0;
1689 "preprocessor directive `%s' not yet implemented",
1694 free_tlist(origline
);
1695 return j
^ PP_NEGATIVE(ct
);
1698 free_tlist(origline
);
1703 * Expand macros in a string. Used in %error directives (and it should
1704 * almost certainly be removed from there, too.)
1706 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1707 * The returned variable should ALWAYS be freed after usage.
1709 void expand_macros_in_string(char **p
)
1711 Token
*line
= tokenize(*p
);
1712 line
= expand_smacro(line
);
1713 *p
= detoken(line
, false);
1717 * Common code for defining an smacro
1719 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1720 int nparam
, Token
*expansion
)
1722 SMacro
*smac
, **smhead
;
1723 struct hash_table
*smtbl
;
1725 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1728 "single-line macro `%s' defined both with and"
1729 " without parameters", mname
);
1731 /* Some instances of the old code considered this a failure,
1732 some others didn't. What is the right thing to do here? */
1733 free_tlist(expansion
);
1734 return false; /* Failure */
1737 * We're redefining, so we have to take over an
1738 * existing SMacro structure. This means freeing
1739 * what was already in it.
1741 nasm_free(smac
->name
);
1742 free_tlist(smac
->expansion
);
1745 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1746 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1747 smac
= nasm_malloc(sizeof(SMacro
));
1748 smac
->next
= *smhead
;
1751 smac
->name
= nasm_strdup(mname
);
1752 smac
->casesense
= casesense
;
1753 smac
->nparam
= nparam
;
1754 smac
->expansion
= expansion
;
1755 smac
->in_progress
= false;
1756 return true; /* Success */
1760 * Undefine an smacro
1762 static void undef_smacro(Context
*ctx
, const char *mname
)
1764 SMacro
**smhead
, *s
, **sp
;
1765 struct hash_table
*smtbl
;
1767 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1768 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1772 * We now have a macro name... go hunt for it.
1775 while ((s
= *sp
) != NULL
) {
1776 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1779 free_tlist(s
->expansion
);
1789 * Decode a size directive
1791 static int parse_size(const char *str
) {
1792 static const char *size_names
[] =
1793 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1794 static const int sizes
[] =
1795 { 0, 1, 4, 16, 8, 10, 2, 32 };
1797 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1801 * find and process preprocessor directive in passed line
1802 * Find out if a line contains a preprocessor directive, and deal
1805 * If a directive _is_ found, it is the responsibility of this routine
1806 * (and not the caller) to free_tlist() the line.
1808 * @param tline a pointer to the current tokeninzed line linked list
1809 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1812 static int do_directive(Token
* tline
)
1814 enum preproc_token i
;
1826 MMacro
*mmac
, **mmhead
;
1827 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1829 struct tokenval tokval
;
1831 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1837 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1838 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1839 || tline
->text
[1] == '!'))
1840 return NO_DIRECTIVE_FOUND
;
1842 i
= pp_token_hash(tline
->text
);
1845 * If we're in a non-emitting branch of a condition construct,
1846 * or walking to the end of an already terminated %rep block,
1847 * we should ignore all directives except for condition
1850 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1851 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1852 return NO_DIRECTIVE_FOUND
;
1856 * If we're defining a macro or reading a %rep block, we should
1857 * ignore all directives except for %macro/%imacro (which
1858 * generate an error), %endm/%endmacro, and (only if we're in a
1859 * %rep block) %endrep. If we're in a %rep block, another %rep
1860 * causes an error, so should be let through.
1862 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1863 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1864 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1865 return NO_DIRECTIVE_FOUND
;
1870 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1872 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1875 /* Directive to tell NASM what the default stack size is. The
1876 * default is for a 16-bit stack, and this can be overriden with
1878 * the following form:
1880 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1882 tline
= tline
->next
;
1883 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1884 tline
= tline
->next
;
1885 if (!tline
|| tline
->type
!= TOK_ID
) {
1886 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1887 free_tlist(origline
);
1888 return DIRECTIVE_FOUND
;
1890 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1891 /* All subsequent ARG directives are for a 32-bit stack */
1893 StackPointer
= "ebp";
1896 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1897 /* All subsequent ARG directives are for a 64-bit stack */
1899 StackPointer
= "rbp";
1902 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1903 /* All subsequent ARG directives are for a 16-bit stack,
1904 * far function call.
1907 StackPointer
= "bp";
1910 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1911 /* All subsequent ARG directives are for a 16-bit stack,
1912 * far function call. We don't support near functions.
1915 StackPointer
= "bp";
1919 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1920 free_tlist(origline
);
1921 return DIRECTIVE_FOUND
;
1923 free_tlist(origline
);
1924 return DIRECTIVE_FOUND
;
1927 /* TASM like ARG directive to define arguments to functions, in
1928 * the following form:
1930 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1934 char *arg
, directive
[256];
1935 int size
= StackSize
;
1937 /* Find the argument name */
1938 tline
= tline
->next
;
1939 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1940 tline
= tline
->next
;
1941 if (!tline
|| tline
->type
!= TOK_ID
) {
1942 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1943 free_tlist(origline
);
1944 return DIRECTIVE_FOUND
;
1948 /* Find the argument size type */
1949 tline
= tline
->next
;
1950 if (!tline
|| tline
->type
!= TOK_OTHER
1951 || tline
->text
[0] != ':') {
1953 "Syntax error processing `%%arg' directive");
1954 free_tlist(origline
);
1955 return DIRECTIVE_FOUND
;
1957 tline
= tline
->next
;
1958 if (!tline
|| tline
->type
!= TOK_ID
) {
1959 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1960 free_tlist(origline
);
1961 return DIRECTIVE_FOUND
;
1964 /* Allow macro expansion of type parameter */
1965 tt
= tokenize(tline
->text
);
1966 tt
= expand_smacro(tt
);
1967 size
= parse_size(tt
->text
);
1970 "Invalid size type for `%%arg' missing directive");
1972 free_tlist(origline
);
1973 return DIRECTIVE_FOUND
;
1977 /* Round up to even stack slots */
1978 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1980 /* Now define the macro for the argument */
1981 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1982 arg
, StackPointer
, offset
);
1983 do_directive(tokenize(directive
));
1986 /* Move to the next argument in the list */
1987 tline
= tline
->next
;
1988 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1989 tline
= tline
->next
;
1990 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1992 free_tlist(origline
);
1993 return DIRECTIVE_FOUND
;
1996 /* TASM like LOCAL directive to define local variables for a
1997 * function, in the following form:
1999 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2001 * The '= LocalSize' at the end is ignored by NASM, but is
2002 * required by TASM to define the local parameter size (and used
2003 * by the TASM macro package).
2005 offset
= LocalOffset
;
2007 char *local
, directive
[256];
2008 int size
= StackSize
;
2010 /* Find the argument name */
2011 tline
= tline
->next
;
2012 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2013 tline
= tline
->next
;
2014 if (!tline
|| tline
->type
!= TOK_ID
) {
2016 "`%%local' missing argument parameter");
2017 free_tlist(origline
);
2018 return DIRECTIVE_FOUND
;
2020 local
= tline
->text
;
2022 /* Find the argument size type */
2023 tline
= tline
->next
;
2024 if (!tline
|| tline
->type
!= TOK_OTHER
2025 || tline
->text
[0] != ':') {
2027 "Syntax error processing `%%local' directive");
2028 free_tlist(origline
);
2029 return DIRECTIVE_FOUND
;
2031 tline
= tline
->next
;
2032 if (!tline
|| tline
->type
!= TOK_ID
) {
2034 "`%%local' missing size type parameter");
2035 free_tlist(origline
);
2036 return DIRECTIVE_FOUND
;
2039 /* Allow macro expansion of type parameter */
2040 tt
= tokenize(tline
->text
);
2041 tt
= expand_smacro(tt
);
2042 size
= parse_size(tt
->text
);
2045 "Invalid size type for `%%local' missing directive");
2047 free_tlist(origline
);
2048 return DIRECTIVE_FOUND
;
2052 /* Round up to even stack slots */
2053 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2055 offset
+= size
; /* Negative offset, increment before */
2057 /* Now define the macro for the argument */
2058 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2059 local
, StackPointer
, offset
);
2060 do_directive(tokenize(directive
));
2062 /* Now define the assign to setup the enter_c macro correctly */
2063 snprintf(directive
, sizeof(directive
),
2064 "%%assign %%$localsize %%$localsize+%d", size
);
2065 do_directive(tokenize(directive
));
2067 /* Move to the next argument in the list */
2068 tline
= tline
->next
;
2069 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2070 tline
= tline
->next
;
2071 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2072 LocalOffset
= offset
;
2073 free_tlist(origline
);
2074 return DIRECTIVE_FOUND
;
2078 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2081 free_tlist(origline
);
2082 return DIRECTIVE_FOUND
;
2085 t
= tline
->next
= expand_smacro(tline
->next
);
2087 if (!t
|| (t
->type
!= TOK_STRING
&&
2088 t
->type
!= TOK_INTERNAL_STRING
)) {
2089 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2090 free_tlist(origline
);
2091 return DIRECTIVE_FOUND
; /* but we did _something_ */
2095 "trailing garbage after `%%depend' ignored");
2097 if (t
->type
!= TOK_INTERNAL_STRING
)
2098 nasm_unquote(p
, NULL
);
2099 if (dephead
&& !in_list(*dephead
, p
)) {
2100 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2104 deptail
= &sl
->next
;
2106 free_tlist(origline
);
2107 return DIRECTIVE_FOUND
;
2110 t
= tline
->next
= expand_smacro(tline
->next
);
2113 if (!t
|| (t
->type
!= TOK_STRING
&&
2114 t
->type
!= TOK_INTERNAL_STRING
)) {
2115 error(ERR_NONFATAL
, "`%%include' expects a file name");
2116 free_tlist(origline
);
2117 return DIRECTIVE_FOUND
; /* but we did _something_ */
2121 "trailing garbage after `%%include' ignored");
2123 if (t
->type
!= TOK_INTERNAL_STRING
)
2124 nasm_unquote(p
, NULL
);
2125 inc
= nasm_malloc(sizeof(Include
));
2128 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2130 /* -MG given but file not found */
2133 inc
->fname
= src_set_fname(nasm_strdup(p
));
2134 inc
->lineno
= src_set_linnum(0);
2136 inc
->expansion
= NULL
;
2139 list
->uplevel(LIST_INCLUDE
);
2141 free_tlist(origline
);
2142 return DIRECTIVE_FOUND
;
2145 tline
= tline
->next
;
2147 tline
= expand_id(tline
);
2148 if (!tok_type_(tline
, TOK_ID
)) {
2149 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2150 free_tlist(origline
);
2151 return DIRECTIVE_FOUND
; /* but we did _something_ */
2154 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2155 ctx
= nasm_malloc(sizeof(Context
));
2157 hash_init(&ctx
->localmac
, HASH_SMALL
);
2158 ctx
->name
= nasm_strdup(tline
->text
);
2159 ctx
->number
= unique
++;
2161 free_tlist(origline
);
2165 tline
= tline
->next
;
2167 tline
= expand_id(tline
);
2168 if (!tok_type_(tline
, TOK_ID
)) {
2169 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2170 free_tlist(origline
);
2171 return DIRECTIVE_FOUND
; /* but we did _something_ */
2174 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2176 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2178 nasm_free(cstk
->name
);
2179 cstk
->name
= nasm_strdup(tline
->text
);
2181 free_tlist(origline
);
2186 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2188 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2191 free_tlist(origline
);
2195 tline
->next
= expand_smacro(tline
->next
);
2196 tline
= tline
->next
;
2198 if (tok_type_(tline
, TOK_STRING
)) {
2200 nasm_unquote(p
, NULL
);
2201 expand_macros_in_string(&p
); /* WHY? */
2202 error(ERR_NONFATAL
, "%s", p
);
2205 p
= detoken(tline
, false);
2206 error(ERR_WARNING
, "%s", p
); /* WARNING!??!! */
2209 free_tlist(origline
);
2213 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2216 j
= if_condition(tline
->next
, i
);
2217 tline
->next
= NULL
; /* it got freed */
2218 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2220 cond
= nasm_malloc(sizeof(Cond
));
2221 cond
->next
= istk
->conds
;
2224 free_tlist(origline
);
2225 return DIRECTIVE_FOUND
;
2229 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2230 if (emitting(istk
->conds
->state
)
2231 || istk
->conds
->state
== COND_NEVER
)
2232 istk
->conds
->state
= COND_NEVER
;
2235 * IMPORTANT: In the case of %if, we will already have
2236 * called expand_mmac_params(); however, if we're
2237 * processing an %elif we must have been in a
2238 * non-emitting mode, which would have inhibited
2239 * the normal invocation of expand_mmac_params(). Therefore,
2240 * we have to do it explicitly here.
2242 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2243 tline
->next
= NULL
; /* it got freed */
2244 istk
->conds
->state
=
2245 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2247 free_tlist(origline
);
2248 return DIRECTIVE_FOUND
;
2252 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2254 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2255 if (emitting(istk
->conds
->state
)
2256 || istk
->conds
->state
== COND_NEVER
)
2257 istk
->conds
->state
= COND_ELSE_FALSE
;
2259 istk
->conds
->state
= COND_ELSE_TRUE
;
2260 free_tlist(origline
);
2261 return DIRECTIVE_FOUND
;
2265 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2267 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2269 istk
->conds
= cond
->next
;
2271 free_tlist(origline
);
2272 return DIRECTIVE_FOUND
;
2278 "`%%%smacro': already defining a macro",
2279 (i
== PP_IMACRO
? "i" : ""));
2280 tline
= tline
->next
;
2282 tline
= expand_id(tline
);
2283 if (!tok_type_(tline
, TOK_ID
)) {
2285 "`%%%smacro' expects a macro name",
2286 (i
== PP_IMACRO
? "i" : ""));
2287 return DIRECTIVE_FOUND
;
2289 defining
= nasm_malloc(sizeof(MMacro
));
2290 defining
->name
= nasm_strdup(tline
->text
);
2291 defining
->casesense
= (i
== PP_MACRO
);
2292 defining
->plus
= false;
2293 defining
->nolist
= false;
2294 defining
->in_progress
= 0;
2295 defining
->rep_nest
= NULL
;
2296 tline
= expand_smacro(tline
->next
);
2298 if (!tok_type_(tline
, TOK_NUMBER
)) {
2300 "`%%%smacro' expects a parameter count",
2301 (i
== PP_IMACRO
? "i" : ""));
2302 defining
->nparam_min
= defining
->nparam_max
= 0;
2304 defining
->nparam_min
= defining
->nparam_max
=
2305 readnum(tline
->text
, &err
);
2308 "unable to parse parameter count `%s'", tline
->text
);
2310 if (tline
&& tok_is_(tline
->next
, "-")) {
2311 tline
= tline
->next
->next
;
2312 if (tok_is_(tline
, "*"))
2313 defining
->nparam_max
= INT_MAX
;
2314 else if (!tok_type_(tline
, TOK_NUMBER
))
2316 "`%%%smacro' expects a parameter count after `-'",
2317 (i
== PP_IMACRO
? "i" : ""));
2319 defining
->nparam_max
= readnum(tline
->text
, &err
);
2322 "unable to parse parameter count `%s'",
2324 if (defining
->nparam_min
> defining
->nparam_max
)
2326 "minimum parameter count exceeds maximum");
2329 if (tline
&& tok_is_(tline
->next
, "+")) {
2330 tline
= tline
->next
;
2331 defining
->plus
= true;
2333 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2334 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2335 tline
= tline
->next
;
2336 defining
->nolist
= true;
2338 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2340 if (!strcmp(mmac
->name
, defining
->name
) &&
2341 (mmac
->nparam_min
<= defining
->nparam_max
2343 && (defining
->nparam_min
<= mmac
->nparam_max
2346 "redefining multi-line macro `%s'", defining
->name
);
2352 * Handle default parameters.
2354 if (tline
&& tline
->next
) {
2355 defining
->dlist
= tline
->next
;
2357 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2358 &defining
->defaults
);
2360 defining
->dlist
= NULL
;
2361 defining
->defaults
= NULL
;
2363 defining
->expansion
= NULL
;
2364 free_tlist(origline
);
2365 return DIRECTIVE_FOUND
;
2370 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2371 return DIRECTIVE_FOUND
;
2373 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2374 defining
->next
= *mmhead
;
2377 free_tlist(origline
);
2378 return DIRECTIVE_FOUND
;
2381 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2382 tline
= tline
->next
;
2383 if (tline
->next
== NULL
) {
2384 free_tlist(origline
);
2385 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2386 return DIRECTIVE_FOUND
;
2388 t
= expand_smacro(tline
->next
);
2390 free_tlist(origline
);
2393 tokval
.t_type
= TOKEN_INVALID
;
2395 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2398 return DIRECTIVE_FOUND
;
2401 "trailing garbage after expression ignored");
2402 if (!is_simple(evalresult
)) {
2403 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2404 return DIRECTIVE_FOUND
;
2407 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2408 mmac
= mmac
->next_active
;
2410 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2411 } else if (mmac
->nparam
== 0) {
2413 "`%%rotate' invoked within macro without parameters");
2415 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2417 rotate
%= (int)mmac
->nparam
;
2419 rotate
+= mmac
->nparam
;
2421 mmac
->rotate
= rotate
;
2423 return DIRECTIVE_FOUND
;
2428 tline
= tline
->next
;
2429 } while (tok_type_(tline
, TOK_WHITESPACE
));
2431 if (tok_type_(tline
, TOK_ID
) &&
2432 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2435 tline
= tline
->next
;
2436 } while (tok_type_(tline
, TOK_WHITESPACE
));
2440 t
= expand_smacro(tline
);
2442 tokval
.t_type
= TOKEN_INVALID
;
2444 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2446 free_tlist(origline
);
2447 return DIRECTIVE_FOUND
;
2451 "trailing garbage after expression ignored");
2452 if (!is_simple(evalresult
)) {
2453 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2454 return DIRECTIVE_FOUND
;
2456 count
= reloc_value(evalresult
) + 1;
2458 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2461 free_tlist(origline
);
2463 tmp_defining
= defining
;
2464 defining
= nasm_malloc(sizeof(MMacro
));
2465 defining
->name
= NULL
; /* flags this macro as a %rep block */
2466 defining
->casesense
= false;
2467 defining
->plus
= false;
2468 defining
->nolist
= nolist
;
2469 defining
->in_progress
= count
;
2470 defining
->nparam_min
= defining
->nparam_max
= 0;
2471 defining
->defaults
= NULL
;
2472 defining
->dlist
= NULL
;
2473 defining
->expansion
= NULL
;
2474 defining
->next_active
= istk
->mstk
;
2475 defining
->rep_nest
= tmp_defining
;
2476 return DIRECTIVE_FOUND
;
2479 if (!defining
|| defining
->name
) {
2480 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2481 return DIRECTIVE_FOUND
;
2485 * Now we have a "macro" defined - although it has no name
2486 * and we won't be entering it in the hash tables - we must
2487 * push a macro-end marker for it on to istk->expansion.
2488 * After that, it will take care of propagating itself (a
2489 * macro-end marker line for a macro which is really a %rep
2490 * block will cause the macro to be re-expanded, complete
2491 * with another macro-end marker to ensure the process
2492 * continues) until the whole expansion is forcibly removed
2493 * from istk->expansion by a %exitrep.
2495 l
= nasm_malloc(sizeof(Line
));
2496 l
->next
= istk
->expansion
;
2497 l
->finishes
= defining
;
2499 istk
->expansion
= l
;
2501 istk
->mstk
= defining
;
2503 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2504 tmp_defining
= defining
;
2505 defining
= defining
->rep_nest
;
2506 free_tlist(origline
);
2507 return DIRECTIVE_FOUND
;
2511 * We must search along istk->expansion until we hit a
2512 * macro-end marker for a macro with no name. Then we set
2513 * its `in_progress' flag to 0.
2515 for (l
= istk
->expansion
; l
; l
= l
->next
)
2516 if (l
->finishes
&& !l
->finishes
->name
)
2520 l
->finishes
->in_progress
= 0;
2522 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2523 free_tlist(origline
);
2524 return DIRECTIVE_FOUND
;
2530 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2532 tline
= tline
->next
;
2534 tline
= expand_id(tline
);
2535 if (!tline
|| (tline
->type
!= TOK_ID
&&
2536 (tline
->type
!= TOK_PREPROC_ID
||
2537 tline
->text
[1] != '$'))) {
2538 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2540 free_tlist(origline
);
2541 return DIRECTIVE_FOUND
;
2544 ctx
= get_ctx(tline
->text
, false);
2546 mname
= tline
->text
;
2548 param_start
= tline
= tline
->next
;
2551 /* Expand the macro definition now for %xdefine and %ixdefine */
2552 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2553 tline
= expand_smacro(tline
);
2555 if (tok_is_(tline
, "(")) {
2557 * This macro has parameters.
2560 tline
= tline
->next
;
2564 error(ERR_NONFATAL
, "parameter identifier expected");
2565 free_tlist(origline
);
2566 return DIRECTIVE_FOUND
;
2568 if (tline
->type
!= TOK_ID
) {
2570 "`%s': parameter identifier expected",
2572 free_tlist(origline
);
2573 return DIRECTIVE_FOUND
;
2575 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2576 tline
= tline
->next
;
2578 if (tok_is_(tline
, ",")) {
2579 tline
= tline
->next
;
2582 if (!tok_is_(tline
, ")")) {
2584 "`)' expected to terminate macro template");
2585 free_tlist(origline
);
2586 return DIRECTIVE_FOUND
;
2591 tline
= tline
->next
;
2593 if (tok_type_(tline
, TOK_WHITESPACE
))
2594 last
= tline
, tline
= tline
->next
;
2599 if (t
->type
== TOK_ID
) {
2600 for (tt
= param_start
; tt
; tt
= tt
->next
)
2601 if (tt
->type
>= TOK_SMAC_PARAM
&&
2602 !strcmp(tt
->text
, t
->text
))
2606 t
->next
= macro_start
;
2611 * Good. We now have a macro name, a parameter count, and a
2612 * token list (in reverse order) for an expansion. We ought
2613 * to be OK just to create an SMacro, store it, and let
2614 * free_tlist have the rest of the line (which we have
2615 * carefully re-terminated after chopping off the expansion
2618 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2619 free_tlist(origline
);
2620 return DIRECTIVE_FOUND
;
2623 tline
= tline
->next
;
2625 tline
= expand_id(tline
);
2626 if (!tline
|| (tline
->type
!= TOK_ID
&&
2627 (tline
->type
!= TOK_PREPROC_ID
||
2628 tline
->text
[1] != '$'))) {
2629 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2630 free_tlist(origline
);
2631 return DIRECTIVE_FOUND
;
2635 "trailing garbage after macro name ignored");
2638 /* Find the context that symbol belongs to */
2639 ctx
= get_ctx(tline
->text
, false);
2640 undef_smacro(ctx
, tline
->text
);
2641 free_tlist(origline
);
2642 return DIRECTIVE_FOUND
;
2646 casesense
= (i
== PP_DEFSTR
);
2648 tline
= tline
->next
;
2650 tline
= expand_id(tline
);
2651 if (!tline
|| (tline
->type
!= TOK_ID
&&
2652 (tline
->type
!= TOK_PREPROC_ID
||
2653 tline
->text
[1] != '$'))) {
2654 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2656 free_tlist(origline
);
2657 return DIRECTIVE_FOUND
;
2660 ctx
= get_ctx(tline
->text
, false);
2662 mname
= tline
->text
;
2664 tline
= expand_smacro(tline
->next
);
2667 while (tok_type_(tline
, TOK_WHITESPACE
))
2668 tline
= delete_Token(tline
);
2670 p
= detoken(tline
, false);
2671 macro_start
= nasm_malloc(sizeof(*macro_start
));
2672 macro_start
->next
= NULL
;
2673 macro_start
->text
= nasm_quote(p
, strlen(p
));
2674 macro_start
->type
= TOK_STRING
;
2675 macro_start
->mac
= NULL
;
2679 * We now have a macro name, an implicit parameter count of
2680 * zero, and a string token to use as an expansion. Create
2681 * and store an SMacro.
2683 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2684 free_tlist(origline
);
2685 return DIRECTIVE_FOUND
;
2690 StrList
*xsl
= NULL
;
2691 StrList
**xst
= &xsl
;
2695 tline
= tline
->next
;
2697 tline
= expand_id(tline
);
2698 if (!tline
|| (tline
->type
!= TOK_ID
&&
2699 (tline
->type
!= TOK_PREPROC_ID
||
2700 tline
->text
[1] != '$'))) {
2702 "`%%pathsearch' expects a macro identifier as first parameter");
2703 free_tlist(origline
);
2704 return DIRECTIVE_FOUND
;
2706 ctx
= get_ctx(tline
->text
, false);
2708 mname
= tline
->text
;
2710 tline
= expand_smacro(tline
->next
);
2714 while (tok_type_(t
, TOK_WHITESPACE
))
2717 if (!t
|| (t
->type
!= TOK_STRING
&&
2718 t
->type
!= TOK_INTERNAL_STRING
)) {
2719 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2721 free_tlist(origline
);
2722 return DIRECTIVE_FOUND
; /* but we did _something_ */
2726 "trailing garbage after `%%pathsearch' ignored");
2728 if (t
->type
!= TOK_INTERNAL_STRING
)
2729 nasm_unquote(p
, NULL
);
2731 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2734 fclose(fp
); /* Don't actually care about the file */
2736 macro_start
= nasm_malloc(sizeof(*macro_start
));
2737 macro_start
->next
= NULL
;
2738 macro_start
->text
= nasm_quote(p
, strlen(p
));
2739 macro_start
->type
= TOK_STRING
;
2740 macro_start
->mac
= NULL
;
2745 * We now have a macro name, an implicit parameter count of
2746 * zero, and a string token to use as an expansion. Create
2747 * and store an SMacro.
2749 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2751 free_tlist(origline
);
2752 return DIRECTIVE_FOUND
;
2758 tline
= tline
->next
;
2760 tline
= expand_id(tline
);
2761 if (!tline
|| (tline
->type
!= TOK_ID
&&
2762 (tline
->type
!= TOK_PREPROC_ID
||
2763 tline
->text
[1] != '$'))) {
2765 "`%%strlen' expects a macro identifier as first parameter");
2766 free_tlist(origline
);
2767 return DIRECTIVE_FOUND
;
2769 ctx
= get_ctx(tline
->text
, false);
2771 mname
= tline
->text
;
2773 tline
= expand_smacro(tline
->next
);
2777 while (tok_type_(t
, TOK_WHITESPACE
))
2779 /* t should now point to the string */
2780 if (t
->type
!= TOK_STRING
) {
2782 "`%%strlen` requires string as second parameter");
2784 free_tlist(origline
);
2785 return DIRECTIVE_FOUND
;
2788 macro_start
= nasm_malloc(sizeof(*macro_start
));
2789 macro_start
->next
= NULL
;
2790 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2791 macro_start
->mac
= NULL
;
2794 * We now have a macro name, an implicit parameter count of
2795 * zero, and a numeric token to use as an expansion. Create
2796 * and store an SMacro.
2798 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2800 free_tlist(origline
);
2801 return DIRECTIVE_FOUND
;
2810 tline
= tline
->next
;
2812 tline
= expand_id(tline
);
2813 if (!tline
|| (tline
->type
!= TOK_ID
&&
2814 (tline
->type
!= TOK_PREPROC_ID
||
2815 tline
->text
[1] != '$'))) {
2817 "`%%substr' expects a macro identifier as first parameter");
2818 free_tlist(origline
);
2819 return DIRECTIVE_FOUND
;
2821 ctx
= get_ctx(tline
->text
, false);
2823 mname
= tline
->text
;
2825 tline
= expand_smacro(tline
->next
);
2829 while (tok_type_(t
, TOK_WHITESPACE
))
2832 /* t should now point to the string */
2833 if (t
->type
!= TOK_STRING
) {
2835 "`%%substr` requires string as second parameter");
2837 free_tlist(origline
);
2838 return DIRECTIVE_FOUND
;
2843 tokval
.t_type
= TOKEN_INVALID
;
2844 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2848 free_tlist(origline
);
2849 return DIRECTIVE_FOUND
;
2850 } else if (!is_simple(evalresult
)) {
2851 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2853 free_tlist(origline
);
2854 return DIRECTIVE_FOUND
;
2856 a1
= evalresult
->value
-1;
2858 while (tok_type_(tt
, TOK_WHITESPACE
))
2861 a2
= 1; /* Backwards compatibility: one character */
2863 tokval
.t_type
= TOKEN_INVALID
;
2864 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2868 free_tlist(origline
);
2869 return DIRECTIVE_FOUND
;
2870 } else if (!is_simple(evalresult
)) {
2871 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2873 free_tlist(origline
);
2874 return DIRECTIVE_FOUND
;
2876 a2
= evalresult
->value
;
2879 len
= nasm_unquote(t
->text
, NULL
);
2882 if (a1
+a2
> (int64_t)len
)
2885 macro_start
= nasm_malloc(sizeof(*macro_start
));
2886 macro_start
->next
= NULL
;
2887 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
2888 macro_start
->type
= TOK_STRING
;
2889 macro_start
->mac
= NULL
;
2892 * We now have a macro name, an implicit parameter count of
2893 * zero, and a numeric token to use as an expansion. Create
2894 * and store an SMacro.
2896 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2898 free_tlist(origline
);
2899 return DIRECTIVE_FOUND
;
2904 casesense
= (i
== PP_ASSIGN
);
2906 tline
= tline
->next
;
2908 tline
= expand_id(tline
);
2909 if (!tline
|| (tline
->type
!= TOK_ID
&&
2910 (tline
->type
!= TOK_PREPROC_ID
||
2911 tline
->text
[1] != '$'))) {
2913 "`%%%sassign' expects a macro identifier",
2914 (i
== PP_IASSIGN
? "i" : ""));
2915 free_tlist(origline
);
2916 return DIRECTIVE_FOUND
;
2918 ctx
= get_ctx(tline
->text
, false);
2920 mname
= tline
->text
;
2922 tline
= expand_smacro(tline
->next
);
2927 tokval
.t_type
= TOKEN_INVALID
;
2929 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2932 free_tlist(origline
);
2933 return DIRECTIVE_FOUND
;
2938 "trailing garbage after expression ignored");
2940 if (!is_simple(evalresult
)) {
2942 "non-constant value given to `%%%sassign'",
2943 (i
== PP_IASSIGN
? "i" : ""));
2944 free_tlist(origline
);
2945 return DIRECTIVE_FOUND
;
2948 macro_start
= nasm_malloc(sizeof(*macro_start
));
2949 macro_start
->next
= NULL
;
2950 make_tok_num(macro_start
, reloc_value(evalresult
));
2951 macro_start
->mac
= NULL
;
2954 * We now have a macro name, an implicit parameter count of
2955 * zero, and a numeric token to use as an expansion. Create
2956 * and store an SMacro.
2958 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2959 free_tlist(origline
);
2960 return DIRECTIVE_FOUND
;
2964 * Syntax is `%line nnn[+mmm] [filename]'
2966 tline
= tline
->next
;
2968 if (!tok_type_(tline
, TOK_NUMBER
)) {
2969 error(ERR_NONFATAL
, "`%%line' expects line number");
2970 free_tlist(origline
);
2971 return DIRECTIVE_FOUND
;
2973 k
= readnum(tline
->text
, &err
);
2975 tline
= tline
->next
;
2976 if (tok_is_(tline
, "+")) {
2977 tline
= tline
->next
;
2978 if (!tok_type_(tline
, TOK_NUMBER
)) {
2979 error(ERR_NONFATAL
, "`%%line' expects line increment");
2980 free_tlist(origline
);
2981 return DIRECTIVE_FOUND
;
2983 m
= readnum(tline
->text
, &err
);
2984 tline
= tline
->next
;
2990 nasm_free(src_set_fname(detoken(tline
, false)));
2992 free_tlist(origline
);
2993 return DIRECTIVE_FOUND
;
2997 "preprocessor directive `%s' not yet implemented",
3001 return DIRECTIVE_FOUND
;
3005 * Ensure that a macro parameter contains a condition code and
3006 * nothing else. Return the condition code index if so, or -1
3009 static int find_cc(Token
* t
)
3015 return -1; /* Probably a %+ without a space */
3018 if (t
->type
!= TOK_ID
)
3022 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3026 j
= elements(conditions
);
3029 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3045 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3046 * %-n) and MMacro-local identifiers (%%foo).
3048 static Token
*expand_mmac_params(Token
* tline
)
3050 Token
*t
, *tt
, **tail
, *thead
;
3056 if (tline
->type
== TOK_PREPROC_ID
&&
3057 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3058 && tline
->text
[2]) || tline
->text
[1] == '%'
3059 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3061 int type
= 0, cc
; /* type = 0 to placate optimisers */
3068 tline
= tline
->next
;
3071 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3072 mac
= mac
->next_active
;
3074 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3076 switch (t
->text
[1]) {
3078 * We have to make a substitution of one of the
3079 * forms %1, %-1, %+1, %%foo, %0.
3083 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3084 text
= nasm_strdup(tmpbuf
);
3088 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3090 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3093 n
= atoi(t
->text
+ 2) - 1;
3094 if (n
>= mac
->nparam
)
3097 if (mac
->nparam
> 1)
3098 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3099 tt
= mac
->params
[n
];
3104 "macro parameter %d is not a condition code",
3109 if (inverse_ccs
[cc
] == -1) {
3111 "condition code `%s' is not invertible",
3116 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3120 n
= atoi(t
->text
+ 2) - 1;
3121 if (n
>= mac
->nparam
)
3124 if (mac
->nparam
> 1)
3125 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3126 tt
= mac
->params
[n
];
3131 "macro parameter %d is not a condition code",
3136 text
= nasm_strdup(conditions
[cc
]);
3140 n
= atoi(t
->text
+ 1) - 1;
3141 if (n
>= mac
->nparam
)
3144 if (mac
->nparam
> 1)
3145 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3146 tt
= mac
->params
[n
];
3149 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3150 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3151 tail
= &(*tail
)->next
;
3155 text
= NULL
; /* we've done it here */
3171 tline
= tline
->next
;
3178 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3180 case TOK_WHITESPACE
:
3181 if (tt
->type
== TOK_WHITESPACE
) {
3182 t
->next
= delete_Token(tt
);
3186 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3187 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3190 t
->next
= delete_Token(tt
);
3194 if (tt
->type
== TOK_NUMBER
) {
3195 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3198 t
->next
= delete_Token(tt
);
3209 * Expand all single-line macro calls made in the given line.
3210 * Return the expanded version of the line. The original is deemed
3211 * to be destroyed in the process. (In reality we'll just move
3212 * Tokens from input to output a lot of the time, rather than
3213 * actually bothering to destroy and replicate.)
3215 #define DEADMAN_LIMIT (1 << 20)
3217 static Token
*expand_smacro(Token
* tline
)
3219 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3220 struct hash_table
*smtbl
;
3221 SMacro
*head
= NULL
, *m
;
3224 unsigned int nparam
, sparam
;
3225 int brackets
, rescan
;
3226 Token
*org_tline
= tline
;
3229 int deadman
= DEADMAN_LIMIT
;
3232 * Trick: we should avoid changing the start token pointer since it can
3233 * be contained in "next" field of other token. Because of this
3234 * we allocate a copy of first token and work with it; at the end of
3235 * routine we copy it back
3239 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3241 tline
->mac
= org_tline
->mac
;
3242 nasm_free(org_tline
->text
);
3243 org_tline
->text
= NULL
;
3250 while (tline
) { /* main token loop */
3252 error(ERR_NONFATAL
, "interminable macro recursion");
3256 if ((mname
= tline
->text
)) {
3257 /* if this token is a local macro, look in local context */
3260 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3261 ctx
= get_ctx(mname
, true);
3263 smtbl
= &ctx
->localmac
;
3265 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3268 * We've hit an identifier. As in is_mmacro below, we first
3269 * check whether the identifier is a single-line macro at
3270 * all, then think about checking for parameters if
3273 for (m
= head
; m
; m
= m
->next
)
3274 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3280 if (m
->nparam
== 0) {
3282 * Simple case: the macro is parameterless. Discard the
3283 * one token that the macro call took, and push the
3284 * expansion back on the to-do stack.
3286 if (!m
->expansion
) {
3287 if (!strcmp("__FILE__", m
->name
)) {
3290 src_get(&num
, &file
);
3291 tline
->text
= nasm_quote(file
, strlen(file
));
3292 tline
->type
= TOK_STRING
;
3296 if (!strcmp("__LINE__", m
->name
)) {
3297 nasm_free(tline
->text
);
3298 make_tok_num(tline
, src_get_linnum());
3301 if (!strcmp("__BITS__", m
->name
)) {
3302 nasm_free(tline
->text
);
3303 make_tok_num(tline
, globalbits
);
3306 tline
= delete_Token(tline
);
3311 * Complicated case: at least one macro with this name
3312 * exists and takes parameters. We must find the
3313 * parameters in the call, count them, find the SMacro
3314 * that corresponds to that form of the macro call, and
3315 * substitute for the parameters when we expand. What a
3318 /*tline = tline->next;
3319 skip_white_(tline); */
3322 while (tok_type_(t
, TOK_SMAC_END
)) {
3323 t
->mac
->in_progress
= false;
3325 t
= tline
->next
= delete_Token(t
);
3328 } while (tok_type_(tline
, TOK_WHITESPACE
));
3329 if (!tok_is_(tline
, "(")) {
3331 * This macro wasn't called with parameters: ignore
3332 * the call. (Behaviour borrowed from gnu cpp.)
3341 sparam
= PARAM_DELTA
;
3342 params
= nasm_malloc(sparam
* sizeof(Token
*));
3343 params
[0] = tline
->next
;
3344 paramsize
= nasm_malloc(sparam
* sizeof(int));
3346 while (true) { /* parameter loop */
3348 * For some unusual expansions
3349 * which concatenates function call
3352 while (tok_type_(t
, TOK_SMAC_END
)) {
3353 t
->mac
->in_progress
= false;
3355 t
= tline
->next
= delete_Token(t
);
3361 "macro call expects terminating `)'");
3364 if (tline
->type
== TOK_WHITESPACE
3366 if (paramsize
[nparam
])
3369 params
[nparam
] = tline
->next
;
3370 continue; /* parameter loop */
3372 if (tline
->type
== TOK_OTHER
3373 && tline
->text
[1] == 0) {
3374 char ch
= tline
->text
[0];
3375 if (ch
== ',' && !paren
&& brackets
<= 0) {
3376 if (++nparam
>= sparam
) {
3377 sparam
+= PARAM_DELTA
;
3378 params
= nasm_realloc(params
,
3383 nasm_realloc(paramsize
,
3387 params
[nparam
] = tline
->next
;
3388 paramsize
[nparam
] = 0;
3390 continue; /* parameter loop */
3393 (brackets
> 0 || (brackets
== 0 &&
3394 !paramsize
[nparam
])))
3396 if (!(brackets
++)) {
3397 params
[nparam
] = tline
->next
;
3398 continue; /* parameter loop */
3401 if (ch
== '}' && brackets
> 0)
3402 if (--brackets
== 0) {
3404 continue; /* parameter loop */
3406 if (ch
== '(' && !brackets
)
3408 if (ch
== ')' && brackets
<= 0)
3414 error(ERR_NONFATAL
, "braces do not "
3415 "enclose all of macro parameter");
3417 paramsize
[nparam
] += white
+ 1;
3419 } /* parameter loop */
3421 while (m
&& (m
->nparam
!= nparam
||
3422 mstrcmp(m
->name
, mname
,
3426 error(ERR_WARNING
| ERR_WARN_MNP
,
3427 "macro `%s' exists, "
3428 "but not taking %d parameters",
3429 mstart
->text
, nparam
);
3432 if (m
&& m
->in_progress
)
3434 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3436 * Design question: should we handle !tline, which
3437 * indicates missing ')' here, or expand those
3438 * macros anyway, which requires the (t) test a few
3442 nasm_free(paramsize
);
3446 * Expand the macro: we are placed on the last token of the
3447 * call, so that we can easily split the call from the
3448 * following tokens. We also start by pushing an SMAC_END
3449 * token for the cycle removal.
3456 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3458 m
->in_progress
= true;
3460 for (t
= m
->expansion
; t
; t
= t
->next
) {
3461 if (t
->type
>= TOK_SMAC_PARAM
) {
3462 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3466 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3467 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3470 new_Token(tline
, ttt
->type
, ttt
->text
,
3476 } else if (t
->type
== TOK_PREPROC_Q
) {
3477 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3479 } else if (t
->type
== TOK_PREPROC_QQ
) {
3480 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3483 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3489 * Having done that, get rid of the macro call, and clean
3490 * up the parameters.
3493 nasm_free(paramsize
);
3495 continue; /* main token loop */
3500 if (tline
->type
== TOK_SMAC_END
) {
3501 tline
->mac
->in_progress
= false;
3502 tline
= delete_Token(tline
);
3505 tline
= tline
->next
;
3513 * Now scan the entire line and look for successive TOK_IDs that resulted
3514 * after expansion (they can't be produced by tokenize()). The successive
3515 * TOK_IDs should be concatenated.
3516 * Also we look for %+ tokens and concatenate the tokens before and after
3517 * them (without white spaces in between).
3522 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3526 if (t
->next
->type
== TOK_ID
||
3527 t
->next
->type
== TOK_PREPROC_ID
||
3528 t
->next
->type
== TOK_NUMBER
) {
3529 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3531 t
->next
= delete_Token(t
->next
);
3534 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3535 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3536 strcmp(t
->next
->next
->text
, "%+") == 0) {
3537 /* free the next whitespace, the %+ token and next whitespace */
3539 for (i
= 1; i
<= 3; i
++) {
3541 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3543 t
->next
= delete_Token(t
->next
);
3548 /* If we concatenaded something, re-scan the line for macros */
3556 *org_tline
= *thead
;
3557 /* since we just gave text to org_line, don't free it */
3559 delete_Token(thead
);
3561 /* the expression expanded to empty line;
3562 we can't return NULL for some reasons
3563 we just set the line to a single WHITESPACE token. */
3564 memset(org_tline
, 0, sizeof(*org_tline
));
3565 org_tline
->text
= NULL
;
3566 org_tline
->type
= TOK_WHITESPACE
;
3575 * Similar to expand_smacro but used exclusively with macro identifiers
3576 * right before they are fetched in. The reason is that there can be
3577 * identifiers consisting of several subparts. We consider that if there
3578 * are more than one element forming the name, user wants a expansion,
3579 * otherwise it will be left as-is. Example:
3583 * the identifier %$abc will be left as-is so that the handler for %define
3584 * will suck it and define the corresponding value. Other case:
3586 * %define _%$abc cde
3588 * In this case user wants name to be expanded *before* %define starts
3589 * working, so we'll expand %$abc into something (if it has a value;
3590 * otherwise it will be left as-is) then concatenate all successive
3593 static Token
*expand_id(Token
* tline
)
3595 Token
*cur
, *oldnext
= NULL
;
3597 if (!tline
|| !tline
->next
)
3602 (cur
->next
->type
== TOK_ID
||
3603 cur
->next
->type
== TOK_PREPROC_ID
3604 || cur
->next
->type
== TOK_NUMBER
))
3607 /* If identifier consists of just one token, don't expand */
3612 oldnext
= cur
->next
; /* Detach the tail past identifier */
3613 cur
->next
= NULL
; /* so that expand_smacro stops here */
3616 tline
= expand_smacro(tline
);
3619 /* expand_smacro possibly changhed tline; re-scan for EOL */
3621 while (cur
&& cur
->next
)
3624 cur
->next
= oldnext
;
3631 * Determine whether the given line constitutes a multi-line macro
3632 * call, and return the MMacro structure called if so. Doesn't have
3633 * to check for an initial label - that's taken care of in
3634 * expand_mmacro - but must check numbers of parameters. Guaranteed
3635 * to be called with tline->type == TOK_ID, so the putative macro
3636 * name is easy to find.
3638 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3644 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3647 * Efficiency: first we see if any macro exists with the given
3648 * name. If not, we can return NULL immediately. _Then_ we
3649 * count the parameters, and then we look further along the
3650 * list if necessary to find the proper MMacro.
3652 for (m
= head
; m
; m
= m
->next
)
3653 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3659 * OK, we have a potential macro. Count and demarcate the
3662 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3665 * So we know how many parameters we've got. Find the MMacro
3666 * structure that handles this number.
3669 if (m
->nparam_min
<= nparam
3670 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3672 * This one is right. Just check if cycle removal
3673 * prohibits us using it before we actually celebrate...
3675 if (m
->in_progress
) {
3678 "self-reference in multi-line macro `%s'", m
->name
);
3684 * It's right, and we can use it. Add its default
3685 * parameters to the end of our list if necessary.
3687 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3689 nasm_realloc(params
,
3690 ((m
->nparam_min
+ m
->ndefs
+
3691 1) * sizeof(*params
)));
3692 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3693 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3698 * If we've gone over the maximum parameter count (and
3699 * we're in Plus mode), ignore parameters beyond
3702 if (m
->plus
&& nparam
> m
->nparam_max
)
3703 nparam
= m
->nparam_max
;
3705 * Then terminate the parameter list, and leave.
3707 if (!params
) { /* need this special case */
3708 params
= nasm_malloc(sizeof(*params
));
3711 params
[nparam
] = NULL
;
3712 *params_array
= params
;
3716 * This one wasn't right: look for the next one with the
3719 for (m
= m
->next
; m
; m
= m
->next
)
3720 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3725 * After all that, we didn't find one with the right number of
3726 * parameters. Issue a warning, and fail to expand the macro.
3728 error(ERR_WARNING
| ERR_WARN_MNP
,
3729 "macro `%s' exists, but not taking %d parameters",
3730 tline
->text
, nparam
);
3736 * Expand the multi-line macro call made by the given line, if
3737 * there is one to be expanded. If there is, push the expansion on
3738 * istk->expansion and return 1. Otherwise return 0.
3740 static int expand_mmacro(Token
* tline
)
3742 Token
*startline
= tline
;
3743 Token
*label
= NULL
;
3744 int dont_prepend
= 0;
3745 Token
**params
, *t
, *mtok
, *tt
;
3748 int i
, nparam
, *paramlen
;
3753 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3754 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3757 m
= is_mmacro(t
, ¶ms
);
3763 * We have an id which isn't a macro call. We'll assume
3764 * it might be a label; we'll also check to see if a
3765 * colon follows it. Then, if there's another id after
3766 * that lot, we'll check it again for macro-hood.
3770 if (tok_type_(t
, TOK_WHITESPACE
))
3771 last
= t
, t
= t
->next
;
3772 if (tok_is_(t
, ":")) {
3774 last
= t
, t
= t
->next
;
3775 if (tok_type_(t
, TOK_WHITESPACE
))
3776 last
= t
, t
= t
->next
;
3778 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3786 * Fix up the parameters: this involves stripping leading and
3787 * trailing whitespace, then stripping braces if they are
3790 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3791 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3793 for (i
= 0; params
[i
]; i
++) {
3795 int comma
= (!m
->plus
|| i
< nparam
- 1);
3799 if (tok_is_(t
, "{"))
3800 t
= t
->next
, brace
= true, comma
= false;
3804 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3805 break; /* ... because we have hit a comma */
3806 if (comma
&& t
->type
== TOK_WHITESPACE
3807 && tok_is_(t
->next
, ","))
3808 break; /* ... or a space then a comma */
3809 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3810 break; /* ... or a brace */
3817 * OK, we have a MMacro structure together with a set of
3818 * parameters. We must now go through the expansion and push
3819 * copies of each Line on to istk->expansion. Substitution of
3820 * parameter tokens and macro-local tokens doesn't get done
3821 * until the single-line macro substitution process; this is
3822 * because delaying them allows us to change the semantics
3823 * later through %rotate.
3825 * First, push an end marker on to istk->expansion, mark this
3826 * macro as in progress, and set up its invocation-specific
3829 ll
= nasm_malloc(sizeof(Line
));
3830 ll
->next
= istk
->expansion
;
3833 istk
->expansion
= ll
;
3835 m
->in_progress
= true;
3840 m
->paramlen
= paramlen
;
3841 m
->unique
= unique
++;
3844 m
->next_active
= istk
->mstk
;
3847 for (l
= m
->expansion
; l
; l
= l
->next
) {
3850 ll
= nasm_malloc(sizeof(Line
));
3851 ll
->finishes
= NULL
;
3852 ll
->next
= istk
->expansion
;
3853 istk
->expansion
= ll
;
3856 for (t
= l
->first
; t
; t
= t
->next
) {
3860 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
3862 case TOK_PREPROC_QQ
:
3863 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3865 case TOK_PREPROC_ID
:
3866 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3874 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3883 * If we had a label, push it on as the first line of
3884 * the macro expansion.
3887 if (dont_prepend
< 0)
3888 free_tlist(startline
);
3890 ll
= nasm_malloc(sizeof(Line
));
3891 ll
->finishes
= NULL
;
3892 ll
->next
= istk
->expansion
;
3893 istk
->expansion
= ll
;
3894 ll
->first
= startline
;
3895 if (!dont_prepend
) {
3897 label
= label
->next
;
3898 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3903 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3909 * Since preprocessor always operate only on the line that didn't
3910 * arrived yet, we should always use ERR_OFFBY1. Also since user
3911 * won't want to see same error twice (preprocessing is done once
3912 * per pass) we will want to show errors only during pass one.
3914 static void error(int severity
, const char *fmt
, ...)
3919 /* If we're in a dead branch of IF or something like it, ignore the error */
3920 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3924 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3927 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3928 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3929 istk
->mstk
->lineno
, buff
);
3931 _error(severity
| ERR_PASS1
, "%s", buff
);
3935 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3936 ListGen
* listgen
, StrList
**deplist
)
3940 istk
= nasm_malloc(sizeof(Include
));
3943 istk
->expansion
= NULL
;
3945 istk
->fp
= fopen(file
, "r");
3947 src_set_fname(nasm_strdup(file
));
3951 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3956 if (tasm_compatible_mode
) {
3957 stdmacpos
= nasm_stdmac
;
3959 stdmacpos
= nasm_stdmac_after_tasm
;
3961 any_extrastdmac
= (extrastdmac
!= NULL
);
3965 dephead
= deptail
= deplist
;
3967 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
3969 strcpy(sl
->str
, file
);
3971 deptail
= &sl
->next
;
3975 static char *pp_getline(void)
3982 * Fetch a tokenized line, either from the macro-expansion
3983 * buffer or from the input file.
3986 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3987 Line
*l
= istk
->expansion
;
3988 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3992 * This is a macro-end marker for a macro with no
3993 * name, which means it's not really a macro at all
3994 * but a %rep block, and the `in_progress' field is
3995 * more than 1, meaning that we still need to
3996 * repeat. (1 means the natural last repetition; 0
3997 * means termination by %exitrep.) We have
3998 * therefore expanded up to the %endrep, and must
3999 * push the whole block on to the expansion buffer
4000 * again. We don't bother to remove the macro-end
4001 * marker: we'd only have to generate another one
4004 l
->finishes
->in_progress
--;
4005 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4006 Token
*t
, *tt
, **tail
;
4008 ll
= nasm_malloc(sizeof(Line
));
4009 ll
->next
= istk
->expansion
;
4010 ll
->finishes
= NULL
;
4014 for (t
= l
->first
; t
; t
= t
->next
) {
4015 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4017 new_Token(NULL
, t
->type
, t
->text
, 0);
4022 istk
->expansion
= ll
;
4026 * Check whether a `%rep' was started and not ended
4027 * within this macro expansion. This can happen and
4028 * should be detected. It's a fatal error because
4029 * I'm too confused to work out how to recover
4035 "defining with name in expansion");
4036 else if (istk
->mstk
->name
)
4038 "`%%rep' without `%%endrep' within"
4039 " expansion of macro `%s'",
4044 * FIXME: investigate the relationship at this point between
4045 * istk->mstk and l->finishes
4048 MMacro
*m
= istk
->mstk
;
4049 istk
->mstk
= m
->next_active
;
4052 * This was a real macro call, not a %rep, and
4053 * therefore the parameter information needs to
4056 nasm_free(m
->params
);
4057 free_tlist(m
->iline
);
4058 nasm_free(m
->paramlen
);
4059 l
->finishes
->in_progress
= false;
4063 istk
->expansion
= l
->next
;
4065 list
->downlevel(LIST_MACRO
);
4068 while (1) { /* until we get a line we can use */
4070 if (istk
->expansion
) { /* from a macro expansion */
4072 Line
*l
= istk
->expansion
;
4074 istk
->mstk
->lineno
++;
4076 istk
->expansion
= l
->next
;
4078 p
= detoken(tline
, false);
4079 list
->line(LIST_MACRO
, p
);
4084 if (line
) { /* from the current input file */
4085 line
= prepreproc(line
);
4086 tline
= tokenize(line
);
4091 * The current file has ended; work down the istk
4098 "expected `%%endif' before end of file");
4099 /* only set line and file name if there's a next node */
4101 src_set_linnum(i
->lineno
);
4102 nasm_free(src_set_fname(i
->fname
));
4105 list
->downlevel(LIST_INCLUDE
);
4113 * We must expand MMacro parameters and MMacro-local labels
4114 * _before_ we plunge into directive processing, to cope
4115 * with things like `%define something %1' such as STRUC
4116 * uses. Unless we're _defining_ a MMacro, in which case
4117 * those tokens should be left alone to go into the
4118 * definition; and unless we're in a non-emitting
4119 * condition, in which case we don't want to meddle with
4122 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4123 tline
= expand_mmac_params(tline
);
4126 * Check the line to see if it's a preprocessor directive.
4128 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4130 } else if (defining
) {
4132 * We're defining a multi-line macro. We emit nothing
4134 * shove the tokenized line on to the macro definition.
4136 Line
*l
= nasm_malloc(sizeof(Line
));
4137 l
->next
= defining
->expansion
;
4139 l
->finishes
= false;
4140 defining
->expansion
= l
;
4142 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4144 * We're in a non-emitting branch of a condition block.
4145 * Emit nothing at all, not even a blank line: when we
4146 * emerge from the condition we'll give a line-number
4147 * directive so we keep our place correctly.
4151 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4153 * We're in a %rep block which has been terminated, so
4154 * we're walking through to the %endrep without
4155 * emitting anything. Emit nothing at all, not even a
4156 * blank line: when we emerge from the %rep block we'll
4157 * give a line-number directive so we keep our place
4163 tline
= expand_smacro(tline
);
4164 if (!expand_mmacro(tline
)) {
4166 * De-tokenize the line again, and emit it.
4168 line
= detoken(tline
, true);
4172 continue; /* expand_mmacro calls free_tlist */
4180 static void pp_cleanup(int pass
)
4183 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4185 free_mmacro(defining
);
4194 nasm_free(i
->fname
);
4205 void pp_include_path(char *path
)
4209 i
= nasm_malloc(sizeof(IncPath
));
4210 i
->path
= path
? nasm_strdup(path
) : NULL
;
4213 if (ipath
!= NULL
) {
4215 while (j
->next
!= NULL
)
4223 void pp_pre_include(char *fname
)
4225 Token
*inc
, *space
, *name
;
4228 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4229 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4230 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4232 l
= nasm_malloc(sizeof(Line
));
4235 l
->finishes
= false;
4239 void pp_pre_define(char *definition
)
4245 equals
= strchr(definition
, '=');
4246 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4247 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4250 space
->next
= tokenize(definition
);
4254 l
= nasm_malloc(sizeof(Line
));
4257 l
->finishes
= false;
4261 void pp_pre_undefine(char *definition
)
4266 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4267 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4268 space
->next
= tokenize(definition
);
4270 l
= nasm_malloc(sizeof(Line
));
4273 l
->finishes
= false;
4278 * Added by Keith Kanios:
4280 * This function is used to assist with "runtime" preprocessor
4281 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4283 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4284 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4287 void pp_runtime(char *definition
)
4291 def
= tokenize(definition
);
4292 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4297 void pp_extra_stdmac(const char **macros
)
4299 extrastdmac
= macros
;
4302 static void make_tok_num(Token
* tok
, int64_t val
)
4305 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4306 tok
->text
= nasm_strdup(numbuf
);
4307 tok
->type
= TOK_NUMBER
;