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 */
170 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
171 size_t len
; /* scratch length field */
172 } a
; /* Auxiliary data */
173 enum pp_token_type type
;
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
215 MMacro
*mstk
; /* stack of active macros/reps */
219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
248 COND_IF_TRUE
, COND_IF_FALSE
,
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
254 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
256 * This state means that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. It's for use in
258 * two circumstances: (i) when we've had our moment of emission
259 * and have now started seeing %elifs, and (ii) when the
260 * condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct.
265 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
268 * These defines are used as the possible return values for do_directive
270 #define NO_DIRECTIVE_FOUND 0
271 #define DIRECTIVE_FOUND 1
274 * Condition codes. Note that we use c_ prefix not C_ because C_ is
275 * used in nasm.h for the "real" condition codes. At _this_ level,
276 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
277 * ones, so we need a different enum...
279 static const char * const conditions
[] = {
280 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
281 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
282 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
285 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
286 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
287 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
290 static const enum pp_conds inverse_ccs
[] = {
291 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
292 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
,
293 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
299 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
300 static int is_condition(enum preproc_token arg
)
302 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
305 /* For TASM compatibility we need to be able to recognise TASM compatible
306 * conditional compilation directives. Using the NASM pre-processor does
307 * not work, so we look for them specifically from the following list and
308 * then jam in the equivalent NASM directive into the input stream.
312 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
313 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
316 static const char * const tasm_directives
[] = {
317 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
318 "ifndef", "include", "local"
321 static int StackSize
= 4;
322 static char *StackPointer
= "ebp";
323 static int ArgOffset
= 8;
324 static int LocalOffset
= 0;
326 static Context
*cstk
;
327 static Include
*istk
;
328 static IncPath
*ipath
= NULL
;
330 static efunc _error
; /* Pointer to client-provided error reporting function */
331 static evalfunc evaluate
;
333 static int pass
; /* HACK: pass 0 = generate dependencies only */
334 static StrList
**dephead
, **deptail
; /* Dependency list */
336 static uint64_t unique
; /* unique identifier numbers */
338 static Line
*predef
= NULL
;
339 static bool do_predef
;
341 static ListGen
*list
;
344 * The current set of multi-line macros we have defined.
346 static struct hash_table mmacros
;
349 * The current set of single-line macros we have defined.
351 static struct hash_table smacros
;
354 * The multi-line macro we are currently defining, or the %rep
355 * block we are currently reading, if any.
357 static MMacro
*defining
;
359 static uint64_t nested_mac_count
;
360 static uint64_t nested_rep_count
;
363 * The number of macro parameters to allocate space for at a time.
365 #define PARAM_DELTA 16
368 * The standard macro set: defined in macros.c in the array nasm_stdmac.
369 * This gives our position in the macro set, when we're processing it.
371 static const macros_t
*stdmacpos
;
374 * The extra standard macros that come from the object format, if
377 static const macros_t
*extrastdmac
= NULL
;
378 static bool any_extrastdmac
;
381 * Tokens are allocated in blocks to improve speed
383 #define TOKEN_BLOCKSIZE 4096
384 static Token
*freeTokens
= NULL
;
390 static Blocks blocks
= { NULL
, NULL
};
393 * Forward declarations.
395 static Token
*expand_mmac_params(Token
* tline
);
396 static Token
*expand_smacro(Token
* tline
);
397 static Token
*expand_id(Token
* tline
);
398 static Context
*get_ctx(const char *name
, bool all_contexts
);
399 static void make_tok_num(Token
* tok
, int64_t val
);
400 static void error(int severity
, const char *fmt
, ...);
401 static void *new_Block(size_t size
);
402 static void delete_Blocks(void);
403 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
404 const char *text
, int txtlen
);
405 static Token
*delete_Token(Token
* t
);
408 * Macros for safe checking of token pointers, avoid *(NULL)
410 #define tok_type_(x,t) ((x) && (x)->type == (t))
411 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
412 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
413 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
415 /* Handle TASM specific directives, which do not contain a % in
416 * front of them. We do it here because I could not find any other
417 * place to do it for the moment, and it is a hack (ideally it would
418 * be nice to be able to use the NASM pre-processor to do it).
420 static char *check_tasm_directive(char *line
)
422 int32_t i
, j
, k
, m
, len
;
423 char *p
= line
, *oldline
, oldchar
;
425 /* Skip whitespace */
426 while (nasm_isspace(*p
) && *p
!= 0)
429 /* Binary search for the directive name */
431 j
= elements(tasm_directives
);
433 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
440 m
= nasm_stricmp(p
, tasm_directives
[k
]);
442 /* We have found a directive, so jam a % in front of it
443 * so that NASM will then recognise it as one if it's own.
448 line
= nasm_malloc(len
+ 2);
450 if (k
== TM_IFDIFI
) {
451 /* NASM does not recognise IFDIFI, so we convert it to
452 * %ifdef BOGUS. This is not used in NASM comaptible
453 * code, but does need to parse for the TASM macro
456 strcpy(line
+ 1, "ifdef BOGUS");
458 memcpy(line
+ 1, p
, len
+ 1);
473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
478 static char *prepreproc(char *line
)
481 char *fname
, *oldline
;
483 if (line
[0] == '#' && line
[1] == ' ') {
486 lineno
= atoi(fname
);
487 fname
+= strspn(fname
, "0123456789 ");
490 fnlen
= strcspn(fname
, "\"");
491 line
= nasm_malloc(20 + fnlen
);
492 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
495 if (tasm_compatible_mode
)
496 return check_tasm_directive(line
);
501 * Free a linked list of tokens.
503 static void free_tlist(Token
* list
)
506 list
= delete_Token(list
);
511 * Free a linked list of lines.
513 static void free_llist(Line
* list
)
519 free_tlist(l
->first
);
527 static void free_mmacro(MMacro
* m
)
530 free_tlist(m
->dlist
);
531 nasm_free(m
->defaults
);
532 free_llist(m
->expansion
);
537 * Free all currently defined macros, and free the hash tables
539 static void free_smacro_table(struct hash_table
*smt
)
543 struct hash_tbl_node
*it
= NULL
;
545 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
546 nasm_free((void *)key
);
548 SMacro
*ns
= s
->next
;
550 free_tlist(s
->expansion
);
558 static void free_mmacro_table(struct hash_table
*mmt
)
562 struct hash_tbl_node
*it
= NULL
;
565 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
566 nasm_free((void *)key
);
568 MMacro
*nm
= m
->next
;
576 static void free_macros(void)
578 free_smacro_table(&smacros
);
579 free_mmacro_table(&mmacros
);
583 * Initialize the hash tables
585 static void init_macros(void)
587 hash_init(&smacros
, HASH_LARGE
);
588 hash_init(&mmacros
, HASH_LARGE
);
592 * Pop the context stack.
594 static void ctx_pop(void)
599 free_smacro_table(&c
->localmac
);
605 * Search for a key in the hash index; adding it if necessary
606 * (in which case we initialize the data pointer to NULL.)
609 hash_findi_add(struct hash_table
*hash
, const char *str
)
611 struct hash_insert hi
;
615 r
= hash_findi(hash
, str
, &hi
);
619 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
620 return hash_add(&hi
, strx
, NULL
);
624 * Like hash_findi, but returns the data element rather than a pointer
625 * to it. Used only when not adding a new element, hence no third
629 hash_findix(struct hash_table
*hash
, const char *str
)
633 p
= hash_findi(hash
, str
, NULL
);
634 return p
? *p
: NULL
;
637 #define BUF_DELTA 512
639 * Read a line from the top file in istk, handling multiple CR/LFs
640 * at the end of the line read, and handling spurious ^Zs. Will
641 * return lines from the standard macro set if this has not already
644 static char *read_line(void)
646 char *buffer
, *p
, *q
;
647 int bufsize
, continued_count
;
651 const unsigned char *p
= stdmacpos
;
656 len
+= pp_directives_len
[c
-0x80]+1;
660 ret
= nasm_malloc(len
+1);
662 while ((c
= *stdmacpos
++)) {
664 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
665 q
+= pp_directives_len
[c
-0x80];
675 /* This was the last of the standard macro chain... */
677 if (any_extrastdmac
) {
678 stdmacpos
= extrastdmac
;
679 any_extrastdmac
= false;
680 } else if (do_predef
) {
682 Token
*head
, **tail
, *t
;
685 * Nasty hack: here we push the contents of
686 * `predef' on to the top-level expansion stack,
687 * since this is the most convenient way to
688 * implement the pre-include and pre-define
691 for (pd
= predef
; pd
; pd
= pd
->next
) {
694 for (t
= pd
->first
; t
; t
= t
->next
) {
695 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
696 tail
= &(*tail
)->next
;
698 l
= nasm_malloc(sizeof(Line
));
699 l
->next
= istk
->expansion
;
711 buffer
= nasm_malloc(BUF_DELTA
);
715 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
719 if (p
> buffer
&& p
[-1] == '\n') {
720 /* Convert backslash-CRLF line continuation sequences into
721 nothing at all (for DOS and Windows) */
722 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
727 /* Also convert backslash-LF line continuation sequences into
728 nothing at all (for Unix) */
729 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
737 if (p
- buffer
> bufsize
- 10) {
738 int32_t offset
= p
- buffer
;
739 bufsize
+= BUF_DELTA
;
740 buffer
= nasm_realloc(buffer
, bufsize
);
741 p
= buffer
+ offset
; /* prevent stale-pointer problems */
745 if (!q
&& p
== buffer
) {
750 src_set_linnum(src_get_linnum() + istk
->lineinc
+
751 (continued_count
* istk
->lineinc
));
754 * Play safe: remove CRs as well as LFs, if any of either are
755 * present at the end of the line.
757 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
761 * Handle spurious ^Z, which may be inserted into source files
762 * by some file transfer utilities.
764 buffer
[strcspn(buffer
, "\032")] = '\0';
766 list
->line(LIST_READ
, buffer
);
772 * Tokenize a line of text. This is a very simple process since we
773 * don't need to parse the value out of e.g. numeric tokens: we
774 * simply split one string into many.
776 static Token
*tokenize(char *line
)
779 enum pp_token_type type
;
781 Token
*t
, **tail
= &list
;
787 if (nasm_isdigit(*p
) ||
788 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
789 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
793 while (nasm_isdigit(*p
));
794 type
= TOK_PREPROC_ID
;
795 } else if (*p
== '{') {
797 while (*p
&& *p
!= '}') {
804 type
= TOK_PREPROC_ID
;
805 } else if (*p
== '?') {
806 type
= TOK_PREPROC_Q
; /* %? */
809 type
= TOK_PREPROC_QQ
; /* %?? */
812 } else if (isidchar(*p
) ||
813 ((*p
== '!' || *p
== '%' || *p
== '$') &&
818 while (isidchar(*p
));
819 type
= TOK_PREPROC_ID
;
825 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
828 while (*p
&& isidchar(*p
))
830 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
835 p
= nasm_skip_string(p
);
840 error(ERR_WARNING
, "unterminated string");
841 /* Handling unterminated strings by UNV */
844 } else if (isnumstart(*p
)) {
846 bool is_float
= false;
862 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
864 if (*p
== '+' || *p
== '-') {
865 /* e can only be followed by +/- if it is either a
866 prefixed hex number or a floating-point number */
870 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
872 } else if (c
== 'P' || c
== 'p') {
874 if (*p
== '+' || *p
== '-')
876 } else if (isnumchar(c
) || c
== '_')
879 /* we need to deal with consequences of the legacy
880 parser, like "1.nolist" being two tokens
881 (TOK_NUMBER, TOK_ID) here; at least give it
882 a shot for now. In the future, we probably need
883 a flex-based scanner with proper pattern matching
884 to do it as well as it can be done. Nothing in
885 the world is going to help the person who wants
886 0x123.p16 interpreted as two tokens, though. */
891 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
892 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
893 (*r
== 'p' || *r
== 'P')) {
897 break; /* Terminate the token */
901 p
--; /* Point to first character beyond number */
903 if (has_e
&& !is_hex
) {
904 /* 1e13 is floating-point, but 1e13h is not */
908 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
909 } else if (nasm_isspace(*p
)) {
910 type
= TOK_WHITESPACE
;
912 while (*p
&& nasm_isspace(*p
))
915 * Whitespace just before end-of-line is discarded by
916 * pretending it's a comment; whitespace just before a
917 * comment gets lumped into the comment.
919 if (!*p
|| *p
== ';') {
924 } else if (*p
== ';') {
930 * Anything else is an operator of some kind. We check
931 * for all the double-character operators (>>, <<, //,
932 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
933 * else is a single-character operator.
936 if ((p
[0] == '>' && p
[1] == '>') ||
937 (p
[0] == '<' && p
[1] == '<') ||
938 (p
[0] == '/' && p
[1] == '/') ||
939 (p
[0] == '<' && p
[1] == '=') ||
940 (p
[0] == '>' && p
[1] == '=') ||
941 (p
[0] == '=' && p
[1] == '=') ||
942 (p
[0] == '!' && p
[1] == '=') ||
943 (p
[0] == '<' && p
[1] == '>') ||
944 (p
[0] == '&' && p
[1] == '&') ||
945 (p
[0] == '|' && p
[1] == '|') ||
946 (p
[0] == '^' && p
[1] == '^')) {
952 /* Handling unterminated string by UNV */
955 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
956 t->text[p-line] = *line;
960 if (type
!= TOK_COMMENT
) {
961 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
970 * this function allocates a new managed block of memory and
971 * returns a pointer to the block. The managed blocks are
972 * deleted only all at once by the delete_Blocks function.
974 static void *new_Block(size_t size
)
978 /* first, get to the end of the linked list */
981 /* now allocate the requested chunk */
982 b
->chunk
= nasm_malloc(size
);
984 /* now allocate a new block for the next request */
985 b
->next
= nasm_malloc(sizeof(Blocks
));
986 /* and initialize the contents of the new block */
987 b
->next
->next
= NULL
;
988 b
->next
->chunk
= NULL
;
993 * this function deletes all managed blocks of memory
995 static void delete_Blocks(void)
997 Blocks
*a
, *b
= &blocks
;
1000 * keep in mind that the first block, pointed to by blocks
1001 * is a static and not dynamically allocated, so we don't
1006 nasm_free(b
->chunk
);
1015 * this function creates a new Token and passes a pointer to it
1016 * back to the caller. It sets the type and text elements, and
1017 * also the a.mac and next elements to NULL.
1019 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1020 const char *text
, int txtlen
)
1025 if (freeTokens
== NULL
) {
1026 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1027 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1028 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1029 freeTokens
[i
].next
= NULL
;
1032 freeTokens
= t
->next
;
1036 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1040 txtlen
= strlen(text
);
1041 t
->text
= nasm_malloc(txtlen
+1);
1042 memcpy(t
->text
, text
, txtlen
);
1043 t
->text
[txtlen
] = '\0';
1048 static Token
*delete_Token(Token
* t
)
1050 Token
*next
= t
->next
;
1052 t
->next
= freeTokens
;
1058 * Convert a line of tokens back into text.
1059 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1060 * will be transformed into ..@ctxnum.xxx
1062 static char *detoken(Token
* tlist
, bool expand_locals
)
1070 for (t
= tlist
; t
; t
= t
->next
) {
1071 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1072 char *p
= getenv(t
->text
+ 2);
1075 t
->text
= nasm_strdup(p
);
1079 /* Expand local macros here and not during preprocessing */
1080 if (expand_locals
&&
1081 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1082 t
->text
[0] == '%' && t
->text
[1] == '$') {
1083 Context
*ctx
= get_ctx(t
->text
, false);
1086 char *p
, *q
= t
->text
+ 2;
1088 q
+= strspn(q
, "$");
1089 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1090 p
= nasm_strcat(buffer
, q
);
1095 if (t
->type
== TOK_WHITESPACE
) {
1097 } else if (t
->text
) {
1098 len
+= strlen(t
->text
);
1101 p
= line
= nasm_malloc(len
+ 1);
1102 for (t
= tlist
; t
; t
= t
->next
) {
1103 if (t
->type
== TOK_WHITESPACE
) {
1105 } else if (t
->text
) {
1116 * A scanner, suitable for use by the expression evaluator, which
1117 * operates on a line of Tokens. Expects a pointer to a pointer to
1118 * the first token in the line to be passed in as its private_data
1121 * FIX: This really needs to be unified with stdscan.
1123 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1125 Token
**tlineptr
= private_data
;
1127 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1131 *tlineptr
= tline
? tline
->next
: NULL
;
1133 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1134 tline
->type
== TOK_COMMENT
));
1137 return tokval
->t_type
= TOKEN_EOS
;
1139 tokval
->t_charptr
= tline
->text
;
1141 if (tline
->text
[0] == '$' && !tline
->text
[1])
1142 return tokval
->t_type
= TOKEN_HERE
;
1143 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1144 return tokval
->t_type
= TOKEN_BASE
;
1146 if (tline
->type
== TOK_ID
) {
1147 p
= tokval
->t_charptr
= tline
->text
;
1149 tokval
->t_charptr
++;
1150 return tokval
->t_type
= TOKEN_ID
;
1153 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1154 if (r
>= p
+MAX_KEYWORD
)
1155 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1156 *s
++ = nasm_tolower(*r
);
1159 /* right, so we have an identifier sitting in temp storage. now,
1160 * is it actually a register or instruction name, or what? */
1161 return nasm_token_hash(ourcopy
, tokval
);
1164 if (tline
->type
== TOK_NUMBER
) {
1166 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1167 tokval
->t_charptr
= tline
->text
;
1169 return tokval
->t_type
= TOKEN_ERRNUM
;
1171 return tokval
->t_type
= TOKEN_NUM
;
1174 if (tline
->type
== TOK_FLOAT
) {
1175 return tokval
->t_type
= TOKEN_FLOAT
;
1178 if (tline
->type
== TOK_STRING
) {
1181 bq
= tline
->text
[0];
1182 tokval
->t_charptr
= tline
->text
;
1183 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1185 if (ep
[0] != bq
|| ep
[1] != '\0')
1186 return tokval
->t_type
= TOKEN_ERRSTR
;
1188 return tokval
->t_type
= TOKEN_STR
;
1191 if (tline
->type
== TOK_OTHER
) {
1192 if (!strcmp(tline
->text
, "<<"))
1193 return tokval
->t_type
= TOKEN_SHL
;
1194 if (!strcmp(tline
->text
, ">>"))
1195 return tokval
->t_type
= TOKEN_SHR
;
1196 if (!strcmp(tline
->text
, "//"))
1197 return tokval
->t_type
= TOKEN_SDIV
;
1198 if (!strcmp(tline
->text
, "%%"))
1199 return tokval
->t_type
= TOKEN_SMOD
;
1200 if (!strcmp(tline
->text
, "=="))
1201 return tokval
->t_type
= TOKEN_EQ
;
1202 if (!strcmp(tline
->text
, "<>"))
1203 return tokval
->t_type
= TOKEN_NE
;
1204 if (!strcmp(tline
->text
, "!="))
1205 return tokval
->t_type
= TOKEN_NE
;
1206 if (!strcmp(tline
->text
, "<="))
1207 return tokval
->t_type
= TOKEN_LE
;
1208 if (!strcmp(tline
->text
, ">="))
1209 return tokval
->t_type
= TOKEN_GE
;
1210 if (!strcmp(tline
->text
, "&&"))
1211 return tokval
->t_type
= TOKEN_DBL_AND
;
1212 if (!strcmp(tline
->text
, "^^"))
1213 return tokval
->t_type
= TOKEN_DBL_XOR
;
1214 if (!strcmp(tline
->text
, "||"))
1215 return tokval
->t_type
= TOKEN_DBL_OR
;
1219 * We have no other options: just return the first character of
1222 return tokval
->t_type
= tline
->text
[0];
1226 * Compare a string to the name of an existing macro; this is a
1227 * simple wrapper which calls either strcmp or nasm_stricmp
1228 * depending on the value of the `casesense' parameter.
1230 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1232 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1236 * Compare a string to the name of an existing macro; this is a
1237 * simple wrapper which calls either strcmp or nasm_stricmp
1238 * depending on the value of the `casesense' parameter.
1240 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1242 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1246 * Return the Context structure associated with a %$ token. Return
1247 * NULL, having _already_ reported an error condition, if the
1248 * context stack isn't deep enough for the supplied number of $
1250 * If all_contexts == true, contexts that enclose current are
1251 * also scanned for such smacro, until it is found; if not -
1252 * only the context that directly results from the number of $'s
1253 * in variable's name.
1255 static Context
*get_ctx(const char *name
, bool all_contexts
)
1261 if (!name
|| name
[0] != '%' || name
[1] != '$')
1265 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1269 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1271 /* i--; Lino - 02/25/02 */
1274 error(ERR_NONFATAL
, "`%s': context stack is only"
1275 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1282 /* Search for this smacro in found context */
1283 m
= hash_findix(&ctx
->localmac
, name
);
1285 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1296 * Check to see if a file is already in a string list
1298 static bool in_list(const StrList
*list
, const char *str
)
1301 if (!strcmp(list
->str
, str
))
1309 * Open an include file. This routine must always return a valid
1310 * file pointer if it returns - it's responsible for throwing an
1311 * ERR_FATAL and bombing out completely if not. It should also try
1312 * the include path one by one until it finds the file or reaches
1313 * the end of the path.
1315 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1320 IncPath
*ip
= ipath
;
1321 int len
= strlen(file
);
1322 size_t prefix_len
= 0;
1326 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1327 memcpy(sl
->str
, prefix
, prefix_len
);
1328 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1329 fp
= fopen(sl
->str
, "r");
1330 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1348 prefix_len
= strlen(prefix
);
1350 /* -MG given and file not found */
1351 if (dhead
&& !in_list(*dhead
, file
)) {
1352 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1354 strcpy(sl
->str
, file
);
1362 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1363 return NULL
; /* never reached - placate compilers */
1367 * Determine if we should warn on defining a single-line macro of
1368 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1369 * return true if _any_ single-line macro of that name is defined.
1370 * Otherwise, will return true if a single-line macro with either
1371 * `nparam' or no parameters is defined.
1373 * If a macro with precisely the right number of parameters is
1374 * defined, or nparam is -1, the address of the definition structure
1375 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1376 * is NULL, no action will be taken regarding its contents, and no
1379 * Note that this is also called with nparam zero to resolve
1382 * If you already know which context macro belongs to, you can pass
1383 * the context pointer as first parameter; if you won't but name begins
1384 * with %$ the context will be automatically computed. If all_contexts
1385 * is true, macro will be searched in outer contexts as well.
1388 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1391 struct hash_table
*smtbl
;
1395 smtbl
= &ctx
->localmac
;
1396 } else if (name
[0] == '%' && name
[1] == '$') {
1398 ctx
= get_ctx(name
, false);
1400 return false; /* got to return _something_ */
1401 smtbl
= &ctx
->localmac
;
1405 m
= (SMacro
*) hash_findix(smtbl
, name
);
1408 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1409 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1411 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1425 * Count and mark off the parameters in a multi-line macro call.
1426 * This is called both from within the multi-line macro expansion
1427 * code, and also to mark off the default parameters when provided
1428 * in a %macro definition line.
1430 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1432 int paramsize
, brace
;
1434 *nparam
= paramsize
= 0;
1437 if (*nparam
>= paramsize
) {
1438 paramsize
+= PARAM_DELTA
;
1439 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1443 if (tok_is_(t
, "{"))
1445 (*params
)[(*nparam
)++] = t
;
1446 while (tok_isnt_(t
, brace
? "}" : ","))
1448 if (t
) { /* got a comma/brace */
1452 * Now we've found the closing brace, look further
1456 if (tok_isnt_(t
, ",")) {
1458 "braces do not enclose all of macro parameter");
1459 while (tok_isnt_(t
, ","))
1463 t
= t
->next
; /* eat the comma */
1470 * Determine whether one of the various `if' conditions is true or
1473 * We must free the tline we get passed.
1475 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1477 enum pp_conditional i
= PP_COND(ct
);
1479 Token
*t
, *tt
, **tptr
, *origline
;
1480 struct tokenval tokval
;
1482 enum pp_token_type needtype
;
1488 j
= false; /* have we matched yet? */
1489 while (cstk
&& tline
) {
1491 if (!tline
|| tline
->type
!= TOK_ID
) {
1493 "`%s' expects context identifiers", pp_directives
[ct
]);
1494 free_tlist(origline
);
1497 if (cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1499 tline
= tline
->next
;
1504 j
= false; /* have we matched yet? */
1507 if (!tline
|| (tline
->type
!= TOK_ID
&&
1508 (tline
->type
!= TOK_PREPROC_ID
||
1509 tline
->text
[1] != '$'))) {
1511 "`%s' expects macro identifiers", pp_directives
[ct
]);
1514 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1516 tline
= tline
->next
;
1522 tline
= expand_smacro(tline
);
1524 while (tok_isnt_(tt
, ","))
1528 "`%s' expects two comma-separated arguments",
1533 j
= true; /* assume equality unless proved not */
1534 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1535 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1536 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1540 if (t
->type
== TOK_WHITESPACE
) {
1544 if (tt
->type
== TOK_WHITESPACE
) {
1548 if (tt
->type
!= t
->type
) {
1549 j
= false; /* found mismatching tokens */
1552 /* When comparing strings, need to unquote them first */
1553 if (t
->type
== TOK_STRING
) {
1554 size_t l1
= nasm_unquote(t
->text
, NULL
);
1555 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1561 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1565 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1566 j
= false; /* found mismatching tokens */
1573 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1574 j
= false; /* trailing gunk on one end or other */
1580 MMacro searching
, *mmac
;
1582 tline
= tline
->next
;
1584 tline
= expand_id(tline
);
1585 if (!tok_type_(tline
, TOK_ID
)) {
1587 "`%s' expects a macro name", pp_directives
[ct
]);
1590 searching
.name
= nasm_strdup(tline
->text
);
1591 searching
.casesense
= true;
1592 searching
.plus
= false;
1593 searching
.nolist
= false;
1594 searching
.in_progress
= 0;
1595 searching
.rep_nest
= NULL
;
1596 searching
.nparam_min
= 0;
1597 searching
.nparam_max
= INT_MAX
;
1598 tline
= expand_smacro(tline
->next
);
1601 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1603 "`%s' expects a parameter count or nothing",
1606 searching
.nparam_min
= searching
.nparam_max
=
1607 readnum(tline
->text
, &j
);
1610 "unable to parse parameter count `%s'",
1613 if (tline
&& tok_is_(tline
->next
, "-")) {
1614 tline
= tline
->next
->next
;
1615 if (tok_is_(tline
, "*"))
1616 searching
.nparam_max
= INT_MAX
;
1617 else if (!tok_type_(tline
, TOK_NUMBER
))
1619 "`%s' expects a parameter count after `-'",
1622 searching
.nparam_max
= readnum(tline
->text
, &j
);
1625 "unable to parse parameter count `%s'",
1627 if (searching
.nparam_min
> searching
.nparam_max
)
1629 "minimum parameter count exceeds maximum");
1632 if (tline
&& tok_is_(tline
->next
, "+")) {
1633 tline
= tline
->next
;
1634 searching
.plus
= true;
1636 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1638 if (!strcmp(mmac
->name
, searching
.name
) &&
1639 (mmac
->nparam_min
<= searching
.nparam_max
1641 && (searching
.nparam_min
<= mmac
->nparam_max
1648 nasm_free(searching
.name
);
1657 needtype
= TOK_NUMBER
;
1660 needtype
= TOK_STRING
;
1664 t
= tline
= expand_smacro(tline
);
1666 while (tok_type_(t
, TOK_WHITESPACE
) ||
1667 (needtype
== TOK_NUMBER
&&
1668 tok_type_(t
, TOK_OTHER
) &&
1669 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1673 j
= tok_type_(t
, needtype
);
1677 t
= tline
= expand_smacro(tline
);
1678 while (tok_type_(t
, TOK_WHITESPACE
))
1683 t
= t
->next
; /* Skip the actual token */
1684 while (tok_type_(t
, TOK_WHITESPACE
))
1686 j
= !t
; /* Should be nothing left */
1691 t
= tline
= expand_smacro(tline
);
1692 while (tok_type_(t
, TOK_WHITESPACE
))
1695 j
= !t
; /* Should be empty */
1699 t
= tline
= expand_smacro(tline
);
1701 tokval
.t_type
= TOKEN_INVALID
;
1702 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1703 NULL
, pass
| CRITICAL
, error
, NULL
);
1708 "trailing garbage after expression ignored");
1709 if (!is_simple(evalresult
)) {
1711 "non-constant value given to `%s'", pp_directives
[ct
]);
1714 j
= reloc_value(evalresult
) != 0;
1719 "preprocessor directive `%s' not yet implemented",
1724 free_tlist(origline
);
1725 return j
^ PP_NEGATIVE(ct
);
1728 free_tlist(origline
);
1733 * Common code for defining an smacro
1735 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1736 int nparam
, Token
*expansion
)
1738 SMacro
*smac
, **smhead
;
1739 struct hash_table
*smtbl
;
1741 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1744 "single-line macro `%s' defined both with and"
1745 " without parameters", mname
);
1747 /* Some instances of the old code considered this a failure,
1748 some others didn't. What is the right thing to do here? */
1749 free_tlist(expansion
);
1750 return false; /* Failure */
1753 * We're redefining, so we have to take over an
1754 * existing SMacro structure. This means freeing
1755 * what was already in it.
1757 nasm_free(smac
->name
);
1758 free_tlist(smac
->expansion
);
1761 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1762 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1763 smac
= nasm_malloc(sizeof(SMacro
));
1764 smac
->next
= *smhead
;
1767 smac
->name
= nasm_strdup(mname
);
1768 smac
->casesense
= casesense
;
1769 smac
->nparam
= nparam
;
1770 smac
->expansion
= expansion
;
1771 smac
->in_progress
= false;
1772 return true; /* Success */
1776 * Undefine an smacro
1778 static void undef_smacro(Context
*ctx
, const char *mname
)
1780 SMacro
**smhead
, *s
, **sp
;
1781 struct hash_table
*smtbl
;
1783 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1784 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1788 * We now have a macro name... go hunt for it.
1791 while ((s
= *sp
) != NULL
) {
1792 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1795 free_tlist(s
->expansion
);
1805 * Parse a mmacro specification.
1807 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1811 tline
= tline
->next
;
1813 tline
= expand_id(tline
);
1814 if (!tok_type_(tline
, TOK_ID
)) {
1815 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1818 def
->name
= nasm_strdup(tline
->text
);
1820 def
->nolist
= false;
1821 def
->in_progress
= 0;
1822 def
->rep_nest
= NULL
;
1823 tline
= expand_smacro(tline
->next
);
1825 if (!tok_type_(tline
, TOK_NUMBER
)) {
1826 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1827 def
->nparam_min
= def
->nparam_max
= 0;
1829 def
->nparam_min
= def
->nparam_max
=
1830 readnum(tline
->text
, &err
);
1833 "unable to parse parameter count `%s'", tline
->text
);
1835 if (tline
&& tok_is_(tline
->next
, "-")) {
1836 tline
= tline
->next
->next
;
1837 if (tok_is_(tline
, "*")) {
1838 def
->nparam_max
= INT_MAX
;
1839 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1841 "`%s' expects a parameter count after `-'", directive
);
1843 def
->nparam_max
= readnum(tline
->text
, &err
);
1845 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1848 if (def
->nparam_min
> def
->nparam_max
) {
1849 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1853 if (tline
&& tok_is_(tline
->next
, "+")) {
1854 tline
= tline
->next
;
1857 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1858 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1859 tline
= tline
->next
;
1864 * Handle default parameters.
1866 if (tline
&& tline
->next
) {
1867 def
->dlist
= tline
->next
;
1869 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1872 def
->defaults
= NULL
;
1874 def
->expansion
= NULL
;
1881 * Decode a size directive
1883 static int parse_size(const char *str
) {
1884 static const char *size_names
[] =
1885 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1886 static const int sizes
[] =
1887 { 0, 1, 4, 16, 8, 10, 2, 32 };
1889 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1893 * find and process preprocessor directive in passed line
1894 * Find out if a line contains a preprocessor directive, and deal
1897 * If a directive _is_ found, it is the responsibility of this routine
1898 * (and not the caller) to free_tlist() the line.
1900 * @param tline a pointer to the current tokeninzed line linked list
1901 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1904 static int do_directive(Token
* tline
)
1906 enum preproc_token i
;
1914 char *p
, *pp
, *mname
;
1918 MMacro
*mmac
, **mmhead
;
1919 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1921 struct tokenval tokval
;
1923 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1930 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1931 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1932 || tline
->text
[1] == '!'))
1933 return NO_DIRECTIVE_FOUND
;
1935 i
= pp_token_hash(tline
->text
);
1938 * If we're in a non-emitting branch of a condition construct,
1939 * or walking to the end of an already terminated %rep block,
1940 * we should ignore all directives except for condition
1943 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1944 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1945 return NO_DIRECTIVE_FOUND
;
1949 * If we're defining a macro or reading a %rep block, we should
1950 * ignore all directives except for %macro/%imacro (which nest),
1951 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1952 * If we're in a %rep block, another %rep nests, so should be let through.
1954 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1955 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1956 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1957 return NO_DIRECTIVE_FOUND
;
1961 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
1963 return NO_DIRECTIVE_FOUND
;
1964 } else if (nested_mac_count
> 0) {
1965 if (i
== PP_ENDMACRO
) {
1967 return NO_DIRECTIVE_FOUND
;
1970 if (!defining
->name
) {
1973 return NO_DIRECTIVE_FOUND
;
1974 } else if (nested_rep_count
> 0) {
1975 if (i
== PP_ENDREP
) {
1977 return NO_DIRECTIVE_FOUND
;
1985 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1987 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1990 /* Directive to tell NASM what the default stack size is. The
1991 * default is for a 16-bit stack, and this can be overriden with
1993 * the following form:
1995 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1997 tline
= tline
->next
;
1998 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1999 tline
= tline
->next
;
2000 if (!tline
|| tline
->type
!= TOK_ID
) {
2001 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2002 free_tlist(origline
);
2003 return DIRECTIVE_FOUND
;
2005 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2006 /* All subsequent ARG directives are for a 32-bit stack */
2008 StackPointer
= "ebp";
2011 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2012 /* All subsequent ARG directives are for a 64-bit stack */
2014 StackPointer
= "rbp";
2017 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2018 /* All subsequent ARG directives are for a 16-bit stack,
2019 * far function call.
2022 StackPointer
= "bp";
2025 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2026 /* All subsequent ARG directives are for a 16-bit stack,
2027 * far function call. We don't support near functions.
2030 StackPointer
= "bp";
2034 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2035 free_tlist(origline
);
2036 return DIRECTIVE_FOUND
;
2038 free_tlist(origline
);
2039 return DIRECTIVE_FOUND
;
2042 /* TASM like ARG directive to define arguments to functions, in
2043 * the following form:
2045 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2049 char *arg
, directive
[256];
2050 int size
= StackSize
;
2052 /* Find the argument name */
2053 tline
= tline
->next
;
2054 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2055 tline
= tline
->next
;
2056 if (!tline
|| tline
->type
!= TOK_ID
) {
2057 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2058 free_tlist(origline
);
2059 return DIRECTIVE_FOUND
;
2063 /* Find the argument size type */
2064 tline
= tline
->next
;
2065 if (!tline
|| tline
->type
!= TOK_OTHER
2066 || tline
->text
[0] != ':') {
2068 "Syntax error processing `%%arg' directive");
2069 free_tlist(origline
);
2070 return DIRECTIVE_FOUND
;
2072 tline
= tline
->next
;
2073 if (!tline
|| tline
->type
!= TOK_ID
) {
2074 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2075 free_tlist(origline
);
2076 return DIRECTIVE_FOUND
;
2079 /* Allow macro expansion of type parameter */
2080 tt
= tokenize(tline
->text
);
2081 tt
= expand_smacro(tt
);
2082 size
= parse_size(tt
->text
);
2085 "Invalid size type for `%%arg' missing directive");
2087 free_tlist(origline
);
2088 return DIRECTIVE_FOUND
;
2092 /* Round up to even stack slots */
2093 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2095 /* Now define the macro for the argument */
2096 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2097 arg
, StackPointer
, offset
);
2098 do_directive(tokenize(directive
));
2101 /* Move to the next argument in the list */
2102 tline
= tline
->next
;
2103 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2104 tline
= tline
->next
;
2105 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2107 free_tlist(origline
);
2108 return DIRECTIVE_FOUND
;
2111 /* TASM like LOCAL directive to define local variables for a
2112 * function, in the following form:
2114 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2116 * The '= LocalSize' at the end is ignored by NASM, but is
2117 * required by TASM to define the local parameter size (and used
2118 * by the TASM macro package).
2120 offset
= LocalOffset
;
2122 char *local
, directive
[256];
2123 int size
= StackSize
;
2125 /* Find the argument name */
2126 tline
= tline
->next
;
2127 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2128 tline
= tline
->next
;
2129 if (!tline
|| tline
->type
!= TOK_ID
) {
2131 "`%%local' missing argument parameter");
2132 free_tlist(origline
);
2133 return DIRECTIVE_FOUND
;
2135 local
= tline
->text
;
2137 /* Find the argument size type */
2138 tline
= tline
->next
;
2139 if (!tline
|| tline
->type
!= TOK_OTHER
2140 || tline
->text
[0] != ':') {
2142 "Syntax error processing `%%local' directive");
2143 free_tlist(origline
);
2144 return DIRECTIVE_FOUND
;
2146 tline
= tline
->next
;
2147 if (!tline
|| tline
->type
!= TOK_ID
) {
2149 "`%%local' missing size type parameter");
2150 free_tlist(origline
);
2151 return DIRECTIVE_FOUND
;
2154 /* Allow macro expansion of type parameter */
2155 tt
= tokenize(tline
->text
);
2156 tt
= expand_smacro(tt
);
2157 size
= parse_size(tt
->text
);
2160 "Invalid size type for `%%local' missing directive");
2162 free_tlist(origline
);
2163 return DIRECTIVE_FOUND
;
2167 /* Round up to even stack slots */
2168 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2170 offset
+= size
; /* Negative offset, increment before */
2172 /* Now define the macro for the argument */
2173 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2174 local
, StackPointer
, offset
);
2175 do_directive(tokenize(directive
));
2177 /* Now define the assign to setup the enter_c macro correctly */
2178 snprintf(directive
, sizeof(directive
),
2179 "%%assign %%$localsize %%$localsize+%d", size
);
2180 do_directive(tokenize(directive
));
2182 /* Move to the next argument in the list */
2183 tline
= tline
->next
;
2184 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2185 tline
= tline
->next
;
2186 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2187 LocalOffset
= offset
;
2188 free_tlist(origline
);
2189 return DIRECTIVE_FOUND
;
2193 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2196 free_tlist(origline
);
2197 return DIRECTIVE_FOUND
;
2200 t
= tline
->next
= expand_smacro(tline
->next
);
2202 if (!t
|| (t
->type
!= TOK_STRING
&&
2203 t
->type
!= TOK_INTERNAL_STRING
)) {
2204 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2205 free_tlist(origline
);
2206 return DIRECTIVE_FOUND
; /* but we did _something_ */
2210 "trailing garbage after `%%depend' ignored");
2212 if (t
->type
!= TOK_INTERNAL_STRING
)
2213 nasm_unquote(p
, NULL
);
2214 if (dephead
&& !in_list(*dephead
, p
)) {
2215 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2219 deptail
= &sl
->next
;
2221 free_tlist(origline
);
2222 return DIRECTIVE_FOUND
;
2225 t
= tline
->next
= expand_smacro(tline
->next
);
2228 if (!t
|| (t
->type
!= TOK_STRING
&&
2229 t
->type
!= TOK_INTERNAL_STRING
)) {
2230 error(ERR_NONFATAL
, "`%%include' expects a file name");
2231 free_tlist(origline
);
2232 return DIRECTIVE_FOUND
; /* but we did _something_ */
2236 "trailing garbage after `%%include' ignored");
2238 if (t
->type
!= TOK_INTERNAL_STRING
)
2239 nasm_unquote(p
, NULL
);
2240 inc
= nasm_malloc(sizeof(Include
));
2243 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2245 /* -MG given but file not found */
2248 inc
->fname
= src_set_fname(nasm_strdup(p
));
2249 inc
->lineno
= src_set_linnum(0);
2251 inc
->expansion
= NULL
;
2254 list
->uplevel(LIST_INCLUDE
);
2256 free_tlist(origline
);
2257 return DIRECTIVE_FOUND
;
2261 static const macros_t
*use_pkg
;
2262 const char *pkg_macro
;
2264 t
= tline
->next
= expand_smacro(tline
->next
);
2267 if (!t
|| (t
->type
!= TOK_STRING
&&
2268 t
->type
!= TOK_INTERNAL_STRING
&&
2269 t
->type
!= TOK_ID
)) {
2270 error(ERR_NONFATAL
, "`%%use' expects a package name");
2271 free_tlist(origline
);
2272 return DIRECTIVE_FOUND
; /* but we did _something_ */
2276 "trailing garbage after `%%use' ignored");
2277 if (t
->type
== TOK_STRING
)
2278 nasm_unquote(t
->text
, NULL
);
2279 use_pkg
= nasm_stdmac_find_package(t
->text
);
2281 error(ERR_NONFATAL
, "unknown `%%use' package: %s", t
->text
);
2282 /* The first string will be <%define>__USE_*__ */
2283 pkg_macro
= (char *)use_pkg
+ 1;
2284 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2285 /* Not already included, go ahead and include it */
2286 stdmacpos
= use_pkg
;
2288 free_tlist(origline
);
2289 return DIRECTIVE_FOUND
;
2292 tline
= tline
->next
;
2294 tline
= expand_id(tline
);
2296 if (!tok_type_(tline
, TOK_ID
)) {
2297 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2298 free_tlist(origline
);
2299 return DIRECTIVE_FOUND
; /* but we did _something_ */
2302 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2303 p
= nasm_strdup(tline
->text
);
2305 p
= NULL
; /* Anonymous context */
2307 ctx
= nasm_malloc(sizeof(Context
));
2309 hash_init(&ctx
->localmac
, HASH_SMALL
);
2311 ctx
->number
= unique
++;
2313 free_tlist(origline
);
2314 return DIRECTIVE_FOUND
;
2317 tline
= tline
->next
;
2319 tline
= expand_id(tline
);
2321 if (!tok_type_(tline
, TOK_ID
)) {
2322 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2323 free_tlist(origline
);
2324 return DIRECTIVE_FOUND
; /* but we did _something_ */
2327 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2328 p
= nasm_strdup(tline
->text
);
2333 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2335 nasm_free(cstk
->name
);
2338 free_tlist(origline
);
2339 return DIRECTIVE_FOUND
;
2343 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2345 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2348 free_tlist(origline
);
2349 return DIRECTIVE_FOUND
;
2354 int severity
= (i
== PP_ERROR
)
2355 ? ERR_NONFATAL
|ERR_NO_SEVERITY
2356 : ERR_WARNING
|ERR_NO_SEVERITY
;
2358 tline
->next
= expand_smacro(tline
->next
);
2359 tline
= tline
->next
;
2361 t
= tline
? tline
->next
: NULL
;
2363 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2364 /* The line contains only a quoted string */
2366 nasm_unquote(p
, NULL
);
2367 error(severity
, "%s: %s", pp_directives
[i
], p
);
2369 /* Not a quoted string, or more than a quoted string */
2370 p
= detoken(tline
, false);
2371 error(severity
, "%s: %s", pp_directives
[i
], p
);
2374 free_tlist(origline
);
2375 return DIRECTIVE_FOUND
;
2379 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2382 j
= if_condition(tline
->next
, i
);
2383 tline
->next
= NULL
; /* it got freed */
2384 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2386 cond
= nasm_malloc(sizeof(Cond
));
2387 cond
->next
= istk
->conds
;
2390 free_tlist(origline
);
2391 return DIRECTIVE_FOUND
;
2395 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2396 if (emitting(istk
->conds
->state
)
2397 || istk
->conds
->state
== COND_NEVER
)
2398 istk
->conds
->state
= COND_NEVER
;
2401 * IMPORTANT: In the case of %if, we will already have
2402 * called expand_mmac_params(); however, if we're
2403 * processing an %elif we must have been in a
2404 * non-emitting mode, which would have inhibited
2405 * the normal invocation of expand_mmac_params(). Therefore,
2406 * we have to do it explicitly here.
2408 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2409 tline
->next
= NULL
; /* it got freed */
2410 istk
->conds
->state
=
2411 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2413 free_tlist(origline
);
2414 return DIRECTIVE_FOUND
;
2418 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2420 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2421 if (emitting(istk
->conds
->state
)
2422 || istk
->conds
->state
== COND_NEVER
)
2423 istk
->conds
->state
= COND_ELSE_FALSE
;
2425 istk
->conds
->state
= COND_ELSE_TRUE
;
2426 free_tlist(origline
);
2427 return DIRECTIVE_FOUND
;
2431 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2433 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2435 istk
->conds
= cond
->next
;
2437 free_tlist(origline
);
2438 return DIRECTIVE_FOUND
;
2444 "`%%%smacro': already defining a macro",
2445 (i
== PP_IMACRO
? "i" : ""));
2446 return DIRECTIVE_FOUND
;
2448 defining
= nasm_malloc(sizeof(MMacro
));
2449 defining
->casesense
= (i
== PP_MACRO
);
2450 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2451 nasm_free(defining
);
2453 return DIRECTIVE_FOUND
;
2456 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2458 if (!strcmp(mmac
->name
, defining
->name
) &&
2459 (mmac
->nparam_min
<= defining
->nparam_max
2461 && (defining
->nparam_min
<= mmac
->nparam_max
2464 "redefining multi-line macro `%s'", defining
->name
);
2465 return DIRECTIVE_FOUND
;
2469 free_tlist(origline
);
2470 return DIRECTIVE_FOUND
;
2474 if (! (defining
&& defining
->name
)) {
2475 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2476 return DIRECTIVE_FOUND
;
2478 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2479 defining
->next
= *mmhead
;
2482 free_tlist(origline
);
2483 return DIRECTIVE_FOUND
;
2491 spec
.casesense
= (i
== PP_UNMACRO
);
2492 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2493 return DIRECTIVE_FOUND
;
2495 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2496 while (mmac_p
&& *mmac_p
) {
2498 if (mmac
->casesense
== spec
.casesense
&&
2499 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2500 mmac
->nparam_min
== spec
.nparam_min
&&
2501 mmac
->nparam_max
== spec
.nparam_max
&&
2502 mmac
->plus
== spec
.plus
) {
2503 *mmac_p
= mmac
->next
;
2506 mmac_p
= &mmac
->next
;
2509 free_tlist(origline
);
2510 free_tlist(spec
.dlist
);
2511 return DIRECTIVE_FOUND
;
2515 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2516 tline
= tline
->next
;
2517 if (tline
->next
== NULL
) {
2518 free_tlist(origline
);
2519 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2520 return DIRECTIVE_FOUND
;
2522 t
= expand_smacro(tline
->next
);
2524 free_tlist(origline
);
2527 tokval
.t_type
= TOKEN_INVALID
;
2529 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2532 return DIRECTIVE_FOUND
;
2535 "trailing garbage after expression ignored");
2536 if (!is_simple(evalresult
)) {
2537 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2538 return DIRECTIVE_FOUND
;
2541 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2542 mmac
= mmac
->next_active
;
2544 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2545 } else if (mmac
->nparam
== 0) {
2547 "`%%rotate' invoked within macro without parameters");
2549 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2551 rotate
%= (int)mmac
->nparam
;
2553 rotate
+= mmac
->nparam
;
2555 mmac
->rotate
= rotate
;
2557 return DIRECTIVE_FOUND
;
2562 tline
= tline
->next
;
2563 } while (tok_type_(tline
, TOK_WHITESPACE
));
2565 if (tok_type_(tline
, TOK_ID
) &&
2566 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2569 tline
= tline
->next
;
2570 } while (tok_type_(tline
, TOK_WHITESPACE
));
2574 t
= expand_smacro(tline
);
2576 tokval
.t_type
= TOKEN_INVALID
;
2578 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2580 free_tlist(origline
);
2581 return DIRECTIVE_FOUND
;
2585 "trailing garbage after expression ignored");
2586 if (!is_simple(evalresult
)) {
2587 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2588 return DIRECTIVE_FOUND
;
2590 count
= reloc_value(evalresult
) + 1;
2592 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2595 free_tlist(origline
);
2597 tmp_defining
= defining
;
2598 defining
= nasm_malloc(sizeof(MMacro
));
2599 defining
->name
= NULL
; /* flags this macro as a %rep block */
2600 defining
->casesense
= false;
2601 defining
->plus
= false;
2602 defining
->nolist
= nolist
;
2603 defining
->in_progress
= count
;
2604 defining
->nparam_min
= defining
->nparam_max
= 0;
2605 defining
->defaults
= NULL
;
2606 defining
->dlist
= NULL
;
2607 defining
->expansion
= NULL
;
2608 defining
->next_active
= istk
->mstk
;
2609 defining
->rep_nest
= tmp_defining
;
2610 return DIRECTIVE_FOUND
;
2613 if (!defining
|| defining
->name
) {
2614 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2615 return DIRECTIVE_FOUND
;
2619 * Now we have a "macro" defined - although it has no name
2620 * and we won't be entering it in the hash tables - we must
2621 * push a macro-end marker for it on to istk->expansion.
2622 * After that, it will take care of propagating itself (a
2623 * macro-end marker line for a macro which is really a %rep
2624 * block will cause the macro to be re-expanded, complete
2625 * with another macro-end marker to ensure the process
2626 * continues) until the whole expansion is forcibly removed
2627 * from istk->expansion by a %exitrep.
2629 l
= nasm_malloc(sizeof(Line
));
2630 l
->next
= istk
->expansion
;
2631 l
->finishes
= defining
;
2633 istk
->expansion
= l
;
2635 istk
->mstk
= defining
;
2637 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2638 tmp_defining
= defining
;
2639 defining
= defining
->rep_nest
;
2640 free_tlist(origline
);
2641 return DIRECTIVE_FOUND
;
2645 * We must search along istk->expansion until we hit a
2646 * macro-end marker for a macro with no name. Then we set
2647 * its `in_progress' flag to 0.
2649 for (l
= istk
->expansion
; l
; l
= l
->next
)
2650 if (l
->finishes
&& !l
->finishes
->name
)
2651 return DIRECTIVE_FOUND
;
2654 l
->finishes
->in_progress
= 1;
2656 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2657 free_tlist(origline
);
2658 return DIRECTIVE_FOUND
;
2664 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2666 tline
= tline
->next
;
2668 tline
= expand_id(tline
);
2669 if (!tline
|| (tline
->type
!= TOK_ID
&&
2670 (tline
->type
!= TOK_PREPROC_ID
||
2671 tline
->text
[1] != '$'))) {
2672 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2674 free_tlist(origline
);
2675 return DIRECTIVE_FOUND
;
2678 ctx
= get_ctx(tline
->text
, false);
2680 mname
= tline
->text
;
2682 param_start
= tline
= tline
->next
;
2685 /* Expand the macro definition now for %xdefine and %ixdefine */
2686 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2687 tline
= expand_smacro(tline
);
2689 if (tok_is_(tline
, "(")) {
2691 * This macro has parameters.
2694 tline
= tline
->next
;
2698 error(ERR_NONFATAL
, "parameter identifier expected");
2699 free_tlist(origline
);
2700 return DIRECTIVE_FOUND
;
2702 if (tline
->type
!= TOK_ID
) {
2704 "`%s': parameter identifier expected",
2706 free_tlist(origline
);
2707 return DIRECTIVE_FOUND
;
2709 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2710 tline
= tline
->next
;
2712 if (tok_is_(tline
, ",")) {
2713 tline
= tline
->next
;
2716 if (!tok_is_(tline
, ")")) {
2718 "`)' expected to terminate macro template");
2719 free_tlist(origline
);
2720 return DIRECTIVE_FOUND
;
2722 return DIRECTIVE_FOUND
;
2725 tline
= tline
->next
;
2727 if (tok_type_(tline
, TOK_WHITESPACE
))
2728 last
= tline
, tline
= tline
->next
;
2733 if (t
->type
== TOK_ID
) {
2734 for (tt
= param_start
; tt
; tt
= tt
->next
)
2735 if (tt
->type
>= TOK_SMAC_PARAM
&&
2736 !strcmp(tt
->text
, t
->text
))
2740 t
->next
= macro_start
;
2745 * Good. We now have a macro name, a parameter count, and a
2746 * token list (in reverse order) for an expansion. We ought
2747 * to be OK just to create an SMacro, store it, and let
2748 * free_tlist have the rest of the line (which we have
2749 * carefully re-terminated after chopping off the expansion
2752 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2753 free_tlist(origline
);
2754 return DIRECTIVE_FOUND
;
2757 tline
= tline
->next
;
2759 tline
= expand_id(tline
);
2760 if (!tline
|| (tline
->type
!= TOK_ID
&&
2761 (tline
->type
!= TOK_PREPROC_ID
||
2762 tline
->text
[1] != '$'))) {
2763 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2764 free_tlist(origline
);
2765 return DIRECTIVE_FOUND
;
2769 "trailing garbage after macro name ignored");
2772 /* Find the context that symbol belongs to */
2773 ctx
= get_ctx(tline
->text
, false);
2774 undef_smacro(ctx
, tline
->text
);
2775 free_tlist(origline
);
2776 return DIRECTIVE_FOUND
;
2780 casesense
= (i
== PP_DEFSTR
);
2782 tline
= tline
->next
;
2784 tline
= expand_id(tline
);
2785 if (!tline
|| (tline
->type
!= TOK_ID
&&
2786 (tline
->type
!= TOK_PREPROC_ID
||
2787 tline
->text
[1] != '$'))) {
2788 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2790 free_tlist(origline
);
2791 return DIRECTIVE_FOUND
;
2794 ctx
= get_ctx(tline
->text
, false);
2796 mname
= tline
->text
;
2798 tline
= expand_smacro(tline
->next
);
2801 while (tok_type_(tline
, TOK_WHITESPACE
))
2802 tline
= delete_Token(tline
);
2804 p
= detoken(tline
, false);
2805 macro_start
= nasm_malloc(sizeof(*macro_start
));
2806 macro_start
->next
= NULL
;
2807 macro_start
->text
= nasm_quote(p
, strlen(p
));
2808 macro_start
->type
= TOK_STRING
;
2809 macro_start
->a
.mac
= NULL
;
2813 * We now have a macro name, an implicit parameter count of
2814 * zero, and a string token to use as an expansion. Create
2815 * and store an SMacro.
2817 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2818 free_tlist(origline
);
2819 return DIRECTIVE_FOUND
;
2824 StrList
*xsl
= NULL
;
2825 StrList
**xst
= &xsl
;
2829 tline
= tline
->next
;
2831 tline
= expand_id(tline
);
2832 if (!tline
|| (tline
->type
!= TOK_ID
&&
2833 (tline
->type
!= TOK_PREPROC_ID
||
2834 tline
->text
[1] != '$'))) {
2836 "`%%pathsearch' expects a macro identifier as first parameter");
2837 free_tlist(origline
);
2838 return DIRECTIVE_FOUND
;
2840 ctx
= get_ctx(tline
->text
, false);
2842 mname
= tline
->text
;
2844 tline
= expand_smacro(tline
->next
);
2848 while (tok_type_(t
, TOK_WHITESPACE
))
2851 if (!t
|| (t
->type
!= TOK_STRING
&&
2852 t
->type
!= TOK_INTERNAL_STRING
)) {
2853 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2855 free_tlist(origline
);
2856 return DIRECTIVE_FOUND
; /* but we did _something_ */
2860 "trailing garbage after `%%pathsearch' ignored");
2862 if (t
->type
!= TOK_INTERNAL_STRING
)
2863 nasm_unquote(p
, NULL
);
2865 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2868 fclose(fp
); /* Don't actually care about the file */
2870 macro_start
= nasm_malloc(sizeof(*macro_start
));
2871 macro_start
->next
= NULL
;
2872 macro_start
->text
= nasm_quote(p
, strlen(p
));
2873 macro_start
->type
= TOK_STRING
;
2874 macro_start
->a
.mac
= NULL
;
2879 * We now have a macro name, an implicit parameter count of
2880 * zero, and a string token to use as an expansion. Create
2881 * and store an SMacro.
2883 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2885 free_tlist(origline
);
2886 return DIRECTIVE_FOUND
;
2892 tline
= tline
->next
;
2894 tline
= expand_id(tline
);
2895 if (!tline
|| (tline
->type
!= TOK_ID
&&
2896 (tline
->type
!= TOK_PREPROC_ID
||
2897 tline
->text
[1] != '$'))) {
2899 "`%%strlen' expects a macro identifier as first parameter");
2900 free_tlist(origline
);
2901 return DIRECTIVE_FOUND
;
2903 ctx
= get_ctx(tline
->text
, false);
2905 mname
= tline
->text
;
2907 tline
= expand_smacro(tline
->next
);
2911 while (tok_type_(t
, TOK_WHITESPACE
))
2913 /* t should now point to the string */
2914 if (t
->type
!= TOK_STRING
) {
2916 "`%%strlen` requires string as second parameter");
2918 free_tlist(origline
);
2919 return DIRECTIVE_FOUND
;
2922 macro_start
= nasm_malloc(sizeof(*macro_start
));
2923 macro_start
->next
= NULL
;
2924 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2925 macro_start
->a
.mac
= NULL
;
2928 * We now have a macro name, an implicit parameter count of
2929 * zero, and a numeric token to use as an expansion. Create
2930 * and store an SMacro.
2932 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2934 free_tlist(origline
);
2935 return DIRECTIVE_FOUND
;
2940 tline
= tline
->next
;
2942 tline
= expand_id(tline
);
2943 if (!tline
|| (tline
->type
!= TOK_ID
&&
2944 (tline
->type
!= TOK_PREPROC_ID
||
2945 tline
->text
[1] != '$'))) {
2947 "`%%strcat' expects a macro identifier as first parameter");
2948 free_tlist(origline
);
2949 return DIRECTIVE_FOUND
;
2951 ctx
= get_ctx(tline
->text
, false);
2953 mname
= tline
->text
;
2955 tline
= expand_smacro(tline
->next
);
2959 for (t
= tline
; t
; t
= t
->next
) {
2961 case TOK_WHITESPACE
:
2964 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
2967 if (!strcmp(t
->text
, ",")) /* permit comma separators */
2969 /* else fall through */
2972 "non-string passed to `%%strcat' (%d)", t
->type
);
2974 free_tlist(origline
);
2975 return DIRECTIVE_FOUND
;
2979 p
= pp
= nasm_malloc(len
);
2981 for (t
= tline
; t
; t
= t
->next
) {
2982 if (t
->type
== TOK_STRING
) {
2983 memcpy(p
, t
->text
, t
->a
.len
);
2989 * We now have a macro name, an implicit parameter count of
2990 * zero, and a numeric token to use as an expansion. Create
2991 * and store an SMacro.
2993 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
2994 macro_start
->text
= nasm_quote(pp
, len
);
2996 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2998 free_tlist(origline
);
2999 return DIRECTIVE_FOUND
;
3008 tline
= tline
->next
;
3010 tline
= expand_id(tline
);
3011 if (!tline
|| (tline
->type
!= TOK_ID
&&
3012 (tline
->type
!= TOK_PREPROC_ID
||
3013 tline
->text
[1] != '$'))) {
3015 "`%%substr' expects a macro identifier as first parameter");
3016 free_tlist(origline
);
3017 return DIRECTIVE_FOUND
;
3019 ctx
= get_ctx(tline
->text
, false);
3021 mname
= tline
->text
;
3023 tline
= expand_smacro(tline
->next
);
3027 while (tok_type_(t
, TOK_WHITESPACE
))
3030 /* t should now point to the string */
3031 if (t
->type
!= TOK_STRING
) {
3033 "`%%substr` requires string as second parameter");
3035 free_tlist(origline
);
3036 return DIRECTIVE_FOUND
;
3041 tokval
.t_type
= TOKEN_INVALID
;
3042 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3046 free_tlist(origline
);
3047 return DIRECTIVE_FOUND
;
3048 } else if (!is_simple(evalresult
)) {
3049 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3051 free_tlist(origline
);
3052 return DIRECTIVE_FOUND
;
3054 a1
= evalresult
->value
-1;
3056 while (tok_type_(tt
, TOK_WHITESPACE
))
3059 a2
= 1; /* Backwards compatibility: one character */
3061 tokval
.t_type
= TOKEN_INVALID
;
3062 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3066 free_tlist(origline
);
3067 return DIRECTIVE_FOUND
;
3068 } else if (!is_simple(evalresult
)) {
3069 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3071 free_tlist(origline
);
3072 return DIRECTIVE_FOUND
;
3074 a2
= evalresult
->value
;
3077 len
= nasm_unquote(t
->text
, NULL
);
3080 if (a1
+a2
> (int64_t)len
)
3083 macro_start
= nasm_malloc(sizeof(*macro_start
));
3084 macro_start
->next
= NULL
;
3085 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3086 macro_start
->type
= TOK_STRING
;
3087 macro_start
->a
.mac
= NULL
;
3090 * We now have a macro name, an implicit parameter count of
3091 * zero, and a numeric token to use as an expansion. Create
3092 * and store an SMacro.
3094 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3096 free_tlist(origline
);
3097 return DIRECTIVE_FOUND
;
3102 casesense
= (i
== PP_ASSIGN
);
3104 tline
= tline
->next
;
3106 tline
= expand_id(tline
);
3107 if (!tline
|| (tline
->type
!= TOK_ID
&&
3108 (tline
->type
!= TOK_PREPROC_ID
||
3109 tline
->text
[1] != '$'))) {
3111 "`%%%sassign' expects a macro identifier",
3112 (i
== PP_IASSIGN
? "i" : ""));
3113 free_tlist(origline
);
3114 return DIRECTIVE_FOUND
;
3116 ctx
= get_ctx(tline
->text
, false);
3118 mname
= tline
->text
;
3120 tline
= expand_smacro(tline
->next
);
3125 tokval
.t_type
= TOKEN_INVALID
;
3127 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3130 free_tlist(origline
);
3131 return DIRECTIVE_FOUND
;
3136 "trailing garbage after expression ignored");
3138 if (!is_simple(evalresult
)) {
3140 "non-constant value given to `%%%sassign'",
3141 (i
== PP_IASSIGN
? "i" : ""));
3142 free_tlist(origline
);
3143 return DIRECTIVE_FOUND
;
3146 macro_start
= nasm_malloc(sizeof(*macro_start
));
3147 macro_start
->next
= NULL
;
3148 make_tok_num(macro_start
, reloc_value(evalresult
));
3149 macro_start
->a
.mac
= NULL
;
3152 * We now have a macro name, an implicit parameter count of
3153 * zero, and a numeric token to use as an expansion. Create
3154 * and store an SMacro.
3156 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3157 free_tlist(origline
);
3158 return DIRECTIVE_FOUND
;
3162 * Syntax is `%line nnn[+mmm] [filename]'
3164 tline
= tline
->next
;
3166 if (!tok_type_(tline
, TOK_NUMBER
)) {
3167 error(ERR_NONFATAL
, "`%%line' expects line number");
3168 free_tlist(origline
);
3169 return DIRECTIVE_FOUND
;
3171 k
= readnum(tline
->text
, &err
);
3173 tline
= tline
->next
;
3174 if (tok_is_(tline
, "+")) {
3175 tline
= tline
->next
;
3176 if (!tok_type_(tline
, TOK_NUMBER
)) {
3177 error(ERR_NONFATAL
, "`%%line' expects line increment");
3178 free_tlist(origline
);
3179 return DIRECTIVE_FOUND
;
3181 m
= readnum(tline
->text
, &err
);
3182 tline
= tline
->next
;
3188 nasm_free(src_set_fname(detoken(tline
, false)));
3190 free_tlist(origline
);
3191 return DIRECTIVE_FOUND
;
3195 "preprocessor directive `%s' not yet implemented",
3197 return DIRECTIVE_FOUND
;
3202 * Ensure that a macro parameter contains a condition code and
3203 * nothing else. Return the condition code index if so, or -1
3206 static int find_cc(Token
* t
)
3212 return -1; /* Probably a %+ without a space */
3215 if (t
->type
!= TOK_ID
)
3219 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3223 j
= elements(conditions
);
3226 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3242 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3243 * %-n) and MMacro-local identifiers (%%foo).
3245 static Token
*expand_mmac_params(Token
* tline
)
3247 Token
*t
, *tt
, **tail
, *thead
;
3253 if (tline
->type
== TOK_PREPROC_ID
&&
3254 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3255 && tline
->text
[2]) || tline
->text
[1] == '%'
3256 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3258 int type
= 0, cc
; /* type = 0 to placate optimisers */
3265 tline
= tline
->next
;
3268 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3269 mac
= mac
->next_active
;
3271 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3273 switch (t
->text
[1]) {
3275 * We have to make a substitution of one of the
3276 * forms %1, %-1, %+1, %%foo, %0.
3280 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3281 text
= nasm_strdup(tmpbuf
);
3285 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3287 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3290 n
= atoi(t
->text
+ 2) - 1;
3291 if (n
>= mac
->nparam
)
3294 if (mac
->nparam
> 1)
3295 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3296 tt
= mac
->params
[n
];
3301 "macro parameter %d is not a condition code",
3306 if (inverse_ccs
[cc
] == -1) {
3308 "condition code `%s' is not invertible",
3313 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3317 n
= atoi(t
->text
+ 2) - 1;
3318 if (n
>= mac
->nparam
)
3321 if (mac
->nparam
> 1)
3322 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3323 tt
= mac
->params
[n
];
3328 "macro parameter %d is not a condition code",
3333 text
= nasm_strdup(conditions
[cc
]);
3337 n
= atoi(t
->text
+ 1) - 1;
3338 if (n
>= mac
->nparam
)
3341 if (mac
->nparam
> 1)
3342 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3343 tt
= mac
->params
[n
];
3346 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3347 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3348 tail
= &(*tail
)->next
;
3352 text
= NULL
; /* we've done it here */
3368 tline
= tline
->next
;
3375 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3377 case TOK_WHITESPACE
:
3378 if (tt
->type
== TOK_WHITESPACE
) {
3379 t
->next
= delete_Token(tt
);
3383 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3384 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3387 t
->next
= delete_Token(tt
);
3391 if (tt
->type
== TOK_NUMBER
) {
3392 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3395 t
->next
= delete_Token(tt
);
3406 * Expand all single-line macro calls made in the given line.
3407 * Return the expanded version of the line. The original is deemed
3408 * to be destroyed in the process. (In reality we'll just move
3409 * Tokens from input to output a lot of the time, rather than
3410 * actually bothering to destroy and replicate.)
3412 #define DEADMAN_LIMIT (1 << 20)
3414 static Token
*expand_smacro(Token
* tline
)
3416 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3417 struct hash_table
*smtbl
;
3418 SMacro
*head
= NULL
, *m
;
3421 unsigned int nparam
, sparam
;
3422 int brackets
, rescan
;
3423 Token
*org_tline
= tline
;
3426 int deadman
= DEADMAN_LIMIT
;
3429 * Trick: we should avoid changing the start token pointer since it can
3430 * be contained in "next" field of other token. Because of this
3431 * we allocate a copy of first token and work with it; at the end of
3432 * routine we copy it back
3436 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3438 tline
->a
.mac
= org_tline
->a
.mac
;
3439 nasm_free(org_tline
->text
);
3440 org_tline
->text
= NULL
;
3447 while (tline
) { /* main token loop */
3449 error(ERR_NONFATAL
, "interminable macro recursion");
3453 if ((mname
= tline
->text
)) {
3454 /* if this token is a local macro, look in local context */
3457 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3458 ctx
= get_ctx(mname
, true);
3460 smtbl
= &ctx
->localmac
;
3462 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3465 * We've hit an identifier. As in is_mmacro below, we first
3466 * check whether the identifier is a single-line macro at
3467 * all, then think about checking for parameters if
3470 for (m
= head
; m
; m
= m
->next
)
3471 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3477 if (m
->nparam
== 0) {
3479 * Simple case: the macro is parameterless. Discard the
3480 * one token that the macro call took, and push the
3481 * expansion back on the to-do stack.
3483 if (!m
->expansion
) {
3484 if (!strcmp("__FILE__", m
->name
)) {
3487 src_get(&num
, &file
);
3488 tline
->text
= nasm_quote(file
, strlen(file
));
3489 tline
->type
= TOK_STRING
;
3493 if (!strcmp("__LINE__", m
->name
)) {
3494 nasm_free(tline
->text
);
3495 make_tok_num(tline
, src_get_linnum());
3498 if (!strcmp("__BITS__", m
->name
)) {
3499 nasm_free(tline
->text
);
3500 make_tok_num(tline
, globalbits
);
3503 tline
= delete_Token(tline
);
3508 * Complicated case: at least one macro with this name
3509 * exists and takes parameters. We must find the
3510 * parameters in the call, count them, find the SMacro
3511 * that corresponds to that form of the macro call, and
3512 * substitute for the parameters when we expand. What a
3515 /*tline = tline->next;
3516 skip_white_(tline); */
3519 while (tok_type_(t
, TOK_SMAC_END
)) {
3520 t
->a
.mac
->in_progress
= false;
3522 t
= tline
->next
= delete_Token(t
);
3525 } while (tok_type_(tline
, TOK_WHITESPACE
));
3526 if (!tok_is_(tline
, "(")) {
3528 * This macro wasn't called with parameters: ignore
3529 * the call. (Behaviour borrowed from gnu cpp.)
3538 sparam
= PARAM_DELTA
;
3539 params
= nasm_malloc(sparam
* sizeof(Token
*));
3540 params
[0] = tline
->next
;
3541 paramsize
= nasm_malloc(sparam
* sizeof(int));
3543 while (true) { /* parameter loop */
3545 * For some unusual expansions
3546 * which concatenates function call
3549 while (tok_type_(t
, TOK_SMAC_END
)) {
3550 t
->a
.mac
->in_progress
= false;
3552 t
= tline
->next
= delete_Token(t
);
3558 "macro call expects terminating `)'");
3561 if (tline
->type
== TOK_WHITESPACE
3563 if (paramsize
[nparam
])
3566 params
[nparam
] = tline
->next
;
3567 continue; /* parameter loop */
3569 if (tline
->type
== TOK_OTHER
3570 && tline
->text
[1] == 0) {
3571 char ch
= tline
->text
[0];
3572 if (ch
== ',' && !paren
&& brackets
<= 0) {
3573 if (++nparam
>= sparam
) {
3574 sparam
+= PARAM_DELTA
;
3575 params
= nasm_realloc(params
,
3580 nasm_realloc(paramsize
,
3584 params
[nparam
] = tline
->next
;
3585 paramsize
[nparam
] = 0;
3587 continue; /* parameter loop */
3590 (brackets
> 0 || (brackets
== 0 &&
3591 !paramsize
[nparam
])))
3593 if (!(brackets
++)) {
3594 params
[nparam
] = tline
->next
;
3595 continue; /* parameter loop */
3598 if (ch
== '}' && brackets
> 0)
3599 if (--brackets
== 0) {
3601 continue; /* parameter loop */
3603 if (ch
== '(' && !brackets
)
3605 if (ch
== ')' && brackets
<= 0)
3611 error(ERR_NONFATAL
, "braces do not "
3612 "enclose all of macro parameter");
3614 paramsize
[nparam
] += white
+ 1;
3616 } /* parameter loop */
3618 while (m
&& (m
->nparam
!= nparam
||
3619 mstrcmp(m
->name
, mname
,
3623 error(ERR_WARNING
| ERR_WARN_MNP
,
3624 "macro `%s' exists, "
3625 "but not taking %d parameters",
3626 mstart
->text
, nparam
);
3629 if (m
&& m
->in_progress
)
3631 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3633 * Design question: should we handle !tline, which
3634 * indicates missing ')' here, or expand those
3635 * macros anyway, which requires the (t) test a few
3639 nasm_free(paramsize
);
3643 * Expand the macro: we are placed on the last token of the
3644 * call, so that we can easily split the call from the
3645 * following tokens. We also start by pushing an SMAC_END
3646 * token for the cycle removal.
3653 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3655 m
->in_progress
= true;
3657 for (t
= m
->expansion
; t
; t
= t
->next
) {
3658 if (t
->type
>= TOK_SMAC_PARAM
) {
3659 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3663 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3664 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3667 new_Token(tline
, ttt
->type
, ttt
->text
,
3673 } else if (t
->type
== TOK_PREPROC_Q
) {
3674 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3676 } else if (t
->type
== TOK_PREPROC_QQ
) {
3677 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3680 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3686 * Having done that, get rid of the macro call, and clean
3687 * up the parameters.
3690 nasm_free(paramsize
);
3692 continue; /* main token loop */
3697 if (tline
->type
== TOK_SMAC_END
) {
3698 tline
->a
.mac
->in_progress
= false;
3699 tline
= delete_Token(tline
);
3702 tline
= tline
->next
;
3710 * Now scan the entire line and look for successive TOK_IDs that resulted
3711 * after expansion (they can't be produced by tokenize()). The successive
3712 * TOK_IDs should be concatenated.
3713 * Also we look for %+ tokens and concatenate the tokens before and after
3714 * them (without white spaces in between).
3719 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3723 if (t
->next
->type
== TOK_ID
||
3724 t
->next
->type
== TOK_PREPROC_ID
||
3725 t
->next
->type
== TOK_NUMBER
) {
3726 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3728 t
->next
= delete_Token(t
->next
);
3731 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3732 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3733 strcmp(t
->next
->next
->text
, "%+") == 0) {
3734 /* free the next whitespace, the %+ token and next whitespace */
3736 for (i
= 1; i
<= 3; i
++) {
3738 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3740 t
->next
= delete_Token(t
->next
);
3745 /* If we concatenaded something, re-scan the line for macros */
3753 *org_tline
= *thead
;
3754 /* since we just gave text to org_line, don't free it */
3756 delete_Token(thead
);
3758 /* the expression expanded to empty line;
3759 we can't return NULL for some reasons
3760 we just set the line to a single WHITESPACE token. */
3761 memset(org_tline
, 0, sizeof(*org_tline
));
3762 org_tline
->text
= NULL
;
3763 org_tline
->type
= TOK_WHITESPACE
;
3772 * Similar to expand_smacro but used exclusively with macro identifiers
3773 * right before they are fetched in. The reason is that there can be
3774 * identifiers consisting of several subparts. We consider that if there
3775 * are more than one element forming the name, user wants a expansion,
3776 * otherwise it will be left as-is. Example:
3780 * the identifier %$abc will be left as-is so that the handler for %define
3781 * will suck it and define the corresponding value. Other case:
3783 * %define _%$abc cde
3785 * In this case user wants name to be expanded *before* %define starts
3786 * working, so we'll expand %$abc into something (if it has a value;
3787 * otherwise it will be left as-is) then concatenate all successive
3790 static Token
*expand_id(Token
* tline
)
3792 Token
*cur
, *oldnext
= NULL
;
3794 if (!tline
|| !tline
->next
)
3799 (cur
->next
->type
== TOK_ID
||
3800 cur
->next
->type
== TOK_PREPROC_ID
3801 || cur
->next
->type
== TOK_NUMBER
))
3804 /* If identifier consists of just one token, don't expand */
3809 oldnext
= cur
->next
; /* Detach the tail past identifier */
3810 cur
->next
= NULL
; /* so that expand_smacro stops here */
3813 tline
= expand_smacro(tline
);
3816 /* expand_smacro possibly changhed tline; re-scan for EOL */
3818 while (cur
&& cur
->next
)
3821 cur
->next
= oldnext
;
3828 * Determine whether the given line constitutes a multi-line macro
3829 * call, and return the MMacro structure called if so. Doesn't have
3830 * to check for an initial label - that's taken care of in
3831 * expand_mmacro - but must check numbers of parameters. Guaranteed
3832 * to be called with tline->type == TOK_ID, so the putative macro
3833 * name is easy to find.
3835 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3841 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3844 * Efficiency: first we see if any macro exists with the given
3845 * name. If not, we can return NULL immediately. _Then_ we
3846 * count the parameters, and then we look further along the
3847 * list if necessary to find the proper MMacro.
3849 for (m
= head
; m
; m
= m
->next
)
3850 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3856 * OK, we have a potential macro. Count and demarcate the
3859 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3862 * So we know how many parameters we've got. Find the MMacro
3863 * structure that handles this number.
3866 if (m
->nparam_min
<= nparam
3867 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3869 * This one is right. Just check if cycle removal
3870 * prohibits us using it before we actually celebrate...
3872 if (m
->in_progress
) {
3875 "self-reference in multi-line macro `%s'", m
->name
);
3881 * It's right, and we can use it. Add its default
3882 * parameters to the end of our list if necessary.
3884 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3886 nasm_realloc(params
,
3887 ((m
->nparam_min
+ m
->ndefs
+
3888 1) * sizeof(*params
)));
3889 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3890 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3895 * If we've gone over the maximum parameter count (and
3896 * we're in Plus mode), ignore parameters beyond
3899 if (m
->plus
&& nparam
> m
->nparam_max
)
3900 nparam
= m
->nparam_max
;
3902 * Then terminate the parameter list, and leave.
3904 if (!params
) { /* need this special case */
3905 params
= nasm_malloc(sizeof(*params
));
3908 params
[nparam
] = NULL
;
3909 *params_array
= params
;
3913 * This one wasn't right: look for the next one with the
3916 for (m
= m
->next
; m
; m
= m
->next
)
3917 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3922 * After all that, we didn't find one with the right number of
3923 * parameters. Issue a warning, and fail to expand the macro.
3925 error(ERR_WARNING
| ERR_WARN_MNP
,
3926 "macro `%s' exists, but not taking %d parameters",
3927 tline
->text
, nparam
);
3933 * Expand the multi-line macro call made by the given line, if
3934 * there is one to be expanded. If there is, push the expansion on
3935 * istk->expansion and return 1. Otherwise return 0.
3937 static int expand_mmacro(Token
* tline
)
3939 Token
*startline
= tline
;
3940 Token
*label
= NULL
;
3941 int dont_prepend
= 0;
3942 Token
**params
, *t
, *mtok
, *tt
;
3945 int i
, nparam
, *paramlen
;
3950 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3951 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3954 m
= is_mmacro(t
, ¶ms
);
3960 * We have an id which isn't a macro call. We'll assume
3961 * it might be a label; we'll also check to see if a
3962 * colon follows it. Then, if there's another id after
3963 * that lot, we'll check it again for macro-hood.
3967 if (tok_type_(t
, TOK_WHITESPACE
))
3968 last
= t
, t
= t
->next
;
3969 if (tok_is_(t
, ":")) {
3971 last
= t
, t
= t
->next
;
3972 if (tok_type_(t
, TOK_WHITESPACE
))
3973 last
= t
, t
= t
->next
;
3975 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3983 * Fix up the parameters: this involves stripping leading and
3984 * trailing whitespace, then stripping braces if they are
3987 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3988 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3990 for (i
= 0; params
[i
]; i
++) {
3992 int comma
= (!m
->plus
|| i
< nparam
- 1);
3996 if (tok_is_(t
, "{"))
3997 t
= t
->next
, brace
= true, comma
= false;
4001 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4002 break; /* ... because we have hit a comma */
4003 if (comma
&& t
->type
== TOK_WHITESPACE
4004 && tok_is_(t
->next
, ","))
4005 break; /* ... or a space then a comma */
4006 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4007 break; /* ... or a brace */
4014 * OK, we have a MMacro structure together with a set of
4015 * parameters. We must now go through the expansion and push
4016 * copies of each Line on to istk->expansion. Substitution of
4017 * parameter tokens and macro-local tokens doesn't get done
4018 * until the single-line macro substitution process; this is
4019 * because delaying them allows us to change the semantics
4020 * later through %rotate.
4022 * First, push an end marker on to istk->expansion, mark this
4023 * macro as in progress, and set up its invocation-specific
4026 ll
= nasm_malloc(sizeof(Line
));
4027 ll
->next
= istk
->expansion
;
4030 istk
->expansion
= ll
;
4032 m
->in_progress
= true;
4037 m
->paramlen
= paramlen
;
4038 m
->unique
= unique
++;
4041 m
->next_active
= istk
->mstk
;
4044 for (l
= m
->expansion
; l
; l
= l
->next
) {
4047 ll
= nasm_malloc(sizeof(Line
));
4048 ll
->finishes
= NULL
;
4049 ll
->next
= istk
->expansion
;
4050 istk
->expansion
= ll
;
4053 for (t
= l
->first
; t
; t
= t
->next
) {
4057 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4059 case TOK_PREPROC_QQ
:
4060 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4062 case TOK_PREPROC_ID
:
4063 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4071 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4080 * If we had a label, push it on as the first line of
4081 * the macro expansion.
4084 if (dont_prepend
< 0)
4085 free_tlist(startline
);
4087 ll
= nasm_malloc(sizeof(Line
));
4088 ll
->finishes
= NULL
;
4089 ll
->next
= istk
->expansion
;
4090 istk
->expansion
= ll
;
4091 ll
->first
= startline
;
4092 if (!dont_prepend
) {
4094 label
= label
->next
;
4095 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4100 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4106 * Since preprocessor always operate only on the line that didn't
4107 * arrived yet, we should always use ERR_OFFBY1. Also since user
4108 * won't want to see same error twice (preprocessing is done once
4109 * per pass) we will want to show errors only during pass one.
4111 static void error(int severity
, const char *fmt
, ...)
4116 /* If we're in a dead branch of IF or something like it, ignore the error */
4117 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4121 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4124 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4125 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
4126 istk
->mstk
->lineno
, buff
);
4128 _error(severity
| ERR_PASS1
, "%s", buff
);
4132 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4133 ListGen
* listgen
, StrList
**deplist
)
4137 istk
= nasm_malloc(sizeof(Include
));
4140 istk
->expansion
= NULL
;
4142 istk
->fp
= fopen(file
, "r");
4144 src_set_fname(nasm_strdup(file
));
4148 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
4151 nested_mac_count
= 0;
4152 nested_rep_count
= 0;
4155 if (tasm_compatible_mode
) {
4156 stdmacpos
= nasm_stdmac
;
4158 stdmacpos
= nasm_stdmac_after_tasm
;
4160 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4165 dephead
= deptail
= deplist
;
4167 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4169 strcpy(sl
->str
, file
);
4171 deptail
= &sl
->next
;
4175 static char *pp_getline(void)
4182 * Fetch a tokenized line, either from the macro-expansion
4183 * buffer or from the input file.
4186 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4187 Line
*l
= istk
->expansion
;
4188 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4192 * This is a macro-end marker for a macro with no
4193 * name, which means it's not really a macro at all
4194 * but a %rep block, and the `in_progress' field is
4195 * more than 1, meaning that we still need to
4196 * repeat. (1 means the natural last repetition; 0
4197 * means termination by %exitrep.) We have
4198 * therefore expanded up to the %endrep, and must
4199 * push the whole block on to the expansion buffer
4200 * again. We don't bother to remove the macro-end
4201 * marker: we'd only have to generate another one
4204 l
->finishes
->in_progress
--;
4205 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4206 Token
*t
, *tt
, **tail
;
4208 ll
= nasm_malloc(sizeof(Line
));
4209 ll
->next
= istk
->expansion
;
4210 ll
->finishes
= NULL
;
4214 for (t
= l
->first
; t
; t
= t
->next
) {
4215 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4217 new_Token(NULL
, t
->type
, t
->text
, 0);
4222 istk
->expansion
= ll
;
4226 * Check whether a `%rep' was started and not ended
4227 * within this macro expansion. This can happen and
4228 * should be detected. It's a fatal error because
4229 * I'm too confused to work out how to recover
4235 "defining with name in expansion");
4236 else if (istk
->mstk
->name
)
4238 "`%%rep' without `%%endrep' within"
4239 " expansion of macro `%s'",
4244 * FIXME: investigate the relationship at this point between
4245 * istk->mstk and l->finishes
4248 MMacro
*m
= istk
->mstk
;
4249 istk
->mstk
= m
->next_active
;
4252 * This was a real macro call, not a %rep, and
4253 * therefore the parameter information needs to
4256 nasm_free(m
->params
);
4257 free_tlist(m
->iline
);
4258 nasm_free(m
->paramlen
);
4259 l
->finishes
->in_progress
= false;
4263 istk
->expansion
= l
->next
;
4265 list
->downlevel(LIST_MACRO
);
4268 while (1) { /* until we get a line we can use */
4270 if (istk
->expansion
) { /* from a macro expansion */
4272 Line
*l
= istk
->expansion
;
4274 istk
->mstk
->lineno
++;
4276 istk
->expansion
= l
->next
;
4278 p
= detoken(tline
, false);
4279 list
->line(LIST_MACRO
, p
);
4284 if (line
) { /* from the current input file */
4285 line
= prepreproc(line
);
4286 tline
= tokenize(line
);
4291 * The current file has ended; work down the istk
4298 "expected `%%endif' before end of file");
4299 /* only set line and file name if there's a next node */
4301 src_set_linnum(i
->lineno
);
4302 nasm_free(src_set_fname(i
->fname
));
4305 list
->downlevel(LIST_INCLUDE
);
4313 * We must expand MMacro parameters and MMacro-local labels
4314 * _before_ we plunge into directive processing, to cope
4315 * with things like `%define something %1' such as STRUC
4316 * uses. Unless we're _defining_ a MMacro, in which case
4317 * those tokens should be left alone to go into the
4318 * definition; and unless we're in a non-emitting
4319 * condition, in which case we don't want to meddle with
4322 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4323 && !(istk
->mstk
&& !istk
->mstk
->in_progress
))
4324 tline
= expand_mmac_params(tline
);
4327 * Check the line to see if it's a preprocessor directive.
4329 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4331 } else if (defining
) {
4333 * We're defining a multi-line macro. We emit nothing
4335 * shove the tokenized line on to the macro definition.
4337 Line
*l
= nasm_malloc(sizeof(Line
));
4338 l
->next
= defining
->expansion
;
4341 defining
->expansion
= l
;
4343 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4345 * We're in a non-emitting branch of a condition block.
4346 * Emit nothing at all, not even a blank line: when we
4347 * emerge from the condition we'll give a line-number
4348 * directive so we keep our place correctly.
4352 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4354 * We're in a %rep block which has been terminated, so
4355 * we're walking through to the %endrep without
4356 * emitting anything. Emit nothing at all, not even a
4357 * blank line: when we emerge from the %rep block we'll
4358 * give a line-number directive so we keep our place
4364 tline
= expand_smacro(tline
);
4365 if (!expand_mmacro(tline
)) {
4367 * De-tokenize the line again, and emit it.
4369 line
= detoken(tline
, true);
4373 continue; /* expand_mmacro calls free_tlist */
4381 static void pp_cleanup(int pass
)
4384 if(defining
->name
) {
4386 "end of file while still defining macro `%s'",
4389 error(ERR_NONFATAL
, "end of file while still in %%rep");
4392 free_mmacro(defining
);
4401 nasm_free(i
->fname
);
4406 nasm_free(src_set_fname(NULL
));
4411 while ((i
= ipath
)) {
4420 void pp_include_path(char *path
)
4424 i
= nasm_malloc(sizeof(IncPath
));
4425 i
->path
= path
? nasm_strdup(path
) : NULL
;
4428 if (ipath
!= NULL
) {
4430 while (j
->next
!= NULL
)
4438 void pp_pre_include(char *fname
)
4440 Token
*inc
, *space
, *name
;
4443 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4444 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4445 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4447 l
= nasm_malloc(sizeof(Line
));
4454 void pp_pre_define(char *definition
)
4460 equals
= strchr(definition
, '=');
4461 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4462 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4465 space
->next
= tokenize(definition
);
4469 l
= nasm_malloc(sizeof(Line
));
4476 void pp_pre_undefine(char *definition
)
4481 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4482 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4483 space
->next
= tokenize(definition
);
4485 l
= nasm_malloc(sizeof(Line
));
4493 * Added by Keith Kanios:
4495 * This function is used to assist with "runtime" preprocessor
4496 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4498 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4499 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4502 void pp_runtime(char *definition
)
4506 def
= tokenize(definition
);
4507 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4512 void pp_extra_stdmac(const macros_t
*macros
)
4514 extrastdmac
= macros
;
4517 static void make_tok_num(Token
* tok
, int64_t val
)
4520 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4521 tok
->text
= nasm_strdup(numbuf
);
4522 tok
->type
= TOK_NUMBER
;