1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
56 typedef struct SMacro SMacro
;
57 typedef struct MMacro MMacro
;
58 typedef struct Context Context
;
59 typedef struct Token Token
;
60 typedef struct Blocks Blocks
;
61 typedef struct Line Line
;
62 typedef struct Include Include
;
63 typedef struct Cond Cond
;
64 typedef struct IncPath IncPath
;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
107 int nparam_min
, nparam_max
;
109 bool plus
; /* is the last parameter greedy? */
110 bool nolist
; /* is this macro listing-inhibited? */
112 Token
*dlist
; /* All defaults as one list */
113 Token
**defaults
; /* Parameter default pointers */
114 int ndefs
; /* number of default parameters */
118 MMacro
*rep_nest
; /* used for nesting %rep */
119 Token
**params
; /* actual parameters */
120 Token
*iline
; /* invocation line */
121 unsigned int nparam
, rotate
;
124 int lineno
; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
133 struct hash_table localmac
;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
157 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
158 TOK_PREPROC_ID
, TOK_STRING
,
159 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
161 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
162 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
169 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
170 enum pp_token_type type
;
174 * Multi-line macro definitions are stored as a linked list of
175 * these, which is essentially a container to allow several linked
178 * Note that in this module, linked lists are treated as stacks
179 * wherever possible. For this reason, Lines are _pushed_ on to the
180 * `expansion' field in MMacro structures, so that the linked list,
181 * if walked, would give the macro lines in reverse order; this
182 * means that we can walk the list when expanding a macro, and thus
183 * push the lines on to the `expansion' field in _istk_ in reverse
184 * order (so that when popped back off they are in the right
185 * order). It may seem cockeyed, and it relies on my design having
186 * an even number of steps in, but it works...
188 * Some of these structures, rather than being actual lines, are
189 * markers delimiting the end of the expansion of a given macro.
190 * This is for use in the cycle-tracking and %rep-handling code.
191 * Such structures have `finishes' non-NULL, and `first' NULL. All
192 * others have `finishes' NULL, but `first' may still be NULL if
202 * To handle an arbitrary level of file inclusion, we maintain a
203 * stack (ie linked list) of these things.
212 MMacro
*mstk
; /* stack of active macros/reps */
216 * Include search path. This is simply a list of strings which get
217 * prepended, in turn, to the name of an include file, in an
218 * attempt to find the file if it's not in the current directory.
226 * Conditional assembly: we maintain a separate stack of these for
227 * each level of file inclusion. (The only reason we keep the
228 * stacks separate is to ensure that a stray `%endif' in a file
229 * included from within the true branch of a `%if' won't terminate
230 * it and cause confusion: instead, rightly, it'll cause an error.)
238 * These states are for use just after %if or %elif: IF_TRUE
239 * means the condition has evaluated to truth so we are
240 * currently emitting, whereas IF_FALSE means we are not
241 * currently emitting but will start doing so if a %else comes
242 * up. In these states, all directives are admissible: %elif,
243 * %else and %endif. (And of course %if.)
245 COND_IF_TRUE
, COND_IF_FALSE
,
247 * These states come up after a %else: ELSE_TRUE means we're
248 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
249 * any %elif or %else will cause an error.
251 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
253 * This state means that we're not emitting now, and also that
254 * nothing until %endif will be emitted at all. It's for use in
255 * two circumstances: (i) when we've had our moment of emission
256 * and have now started seeing %elifs, and (ii) when the
257 * condition construct in question is contained within a
258 * non-emitting branch of a larger condition construct.
262 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
265 * These defines are used as the possible return values for do_directive
267 #define NO_DIRECTIVE_FOUND 0
268 #define DIRECTIVE_FOUND 1
271 * Condition codes. Note that we use c_ prefix not C_ because C_ is
272 * used in nasm.h for the "real" condition codes. At _this_ level,
273 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
274 * ones, so we need a different enum...
276 static const char * const conditions
[] = {
277 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
278 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
279 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
282 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
283 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
284 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
287 static const enum pp_conds inverse_ccs
[] = {
288 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
289 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_E
, c_G
, c_GE
, c_L
, c_LE
, c_O
, c_P
, c_S
,
290 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
296 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
297 static int is_condition(enum preproc_token arg
)
299 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
302 /* For TASM compatibility we need to be able to recognise TASM compatible
303 * conditional compilation directives. Using the NASM pre-processor does
304 * not work, so we look for them specifically from the following list and
305 * then jam in the equivalent NASM directive into the input stream.
309 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
310 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
313 static const char * const tasm_directives
[] = {
314 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
315 "ifndef", "include", "local"
318 static int StackSize
= 4;
319 static char *StackPointer
= "ebp";
320 static int ArgOffset
= 8;
321 static int LocalOffset
= 0;
323 static Context
*cstk
;
324 static Include
*istk
;
325 static IncPath
*ipath
= NULL
;
327 static efunc _error
; /* Pointer to client-provided error reporting function */
328 static evalfunc evaluate
;
330 static int pass
; /* HACK: pass 0 = generate dependencies only */
331 static StrList
**dephead
, **deptail
; /* Dependency list */
333 static uint64_t unique
; /* unique identifier numbers */
335 static Line
*predef
= NULL
;
336 static bool do_predef
;
338 static ListGen
*list
;
341 * The current set of multi-line macros we have defined.
343 static struct hash_table mmacros
;
346 * The current set of single-line macros we have defined.
348 static struct hash_table smacros
;
351 * The multi-line macro we are currently defining, or the %rep
352 * block we are currently reading, if any.
354 static MMacro
*defining
;
357 * The number of macro parameters to allocate space for at a time.
359 #define PARAM_DELTA 16
362 * The standard macro set: defined in macros.c in the array nasm_stdmac.
363 * This gives our position in the macro set, when we're processing it.
365 static const macros_t
*stdmacpos
;
368 * The extra standard macros that come from the object format, if
371 static const macros_t
*extrastdmac
= NULL
;
372 static bool any_extrastdmac
;
375 * Tokens are allocated in blocks to improve speed
377 #define TOKEN_BLOCKSIZE 4096
378 static Token
*freeTokens
= NULL
;
384 static Blocks blocks
= { NULL
, NULL
};
387 * Forward declarations.
389 static Token
*expand_mmac_params(Token
* tline
);
390 static Token
*expand_smacro(Token
* tline
);
391 static Token
*expand_id(Token
* tline
);
392 static Context
*get_ctx(const char *name
, bool all_contexts
);
393 static void make_tok_num(Token
* tok
, int64_t val
);
394 static void error(int severity
, const char *fmt
, ...);
395 static void *new_Block(size_t size
);
396 static void delete_Blocks(void);
397 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
398 const char *text
, int txtlen
);
399 static Token
*delete_Token(Token
* t
);
402 * Macros for safe checking of token pointers, avoid *(NULL)
404 #define tok_type_(x,t) ((x) && (x)->type == (t))
405 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
406 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
407 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
409 /* Handle TASM specific directives, which do not contain a % in
410 * front of them. We do it here because I could not find any other
411 * place to do it for the moment, and it is a hack (ideally it would
412 * be nice to be able to use the NASM pre-processor to do it).
414 static char *check_tasm_directive(char *line
)
416 int32_t i
, j
, k
, m
, len
;
417 char *p
= line
, *oldline
, oldchar
;
419 /* Skip whitespace */
420 while (nasm_isspace(*p
) && *p
!= 0)
423 /* Binary search for the directive name */
425 j
= elements(tasm_directives
);
427 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
434 m
= nasm_stricmp(p
, tasm_directives
[k
]);
436 /* We have found a directive, so jam a % in front of it
437 * so that NASM will then recognise it as one if it's own.
442 line
= nasm_malloc(len
+ 2);
444 if (k
== TM_IFDIFI
) {
445 /* NASM does not recognise IFDIFI, so we convert it to
446 * %ifdef BOGUS. This is not used in NASM comaptible
447 * code, but does need to parse for the TASM macro
450 strcpy(line
+ 1, "ifdef BOGUS");
452 memcpy(line
+ 1, p
, len
+ 1);
467 * The pre-preprocessing stage... This function translates line
468 * number indications as they emerge from GNU cpp (`# lineno "file"
469 * flags') into NASM preprocessor line number indications (`%line
472 static char *prepreproc(char *line
)
475 char *fname
, *oldline
;
477 if (line
[0] == '#' && line
[1] == ' ') {
480 lineno
= atoi(fname
);
481 fname
+= strspn(fname
, "0123456789 ");
484 fnlen
= strcspn(fname
, "\"");
485 line
= nasm_malloc(20 + fnlen
);
486 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
489 if (tasm_compatible_mode
)
490 return check_tasm_directive(line
);
495 * Free a linked list of tokens.
497 static void free_tlist(Token
* list
)
500 list
= delete_Token(list
);
505 * Free a linked list of lines.
507 static void free_llist(Line
* list
)
513 free_tlist(l
->first
);
521 static void free_mmacro(MMacro
* m
)
524 free_tlist(m
->dlist
);
525 nasm_free(m
->defaults
);
526 free_llist(m
->expansion
);
531 * Free all currently defined macros, and free the hash tables
533 static void free_smacro_table(struct hash_table
*smt
)
537 struct hash_tbl_node
*it
= NULL
;
539 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
540 nasm_free((void *)key
);
542 SMacro
*ns
= s
->next
;
544 free_tlist(s
->expansion
);
552 static void free_mmacro_table(struct hash_table
*mmt
)
556 struct hash_tbl_node
*it
= NULL
;
559 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
560 nasm_free((void *)key
);
562 MMacro
*nm
= m
->next
;
570 static void free_macros(void)
572 free_smacro_table(&smacros
);
573 free_mmacro_table(&mmacros
);
577 * Initialize the hash tables
579 static void init_macros(void)
581 hash_init(&smacros
, HASH_LARGE
);
582 hash_init(&mmacros
, HASH_LARGE
);
586 * Pop the context stack.
588 static void ctx_pop(void)
593 free_smacro_table(&c
->localmac
);
599 * Search for a key in the hash index; adding it if necessary
600 * (in which case we initialize the data pointer to NULL.)
603 hash_findi_add(struct hash_table
*hash
, const char *str
)
605 struct hash_insert hi
;
609 r
= hash_findi(hash
, str
, &hi
);
613 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
614 return hash_add(&hi
, strx
, NULL
);
618 * Like hash_findi, but returns the data element rather than a pointer
619 * to it. Used only when not adding a new element, hence no third
623 hash_findix(struct hash_table
*hash
, const char *str
)
627 p
= hash_findi(hash
, str
, NULL
);
628 return p
? *p
: NULL
;
631 #define BUF_DELTA 512
633 * Read a line from the top file in istk, handling multiple CR/LFs
634 * at the end of the line read, and handling spurious ^Zs. Will
635 * return lines from the standard macro set if this has not already
638 static char *read_line(void)
640 char *buffer
, *p
, *q
;
641 int bufsize
, continued_count
;
645 const unsigned char *p
= stdmacpos
;
650 len
+= pp_directives_len
[c
-0x80]+1;
654 ret
= nasm_malloc(len
+1);
656 while ((c
= *stdmacpos
++)) {
658 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
659 q
+= pp_directives_len
[c
-0x80];
669 /* This was the last of the standard macro chain... */
671 if (any_extrastdmac
) {
672 stdmacpos
= extrastdmac
;
673 any_extrastdmac
= false;
674 } else if (do_predef
) {
676 Token
*head
, **tail
, *t
;
679 * Nasty hack: here we push the contents of
680 * `predef' on to the top-level expansion stack,
681 * since this is the most convenient way to
682 * implement the pre-include and pre-define
685 for (pd
= predef
; pd
; pd
= pd
->next
) {
688 for (t
= pd
->first
; t
; t
= t
->next
) {
689 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
690 tail
= &(*tail
)->next
;
692 l
= nasm_malloc(sizeof(Line
));
693 l
->next
= istk
->expansion
;
705 buffer
= nasm_malloc(BUF_DELTA
);
709 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
713 if (p
> buffer
&& p
[-1] == '\n') {
714 /* Convert backslash-CRLF line continuation sequences into
715 nothing at all (for DOS and Windows) */
716 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
721 /* Also convert backslash-LF line continuation sequences into
722 nothing at all (for Unix) */
723 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
731 if (p
- buffer
> bufsize
- 10) {
732 int32_t offset
= p
- buffer
;
733 bufsize
+= BUF_DELTA
;
734 buffer
= nasm_realloc(buffer
, bufsize
);
735 p
= buffer
+ offset
; /* prevent stale-pointer problems */
739 if (!q
&& p
== buffer
) {
744 src_set_linnum(src_get_linnum() + istk
->lineinc
+
745 (continued_count
* istk
->lineinc
));
748 * Play safe: remove CRs as well as LFs, if any of either are
749 * present at the end of the line.
751 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
755 * Handle spurious ^Z, which may be inserted into source files
756 * by some file transfer utilities.
758 buffer
[strcspn(buffer
, "\032")] = '\0';
760 list
->line(LIST_READ
, buffer
);
766 * Tokenize a line of text. This is a very simple process since we
767 * don't need to parse the value out of e.g. numeric tokens: we
768 * simply split one string into many.
770 static Token
*tokenize(char *line
)
773 enum pp_token_type type
;
775 Token
*t
, **tail
= &list
;
781 if (nasm_isdigit(*p
) ||
782 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
783 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
787 while (nasm_isdigit(*p
));
788 type
= TOK_PREPROC_ID
;
789 } else if (*p
== '{') {
791 while (*p
&& *p
!= '}') {
798 type
= TOK_PREPROC_ID
;
799 } else if (*p
== '?') {
800 type
= TOK_PREPROC_Q
; /* %? */
803 type
= TOK_PREPROC_QQ
; /* %?? */
806 } else if (isidchar(*p
) ||
807 ((*p
== '!' || *p
== '%' || *p
== '$') &&
812 while (isidchar(*p
));
813 type
= TOK_PREPROC_ID
;
819 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
822 while (*p
&& isidchar(*p
))
824 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
829 p
= nasm_skip_string(p
);
834 error(ERR_WARNING
, "unterminated string");
835 /* Handling unterminated strings by UNV */
838 } else if (isnumstart(*p
)) {
840 bool is_float
= false;
856 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
858 if (*p
== '+' || *p
== '-') {
859 /* e can only be followed by +/- if it is either a
860 prefixed hex number or a floating-point number */
864 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
866 } else if (c
== 'P' || c
== 'p') {
868 if (*p
== '+' || *p
== '-')
870 } else if (isnumchar(c
) || c
== '_')
873 /* we need to deal with consequences of the legacy
874 parser, like "1.nolist" being two tokens
875 (TOK_NUMBER, TOK_ID) here; at least give it
876 a shot for now. In the future, we probably need
877 a flex-based scanner with proper pattern matching
878 to do it as well as it can be done. Nothing in
879 the world is going to help the person who wants
880 0x123.p16 interpreted as two tokens, though. */
885 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
886 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
887 (*r
== 'p' || *r
== 'P')) {
891 break; /* Terminate the token */
895 p
--; /* Point to first character beyond number */
897 if (has_e
&& !is_hex
) {
898 /* 1e13 is floating-point, but 1e13h is not */
902 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
903 } else if (nasm_isspace(*p
)) {
904 type
= TOK_WHITESPACE
;
906 while (*p
&& nasm_isspace(*p
))
909 * Whitespace just before end-of-line is discarded by
910 * pretending it's a comment; whitespace just before a
911 * comment gets lumped into the comment.
913 if (!*p
|| *p
== ';') {
918 } else if (*p
== ';') {
924 * Anything else is an operator of some kind. We check
925 * for all the double-character operators (>>, <<, //,
926 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
927 * else is a single-character operator.
930 if ((p
[0] == '>' && p
[1] == '>') ||
931 (p
[0] == '<' && p
[1] == '<') ||
932 (p
[0] == '/' && p
[1] == '/') ||
933 (p
[0] == '<' && p
[1] == '=') ||
934 (p
[0] == '>' && p
[1] == '=') ||
935 (p
[0] == '=' && p
[1] == '=') ||
936 (p
[0] == '!' && p
[1] == '=') ||
937 (p
[0] == '<' && p
[1] == '>') ||
938 (p
[0] == '&' && p
[1] == '&') ||
939 (p
[0] == '|' && p
[1] == '|') ||
940 (p
[0] == '^' && p
[1] == '^')) {
946 /* Handling unterminated string by UNV */
949 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
950 t->text[p-line] = *line;
954 if (type
!= TOK_COMMENT
) {
955 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
964 * this function allocates a new managed block of memory and
965 * returns a pointer to the block. The managed blocks are
966 * deleted only all at once by the delete_Blocks function.
968 static void *new_Block(size_t size
)
972 /* first, get to the end of the linked list */
975 /* now allocate the requested chunk */
976 b
->chunk
= nasm_malloc(size
);
978 /* now allocate a new block for the next request */
979 b
->next
= nasm_malloc(sizeof(Blocks
));
980 /* and initialize the contents of the new block */
981 b
->next
->next
= NULL
;
982 b
->next
->chunk
= NULL
;
987 * this function deletes all managed blocks of memory
989 static void delete_Blocks(void)
991 Blocks
*a
, *b
= &blocks
;
994 * keep in mind that the first block, pointed to by blocks
995 * is a static and not dynamically allocated, so we don't
1000 nasm_free(b
->chunk
);
1009 * this function creates a new Token and passes a pointer to it
1010 * back to the caller. It sets the type and text elements, and
1011 * also the mac and next elements to NULL.
1013 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1014 const char *text
, int txtlen
)
1019 if (freeTokens
== NULL
) {
1020 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1021 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1022 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1023 freeTokens
[i
].next
= NULL
;
1026 freeTokens
= t
->next
;
1030 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1034 txtlen
= strlen(text
);
1035 t
->text
= nasm_malloc(txtlen
+1);
1036 memcpy(t
->text
, text
, txtlen
);
1037 t
->text
[txtlen
] = '\0';
1042 static Token
*delete_Token(Token
* t
)
1044 Token
*next
= t
->next
;
1046 t
->next
= freeTokens
;
1052 * Convert a line of tokens back into text.
1053 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1054 * will be transformed into ..@ctxnum.xxx
1056 static char *detoken(Token
* tlist
, bool expand_locals
)
1064 for (t
= tlist
; t
; t
= t
->next
) {
1065 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1066 char *p
= getenv(t
->text
+ 2);
1069 t
->text
= nasm_strdup(p
);
1073 /* Expand local macros here and not during preprocessing */
1074 if (expand_locals
&&
1075 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1076 t
->text
[0] == '%' && t
->text
[1] == '$') {
1077 Context
*ctx
= get_ctx(t
->text
, false);
1080 char *p
, *q
= t
->text
+ 2;
1082 q
+= strspn(q
, "$");
1083 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1084 p
= nasm_strcat(buffer
, q
);
1089 if (t
->type
== TOK_WHITESPACE
) {
1091 } else if (t
->text
) {
1092 len
+= strlen(t
->text
);
1095 p
= line
= nasm_malloc(len
+ 1);
1096 for (t
= tlist
; t
; t
= t
->next
) {
1097 if (t
->type
== TOK_WHITESPACE
) {
1099 } else if (t
->text
) {
1110 * A scanner, suitable for use by the expression evaluator, which
1111 * operates on a line of Tokens. Expects a pointer to a pointer to
1112 * the first token in the line to be passed in as its private_data
1115 * FIX: This really needs to be unified with stdscan.
1117 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1119 Token
**tlineptr
= private_data
;
1121 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1125 *tlineptr
= tline
? tline
->next
: NULL
;
1127 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1128 tline
->type
== TOK_COMMENT
));
1131 return tokval
->t_type
= TOKEN_EOS
;
1133 tokval
->t_charptr
= tline
->text
;
1135 if (tline
->text
[0] == '$' && !tline
->text
[1])
1136 return tokval
->t_type
= TOKEN_HERE
;
1137 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1138 return tokval
->t_type
= TOKEN_BASE
;
1140 if (tline
->type
== TOK_ID
) {
1141 p
= tokval
->t_charptr
= tline
->text
;
1143 tokval
->t_charptr
++;
1144 return tokval
->t_type
= TOKEN_ID
;
1147 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1148 if (r
>= p
+MAX_KEYWORD
)
1149 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1150 *s
++ = nasm_tolower(*r
);
1153 /* right, so we have an identifier sitting in temp storage. now,
1154 * is it actually a register or instruction name, or what? */
1155 return nasm_token_hash(ourcopy
, tokval
);
1158 if (tline
->type
== TOK_NUMBER
) {
1160 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1161 tokval
->t_charptr
= tline
->text
;
1163 return tokval
->t_type
= TOKEN_ERRNUM
;
1165 return tokval
->t_type
= TOKEN_NUM
;
1168 if (tline
->type
== TOK_FLOAT
) {
1169 return tokval
->t_type
= TOKEN_FLOAT
;
1172 if (tline
->type
== TOK_STRING
) {
1175 bq
= tline
->text
[0];
1176 tokval
->t_charptr
= tline
->text
;
1177 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1179 if (ep
[0] != bq
|| ep
[1] != '\0')
1180 return tokval
->t_type
= TOKEN_ERRSTR
;
1182 return tokval
->t_type
= TOKEN_STR
;
1185 if (tline
->type
== TOK_OTHER
) {
1186 if (!strcmp(tline
->text
, "<<"))
1187 return tokval
->t_type
= TOKEN_SHL
;
1188 if (!strcmp(tline
->text
, ">>"))
1189 return tokval
->t_type
= TOKEN_SHR
;
1190 if (!strcmp(tline
->text
, "//"))
1191 return tokval
->t_type
= TOKEN_SDIV
;
1192 if (!strcmp(tline
->text
, "%%"))
1193 return tokval
->t_type
= TOKEN_SMOD
;
1194 if (!strcmp(tline
->text
, "=="))
1195 return tokval
->t_type
= TOKEN_EQ
;
1196 if (!strcmp(tline
->text
, "<>"))
1197 return tokval
->t_type
= TOKEN_NE
;
1198 if (!strcmp(tline
->text
, "!="))
1199 return tokval
->t_type
= TOKEN_NE
;
1200 if (!strcmp(tline
->text
, "<="))
1201 return tokval
->t_type
= TOKEN_LE
;
1202 if (!strcmp(tline
->text
, ">="))
1203 return tokval
->t_type
= TOKEN_GE
;
1204 if (!strcmp(tline
->text
, "&&"))
1205 return tokval
->t_type
= TOKEN_DBL_AND
;
1206 if (!strcmp(tline
->text
, "^^"))
1207 return tokval
->t_type
= TOKEN_DBL_XOR
;
1208 if (!strcmp(tline
->text
, "||"))
1209 return tokval
->t_type
= TOKEN_DBL_OR
;
1213 * We have no other options: just return the first character of
1216 return tokval
->t_type
= tline
->text
[0];
1220 * Compare a string to the name of an existing macro; this is a
1221 * simple wrapper which calls either strcmp or nasm_stricmp
1222 * depending on the value of the `casesense' parameter.
1224 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1226 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1230 * Compare a string to the name of an existing macro; this is a
1231 * simple wrapper which calls either strcmp or nasm_stricmp
1232 * depending on the value of the `casesense' parameter.
1234 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1236 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1240 * Return the Context structure associated with a %$ token. Return
1241 * NULL, having _already_ reported an error condition, if the
1242 * context stack isn't deep enough for the supplied number of $
1244 * If all_contexts == true, contexts that enclose current are
1245 * also scanned for such smacro, until it is found; if not -
1246 * only the context that directly results from the number of $'s
1247 * in variable's name.
1249 static Context
*get_ctx(const char *name
, bool all_contexts
)
1255 if (!name
|| name
[0] != '%' || name
[1] != '$')
1259 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1263 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1265 /* i--; Lino - 02/25/02 */
1268 error(ERR_NONFATAL
, "`%s': context stack is only"
1269 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1276 /* Search for this smacro in found context */
1277 m
= hash_findix(&ctx
->localmac
, name
);
1279 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1290 * Check to see if a file is already in a string list
1292 static bool in_list(const StrList
*list
, const char *str
)
1295 if (!strcmp(list
->str
, str
))
1303 * Open an include file. This routine must always return a valid
1304 * file pointer if it returns - it's responsible for throwing an
1305 * ERR_FATAL and bombing out completely if not. It should also try
1306 * the include path one by one until it finds the file or reaches
1307 * the end of the path.
1309 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1314 IncPath
*ip
= ipath
;
1315 int len
= strlen(file
);
1316 size_t prefix_len
= 0;
1320 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1321 memcpy(sl
->str
, prefix
, prefix_len
);
1322 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1323 fp
= fopen(sl
->str
, "r");
1324 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1342 prefix_len
= strlen(prefix
);
1344 /* -MG given and file not found */
1345 if (dhead
&& !in_list(*dhead
, file
)) {
1346 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1348 strcpy(sl
->str
, file
);
1356 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1357 return NULL
; /* never reached - placate compilers */
1361 * Determine if we should warn on defining a single-line macro of
1362 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1363 * return true if _any_ single-line macro of that name is defined.
1364 * Otherwise, will return true if a single-line macro with either
1365 * `nparam' or no parameters is defined.
1367 * If a macro with precisely the right number of parameters is
1368 * defined, or nparam is -1, the address of the definition structure
1369 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1370 * is NULL, no action will be taken regarding its contents, and no
1373 * Note that this is also called with nparam zero to resolve
1376 * If you already know which context macro belongs to, you can pass
1377 * the context pointer as first parameter; if you won't but name begins
1378 * with %$ the context will be automatically computed. If all_contexts
1379 * is true, macro will be searched in outer contexts as well.
1382 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1385 struct hash_table
*smtbl
;
1389 smtbl
= &ctx
->localmac
;
1390 } else if (name
[0] == '%' && name
[1] == '$') {
1392 ctx
= get_ctx(name
, false);
1394 return false; /* got to return _something_ */
1395 smtbl
= &ctx
->localmac
;
1399 m
= (SMacro
*) hash_findix(smtbl
, name
);
1402 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1403 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1405 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1419 * Count and mark off the parameters in a multi-line macro call.
1420 * This is called both from within the multi-line macro expansion
1421 * code, and also to mark off the default parameters when provided
1422 * in a %macro definition line.
1424 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1426 int paramsize
, brace
;
1428 *nparam
= paramsize
= 0;
1431 if (*nparam
>= paramsize
) {
1432 paramsize
+= PARAM_DELTA
;
1433 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1437 if (tok_is_(t
, "{"))
1439 (*params
)[(*nparam
)++] = t
;
1440 while (tok_isnt_(t
, brace
? "}" : ","))
1442 if (t
) { /* got a comma/brace */
1446 * Now we've found the closing brace, look further
1450 if (tok_isnt_(t
, ",")) {
1452 "braces do not enclose all of macro parameter");
1453 while (tok_isnt_(t
, ","))
1457 t
= t
->next
; /* eat the comma */
1464 * Determine whether one of the various `if' conditions is true or
1467 * We must free the tline we get passed.
1469 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1471 enum pp_conditional i
= PP_COND(ct
);
1473 Token
*t
, *tt
, **tptr
, *origline
;
1474 struct tokenval tokval
;
1476 enum pp_token_type needtype
;
1482 j
= false; /* have we matched yet? */
1483 while (cstk
&& tline
) {
1485 if (!tline
|| tline
->type
!= TOK_ID
) {
1487 "`%s' expects context identifiers", pp_directives
[ct
]);
1488 free_tlist(origline
);
1491 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1493 tline
= tline
->next
;
1498 j
= false; /* have we matched yet? */
1501 if (!tline
|| (tline
->type
!= TOK_ID
&&
1502 (tline
->type
!= TOK_PREPROC_ID
||
1503 tline
->text
[1] != '$'))) {
1505 "`%s' expects macro identifiers", pp_directives
[ct
]);
1508 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1510 tline
= tline
->next
;
1516 tline
= expand_smacro(tline
);
1518 while (tok_isnt_(tt
, ","))
1522 "`%s' expects two comma-separated arguments",
1527 j
= true; /* assume equality unless proved not */
1528 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1529 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1530 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1534 if (t
->type
== TOK_WHITESPACE
) {
1538 if (tt
->type
== TOK_WHITESPACE
) {
1542 if (tt
->type
!= t
->type
) {
1543 j
= false; /* found mismatching tokens */
1546 /* When comparing strings, need to unquote them first */
1547 if (t
->type
== TOK_STRING
) {
1548 size_t l1
= nasm_unquote(t
->text
, NULL
);
1549 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1555 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1559 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1560 j
= false; /* found mismatching tokens */
1567 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1568 j
= false; /* trailing gunk on one end or other */
1574 MMacro searching
, *mmac
;
1576 tline
= tline
->next
;
1578 tline
= expand_id(tline
);
1579 if (!tok_type_(tline
, TOK_ID
)) {
1581 "`%s' expects a macro name", pp_directives
[ct
]);
1584 searching
.name
= nasm_strdup(tline
->text
);
1585 searching
.casesense
= true;
1586 searching
.plus
= false;
1587 searching
.nolist
= false;
1588 searching
.in_progress
= 0;
1589 searching
.rep_nest
= NULL
;
1590 searching
.nparam_min
= 0;
1591 searching
.nparam_max
= INT_MAX
;
1592 tline
= expand_smacro(tline
->next
);
1595 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1597 "`%s' expects a parameter count or nothing",
1600 searching
.nparam_min
= searching
.nparam_max
=
1601 readnum(tline
->text
, &j
);
1604 "unable to parse parameter count `%s'",
1607 if (tline
&& tok_is_(tline
->next
, "-")) {
1608 tline
= tline
->next
->next
;
1609 if (tok_is_(tline
, "*"))
1610 searching
.nparam_max
= INT_MAX
;
1611 else if (!tok_type_(tline
, TOK_NUMBER
))
1613 "`%s' expects a parameter count after `-'",
1616 searching
.nparam_max
= readnum(tline
->text
, &j
);
1619 "unable to parse parameter count `%s'",
1621 if (searching
.nparam_min
> searching
.nparam_max
)
1623 "minimum parameter count exceeds maximum");
1626 if (tline
&& tok_is_(tline
->next
, "+")) {
1627 tline
= tline
->next
;
1628 searching
.plus
= true;
1630 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1632 if (!strcmp(mmac
->name
, searching
.name
) &&
1633 (mmac
->nparam_min
<= searching
.nparam_max
1635 && (searching
.nparam_min
<= mmac
->nparam_max
1642 nasm_free(searching
.name
);
1651 needtype
= TOK_NUMBER
;
1654 needtype
= TOK_STRING
;
1658 t
= tline
= expand_smacro(tline
);
1660 while (tok_type_(t
, TOK_WHITESPACE
) ||
1661 (needtype
== TOK_NUMBER
&&
1662 tok_type_(t
, TOK_OTHER
) &&
1663 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1667 j
= tok_type_(t
, needtype
);
1671 t
= tline
= expand_smacro(tline
);
1672 while (tok_type_(t
, TOK_WHITESPACE
))
1677 t
= t
->next
; /* Skip the actual token */
1678 while (tok_type_(t
, TOK_WHITESPACE
))
1680 j
= !t
; /* Should be nothing left */
1685 t
= tline
= expand_smacro(tline
);
1686 while (tok_type_(t
, TOK_WHITESPACE
))
1689 j
= !t
; /* Should be empty */
1693 t
= tline
= expand_smacro(tline
);
1695 tokval
.t_type
= TOKEN_INVALID
;
1696 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1697 NULL
, pass
| CRITICAL
, error
, NULL
);
1702 "trailing garbage after expression ignored");
1703 if (!is_simple(evalresult
)) {
1705 "non-constant value given to `%s'", pp_directives
[ct
]);
1708 j
= reloc_value(evalresult
) != 0;
1713 "preprocessor directive `%s' not yet implemented",
1718 free_tlist(origline
);
1719 return j
^ PP_NEGATIVE(ct
);
1722 free_tlist(origline
);
1727 * Common code for defining an smacro
1729 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1730 int nparam
, Token
*expansion
)
1732 SMacro
*smac
, **smhead
;
1733 struct hash_table
*smtbl
;
1735 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1738 "single-line macro `%s' defined both with and"
1739 " without parameters", mname
);
1741 /* Some instances of the old code considered this a failure,
1742 some others didn't. What is the right thing to do here? */
1743 free_tlist(expansion
);
1744 return false; /* Failure */
1747 * We're redefining, so we have to take over an
1748 * existing SMacro structure. This means freeing
1749 * what was already in it.
1751 nasm_free(smac
->name
);
1752 free_tlist(smac
->expansion
);
1755 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1756 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1757 smac
= nasm_malloc(sizeof(SMacro
));
1758 smac
->next
= *smhead
;
1761 smac
->name
= nasm_strdup(mname
);
1762 smac
->casesense
= casesense
;
1763 smac
->nparam
= nparam
;
1764 smac
->expansion
= expansion
;
1765 smac
->in_progress
= false;
1766 return true; /* Success */
1770 * Undefine an smacro
1772 static void undef_smacro(Context
*ctx
, const char *mname
)
1774 SMacro
**smhead
, *s
, **sp
;
1775 struct hash_table
*smtbl
;
1777 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1778 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1782 * We now have a macro name... go hunt for it.
1785 while ((s
= *sp
) != NULL
) {
1786 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1789 free_tlist(s
->expansion
);
1799 * Decode a size directive
1801 static int parse_size(const char *str
) {
1802 static const char *size_names
[] =
1803 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1804 static const int sizes
[] =
1805 { 0, 1, 4, 16, 8, 10, 2, 32 };
1807 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1811 * find and process preprocessor directive in passed line
1812 * Find out if a line contains a preprocessor directive, and deal
1815 * If a directive _is_ found, it is the responsibility of this routine
1816 * (and not the caller) to free_tlist() the line.
1818 * @param tline a pointer to the current tokeninzed line linked list
1819 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1822 static int do_directive(Token
* tline
)
1824 enum preproc_token i
;
1836 MMacro
*mmac
, **mmhead
;
1837 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1839 struct tokenval tokval
;
1841 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1847 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1848 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1849 || tline
->text
[1] == '!'))
1850 return NO_DIRECTIVE_FOUND
;
1852 i
= pp_token_hash(tline
->text
);
1855 * If we're in a non-emitting branch of a condition construct,
1856 * or walking to the end of an already terminated %rep block,
1857 * we should ignore all directives except for condition
1860 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1861 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1862 return NO_DIRECTIVE_FOUND
;
1866 * If we're defining a macro or reading a %rep block, we should
1867 * ignore all directives except for %macro/%imacro (which
1868 * generate an error), %endm/%endmacro, and (only if we're in a
1869 * %rep block) %endrep. If we're in a %rep block, another %rep
1870 * causes an error, so should be let through.
1872 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1873 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1874 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1875 return NO_DIRECTIVE_FOUND
;
1880 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1882 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1885 /* Directive to tell NASM what the default stack size is. The
1886 * default is for a 16-bit stack, and this can be overriden with
1888 * the following form:
1890 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1892 tline
= tline
->next
;
1893 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1894 tline
= tline
->next
;
1895 if (!tline
|| tline
->type
!= TOK_ID
) {
1896 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1897 free_tlist(origline
);
1898 return DIRECTIVE_FOUND
;
1900 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1901 /* All subsequent ARG directives are for a 32-bit stack */
1903 StackPointer
= "ebp";
1906 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1907 /* All subsequent ARG directives are for a 64-bit stack */
1909 StackPointer
= "rbp";
1912 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1913 /* All subsequent ARG directives are for a 16-bit stack,
1914 * far function call.
1917 StackPointer
= "bp";
1920 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1921 /* All subsequent ARG directives are for a 16-bit stack,
1922 * far function call. We don't support near functions.
1925 StackPointer
= "bp";
1929 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1930 free_tlist(origline
);
1931 return DIRECTIVE_FOUND
;
1933 free_tlist(origline
);
1934 return DIRECTIVE_FOUND
;
1937 /* TASM like ARG directive to define arguments to functions, in
1938 * the following form:
1940 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1944 char *arg
, directive
[256];
1945 int size
= StackSize
;
1947 /* Find the argument name */
1948 tline
= tline
->next
;
1949 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1950 tline
= tline
->next
;
1951 if (!tline
|| tline
->type
!= TOK_ID
) {
1952 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1953 free_tlist(origline
);
1954 return DIRECTIVE_FOUND
;
1958 /* Find the argument size type */
1959 tline
= tline
->next
;
1960 if (!tline
|| tline
->type
!= TOK_OTHER
1961 || tline
->text
[0] != ':') {
1963 "Syntax error processing `%%arg' directive");
1964 free_tlist(origline
);
1965 return DIRECTIVE_FOUND
;
1967 tline
= tline
->next
;
1968 if (!tline
|| tline
->type
!= TOK_ID
) {
1969 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1970 free_tlist(origline
);
1971 return DIRECTIVE_FOUND
;
1974 /* Allow macro expansion of type parameter */
1975 tt
= tokenize(tline
->text
);
1976 tt
= expand_smacro(tt
);
1977 size
= parse_size(tt
->text
);
1980 "Invalid size type for `%%arg' missing directive");
1982 free_tlist(origline
);
1983 return DIRECTIVE_FOUND
;
1987 /* Round up to even stack slots */
1988 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1990 /* Now define the macro for the argument */
1991 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1992 arg
, StackPointer
, offset
);
1993 do_directive(tokenize(directive
));
1996 /* Move to the next argument in the list */
1997 tline
= tline
->next
;
1998 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1999 tline
= tline
->next
;
2000 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2002 free_tlist(origline
);
2003 return DIRECTIVE_FOUND
;
2006 /* TASM like LOCAL directive to define local variables for a
2007 * function, in the following form:
2009 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2011 * The '= LocalSize' at the end is ignored by NASM, but is
2012 * required by TASM to define the local parameter size (and used
2013 * by the TASM macro package).
2015 offset
= LocalOffset
;
2017 char *local
, directive
[256];
2018 int size
= StackSize
;
2020 /* Find the argument name */
2021 tline
= tline
->next
;
2022 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2023 tline
= tline
->next
;
2024 if (!tline
|| tline
->type
!= TOK_ID
) {
2026 "`%%local' missing argument parameter");
2027 free_tlist(origline
);
2028 return DIRECTIVE_FOUND
;
2030 local
= tline
->text
;
2032 /* Find the argument size type */
2033 tline
= tline
->next
;
2034 if (!tline
|| tline
->type
!= TOK_OTHER
2035 || tline
->text
[0] != ':') {
2037 "Syntax error processing `%%local' directive");
2038 free_tlist(origline
);
2039 return DIRECTIVE_FOUND
;
2041 tline
= tline
->next
;
2042 if (!tline
|| tline
->type
!= TOK_ID
) {
2044 "`%%local' missing size type parameter");
2045 free_tlist(origline
);
2046 return DIRECTIVE_FOUND
;
2049 /* Allow macro expansion of type parameter */
2050 tt
= tokenize(tline
->text
);
2051 tt
= expand_smacro(tt
);
2052 size
= parse_size(tt
->text
);
2055 "Invalid size type for `%%local' missing directive");
2057 free_tlist(origline
);
2058 return DIRECTIVE_FOUND
;
2062 /* Round up to even stack slots */
2063 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2065 offset
+= size
; /* Negative offset, increment before */
2067 /* Now define the macro for the argument */
2068 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2069 local
, StackPointer
, offset
);
2070 do_directive(tokenize(directive
));
2072 /* Now define the assign to setup the enter_c macro correctly */
2073 snprintf(directive
, sizeof(directive
),
2074 "%%assign %%$localsize %%$localsize+%d", size
);
2075 do_directive(tokenize(directive
));
2077 /* Move to the next argument in the list */
2078 tline
= tline
->next
;
2079 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2080 tline
= tline
->next
;
2081 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2082 LocalOffset
= offset
;
2083 free_tlist(origline
);
2084 return DIRECTIVE_FOUND
;
2088 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2091 free_tlist(origline
);
2092 return DIRECTIVE_FOUND
;
2095 t
= tline
->next
= expand_smacro(tline
->next
);
2097 if (!t
|| (t
->type
!= TOK_STRING
&&
2098 t
->type
!= TOK_INTERNAL_STRING
)) {
2099 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2100 free_tlist(origline
);
2101 return DIRECTIVE_FOUND
; /* but we did _something_ */
2105 "trailing garbage after `%%depend' ignored");
2107 if (t
->type
!= TOK_INTERNAL_STRING
)
2108 nasm_unquote(p
, NULL
);
2109 if (dephead
&& !in_list(*dephead
, p
)) {
2110 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2114 deptail
= &sl
->next
;
2116 free_tlist(origline
);
2117 return DIRECTIVE_FOUND
;
2120 t
= tline
->next
= expand_smacro(tline
->next
);
2123 if (!t
|| (t
->type
!= TOK_STRING
&&
2124 t
->type
!= TOK_INTERNAL_STRING
)) {
2125 error(ERR_NONFATAL
, "`%%include' expects a file name");
2126 free_tlist(origline
);
2127 return DIRECTIVE_FOUND
; /* but we did _something_ */
2131 "trailing garbage after `%%include' ignored");
2133 if (t
->type
!= TOK_INTERNAL_STRING
)
2134 nasm_unquote(p
, NULL
);
2135 inc
= nasm_malloc(sizeof(Include
));
2138 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2140 /* -MG given but file not found */
2143 inc
->fname
= src_set_fname(nasm_strdup(p
));
2144 inc
->lineno
= src_set_linnum(0);
2146 inc
->expansion
= NULL
;
2149 list
->uplevel(LIST_INCLUDE
);
2151 free_tlist(origline
);
2152 return DIRECTIVE_FOUND
;
2156 static const macros_t
*use_pkg
;
2157 const char *pkg_macro
;
2159 t
= tline
->next
= expand_smacro(tline
->next
);
2162 if (!t
|| (t
->type
!= TOK_STRING
&&
2163 t
->type
!= TOK_INTERNAL_STRING
&&
2164 t
->type
!= TOK_ID
)) {
2165 error(ERR_NONFATAL
, "`%%use' expects a package name");
2166 free_tlist(origline
);
2167 return DIRECTIVE_FOUND
; /* but we did _something_ */
2171 "trailing garbage after `%%use' ignored");
2172 if (t
->type
== TOK_STRING
)
2173 nasm_unquote(t
->text
, NULL
);
2174 use_pkg
= nasm_stdmac_find_package(t
->text
);
2176 error(ERR_NONFATAL
, "unknown `%%use' package: %s", t
->text
);
2177 /* The first string will be <%define>__USE_*__ */
2178 pkg_macro
= (char *)use_pkg
+ 1;
2179 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2180 /* Not already included, go ahead and include it */
2181 stdmacpos
= use_pkg
;
2183 free_tlist(origline
);
2184 return DIRECTIVE_FOUND
;
2187 tline
= tline
->next
;
2189 tline
= expand_id(tline
);
2190 if (!tok_type_(tline
, TOK_ID
)) {
2191 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2192 free_tlist(origline
);
2193 return DIRECTIVE_FOUND
; /* but we did _something_ */
2196 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2197 ctx
= nasm_malloc(sizeof(Context
));
2199 hash_init(&ctx
->localmac
, HASH_SMALL
);
2200 ctx
->name
= nasm_strdup(tline
->text
);
2201 ctx
->number
= unique
++;
2203 free_tlist(origline
);
2207 tline
= tline
->next
;
2209 tline
= expand_id(tline
);
2210 if (!tok_type_(tline
, TOK_ID
)) {
2211 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2212 free_tlist(origline
);
2213 return DIRECTIVE_FOUND
; /* but we did _something_ */
2216 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2218 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2220 nasm_free(cstk
->name
);
2221 cstk
->name
= nasm_strdup(tline
->text
);
2223 free_tlist(origline
);
2228 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2230 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2233 free_tlist(origline
);
2239 int severity
= PP_ERROR
? ERR_NONFATAL
|ERR_NO_SEVERITY
:
2240 ERR_WARNING
|ERR_NO_SEVERITY
;
2242 tline
->next
= expand_smacro(tline
->next
);
2243 tline
= tline
->next
;
2245 t
= tline
? tline
->next
: NULL
;
2247 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2248 /* The line contains only a quoted string */
2250 nasm_unquote(p
, NULL
);
2251 error(severity
, "%s: %s", pp_directives
[i
], p
);
2253 /* Not a quoted string, or more than a quoted string */
2254 p
= detoken(tline
, false);
2255 error(severity
, "%s: %s", pp_directives
[i
], p
);
2258 free_tlist(origline
);
2263 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2266 j
= if_condition(tline
->next
, i
);
2267 tline
->next
= NULL
; /* it got freed */
2268 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2270 cond
= nasm_malloc(sizeof(Cond
));
2271 cond
->next
= istk
->conds
;
2274 free_tlist(origline
);
2275 return DIRECTIVE_FOUND
;
2279 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2280 if (emitting(istk
->conds
->state
)
2281 || istk
->conds
->state
== COND_NEVER
)
2282 istk
->conds
->state
= COND_NEVER
;
2285 * IMPORTANT: In the case of %if, we will already have
2286 * called expand_mmac_params(); however, if we're
2287 * processing an %elif we must have been in a
2288 * non-emitting mode, which would have inhibited
2289 * the normal invocation of expand_mmac_params(). Therefore,
2290 * we have to do it explicitly here.
2292 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2293 tline
->next
= NULL
; /* it got freed */
2294 istk
->conds
->state
=
2295 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2297 free_tlist(origline
);
2298 return DIRECTIVE_FOUND
;
2302 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2304 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2305 if (emitting(istk
->conds
->state
)
2306 || istk
->conds
->state
== COND_NEVER
)
2307 istk
->conds
->state
= COND_ELSE_FALSE
;
2309 istk
->conds
->state
= COND_ELSE_TRUE
;
2310 free_tlist(origline
);
2311 return DIRECTIVE_FOUND
;
2315 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2317 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2319 istk
->conds
= cond
->next
;
2321 free_tlist(origline
);
2322 return DIRECTIVE_FOUND
;
2328 "`%%%smacro': already defining a macro",
2329 (i
== PP_IMACRO
? "i" : ""));
2330 tline
= tline
->next
;
2332 tline
= expand_id(tline
);
2333 if (!tok_type_(tline
, TOK_ID
)) {
2335 "`%%%smacro' expects a macro name",
2336 (i
== PP_IMACRO
? "i" : ""));
2337 return DIRECTIVE_FOUND
;
2339 defining
= nasm_malloc(sizeof(MMacro
));
2340 defining
->name
= nasm_strdup(tline
->text
);
2341 defining
->casesense
= (i
== PP_MACRO
);
2342 defining
->plus
= false;
2343 defining
->nolist
= false;
2344 defining
->in_progress
= 0;
2345 defining
->rep_nest
= NULL
;
2346 tline
= expand_smacro(tline
->next
);
2348 if (!tok_type_(tline
, TOK_NUMBER
)) {
2350 "`%%%smacro' expects a parameter count",
2351 (i
== PP_IMACRO
? "i" : ""));
2352 defining
->nparam_min
= defining
->nparam_max
= 0;
2354 defining
->nparam_min
= defining
->nparam_max
=
2355 readnum(tline
->text
, &err
);
2358 "unable to parse parameter count `%s'", tline
->text
);
2360 if (tline
&& tok_is_(tline
->next
, "-")) {
2361 tline
= tline
->next
->next
;
2362 if (tok_is_(tline
, "*"))
2363 defining
->nparam_max
= INT_MAX
;
2364 else if (!tok_type_(tline
, TOK_NUMBER
))
2366 "`%%%smacro' expects a parameter count after `-'",
2367 (i
== PP_IMACRO
? "i" : ""));
2369 defining
->nparam_max
= readnum(tline
->text
, &err
);
2372 "unable to parse parameter count `%s'",
2374 if (defining
->nparam_min
> defining
->nparam_max
)
2376 "minimum parameter count exceeds maximum");
2379 if (tline
&& tok_is_(tline
->next
, "+")) {
2380 tline
= tline
->next
;
2381 defining
->plus
= true;
2383 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2384 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2385 tline
= tline
->next
;
2386 defining
->nolist
= true;
2388 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2390 if (!strcmp(mmac
->name
, defining
->name
) &&
2391 (mmac
->nparam_min
<= defining
->nparam_max
2393 && (defining
->nparam_min
<= mmac
->nparam_max
2396 "redefining multi-line macro `%s'", defining
->name
);
2402 * Handle default parameters.
2404 if (tline
&& tline
->next
) {
2405 defining
->dlist
= tline
->next
;
2407 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2408 &defining
->defaults
);
2410 defining
->dlist
= NULL
;
2411 defining
->defaults
= NULL
;
2413 defining
->expansion
= NULL
;
2414 free_tlist(origline
);
2415 return DIRECTIVE_FOUND
;
2420 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2421 return DIRECTIVE_FOUND
;
2423 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2424 defining
->next
= *mmhead
;
2427 free_tlist(origline
);
2428 return DIRECTIVE_FOUND
;
2431 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2432 tline
= tline
->next
;
2433 if (tline
->next
== NULL
) {
2434 free_tlist(origline
);
2435 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2436 return DIRECTIVE_FOUND
;
2438 t
= expand_smacro(tline
->next
);
2440 free_tlist(origline
);
2443 tokval
.t_type
= TOKEN_INVALID
;
2445 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2448 return DIRECTIVE_FOUND
;
2451 "trailing garbage after expression ignored");
2452 if (!is_simple(evalresult
)) {
2453 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2454 return DIRECTIVE_FOUND
;
2457 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2458 mmac
= mmac
->next_active
;
2460 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2461 } else if (mmac
->nparam
== 0) {
2463 "`%%rotate' invoked within macro without parameters");
2465 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2467 rotate
%= (int)mmac
->nparam
;
2469 rotate
+= mmac
->nparam
;
2471 mmac
->rotate
= rotate
;
2473 return DIRECTIVE_FOUND
;
2478 tline
= tline
->next
;
2479 } while (tok_type_(tline
, TOK_WHITESPACE
));
2481 if (tok_type_(tline
, TOK_ID
) &&
2482 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2485 tline
= tline
->next
;
2486 } while (tok_type_(tline
, TOK_WHITESPACE
));
2490 t
= expand_smacro(tline
);
2492 tokval
.t_type
= TOKEN_INVALID
;
2494 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2496 free_tlist(origline
);
2497 return DIRECTIVE_FOUND
;
2501 "trailing garbage after expression ignored");
2502 if (!is_simple(evalresult
)) {
2503 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2504 return DIRECTIVE_FOUND
;
2506 count
= reloc_value(evalresult
) + 1;
2508 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2511 free_tlist(origline
);
2513 tmp_defining
= defining
;
2514 defining
= nasm_malloc(sizeof(MMacro
));
2515 defining
->name
= NULL
; /* flags this macro as a %rep block */
2516 defining
->casesense
= false;
2517 defining
->plus
= false;
2518 defining
->nolist
= nolist
;
2519 defining
->in_progress
= count
;
2520 defining
->nparam_min
= defining
->nparam_max
= 0;
2521 defining
->defaults
= NULL
;
2522 defining
->dlist
= NULL
;
2523 defining
->expansion
= NULL
;
2524 defining
->next_active
= istk
->mstk
;
2525 defining
->rep_nest
= tmp_defining
;
2526 return DIRECTIVE_FOUND
;
2529 if (!defining
|| defining
->name
) {
2530 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2531 return DIRECTIVE_FOUND
;
2535 * Now we have a "macro" defined - although it has no name
2536 * and we won't be entering it in the hash tables - we must
2537 * push a macro-end marker for it on to istk->expansion.
2538 * After that, it will take care of propagating itself (a
2539 * macro-end marker line for a macro which is really a %rep
2540 * block will cause the macro to be re-expanded, complete
2541 * with another macro-end marker to ensure the process
2542 * continues) until the whole expansion is forcibly removed
2543 * from istk->expansion by a %exitrep.
2545 l
= nasm_malloc(sizeof(Line
));
2546 l
->next
= istk
->expansion
;
2547 l
->finishes
= defining
;
2549 istk
->expansion
= l
;
2551 istk
->mstk
= defining
;
2553 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2554 tmp_defining
= defining
;
2555 defining
= defining
->rep_nest
;
2556 free_tlist(origline
);
2557 return DIRECTIVE_FOUND
;
2561 * We must search along istk->expansion until we hit a
2562 * macro-end marker for a macro with no name. Then we set
2563 * its `in_progress' flag to 0.
2565 for (l
= istk
->expansion
; l
; l
= l
->next
)
2566 if (l
->finishes
&& !l
->finishes
->name
)
2570 l
->finishes
->in_progress
= 0;
2572 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2573 free_tlist(origline
);
2574 return DIRECTIVE_FOUND
;
2580 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2582 tline
= tline
->next
;
2584 tline
= expand_id(tline
);
2585 if (!tline
|| (tline
->type
!= TOK_ID
&&
2586 (tline
->type
!= TOK_PREPROC_ID
||
2587 tline
->text
[1] != '$'))) {
2588 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2590 free_tlist(origline
);
2591 return DIRECTIVE_FOUND
;
2594 ctx
= get_ctx(tline
->text
, false);
2596 mname
= tline
->text
;
2598 param_start
= tline
= tline
->next
;
2601 /* Expand the macro definition now for %xdefine and %ixdefine */
2602 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2603 tline
= expand_smacro(tline
);
2605 if (tok_is_(tline
, "(")) {
2607 * This macro has parameters.
2610 tline
= tline
->next
;
2614 error(ERR_NONFATAL
, "parameter identifier expected");
2615 free_tlist(origline
);
2616 return DIRECTIVE_FOUND
;
2618 if (tline
->type
!= TOK_ID
) {
2620 "`%s': parameter identifier expected",
2622 free_tlist(origline
);
2623 return DIRECTIVE_FOUND
;
2625 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2626 tline
= tline
->next
;
2628 if (tok_is_(tline
, ",")) {
2629 tline
= tline
->next
;
2632 if (!tok_is_(tline
, ")")) {
2634 "`)' expected to terminate macro template");
2635 free_tlist(origline
);
2636 return DIRECTIVE_FOUND
;
2641 tline
= tline
->next
;
2643 if (tok_type_(tline
, TOK_WHITESPACE
))
2644 last
= tline
, tline
= tline
->next
;
2649 if (t
->type
== TOK_ID
) {
2650 for (tt
= param_start
; tt
; tt
= tt
->next
)
2651 if (tt
->type
>= TOK_SMAC_PARAM
&&
2652 !strcmp(tt
->text
, t
->text
))
2656 t
->next
= macro_start
;
2661 * Good. We now have a macro name, a parameter count, and a
2662 * token list (in reverse order) for an expansion. We ought
2663 * to be OK just to create an SMacro, store it, and let
2664 * free_tlist have the rest of the line (which we have
2665 * carefully re-terminated after chopping off the expansion
2668 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2669 free_tlist(origline
);
2670 return DIRECTIVE_FOUND
;
2673 tline
= tline
->next
;
2675 tline
= expand_id(tline
);
2676 if (!tline
|| (tline
->type
!= TOK_ID
&&
2677 (tline
->type
!= TOK_PREPROC_ID
||
2678 tline
->text
[1] != '$'))) {
2679 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2680 free_tlist(origline
);
2681 return DIRECTIVE_FOUND
;
2685 "trailing garbage after macro name ignored");
2688 /* Find the context that symbol belongs to */
2689 ctx
= get_ctx(tline
->text
, false);
2690 undef_smacro(ctx
, tline
->text
);
2691 free_tlist(origline
);
2692 return DIRECTIVE_FOUND
;
2696 casesense
= (i
== PP_DEFSTR
);
2698 tline
= tline
->next
;
2700 tline
= expand_id(tline
);
2701 if (!tline
|| (tline
->type
!= TOK_ID
&&
2702 (tline
->type
!= TOK_PREPROC_ID
||
2703 tline
->text
[1] != '$'))) {
2704 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2706 free_tlist(origline
);
2707 return DIRECTIVE_FOUND
;
2710 ctx
= get_ctx(tline
->text
, false);
2712 mname
= tline
->text
;
2714 tline
= expand_smacro(tline
->next
);
2717 while (tok_type_(tline
, TOK_WHITESPACE
))
2718 tline
= delete_Token(tline
);
2720 p
= detoken(tline
, false);
2721 macro_start
= nasm_malloc(sizeof(*macro_start
));
2722 macro_start
->next
= NULL
;
2723 macro_start
->text
= nasm_quote(p
, strlen(p
));
2724 macro_start
->type
= TOK_STRING
;
2725 macro_start
->mac
= NULL
;
2729 * We now have a macro name, an implicit parameter count of
2730 * zero, and a string token to use as an expansion. Create
2731 * and store an SMacro.
2733 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2734 free_tlist(origline
);
2735 return DIRECTIVE_FOUND
;
2740 StrList
*xsl
= NULL
;
2741 StrList
**xst
= &xsl
;
2745 tline
= tline
->next
;
2747 tline
= expand_id(tline
);
2748 if (!tline
|| (tline
->type
!= TOK_ID
&&
2749 (tline
->type
!= TOK_PREPROC_ID
||
2750 tline
->text
[1] != '$'))) {
2752 "`%%pathsearch' expects a macro identifier as first parameter");
2753 free_tlist(origline
);
2754 return DIRECTIVE_FOUND
;
2756 ctx
= get_ctx(tline
->text
, false);
2758 mname
= tline
->text
;
2760 tline
= expand_smacro(tline
->next
);
2764 while (tok_type_(t
, TOK_WHITESPACE
))
2767 if (!t
|| (t
->type
!= TOK_STRING
&&
2768 t
->type
!= TOK_INTERNAL_STRING
)) {
2769 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2771 free_tlist(origline
);
2772 return DIRECTIVE_FOUND
; /* but we did _something_ */
2776 "trailing garbage after `%%pathsearch' ignored");
2778 if (t
->type
!= TOK_INTERNAL_STRING
)
2779 nasm_unquote(p
, NULL
);
2781 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2784 fclose(fp
); /* Don't actually care about the file */
2786 macro_start
= nasm_malloc(sizeof(*macro_start
));
2787 macro_start
->next
= NULL
;
2788 macro_start
->text
= nasm_quote(p
, strlen(p
));
2789 macro_start
->type
= TOK_STRING
;
2790 macro_start
->mac
= NULL
;
2795 * We now have a macro name, an implicit parameter count of
2796 * zero, and a string token to use as an expansion. Create
2797 * and store an SMacro.
2799 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2801 free_tlist(origline
);
2802 return DIRECTIVE_FOUND
;
2808 tline
= tline
->next
;
2810 tline
= expand_id(tline
);
2811 if (!tline
|| (tline
->type
!= TOK_ID
&&
2812 (tline
->type
!= TOK_PREPROC_ID
||
2813 tline
->text
[1] != '$'))) {
2815 "`%%strlen' expects a macro identifier as first parameter");
2816 free_tlist(origline
);
2817 return DIRECTIVE_FOUND
;
2819 ctx
= get_ctx(tline
->text
, false);
2821 mname
= tline
->text
;
2823 tline
= expand_smacro(tline
->next
);
2827 while (tok_type_(t
, TOK_WHITESPACE
))
2829 /* t should now point to the string */
2830 if (t
->type
!= TOK_STRING
) {
2832 "`%%strlen` requires string as second parameter");
2834 free_tlist(origline
);
2835 return DIRECTIVE_FOUND
;
2838 macro_start
= nasm_malloc(sizeof(*macro_start
));
2839 macro_start
->next
= NULL
;
2840 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2841 macro_start
->mac
= NULL
;
2844 * We now have a macro name, an implicit parameter count of
2845 * zero, and a numeric token to use as an expansion. Create
2846 * and store an SMacro.
2848 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2850 free_tlist(origline
);
2851 return DIRECTIVE_FOUND
;
2860 tline
= tline
->next
;
2862 tline
= expand_id(tline
);
2863 if (!tline
|| (tline
->type
!= TOK_ID
&&
2864 (tline
->type
!= TOK_PREPROC_ID
||
2865 tline
->text
[1] != '$'))) {
2867 "`%%substr' expects a macro identifier as first parameter");
2868 free_tlist(origline
);
2869 return DIRECTIVE_FOUND
;
2871 ctx
= get_ctx(tline
->text
, false);
2873 mname
= tline
->text
;
2875 tline
= expand_smacro(tline
->next
);
2879 while (tok_type_(t
, TOK_WHITESPACE
))
2882 /* t should now point to the string */
2883 if (t
->type
!= TOK_STRING
) {
2885 "`%%substr` requires string as second parameter");
2887 free_tlist(origline
);
2888 return DIRECTIVE_FOUND
;
2893 tokval
.t_type
= TOKEN_INVALID
;
2894 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2898 free_tlist(origline
);
2899 return DIRECTIVE_FOUND
;
2900 } else if (!is_simple(evalresult
)) {
2901 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2903 free_tlist(origline
);
2904 return DIRECTIVE_FOUND
;
2906 a1
= evalresult
->value
-1;
2908 while (tok_type_(tt
, TOK_WHITESPACE
))
2911 a2
= 1; /* Backwards compatibility: one character */
2913 tokval
.t_type
= TOKEN_INVALID
;
2914 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2918 free_tlist(origline
);
2919 return DIRECTIVE_FOUND
;
2920 } else if (!is_simple(evalresult
)) {
2921 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2923 free_tlist(origline
);
2924 return DIRECTIVE_FOUND
;
2926 a2
= evalresult
->value
;
2929 len
= nasm_unquote(t
->text
, NULL
);
2932 if (a1
+a2
> (int64_t)len
)
2935 macro_start
= nasm_malloc(sizeof(*macro_start
));
2936 macro_start
->next
= NULL
;
2937 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
2938 macro_start
->type
= TOK_STRING
;
2939 macro_start
->mac
= NULL
;
2942 * We now have a macro name, an implicit parameter count of
2943 * zero, and a numeric token to use as an expansion. Create
2944 * and store an SMacro.
2946 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2948 free_tlist(origline
);
2949 return DIRECTIVE_FOUND
;
2954 casesense
= (i
== PP_ASSIGN
);
2956 tline
= tline
->next
;
2958 tline
= expand_id(tline
);
2959 if (!tline
|| (tline
->type
!= TOK_ID
&&
2960 (tline
->type
!= TOK_PREPROC_ID
||
2961 tline
->text
[1] != '$'))) {
2963 "`%%%sassign' expects a macro identifier",
2964 (i
== PP_IASSIGN
? "i" : ""));
2965 free_tlist(origline
);
2966 return DIRECTIVE_FOUND
;
2968 ctx
= get_ctx(tline
->text
, false);
2970 mname
= tline
->text
;
2972 tline
= expand_smacro(tline
->next
);
2977 tokval
.t_type
= TOKEN_INVALID
;
2979 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2982 free_tlist(origline
);
2983 return DIRECTIVE_FOUND
;
2988 "trailing garbage after expression ignored");
2990 if (!is_simple(evalresult
)) {
2992 "non-constant value given to `%%%sassign'",
2993 (i
== PP_IASSIGN
? "i" : ""));
2994 free_tlist(origline
);
2995 return DIRECTIVE_FOUND
;
2998 macro_start
= nasm_malloc(sizeof(*macro_start
));
2999 macro_start
->next
= NULL
;
3000 make_tok_num(macro_start
, reloc_value(evalresult
));
3001 macro_start
->mac
= NULL
;
3004 * We now have a macro name, an implicit parameter count of
3005 * zero, and a numeric token to use as an expansion. Create
3006 * and store an SMacro.
3008 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3009 free_tlist(origline
);
3010 return DIRECTIVE_FOUND
;
3014 * Syntax is `%line nnn[+mmm] [filename]'
3016 tline
= tline
->next
;
3018 if (!tok_type_(tline
, TOK_NUMBER
)) {
3019 error(ERR_NONFATAL
, "`%%line' expects line number");
3020 free_tlist(origline
);
3021 return DIRECTIVE_FOUND
;
3023 k
= readnum(tline
->text
, &err
);
3025 tline
= tline
->next
;
3026 if (tok_is_(tline
, "+")) {
3027 tline
= tline
->next
;
3028 if (!tok_type_(tline
, TOK_NUMBER
)) {
3029 error(ERR_NONFATAL
, "`%%line' expects line increment");
3030 free_tlist(origline
);
3031 return DIRECTIVE_FOUND
;
3033 m
= readnum(tline
->text
, &err
);
3034 tline
= tline
->next
;
3040 nasm_free(src_set_fname(detoken(tline
, false)));
3042 free_tlist(origline
);
3043 return DIRECTIVE_FOUND
;
3047 "preprocessor directive `%s' not yet implemented",
3051 return DIRECTIVE_FOUND
;
3055 * Ensure that a macro parameter contains a condition code and
3056 * nothing else. Return the condition code index if so, or -1
3059 static int find_cc(Token
* t
)
3065 return -1; /* Probably a %+ without a space */
3068 if (t
->type
!= TOK_ID
)
3072 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3076 j
= elements(conditions
);
3079 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3095 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3096 * %-n) and MMacro-local identifiers (%%foo).
3098 static Token
*expand_mmac_params(Token
* tline
)
3100 Token
*t
, *tt
, **tail
, *thead
;
3106 if (tline
->type
== TOK_PREPROC_ID
&&
3107 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3108 && tline
->text
[2]) || tline
->text
[1] == '%'
3109 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3111 int type
= 0, cc
; /* type = 0 to placate optimisers */
3118 tline
= tline
->next
;
3121 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3122 mac
= mac
->next_active
;
3124 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3126 switch (t
->text
[1]) {
3128 * We have to make a substitution of one of the
3129 * forms %1, %-1, %+1, %%foo, %0.
3133 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3134 text
= nasm_strdup(tmpbuf
);
3138 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3140 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3143 n
= atoi(t
->text
+ 2) - 1;
3144 if (n
>= mac
->nparam
)
3147 if (mac
->nparam
> 1)
3148 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3149 tt
= mac
->params
[n
];
3154 "macro parameter %d is not a condition code",
3159 if (inverse_ccs
[cc
] == -1) {
3161 "condition code `%s' is not invertible",
3166 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3170 n
= atoi(t
->text
+ 2) - 1;
3171 if (n
>= mac
->nparam
)
3174 if (mac
->nparam
> 1)
3175 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3176 tt
= mac
->params
[n
];
3181 "macro parameter %d is not a condition code",
3186 text
= nasm_strdup(conditions
[cc
]);
3190 n
= atoi(t
->text
+ 1) - 1;
3191 if (n
>= mac
->nparam
)
3194 if (mac
->nparam
> 1)
3195 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3196 tt
= mac
->params
[n
];
3199 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3200 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3201 tail
= &(*tail
)->next
;
3205 text
= NULL
; /* we've done it here */
3221 tline
= tline
->next
;
3228 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3230 case TOK_WHITESPACE
:
3231 if (tt
->type
== TOK_WHITESPACE
) {
3232 t
->next
= delete_Token(tt
);
3236 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3237 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3240 t
->next
= delete_Token(tt
);
3244 if (tt
->type
== TOK_NUMBER
) {
3245 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3248 t
->next
= delete_Token(tt
);
3259 * Expand all single-line macro calls made in the given line.
3260 * Return the expanded version of the line. The original is deemed
3261 * to be destroyed in the process. (In reality we'll just move
3262 * Tokens from input to output a lot of the time, rather than
3263 * actually bothering to destroy and replicate.)
3265 #define DEADMAN_LIMIT (1 << 20)
3267 static Token
*expand_smacro(Token
* tline
)
3269 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3270 struct hash_table
*smtbl
;
3271 SMacro
*head
= NULL
, *m
;
3274 unsigned int nparam
, sparam
;
3275 int brackets
, rescan
;
3276 Token
*org_tline
= tline
;
3279 int deadman
= DEADMAN_LIMIT
;
3282 * Trick: we should avoid changing the start token pointer since it can
3283 * be contained in "next" field of other token. Because of this
3284 * we allocate a copy of first token and work with it; at the end of
3285 * routine we copy it back
3289 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3291 tline
->mac
= org_tline
->mac
;
3292 nasm_free(org_tline
->text
);
3293 org_tline
->text
= NULL
;
3300 while (tline
) { /* main token loop */
3302 error(ERR_NONFATAL
, "interminable macro recursion");
3306 if ((mname
= tline
->text
)) {
3307 /* if this token is a local macro, look in local context */
3310 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3311 ctx
= get_ctx(mname
, true);
3313 smtbl
= &ctx
->localmac
;
3315 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3318 * We've hit an identifier. As in is_mmacro below, we first
3319 * check whether the identifier is a single-line macro at
3320 * all, then think about checking for parameters if
3323 for (m
= head
; m
; m
= m
->next
)
3324 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3330 if (m
->nparam
== 0) {
3332 * Simple case: the macro is parameterless. Discard the
3333 * one token that the macro call took, and push the
3334 * expansion back on the to-do stack.
3336 if (!m
->expansion
) {
3337 if (!strcmp("__FILE__", m
->name
)) {
3340 src_get(&num
, &file
);
3341 tline
->text
= nasm_quote(file
, strlen(file
));
3342 tline
->type
= TOK_STRING
;
3346 if (!strcmp("__LINE__", m
->name
)) {
3347 nasm_free(tline
->text
);
3348 make_tok_num(tline
, src_get_linnum());
3351 if (!strcmp("__BITS__", m
->name
)) {
3352 nasm_free(tline
->text
);
3353 make_tok_num(tline
, globalbits
);
3356 tline
= delete_Token(tline
);
3361 * Complicated case: at least one macro with this name
3362 * exists and takes parameters. We must find the
3363 * parameters in the call, count them, find the SMacro
3364 * that corresponds to that form of the macro call, and
3365 * substitute for the parameters when we expand. What a
3368 /*tline = tline->next;
3369 skip_white_(tline); */
3372 while (tok_type_(t
, TOK_SMAC_END
)) {
3373 t
->mac
->in_progress
= false;
3375 t
= tline
->next
= delete_Token(t
);
3378 } while (tok_type_(tline
, TOK_WHITESPACE
));
3379 if (!tok_is_(tline
, "(")) {
3381 * This macro wasn't called with parameters: ignore
3382 * the call. (Behaviour borrowed from gnu cpp.)
3391 sparam
= PARAM_DELTA
;
3392 params
= nasm_malloc(sparam
* sizeof(Token
*));
3393 params
[0] = tline
->next
;
3394 paramsize
= nasm_malloc(sparam
* sizeof(int));
3396 while (true) { /* parameter loop */
3398 * For some unusual expansions
3399 * which concatenates function call
3402 while (tok_type_(t
, TOK_SMAC_END
)) {
3403 t
->mac
->in_progress
= false;
3405 t
= tline
->next
= delete_Token(t
);
3411 "macro call expects terminating `)'");
3414 if (tline
->type
== TOK_WHITESPACE
3416 if (paramsize
[nparam
])
3419 params
[nparam
] = tline
->next
;
3420 continue; /* parameter loop */
3422 if (tline
->type
== TOK_OTHER
3423 && tline
->text
[1] == 0) {
3424 char ch
= tline
->text
[0];
3425 if (ch
== ',' && !paren
&& brackets
<= 0) {
3426 if (++nparam
>= sparam
) {
3427 sparam
+= PARAM_DELTA
;
3428 params
= nasm_realloc(params
,
3433 nasm_realloc(paramsize
,
3437 params
[nparam
] = tline
->next
;
3438 paramsize
[nparam
] = 0;
3440 continue; /* parameter loop */
3443 (brackets
> 0 || (brackets
== 0 &&
3444 !paramsize
[nparam
])))
3446 if (!(brackets
++)) {
3447 params
[nparam
] = tline
->next
;
3448 continue; /* parameter loop */
3451 if (ch
== '}' && brackets
> 0)
3452 if (--brackets
== 0) {
3454 continue; /* parameter loop */
3456 if (ch
== '(' && !brackets
)
3458 if (ch
== ')' && brackets
<= 0)
3464 error(ERR_NONFATAL
, "braces do not "
3465 "enclose all of macro parameter");
3467 paramsize
[nparam
] += white
+ 1;
3469 } /* parameter loop */
3471 while (m
&& (m
->nparam
!= nparam
||
3472 mstrcmp(m
->name
, mname
,
3476 error(ERR_WARNING
| ERR_WARN_MNP
,
3477 "macro `%s' exists, "
3478 "but not taking %d parameters",
3479 mstart
->text
, nparam
);
3482 if (m
&& m
->in_progress
)
3484 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3486 * Design question: should we handle !tline, which
3487 * indicates missing ')' here, or expand those
3488 * macros anyway, which requires the (t) test a few
3492 nasm_free(paramsize
);
3496 * Expand the macro: we are placed on the last token of the
3497 * call, so that we can easily split the call from the
3498 * following tokens. We also start by pushing an SMAC_END
3499 * token for the cycle removal.
3506 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3508 m
->in_progress
= true;
3510 for (t
= m
->expansion
; t
; t
= t
->next
) {
3511 if (t
->type
>= TOK_SMAC_PARAM
) {
3512 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3516 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3517 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3520 new_Token(tline
, ttt
->type
, ttt
->text
,
3526 } else if (t
->type
== TOK_PREPROC_Q
) {
3527 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3529 } else if (t
->type
== TOK_PREPROC_QQ
) {
3530 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3533 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3539 * Having done that, get rid of the macro call, and clean
3540 * up the parameters.
3543 nasm_free(paramsize
);
3545 continue; /* main token loop */
3550 if (tline
->type
== TOK_SMAC_END
) {
3551 tline
->mac
->in_progress
= false;
3552 tline
= delete_Token(tline
);
3555 tline
= tline
->next
;
3563 * Now scan the entire line and look for successive TOK_IDs that resulted
3564 * after expansion (they can't be produced by tokenize()). The successive
3565 * TOK_IDs should be concatenated.
3566 * Also we look for %+ tokens and concatenate the tokens before and after
3567 * them (without white spaces in between).
3572 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3576 if (t
->next
->type
== TOK_ID
||
3577 t
->next
->type
== TOK_PREPROC_ID
||
3578 t
->next
->type
== TOK_NUMBER
) {
3579 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3581 t
->next
= delete_Token(t
->next
);
3584 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3585 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3586 strcmp(t
->next
->next
->text
, "%+") == 0) {
3587 /* free the next whitespace, the %+ token and next whitespace */
3589 for (i
= 1; i
<= 3; i
++) {
3591 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3593 t
->next
= delete_Token(t
->next
);
3598 /* If we concatenaded something, re-scan the line for macros */
3606 *org_tline
= *thead
;
3607 /* since we just gave text to org_line, don't free it */
3609 delete_Token(thead
);
3611 /* the expression expanded to empty line;
3612 we can't return NULL for some reasons
3613 we just set the line to a single WHITESPACE token. */
3614 memset(org_tline
, 0, sizeof(*org_tline
));
3615 org_tline
->text
= NULL
;
3616 org_tline
->type
= TOK_WHITESPACE
;
3625 * Similar to expand_smacro but used exclusively with macro identifiers
3626 * right before they are fetched in. The reason is that there can be
3627 * identifiers consisting of several subparts. We consider that if there
3628 * are more than one element forming the name, user wants a expansion,
3629 * otherwise it will be left as-is. Example:
3633 * the identifier %$abc will be left as-is so that the handler for %define
3634 * will suck it and define the corresponding value. Other case:
3636 * %define _%$abc cde
3638 * In this case user wants name to be expanded *before* %define starts
3639 * working, so we'll expand %$abc into something (if it has a value;
3640 * otherwise it will be left as-is) then concatenate all successive
3643 static Token
*expand_id(Token
* tline
)
3645 Token
*cur
, *oldnext
= NULL
;
3647 if (!tline
|| !tline
->next
)
3652 (cur
->next
->type
== TOK_ID
||
3653 cur
->next
->type
== TOK_PREPROC_ID
3654 || cur
->next
->type
== TOK_NUMBER
))
3657 /* If identifier consists of just one token, don't expand */
3662 oldnext
= cur
->next
; /* Detach the tail past identifier */
3663 cur
->next
= NULL
; /* so that expand_smacro stops here */
3666 tline
= expand_smacro(tline
);
3669 /* expand_smacro possibly changhed tline; re-scan for EOL */
3671 while (cur
&& cur
->next
)
3674 cur
->next
= oldnext
;
3681 * Determine whether the given line constitutes a multi-line macro
3682 * call, and return the MMacro structure called if so. Doesn't have
3683 * to check for an initial label - that's taken care of in
3684 * expand_mmacro - but must check numbers of parameters. Guaranteed
3685 * to be called with tline->type == TOK_ID, so the putative macro
3686 * name is easy to find.
3688 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3694 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3697 * Efficiency: first we see if any macro exists with the given
3698 * name. If not, we can return NULL immediately. _Then_ we
3699 * count the parameters, and then we look further along the
3700 * list if necessary to find the proper MMacro.
3702 for (m
= head
; m
; m
= m
->next
)
3703 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3709 * OK, we have a potential macro. Count and demarcate the
3712 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3715 * So we know how many parameters we've got. Find the MMacro
3716 * structure that handles this number.
3719 if (m
->nparam_min
<= nparam
3720 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3722 * This one is right. Just check if cycle removal
3723 * prohibits us using it before we actually celebrate...
3725 if (m
->in_progress
) {
3728 "self-reference in multi-line macro `%s'", m
->name
);
3734 * It's right, and we can use it. Add its default
3735 * parameters to the end of our list if necessary.
3737 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3739 nasm_realloc(params
,
3740 ((m
->nparam_min
+ m
->ndefs
+
3741 1) * sizeof(*params
)));
3742 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3743 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3748 * If we've gone over the maximum parameter count (and
3749 * we're in Plus mode), ignore parameters beyond
3752 if (m
->plus
&& nparam
> m
->nparam_max
)
3753 nparam
= m
->nparam_max
;
3755 * Then terminate the parameter list, and leave.
3757 if (!params
) { /* need this special case */
3758 params
= nasm_malloc(sizeof(*params
));
3761 params
[nparam
] = NULL
;
3762 *params_array
= params
;
3766 * This one wasn't right: look for the next one with the
3769 for (m
= m
->next
; m
; m
= m
->next
)
3770 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3775 * After all that, we didn't find one with the right number of
3776 * parameters. Issue a warning, and fail to expand the macro.
3778 error(ERR_WARNING
| ERR_WARN_MNP
,
3779 "macro `%s' exists, but not taking %d parameters",
3780 tline
->text
, nparam
);
3786 * Expand the multi-line macro call made by the given line, if
3787 * there is one to be expanded. If there is, push the expansion on
3788 * istk->expansion and return 1. Otherwise return 0.
3790 static int expand_mmacro(Token
* tline
)
3792 Token
*startline
= tline
;
3793 Token
*label
= NULL
;
3794 int dont_prepend
= 0;
3795 Token
**params
, *t
, *mtok
, *tt
;
3798 int i
, nparam
, *paramlen
;
3803 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3804 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3807 m
= is_mmacro(t
, ¶ms
);
3813 * We have an id which isn't a macro call. We'll assume
3814 * it might be a label; we'll also check to see if a
3815 * colon follows it. Then, if there's another id after
3816 * that lot, we'll check it again for macro-hood.
3820 if (tok_type_(t
, TOK_WHITESPACE
))
3821 last
= t
, t
= t
->next
;
3822 if (tok_is_(t
, ":")) {
3824 last
= t
, t
= t
->next
;
3825 if (tok_type_(t
, TOK_WHITESPACE
))
3826 last
= t
, t
= t
->next
;
3828 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3836 * Fix up the parameters: this involves stripping leading and
3837 * trailing whitespace, then stripping braces if they are
3840 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3841 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3843 for (i
= 0; params
[i
]; i
++) {
3845 int comma
= (!m
->plus
|| i
< nparam
- 1);
3849 if (tok_is_(t
, "{"))
3850 t
= t
->next
, brace
= true, comma
= false;
3854 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3855 break; /* ... because we have hit a comma */
3856 if (comma
&& t
->type
== TOK_WHITESPACE
3857 && tok_is_(t
->next
, ","))
3858 break; /* ... or a space then a comma */
3859 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3860 break; /* ... or a brace */
3867 * OK, we have a MMacro structure together with a set of
3868 * parameters. We must now go through the expansion and push
3869 * copies of each Line on to istk->expansion. Substitution of
3870 * parameter tokens and macro-local tokens doesn't get done
3871 * until the single-line macro substitution process; this is
3872 * because delaying them allows us to change the semantics
3873 * later through %rotate.
3875 * First, push an end marker on to istk->expansion, mark this
3876 * macro as in progress, and set up its invocation-specific
3879 ll
= nasm_malloc(sizeof(Line
));
3880 ll
->next
= istk
->expansion
;
3883 istk
->expansion
= ll
;
3885 m
->in_progress
= true;
3890 m
->paramlen
= paramlen
;
3891 m
->unique
= unique
++;
3894 m
->next_active
= istk
->mstk
;
3897 for (l
= m
->expansion
; l
; l
= l
->next
) {
3900 ll
= nasm_malloc(sizeof(Line
));
3901 ll
->finishes
= NULL
;
3902 ll
->next
= istk
->expansion
;
3903 istk
->expansion
= ll
;
3906 for (t
= l
->first
; t
; t
= t
->next
) {
3910 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
3912 case TOK_PREPROC_QQ
:
3913 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3915 case TOK_PREPROC_ID
:
3916 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3924 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3933 * If we had a label, push it on as the first line of
3934 * the macro expansion.
3937 if (dont_prepend
< 0)
3938 free_tlist(startline
);
3940 ll
= nasm_malloc(sizeof(Line
));
3941 ll
->finishes
= NULL
;
3942 ll
->next
= istk
->expansion
;
3943 istk
->expansion
= ll
;
3944 ll
->first
= startline
;
3945 if (!dont_prepend
) {
3947 label
= label
->next
;
3948 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3953 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3959 * Since preprocessor always operate only on the line that didn't
3960 * arrived yet, we should always use ERR_OFFBY1. Also since user
3961 * won't want to see same error twice (preprocessing is done once
3962 * per pass) we will want to show errors only during pass one.
3964 static void error(int severity
, const char *fmt
, ...)
3969 /* If we're in a dead branch of IF or something like it, ignore the error */
3970 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3974 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3977 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3978 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3979 istk
->mstk
->lineno
, buff
);
3981 _error(severity
| ERR_PASS1
, "%s", buff
);
3985 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3986 ListGen
* listgen
, StrList
**deplist
)
3990 istk
= nasm_malloc(sizeof(Include
));
3993 istk
->expansion
= NULL
;
3995 istk
->fp
= fopen(file
, "r");
3997 src_set_fname(nasm_strdup(file
));
4001 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
4006 if (tasm_compatible_mode
) {
4007 stdmacpos
= nasm_stdmac
;
4009 stdmacpos
= nasm_stdmac_after_tasm
;
4011 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4016 dephead
= deptail
= deplist
;
4018 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4020 strcpy(sl
->str
, file
);
4022 deptail
= &sl
->next
;
4026 static char *pp_getline(void)
4033 * Fetch a tokenized line, either from the macro-expansion
4034 * buffer or from the input file.
4037 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4038 Line
*l
= istk
->expansion
;
4039 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4043 * This is a macro-end marker for a macro with no
4044 * name, which means it's not really a macro at all
4045 * but a %rep block, and the `in_progress' field is
4046 * more than 1, meaning that we still need to
4047 * repeat. (1 means the natural last repetition; 0
4048 * means termination by %exitrep.) We have
4049 * therefore expanded up to the %endrep, and must
4050 * push the whole block on to the expansion buffer
4051 * again. We don't bother to remove the macro-end
4052 * marker: we'd only have to generate another one
4055 l
->finishes
->in_progress
--;
4056 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4057 Token
*t
, *tt
, **tail
;
4059 ll
= nasm_malloc(sizeof(Line
));
4060 ll
->next
= istk
->expansion
;
4061 ll
->finishes
= NULL
;
4065 for (t
= l
->first
; t
; t
= t
->next
) {
4066 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4068 new_Token(NULL
, t
->type
, t
->text
, 0);
4073 istk
->expansion
= ll
;
4077 * Check whether a `%rep' was started and not ended
4078 * within this macro expansion. This can happen and
4079 * should be detected. It's a fatal error because
4080 * I'm too confused to work out how to recover
4086 "defining with name in expansion");
4087 else if (istk
->mstk
->name
)
4089 "`%%rep' without `%%endrep' within"
4090 " expansion of macro `%s'",
4095 * FIXME: investigate the relationship at this point between
4096 * istk->mstk and l->finishes
4099 MMacro
*m
= istk
->mstk
;
4100 istk
->mstk
= m
->next_active
;
4103 * This was a real macro call, not a %rep, and
4104 * therefore the parameter information needs to
4107 nasm_free(m
->params
);
4108 free_tlist(m
->iline
);
4109 nasm_free(m
->paramlen
);
4110 l
->finishes
->in_progress
= false;
4114 istk
->expansion
= l
->next
;
4116 list
->downlevel(LIST_MACRO
);
4119 while (1) { /* until we get a line we can use */
4121 if (istk
->expansion
) { /* from a macro expansion */
4123 Line
*l
= istk
->expansion
;
4125 istk
->mstk
->lineno
++;
4127 istk
->expansion
= l
->next
;
4129 p
= detoken(tline
, false);
4130 list
->line(LIST_MACRO
, p
);
4135 if (line
) { /* from the current input file */
4136 line
= prepreproc(line
);
4137 tline
= tokenize(line
);
4142 * The current file has ended; work down the istk
4149 "expected `%%endif' before end of file");
4150 /* only set line and file name if there's a next node */
4152 src_set_linnum(i
->lineno
);
4153 nasm_free(src_set_fname(i
->fname
));
4156 list
->downlevel(LIST_INCLUDE
);
4164 * We must expand MMacro parameters and MMacro-local labels
4165 * _before_ we plunge into directive processing, to cope
4166 * with things like `%define something %1' such as STRUC
4167 * uses. Unless we're _defining_ a MMacro, in which case
4168 * those tokens should be left alone to go into the
4169 * definition; and unless we're in a non-emitting
4170 * condition, in which case we don't want to meddle with
4173 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4174 tline
= expand_mmac_params(tline
);
4177 * Check the line to see if it's a preprocessor directive.
4179 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4181 } else if (defining
) {
4183 * We're defining a multi-line macro. We emit nothing
4185 * shove the tokenized line on to the macro definition.
4187 Line
*l
= nasm_malloc(sizeof(Line
));
4188 l
->next
= defining
->expansion
;
4191 defining
->expansion
= l
;
4193 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4195 * We're in a non-emitting branch of a condition block.
4196 * Emit nothing at all, not even a blank line: when we
4197 * emerge from the condition we'll give a line-number
4198 * directive so we keep our place correctly.
4202 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4204 * We're in a %rep block which has been terminated, so
4205 * we're walking through to the %endrep without
4206 * emitting anything. Emit nothing at all, not even a
4207 * blank line: when we emerge from the %rep block we'll
4208 * give a line-number directive so we keep our place
4214 tline
= expand_smacro(tline
);
4215 if (!expand_mmacro(tline
)) {
4217 * De-tokenize the line again, and emit it.
4219 line
= detoken(tline
, true);
4223 continue; /* expand_mmacro calls free_tlist */
4231 static void pp_cleanup(int pass
)
4234 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4236 free_mmacro(defining
);
4245 nasm_free(i
->fname
);
4250 nasm_free(src_set_fname(NULL
));
4255 while ((i
= ipath
)) {
4264 void pp_include_path(char *path
)
4268 i
= nasm_malloc(sizeof(IncPath
));
4269 i
->path
= path
? nasm_strdup(path
) : NULL
;
4272 if (ipath
!= NULL
) {
4274 while (j
->next
!= NULL
)
4282 void pp_pre_include(char *fname
)
4284 Token
*inc
, *space
, *name
;
4287 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4288 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4289 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4291 l
= nasm_malloc(sizeof(Line
));
4298 void pp_pre_define(char *definition
)
4304 equals
= strchr(definition
, '=');
4305 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4306 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4309 space
->next
= tokenize(definition
);
4313 l
= nasm_malloc(sizeof(Line
));
4320 void pp_pre_undefine(char *definition
)
4325 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4326 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4327 space
->next
= tokenize(definition
);
4329 l
= nasm_malloc(sizeof(Line
));
4337 * Added by Keith Kanios:
4339 * This function is used to assist with "runtime" preprocessor
4340 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4342 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4343 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4346 void pp_runtime(char *definition
)
4350 def
= tokenize(definition
);
4351 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4356 void pp_extra_stdmac(const macros_t
*macros
)
4358 extrastdmac
= macros
;
4361 static void make_tok_num(Token
* tok
, int64_t val
)
4364 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4365 tok
->text
= nasm_strdup(numbuf
);
4366 tok
->type
= TOK_NUMBER
;